예제 #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 virtual IEnumerable ValidateAddress(PXAdapter adapter)
        {
            if (BranchLocationRecords.Current != null && BranchLocationRecords.Current.IsValidated == false)
            {
                if (BranchLocationRecords.Current.CountryID != null && BranchLocationRecords.Current.City != null &&
                    BranchLocationRecords.Current.State != null && BranchLocationRecords.Current.PostalCode != null)
                {
                    string addressString = ServiceOrderCore.GetAddress(
                        BranchLocationRecords.Current.AddressLine1,
                        BranchLocationRecords.Current.AddressLine2,
                        BranchLocationRecords.Current.City,
                        BranchLocationRecords.Current.State,
                        BranchLocationRecords.Current.CountryID,
                        BranchLocationRecords.Current.PostalCode);

                    if (Geocoder.GetStatus(addressString, SetupRecord.Current.MapApiKey).CompareTo(System.Net.HttpStatusCode.OK.ToString()) == 0)
                    {
                        BranchLocationRecords.Cache.SetValue <FSBranchLocation.isValidated>(BranchLocationRecords.Current, true);
                    }
                    else
                    {
                        throw new PXException(TX.Error.ADDRESS_VALIDATION_FAILED);
                    }
                }
                else
                {
                    throw new PXException(TX.Error.VALIDATE_ADDRESS_MISSING_FIELDS);
                }
            }

            return(adapter.Get());
        }
예제 #3
0
        protected virtual void _(Events.RowSelecting <FSAdjust> e)
        {
            if (e.Row == null)
            {
                return;
            }

            FSAdjust fsAdjustRow = (FSAdjust)e.Row;

            using (new PXConnectionScope())
            {
                fsAdjustRow.SOCuryCompletedBillableTotal = ServiceOrderCore.GetServiceOrderBillableTotal(e.Cache.Graph, fsAdjustRow.AdjdOrderType, fsAdjustRow.AdjdOrderNbr);
            }
        }
        private void CloneServices(AppointmentEntry sourceAppointmentGraph, AppointmentEntry newAppointmentGraph, FSAppointment newAppointmentRow)
        {
            foreach (FSAppointmentDetService sourceRow in sourceAppointmentGraph.AppointmentDetServices.Select())
            {
                FSSODet fsSODetRow = ServiceOrderCore.GetSODetFromAppointmentDet(sourceAppointmentGraph, sourceRow);

                if (fsSODetRow != null &&
                    (fsSODetRow.Status == ID.Status_AppointmentDet.CANCELED ||
                     fsSODetRow.Status == ID.Status_AppointmentDet.COMPLETED))
                {
                    continue;
                }

                FSAppointmentDetService newRow = PXCache <FSAppointmentDetService> .CreateCopy(sourceRow);

                newRow.ActualDuration      = 0;
                newRow.ActualDateTimeBegin = null;
                newRow.ActualDateTimeEnd   = null;
                newRow.Qty = 0;

                newRow = AppointmentEntry.InsertServicePartLine <FSAppointmentDetService, FSAppointmentDetService>(
                    newAppointmentGraph.AppointmentDetServices.Cache,
                    newRow,
                    sourceAppointmentGraph.AppointmentDetServices.Cache,
                    sourceRow,
                    null,
                    sourceRow.SODetID,
                    copyTranDate: false,
                    tranDate: sourceRow.TranDate,
                    SetValuesAfterAssigningSODetID: true,
                    copyingFromQuote: false);

                PXNoteAttribute.CopyNoteAndFiles(
                    sourceAppointmentGraph.AppointmentDetServices.Cache,
                    sourceRow,
                    newAppointmentGraph.AppointmentDetServices.Cache,
                    newRow,
                    copyNotes: true,
                    copyFiles: false);


                newAppointmentGraph.AppointmentDetServices.SetValueExt <FSAppointmentDetService.acctID>(newRow, sourceRow.AcctID);
                newAppointmentGraph.AppointmentDetServices.SetValueExt <FSAppointmentDetService.subID>(newRow, sourceRow.SubID);

                AppointmentCore.UpdateAppointmentsInfoInServiceOrder(newAppointmentGraph.AppointmentDetServices.Cache, newRow, ServiceOrderRelated);
            }
        }
        public static void X_SubID_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 (row.AcctID == null)
            {
                return;
            }

            e.NewValue = ServiceOrderCore.Get_IFSSODetBase_SubID_DefaultValue(cache, row, fsServiceOrderRow, fsSrvOrdTypeRow);
        }
        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);
            }
        }
예제 #8
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);
        }
        public virtual FSContractPostDoc CreateInvoiceByContract(PXGraph graphProcess, DateTime?invoiceDate, string invoiceFinPeriodID, FSContractPostBatch fsContractPostBatchRow, FSServiceContract fsServiceContractRow, FSContractPeriod fsContractPeriodRow, List <ContractInvoiceLine> docLines)
        {
            if (docLines.Count == 0)
            {
                return(null);
            }

            FSSetup fsSetupRow = ServiceManagementSetup.GetServiceManagementSetup(graphProcess);

            ARInvoice arInvoiceRow = new ARInvoice();

            arInvoiceRow.DocType = ARInvoiceType.Invoice;

            arInvoiceRow.DocDate     = invoiceDate;
            arInvoiceRow.FinPeriodID = invoiceFinPeriodID;
            arInvoiceRow.Hold        = true;
            arInvoiceRow             = Base.Document.Insert(arInvoiceRow);

            Base.Document.Cache.SetValueExt <ARInvoice.customerID>(arInvoiceRow, fsServiceContractRow.BillCustomerID);
            Base.Document.Cache.SetValueExt <ARInvoice.customerLocationID>(arInvoiceRow, fsServiceContractRow.BillLocationID);
            Base.Document.Cache.SetValueExt <ARInvoice.branchID>(arInvoiceRow, fsServiceContractRow.BranchID);
            Base.Document.Cache.SetValueExt <ARInvoice.docDesc>(arInvoiceRow, "Contract Standardized Billing: " + fsServiceContractRow.RefNbr + " " + fsServiceContractRow.DocDesc);

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

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

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

            foreach (ContractInvoiceLine docLine in docLines)
            {
                arTranRow = new ARTran();
                arTranRow = Base.Transactions.Insert(arTranRow);

                Base.Transactions.Cache.SetValueExt <ARTran.inventoryID>(arTranRow, docLine.InventoryID);
                Base.Transactions.Cache.SetValueExt <ARTran.uOM>(arTranRow, docLine.UOM);
                Base.Transactions.Cache.SetValueExt <ARTran.salesPersonID>(arTranRow, docLine.SalesPersonID);

                arTranRow = Base.Transactions.Update(arTranRow);

                if (docLine.AcctID != null)
                {
                    acctID = docLine.AcctID;
                }
                else
                {
                    acctID = (int?)ServiceOrderCore.Get_INItemAcctID_DefaultValue(
                        graphProcess,
                        fsSetupRow.ContractSalesAcctSource,
                        docLine.InventoryID,
                        fsServiceContractRow);
                }

                Base.Transactions.Cache.SetValueExt <ARTran.accountID>(arTranRow, acctID);

                if (docLine.SubID != null)
                {
                    try
                    {
                        Base.Transactions.Cache.SetValueExt <ARTran.subID>(arTranRow, docLine.SubID);
                    }
                    catch (PXException)
                    {
                        arTranRow.SubID = null;
                    }
                }
                else
                {
                    InvoicingFunctions.SetCombinedSubID(
                        graphProcess,
                        Base.Transactions.Cache,
                        arTranRow,
                        null,
                        null,
                        fsSetupRow,
                        arTranRow.BranchID,
                        arTranRow.InventoryID,
                        arInvoiceRow.CustomerLocationID,
                        fsServiceContractRow.BranchLocationID);
                }

                Base.Transactions.Cache.SetValueExt <ARTran.qty>(arTranRow, docLine.Qty);

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

                Base.Transactions.Cache.SetValueExt <ARTran.tranDesc>(arTranRow, docLine.TranDescPrefix + arTranRow.TranDesc);

                arTranRow = Base.Transactions.Update(arTranRow);

                Base.Transactions.Cache.SetValueExt <ARTran.commissionable>(arTranRow, docLine.Commissionable ?? false);

                fsxARTranRow = Base.Transactions.Cache.GetExtension <FSxARTran>(arTranRow);

                fsxARTranRow.Source             = ID.Billing_By.CONTRACT;
                fsxARTranRow.ServiceContractID  = fsServiceContractRow.ServiceContractID;
                fsxARTranRow.ContractPeriodID   = fsContractPeriodRow.ContractPeriodID;
                fsxARTranRow.BillCustomerID     = fsServiceContractRow.BillCustomerID;
                fsxARTranRow.CustomerLocationID = fsServiceContractRow.BillLocationID;

                arTranRow = Base.Transactions.Update(arTranRow);
            }

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

            Base.Document.Cache.SetValueExt <ARInvoice.hold>(arInvoiceRow, false);

            Exception newException = null;

            try
            {
                Base.Save.Press();
            }
            catch (Exception e)
            {
                List <ErrorInfo> errorList = this.GetErrorInfo();

                var exceptionWithContextMessage = ExceptionHelper.GetExceptionWithContextMessage(
                    PXMessages.Localize(TX.Messages.ERROR_CREATING_INVOICE_IN_POSTING_BATCH),
                    e);

                newException = InvoicingFunctions.GetErrorInfoInLines(errorList, exceptionWithContextMessage);
            }

            if (newException != null)
            {
                throw newException;
            }

            arInvoiceRow = Base.Document.Current;

            FSContractPostDoc fsContractCreatedDocRow = new FSContractPostDoc()
            {
                ContractPeriodID    = fsContractPeriodRow.ContractPeriodID,
                ContractPostBatchID = fsContractPostBatchRow.ContractPostBatchID,
                PostDocType         = arInvoiceRow.DocType,
                PostedTO            = ID.Batch_PostTo.AR,
                PostRefNbr          = arInvoiceRow.RefNbr,
                ServiceContractID   = fsServiceContractRow.ServiceContractID
            };

            return(fsContractCreatedDocRow);
        }
        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);
        }
예제 #11
0
        protected virtual void UpdateBillingInfoInDoc(PXGraph tempGraph, FSServiceOrder fsServiceOrderRow, int?newCBID, int?newBillingCycleID, bool updateCutOffDate, bool forceUpdate, Dictionary <BillingCycleIDAndDate, DateTime?> cutOffDateCache)
        {
            if (fsServiceOrderRow.CBID == newCBID && updateCutOffDate == false && fsServiceOrderRow.CutOffDate != null && forceUpdate == false)
            {
                return;
            }

            if (fsServiceOrderRow.PostedBy == null)
            {
                using (PXTransactionScope ts = new PXTransactionScope())
                {
                    DateTime?newCutOffDate = null;
                    BillingCycleIDAndDate billingCycleIDAndDate = null;

                    if (newBillingCycleID != null)
                    {
                        billingCycleIDAndDate = new BillingCycleIDAndDate();
                        billingCycleIDAndDate.BillingCycleID = newBillingCycleID;
                        billingCycleIDAndDate.DocDate        = fsServiceOrderRow.OrderDate;

                        if (cutOffDateCache.TryGetValue(billingCycleIDAndDate, out newCutOffDate) == false)
                        {
                            newCutOffDate = ServiceOrderCore.GetCutOffDate(tempGraph, fsServiceOrderRow.CBID, fsServiceOrderRow.OrderDate);
                            cutOffDateCache.Add(billingCycleIDAndDate, newCutOffDate);
                        }
                    }

                    if (fsServiceOrderRow.CBID != newCBID || fsServiceOrderRow.CutOffDate != newCutOffDate)
                    {
                        PXUpdate <
                            Set <FSServiceOrder.cBID, Required <FSServiceOrder.cBID>,
                                 Set <FSServiceOrder.cutOffDate, Required <FSServiceOrder.cutOffDate> > >,
                            FSServiceOrder,
                            Where <
                                FSServiceOrder.sOID, Equal <Required <FSServiceOrder.sOID> > > >
                        .Update(tempGraph, newCBID, newCutOffDate, fsServiceOrderRow.SOID);
                    }

                    var appointmentSet = PXSelect <FSAppointment,
                                                   Where <
                                                       FSAppointment.sOID, Equal <Required <FSAppointment.sOID> > > >
                                         .Select(tempGraph, fsServiceOrderRow.SOID);

                    foreach (FSAppointment fsAppointmentRow in appointmentSet)
                    {
                        newCutOffDate = null;

                        if (newBillingCycleID != null)
                        {
                            billingCycleIDAndDate.DocDate = fsAppointmentRow.ExecutionDate;

                            if (cutOffDateCache.TryGetValue(billingCycleIDAndDate, out newCutOffDate) == false)
                            {
                                newCutOffDate = ServiceOrderCore.GetCutOffDate(tempGraph, fsServiceOrderRow.CBID, fsAppointmentRow.ExecutionDate);
                                cutOffDateCache.Add(billingCycleIDAndDate, newCutOffDate);
                            }
                        }

                        if (fsAppointmentRow.CutOffDate != newCutOffDate)
                        {
                            PXUpdate <
                                Set <FSAppointment.cutOffDate, Required <FSAppointment.cutOffDate> >,
                                FSAppointment,
                                Where <
                                    FSAppointment.appointmentID, Equal <Required <FSAppointment.appointmentID> > > >
                            .Update(tempGraph, newCutOffDate, fsAppointmentRow.AppointmentID);
                        }
                    }

                    ts.Complete();
                }
            }
        }
        private void CloneParts(AppointmentEntry sourceAppointmentGraph, AppointmentEntry newAppointmentGraph, FSAppointment newAppointmentRow)
        {
            foreach (FSAppointmentDetPart sourceRow in sourceAppointmentGraph.AppointmentDetParts.Select())
            {
                FSSODet fsSODetRow = ServiceOrderCore.GetSODetFromAppointmentDet(sourceAppointmentGraph, sourceRow);

                if (fsSODetRow != null &&
                    (fsSODetRow.Status == ID.Status_AppointmentDet.CANCELED ||
                     fsSODetRow.Status == ID.Status_AppointmentDet.COMPLETED))
                {
                    continue;
                }

                FSAppointmentDet sumFSAppointmentDetPartBillable =
                    PXSelectJoinGroupBy <FSAppointmentDet,
                                         InnerJoin <FSAppointment, On <FSAppointment.srvOrdType, Equal <FSAppointmentDet.srvOrdType>,
                                                                       And <FSAppointment.refNbr, Equal <FSAppointmentDetPart.refNbr> > > >,
                                         Where <FSAppointmentDet.sODetID, Equal <Required <FSAppointmentDet.sODetID> >,
                                                And <FSAppointment.status, NotEqual <FSAppointment.status.Canceled>,
                                                     And <FSAppointmentDet.status, NotEqual <FSAppointmentDet.status.Canceled> > > >,
                                         Aggregate <GroupBy <FSAppointmentDet.sODetID, Sum <FSAppointmentDet.billableQty> > > >
                    .Select(dummyGraph, sourceRow.SODetID);

                decimal?openQty    = fsSODetRow.BillableQty - sumFSAppointmentDetPartBillable.BillableQty;
                bool?   lineCloned = false;

                FSAppointmentDetPart newRow = PXCache <FSAppointmentDetPart> .CreateCopy(sourceRow);

                if (openQty >= sourceRow.BillableQty)
                {
                    newRow = AppointmentEntry.InsertServicePartLine <FSAppointmentDetPart, FSAppointmentDetPart>(
                        newAppointmentGraph.AppointmentDetParts.Cache,
                        newRow,
                        sourceAppointmentGraph.AppointmentDetParts.Cache,
                        sourceRow,
                        null,
                        sourceRow.SODetID,
                        copyTranDate: false,
                        tranDate: sourceRow.TranDate,
                        SetValuesAfterAssigningSODetID: true,
                        copyingFromQuote: false);
                    lineCloned = true;
                }
                else
                {
                    if (openQty > 0)
                    {
                        decimal?remainingQty = sourceRow.BillableQty - openQty;

                        newRow.Qty             = 0;
                        sourceRow.EstimatedQty = openQty;
                        sourceRow.BillableQty  = openQty;
                        newRow = AppointmentEntry.InsertServicePartLine <FSAppointmentDetPart, FSAppointmentDetPart>(
                            newAppointmentGraph.AppointmentDetParts.Cache,
                            newRow,
                            sourceAppointmentGraph.AppointmentDetParts.Cache,
                            sourceRow,
                            null,
                            sourceRow.SODetID,
                            copyTranDate: false,
                            tranDate: sourceRow.TranDate,
                            SetValuesAfterAssigningSODetID: true,
                            copyingFromQuote: false);
                        lineCloned = true;

                        FSAppointmentDetPart secondNewRow = PXCache <FSAppointmentDetPart> .CreateCopy(sourceRow);

                        secondNewRow.Qty       = 0;
                        sourceRow.EstimatedQty = remainingQty;
                        sourceRow.BillableQty  = remainingQty;
                        secondNewRow.SODetID   = null;
                        secondNewRow           = AppointmentEntry.InsertServicePartLine <FSAppointmentDetPart, FSAppointmentDetPart>(
                            newAppointmentGraph.AppointmentDetParts.Cache,
                            secondNewRow,
                            sourceAppointmentGraph.AppointmentDetParts.Cache,
                            sourceRow,
                            null,
                            null,
                            copyTranDate: false,
                            tranDate: sourceRow.TranDate,
                            SetValuesAfterAssigningSODetID: false,
                            copyingFromQuote: false);
                    }
                    else
                    {
                        newRow.SODetID = null;
                        newRow.Qty     = 0;
                        newRow         = AppointmentEntry.InsertServicePartLine <FSAppointmentDetPart, FSAppointmentDetPart>(
                            newAppointmentGraph.AppointmentDetParts.Cache,
                            newRow,
                            sourceAppointmentGraph.AppointmentDetParts.Cache,
                            sourceRow,
                            null,
                            null,
                            copyTranDate: false,
                            tranDate: sourceRow.TranDate,
                            SetValuesAfterAssigningSODetID: false,
                            copyingFromQuote: false);
                    }
                }


                if (lineCloned == true)
                {
                    PXNoteAttribute.CopyNoteAndFiles(
                        sourceAppointmentGraph.AppointmentDetParts.Cache,
                        sourceRow,
                        newAppointmentGraph.AppointmentDetParts.Cache,
                        newRow,
                        copyNotes: true,
                        copyFiles: false);

                    AppointmentCore.UpdateAppointmentsInfoInServiceOrder(newAppointmentGraph.AppointmentDetParts.Cache, newRow, ServiceOrderRelated);
                }
            }
        }
        protected virtual void CloneAppointment(AppointmentEntry graphOriginalAppointment, AppointmentEntry graphNewAppointment)
        {
            if (AppointmentSelected.Current == null)
            {
                return;
            }

            graphNewAppointment.Clear(PXClearOption.ClearAll);
            graphNewAppointment.clearLocalServiceOrder();

            FSAppointment fsAppointmentRow = PXCache <FSAppointment> .CreateCopy(AppointmentSelected.Current);

            //Clear key and special fields
            fsAppointmentRow.RefNbr                     = null;
            fsAppointmentRow.AppointmentID              = null;
            fsAppointmentRow.NoteID                     = null;
            fsAppointmentRow.CuryInfoID                 = null;
            fsAppointmentRow.AgreementSignature         = false;
            fsAppointmentRow.FullNameSignature          = null;
            fsAppointmentRow.customerSignaturePath      = null;
            fsAppointmentRow.BillServiceContractID      = null;
            fsAppointmentRow.HandleManuallyActualTime   = null;
            fsAppointmentRow.HandleManuallyScheduleTime = null;

            fsAppointmentRow.OriginalAppointmentID = AppointmentSelected.Current.AppointmentID;

            fsAppointmentRow.ScheduledDateTimeBegin = AppointmentCore.GetDateTimeEnd(
                Filter.Current.ScheduledDate, Filter.Current.ScheduledStartTime);

            fsAppointmentRow.ScheduledDateTimeEnd =
                AppointmentCore.GetDateTimeEnd(
                    Filter.Current.ScheduledDate, Filter.Current.ScheduledEndTime);

            fsAppointmentRow.ExecutionDate = fsAppointmentRow.ScheduledDateTimeBegin.Value.Date;

            fsAppointmentRow.CutOffDate
                = ServiceOrderCore.GetCutOffDate(graphNewAppointment, graphOriginalAppointment.ServiceOrderRelated.Current.CBID, fsAppointmentRow.ExecutionDate);

            fsAppointmentRow.Status = ID.Status_Appointment.MANUAL_SCHEDULED;
            fsAppointmentRow.Hold   = false;

            fsAppointmentRow.AdditionalCommentsCustomer = null;
            fsAppointmentRow.AdditionalCommentsStaff    = null;

            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            //Clean total fields
            fsAppointmentRow.EstimatedDurationTotal = 0;
            fsAppointmentRow.ActualDurationTotal    = 0;

            fsAppointmentRow.CuryEstimatedLineTotal = 0;
            fsAppointmentRow.CuryLineTotal          = 0;
            fsAppointmentRow.CuryBillableLineTotal  = 0;
            fsAppointmentRow.CuryCostTotal          = 0;

            fsAppointmentRow.EstimatedLineTotal = 0;
            fsAppointmentRow.LineTotal          = 0;
            fsAppointmentRow.BillableLineTotal  = 0;
            fsAppointmentRow.CostTotal          = 0;
            //----------------------------------------------------------------------

            graphNewAppointment.IsCloningAppointment = true;
            fsAppointmentRow = graphNewAppointment.AppointmentRecords.Insert(fsAppointmentRow);

            graphNewAppointment.AttributeListRecords.Current = graphNewAppointment.AttributeListRecords.Select();
            graphNewAppointment.Answers.Current = graphNewAppointment.Answers.Select();
            graphNewAppointment.Answers.CopyAllAttributes(graphNewAppointment.AppointmentRecords.Current, AppointmentSelected.Current);

            PXNoteAttribute.CopyNoteAndFiles(
                AppointmentSelected.Cache,
                AppointmentSelected.Current,
                graphNewAppointment.AppointmentSelected.Cache,
                fsAppointmentRow,
                copyNotes: true,
                copyFiles: false);


            this.CloneParts(graphOriginalAppointment, graphNewAppointment, fsAppointmentRow);
            this.CloneServices(graphOriginalAppointment, graphNewAppointment, fsAppointmentRow);
            this.CloneEmployees(graphOriginalAppointment, graphNewAppointment, fsAppointmentRow);
            this.CloneAttendees(graphOriginalAppointment, graphNewAppointment, fsAppointmentRow);
            this.CloneResources(graphOriginalAppointment, graphNewAppointment, fsAppointmentRow);

            if (graphNewAppointment.AppointmentRecords.Current.ScheduledDateTimeEnd != Filter.Current.ScheduledEndTime)
            {
                graphNewAppointment.AppointmentRecords.Cache.SetValueExt <FSAppointment.handleManuallyScheduleTime>(fsAppointmentRow, true);
                graphNewAppointment.AppointmentRecords.Cache.SetValueExt <FSAppointment.scheduledDateTimeEnd>(fsAppointmentRow, Filter.Current.ScheduledEndTime);
            }

            graphNewAppointment.AppointmentRecords.Cache.SetDefaultExt <FSAppointment.billContractPeriodID>(fsAppointmentRow);
            graphNewAppointment.Save.Press();
        }
예제 #14
0
        public virtual FSContractPostDoc CreateInvoiceByContract(PXGraph graphProcess, DateTime?invoiceDate, string invoiceFinPeriodID, FSContractPostBatch fsContractPostBatchRow, FSServiceContract fsServiceContractRow, FSContractPeriod fsContractPeriodRow, List <ContractInvoiceLine> docLines)
        {
            if (docLines.Count == 0)
            {
                return(null);
            }

            FSSetup fsSetupRow = ServiceManagementSetup.GetServiceManagementSetup(graphProcess);

            ARInvoice arInvoiceRow = new ARInvoice();

            arInvoiceRow.DocType = ARInvoiceType.Invoice;
            AutoNumberHelper.CheckAutoNumbering(Base, Base.ARSetup.SelectSingle().InvoiceNumberingID);

            arInvoiceRow.DocDate     = invoiceDate;
            arInvoiceRow.FinPeriodID = invoiceFinPeriodID;
            arInvoiceRow.Hold        = true;
            arInvoiceRow             = Base.Document.Insert(arInvoiceRow);

            Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.customerID>(arInvoiceRow, fsServiceContractRow.BillCustomerID);
            Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.customerLocationID>(arInvoiceRow, fsServiceContractRow.BillLocationID);
            Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.branchID>(arInvoiceRow, fsServiceContractRow.BranchID);
            Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.docDesc>(arInvoiceRow, (PXMessages.LocalizeFormatNoPrefix(TX.Messages.CONTRACT_WITH_STANDARDIZED_BILLING, fsServiceContractRow.RefNbr, (string.IsNullOrEmpty(fsServiceContractRow.DocDesc) ? string.Empty : fsServiceContractRow.DocDesc))));

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

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

            Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.projectID>(arInvoiceRow, fsServiceContractRow.ProjectID);

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

            foreach (ContractInvoiceLine docLine in docLines)
            {
                arTranRow = new ARTran();
                arTranRow = Base.Transactions.Insert(arTranRow);

                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.inventoryID>(arTranRow, docLine.InventoryID);
                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.uOM>(arTranRow, docLine.UOM);
                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.salesPersonID>(arTranRow, docLine.SalesPersonID);

                arTranRow = Base.Transactions.Update(arTranRow);

                if (docLine.AcctID != null)
                {
                    acctID = docLine.AcctID;
                }
                else
                {
                    acctID = (int?)ServiceOrderCore.Get_INItemAcctID_DefaultValue(graphProcess,
                                                                                  fsSetupRow.ContractSalesAcctSource,
                                                                                  docLine.InventoryID,
                                                                                  fsServiceContractRow);
                }

                Base.Transactions.Cache.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,
                                                        fsSetupRow,
                                                        arTranRow.BranchID,
                                                        arTranRow.InventoryID,
                                                        arInvoiceRow.CustomerLocationID,
                                                        fsServiceContractRow.BranchLocationID);
                }

                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.qty>(arTranRow, docLine.Qty);

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

                if (docLine.ServiceContractID != null &&
                    docLine.ContractRelated == false &&
                    (docLine.SODetID != null || docLine.AppDetID != null))
                {
                    Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.curyExtPrice>(arTranRow, docLine.CuryBillableExtPrice);
                    Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.discPct>(arTranRow, docLine.DiscPct);
                }

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

                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.taskID>(arTranRow, docLine.ProjectTaskID);
                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.costCodeID>(arTranRow, docLine.CostCodeID);

                arTranRow = Base.Transactions.Update(arTranRow);

                Base.Transactions.Cache.SetValueExtIfDifferent <ARTran.commissionable>(arTranRow, docLine.Commissionable ?? false);

                fsxARTranRow = Base.Transactions.Cache.GetExtension <FSxARTran>(arTranRow);

                fsxARTranRow.Source            = ID.DocumentSource.INVOICE_FROM_SERVICECONTRACT;
                fsxARTranRow.ServiceContractID = fsServiceContractRow.ServiceContractID;
                fsxARTranRow.ContractPeriodID  = fsContractPeriodRow.ContractPeriodID;

                fsxARTranRow.BillCustomerID     = fsServiceContractRow.CustomerID;
                fsxARTranRow.CustomerLocationID = fsServiceContractRow.CustomerLocationID;

                arTranRow = Base.Transactions.Update(arTranRow);
            }

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

            Base.Document.Cache.SetValueExtIfDifferent <ARInvoice.hold>(arInvoiceRow, false);

            Exception newException = null;

            try
            {
                Base.Save.Press();
            }
            catch (Exception e)
            {
                List <ErrorInfo> errorList = this.GetErrorInfo();
                newException = InvoicingFunctions.GetErrorInfoInLines(errorList, e);
            }

            if (newException != null)
            {
                throw newException;
            }

            arInvoiceRow = Base.Document.Current;

            FSContractPostDoc fsContractCreatedDocRow = new FSContractPostDoc()
            {
                ContractPeriodID    = fsContractPeriodRow.ContractPeriodID,
                ContractPostBatchID = fsContractPostBatchRow.ContractPostBatchID,
                PostDocType         = arInvoiceRow.DocType,
                PostedTO            = ID.Batch_PostTo.AR,
                PostRefNbr          = arInvoiceRow.RefNbr,
                ServiceContractID   = fsServiceContractRow.ServiceContractID
            };

            return(fsContractCreatedDocRow);
        }
예제 #15
0
        public virtual void CloneParts(AppointmentEntry gOriginalAppt, AppointmentEntry gNewAppt, FSAppointment newAppointmentRow, FSAppointmentDet sourceRow)
        {
            if (sourceRow == null || sourceRow.Status == ID.Status_AppointmentDet.CANCELED)
            {
                return;
            }

            FSSODet fsSODetRow = ServiceOrderCore.GetSODetFromAppointmentDet(gOriginalAppt, sourceRow);

            if (fsSODetRow == null)
            {
                return;
            }

            if (ReadingGraph == null)
            {
                ReadingGraph = new PXGraph();
            }

            FSAppointmentDet takenQtyByAppointments =
                PXSelectJoinGroupBy <FSAppointmentDet,
                                     InnerJoin <FSAppointment,
                                                On <
                                                    FSAppointment.srvOrdType, Equal <FSAppointmentDet.srvOrdType>,
                                                    And <FSAppointment.refNbr, Equal <FSAppointmentDet.refNbr> > > >,
                                     Where <
                                         FSAppointmentDet.sODetID, Equal <Required <FSAppointmentDet.sODetID> >,
                                         And <FSAppointment.status, NotEqual <FSAppointment.status.Canceled>,
                                              And <FSAppointmentDet.isCanceledNotPerformed, NotEqual <True> > > >,
                                     Aggregate <
                                         GroupBy <FSAppointmentDet.sODetID, Sum <FSAppointmentDet.billableQty> > > >
                .Select(ReadingGraph, sourceRow.SODetID);

            decimal?openQty    = fsSODetRow.BillableQty - (takenQtyByAppointments?.BillableQty ?? 0m);
            decimal?qtyFromBal = openQty >= sourceRow.BillableQty ? sourceRow.BillableQty :
                                 openQty > 0m ? openQty : 0m;
            decimal?         missingQty = sourceRow.BillableQty - qtyFromBal;
            FSAppointmentDet noteLine   = null;

            // Creates a copy to not edit the original row.
            FSAppointmentDet sourceRowCopy = PXCache <FSAppointmentDet> .CreateCopy(sourceRow);

            ClearSourceLineBeforeCopy(sourceRowCopy);

            if (qtyFromBal > 0)
            {
                sourceRowCopy.EstimatedQty = qtyFromBal;
                sourceRowCopy.BillableQty  = qtyFromBal;

                FSAppointmentDet newRow = new FSAppointmentDet();
                newRow = AppointmentEntry.InsertDetailLine <FSAppointmentDet, FSAppointmentDet>(
                    gNewAppt.AppointmentDetails.Cache,
                    newRow,
                    gOriginalAppt.AppointmentDetails.Cache,
                    sourceRowCopy,
                    null,
                    sourceRowCopy.SODetID,
                    copyTranDate: false,
                    tranDate: sourceRowCopy.TranDate,
                    SetValuesAfterAssigningSODetID: true,
                    copyingFromQuote: false);
                noteLine = newRow;
            }

            if (missingQty > 0)
            {
                sourceRowCopy.EstimatedQty = missingQty;
                sourceRowCopy.BillableQty  = missingQty;

                FSAppointmentDet newRow = new FSAppointmentDet();
                newRow = AppointmentEntry.InsertDetailLine <FSAppointmentDet, FSAppointmentDet>(
                    gNewAppt.AppointmentDetails.Cache,
                    newRow,
                    gOriginalAppt.AppointmentDetails.Cache,
                    sourceRowCopy,
                    null,
                    null,
                    copyTranDate: false,
                    tranDate: sourceRowCopy.TranDate,
                    SetValuesAfterAssigningSODetID: true,
                    copyingFromQuote: false);

                if (noteLine == null)
                {
                    noteLine = newRow;
                }
            }

            if (noteLine != null)
            {
                PXNoteAttribute.CopyNoteAndFiles(gOriginalAppt.AppointmentDetails.Cache,
                                                 sourceRow,
                                                 gNewAppt.AppointmentDetails.Cache,
                                                 noteLine,
                                                 copyNotes: true,
                                                 copyFiles: false);
            }
        }
예제 #16
0
        public virtual void CloneAppointment(AppointmentEntry graphOriginalAppointment, AppointmentEntry graphNewAppointment)
        {
            if (AppointmentSelected.Current == null)
            {
                return;
            }

            graphNewAppointment.Clear(PXClearOption.ClearAll);
            graphNewAppointment.clearLocalServiceOrder();

            FSAppointment fsAppointmentRow = PXCache <FSAppointment> .CreateCopy(AppointmentSelected.Current);

            var itemLineRefNbr = new Dictionary <string, string>();

            //Clear key and special fields
            fsAppointmentRow.RefNbr                     = null;
            fsAppointmentRow.AppointmentID              = null;
            fsAppointmentRow.NoteID                     = null;
            fsAppointmentRow.CuryInfoID                 = null;
            fsAppointmentRow.FullNameSignature          = null;
            fsAppointmentRow.customerSignaturePath      = null;
            fsAppointmentRow.BillServiceContractID      = null;
            fsAppointmentRow.HandleManuallyActualTime   = null;
            fsAppointmentRow.HandleManuallyScheduleTime = null;
            fsAppointmentRow.LogLineCntr                = 0;
            fsAppointmentRow.FinPeriodID                = null;
            fsAppointmentRow.PostingStatusAPARSO        = ID.Status_Posting.PENDING_TO_POST;
            fsAppointmentRow.PendingAPARSOPost          = true;

            fsAppointmentRow.OriginalAppointmentID = AppointmentSelected.Current.AppointmentID;

            fsAppointmentRow.ScheduledDateTimeBegin = AppointmentCore.GetDateTimeEnd(
                Filter.Current.ScheduledDate, Filter.Current.ScheduledStartTime);

            DateTime?scheduledDateTimeEnd = null;

            fsAppointmentRow.ScheduledDateTimeEnd =
                scheduledDateTimeEnd =
                    AppointmentCore.GetDateTimeEnd(
                        Filter.Current.ScheduledDate, Filter.Current.ScheduledEndTime);

            fsAppointmentRow.ExecutionDate = fsAppointmentRow.ScheduledDateTimeBegin.Value.Date;

            fsAppointmentRow.CutOffDate
                = ServiceOrderCore.GetCutOffDate(graphNewAppointment, graphOriginalAppointment.ServiceOrderRelated.Current.CBID, fsAppointmentRow.ExecutionDate, fsAppointmentRow.SrvOrdType);

            fsAppointmentRow.Status = ID.Status_Appointment.MANUAL_SCHEDULED;
            fsAppointmentRow.Hold   = false;

            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            //Clean total fields
            fsAppointmentRow.EstimatedDurationTotal = 0;
            fsAppointmentRow.ActualDurationTotal    = 0;

            fsAppointmentRow.CuryEstimatedLineTotal = 0;
            fsAppointmentRow.CuryLineTotal          = 0;
            fsAppointmentRow.CuryBillableLineTotal  = 0;
            fsAppointmentRow.CuryCostTotal          = 0;

            fsAppointmentRow.EstimatedLineTotal = 0;
            fsAppointmentRow.LineTotal          = 0;
            fsAppointmentRow.BillableLineTotal  = 0;
            fsAppointmentRow.CostTotal          = 0;
            fsAppointmentRow.LineCntr           = 0;
            //----------------------------------------------------------------------

            graphNewAppointment.IsCloningAppointment = true;
            fsAppointmentRow = graphNewAppointment.AppointmentRecords.Insert(fsAppointmentRow);

            graphNewAppointment.Answers.Current = graphNewAppointment.Answers.Select();
            graphNewAppointment.Answers.CopyAllAttributes(graphNewAppointment.AppointmentRecords.Current, AppointmentSelected.Current);

            PXNoteAttribute.CopyNoteAndFiles(AppointmentSelected.Cache,
                                             AppointmentSelected.Current,
                                             graphNewAppointment.AppointmentSelected.Cache,
                                             fsAppointmentRow,
                                             copyNotes: true,
                                             copyFiles: false);


            foreach (FSAppointmentDet sourceRow in graphOriginalAppointment.AppointmentDetails.Select())
            {
                if (sourceRow.IsInventoryItem == true)
                {
                    this.CloneParts(graphOriginalAppointment, graphNewAppointment, fsAppointmentRow, sourceRow);
                }
                else if (sourceRow.IsInventoryItem == false && sourceRow.IsPickupDelivery == false)
                {
                    this.CloneServices(graphOriginalAppointment, graphNewAppointment, fsAppointmentRow, sourceRow, itemLineRefNbr);
                }
            }

            this.CloneEmployees(graphOriginalAppointment, graphNewAppointment, fsAppointmentRow, itemLineRefNbr);
            this.CloneResources(graphOriginalAppointment, graphNewAppointment, fsAppointmentRow);

            if (graphNewAppointment.AppointmentRecords.Current.ScheduledDateTimeEnd != scheduledDateTimeEnd)
            {
                graphNewAppointment.AppointmentRecords.Cache.SetValueExt <FSAppointment.handleManuallyScheduleTime>(fsAppointmentRow, true);
                graphNewAppointment.AppointmentRecords.Cache.SetValueExt <FSAppointment.scheduledDateTimeEnd>(fsAppointmentRow, scheduledDateTimeEnd);
            }

            graphNewAppointment.AppointmentRecords.Cache.SetDefaultExt <FSAppointment.billContractPeriodID>(fsAppointmentRow);
            graphNewAppointment.Save.Press();
        }