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