Payment creation util in D365fo x++
Payment creation util in D365fo x++
Public class DSPMPaymentCreationUtil
{
public static void main(Args _args)
{
DSPMRentReservation DSPMRentReservation = _args.record();
DSPMRentReservationLine DSPMRentReservationLine;
CustTable CustTable = CustTable::find(DSPMRentReservation.CustAccount);
if(!CustTable)
{throw error("Customer not found!");}
select count(Property)
from DSPMRentReservationLine
group by DSPMRentReservationLine.Property
where DSPMRentReservationLine.ReservationId == DSPMRentReservation.ReservationId
&& DSPMRentReservationLine.PaymentStatus == DSPMPaymentStatus::None;
if(DSPMRentReservationLine.Property)
{
DSPMRentReservationLine.clear();
ledgerJournalTable ledgerJournalTable = DSPMPaymentCreationUtil::createLedgerJournal(DSPMRentReservation.ReservationId);
if(ledgerJournalTable)
{
while select ReservationId, Property, sum(PropertyPrice)
from DSPMRentReservationLine
group by DSPMRentReservationLine.ReservationId, DSPMRentReservationLine.Property
where DSPMRentReservationLine.ReservationId == DSPMRentReservation.ReservationId
&& DSPMRentReservationLine.PaymentStatus == DSPMPaymentStatus::None
{
DSPMPaymentCreationUtil::createLedgerJournalLines(ledgerJournalTable, CustTable, DSPMRentReservationLine.PropertyPrice, DSPMRentReservation.ReservationId, DSPMRentReservationLine.Property);
}
ledgerJournalTable.reread();
if(ledgerJournalTable.Posted)
{DSPMPaymentCreationUtil::updatePaymentStatus(ledgerJournalTable.JournalNum);}
}
DSPMUtil::refreshForm(_args.caller());
}
else{warning(strFmt('unpaid line(s) not found!'));}
}
public static ledgerJournalTable createLedgerJournal(DocumentNum _DocumentNum)
{
LedgerJournalTable ledgerJournalTable;
LedgerJournalName ledgerJournalName;
try
{
DSPMPropertyParameters propertyParameters = DSPMPropertyParameters::find();
if(propertyParameters.JournalName == '')
{throw Error("Jounral name is not defined in property parameters");}
select firstonly1 ledgerJournalName where ledgerJournalName.JournalName == propertyParameters.JournalName;
ttsbegin;
ledgerJournalTable.clear();
ledgerJournalTable.JournalName = ledgerJournalName.JournalName;
ledgerJournalTable.initFromLedgerJournalName();
ledgerJournalTable.JournalNum = JournalTableData::newTable(LedgerjournalTable).nextJournalId();
ledgerJournalTable.DocumentNum = _DocumentNum;
ledgerJournalTable.insert();
ttscommit;
}
catch
{
error(infolog.text(infologLine()));
error(strFmt("Journal %1 creation has been failed.", ledgerJournalTable.JournalNum));
}
return ledgerJournalTable;
}
public static ledgerJournalTable createLedgerJournalLines(ledgerJournalTable ledgerJournalTable, CustTable CustTable, Amount _creditAmt, DocumentNum _DocumentNum, Refrecid _property)
{
LedgerJournalTrans ledgerJournalTrans;
LedgerJournalName ledgerJournalName;
NumberSeq numberseq;
Voucher voucher;
LedgerJournalCheckPost journalCheckPost;
try
{
ttsbegin;
if (ledgerJournalTable)
{
DSPMPropertyParameters propertyParameters = DSPMPropertyParameters::find();
select firstonly1 ledgerJournalName where ledgerJournalName.JournalName == propertyParameters.JournalName;
// Line
ledgerJournalTrans.clear();
numberSeq = NumberSeq::newGetVoucherFromId((ledgerjournalname.NumberSequenceTable));
ledgerJournalTrans.JournalNum = ledgerJournalTable.JournalNum;
ledgerJournalTrans.Voucher = numberSeq.voucher();
ledgerJournalTrans.LineNum = LedgerJournalTrans::lastLineNum(ledgerJournalTrans.JournalNum);
ledgerJournalTrans.TransDate = today();
LedgerjournalTrans.AccountType = LedgerJournalACType::Cust;
LedgerjournalTrans.parmAccount(CustTable.AccountNum, LedgerjournalTrans.AccountType);
ledgerJournalTrans.CurrencyCode = CustTable.Currency;
ledgerJournalTrans.ExchRate = ExchangeRateHelper::exchRate(CustTable.Currency);
LedgerjournalTrans.DefaultDimension = CustTable.DefaultDimension;
ledgerJournalTrans.AmountCurCredit = _creditAmt;
ledgerJournalTrans.PaymReference = _DocumentNum;
ledgerJournalTrans.DocumentNum = _DocumentNum;
LedgerjournalTrans.OffsetAccountType = propertyParameters.OffsetAccountType;
LedgerjournalTrans.OffsetLedgerDimension = propertyParameters.OffsetLedgerDimension;
LedgerjournalTrans.DSPMProperty = _property;
ledgerJournalTrans.insert();
journalCheckPost = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable, NoYes::No);
boolean SimulatePosting = journalCheckPost.parmSimulatePosting(true);
boolean validate = journalCheckPost.validate();
journalCheckPost.run();
ttscommit;
if (ledgerJournalTrans)
{
info(strFmt("Journal %1 has been created.", ledgerJournalTable.JournalNum));
if (SimulatePosting && validate)
{DSPMPaymentCreationUtil::postLedgerJournal(ledgerJournalTable);}
else
{strFmt("Auto Posting for Journal %1 failed!", 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 ledgerJournalTable;
}
public static void postLedgerJournal(LedgerJournalTable ledgerJournalTable)
{
LedgerJournalCheckPost journalCheckPost;
try
{
if (ledgerJournalTable)
{
//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 updatePaymentStatus(LedgerJournalId _LedgerJournalId)
{
ledgerJournalTrans ledgerJournalTrans;
DSPMRentReservationLine DSPMRentReservationLine;
ttsbegin;
update_recordset DSPMRentReservationLine
setting PaymentStatus = DSPMPaymentStatus::Posted
join ledgerJournalTrans
where ledgerJournalTrans.DSPMProperty == DSPMRentReservationLine.Property
&& ledgerJournalTrans.PaymReference == DSPMRentReservationLine.ReservationId
&& ledgerJournalTrans.JournalNum == _LedgerJournalId;
ttscommit;
}
}
Comments
Post a Comment