BhavyaSri
BhavyaSri

Reputation: 150

On Invoice and Memos after release we have enabled custom fields both header and footer but while modifying the balance is updating with wrong value

I had a requirement to enable some custom fields both header and footer if status is closed and balance = 0 then if we modify any footer field value on persist delegate setting the value for the modified field and updating Transactions cache (sample code below) then the balance 0 is getting updating with the value of Invoice total field.

public class KWARInvoiceEntryExt : PXGraphExtension<ARInvoiceEntry>
    {
[PXOverride]
        public void Persist(Action del)
        {            
            if ((Base.Document.Cache.GetStatus(Base.Document.Current) == PXEntryStatus.Inserted || Base.Document.Cache.GetStatus(Base.Document.Current) == PXEntryStatus.Updated))
            {
                foreach (ARTran items in Base.Transactions.Select())
                {

                    if (Base.Document.Current.DocType == ARDocType.Invoice)
                    {
                        if (Base.Document.Current.Released == true)
                        {
                             Base.Transactions.Cache.SetValue<ARTran.curyTranAmt>(items, items.CuryTranAmt);
                        }
                    }
                        Base.Transactions.Update(items);
                }
            }
            del();
        }
   }

Before modifying any field in the grid, see the balance value

After modifying grid field, see the balance value

Upvotes: 1

Views: 184

Answers (1)

Evgeny Kralko
Evgeny Kralko

Reputation: 342

This happens inside the ARInvoiceEntry.ARTran_RowUpdated event. Normally in Acumatica, you cannot update released transactions so no additional conditions there and once you are trying to update any value and save transaction - balance will be calculated the same way as for balanced document.

protected virtual void ARTran_RowUpdated(PXCache sender, PXRowUpdatedEventArgs e)
{
    ARTran row = (ARTran)e.Row;
    ARTran oldRow = (ARTran)e.OldRow;
    if (row != null)
    {
        if ((!sender.ObjectsEqual<ARTran.branchID>(e.Row, e.OldRow) || !sender.ObjectsEqual<ARTran.inventoryID>(e.Row, e.OldRow) ||
            !sender.ObjectsEqual<ARTran.baseQty>(e.Row, e.OldRow) || !sender.ObjectsEqual<ARTran.curyUnitPrice>(e.Row, e.OldRow) || !sender.ObjectsEqual<ARTran.curyTranAmt>(e.Row, e.OldRow) ||
            !sender.ObjectsEqual<ARTran.curyExtPrice>(e.Row, e.OldRow) || !sender.ObjectsEqual<ARTran.curyDiscAmt>(e.Row, e.OldRow) ||
            !sender.ObjectsEqual<ARTran.discPct>(e.Row, e.OldRow) || !sender.ObjectsEqual<ARTran.manualDisc>(e.Row, e.OldRow) ||
            !sender.ObjectsEqual<ARTran.discountID>(e.Row, e.OldRow)) && row.LineType != SOLineType.Discount)
            RecalculateDiscounts(sender, row);

        if (row.ManualDisc != true)
        {
            var discountCode = (ARDiscount)PXSelectorAttribute.Select<SOLine.discountID>(sender, row);
            row.DiscPctDR = (discountCode != null && discountCode.IsAppliedToDR == true) ? row.DiscPct : 0.0m;
        }

        if ((e.ExternalCall || sender.Graph.IsImport)
            && sender.ObjectsEqual<ARTran.inventoryID>(e.Row, e.OldRow) && sender.ObjectsEqual<ARTran.uOM>(e.Row, e.OldRow)
            && sender.ObjectsEqual<ARTran.qty>(e.Row, e.OldRow) && sender.ObjectsEqual<ARTran.branchID>(e.Row, e.OldRow)
            && sender.ObjectsEqual<ARTran.siteID>(e.Row, e.OldRow) && sender.ObjectsEqual<ARTran.manualPrice>(e.Row, e.OldRow)
            && (!sender.ObjectsEqual<ARTran.curyUnitPrice>(e.Row, e.OldRow) || !sender.ObjectsEqual<ARTran.curyExtPrice>(e.Row, e.OldRow))
            && row.ManualPrice == oldRow.ManualPrice)
            row.ManualPrice = true;

        if (row.ManualPrice != true)
        {
            row.CuryUnitPriceDR = row.CuryUnitPrice;
        }

        // BALANCE WILL BE CALCULATED HERE!!!
        //
        TaxAttribute.Calculate<ARTran.taxCategoryID>(sender, e); 

        //Validate that Sales Account <> Deferral Account:
        if (!sender.ObjectsEqual<ARTran.accountID, ARTran.deferredCode>(e.Row, e.OldRow))
        {
            if (!string.IsNullOrEmpty(row.DeferredCode))
            {
                DRDeferredCode defCode = PXSelect<DRDeferredCode, Where<DRDeferredCode.deferredCodeID, Equal<Required<DRDeferredCode.deferredCodeID>>>>.Select(this, row.DeferredCode);
                if (defCode != null)
                {
                    if (defCode.AccountID == row.AccountID)
                    {
                        sender.RaiseExceptionHandling<ARTran.accountID>(e.Row, row.AccountID,
                            new PXSetPropertyException(Messages.AccountIsSameAsDeferred, PXErrorLevel.Warning));
                    }
                }
            }
        }
    }
}

As a result, you just need to override ARInvoiceEntry.ARTran_RowUpdated event and disable calculation for the released document. Something like this:

[Serializable]
public class ARInvoiceEntry_Extension : PXGraphExtension<ARInvoiceEntry>
{
    public delegate void EventDelegate(PXCache sender, PXRowUpdatedEventArgs e);

    [PXOverride]
    public void ARTran_RowUpdated(PXCache sender, PXRowUpdatedEventArgs e, EventDelegate baseMethod)
    {
        if (Base.Document.Current.Released != true)
        {
            baseMethod.Invoke(sender, e);
        }
    }
}

Upvotes: 1

Related Questions