コード例 #1
0
        public ActionResult CancelAll()
        {
            IUnDoneStockService dss = new UnDoneStockService(Settings.Default.db);

            dss.SetStateCancel(PartType.Product);

            return(RedirectToAction("Index"));
        }
コード例 #2
0
        public ActionResult ImportUnDoneStock(HttpPostedFileBase undoneStock)
        {
            int excelStartFromLine = 9;
            int csvStartFromLine   = 13;

            if (undoneStock == null)
            {
                throw new Exception("No file is uploaded to system");
            }

            var appData  = Server.MapPath("~/TmpFile/");
            var filename = Path.Combine(appData,
                                        DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(undoneStock.FileName));

            undoneStock.SaveAs(filename);
            string ex = Path.GetExtension(filename);

            List <UnDoneStockImportModel> records = new List <UnDoneStockImportModel>();
            IUnDoneStockService           uss     = new UnDoneStockService(Settings.Default.db);

            if (Path.GetExtension(filename).Equals(".xlsx"))
            {
                FileInfo file = new FileInfo(filename);
                using (ExcelPackage ep = new ExcelPackage(file))
                {
                    ExcelWorksheet ws = ep.Workbook.Worksheets.First();

                    for (int i = excelStartFromLine; i <= ws.Dimension.End.Row; i++)
                    {
                        int feedback = int.Parse(ws.Cells[i, 17].Value.ToString());
                        if (feedback == 0)
                        {
                            records.Add(new UnDoneStockImportModel()
                            {
                                KanbanNr   = ws.Cells[i, 13].Value.ToString(),
                                Quantity   = ws.Cells[i, 16].Value.ToString(),
                                SourceType = 1
                            });
                        }
                    }
                }
                uss.SetStateCancel(PartType.BlueCard);
            }
            else if (Path.GetExtension(filename).Equals(".csv"))
            {
                CsvConfiguration configration = new CsvConfiguration();
                configration.Delimiter        = Settings.Default.csvDelimiter;
                configration.HasHeaderRecord  = true;
                configration.SkipEmptyRecords = true;
                configration.RegisterClassMap <UnDoneStockCsvModelMap>();
                configration.TrimHeaders = true;
                configration.TrimFields  = true;

                using (TextReader treader = System.IO.File.OpenText(filename))
                {
                    for (int i = 0; true; i++)
                    {
                        string s = treader.ReadLine();

                        if (i >= csvStartFromLine)
                        {
                            if (string.IsNullOrWhiteSpace(s))
                            {
                                break;
                            }

                            string[] fields = s.Split(char.Parse(Settings.Default.csvDelimiter));

                            try
                            {
                                records.Add(new UnDoneStockImportModel()
                                {
                                    KanbanNr   = fields[6],
                                    Quantity   = fields[5],
                                    SourceType = 2
                                });
                            }
                            catch (Exception)
                            {
                                break;
                            }
                        }
                    }
                }
                uss.SetStateCancel(PartType.WhiteCard);
            }

            List <UnDoneStockRecord> usr = new List <UnDoneStockRecord>();

            foreach (UnDoneStockImportModel r in records)
            {
                usr.Add(new UnDoneStockRecord()
                {
                    PartNr     = r.KanbanNr,
                    KanbanNr   = r.KanbanNr,
                    Quantity   = r.CutQuantity,
                    SourceType = r.SourceType
                });
            }

            if (records.Count > 0)
            {
                Hashtable results = uss.ValidateUnDoneStock(usr);

                if (results.ContainsKey("SUCCESS"))
                {
                    foreach (UnDoneStockRecord u in results["SUCCESS"] as List <UnDoneStockRecord> )
                    {
                        UnDoneStock uds = new UnDoneStock()
                        {
                            partNr     = u.PartNr,
                            quantity   = u.Quantity,
                            kanbanNr   = u.KanbanNr,
                            sourceType = u.SourceType,
                            state      = u.State,
                            createdAt  = DateTime.Now,
                            updatedAt  = DateTime.Now
                        };

                        uss.Create(uds);
                    }
                }

                if (results.ContainsKey("WARN"))
                {
                    ViewBag.Msg = "Has Warning!";

                    return(View(results["WARN"] as List <UnDoneStockRecord>));
                }
                else
                {
                    ViewBag.Msg = "Finish Success!";

                    return(View());
                }
            }
            else
            {
                ViewBag.Msg = "No Record!";

                return(View());
            }
        }