public UpdateInventoryPost()
        {
            graphPostInfoEntry        = PXGraph.CreateInstance <PostInfoEntry>();
            graphUpdatePostBatchMaint = PXGraph.CreateInstance <InventoryPostBatchMaint>();

            UpdateInventoryFilter filter = Filter.Current;

            Appointments.SetProcessDelegate(
                delegate(List <FSAppointmentDet> fsAppointmentInventoryItem)
            {
                CreateDocuments(fsAppointmentInventoryItem, filter);
            });
        }
        protected virtual void UpdateInventoryFilter_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
        {
            if (e.Row == null)
            {
                return;
            }

            UpdateInventoryFilter createInvoiceFilterRow = (UpdateInventoryFilter)e.Row;

            string errorMessage         = PXUIFieldAttribute.GetErrorOnly <UpdateInventoryFilter.finPeriodID>(cache, createInvoiceFilterRow);
            bool   enableProcessButtons = string.IsNullOrEmpty(errorMessage) == true;

            Appointments.SetProcessAllEnabled(enableProcessButtons);
            Appointments.SetProcessEnabled(enableProcessButtons);
        }
        /// <summary>
        /// Creates one or more documents in Inventory depending of the number of FSAppointmentInventoryItem in the list [fsAppointmentInventoryItemRows].
        /// </summary>
        public virtual int CreateDocumentsInIN(FSPostBatch fsPostBatchRow, List <FSAppointmentDet> fsAppointmentInventoryItemRows, List <SharedClasses.AppointmentInventoryItemGroup> listGroupToUpdateInInventory, UpdateInventoryFilter filter)
        {
            INReceiptEntry graphINReceiptEntry = PXGraph.CreateInstance <INReceiptEntry>();
            INIssueEntry   graphINIssueEntry   = PXGraph.CreateInstance <INIssueEntry>();
            int            linesPosted         = 0;

            foreach (SharedClasses.AppointmentInventoryItemGroup groupToUpdateInInventory in listGroupToUpdateInInventory)
            {
                string iNRefNbr  = null;
                string iNDocType = null;

                foreach (SharedClasses.AppointmentInventoryItemInfo appointmentInventoryItemInfoRow in groupToUpdateInInventory.AppointmentInventoryItems)
                {
                    using (var ts_GroupToUpdateInInventory = new PXTransactionScope())
                    {
                        try
                        {
                            if (groupToUpdateInInventory.ServiceType == ID.Service_Action_Type.PICKED_UP_ITEMS)
                            {
                                // create receipt
                                CreateDocumentReceipt(graphINReceiptEntry,
                                                      appointmentInventoryItemInfoRow,
                                                      fsAppointmentInventoryItemRows[appointmentInventoryItemInfoRow.Index],
                                                      filter.DocumentDate,
                                                      filter.FinPeriodID,
                                                      fsPostBatchRow,
                                                      ref iNRefNbr,
                                                      ref iNDocType);
                            }
                            else if (groupToUpdateInInventory.ServiceType == ID.Service_Action_Type.DELIVERED_ITEMS)
                            {
                                // create issue
                                CreateDocumentIssue(graphINIssueEntry,
                                                    appointmentInventoryItemInfoRow,
                                                    fsAppointmentInventoryItemRows[appointmentInventoryItemInfoRow.Index],
                                                    filter.DocumentDate,
                                                    filter.FinPeriodID,
                                                    fsPostBatchRow,
                                                    ref iNRefNbr,
                                                    ref iNDocType);
                            }
                            else if (groupToUpdateInInventory.ServiceType == ID.Service_Action_Type.NO_ITEMS_RELATED)
                            {
                                PXProcessing <FSAppointmentDet> .SetError(appointmentInventoryItemInfoRow.Index, TX.Error.APPOINTMENT_ITEM_CANNOT_BE_POSTED_TO_IN_NO_ITEMS_RELATED);
                            }

                            PXProcessing <FSAppointmentDet> .SetInfo(appointmentInventoryItemInfoRow.Index, TX.Messages.RECORD_PROCESSED_SUCCESSFULLY);

                            linesPosted++;
                            ts_GroupToUpdateInInventory.Complete();
                        }
                        catch (Exception e)
                        {
                            Exception latestException = ExceptionHelper.GetExceptionWithContextMessage(PXMessages.Localize(TX.Messages.COULD_NOT_PROCESS_RECORD), e);
                            PXProcessing <FSAppointmentDet> .SetError(appointmentInventoryItemInfoRow.Index, latestException);

                            ts_GroupToUpdateInInventory.Dispose();
                            graphINReceiptEntry.Actions.PressCancel();
                        }
                    }
                }
            }

            return(linesPosted);
        }
        /// <summary>
        /// Defines where the AppointmentInventoryItems are going to be posted, depending of the ServiceType (Pickup or Delivery).
        /// </summary>
        /// <param name="fsAppointmentInventoryItemRows"> Items to be posted (Original List in the screen).</param>
        /// <param name="listGroupToInvoice"> Items to be posted (Groups to be posted after grouping rules).</param>
        /// <param name="filter"> Header of the screen (Filters).</param>
        public virtual void CreateDocumentByGroup(List <FSAppointmentDet> fsAppointmentInventoryItemRows, List <SharedClasses.AppointmentInventoryItemGroup> listGroupToInvoice, UpdateInventoryFilter filter)
        {
            FSPostBatch fsPostBatchRow  = CreateFSPostBatch(fsAppointmentInventoryItemRows.Count, ID.Batch_PostTo.IN, filter.CutOffDate, filter.FinPeriodID, filter.DocumentDate);
            int         documentsPosted = 0;

            documentsPosted = CreateDocumentsInIN(fsPostBatchRow, fsAppointmentInventoryItemRows, listGroupToInvoice, filter);

            if (documentsPosted > 0)
            {
                fsPostBatchRow.QtyDoc = documentsPosted;
                UpdateFSPostBatch(fsPostBatchRow);
            }
            else
            {
                DeleteFSPostBatch(fsPostBatchRow);
            }
        }
        /// <summary>
        /// Group the Appointment List [fsAppointmentRows] to determine how to post them.
        /// </summary>
        public virtual void CreateDocuments(List <FSAppointmentDet> fsAppointmentInventoryItemRows, UpdateInventoryFilter filter)
        {
            List <SharedClasses.AppointmentInventoryItemInfo> appointmentList = fsAppointmentInventoryItemRows.Select((n, i) => GetAppointmentInventoryItemInfo(n.AppointmentID, n.AppDetID, i)).ToList();

            List <SharedClasses.AppointmentInventoryItemGroup> listGroupToUpdateInIN = null;

            if (RouteSetupRecord.Current != null)
            {
                if (RouteSetupRecord.Current.GroupINDocumentsByPostingProcess == true)
                {
                    listGroupToUpdateInIN =
                        appointmentList.GroupBy(
                            u => new { u.ServiceType },
                            (key, group) => new
                    {
                        Key   = key,
                        Group = (List <SharedClasses.AppointmentInventoryItemInfo>)group.ToList()
                    })
                        .Select(List => new SharedClasses.AppointmentInventoryItemGroup((int)(List.Key.ServiceType == ID.Service_Action_Type.PICKED_UP_ITEMS ? 1 : 0), List.Key.ServiceType, List.Group))
                        .OrderBy(List => List.Pivot)
                        .ToList();
                }
                else
                {
                    listGroupToUpdateInIN =
                        appointmentList.GroupBy(
                            u => new { u.AppointmentID, u.ServiceType },
                            (key, group) => new
                    {
                        Key   = key,
                        Group = (List <SharedClasses.AppointmentInventoryItemInfo>)group.ToList()
                    })
                        .Select(List => new SharedClasses.AppointmentInventoryItemGroup((int)List.Key.AppointmentID, List.Key.ServiceType, List.Group))
                        .OrderBy(List => List.Pivot)
                        .ToList();
                }

                if (listGroupToUpdateInIN != null && listGroupToUpdateInIN.Count > 0)
                {
                    CreateDocumentByGroup(fsAppointmentInventoryItemRows, listGroupToUpdateInIN, filter);
                }
            }
        }