Exemple #1
0
            //method to convert the excel format back to c# list class
            public static List <StationerySupplier> ConvertToList(Stream stream)
            {
                List <StationerySupplier> list = new List <StationerySupplier>();

                try
                {
                    var excel      = new ExcelPackage(stream);
                    var ws         = excel.Workbook.Worksheets.First();
                    int currentRow = 2;
                    while (ws.Cells[currentRow, 1].Value != null)
                    {
                        var stationerySupplier = new StationerySupplier
                        {
                            ItemNum    = (string)ws.Cells[currentRow, 1].Value,
                            Rank       = Convert.ToInt32(ws.Cells[currentRow, 4].Value),
                            Price      = Convert.ToDouble(ws.Cells[currentRow, 3].Value),
                            SupplierId = Convert.ToInt32(ws.Cells[currentRow, 5].Value)
                        };
                        list.Add(stationerySupplier);
                        currentRow++;
                    }
                }
                catch (Exception)
                {
                    throw new Exception("Problems reading uploaded file, please follow template provided");
                }

                return(list);
            }
        public ActionResult Create(StationeryDTO stationeryDto) //Create in MVC architecture
        {
            if (ModelState.IsValid)
            {
                //This is to check if supplier are unique
                var theList = new List <string>
                {
                    stationeryDto.SupplierName1,
                    stationeryDto.SupplierName2,
                    stationeryDto.SupplierName3
                };
                var isUnique = theList.Distinct().Count() == theList.Count();
                if (isUnique == false)
                {
                    ViewBag.DistinctError      = "Please select different suppliers";
                    stationeryDto.CategoryList = _categoryRepo.GetCategories();
                    stationeryDto.SupplierList = _supplierRepo.GetSupplierList();
                    return(View(stationeryDto));
                }

                var selectedCategory = _categoryRepo.GetById(stationeryDto.CategoryId);
                var initial          = selectedCategory.CategoryName.Substring(0, 1);
                var number           = _stationeryRepo.GetLastRunningPlusOne(initial).ToString();
                var generatedItemNum = initial + number.PadLeft(3, '0');
                var st = new Stationery
                {
                    ItemNum       = generatedItemNum,
                    CategoryId    = stationeryDto.CategoryId,
                    Description   = stationeryDto.Description,
                    ReorderLevel  = stationeryDto.ReorderLevel,
                    ReorderQty    = stationeryDto.ReorderQty,
                    AverageCost   = 0,
                    UnitOfMeasure = stationeryDto.UnitOfMeasure,
                    CurrentQty    = 0,
                    BinNum        = stationeryDto.BinNum,
                    AvailableQty  = 0
                };
                _stationeryRepo.Add(st);

                var sp1 = new StationerySupplier
                {
                    ItemNum    = generatedItemNum,
                    SupplierId = int.Parse(stationeryDto.SupplierName1),
                    Price      = stationeryDto.Price1,
                    Rank       = 1
                };
                _stationerySupplierRepo.Add(sp1);


                var sp2 = new StationerySupplier
                {
                    ItemNum    = generatedItemNum,
                    SupplierId = int.Parse(stationeryDto.SupplierName2),
                    Price      = stationeryDto.Price2,
                    Rank       = 2
                };
                _stationerySupplierRepo.Add(sp2);

                var sp3 = new StationerySupplier
                {
                    ItemNum    = generatedItemNum,
                    SupplierId = int.Parse(stationeryDto.SupplierName3),
                    Price      = stationeryDto.Price3,
                    Rank       = 3
                };
                _stationerySupplierRepo.Add(sp3);
                return(RedirectToAction("Index"));
            }

            stationeryDto.CategoryList = _categoryRepo.GetCategories();
            stationeryDto.SupplierList = _supplierRepo.GetSupplierList();
            return(View(stationeryDto));
        }
        public ActionResult Edit(StationeryDTO stationeryDto) //Edit in MVC architecture
        {
            if (ModelState.IsValid)
            {
                var theList = new List <string>
                {
                    stationeryDto.SupplierName1,
                    stationeryDto.SupplierName2,
                    stationeryDto.SupplierName3
                };
                var isUnique = theList.Distinct().Count() == theList.Count();
                if (isUnique == false)
                {
                    //Check Supplier is different
                    ViewBag.DistinctError      = "Please select different suppliers";
                    stationeryDto.CategoryList = _categoryRepo.GetCategories();
                    stationeryDto.SupplierList = _supplierRepo.GetSupplierList();
                    return(View(stationeryDto));
                }

                var stationery = _stationeryRepo.GetById(stationeryDto.ItemNum);
                stationery.CategoryId    = stationeryDto.CategoryId;
                stationery.Description   = stationeryDto.Description;
                stationery.ReorderLevel  = stationeryDto.ReorderLevel;
                stationery.ReorderQty    = stationeryDto.ReorderQty;
                stationery.UnitOfMeasure = stationeryDto.UnitOfMeasure;
                stationery.BinNum        = stationeryDto.BinNum;
                _stationeryRepo.Update(stationery);
                _stationerySupplierRepo.DeleteStationerySupplier(stationeryDto.ItemNum);

                var sp1 = new StationerySupplier
                {
                    ItemNum    = stationeryDto.ItemNum,
                    SupplierId = Int32.Parse(stationeryDto.SupplierName1),
                    Price      = stationeryDto.Price1,
                    Rank       = 1
                };
                _stationerySupplierRepo.Add(sp1);

                var sp2 = new StationerySupplier
                {
                    ItemNum    = stationeryDto.ItemNum,
                    SupplierId = int.Parse(stationeryDto.SupplierName2),
                    Price      = stationeryDto.Price2,
                    Rank       = 2
                };
                _stationerySupplierRepo.Add(sp2);

                var sp3 = new StationerySupplier
                {
                    ItemNum    = stationeryDto.ItemNum,
                    SupplierId = Int32.Parse(stationeryDto.SupplierName3),
                    Price      = stationeryDto.Price3,
                    Rank       = 3
                };
                _stationerySupplierRepo.Add(sp3);

                return(RedirectToAction("Index"));
            }

            stationeryDto.CategoryList = _categoryRepo.GetCategories();
            stationeryDto.SupplierList = _supplierRepo.GetSupplierList();
            return(View(stationeryDto));
        }
        public ActionResult Create(int?supplierId, string error = null)
        {
            //catch error from redirect (from POST) and display back into page
            ViewBag.Error = error;

            if (supplierId == null) //allow user to select supplier if non is chosen yet
            {
                var emptySupplier = new Supplier
                {
                    SupplierId   = -1,
                    SupplierName = "Select a Supplier"
                };

                ViewBag.Suppliers = _supplierRepo.GetAll().Concat(new[] { emptySupplier });
                ViewBag.Supplier  = emptySupplier;

                var emptyPo = new PurchaseOrderDTO {
                    SupplierId = -1
                };
                return(View(emptyPo));
            }

            //get supplier
            var supplier = _supplierRepo.GetById(Convert.ToInt32(supplierId));
            var po       = new PurchaseOrderDTO()
            {   //view model
                Supplier        = supplier,
                SupplierId      = supplier.SupplierId,
                CreateDate      = DateTime.Today,
                SupplierAddress = supplier.Address1 + Environment.NewLine
                                  + supplier.Address2 + Environment.NewLine
                                  + supplier.Address3,
                SupplierContact = supplier.ContactName
            };

            //set empty Stationery template for dropdown
            var emptyStationery = new Stationery
            {
                ItemNum       = "select a stationery",
                Description   = "select a stationery",
                UnitOfMeasure = "-",
                AverageCost   = 0.00
            };

            //get list of recommended for purchase stationery and put in purchase order details
            foreach (KeyValuePair <Supplier, List <Stationery> > kvp in _stationeryRepo.GetOutstandingStationeryByAllSupplier())
            {
                if (kvp.Key.SupplierId == supplierId)
                {
                    foreach (var stationery in kvp.Value)
                    {
                        PurchaseOrderDetailDTO pdetails = new PurchaseOrderDetailDTO
                        {
                            OrderQty = Math.Max(Convert.ToInt32(stationery.ReorderLevel - stationery.AvailableQty),
                                                Convert.ToInt32(stationery.ReorderQty)),
                            UnitPrice  = stationery.UnitPrice(Convert.ToInt32(supplierId)),
                            ItemNum    = stationery.ItemNum,
                            ReorderQty = stationery.ReorderQty
                        };
                        po.PurchaseOrderDetailsDTO.Add(pdetails);
                    }
                    break;
                }
            }

            //no of purchase detail lines to show
            var countOfLines = Math.Max(po.PurchaseOrderDetailsDTO.Count, 1);
            //no ofstationery that belong to supplier
            var countOfStationery = Math.Max(po.PurchaseOrderDetailsDTO.Count, 0);

            //create empty puchase details so user can add up to 100 line items per PO
            for (int i = countOfLines; i < 100; i++)
            {
                var pdetails = new PurchaseOrderDetailDTO
                {
                    OrderQty  = 0,
                    UnitPrice = emptyStationery.AverageCost,
                    ItemNum   = emptyStationery.ItemNum
                };
                po.PurchaseOrderDetailsDTO.Add(pdetails);
            }

            //fill ViewBag to populate stationery dropdown lists
            var stationerySupplier = new StationerySupplier
            {
                ItemNum    = emptyStationery.ItemNum,
                Price      = emptyStationery.AverageCost,
                Stationery = emptyStationery
            };
            var sslist = new List <StationerySupplier> {
                stationerySupplier
            };

            sslist.AddRange(_stationerySupplierRepo.GetStationerySupplierBySupplierId(supplierId).ToList());
            ViewBag.Stationery        = sslist;
            ViewBag.Suppliers         = _supplierRepo.GetAll();
            ViewBag.Supplier          = supplier;
            ViewBag.countOfStationery = countOfStationery;
            ViewBag.countOfLines      = countOfLines;
            ViewBag.GST_RATE          = GstRate;

            return(View(po));
        }