Thursday, 26 May 2016

General Ledger > Periodic > Ledger Settlements

If you select any of the two trasactions ( positive/ negative) , and click include it moves in to lower temporary grid. Then if balance field is zero then only "Accept " Button is enabled else no. 

So if the balances are different then also it should settle the required amount is the new requirement.So for that we need to add the new method,


public static void partialLedgerTransSettle(
    GeneralJournalAccountEntry _temporaryGeneralJournalAccountEntry,
    Map _temporaryTransRecIdToTransRecId)
{
    LedgerTransSettlement   ledgerTransSettlement;
    RecordInsertList        settlementCollection;
    Amount                  postiveBal,negativebal;
    RefRecId                refRecId;
    GeneralJournalAccountEntry  accountEntry;

    ttsbegin;

    settlementCollection = new RecordInsertList(tablenum(LedgerTransSettlement));

    ledgerTransSettlement.SettleId = NumberSeq::newGetNum(CompanyInfo::numRefParmId()).num();

    //Get sum of Positive records
    select sum(AccountingCurrencyAmount) from _temporaryGeneralJournalAccountEntry
        where _temporaryGeneralJournalAccountEntry.AccountingCurrencyAmount > 0;

    postiveBal = _temporaryGeneralJournalAccountEntry.AccountingCurrencyAmount;

   //Get Sum of Negative records
    select sum(AccountingCurrencyAmount) from _temporaryGeneralJournalAccountEntry
        where _temporaryGeneralJournalAccountEntry.AccountingCurrencyAmount < 0;

    negativebal = _temporaryGeneralJournalAccountEntry.AccountingCurrencyAmount;

    //If (Positive value is greater than negative),find positive record
    //where difference of Positive and negative is greater than record amount.
    //Update the selected record with Difference amount

    if (abs(negativebal) < abs(postiveBal)    )
    {
        postiveBal = abs(postiveBal)-abs(negativebal);

        select forUpdate _temporaryGeneralJournalAccountEntry
            where _temporaryGeneralJournalAccountEntry.AccountingCurrencyAmount > 0
                && _temporaryGeneralJournalAccountEntry.AccountingCurrencyAmount > postiveBal;

        _temporaryGeneralJournalAccountEntry.AccountingCurrencyAmount   =   postiveBal;
        _temporaryGeneralJournalAccountEntry.TransactionCurrencyAmount  =   postiveBal;
        _temporaryGeneralJournalAccountEntry.ReportingCurrencyAmount    =   postiveBal;
        _temporaryGeneralJournalAccountEntry.update();
        refRecid = _temporaryGeneralJournalAccountEntry.RecId;
       
         //Update Record of GL account Entry with Amount as Amount - Balance Amount
        select accountEntry where
            accountEntry.RecId  ==  _temporaryTransRecIdToTransRecId.lookup(refRecId);
        accountEntry.selectForUpdate(true);      
        accountEntry.TransactionCurrencyAmount  =   accountEntry.TransactionCurrencyAmount - postiveBal;
        accountEntry.AccountingCurrencyAmount   =   accountEntry.AccountingCurrencyAmount - postiveBal;
        accountEntry.ReportingCurrencyAmount    =   accountEntry.ReportingCurrencyAmount - postiveBal;
        accountEntry.update();
    }
    //Find Negative record where difference of Positive and negative is greater than record amount
    //Update the selected record with Difference amount
    //Record the Recid of selected record
    else
    {
        postiveBal = abs(negativebal)-abs(postiveBal);

        select RecId from _temporaryGeneralJournalAccountEntry
            where _temporaryGeneralJournalAccountEntry.AccountingCurrencyAmount < 0
                && _temporaryGeneralJournalAccountEntry.AccountingCurrencyAmount < -postiveBal;
        _temporaryGeneralJournalAccountEntry.AccountingCurrencyAmount   =  -postiveBal;
        _temporaryGeneralJournalAccountEntry.TransactionCurrencyAmount  =  -postiveBal;
        _temporaryGeneralJournalAccountEntry.ReportingCurrencyAmount    =  -postiveBal;
        _temporaryGeneralJournalAccountEntry.update();
        refRecid = _temporaryGeneralJournalAccountEntry.RecId;
       
        select accountEntry where
        accountEntry.RecId  ==  _temporaryTransRecIdToTransRecId.lookup(refRecId);
        accountEntry.selectForUpdate(true);
        accountEntry.TransactionCurrencyAmount  =   -postiveBal;
        accountEntry.AccountingCurrencyAmount   =   -postiveBal;
        accountEntry.ReportingCurrencyAmount    =   -postiveBal;
        accountEntry.update();
    }
    while select RecId from _temporaryGeneralJournalAccountEntry
    {
        ledgerTransSettlement.TransRecId = _temporaryTransRecIdToTransRecId.lookup(_temporaryGeneralJournalAccountEntry.RecId);

        settlementCollection.add(ledgerTransSettlement);
    }

    settlementCollection.insertDatabase();
    //Delete_From code to exclude recId which was selected/updated in code above
     delete_from _temporaryGeneralJournalAccountEntry
        where _temporaryGeneralJournalAccountEntry.RecId != refRecId;
 
    //Insert Record in to GL account Entry with Balance Amount
    select _temporaryGeneralJournalAccountEntry where _temporaryGeneralJournalAccountEntry.RecId == refRecId;
    accountEntry.data(_temporaryGeneralJournalAccountEntry);
    accountEntry.insert();
   
    ttscommit;
}


No comments:

Post a Comment