Esempio n. 1
0
 public FrmImportProduct()
 {
     InitializeComponent();
     _service            = new ImportProductService();
     _dialogModel        = new ImportProductDialogModel();
     _vm._source         = importProductModelBindingSource;
     _vm._unitSource     = unitBindingSource;
     _vm._supplierSource = supplierBindingSource;
     _vm.Initialize();
     btnImport.Click += BtnImport_Click;
     this.Load       += delegate { _vm.Load(); };
     btnSave.Click   += BtnSave_Click;;
     btnExit.Click   += BtnExit_Click;
     DataBindings.Add("Text", _vm, "Title");
 }
Esempio n. 2
0
        public ImportDataException ProductExists(MappingTable mappingTable, DataRow row)
        {
            if (mappingTable.TableName.ToUpperInvariant() != "INGREDIENT")
            {
                return(null);
            }

            int?starchefKeyOrdinal  = mappingTable.ColumnOrdinalOf("product_id");
            int?supplierNameOrdinal = mappingTable.ColumnOrdinalOf("supplier_id");
            int?supplierCodeOrdinal = mappingTable.ColumnOrdinalOf("supplier_code");

            string starchefKey  = null;
            string supplierName = string.Empty;
            string supplierCode = string.Empty;

            if (starchefKeyOrdinal != null)
            {
                starchefKey = row[(int)starchefKeyOrdinal].ToString().Trim();
            }

            if (supplierNameOrdinal != null)
            {
                supplierName = row[(int)supplierNameOrdinal].ToString();
            }
            if (supplierCodeOrdinal != null)
            {
                supplierCode = row[(int)supplierCodeOrdinal].ToString();
            }

            if (string.IsNullOrWhiteSpace(supplierName))
            {
                supplierName = "";
            }
            if (string.IsNullOrWhiteSpace(supplierCode))
            {
                supplierCode = "|=============|"; // this should never match with the database
            }

            using (var ipService = new ImportProductService(_config.TargetConnectionString))
            {
                if (!string.IsNullOrWhiteSpace(starchefKey) && !ipService.Valid(starchefKey))
                {
                    string columnName  = mappingTable.ColumnNameOf("product_id");
                    string mappingName = mappingTable.MappingNameOf("product_id");
                    return(new ImportDataException
                    {
                        ExceptionType = ExceptionType.NoRecordExistForUpdate,
                        TemplateColumnName = columnName,
                        IsValid = false,
                        TemplateMappingColumn = mappingName
                    });
                }
                else if (string.IsNullOrWhiteSpace(starchefKey) && !ipService.Valid(supplierName, supplierCode))
                {
                    string columnName  = mappingTable.ColumnNameOf("supplier_id") + " or " + mappingTable.ColumnNameOf("supplier_code");
                    string mappingName = mappingTable.MappingNameOf("supplier_id") + mappingTable.MappingNameOf("supplier_code");
                    return(new ImportDataException
                    {
                        ExceptionType = ExceptionType.NoRecordExistForUpdate,
                        TemplateColumnName = columnName,
                        IsValid = false,
                        TemplateMappingColumn = mappingName
                    });
                }

                return(null);
            }
        }
 public ImportProductViewModel()
 {
     _service = new ImportProductService();
 }
Esempio n. 4
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;
                }
            }
        }