public virtual void RecalculateChangeOrders(PMProject project, ProjectBalance projectBalance, Dictionary <BudgetKeyTuple, PMBudgetEx> existingBudgetRecords)
        {
            var select = new PXSelect <PMChangeOrderBudget,
                                       Where <PMChangeOrderBudget.projectID, Equal <Required <PMChangeOrderBudget.projectID> >,
                                              And <PMChangeOrderBudget.released, Equal <True> > > >(this);

            foreach (PMChangeOrderBudget change in select.Select(project.ContractID))
            {
                BudgetKeyTuple key = GetKey(change);

                PMBudgetEx existing = null;
                if (existingBudgetRecords.TryGetValue(key, out existing))
                {
                    PMBudgetAccum budget = new PMBudgetAccum();
                    budget.ProjectID      = key.ProjectID;
                    budget.ProjectTaskID  = key.ProjectTaskID;
                    budget.AccountGroupID = key.AccountGroupID;
                    budget.InventoryID    = key.InventoryID;
                    budget.CostCodeID     = key.CostCodeID;

                    budget = Budget.Insert(budget);
                    budget.ChangeOrderAmount += change.Amount.GetValueOrDefault();
                    budget.RevisedAmount     += change.Amount.GetValueOrDefault();

                    var rollup = projectBalance.CalculateRollupQty <PMChangeOrderBudget>(change, existing);
                    if (rollup.Qty.GetValueOrDefault() != 0)
                    {
                        budget.ChangeOrderQty += change.Qty.GetValueOrDefault();
                        budget.RevisedQty     += change.Qty.GetValueOrDefault();
                    }
                }
                else
                {
                    PMAccountGroup accountGroup = null;
                    if (AccountGroups.TryGetValue(key.AccountGroupID, out accountGroup))
                    {
                        PMBudgetAccum budget = new PMBudgetAccum();
                        budget.ProjectID      = key.ProjectID;
                        budget.ProjectTaskID  = key.ProjectTaskID;
                        budget.AccountGroupID = key.AccountGroupID;
                        budget.InventoryID    = key.InventoryID;
                        budget.CostCodeID     = key.CostCodeID;
                        budget.Type           = accountGroup.IsExpense == true ? GL.AccountType.Expense : accountGroup.Type;

                        budget = Budget.Insert(budget);
                        budget.ChangeOrderAmount += change.Amount.GetValueOrDefault();
                        budget.RevisedAmount     += change.Amount.GetValueOrDefault();
                    }
                }
            }
        }
        public List <PXDataFieldParam> BuildBudgetClearCommandWithChangeOrders(PMValidationFilter options, PMBudgetEx status)
        {
            List <PXDataFieldParam> list = new List <PXDataFieldParam>();

            list.Add(new PXDataFieldRestrict(typeof(PMBudget.projectID).Name, PXDbType.Int, 4, status.ProjectID, PXComp.EQ));
            list.Add(new PXDataFieldRestrict(typeof(PMBudget.accountGroupID).Name, PXDbType.Int, 4, status.AccountGroupID, PXComp.EQ));
            list.Add(new PXDataFieldRestrict(typeof(PMBudget.projectTaskID).Name, PXDbType.Int, 4, status.ProjectTaskID, PXComp.EQ));
            list.Add(new PXDataFieldRestrict(typeof(PMBudget.inventoryID).Name, PXDbType.Int, 4, status.InventoryID, PXComp.EQ));
            list.Add(new PXDataFieldRestrict(typeof(PMBudget.costCodeID).Name, PXDbType.Int, 4, status.CostCodeID, PXComp.EQ));

            list.Add(new PXDataFieldAssign(typeof(PMBudget.type).Name, PXDbType.Char, 1, GetAccountGroupType(status.AccountGroupID)));
            list.Add(new PXDataFieldAssign(typeof(PMBudget.actualAmount).Name, PXDbType.Decimal, 0m));
            list.Add(new PXDataFieldAssign(typeof(PMBudget.actualQty).Name, PXDbType.Decimal, 0m));
            list.Add(new PXDataFieldAssign(typeof(PMBudget.revisedQty).Name, PXDbType.Decimal, status.Qty));
            list.Add(new PXDataFieldAssign(typeof(PMBudget.revisedAmount).Name, PXDbType.Decimal, status.Amount));
            list.Add(new PXDataFieldAssign(typeof(PMBudget.changeOrderQty).Name, PXDbType.Decimal, 0m));
            list.Add(new PXDataFieldAssign(typeof(PMBudget.changeOrderAmount).Name, PXDbType.Decimal, 0m));

            if (options.RebuildCommitments == true)
            {
                list.Add(new PXDataFieldAssign(typeof(PMBudget.committedAmount).Name, PXDbType.Decimal, 0m));
                list.Add(new PXDataFieldAssign(typeof(PMBudget.committedQty).Name, PXDbType.Decimal, 0m));
                list.Add(new PXDataFieldAssign(typeof(PMBudget.committedOpenAmount).Name, PXDbType.Decimal, 0m));
                list.Add(new PXDataFieldAssign(typeof(PMBudget.committedOpenQty).Name, PXDbType.Decimal, 0m));
                list.Add(new PXDataFieldAssign(typeof(PMBudget.committedReceivedQty).Name, PXDbType.Decimal, 0m));
                list.Add(new PXDataFieldAssign(typeof(PMBudget.committedInvoicedAmount).Name, PXDbType.Decimal, 0m));
                list.Add(new PXDataFieldAssign(typeof(PMBudget.committedInvoicedQty).Name, PXDbType.Decimal, 0m));
                list.Add(new PXDataFieldAssign(typeof(PMBudget.committedOrigQty).Name, PXDbType.Decimal, 0m));
                list.Add(new PXDataFieldAssign(typeof(PMBudget.committedOrigAmount).Name, PXDbType.Decimal, 0m));
            }

            if (options.RecalculateDraftInvoicesAmount == true)
            {
                list.Add(new PXDataFieldAssign(typeof(PMBudget.invoicedAmount).Name, PXDbType.Decimal, 0m));
            }

            return(list);
        }