vendor invoice creation in D365fo x++

vendor invoice creation in D365fo x++

public class DSAQMVendInvoiceUtill { public static void main(Args _args) { System.Exception ex; try { CustInvoiceTable CustInvoiceTable = _args.record(); if(!CustParameters::find().DSAQMJournalName) { throw Error("Journal name must be filled in AR parameters"); } if(CustInvoiceTable) { LedgerJournalTable LedgerJournalTable = DSAQMVendInvoiceUtill::createHeader(CustInvoiceTable); if(LedgerJournalTable) { DSAQMVendInvoiceUtill::createLines(CustInvoiceTable, LedgerJournalTable); } LedgerJournalCheckPost jourCheckPost = ledgerJournalCheckPost::newLedgerJournalTable(LedgerJournalTable, NoYes::Yes, NoYes::Yes); if (jourCheckPost.validate()) { jourCheckPost.run(); } info(strFmt("%1 created successfully!", LedgerJournalTable.JournalNum)); } } catch(ex) { } } public static LedgerJournalTable createHeader(CustInvoiceTable _custInvoiceTable) { LedgerJournalTable journalTable; JournalTableData journalTableData; LedgerJournalName ledgerJournalName = LedgerJournalName::find(CustParameters::find().DSAQMJournalName); journalTable.initFromLedgerJournalName(ledgerJournalName.JournalName); journalTableData = JournalTableData::newTable(journalTable); journalTable.JournalNum = journalTableData.nextJournalId(); journalTable.DSAQMInvoiceId = _custInvoiceTable.InvoiceId; journalTable.CurrencyCode = _custInvoiceTable.CurrencyCode; journalTable.Name = _custInvoiceTable.Description; journalTable.NameInEnglish = _custInvoiceTable.Description; journalTable.ALF_NameInArabic = _custInvoiceTable.ArabicDescription; journalTable.WorkflowApprovalStatus = LedgerJournalWFApprovalStatus::Approved; if (journalTable.validateWrite()) { journalTable.insert(); } return journalTable; } public static void createLines(CustInvoiceTable _custInvoiceTable, LedgerJournalTable _ledgerJournalTable) { LedgerJournalTrans journalTrans; CustInvoiceLine CustInvoiceLine; while select CustInvoiceLine where CustInvoiceLine.ParentRecId == _custInvoiceTable.RecId { DimensionAttributeValueCombination vendDim; select vendDim where vendDim.DisplayValue == _custInvoiceTable.DSAQMVendAccount; NumberSeq numberSeq = NumberSeq::newGetVoucherFromId(_ledgerJournalTable.NumberSequenceTable, true); journalTrans.JournalNum = _ledgerJournalTable.JournalNum; journalTrans.Voucher = numberSeq.voucher(); journalTrans.Invoice = _custInvoiceTable.InvoiceId; journalTrans.Txt = CustInvoiceLine.Description; journalTrans.ALF_TxtInArabic = CustInvoiceLine.ALF_DescriptionInArabic; journalTrans.DocumentDate = today(); journalTrans.TransDate = _custInvoiceTable.InvoiceDate; journalTrans.TransactionType = LedgerTransType::Vend; journalTrans.CurrencyCode = _custInvoiceTable.CurrencyCode; journalTrans.AmountCurDebit = abs(CustInvoiceLine.AmountCur); journalTrans.AmountCurCredit = 0.00; journalTrans.Payment = _custInvoiceTable.Payment; journalTrans.TaxItemGroup = _custInvoiceTable.DSAQMTaxItemGroup; journalTrans.TaxGroup = CustParameters::find().DSAQMTaxGroup; journalTrans.BankAccountId = _custInvoiceTable.ALF_CompanyBankAccountId; journalTrans.ExchRate = DSAQMVendInvoiceUtill::CurrentExchangeRate(Ledger::accountingCurrency(CompanyInfo::current()), journalTrans.CurrencyCode, journalTrans.TransDate); journalTrans.Approved = NoYes::Yes; journalTrans.Approver = HcmWorkerLookup::currentWorker(); journalTrans.AccountType = LedgerJournalACType::Vend; journalTrans.LedgerDimension = vendDim.RecId; journalTrans.OffsetAccountType = LedgerJournalACType::Ledger; journalTrans.OffsetLedgerDimension = LedgerDimensionFacade::serviceCreateLedgerDimension(CustInvoiceLine.LedgerDimension, CustInvoiceLine.DefaultDimension); journalTrans.DefaultDimension = CustInvoiceLine.DefaultDimension; journalTrans.OffsetDefaultDimension = CustInvoiceLine.DefaultDimension; journalTrans.insert(); numberSeq.used(); } } public static real CurrentExchangeRate(CurrencyCode fromCurrency, CurrencyCode toCurrency, TransDate transDate) { if(fromCurrency == toCurrency) { return 100; } ExchangeRateCurrencyPair exchangeRateCurrencyPair; select firstonly exchangeRateCurrencyPair where exchangeRateCurrencyPair.ExchangeRateType == Ledger::find(Ledger::current()).DefaultExchangeRateType && exchangeRateCurrencyPair.FromCurrencyCode == fromCurrency && exchangeRateCurrencyPair.ToCurrencyCode == toCurrency; return exchangeRate::findByDate(exchangeRateCurrencyPair.RecId, transDate).ExchangeRate; } }

Comments

Popular posts from this blog

D365FO – AX – X++ –Refresh, Reread, Research, and ExecuteQuery

Create Inventory Journal through Code in D365FO X++

SalesLine Reservation in D365fo x++