Well, you throw a lot of things together, so there is a lot that can be said.
It might sounds unimportant, but I think you should start with paying attention to things like variable scope, indentation and letter casing. It'll make your code much easier to read than when things like indented or named randomly. They you should split your code to smaller pieces. When code is split to methods with descriptive names, it's much easier to work with, because you can focus on smaller individual pieces. It'll also help you with variable scope.
Regarding your actual code, one problem is already in the very first select statement. It won't work correctly for invoice that contain multiple sales orders. The correct relation is through CustInvoiceSalesLink table.
I wonder if you really want CustInvoiceJour.InterCompanyPurchId. Again, there may be just a single value even if the invoice contains multiple orders. Maybe you should use SalesTable.InterCompanyPurchId instead.
I see you use purchLine.CustomerRef, but you neither aggregate or group by this it, therefore it'll always be empty.
I wonder why you must group by all the fields when selecting from DTCustomerDetailsTable. Isn't it a bug in code or in the data model?
You should also get used to checking whether there isn't already logic you're going to implement. For example, notice how I used custInvoiceJour.custTable_OrderAccount().name() in the code below. There may be cases where you'll rather make your own query, but productivity is often more valuable.
You should try to reduce the number of nested while selects and use joins instead. It makes a huge difference in performance.
You seem to be missing a logic for the situation when the query fails to find any purchTable/purchLine.
Here is an example of some refactoring to make the code easier to read:
public class YourClass
{
public void processOperation()
{
CustInvoiceJour custInvoiceJour;
CustInvoiceSalesLine
SalesTable salesTable;
while select custInvoiceJour
where custInvoiceJour.SalesId == 'sa01SO01-10001732'
join InterCompanyOriginalSalesId, InterCompanyCompanyId, WorkerSalesResponsible from salesTable
where salesTable.InterCompanyPurchId == custInvoiceJour.InterCompanyPurchId
{
DTPaymentTrackingTbl paymentTracking;
paymentTracking.initFromCustInvoiceJour(custInvoiceJour);
paymentTracking.DTWorkerName = HcmWorker::find(salesTable.WorkerSalesResponsible).name();
DTCustomerDetailsTable dTCustomerDetailsTable;
while select crosscompany dTCustomerDetailsTable
group by SalesId, PackingSlipId, InvoiceId, InvoiceAmount, AmountNotSettled,
DTCustInvoicePaymType, DTInvoiceStatus, DataAreaId
where dTCustomerDetailsTable.DataAreaId == salesTable.InterCompanyCompanyId
&& dTCustomerDetailsTable.SalesId == salesTable.InterCompanyOriginalSalesId
{
paymentTracking.initFromDTCustomerDetailsTable(dTCustomerDetailsTable);
paymentTracking.DTEntityName = salesTable.InterCompanyCompanyId;
Purchtable purchTable;
Purchline purchLine;
select firstOnly crosscompany CustomerRef, sum(LineAmount) from purchLine
group by purchTable.PurchId, purchTable.PurchStatus, purchTable.IntercompanyCompanyId, purchTable.CreatedDateTime
where purchLine.DataAreaId == salesTable.InterCompanyCompanyId
join purchTable
where purchTable.PurchId == purchLine.PurchId
&& purchTable.PurchId == intPruchId;
paymentTracking.InterCompanyPurchId = purchTable.PurchId;
paymentTracking.PurchStatus = purchTable.PurchStatus;
paymentTracking.DTDateTime = purchTable.CreatedDateTime;
paymentTracking.CustomerRef = purchLine.CustomerRef;
paymentTracking.LineAmount = purchLine.LineAmount;
if (DTPaymentTrackingTbl::exist(
paymentTracking.SalesId,
paymentTracking.InvoiceId,
paymentTracking.PackingSlipId)
{
this.updateInvoicesDetails(paymentTracking);
}
else
{
paymentTracking.insert();
}
}
}
}
}
public class DTPaymentTrackingTbl extends common
{
public void initFromCustInvoiceJour(CustInvoiceJour _custInvoiceJour)
{
this.InvoiceAccount = _custInvoiceJour.InvoiceAccount;
this.Name = _custInvoiceJour.custTable_OrderAccount().name();
}
public void initFromDTCustomerDetailsTable(DTCustomerDetailsTable _custDetails)
{
this.SalesId = _custDetails.SalesId;
this.PackingSlipId = _custDetails.PackingSlipId;
this.InvoiceId = _custDetails.InvoiceId;
this.InvoiceAmount = _custDetails.InvoiceAmount;
this.AmountNotSettled = _custDetails.AmountNotSettled;
this.DTCustInvoicePaymtType = _custDetails.DTCustInvoicePaymtType;
this.DTInvoiceStatus = _custDetails.DTInvoiceStatus;
}
public static boolean exist(...)
{
...
}
}