/// <summary>
        /// Gets the information of the Appointment and AppointmentInventoryItem using as reference the [appointmentID] and [appointmentInventoryItemID].
        /// </summary>
        private SharedClasses.AppointmentInventoryItemInfo GetAppointmentInventoryItemInfo(int?appointmentID, int?appDetID, int index)
        {
            PXResult <FSAppointment, FSServiceOrder, FSSrvOrdType, FSAppointmentInventoryItem, FSAppointmentDet> bqlResult =
                (PXResult <FSAppointment, FSServiceOrder, FSSrvOrdType, FSAppointmentInventoryItem, FSAppointmentDet>)
                PXSelectReadonly2 <FSAppointment,
                                   InnerJoin <FSServiceOrder,
                                              On <FSServiceOrder.sOID, Equal <FSAppointment.sOID>,
                                                  And <FSAppointment.appointmentID, Equal <Required <FSAppointment.appointmentID> > > >,
                                              InnerJoin <FSSrvOrdType,
                                                         On <FSSrvOrdType.srvOrdType, Equal <FSAppointment.srvOrdType> >,
                                                         InnerJoin <FSAppointmentInventoryItem,
                                                                    On <FSAppointmentInventoryItem.appointmentID, Equal <FSAppointment.appointmentID>,
                                                                        And <FSAppointmentInventoryItem.appDetID, Equal <Required <FSAppointmentInventoryItem.appDetID> > > >,
                                                                    InnerJoin <FSAppointmentDet,
                                                                               On <FSAppointmentDet.sODetID, Equal <FSAppointmentInventoryItem.sODetID> > > > > > >
                .Select(this, appointmentID, appDetID);

            SharedClasses.AppointmentInventoryItemInfo appointmentInfoToReturn = new SharedClasses.AppointmentInventoryItemInfo();

            if (bqlResult != null)
            {
                FSAppointment              fsAppointmentRow              = (FSAppointment)bqlResult;
                FSServiceOrder             fsServiceOrderRow             = (FSServiceOrder)bqlResult;
                FSSrvOrdType               fsSrvOrdTypeRow               = (FSSrvOrdType)bqlResult;
                FSAppointmentInventoryItem fsAppointmentInventoryItemRow = (FSAppointmentInventoryItem)bqlResult;
                FSAppointmentDet           fsAppointmentDetRow           = (FSAppointmentDet)bqlResult;

                appointmentInfoToReturn = new SharedClasses.AppointmentInventoryItemInfo(fsAppointmentRow, fsServiceOrderRow, fsSrvOrdTypeRow, fsAppointmentDetRow, fsAppointmentInventoryItemRow, index);
            }

            return(appointmentInfoToReturn);
        }
            private int?GetAcct(
                PXGraph graph,
                IFSSODetBase fsSODetBase,
                FSAppointmentInventoryItem fsAppointmentInventoryItem,
                FSServiceOrder fsServiceOrderRow,
                FSSrvOrdType fsSrvOrdTypeRow)
            {
                int?acctID = null;

                if (fsSODetBase != null)
                {
                    if (fsSODetBase.AcctID != null)
                    {
                        acctID = fsSODetBase.AcctID;
                    }
                    else
                    {
                        acctID = ServiceOrderCore.Get_TranAcctID_DefaultValue(
                            graph,
                            fsSrvOrdTypeRow.SalesAcctSource,
                            fsSODetBase.InventoryID,
                            fsServiceOrderRow);
                    }
                }
                else if (fsAppointmentInventoryItem != null)
                {
                    acctID = ServiceOrderCore.Get_TranAcctID_DefaultValue(
                        graph,
                        fsSrvOrdTypeRow.SalesAcctSource,
                        fsAppointmentInventoryItem.InventoryID,
                        fsServiceOrderRow);
                }

                return(acctID);
            }
        public ContractInvoiceLine(PXResult <FSAppointmentInventoryItem, FSSODet, FSAppointment> row) : this((IDocLine)(FSAppointmentInventoryItem)row)
        {
            FSAppointmentInventoryItem fsAppointmentInventoryItemRow = (FSAppointmentInventoryItem)row;
            FSAppointment fsAppointmentRow = (FSAppointment)row;
            FSSODet       fsSODetRow       = (FSSODet)row;

            AppointmentID = fsAppointmentInventoryItemRow.AppointmentID;
            AppDetID      = fsAppointmentInventoryItemRow.AppDetID;

            BillingRule      = fsSODetRow.BillingRule;
            ContractRelated  = fsAppointmentInventoryItemRow.ContractRelated;
            Qty              = fsAppointmentInventoryItemRow.ContractRelated == true ? fsAppointmentInventoryItemRow.Qty : fsAppointmentInventoryItemRow.BillableQty;
            OverageItemPrice = fsAppointmentInventoryItemRow.OverageItemPrice;
            SalesPersonID    = fsAppointmentRow.SalesPersonID;
            Commissionable   = fsAppointmentRow.Commissionable;
        }
 public AppointmentInventoryItemInfo(
     FSAppointment fsAppointmentRow,
     FSServiceOrder fsServiceOrderRow,
     FSSrvOrdType fsSrvOrdTypeRow,
     FSAppointmentDet fsAppointmentDet,
     FSAppointmentInventoryItem fsAppointmentInventoryItemRow,
     int index)
 {
     this.FSAppointmentRow           = fsAppointmentRow;
     this.FSServiceOrderRow          = fsServiceOrderRow;
     this.FSSrvOrdTypeRow            = fsSrvOrdTypeRow;
     this.FSAppointmentDet           = fsAppointmentDet;
     this.FSAppointmentInventoryItem = fsAppointmentInventoryItemRow;
     this.AppointmentID = fsAppointmentRow.AppointmentID;
     this.ServiceType   = fsAppointmentDet.ServiceType;
     this.Index         = index;
 }
        public virtual IEnumerable ViewPostBatch(PXAdapter adapter)
        {
            if (Appointments.Current != null)
            {
                FSAppointmentInventoryItem fsAppointmentInventoryItemRow = Appointments.Current;

                if (!string.IsNullOrEmpty(fsAppointmentInventoryItemRow.Mem_BatchNbr))
                {
                    graphUpdatePostBatchMaint.BatchRecords.Current = graphUpdatePostBatchMaint.BatchRecords.Search <FSPostBatch.batchNbr>(fsAppointmentInventoryItemRow.Mem_BatchNbr);
                    throw new PXRedirectRequiredException(graphUpdatePostBatchMaint, null)
                          {
                              Mode = PXBaseRedirectException.WindowMode.NewWindow
                          };
                }
            }

            return(adapter.Get());
        }
        protected virtual void VerifyIsAlreadyPosted <Field>(PXCache cache, FSAppointmentDet fsAppointmentDetRow, FSAppointmentInventoryItem fsAppointmentInventoryItemRow, FSBillingCycle billingCycleRow)
            where Field : class, IBqlField
        {
            if ((fsAppointmentDetRow == null && fsAppointmentInventoryItemRow == null) || ServiceOrderRelated.Current == null || billingCycleRow == null)
            {
                return;
            }

            IFSSODetBase row   = null;
            int?         pivot = -1;

            if (fsAppointmentDetRow != null)
            {
                row   = fsAppointmentDetRow;
                pivot = fsAppointmentDetRow.SODetID;
            }
            else if (fsAppointmentInventoryItemRow != null)
            {
                row   = fsAppointmentInventoryItemRow;
                pivot = fsAppointmentInventoryItemRow.AppDetID > 0 ? fsAppointmentInventoryItemRow.AppDetID : null;
            }

            PXEntryStatus status            = ServiceOrderRelated.Cache.GetStatus(ServiceOrderRelated.Current);
            bool          needsVerify       = status == PXEntryStatus.Updated || status == PXEntryStatus.Notchanged;
            bool          isSOAlreadyPosted = ServiceOrderPostedIn.Select().Count > 0;

            if (needsVerify == true &&
                pivot == null &&
                IsInstructionOrComment(row) == false &&
                billingCycleRow.BillingBy == ID.Billing_By.SERVICE_ORDER &&
                isSOAlreadyPosted == true)
            {
                cache.RaiseExceptionHandling <Field>(row,
                                                     row.InventoryID,
                                                     new PXSetPropertyException(
                                                         PXMessages.LocalizeFormat(TX.Error.CANNOT_ADD_INVENTORY_TYPE_LINES_BECAUSE_SO_POSTED, GetLineType(row.LineType)),
                                                         PXErrorLevel.RowError));
            }
        }
        /// <summary>
        /// Update the references in <c>FSPostInfo</c> and <c>FSPostDet</c> when the posting process of every AppointmentInventoryItem is complete in IN.
        /// </summary>
        private void UpdateIssuePostInfo(INIssueEntry graphINIssueEntry, InventoryPostBatchMaint graphInventoryPostBatchMaint, PostInfoEntry graphPostInfoEntry, FSAppointmentInventoryItem fsAppointmentInventoryItemRow, SharedClasses.AppointmentInventoryItemInfo appointmentInventoryItemInfoRow, FSPostBatch fsPostBatchRow)
        {
            //Create | Update Post info
            FSPostInfo fsPostInfoRow;
            FSPostDet  fsPostDet;

            fsPostBatchRow = graphInventoryPostBatchMaint.BatchRecords.Current = graphInventoryPostBatchMaint.BatchRecords.Search <FSPostBatch.batchID>(fsPostBatchRow.BatchID);

            fsPostInfoRow = PXSelect <FSPostInfo,
                                      Where <
                                          FSPostInfo.postID, Equal <Required <FSPostInfo.postID> > > >
                            .Select(this, appointmentInventoryItemInfoRow.FSAppointmentInventoryItem.PostID);

            if (fsPostInfoRow == null || fsPostInfoRow.PostID == null)
            {
                fsPostInfoRow = new FSPostInfo();
                fsPostInfoRow = graphPostInfoEntry.PostInfoRecords.Current = graphPostInfoEntry.PostInfoRecords.Insert(fsPostInfoRow);
            }
            else
            {
                fsPostInfoRow = graphPostInfoEntry.PostInfoRecords.Current = graphPostInfoEntry.PostInfoRecords.Search <FSPostInfo.postID>(fsPostInfoRow.PostID);
            }

            fsPostInfoRow.INPosted  = true;
            fsPostInfoRow.INDocType = graphINIssueEntry.issue.Current.DocType;
            fsPostInfoRow.INRefNbr  = graphINIssueEntry.issue.Current.RefNbr;

            foreach (INTran inTranRowLocal in graphINIssueEntry.transactions.Select())
            {
                FSxINTran fsxINTranRow = graphINIssueEntry.transactions.Cache.GetExtension <FSxINTran>(inTranRowLocal);

                if (fsxINTranRow != null && appointmentInventoryItemInfoRow.FSAppointmentInventoryItem.AppDetID == fsxINTranRow.AppDetID)
                {
                    fsPostInfoRow.INLineNbr = inTranRowLocal.LineNbr;
                    break;
                }
            }

            fsPostInfoRow.AppointmentID = appointmentInventoryItemInfoRow.FSAppointmentRow.AppointmentID;
            fsPostInfoRow.SOID          = appointmentInventoryItemInfoRow.FSAppointmentRow.SOID;

            fsPostInfoRow = graphPostInfoEntry.PostInfoRecords.Update(fsPostInfoRow);

            graphPostInfoEntry.Save.Press();
            fsPostInfoRow = graphPostInfoEntry.PostInfoRecords.Current;

            fsPostDet = new FSPostDet();

            fsPostDet.PostID    = fsPostInfoRow.PostID;
            fsPostDet.INPosted  = fsPostInfoRow.INPosted;
            fsPostDet.INDocType = fsPostInfoRow.INDocType;
            fsPostDet.INRefNbr  = fsPostInfoRow.INRefNbr;
            fsPostDet.INLineNbr = fsPostInfoRow.INLineNbr;

            graphInventoryPostBatchMaint.BatchDetails.Insert(fsPostDet);
            graphInventoryPostBatchMaint.Save.Press();

            fsAppointmentInventoryItemRow.Mem_BatchNbr = fsPostBatchRow.BatchNbr;

            fsPostInfoRow = graphPostInfoEntry.PostInfoRecords.Current;

            PXUpdate <
                Set <FSAppointmentInventoryItem.postID, Required <FSAppointmentInventoryItem.postID> >,
                FSAppointmentInventoryItem,
                Where <
                    FSAppointmentInventoryItem.appDetID, Equal <Required <FSAppointmentInventoryItem.appDetID> > > >
            .Update(this, fsPostInfoRow.PostID, appointmentInventoryItemInfoRow.FSAppointmentInventoryItem.AppDetID);
        }
        /// <summary>
        /// Creates an Issue document using the parameters <c>fsAppointmentRow</c>, <c>fsServiceOrderRow</c>, <c>fsServiceOrderTypeRow</c> and its posting information.
        /// </summary>
        protected void CreateDocumentIssue(INIssueEntry graphINIssueEntry, SharedClasses.AppointmentInventoryItemInfo appointmentInventoryItemInfoRow, FSAppointmentInventoryItem fsAppointmentInventoryItemRow, DateTime?documentDate, string documentPeriod, FSPostBatch fsPostBatchRow, ref string inRefNbr, ref string inDocType)
        {
            if (appointmentInventoryItemInfoRow != null)
            {
                INRegister inRegisterRow;
                #region IN Issue Header
                if (string.IsNullOrEmpty(inRefNbr))
                {
                    inRegisterRow = new INRegister();

                    inRegisterRow.DocType     = INDocType.Issue;
                    inRegisterRow.TranDate    = documentDate;
                    inRegisterRow.FinPeriodID = documentPeriod;
                    inRegisterRow.TranDesc    = appointmentInventoryItemInfoRow.FSAppointmentRow.DocDesc;
                    inRegisterRow.Hold        = false;
                    inRegisterRow             = graphINIssueEntry.issue.Current = graphINIssueEntry.issue.Insert(inRegisterRow);

                    inRegisterRow = graphINIssueEntry.issue.Update(inRegisterRow);
                }
                else
                {
                    inRegisterRow = graphINIssueEntry.issue.Current = graphINIssueEntry.issue.Search <INRegister.refNbr>(inRefNbr);
                }
                #endregion
                INTran inTranRow;

                inTranRow          = new INTran();
                inTranRow.TranType = INTranType.Issue;

                inTranRow = graphINIssueEntry.transactions.Current = graphINIssueEntry.transactions.Insert(inTranRow);

                graphINIssueEntry.transactions.Cache.SetValueExt <INTran.inventoryID>(inTranRow, appointmentInventoryItemInfoRow.FSAppointmentInventoryItem.InventoryID);

                if (PXAccess.FeatureInstalled <FeaturesSet.subItem>())
                {
                    graphINIssueEntry.transactions.Cache.SetValueExt <INTran.subItemID>(inTranRow, appointmentInventoryItemInfoRow.FSAppointmentInventoryItem.SubItemID);
                }

                graphINIssueEntry.transactions.Cache.SetValueExt <INTran.siteID>(inTranRow, appointmentInventoryItemInfoRow.FSAppointmentInventoryItem.SiteID);
                graphINIssueEntry.transactions.Cache.SetValueExt <INTran.qty>(inTranRow, appointmentInventoryItemInfoRow.FSAppointmentInventoryItem.Qty);
                graphINIssueEntry.transactions.Cache.SetValueExt <INTran.unitPrice>(inTranRow, appointmentInventoryItemInfoRow.FSAppointmentInventoryItem.UnitPrice);
                graphINIssueEntry.transactions.Cache.SetValueExt <INTran.tranAmt>(inTranRow, appointmentInventoryItemInfoRow.FSAppointmentInventoryItem.TranAmt);
                graphINIssueEntry.transactions.Cache.SetValueExt <INTran.projectID>(inTranRow, appointmentInventoryItemInfoRow.FSAppointmentInventoryItem.ProjectID);
                graphINIssueEntry.transactions.Cache.SetValueExt <INTran.taskID>(inTranRow, appointmentInventoryItemInfoRow.FSAppointmentInventoryItem.ProjectTaskID);

                FSxINTran fsxINTranRow = graphINIssueEntry.transactions.Cache.GetExtension <FSxINTran>(graphINIssueEntry.transactions.Current);

                fsxINTranRow.Source             = ID.Billing_By.APPOINTMENT;
                fsxINTranRow.SOID               = appointmentInventoryItemInfoRow.FSAppointmentRow.SOID;
                fsxINTranRow.BillCustomerID     = appointmentInventoryItemInfoRow.FSServiceOrderRow.BillCustomerID;
                fsxINTranRow.CustomerLocationID = appointmentInventoryItemInfoRow.FSServiceOrderRow.BillLocationID;
                fsxINTranRow.AppointmentID      = appointmentInventoryItemInfoRow.FSAppointmentRow.AppointmentID;
                fsxINTranRow.AppointmentDate    = new DateTime(
                    appointmentInventoryItemInfoRow.FSAppointmentRow.ActualDateTimeBegin.Value.Year,
                    appointmentInventoryItemInfoRow.FSAppointmentRow.ActualDateTimeBegin.Value.Month,
                    appointmentInventoryItemInfoRow.FSAppointmentRow.ActualDateTimeBegin.Value.Day,
                    0,
                    0,
                    0);
                fsxINTranRow.AppDetID = (int)appointmentInventoryItemInfoRow.FSAppointmentInventoryItem.AppDetID;

                inTranRow = graphINIssueEntry.transactions.Update(inTranRow);

                graphINIssueEntry.Save.Press();

                if (string.IsNullOrEmpty(inRefNbr))
                {
                    inRefNbr  = graphINIssueEntry.issue.Current.RefNbr;
                    inDocType = graphINIssueEntry.issue.Current.DocType;
                }

                UpdateIssuePostInfo(graphINIssueEntry, graphUpdatePostBatchMaint, graphPostInfoEntry, fsAppointmentInventoryItemRow, appointmentInventoryItemInfoRow, fsPostBatchRow);
            }
            else
            {
                throw new PXException(TX.Error.NOTHING_TO_BE_POSTED);
            }
        }
            /// <summary>
            /// Adds the line <c>fsAppointmentDetRow</c> or <c>fsAppointmentInventoryItem</c> to the List <c>fsAppointmentDetailsToPostInAR_AP</c> or <c>fsAppointmentDetailsToPostInSO</c> depending on <c>addToArApList</c> and <c>addToSoList</c> flags.
            /// </summary>
            private void AddAppointmentLineToList(PXGraph graph, FSAppointmentDet fsAppointmentDetRow, FSAppointmentInventoryItem fsAppointmentInventoryItem, AppointmentInfo appointmentInfo, bool addToArApList, bool addToSoList)
            {
                PostingLine appointmentLineRow = new PostingLine();

                if (fsAppointmentDetRow != null)
                {
                    int?itemID = fsAppointmentDetRow.InventoryID;

                    appointmentLineRow.TableSource = ID.TablePostSource.FSAPPOINTMENT_DET;
                    appointmentLineRow.LineType    = fsAppointmentDetRow.LineType;
                    appointmentLineRow.LineID      = fsAppointmentDetRow.AppDetID;
                    appointmentLineRow.BranchID    = appointmentInfo.FSServiceOrderRow.BranchID;
                    appointmentLineRow.InventoryID = itemID;
                    appointmentLineRow.UOM         = fsAppointmentDetRow.UOM;

                    appointmentLineRow.AcctID = this.GetAcct(graph, fsAppointmentDetRow, null, appointmentInfo.FSServiceOrderRow, appointmentInfo.FSSrvOrdTypeRow);
                    appointmentLineRow.SubID  = fsAppointmentDetRow.SubID;

                    appointmentLineRow.SiteID        = fsAppointmentDetRow.SiteID;
                    appointmentLineRow.LocationID    = fsAppointmentDetRow.SiteLocationID;
                    appointmentLineRow.SubItemID     = fsAppointmentDetRow.SubItemID;
                    appointmentLineRow.TranDesc      = fsAppointmentDetRow.TranDesc;
                    appointmentLineRow.ProjectID     = fsAppointmentDetRow.ProjectID;
                    appointmentLineRow.ProjectTaskID = fsAppointmentDetRow.ProjectTaskID;
                    appointmentLineRow.CuryUnitPrice = fsAppointmentDetRow.CuryUnitPrice;
                    appointmentLineRow.Qty           = fsAppointmentDetRow.Qty;
                    appointmentLineRow.CuryTranAmt   = fsAppointmentDetRow.TranAmt;
                    appointmentLineRow.PostID        = fsAppointmentDetRow.PostID;
                    appointmentLineRow.IsBillable    = fsAppointmentDetRow.IsBillable;
                }
                else if (fsAppointmentInventoryItem != null)
                {
                    appointmentLineRow.TableSource = ID.TablePostSource.FSAPPOINTMENT_DET;
                    appointmentLineRow.LineType    = fsAppointmentInventoryItem.LineType;
                    appointmentLineRow.LineID      = fsAppointmentInventoryItem.AppDetID;
                    appointmentLineRow.BranchID    = appointmentInfo.FSServiceOrderRow.BranchID;
                    appointmentLineRow.InventoryID = fsAppointmentInventoryItem.InventoryID;
                    appointmentLineRow.UOM         = fsAppointmentInventoryItem.UOM;

                    appointmentLineRow.AcctID = this.GetAcct(graph, null, fsAppointmentInventoryItem, appointmentInfo.FSServiceOrderRow, appointmentInfo.FSSrvOrdTypeRow);
                    appointmentLineRow.SubID  = null;

                    appointmentLineRow.SiteID        = fsAppointmentInventoryItem.SiteID;
                    appointmentLineRow.SubItemID     = fsAppointmentInventoryItem.SubItemID;
                    appointmentLineRow.TranDesc      = fsAppointmentInventoryItem.TranDesc;
                    appointmentLineRow.ProjectID     = fsAppointmentInventoryItem.ProjectID;
                    appointmentLineRow.ProjectTaskID = fsAppointmentInventoryItem.ProjectTaskID;
                    appointmentLineRow.CuryUnitPrice = fsAppointmentInventoryItem.CuryUnitPrice;
                    appointmentLineRow.Qty           = fsAppointmentInventoryItem.Qty;
                    appointmentLineRow.CuryTranAmt   = fsAppointmentInventoryItem.TranAmt;
                    appointmentLineRow.PostID        = fsAppointmentInventoryItem.PostID;
                    appointmentLineRow.IsBillable    = true;
                }

                if (addToArApList)
                {
                    this.FSPostingLineDetailsToPostInAR_AP.Add(appointmentLineRow);
                }
                else if (addToSoList)
                {
                    this.FSPostingLineDetailsToPostInSO.Add(appointmentLineRow);
                }
            }
        public override List <DocLineExt> GetInvoiceLines(Guid currentProcessID, int billingCycleID, string groupKey, bool getOnlyTotal, out decimal?invoiceTotal, string postTo)
        {
            PXGraph tempGraph = new PXGraph();

            if (getOnlyTotal == true)
            {
                /* Always keep both BQLs with the same Joins and Where conditions */
                FSAppointmentDet fsAppointmentDetRow =
                    PXSelectJoinGroupBy <FSAppointmentDet,
                                         InnerJoin <FSAppointment,
                                                    On <FSAppointment.appointmentID, Equal <FSAppointmentDet.appointmentID> >,
                                                    InnerJoin <FSServiceOrder,
                                                               On <FSServiceOrder.sOID, Equal <FSAppointment.sOID> >,
                                                               InnerJoin <FSSrvOrdType,
                                                                          On <FSSrvOrdType.srvOrdType, Equal <FSServiceOrder.srvOrdType> >,
                                                                          InnerJoin <FSPostDoc,
                                                                                     On <
                                                                                         FSPostDoc.appointmentID, Equal <FSAppointment.appointmentID>,
                                                                                         And <FSPostDoc.entityType, Equal <ListField_PostDoc_EntityType.Appointment> > >,
                                                                                     LeftJoin <FSPostInfo,
                                                                                               On <
                                                                                                   FSPostInfo.postID, Equal <FSAppointmentDet.postID> > > > > > >,
                                         Where <
                                             FSPostDoc.processID, Equal <Required <FSPostDoc.processID> >,
                                             And <FSPostDoc.billingCycleID, Equal <Required <FSPostDoc.billingCycleID> >,
                                                  And <FSPostDoc.groupKey, Equal <Required <FSPostDoc.groupKey> >,
                                                       And <FSAppointmentDet.lineType, NotEqual <FSAppointmentDet.lineType.Comment_Service>,
                                                            And <FSAppointmentDet.lineType, NotEqual <FSAppointmentDet.lineType.Comment_Part>,
                                                                 And <FSAppointmentDet.lineType, NotEqual <FSAppointmentDet.lineType.Instruction_Part>,
                                                                      And <FSAppointmentDet.lineType, NotEqual <FSAppointmentDet.lineType.Instruction_Service>,
                                                                           And <FSAppointmentDet.status, NotEqual <FSAppointmentDet.status.Canceled>,
                                                                                And <FSAppointmentDet.lineType, NotEqual <ListField_LineType_Pickup_Delivery.Pickup_Delivery>,
                                                                                     And <FSAppointmentDet.isPrepaid, Equal <False>,
                                                                                          And <
                                                                                              Where2 <
                                                                                                  Where <
                                                                                                      FSAppointmentDet.postID, IsNull>,
                                                                                                  Or <
                                                                                                      Where <
                                                                                                          FSPostInfo.aRPosted, Equal <False>,
                                                                                                          And <FSPostInfo.aPPosted, Equal <False>,
                                                                                                               And <FSPostInfo.sOPosted, Equal <False>,
                                                                                                                    And <
                                                                                                                        Where2 <
                                                                                                                            Where <
                                                                                                                                Required <FSPostBatch.postTo>, NotEqual <FSPostBatch.postTo.SO> >,
                                                                                                                            Or <
                                                                                                                                Where <
                                                                                                                                    Required <FSPostBatch.postTo>, Equal <FSPostBatch.postTo.SO>,
                                                                                                                                    And <FSPostInfo.iNPosted, Equal <False> > > > > > > > > > > > > > > > > > > > > >,
                                         Aggregate <
                                             Sum <FSAppointmentDet.tranAmt> > >
                    .Select(tempGraph, currentProcessID, billingCycleID, groupKey, postTo, postTo);

                invoiceTotal = fsAppointmentDetRow.TranAmt;

                FSAppointmentInventoryItem fsAppointmentInventoryItem =
                    PXSelectJoinGroupBy <FSAppointmentInventoryItem,
                                         InnerJoin <FSAppointment,
                                                    On <FSAppointment.appointmentID, Equal <FSAppointmentInventoryItem.appointmentID> >,
                                                    InnerJoin <FSServiceOrder,
                                                               On <FSServiceOrder.sOID, Equal <FSAppointment.sOID> >,
                                                               InnerJoin <FSSrvOrdType,
                                                                          On <FSSrvOrdType.srvOrdType, Equal <FSServiceOrder.srvOrdType> >,
                                                                          InnerJoin <FSPostDoc,
                                                                                     On <
                                                                                         FSPostDoc.appointmentID, Equal <FSAppointment.appointmentID>,
                                                                                         And <FSPostDoc.entityType, Equal <ListField_PostDoc_EntityType.Appointment> > >,
                                                                                     LeftJoin <FSPostInfo,
                                                                                               On <
                                                                                                   FSPostInfo.postID, Equal <FSAppointmentInventoryItem.postID> > > > > > >,
                                         Where <
                                             FSAppointmentInventoryItem.lineType, Equal <ListField_LineType_Pickup_Delivery.Pickup_Delivery>,
                                             And <FSPostDoc.processID, Equal <Required <FSPostDoc.processID> >,
                                                  And <FSPostDoc.billingCycleID, Equal <Required <FSPostDoc.billingCycleID> >,
                                                       And <FSPostDoc.groupKey, Equal <Required <FSPostDoc.groupKey> >,
                                                            And <
                                                                Where2 <
                                                                    Where <
                                                                        FSAppointmentInventoryItem.postID, IsNull>,
                                                                    Or <
                                                                        Where <
                                                                            FSPostInfo.aRPosted, Equal <False>,
                                                                            And <FSPostInfo.aPPosted, Equal <False>,
                                                                                 And <FSPostInfo.sOPosted, Equal <False>,
                                                                                      And <
                                                                                          Where2 <
                                                                                              Where <
                                                                                                  Required <FSPostBatch.postTo>, NotEqual <FSPostBatch.postTo.SO> >,
                                                                                              Or <
                                                                                                  Where <
                                                                                                      Required <FSPostBatch.postTo>, Equal <FSPostBatch.postTo.SO>,
                                                                                                      And <FSPostInfo.iNPosted, Equal <False> > > > > > > > > > > > > > > >,
                                         Aggregate <
                                             Sum <FSAppointmentInventoryItem.tranAmt> > >
                    .Select(tempGraph, currentProcessID, billingCycleID, groupKey, postTo, postTo);

                invoiceTotal += fsAppointmentInventoryItem.TranAmt ?? 0;

                return(null);
            }
            else
            {
                invoiceTotal = null;

                /* Always keep both BQLs with the same Joins and Where conditions */
                var resultSet1 = PXSelectJoin <FSAppointmentDet,
                                               InnerJoin <FSAppointment,
                                                          On <FSAppointment.appointmentID, Equal <FSAppointmentDet.appointmentID> >,
                                                          InnerJoin <FSServiceOrder,
                                                                     On <FSServiceOrder.sOID, Equal <FSAppointment.sOID> >,
                                                                     InnerJoin <FSSrvOrdType,
                                                                                On <FSSrvOrdType.srvOrdType, Equal <FSServiceOrder.srvOrdType> >,
                                                                                InnerJoin <FSPostDoc,
                                                                                           On <
                                                                                               FSPostDoc.appointmentID, Equal <FSAppointment.appointmentID>,
                                                                                               And <FSPostDoc.entityType, Equal <ListField_PostDoc_EntityType.Appointment> > >,
                                                                                           LeftJoin <FSPostInfo,
                                                                                                     On <
                                                                                                         FSPostInfo.postID, Equal <FSAppointmentDet.postID> >,
                                                                                                     LeftJoin <FSSODet,
                                                                                                               On <FSSODet.srvOrdType, Equal <FSServiceOrder.srvOrdType>,
                                                                                                                   And <FSSODet.refNbr, Equal <FSServiceOrder.refNbr>,
                                                                                                                        And <FSSODet.sODetID, Equal <FSAppointmentDet.sODetID> > > >,
                                                                                                               LeftJoin <FSSODetSplit,
                                                                                                                         On <FSSODetSplit.srvOrdType, Equal <FSSODet.srvOrdType>,
                                                                                                                             And <FSSODetSplit.refNbr, Equal <FSSODet.refNbr>,
                                                                                                                                  And <FSSODetSplit.lineNbr, Equal <FSSODet.lineNbr> > > >,
                                                                                                                         LeftJoin <INItemPlan,
                                                                                                                                   On <INItemPlan.planID, Equal <FSSODetSplit.planID> > > > > > > > > >,
                                               Where <
                                                   FSPostDoc.processID, Equal <Required <FSPostDoc.processID> >,
                                                   And <FSPostDoc.billingCycleID, Equal <Required <FSPostDoc.billingCycleID> >,
                                                        And <FSPostDoc.groupKey, Equal <Required <FSPostDoc.groupKey> >,
                                                             And <FSAppointmentDet.lineType, NotEqual <FSAppointmentDet.lineType.Comment_Service>,
                                                                  And <FSAppointmentDet.lineType, NotEqual <FSAppointmentDet.lineType.Comment_Part>,
                                                                       And <FSAppointmentDet.lineType, NotEqual <FSAppointmentDet.lineType.Instruction_Part>,
                                                                            And <FSAppointmentDet.lineType, NotEqual <FSAppointmentDet.lineType.Instruction_Service>,
                                                                                 And <FSAppointmentDet.status, NotEqual <FSAppointmentDet.status.Canceled>,
                                                                                      And <FSAppointmentDet.lineType, NotEqual <ListField_LineType_Pickup_Delivery.Pickup_Delivery>,
                                                                                           And <FSAppointmentDet.isPrepaid, Equal <False>,
                                                                                                And <
                                                                                                    Where2 <
                                                                                                        Where <
                                                                                                            FSAppointmentDet.postID, IsNull>,
                                                                                                        Or <
                                                                                                            Where <
                                                                                                                FSPostInfo.aRPosted, Equal <False>,
                                                                                                                And <FSPostInfo.aPPosted, Equal <False>,
                                                                                                                     And <FSPostInfo.sOPosted, Equal <False>,
                                                                                                                          And <
                                                                                                                              Where2 <
                                                                                                                                  Where <
                                                                                                                                      Required <FSPostBatch.postTo>, NotEqual <FSPostBatch.postTo.SO> >,
                                                                                                                                  Or <
                                                                                                                                      Where <
                                                                                                                                          Required <FSPostBatch.postTo>, Equal <FSPostBatch.postTo.SO>,
                                                                                                                                          And <FSPostInfo.iNPosted, Equal <False> > > > > > > > > > > > > > > > > > > > > >,
                                               OrderBy <
                                                   Asc <FSAppointment.executionDate,
                                                        Asc <FSAppointmentDet.appointmentID,
                                                             Asc <FSAppointmentDet.appDetID> > > > >
                                 .Select(tempGraph, currentProcessID, billingCycleID, groupKey, postTo, postTo);

                var docLines = new List <DocLineExt>();

                foreach (PXResult <FSAppointmentDet, FSAppointment, FSServiceOrder, FSSrvOrdType, FSPostDoc, FSPostInfo, FSSODet, FSSODetSplit, INItemPlan> row in resultSet1)
                {
                    docLines.Add(new DocLineExt(row));
                }

                var resultSet2 = PXSelectJoin <FSAppointmentInventoryItem,
                                               InnerJoin <FSAppointment,
                                                          On <FSAppointment.appointmentID, Equal <FSAppointmentInventoryItem.appointmentID> >,
                                                          InnerJoin <FSServiceOrder,
                                                                     On <FSServiceOrder.sOID, Equal <FSAppointment.sOID> >,
                                                                     InnerJoin <FSSrvOrdType,
                                                                                On <FSSrvOrdType.srvOrdType, Equal <FSServiceOrder.srvOrdType> >,
                                                                                InnerJoin <FSPostDoc,
                                                                                           On <
                                                                                               FSPostDoc.appointmentID, Equal <FSAppointment.appointmentID>,
                                                                                               And <FSPostDoc.entityType, Equal <ListField_PostDoc_EntityType.Appointment> > >,
                                                                                           LeftJoin <FSPostInfo,
                                                                                                     On <
                                                                                                         FSPostInfo.postID, Equal <FSAppointmentInventoryItem.postID> > > > > > >,
                                               Where <
                                                   FSAppointmentInventoryItem.lineType, Equal <ListField_LineType_Pickup_Delivery.Pickup_Delivery>,
                                                   And <FSPostDoc.processID, Equal <Required <FSPostDoc.processID> >,
                                                        And <FSPostDoc.billingCycleID, Equal <Required <FSPostDoc.billingCycleID> >,
                                                             And <FSPostDoc.groupKey, Equal <Required <FSPostDoc.groupKey> >,
                                                                  And <
                                                                      Where2 <
                                                                          Where <
                                                                              FSAppointmentInventoryItem.postID, IsNull>,
                                                                          Or <
                                                                              Where <
                                                                                  FSPostInfo.aRPosted, Equal <False>,
                                                                                  And <FSPostInfo.aPPosted, Equal <False>,
                                                                                       And <FSPostInfo.sOPosted, Equal <False>,
                                                                                            And <
                                                                                                Where2 <
                                                                                                    Where <
                                                                                                        Required <FSPostBatch.postTo>, NotEqual <FSPostBatch.postTo.SO> >,
                                                                                                    Or <
                                                                                                        Where <
                                                                                                            Required <FSPostBatch.postTo>, Equal <FSPostBatch.postTo.SO>,
                                                                                                            And <FSPostInfo.iNPosted, Equal <False> > > > > > > > > > > > > > > >,
                                               OrderBy <
                                                   Asc <FSAppointment.executionDate,
                                                        Asc <FSAppointmentInventoryItem.appointmentID,
                                                             Asc <FSAppointmentInventoryItem.appDetID> > > > >
                                 .Select(tempGraph, currentProcessID, billingCycleID, groupKey, postTo, postTo);

                DocLineExt docLineExtRow;

                foreach (PXResult <FSAppointmentInventoryItem, FSAppointment, FSServiceOrder, FSSrvOrdType, FSPostDoc, FSPostInfo, INItemPlan> row in resultSet2)
                {
                    docLineExtRow = new DocLineExt(row);

                    docLineExtRow.docLine.AcctID = ServiceOrderCore.Get_TranAcctID_DefaultValue(
                        this,
                        docLineExtRow.fsSrvOrdType.SalesAcctSource,
                        docLineExtRow.docLine.InventoryID,
                        docLineExtRow.fsServiceOrder);

                    docLines.Add(docLineExtRow);
                }

                return(docLines);
            }
        }