protected virtual void SOImportFilter_RowSelected(PXCache sender, PXRowSelectedEventArgs e)
        {
            SOImportFilter row = e.Row as SOImportFilter;

            if (row != null)
            {
                PrepareRecords.SetEnabled(row.TODate != null && row.LastSyncDate != null && (!string.IsNullOrEmpty(row.IntegrationID) || row.ProcessAllTypes == true));
                PXUIFieldAttribute.SetEnabled <SOImportFilter.processAllTypes>(Filter.Cache, null, string.IsNullOrEmpty(row.IntegrationID));
                PXUIFieldAttribute.SetEnabled <SOImportFilter.integrationID>(Filter.Cache, null, (row.ProcessAllTypes != null && row.ProcessAllTypes != true));
            }
        }
        public SOImportProcess()
        {
            ImportOrderList.SetProcessCaption(SOConstants.btnImport);
            ImportOrderList.SetProcessAllCaption(SOConstants.btnImportAll);
            SOImportFilter currentFilter = this.Filter.Current;

            ImportOrderList.SetProcessDelegate(
                delegate(List <SOProcessOrder> list)
            {
                ImportRecords(list, currentFilter);
            });
        }
        protected virtual IEnumerable importOrderList()
        {
            SOImportFilter filter = Filter.Current;

            if (filter != null && filter.LastSyncDate != null && filter.TODate != null && (!string.IsNullOrEmpty(filter.IntegrationID) && filter.ProcessAllTypes == false ||
                                                                                           string.IsNullOrEmpty(filter.IntegrationID) && filter.ProcessAllTypes == true))
            {
                PXSelectBase <SOProcessOrder> preparedorder = new PXSelect <SOProcessOrder,
                                                                            Where <SOProcessOrder.prepareStatus, Equal <True>, And <SOProcessOrder.importStatus, Equal <False>,
                                                                                                                                    And <Where2 <Where <SOProcessOrder.amazonOrderDate, GreaterEqual <Current <SOImportFilter.lastSyncDate> >,
                                                                                                                                                        And <SOProcessOrder.amazonOrderDate, Less <Required <SOImportFilter.tODate> > > >,
                                                                                                                                                 Or <Where <SOProcessOrder.amazonOrderLastUpdated, GreaterEqual <Current <SOImportFilter.lastSyncDate> >,
                                                                                                                                                            And <SOProcessOrder.amazonOrderLastUpdated, Less <Required <SOImportFilter.tODate> > > > > > > > > >(this);
                if (!string.IsNullOrEmpty(filter.IntegrationID) && filter.ProcessAllTypes == false)
                {
                    preparedorder.WhereAnd <Where <SOProcessOrder.integrationID, Equal <Current <SOImportFilter.integrationID> > > >();
                }
                return(preparedorder.Select(filter.TODate.Value.AddDays(1).AddTicks(-1), filter.TODate.Value.AddDays(1).AddTicks(-1)));
            }
            return(null);
        }
        private void PrepareAllRecords(SOImportProcess graph, List <string> importedRecords, SOImportFilter currentFilter, out SOProcessOrder processrecord, SOAmazonSetup objSOAmazonSetup, int?processID, out int?processedCount, SOPartialMaint logGraph)
        {
            processrecord = new SOProcessOrder();
            InvokeServicesCallResponse objSyncOrderResponse = new InvokeServicesCallResponse();
            List <ListOrderItems>      liListOrderResponse  = new List <ListOrderItems>();

            objOrdersParameters.objSOAmazonSetup     = objSOAmazonSetup;
            objOrdersParameters.objSOImportFilter    = currentFilter;
            objOrdersParameters.processrecord        = processrecord;
            objOrdersParameters.objSyncOrderResponse = objSyncOrderResponse;
            objOrdersParameters.processID            = processID;
            objOrdersParameters.importedRecords      = importedRecords;
            objOrdersParameters.liListOrderResponse  = liListOrderResponse;
            objOrdersParameters.objSOPartialMaint    = logGraph;
            ListOrders(graph, objOrdersParameters);
            processedCount = Convert.ToInt32(graph.ImportOrderList.Cache.Inserted.Count());
        }
        protected virtual void prepareRecords()
        {
            List <string>  importedRecords = new List <string>();
            SOImportFilter currentFilter   = Filter.Current;

            PXLongOperation.StartOperation(this, delegate()
            {
                SOOrderEntry orderEntry = PXGraph.CreateInstance <SOOrderEntry>();
                SOImportProcess graph   = PXGraph.CreateInstance <SOImportProcess>();
                SOPartialMaint logGraph = PXGraph.CreateInstance <SOPartialMaint>();
                if (currentFilter != null)
                {
                    if ((string.IsNullOrEmpty(currentFilter.IntegrationID) && currentFilter.ProcessAllTypes == false) || (!string.IsNullOrEmpty(currentFilter.IntegrationID) && currentFilter.ProcessAllTypes == true))
                    {
                        throw new PXException(SOMessages.validationIntegrationIdandprocessall);
                    }
                    if (currentFilter.TODate.Value.Date > graph.Accessinfo.BusinessDate.Value.Date)
                    {
                        throw new PXException(SOMessages.validationTodateandBusinessDate);
                    }
                    if (currentFilter.LastSyncDate > currentFilter.TODate)
                    {
                        throw new PXException(SOMessages.validationFromandTodate);
                    }
                    try
                    {
                        orderEntry.Clear();
                        SOSetupAmazonExt objSOOSetupext = orderEntry.sosetup.Current.GetExtension <SOSetupAmazonExt>();
                        if (SOHelper.IsSOPreferencesDetailsExist(orderEntry, objSOOSetupext))
                        {
                            SOAmazonSetup objSOAmazonSetupDetails = null;
                            objOrdersParameters          = new OrdersParameters();
                            SOProcessOrder processrecord = null;
                            int?processedCount           = 0;
                            bool isValidConfiguration    = SOHelper.MarketplaceConfigurations(graph, currentFilter.IntegrationID, out objSOAmazonSetupDetails);

                            // To Get the Max date & time if the same date is processed again
                            //Dhiren-4/11/2019 - We need to revisit date range Delta logic.
                            //SOProcessOrder objProcessOrder = new PXSelect<SOProcessOrder, Where<SOProcessOrder.integrationID, Equal<Required<SOProcessOrder.integrationID>>,
                            //                                                             And<SOProcessOrder.amazonOrderDate, Between<Required<SOProcessOrder.amazonOrderDate>, Required<SOProcessOrder.amazonOrderDate>>>>,
                            //                                                             OrderBy<Desc<SOProcessOrder.amazonOrderDate>>>(graph).SelectSingle(currentFilter.IntegrationID, currentFilter.LastSyncDate, currentFilter.TODate);

                            //currentFilter.FromDate = objProcessOrder != null && objProcessOrder.AmazonOrderDate.HasValue ? objProcessOrder.AmazonOrderDate : currentFilter.LastSyncDate;
                            currentFilter.FromDate = currentFilter.LastSyncDate;
                            Filter.Cache.Update(currentFilter);
                            if (currentFilter.IntegrationID != null && currentFilter.ProcessAllTypes == false)
                            {
                                if (isValidConfiguration)
                                {
                                    int?processId = SOHelper.GetProcessID(graph);
                                    PrepareAllRecords(graph, importedRecords, currentFilter, out processrecord, objSOAmazonSetupDetails, processId, out processedCount, logGraph);
                                    if (processedCount > 0)
                                    {
                                        LogProcessCount(processedCount, currentFilter.IntegrationID, logGraph);
                                    }
                                    graph.Actions.PressSave();
                                }
                                else
                                {
                                    throw new PXException(SOMessages.apidetailsMissing);
                                }
                            }
                            else if (string.IsNullOrEmpty(currentFilter.IntegrationID) && currentFilter.ProcessAllTypes == true)
                            {
                                ImportOrderList.Cache.Clear();
                                int?processId = SOHelper.GetProcessID(graph);
                                foreach (SOAmazonSetup objSOAmazonSetup in PXSelectReadonly <SOAmazonSetup, Where <SOAmazonSetup.status, Equal <True> > > .Select(graph))
                                {
                                    try
                                    {
                                        PrepareAllRecords(graph, importedRecords, currentFilter, out processrecord, objSOAmazonSetup, processId, out processedCount, logGraph);
                                        if (processedCount > 0)
                                        {
                                            LogProcessCount(processedCount, objSOAmazonSetup.IntegrationID, logGraph);
                                        }
                                        processId++;
                                        graph.Actions.PressSave();
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new PXException(!string.IsNullOrEmpty(ex.Message) ? ex.Message :
                                                              ex.InnerException != null && ex.InnerException.InnerException != null ? ex.InnerException.InnerException.Message
                                                    : SOConstants.exceptionIsEmpty);
                                    }
                                }
                            }
                        }
                        else
                        {
                            throw new PXException(SOMessages.configMissing);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new PXException(!string.IsNullOrEmpty(ex.Message) ? ex.Message :
                                              ex.InnerException != null && ex.InnerException.InnerException != null ? ex.InnerException.InnerException.Message
                            : SOConstants.exceptionIsEmpty);
                    }
                }
            });
        }
        public virtual void ImportAmazonRecords(SOImportProcess graph, List <SOProcessOrder> list, SOImportFilter currentFilter)
        {
            if (list.Count < 0)
            {
                return;
            }
            List <Order>          amwOrder        = null;
            List <OrderItem>      amwLineItems    = null;
            string                amwOrderID      = string.Empty;
            List <SOAmazonSetup>  liSOAmazonSetup = new List <SOAmazonSetup>();
            List <SOFieldMapping> liUsrMapping    = new List <SOFieldMapping>();
            SOAmazonSetup         objamwSetup     = null;
            SOOrderProcessLog     objProcessLog   = null;
            SOPartialMaint        logGraph        = PXGraph.CreateInstance <SOPartialMaint>();

            if (currentFilter.ProcessAllTypes == true)
            {
                foreach (SOAmazonSetup objSetup in PXSelect <SOAmazonSetup, Where <SOAmazonSetup.status, Equal <True> > > .Select(graph))
                {
                    liSOAmazonSetup.Add(objSetup);
                }
            }
            else if (SOHelper.MarketplaceConfigurations(graph, currentFilter.IntegrationID, out objamwSetup))
            {
                liSOAmazonSetup.Add(objamwSetup);
            }
            if (liSOAmazonSetup.Count > 0)
            {
                ARPaymentEntry docgraph   = PXGraph.CreateInstance <ARPaymentEntry>();
                SOOrderEntry   orderEntry = PXGraph.CreateInstance <SOOrderEntry>();
                orderEntry.RowUpdated.AddHandler <SOOrder>((sender, e) =>
                {
                    if (!sender.ObjectsEqual <SOOrder.orderDate>(e.Row, e.OldRow))
                    {
                        SOOrder order   = (SOOrder)e.Row;
                        order.OrderDate = order.OrderDate.HasValue ? order.OrderDate.Value.Date : order.OrderDate;
                    }
                });
                InvokeServicesCallResponse   objSyncOrderResponse = null;
                PrepareAndImportOrdersParams objScheduleParams    = null;
                objProcessLog                      = new SOOrderProcessLog();
                objProcessLog.ProcessDate          = PX.Common.PXTimeZoneInfo.Now;
                objProcessLog.TotalRecordstoImport = list.Count;
                objProcessLog.Operation            = SOConstants.btnImport;
                objProcessLog.ImportedRecordsCount = 0;
                objProcessLog.FailedRecordsCount   = 0;

                bool          isErrorOccured = false;
                List <string> liCarriers     = PXSelect <Carrier> .Select(graph).RowCast <Carrier>().Select(c => c.CarrierID).ToList();

                foreach (SOProcessOrder currentRecord in list)
                {
                    try
                    {
                        orderEntry.Clear();
                        logGraph.Clear();
                        docgraph.Clear();
                        objProcessLog.IntegrationID = currentFilter.IntegrationID != null ? currentFilter.IntegrationID : currentRecord.IntegrationID;
                        logGraph.OrderProcessLog.Insert(objProcessLog);
                        logGraph.Actions.PressSave();

                        foreach (SOAmazonSetup objSOAmazonSetup in liSOAmazonSetup.Where(x => x.IntegrationID == currentRecord.IntegrationID))
                        {
                            if (orderEntry.sosetup.Current != null)
                            {
                                if (!SOHelper.CheckOrderExist(graph, currentRecord.AmazonOrderID, currentRecord.IntegrationID))
                                {
                                    amwOrderID = currentRecord.AmazonOrderID;
                                    if (amwLineItems != null && amwLineItems.Count > 0)
                                    {
                                        amwLineItems.Clear();
                                    }
                                    amwOrder = new List <Order>();
                                    amwOrder.Add(SOHelper.SchemaDeserialization(graph, amwOrderID));
                                    objServiceCallParams = new ServiceCallParameters();
                                    objServiceCallParams.objSOAmazonSetup = objSOAmazonSetup;
                                    objServiceCallParams.amwOrderID       = currentRecord.AmazonOrderID;
                                    objServiceCallParams.methodCall       = SOConstants.invokeListOrderItems;
                                    objSyncOrderResponse = new InvokeServicesCallResponse();
                                    objSyncOrderResponse = new SOOrdersServiceCall(clientOrder).InvokeServicesCalls(graph, objServiceCallParams);
                                    amwLineItems         = objSyncOrderResponse != null && objSyncOrderResponse.objListOrderItemsResponse != null &&
                                                           objSyncOrderResponse.objListOrderItemsResponse.ListOrderItemsResult != null &&
                                                           objSyncOrderResponse.objListOrderItemsResponse.ListOrderItemsResult.OrderItems.Count > 0 ?
                                                           objSyncOrderResponse.objListOrderItemsResponse.ListOrderItemsResult.OrderItems : amwLineItems;
                                    objScheduleParams = new PrepareAndImportOrdersParams();
                                    objScheduleParams.objSOPartialMaint       = logGraph;
                                    objScheduleParams.objSOAmazonSetup        = objSOAmazonSetup;
                                    objScheduleParams.objSOOrderEntry         = orderEntry;
                                    objScheduleParams.paymentGraph            = docgraph;
                                    objScheduleParams.objSOProcessOrderRecord = currentRecord;
                                    objScheduleParams.ObjCurrentOrder         = amwOrder[0];
                                    objScheduleParams.objamwLineItems         = amwLineItems;
                                    objScheduleParams.objliUsrMapping         = liUsrMapping;
                                    objScheduleParams.listOfCarriers          = liCarriers;
                                    objScheduleParams.CurrentOrderIndex       = list.IndexOf(currentRecord);
                                    CreateSO.CreateSalesOrderandPayments(objScheduleParams);
                                }
                                else
                                {
                                    isErrorOccured = true;
                                    SOLogService.LogImportStatus(objScheduleParams, true, SOMessages.recordAlreadyImported);
                                    PXProcessing <SOProcessOrder> .SetInfo(list.IndexOf(currentRecord), SOMessages.recordAlreadyImported);
                                }
                            }
                            else
                            {
                                throw new PXException(SOMessages.configMissing);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        isErrorOccured = true;
                        SOLogService.LogImportStatus(objScheduleParams, false, ex.Message);
                        PXProcessing <SOProcessOrder> .SetError(list.IndexOf(currentRecord), ex.Message);
                    }
                }
                if (isErrorOccured)
                {
                    throw new PXException(SOMessages.showErrorMsgOrders);
                }
            }
        }
        public static void ImportRecords(List <SOProcessOrder> list, SOImportFilter currentFilter)
        {
            SOImportProcess graph = PXGraph.CreateInstance <SOImportProcess>();

            graph.ImportAmazonRecords(graph, list, currentFilter);
        }