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