Thursday 20 February 2020

Create and post General Journal

Create and post General Journal

public void run()
{
    Name                        journalName;
    AmountMST                   expenseAssesableValue;

    ProjParameters              projParameters;
    ALE_PayrollRun              payrollRun,payrollRunGroup;
    ALE_PayrollCodeMappingTable payrollCodeMappingTable;
    ALE_ProjAllocationDetails   projAllocationDetails,projAllocationDetailsUpdate;
    DimensionAttributeValueCombination  ledgerDimensionvalueCombination,davc;

    AxLedgerJournalTable        journalTable; // class
    AxLedgerJournalTrans        journalTrans; // class
    LedgerJournalTrans          ledgerJournalTrans;
    LedgerJournalTrans_Project  ledgerJournalTrans_Project;

    LedgerJournalTransTaxExtensionIN    ledgerJournalTransTaxExtensionIN;
    LedgerJournalTrans_ProjectTaxExtensionIN    ledgerJournalTrans_ProjectTaxExtensionIN;
    RefRecId                    recId;
    DimensionDefault            workerDim,mergedDim;
    DimensionDefault            projDim;
    LedgerDimensionAccount      offsetLD;
    DefaultDimensionView        dimView;
    Ledger                      ledger;

    ledgerJournalCheckPost      ledgerJournalCheckPost;// table

    select JournalNameIdRevenue from projParameters;
    ttsBegin;
    try
    {
        while select projAllocationDetails group by ProjId,EmployeeId,MonthYear where projAllocationDetails.Status == '0'
        {
            while select payrollRunGroup group by EmployeeId,MonthYear where payrollRunGroup.EmployeeId == projAllocationDetails.EmployeeId
            {
                //delete_from reveueAllocationFeeTmp;
                journalTable = new AxLedgerJournalTable();
                journalTrans = new AxLedgerJournalTrans();
                //Journal Name
                journalTable.parmJournalName(projParameters.JournalNameIdRevenue);//"Genjur");
                journalName = LedgerJournalName::find(projParameters.JournalNameIdRevenue).Name + '-' + projAllocationDetails.ProjId + ' - ' + strFmt("%1",projAllocationDetails.Projdate);
                journalTable.parmName(journalName);

                journalTable.save();

                select AccountingCurrency from ledger where ledger.Name == curext();
                while select EmployeeId,ProjId,Projdate FROM projAllocationDetailsUpdate
                    join Amount,BatchType,PayrollCode from payrollRun where projAllocationDetailsUpdate.EmployeeId == projAllocationDetails.EmployeeId
                    && projAllocationDetailsUpdate.Status == '0' && projAllocationDetailsUpdate.ProjId == projAllocationDetails.ProjId
                    && payrollRun.EmployeeId == projAllocationDetailsUpdate.EmployeeId
                    && payrollRun.MonthYear == payrollRunGroup.MonthYear
                {
                    ledgerJournalTrans.clear();
                    journalTrans.parmJournalNum(journalTable.ledgerJournalTable().JournalNum);
                    journalTrans.parmTransDate(projAllocationDetailsUpdate.Projdate);
                    journalTrans.parmCurrencyCode(ledger.AccountingCurrency);


                    journalTrans.parmAccountType(LedgerJournalACType::Project);

                    select ledgerDimensionvalueCombination where ledgerDimensionvalueCombination.DisplayValue == projAllocationDetailsUpdate.ProjId;

                    if(ledgerDimensionvalueCombination.recid != 0)
                    {
                        journalTrans.parmLedgerDimension(ledgerDimensionvalueCombination.recid);
                    }
                    else
                    {
                        recId = DimensionStorage::getDynamicAccount( projAllocationDetailsUpdate.ProjId, 3, 129);

                        davc = DimensionAttributeValueCombination::find(recId);

                        journalTrans.parmLedgerDimension(davc.RecId);

                    }
                    //ledgerDim = DimensionDefaultingService::serviceCreateLedgerDimension( projDim);
                    //journalTrans.parmLedgerDimension(ledgerDim);
                    select DefaultDimension from dimView where dimView.DisplayValue == projAllocationDetailsUpdate.EmployeeId;
                    workerDim = dimView.DefaultDimension;
                   
                    projDim = ProjTable::find(projAllocationDetailsUpdate.ProjId).DefaultDimension;
                   
                    mergedDim      = DimensionDefaultingService::serviceMergeDefaultDimensions(projDim,workerDim);
                   
                    journalTrans.parmDefaultDimension(mergedDim);

                    if(ALE_PayrollCodeMappingTable::findByPayrollCode(payrollRun.PayrollCode).ALE_PayrollCodeType == 0 )
                    {
                        journalTrans.parmAmountCurDebit(abs(payrollRun.Amount));
                    }
                    else
                    {
                        journalTrans.parmAmountCurCredit(abs(payrollRun.Amount));
                    }


                    select AccountCode,CategoryId FROM  payrollCodeMappingTable where payrollCodeMappingTable.PayrollCode == payrollRun.PayrollCode;

                    ledgerDimensionvalueCombination.clear();
                    select ledgerDimensionvalueCombination where ledgerDimensionvalueCombination.MainAccount == MainAccount::findByMainAccountId( payrollCodeMappingTable.AccountCode).RecId;
                   
                    offsetLD = DimensionDefaultingService::serviceCreateLedgerDimension( ledgerDimensionvalueCombination.RecId,projDim,workerDim);


                    journalTrans.parmOffsetLedgerDimension(offsetLD);
                    journalTrans.parmOffsetAccountType(LedgerJournalACType::Ledger );

                    journalTrans.save();

                    ledgerJournalTrans = journalTrans.ledgerJournalTrans();
                    ledgerJournalTransTaxExtensionIN.LedgerJournalTrans = ledgerJournalTrans.RecId;
                    ledgerJournalTransTaxExtensionIN.insert();
                    if(ledgerJournalTrans)
                    {

                        ledgerJournalTrans_Project.ProjTransDate = systemDateGet();
                        if(ledgerJournalTrans.AmountCurDebit > 0)
                        {
                            ledgerJournalTrans_Project.CostPrice = ledgerJournalTrans.AmountCurDebit;
                            ledgerJournalTrans_Project.Qty = 1;
                        }
                        else
                        {
                            ledgerJournalTrans_Project.CostPrice = ledgerJournalTrans.AmountCurCredit;
                            ledgerJournalTrans_Project.Qty = -1;
                        }

                        ledgerJournalTrans_Project.SalesCurrencyId = ledgerJournalTrans.CurrencyCode;


                        ledgerJournalTrans_Project.LinePropertyId = "Billable";


                        ledgerJournalTrans_Project.ProjId = projAllocationDetailsUpdate.ProjId;

                        ledgerJournalTrans_Project.RefRecId = ledgerJournalTrans.RecId;
                        ledgerJournalTrans_Project.CategoryId = payrollCodeMappingTable.CategoryId;



                        ledgerJournalTrans_Project.Worker = HcmWorker::findByPersonnelNumber(projAllocationDetailsUpdate.EmployeeId).RecId;

                        ledgerJournalTrans_Project.insert();

                        ledgerJournalTrans_ProjectTaxExtensionIN.LedgerJournalTrans_Project = ledgerJournalTrans_Project.RecId;
                        expenseAssesableValue  = ledgerJournalTrans_Project.Qty * ledgerJournalTrans_Project.SalesPrice;
                        ledgerJournalTrans_ProjectTaxExtensionIN.AssessableValue = expenseAssesableValue;

                        ledgerJournalTrans_ProjectTaxExtensionIN.insert();

                        ledgerJournalTrans_Project.clear();


                    }

                }
                if(ledgerJournalTrans.RecId)
                {
                    ledgerJournalCheckPost = ledgerJournalCheckPost::newLedgerJournalTable(journalTable.ledgerJournalTable(),NoYes::Yes);
                    ledgerJournalCheckPost.run();
                }
                info(strFmt("%1",ledgerJournalTrans.JournalNum));
            }

        }

    }
    catch(Exception::Error)
    {

        error('An Exception has occurred');


    }
    ttsCommit;
}