After creating a payment journal (http://axwonders.blogspot.com/2011/06/create-axapta-payment-journal.html ), you might want to mark the invoices for settlement. This task is done by looping through the just created Payment Journal and then by querying the CustTransOpen Table and instantiating the CustTrans table from the CustTransOpen.CustTrans() method.
The following is the code to mark invoices. Also, the code loops through each payment journal transaction.
Also, the logic behind it is that the SpecTrans table needs to have two records in order to offset an open invoice with a transaction. So the code below inserts two records to the SpecTrans table. One is for all the open transactions (invoices - Sales) and the other is for all the payment transactions (paymet - Payment)
For example:
select firstonly invCustTrans where invCustTrans.AccountNum == custTable.AccountNum
&& invCustTrans.TransType == LedgerTransType::Sales
&& !invCustTrans.LastSettleDate;
the code above will select all the customer transactions with a TransType of Sales.
On the other hand, the code below will select all the customer transaction with a TransType of Payment.
select firstonly payCustTrans where payCustTrans.AccountNum == custTable.AccountNum
&& payCustTrans.TransType == LedgerTransType::Payment
&& !payCustTrans.LastSettleDate;
Then we need to insert the two instances of CustTrans into the SpecTrans table:
specOffsetVoucher.insert(invCustTrans.dataAreaId, invCustTrans.TableId, invCustTrans.RecId, invCustTrans.AmountCur, invCustTrans.CurrencyCode, NoYes::No);
specOffsetVoucher.insert(payCustTrans.dataAreaId, payCustTrans.TableId, payCustTrans.RecId, payCustTrans.AmountCur, payCustTrans.CurrencyCode, true);
The following is the whole code: