public async Task <int> CreateProduct(string storeId, string name, string sku, int isInStock, string productType, Mark markForLog)
        {
            var stockItem        = new CreatteProductModel(name, sku, isInStock, productType);
            var methodParameters = stockItem.ToJson();

            try
            {
                const int maxCheckCount    = 2;
                const int delayBeforeCheck = 1800000;

                var privateClient = this._clientFactory.CreateMagentoCatalogProductRepositoryServiceClient();

                var res        = new List <UpdateRessult <CreatteProductModel> >();
                var stockItems = new List <CreatteProductModel> {
                    stockItem
                };

                await stockItems.DoInBatchAsync(10, async x =>
                {
                    await ActionPolicies.GetAsync.Do(async() =>
                    {
                        var statusChecker = new StatusChecker(maxCheckCount);
                        TimerCallback tcb = statusChecker.CheckStatus;

                        privateClient = this._clientFactory.RefreshMagentoCatalogProductRepositoryServiceClient(privateClient);

                        var updateResult = new UpdateRessult <CreatteProductModel>(x, 0);
                        res.Add(updateResult);

                        using (var stateTimer = new Timer(tcb, privateClient, 1000, delayBeforeCheck))
                        {
                            MagentoLogger.LogTraceStarted(this.CreateMethodCallInfo(methodParameters, mark: markForLog));

                            var catalogInventoryDataStockItemInterface = new CatalogDataProductInterface()
                            {
                                sku                     = x.Sku,
                                name                    = x.Name,
                                price                   = "1",
                                priceSpecified          = true,
                                status                  = 1,
                                statusSpecified         = true,
                                typeId                  = productType,
                                attributeSetId          = 4,
                                attributeSetIdSpecified = true,
                                weight                  = "1",
                                weightSpecified         = true,
                            };
                            if (productType == "bundle")
                            {
                                catalogInventoryDataStockItemInterface.customAttributes = new[]
                                {
                                    new FrameworkAttributeInterface {
                                        value = "1", attributeCode = "price_view"
                                    },
                                    new FrameworkAttributeInterface {
                                        value = "1", attributeCode = "price_type"
                                    }
                                };
                            }
                            var catalogInventoryStockRegistryV1UpdateStockItemBySkuRequest = new CatalogProductRepositoryV1SaveRequest()
                            {
                                product = catalogInventoryDataStockItemInterface
                            };

                            var temp = await privateClient.catalogProductRepositoryV1SaveAsync(catalogInventoryStockRegistryV1UpdateStockItemBySkuRequest).ConfigureAwait(false);

                            updateResult.ErrorCode = temp.catalogProductRepositoryV1SaveResponse.result.id;
                        }
                    }).ConfigureAwait(false);
                }).ConfigureAwait(false);

                MagentoLogger.LogTraceEnded(this.CreateMethodCallInfo(methodParameters, mark: markForLog, methodResult: res.ToJson()));

                return(res.First().ErrorCode);
            }
            catch (Exception exc)
            {
                throw new MagentoSoapException($"An error occured during PutStockItemsAsync({methodParameters})", exc);
            }
        }
Exemple #2
0
        public virtual async Task <bool> PutStockItemsAsync(List <PutStockItem> stockItems, Mark markForLog = null)
        {
            var methodParameters = stockItems.ToJson();

            try
            {
                const int maxCheckCount    = 2;
                const int delayBeforeCheck = 1800000;

                var privateClient = this.CreateMagentoCatalogInventoryStockServiceClient(this.BaseMagentoUrl);

                var res = new List <UpdateRessult>();

                await stockItems.DoInBatchAsync(10, async x =>
                {
                    await ActionPolicies.GetAsync.Do(async() =>
                    {
                        var statusChecker = new StatusChecker(maxCheckCount);
                        TimerCallback tcb = statusChecker.CheckStatus;

                        if (privateClient.State != CommunicationState.Opened &&
                            privateClient.State != CommunicationState.Created &&
                            privateClient.State != CommunicationState.Opening)
                        {
                            privateClient = this.CreateMagentoCatalogInventoryStockServiceClient(this.BaseMagentoUrl);
                        }

                        var updateResult = new UpdateRessult(x, 0);
                        res.Add(updateResult);

                        using (var stateTimer = new Timer(tcb, privateClient, 1000, delayBeforeCheck))
                        {
                            MagentoLogger.LogTraceStarted(this.CreateMethodCallInfo(methodParameters, mark: markForLog));

                            var catalogInventoryDataStockItemInterface = new CatalogInventoryDataStockItemInterface()
                            {
                                qty                            = x.Qty.ToString(),
                                productId                      = int.Parse(x.ProductId),
                                productIdSpecified             = true,
                                isInStock                      = x.Qty > 0,
                                isQtyDecimal                   = false,
                                showDefaultNotificationMessage = false,
                                useConfigMinQty                = true,
                                minQty                         = 0,
                                useConfigMinSaleQty            = 1,
                                minSaleQty                     = 1,
                                useConfigMaxSaleQty            = true,
                                maxSaleQty                     = 10000,
                                useConfigBackorders            = true,
                                backorders                     = 0,
                                useConfigNotifyStockQty        = true,
                                notifyStockQty                 = 1,
                                useConfigQtyIncrements         = true,
                                qtyIncrements                  = 0,
                                useConfigEnableQtyInc          = false,
                                enableQtyIncrements            = false,
                                useConfigManageStock           = true,
                                manageStock                    = true,
                                //lowStockDate = "2016-02-29 20:48:26",
                                isDecimalDivided       = false,
                                stockStatusChangedAuto = 1
                            };
                            var catalogInventoryStockRegistryV1UpdateStockItemBySkuRequest = new CatalogInventoryStockRegistryV1UpdateStockItemBySkuRequest()
                            {
                                productSku = x.Sku,
                                stockItem  = catalogInventoryDataStockItemInterface
                            };

                            var temp = await privateClient.catalogInventoryStockRegistryV1UpdateStockItemBySkuAsync(catalogInventoryStockRegistryV1UpdateStockItemBySkuRequest).ConfigureAwait(false);

                            updateResult.Success = temp.catalogInventoryStockRegistryV1UpdateStockItemBySkuResponse.result;
                        }
                    }).ConfigureAwait(false);
                }).ConfigureAwait(false);

                MagentoLogger.LogTraceEnded(this.CreateMethodCallInfo(methodParameters, mark: markForLog, methodResult: res.ToJson()));

                return(res.All(x => x.Success > 0));
            }
            catch (Exception exc)
            {
                throw new MagentoSoapException(string.Format("An error occured during PutStockItemsAsync({0})", methodParameters), exc);
            }
        }