Пример #1
0
        public string GetValidationWarning(int costCodeID)
        {
            PMTask task = SelectFrom <PMTask>
                          .Where <PMTask.taskID.IsEqual <@P.AsInt> >
                          .View
                          .Select(_Graph, GetTaskID());

            int?accountGroupID = GetAccountGroupID();

            if (accountGroupID == null)
            {
                Account account = SelectFrom <Account>
                                  .Where <Account.accountID.IsEqual <@P.AsInt> >
                                  .View
                                  .Select(_Graph, GetAccountID());

                if (account != null)
                {
                    accountGroupID = account.AccountGroupID;
                }
            }

            PMAccountGroup accountGroup = SelectFrom <PMAccountGroup>
                                          .Where <PMAccountGroup.groupID.IsEqual <@P.AsInt> >
                                          .View
                                          .Select(_Graph, accountGroupID);


            PMCostCode costCode = SelectFrom <PMCostCode>
                                  .Where <PMCostCode.costCodeID.IsEqual <@P.AsInt> >
                                  .View
                                  .Select(_Graph, costCodeID);

            return(string.Format(Messages.CostCodeNotInBudget, costCode.CostCodeCD, task.TaskCD, accountGroup?.GroupCD));
        }
Пример #2
0
        protected virtual PMBudgetAccum GetTargetBudget(int?accountGroupID, ARTran line)
        {
            PMAccountGroup ag = PXSelect <PMAccountGroup, Where <PMAccountGroup.groupID, Equal <Required <PMAccountGroup.groupID> > > > .Select(Base, accountGroupID);

            PMProject project = PXSelect <PMProject, Where <PMProject.contractID, Equal <Required <PMProject.contractID> > > > .Select(Base, line.ProjectID);

            bool          isExisting;
            BudgetService budgetService = new BudgetService(Base);

            PX.Objects.PM.Lite.PMBudget budget = budgetService.SelectProjectBalance(ag, project, line.TaskID, line.InventoryID, line.CostCodeID, out isExisting);

            PMBudgetAccum target = new PMBudgetAccum();

            target.Type           = budget.Type;
            target.ProjectID      = budget.ProjectID;
            target.ProjectTaskID  = budget.TaskID;
            target.AccountGroupID = budget.AccountGroupID;
            target.InventoryID    = budget.InventoryID;
            target.CostCodeID     = budget.CostCodeID;
            target.UOM            = budget.UOM;
            target.Description    = budget.Description;
            target.CuryInfoID     = project.CuryInfoID;

            return(target);
        }
Пример #3
0
        public virtual PMBudgetLite SelectProjectBalance(PMAccountGroup ag, PMProject project, int?taskID, int?inventoryID, int?costCodeID, out bool isExisting)
        {
            BudgetKeyTuple key = new BudgetKeyTuple(project.ContractID.Value, taskID.Value, ag.GroupID.Value, inventoryID.GetValueOrDefault(PMInventorySelectorAttribute.EmptyInventoryID), costCodeID.GetValueOrDefault(CostCodeAttribute.GetDefaultCostCode()));

            string budgetLevel = BudgetLevels.Task;
            string updateMode  = CostBudgetUpdateModes.Summary;

            if (ag.Type == GL.AccountType.Income)
            {
                budgetLevel = project.BudgetLevel;
                updateMode  = settings.RevenueBudgetUpdateMode;
            }
            else if (ag.IsExpense == true)
            {
                budgetLevel = project.CostBudgetLevel;
                updateMode  = settings.CostBudgetUpdateMode;
            }

            PMBudgetLite target = SelectExistingBalance(key, budgetLevel, updateMode);

            if (target != null)
            {
                isExisting = true;
            }
            else
            {
                isExisting = false;
                target     = BuildTarget(key, ag, budgetLevel, updateMode);
            }

            return(target);
        }
        protected virtual void PMBillingRule_CapsAccountGroupID_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
        {
            PMBillingRule row = e.Row as PMBillingRule;

            if (row != null && row.CapsAccountGroupID == null && (row.LimitAmt == true || row.LimitQty == true))
            {
                PMAccountGroup ag = PXSelect <PMAccountGroup> .Search <PMAccountGroup.groupID>(this, row.AccountGroupID);

                e.NewValue = ag.GroupCD;
            }
        }
        public virtual string GetAccountGroupType(int?accountGroup)
        {
            PMAccountGroup ag = AccountGroups[accountGroup.Value];

            if (ag.Type == PMAccountType.OffBalance)
            {
                return(ag.IsExpense == true ? GL.AccountType.Expense : ag.Type);
            }
            else
            {
                return(ag.Type);
            }
        }
        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();
                    }
                }
            }
        }
        protected virtual void _(Events.FieldDefaulting <PMBudget, PMBudget.type> e)
        {
            PMAccountGroup ag = PXSelect <PMAccountGroup, Where <PMAccountGroup.groupID, Equal <Required <PMAccountGroup.groupID> > > > .Select(this, e.Row.AccountGroupID);

            if (ag != null)
            {
                if (ag.Type == PMAccountType.OffBalance)
                {
                    e.NewValue = ag.IsExpense == true ? GL.AccountType.Expense : ag.Type;
                }
                else
                {
                    e.NewValue = ag.Type;
                }
            }
        }
        protected virtual void _(Events.FieldUpdated <PMBudget, PMBudget.accountGroupID> e)
        {
            PMAccountGroup ag = PXSelect <PMAccountGroup, Where <PMAccountGroup.groupID, Equal <Required <PMAccountGroup.groupID> > > > .Select(this, e.Row.AccountGroupID);

            if (ag != null)
            {
                if (ag.IsExpense == true)
                {
                    e.Row.Type = GL.AccountType.Expense;
                }
                else
                {
                    e.Row.Type = ag.Type;
                }
            }
        }
        protected virtual void PMTran_AccountID_FieldVerifying(PXCache sender, PXFieldVerifyingEventArgs e)
        {
            PMTran row = e.Row as PMTran;

            if (row != null && e.NewValue != null)
            {
                Account item = PXSelect <Account, Where2 <Match <Current <AccessInfo.userName> >, And <Account.accountID, Equal <Required <Account.accountID> > > > > .Select(sender.Graph, e.NewValue);

                if (row != null && item != null && (item.AccountGroupID == null || item.AccountGroupID != row.AccountGroupID))
                {
                    PMAccountGroup accountGroup = PXSelect <PMAccountGroup, Where <PMAccountGroup.groupID, Equal <Required <PMAccountGroup.groupID> > > > .Select(sender.Graph, row.AccountGroupID);

                    throw new PXException(PM.Messages.AccountIsNotAssociatedWithAccountGroup, item.AccountCD, accountGroup.GroupCD);
                }
            }
        }
Пример #10
0
        public string GetTrace()
        {
            //Add transaction properties:
            PMAccountGroup ag = PXSelect <PMAccountGroup, Where <PMAccountGroup.groupID, Equal <Required <PMAccountGroup.groupID> > > > .Select(graph, tran.AccountGroupID);

            if (ag != null)
            {
                trace.AppendFormat(" PMTran.AccountGroup={0} ", ag.GroupCD);
            }

            IN.InventoryItem inventoryItem = PXSelect <IN.InventoryItem, Where <IN.InventoryItem.inventoryID, Equal <Required <IN.InventoryItem.inventoryID> > > > .Select(graph, tran.InventoryID);

            if (inventoryItem != null)
            {
                trace.AppendFormat(" PMTran.InventoryID={0} ", inventoryItem.InventoryCD);
            }

            return(trace.ToString());
        }
        protected virtual void _(Events.FieldDefaulting <PMBudget, PMBudget.rate> e)
        {
            PMAccountGroup ag = PXSelect <PMAccountGroup, Where <PMAccountGroup.groupID, Equal <Required <PMAccountGroup.groupID> > > > .Select(this, e.Row.AccountGroupID);

            if (ag != null)
            {
                if (ag.IsExpense == true)
                {
                    if (e.Row.InventoryID != null && e.Row.InventoryID != PMInventorySelectorAttribute.EmptyInventoryID)
                    {
                        InventoryItem item = (InventoryItem)PXSelectorAttribute.Select <PMCostBudget.inventoryID>(e.Cache, e.Row);
                        e.NewValue = item?.StdCost;
                    }
                }
                else
                {
                    if (e.Row.InventoryID != null && e.Row.InventoryID != PMInventorySelectorAttribute.EmptyInventoryID)
                    {
                        string customerPriceClass = ARPriceClass.EmptyPriceClass;

                        PMTask      projectTask = (PMTask)PXSelectorAttribute.Select <PMRevenueBudget.projectTaskID>(e.Cache, e.Row);
                        CR.Location c           = (CR.Location)PXSelectorAttribute.Select <PMTask.locationID>(e.Cache, projectTask);
                        if (c != null && !string.IsNullOrEmpty(c.CPriceClassID))
                        {
                            customerPriceClass = c.CPriceClassID;
                        }

                        CM.CurrencyInfo dummy = new CM.CurrencyInfo();
                        dummy.CuryID     = Accessinfo.BaseCuryID;
                        dummy.BaseCuryID = Accessinfo.BaseCuryID;
                        dummy.CuryRate   = 1;

                        e.NewValue = ARSalesPriceMaint.CalculateSalesPrice(Caches[typeof(PMTran)], customerPriceClass, projectTask.CustomerID, e.Row.InventoryID, dummy, e.Row.Qty, e.Row.UOM, Accessinfo.BusinessDate.Value, true);
                    }
                }
            }
        }
Пример #12
0
        public virtual Result Calculate(PMProject project, PMTran pmt, PMAccountGroup ag, string accountType, int amountSign, int qtySign)
        {
            PMBudgetLite target = null;
            bool         isExisting;

            target = service.SelectProjectBalance(pmt, ag, project, out isExisting);

            var rollupQty = CalculateRollupQty(pmt, target);

            List <PMHistory>  list     = new List <PMHistory>();
            PMTaskTotal       ta       = null;
            PMBudget          ps       = null;
            PMForecastHistory forecast = null;

            if (pmt.TaskID != null && (rollupQty != 0 || pmt.Amount != 0))             //TaskID will be null for Contract
            {
                ps                = new PMBudget();
                ps.ProjectID      = target.ProjectID;
                ps.ProjectTaskID  = target.TaskID;
                ps.AccountGroupID = target.AccountGroupID;
                ps.Type           = target.Type;
                ps.InventoryID    = target.InventoryID;
                ps.CostCodeID     = target.CostCodeID;
                ps.UOM            = target.UOM;
                ps.IsProduction   = target.IsProduction;
                ps.Description    = target.Description;
                if (ps.CuryInfoID == null)
                {
                    ps.CuryInfoID = project.CuryInfoID;
                }
                decimal amt     = amountSign * pmt.Amount.GetValueOrDefault();
                decimal curyAmt = amountSign * pmt.ProjectCuryAmount.GetValueOrDefault();

                ps.ActualQty        = rollupQty * qtySign;
                ps.ActualAmount     = amt;
                ps.CuryActualAmount = curyAmt;

                #region PMTask Totals Update

                ta           = new PMTaskTotal();
                ta.ProjectID = ps.ProjectID;
                ta.TaskID    = ps.TaskID;

                string accType = ag.IsExpense == true ? AccountType.Expense : ag.Type;
                switch (accType)
                {
                case AccountType.Asset:
                    ta.CuryAsset = curyAmt;
                    ta.Asset     = amt;
                    break;

                case AccountType.Liability:
                    ta.CuryLiability = curyAmt;
                    ta.Liability     = amt;
                    break;

                case AccountType.Income:
                    ta.CuryIncome = curyAmt;
                    ta.Income     = amt;
                    break;

                case AccountType.Expense:
                    ta.CuryExpense = curyAmt;
                    ta.Expense     = amt;
                    break;
                }

                #endregion

                #region History
                PMHistory hist = new PMHistory();
                hist.ProjectID      = ps.ProjectID;
                hist.ProjectTaskID  = ps.TaskID;
                hist.AccountGroupID = ps.AccountGroupID;
                hist.InventoryID    = pmt.InventoryID ?? ps.InventoryID;
                hist.CostCodeID     = pmt.CostCodeID ?? ps.CostCodeID;
                hist.PeriodID       = pmt.FinPeriodID;
                hist.BranchID       = pmt.BranchID;
                decimal baseQty = 0;
                list.Add(hist);
                if (pmt.InventoryID != null && pmt.InventoryID != PMInventorySelectorAttribute.EmptyInventoryID && pmt.Qty != 0)
                {
                    if (PXAccess.FeatureInstalled <FeaturesSet.multipleUnitMeasure>())
                    {
                        baseQty = qtySign * IN.INUnitAttribute.ConvertToBase(graph.Caches[typeof(PMHistory)], pmt.InventoryID, pmt.UOM, pmt.Qty.Value, PX.Objects.IN.INPrecision.QUANTITY);
                    }
                    else
                    {
                        IN.InventoryItem initem = PXSelectorAttribute.Select <PMTran.inventoryID>(graph.Caches[typeof(PMTran)], pmt) as IN.InventoryItem;
                        if (initem != null && !string.IsNullOrEmpty(pmt.UOM))
                        {
                            baseQty = qtySign * IN.INUnitAttribute.ConvertGlobalUnits(graph, pmt.UOM, initem.BaseUnit, pmt.Qty ?? 0, IN.INPrecision.QUANTITY);
                        }
                    }
                }
                hist.FinPTDCuryAmount = curyAmt;
                hist.FinPTDAmount     = amt;
                hist.FinYTDCuryAmount = curyAmt;
                hist.FinYTDAmount     = amt;
                hist.FinPTDQty        = baseQty;
                hist.FinYTDQty        = baseQty;
                if (pmt.FinPeriodID == pmt.TranPeriodID)
                {
                    hist.TranPTDCuryAmount = curyAmt;
                    hist.TranPTDAmount     = amt;
                    hist.TranYTDCuryAmount = curyAmt;
                    hist.TranYTDAmount     = amt;
                    hist.TranPTDQty        = baseQty;
                    hist.TranYTDQty        = baseQty;
                }
                else
                {
                    PMHistory tranHist = new PMHistory();
                    tranHist.ProjectID      = ps.ProjectID;
                    tranHist.ProjectTaskID  = ps.TaskID;
                    tranHist.AccountGroupID = ps.AccountGroupID;
                    tranHist.InventoryID    = pmt.InventoryID ?? PM.PMInventorySelectorAttribute.EmptyInventoryID;
                    tranHist.CostCodeID     = pmt.CostCodeID ?? CostCodeAttribute.GetDefaultCostCode();
                    tranHist.PeriodID       = pmt.TranPeriodID;
                    tranHist.BranchID       = pmt.BranchID;
                    list.Add(tranHist);
                    tranHist.TranPTDCuryAmount = curyAmt;
                    tranHist.TranPTDAmount     = amt;
                    tranHist.TranYTDCuryAmount = curyAmt;
                    tranHist.TranYTDAmount     = amt;
                    tranHist.TranPTDQty        = baseQty;
                    tranHist.TranYTDQty        = baseQty;
                }
                #endregion

                forecast                  = new PMForecastHistory();
                forecast.ProjectID        = ps.ProjectID;
                forecast.ProjectTaskID    = ps.ProjectTaskID;
                forecast.AccountGroupID   = ps.AccountGroupID;
                forecast.InventoryID      = ps.InventoryID;
                forecast.CostCodeID       = ps.CostCodeID;
                forecast.PeriodID         = pmt.TranPeriodID;
                forecast.ActualQty        = ps.ActualQty;
                forecast.CuryActualAmount = ps.CuryActualAmount;
                forecast.ActualAmount     = ps.ActualAmount;
            }
            return(new Result(list, ps, ta, forecast));
        }
Пример #13
0
 public virtual PMBudgetLite SelectProjectBalance(IProjectFilter filter, PMAccountGroup ag, PMProject project, out bool isExisting)
 {
     return(SelectProjectBalance(ag, project, filter.TaskID, filter.InventoryID, filter.CostCodeID, out isExisting));
 }
Пример #14
0
        protected virtual PMBudgetLite BuildTarget(BudgetKeyTuple key, PMAccountGroup accountGroup, string budgetLevel, string budgetUpdateMode)
        {
            if (accountGroup == null)
            {
                throw new ArgumentNullException(nameof(accountGroup));
            }
            if (accountGroup.GroupID != key.AccountGroupID)
            {
                throw new ArgumentException("AccountGroup doesnot match key.AccountGroupID");
            }

            PMBudgetLite target = new PMBudgetLite();

            target.ProjectID      = key.ProjectID;
            target.ProjectTaskID  = key.ProjectTaskID;
            target.AccountGroupID = key.AccountGroupID;
            target.InventoryID    = PMInventorySelectorAttribute.EmptyInventoryID;
            target.CostCodeID     = CostCodeAttribute.GetDefaultCostCode();
            target.IsProduction   = false;
            target.Type           = accountGroup.IsExpense == true ? GL.AccountType.Expense : accountGroup.Type;

            if (budgetLevel == BudgetLevels.Task)
            {
                //default
            }
            else if (budgetLevel == BudgetLevels.Item)
            {
                target.InventoryID = budgetUpdateMode == CostBudgetUpdateModes.Summary ? PMInventorySelectorAttribute.EmptyInventoryID : key.InventoryID;
                if (target.InventoryID != PMInventorySelectorAttribute.EmptyInventoryID)
                {
                    InventoryItem item = PXSelect <InventoryItem, Where <InventoryItem.inventoryID, Equal <Required <InventoryItem.inventoryID> > > > .Select(graph, target.InventoryID);

                    if (item != null)
                    {
                        target.Description = item.Descr;
                        target.UOM         = item.BaseUnit;
                    }
                }
            }
            else if (budgetLevel == BudgetLevels.CostCode)
            {
                target.CostCodeID = budgetUpdateMode == CostBudgetUpdateModes.Summary ? CostCodeAttribute.GetDefaultCostCode() : key.CostCodeID;
                PMCostCode costCode = PXSelect <PMCostCode, Where <PMCostCode.costCodeID, Equal <Required <PMCostCode.costCodeID> > > > .Select(graph, target.CostCodeID);

                if (costCode != null)
                {
                    target.Description = costCode.Description;
                }
            }
            else if (budgetLevel == BudgetLevels.Detail)
            {
                target.InventoryID = budgetUpdateMode == CostBudgetUpdateModes.Summary ? PMInventorySelectorAttribute.EmptyInventoryID : key.InventoryID;
                target.CostCodeID  = budgetUpdateMode == CostBudgetUpdateModes.Summary ? CostCodeAttribute.GetDefaultCostCode() : key.CostCodeID;
                PMCostCode costCode = PXSelect <PMCostCode, Where <PMCostCode.costCodeID, Equal <Required <PMCostCode.costCodeID> > > > .Select(graph, target.CostCodeID);

                if (costCode != null)
                {
                    target.Description = costCode.Description;
                }
            }
            else
            {
                throw new ArgumentException(string.Format("Unknown budget level = {0}", budgetLevel), nameof(budgetLevel));
            }

            return(target);
        }
        public virtual Result Calculate(PMTran pmt, PMAccountGroup ag, Account acc, int mult)
        {
            PMBudget status = SelectProjectBalance(pmt);

            int?      inventoryID = status != null ? status.InventoryID : PMInventorySelectorAttribute.EmptyInventoryID;
            int?      costCodeID  = status != null ? status.CostCodeID : CostCodeAttribute.GetDefaultCostCode();
            RollupQty rollup      = null;

            if (settings.CostBudgetUpdateMode == CostBudgetUpdateModes.Detailed && ag.IsExpense == true && pmt.InventoryID != settings.EmptyInventoryID)
            {
                if (status == null || status.InventoryID == settings.EmptyInventoryID)
                {
                    rollup      = new RollupQty(pmt.UOM, pmt.Qty);
                    inventoryID = pmt.InventoryID;
                    if (pmt.CostCodeID != null)
                    {
                        costCodeID = pmt.CostCodeID;
                    }
                }
            }

            if (rollup == null)
            {
                rollup = CalculateRollupQty(pmt, status);
            }

            List <PMHistory> list = new List <PMHistory>();

            PMTaskTotal ta = null;
            PMBudget    ps = null;

            if (pmt.TaskID != null && (rollup.Qty != 0 || pmt.Amount != 0))             //TaskID will be null for Contract
            {
                ps                = new PMBudget();
                ps.ProjectID      = pmt.ProjectID;
                ps.ProjectTaskID  = pmt.TaskID;
                ps.AccountGroupID = ag.GroupID;
                if (ag.Type == PMAccountType.OffBalance)
                {
                    ps.Type = ag.IsExpense == true ? GL.AccountType.Expense : ag.Type;
                }
                else
                {
                    ps.Type = ag.Type;
                }
                ps.InventoryID = inventoryID;
                ps.CostCodeID  = costCodeID;
                ps.UOM         = rollup.UOM;
                if (status != null)
                {
                    ps.IsProduction = status.IsProduction;
                }

                decimal amt = mult * pmt.Amount.GetValueOrDefault();

                if (!string.IsNullOrEmpty(ps.UOM))
                {
                    ps.ActualQty = rollup.Qty;                     // commented out otherwise invoice produces -ve Qty. * mult;
                }
                ps.ActualAmount = amt;

                #region PMTask Totals Update

                ta           = new PMTaskTotal();
                ta.ProjectID = pmt.ProjectID;
                ta.TaskID    = pmt.TaskID;

                string accType = null;
                int    multFix = 1;             //flip back the sign if it was changed because of ag.Type<>acc.type
                if (pmt.TranType == BatchModule.PM && acc != null && !string.IsNullOrEmpty(acc.Type))
                {
                    //Only transactions that originated in PM were inverted and require to be fixed.
                    accType = ag.Type;

                    if (acc.Type != ag.Type)
                    {
                        multFix = -1;
                    }
                }
                else
                {
                    accType = ag.Type;
                }

                switch (accType)
                {
                case AccountType.Asset:
                    ta.Asset = amt * multFix;
                    break;

                case AccountType.Liability:
                    ta.Liability = amt * multFix;
                    break;

                case AccountType.Income:
                    ta.Income = amt * multFix;
                    break;

                case AccountType.Expense:
                    ta.Expense = amt * multFix;
                    break;
                }

                #endregion

                #region History
                PMHistory hist = new PMHistory();
                hist.ProjectID      = pmt.ProjectID;
                hist.ProjectTaskID  = pmt.TaskID;
                hist.AccountGroupID = ag.GroupID;
                hist.InventoryID    = pmt.InventoryID ?? PMInventorySelectorAttribute.EmptyInventoryID;
                hist.CostCodeID     = pmt.CostCodeID ?? CostCodeAttribute.GetDefaultCostCode();
                hist.PeriodID       = pmt.FinPeriodID;
                decimal baseQty = 0;
                list.Add(hist);
                if (pmt.InventoryID != null && pmt.InventoryID != PMInventorySelectorAttribute.EmptyInventoryID && pmt.Qty != 0 && !string.IsNullOrEmpty(rollup.UOM))
                {
                    if (PXAccess.FeatureInstalled <FeaturesSet.multipleUnitMeasure>())
                    {
                        baseQty = mult * IN.INUnitAttribute.ConvertToBase(graph.Caches[typeof(PMHistory)], pmt.InventoryID, pmt.UOM, pmt.Qty.Value, PX.Objects.IN.INPrecision.QUANTITY);
                    }
                    else
                    {
                        IN.InventoryItem initem = PXSelectorAttribute.Select <PMTran.inventoryID>(graph.Caches[typeof(PMTran)], pmt) as IN.InventoryItem;
                        if (initem != null)
                        {
                            baseQty = mult * IN.INUnitAttribute.ConvertGlobalUnits(graph, pmt.UOM, initem.BaseUnit, pmt.Qty ?? 0, IN.INPrecision.QUANTITY);
                        }
                    }
                }
                hist.FinPTDAmount = amt;
                hist.FinYTDAmount = amt;
                hist.FinPTDQty    = baseQty;
                hist.FinYTDQty    = baseQty;
                if (pmt.FinPeriodID == pmt.TranPeriodID)
                {
                    hist.TranPTDAmount = amt;
                    hist.TranYTDAmount = amt;
                    hist.TranPTDQty    = baseQty;
                    hist.TranYTDQty    = baseQty;
                }
                else
                {
                    PMHistory tranHist = new PMHistory();
                    tranHist.ProjectID      = pmt.ProjectID;
                    tranHist.ProjectTaskID  = pmt.TaskID;
                    tranHist.AccountGroupID = ag.GroupID;
                    tranHist.InventoryID    = pmt.InventoryID ?? PM.PMInventorySelectorAttribute.EmptyInventoryID;
                    tranHist.CostCodeID     = pmt.CostCodeID ?? CostCodeAttribute.GetDefaultCostCode();
                    tranHist.PeriodID       = pmt.TranPeriodID;
                    list.Add(tranHist);
                    tranHist.TranPTDAmount = amt;
                    tranHist.TranYTDAmount = amt;
                    tranHist.TranPTDQty    = baseQty;
                    tranHist.TranYTDQty    = baseQty;
                }
                #endregion
            }
            return(new Result(list, ps, ta));
        }
        public virtual void RunProjectBalanceVerification(PMProject project)
        {
            PXDatabase.Delete <PMTaskTotal>(new PXDataFieldRestrict(typeof(PMTaskTotal.projectID).Name, PXDbType.Int, 4, project.ContractID, PXComp.EQ));
            PXDatabase.Delete <PMTaskAllocTotal>(new PXDataFieldRestrict(typeof(PMTaskAllocTotal.projectID).Name, PXDbType.Int, 4, project.ContractID, PXComp.EQ));
            PXDatabase.Delete <PMHistory>(new PXDataFieldRestrict(typeof(PMHistory.projectID).Name, PXDbType.Int, 4, project.ContractID, PXComp.EQ));

            foreach (PMProjectStatusEx status in PXSelect <PMProjectStatusEx, Where <PMProjectStatusEx.projectID, Equal <Required <PMProjectStatusEx.projectID> > > > .Select(this, project.ContractID))
            {
                PXDatabase.Update <PMProjectStatus>(new PXDataFieldRestrict(typeof(PMProjectStatus.projectID).Name, PXDbType.Int, 4, project.ContractID, PXComp.EQ),
                                                    new PXDataFieldAssign(typeof(PMProjectStatus.actualAmount).Name, PXDbType.Decimal, 0m),
                                                    new PXDataFieldAssign(typeof(PMProjectStatus.actualQty).Name, PXDbType.Decimal, 0m));

                PMHistory2Accum hist2 = new PMHistory2Accum();
                hist2.ProjectID      = status.ProjectID;
                hist2.ProjectTaskID  = status.ProjectTaskID;
                hist2.AccountGroupID = status.AccountGroupID;
                hist2.InventoryID    = status.InventoryID ?? PMProjectStatus.EmptyInventoryID;
                hist2.PeriodID       = status.PeriodID;

                hist2 = History2.Insert(hist2);
                hist2.PTDBudgetAmount  += status.Amount;
                hist2.PTDBudgetQty     += status.Qty;
                hist2.BudgetAmount     += status.Amount;
                hist2.BudgetQty        += status.Qty;
                hist2.PTDRevisedAmount += status.RevisedAmount;
                hist2.PTDRevisedQty    += status.RevisedQty;
                hist2.RevisedAmount    += status.RevisedAmount;
                hist2.RevisedQty       += status.RevisedQty;
            }

            PXSelectBase <PMTran> select = new PXSelectJoin <PMTran,
                                                             LeftJoin <Account, On <PMTran.accountID, Equal <Account.accountID> >,
                                                                       InnerJoin <PMProject, On <PMProject.contractID, Equal <PMTran.projectID> >,
                                                                                  LeftJoin <PMTask, On <PMTask.projectID, Equal <PMTran.projectID>, And <PMTask.taskID, Equal <PMTran.taskID> > >,
                                                                                            LeftJoin <OffsetAccount, On <PMTran.offsetAccountID, Equal <OffsetAccount.accountID> >,
                                                                                                      LeftJoin <PMAccountGroup, On <PMAccountGroup.groupID, Equal <Account.accountGroupID> >,
                                                                                                                LeftJoin <OffsetPMAccountGroup, On <OffsetPMAccountGroup.groupID, Equal <OffsetAccount.accountGroupID> > > > > > > >,
                                                             Where <PMTran.projectID, Equal <Required <PMTran.projectID> >,
                                                                    And <PMTran.released, Equal <True> > > >(this);


            foreach (PXResult <PMTran, Account, PMProject, PMTask, OffsetAccount, PMAccountGroup, OffsetPMAccountGroup> res in select.Select(project.ContractID))
            {
                PMTran               tran      = (PMTran)res;
                Account              acc       = (Account)res;
                PMAccountGroup       ag        = (PMAccountGroup)res;
                OffsetAccount        offsetAcc = (OffsetAccount)res;
                OffsetPMAccountGroup offsetAg  = (OffsetPMAccountGroup)res;
                PMTask               task      = (PMTask)res;

                IList <PMHistory> list = RegisterReleaseProcess.UpdateProjectBalance(this, task, tran, acc, ag, offsetAcc, offsetAg);

                #region History Update
                foreach (PMHistory item in list)
                {
                    PMHistoryAccum hist = new PMHistoryAccum();
                    hist.ProjectID      = item.ProjectID;
                    hist.ProjectTaskID  = item.ProjectTaskID;
                    hist.AccountGroupID = item.AccountGroupID;
                    hist.InventoryID    = item.InventoryID;
                    hist.PeriodID       = item.PeriodID;

                    hist = History.Insert(hist);
                    hist.FinPTDAmount  += item.FinPTDAmount.GetValueOrDefault();
                    hist.FinYTDAmount  += item.FinYTDAmount.GetValueOrDefault();
                    hist.FinPTDQty     += item.FinPTDQty.GetValueOrDefault();
                    hist.FinYTDQty     += item.FinYTDQty.GetValueOrDefault();
                    hist.TranPTDAmount += item.TranPTDAmount.GetValueOrDefault();
                    hist.TranYTDAmount += item.TranYTDAmount.GetValueOrDefault();
                    hist.TranPTDQty    += item.TranPTDQty.GetValueOrDefault();
                    hist.TranYTDQty    += item.TranYTDQty.GetValueOrDefault();
                }



                #endregion
            }



            PXSelectBase <PMTran> select2 = new PXSelect <PMTran,
                                                          Where <PMTran.origProjectID, Equal <Required <PMTran.origProjectID> >,
                                                                 And <PMTran.origTaskID, IsNotNull,
                                                                      And <PMTran.origAccountGroupID, IsNotNull> > > >(this);

            foreach (PMTran tran in select2.Select(project.ContractID))
            {
                PMTaskAllocTotalAccum tat = new PMTaskAllocTotalAccum();
                tat.ProjectID      = tran.OrigProjectID;
                tat.TaskID         = tran.OrigTaskID;
                tat.AccountGroupID = tran.OrigAccountGroupID;
                tat.InventoryID    = tran.InventoryID;

                tat           = AllocationTotals.Insert(tat);
                tat.Amount   += tran.Amount;
                tat.Quantity += tran.Qty;
            }

            foreach (PMProjectStatusAccum item in this.Caches[typeof(PMProjectStatusAccum)].Inserted)
            {
                Debug.Print("Task={0} AG={1} Qty={2} Amt={3}", item.ProjectTaskID, item.AccountGroupID, item.ActualQty, item.ActualAmount);
            }
        }
Пример #17
0
        public virtual void RunProjectBalanceVerification(PMProject project, bool recalculateUnbilledSummary)
        {
            PXDatabase.Delete <PMTaskTotal>(new PXDataFieldRestrict(typeof(PMTaskTotal.projectID).Name, PXDbType.Int, 4, project.ContractID, PXComp.EQ));
            PXDatabase.Delete <PMTaskAllocTotal>(new PXDataFieldRestrict(typeof(PMTaskAllocTotal.projectID).Name, PXDbType.Int, 4, project.ContractID, PXComp.EQ));
            PXDatabase.Delete <PMHistory>(new PXDataFieldRestrict(typeof(PMHistory.projectID).Name, PXDbType.Int, 4, project.ContractID, PXComp.EQ));

            if (recalculateUnbilledSummary)
            {
                PXDatabase.Delete <PMUnbilledDailySummary>(new PXDataFieldRestrict(typeof(PMUnbilledDailySummary.projectID).Name, PXDbType.Int, 4, project.ContractID, PXComp.EQ));
            }


            foreach (PMProjectStatusEx status in PXSelect <PMProjectStatusEx, Where <PMProjectStatusEx.projectID, Equal <Required <PMProjectStatusEx.projectID> > > > .Select(this, project.ContractID))
            {
                PXDatabase.Update <PMProjectStatus>(new PXDataFieldRestrict(typeof(PMProjectStatus.projectID).Name, PXDbType.Int, 4, project.ContractID, PXComp.EQ),
                                                    new PXDataFieldAssign(typeof(PMProjectStatus.actualAmount).Name, PXDbType.Decimal, 0m),
                                                    new PXDataFieldAssign(typeof(PMProjectStatus.actualQty).Name, PXDbType.Decimal, 0m));

                PMHistory2Accum hist2 = new PMHistory2Accum();
                hist2.ProjectID      = status.ProjectID;
                hist2.ProjectTaskID  = status.ProjectTaskID;
                hist2.AccountGroupID = status.AccountGroupID;
                hist2.InventoryID    = status.InventoryID ?? PMProjectStatus.EmptyInventoryID;
                hist2.PeriodID       = status.PeriodID;

                hist2 = History2.Insert(hist2);
                hist2.PTDBudgetAmount  += status.Amount;
                hist2.PTDBudgetQty     += status.Qty;
                hist2.BudgetAmount     += status.Amount;
                hist2.BudgetQty        += status.Qty;
                hist2.PTDRevisedAmount += status.RevisedAmount;
                hist2.PTDRevisedQty    += status.RevisedQty;
                hist2.RevisedAmount    += status.RevisedAmount;
                hist2.RevisedQty       += status.RevisedQty;
            }

            PXSelectBase <PMTran> select = new PXSelectJoinGroupBy <PMTran,
                                                                    LeftJoin <Account, On <PMTran.accountID, Equal <Account.accountID> >,
                                                                              LeftJoin <OffsetAccount, On <PMTran.offsetAccountID, Equal <OffsetAccount.accountID> >,
                                                                                        LeftJoin <PMAccountGroup, On <PMAccountGroup.groupID, Equal <Account.accountGroupID> >,
                                                                                                  LeftJoin <OffsetPMAccountGroup, On <OffsetPMAccountGroup.groupID, Equal <OffsetAccount.accountGroupID> > > > > >,
                                                                    Where <PMTran.projectID, Equal <Required <PMTran.projectID> >,
                                                                           And <PMTran.released, Equal <True> > >,
                                                                    Aggregate <GroupBy <PMTran.tranType,
                                                                                        GroupBy <PMTran.finPeriodID,
                                                                                                 GroupBy <PMTran.tranPeriodID,
                                                                                                          GroupBy <PMTran.projectID,
                                                                                                                   GroupBy <PMTran.taskID,
                                                                                                                            GroupBy <PMTran.inventoryID,
                                                                                                                                     GroupBy <PMTran.accountID,
                                                                                                                                              GroupBy <PMTran.accountGroupID,
                                                                                                                                                       GroupBy <PMTran.offsetAccountID,
                                                                                                                                                                GroupBy <PMTran.offsetAccountGroupID,
                                                                                                                                                                         GroupBy <PMTran.uOM,
                                                                                                                                                                                  GroupBy <PMTran.released,
                                                                                                                                                                                           Sum <PMTran.qty,
                                                                                                                                                                                                Sum <PMTran.amount> > > > > > > > > > > > > > > >(this);

            if (recalculateUnbilledSummary)
            {
                select = new PXSelectJoinGroupBy <PMTran,
                                                  LeftJoin <Account, On <PMTran.accountID, Equal <Account.accountID> >,
                                                            LeftJoin <OffsetAccount, On <PMTran.offsetAccountID, Equal <OffsetAccount.accountID> >,
                                                                      LeftJoin <PMAccountGroup, On <PMAccountGroup.groupID, Equal <Account.accountGroupID> >,
                                                                                LeftJoin <OffsetPMAccountGroup, On <OffsetPMAccountGroup.groupID, Equal <OffsetAccount.accountGroupID> > > > > >,
                                                  Where <PMTran.projectID, Equal <Required <PMTran.projectID> >,
                                                         And <PMTran.released, Equal <True> > >,
                                                  Aggregate <GroupBy <PMTran.tranType,
                                                                      GroupBy <PMTran.finPeriodID,
                                                                               GroupBy <PMTran.tranPeriodID,
                                                                                        GroupBy <PMTran.projectID,
                                                                                                 GroupBy <PMTran.taskID,
                                                                                                          GroupBy <PMTran.inventoryID,
                                                                                                                   GroupBy <PMTran.date,
                                                                                                                            GroupBy <PMTran.accountID,
                                                                                                                                     GroupBy <PMTran.accountGroupID,
                                                                                                                                              GroupBy <PMTran.offsetAccountID,
                                                                                                                                                       GroupBy <PMTran.offsetAccountGroupID,
                                                                                                                                                                GroupBy <PMTran.uOM,
                                                                                                                                                                         GroupBy <PMTran.released,
                                                                                                                                                                                  Sum <PMTran.qty,
                                                                                                                                                                                       Sum <PMTran.amount,
                                                                                                                                                                                            Max <PMTran.billable,
                                                                                                                                                                                                 Max <PMTran.billed,
                                                                                                                                                                                                      Max <PMTran.reversed> > > > > > > > > > > > > > > > > > > >(this);
            }
            else
            {
                select = new PXSelectJoinGroupBy <PMTran,
                                                  LeftJoin <Account, On <PMTran.accountID, Equal <Account.accountID> >,
                                                            LeftJoin <OffsetAccount, On <PMTran.offsetAccountID, Equal <OffsetAccount.accountID> >,
                                                                      LeftJoin <PMAccountGroup, On <PMAccountGroup.groupID, Equal <Account.accountGroupID> >,
                                                                                LeftJoin <OffsetPMAccountGroup, On <OffsetPMAccountGroup.groupID, Equal <OffsetAccount.accountGroupID> > > > > >,
                                                  Where <PMTran.projectID, Equal <Required <PMTran.projectID> >,
                                                         And <PMTran.released, Equal <True> > >,
                                                  Aggregate <GroupBy <PMTran.tranType,
                                                                      GroupBy <PMTran.finPeriodID,
                                                                               GroupBy <PMTran.tranPeriodID,
                                                                                        GroupBy <PMTran.projectID,
                                                                                                 GroupBy <PMTran.taskID,
                                                                                                          GroupBy <PMTran.inventoryID,
                                                                                                                   GroupBy <PMTran.accountID,
                                                                                                                            GroupBy <PMTran.accountGroupID,
                                                                                                                                     GroupBy <PMTran.offsetAccountID,
                                                                                                                                              GroupBy <PMTran.offsetAccountGroupID,
                                                                                                                                                       GroupBy <PMTran.uOM,
                                                                                                                                                                GroupBy <PMTran.released,
                                                                                                                                                                         Sum <PMTran.qty,
                                                                                                                                                                              Sum <PMTran.amount> > > > > > > > > > > > > > > >(this);
            }

            foreach (PXResult <PMTran, Account, OffsetAccount, PMAccountGroup, OffsetPMAccountGroup> res in select.Select(project.ContractID))
            {
                PMTran               tran      = (PMTran)res;
                Account              acc       = (Account)res;
                PMAccountGroup       ag        = (PMAccountGroup)res;
                OffsetAccount        offsetAcc = (OffsetAccount)res;
                OffsetPMAccountGroup offsetAg  = (OffsetPMAccountGroup)res;

                //suppose we have allocated unbilled 100 - unearned 100
                //during billing we reduced the amount to 80.
                //as a result of this. only 80 will be reversed. leaving 20 on the unbilled.
                //plus a remainder transaction will be generated. (if we allow this remainder to update balance it will add additional 20 to the unbilled.)
                if (tran.RemainderOfTranID != null)
                {
                    continue;                     //skip remainder transactions.
                }
                IList <PMHistory> list = RegisterReleaseProcess.UpdateProjectBalance(this, tran, acc, ag, offsetAcc, offsetAg);
                RegisterReleaseProcess.AddToUnbilledSummary(this, tran);

                #region History Update
                foreach (PMHistory item in list)
                {
                    PMHistoryAccum hist = new PMHistoryAccum();
                    hist.ProjectID      = item.ProjectID;
                    hist.ProjectTaskID  = item.ProjectTaskID;
                    hist.AccountGroupID = item.AccountGroupID;
                    hist.InventoryID    = item.InventoryID;
                    hist.PeriodID       = item.PeriodID;

                    hist = History.Insert(hist);
                    hist.FinPTDAmount  += item.FinPTDAmount.GetValueOrDefault();
                    hist.FinYTDAmount  += item.FinYTDAmount.GetValueOrDefault();
                    hist.FinPTDQty     += item.FinPTDQty.GetValueOrDefault();
                    hist.FinYTDQty     += item.FinYTDQty.GetValueOrDefault();
                    hist.TranPTDAmount += item.TranPTDAmount.GetValueOrDefault();
                    hist.TranYTDAmount += item.TranYTDAmount.GetValueOrDefault();
                    hist.TranPTDQty    += item.TranPTDQty.GetValueOrDefault();
                    hist.TranYTDQty    += item.TranYTDQty.GetValueOrDefault();
                }



                #endregion
            }

            PXSelectBase <PMTran> select2 = new PXSelect <PMTran,
                                                          Where <PMTran.origProjectID, Equal <Required <PMTran.origProjectID> >,
                                                                 And <PMTran.origTaskID, IsNotNull,
                                                                      And <PMTran.origAccountGroupID, IsNotNull> > > >(this);

            foreach (PMTran tran in select2.Select(project.ContractID))
            {
                PMTaskAllocTotalAccum tat = new PMTaskAllocTotalAccum();
                tat.ProjectID      = tran.OrigProjectID;
                tat.TaskID         = tran.OrigTaskID;
                tat.AccountGroupID = tran.OrigAccountGroupID;
                tat.InventoryID    = tran.InventoryID;

                tat           = AllocationTotals.Insert(tat);
                tat.Amount   += tran.Amount;
                tat.Quantity += tran.Qty;
            }

            //foreach (PMProjectStatusAccum item in this.Caches[typeof(PMProjectStatusAccum)].Inserted)
            //{
            //	Debug.Print("Task={0} AG={1} Qty={2} Amt={3}", item.ProjectTaskID, item.AccountGroupID, item.ActualQty, item.ActualAmount);

            //}
        }
        public virtual List <Result> Calculate(PMProject project, PMTran tran, Account acc, PMAccountGroup ag, Account offsetAcc, PMAccountGroup offsetAg)
        {
            List <Result> list = new List <Result>();

            if (tran.TaskID != null)
            {
                int invert = 1;
                if (tran.TranType == BatchModule.PM && IsFlipRequired(acc.Type, ag.Type))
                {
                    //Invert transactions that originated in PM. All other transactions were already inverted when they were transformed from GLTran to PMTran.
                    invert = -1;
                }

                bool debitcreditcancelout = false;
                if (offsetAcc != null && acc.AccountID == offsetAcc.AccountID)
                {
                    debitcreditcancelout = true;
                }
                if (offsetAg != null && ag.GroupID == offsetAg.GroupID)
                {
                    debitcreditcancelout = true;
                }

                if (offsetAcc != null && string.IsNullOrEmpty(acc.Type) && offsetAcc.AccountGroupID == tran.AccountGroupID)
                {
                    return(list);
                }

                if (string.IsNullOrEmpty(ag.Type))
                {
                    //offbalance tran.
                    PMAccountGroup tranAG = PXSelect <PMAccountGroup, Where <PMAccountGroup.groupID, Equal <Required <PMAccountGroup.groupID> > > > .Select(graph, tran.AccountGroupID);

                    if (tranAG != null)
                    {
                        //DEBIT ONLY
                        if (tranAG.Type == AccountType.Income || tranAG.Type == AccountType.Liability)
                        {
                            list.Add(Calculate(project, tran, tranAG, null, -1 * invert));
                        }
                        else
                        {
                            list.Add(Calculate(project, tran, tranAG, null, 1 * invert));
                        }
                    }
                }
                else
                {
                    if (!debitcreditcancelout)
                    {
                        //DEBIT
                        if (acc.Type == AccountType.Income || acc.Type == AccountType.Liability)
                        {
                            list.Add(Calculate(project, tran, ag, acc, -1 * invert));
                        }
                        else
                        {
                            list.Add(Calculate(project, tran, ag, acc, 1 * invert));
                        }
                    }
                }

                //CREDIT
                if (offsetAcc != null && offsetAg != null && offsetAcc.AccountID != null && offsetAg.GroupID != null && !debitcreditcancelout)
                {
                    int offsetInvert = 1;
                    if (IsFlipRequired(offsetAcc.Type, offsetAg.Type))
                    {
                        offsetInvert = -1;
                    }

                    if (offsetAcc.Type == AccountType.Income || offsetAcc.Type == AccountType.Liability)
                    {
                        list.Add(Calculate(project, tran, offsetAg, offsetAcc, 1 * offsetInvert));
                    }
                    else
                    {
                        list.Add(Calculate(project, tran, offsetAg, offsetAcc, -1 * offsetInvert));
                    }
                }
            }

            return(list);
        }
        public virtual void ProcessTransaction(PMProject project, PXResult <PMTran, Account, OffsetAccount, PMAccountGroup, OffsetPMAccountGroup> res, ProjectBalance pb)
        {
            PMTran               tran      = (PMTran)res;
            Account              acc       = (Account)res;
            PMAccountGroup       ag        = (PMAccountGroup)res;
            OffsetAccount        offsetAcc = (OffsetAccount)res;
            OffsetPMAccountGroup offsetAg  = (OffsetPMAccountGroup)res;

            IList <ProjectBalance.Result> balances = pb.Calculate(project, tran, acc, ag, offsetAcc, offsetAg);

            foreach (ProjectBalance.Result balance in balances)
            {
                if (balance.Status != null)
                {
                    PMBudgetAccum ps = new PMBudgetAccum();
                    ps.ProjectID      = balance.Status.ProjectID;
                    ps.ProjectTaskID  = balance.Status.ProjectTaskID;
                    ps.AccountGroupID = balance.Status.AccountGroupID;
                    ps.InventoryID    = balance.Status.InventoryID;
                    ps.CostCodeID     = balance.Status.CostCodeID;
                    ps.UOM            = balance.Status.UOM;
                    ps.IsProduction   = balance.Status.IsProduction;
                    ps.Type           = balance.Status.Type;
                    ps.Description    = balance.Status.Description;

                    ps = Budget.Insert(ps);

                    ps.ActualQty    += balance.Status.ActualQty.GetValueOrDefault();
                    ps.ActualAmount += balance.Status.ActualAmount.GetValueOrDefault();
                }

                if (balance.TaskTotal != null)
                {
                    PMTaskTotal ta = new PMTaskTotal();
                    ta.ProjectID = balance.TaskTotal.ProjectID;
                    ta.TaskID    = balance.TaskTotal.TaskID;

                    ta            = TaskTotals.Insert(ta);
                    ta.Asset     += balance.TaskTotal.Asset.GetValueOrDefault();
                    ta.Liability += balance.TaskTotal.Liability.GetValueOrDefault();
                    ta.Income    += balance.TaskTotal.Income.GetValueOrDefault();
                    ta.Expense   += balance.TaskTotal.Expense.GetValueOrDefault();
                }


                foreach (PMHistory item in balance.History)
                {
                    PMHistoryAccum hist = new PMHistoryAccum();
                    hist.ProjectID      = item.ProjectID;
                    hist.ProjectTaskID  = item.ProjectTaskID;
                    hist.AccountGroupID = item.AccountGroupID;
                    hist.InventoryID    = item.InventoryID;
                    hist.CostCodeID     = item.CostCodeID;
                    hist.PeriodID       = item.PeriodID;

                    hist = History.Insert(hist);
                    hist.FinPTDAmount  += item.FinPTDAmount.GetValueOrDefault();
                    hist.FinYTDAmount  += item.FinYTDAmount.GetValueOrDefault();
                    hist.FinPTDQty     += item.FinPTDQty.GetValueOrDefault();
                    hist.FinYTDQty     += item.FinYTDQty.GetValueOrDefault();
                    hist.TranPTDAmount += item.TranPTDAmount.GetValueOrDefault();
                    hist.TranYTDAmount += item.TranYTDAmount.GetValueOrDefault();
                    hist.TranPTDQty    += item.TranPTDQty.GetValueOrDefault();
                    hist.TranYTDQty    += item.TranYTDQty.GetValueOrDefault();
                }
            }
        }
        public string AccountGroupFromID(int?accountGroupID)
        {
            PMAccountGroup ag = PXSelect <PMAccountGroup, Where <PMAccountGroup.groupID, Equal <Required <PMAccountGroup.groupID> > > > .Select(this, accountGroupID);

            return(ag.GroupCD);
        }
        public virtual PMTran ExpenseTransactionFromBudget(PMBudget budget, PMProject project, PMTask task, PMAccountGroup accountGroup, InventoryItem item, PMCostCode costcode)
        {
            PMTran tran = new PMTran();

            tran.AccountGroupID = budget.AccountGroupID;
            tran.ProjectID      = budget.ProjectID;
            tran.TaskID         = budget.ProjectTaskID;
            tran.InventoryID    = budget.InventoryID;
            tran.AccountID      = item.InventoryID != null ? item.COGSAcctID : accountGroup.AccountID;
            tran.SubID          = item.InventoryID != null ? item.COGSSubID : accountGroup.AccountID;
            tran.Amount         = budget.RevisedAmount;
            tran.Qty            = budget.RevisedQty;
            tran.UOM            = budget.UOM;
            tran.BAccountID     = task.CustomerID;
            tran.LocationID     = task.LocationID;
            tran.Billable       = true;
            tran.UseBillableQty = true;
            tran.BillableQty    = budget.RevisedQty;
            tran.Released       = true;

            return(tran);
        }