Tuesday, October 12, 2021

Auto Settle Vendor Invoice/Payment from same location In D365FO using X++.

 Hi guys, Today we see how to Auto Settle Vendor Invoice/Payment from the same location In D365FO using X++.


I have a requirement like first I have to fetch the invoice records of vendor and I have to settle that invoice with payment voucher which is from the same location. Example: INV001(from HYD) with PAY001(from Hyd). 

      VendTable VendTable;

VendTrans invVendTrans, payVendTrans; CustVendTransData custVendTransData; LedgerJournalTrans ledgerJournalTrans, payLedgerJournalTrans; CustVendOpenTransManager manager; VendTransOpen vendTransOpen; DimensionAttributeValueSetStorage invDimensionAttributeValueSetStorage ; DimensionAttribute invDimensionAttribute; DimensionValue dimensionValue; date payDate = mkDate(30,08,2021); ; #define.DimensionName("Location") // Find the oldest unsettled invoice while select invVendTrans order by TransDate asc where invVendTrans.AccountNum == VendAccount && invVendTrans.AmountMST <= 0 && //invVendTrans.TransType == LedgerTransType::Vend && invVendTrans.TransDate <= payDate && invVendTrans.TransType != LedgerTransType::Settlement && !invVendTrans.closed join ledgerJournaltrans where ledgerJournaltrans.Voucher == invVendTrans.Voucher && (ledgerJournalTrans.AccountType == LedgerJournalACType::Vend || ledgerJournalTrans.OffsetAccountType == LedgerJournalACType::Vend) { try { invDimensionAttributeValueSetStorage = dimensionAttributeValueSetStorage::find(ledgerJournaltrans.DefaultDimension); invDimensionAttribute = dimensionAttribute::findbyname(#DimensionName); dimensionValue = invDimensionAttributeValueSetStorage.getDisplayValueByDimensionAttribute(invDimensionAttribute.recId); if (!dimensionValue) { continue; } ttsBegin; VendTable = VendTable::find(invVendTrans.AccountNum); DimensionAttributeValueSetItem dimensionAttributeValueSetItem; DimensionAttributeValue dimensionAttributeValue; DimensionAttribute dimensionAttribute; // Find the oldest unsettled payment of same location while select firstonly payVendTrans order by TransDate asc where payVendTrans.AccountNum == vendTable.AccountNum && payVendTrans.AmountMST >= 0 && //payVendTrans.TransType == LedgerTransType::Payment && payVendTrans.TransDate <= payDate && payVendTrans.TransType != LedgerTransType::Settlement && !payVendTrans.closed join payLedgerJournalTrans where payLedgerJournalTrans.Voucher == payVendTrans.Voucher join dimensionAttributeValueSetItem where dimensionAttributeValueSetItem.DimensionAttributeValueSet == payLedgerJournalTrans.DefaultDimension || dimensionAttributeValueSetItem.DimensionAttributeValueSet == payLedgerJournalTrans.OffsetDefaultDimension // We can use DefaultDimension from vend Trans table also. //join dimensionAttributeValueSetItem // where dimensionAttributeValueSetItem.DimensionAttributeValueSet == payVendTrans.DefaultDimension join dimensionAttributeValue where dimensionAttributeValue.RecId == dimensionAttributeValueSetItem.DimensionAttributeValue && dimensionAttributeValue.DisplayValue == dimensionValue join dimensionAttribute where dimensionAttribute.RecId == dimensionAttributeValue.DimensionAttribute && dimensionAttribute.Name == #DimensionName join vendTransOpen where vendTransOpen.RefRecId == payVendTrans.RecId && vendTransOpen.AccountNum == payVendTrans.AccountNum { manager = custvendopentransmanager::construct(vendtable); manager.updatetransmarked(vendtransopen,true); // manager.updateSettleAmount(vendTransOpen, invVendTrans.AmountMST); // manager.settleMarkedTrans(); custVendTransData = CustVendTransData::construct(invVendTrans); custVendTransData.markForSettlement(VendTable); custVendTransData = CustVendTransData::construct(payVendTrans); custVendTransData.markForSettlement(VendTable); // Settle all marked transactions //VendTrans::settleTransact(VendTable, null, true, SettleDatePrinc::DaysDate, systemdateget()); VendTrans::settleTransact(VendTable, null, true, SettleDatePrinc::SelectDate, systemdateget()); invVendTrans.reread(); if (invVendTrans.Closed) { break; } } ttscommit; } catch (Exception::Error) { ttsAbort; continue; } catch (Exception::Deadlock) { ttsAbort; continue; } catch (Exception::Warning) { CLRInterop::getLastException(); continue; } catch (Exception::CLRError) { ttsAbort; CLRInterop::getLastException(); continue; } }

Keep daxing!!

No comments:

Post a Comment