public static void Run(PMAllocator graph, PMProject item, DateTime?date, DateTime?fromDate, DateTime?toDate)
        {
            PXSelectBase <PMTask> select = new PXSelect <PMTask, Where <PMTask.projectID, Equal <Required <PMTask.projectID> >,
                                                                        And <PMTask.allocationID, IsNotNull,
                                                                             And <PMTask.isActive, Equal <True> > > > >(graph);

            List <PMTask> tasks = new List <PMTask>();

            foreach (PMTask pmTask in select.Select(item.ContractID))
            {
                tasks.Add(pmTask);
            }

            graph.Clear();
            graph.FilterStartDate = fromDate;
            graph.FilterEndDate   = toDate;
            graph.PostingDate     = date;
            graph.Execute(tasks);
            if (graph.Document.Current != null)
            {
                graph.Actions.PressSave();
                PMSetup setup = PXSelect <PMSetup> .Select(graph);

                PMRegister doc = graph.Caches[typeof(PMRegister)].Current as PMRegister;
                if (doc != null && setup.AutoReleaseAllocation == true)
                {
                    RegisterRelease.Release(doc);
                }
            }
            else
            {
                throw new PXSetPropertyException(Warnings.NothingToAllocate, PXErrorLevel.RowWarning);
            }
        }
        public static void ReverseAllocatedTran(PMTran tran)
        {
            RegisterEntry pmEntry = PXGraph.CreateInstance <RegisterEntry>();

            pmEntry.FieldVerifying.AddHandler <PMTran.projectID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });
            pmEntry.FieldVerifying.AddHandler <PMTran.taskID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });
            pmEntry.FieldVerifying.AddHandler <PMTran.inventoryID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });

            PMRegister reversalDoc = (PMRegister)pmEntry.Document.Cache.Insert();

            reversalDoc.OrigDocType  = PMOrigDocType.AllocationReversal;
            reversalDoc.OrigDocNbr   = tran.RefNbr;
            reversalDoc.Description  = "Allocation Reversal to Non-Billable";
            pmEntry.Document.Current = reversalDoc;

            PMBillEngine engine = PXGraph.CreateInstance <PMBillEngine>();

            foreach (PMTran reverse in engine.ReverseTran(tran))
            {
                reverse.Reversed = true;
                pmEntry.Transactions.Insert(reverse);
            }

            tran.Reversed = true;
            pmEntry.Transactions.Update(tran);

            pmEntry.Save.Press();

            PMSetup setup = PXSelect <PMSetup> .Select(pmEntry);

            if (setup.AutoReleaseAllocation == true)
            {
                RegisterRelease.Release(reversalDoc);
            }
        }
        public static void ReverseAllocatedTran(PMTran tran)
        {
            RegisterEntry pmEntry = PXGraph.CreateInstance <RegisterEntry>();

            pmEntry.FieldVerifying.AddHandler <PMTran.projectID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });
            pmEntry.FieldVerifying.AddHandler <PMTran.taskID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });
            pmEntry.FieldVerifying.AddHandler <PMTran.inventoryID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });

            PMRegister reversalDoc = (PMRegister)pmEntry.Document.Cache.Insert();

            reversalDoc.OrigDocType  = PMOrigDocType.AllocationReversal;
            reversalDoc.OrigDocNbr   = tran.RefNbr;
            reversalDoc.Description  = PXMessages.LocalizeNoPrefix(Messages.AllocationReversalNonBilling);
            pmEntry.Document.Current = reversalDoc;

            PMBillEngine engine = PXGraph.CreateInstance <PMBillEngine>();

            foreach (PMTran reverse in engine.ReverseTran(tran))
            {
                reverse.Reversed     = true;
                reverse.Date         = null;
                reverse.FinPeriodID  = null;
                reverse.TranDate     = null;
                reverse.TranPeriodID = null;

                pmEntry.Transactions.Insert(reverse);
            }

            tran.Reversed = true;
            PM.RegisterReleaseProcess.SubtractFromUnbilledSummary(pmEntry, tran);
            pmEntry.Transactions.Update(tran);

            pmEntry.Save.Press();

            PMSetup setup = PXSelect <PMSetup> .Select(pmEntry);

            if (setup.AutoReleaseAllocation == true)
            {
                RegisterRelease.Release(reversalDoc);
            }
        }
Example #4
0
        public static void Run(PMAllocator graph, PMTask item, DateTime?date, DateTime?fromDate, DateTime?toDate)
        {
            graph.Clear();
            graph.FilterStartDate = fromDate;
            graph.FilterEndDate   = toDate;
            graph.Execute(item);
            if (graph.Document.Current != null)
            {
                graph.Actions.PressSave();
                PMSetup setup = PXSelect <PMSetup> .Select(graph);

                PMRegister doc = graph.Caches[typeof(PMRegister)].Current as PMRegister;
                if (doc != null && setup.AutoReleaseAllocation == true)
                {
                    RegisterRelease.Release(doc);
                }
            }
            else
            {
                throw new PXSetPropertyException(Warnings.NothingToAllocate, PXErrorLevel.RowWarning);
            }
        }
Example #5
0
        public static void ReverseAllocatedTran(PMTran tran)
        {
            RegisterEntry pmEntry = PXGraph.CreateInstance <RegisterEntry>();

            pmEntry.FieldVerifying.AddHandler <PMTran.projectID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });
            pmEntry.FieldVerifying.AddHandler <PMTran.taskID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });
            pmEntry.FieldVerifying.AddHandler <PMTran.inventoryID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });

            PMRegister origDoc = PXSelectReadonly <PMRegister, Where <PMRegister.refNbr, Equal <Required <PMRegister.refNbr> > > > .Select(pmEntry, tran.RefNbr);

            PMRegister reversalDoc = (PMRegister)pmEntry.Document.Cache.Insert();

            reversalDoc.OrigDocType  = PMOrigDocType.Reversal;
            reversalDoc.OrigNoteID   = origDoc.NoteID;
            reversalDoc.Description  = PXMessages.LocalizeNoPrefix(Messages.AllocationReversal);
            pmEntry.Document.Current = reversalDoc;

            PMBillEngine engine = PXGraph.CreateInstance <PMBillEngine>();

            foreach (PMTran reverse in engine.ReverseTran(tran))
            {
                pmEntry.Transactions.Insert(reverse);
            }
            tran.ExcludedFromBilling       = true;
            tran.ExcludedFromBillingReason = PXMessages.LocalizeNoPrefix(Messages.ExcludedFromBillingAsReversed);
            PM.RegisterReleaseProcess.SubtractFromUnbilledSummary(pmEntry, tran);
            pmEntry.Transactions.Update(tran);

            pmEntry.Save.Press();

            PMSetup setup = PXSelect <PMSetup> .Select(pmEntry);

            if (setup.AutoReleaseAllocation == true)
            {
                RegisterRelease.Release(reversalDoc);
            }
        }
Example #6
0
        public virtual bool Bill(int?projectID, DateTime?invoiceDate)
        {
            PMProject project = PXSelect <PMProject, Where <PMProject.contractID, Equal <Required <PMProject.contractID> > > > .Select(this, projectID);

            ContractBillingSchedule schedule = PXSelect <ContractBillingSchedule> .Search <ContractBillingSchedule.contractID>(this, project.ContractID);

            DateTime billingDate;

            if (invoiceDate == null)
            {
                if (schedule.Type == BillingType.OnDemand)
                {
                    billingDate = Accessinfo.BusinessDate ?? DateTime.Now;
                }
                else
                {
                    billingDate = schedule.NextDate.Value;
                }
            }
            else
            {
                billingDate = invoiceDate.Value;
            }

            Customer customer = null;

            if (project.CustomerID != null)
            {
                customer = PXSelect <Customer, Where <Customer.bAccountID, Equal <Required <Customer.bAccountID> > > > .Select(this, project.CustomerID);
            }

            if (customer == null)
            {
                throw new PXException(Messages.NoCustomer);
            }

            List <BillingData> list          = new List <BillingData>();
            List <PMTran>      reversalTrans = new List <PMTran>();

            PXSelectBase <PMTask> selectTasks = new PXSelect <PMTask,
                                                              Where <PMTask.projectID, Equal <Required <PMTask.projectID> >,
                                                                     And <PMTask.billingID, IsNotNull,
                                                                          And <PMTask.isActive, Equal <True> > > > >(this);

            foreach (PMTask task in selectTasks.Select(project.ContractID))
            {
                if ((task.BillingOption == PMBillingOption.OnTaskCompletion && task.IsCompleted == true) ||
                    (task.BillingOption == PMBillingOption.OnProjectCompetion && project.IsCompleted == true) ||
                    task.BillingOption == PMBillingOption.OnBilling)
                {
                    list.AddRange(BillTask(task, billingDate));
                    reversalTrans.AddRange(ReverseWipTask(task, billingDate));
                }
            }

            //Regroup by Invoices:
            Dictionary <string, List <BillingData> > invoices = new Dictionary <string, List <BillingData> >();
            string emptyInvoiceDescriptionKey = "!@#$%^&";

            foreach (BillingData data in list)
            {
                if (string.IsNullOrEmpty(data.Rule.InvoiceDescription))
                {
                    if (invoices.ContainsKey(emptyInvoiceDescriptionKey))
                    {
                        invoices[emptyInvoiceDescriptionKey].Add(data);
                    }
                    else
                    {
                        invoices.Add(emptyInvoiceDescriptionKey, new List <BillingData>(new BillingData[] { data }));
                    }
                }
                else
                {
                    if (invoices.ContainsKey(data.Rule.InvoiceDescription))
                    {
                        invoices[data.Rule.InvoiceDescription].Add(data);
                    }
                    else
                    {
                        invoices.Add(data.Rule.InvoiceDescription, new List <BillingData>(new BillingData[] { data }));
                    }
                }

                //Reverse On Billing:

                if (data.PMTran != null)
                {
                    bool reverseOnBilling = false;

                    if (data.PMTran.Reverse == PMReverse.OnBilling)
                    {
                        reverseOnBilling = true;
                    }
                    else if (data.PMTran.Reverse != PMReverse.Never && IsNonGL(data.PMTran))
                    {
                        reverseOnBilling = true;
                    }

                    if (reverseOnBilling)
                    {
                        reversalTrans.AddRange(ReverseTran(data.PMTran));
                    }
                }
            }


            //Schedule update:
            schedule.NextDate = GetNextBillingDate(this, schedule, schedule.NextDate);
            schedule.LastDate = this.Accessinfo.BusinessDate;
            BillingSchedule.Update(schedule);


            //ContractDetail update:
            PXSelectBase <ContractDetailExt> cis = new PXSelect <ContractDetailExt, Where <ContractDetailExt.contractID, Equal <Required <ContractDetailExt.contractID> >,
                                                                                           And <ContractDetailExt.resetUsage, Equal <ResetUsageOption.onBilling> > > >(this);

            foreach (ContractDetailExt ci in cis.Select(project.ContractID))
            {
                ci.Used = 0;
                ContractDetail.Update(ci);
            }

            List <ARRegister> doclist     = new List <ARRegister>();
            PMRegister        reversalDoc = null;

            if (invoices.Count > 0)
            {
                using (PXTransactionScope ts = new PXTransactionScope())
                {
                    ARInvoiceEntry invoiceEntry = PXGraph.CreateInstance <ARInvoiceEntry>();
                    invoiceEntry.FieldVerifying.AddHandler <ARInvoice.projectID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });
                    invoiceEntry.FieldVerifying.AddHandler <ARTran.projectID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });
                    invoiceEntry.FieldVerifying.AddHandler <ARTran.taskID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });

                    foreach (KeyValuePair <string, List <BillingData> > kv in invoices)
                    {
                        invoiceEntry.Clear(PXClearOption.ClearAll);

                        string    description = kv.Key == emptyInvoiceDescriptionKey ? null : kv.Key;
                        string    docDesc     = description ?? string.Format(PXMessages.LocalizeNoPrefix(CT.Messages.BillingFor), project.ContractCD, project.Description);
                        ARInvoice invoice     = (ARInvoice)invoiceEntry.Caches[typeof(ARInvoice)].CreateInstance();
                        invoice.DocType = GetDocType(kv.Value);
                        int mult = 1;
                        if (invoice.DocType == ARDocType.CreditMemo)
                        {
                            mult = -1;
                        }

                        invoice = (ARInvoice)invoiceEntry.Caches[typeof(ARInvoice)].Insert(invoice);
                        invoiceEntry.Caches[typeof(ARInvoice)].SetValueExt <ARInvoice.customerID>(invoice, customer.AcctCD);
                        invoiceEntry.Caches[typeof(ARInvoice)].SetValue <ARInvoice.customerLocationID>(invoice, project.LocationID);
                        invoiceEntry.Caches[typeof(ARInvoice)].SetValueExt <ARInvoice.docDate>(invoice, billingDate);
                        invoiceEntry.Caches[typeof(ARInvoice)].SetValue <ARInvoice.docDesc>(invoice, docDesc);
                        invoiceEntry.Caches[typeof(ARInvoice)].SetValue <ARInvoice.projectID>(invoice, project.ContractID);

                        CurrencyInfo curyinfo = (CurrencyInfo)invoiceEntry.Caches[typeof(CurrencyInfo)].Current;
                        foreach (BillingData data in kv.Value)
                        {
                            data.Tran.ExtPrice = data.Tran.ExtPrice.GetValueOrDefault() * mult;
                            data.Tran.Qty      = data.Tran.Qty.GetValueOrDefault() * mult;
                            data.Tran.TranAmt  = data.Tran.TranAmt.GetValueOrDefault() * mult;

                            decimal curyamount;
                            PXDBCurrencyAttribute.CuryConvCury(invoiceEntry.Caches[typeof(CurrencyInfo)], curyinfo, data.Tran.UnitPrice.GetValueOrDefault(), out curyamount);
                            data.Tran.CuryUnitPrice = curyamount;
                            PXDBCurrencyAttribute.CuryConvCury(invoiceEntry.Caches[typeof(CurrencyInfo)], curyinfo, data.Tran.ExtPrice.GetValueOrDefault(), out curyamount);
                            data.Tran.CuryExtPrice     = curyamount;
                            data.Tran.CuryTranAmt      = data.Tran.CuryExtPrice;
                            data.Tran.FreezeManualDisc = true;
                            data.Tran.CuryInfoID       = curyinfo.CuryInfoID;
                            ARTran newTran = (ARTran)invoiceEntry.Caches[typeof(ARTran)].Insert(data.Tran);

                            if (data.Tran.TranAmt > newTran.TranAmt)
                            {
                                newTran.PMDeltaOption = ARTran.pMDeltaOption.CompleteLine;                                //autocomplete when currency descrepency exists.
                            }

                            if (data.Tran.AccountID != null)
                            {
                                ARTran copy = (ARTran)invoiceEntry.Caches[typeof(ARTran)].CreateCopy(newTran);
                                copy.AccountID = data.Tran.AccountID;

                                copy = (ARTran)invoiceEntry.Caches[typeof(ARTran)].Update(copy);

                                if (data.SubCD != null)
                                {
                                    invoiceEntry.Caches[typeof(ARTran)].SetValueExt <ARTran.subID>(copy, data.SubCD);
                                }
                            }

                            if (data.Note != null)
                            {
                                PXNoteAttribute.SetNote(invoiceEntry.Caches[typeof(ARTran)], newTran, data.Note);
                            }
                            if (data.Files != null && data.Files.Length > 0)
                            {
                                PXNoteAttribute.SetFileNotes(invoiceEntry.Caches[typeof(ARTran)], newTran, data.Files);
                            }


                            //item.RefLineNbr = newTran.LineNbr;
                        }

                        ARInvoice oldInvoice = (ARInvoice)invoiceEntry.Caches[typeof(ARInvoice)].CreateCopy(invoice);

                        invoice.CuryOrigDocAmt = invoice.CuryDocBal;
                        invoiceEntry.Caches[typeof(ARInvoice)].RaiseRowUpdated(invoice, oldInvoice);
                        invoiceEntry.Caches[typeof(ARInvoice)].SetValue <ARInvoice.curyOrigDocAmt>(invoice, invoice.CuryDocBal);

                        if (project.AutomaticReleaseAR == true)
                        {
                            invoiceEntry.Caches[typeof(ARInvoice)].SetValueExt <ARInvoice.hold>(invoice, false);
                        }

                        doclist.Add((ARInvoice)invoiceEntry.Caches[typeof(ARInvoice)].Current);
                        invoiceEntry.Save.Press();
                    }

                    Actions.PressSave();

                    if (reversalTrans.Count > 0)
                    {
                        RegisterEntry pmEntry = PXGraph.CreateInstance <RegisterEntry>();
                        pmEntry.FieldVerifying.AddHandler <PMTran.projectID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });                    //Project can be completed.
                        pmEntry.FieldVerifying.AddHandler <PMTran.taskID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });                       //Task can be completed.
                        pmEntry.FieldVerifying.AddHandler <PMTran.inventoryID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; });
                        reversalDoc              = (PMRegister)pmEntry.Document.Cache.Insert();
                        reversalDoc.OrigDocType  = PMOrigDocType.AllocationReversal;
                        reversalDoc.Description  = "Allocation Reversal on Billing";
                        pmEntry.Document.Current = reversalDoc;

                        foreach (PMTran tran in reversalTrans)
                        {
                            pmEntry.Transactions.Insert(tran);
                        }
                        pmEntry.Save.Press();
                    }

                    ts.Complete();
                }
            }
            else
            {
                this.Persist(typeof(ContractBillingSchedule), PXDBOperation.Update);
                this.Persist(typeof(Contract), PXDBOperation.Update);
            }

            if (project.AutomaticReleaseAR == true)
            {
                try
                {
                    ARDocumentRelease.ReleaseDoc(doclist, false);
                }
                catch (Exception ex)
                {
                    throw new PXException(Messages.AutoReleaseARFailed, ex);
                }

                if (reversalDoc != null)
                {
                    try
                    {
                        RegisterRelease.Release(reversalDoc);
                    }
                    catch (Exception ex)
                    {
                        throw new PXException(Messages.AutoReleaseOfReversalFailed, ex);
                    }
                }
            }

            return(doclist.Count > 0);
        }