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