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