Example #1
0
 protected void Page_Load(object sender, EventArgs e)
 {
     //_code = GetUniqueIDRelativeTo();
     //_productName = Product.SearchById(_code).name;
     //_description = Product.SearchById(_code).description;
     if (!String.IsNullOrEmpty(Request.QueryString["ID"]))
     {
         id = int.Parse(Request.QueryString["ID"]);
     }
     DataSetTableAdapters.ProductTableAdapter ds = new DataSetTableAdapters.ProductTableAdapter();
     item = ds.GetDataByID(id)[0];
     Page.DataBind();
 }
        /// <summary>
        /// Run Shopify account update.
        /// </summary>
        /// <param name="shopID">Shop ID from the ShopifyVisma Shop table</param>
        /// <param name="updateType">Update type (1 - regular; 2 - full)</param>
        /// <returns></returns>
        public bool UpdateRecords(int shopID, short updateType = 1)
        {
            shopTA     = new DataSetTableAdapters.ShopTableAdapter();
            customerTA = new DataSetTableAdapters.CustomerTableAdapter();
            productTA  = new DataSetTableAdapters.ProductTableAdapter();
            orderTA    = new DataSetTableAdapters.OrderTableAdapter();

            bool   isFullUpdate   = (updateType > 1);
            string updateTypeText = (isFullUpdate) ? "full" : "regular";


            // Initialize connections to Shopify and Visma
            shop = Shopify.GetShopByID(shopID);

            if (shop == null)
            {
                LogError(string.Format("Unable to load shop {0} from database", shopID), null);
                return(false);
            }

            shop.ProcessShopData();

            visma = new Visma(shop.data.VismaCompany);

            DateTime?lastShopifyUpdateDate = null;

            try
            {
                lastShopifyUpdateDate = shop.data.ShopifyUpdatedDate;
            }
            catch (StrongTypingException ex) { }

            DateTime?lastVismaUpdateDate  = null;
            DateTime?limitVismaUpdateDate = null;   // Query Visma records with lastVismaUpdateDate - 1 day

            try
            {
                lastVismaUpdateDate = shop.data.VismaUpdatedDate;

                if (lastShopifyUpdateDate.HasValue)
                {
                    limitVismaUpdateDate = lastVismaUpdateDate;
                    limitVismaUpdateDate = limitVismaUpdateDate.Value.AddDays(-1);
                }
            }
            catch (StrongTypingException ex) { }

            DateTime currentUpdateDate = DateTime.Now;

            Log(string.Format("\n== Starting {0} update for Shop {1} [{2}] at {3}. ==", updateTypeText, shop.account, shopID, currentUpdateDate));
            Log(string.Format("Last Shopify update: {0}, Last Visma update: {1}\n", lastShopifyUpdateDate, lastVismaUpdateDate));

            StatusUpdate(string.Format("Starting new update for shop {0} [{1}].", shop.account, shopID));

            try
            {
                // Update Customers
                bool resultCustomers = UpdateCustomers(limitVismaUpdateDate, 0);

                // Update Products
                bool resultProducts = UpdateProducts(limitVismaUpdateDate, 0, true, isFullUpdate);

                // Update Specific Prices
                bool resultPrices = UpdateSpecificPrices(limitVismaUpdateDate, 50, 0);

                // Update Orders
                bool resultOrders = UpdateOrders(lastShopifyUpdateDate, 0);

                // Update Shop update times
                DateTime?fullUpdateDate = null;
                if (isFullUpdate)
                {
                    fullUpdateDate = currentUpdateDate;
                }
                UpdateShop(currentUpdateDate, currentUpdateDate, fullUpdateDate);
            }
            catch (Exception ex)
            {
                LogError(string.Format("Error updating shop {0}", shop.account), ex);
                StatusUpdate("Error updating shop.");
                return(false);
            }


            Log(string.Format("\n=== Finished update at {0}.===\n\n", DateTime.Now));
            StatusUpdate("Update finished.");

            return(true);
        }