Пример #1
0
        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;

            Vendor vendorRow = SharedFunctions.GetVendorRow(graphProcess, fsServiceOrderRow.BillCustomerID);

            if (vendorRow == null)
            {
                throw new PXException(TX.Error.AP_POSTING_VENDOR_NOT_FOUND);
            }

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

            APInvoice apInvoiceRow = new APInvoice();

            if (invtMult >= 0)
            {
                apInvoiceRow.DocType = APDocType.DebitAdj;
                AutoNumberHelper.CheckAutoNumbering(Base, Base.APSetup.SelectSingle().DebitAdjNumberingID);
            }
            else
            {
                apInvoiceRow.DocType = APDocType.Invoice;
                AutoNumberHelper.CheckAutoNumbering(Base, Base.APSetup.SelectSingle().InvoiceNumberingID);
            }

            apInvoiceRow.DocDate     = invoiceDate;
            apInvoiceRow.FinPeriodID = invoiceFinPeriodID;
            apInvoiceRow             = PXCache <APInvoice> .CreateCopy(Base.Document.Insert(apInvoiceRow));

            initialHold         = apInvoiceRow.Hold;
            apInvoiceRow.NoteID = null;
            PXNoteAttribute.GetNoteIDNow(Base.Document.Cache, apInvoiceRow);
            apInvoiceRow.VendorID         = fsServiceOrderRow.BillCustomerID;
            apInvoiceRow.VendorLocationID = fsServiceOrderRow.BillLocationID;
            apInvoiceRow.CuryID           = fsServiceOrderRow.CuryID;
            apInvoiceRow.TaxZoneID        = fsAppointmentRow != null ? fsAppointmentRow.TaxZoneID : fsServiceOrderRow.TaxZoneID;
            apInvoiceRow.TaxCalcMode      = fsAppointmentRow != null ? fsAppointmentRow.TaxCalcMode : fsServiceOrderRow.TaxCalcMode;

            apInvoiceRow.SuppliedByVendorLocationID = fsServiceOrderRow.BillLocationID;

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

            if (termsID == null)
            {
                termsID = fsSrvOrdTypeRow.DfltTermIDAP;
            }

            apInvoiceRow.FinPeriodID = invoiceFinPeriodID;
            apInvoiceRow.TermsID     = termsID;
            apInvoiceRow.DocDesc     = fsServiceOrderRow.DocDesc;
            apInvoiceRow.Hold        = true;
            apInvoiceRow             = Base.Document.Update(apInvoiceRow);
            apInvoiceRow.TaxCalcMode = PX.Objects.TX.TaxCalculationMode.TaxSetting;
            apInvoiceRow             = Base.Document.Update(apInvoiceRow);

            InvoicingFunctions.SetContactAndAddress(Base, fsServiceOrderRow);

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

            IDocLine  docLine      = null;
            APTran    apTranRow    = null;
            FSxAPTran fsxAPTranRow = null;
            PMTask    pmTaskRow    = null;

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

                apTranRow = new APTran();
                apTranRow = Base.Transactions.Insert(apTranRow);

                Base.Transactions.Cache.SetValueExtIfDifferent <APTran.branchID>(apTranRow, docLine.BranchID);
                Base.Transactions.Cache.SetValueExtIfDifferent <APTran.inventoryID>(apTranRow, docLine.InventoryID);
                Base.Transactions.Cache.SetValueExtIfDifferent <APTran.uOM>(apTranRow, 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.AcctID != null)
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <APTran.accountID>(apTranRow, docLine.AcctID);
                }

                if (docLine.SubID != null)
                {
                    try
                    {
                        Base.Transactions.Cache.SetValueExtIfDifferent <APTran.subID>(apTranRow, docLine.SubID);
                    }
                    catch (PXException)
                    {
                        apTranRow.SubID = null;
                    }
                }
                else
                {
                    InvoicingFunctions.SetCombinedSubID(graphProcess,
                                                        Base.Transactions.Cache,
                                                        null,
                                                        apTranRow,
                                                        null,
                                                        fsSrvOrdTypeRow,
                                                        apTranRow.BranchID,
                                                        apTranRow.InventoryID,
                                                        apInvoiceRow.VendorLocationID,
                                                        fsServiceOrderRow.BranchLocationID,
                                                        fsServiceOrderRow.SalesPersonID,
                                                        docLine.IsService);
                }

                if (docLine.ProjectID != null && docLine.ProjectTaskID != null)
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <APTran.taskID>(apTranRow, docLine.ProjectTaskID);
                }

                Base.Transactions.Cache.SetValueExtIfDifferent <APTran.qty>(apTranRow, docLine.GetQty(FieldType.BillableField));
                Base.Transactions.Cache.SetValueExtIfDifferent <APTran.tranDesc>(apTranRow, docLine.TranDesc);

                apTranRow = Base.Transactions.Update(apTranRow);
                Base.Transactions.Cache.SetValueExtIfDifferent <APTran.curyUnitCost>(apTranRow, docLine.CuryUnitPrice * invtMult);

                if (docLine.ProjectID != null)
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <APTran.projectID>(apTranRow, docLine.ProjectID);
                }

                Base.Transactions.Cache.SetValueExtIfDifferent <APTran.taxCategoryID>(apTranRow, docLine.TaxCategoryID);
                Base.Transactions.Cache.SetValueExtIfDifferent <APTran.costCodeID>(apTranRow, docLine.CostCodeID);

                if (docLine.IsBillable == false)
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <APTran.manualDisc>(apTranRow, true);
                    Base.Transactions.Cache.SetValueExtIfDifferent <APTran.curyDiscAmt>(apTranRow, docLine.GetQty(FieldType.BillableField) * docLine.CuryUnitPrice * invtMult);
                }
                else
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <APTran.discPct>(apTranRow, docLine.DiscPct);
                }

                fsxAPTranRow = Base.Transactions.Cache.GetExtension <FSxAPTran>(apTranRow);

                fsxAPTranRow.Source           = docLine.BillingBy;
                fsxAPTranRow.SOID             = fsServiceOrderRow.SOID;
                fsxAPTranRow.ServiceOrderDate = fsServiceOrderRow.OrderDate;

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

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

                if (docLine.BillingBy == ID.Billing_By.APPOINTMENT)
                {
                    //// TODO AC-142850
                    ////fsxAPTranRow.AppointmentDate = new DateTime(
                    ////                                fsAppointmentRow.ActualDateTimeBegin.Value.Year,
                    ////                                fsAppointmentRow.ActualDateTimeBegin.Value.Month,
                    ////                                fsAppointmentRow.ActualDateTimeBegin.Value.Day,
                    ////                                0,
                    ////                                0,
                    ////                                0);
                }

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

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

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

            apInvoiceRow = Base.Document.Update(apInvoiceRow);

            if (Base.APSetup.Current.RequireControlTotal == true)
            {
                Base.Document.Cache.SetValueExtIfDifferent <APInvoice.curyOrigDocAmt>(apInvoiceRow, apInvoiceRow.CuryDocBal);
            }

            if (initialHold != true)
            {
                Base.Document.Cache.SetValueExtIfDifferent <APInvoice.hold>(apInvoiceRow, false);
            }

            apInvoiceRow = Base.Document.Update(apInvoiceRow);
        }
        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);
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
        }