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.logic;
018
019import com.echothree.model.control.accounting.server.control.AccountingControl;
020import com.echothree.model.control.party.common.PartyTypes;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.control.period.common.PeriodConstants;
023import com.echothree.model.control.period.server.control.PeriodControl;
024import com.echothree.model.data.accounting.server.entity.GlAccount;
025import com.echothree.model.data.accounting.server.entity.GlAccountSummary;
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.server.persistence.Session;
031import java.util.List;
032
033public class PostingLogic {
034
035    private PostingLogic() {
036        super();
037    }
038
039    private static class PostingLogicHolder {
040        static PostingLogic instance = new PostingLogic();
041    }
042
043    public static PostingLogic getInstance() {
044        return PostingLogicHolder.instance;
045    }
046    
047    private void updateGlAccountSummary(final AccountingControl accountingControl, final GlAccount glAccount, final Party groupParty, final Period period,
048            final long amount) {
049        GlAccountSummary glAccountSummary = accountingControl.getGlAccountSummaryForUpdate(glAccount, groupParty, period);
050        
051        if(glAccountSummary == null) {
052            glAccountSummary = accountingControl.createGlAccountSummary(glAccount, groupParty, period, amount);
053        } else {
054            glAccountSummary.setBalance(glAccountSummary.getBalance() + amount);
055        }
056        
057    }
058    
059    private void postTransactionGlEntry(final AccountingControl accountingControl, final PartyControl partyControl, final TransactionGlEntry transactionGlEntry,
060            final Period period) {
061        long amount = transactionGlEntry.getAmount();
062        Party groupParty = transactionGlEntry.getGroupParty();
063        GlAccount glAccount = transactionGlEntry.getGlAccount();
064        
065        for(int i = 0; i < 3; i++) {
066            String partyTypeName = groupParty.getLastDetail().getPartyType().getPartyTypeName();
067            
068            updateGlAccountSummary(accountingControl, glAccount, groupParty, period, amount);
069            
070            if(partyTypeName.equals(PartyTypes.COMPANY.name())) {
071                break;
072            } else if(partyTypeName.equals(PartyTypes.DIVISION.name())) {
073                groupParty = partyControl.getPartyDivision(groupParty).getCompanyParty();
074            } else if(partyTypeName.equals(PartyTypes.DEPARTMENT.name())) {
075                groupParty = partyControl.getPartyDepartment(groupParty).getDivisionParty();
076            }
077        }
078    }
079    
080    public void postTransaction(final Transaction transaction) {
081        var accountingControl = Session.getModelController(AccountingControl.class);
082        var partyControl = Session.getModelController(PartyControl.class);
083        var periodControl = Session.getModelController(PeriodControl.class);
084        Long postingTime = transaction.getLastDetail().getPostingTime();
085        List<TransactionGlEntry> transactionGlEntries = accountingControl.getTransactionGlEntriesByTransaction(transaction);
086        List<Period> periods = periodControl.getContainingPeriodsUsingNames(PeriodConstants.PeriodKind_FISCAL, postingTime);
087        
088        transactionGlEntries.forEach((transactionGlEntry) -> {
089            periods.forEach((period) -> {
090                postTransactionGlEntry(accountingControl, partyControl, transactionGlEntry, period);
091            });
092        });
093    }
094    
095}