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