public static void SetPoNumber(string oid, Order order)
        {
            var orderFields = RoiClient.GetOrderFields(oid, Token);

            foreach (var field in orderFields)
            {
                if (field.Name == "PaymentPurchaseOrderNumber")
                {
                    var poNumber = field.Value;
                    order.PoNumber = poNumber;
                    break;
                }
            }
        }
        public static async Task <List <string> > TharsternSubmission(List <PrintOptions> printOptions, DeliveryDetails deliveryDetails, Order orderDetails, Dictionary <string, string> messages, AddressDetails customerAddress)
        {
            List <string> lstQuotesAndErrors = new List <string>();

            try
            {
                Logger.WriteLog("############ Tharstern - Order Submission started for PDQOrderId:" + orderDetails.RoiOrderId + " " + "############");

                TharsternEngine tharsternEngine = new TharsternEngine(printOptions, deliveryDetails);

                var po = orderDetails.PoNumber;

                lstQuotesAndErrors = await tharsternEngine.SubEstimateToTharstern(orderDetails, po, messages, customerAddress);

                Logger.WriteLog("############ Tharstern - Order Submission completed for PDQOrderId:" + orderDetails.RoiOrderId + " " + "############");
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    Logger.WriteLog(ex.InnerException.ToString());
                }
                Logger.WriteLog("############ Tharstern - Order Submission failed for PDQOrderId:" + orderDetails.RoiOrderId + " " + ex.Message + "############");
            }
            return(lstQuotesAndErrors);
        }
        public static async Task ProcessRoiOrders()
        {
            OrderManager orderManager = new OrderManager();

            var cutOffDate = new DateTime(2019, 12, 18);
            var days       = DateTime.Now.Subtract(cutOffDate).Days;
            var orderIds   = RoiClient.GetOrderIdListByDateRange(DateTime.Now.AddDays(-1 * days), DateTime.Now, Token);

            Dictionary <string, string> messages = new Dictionary <string, string>();

            foreach (var oid in orderIds)
            {
                if (orderManager.OrderAlreadyPushedToTharstern(Convert.ToInt64(oid)))
                {
                    continue;
                }

                if (orderManager.OrderFailedInProcessing(Convert.ToInt64(oid)))
                {
                    continue;
                }

                Order order = new Order();

                //Order Details
                var orderDetails = RoiClient.GetOrder(oid, Token);

                bool cancelledOrder = false;

                cancelledOrder = IsCancelledOrder(messages, orderDetails, false);

                if (cancelledOrder)
                {
                    continue;
                }

                SetPoNumber(oid, order);

                var user = RoiClient.GetUserById(orderDetails.userId, Token);

                var accCode = user.CustomUserFields[25].Value; //UserProfileAccountNumber

                bool isOlam = IsOlamCustomer(user);

                if (isOlam)
                {
                    orderManager.AddNonReadyOrders(oid, "Rejected - OLAM");

                    if (!messages.ContainsKey(oid))
                    {
                        messages.Add(oid, "Olam account order - please process this order manually.");
                    }
                    else
                    {
                        messages[oid] += " | " + "Olam account order - please process this order manually.";
                    }

                    continue;
                }
                //check account number exists

                if (CheckCustomerCode(oid, accCode, messages))
                {
                    continue;
                }

                order.RoiOrderId      = oid;
                order.User            = user.LoginName;
                order.AccountCode     = accCode;
                order.OrderTotal      = orderDetails.orderTotal.amount;
                order.CurrencyUnit    = orderDetails.orderTotal.unit.ToString();
                order.OrderPlacedDate = orderDetails.dateTimeOrderPlaced;
                order.User            = orderDetails.userId;
                order.OrderPartsCount = orderDetails.documentIdList.Count;

                orderManager.DownloadRoiOrder(Convert.ToInt64(oid), orderDetails.orderTotal.amount, orderDetails.dateTimeOrderPlaced);

                List <PrintOptions> printOptionsList = new List <PrintOptions>();

                DeliveryDetails deliveryDetails = new DeliveryDetails();

                ProcessOrderItems(orderDetails, printOptionsList, deliveryDetails, messages);

                if (messages.ContainsKey(oid))
                {
                    continue;
                }

                if (orderManager.CheckNotReadyOrders(orderDetails.orderId))
                {
                    continue;
                }

                order.Email      = deliveryDetails.Reference;
                order.UserName   = deliveryDetails.UserName;
                order.ExternalId = orderDetails.externalId;

                bool continueFlag = true;

                foreach (var printoption in printOptionsList)
                {
                    if (string.IsNullOrEmpty(printoption.Size))
                    {
                        if (!messages.ContainsKey(oid))
                        {
                            messages.Add(oid, "Product option SIZE Missing, product option may be html literal, please review the product option");
                        }
                        else
                        {
                            messages[oid] += " | " + "Product option SIZE Missing, product option may be html literal, please review the product option";
                        }
                        continueFlag = false;
                    }

                    if (string.IsNullOrEmpty(printoption.Stock))
                    {
                        if (!messages.ContainsKey(oid))
                        {
                            messages.Add(oid, "Product option STOCK Missing, product option may be html literal, please review the product option");
                        }
                        else
                        {
                            messages[oid] += " | " + "Product option STOCK Missing, product option may be html literal, please review the product option";
                        }

                        continueFlag = false;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(printoption.StockFullString))
                        {
                            bool containsInt = printoption.StockFullString.Any(char.IsDigit);

                            if (!containsInt)
                            {
                                if (!messages.ContainsKey(oid))
                                {
                                    messages.Add(oid, "Invalid Stock value: " + printoption.StockFullString + ", product option may be html literal, please review the product option");
                                }
                                else
                                {
                                    messages[oid] += " | " + "Invalid Stock value: " + printoption.StockFullString + ", product option may be html literal, please review the product option";
                                }

                                continueFlag = false;
                            }
                        }
                    }
                }

                if (continueFlag)
                {
                    AddressDetails customerAddress = BuildCustomerAddress(user);

                    await TharsternSubmission(printOptionsList, deliveryDetails, order, messages, customerAddress);

                    orderManager.MarkOrderAllPartsPushedToTharstern(Convert.ToInt64(oid), printOptionsList.Count);
                }
            }

            //send email

            SendProcessingSummaryEmail(messages);
        }