Customer Journal Creation And Settlement in d365fo x++

 Customer Journal Creation And Settlement in d365fo x++

Code: 

class DSACustJournalCreationAndSettlement { public static ledgerJournalTable createLedgerJournal(SalesId _saleId, TaxGroup _taxGroup, TaxItemGroup _taxItemGroup, CurrencyCode _currency, SalesCategory _category, ParmId _parmId) { InventPosting inventPosting; LedgerJournalTrans ledgerJournalTrans; LedgerJournalTable ledgerJournalTable; LedgerJournalName ledgerJournalName; NumberSeq numberseq; Voucher voucher; CustParameters custParameters; SalesTable salesTable; LedgerJournalId ledgerJournalId; LedgerJournalCheckPost journalCheckPost; try { ledgerJournalId = ''; select firstonly1 salesTable where salesTable.SalesId == _saleId; select firstonly1 LedgerDimension, RecId, InventAccountType, CategoryRelation from inventPosting where inventPosting.InventAccountType == InventAccountType::SalesRevenue && inventPosting.CategoryRelation == _category; if(inventPosting.LedgerDimension == 0) { throw Error(strFmt("Offset account is not defiend for '%1' category",_category)); } select firstonly1 custParameters; if(custParameters.DSAJournalName == '') { throw Error("Jounral name is not defined in parameters"); } select firstonly1 ledgerJournalName where ledgerJournalName.JournalName == custParameters.DSAJournalName; ttsbegin; // Header ledgerJournalTable.clear(); ledgerJournalTable.JournalName = ledgerJournalName.JournalName; ledgerJournalTable.initFromLedgerJournalName(); ledgerJournalTable.JournalNum = JournalTableData::newTable(LedgerjournalTable).nextJournalId(); ledgerJournalTable.ALF_NameInArabic = salesTable.SalesId + " - " + custParameters.DSAJournalArabicDescription; ledgerJournalTable.NameInEnglish = salesTable.SalesId + " - " + custParameters.DSAJournalDescription; ledgerJournalTable.DocumentNum = _parmId; ledgerJournalTable.insert(); ttscommit; } catch { error(infolog.text(infologLine())); error(strFmt("Journal %1 creation has been failed.", ledgerJournalTable.JournalNum)); } return ledgerJournalTable; } public static LedgerJournalId createLedgerJournalLines(ledgerJournalTable ledgerJournalTable, SalesId _saleId, Amount _creditAmt, TaxGroup _taxGroup, TaxItemGroup _taxItemGroup, CurrencyCode _currency, Description1000 _invoice, SalesCategory _category, ParmId _parmId) { InventPosting inventPosting; LedgerJournalTrans ledgerJournalTrans; LedgerJournalName ledgerJournalName; NumberSeq numberseq; Voucher voucher; CustParameters custParameters; SalesTable salesTable; LedgerJournalId ledgerJournalId; LedgerJournalCheckPost journalCheckPost; try { ledgerJournalId = ''; select firstonly1 salesTable where salesTable.SalesId == _saleId; select firstonly1 LedgerDimension, RecId, InventAccountType, CategoryRelation from inventPosting where inventPosting.InventAccountType == InventAccountType::SalesRevenue && inventPosting.CategoryRelation == _category; if(inventPosting.LedgerDimension == 0) { throw Error(strFmt("Offset account is not defiend for '%1' category",_category)); } select firstonly1 custParameters; if(custParameters.DSAJournalName == '') { throw Error("Jounral name is not defined in parameters"); } select firstonly1 ledgerJournalName where ledgerJournalName.JournalName == custParameters.DSAJournalName; ttsbegin; if (ledgerJournalTable.RecId) { // Line ledgerJournalTrans.clear(); ledgerJournalTrans.JournalNum = ledgerJournalTable.JournalNum; ledgerJournalTrans.TransDate = today(); ledgerJournalTrans.LineNum = LedgerJournalTrans::lastLineNum(ledgerJournalTrans.JournalNum); ledgerJournalTrans.ALF_TxtInArabic = salesTable.SalesId + " - " + custParameters.DSAJournalArabicDescription; ledgerJournalTrans.Txt = salesTable.SalesId + " - " + custParameters.DSAJournalDescription; //voucher = new JournalVoucherNum(JournalTableData::newTable(ledgerJournalTable)).getNew(false); //ledgerJournalTrans.Voucher = voucher; numberSeq = NumberSeq::newGetVoucherFromId((ledgerjournalname.NumberSequenceTable)); ledgerJournalTrans.Voucher = numberSeq.voucher(); ledgerJournalTrans.CurrencyCode = _currency; ledgerJournalTrans.AmountCurCredit = _creditAmt; ledgerJournalTrans.AccountType = LedgerJournalACType::Cust; ledgerJournalTrans.LedgerDimension =  LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(salesTable.CustAccount,LedgerJournalACType::Cust); ledgerJournalTrans.DefaultDimension = salesTable.DefaultDimension; ledgerJournalTrans.OffsetAccountType = LedgerJournalACType::Ledger; ledgerJournalTrans.OffsetLedgerDimension  = LedgerDimensionFacade::serviceCreateLedgerDimension(inventPosting.LedgerDimension, salesTable.DefaultDimension); ledgerJournalTrans.modifiedField(fieldNum(LedgerJournalTrans, OffsetLedgerDimension)); ledgerJournalTrans.TaxGroup = _taxGroup; ledgerJournalTrans.TaxItemGroup = _taxItemGroup; ledgerJournalTrans.ExchRate = 100; ledgerJournalTrans.TransactionType = LedgerTransType::GeneralJournal; ledgerJournalTrans.Prepayment = NoYes::No; ledgerJournalTrans.PaymReference = _invoice; ledgerJournalTrans.PaymMode = ''; ledgerJournalTrans.DocumentNum = _parmId; ledgerJournalTrans.insert(); journalCheckPost = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable, NoYes::No); journalCheckPost.parmSimulatePosting(true); journalCheckPost.validate(); journalCheckPost.run(); ttscommit; if (ledgerJournalTrans.RecId) { ledgerJournalId = ledgerJournalTable.JournalNum; ledgerJournalTrans.selectForUpdate(true); ttsbegin; ledgerJournalTrans.Prepayment = NoYes::No; ledgerJournalTrans.update(); ttscommit; info(strFmt("Journal %1 has been created.", ledgerJournalTable.JournalNum)); } else { warning(strFmt("Journal %1 has not been created.", ledgerJournalTable.JournalNum)); } } else { warning(strFmt("Journal %1 has not been created.", ledgerJournalTable.JournalNum)); } } catch { error(infolog.text(infologLine())); error(strFmt("Journal %1 creation has been failed.", ledgerJournalTable.JournalNum)); } return ledgerJournalId; } public static void postLedgerJournal(LedgerJournalId _jounralId, InvoiceId _invoice ) { LedgerJournalCheckPost journalCheckPost; LedgerJournalTable ledgerJournalTable = ledgerJournalTable::find(_jounralId); LedgerJournalTrans ledgerJournalTrans; try { if (ledgerJournalTable) { // update invoice ref if(_invoice != '') { select firstonly forupdate ledgerJournalTrans where ledgerJournalTrans.JournalNum == _jounralId; if(ledgerJournalTrans) { ttsbegin; ledgerJournalTrans.DocumentNum = _invoice; ledgerJournalTrans.update(); ttscommit; } } //posting ttsbegin; journalCheckPost = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable, NoYes::Yes); journalCheckPost.run(); ttscommit; ledgerJournalTable.reread(); if (ledgerJournalTable.Posted == NoYes::Yes) { info(strFmt("Journal %1 has been posted.", ledgerJournalTable.JournalNum)); } else { warning(strFmt("Journal %1 has not been posted.", ledgerJournalTable.JournalNum)); } } else { warning(strFmt("Journal %1 has not been found.", ledgerJournalTable.JournalNum)); } } catch { error(infolog.text(infologLine())); error(strFmt("Journal %1 posting has been failed.", ledgerJournalTable.JournalNum)); } } public static void deleteLedgerJournal(LedgerJournalId _jounralId) { LedgerJournalTable ledgerJournalTable; delete_from ledgerJournalTable where ledgerJournalTable.JournalNum == _jounralId; } public static void settleTransactions(SalesId _salesId, InvoiceId _invoice, LedgerJournalId _jounralId) { LedgerJournalTrans paymLedgerJournalTransLoc, ledgerJournalTrans; CustTransOpen invCustTransOpen, paymCustTransOpen; CustTrans invCustTrans, paymCustTrans; CustInvoiceJour custInvoiceJour; try { select firstonly1 custInvoiceJour where custInvoiceJour.InvoiceId == _invoice && custInvoiceJour.SalesId == _salesId; select firstonly1 ledgerJournalTrans where ledgerJournalTrans.JournalNum == _jounralId; // Given the payment LedgerJournalTrans, find the related CustTrans which has CustTransOpen select firstonly paymLedgerJournalTransLoc where paymLedgerJournalTransLoc.RecId == ledgerJournalTrans.RecId join paymCustTrans where paymCustTrans.Voucher == paymLedgerJournalTransLoc.Voucher && paymCustTrans.AccountNum == custInvoiceJour.InvoiceAccount && paymCustTrans.RecId == paymLedgerJournalTransLoc.CustTransId && paymCustTrans.TransType == LedgerTransType::GeneralJournal join paymCustTransOpen where paymCustTransOpen.RefRecId == paymCustTrans.RecId; // Find the related invoice to be settled against the payment journal in the query above select firstonly invCustTrans where invCustTrans.AccountNum == custInvoiceJour.InvoiceAccount && invCustTrans.Voucher == custInvoiceJour.LedgerVoucher && invCustTrans.TransType == LedgerTransType::Sales join invCustTransOpen where invCustTransOpen.RefRecId == invCustTrans.RecId; // Settlement CustTable custTable = CustTable::find(custInvoiceJour.InvoiceAccount); SpecTransExecutionContext specTransExecutionContext = SpecTransExecutionContext::newFromSource(custTable); SpecTransManager specTransManager = SpecTransManager::newFromSpec(specTransExecutionContext.parmSpecContext()); Amount settleAmount = paymCustTransOpen.AmountCur; //info(strFmt("invoice amt %1, journal amt %2",invCustTransOpen.AmountCur,paymCustTransOpen.AmountCur)); if(settleAmount < 0) { settleAmount = -paymCustTransOpen.AmountCur; } if(settleAmount > invCustTransOpen.AmountCur) { settleAmount = invCustTransOpen.AmountCur; } //Payment specTransManager.insert(paymCustTransOpen.DataAreaId, paymCustTransOpen.TableId, paymCustTransOpen.RecId, -settleAmount, paymCustTrans.CurrencyCode); //Invoice specTransManager.insert(invCustTransOpen.DataAreaId, invCustTransOpen.TableId, invCustTransOpen.RecId, settleAmount, invCustTrans.CurrencyCode); //Settle if(CustTrans::settleTransaction(specTransExecutionContext, CustTransSettleTransactionParameters::construct())) { info(strFmt("Settlement completed for Invoice %1, Journal reversal payment %2, Amount %3", _invoice, _jounralId, settleAmount)); } } catch { error(infolog.text(infologLine())); error("Settlement failed."); } } public static void settleInvoices(CustAccount _customer, InvoiceId _oldInvoice, InvoiceId _newInvoice) { CustTable custTable; CustTransOpen invCustTransOpen, payCustTransOpen; CustTrans invCustTrans, payCustTrans; custTable = CustTable::find(_customer); try { //for invoice select firstonly invCustTrans where invCustTrans.AccountNum == custTable.AccountNum && invCustTrans.TransType == LedgerTransType::Sales && invCustTrans.Invoice == _oldInvoice join invCustTransOpen where invCustTransOpen.RefRecId == invCustTrans.RecId; //for payment select firstonly payCustTrans where payCustTrans.AccountNum == custTable.AccountNum && payCustTrans.TransType == LedgerTransType::Sales && payCustTrans.Invoice == _newInvoice join payCustTransOpen where payCustTransOpen.RefRecId == payCustTrans.RecId; //ttsbegin; //Mark for settlement SpecTransExecutionContext specTransExecutionContext = SpecTransExecutionContext::newFromSource(custTable); SpecTransManager specTransManager = SpecTransManager::newFromSpec(specTransExecutionContext.parmSpecContext()); Amount settleAmount = payCustTrans.AmountCur; if(settleAmount < 0) { settleAmount = -1 * payCustTrans.AmountCur; } if(settleAmount > invCustTrans.AmountCur) { settleAmount = invCustTrans.AmountCur; } specTransManager.insert(invCustTransOpen.dataAreaId, invCustTransOpen.TableId, invCustTransOpen.RecId, settleAmount, invCustTrans.CurrencyCode); specTransManager.insert(payCustTransOpen.dataAreaId, payCustTransOpen.TableId, payCustTransOpen.RecId, -settleAmount, payCustTrans.CurrencyCode); //ttscommit; if(CustTrans::settleTransaction(specTransExecutionContext, CustTransSettleTransactionParameters::construct())) { info(strFmt("Settlement completed for Invoice %1, against %2, Amount %3", invCustTrans.Invoice, payCustTrans.Invoice, settleAmount)); } } catch { error(infolog.text(infologLine())); error("Settlement failed."); } } public static void settleInvoices_DEL(CustAccount _customer, InvoiceId _oldInvoice, InvoiceId _newInvoice) { CustTable custTable; CustTrans invCustTrans, payCustTrans; SpecTransManager manager; CustVendTransData custVendTransData; custTable = CustTable::find(_customer); try { select firstonly invCustTrans where invCustTrans.AccountNum == custTable.AccountNum && invCustTrans.Invoice == _oldInvoice && invCustTrans.TransType == LedgerTransType::Sales; select firstonly payCustTrans where payCustTrans.AccountNum == custTable.AccountNum && payCustTrans.Invoice == _newInvoice && payCustTrans.TransType == LedgerTransType::Sales; ttsbegin; custVendTransData = CustVendTransData::construct(invCustTrans); custVendTransData.markForSettlement(CustTable); custVendTransData = CustVendTransData::construct(payCustTrans); custVendTransData.markForSettlement(CustTable); ttscommit; if(CustTrans::settleTransact(custTable, null, true, SettleDatePrinc::DaysDate, systemdateget())) { info('Invoices settled.'); } } catch { error(infolog.text(infologLine())); error("Settlement failed."); } } }

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++