001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.vendor.server.transfer;
018
019import com.echothree.model.control.accounting.server.control.AccountingControl;
020import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl;
021import com.echothree.model.control.carrier.server.control.CarrierControl;
022import com.echothree.model.control.comment.common.CommentConstants;
023import com.echothree.model.control.communication.server.control.CommunicationControl;
024import com.echothree.model.control.contact.server.control.ContactControl;
025import com.echothree.model.control.contactlist.server.control.ContactListControl;
026import com.echothree.model.control.core.server.control.EntityInstanceControl;
027import com.echothree.model.control.document.server.control.DocumentControl;
028import com.echothree.model.control.invoice.server.control.InvoiceControl;
029import com.echothree.model.control.item.server.control.ItemControl;
030import com.echothree.model.control.party.common.PartyOptions;
031import com.echothree.model.control.party.server.control.PartyControl;
032import com.echothree.model.control.payment.server.control.BillingControl;
033import com.echothree.model.control.printer.server.control.PrinterControl;
034import com.echothree.model.control.returnpolicy.server.control.ReturnPolicyControl;
035import com.echothree.model.control.scale.server.control.ScaleControl;
036import com.echothree.model.control.shipment.server.control.PartyFreeOnBoardControl;
037import com.echothree.model.control.subscription.server.control.SubscriptionControl;
038import com.echothree.model.control.term.server.control.TermControl;
039import com.echothree.model.control.user.server.control.UserControl;
040import com.echothree.model.control.vendor.common.VendorOptions;
041import com.echothree.model.control.vendor.common.transfer.VendorTransfer;
042import com.echothree.model.control.vendor.common.workflow.VendorStatusConstants;
043import com.echothree.model.control.vendor.server.control.VendorControl;
044import com.echothree.model.control.workflow.server.control.WorkflowControl;
045import com.echothree.model.data.communication.server.factory.CommunicationEventFactory;
046import com.echothree.model.data.invoice.server.factory.InvoiceFactory;
047import com.echothree.model.data.party.server.entity.Party;
048import com.echothree.model.data.user.server.entity.UserVisit;
049import com.echothree.model.data.vendor.server.entity.Vendor;
050import com.echothree.model.data.vendor.server.factory.VendorItemFactory;
051import com.echothree.util.common.transfer.ListWrapper;
052import com.echothree.util.server.persistence.Session;
053import com.echothree.util.server.string.AmountUtils;
054import javax.enterprise.context.RequestScoped;
055
056@RequestScoped
057public class VendorTransferCache
058        extends BaseVendorTransferCache<Party, VendorTransfer> {
059
060    AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
061    BillingControl billingControl = Session.getModelController(BillingControl.class);
062    CancellationPolicyControl cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class);
063    CarrierControl carrierControl = Session.getModelController(CarrierControl.class);
064    CommunicationControl communicationControl = Session.getModelController(CommunicationControl.class);
065    ContactControl contactControl = Session.getModelController(ContactControl.class);
066    ContactListControl contactListControl = Session.getModelController(ContactListControl.class);
067    DocumentControl documentControl = Session.getModelController(DocumentControl.class);
068    EntityInstanceControl entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
069    ItemControl itemControl = Session.getModelController(ItemControl.class);
070    InvoiceControl invoiceControl = Session.getModelController(InvoiceControl.class);
071    PartyControl partyControl = Session.getModelController(PartyControl.class);
072    PartyFreeOnBoardControl partyFreeOnBoardControl = Session.getModelController(PartyFreeOnBoardControl.class);
073    PrinterControl printerControl = Session.getModelController(PrinterControl.class);
074    ReturnPolicyControl returnPolicyControl = Session.getModelController(ReturnPolicyControl.class);
075    ScaleControl scaleControl = Session.getModelController(ScaleControl.class);
076    SubscriptionControl subscriptionControl = Session.getModelController(SubscriptionControl.class);
077    TermControl termControl = Session.getModelController(TermControl.class);
078    UserControl userControl = Session.getModelController(UserControl.class);
079    VendorControl vendorControl = Session.getModelController(VendorControl.class);
080    WorkflowControl workflowControl = Session.getModelController(WorkflowControl.class);
081    
082    boolean includeUserLogin;
083    boolean includeRecoveryAnswer;
084    boolean includePartyAliases;
085    boolean includePartyContactMechanisms;
086    boolean includePartyContactLists;
087    boolean includePartyDocuments;
088    boolean includePartyPrinterGroupUses;
089    boolean includePartyScaleUses;
090    boolean includePartyCarriers;
091    boolean includePartyCarrierAccounts;
092    boolean includeVendorItems;
093    boolean includeBillingAccounts;
094    boolean includeInvoicesFrom;
095    boolean includeInvoicesTo;
096    boolean includePurchasingComments;
097    boolean includePartyCreditLimits;
098    boolean includePartyTerm;
099    boolean includePartyFreeOnBoard;
100    boolean includeSubscriptions;
101    boolean includeCommunicationEvents;
102    boolean hasVendorItemsLimits;
103    boolean hasInvoiceLimits;
104    boolean hasCommunicationEventLimits;
105
106    /** Creates a new instance of VendorTransferCache */
107    protected VendorTransferCache() {
108        super();
109
110        var options = session.getOptions();
111        if(options != null) {
112            setIncludeUuid(options.contains(PartyOptions.PartyIncludeUuid) || options.contains(VendorOptions.VendorIncludeUuid));
113            includeUserLogin = options.contains(PartyOptions.PartyIncludeUserLogin);
114            includeRecoveryAnswer = options.contains(PartyOptions.PartyIncludeRecoveryAnswer);
115            includePartyAliases = options.contains(PartyOptions.PartyIncludePartyAliases);
116            includePartyContactMechanisms = options.contains(PartyOptions.PartyIncludePartyContactMechanisms);
117            includePartyContactLists = options.contains(PartyOptions.PartyIncludePartyContactLists);
118            includePartyDocuments = options.contains(PartyOptions.PartyIncludePartyDocuments);
119            includePartyPrinterGroupUses = options.contains(PartyOptions.PartyIncludePartyPrinterGroupUses);
120            includePartyScaleUses = options.contains(PartyOptions.PartyIncludePartyScaleUses);
121            includePartyCarriers = options.contains(PartyOptions.PartyIncludePartyCarriers);
122            includePartyCarrierAccounts = options.contains(PartyOptions.PartyIncludePartyCarrierAccounts);
123            includeBillingAccounts = options.contains(VendorOptions.VendorIncludeBillingAccounts);
124            includeVendorItems = options.contains(VendorOptions.VendorIncludeVendorItems);
125            includeInvoicesFrom = options.contains(VendorOptions.VendorIncludeInvoicesFrom);
126            includeInvoicesTo = options.contains(VendorOptions.VendorIncludeInvoicesTo);
127            includePurchasingComments = options.contains(VendorOptions.VendorIncludePurchasingComments);
128            includePartyCreditLimits = options.contains(VendorOptions.VendorIncludePartyCreditLimits);
129            includePartyTerm = options.contains(VendorOptions.VendorIncludePartyTerm);
130            includePartyFreeOnBoard = options.contains(VendorOptions.VendorIncludePartyFreeOnBoard);
131            includeSubscriptions = options.contains(VendorOptions.VendorIncludeSubscriptions);
132            includeCommunicationEvents = options.contains(VendorOptions.VendorIncludeCommunicationEvents);
133            setIncludeEntityAttributeGroups(options.contains(VendorOptions.VendorIncludeEntityAttributeGroups));
134            setIncludeTagScopes(options.contains(VendorOptions.VendorIncludeTagScopes));
135        }
136        
137        setIncludeEntityInstance(true);
138        
139        hasVendorItemsLimits = session.hasLimit(VendorItemFactory.class);
140        hasInvoiceLimits = session.hasLimit(InvoiceFactory.class);
141        hasCommunicationEventLimits = session.hasLimit(CommunicationEventFactory.class);
142    }
143
144    public VendorTransfer getTransfer(UserVisit userVisit, Vendor vendor) {
145        return getTransfer(userVisit, vendor.getParty());
146    }
147
148    public VendorTransfer getTransfer(UserVisit userVisit, Party party) {
149        var vendorTransfer = get(party);
150
151        if(vendorTransfer == null) {
152            var partyDetail = party.getLastDetail();
153            var partyName = partyDetail.getPartyName();
154            var partyTypeTransfer = partyControl.getPartyTypeTransfer(userVisit, partyDetail.getPartyType());
155            var preferredLanguage = partyDetail.getPreferredLanguage();
156            var preferredLanguageTransfer = preferredLanguage == null ? null : partyControl.getLanguageTransfer(userVisit, preferredLanguage);
157            var preferredCurrency = partyDetail.getPreferredCurrency();
158            var preferredCurrencyTransfer = preferredCurrency == null ? null : accountingControl.getCurrencyTransfer(userVisit, preferredCurrency);
159            var preferredTimeZone = partyDetail.getPreferredTimeZone();
160            var preferredTimeZoneTransfer = preferredTimeZone == null ? null : partyControl.getTimeZoneTransfer(userVisit, preferredTimeZone);
161            var preferredDateTimeFormat = partyDetail.getPreferredDateTimeFormat();
162            var preferredDateTimeFormatTransfer = preferredDateTimeFormat == null ? null : partyControl.getDateTimeFormatTransfer(userVisit, preferredDateTimeFormat);
163            var vendor = vendorControl.getVendor(party);
164            var vendorName = vendor.getVendorName();
165            var vendorType = vendorControl.getVendorTypeTransfer(userVisit, vendor.getVendorType());
166            var minimumPurchaseOrderLines = vendor.getMinimumPurchaseOrderLines();
167            var maximumPurchaseOrderLines = vendor.getMaximumPurchaseOrderLines();
168            var currency = partyControl.getPreferredCurrency(party);
169            var unformattedMinimumPurchaseOrderAmount = vendor.getMinimumPurchaseOrderAmount();
170            var minimumPurchaseOrderAmount = AmountUtils.getInstance().formatCostLine(currency, unformattedMinimumPurchaseOrderAmount);
171            var unformattedMaximumPurchaseOrderAmount = vendor.getMaximumPurchaseOrderAmount();
172            var maximumPurchaseOrderAmount = AmountUtils.getInstance().formatCostLine(currency, unformattedMaximumPurchaseOrderAmount);
173            var useItemPurchasingCategories = vendor.getUseItemPurchasingCategories();
174            var defaultItemAliasType = vendor.getDefaultItemAliasType();
175            var defaultItemAliasTypeTransfer = defaultItemAliasType == null ? null : itemControl.getItemAliasTypeTransfer(userVisit, defaultItemAliasType);
176            var cancellationPolicy = vendor.getCancellationPolicy();
177            var cancellationPolicyTransfer = cancellationPolicy == null ? null : cancellationPolicyControl.getCancellationPolicyTransfer(userVisit, cancellationPolicy);
178            var returnPolicy = vendor.getReturnPolicy();
179            var returnPolicyTransfer = returnPolicy == null ? null : returnPolicyControl.getReturnPolicyTransfer(userVisit, returnPolicy);
180            var apGlAccount = vendor.getApGlAccount();
181            var apGlAccountTransfer = apGlAccount == null ? null : accountingControl.getGlAccountTransfer(userVisit, apGlAccount);
182            var holdUntilComplete = vendor.getHoldUntilComplete();
183            var allowBackorders = vendor.getAllowBackorders();
184            var allowSubstitutions = vendor.getAllowSubstitutions();
185            var allowCombiningShipments = vendor.getAllowCombiningShipments();
186            var requireReference = vendor.getRequireReference();
187            var allowReferenceDuplicates = vendor.getAllowReferenceDuplicates();
188            var referenceValidationPattern = vendor.getReferenceValidationPattern();
189            var person = partyControl.getPerson(party);
190            var personTransfer = person == null ? null : partyControl.getPersonTransfer(userVisit, person);
191            var partyGroup = partyControl.getPartyGroup(party);
192            var partyGroupTransfer = partyGroup == null ? null : partyControl.getPartyGroupTransfer(userVisit, partyGroup);
193
194            var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(party.getPrimaryKey());
195            var vendorStatusTransfer = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
196                    VendorStatusConstants.Workflow_VENDOR_STATUS, entityInstance);
197
198            vendorTransfer = new VendorTransfer(partyName, partyTypeTransfer, preferredLanguageTransfer, preferredCurrencyTransfer, preferredTimeZoneTransfer,
199                    preferredDateTimeFormatTransfer, personTransfer, partyGroupTransfer, vendorName, vendorType, minimumPurchaseOrderLines,
200                    maximumPurchaseOrderLines, unformattedMinimumPurchaseOrderAmount, minimumPurchaseOrderAmount, unformattedMaximumPurchaseOrderAmount,
201                    maximumPurchaseOrderAmount, useItemPurchasingCategories, defaultItemAliasTypeTransfer, cancellationPolicyTransfer, returnPolicyTransfer,
202                    apGlAccountTransfer, holdUntilComplete, allowBackorders, allowSubstitutions, allowCombiningShipments, requireReference,
203                    allowReferenceDuplicates, referenceValidationPattern, vendorStatusTransfer);
204            put(userVisit, party, vendorTransfer);
205
206            if(includeUserLogin) {
207                vendorTransfer.setUserLogin(userControl.getUserLoginTransfer(userVisit, party));
208            }
209
210            if(includeRecoveryAnswer) {
211                vendorTransfer.setRecoveryAnswer(userControl.getRecoveryAnswerTransfer(userVisit, party));
212            }
213
214            if(includePartyAliases) {
215                vendorTransfer.setPartyAliases(new ListWrapper<>(partyControl.getPartyAliasTransfersByParty(userVisit, party)));
216            }
217
218            if(includePartyContactMechanisms) {
219                vendorTransfer.setPartyContactMechanisms(new ListWrapper<>(contactControl.getPartyContactMechanismTransfersByParty(userVisit, party)));
220            }
221            
222            if(includePartyContactLists) {
223                vendorTransfer.setPartyContactLists(new ListWrapper<>(contactListControl.getPartyContactListTransfersByParty(userVisit, party)));
224            }
225
226            if(includePartyDocuments) {
227                vendorTransfer.setPartyDocuments(new ListWrapper<>(documentControl.getPartyDocumentTransfersByParty(userVisit, party)));
228            }
229
230            if(includePartyPrinterGroupUses) {
231                vendorTransfer.setPartyPrinterGroupUses(new ListWrapper<>(printerControl.getPartyPrinterGroupUseTransfersByParty(userVisit, party)));
232            }
233
234            if(includePartyScaleUses) {
235                vendorTransfer.setPartyScaleUses(new ListWrapper<>(scaleControl.getPartyScaleUseTransfersByParty(userVisit, party)));
236            }
237
238            if(includePartyCarriers) {
239                vendorTransfer.setPartyCarriers(new ListWrapper<>(carrierControl.getPartyCarrierTransfersByParty(userVisit, party)));
240            }
241
242            if(includePartyCarrierAccounts) {
243                vendorTransfer.setPartyCarrierAccounts(new ListWrapper<>(carrierControl.getPartyCarrierAccountTransfersByParty(userVisit, party)));
244            }
245            
246            if(includeBillingAccounts) {
247                vendorTransfer.setBillingAccounts(new ListWrapper<>(billingControl.getBillingAccountTransfersByBillFrom(userVisit, party)));
248            }
249            
250            if(includeVendorItems) {
251                vendorTransfer.setVendorItems(new ListWrapper<>(vendorControl.getVendorItemTransfersByVendorParty(userVisit, party)));
252                
253                if(hasVendorItemsLimits) {
254                    vendorTransfer.setVendorItemsCount(vendorControl.countVendorItemsByVendorParty(party));
255                }
256            }
257            
258            if(includeInvoicesFrom) {
259                vendorTransfer.setInvoicesFrom(new ListWrapper<>(invoiceControl.getInvoiceTransfersByInvoiceFrom(userVisit, party)));
260                
261                if(hasInvoiceLimits) {
262                    vendorTransfer.setInvoicesFromCount(invoiceControl.countInvoicesByInvoiceFrom(party));
263                }
264            }
265            
266            if(includeInvoicesTo) {
267                vendorTransfer.setInvoicesTo(new ListWrapper<>(invoiceControl.getInvoiceTransfersByInvoiceTo(userVisit, party)));
268                
269                if(hasInvoiceLimits) {
270                    vendorTransfer.setInvoicesToCount(invoiceControl.countInvoicesByInvoiceTo(party));
271                }
272            }
273
274            if(includePurchasingComments) {
275                setupComments(userVisit, null, entityInstance, vendorTransfer, CommentConstants.CommentType_VENDOR_PURCHASING);
276            }
277
278            if(includePartyCreditLimits) {
279                vendorTransfer.setPartyCreditLimits(new ListWrapper<>(termControl.getPartyCreditLimitTransfersByParty(userVisit, party)));
280            }
281
282            if(includePartyTerm) {
283                vendorTransfer.setPartyTerm(termControl.getPartyTermTransfer(userVisit, party));
284            }
285
286            if(includePartyFreeOnBoard) {
287                vendorTransfer.setPartyFreeOnBoard(partyFreeOnBoardControl.getPartyFreeOnBoardTransfer(userVisit, party));
288            }
289
290            if(includeSubscriptions) {
291                vendorTransfer.setSubscriptions(new ListWrapper<>(subscriptionControl.getSubscriptionTransfersByParty(userVisit, party)));
292            }
293
294            if(includeCommunicationEvents) {
295                vendorTransfer.setCommunicationEvents(new ListWrapper<>(communicationControl.getCommunicationEventTransfersByParty(userVisit, party)));
296
297                if(hasCommunicationEventLimits) {
298                    vendorTransfer.setCommunicationEventsCount(communicationControl.countCommunicationEventsByParty(party));
299                }
300            }
301        }
302
303        return vendorTransfer;
304    }
305}