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