Friday, June 22, 2018

Create and Post Project Expense journal in Dynamics 365 finance and operation

class Job_CreatePostProjExpJournalLines
{      
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    { 
        LedgerJournalName ledgerJournalName;
        LedgerJournalTable ledgerJournalTable;
        LedgerJournalTrans ledgerJournalTrans;
        LedgerJournalTrans_Project ledgerJournalTrans_Project;
        NumberSequenceTable numberSequenceTable;
        ledgerJournalCheckPost ledgerJournalCheckPost;
        ProjLinePropertySetup projLinePropertySetup;
        DirPersonUser personUser;
        HcmWorker worker;
        boolean priceFound = false;
        CostPrice costPriceMST;
        ProjPostCostJournal projPostCostJournal;
        ResourceView resourceView;
        NumberSeq numberseq;
        Amount amount = 753.00;
        boolean result = false;
        TransDate invoiceDate = mkdate(22,06,2018);
        ttsbegin;
        ledgerJournalName = LedgerJournalName::find("PrjJrn");
        if(ledgerJournalName)
        {
            ledgerJournalTable.JournalName = ledgerJournalName.JournalName;
            ledgerJournalTable.initFromLedgerJournalName();
            ledgerJournalTable.JournalNum = JournalTableData::newTable(ledgerJournalTable).nextJournalId();
            ledgerJournalTable.Name = ledgerJournalName.Name + "Sangeet";
            ledgerJournalTable.JournalType = LedgerJournalType::Cost;
            ledgerJournalTable.insert();
            if(ledgerJournalTable)
            {
                select NumberSequence from numberSequenceTable
                    where NumberSequenceTable.RecId == ledgerJournalName.NumberSequenceTable;
                numberseq = NumberSeq::NewGetVoucherFromCode(numberSequenceTable.NumberSequence);
                ledgerJournalTrans.initValue();
                ledgerJournalTrans.JournalNum = ledgerJournalTable.JournalNum;
                ledgerJournalTrans.Voucher = numberseq.voucher();
                ledgerJournalTrans.TransDate = mkdate(22,06,2018);
                ledgerJournalTrans.Qty = 1;
                ledgerJournalTrans.AccountType = LedgerJournalACType::Project;
                ledgerJournalTrans.OffsetAccountType = LedgerJournalACType::Vend;
                ledgerJournalTrans.parmOffsetAccount("1011",ledgerJournalTrans.OffsetAccountType);
                ledgerJournalTrans.TransactionType = LedgerTransType::Payment;
                ledgerJournalTrans.CurrencyCode = "USD";
                ledgerJournalTrans.Invoice = "INV_0011";
                ledgerJournalTrans.Txt = strFmt("Expense Journal Auto Create");
                ledgerJournalTrans.Approved = NoYes::Yes;
                if(amount> 0)
                {
                    ledgerJournalTrans.AmountCurDebit = amount;
                }
                else
                {
                    ledgerJournalTrans.AmountCurCredit = abs(amount);
                }
                ledgerJournalTrans.Company = strUpr(curExt());
                ledgerJournalTrans.OffsetCompany = strUpr(curExt());
                select firstonly personUser where personUser.User == curUserId()
                    join worker
                    where worker.Person == personUser.PersonParty;
                ledgerJournalTrans.Approver = worker.RecId;
                ledgerJournalTrans.PostingProfile = 'GEN';
                ledgerJournalTrans.parmAccount("000004",ledgerJournalTrans.AccountType);
                ledgerJournalTrans.insert();
                if(ledgerJournalTrans)
                {
                    ledgerJournalTrans_Project.initValue();
                    ledgerJournalTrans_Project.CategoryId = "Cat1";
                    ledgerJournalTrans_Project.Qty = 1;
                    ledgerJournalTrans_Project.SalesCurrencyId = ledgerJournalTrans.CurrencyCode;
                    ledgerJournalTrans_Project.LinePropertyId = "Billable";
                    ledgerJournalTrans_Project.ProjTransDate = invoiceDate;
                    select resourceView
                        where resourceView.ResourceCompanyId == curExt();
                    ledgerJournalTrans_Project.Resource = resourceView.RecId;
                    ledgerJournalTrans_Project.ProjId = "000004";
                    ledgerJournalTrans_Project.RefRecId = ledgerJournalTrans.RecId;
                    ledgerJournalTrans_Project.Worker = ledgerJournalTrans.Approver;
                    ledgerJournalTrans_Project.CostPrice = amount;
                    costPriceMST = ProjCostSalesPrice::costPrice(ledgerJournalTrans, ledgerJournalTrans_Project, false);
                    [ledgerJournalTrans_Project.SalesPrice, priceFound] = ProjCostSalesPrice::findCostSalesPrice(ledgerJournalTrans_Project.ProjId,
                        ledgerJournalTrans_Project.Resource,
                        ledgerJournalTrans_Project.CategoryId,
                        costPriceMST,
                        CompanyInfoHelper::standardCurrency(),
                        ledgerJournalTrans_Project.SalesCurrencyId,
                        ledgerJournalTrans_Project.ProjTransDate,
                        ledgerJournalTrans_Project.ProjPriceGroupID,
                        false);
                    ledgerJournalTrans_Project.insert();
                    result = true;
                }
            }
        }
        ttscommit;
        if(result == true)
        {
            ledgerJournalCheckPost = ledgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable,NoYes::Yes);
            ledgerJournalCheckPost.run();
            info(Strfmt("Project Expense journal posted successfully %1 in %2", ledgerJournalTable.JournalNum,curext()));
        }
    }


}

No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...