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.logic;
018
019import com.echothree.model.control.accounting.common.TransactionTimeTypes;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.party.common.PartyTypes;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.control.period.common.PeriodConstants;
024import com.echothree.model.control.period.server.control.PeriodControl;
025import com.echothree.model.data.accounting.server.entity.GlAccount;
026import com.echothree.model.data.accounting.server.entity.Transaction;
027import com.echothree.model.data.accounting.server.entity.TransactionGlEntry;
028import com.echothree.model.data.party.server.entity.Party;
029import com.echothree.model.data.period.server.entity.Period;
030import com.echothree.util.common.persistence.BasePK;
031import com.echothree.util.server.persistence.Session;
032import javax.enterprise.context.ApplicationScoped;
033import javax.inject.Inject;
034
035@ApplicationScoped
036public class PostingLogic {
037
038    @Inject
039    AccountingControl accountingControl;
040
041    @Inject
042    PartyControl partyControl;
043
044    @Inject
045    PeriodControl periodControl;
046
047    @Inject
048    TransactionTimeLogic transactionTimeLogic;
049
050    protected PostingLogic() {
051        super();
052    }
053    
054    private void updateGlAccountSummary(final AccountingControl accountingControl, final GlAccount glAccount,
055            final Party groupParty, final Period period, final Long debit, final Long credit) {
056        var glAccountSummary = accountingControl.getGlAccountSummaryForUpdate(glAccount, groupParty, period);
057
058        var debitAdjustment = debit == null ? 0L : debit;
059        var creditAdjustment = credit == null ? 0L : credit;
060        var balanceAdjustment = creditAdjustment - debitAdjustment;
061        if(glAccountSummary == null) {
062            accountingControl.createGlAccountSummary(glAccount, groupParty, period, debitAdjustment, creditAdjustment,
063                    balanceAdjustment);
064        } else {
065            glAccountSummary.setDebitTotal(glAccountSummary.getDebitTotal() + debitAdjustment);
066            glAccountSummary.setCreditTotal(glAccountSummary.getCreditTotal() + creditAdjustment);
067            glAccountSummary.setBalance(glAccountSummary.getBalance() + balanceAdjustment);
068        }
069    }
070    
071    private void postTransactionGlEntry(final AccountingControl accountingControl, final PartyControl partyControl,
072            final TransactionGlEntry transactionGlEntry, final Period period) {
073        var glAccount = transactionGlEntry.getGlAccount();
074        var debit = transactionGlEntry.getDebit();
075        var credit = transactionGlEntry.getCredit();
076
077        // Go through this for each one of the COMPANY, DIVISION, and DEPARTMENT, updating the
078        // GL Account Summaries as needed.
079        var groupParty = transactionGlEntry.getGroupParty();
080        for(var i = 0; i < 3; i++) {
081            var partyTypeName = groupParty.getLastDetail().getPartyType().getPartyTypeName();
082            
083            updateGlAccountSummary(accountingControl, glAccount, groupParty, period, debit, credit);
084            
085            if(partyTypeName.equals(PartyTypes.COMPANY.name())) {
086                break;
087            } else if(partyTypeName.equals(PartyTypes.DIVISION.name())) {
088                groupParty = partyControl.getPartyDivision(groupParty).getCompanyParty();
089            } else if(partyTypeName.equals(PartyTypes.DEPARTMENT.name())) {
090                groupParty = partyControl.getPartyDepartment(groupParty).getDivisionParty();
091            }
092        }
093    }
094    
095    public void postTransaction(final Session session, final Transaction transaction, final BasePK createdBy) {
096        var postedTime = session.getStartTime();
097        var periods = periodControl.getContainingPeriodsUsingNames(PeriodConstants.PeriodKind_FISCAL, postedTime);
098        var transactionGlEntries = accountingControl.getTransactionGlEntriesByTransaction(transaction);
099        transactionGlEntries.forEach((transactionGlEntry) ->
100                periods.forEach((period) ->
101                        postTransactionGlEntry(accountingControl, partyControl, transactionGlEntry, period)
102                )
103        );
104
105        transactionTimeLogic.createTransactionTime(null, transaction, TransactionTimeTypes.POSTED_TIME.name(),
106                postedTime, createdBy);
107    }
108    
109}