Esempio n. 1
0
        public long UpsertProductAndInventory(Product product)
        {
            long productMonsterId;

            using (var transaction = _inventoryRepository.BeginTransaction())
            {
                var existing = _inventoryRepository.RetrieveProduct(product.id);
                if (existing == null)
                {
                    var data = new ShopifyProduct();
                    data.ShopifyProductId   = product.id;
                    data.ShopifyTitle       = product.title ?? "";
                    data.ShopifyProductType = product.product_type;
                    data.ShopifyVendor      = product.vendor;
                    data.DateCreated        = DateTime.UtcNow;
                    data.LastUpdated        = DateTime.UtcNow;

                    _executionLogService.Log(LogBuilder.DetectedNewProduct(data));
                    _inventoryRepository.InsertProduct(data);
                    _inventoryRepository.SaveChanges();

                    productMonsterId = data.MonsterId;
                }
                else
                {
                    existing.ShopifyTitle       = product.title ?? "";
                    existing.ShopifyProductType = product.product_type;
                    existing.ShopifyVendor      = product.vendor;
                    existing.LastUpdated        = DateTime.UtcNow;
                    _inventoryRepository.SaveChanges();

                    productMonsterId = existing.MonsterId;
                }

                _shopifyJsonService.Upsert(ShopifyJsonType.Product, product.id, product.SerializeToJson());
                transaction.Commit();
            }


            // Write the Product Variants
            UpsertProduct(productMonsterId, product);

            // Flags the missing Variants
            ProcessMissingVariants(productMonsterId, product);

            // Pull and write the Inventory
            PullAndUpsertInventory(productMonsterId);

            return(productMonsterId);
        }
        public void RunLocations()
        {
            var dataLocations = _locationRepository.RetreiveLocations();

            var shopifyLocations
                = _productApi
                  .RetrieveLocations()
                  .DeserializeFromJson <LocationList>();

            foreach (var shopifyLoc in shopifyLocations.locations)
            {
                var dataLocation = dataLocations.FindByShopifyId(shopifyLoc);

                using (var transaction = _locationRepository.BeginTransaction())
                {
                    if (dataLocation == null)
                    {
                        var newDataLocation = new ShopifyLocation
                        {
                            ShopifyLocationId   = shopifyLoc.id,
                            ShopifyLocationName = shopifyLoc.name,
                            ShopifyActive       = shopifyLoc.active,
                            DateCreated         = DateTime.UtcNow,
                            LastUpdated         = DateTime.UtcNow,
                        };

                        _locationRepository.InsertLocation(newDataLocation);
                    }
                    else
                    {
                        dataLocation.LastUpdated         = DateTime.UtcNow;
                        dataLocation.ShopifyLocationName = shopifyLoc.name;
                        dataLocation.ShopifyActive       = shopifyLoc.active;
                    }
                    _locationRepository.SaveChanges();
                    _shopifyJsonService.Upsert(ShopifyJsonType.Location, shopifyLoc.id, shopifyLoc.SerializeToJson());

                    transaction.Commit();
                }
            }
        }
        private void UpdateInventoryLevel(AcumaticaStockItem stockItem, ShopifyInventoryLevel level)
        {
            var location         = _syncInventoryRepository.RetrieveLocation(level.ShopifyLocationId);
            var warehouseIds     = location.MatchedWarehouseIds();
            var warehouseDetails = stockItem.Inventory(warehouseIds);

            var available = (int)warehouseDetails.Sum(x => x.AcumaticaAvailQty);
            var sku       = level.ShopifyVariant.ShopifySku;

            var levelDto = new InventoryLevel
            {
                inventory_item_id = level.ShopifyInventoryItemId,
                available         = available,
                location_id       = location.ShopifyLocationId,
            };

            var levelJson = levelDto.SerializeToJson();

            _inventoryApi.SetInventoryLevels(levelJson);


            using (var transaction = _syncInventoryRepository.BeginTransaction())
            {
                var log = $"Updated Shopify Variant {sku} " +
                          $"in Location {location.ShopifyLocationName} to Available Qty {available}";

                _executionLogService.Log(log);

                warehouseDetails.ForEach(x => x.IsInventorySynced = true);

                // Update Shopify Inventory Level records
                //
                level.ShopifyAvailableQuantity = available;
                level.LastUpdated = DateTime.UtcNow;

                _inventoryRepository.SaveChanges();
                transaction.Commit();
            }
        }