protected void ListOrdersByNextToken(SOImportProcess graph, OrdersParameters objOrderParams)
 {
     objServiceCallParams = new ServiceCallParameters();
     objServiceCallParams.objSOAmazonSetup    = objOrderParams.objSOAmazonSetup;
     objServiceCallParams.amwOrderID          = string.Empty;
     objServiceCallParams.methodCall          = SOConstants.invokeListOrdersByNextToken;
     objServiceCallParams.fromDate            = objOrderParams.objSOImportFilter.FromDate;
     objServiceCallParams.toDate              = objOrderParams.objSOImportFilter.TODate;
     objServiceCallParams.nextToken           = objOrderParams.nextToken;
     objServiceCallParams.liListOrderResponse = objOrderParams.liListOrderResponse;
     objOrderParams.objSyncOrderResponse      = new SOOrdersServiceCall(clientOrder).InvokeServicesCalls(graph, objServiceCallParams);
     if (objOrderParams.objSyncOrderResponse != null && objOrderParams.objSyncOrderResponse.objListOrdersByNextToken != null && objOrderParams.objSyncOrderResponse.objListOrdersByNextToken.ListOrdersByNextTokenResult != null &&
         objOrderParams.objSyncOrderResponse.objListOrdersByNextToken.ListOrdersByNextTokenResult.Orders != null && objOrderParams.objSyncOrderResponse.objListOrdersByNextToken.ListOrdersByNextTokenResult.Orders.Count > 0)
     {
         objOrderParams.integrationID = objOrderParams.objSOAmazonSetup.IntegrationID;
         foreach (Order item in objOrderParams.objSyncOrderResponse.objListOrdersByNextToken.ListOrdersByNextTokenResult.Orders)
         {
             objOrderParams.objItem = item;
             LoadDataToGrid(graph, objOrderParams);
         }
         if (objOrderParams.objSyncOrderResponse.objListOrdersByNextToken.ListOrdersByNextTokenResult.NextToken != null)
         {
             objOrderParams.nextToken = objOrderParams.objSyncOrderResponse.objListOrdersByNextToken.ListOrdersByNextTokenResult.NextToken;
             ListOrdersByNextToken(graph, objOrderParams);
         }
     }
 }
        public virtual void LoadDataToGrid(SOImportProcess graph, OrdersParameters objOrderParams)
        {
            string integrationId = Filter.Current.IntegrationID == null ? objOrderParams.objSOAmazonSetup.IntegrationID : Filter.Current.IntegrationID;

            if (!SOHelper.CheckOrderExist(graph, integrationId, objOrderParams.objItem.AmazonOrderId))
            {
                objOrderParams.processrecord                        = new SOProcessOrder();
                objOrderParams.processrecord.ProcessID              = objOrderParams.processID;
                objOrderParams.processrecord.AmazonOrderID          = objOrderParams.objItem.AmazonOrderId;
                objOrderParams.processrecord.BuyerEmailID           = objOrderParams.objItem.BuyerEmail;
                objOrderParams.processrecord.AmazonOrderDate        = objOrderParams.objItem.PurchaseDate != null ? objOrderParams.objItem.PurchaseDate : (DateTime?)null;
                objOrderParams.processrecord.AmazonOrderLastUpdated = objOrderParams.objItem.LastUpdateDate != null ? objOrderParams.objItem.LastUpdateDate : (DateTime?)null;
                objOrderParams.processrecord.OrderAmount            = objOrderParams.objItem.OrderTotal != null?Convert.ToDecimal(objOrderParams.objItem.OrderTotal.Amount) : 0m;

                objOrderParams.processrecord.AmazonStatus  = objOrderParams.objItem.OrderStatus;
                objOrderParams.processrecord.SynDatetime   = PX.Common.PXTimeZoneInfo.Now;
                objOrderParams.processrecord.IntegrationID = objOrderParams.integrationID;
                string xmlFeedContent = SOHelper.ListToXMLConverstion(objOrderParams.objItem);
                objOrderParams.processrecord.OrderSchema   = xmlFeedContent;
                objOrderParams.processrecord.PrepareStatus = true;
                objOrderParams.processrecord.ImportStatus  = false;
                graph.ImportOrderList.Cache.Insert(objOrderParams.processrecord);
            }
        }
        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);
                    }
                }
            });
        }