001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.common.transfer.CurrencyTransfer;
020import com.echothree.model.control.accounting.common.transfer.GlAccountTransfer;
021import com.echothree.model.control.accounting.server.control.AccountingControl;
022import com.echothree.model.control.cancellationpolicy.common.transfer.CancellationPolicyTransfer;
023import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl;
024import com.echothree.model.control.carrier.server.control.CarrierControl;
025import com.echothree.model.control.comment.common.CommentConstants;
026import com.echothree.model.control.communication.server.control.CommunicationControl;
027import com.echothree.model.control.contact.server.control.ContactControl;
028import com.echothree.model.control.contactlist.server.ContactListControl;
029import com.echothree.model.control.core.server.control.CoreControl;
030import com.echothree.model.control.document.server.control.DocumentControl;
031import com.echothree.model.control.invoice.server.control.InvoiceControl;
032import com.echothree.model.control.item.common.transfer.ItemAliasTypeTransfer;
033import com.echothree.model.control.item.server.control.ItemControl;
034import com.echothree.model.control.party.common.PartyOptions;
035import com.echothree.model.control.party.common.transfer.DateTimeFormatTransfer;
036import com.echothree.model.control.party.common.transfer.LanguageTransfer;
037import com.echothree.model.control.party.common.transfer.PartyGroupTransfer;
038import com.echothree.model.control.party.common.transfer.PartyTypeTransfer;
039import com.echothree.model.control.party.common.transfer.PersonTransfer;
040import com.echothree.model.control.party.common.transfer.TimeZoneTransfer;
041import com.echothree.model.control.party.server.control.PartyControl;
042import com.echothree.model.control.payment.server.control.BillingControl;
043import com.echothree.model.control.printer.server.control.PrinterControl;
044import com.echothree.model.control.returnpolicy.common.transfer.ReturnPolicyTransfer;
045import com.echothree.model.control.returnpolicy.server.control.ReturnPolicyControl;
046import com.echothree.model.control.scale.server.control.ScaleControl;
047import com.echothree.model.control.shipment.server.control.PartyFreeOnBoardControl;
048import com.echothree.model.control.subscription.server.control.SubscriptionControl;
049import com.echothree.model.control.term.server.control.TermControl;
050import com.echothree.model.control.user.server.control.UserControl;
051import com.echothree.model.control.vendor.common.VendorOptions;
052import com.echothree.model.control.vendor.common.transfer.VendorTransfer;
053import com.echothree.model.control.vendor.common.transfer.VendorTypeTransfer;
054import com.echothree.model.control.vendor.common.workflow.VendorStatusConstants;
055import com.echothree.model.control.vendor.server.control.VendorControl;
056import com.echothree.model.control.workflow.common.transfer.WorkflowEntityStatusTransfer;
057import com.echothree.model.control.workflow.server.control.WorkflowControl;
058import com.echothree.model.data.accounting.server.entity.Currency;
059import com.echothree.model.data.accounting.server.entity.GlAccount;
060import com.echothree.model.data.cancellationpolicy.server.entity.CancellationPolicy;
061import com.echothree.model.data.communication.server.factory.CommunicationEventFactory;
062import com.echothree.model.data.core.server.entity.EntityInstance;
063import com.echothree.model.data.invoice.server.factory.InvoiceFactory;
064import com.echothree.model.data.item.server.entity.ItemAliasType;
065import com.echothree.model.data.party.server.entity.DateTimeFormat;
066import com.echothree.model.data.party.server.entity.Language;
067import com.echothree.model.data.party.server.entity.Party;
068import com.echothree.model.data.party.server.entity.PartyDetail;
069import com.echothree.model.data.party.server.entity.PartyGroup;
070import com.echothree.model.data.party.server.entity.Person;
071import com.echothree.model.data.party.server.entity.TimeZone;
072import com.echothree.model.data.returnpolicy.server.entity.ReturnPolicy;
073import com.echothree.model.data.user.server.entity.UserVisit;
074import com.echothree.model.data.vendor.server.entity.Vendor;
075import com.echothree.model.data.vendor.server.factory.VendorItemFactory;
076import com.echothree.util.common.transfer.ListWrapper;
077import com.echothree.util.server.persistence.Session;
078import com.echothree.util.server.string.AmountUtils;
079
080public class VendorTransferCache
081        extends BaseVendorTransferCache<Party, VendorTransfer> {
082
083    AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
084    BillingControl billingControl = Session.getModelController(BillingControl.class);
085    CancellationPolicyControl cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class);
086    CarrierControl carrierControl = Session.getModelController(CarrierControl.class);
087    CommunicationControl communicationControl = Session.getModelController(CommunicationControl.class);
088    ContactControl contactControl = Session.getModelController(ContactControl.class);
089    ContactListControl contactListControl = Session.getModelController(ContactListControl.class);
090    CoreControl coreControl = Session.getModelController(CoreControl.class);
091    DocumentControl documentControl = Session.getModelController(DocumentControl.class);
092    ItemControl itemControl = Session.getModelController(ItemControl.class);
093    InvoiceControl invoiceControl = Session.getModelController(InvoiceControl.class);
094    PartyControl partyControl = Session.getModelController(PartyControl.class);
095    PartyFreeOnBoardControl partyFreeOnBoardControl = Session.getModelController(PartyFreeOnBoardControl.class);
096    PrinterControl printerControl = Session.getModelController(PrinterControl.class);
097    ReturnPolicyControl returnPolicyControl = Session.getModelController(ReturnPolicyControl.class);
098    ScaleControl scaleControl = Session.getModelController(ScaleControl.class);
099    SubscriptionControl subscriptionControl = Session.getModelController(SubscriptionControl.class);
100    TermControl termControl = Session.getModelController(TermControl.class);
101    UserControl userControl = Session.getModelController(UserControl.class);
102    WorkflowControl workflowControl = Session.getModelController(WorkflowControl.class);
103    
104    boolean includeUserLogin;
105    boolean includeRecoveryAnswer;
106    boolean includePartyAliases;
107    boolean includePartyContactMechanisms;
108    boolean includePartyContactLists;
109    boolean includePartyDocuments;
110    boolean includePartyPrinterGroupUses;
111    boolean includePartyScaleUses;
112    boolean includePartyCarriers;
113    boolean includePartyCarrierAccounts;
114    boolean includeVendorItems;
115    boolean includeBillingAccounts;
116    boolean includeInvoicesFrom;
117    boolean includeInvoicesTo;
118    boolean includePurchasingComments;
119    boolean includePartyCreditLimits;
120    boolean includePartyTerm;
121    boolean includePartyFreeOnBoard;
122    boolean includeSubscriptions;
123    boolean includeCommunicationEvents;
124    boolean hasVendorItemsLimits;
125    boolean hasInvoiceLimits;
126    boolean hasCommunicationEventLimits;
127
128    /** Creates a new instance of VendorTransferCache */
129    public VendorTransferCache(UserVisit userVisit, VendorControl vendorControl) {
130        super(userVisit, vendorControl);
131
132        var options = session.getOptions();
133        if(options != null) {
134            setIncludeKey(options.contains(PartyOptions.PartyIncludeKey) || options.contains(VendorOptions.VendorIncludeKey));
135            setIncludeGuid(options.contains(PartyOptions.PartyIncludeGuid) || options.contains(VendorOptions.VendorIncludeGuid));
136            includeUserLogin = options.contains(PartyOptions.PartyIncludeUserLogin);
137            includeRecoveryAnswer = options.contains(PartyOptions.PartyIncludeRecoveryAnswer);
138            includePartyAliases = options.contains(PartyOptions.PartyIncludePartyAliases);
139            includePartyContactMechanisms = options.contains(PartyOptions.PartyIncludePartyContactMechanisms);
140            includePartyContactLists = options.contains(PartyOptions.PartyIncludePartyContactLists);
141            includePartyDocuments = options.contains(PartyOptions.PartyIncludePartyDocuments);
142            includePartyPrinterGroupUses = options.contains(PartyOptions.PartyIncludePartyPrinterGroupUses);
143            includePartyScaleUses = options.contains(PartyOptions.PartyIncludePartyScaleUses);
144            includePartyCarriers = options.contains(PartyOptions.PartyIncludePartyCarriers);
145            includePartyCarrierAccounts = options.contains(PartyOptions.PartyIncludePartyCarrierAccounts);
146            includeBillingAccounts = options.contains(VendorOptions.VendorIncludeBillingAccounts);
147            includeVendorItems = options.contains(VendorOptions.VendorIncludeVendorItems);
148            includeInvoicesFrom = options.contains(VendorOptions.VendorIncludeInvoicesFrom);
149            includeInvoicesTo = options.contains(VendorOptions.VendorIncludeInvoicesTo);
150            includePurchasingComments = options.contains(VendorOptions.VendorIncludePurchasingComments);
151            includePartyCreditLimits = options.contains(VendorOptions.VendorIncludePartyCreditLimits);
152            includePartyTerm = options.contains(VendorOptions.VendorIncludePartyTerm);
153            includePartyFreeOnBoard = options.contains(VendorOptions.VendorIncludePartyFreeOnBoard);
154            includeSubscriptions = options.contains(VendorOptions.VendorIncludeSubscriptions);
155            includeCommunicationEvents = options.contains(VendorOptions.VendorIncludeCommunicationEvents);
156            setIncludeEntityAttributeGroups(options.contains(VendorOptions.VendorIncludeEntityAttributeGroups));
157            setIncludeTagScopes(options.contains(VendorOptions.VendorIncludeTagScopes));
158        }
159        
160        setIncludeEntityInstance(true);
161        
162        hasVendorItemsLimits = session.hasLimit(VendorItemFactory.class);
163        hasInvoiceLimits = session.hasLimit(InvoiceFactory.class);
164        hasCommunicationEventLimits = session.hasLimit(CommunicationEventFactory.class);
165    }
166
167    public VendorTransfer getTransfer(Vendor vendor) {
168        return getTransfer(vendor.getParty());
169    }
170
171    public VendorTransfer getTransfer(Party party) {
172        VendorTransfer vendorTransfer = get(party);
173
174        if(vendorTransfer == null) {
175            PartyDetail partyDetail = party.getLastDetail();
176            String partyName = partyDetail.getPartyName();
177            PartyTypeTransfer partyTypeTransfer = partyControl.getPartyTypeTransfer(userVisit, partyDetail.getPartyType());
178            Language preferredLanguage = partyDetail.getPreferredLanguage();
179            LanguageTransfer preferredLanguageTransfer = preferredLanguage == null ? null : partyControl.getLanguageTransfer(userVisit, preferredLanguage);
180            Currency preferredCurrency = partyDetail.getPreferredCurrency();
181            CurrencyTransfer preferredCurrencyTransfer = preferredCurrency == null ? null : accountingControl.getCurrencyTransfer(userVisit, preferredCurrency);
182            TimeZone preferredTimeZone = partyDetail.getPreferredTimeZone();
183            TimeZoneTransfer preferredTimeZoneTransfer = preferredTimeZone == null ? null : partyControl.getTimeZoneTransfer(userVisit, preferredTimeZone);
184            DateTimeFormat preferredDateTimeFormat = partyDetail.getPreferredDateTimeFormat();
185            DateTimeFormatTransfer preferredDateTimeFormatTransfer = preferredDateTimeFormat == null ? null : partyControl.getDateTimeFormatTransfer(userVisit, preferredDateTimeFormat);
186            Vendor vendor = vendorControl.getVendor(party);
187            String vendorName = vendor.getVendorName();
188            VendorTypeTransfer vendorType = vendorControl.getVendorTypeTransfer(userVisit, vendor.getVendorType());
189            Integer minimumPurchaseOrderLines = vendor.getMinimumPurchaseOrderLines();
190            Integer maximumPurchaseOrderLines = vendor.getMaximumPurchaseOrderLines();
191            Currency currency = partyControl.getPreferredCurrency(party);
192            Long unformattedMinimumPurchaseOrderAmount = vendor.getMinimumPurchaseOrderAmount();
193            String minimumPurchaseOrderAmount = AmountUtils.getInstance().formatCostLine(currency, unformattedMinimumPurchaseOrderAmount);
194            Long unformattedMaximumPurchaseOrderAmount = vendor.getMaximumPurchaseOrderAmount();
195            String maximumPurchaseOrderAmount = AmountUtils.getInstance().formatCostLine(currency, unformattedMaximumPurchaseOrderAmount);
196            Boolean useItemPurchasingCategories = vendor.getUseItemPurchasingCategories();
197            ItemAliasType defaultItemAliasType = vendor.getDefaultItemAliasType();
198            ItemAliasTypeTransfer defaultItemAliasTypeTransfer = defaultItemAliasType == null ? null : itemControl.getItemAliasTypeTransfer(userVisit, defaultItemAliasType);
199            CancellationPolicy cancellationPolicy = vendor.getCancellationPolicy();
200            CancellationPolicyTransfer cancellationPolicyTransfer = cancellationPolicy == null ? null : cancellationPolicyControl.getCancellationPolicyTransfer(userVisit, cancellationPolicy);
201            ReturnPolicy returnPolicy = vendor.getReturnPolicy();
202            ReturnPolicyTransfer returnPolicyTransfer = returnPolicy == null ? null : returnPolicyControl.getReturnPolicyTransfer(userVisit, returnPolicy);
203            GlAccount apGlAccount = vendor.getApGlAccount();
204            GlAccountTransfer apGlAccountTransfer = apGlAccount == null ? null : accountingControl.getGlAccountTransfer(userVisit, apGlAccount);
205            Boolean holdUntilComplete = vendor.getHoldUntilComplete();
206            Boolean allowBackorders = vendor.getAllowBackorders();
207            Boolean allowSubstitutions = vendor.getAllowSubstitutions();
208            Boolean allowCombiningShipments = vendor.getAllowCombiningShipments();
209            Boolean requireReference = vendor.getRequireReference();
210            Boolean allowReferenceDuplicates = vendor.getAllowReferenceDuplicates();
211            String referenceValidationPattern = vendor.getReferenceValidationPattern();
212            Person person = partyControl.getPerson(party);
213            PersonTransfer personTransfer = person == null ? null : partyControl.getPersonTransfer(userVisit, person);
214            PartyGroup partyGroup = partyControl.getPartyGroup(party);
215            PartyGroupTransfer partyGroupTransfer = partyGroup == null ? null : partyControl.getPartyGroupTransfer(userVisit, partyGroup);
216
217            EntityInstance entityInstance = coreControl.getEntityInstanceByBasePK(party.getPrimaryKey());
218            WorkflowEntityStatusTransfer vendorStatusTransfer = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
219                    VendorStatusConstants.Workflow_VENDOR_STATUS, entityInstance);
220
221            vendorTransfer = new VendorTransfer(partyName, partyTypeTransfer, preferredLanguageTransfer, preferredCurrencyTransfer, preferredTimeZoneTransfer,
222                    preferredDateTimeFormatTransfer, personTransfer, partyGroupTransfer, vendorName, vendorType, minimumPurchaseOrderLines,
223                    maximumPurchaseOrderLines, unformattedMinimumPurchaseOrderAmount, minimumPurchaseOrderAmount, unformattedMaximumPurchaseOrderAmount,
224                    maximumPurchaseOrderAmount, useItemPurchasingCategories, defaultItemAliasTypeTransfer, cancellationPolicyTransfer, returnPolicyTransfer,
225                    apGlAccountTransfer, holdUntilComplete, allowBackorders, allowSubstitutions, allowCombiningShipments, requireReference,
226                    allowReferenceDuplicates, referenceValidationPattern, vendorStatusTransfer);
227            put(party, vendorTransfer);
228
229            if(includeUserLogin) {
230                vendorTransfer.setUserLogin(userControl.getUserLoginTransfer(userVisit, party));
231            }
232
233            if(includeRecoveryAnswer) {
234                vendorTransfer.setRecoveryAnswer(userControl.getRecoveryAnswerTransfer(userVisit, party));
235            }
236
237            if(includePartyAliases) {
238                vendorTransfer.setPartyAliases(new ListWrapper<>(partyControl.getPartyAliasTransfersByParty(userVisit, party)));
239            }
240
241            if(includePartyContactMechanisms) {
242                vendorTransfer.setPartyContactMechanisms(new ListWrapper<>(contactControl.getPartyContactMechanismTransfersByParty(userVisit, party)));
243            }
244            
245            if(includePartyContactLists) {
246                vendorTransfer.setPartyContactLists(new ListWrapper<>(contactListControl.getPartyContactListTransfersByParty(userVisit, party)));
247            }
248
249            if(includePartyDocuments) {
250                vendorTransfer.setPartyDocuments(new ListWrapper<>(documentControl.getPartyDocumentTransfersByParty(userVisit, party)));
251            }
252
253            if(includePartyPrinterGroupUses) {
254                vendorTransfer.setPartyPrinterGroupUses(new ListWrapper<>(printerControl.getPartyPrinterGroupUseTransfersByParty(userVisit, party)));
255            }
256
257            if(includePartyScaleUses) {
258                vendorTransfer.setPartyScaleUses(new ListWrapper<>(scaleControl.getPartyScaleUseTransfersByParty(userVisit, party)));
259            }
260
261            if(includePartyCarriers) {
262                vendorTransfer.setPartyCarriers(new ListWrapper<>(carrierControl.getPartyCarrierTransfersByParty(userVisit, party)));
263            }
264
265            if(includePartyCarrierAccounts) {
266                vendorTransfer.setPartyCarrierAccounts(new ListWrapper<>(carrierControl.getPartyCarrierAccountTransfersByParty(userVisit, party)));
267            }
268            
269            if(includeBillingAccounts) {
270                vendorTransfer.setBillingAccounts(new ListWrapper<>(billingControl.getBillingAccountTransfersByBillFrom(userVisit, party)));
271            }
272            
273            if(includeVendorItems) {
274                vendorTransfer.setVendorItems(new ListWrapper<>(vendorControl.getVendorItemTransfersByVendorParty(userVisit, party)));
275                
276                if(hasVendorItemsLimits) {
277                    vendorTransfer.setVendorItemsCount(vendorControl.countVendorItemsByVendorParty(party));
278                }
279            }
280            
281            if(includeInvoicesFrom) {
282                vendorTransfer.setInvoicesFrom(new ListWrapper<>(invoiceControl.getInvoiceTransfersByInvoiceFrom(userVisit, party)));
283                
284                if(hasInvoiceLimits) {
285                    vendorTransfer.setInvoicesFromCount(invoiceControl.countInvoicesByInvoiceFrom(party));
286                }
287            }
288            
289            if(includeInvoicesTo) {
290                vendorTransfer.setInvoicesTo(new ListWrapper<>(invoiceControl.getInvoiceTransfersByInvoiceTo(userVisit, party)));
291                
292                if(hasInvoiceLimits) {
293                    vendorTransfer.setInvoicesToCount(invoiceControl.countInvoicesByInvoiceTo(party));
294                }
295            }
296
297            if(includePurchasingComments) {
298                setupComments(null, entityInstance, vendorTransfer, CommentConstants.CommentType_VENDOR_PURCHASING);
299            }
300
301            if(includePartyCreditLimits) {
302                vendorTransfer.setPartyCreditLimits(new ListWrapper<>(termControl.getPartyCreditLimitTransfersByParty(userVisit, party)));
303            }
304
305            if(includePartyTerm) {
306                vendorTransfer.setPartyTerm(termControl.getPartyTermTransfer(userVisit, party));
307            }
308
309            if(includePartyFreeOnBoard) {
310                vendorTransfer.setPartyFreeOnBoard(partyFreeOnBoardControl.getPartyFreeOnBoardTransfer(userVisit, party));
311            }
312
313            if(includeSubscriptions) {
314                vendorTransfer.setSubscriptions(new ListWrapper<>(subscriptionControl.getSubscriptionTransfersByParty(userVisit, party)));
315            }
316
317            if(includeCommunicationEvents) {
318                vendorTransfer.setCommunicationEvents(new ListWrapper<>(communicationControl.getCommunicationEventTransfersByParty(userVisit, party)));
319
320                if(hasCommunicationEventLimits) {
321                    vendorTransfer.setCommunicationEventsCount(communicationControl.countCommunicationEventsByParty(party));
322                }
323            }
324        }
325
326        return vendorTransfer;
327    }
328}