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 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); }
protected virtual PMBudgetLite SelectExistingBalance(BudgetKeyTuple key, string budgetLevel, string budgetUpdateMode) { PMBudgetLite result = null; PMBudgetLite fallback = null; foreach (PMBudgetLite budget in SelectExistingBalances(key, budgetLevel, budgetUpdateMode)) { if (budget.CostCodeID == key.CostCodeID && budget.InventoryID == key.InventoryID) { result = budget; break; } else { if (budgetLevel == BudgetLevels.CostCode) { if (budget.InventoryID == PMInventorySelectorAttribute.EmptyInventoryID && budget.CostCodeID == key.CostCodeID) { result = budget; break; } } else { if (budget.InventoryID == key.InventoryID && budget.CostCodeID == CostCodeAttribute.DefaultCostCode) { result = budget; break; } } if (budget.InventoryID == PMInventorySelectorAttribute.EmptyInventoryID && budget.CostCodeID == CostCodeAttribute.DefaultCostCode) { fallback = budget; } } } return(result ?? fallback); }
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(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)); }