001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.CurrencyTransfer;
020import com.echothree.model.control.accounting.common.transfer.GlAccountTransfer;
021import com.echothree.model.control.accounting.common.transfer.TransactionGlAccountCategoryTransfer;
022import com.echothree.model.control.accounting.common.transfer.TransactionGlEntryTransfer;
023import com.echothree.model.control.accounting.common.transfer.TransactionTransfer;
024import com.echothree.model.control.accounting.server.control.AccountingControl;
025import com.echothree.model.control.party.common.transfer.PartyTransfer;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.data.accounting.server.entity.Currency;
028import com.echothree.model.data.accounting.server.entity.GlAccount;
029import com.echothree.model.data.accounting.server.entity.TransactionGlAccountCategory;
030import com.echothree.model.data.accounting.server.entity.TransactionGlEntry;
031import com.echothree.model.data.user.server.entity.UserVisit;
032import com.echothree.util.server.persistence.Session;
033import com.echothree.util.server.string.AmountUtils;
034
035public class TransactionGlEntryTransferCache
036        extends BaseAccountingTransferCache<TransactionGlEntry, TransactionGlEntryTransfer> {
037
038    PartyControl partyControl = Session.getModelController(PartyControl.class);
039
040    /** Creates a new instance of TransactionGlEntryTransferCache */
041    public TransactionGlEntryTransferCache(UserVisit userVisit, AccountingControl accountingControl) {
042        super(userVisit, accountingControl);
043    }
044
045    @Override
046    public TransactionGlEntryTransfer getTransfer(TransactionGlEntry transactionGlEntry) {
047        TransactionGlEntryTransfer transactionGlEntryTransfer = get(transactionGlEntry);
048
049        if(transactionGlEntryTransfer == null) {
050            TransactionTransfer transactionTransfer = accountingControl.getTransactionTransfer(userVisit, transactionGlEntry.getTransaction());
051            Integer transactionGlEntrySequence = transactionGlEntry.getTransactionGlEntrySequence();
052            TransactionGlEntry parentTransactionGlEntry = transactionGlEntry.getParentTransactionGlEntry();
053            TransactionGlEntryTransfer parentTransactionGlEntryTransfer = parentTransactionGlEntry == null ? null : accountingControl.getTransactionGlEntryTransfer(userVisit, parentTransactionGlEntry);
054            PartyTransfer groupPartyTransfer = partyControl.getPartyTransfer(userVisit, transactionGlEntry.getGroupParty());
055            TransactionGlAccountCategory transactionGlAccountCategory = transactionGlEntry.getTransactionGlAccountCategory();
056            TransactionGlAccountCategoryTransfer transactionGlAccountCategoryTransfer = transactionGlAccountCategory == null ? null : accountingControl.getTransactionGlAccountCategoryTransfer(userVisit, transactionGlEntry.getTransactionGlAccountCategory());
057            GlAccount glAccount = transactionGlEntry.getGlAccount();
058            GlAccountTransfer glAccountTransfer = accountingControl.getGlAccountTransfer(userVisit, glAccount);
059            Currency originalCurrency = transactionGlEntry.getOriginalCurrency();
060            CurrencyTransfer originalCurrencyTransfer = originalCurrency == null ? null : accountingControl.getCurrencyTransfer(userVisit, originalCurrency);
061            Long unformattedOriginalAmount = transactionGlEntry.getOriginalAmount();
062            String originalDebit = unformattedOriginalAmount >= 0 ? null : AmountUtils.getInstance().formatAmount(originalCurrency, -unformattedOriginalAmount);
063            String originalCredit = originalDebit == null ? AmountUtils.getInstance().formatAmount(originalCurrency, unformattedOriginalAmount) : null;
064            Long unformattedAmount = transactionGlEntry.getAmount();
065            String debit = unformattedAmount >= 0 ? null : AmountUtils.getInstance().formatAmount(originalCurrency, -unformattedAmount);
066            String credit = debit == null ? AmountUtils.getInstance().formatAmount(originalCurrency, unformattedAmount) : null;
067
068            transactionGlEntryTransfer = new TransactionGlEntryTransfer(transactionTransfer, transactionGlEntrySequence, parentTransactionGlEntryTransfer, groupPartyTransfer,
069                    transactionGlAccountCategoryTransfer, glAccountTransfer, originalCurrencyTransfer, unformattedOriginalAmount, originalDebit, originalCredit, unformattedAmount, debit, credit);
070
071            put(transactionGlEntry, transactionGlEntryTransfer);
072        }
073
074        return transactionGlEntryTransfer;
075    }
076
077}