Esempio n. 1
0
        protected virtual IEnumerable customers()
        {
            BillingFilter filter = Filter.Current;

            if (filter == null)
            {
                yield break;
            }
            bool found = false;

            foreach (CustomersList item in Customers.Cache.Inserted)
            {
                found = true;
                yield return(item);
            }
            if (found)
            {
                yield break;
            }

            PXSelectBase <EPExpenseClaimDetails> sel = new PXSelectJoinGroupBy <EPExpenseClaimDetails, InnerJoin <Customer, On <EPExpenseClaimDetails.customerID, Equal <Customer.bAccountID> >,
                                                                                                                  LeftJoin <Contract, On <EPExpenseClaimDetails.contractID, Equal <Contract.contractID> > > >,
                                                                                Where <EPExpenseClaimDetails.released, Equal <boolTrue>,
                                                                                       And <EPExpenseClaimDetails.billable, Equal <boolTrue>,
                                                                                            And <EPExpenseClaimDetails.billed, Equal <boolFalse>,
                                                                                                 And <EPExpenseClaimDetails.expenseDate, LessEqual <Current <BillingFilter.endDate> > > > > >,
                                                                                Aggregate <GroupBy <EPExpenseClaimDetails.customerID,
                                                                                                    GroupBy <EPExpenseClaimDetails.customerLocationID> > > >(this);

            if (filter.CustomerClassID != null)
            {
                sel.WhereAnd <Where <Customer.customerClassID, Equal <Current <BillingFilter.customerClassID> > > >();
            }
            if (filter.CustomerID != null)
            {
                sel.WhereAnd <Where <Customer.bAccountID, Equal <Current <BillingFilter.customerID> > > >();
            }

            foreach (PXResult <EPExpenseClaimDetails, Customer, Contract> res in sel.Select())
            {
                CustomersList         retitem      = new CustomersList();
                Customer              customer     = res;
                EPExpenseClaimDetails claimdetaisl = res;
                Contract              contract     = (Contract)res;

                if (contract.BaseType == PMProject.ProjectBaseType.Project)
                {
                    continue;
                }


                retitem.CustomerID      = customer.BAccountID;
                retitem.LocationID      = claimdetaisl.CustomerLocationID;
                retitem.CustomerClassID = customer.CustomerClassID;

                retitem.Selected = false;

                yield return(Customers.Insert(retitem));
            }
        }
 public static void Bill(EPCustomerBillingProcess docgraph, CustomersList customer, BillingFilter filter)
 {
     docgraph.Bill(customer, filter);
 }
        public virtual void Bill(CustomersList customer, EPCustomerBilling.BillingFilter filter)
        {
            ARInvoiceEntry arGraph = PXGraph.CreateInstance <ARInvoiceEntry>();
            RegisterEntry  pmGraph = PXGraph.CreateInstance <RegisterEntry>();

            arGraph.Clear();
            pmGraph.Clear();

            PMRegister pmDoc = null;
            ARInvoice  arDoc = null;

            List <ARRegister>            doclist = new List <ARRegister>();
            List <EPExpenseClaimDetails> listOfDirectBilledClaims = new List <EPExpenseClaimDetails>();

            PXSelectBase <EPExpenseClaimDetails> select = new PXSelectJoin <EPExpenseClaimDetails,
                                                                            LeftJoin <Contract, On <EPExpenseClaimDetails.contractID, Equal <Contract.contractID>, And <Where <Contract.baseType, Equal <Contract.ContractBaseType>, Or <Contract.nonProject, Equal <True> > > > >
                                                                                      , LeftJoin <Account, On <EPExpenseClaimDetails.expenseAccountID, Equal <Account.accountID> > >
                                                                                      >,
                                                                            Where <EPExpenseClaimDetails.released, Equal <boolTrue>,
                                                                                   And <EPExpenseClaimDetails.billable, Equal <boolTrue>,
                                                                                        And <EPExpenseClaimDetails.billed, Equal <boolFalse>,
                                                                                             And <EPExpenseClaimDetails.customerID, Equal <Required <EPExpenseClaimDetails.customerID> >,
                                                                                                  And <EPExpenseClaimDetails.customerLocationID, Equal <Required <EPExpenseClaimDetails.customerLocationID> >,
                                                                                                       And <EPExpenseClaimDetails.expenseDate, LessEqual <Required <EPExpenseClaimDetails.expenseDate> >,
                                                                                                            And <Where <EPExpenseClaimDetails.contractID, Equal <Contract.contractID>, Or <EPExpenseClaimDetails.contractID, IsNull> > > > > > > > >,
                                                                            OrderBy <Asc <EPExpenseClaimDetails.branchID> > >(this);

            arGraph.RowPersisted.AddHandler <ARInvoice>(
                delegate(PXCache sender, PXRowPersistedEventArgs e)
            {
                if (e.TranStatus == PXTranStatus.Open)
                {
                    foreach (EPExpenseClaimDetails row in listOfDirectBilledClaims.Select(claimdetail => Transactions.Locate(claimdetail)))
                    {
                        row.ARDocType = ((ARInvoice)e.Row).DocType;
                        row.ARRefNbr  = ((ARInvoice)e.Row).RefNbr;
                    }
                }
            });


            decimal signOperation = 1m;

            decimal tipQty = 1m;

            foreach (PXResult <EPExpenseClaimDetails, Contract, Account> res in select.Select(customer.CustomerID, customer.LocationID, filter.EndDate))
            {
                EPExpenseClaimDetails row = (EPExpenseClaimDetails)res;

                if (row.ContractID != null && !ProjectDefaultAttribute.IsNonProject(row.ContractID))
                {
                    if (pmDoc == null)
                    {
                        pmDoc             = (PMRegister)pmGraph.Document.Cache.Insert();
                        pmDoc.OrigDocType = PMOrigDocType.ExpenseClaim;
                        pmDoc.OrigDocNbr  = row.RefNbr;
                    }

                    PMTran usage = InsertPMTran(pmGraph, res);
                    if (usage.Released == true)                     //contract trans are created as released
                    {
                        UsageMaint.AddUsage(pmGraph.Transactions.Cache, usage.ProjectID, usage.InventoryID, usage.BillableQty ?? 0m, usage.UOM);
                    }
                }
                else
                {
                    if (arDoc == null || arDoc.BranchID != row.BranchID)
                    {
                        if (arDoc != null)
                        {
                            arDoc.CuryOrigDocAmt = arDoc.CuryDocBal;
                            arGraph.Document.Update(arDoc);
                            arGraph.Save.Press();
                            listOfDirectBilledClaims.Clear();
                        }
                        EPExpenseClaimDetails summDetail = PXSelectJoinGroupBy <EPExpenseClaimDetails,
                                                                                LeftJoin <Contract, On <EPExpenseClaimDetails.contractID, Equal <Contract.contractID>, And <Where <Contract.baseType, Equal <Contract.ContractBaseType>, Or <Contract.nonProject, Equal <True> > > > >
                                                                                          >,
                                                                                Where <EPExpenseClaimDetails.released, Equal <boolTrue>,
                                                                                       And <EPExpenseClaimDetails.billable, Equal <boolTrue>,
                                                                                            And <EPExpenseClaimDetails.billed, Equal <boolFalse>,
                                                                                                 And <EPExpenseClaimDetails.customerID, Equal <Required <EPExpenseClaimDetails.customerID> >,
                                                                                                      And <EPExpenseClaimDetails.customerLocationID, Equal <Required <EPExpenseClaimDetails.customerLocationID> >,
                                                                                                           And <EPExpenseClaimDetails.expenseDate, LessEqual <Required <EPExpenseClaimDetails.expenseDate> >,
                                                                                                                And <EPExpenseClaimDetails.branchID, Equal <Required <EPExpenseClaimDetails.branchID> >,
                                                                                                                     And <Where <Contract.nonProject, Equal <True>, Or <EPExpenseClaimDetails.contractID, IsNull> > > > > > > > > >
                                                                                , Aggregate <Sum <EPExpenseClaimDetails.curyTranAmt> >
                                                                                > .Select(this, customer.CustomerID, customer.LocationID, filter.EndDate, row.BranchID);

                        if (summDetail.CuryTranAmt < 0)
                        {
                            signOperation = -1;
                        }
                        else
                        {
                            signOperation = 1;
                        }

                        arDoc = (ARInvoice)arGraph.Document.Cache.Insert();
                        //arDocList.Add(arDoc);
                        if (signOperation < 0)
                        {
                            arGraph.Document.Cache.SetValueExt <ARInvoice.docType>(arDoc, AR.ARDocType.CreditMemo);
                        }
                        else
                        {
                            arGraph.Document.Cache.SetValueExt <ARInvoice.docType>(arDoc, AR.ARDocType.Invoice);
                        }
                        arGraph.Document.Cache.SetValueExt <ARInvoice.customerID>(arDoc, row.CustomerID);
                        arGraph.Document.Cache.SetValueExt <ARInvoice.customerLocationID>(arDoc, row.CustomerLocationID);
                        arGraph.Document.Cache.SetValueExt <ARInvoice.docDate>(arDoc, filter.InvoiceDate);
                        arGraph.Document.Cache.SetValueExt <ARInvoice.branchID>(arDoc, row.BranchID);
                        arDoc.OrigModule  = BatchModule.EP;
                        arDoc.OrigRefNbr  = row.RefNbr;
                        arDoc             = arGraph.Document.Update(arDoc);
                        arDoc.FinPeriodID = filter.InvFinPeriodID;
                        if (Setup.Current.AutomaticReleaseAR == true)
                        {
                            arDoc.Hold = false;
                        }
                        doclist.Add(arDoc);
                    }

                    //insert ARTran
                    InsertARTran(arGraph, row, signOperation);
                    if ((row.CuryTipAmt ?? 0) != 0)
                    {
                        if (signOperation < 0 == row.ClaimCuryTranAmtWithTaxes < 0)
                        {
                            tipQty = 1;
                        }
                        else
                        {
                            tipQty = -1;
                        }
                        InsertARTran(arGraph, row, signOperation, tipQty, true);
                    }
                    listOfDirectBilledClaims.Add(row);
                }

                row.Billed = true;
                Transactions.Update(row);
            }

            if (arDoc != null)
            {
                arDoc.CuryOrigDocAmt = arDoc.CuryDocBal;
                arGraph.Document.Update(arDoc);
                arGraph.Save.Press();
            }

            if (pmDoc != null)
            {
                pmGraph.Save.Press();
            }

            this.Persist(typeof(EPExpenseClaimDetails), PXDBOperation.Update);

            if (Setup.Current.AutomaticReleaseAR == true)
            {
                ARDocumentRelease.ReleaseDoc(doclist, false);
            }
        }
Esempio n. 4
0
        public virtual void Bill(CustomersList customer, PX.Objects.EP.EPCustomerBilling.BillingFilter filter)
        {
            ARInvoiceEntry arGraph = PXGraph.CreateInstance <ARInvoiceEntry>();
            RegisterEntry  pmGraph = PXGraph.CreateInstance <RegisterEntry>();

            arGraph.Clear();
            pmGraph.Clear();

            PMRegister pmDoc = null;
            ARInvoice  arDoc = null;

            List <ARRegister>            doclist = new List <ARRegister>();
            List <EPExpenseClaimDetails> listOfDirectBilledClaims = new List <EPExpenseClaimDetails>();

            PXSelectBase <EPExpenseClaimDetails> select = new PXSelectJoin <EPExpenseClaimDetails,
                                                                            InnerJoin <EPExpenseClaim, On <EPExpenseClaimDetails.refNbr, Equal <EPExpenseClaim.refNbr> > >,
                                                                            Where <EPExpenseClaimDetails.released, Equal <boolTrue>,
                                                                                   And <EPExpenseClaimDetails.billable, Equal <boolTrue>,
                                                                                        And <EPExpenseClaimDetails.billed, Equal <boolFalse>,
                                                                                             And <EPExpenseClaimDetails.customerID, Equal <Required <EPExpenseClaimDetails.customerID> >,
                                                                                                  And <EPExpenseClaimDetails.customerLocationID, Equal <Required <EPExpenseClaimDetails.customerLocationID> >,
                                                                                                       And <EPExpenseClaimDetails.expenseDate, LessEqual <Required <EPExpenseClaimDetails.expenseDate> > > > > > > >,
                                                                            OrderBy <Asc <EPExpenseClaimDetails.customerID, Asc <EPExpenseClaimDetails.customerLocationID> > > >(this);

            foreach (PXResult <EPExpenseClaimDetails, EPExpenseClaim> res in select.Select(customer.CustomerID, customer.LocationID, filter.EndDate))
            {
                EPExpenseClaimDetails row = (EPExpenseClaimDetails)res;
                EPExpenseClaim        doc = (EPExpenseClaim)res;

                if (row.ContractID != null && !ProjectDefaultAttribute.IsNonProject(this, row.ContractID))
                {
                    if (pmDoc == null)
                    {
                        pmDoc             = (PMRegister)pmGraph.Document.Cache.Insert();
                        pmDoc.OrigDocType = PMOrigDocType.ExpenseClaim;
                        pmDoc.OrigDocNbr  = doc.RefNbr;
                    }

                    InsertPMTran(pmGraph, row, doc, Setup.Current.CopyNotesPM == true, Setup.Current.CopyFilesPM == true);
                }
                else
                {
                    if (arDoc == null ||
                        arDoc.CustomerID != row.CustomerID ||
                        arDoc.CustomerLocationID != row.CustomerLocationID)
                    {
                        if (arDoc != null)
                        {
                            arDoc.CuryOrigDocAmt = arDoc.CuryDocBal;
                            arGraph.Document.Update(arDoc);
                            arGraph.Save.Press();
                        }
                        arDoc                    = (ARInvoice)arGraph.Document.Cache.Insert();
                        arDoc.DocType            = AR.ARDocType.Invoice;
                        arDoc.CustomerID         = row.CustomerID;
                        arDoc.CustomerLocationID = row.CustomerLocationID;
                        arDoc                    = arGraph.Document.Update(arDoc);
                        arGraph.Document.Cache.RaiseFieldUpdated <AR.ARInvoice.customerID>(arDoc, null);
                        if (Setup.Current.AutomaticReleaseAR == true)
                        {
                            arDoc.Hold = false;
                        }
                        doclist.Add(arDoc);
                    }

                    //insert ARTran
                    InsertARTran(arGraph, row, Setup.Current.CopyNotesAR == true, Setup.Current.CopyFilesAR == true);
                    listOfDirectBilledClaims.Add(row);
                }

                row.Billed = true;
                Transactions.Update(row);
            }

            if (arDoc != null)
            {
                arDoc.CuryOrigDocAmt = arDoc.CuryDocBal;
                arGraph.Document.Update(arDoc);

                arGraph.RowPersisted.AddHandler <ARInvoice>(
                    delegate(PXCache sender, PXRowPersistedEventArgs e)
                {
                    if (e.TranStatus == PXTranStatus.Open)
                    {
                        foreach (EPExpenseClaimDetails claimdetail in listOfDirectBilledClaims)
                        {
                            EPExpenseClaimDetails row = Transactions.Locate(claimdetail);

                            row.ARDocType = ((ARInvoice)e.Row).DocType;
                            row.ARRefNbr  = ((ARInvoice)e.Row).RefNbr;
                        }
                    }
                });

                arGraph.Save.Press();
            }
            if (pmDoc != null)
            {
                pmGraph.Save.Press();
            }

            this.Persist(typeof(EPExpenseClaimDetails), PXDBOperation.Update);

            if (Setup.Current.AutomaticReleaseAR == true)
            {
                ARDocumentRelease.ReleaseDoc(doclist, false);
            }
        }
        public virtual void Bill(CustomersList customer, EPCustomerBilling.BillingFilter filter)
        {
            ARInvoiceEntry arGraph = PXGraph.CreateInstance <ARInvoiceEntry>();
            RegisterEntry  pmGraph = PXGraph.CreateInstance <RegisterEntry>();

            arGraph.Clear();
            pmGraph.Clear();

            PMRegister pmDoc = null;
            ARInvoice  arDoc = null;

            List <ARRegister>            doclist = new List <ARRegister>();
            List <EPExpenseClaimDetails> listOfDirectBilledClaims = new List <EPExpenseClaimDetails>();

            PXSelectBase <EPExpenseClaimDetails> select = new PXSelectJoin <EPExpenseClaimDetails,
                                                                            LeftJoin <Contract, On <EPExpenseClaimDetails.contractID, Equal <Contract.contractID>,
                                                                                                    And <Where <Contract.baseType, Equal <CTPRType.contract>,
                                                                                                                Or <Contract.nonProject, Equal <True> > > > >,
                                                                                      LeftJoin <Account, On <EPExpenseClaimDetails.expenseAccountID, Equal <Account.accountID> > > >,
                                                                            Where <EPExpenseClaimDetails.released, Equal <boolTrue>,
                                                                                   And <EPExpenseClaimDetails.billable, Equal <boolTrue>,
                                                                                        And <EPExpenseClaimDetails.billed, Equal <boolFalse>,
                                                                                             And <EPExpenseClaimDetails.customerID, Equal <Required <EPExpenseClaimDetails.customerID> >,
                                                                                                  And <EPExpenseClaimDetails.customerLocationID, Equal <Required <EPExpenseClaimDetails.customerLocationID> >,
                                                                                                       And <EPExpenseClaimDetails.expenseDate, LessEqual <Required <EPExpenseClaimDetails.expenseDate> >,
                                                                                                            And <Where <EPExpenseClaimDetails.contractID, Equal <Contract.contractID>,
                                                                                                                        Or <EPExpenseClaimDetails.contractID, IsNull> > > > > > > > >,
                                                                            OrderBy <Asc <EPExpenseClaimDetails.branchID> > >(this);

            arGraph.RowPersisted.AddHandler <ARInvoice>(
                delegate(PXCache sender, PXRowPersistedEventArgs e)
            {
                if (e.TranStatus == PXTranStatus.Open)
                {
                    foreach (EPExpenseClaimDetails row in listOfDirectBilledClaims.Select(claimdetail => Transactions.Locate(claimdetail)))
                    {
                        row.ARDocType = ((ARInvoice)e.Row).DocType;
                        row.ARRefNbr  = ((ARInvoice)e.Row).RefNbr;
                    }
                }
            });

            decimal signOperation = 1m;
            decimal tipQty        = 1m;

            var resultset = select.Select(customer.CustomerID, customer.LocationID, filter.EndDate).AsEnumerable();

            FinPeriodUtils.ValidateFinPeriod <EPExpenseClaimDetails>(
                resultset.RowCast <EPExpenseClaimDetails>(),
                m => filter.InvFinPeriodID,
                m => m.BranchID.SingleToArray());

            foreach (PXResult <EPExpenseClaimDetails, Contract, Account> res in resultset)
            {
                EPExpenseClaimDetails row = (EPExpenseClaimDetails)res;

                if (row.ContractID != null && !ProjectDefaultAttribute.IsNonProject(row.ContractID))
                {
                    if (pmDoc == null)
                    {
                        EPExpenseClaim claim = PXSelect <EPExpenseClaim, Where <EPExpenseClaim.refNbr, Equal <Required <EPExpenseClaim.refNbr> > > > .Select(this, row.RefNbr);

                        pmDoc             = (PMRegister)pmGraph.Document.Cache.Insert();
                        pmDoc.OrigDocType = PMOrigDocType.ExpenseClaim;
                        pmDoc.OrigNoteID  = claim.NoteID;
                    }

                    PMTran usage = InsertPMTran(pmGraph, res);
                    if (usage.Released == true)                     //contract trans are created as released
                    {
                        UsageMaint.AddUsage(pmGraph.Transactions.Cache, usage.ProjectID, usage.InventoryID, usage.BillableQty ?? 0m, usage.UOM);
                    }
                }
                else
                {
                    if (arDoc == null || arDoc.BranchID != row.BranchID)
                    {
                        if (arDoc != null)
                        {
                            arDoc.CuryOrigDocAmt = arDoc.CuryDocBal;
                            arGraph.Document.Update(arDoc);
                            arGraph.Save.Press();
                            listOfDirectBilledClaims.Clear();
                        }

                        EPExpenseClaimDetails summDetail = PXSelectJoinGroupBy <EPExpenseClaimDetails,
                                                                                LeftJoin <Contract, On <EPExpenseClaimDetails.contractID, Equal <Contract.contractID>,
                                                                                                        And <Where <Contract.baseType, Equal <CTPRType.contract>,
                                                                                                                    Or <Contract.nonProject, Equal <True> > > > > >,
                                                                                Where <EPExpenseClaimDetails.released, Equal <boolTrue>,
                                                                                       And <EPExpenseClaimDetails.billable, Equal <boolTrue>,
                                                                                            And <EPExpenseClaimDetails.billed, Equal <boolFalse>,
                                                                                                 And <EPExpenseClaimDetails.customerID, Equal <Required <EPExpenseClaimDetails.customerID> >,
                                                                                                      And <EPExpenseClaimDetails.customerLocationID, Equal <Required <EPExpenseClaimDetails.customerLocationID> >,
                                                                                                           And <EPExpenseClaimDetails.expenseDate, LessEqual <Required <EPExpenseClaimDetails.expenseDate> >,
                                                                                                                And <EPExpenseClaimDetails.branchID, Equal <Required <EPExpenseClaimDetails.branchID> >,
                                                                                                                     And <Where <Contract.nonProject, Equal <True>,
                                                                                                                                 Or <EPExpenseClaimDetails.contractID, IsNull> > > > > > > > > >,
                                                                                Aggregate <Sum <EPExpenseClaimDetails.curyTranAmt> > >
                                                           .Select(this, customer.CustomerID, customer.LocationID, filter.EndDate, row.BranchID);

                        signOperation = summDetail.CuryTranAmt < 0 ? -1 : 1;

                        // OrigModule should be set before Insert() method
                        // to organize proper defaulting for any other fields
                        // which depend on OrigModule value.
                        //
                        arDoc            = new ARInvoice();
                        arDoc.OrigModule = BatchModule.EP;
                        arGraph.Document.Cache.SetValueExt <ARInvoice.docType>(arDoc,
                                                                               signOperation < 0 ? ARDocType.CreditMemo : ARDocType.Invoice);

                        arDoc = (ARInvoice)arGraph.Document.Cache.Insert(arDoc);

                        arGraph.Document.Cache.SetValueExt <ARInvoice.customerID>(arDoc, row.CustomerID);
                        arGraph.Document.Cache.SetValueExt <ARInvoice.customerLocationID>(arDoc, row.CustomerLocationID);
                        arGraph.Document.Cache.SetValueExt <ARInvoice.docDate>(arDoc, filter.InvoiceDate);
                        arGraph.Document.Cache.SetValueExt <ARInvoice.branchID>(arDoc, row.BranchID);
                        arDoc.OrigRefNbr  = row.RefNbr;
                        arDoc             = arGraph.Document.Update(arDoc);
                        arDoc.FinPeriodID = filter.InvFinPeriodID;
                        doclist.Add(arDoc);
                    }

                    // Insert ARTran.
                    //
                    InsertARTran(arGraph, row, signOperation);
                    if ((row.CuryTipAmt ?? 0) != 0)
                    {
                        tipQty = signOperation < 0 == row.ClaimCuryTranAmtWithTaxes < 0 ? 1 : -1;
                        InsertARTran(arGraph, row, signOperation, tipQty, true);
                    }

                    listOfDirectBilledClaims.Add(row);
                }

                row.Billed = true;
                Transactions.Update(row);
            }

            if (arDoc != null)
            {
                arDoc.CuryOrigDocAmt = arDoc.CuryDocBal;
                if (arGraph.ARSetup.Current.HoldEntry == false || Setup.Current.AutomaticReleaseAR == true)
                {
                    arDoc = PXCache <ARInvoice> .CreateCopy(arDoc);

                    arDoc.Hold = false;
                    arDoc      = arGraph.Document.Update(arDoc);
                }
                arGraph.Document.Update(arDoc);
                arGraph.Save.Press();
            }

            if (pmDoc != null)
            {
                pmGraph.Save.Press();
            }

            Persist(typeof(EPExpenseClaimDetails), PXDBOperation.Update);

            if (Setup.Current.AutomaticReleaseAR == true)
            {
                ARDocumentRelease.ReleaseDoc(doclist, false);
            }
        }