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).SingleOrDefault();
                if (order != null)
                {
                    var store = context.stores_data.Where(x => x.id == order.storeId).SingleOrDefault();
                    if (store != null)
                    {
                        cartAPI api = new cartAPI();
                        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", "");
                            var asr = resultStatus;
                        }
                    }
                    order.tracking_code = trackingNumber;
                    order.date_modifed = DateTime.Now;

                }

                return true;

            }
            catch (InvalidOperationException exc)
            {
                return false;
            }
            catch (ArgumentNullException exc)
            {
                return false;
            }
            catch (NullReferenceException exc)
            {
                return false;
            }
            catch (OptimisticConcurrencyException exc)
            {
                return false;
            }
            catch (UpdateException exc)
            {
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }
        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)
                    {
                        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);

                        }

                    }
                    UpdateStoreLastRun(st.Id);

                }
            }
        }