/// <summary>
        /// Creates an Issue document using the parameters <c>fsAppointmentRow</c>, <c>fsServiceOrderRow</c>, <c>fsServiceOrderTypeRow</c> and its posting information.
        /// </summary>
        protected virtual void CreateDocumentIssue(INIssueEntry graphINIssueEntry, SharedClasses.AppointmentInventoryItemInfo appointmentInventoryItemInfoRow, FSAppointmentDet 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>
        /// Update the references in <c>FSPostInfo</c> and <c>FSPostDet</c> when the posting process of every AppointmentInventoryItem is complete in IN.
        /// </summary>
        public virtual void UpdateIssuePostInfo(INIssueEntry graphINIssueEntry, InventoryPostBatchMaint graphInventoryPostBatchMaint, PostInfoEntry graphPostInfoEntry, FSAppointmentDet 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 <FSAppointmentDet.postID, Required <FSAppointmentDet.postID> >,
                FSAppointmentDet,
                Where <
                    FSAppointmentDet.appDetID, Equal <Required <FSAppointmentDet.appDetID> > > >
            .Update(this, fsPostInfoRow.PostID, appointmentInventoryItemInfoRow.FSAppointmentInventoryItem.AppDetID);
        }
        public virtual void CreateInvoice(PXGraph graphProcess, List <DocLineExt> docLines, List <DocLineExt> docLinesGrouped, short invtMult, DateTime?invoiceDate, string invoiceFinPeriodID, OnDocumentHeaderInsertedDelegate onDocumentHeaderInserted, OnTransactionInsertedDelegate onTransactionInserted, PXQuickProcess.ActionFlow quickProcessFlow)
        {
            if (docLinesGrouped.Count == 0)
            {
                return;
            }

            bool?initialHold = false;

            FSServiceOrder fsServiceOrderRow = docLinesGrouped[0].fsServiceOrder;
            FSSrvOrdType   fsSrvOrdTypeRow   = docLinesGrouped[0].fsSrvOrdType;
            FSPostDoc      fsPostDocRow      = docLinesGrouped[0].fsPostDoc;
            FSAppointment  fsAppointmentRow  = docLinesGrouped[0].fsAppointment;

            Base.FieldDefaulting.AddHandler <INRegister.branchID>((sender, e) =>
            {
                e.NewValue = fsServiceOrderRow.BranchID;
                e.Cancel   = true;
            });

            INRegister inRegisterRow = new INRegister();

            inRegisterRow.DocType = INDocType.Issue;
            AutoNumberHelper.CheckAutoNumbering(Base, Base.insetup.SelectSingle().IssueNumberingID);

            inRegisterRow.TranDate    = invoiceDate;
            inRegisterRow.FinPeriodID = invoiceFinPeriodID;
            inRegisterRow.TranDesc    = fsAppointmentRow != null ? fsAppointmentRow.DocDesc : fsServiceOrderRow.DocDesc;

            inRegisterRow = PXCache <INRegister> .CreateCopy(Base.issue.Insert(inRegisterRow));

            initialHold          = inRegisterRow.Hold;
            inRegisterRow.NoteID = null;
            PXNoteAttribute.GetNoteIDNow(Base.issue.Cache, inRegisterRow);

            Base.issue.Cache.SetValueExtIfDifferent <INRegister.hold>(inRegisterRow, true);

            inRegisterRow = Base.issue.Update(inRegisterRow);

            if (onDocumentHeaderInserted != null)
            {
                onDocumentHeaderInserted(Base, inRegisterRow);
            }

            IDocLine  docLine      = null;
            INTran    inTranRow    = null;
            FSxINTran fsxINTranRow = null;
            PMTask    pmTaskRow    = null;

            List <GroupDocLineExt> singleLines =
                docLines.Where(x => x.docLine.LineType == ID.LineType_ALL.INVENTORY_ITEM).GroupBy(
                    x => new { x.docLine.DocID, x.docLine.LineID },
                    (key, group)
                    => new GroupDocLineExt(key.DocID, key.LineID, group.ToList())).ToList();

            foreach (GroupDocLineExt singleLine in singleLines)
            {
                DocLineExt docLineExt = singleLine.Group.First();

                docLine           = docLineExt.docLine;
                fsPostDocRow      = docLineExt.fsPostDoc;
                fsServiceOrderRow = docLineExt.fsServiceOrder;
                fsSrvOrdTypeRow   = docLineExt.fsSrvOrdType;
                fsAppointmentRow  = docLineExt.fsAppointment;

                inTranRow = new INTran();

                inTranRow.BranchID = docLine.BranchID;
                inTranRow.TranType = INTranType.Issue;

                inTranRow = PXCache <INTran> .CreateCopy(Base.transactions.Insert(inTranRow));

                inTranRow.InventoryID = docLine.InventoryID;
                inTranRow.UOM         = docLine.UOM;

                pmTaskRow = docLineExt.pmTask;

                if (pmTaskRow != null && pmTaskRow.Status == ProjectTaskStatus.Completed)
                {
                    throw new PXException(TX.Error.POSTING_PMTASK_ALREADY_COMPLETED, fsServiceOrderRow.RefNbr, docLine.LineRef, pmTaskRow.TaskCD);
                }

                if (docLine.ProjectID != null && docLine.ProjectTaskID != null)
                {
                    inTranRow.ProjectID = docLine.ProjectID;
                    inTranRow.TaskID    = docLine.ProjectTaskID;
                }

                inTranRow.SiteID     = docLine.SiteID;
                inTranRow.LocationID = docLine.SiteLocationID;
                inTranRow.TranDesc   = docLine.TranDesc;
                inTranRow.CostCodeID = docLine.CostCodeID;
                inTranRow.ReasonCode = fsSrvOrdTypeRow.ReasonCode;

                inTranRow = PXCache <INTran> .CreateCopy(Base.transactions.Update(inTranRow));

                INTranSplit currentSplit = Base.splits.Select();

                if (fsAppointmentRow == null)
                {
                    bool qtyAssigned = false;

                    if (currentSplit != null &&
                        singleLine.Group != null &&
                        singleLine.Group.Count > 0)
                    {
                        Base.splits.Delete(currentSplit);
                    }

                    foreach (DocLineExt splitLine in singleLine.Group)
                    {
                        if (splitLine.fsSODetSplit.SplitLineNbr != null && splitLine.fsSODetSplit.Completed == false)
                        {
                            INTranSplit split = new INTranSplit();
                            split = Base.splits.Insert(split);
                            INTranSplit copySplit = (INTranSplit)Base.splits.Cache.CreateCopy(split);

                            copySplit.SiteID       = splitLine.fsSODetSplit.SiteID != null ? splitLine.fsSODetSplit.SiteID : copySplit.SiteID;
                            copySplit.LocationID   = splitLine.fsSODetSplit.LocationID != null ? splitLine.fsSODetSplit.LocationID : copySplit.LocationID;
                            copySplit.LotSerialNbr = splitLine.fsSODetSplit.LotSerialNbr;
                            copySplit.Qty          = splitLine.fsSODetSplit.Qty;

                            split       = Base.splits.Update(copySplit);
                            qtyAssigned = true;
                        }
                    }

                    inTranRow = (INTran)Base.transactions.Cache.CreateCopy(Base.transactions.Current);

                    if (qtyAssigned == false)
                    {
                        inTranRow.Qty = docLine.GetQty(FieldType.BillableField);
                    }
                    else if (inTranRow.Qty != docLine.GetQty(FieldType.BillableField))
                    {
                        throw new PXException(TX.Error.QTY_POSTED_ERROR);
                    }
                }
                else
                {
                    bool qtyAssigned = false;
                    if (string.IsNullOrEmpty(docLine.LotSerialNbr) == false)
                    {
                        if (currentSplit != null)
                        {
                            Base.splits.Delete(currentSplit);
                        }

                        INTranSplit split = new INTranSplit();
                        split = Base.splits.Insert(split);
                        INTranSplit copySplit = (INTranSplit)Base.splits.Cache.CreateCopy(split);

                        copySplit.SiteID       = docLine.SiteID;
                        copySplit.LocationID   = docLine.SiteLocationID != null ? docLine.SiteLocationID : copySplit.LocationID;
                        copySplit.LotSerialNbr = docLine.LotSerialNbr != null ? docLine.LotSerialNbr : copySplit.LotSerialNbr;
                        copySplit.Qty          = docLine.GetQty(FieldType.BillableField);

                        split       = Base.splits.Update(copySplit);
                        qtyAssigned = true;
                    }

                    inTranRow = (INTran)Base.transactions.Cache.CreateCopy(Base.transactions.Current);

                    if (qtyAssigned == false)
                    {
                        inTranRow.Qty = docLine.GetQty(FieldType.BillableField);
                    }
                    else if (inTranRow.Qty != docLine.GetQty(FieldType.BillableField))
                    {
                        throw new PXException(TX.Error.QTY_POSTED_ERROR);
                    }
                }

                inTranRow.UnitPrice = docLine.CuryUnitPrice * invtMult;
                inTranRow.TranAmt   = docLine.GetTranAmt(FieldType.BillableField) * invtMult;

                fsxINTranRow                    = Base.transactions.Cache.GetExtension <FSxINTran>(inTranRow);
                fsxINTranRow.Source             = docLine.BillingBy;
                fsxINTranRow.SOID               = fsServiceOrderRow.SOID;
                fsxINTranRow.BillCustomerID     = fsServiceOrderRow.CustomerID;
                fsxINTranRow.CustomerLocationID = fsServiceOrderRow.LocationID;
                fsxINTranRow.SODetID            = docLine.PostSODetID;
                fsxINTranRow.AppointmentID      = docLine.PostAppointmentID;
                fsxINTranRow.AppointmentDate    = fsAppointmentRow?.ExecutionDate;
                fsxINTranRow.AppDetID           = docLine.PostAppDetID;

                SharedFunctions.CopyNotesAndFiles(Base.transactions.Cache, inTranRow, docLine, fsSrvOrdTypeRow);

                fsPostDocRow.INDocLineRef = inTranRow = Base.transactions.Update(inTranRow);

                if (onTransactionInserted != null)
                {
                    onTransactionInserted(Base, inTranRow);
                }
            }

            inRegisterRow = Base.issue.Update(inRegisterRow);

            if (Base.insetup.Current?.RequireControlTotal == true)
            {
                Base.issue.Cache.SetValueExtIfDifferent <INRegister.controlQty>(inRegisterRow, inRegisterRow.TotalQty);
                Base.issue.Cache.SetValueExtIfDifferent <INRegister.controlAmount>(inRegisterRow, inRegisterRow.TotalAmount);
            }

            if (initialHold != true)
            {
                Base.issue.Cache.SetValueExtIfDifferent <INRegister.hold>(inRegisterRow, false);
            }

            inRegisterRow = Base.issue.Update(inRegisterRow);
        }