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.tax.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.contact.server.control.ContactControl;
022import com.echothree.model.control.tax.common.transfer.TaxTransfer;
023import com.echothree.model.control.tax.server.control.TaxControl;
024import com.echothree.model.data.tax.server.entity.Tax;
025import com.echothree.model.data.user.server.entity.UserVisit;
026import javax.enterprise.context.RequestScoped;
027
028@RequestScoped
029public class TaxTransferCache
030        extends BaseTaxTransferCache<Tax, TaxTransfer> {
031
032    @Inject
033    AccountingControl accountingControl;
034
035    @Inject
036    ContactControl contactControl;
037
038    @Inject
039    TaxControl taxControl;
040
041    /** Creates a new instance of TaxTransferCache */
042    protected TaxTransferCache() {
043        super();
044        
045        setIncludeEntityInstance(true);
046    }
047    
048    @Override
049    public TaxTransfer getTransfer(UserVisit userVisit, Tax tax) {
050        var taxTransfer = get(tax);
051        
052        if(taxTransfer == null) {
053            var taxDetail = tax.getLastDetail();
054            var taxName = taxDetail.getTaxName();
055            var contactMechanismPurpose = contactControl.getContactMechanismPurposeTransfer(userVisit, taxDetail.getContactMechanismPurpose());
056            var glAccount = accountingControl.getGlAccountTransfer(userVisit, taxDetail.getGlAccount());
057            var percent = formatFractionalPercent(taxDetail.getPercent());
058            var isDefault = taxDetail.getIsDefault();
059            var sortOrder = taxDetail.getSortOrder();
060            var description = taxControl.getBestTaxDescription(tax, getLanguage(userVisit));
061            
062            taxTransfer = new TaxTransfer(taxName, contactMechanismPurpose, glAccount, percent, isDefault, sortOrder, description);
063            put(userVisit, tax, taxTransfer);
064        }
065        
066        return taxTransfer;
067    }
068    
069}