/// <summary>
        /// Generates Appointments (Routes Contract) or Service Orders (Service Contract) for each TimeSlot in the [scheduleRules] List.
        /// </summary>
        public void GenerateAPPSOUpdateContracts(List <Schedule> scheduleRules, string recordType, DateTime?fromDate, DateTime?toDate, FSSchedule fsScheduleRow)
        {
            var      generator      = new TimeSlotGenerator();
            DateTime processEndDate = (DateTime)GetProcessEndDate(scheduleRules.ElementAt(0), toDate);

            var period = new Period((DateTime)fromDate, processEndDate);

            // Determines the next generationID number
            if (nextGenerationID == null)
            {
                FSProcessIdentity fsProcessIdentityRow = new FSProcessIdentity();
                fsProcessIdentityRow.ProcessType  = recordType;
                fsProcessIdentityRow.FilterFromTo = fromDate;
                fsProcessIdentityRow.FilterUpTo   = toDate;

                ProcessIdentityMaint graphProcessIdentityMaint = PXGraph.CreateInstance <ProcessIdentityMaint>();
                graphProcessIdentityMaint.processIdentityRecords.Insert(fsProcessIdentityRow);
                graphProcessIdentityMaint.Save.Press();

                nextGenerationID = graphProcessIdentityMaint.processIdentityRecords.Current.ProcessID;
            }

            List <TimeSlot> timeSlots   = generator.GenerateCalendar(period, scheduleRules, nextGenerationID);
            DateTime?       failsOnDate = null;

            // Transaction to create the appointments (Routes Contract) or Service Orders (Service Contract)
            using (PXTransactionScope ts = new PXTransactionScope())
            {
                Customer customerRow = PXSelect <Customer,
                                                 Where <
                                                     Customer.bAccountID, Equal <Required <Customer.bAccountID> > > >
                                       .Select(this, fsScheduleRow.CustomerID)
                                       .FirstOrDefault();

                if (customerRow.Status != BAccount.status.Active && customerRow.Status != BAccount.status.OneTime)
                {
                    throw new PXException(PXMessages.LocalizeFormat(AR.Messages.CustomerIsInStatus, new Customer.status.ListAttribute().ValueLabelDic[customerRow.Status]));
                }

                try
                {
                    foreach (var timeSlot in timeSlots)
                    {
                        failsOnDate = timeSlot.DateTimeBegin;

                        if (recordType == ID.RecordType_ServiceContract.SERVICE_CONTRACT)
                        {
                            bool createAppointmnet = fsScheduleRow.ScheduleGenType == ID.ScheduleGenType_ServiceContract.APPOINTMENT;

                            CreateServiceOrder(timeSlot, createAppointmnet);
                        }
                        else if (recordType == ID.RecordType_ServiceContract.ROUTE_SERVICE_CONTRACT)
                        {
                            CreateServiceOrder(timeSlot, true, true);
                        }
                    }

                    DateTime?lastGenerationDate = null;

                    if (timeSlots.Count > 0)
                    {
                        lastGenerationDate = timeSlots.Max(a => a.DateTimeBegin);
                    }

                    CreateContractGenerationHistory((int)nextGenerationID,
                                                    scheduleRules.ElementAt(0).ScheduleID,
                                                    processEndDate,
                                                    lastGenerationDate,
                                                    recordType);

                    UpdateGeneratedSchedule(scheduleRules.ElementAt(0).ScheduleID, processEndDate, lastGenerationDate, fsScheduleRow);
                }
                catch (Exception e)
                {
                    var exceptionWithContextMessage = ExceptionHelper.GetExceptionWithContextMessage(PXMessages.Localize(TX.Messages.COULD_NOT_PROCESS_RECORD), e);

                    FSGenerationLogError fsGenerationLogErrorRow = new FSGenerationLogError();
                    fsGenerationLogErrorRow.ProcessType  = recordType;
                    fsGenerationLogErrorRow.ErrorMessage = exceptionWithContextMessage.Message;
                    fsGenerationLogErrorRow.ScheduleID   = scheduleRules.ElementAt(0).ScheduleID;
                    fsGenerationLogErrorRow.GenerationID = nextGenerationID;
                    fsGenerationLogErrorRow.ErrorDate    = failsOnDate;

                    ts.Dispose();

                    var grapGenerationLogErrorMaint = PXGraph.CreateInstance <GenerationLogErrorMaint>();

                    grapGenerationLogErrorMaint.LogErrorMessageRecords.Insert(fsGenerationLogErrorRow);
                    grapGenerationLogErrorMaint.Save.Press();
                    throw exceptionWithContextMessage;
                }

                ts.Complete(this);
            }
        }
        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);
        }
        /// <summary>
        /// Creates one or more documents in Inventory depending of the number of FSAppointmentInventoryItem in the list [fsAppointmentInventoryItemRows].
        /// </summary>
        public virtual int CreateDocumentsInIN(FSPostBatch fsPostBatchRow, List <FSAppointmentDet> fsAppointmentInventoryItemRows, List <SharedClasses.AppointmentInventoryItemGroup> listGroupToUpdateInInventory, UpdateInventoryFilter filter)
        {
            INReceiptEntry graphINReceiptEntry = PXGraph.CreateInstance <INReceiptEntry>();
            INIssueEntry   graphINIssueEntry   = PXGraph.CreateInstance <INIssueEntry>();
            int            linesPosted         = 0;

            foreach (SharedClasses.AppointmentInventoryItemGroup groupToUpdateInInventory in listGroupToUpdateInInventory)
            {
                string iNRefNbr  = null;
                string iNDocType = null;

                foreach (SharedClasses.AppointmentInventoryItemInfo appointmentInventoryItemInfoRow in groupToUpdateInInventory.AppointmentInventoryItems)
                {
                    using (var ts_GroupToUpdateInInventory = new PXTransactionScope())
                    {
                        try
                        {
                            if (groupToUpdateInInventory.ServiceType == ID.Service_Action_Type.PICKED_UP_ITEMS)
                            {
                                // create receipt
                                CreateDocumentReceipt(graphINReceiptEntry,
                                                      appointmentInventoryItemInfoRow,
                                                      fsAppointmentInventoryItemRows[appointmentInventoryItemInfoRow.Index],
                                                      filter.DocumentDate,
                                                      filter.FinPeriodID,
                                                      fsPostBatchRow,
                                                      ref iNRefNbr,
                                                      ref iNDocType);
                            }
                            else if (groupToUpdateInInventory.ServiceType == ID.Service_Action_Type.DELIVERED_ITEMS)
                            {
                                // create issue
                                CreateDocumentIssue(graphINIssueEntry,
                                                    appointmentInventoryItemInfoRow,
                                                    fsAppointmentInventoryItemRows[appointmentInventoryItemInfoRow.Index],
                                                    filter.DocumentDate,
                                                    filter.FinPeriodID,
                                                    fsPostBatchRow,
                                                    ref iNRefNbr,
                                                    ref iNDocType);
                            }
                            else if (groupToUpdateInInventory.ServiceType == ID.Service_Action_Type.NO_ITEMS_RELATED)
                            {
                                PXProcessing <FSAppointmentDet> .SetError(appointmentInventoryItemInfoRow.Index, TX.Error.APPOINTMENT_ITEM_CANNOT_BE_POSTED_TO_IN_NO_ITEMS_RELATED);
                            }

                            PXProcessing <FSAppointmentDet> .SetInfo(appointmentInventoryItemInfoRow.Index, TX.Messages.RECORD_PROCESSED_SUCCESSFULLY);

                            linesPosted++;
                            ts_GroupToUpdateInInventory.Complete();
                        }
                        catch (Exception e)
                        {
                            Exception latestException = ExceptionHelper.GetExceptionWithContextMessage(PXMessages.Localize(TX.Messages.COULD_NOT_PROCESS_RECORD), e);
                            PXProcessing <FSAppointmentDet> .SetError(appointmentInventoryItemInfoRow.Index, latestException);

                            ts_GroupToUpdateInInventory.Dispose();
                            graphINReceiptEntry.Actions.PressCancel();
                        }
                    }
                }
            }

            return(linesPosted);
        }