Exemple #1
0
 public static Shopify GetShopByID(int ID)
 {
     DataSetTableAdapters.ShopTableAdapter shopTA = new DataSetTableAdapters.ShopTableAdapter();
     DataSet.ShopDataTable shopDT = shopTA.GetDataByID(ID);
     if (shopDT.Count > 0)
     {
         Shopify shop = new Shopify(ID, shopDT[0].ShopifyStoreAccount, shopDT[0].ShopifyAccessToken);
         shop.data = shopDT[0];
         return(shop);
     }
     else
     {
         return(null);
     }
 }
        private void CheckAutoUpdate()
        {
            // Do not run if an update is already running
            if (this.updateRunning)
            {
                return;
            }

            int?shopID = Shopify.GetAutoUpdateShopID();

            if (shopID.HasValue)
            {
                UpdateTextBox(string.Format("Starting Auto Update for shop {0}", shopID.Value));
                StartUpdate(shopID.Value, 1);
            }
        }
        /// <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);
        }