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.accounting.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.accounting.common.transfer.TransactionGlEntryTransfer;
021import com.echothree.model.control.accounting.server.control.AccountingControl;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.data.accounting.server.entity.TransactionGlEntry;
024import com.echothree.model.data.user.server.entity.UserVisit;
025import com.echothree.util.server.string.AmountUtils;
026import javax.enterprise.context.RequestScoped;
027
028@RequestScoped
029public class TransactionGlEntryTransferCache
030        extends BaseAccountingTransferCache<TransactionGlEntry, TransactionGlEntryTransfer> {
031
032    @Inject
033    AccountingControl accountingControl;
034
035    @Inject
036    PartyControl partyControl;
037
038    /** Creates a new instance of TransactionGlEntryTransferCache */
039    protected TransactionGlEntryTransferCache() {
040        super();
041    }
042
043    @Override
044    public TransactionGlEntryTransfer getTransfer(UserVisit userVisit, TransactionGlEntry transactionGlEntry) {
045        var transactionGlEntryTransfer = get(transactionGlEntry);
046
047        if(transactionGlEntryTransfer == null) {
048            var transactionTransfer = accountingControl.getTransactionTransfer(userVisit, transactionGlEntry.getTransaction());
049            var transactionGlEntrySequence = transactionGlEntry.getTransactionGlEntrySequence();
050            var groupPartyTransfer = partyControl.getPartyTransfer(userVisit, transactionGlEntry.getGroupParty());
051            var transactionGlAccountCategory = transactionGlEntry.getTransactionGlAccountCategory();
052            var transactionGlAccountCategoryTransfer = transactionGlAccountCategory == null ? null : accountingControl.getTransactionGlAccountCategoryTransfer(userVisit, transactionGlEntry.getTransactionGlAccountCategory());
053            var glAccount = transactionGlEntry.getGlAccount();
054            var glAccountCurrency = glAccount.getLastDetail().getCurrency();
055            var glAccountTransfer = accountingControl.getGlAccountTransfer(userVisit, glAccount);
056            var originalCurrency = transactionGlEntry.getOriginalCurrency();
057            var originalCurrencyTransfer = originalCurrency == null ? null : accountingControl.getCurrencyTransfer(userVisit, originalCurrency);
058            var unformattedOriginalDebit = transactionGlEntry.getOriginalDebit();
059            var originalDebit = unformattedOriginalDebit == null ? null : AmountUtils.getInstance().formatAmount(originalCurrency, unformattedOriginalDebit);
060            var unformattedOriginalCredit = transactionGlEntry.getOriginalCredit();
061            var originalCredit = unformattedOriginalCredit == null ? null : AmountUtils.getInstance().formatAmount(originalCurrency, unformattedOriginalCredit);
062            var unformattedDebit = transactionGlEntry.getDebit();
063            var debit = unformattedDebit == null ? null : AmountUtils.getInstance().formatAmount(glAccountCurrency, unformattedDebit);
064            var unformattedCredit = transactionGlEntry.getCredit();
065            var credit = unformattedCredit == null ? null : AmountUtils.getInstance().formatAmount(glAccountCurrency, unformattedCredit);
066
067            transactionGlEntryTransfer = new TransactionGlEntryTransfer(transactionTransfer, transactionGlEntrySequence,
068                    groupPartyTransfer, transactionGlAccountCategoryTransfer, glAccountTransfer, originalCurrencyTransfer,
069                    unformattedOriginalDebit, originalDebit, unformattedOriginalCredit, originalCredit, unformattedDebit,
070                    debit, unformattedCredit, credit);
071
072            put(userVisit, transactionGlEntry, transactionGlEntryTransfer);
073        }
074
075        return transactionGlEntryTransfer;
076    }
077
078}