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.party.server.transfer;
018
019import com.echothree.model.control.inventory.server.control.InventoryCostingMethodControl;
020import javax.inject.Inject;
021import com.echothree.model.control.accounting.server.control.AccountingControl;
022import com.echothree.model.control.carrier.server.control.CarrierControl;
023import com.echothree.model.control.contact.server.control.ContactControl;
024import com.echothree.model.control.document.server.control.DocumentControl;
025import com.echothree.model.control.invoice.server.control.InvoiceControl;
026import com.echothree.model.control.party.common.PartyOptions;
027import com.echothree.model.control.party.common.transfer.CompanyTransfer;
028import com.echothree.model.control.party.server.control.PartyControl;
029import com.echothree.model.control.printer.server.control.PrinterControl;
030import com.echothree.model.control.scale.server.control.ScaleControl;
031import com.echothree.model.data.invoice.server.factory.InvoiceFactory;
032import com.echothree.model.data.party.server.entity.Party;
033import com.echothree.model.data.party.server.entity.PartyCompany;
034import com.echothree.model.data.user.server.entity.UserVisit;
035import com.echothree.util.common.transfer.ListWrapper;
036import javax.enterprise.context.RequestScoped;
037
038@RequestScoped
039public class CompanyTransferCache
040        extends BasePartyTransferCache<Party, CompanyTransfer> {
041
042    @Inject
043    AccountingControl accountingControl;
044
045    @Inject
046    CarrierControl carrierControl;
047
048    @Inject
049    ContactControl contactControl;
050
051    @Inject
052    DocumentControl documentControl;
053
054    @Inject
055    InventoryCostingMethodControl inventoryCostingMethodControl;
056
057    @Inject
058    InvoiceControl invoiceControl;
059
060    @Inject
061    PartyControl partyControl;
062
063    @Inject
064    PrinterControl printerControl;
065
066    @Inject
067    ScaleControl scaleControl;
068
069    boolean includePartyContactMechanisms;
070    boolean includePartyDocuments;
071    boolean includePartyPrinterGroupUses;
072    boolean includePartyScaleUses;
073    boolean includePartyCarriers;
074    boolean includePartyCarrierAccounts;
075    boolean includeBillingAccounts;
076    boolean includeInvoicesFrom;
077    boolean includeInvoicesTo;
078    boolean includePartyInventoryCostingMethod;
079    boolean hasInvoiceLimits;
080    
081    /** Creates a new instance of CompanyTransferCache */
082    protected CompanyTransferCache() {
083        super();
084        
085        var options = session.getOptions();
086        if(options != null) {
087            setIncludeUuid(options.contains(PartyOptions.PartyIncludeUuid) || options.contains(PartyOptions.CompanyIncludeUuid));
088            includePartyContactMechanisms = options.contains(PartyOptions.PartyIncludePartyContactMechanisms);
089            includePartyDocuments = options.contains(PartyOptions.PartyIncludePartyDocuments);
090            includePartyPrinterGroupUses = options.contains(PartyOptions.PartyIncludePartyPrinterGroupUses);
091            includePartyScaleUses = options.contains(PartyOptions.PartyIncludePartyScaleUses);
092            includePartyCarriers = options.contains(PartyOptions.PartyIncludePartyCarriers);
093            includePartyCarrierAccounts = options.contains(PartyOptions.PartyIncludePartyCarrierAccounts);
094            includeBillingAccounts = options.contains(PartyOptions.CompanyIncludeBillingAccounts);
095            includeInvoicesFrom = options.contains(PartyOptions.CompanyIncludeInvoicesFrom);
096            includeInvoicesTo = options.contains(PartyOptions.CompanyIncludeInvoicesTo);
097            includePartyInventoryCostingMethod = options.contains(PartyOptions.CompanyIncludePartyInventoryCostingMethod);
098            setIncludeEntityAttributeGroups(options.contains(PartyOptions.CompanyIncludeEntityAttributeGroups));
099            setIncludeTagScopes(options.contains(PartyOptions.CompanyIncludeTagScopes));
100        }
101        
102        setIncludeEntityInstance(true);
103        
104        hasInvoiceLimits = session.hasLimit(InvoiceFactory.class);
105    }
106    
107    public CompanyTransfer getTransfer(UserVisit userVisit, PartyCompany partyCompany) {
108        return getTransfer(userVisit, partyCompany.getParty());
109    }
110
111    @Override
112    public CompanyTransfer getTransfer(UserVisit userVisit, Party party) {
113        var companyTransfer = get(party);
114        
115        if(companyTransfer == null) {
116            var partyDetail = party.getLastDetail();
117            var partyName = partyDetail.getPartyName();
118            var partyTypeTransfer = partyControl.getPartyTypeTransfer(userVisit, partyDetail.getPartyType());
119            var preferredLanguage = partyDetail.getPreferredLanguage();
120            var preferredLanguageTransfer = preferredLanguage == null ? null : partyControl.getLanguageTransfer(userVisit, preferredLanguage);
121            var preferredCurrency = partyDetail.getPreferredCurrency();
122            var preferredCurrencyTransfer = preferredCurrency == null ? null : accountingControl.getCurrencyTransfer(userVisit, preferredCurrency);
123            var preferredTimeZone = partyDetail.getPreferredTimeZone();
124            var preferredTimeZoneTransfer = preferredTimeZone == null ? null : partyControl.getTimeZoneTransfer(userVisit, preferredTimeZone);
125            var preferredDateTimeFormat = partyDetail.getPreferredDateTimeFormat();
126            var preferredDateTimeFormatTransfer = preferredDateTimeFormat == null ? null : partyControl.getDateTimeFormatTransfer(userVisit, preferredDateTimeFormat);
127            var person = partyControl.getPerson(party);
128            var personTransfer = person == null ? null : partyControl.getPersonTransfer(userVisit, person);
129            var partyGroup = partyControl.getPartyGroup(party);
130            var partyGroupTransfer = partyGroup == null ? null : partyControl.getPartyGroupTransfer(userVisit, partyGroup);
131            var partyCompany = partyControl.getPartyCompany(party);
132            var companyName = partyCompany.getPartyCompanyName();
133            var isDefault = partyCompany.getIsDefault().toString();
134            var sortOrder = partyCompany.getSortOrder().toString();
135            
136            companyTransfer = new CompanyTransfer(partyName, partyTypeTransfer, preferredLanguageTransfer, preferredCurrencyTransfer, preferredTimeZoneTransfer, preferredDateTimeFormatTransfer,
137                    personTransfer, partyGroupTransfer, companyName, isDefault, sortOrder);
138            put(userVisit, party, companyTransfer);
139            
140            if(includePartyContactMechanisms) {
141                companyTransfer.setPartyContactMechanisms(new ListWrapper<>(contactControl.getPartyContactMechanismTransfersByParty(userVisit, party)));
142            }
143
144            if(includePartyDocuments) {
145                companyTransfer.setPartyDocuments(new ListWrapper<>(documentControl.getPartyDocumentTransfersByParty(userVisit, party)));
146            }
147
148            if(includePartyPrinterGroupUses) {
149                companyTransfer.setPartyPrinterGroupUses(new ListWrapper<>(printerControl.getPartyPrinterGroupUseTransfersByParty(userVisit, party)));
150            }
151
152            if(includePartyScaleUses) {
153                companyTransfer.setPartyScaleUses(new ListWrapper<>(scaleControl.getPartyScaleUseTransfersByParty(userVisit, party)));
154            }
155
156            if(includePartyCarriers) {
157                companyTransfer.setPartyCarriers(new ListWrapper<>(carrierControl.getPartyCarrierTransfersByParty(userVisit, party)));
158            }
159
160            if(includePartyCarrierAccounts) {
161                companyTransfer.setPartyCarrierAccounts(new ListWrapper<>(carrierControl.getPartyCarrierAccountTransfersByParty(userVisit, party)));
162            }
163
164            if(includeInvoicesFrom) {
165                companyTransfer.setInvoicesFrom(new ListWrapper<>(invoiceControl.getInvoiceTransfersByInvoiceFrom(userVisit, party)));
166                
167                if(hasInvoiceLimits) {
168                    companyTransfer.setInvoicesFromCount(invoiceControl.countInvoicesByInvoiceFrom(party));
169                }
170            }
171            
172            if(includeInvoicesTo) {
173                companyTransfer.setInvoicesTo(new ListWrapper<>(invoiceControl.getInvoiceTransfersByInvoiceTo(userVisit, party)));
174                
175                if(hasInvoiceLimits) {
176                    companyTransfer.setInvoicesToCount(invoiceControl.countInvoicesByInvoiceTo(party));
177                }
178            }
179
180            if(includePartyInventoryCostingMethod) {
181                var partyInventoryCostingMethod = inventoryCostingMethodControl.getPartyInventoryCostingMethod(party);
182
183                if(partyInventoryCostingMethod != null) {
184                    companyTransfer.setPartyInventoryCostingMethod(inventoryCostingMethodControl.getPartyInventoryCostingMethodTransfer(userVisit, partyInventoryCostingMethod));
185                }
186            }
187        }
188        
189        return companyTransfer;
190    }
191    
192}