protected virtual void _(Events.FieldDefaulting <PMBudget, PMBudget.description> e)
        {
            if (e.Row == null)
            {
                return;
            }

            if (CostCodeAttribute.UseCostCode())
            {
                if (e.Row.CostCodeID != null && e.Row.CostCodeID != CostCodeAttribute.GetDefaultCostCode())
                {
                    PMCostCode costCode = PXSelectorAttribute.Select <PMBudget.costCodeID>(e.Cache, e.Row) as PMCostCode;
                    if (costCode != null)
                    {
                        e.NewValue = costCode.Description;
                    }
                }
            }
            else
            {
                if (e.Row.InventoryID != null && e.Row.InventoryID != PMInventorySelectorAttribute.EmptyInventoryID)
                {
                    InventoryItem item = PXSelectorAttribute.Select <PMBudget.inventoryID>(e.Cache, e.Row) as InventoryItem;
                    if (item != null)
                    {
                        e.NewValue = item.Descr;
                    }
                }
            }
        }
Esempio n. 2
0
        public virtual void EnsureDefaultCostCode(PMSetup row)
        {
            PMCostCode costcode = DefaultCostCode.SelectWindowed(0, 1);

            if (costcode == null)
            {
                InsertDefaultCostCode(row);
            }
            else
            {
                if (costcode.CostCodeCD.Length != GetCostCodeLength())
                {
                    costcode.CostCodeCD = new string('0', GetCostCodeLength());
                    if (DefaultCostCode.Cache.GetStatus(costcode) == PXEntryStatus.Notchanged)
                    {
                        DefaultCostCode.Cache.SetStatus(costcode, PXEntryStatus.Updated);
                    }

                    DefaultCostCode.Cache.IsDirty = true;
                }
                if (costcode.NoteID == null)
                {
                    costcode.NoteID = Guid.NewGuid();
                    if (DefaultCostCode.Cache.GetStatus(costcode) == PXEntryStatus.Notchanged)
                    {
                        DefaultCostCode.Cache.SetStatus(costcode, PXEntryStatus.Updated);
                    }

                    DefaultCostCode.Cache.IsDirty = true;
                }
            }
        }
Esempio n. 3
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));
        }
Esempio n. 4
0
        public virtual void InsertDefaultCostCode(PMSetup row)
        {
            PMCostCode rec = new PMCostCode();

            rec.CostCodeCD  = "000000";
            rec.Description = "DEFAULT";
            rec.IsDefault   = true;
            rec             = DefaultCostCode.Insert(rec);
        }
Esempio n. 5
0
        public virtual void EnsureDefaultCostCode(PMSetup row)
        {
            PMCostCode costcode = DefaultCostCode.SelectWindowed(0, 1);

            if (costcode == null)
            {
                InsertDefaultCostCode(row);
            }
        }
Esempio n. 6
0
        public virtual void InsertDefaultCostCode(PMSetup row)
        {
            PMCostCode rec = new PMCostCode();

            rec.CostCodeCD  = new string('0', GetCostCodeLength());
            rec.Description = "DEFAULT";
            rec.IsDefault   = true;
            rec             = DefaultCostCode.Insert(rec);
        }
        public bool PrepareImportRow(string viewName, IDictionary keys, IDictionary values)
        {
            if (!CostCodeAttribute.UseCostCode())
            {
                PMCostCode defaultCostCode = PXSelect <PMCostCode, Where <PMCostCode.isDefault, Equal <True> > > .Select(this);

                if (defaultCostCode != null)
                {
                    keys[nameof(PMBudget.CostCodeID)]   = defaultCostCode.CostCodeCD;
                    values[nameof(PMBudget.CostCodeID)] = defaultCostCode.CostCodeCD;
                }
            }

            return(true);
        }
        public void FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
        {
            int?costCodeID = (int?)sender.GetValue(e.Row, costCodeField.Name);

            if (costCodeID != null && costCodeID != CostCodeAttribute.GetDefaultCostCode())
            {
                PMCostCode costCode = PXSelect <PMCostCode, Where <PMCostCode.costCodeID, Equal <Required <PMCostCode.costCodeID> > > > .Select(sender.Graph, costCodeID);

                if (costCode != null)
                {
                    var select = new PXSelect <PMWorkCode, Where <PMWorkCode.costCodeFrom, LessEqual <Required <PMWorkCode.costCodeFrom> >,
                                                                  And <PMWorkCode.costCodeTo, GreaterEqual <Required <PMWorkCode.costCodeTo> > > > >(sender.Graph);
                    PMWorkCode code = select.Select(costCode.CostCodeCD, costCode.CostCodeCD);
                    if (code != null)
                    {
                        e.NewValue = code.WorkCodeID;
                    }
                }
            }
        }
Esempio n. 9
0
        public override void SubstituteKeyFieldSelecting(PXCache sender, PXFieldSelectingEventArgs e)
        {
            object value = e.ReturnValue;

            e.ReturnValue = null;
            base.FieldSelecting(sender, e);

            PMCostCode costCode = PXSelect <PMCostCode, Where <PMCostCode.costCodeID, Equal <Required <PMCostCode.costCodeID> > > > .SelectWindowed(sender.Graph, 0, 1, value);

            if (costCode != null)
            {
                e.ReturnValue = costCode.CostCodeCD;
            }
            else
            {
                if (e.Row != null)
                {
                    e.ReturnValue = null;
                }
            }
        }
Esempio n. 10
0
        public override void SubstituteKeyFieldUpdating(PXCache sender, PXFieldUpdatingEventArgs e)
        {
            PMCostCode costCode = null;

            if (e.NewValue != null)
            {
                costCode = PXSelect <PMCostCode, Where <PMCostCode.costCodeCD, Equal <Required <PMCostCode.costCodeCD> > > > .SelectWindowed(sender.Graph, 0, 1, e.NewValue);

                if (costCode != null)
                {
                    e.NewValue = costCode.CostCodeID;
                    e.Cancel   = true;
                }
                else if (e.NewValue.GetType() == typeof(int))
                {
                    costCode = PXSelect <PMCostCode, Where <PMCostCode.costCodeID, Equal <Required <PMCostCode.costCodeID> > > > .SelectWindowed(sender.Graph, 0, 1, e.NewValue);
                }
                if (costCode == null)
                {
                    throw new PXSetPropertyException(PXMessages.LocalizeFormat(ErrorMessages.ValueDoesntExist, _FieldName, e.NewValue));
                }
            }
        }
        protected virtual IEnumerable GetRecords()
        {
            PXResultset <PMCostCode> resultset = PXSelect <PMCostCode> .Select(_Graph);

            Dictionary <int, PMCostCode> list = new Dictionary <int, PMCostCode>(resultset.Count);

            foreach (PMCostCode record in resultset)
            {
                record.IsProjectOverride = false;
                if (!list.ContainsKey(record.CostCodeID.Value))
                {
                    list.Add(record.CostCodeID.Value, record);
                }
            }

            if (!DisableProjectSpecific)
            {
                foreach (PMBudgetedCostCode budget in GetProjectSpecificRecords().Values)
                {
                    PMCostCode found = null;
                    if (list.TryGetValue(budget.CostCodeID.Value, out found))
                    {
                        PMCostCode record = new PMCostCode();
                        record.CostCodeID        = found.CostCodeID;
                        record.CostCodeCD        = found.CostCodeCD;
                        record.NoteID            = found.NoteID;
                        record.IsDefault         = false;
                        record.Description       = budget.Description;
                        record.IsProjectOverride = true;

                        list[budget.CostCodeID.Value] = record;
                    }
                }
            }

            return(list.Values);
        }
Esempio n. 12
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 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);
        }
        public virtual Result Calculate(PMProject project, PMTran pmt, PMAccountGroup ag, Account acc, int mult)
        {
            PMBudget existing = null;

            if (ag.Type == GL.AccountType.Income && project.BudgetLevel != BudgetLevels.CostCode)
            {
                existing = SelectProjectBalanceByInventory(pmt);
            }
            else
            {
                existing = SelectProjectBalance(pmt);
            }

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

            if (CostCodeAttribute.UseCostCode())
            {
                if (settings.CostBudgetUpdateMode == CostBudgetUpdateModes.Detailed && ag.IsExpense == true)
                {
                    if (existing == null || existing.CostCodeID == CostCodeAttribute.GetDefaultCostCode())
                    {
                        rollup = new RollupQty(pmt.UOM, pmt.Qty);
                        if (pmt.CostCodeID != null)
                        {
                            costCodeID = pmt.CostCodeID;
                        }
                    }
                }
                else if (ag.Type == GL.AccountType.Income && project.BudgetLevel != BudgetLevels.CostCode)
                {
                    costCodeID = CostCodeAttribute.GetDefaultCostCode();
                }
            }
            else
            {
                if (settings.CostBudgetUpdateMode == CostBudgetUpdateModes.Detailed && ag.IsExpense == true && pmt.InventoryID != settings.EmptyInventoryID)
                {
                    if (existing == null || existing.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, existing);
            }

            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 (existing != null)
                {
                    ps.IsProduction = existing.IsProduction;
                    ps.Description  = existing.Description;
                }
                else
                {
                    //initialize description:
                    if (CostCodeAttribute.UseCostCode())
                    {
                        PMCostCode costCode = PXSelect <PMCostCode, Where <PMCostCode.costCodeID, Equal <Required <PMCostCode.costCodeID> > > > .Select(graph, ps.CostCodeID);

                        if (costCode != null)
                        {
                            ps.Description = costCode.Description;
                        }
                    }
                    else if (inventoryID != PMInventorySelectorAttribute.EmptyInventoryID)
                    {
                        InventoryItem item = PXSelect <InventoryItem, Where <InventoryItem.inventoryID, Equal <Required <InventoryItem.inventoryID> > > > .Select(graph, inventoryID);

                        if (item != null)
                        {
                            ps.Description = item.Descr;
                        }
                    }
                }

                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;

                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.
                    if (IsFlipRequired(acc.Type, ag.Type))
                    {
                        multFix = -1;
                    }
                }

                string accType = ag.Type == PMAccountType.OffBalance && ag.IsExpense == true ? AccountType.Expense : 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)
                {
                    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));
        }