Ejemplo n.º 1
0
        public void Insert(ImportOperation importOperation, Config config)
        {
            IList <MappingTable> mappingTables = new Mapping.Setup().Load(config);

            DataTable dataTable = new ReaderService(config.ExcelImportConnectionString).Load(config.ExcelImportSheetName, config.ProductDataStartsFrom);

            List <ProcessedImportRow> exceptionsRows = new List <ProcessedImportRow>();
            DataTable spplierExceptionRows           = new DataTable();

            if (config.ImportType == "Fourth_2.0")
            {
                spplierExceptionRows = FindProductNotInReceivedFile(config, dataTable);
            }
            if (spplierExceptionRows.Rows.Count > 0)
            {
                DataTable exceptionReport = new ExceptionReport()
                                            .Generate(spplierExceptionRows, config.DisplayFileName, config.ImportType);
                if (exceptionReport != null && exceptionReport.Rows.Count > 0)
                {
                    new ExceptionReportService().Export(exceptionReport,
                                                        ConfigurationManager.AppSettings["ExceptionReportFilePath"] + config.ExceptionReportFileName);
                }
                config.FailedRows    = dataTable.Rows.Count;
                config.ProcessedRows = 0;
            }
            else
            {
                int?ingredientOrdinal   = mappingTables.ColumnOrdinalOf("product", "product_name");
                int?ingredientOrdinalPk = mappingTables.ColumnOrdinalOf("ingredient", "product_id");
                int?supplierId          = mappingTables.ColumnOrdinalOf("ingredient", "supplier_code");
                int rowNum        = config.ProductDataStartsFrom;
                int processedRows = 0;
                int failedRows    = 0;

                //Update starts from this column
                int startingCell = mappingTables.GetMinColumnOrdinal();
                CollectProductIds.Clear();

                foreach (DataRow row in dataTable.Rows)
                {
                    if (IsEmpty(row, startingCell))
                    {
                        rowNum++;
                        continue;
                    }
                    ProcessedImportRow processedImportRow = new ProcessedImportRow
                    {
                        Row     = row,
                        IsValid = true,
                        ImportDataExceptions = new List <ImportDataException>(),
                        RowIndex             = rowNum,
                        IngredientName       =
                            ingredientOrdinal == null ? string.Empty : row[(int)ingredientOrdinal].ToString(),
                        IngredientId =
                            (ingredientOrdinalPk == null || ingredientOrdinalPk < 0) ? string.Empty : row[(int)ingredientOrdinalPk].ToString(),
                        DistributorCode =
                            (supplierId == null || supplierId <= 0) ? string.Empty : row[(int)supplierId].ToString(),
                    };

                    GeneratorDelegates generatorDelegates = new GeneratorDelegates();

                    if (importOperation == ImportOperation.Insert)
                    {
                        generatorDelegates = ProductInsertDelegates(config);
                    }
                    else if (importOperation == ImportOperation.FuturePrice)
                    {
                        generatorDelegates = FuturePriceDelegates(config);
                    }
                    else if (importOperation == ImportOperation.InternalFuturePrice)
                    {
                        generatorDelegates = InternalFuturePriceDelegates(config);
                    }
                    else if (importOperation == ImportOperation.FuturePriceWithInvoiceCostPrice)
                    {
                        generatorDelegates = FuturePriceDelegates(config);
                    }
                    else if (importOperation == ImportOperation.IntolUpdate)
                    {
                        generatorDelegates = IntoleranceUpdateDelegates(config);
                    }
                    else if (importOperation == ImportOperation.NutritionUpdate)
                    {
                        generatorDelegates = NutrientUpdateDelegates(config);
                    }
                    else if (importOperation == ImportOperation.PriceOverride)
                    {
                        generatorDelegates = PriceOverrideDelegates(config);
                    }
                    else if (importOperation == ImportOperation.SupIntolUpdate)
                    {
                        generatorDelegates = SupIntoleranceUpdateDelegates(config);
                    }

                    bool result = new Sql.Generate().Query(mappingTables, processedImportRow, generatorDelegates);

                    if (processedImportRow.IsValid)
                    {
                        using (var ips = new ImportProductService(config.TargetConnectionString))
                        {
                            ips.ExecuteSql(processedImportRow.SqlQuery);

                            // add product to the queue for sending to MSMQ
                            if (!string.IsNullOrEmpty(processedImportRow.IngredientId) || !string.IsNullOrEmpty(processedImportRow.DistributorCode))
                            {
                                //New validation for stock count
                                int productId = 0;
                                if (int.TryParse(processedImportRow.IngredientId, out productId))
                                {
                                    ips.ValidateStockInventory(productId);
                                }

                                var item = !string.IsNullOrEmpty(processedImportRow.IngredientId)
                                    ? new Tuple <int?, string>(Convert.ToInt32(processedImportRow.IngredientId), null)
                                    : new Tuple <int?, string>(null, processedImportRow.DistributorCode);
                                CollectProductIds.Add(item);
                            }

                            processedRows++;
                        }
                    }
                    else
                    {
                        exceptionsRows.Add(processedImportRow);
                        failedRows++;
                    }
                    rowNum++;
                }

                List <MappingColumn> mappingColumns = MappingExtensions.GetTemplateColumns(mappingTables);
                using (var exService = new ExceptionMessageService(config.TargetConnectionString))
                {
                    List <ExceptionMessage> exceptionMessages = exService.GetExceptionMessages();

                    IList <int> dataRows;
                    DataTable   exceptionReport = new ExceptionReport()
                                                  .Generate(mappingColumns, config.DisplayFileName, exceptionsRows, exceptionMessages,
                                                            config.ImportType, out dataRows);
                    if (exceptionReport != null && exceptionReport.Rows.Count > 0)
                    {
                        new ExceptionReportService().Export(exceptionReport,
                                                            ConfigurationManager.AppSettings["ExceptionReportFilePath"] + config.ExceptionReportFileName,
                                                            dataRows);
                    }
                    config.FailedRows    = failedRows;
                    config.ProcessedRows = processedRows;
                }
            }
        }