public static bool RecordCostTrans(RegisterEntry registerEntry, List <EPActivityApprove> activities)
        {
            registerEntry.FieldVerifying.AddHandler <PMTran.inventoryID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });//restriction should be applicable only for budgeting.
            EmployeeCostEngine costEngine = new EmployeeCostEngine(registerEntry);

            registerEntry.Views.Caches.Add(typeof(EPActivity));
            PXCache activityCache = registerEntry.Caches <EPActivity>();

            registerEntry.Document.Cache.Insert();
            bool success       = true;
            bool activityAdded = false;

            for (int i = 0; i < activities.Count; i++)
            {
                EPActivity activity = PXSelect <EPActivity> .Search <EPActivity.taskID>(registerEntry, activities[i].TaskID);

                if (activity.Released == true) //activity can be released to PM via Timecard prior to releasing the case.
                {
                    continue;
                }

                try
                {
                    if (activity.ProjectTaskID != null)                    //cost transactions are created only if project is set.
                    {
                        EPEmployee employee = PXSelect <EPEmployee> .Search <EPEmployee.userID>(registerEntry, activity.Owner);

                        activity.LabourItemID = costEngine.GetLaborClass(activity);
                        activityCache.Update(activity);

                        decimal?cost = costEngine.CalculateEmployeeCost(activity, employee.BAccountID, activity.StartDate.Value);
                        registerEntry.CreateTransaction(activity, employee.BAccountID, activity.StartDate.Value, activity.TimeSpent, activity.TimeBillable, cost);
                        activity.EmployeeRate = cost;
                        activityAdded         = true;
                    }

                    activity.Released = true;
                    activityCache.Update(activity);
                }
                catch (Exception e)
                {
                    PXProcessing <EPActivityApprove> .SetError(i, e is PXOuterException?e.Message + "\r\n" + String.Join("\r\n", ((PXOuterException)e).InnerMessages) : e.Message);

                    success = false;
                }
            }
            if (success)
            {
                if (activityAdded)
                {
                    registerEntry.Save.Press();
                }
                else
                {
                    activityCache.Persist(PXDBOperation.Update);
                }
            }

            return(success);
        }
Exemple #2
0
        public static bool RecordCostTrans(RegisterEntry registerEntry, List <EPActivityApprove> activities, out bool activityAdded)
        {
            registerEntry.FieldVerifying.AddHandler <PMTran.inventoryID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });           //restriction should be applicable only for budgeting.
            EmployeeCostEngine costEngine = new EmployeeCostEngine(registerEntry);

            registerEntry.Views.Caches.Add(typeof(EPActivityApprove));
            PXCache activityCache = registerEntry.Caches <EPActivityApprove>();

            registerEntry.Document.Cache.Insert();
            bool success = true;

            activityAdded = false;
            for (int i = 0; i < activities.Count; i++)
            {
                //Check the UNMERGED state of Released (it could be set to true just now by the calling code):
                EPActivityApprove activity = PXSelect <EPActivityApprove> .Search <EPActivityApprove.noteID>(registerEntry, activities[i].NoteID);

                if (activity.Released == true)                //activity can be released to PM via Timecard prior to releasing the case.
                {
                    continue;
                }

                try
                {
                    if (activity.ProjectTaskID != null)                    //cost transactions are created only if project is set.
                    {
                        EPEmployee employee = PXSelect <EPEmployee> .Search <EPEmployee.userID>(registerEntry, activity.OwnerID);

                        activity.LabourItemID = costEngine.GetLaborClass(activity);
                        if (activity.LabourItemID == null)
                        {
                            throw new PXException(Messages.CannotFindLabor, employee.AcctName);
                        }
                        activityCache.Update(activity);

                        var cost = costEngine.CalculateEmployeeCost(activity.TimeCardCD, activity.EarningTypeID, activity.LabourItemID, activity.ProjectID, activity.ProjectTaskID, activity.CertifiedJob, activity.UnionID, employee.BAccountID, activity.Date.Value);
                        if (cost == null)
                        {
                            throw new PXException(Messages.EmployeeCostRateNotFound);
                        }
                        EPSetup epsetup = PXSelect <EPSetup> .Select(registerEntry);

                        if (epsetup.PostingOption != EPPostOptions.DoNotPost)
                        {
                            registerEntry.CreateTransaction(activity, employee.BAccountID, activity.Date.Value, activity.TimeSpent, activity.TimeBillable, cost.Rate, cost.OvertimeMultiplier);
                        }
                        activity.EmployeeRate = cost.Rate;
                        activityAdded         = true;
                    }

                    activity.Released       = true;
                    activity.ApprovalStatus = ActivityStatusAttribute.Released;
                    if (activity.RefNoteID != null)
                    {
                        PXUpdate <
                            Set <CRActivity.isLocked, True>,
                            CRActivity,
                            Where <CRActivity.noteID, Equal <Required <CRActivity.noteID> > > >
                        .Update(registerEntry, activity.RefNoteID);
                    }
                    activityCache.Update(activity);
                }
                catch (Exception e)
                {
                    PXProcessing <EPActivityApprove> .SetError(i, e is PXOuterException?e.Message + "\r\n" + String.Join("\r\n", ((PXOuterException)e).InnerMessages) : e.Message);

                    success = false;
                }
            }

            if (activityAdded)
            {
                registerEntry.Save.Press();
            }
            else
            {
                activityCache.Persist(PXDBOperation.Update);
                registerEntry.SelectTimeStamp();
            }

            return(success);
        }