public static bool RecordContractUsage(List <EPActivityApprove> activities, string description)
        {
            RegisterEntry registerEntry = PXGraph.CreateInstance <RegisterEntry>();

            registerEntry.FieldVerifying.AddHandler <PMTran.projectID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });
            registerEntry.FieldVerifying.AddHandler <PMTran.inventoryID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });//restriction should be applicable only for budgeting.
            registerEntry.Document.Cache.Insert();
            registerEntry.Document.Current.Description = description;
            registerEntry.Document.Current.Released    = true;
            registerEntry.Views.Caches.Add(typeof(EPActivity));
            PXCache activityCache = registerEntry.Caches <EPActivity>();

            bool success       = true;
            bool activityAdded = false;

            for (int i = 0; i < activities.Count; i++)
            {
                EPActivity activity = activities[i];
                try
                {
                    if (activity.RefNoteID != null && PXSelect <CRCase, Where <CRCase.noteID, Equal <Required <CRCase.noteID> > > > .Select(registerEntry, activity.RefNoteID).Count == 1)
                    {
                        //Add Contract-Usage
                        activityAdded   = registerEntry.CreateContractUsage(activity) != null || activityAdded;
                        activity.Billed = 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 List <EPActivityApprove> RecordContractUsage(List <EPActivityApprove> activities, string description)
        {
            RegisterEntry registerEntry = CreateInstance <RegisterEntry>();

            registerEntry.FieldVerifying.AddHandler <PMTran.projectID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });
            registerEntry.FieldVerifying.AddHandler <PMTran.inventoryID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });//restriction should be applicable only for budgeting.
            registerEntry.Document.Cache.Insert();
            registerEntry.Document.Current.Description = description;
            registerEntry.Document.Current.Released    = true;
            registerEntry.Document.Current.Status      = PMRegister.status.Released;
            registerEntry.Views.Caches.Add(typeof(EPActivityApprove));
            PXCache activityCache = registerEntry.Caches <EPActivityApprove>();
            List <EPActivityApprove> useActivities = new List <EPActivityApprove>();

            for (int i = 0; i < activities.Count; i++)
            {
                EPActivityApprove activity = activities[i];
                try
                {
                    PMTran usage = null;
                    if (activity.ContractID != null)
                    {
                        //Contract without CRM mode
                        usage           = CreateContractUsage(registerEntry, activity.ContractID, activity, activity.TimeBillable.GetValueOrDefault());
                        activity.Billed = true;
                        activityCache.Update(activity);
                    }
                    else if (activity.RefNoteID != null &&
                             PXSelectJoin <CRCase,
                                           InnerJoin <CRActivityLink,
                                                      On <CRActivityLink.refNoteID, Equal <CRCase.noteID> > >,
                                           Where <CRActivityLink.noteID, Equal <Required <PMTimeActivity.refNoteID> > > > .Select(registerEntry, activity.RefNoteID).Count == 1)
                    {
                        //Add Contract-Usage
                        usage           = registerEntry.CreateContractUsage(activity, activity.TimeBillable.GetValueOrDefault());
                        activity.Billed = true;
                        activityCache.Update(activity);
                    }
                    if (usage != null)
                    {
                        UsageMaint.AddUsage(activityCache, usage.ProjectID, usage.InventoryID, usage.BillableQty ?? 0m, usage.UOM);
                    }

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

            if (registerEntry.Transactions.Cache.IsInsertedUpdatedDeleted)
            {
                registerEntry.Save.Press();
            }
            else
            {
                activityCache.Persist(PXDBOperation.Update);
            }

            return(useActivities);
        }