public ActionResult Create()
        {
            var stationeryDto = new StationeryDTO
            {
                CategoryList = _categoryRepo.GetCategories(),
                SupplierList = _supplierRepo.GetSupplierList()
            };

            return(View(stationeryDto));
        }
        public ActionResult Edit(string id) //Edit in MVC architecture
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var st = _stationeryRepo.GetById(id);

            if (st == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var nDto = new StationeryDTO
            {
                SupplierList  = _supplierRepo.GetSupplierList(),
                CategoryList  = _categoryRepo.GetCategories(),
                ItemNum       = id,
                BinNum        = st.BinNum,
                CategoryId    = st.CategoryId,
                Description   = st.Description,
                ReorderLevel  = st.ReorderLevel,
                ReorderQty    = st.ReorderQty,
                UnitOfMeasure = st.UnitOfMeasure
            };
            var ss1 = _stationerySupplierRepo.GetSSByIdRank(id, 1);

            nDto.SupplierName1 = ss1.SupplierId.ToString();
            nDto.Price1        = ss1.Price;
            var ss2 = _stationerySupplierRepo.GetSSByIdRank(id, 2);

            nDto.SupplierName2 = ss2.SupplierId.ToString();
            nDto.Price2        = ss2.Price;
            var ss3 = _stationerySupplierRepo.GetSSByIdRank(id, 3);

            nDto.SupplierName3 = ss3.SupplierId.ToString();
            nDto.Price3        = ss3.Price;

            return(View(nDto));
        }
        public async Task <IHttpActionResult> GetStationery(string id)
        {
            var stationery = await _stationeryRepo.GetByIdAsync(id);

            if (stationery == null)
            {
                return(NotFound());
            }

            var dto = new StationeryDTO()
            {
                Description   = stationery.Description,
                BinNum        = stationery.BinNum,
                AvailableQty  = stationery.AvailableQty,
                Category      = stationery.Category.CategoryName,
                ItemNum       = stationery.ItemNum,
                ReorderLevel  = stationery.ReorderLevel,
                ReorderQty    = stationery.ReorderQty,
                UnitOfMeasure = stationery.UnitOfMeasure
            };

            return(Ok(dto));
        }
        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(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));
        }