Wrongly amounts are deducting invoice while posting payment journal

I am trying to create payment journal lines and post them automatically through X++ , I am facing issue while making payment for more than one invoice ,after posting invoice amounts are deducting sum of amount to only one invoice instead of deducting for all invoices.

Please give me some suggestion on this issue.

code:

To create payment lines:

public void createLedgerLine(REV_PAYMENT_XML_HEADER paymentxmlheader ,

REV_PAYMENT_XML_DETAILS paymentxmldetails, Mapping _HelperMapping)
{
// Mapping _HelperMapping;
RecId ledgerdim;
LedgerJournalTrans LedgerJournalTransInLedgerline,_ledjountrans1,ledjtr,LedgerJournalTrans;
LedgerJournalTable ledgerJournalTablecheck;
str invid,custno;
LedgerJournalCheckPost _ledgerJournalCheckPost;
boolean validatebankcode;
Approved approve;
HcmApprover _approver;
NumberSeq numseq;
LedgerJournalName ljn;
;

ttsBegin;
_approver = HcmWorker::findByPerson(DirPartyTable::findByName(“Default”).RecId).RecId;//Approver with name Default
approve = NoYes::Yes;
journalTrans = new AxLedgerJournalTrans();
invid = paymentxmldetails.invNo;
custno = paymentxmlheader.vendorCrossRef;
select ledgerJournalTablecheck where ledgerJournalTablecheck.JournalName==_JnlNum;
if(CustTable::find(paymentxmlheader.vendorCrossRef))
{
// journalTrans.parmVoucher(NumberSeq::newGetVoucherFromCode(LedgerJournalName::find(ledgerJournalTablecheck.JournalName)));
journalTrans.parmTransDate(today());
journalTrans.parmCompany(_HelperMapping.ImosAxCompanyCode);
journalTrans.parmAccountType(LedgerJournalACType::Cust);
journalTrans.parmJournalNum(_JnlNum);
journalTrans.parmOffsetAccountType(LedgerJournalACType::Bank);
journalTrans.parmCurrencyCode(paymentxmldetails.CURRENCY);
ledgerdim = DimensionStorage::getDynamicAccount(paymentxmlheader.vendorCrossRef,LedgerJournalACType::Cust);
journalTrans.parmLedgerDimension(ledgerdim);
journalTrans.parmApprover(_approver);
journalTrans.parmApproved(approve);
journalTrans.parmMarkedInvoice(paymentxmldetails.invNo);
//journalTrans.parmInvoice(paymentxmldetails.invNo);

if(paymentxmldetails.currencyAmount>0)
{
curext();
journalTrans.parmAmountCurCredit(paymentxmldetails.currencyAmount);
}
else
{
curext();
journalTrans.parmAmountCurDebit(paymentxmldetails.currencyAmount);
}
journalTrans.parmSettleVoucher(SettlementType::OpenTransact);
validatebankcode = this.Validate_Bank(paymentxmldetails);
if(!validatebankcode)
{
_errorfree = false;
_Fullerrorlog=“Bank Account “+paymentxmldetails.bankCode+” not present in AX \n” ;
}

journalTrans.parmPaymMode(‘BT’);
journalTrans.parmOffsetLedgerDimension(DimensionStorage::getDynamicAccount(_BankAccounttable.AccountID,LedgerJournalACType::Bank));
journalTrans.save();

select LedgerJournalTransInLedgerline where LedgerJournalTransInLedgerline.JournalNum == journalTable.ledgerJournalTable().JournalNum;
this.insertintospectrans(LedgerJournalTransInLedgerline,invid,custno);

TotalNumberOfDetailsLine–;
journalTrans.parmOffsetLedgerDimension(DimensionStorage::getDynamicAccount(_BankAccounttable.AccountID,LedgerJournalTransInLedgerline.OffsetAccountType));

journalTrans.Save();//
}
else
{
_Fullerrorlog += "The Customer Code "+ paymentxmlheader.vendorCrossRef + “is not present in AX For the company \n”+ _HelperMapping.ImosAxCompanyCode ;
}
if(TotalNumberOfDetailsLine==0 && validatebankcode && _errorfree )
{
ledgerJournalTablecheck = ledgerJournalTable::find(LedgerJournalTransInLedgerline.JournalNum);
_ledgerJournalCheckPost = ledgerJournalCheckPost::newLedgerJournalTable(journalTable.ledgerJournalTable(),NoYes::Yes);
_ledgerJournalCheckPost.validate();
_ledgerJournalCheckPost.run();
}
ttsCommit;
}

insert spec trans:

select cto where cto.RefRecId == CustTrans::findFromInvoice(in,cs).RecId && cto.AccountNum== cs ;
info(strFmt(“voucher %1”,_ledgerJournalTrans.Voucher));
info(strFmt(“recid:%1”,cto.RecId));
if(cto)
{
ttsBegin;
insertSpecTrans.SpecCompany = _ledgerJournalTrans.Company;
insertSpecTrans.SpecTableId = TableNum(ledgerJournalTrans);
insertSpecTrans.RefCompany = _ledgerJournalTrans.Company;
insertSpecTrans.RefTableId = TableNum(custTransOPen);
insertSpecTrans.RefRecId = custTransOPen::findRefId(CustTrans::findFromInvoice(invid,custno).RecId).RecId;
insertSpecTrans.SpecRecId = _ledgerJournalTrans.RecId;
insertSpecTrans.Balance01 = cto.AmountCur;
insertSpecTrans.Code = _ledgerJournalTrans.CurrencyCode;
insertSpecTrans.Payment = NoYes::No;
insertSpecTrans.LineNum = 1;
insertSpecTrans.insert();
ttsCommit;