0
votes

I have a customization to the Sales Orders screen, where I use a RowPersisting event to update the Requested Date to the current date upon saving. The problem is, if the save is not executed, but the Actions.PrepareInvoice is initiated, the new Requested Date (hopefully set by the RowPersisting event) is not used. I've tried to override the base method as follows (to save the Sales Order record with the new Requested Date before the Prepare Invoice process is run):

    public delegate IEnumerable PrepareInvoiceDelegate(PXAdapter adapter);
    [PXOverride]
    public IEnumerable PrepareInvoice(PXAdapter adapter, PrepareInvoiceDelegate baseMethod)
    {
        Base.Actions.PressSave();
        return baseMethod(adapter);
    }

But I receive the following error - "Error: The previous operation has not been complete yet."

How can I ensure that a modified Requested Date is used for the Prepare Invoice process if the Sales Order record is not yet saved?

2

2 Answers

0
votes

The PrepareInvoice method in case if the call is not from Processing Page(adapter.MassProcess==false) is calling this.Save.Press() anyway. Below is the code from PrepareInvoice action. Before creation of the Invoice there is being called this.Save.Press() so any your update will be saved and used for creation of the invoice.

[PXUIField(DisplayName = "Prepare Invoice", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select, Visible = false), PXButton]
public virtual IEnumerable PrepareInvoice(PXAdapter adapter)
{
    List<SOOrder> list = adapter.Get<SOOrder>().ToList<SOOrder>();
    foreach (SOOrder current in list)
    {
        if (this.Document.Cache.GetStatus(current) != PXEntryStatus.Inserted)
        {
            this.Document.Cache.SetStatus(current, PXEntryStatus.Updated);
        }
    }
    if (!adapter.MassProcess)
    {
        try
        {
            this.RecalculateAvalaraTaxesSync = true;
            this.Save.Press();
        }
        finally
        {
            this.RecalculateAvalaraTaxesSync = false;
        }
    }
    PXLongOperation.StartOperation(this, delegate
    {
        DocumentList<ARInvoice, SOInvoice> documentList = new DocumentList<ARInvoice, SOInvoice>(PXGraph.CreateInstance<SOShipmentEntry>());
        SOOrderEntry.InvoiceOrder(adapter.Arguments, list, documentList, adapter.MassProcess);
        if (!adapter.MassProcess && documentList.Count > 0)
        {
            using (new PXTimeStampScope(null))
            {
                SOInvoiceEntry sOInvoiceEntry = PXGraph.CreateInstance<SOInvoiceEntry>();
                sOInvoiceEntry.Document.Current = sOInvoiceEntry.Document.Search<ARInvoice.docType, ARInvoice.refNbr>(documentList[0].DocType, documentList[0].RefNbr, new object[]
                {
                    documentList[0].DocType
                });
                throw new PXRedirectRequiredException(sOInvoiceEntry, "Invoice");
            }
        }
    });
    return list;
}
-1
votes

The solution seems to be to recreate the Action method in a graph extension and add the following code:

        if (!adapter.MassProcess)
        {
            //****Code added to update the Requested Date to today's date...
            var soorder = (SOOrder)Base.Caches[typeof(SOOrder)].Current;
            soorder.RequestDate = DateTime.Now;
            Base.Caches[typeof(SOOrder)].Update(soorder);
            //****End of code added...

            try
            {
                Base.RecalculateAvalaraTaxesSync = true;
                Base.Save.Press();
            }
            finally
            {
                Base.RecalculateAvalaraTaxesSync = false;
            }
        }