Example #1
0
            private int?GetAcct(PXGraph graph,
                                IFSSODetBase fsSODetBase,
                                FSAppointmentDet 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,
                                                                              fsSODetBase.SiteID,
                                                                              fsServiceOrderRow);
                    }
                }
                else if (fsAppointmentInventoryItem != null)
                {
                    acctID = ServiceOrderCore.Get_TranAcctID_DefaultValue(graph,
                                                                          fsSrvOrdTypeRow.SalesAcctSource,
                                                                          fsAppointmentInventoryItem.InventoryID,
                                                                          fsAppointmentInventoryItem.SiteID,
                                                                          fsServiceOrderRow);
                }

                return(acctID);
            }
        public static void X_AcctID_FieldDefaulting <DAC>(PXCache cache, PXFieldDefaultingEventArgs e,
                                                          FSSrvOrdType fsSrvOrdTypeRow,
                                                          FSServiceOrder fsServiceOrderRow)
            where DAC : class, IBqlTable, IFSSODetBase, new()
        {
            if (e.Row == null || fsSrvOrdTypeRow == null || fsServiceOrderRow == null)
            {
                return;
            }

            var row = (DAC)e.Row;

            if (IsInventoryLine(row.LineType) == false)
            {
                e.NewValue = null;
            }
            else
            {
                e.NewValue = ServiceOrderCore.Get_TranAcctID_DefaultValue(cache.Graph, fsSrvOrdTypeRow.SalesAcctSource, row.InventoryID, fsServiceOrderRow);
            }
        }
        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 <FSLineType.Comment>,
                                                            And <FSAppointmentDet.lineType, NotEqual <FSLineType.Instruction>,
                                                                 And <FSAppointmentDet.isCanceledNotPerformed, NotEqual <True>,
                                                                      And <FSAppointmentDet.lineType, NotEqual <ListField_LineType_Pickup_Delivery.Pickup_Delivery>,
                                                                           And <FSAppointmentDet.isPrepaid, Equal <False>,
                                                                                And <FSAppointmentDet.isBillable, Equal <True>,
                                                                                     And <
                                                                                         Where2 <
                                                                                             Where <
                                                                                                 FSAppointmentDet.postID, IsNull>,
                                                                                             Or <
                                                                                                 Where <
                                                                                                     FSPostInfo.aRPosted, Equal <False>,
                                                                                                     And <FSPostInfo.aPPosted, Equal <False>,
                                                                                                          And <FSPostInfo.sOPosted, Equal <False>,
                                                                                                               And <FSPostInfo.sOInvPosted, 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.billableTranAmt> > >
                    .Select(tempGraph, currentProcessID, billingCycleID, groupKey, postTo, postTo);

                invoiceTotal = fsAppointmentDetRow.BillableTranAmt;

                FSAppointmentDet fsAppointmentInventoryItem =
                    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 <
                                             FSAppointmentDet.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 <
                                                                        FSAppointmentDet.postID, IsNull>,
                                                                    Or <
                                                                        Where <
                                                                            FSPostInfo.aRPosted, Equal <False>,
                                                                            And <FSPostInfo.aPPosted, Equal <False>,
                                                                                 And <FSPostInfo.sOPosted, Equal <False>,
                                                                                      And <FSPostInfo.sOInvPosted, 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.billableTranAmt> > >
                    .Select(tempGraph, currentProcessID, billingCycleID, groupKey, postTo, postTo);

                invoiceTotal += fsAppointmentInventoryItem.BillableTranAmt ?? 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>,
                                                                                                                                       And <FSSODetSplit.completed, Equal <False> > > > >,
                                                                                                                         LeftJoin <INItemPlan,
                                                                                                                                   On <INItemPlan.planID, Equal <FSSODetSplit.planID> >,
                                                                                                                                   LeftJoin <PMTask,
                                                                                                                                             On <PMTask.taskID, Equal <FSAppointmentDet.projectTaskID> > > > > > > > > > >,
                                               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 <FSLineType.Comment>,
                                                                  And <FSAppointmentDet.lineType, NotEqual <FSLineType.Instruction>,
                                                                       And <FSAppointmentDet.isCanceledNotPerformed, NotEqual <True>,
                                                                            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 <FSPostInfo.sOInvPosted, 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, PMTask> row in resultSet1)
                {
                    docLines.Add(new DocLineExt(row));
                }

                var resultSet2 = 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 <PMTask,
                                                                                                               On <PMTask.taskID, Equal <FSAppointmentDet.projectTaskID> > > > > > > >,
                                               Where <
                                                   FSAppointmentDet.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 <
                                                                              FSAppointmentDet.postID, IsNull>,
                                                                          Or <
                                                                              Where <
                                                                                  FSPostInfo.aRPosted, Equal <False>,
                                                                                  And <FSPostInfo.aPPosted, Equal <False>,
                                                                                       And <FSPostInfo.sOPosted, Equal <False>,
                                                                                            And <FSPostInfo.sOInvPosted, 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);

                DocLineExt docLineExtRow;

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

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

                    docLines.Add(docLineExtRow);
                }

                return(docLines);
            }
        }
        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 = docLines[0].fsServiceOrder;
            FSSrvOrdType   fsSrvOrdTypeRow   = docLines[0].fsSrvOrdType;
            FSPostDoc      fsPostDocRow      = docLines[0].fsPostDoc;
            FSAppointment  fsAppointmentRow  = docLines[0].fsAppointment;

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

            ARInvoice arInvoiceRow = new ARInvoice();

            if (invtMult >= 0)
            {
                arInvoiceRow.DocType = ARInvoiceType.Invoice;
                AutoNumberHelper.CheckAutoNumbering(Base, Base.ARSetup.SelectSingle().InvoiceNumberingID);
            }
            else
            {
                arInvoiceRow.DocType = ARInvoiceType.CreditMemo;
                AutoNumberHelper.CheckAutoNumbering(Base, Base.ARSetup.SelectSingle().CreditAdjNumberingID);
            }

            arInvoiceRow.DocDate     = invoiceDate;
            arInvoiceRow.FinPeriodID = invoiceFinPeriodID;
            arInvoiceRow.InvoiceNbr  = fsServiceOrderRow.CustPORefNbr;
            arInvoiceRow             = Base.Document.Insert(arInvoiceRow);
            initialHold         = arInvoiceRow.Hold;
            arInvoiceRow.NoteID = null;
            PXNoteAttribute.GetNoteIDNow(Base.Document.Cache, arInvoiceRow);

            Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.hold>(arInvoiceRow, true);
            Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.customerID>(arInvoiceRow, fsServiceOrderRow.BillCustomerID);
            Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.customerLocationID>(arInvoiceRow, fsServiceOrderRow.BillLocationID);
            Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.curyID>(arInvoiceRow, fsServiceOrderRow.CuryID);

            Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.taxZoneID>(arInvoiceRow, fsAppointmentRow != null ? fsAppointmentRow.TaxZoneID : fsServiceOrderRow.TaxZoneID);
            Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.taxCalcMode>(arInvoiceRow, fsAppointmentRow != null ? fsAppointmentRow.TaxCalcMode : fsServiceOrderRow.TaxCalcMode);

            string termsID = InvoicingFunctions.GetTermsIDFromCustomerOrVendor(graphProcess, fsServiceOrderRow.BillCustomerID, null);

            if (termsID != null)
            {
                Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.termsID>(arInvoiceRow, termsID);
            }
            else
            {
                Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.termsID>(arInvoiceRow, fsSrvOrdTypeRow.DfltTermIDARSO);
            }

            if (fsServiceOrderRow.ProjectID != null)
            {
                Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.projectID>(arInvoiceRow, fsServiceOrderRow.ProjectID);
            }

            Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.docDesc>(arInvoiceRow, fsServiceOrderRow.DocDesc);
            arInvoiceRow.FinPeriodID = invoiceFinPeriodID;
            arInvoiceRow             = Base.Document.Update(arInvoiceRow);

            InvoicingFunctions.SetContactAndAddress(Base, fsServiceOrderRow);

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

            IDocLine  docLine      = null;
            ARTran    arTranRow    = null;
            FSxARTran fsxARTranRow = null;
            PMTask    pmTaskRow    = null;
            List <SharedClasses.SOARLineEquipmentComponent> componentList = new List <SharedClasses.SOARLineEquipmentComponent>();
            int?acctID;
            int?pivotAppointmentDetID = -1;

            foreach (DocLineExt docLineExt in docLines)
            {
                docLine = docLineExt.docLine;
                if (docLineExt.fsAppointment != null)
                {
                    if (pivotAppointmentDetID != docLineExt.docLine.LineID)
                    {
                        pivotAppointmentDetID = docLineExt.docLine.LineID;
                    }
                    else
                    {
                        continue;
                    }
                }

                FSSODetSplit fsSODetSplitRow = docLineExt.fsSODetSplit;
                fsPostDocRow      = docLineExt.fsPostDoc;
                fsServiceOrderRow = docLineExt.fsServiceOrder;
                fsSrvOrdTypeRow   = docLineExt.fsSrvOrdType;
                fsAppointmentRow  = docLineExt.fsAppointment;

                arTranRow = new ARTran();
                arTranRow = Base.Transactions.Insert(arTranRow);

                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.branchID>(arTranRow, docLine.BranchID);
                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.inventoryID>(arTranRow, docLine.InventoryID);

                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)
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.taskID>(arTranRow, docLine.ProjectTaskID);
                }

                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.uOM>(arTranRow, fsSODetSplitRow != null && fsSODetSplitRow.SplitLineNbr > 0 ? fsSODetSplitRow.UOM : docLine.UOM);
                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.siteID>(arTranRow, fsSODetSplitRow != null && fsSODetSplitRow.SplitLineNbr > 0 ? fsSODetSplitRow.SiteID : docLine.SiteID);
                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.locationID>(arTranRow, fsSODetSplitRow != null && fsSODetSplitRow.SplitLineNbr > 0 ? fsSODetSplitRow.LocationID : docLine.SiteLocationID);

                if (docLine.IsService == true || fsAppointmentRow != null)
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.qty>(arTranRow, docLine.GetQty(FieldType.BillableField));
                }
                else
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.qty>(arTranRow, fsSODetSplitRow != null && fsSODetSplitRow.SplitLineNbr > 0 ? fsSODetSplitRow.Qty : docLine.GetQty(FieldType.BillableField));
                }

                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.tranDesc>(arTranRow, docLine.TranDesc);

                fsPostDocRow.DocLineRef = arTranRow = Base.Transactions.Update(arTranRow);

                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.salesPersonID>(arTranRow, fsAppointmentRow == null ? fsServiceOrderRow.SalesPersonID : fsAppointmentRow.SalesPersonID);

                if (docLine.AcctID != null)
                {
                    acctID = docLine.AcctID;
                }
                else
                {
                    acctID = ServiceOrderCore.Get_TranAcctID_DefaultValue(graphProcess,
                                                                          fsSrvOrdTypeRow.SalesAcctSource,
                                                                          docLine.InventoryID,
                                                                          docLine.SiteID,
                                                                          fsServiceOrderRow);
                }

                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,
                                                        fsSrvOrdTypeRow,
                                                        arTranRow.BranchID,
                                                        arTranRow.InventoryID,
                                                        arInvoiceRow.CustomerLocationID,
                                                        fsServiceOrderRow.BranchLocationID,
                                                        fsServiceOrderRow.SalesPersonID,
                                                        docLine.IsService);
                }

                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.manualPrice>(arTranRow, docLine.ManualPrice);
                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.curyUnitPrice>(arTranRow, docLine.CuryUnitPrice * invtMult);

                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.taxCategoryID>(arTranRow, docLine.TaxCategoryID);
                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.commissionable>(arTranRow, fsAppointmentRow?.Commissionable ?? fsServiceOrderRow.Commissionable ?? false);
                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.costCodeID>(arTranRow, docLine.CostCodeID);

                if (docLine.IsBillable == false)
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.manualDisc>(arTranRow, true);
                    Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.curyDiscAmt>(arTranRow, docLine.GetQty(FieldType.BillableField) * docLine.CuryUnitPrice * invtMult);
                }
                else
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.curyExtPrice>(arTranRow, ((docLine.CuryBillableExtPrice * invtMult) / docLine.GetQty(FieldType.BillableField)) * arTranRow.Qty);
                    Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.discPct>(arTranRow, docLine.DiscPct);
                }


                if (fsAppointmentRow != null && !string.IsNullOrEmpty(docLine.LotSerialNbr))
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.lotSerialNbr>(arTranRow, docLine.LotSerialNbr);
                }
                else if (fsSODetSplitRow.SplitLineNbr > 0 && !string.IsNullOrEmpty(fsSODetSplitRow.LotSerialNbr))
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.lotSerialNbr>(arTranRow, fsSODetSplitRow.LotSerialNbr);
                }

                fsxARTranRow                  = Base.Transactions.Cache.GetExtension <FSxARTran>(arTranRow);
                fsxARTranRow.Source           = docLine.BillingBy;
                fsxARTranRow.SOID             = fsServiceOrderRow.SOID;
                fsxARTranRow.ServiceOrderDate = fsServiceOrderRow.OrderDate;

                fsxARTranRow.BillCustomerID     = fsServiceOrderRow.CustomerID;
                fsxARTranRow.CustomerLocationID = fsServiceOrderRow.LocationID;

                fsxARTranRow.SODetID         = docLine.PostSODetID;
                fsxARTranRow.AppointmentID   = docLine.PostAppointmentID;
                fsxARTranRow.AppointmentDate = fsAppointmentRow?.ExecutionDate;
                fsxARTranRow.AppDetID        = docLine.PostAppDetID;

                fsxARTranRow.Mem_PreviousPostID = docLine.PostID;
                fsxARTranRow.Mem_TableSource    = docLine.SourceTable;

                SharedFunctions.CopyNotesAndFiles(Base.Transactions.Cache, arTranRow, docLine, fsSrvOrdTypeRow);
                fsPostDocRow.DocLineRef = arTranRow = Base.Transactions.Update(arTranRow);

                if (fsAppointmentRow != null && !string.IsNullOrEmpty(docLine.LotSerialNbr))
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.lotSerialNbr>(arTranRow, docLine.LotSerialNbr);
                }
                else if (fsSODetSplitRow != null && fsSODetSplitRow.SplitLineNbr > 0 && !string.IsNullOrEmpty(fsSODetSplitRow.LotSerialNbr))
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.lotSerialNbr>(arTranRow, fsSODetSplitRow.LotSerialNbr);
                }

                if (PXAccess.FeatureInstalled <FeaturesSet.equipmentManagementModule>())
                {
                    if (docLine.EquipmentAction != null)
                    {
                        Base.Transactions.Cache.SetValueExtIfDifferent <FSxARTran.equipmentAction>(arTranRow, docLine.EquipmentAction);
                        Base.Transactions.Cache.SetValueExtIfDifferent <FSxARTran.sMEquipmentID>(arTranRow, docLine.SMEquipmentID);
                        Base.Transactions.Cache.SetValueExtIfDifferent <FSxARTran.equipmentLineRef>(arTranRow, docLine.EquipmentLineRef);

                        fsxARTranRow.Comment = docLine.Comment;

                        if (docLine.EquipmentAction == ID.Equipment_Action.SELLING_TARGET_EQUIPMENT ||
                            ((docLine.EquipmentAction == ID.Equipment_Action.CREATING_COMPONENT ||
                              docLine.EquipmentAction == ID.Equipment_Action.UPGRADING_COMPONENT ||
                              docLine.EquipmentAction == ID.Equipment_Action.NONE) &&
                             string.IsNullOrEmpty(docLine.NewTargetEquipmentLineNbr) == false))
                        {
                            componentList.Add(new SharedClasses.SOARLineEquipmentComponent(docLine, arTranRow, fsxARTranRow));
                        }
                        else
                        {
                            fsxARTranRow.ComponentID = docLine.ComponentID;
                        }
                    }
                }

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

            if (componentList.Count > 0)
            {
                //Assigning the NewTargetEquipmentLineNbr field value for the component type records
                foreach (SharedClasses.SOARLineEquipmentComponent currLineModel in componentList.Where(x => x.equipmentAction == ID.Equipment_Action.SELLING_TARGET_EQUIPMENT))
                {
                    foreach (SharedClasses.SOARLineEquipmentComponent currLineComponent in componentList.Where(x => (x.equipmentAction == ID.Equipment_Action.CREATING_COMPONENT ||
                                                                                                                     x.equipmentAction == ID.Equipment_Action.UPGRADING_COMPONENT ||
                                                                                                                     x.equipmentAction == ID.Equipment_Action.NONE)))
                    {
                        if (currLineComponent.sourceNewTargetEquipmentLineNbr == currLineModel.sourceLineRef)
                        {
                            currLineComponent.fsxARTranRow.ComponentID = currLineComponent.componentID;
                            currLineComponent.fsxARTranRow.NewTargetEquipmentLineNbr = currLineModel.currentLineRef;
                        }
                    }
                }
            }

            arInvoiceRow = Base.Document.Update(arInvoiceRow);

            if (Base.ARSetup.Current.RequireControlTotal == true)
            {
                Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.curyOrigDocAmt>(arInvoiceRow, arInvoiceRow.CuryDocBal);
            }

            if (initialHold != true || quickProcessFlow != PXQuickProcess.ActionFlow.NoFlow)
            {
                Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.hold>(arInvoiceRow, false);
            }

            arInvoiceRow = Base.Document.Update(arInvoiceRow);
        }
        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 <ARInvoice.branchID>((sender, e) =>
            {
                e.NewValue = fsServiceOrderRow.BranchID;
                e.Cancel   = true;
            });

            ARInvoice arInvoiceRow = new ARInvoice();

            if (invtMult >= 0)
            {
                arInvoiceRow.DocType = ARInvoiceType.Invoice;
            }
            else
            {
                arInvoiceRow.DocType = ARInvoiceType.CreditMemo;
            }

            arInvoiceRow.DocDate     = invoiceDate;
            arInvoiceRow.FinPeriodID = invoiceFinPeriodID;
            arInvoiceRow.InvoiceNbr  = fsServiceOrderRow.CustPORefNbr;
            arInvoiceRow             = Base.Document.Insert(arInvoiceRow);
            initialHold         = arInvoiceRow.Hold;
            arInvoiceRow.NoteID = null;
            PXNoteAttribute.GetNoteIDNow(Base.Document.Cache, arInvoiceRow);

            Base.Document.Cache.SetValueExt <ARInvoice.hold>(arInvoiceRow, true);
            Base.Document.Cache.SetValueExt <ARInvoice.customerID>(arInvoiceRow, fsServiceOrderRow.BillCustomerID);
            Base.Document.Cache.SetValueExt <ARInvoice.customerLocationID>(arInvoiceRow, fsServiceOrderRow.BillLocationID);
            Base.Document.Cache.SetValueExt <ARInvoice.curyID>(arInvoiceRow, fsServiceOrderRow.CuryID);

            Base.Document.Cache.SetValueExt <ARInvoice.taxZoneID>(arInvoiceRow, fsAppointmentRow != null ? fsAppointmentRow.TaxZoneID : fsServiceOrderRow.TaxZoneID);

            string termsID = InvoicingFunctions.GetTermsIDFromCustomerOrVendor(graphProcess, fsServiceOrderRow.BillCustomerID, null);

            if (termsID != null)
            {
                Base.Document.Cache.SetValueExt <ARInvoice.termsID>(arInvoiceRow, termsID);
            }
            else
            {
                Base.Document.Cache.SetValueExt <ARInvoice.termsID>(arInvoiceRow, fsSrvOrdTypeRow.DfltTermIDARSO);
            }

            if (fsServiceOrderRow.ProjectID != null)
            {
                Base.Document.Cache.SetValueExt <ARInvoice.projectID>(arInvoiceRow, fsServiceOrderRow.ProjectID);
            }

            Base.Document.Cache.SetValueExt <ARInvoice.docDesc>(arInvoiceRow, fsServiceOrderRow.DocDesc);
            arInvoiceRow.FinPeriodID = invoiceFinPeriodID;
            arInvoiceRow             = Base.Document.Update(arInvoiceRow);

            InvoicingFunctions.SetAddress(Base, fsServiceOrderRow);

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

            IDocLine  docLine      = null;
            ARTran    arTranRow    = null;
            FSxARTran fsxARTranRow = null;
            int?      acctID;

            foreach (DocLineExt docLineExt in docLinesGrouped)
            {
                docLine           = docLineExt.docLine;
                fsPostDocRow      = docLineExt.fsPostDoc;
                fsServiceOrderRow = docLineExt.fsServiceOrder;
                fsSrvOrdTypeRow   = docLineExt.fsSrvOrdType;
                fsAppointmentRow  = docLineExt.fsAppointment;

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

                if (docLine.ProjectID != null && docLine.ProjectTaskID != null)
                {
                    PMTask pmTaskRow = PXSelect <PMTask, Where <PMTask.taskID, Equal <Required <PMTask.taskID> > > > .Select(graphProcess, docLine.ProjectTaskID);

                    Base.Transactions.Cache.SetValueExt <ARTran.taskID>(arTranRow, pmTaskRow.TaskCD);
                }

                Base.Transactions.Cache.SetValueExt <ARTran.qty>(arTranRow, docLine.GetQty(FieldType.BillableField));
                Base.Transactions.Cache.SetValueExt <ARTran.tranDesc>(arTranRow, docLine.TranDesc);

                fsPostDocRow.DocLineRef = arTranRow = Base.Transactions.Update(arTranRow);

                Base.Transactions.Cache.SetValueExt <ARTran.salesPersonID>(arTranRow, fsAppointmentRow == null ? fsServiceOrderRow.SalesPersonID : fsAppointmentRow.SalesPersonID);

                if (docLine.AcctID != null)
                {
                    acctID = docLine.AcctID;
                }
                else
                {
                    acctID = (int?)ServiceOrderCore.Get_TranAcctID_DefaultValue(
                        graphProcess,
                        fsSrvOrdTypeRow.SalesAcctSource,
                        docLine.InventoryID,
                        fsServiceOrderRow);
                }

                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,
                        fsSrvOrdTypeRow,
                        arTranRow.BranchID,
                        arTranRow.InventoryID,
                        arInvoiceRow.CustomerLocationID,
                        fsServiceOrderRow.BranchLocationID,
                        fsServiceOrderRow.SalesPersonID,
                        docLine.IsService);
                }

                Base.Transactions.Cache.SetValueExt <ARTran.curyUnitPrice>(arTranRow, docLine.CuryUnitPrice * invtMult);

                Base.Transactions.Cache.SetValueExt <ARTran.taxCategoryID>(arTranRow, docLine.TaxCategoryID);
                //Base.Transactions.Cache.SetValueExt<ARTran.curyExtPrice>(arTranRow, docLine.GetTranAmt(FieldType.BillableField) * invtMult);
                Base.Transactions.Cache.SetValueExt <ARTran.commissionable>(arTranRow, fsAppointmentRow?.Commissionable ?? fsServiceOrderRow.Commissionable ?? false);

                Base.Transactions.Cache.SetValueExt <ARTran.costCodeID>(arTranRow, docLine.CostCodeID);

                fsxARTranRow                    = Base.Transactions.Cache.GetExtension <FSxARTran>(arTranRow);
                fsxARTranRow.Source             = docLine.BillingBy;
                fsxARTranRow.SOID               = fsServiceOrderRow.SOID;
                fsxARTranRow.ServiceOrderDate   = fsServiceOrderRow.OrderDate;
                fsxARTranRow.BillCustomerID     = fsServiceOrderRow.BillCustomerID;
                fsxARTranRow.CustomerLocationID = fsServiceOrderRow.BillLocationID;
                fsxARTranRow.SODetID            = docLine.PostSODetID;
                fsxARTranRow.AppointmentID      = docLine.PostAppointmentID;
                fsxARTranRow.AppointmentDate    = fsAppointmentRow?.ExecutionDate;
                fsxARTranRow.AppDetID           = docLine.PostAppDetID;

                fsxARTranRow.Mem_PreviousPostID = docLine.PostID;
                fsxARTranRow.Mem_TableSource    = docLine.SourceTable;

                SharedFunctions.CopyNotesAndFiles(Base.Transactions.Cache, arTranRow, docLine, fsSrvOrdTypeRow);
                fsPostDocRow.DocLineRef = arTranRow = Base.Transactions.Update(arTranRow);

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

            arInvoiceRow = Base.Document.Update(arInvoiceRow);

            if (Base.ARSetup.Current.RequireControlTotal == true)
            {
                Base.Document.Cache.SetValueExt <ARInvoice.curyOrigDocAmt>(arInvoiceRow, arInvoiceRow.CuryDocBal);
            }

            if (initialHold != true)
            {
                Base.Document.Cache.SetValueExt <ARInvoice.hold>(arInvoiceRow, false);
            }

            arInvoiceRow = Base.Document.Update(arInvoiceRow);
        }