Hey André, this is the entire code i wrote.
I created two containers, one for the PackingSlipJour(conPS) an the other for the matching Invoices(conInv), then, in the second method I selected for each element in the container the GeneralJournalAccountEntry and put them all in one container(conGJAE) then as a last step I settled all GJAEs in the container.
public void run()
{
while select _generalJournalAccountEntry order by LedgerAccount desc
where _generalJournalAccountEntry.PostingType == LedgerPostingType::PurchPckSlpPurchaseOffsetAccount
/*notExists join ledgerTransSettlement
where _generalJournalAccountEntry.RecId == ledgerTransSettlement.TransRecId*/
{
if(!LedgerTransSettlement::findTransRecId(_generalJournalAccountEntry.RecId))
{
select _vendPackingSlipJour
join _generalJournalEntry
where _generalJournalEntry.RecId == _generalJournalAccountEntry.GeneralJournalEntry
join subledgerVoucherGeneralJournalEntry
join _vendPackingSlipVersion
where _vendPackingSlipVersion.VendPackingSlipJour == _vendPackingSlipJour.RecId
&& ( _vendPackingSlipVersion.LedgerVoucher == subledgerVoucherGeneralJournalEntry.Voucher
|| subledgerVoucherGeneralJournalEntry.Voucher == _vendPackingSlipVersion.CostLedgerVoucher )
&& _vendPackingSlipVersion.AccountingDate == subledgerVoucherGeneralJournalEntry.AccountingDate
&& subledgerVoucherGeneralJournalEntry.GeneralJournalEntry == _generalJournalEntry.RecId;
if(_vendPackingSlipJour)
{
conPS+=_vendPackingSlipJour.RecId;
for(k=1;k<=conLen(conPS);k++)
{
_vendPackingSlipJour=VendPackingSlipJour::findRecId(conPeek(conPS,k));
while select _vendPackingSlipTrans
where _vendPackingSlipJour.RecId == _vendPackingSlipTrans.VendPackingSlipJour
join _vendInvoiceTrans
join PackingSlipSourceDocumentLine,InvoiceSourceDocumentLIne from _vendInvoicePackingSlipQuantityMatch
where _vendPackingSlipTrans.SourceDocumentLine == _vendInvoicePackingSlipQuantityMatch.PackingSlipSourceDocumentLine
&& _vendInvoicePackingSlipQuantityMatch.InvoiceSourceDocumentLIne == _vendInvoiceTrans.SourceDocumentLine
join _vendInvoiceJour
where _vendInvoiceJour.InvoiceAccount == _vendPackingSlipJour.InvoiceAccount
&& _vendInvoiceTrans.PurchId == _vendInvoiceJour.PurchId
&& _vendInvoiceTrans.InvoiceId == _vendInvoiceJour.InvoiceId
&& _vendInvoiceTrans.InvoiceDate == _vendInvoiceJour.InvoiceDate
&& _vendInvoiceTrans.numberSequenceGroup == _vendInvoiceJour.numberSequenceGroup
&& _vendInvoiceTrans.InternalInvoiceId == _vendInvoiceJour.InternalInvoiceId
{
if(!conFind(conInv,_vendInvoiceJour.RecId))
{
conInv+=_vendInvoiceJour.RecId;
}
}
}
for (k=1;k<=conLen(conInv);k++)
{
_vendInvoiceJour=VendInvoiceJour::findRecId(conPeek(conInv,k));
while select _vendPackingSlipTrans
join _vendInvoiceTrans
where _vendInvoiceTrans.PurchId == _vendInvoiceJour.PurchId
&& _vendInvoiceTrans.InvoiceId == _vendInvoiceJour.InvoiceId
&& _vendInvoiceTrans.InvoiceDate == _vendInvoiceJour.InvoiceDate
&& _vendInvoiceTrans.numberSequenceGroup == _vendInvoiceJour.numberSequenceGroup
&& _vendInvoiceTrans.InternalInvoiceId == _vendInvoiceJour.InternalInvoiceId
join PackingSlipSourceDocumentLine,InvoiceSourceDocumentLIne from _vendInvoicePackingSlipQuantityMatch
where _vendPackingSlipTrans.SourceDocumentLine == _vendInvoicePackingSlipQuantityMatch.PackingSlipSourceDocumentLine
&& _vendInvoicePackingSlipQuantityMatch.InvoiceSourceDocumentLIne == _vendInvoiceTrans.SourceDocumentLine
join _vendPackingSlipJour
where _vendPackingSlipJour.RecId == _vendPackingSlipTrans.VendPackingSlipJour
&& _vendInvoiceJour.InvoiceAccount == _vendPackingSlipJour.InvoiceAccount
{
if(!conFind(conPS,_vendPackingSlipJour.RecId))
{
conPS+=_vendPackingSlipJour.RecId;
}
}
}
for(k=1;k<=conLen(conPS);k++)
{
_vendPackingSlipJour=VendPackingSlipJour::findRecId(conPeek(conPS,k));
while select _vendPackingSlipTrans
where _vendPackingSlipJour.RecId == _vendPackingSlipTrans.VendPackingSlipJour
join _vendInvoiceTrans
join PackingSlipSourceDocumentLine,InvoiceSourceDocumentLIne from _vendInvoicePackingSlipQuantityMatch
where _vendPackingSlipTrans.SourceDocumentLine == _vendInvoicePackingSlipQuantityMatch.PackingSlipSourceDocumentLine
&& _vendInvoicePackingSlipQuantityMatch.InvoiceSourceDocumentLIne == _vendInvoiceTrans.SourceDocumentLine
join _vendInvoiceJour
where _vendInvoiceJour.InvoiceAccount == _vendPackingSlipJour.InvoiceAccount
&& _vendInvoiceTrans.PurchId == _vendInvoiceJour.PurchId
&& _vendInvoiceTrans.InvoiceId == _vendInvoiceJour.InvoiceId
&& _vendInvoiceTrans.InvoiceDate == _vendInvoiceJour.InvoiceDate
&& _vendInvoiceTrans.numberSequenceGroup == _vendInvoiceJour.numberSequenceGroup
&& _vendInvoiceTrans.InternalInvoiceId == _vendInvoiceJour.InternalInvoiceId
{
if(!conFind(conInv,_vendInvoiceJour.RecId))
{
conInv+=_vendInvoiceJour.RecId;
}
}
}
if (conLen(conPS) && conLen(conInv))
{
nb += this.LettrageBLFF(conPS,conInv);
}
}
conPS=conDel(conPS,1,conLen(conPS));
conInv=conDel(conInv,1,conLen(conInv));
}
}
if(nb)
info("Lettrage éffectué.");
else
info("Aucun lettrage n'a été éffectué.");
}
public int LettrageBLFF(container conPckSlp, container conInvoice)
{
settlementCollection = new RecordInsertList(tablenum(LedgerTransSettlement));
for (i=1;i<=conLen(conPckSlp);i++)
{
_vendPackingSlipJour=VendPackingSlipJour::findRecId(conPeek(conPckSlp,i));
while select gjaeCr
where gjaeCr.PostingType == LedgerPostingType::PurchPckSlpPurchaseOffsetAccount
join gjeCr
where gjaeCr.GeneralJournalEntry == gjeCr.RecId
&& gjeCr.DocumentNumber == _vendPackingSlipJour.PackingSlipId
&& gjeCr.AccountingDate == _vendPackingSlipJour.DeliveryDate
notexists join ledgerTransSettlement
where gjaeCr.RecId == ledgerTransSettlement.TransRecId
{
if(!conFind(conGJAE,gjaeCr.RecId))
{
conGJAE+=gjaeCr.RecId;
sumGJAECr += gjaeCr.AccountingCurrencyAmount;
}
}
}
for(i=1;i<=conLen(conInvoice);i++)
{
_vendInvoiceJour=VendInvoiceJour::findRecId(conPeek(conInvoice,i));
while select gjaeDeb
where gjaeDeb.PostingType == LedgerPostingType::PurchPckSlpPurchaseOffsetAccount
join gjeDeb
where gjaeDeb.GeneralJournalEntry == gjeDeb.RecId
&& gjeDeb.DocumentNumber == _vendInvoiceJour.InvoiceId
&& gjeDeb.AccountingDate == _vendInvoiceJour.InvoiceDate
&& gjeDeb.SubledgerVoucher == _vendInvoiceJour.LedgerVoucher
notexists join ledgerTransSettlement
where gjaeDeb.RecId == ledgerTransSettlement.TransRecId
{
if(!conFind(conGJAE,gjaeDeb.RecId))
{
conGJAE+=gjaeDeb.RecId;
sumGJAEDeb += gjaeDeb.AccountingCurrencyAmount;
}
}
}
if(sumGJAEDeb+sumGJAECr==0)
{
ledgerTransSettlement.SettleId = NumberSeq::newGetNum(CompanyInfo::numRefParmId()).num();
for(i=1;i<=conLen(conGJAE);i++)
{
ledgerTransSettlement.TransRecId = conPeek(conGJAE,i);
settlementCollection.add(ledgerTransSettlement);
}
settlementCollection.insertDatabase();
return conLen(conGJAE);
}
conGJAE=conDel(conGJAE,1,conLen(conGJAE));
sumGJAEDeb=0;sumGJAECr=0;
return 0;
}