Search code examples
c#acumatica

Acumatica Update Revenue Budget Fields from Tasks on Project level


In Acumatica (Build 2020.5.2.368) we have done extensive customisations on the Project Task (DAC: PMTask) which saves correctly when we hit the Save button. When we want to push over some of the changes to the Revenue Budget tab (DAC: PMRevenueBudget) - we use the following code:

// Task updated
    protected void PMTask_RowUpdated(PXCache cache, PXRowUpdatedEventArgs e)
    {
        var row = (PMTask)e.Row;
        PMTaskExt TaskExt = row.GetExtension<PMTaskExt>();
                        // Check Qty
        if (TaskExt.UsrQuantity == 0)
        {
            TaskExt.UsrQuantity = 1;
        }

        if (TaskExt.UsrCostPrice != 0 && TaskExt.UsrMarginPerc != 0)
        {
            // do custom calculations here -- I removed the part of the code as it was useless for the purpose of posting here.
            // Set custom fields
            // These fields all have values greater than 0 --> Double Checked this
            TaskExt.UsrMarkupPerc = TaskExt.UsrCostPrice * TaskExt.UsrQuantity / 100;
            TaskExt.UsrLineCost = TaskExt.UsrCostPrice * TaskExt.UsrQuantity;
            TaskExt.UsrSellPrice = TaskExt.UsrUnitPrice * TaskExt.UsrQuantity;
            TaskExt.UsrTotalRevenue = TaskExt.UsrSellPrice - TaskExt.UsrLineCost;
            TaskExt.UsrCostPriceCalculation = Convert.ToDecimal(12.00);
            TaskExt.UsrUnitPriceCalculation = Convert.ToDecimal(88.99);               
        }
        else 
        {
            // More calculations
        }                     
        

        int revAccountID = 0;
        int costAccountID = 0;
        foreach (PMAccountGroup pmag in AccountInfo.Select())
        {
            if (pmag.GroupCD.Trim().Equals("INCOME"))
            {
                revAccountID = (int)pmag.GroupID; // 898
            }

            if (pmag.GroupCD.Trim().Equals("EXPENSES"))
            {
                costAccountID = (int)pmag.GroupID;
            }
        }

        // Find the Inventory Type
        InventoryItem inventory = InvetoryInfo.Search<InventoryItem.inventoryID>(TaskExt.UsrInventoryID);
        string invUOM = "";
        if (inventory != null)
        {
            invUOM = inventory.BaseUnit;
        }

        // Task --> Revenue Budget
        PMRevenueBudget revbudgetItem = Base.RevenueBudget.Search<PMRevenueBudget.projectTaskID>(thisTask.TaskID);
        if (revbudgetItem != null)
        {
            revbudgetItem.AccountGroupID = revAccountID;
            revbudgetItem.InventoryID = thiskExt.UsrInventoryID;
            revbudgetItem.CuryUnitRate = thiskExt.UsrUnitPrice;
            revbudgetItem.Qty = thiskExt.UsrQuantity;
            revbudgetItem.CuryAmount = thiskExt.UsrSellPrice;
            revbudgetItem.UOM = invUOM;
            revbudgetItem.RevisedQty = revbudgetItem.Qty;
            revbudgetItem.CuryRevisedAmount = revbudgetItem.CuryAmount;
            // --> Base.RevenueBudget.Update(revbudgetItem); <-- This works to update the cahce, but as soon as the user hits save, it reverts the changes back to 0.
        }
    }

I have tried it in the row persisting and persisted events but then get the

"Another process has added/updated record"

error which reverts everything. How can I get the values over and save it to the table when I hit the Save button?

Any help would be greatly appreciated.


Solution

  • So, the best solution to this we found so far is to update the Graph in the row persisted.

    protected void PMProject_RowPersisted(PXCache cache, PXRowPersistedEventArgs e)
        {
            var row = (PMProject)e.Row;
            if (e.TranStatus == PXTranStatus.Completed)
            {
                var target = PXGraph.CreateInstance<ProjectTaskEntry>();
                var target2 = PXGraph.CreateInstance<ProjectEntry>();
    
                //var target = PXGraph.CreateInstance<ProjectEntry>();
                foreach (PMTask taskitem in Base.Tasks.Select())
                {
                    PMTaskExt TaskExt = taskitem.GetExtension<PMTaskExt>();                    
                    target.Task.Update(taskitem);
                    
                    foreach (PMRevenueBudget revbudgetItem in Base.RevenueBudget.Search<PMRevenueBudget.projectTaskID>(taskitem.TaskID))
                    {
                        revbudgetItem.Qty = TaskExt.UsrQuantity;
                        target2.RevenueBudget.Update(revbudgetItem);
                    }
                }              
    
                target2.Save.PressButton();
    
            }
        }