vendor open transaction settlement

Hi all .

Requirement is need to settle open transaction against vendor . below code i used but it was settle all transaction for that vendor … but i want to mark the invoice and payment then for that vendor then need to settle if payment is 2000 rs means i want to settle the first invoice amount upto match the 2000 rs … here how to write code for this . can u any give me sample code for this

static void VendSettlement(Args _args)
{
VendTable vendTable;
VendTrans invCustTrans, payCustTrans;
SpecTransManager manager;
CustVendTransData custVendTransData;

while select vendTable where vendTable.AccountNum == ‘0000000’
{

while select * from invCustTrans
order by TransDate asc
where invCustTrans.AccountNum == vendTable.AccountNum &&
invCustTrans.TransType == LedgerTransType::Vend &&
!invCustTrans.LastSettleDate
{

while select * from payCustTrans
order by TransDate asc
where payCustTrans.AccountNum == vendTable.AccountNum &&
payCustTrans.TransType != LedgerTransType::Vend &&
!payCustTrans.LastSettleDate
{

ttsbegin;
custVendTransData = CustVendTransData::construct(invCustTrans);
custVendTransData.markForSettlement(vendTable);

custVendTransData = CustVendTransData::construct(payCustTrans);
custVendTransData.markForSettlement(vendTable);
ttscommit;

VendTrans::settleTransact(vendTable, null, true,
SettleDatePrinc::DaysDate, systemdateget());
info(strfmt(" Customer Account:- %1 Transactions settled",vendTable.AccountNum));
}

}

}

}

Can you explain it clearly? May be with an example.

Actually our client has made Invoices and Payments not in a standard way; say for an example our client has made 10 vendor invoices and made respective payments but not settling against the open invoice using functions>Settlement options;hence the invoice and payments lies open at vendor level even though it is settled, here transactions are more than 1000 lines, so wants to settle on FIFO basis