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.customer.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.customer.common.CustomerOptions;
031import com.echothree.model.control.customer.common.transfer.CustomerTransfer;
032import com.echothree.model.control.customer.common.transfer.CustomerTypeTransfer;
033import com.echothree.model.control.customer.common.workflow.CustomerCreditStatusConstants;
034import com.echothree.model.control.customer.common.workflow.CustomerStatusConstants;
035import com.echothree.model.control.customer.server.control.CustomerControl;
036import com.echothree.model.control.document.server.control.DocumentControl;
037import com.echothree.model.control.invoice.server.control.InvoiceControl;
038import com.echothree.model.control.offer.common.transfer.OfferUseTransfer;
039import com.echothree.model.control.offer.server.control.OfferUseControl;
040import com.echothree.model.control.party.common.PartyOptions;
041import com.echothree.model.control.party.common.transfer.DateTimeFormatTransfer;
042import com.echothree.model.control.party.common.transfer.LanguageTransfer;
043import com.echothree.model.control.party.common.transfer.PartyGroupTransfer;
044import com.echothree.model.control.party.common.transfer.PartyTypeTransfer;
045import com.echothree.model.control.party.common.transfer.PersonTransfer;
046import com.echothree.model.control.party.common.transfer.ProfileTransfer;
047import com.echothree.model.control.party.common.transfer.TimeZoneTransfer;
048import com.echothree.model.control.party.server.control.PartyControl;
049import com.echothree.model.control.payment.server.control.BillingControl;
050import com.echothree.model.control.payment.server.control.PartyPaymentMethodControl;
051import com.echothree.model.control.printer.server.control.PrinterControl;
052import com.echothree.model.control.returnpolicy.common.transfer.ReturnPolicyTransfer;
053import com.echothree.model.control.returnpolicy.server.control.ReturnPolicyControl;
054import com.echothree.model.control.scale.server.control.ScaleControl;
055import com.echothree.model.control.shipment.server.control.PartyFreeOnBoardControl;
056import com.echothree.model.control.subscription.server.control.SubscriptionControl;
057import com.echothree.model.control.term.server.control.TermControl;
058import com.echothree.model.control.user.server.control.UserControl;
059import com.echothree.model.control.workflow.common.transfer.WorkflowEntityStatusTransfer;
060import com.echothree.model.control.workflow.server.control.WorkflowControl;
061import com.echothree.model.data.accounting.server.entity.Currency;
062import com.echothree.model.data.accounting.server.entity.GlAccount;
063import com.echothree.model.data.cancellationpolicy.server.entity.CancellationPolicy;
064import com.echothree.model.data.communication.server.factory.CommunicationEventFactory;
065import com.echothree.model.data.core.server.entity.EntityInstance;
066import com.echothree.model.data.customer.server.entity.Customer;
067import com.echothree.model.data.invoice.server.factory.InvoiceFactory;
068import com.echothree.model.data.party.server.entity.DateTimeFormat;
069import com.echothree.model.data.party.server.entity.Language;
070import com.echothree.model.data.party.server.entity.Party;
071import com.echothree.model.data.party.server.entity.PartyDetail;
072import com.echothree.model.data.party.server.entity.PartyGroup;
073import com.echothree.model.data.party.server.entity.Person;
074import com.echothree.model.data.party.server.entity.Profile;
075import com.echothree.model.data.party.server.entity.TimeZone;
076import com.echothree.model.data.returnpolicy.server.entity.ReturnPolicy;
077import com.echothree.model.data.user.server.entity.UserVisit;
078import com.echothree.util.common.transfer.ListWrapper;
079import com.echothree.util.server.persistence.Session;
080
081public class CustomerTransferCache
082        extends BaseCustomerTransferCache<Party, CustomerTransfer> {
083
084    AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
085    BillingControl billingControl = Session.getModelController(BillingControl.class);
086    CancellationPolicyControl cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class);
087    CarrierControl carrierControl = Session.getModelController(CarrierControl.class);
088    CommunicationControl communicationControl = Session.getModelController(CommunicationControl.class);
089    ContactControl contactControl = Session.getModelController(ContactControl.class);
090    ContactListControl contactListControl = Session.getModelController(ContactListControl.class);
091    CoreControl coreControl = Session.getModelController(CoreControl.class);
092    DocumentControl documentControl = Session.getModelController(DocumentControl.class);
093    InvoiceControl invoiceControl = Session.getModelController(InvoiceControl.class);
094    OfferUseControl offerUseControl = Session.getModelController(OfferUseControl.class);
095    PartyControl partyControl = Session.getModelController(PartyControl.class);
096    PartyPaymentMethodControl partyPaymentMethodControl = Session.getModelController(PartyPaymentMethodControl.class);
097    PartyFreeOnBoardControl partyFreeOnBoardControl = Session.getModelController(PartyFreeOnBoardControl.class);
098    PrinterControl printerControl = Session.getModelController(PrinterControl.class);
099    ReturnPolicyControl returnPolicyControl = Session.getModelController(ReturnPolicyControl.class);
100    ScaleControl scaleControl = Session.getModelController(ScaleControl.class);
101    SubscriptionControl subscriptionControl = Session.getModelController(SubscriptionControl.class);
102    TermControl termControl = Session.getModelController(TermControl.class);
103    UserControl userControl = Session.getModelController(UserControl.class);
104    WorkflowControl workflowControl = Session.getModelController(WorkflowControl.class);
105
106    boolean includeUserLogin;
107    boolean includeRecoveryAnswer;
108    boolean includePartyAliases;
109    boolean includePartyContactMechanisms;
110    boolean includePartyContactLists;
111    boolean includePartyDocuments;
112    boolean includePartyPrinterGroupUses;
113    boolean includePartyScaleUses;
114    boolean includePartyCarriers;
115    boolean includePartyCarrierAccounts;
116    boolean includeCustomerServiceComments;
117    boolean includeOrderEntryComments;
118    boolean includeBillingAccounts;
119    boolean includeInvoicesFrom;
120    boolean includeInvoicesTo;
121    boolean hasInvoiceLimits;
122    boolean includePartyCreditLimits;
123    boolean includePartyTerm;
124    boolean includePartyFreeOnBoard;
125    boolean includePartyPaymentMethods;
126    boolean includePartyCancellationPolicies;
127    boolean includePartyReturnPolicies;
128    boolean includeSubscriptions;
129    boolean includeCommunicationEvents;
130    boolean hasCommunicationEventLimits;
131
132    /** Creates a new instance of CustomerTransferCache */
133    public CustomerTransferCache(UserVisit userVisit, CustomerControl customerControl) {
134        super(userVisit, customerControl);
135
136        var options = session.getOptions();
137        if(options != null) {
138            setIncludeKey(options.contains(PartyOptions.PartyIncludeKey) || options.contains(CustomerOptions.CustomerIncludeKey));
139            setIncludeGuid(options.contains(PartyOptions.PartyIncludeGuid) || options.contains(CustomerOptions.CustomerIncludeGuid));
140            includeUserLogin = options.contains(PartyOptions.PartyIncludeUserLogin);
141            includeRecoveryAnswer = options.contains(PartyOptions.PartyIncludeRecoveryAnswer);
142            includePartyAliases = options.contains(PartyOptions.PartyIncludePartyAliases);
143            includePartyContactMechanisms = options.contains(PartyOptions.PartyIncludePartyContactMechanisms);
144            includePartyContactLists = options.contains(PartyOptions.PartyIncludePartyContactLists);
145            includePartyDocuments = options.contains(PartyOptions.PartyIncludePartyDocuments);
146            includePartyPrinterGroupUses = options.contains(PartyOptions.PartyIncludePartyPrinterGroupUses);
147            includePartyScaleUses = options.contains(PartyOptions.PartyIncludePartyScaleUses);
148            includePartyCarriers = options.contains(PartyOptions.PartyIncludePartyCarriers);
149            includePartyCarrierAccounts = options.contains(PartyOptions.PartyIncludePartyCarrierAccounts);
150            includeCustomerServiceComments = options.contains(CustomerOptions.CustomerIncludeCustomerServiceComments);
151            includeOrderEntryComments = options.contains(CustomerOptions.CustomerIncludeOrderEntryComments);
152            includeBillingAccounts = options.contains(CustomerOptions.CustomerIncludeBillingAccounts);
153            includeInvoicesFrom = options.contains(CustomerOptions.CustomerIncludeInvoicesFrom);
154            includeInvoicesTo = options.contains(CustomerOptions.CustomerIncludeInvoicesTo);
155            includePartyCreditLimits = options.contains(CustomerOptions.CustomerIncludePartyCreditLimits);
156            includePartyTerm = options.contains(CustomerOptions.CustomerIncludePartyTerm);
157            includePartyFreeOnBoard = options.contains(CustomerOptions.CustomerIncludePartyFreeOnBoard);
158            includePartyPaymentMethods = options.contains(CustomerOptions.CustomerIncludePartyPaymentMethods);
159            includePartyCancellationPolicies = options.contains(CustomerOptions.CustomerIncludePartyCancellationPolicies);
160            includePartyReturnPolicies = options.contains(CustomerOptions.CustomerIncludePartyReturnPolicies);
161            includeSubscriptions = options.contains(CustomerOptions.CustomerIncludeSubscriptions);
162            includeCommunicationEvents = options.contains(CustomerOptions.CustomerIncludeCommunicationEvents);
163            setIncludeEntityAttributeGroups(options.contains(CustomerOptions.CustomerIncludeEntityAttributeGroups));
164            setIncludeTagScopes(options.contains(CustomerOptions.CustomerIncludeTagScopes));
165        }
166        
167        setIncludeEntityInstance(true);
168        
169        hasInvoiceLimits = session.hasLimit(InvoiceFactory.class);
170        hasCommunicationEventLimits = session.hasLimit(CommunicationEventFactory.class);
171    }
172
173    public CustomerTransfer getTransfer(Customer customer) {
174        return getTransfer(customer.getParty());
175    }
176
177    public CustomerTransfer getTransfer(Party party) {
178        CustomerTransfer customerTransfer = get(party);
179
180        if(customerTransfer == null) {
181            PartyDetail partyDetail = party.getLastDetail();
182            String partyName = partyDetail.getPartyName();
183            PartyTypeTransfer partyTypeTransfer = partyControl.getPartyTypeTransfer(userVisit, partyDetail.getPartyType());
184            Language preferredLanguage = partyDetail.getPreferredLanguage();
185            LanguageTransfer preferredLanguageTransfer = preferredLanguage == null ? null : partyControl.getLanguageTransfer(userVisit, preferredLanguage);
186            Currency preferredCurrency = partyDetail.getPreferredCurrency();
187            CurrencyTransfer preferredCurrencyTransfer = preferredCurrency == null ? null : accountingControl.getCurrencyTransfer(userVisit, preferredCurrency);
188            TimeZone preferredTimeZone = partyDetail.getPreferredTimeZone();
189            TimeZoneTransfer preferredTimeZoneTransfer = preferredTimeZone == null ? null : partyControl.getTimeZoneTransfer(userVisit, preferredTimeZone);
190            DateTimeFormat preferredDateTimeFormat = partyDetail.getPreferredDateTimeFormat();
191            DateTimeFormatTransfer preferredDateTimeFormatTransfer = preferredDateTimeFormat == null ? null : partyControl.getDateTimeFormatTransfer(userVisit, preferredDateTimeFormat);
192            Customer customer = customerControl.getCustomer(party);
193            String customerName = customer.getCustomerName();
194            CustomerTypeTransfer customerTypeTransfer = customerControl.getCustomerTypeTransfer(userVisit, customer.getCustomerType());
195            OfferUseTransfer initialOfferUse = offerUseControl.getOfferUseTransfer(userVisit, customer.getInitialOfferUse());
196            CancellationPolicy cancellationPolicy = customer.getCancellationPolicy();
197            CancellationPolicyTransfer cancellationPolicyTransfer = cancellationPolicy == null ? null : cancellationPolicyControl.getCancellationPolicyTransfer(userVisit, cancellationPolicy);
198            ReturnPolicy returnPolicy = customer.getReturnPolicy();
199            ReturnPolicyTransfer returnPolicyTransfer = returnPolicy == null ? null : returnPolicyControl.getReturnPolicyTransfer(userVisit, returnPolicy);
200            GlAccount arGlAccount = customer.getArGlAccount();
201            GlAccountTransfer arGlAccountTransfer = arGlAccount == null ? null : accountingControl.getGlAccountTransfer(userVisit, arGlAccount);
202            Boolean holdUntilComplete = customer.getHoldUntilComplete();
203            Boolean allowBackorders = customer.getAllowBackorders();
204            Boolean allowSubstitutions = customer.getAllowSubstitutions();
205            Boolean allowCombiningShipments = customer.getAllowCombiningShipments();
206            Boolean requireReference = customer.getRequireReference();
207            Boolean allowReferenceDuplicates = customer.getAllowReferenceDuplicates();
208            String referenceValidationPattern = customer.getReferenceValidationPattern();
209            Person person = partyControl.getPerson(party);
210            PersonTransfer personTransfer = person == null ? null : partyControl.getPersonTransfer(userVisit, person);
211            PartyGroup partyGroup = partyControl.getPartyGroup(party);
212            PartyGroupTransfer partyGroupTransfer = partyGroup == null ? null : partyControl.getPartyGroupTransfer(userVisit, partyGroup);
213            Profile profile = partyControl.getProfile(party);
214            ProfileTransfer profileTransfer = profile == null ? null : partyControl.getProfileTransfer(userVisit, profile);
215
216            EntityInstance entityInstance = coreControl.getEntityInstanceByBasePK(party.getPrimaryKey());
217            WorkflowEntityStatusTransfer customerStatusTransfer = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
218                    CustomerStatusConstants.Workflow_CUSTOMER_STATUS, entityInstance);
219
220            WorkflowEntityStatusTransfer customerCreditStatusTransfer = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
221                    CustomerCreditStatusConstants.Workflow_CUSTOMER_CREDIT_STATUS, entityInstance);
222
223            customerTransfer = new CustomerTransfer(partyName, partyTypeTransfer, preferredLanguageTransfer, preferredCurrencyTransfer,
224                    preferredTimeZoneTransfer, preferredDateTimeFormatTransfer, personTransfer, partyGroupTransfer, profileTransfer, customerName,
225                    customerTypeTransfer, initialOfferUse, cancellationPolicyTransfer, returnPolicyTransfer, arGlAccountTransfer, holdUntilComplete,
226                    allowBackorders, allowSubstitutions, allowCombiningShipments, requireReference, allowReferenceDuplicates, referenceValidationPattern,
227                    customerStatusTransfer, customerCreditStatusTransfer);
228            put(party, customerTransfer, entityInstance);
229
230            if(includeUserLogin) {
231                customerTransfer.setUserLogin(userControl.getUserLoginTransfer(userVisit, party));
232            }
233
234            if(includeRecoveryAnswer) {
235                customerTransfer.setRecoveryAnswer(userControl.getRecoveryAnswerTransfer(userVisit, party));
236            }
237
238            if(includePartyAliases) {
239                customerTransfer.setPartyAliases(new ListWrapper<>(partyControl.getPartyAliasTransfersByParty(userVisit, party)));
240            }
241
242            if(includePartyContactMechanisms) {
243                customerTransfer.setPartyContactMechanisms(new ListWrapper<>(contactControl.getPartyContactMechanismTransfersByParty(userVisit, party)));
244            }
245            
246            if(includePartyContactLists) {
247                customerTransfer.setPartyContactLists(new ListWrapper<>(contactListControl.getPartyContactListTransfersByParty(userVisit, party)));
248            }
249            
250            if(includePartyDocuments) {
251                customerTransfer.setPartyDocuments(new ListWrapper<>(documentControl.getPartyDocumentTransfersByParty(userVisit, party)));
252            }
253
254            if(includePartyPrinterGroupUses) {
255                customerTransfer.setPartyPrinterGroupUses(new ListWrapper<>(printerControl.getPartyPrinterGroupUseTransfersByParty(userVisit, party)));
256            }
257
258            if(includePartyScaleUses) {
259                customerTransfer.setPartyScaleUses(new ListWrapper<>(scaleControl.getPartyScaleUseTransfersByParty(userVisit, party)));
260            }
261
262            if(includePartyCarriers) {
263                customerTransfer.setPartyCarriers(new ListWrapper<>(carrierControl.getPartyCarrierTransfersByParty(userVisit, party)));
264            }
265
266            if(includePartyCarrierAccounts) {
267                customerTransfer.setPartyCarrierAccounts(new ListWrapper<>(carrierControl.getPartyCarrierAccountTransfersByParty(userVisit, party)));
268            }
269
270            if(includeCustomerServiceComments) {
271                setupComments(null, entityInstance, customerTransfer, CommentConstants.CommentType_CUSTOMER_CUSTOMER_SERVICE);
272            }
273
274            if(includeOrderEntryComments) {
275                setupComments(null, entityInstance, customerTransfer, CommentConstants.CommentType_CUSTOMER_ORDER_ENTRY);
276            }
277            
278            if(includeBillingAccounts) {
279                customerTransfer.setBillingAccounts(new ListWrapper<>(billingControl.getBillingAccountTransfersByBillFrom(userVisit, party)));
280            }
281            
282            if(includeInvoicesFrom) {
283                customerTransfer.setInvoicesFrom(new ListWrapper<>(invoiceControl.getInvoiceTransfersByInvoiceFrom(userVisit, party)));
284                
285                if(hasInvoiceLimits) {
286                    customerTransfer.setInvoicesFromCount(invoiceControl.countInvoicesByInvoiceFrom(party));
287                }
288            }
289            
290            if(includeInvoicesTo) {
291                customerTransfer.setInvoicesTo(new ListWrapper<>(invoiceControl.getInvoiceTransfersByInvoiceTo(userVisit, party)));
292                
293                if(hasInvoiceLimits) {
294                    customerTransfer.setInvoicesToCount(invoiceControl.countInvoicesByInvoiceTo(party));
295                }
296            }
297
298            if(includePartyCreditLimits) {
299                customerTransfer.setPartyCreditLimits(new ListWrapper<>(termControl.getPartyCreditLimitTransfersByParty(userVisit, party)));
300            }
301
302            if(includePartyTerm) {
303                customerTransfer.setPartyTerm(termControl.getPartyTermTransfer(userVisit, party));
304            }
305
306            if(includePartyFreeOnBoard) {
307                customerTransfer.setPartyFreeOnBoard(partyFreeOnBoardControl.getPartyFreeOnBoardTransfer(userVisit, party));
308            }
309
310            if(includePartyPaymentMethods) {
311                customerTransfer.setPartyPaymentMethods(new ListWrapper<>(partyPaymentMethodControl.getPartyPaymentMethodTransfersByParty(userVisit, party)));
312            }
313
314            if(includePartyCancellationPolicies) {
315                customerTransfer.setPartyCancellationPolicies(new ListWrapper<>(cancellationPolicyControl.getPartyCancellationPolicyTransfersByParty(userVisit, party)));
316            }
317
318            if(includePartyReturnPolicies) {
319                customerTransfer.setPartyReturnPolicies(new ListWrapper<>(returnPolicyControl.getPartyReturnPolicyTransfersByParty(userVisit, party)));
320            }
321
322            if(includeSubscriptions) {
323                customerTransfer.setSubscriptions(new ListWrapper<>(subscriptionControl.getSubscriptionTransfersByParty(userVisit, party)));
324            }
325
326            if(includeCommunicationEvents) {
327                customerTransfer.setCommunicationEvents(new ListWrapper<>(communicationControl.getCommunicationEventTransfersByParty(userVisit, party)));
328
329                if(hasCommunicationEventLimits) {
330                    customerTransfer.setCommunicationEventsCount(communicationControl.countCommunicationEventsByParty(party));
331                }
332            }
333        }
334
335        return customerTransfer;
336    }
337}