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