public CreateInvoiceByContractPost() { CreateInvoiceByContractPost graphCreateInvoiceByServiceOrderPost = null; Contracts.SetProcessDelegate( delegate(List <ContractPeriodToPost> contractPeriodToPostRows) { graphCreateInvoiceByServiceOrderPost = PXGraph.CreateInstance <CreateInvoiceByContractPost>(); int rowIndex = 0; InvoiceContractPeriodFilter filter = Filter.Current; FSSetup fsSetupRow = ServiceManagementSetup.GetServiceManagementSetup(graphCreateInvoiceByServiceOrderPost); ContractPostBatchEntry contractPostBatchGraph = PXGraph.CreateInstance <ContractPostBatchEntry>(); ContractPostPeriodEntry contractPostPeriodGraph = PXGraph.CreateInstance <ContractPostPeriodEntry>(); FSContractPostDoc fsContractPostDocRow = null; FSContractPostBatch fsContractPostBatchRow = null; List <ContractInvoiceLine> invoiceDet = new List <ContractInvoiceLine>(); List <ContractInvoiceLine> contractPeriodLines = null; List <ContractInvoiceLine> contractInvoiceDetList = null; List <ContractInvoiceLine> equipmentInvoiceDetList = null; FSServiceContract fsServiceContractRow = null; FSContractPeriod fsContractPeriodRow = null; IInvoiceContractGraph invoiceGraph = GetInvoiceGraph(fsSetupRow); foreach (ContractPeriodToPost contractPeriodToPostRow in contractPeriodToPostRows) { fsContractPostDocRow = null; GetContractAndPeriod(contractPeriodToPostRow.ServiceContractID, contractPeriodToPostRow.ContractPeriodID, out fsServiceContractRow, out fsContractPeriodRow); contractPeriodLines = GetContractPeriodLines(contractPeriodToPostRow); if (contractPeriodLines.Count > 0 && fsServiceContractRow != null && fsContractPeriodRow != null) { contractInvoiceDetList = GetContractInvoiceLines(contractPeriodToPostRow); invoiceDet.AddRange(contractInvoiceDetList); if (fsSetupRow.ContractPostTo == ID.Contract_PostTo.SALES_ORDER_MODULE) { equipmentInvoiceDetList = GetEquipmentInvoiceLines(contractPeriodToPostRow); } List <ContractInvoiceLine> contractInvoiceLines = GetInvoiceLines(invoiceDet, contractPeriodLines, contractInvoiceDetList, fsSetupRow.ContractPostTo); if (equipmentInvoiceDetList != null && equipmentInvoiceDetList.Count > 0) { contractInvoiceLines.AddRange(equipmentInvoiceDetList); invoiceDet.AddRange(equipmentInvoiceDetList); } try { using (var ts = new PXTransactionScope()) { if (fsContractPostBatchRow == null) { fsContractPostBatchRow = contractPostBatchGraph.CreatePostingBatch(filter.UpToDate, filter.InvoiceDate, filter.InvoiceFinPeriodID, SetupRecord.Current.ContractPostTo); } fsContractPostDocRow = invoiceGraph.CreateInvoiceByContract(graphCreateInvoiceByServiceOrderPost, filter.InvoiceDate, filter.InvoiceFinPeriodID, fsContractPostBatchRow, fsServiceContractRow, fsContractPeriodRow, contractInvoiceLines); fsContractPostDocRow = CreateContractPostDoc(contractPostPeriodGraph, fsContractPostDocRow); CreateContractPostDet(contractPostPeriodGraph, fsContractPostDocRow, invoiceDet); UpdateSourcePostDoc(fsSetupRow, fsContractPostDocRow, fsServiceContractRow); CreateContractPostRegister(this, fsContractPostDocRow); contractPeriodToPostRow.ContractPostBatchID = fsContractPostBatchRow.ContractPostBatchID; ts.Complete(); } PXProcessing <ContractPeriodToPost> .SetInfo(rowIndex, TX.Messages.RECORD_PROCESSED_SUCCESSFULLY); } catch (Exception e) { PXProcessing <ContractPeriodToPost> .SetError(rowIndex, e.Message); } } else { PXProcessing <ContractPeriodToPost> .SetError(rowIndex, TX.Error.PERIOD_WITHOUT_DETAILS); } rowIndex++; } }); }
public virtual FSContractPostDoc CreateInvoiceByContract(PXGraph graphProcess, DateTime?invoiceDate, string invoiceFinPeriodID, FSContractPostBatch fsContractPostBatchRow, FSServiceContract fsServiceContractRow, FSContractPeriod fsContractPeriodRow, List <ContractInvoiceLine> docLines) { if (docLines.Count == 0) { return(null); } FSSetup fsSetupRow = ServiceManagementSetup.GetServiceManagementSetup(graphProcess); ARInvoice arInvoiceRow = new ARInvoice(); arInvoiceRow.DocType = ARInvoiceType.Invoice; arInvoiceRow.DocDate = invoiceDate; arInvoiceRow.FinPeriodID = invoiceFinPeriodID; arInvoiceRow.Hold = true; arInvoiceRow = Base.Document.Insert(arInvoiceRow); Base.Document.Cache.SetValueExt <ARInvoice.customerID>(arInvoiceRow, fsServiceContractRow.BillCustomerID); Base.Document.Cache.SetValueExt <ARInvoice.customerLocationID>(arInvoiceRow, fsServiceContractRow.BillLocationID); Base.Document.Cache.SetValueExt <ARInvoice.branchID>(arInvoiceRow, fsServiceContractRow.BranchID); Base.Document.Cache.SetValueExt <ARInvoice.docDesc>(arInvoiceRow, "Contract Standardized Billing: " + fsServiceContractRow.RefNbr + " " + fsServiceContractRow.DocDesc); string termsID = InvoicingFunctions.GetTermsIDFromCustomerOrVendor(graphProcess, fsServiceContractRow.BillCustomerID, null); if (termsID != null) { Base.Document.Cache.SetValueExt <ARInvoice.termsID>(arInvoiceRow, termsID); } else { Base.Document.Cache.SetValueExt <ARInvoice.termsID>(arInvoiceRow, fsSetupRow.DfltContractTermIDARSO); } ARTran arTranRow = null; FSxARTran fsxARTranRow = null; int? acctID; foreach (ContractInvoiceLine docLine in docLines) { arTranRow = new ARTran(); arTranRow = Base.Transactions.Insert(arTranRow); Base.Transactions.Cache.SetValueExt <ARTran.inventoryID>(arTranRow, docLine.InventoryID); Base.Transactions.Cache.SetValueExt <ARTran.uOM>(arTranRow, docLine.UOM); Base.Transactions.Cache.SetValueExt <ARTran.salesPersonID>(arTranRow, docLine.SalesPersonID); arTranRow = Base.Transactions.Update(arTranRow); if (docLine.AcctID != null) { acctID = docLine.AcctID; } else { acctID = (int?)ServiceOrderCore.Get_INItemAcctID_DefaultValue( graphProcess, fsSetupRow.ContractSalesAcctSource, docLine.InventoryID, fsServiceContractRow); } Base.Transactions.Cache.SetValueExt <ARTran.accountID>(arTranRow, acctID); if (docLine.SubID != null) { try { Base.Transactions.Cache.SetValueExt <ARTran.subID>(arTranRow, docLine.SubID); } catch (PXException) { arTranRow.SubID = null; } } else { InvoicingFunctions.SetCombinedSubID( graphProcess, Base.Transactions.Cache, arTranRow, null, null, fsSetupRow, arTranRow.BranchID, arTranRow.InventoryID, arInvoiceRow.CustomerLocationID, fsServiceContractRow.BranchLocationID); } Base.Transactions.Cache.SetValueExt <ARTran.qty>(arTranRow, docLine.Qty); Base.Transactions.Cache.SetValueExt <ARTran.curyUnitPrice>(arTranRow, docLine.CuryUnitPrice); Base.Transactions.Cache.SetValueExt <ARTran.tranDesc>(arTranRow, docLine.TranDescPrefix + arTranRow.TranDesc); arTranRow = Base.Transactions.Update(arTranRow); Base.Transactions.Cache.SetValueExt <ARTran.commissionable>(arTranRow, docLine.Commissionable ?? false); fsxARTranRow = Base.Transactions.Cache.GetExtension <FSxARTran>(arTranRow); fsxARTranRow.Source = ID.Billing_By.CONTRACT; fsxARTranRow.ServiceContractID = fsServiceContractRow.ServiceContractID; fsxARTranRow.ContractPeriodID = fsContractPeriodRow.ContractPeriodID; fsxARTranRow.BillCustomerID = fsServiceContractRow.BillCustomerID; fsxARTranRow.CustomerLocationID = fsServiceContractRow.BillLocationID; arTranRow = Base.Transactions.Update(arTranRow); } if (Base.ARSetup.Current.RequireControlTotal == true) { Base.Document.Cache.SetValueExt <ARInvoice.curyOrigDocAmt>(arInvoiceRow, arInvoiceRow.CuryDocBal); } Base.Document.Cache.SetValueExt <ARInvoice.hold>(arInvoiceRow, false); Exception newException = null; try { Base.Save.Press(); } catch (Exception e) { List <ErrorInfo> errorList = this.GetErrorInfo(); var exceptionWithContextMessage = ExceptionHelper.GetExceptionWithContextMessage( PXMessages.Localize(TX.Messages.ERROR_CREATING_INVOICE_IN_POSTING_BATCH), e); newException = InvoicingFunctions.GetErrorInfoInLines(errorList, exceptionWithContextMessage); } if (newException != null) { throw newException; } arInvoiceRow = Base.Document.Current; FSContractPostDoc fsContractCreatedDocRow = new FSContractPostDoc() { ContractPeriodID = fsContractPeriodRow.ContractPeriodID, ContractPostBatchID = fsContractPostBatchRow.ContractPostBatchID, PostDocType = arInvoiceRow.DocType, PostedTO = ID.Batch_PostTo.AR, PostRefNbr = arInvoiceRow.RefNbr, ServiceContractID = fsServiceContractRow.ServiceContractID }; return(fsContractCreatedDocRow); }
public virtual FSContractPostDoc CreateInvoiceByContract(PXGraph graphProcess, DateTime?invoiceDate, string invoiceFinPeriodID, FSContractPostBatch fsContractPostBatchRow, FSServiceContract fsServiceContractRow, FSContractPeriod fsContractPeriodRow, List <ContractInvoiceLine> docLines) { if (docLines.Count == 0) { return(null); } FSSetup fsSetupRow = ServiceManagementSetup.GetServiceManagementSetup(graphProcess); ARInvoice arInvoiceRow = new ARInvoice(); arInvoiceRow.DocType = ARInvoiceType.Invoice; AutoNumberHelper.CheckAutoNumbering(Base, Base.ARSetup.SelectSingle().InvoiceNumberingID); arInvoiceRow.DocDate = invoiceDate; arInvoiceRow.FinPeriodID = invoiceFinPeriodID; arInvoiceRow.Hold = true; arInvoiceRow = Base.Document.Insert(arInvoiceRow); Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.customerID>(arInvoiceRow, fsServiceContractRow.BillCustomerID); Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.customerLocationID>(arInvoiceRow, fsServiceContractRow.BillLocationID); Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.branchID>(arInvoiceRow, fsServiceContractRow.BranchID); Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.docDesc>(arInvoiceRow, (PXMessages.LocalizeFormatNoPrefix(TX.Messages.CONTRACT_WITH_STANDARDIZED_BILLING, fsServiceContractRow.RefNbr, (string.IsNullOrEmpty(fsServiceContractRow.DocDesc) ? string.Empty : fsServiceContractRow.DocDesc)))); string termsID = InvoicingFunctions.GetTermsIDFromCustomerOrVendor(graphProcess, fsServiceContractRow.BillCustomerID, null); if (termsID != null) { Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.termsID>(arInvoiceRow, termsID); } else { Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.termsID>(arInvoiceRow, fsSetupRow.DfltContractTermIDARSO); } Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.projectID>(arInvoiceRow, fsServiceContractRow.ProjectID); ARTran arTranRow = null; FSxARTran fsxARTranRow = null; int? acctID; foreach (ContractInvoiceLine docLine in docLines) { arTranRow = new ARTran(); arTranRow = Base.Transactions.Insert(arTranRow); Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.inventoryID>(arTranRow, docLine.InventoryID); Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.uOM>(arTranRow, docLine.UOM); Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.salesPersonID>(arTranRow, docLine.SalesPersonID); arTranRow = Base.Transactions.Update(arTranRow); if (docLine.AcctID != null) { acctID = docLine.AcctID; } else { acctID = (int?)ServiceOrderCore.Get_INItemAcctID_DefaultValue(graphProcess, fsSetupRow.ContractSalesAcctSource, docLine.InventoryID, fsServiceContractRow); } Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.accountID>(arTranRow, acctID); if (docLine.SubID != null) { try { Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.subID>(arTranRow, docLine.SubID); } catch (PXException) { arTranRow.SubID = null; } } else { InvoicingFunctions.SetCombinedSubID(graphProcess, Base.Transactions.Cache, arTranRow, null, null, fsSetupRow, arTranRow.BranchID, arTranRow.InventoryID, arInvoiceRow.CustomerLocationID, fsServiceContractRow.BranchLocationID); } Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.qty>(arTranRow, docLine.Qty); Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.curyUnitPrice>(arTranRow, docLine.CuryUnitPrice); if (docLine.ServiceContractID != null && docLine.ContractRelated == false && (docLine.SODetID != null || docLine.AppDetID != null)) { Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.curyExtPrice>(arTranRow, docLine.CuryBillableExtPrice); Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.discPct>(arTranRow, docLine.DiscPct); } Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.tranDesc>(arTranRow, docLine.TranDescPrefix + arTranRow.TranDesc); Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.taskID>(arTranRow, docLine.ProjectTaskID); Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.costCodeID>(arTranRow, docLine.CostCodeID); arTranRow = Base.Transactions.Update(arTranRow); Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.commissionable>(arTranRow, docLine.Commissionable ?? false); fsxARTranRow = Base.Transactions.Cache.GetExtension <FSxARTran>(arTranRow); fsxARTranRow.Source = ID.DocumentSource.INVOICE_FROM_SERVICECONTRACT; fsxARTranRow.ServiceContractID = fsServiceContractRow.ServiceContractID; fsxARTranRow.ContractPeriodID = fsContractPeriodRow.ContractPeriodID; fsxARTranRow.BillCustomerID = fsServiceContractRow.CustomerID; fsxARTranRow.CustomerLocationID = fsServiceContractRow.CustomerLocationID; arTranRow = Base.Transactions.Update(arTranRow); } if (Base.ARSetup.Current.RequireControlTotal == true) { Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.curyOrigDocAmt>(arInvoiceRow, arInvoiceRow.CuryDocBal); } Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.hold>(arInvoiceRow, false); Exception newException = null; try { Base.Save.Press(); } catch (Exception e) { List <ErrorInfo> errorList = this.GetErrorInfo(); newException = InvoicingFunctions.GetErrorInfoInLines(errorList, e); } if (newException != null) { throw newException; } arInvoiceRow = Base.Document.Current; FSContractPostDoc fsContractCreatedDocRow = new FSContractPostDoc() { ContractPeriodID = fsContractPeriodRow.ContractPeriodID, ContractPostBatchID = fsContractPostBatchRow.ContractPostBatchID, PostDocType = arInvoiceRow.DocType, PostedTO = ID.Batch_PostTo.AR, PostRefNbr = arInvoiceRow.RefNbr, ServiceContractID = fsServiceContractRow.ServiceContractID }; return(fsContractCreatedDocRow); }