public void CheckAllOrders()
        {
            List <StoreDto> listOfStores = GetAllStores();
            cartAPI         api          = new cartAPI();

            //UpsDbMethods upsDb = new UpsDbMethods();
            //bool rer = upsDb.ReadAllUpsData();
            //Calls the authorization url
            if (listOfStores != null)
            {
                foreach (var st in listOfStores)
                {
                    List <string> OrderStatuses = GetAllOrderStatusTextByStore(st.Id);
                    foreach (var orderStatuse in OrderStatuses)
                    {
                        try
                        {
                            string lastRun = String.Format("{0:MM/dd/yyyy}", st.LastRun);
                            //string status = GetOrderStatusText(st.OrderStatus);
                            XDocument xdoc = XDocument.Parse(api.getOrder(st.Url, st.ApiKey, 100, 1, false, "", orderStatuse, lastRun, "", "").OuterXml);
                            if (!xdoc.Root.Name.LocalName.Equals("Error"))
                            {
                                var OrderInformation = from x in xdoc.Descendants("Order")
                                                       select new
                                {
                                    //---OrderInformation
                                    OrderID         = x.Descendants("OrderID").First().Value,
                                    Total           = x.Descendants("Total").First().Value,
                                    InvoiceNumber   = x.Descendants("InvoiceNumber").First().Value,
                                    CustomerID      = x.Descendants("CustomerID").First().Value,
                                    OrderStatus     = x.Descendants("OrderStatus").First().Value,
                                    DateStarted     = x.Descendants("DateStarted").First().Value,
                                    LastUpdate      = x.Descendants("LastUpdate").First().Value,
                                    OrderComment    = x.Descendants("Comments").Descendants("OrderComment").First().Value,
                                    OrderIntComment = x.Descendants("Comments").Descendants("OrderInternalComment").First().Value,
                                    OrderExtComment = x.Descendants("Comments").Descendants("OrderExternalComment").First().Value,
                                    Shippinginfo    = x.Descendants("ShippingInformation").Descendants("Shipment"),
                                    ItemInfo        = x.Descendants("ShippingInformation").Descendants("OrderItems").Descendants("Item"),
                                    Email           = x.Descendants("BillingAddress").Descendants("Email").First().Value
                                };
                                var result = orders.CreateOrdersFromStore(OrderInformation, st.Id);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error Getting Store: " + st.Name + " Error:" + ex.Message + " at: " + DateTime.Now);
                        }
                    }
                    UpdateStoreLastRun(st.Id);
                }
            }
        }
        public string CheckForAnOrder(int storeId, string invoice)
        {
            if (CheckIfOrderExist(invoice))
            {
                return("Invoice " + invoice + " is already on the records");
            }
            cartAPI   api   = new cartAPI();
            var       store = GetStoreById(storeId);
            XDocument xdoc  = XDocument.Parse(api.getOrder(store.Url, store.ApiKey, 100, 1, false, invoice, "", "", "", "").OuterXml);

            if (!xdoc.Root.Name.LocalName.Equals("Error"))
            {
                var OrderInformation = from x in xdoc.Descendants("Order")
                                       select new
                {
                    //---OrderInformation
                    OrderID         = x.Descendants("OrderID").First().Value,
                    Total           = x.Descendants("Total").First().Value,
                    InvoiceNumber   = x.Descendants("InvoiceNumber").First().Value,
                    CustomerID      = x.Descendants("CustomerID").First().Value,
                    OrderStatus     = x.Descendants("OrderStatus").First().Value,
                    DateStarted     = x.Descendants("DateStarted").First().Value,
                    LastUpdate      = x.Descendants("LastUpdate").First().Value,
                    OrderComment    = x.Descendants("Comments").Descendants("OrderComment").First().Value,
                    OrderIntComment = x.Descendants("Comments").Descendants("OrderInternalComment").First().Value,
                    OrderExtComment = x.Descendants("Comments").Descendants("OrderExternalComment").First().Value,
                    Shippinginfo    = x.Descendants("ShippingInformation").Descendants("Shipment"),
                    ItemInfo        = x.Descendants("ShippingInformation").Descendants("OrderItems").Descendants("Item"),
                    Email           = x.Descendants("BillingAddress").Descendants("Email").First().Value
                };
                var result = orders.CreateOrdersFromStore(OrderInformation, store.Id);

                return("Invoice " + invoice + " has been added succesfully");
            }
            else
            {
                return("Invoice " + invoice + " was not found in the selected store. Try another store");
            }
        }
        public bool UpdateOrderByInvoice(string invoiceNumber, string trackingNumber)
        {
            var context = new orderstatusEntities();

            try
            {
                var order = context.orders.Where(x => x.invoice_number == invoiceNumber).OrderByDescending(x => x.date_created).FirstOrDefault();
                if (order != null)
                {
                    var store = context.stores_data.FirstOrDefault(x => x.id == order.storeId);
                    if (store != null)
                    {
                        cartAPI api     = new cartAPI();
                        var     results = api.getOrderStatus(store.url, store.api_key, order.invoice_number, "");
                        if (results != null && results.ChildNodes[2] != null && !results.ChildNodes[2].InnerText.ToString().Equals("Shipped") && !results.ChildNodes[2].InnerText.ToString().Equals("Complete"))
                        {
                            var result = api.updateOrderShipment(store.url, store.api_key, order.invoice_number, "",
                                                                 trackingNumber, DateTime.Now.ToShortDateString(), "");

                            if (result.InnerText.Equals("OK"))
                            {
                                var resultStatus = api.updateOrderStatus(store.url, store.api_key, order.invoice_number, "Shipped", "");
                                using (StreamWriter writer = File.AppendText(ConfigurationManager.AppSettings["logPathFile"]))
                                {
                                    writer.WriteLine("Order Invoice: " + order.invoice_number + " updated from " + results.ChildNodes[2].InnerText.ToString() + " to Shipped");
                                }

                                var asr = resultStatus;
                            }
                        }
                    }
                    order.tracking_code = trackingNumber;
                    order.date_modifed  = DateTime.Now;
                    context.SaveChanges();
                }

                return(true);
            }
            catch (InvalidOperationException exc)
            {
                log.Error("Error UpdateOrderByInvoice: " + exc.InnerException.Message);
                return(false);
            }
            catch (ArgumentNullException exc)
            {
                log.Error("Error UpdateOrderByInvoice: " + exc.InnerException.Message);
                return(false);
            }
            catch (NullReferenceException exc)
            {
                log.Error("Error UpdateOrderByInvoice: " + exc.InnerException.Message);
                return(false);
            }
            catch (OptimisticConcurrencyException exc)
            {
                log.Error("Error UpdateOrderByInvoice: " + exc.InnerException.Message);
                return(false);
            }
            catch (UpdateException exc)
            {
                log.Error("Error UpdateOrderByInvoice: " + exc.InnerException.Message);
                return(false);
            }
            finally
            {
                context.Dispose();
            }
        }