Hello Everyone,
we are trying to associate Credit Memo with sales order,
I have created an SO extension graph and in persist inserting the credit memo details based on the order total, with this approach if we are creating an order manually and saving, the implementation is working as expected. But when any order is synced from BigCommerce, it is not updating as expected and we observed discrepancies in Credit Memo applied to order total and balance amount.
public class KNBCBRSOOrderEntryExt : PXGraphExtension<SOOrderEntry>
{
public static bool IsActive() { return true; }
#region Views
public PXSelect<Customer> CustomerUpdate;
#endregion
public delegate void PersistDelegate();
PXOverride]
public void Persist(PersistDelegate del)
{
del();
List<ARInvoice> curyDocBalList = PXSelect<ARInvoice, Where<ARInvoice.docType, Equal<ARDocType.creditMemo>,
And<ARInvoice.status, Equal<ARDocStatus.open>,
And<ARInvoice.customerID, Equal<Required<ARInvoice.customerID>>>>>,
OrderBy<Asc<ARInvoice.createdDateTime>>>
.Select(Base, Base.Document?.Current?.CustomerID).RowCast<ARInvoice>().ToList();
decimal? CuryDocBalSum = 0;
decimal? RemainingAmt = Base.Document.Current.CuryOrderTotal;
foreach (ARInvoice invoiceCM in curyDocBalList)
{
try
{
if (invoiceCM.CuryDocBal < Base.Document.Current.CuryOrderTotal)
{
CuryDocBalSum += invoiceCM.CuryDocBal;
if (CuryDocBalSum <= Base.Document.Current.CuryOrderTotal)
{
SOAdjust sOAdjust = new SOAdjust();
sOAdjust.AdjgDocType = invoiceCM.DocType;
sOAdjust.AdjgRefNbr = invoiceCM.RefNbr;
sOAdjust.CuryAdjdAmt = invoiceCM.CuryDocBal;
Base.Adjustments.Insert(sOAdjust);
RemainingAmt -= invoiceCM.CuryDocBal;
}
else
{
decimal? amountToInsert = RemainingAmt;
if (amountToInsert > 0)
{
SOAdjust sOAdjust = new SOAdjust();
sOAdjust.AdjgDocType = invoiceCM.DocType;
sOAdjust.AdjgRefNbr = invoiceCM.RefNbr;
sOAdjust.CuryAdjdAmt = amountToInsert;
Base.Adjustments.Insert(sOAdjust);
RemainingAmt = 0;
break;
}
}
}
else
{
SOAdjust sOAdjust = new SOAdjust();
if (RemainingAmt > 0)
{
sOAdjust.AdjgDocType = invoiceCM.DocType;
sOAdjust.AdjgRefNbr = invoiceCM.RefNbr;
sOAdjust.CuryAdjdAmt = RemainingAmt;
Base.Adjustments.Insert(sOAdjust);
RemainingAmt = 0;
break;
}
}
}
catch (Exception ex)
{
throw new PXException(ex.Message);
}
}
del();
}
}