private void CheckFullAssortmentAndDeactivateProducts(IEnumerable <AssortmentStockPriceProduct> productsSource)
        {
            using (var helper = new AssortmentHelper(Connector.Connection, Version))
            {
                var skuList      = helper.GetSkuList();
                var skuInXmlList = (from p in productsSource
                                    select p.ManufacturerID).ToList();

                var toDeactivate = skuList.Where(x => !skuInXmlList.Contains(x.Key));

                //exclude shipping costs
                toDeactivate = toDeactivate.Where(x => !x.Key.StartsWith("ShippingCostProductID_"));

                if (toDeactivate.Count() > 0)
                {
                    SortedDictionary <string, eav_attribute> attributeList = helper.GetAttributeList(PRODUCT_ENTITY_TYPE_ID);

                    foreach (var p in toDeactivate)
                    {
                        SetProductStatus(p.Value.entity_id, (int)PRODUCT_STATUS.DISABLED, helper, attributeList, StoreList[CurrentStoreCode].store_id);

                        if (RequiresStockUpdate)
                        {
                            helper.SyncStock(new cataloginventory_stock_item()
                            {
                                product_id  = p.Value.entity_id,
                                qty         = 0,
                                is_in_stock = false
                            });
                        }
                    }
                }
            }
        }
        private void ProcessProductCollection(Dictionary <int, List <AssortmentRetailStock> > retailStockList, Dictionary <string, int> stockStoreList, List <AssortmentStockPriceProduct> products)
        {
            int totalProcessed = 0;
            SortedDictionary <string, eav_attribute> attributeList;
            int totalRecords = products.Count();

            Logger.DebugFormat("Found {0} products", totalRecords);

            using (var helper = new AssortmentHelper(Connector.Connection, Version))
            {
                attributeList = helper.GetAttributeList(PRODUCT_ENTITY_TYPE_ID);
            }

            ParallelOptions options = new ParallelOptions()
            {
                MaxDegreeOfParallelism = 1
            };

            Parallel.ForEach(Partitioner.Create(0, totalRecords), options, (range, loopState) =>
            {
                using (var helper = new AssortmentHelper(Connector.Connection, Version))
                {
                    var skuList = helper.GetSkuList();

                    for (int index = range.Item1; index < range.Item2; index++)
                    {
                        int productID = Convert.ToInt32(products[index].ProductID);

                        IEnumerable <AssortmentRetailStock> productRetailStock = null;

                        ProcessProduct(helper, products[index], skuList, attributeList, stockStoreList, productRetailStock);

                        Interlocked.Increment(ref totalProcessed);
                        if (totalProcessed % 100 == 0)
                        {
                            Logger.DebugFormat(String.Format("Processed {0} of {1} records", totalProcessed, totalRecords));
                        }
                    }
                }
            });
        }
        private void DeactivateAssortmentPartial(IEnumerable <AssortmentStockPriceProduct> productsSource)
        {
            using (var helper = new AssortmentHelper(Connector.Connection, Version))
            {
                var skuList      = helper.GetSkuList();
                var toDeactivate = (from p in productsSource
                                    let entity_id = (skuList.ContainsKey(p.ManufacturerID) ? skuList[p.ManufacturerID].entity_id : 0)
                                                    where !p.IsNonAssortmentItem && entity_id != 0 && //only products that are already present in magento
                                                    !p.ManufacturerID.StartsWith("ShippingCostProductID_")
                                                    select new KeyValuePair <string, int>(
                                        p.ManufacturerID,
                                        entity_id
                                        )).ToList();


                //exclude shipping costs


                if (toDeactivate.Count() > 0)
                {
                    SortedDictionary <string, eav_attribute> attributeList = helper.GetAttributeList(PRODUCT_ENTITY_TYPE_ID);

                    foreach (var p in toDeactivate)
                    {
                        SetProductStatus(p.Value, (int)PRODUCT_STATUS.DISABLED, helper, attributeList, StoreList[CurrentStoreCode].store_id);

                        if (RequiresStockUpdate)
                        {
                            helper.SyncStock(new cataloginventory_stock_item()
                            {
                                product_id  = p.Value,
                                qty         = 0,
                                is_in_stock = false
                            });
                        }
                    }
                }
            }
        }
        private void SyncProducts()
        {
            bool doRetailStock = ((ConnectorSystemType)Connector.ConnectorType).Has(ConnectorSystemType.ExportRetailStock);

            XDocument retailStock = null;
            Dictionary <int, List <AssortmentRetailStock> > retailStockList = new Dictionary <int, List <AssortmentRetailStock> >();

            if (IsPrimaryLanguage && doRetailStock)
            {
                retailStockList = (from r in Assortment
                                   select new
                {
                    product_id = r.ProductID,
                    store_records = r.RetailStock
                }).Distinct().ToDictionary(x => x.product_id, y => y.store_records);
            }

            Dictionary <string, int> stockStoreList = new Dictionary <string, int>();

            if (doRetailStock)
            {
                using (var helper = new AssortmentHelper(Connector.Connection, Version))
                {
                    stockStoreList = helper.GetStockStoreList();
                }
            }

            var productsSource = (from p in Assortment
                                  select p);

            List <AssortmentStockPriceProduct> products = productsSource.Where(x => !x.IsConfigurable).ToList();

            ProcessProductCollection(retailStockList, stockStoreList, products);

            using (var helper = new AssortmentHelper(Connector.Connection, Version))
            {
                var skuList      = helper.GetSkuList();
                var skuInXmlList = (from p in productsSource
                                    select p.ManufacturerID).ToList();

                var toDeactivate = skuList.Where(x => !skuInXmlList.Contains(x.Key));

                //exclude shipping costs
                toDeactivate = toDeactivate.Where(x => !x.Key.StartsWith("ShippingCostProductID_"));

                if (toDeactivate.Count() > 0)
                {
                    SortedDictionary <string, eav_attribute> attributeList = helper.GetAttributeList(PRODUCT_ENTITY_TYPE_ID);

                    foreach (var p in toDeactivate)
                    {
                        SetProductStatus(p.Value.entity_id, (int)PRODUCT_STATUS.DISABLED, helper, attributeList, StoreList[CurrentStoreCode].store_id);

                        if (RequiresStockUpdate)
                        {
                            helper.SyncStock(new cataloginventory_stock_item()
                            {
                                product_id  = p.Value.entity_id,
                                qty         = 0,
                                is_in_stock = false
                            });
                        }
                    }
                }
            }
        }