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