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); } }
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); } }