public IActionResult Upsert(int?id)
        {
            UserDetails();
            CartCount();
            CollateralVM collateralVM = new CollateralVM()
            {
                Collateral = new Collateral(),
                SizeList   = _unitOfWork.Size.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                }),
                UnitList = _unitOfWork.Unit.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                }),
                BrandList = _unitOfWork.Brand.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                }),
                LocationList = _unitOfWork.Location.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                })
            };

            if (id == null)
            {
                // for create
                return(View(collateralVM));
            }

            collateralVM.Collateral = _unitOfWork.Collateral.Get(id.GetValueOrDefault());
            if (collateralVM.Collateral == null)
            {
                return(NotFound());
            }
            return(View(collateralVM));
        }
        public IActionResult Transfer(int?id)
        {
            UserDetails();
            CartCount();

            CollateralVM collateralVM = new CollateralVM()
            {
                LocationList = _unitOfWork.Location.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                })
            };

            collateralVM.Collateral = _unitOfWork.Collateral.Get(id.GetValueOrDefault());
            SD.collateralName       = collateralVM.Collateral.Location.Name;
            SD.collateralLoc        = collateralVM.Collateral.Name;
            SD.collateralLocId      = collateralVM.Collateral.LocationId;
            return(View(collateralVM));
        }
        public IActionResult Upsert(CollateralVM collateralVM)
        {
            UserDetails();
            CartCount();
            if (ModelState.IsValid)
            {
                string webRootPath = _hostEnvironment.WebRootPath;
                var    files       = HttpContext.Request.Form.Files;
                if (files.Count > 0)
                {
                    string fileName  = Guid.NewGuid().ToString();
                    var    uploads   = Path.Combine(webRootPath, @"img\CorpComm\Collaterals");
                    var    extension = Path.GetExtension(files[0].FileName);

                    if (collateralVM.Collateral.ImgUrl != null)
                    {
                        // this is an edit and we need to remove old image
                        var imagePath = Path.Combine(webRootPath, collateralVM.Collateral.ImgUrl.TrimStart('\\'));
                        if (System.IO.File.Exists(imagePath))
                        {
                            System.IO.File.Delete(imagePath);
                        }
                    }
                    using (var fileStreams = new FileStream(Path.Combine(uploads, fileName + extension), FileMode.Create))
                    {
                        files[0].CopyTo(fileStreams);
                    }
                    collateralVM.Collateral.ImgUrl = @"\img\CorpComm\Collaterals\" + fileName + extension;
                }
                else
                {
                    // only for update when they do not change the image
                    if (collateralVM.Collateral.Id != 0)
                    {
                        Collateral objFromDb = _unitOfWork.Collateral.Get(collateralVM.Collateral.Id);
                        collateralVM.Collateral.ImgUrl = objFromDb.ImgUrl;
                    }
                }

                if (collateralVM.Collateral.Id == 0)
                {
                    _unitOfWork.Collateral.Add(collateralVM.Collateral);
                }
                else
                {
                    _unitOfWork.Collateral.Update(collateralVM.Collateral);
                }
                _unitOfWork.Save();
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                collateralVM.SizeList = _unitOfWork.Size.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                });
                collateralVM.UnitList = _unitOfWork.Unit.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                });
                collateralVM.BrandList = _unitOfWork.Brand.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                });
                collateralVM.LocationList = _unitOfWork.Location.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                });

                if (collateralVM.Collateral.Id != 0)
                {
                    collateralVM.Collateral = _unitOfWork.Collateral.Get(collateralVM.Collateral.Id);
                }
            }
            return(View(collateralVM));
        }
        public IActionResult Transfer(CollateralVM collateralVM, int qtyfrom)
        {
            Collateral objMinusItem = _unitOfWork.Collateral.Get(collateralVM.Collateral.Id);

            objMinusItem.Count -= qtyfrom;

            History histMinus = new History();

            if (collateralVM.Collateral.Id != 0)
            {
                histMinus.LoginUser      = ViewBag.DisplayName;
                histMinus.CollateralId   = objMinusItem.Id;
                histMinus.CollateralName = objMinusItem.Name;
                histMinus.Quantity       = qtyfrom;
                histMinus.RequestDate    = DateTime.Now;;

                if (SD.collateralName == SD.DO_Edsa)
                {
                    histMinus.ReconRemarks = "Transfered (" + qtyfrom + ") to " + SD.DO_LKG;
                }

                if (SD.collateralName == SD.DO_LKG)
                {
                    histMinus.ReconRemarks = "Transfered (" + qtyfrom + ") to " + SD.DO_Edsa;
                }

                _unitOfWork.History.Add(histMinus);
                _unitOfWork.Save();
            }

            if (SD.collateralName == SD.DO_LKG)
            {
                Collateral collateral = _unitOfWork.Collateral.GetFirstOrDefault(c => c.Name == SD.collateralLoc && c.LocationId != SD.collateralLocId);
                collateral.Count += qtyfrom;

                History histPlusEdsa = new History();
                if (collateralVM.Collateral.Id != 0)
                {
                    histPlusEdsa.LoginUser      = ViewBag.DisplayName;
                    histPlusEdsa.CollateralId   = collateral.Id;
                    histPlusEdsa.CollateralName = collateral.Name;
                    histPlusEdsa.Quantity       = qtyfrom;
                    histPlusEdsa.RequestDate    = DateTime.Now;;
                    histPlusEdsa.ReconRemarks   = "Transfered (" + qtyfrom + ") from " + SD.DO_LKG;

                    _unitOfWork.History.Add(histPlusEdsa);
                    _unitOfWork.Save();
                }
            }

            if (SD.collateralName == SD.DO_Edsa)
            {
                Collateral collateral = _unitOfWork.Collateral.GetFirstOrDefault(c => c.Name == SD.collateralLoc && c.LocationId != SD.collateralLocId);
                collateral.Count += qtyfrom;

                History histPlusLKG = new History();
                if (collateralVM.Collateral.Id != 0)
                {
                    histPlusLKG.LoginUser      = ViewBag.DisplayName;
                    histPlusLKG.CollateralId   = collateral.Id;
                    histPlusLKG.CollateralName = collateral.Name;
                    histPlusLKG.Quantity       = qtyfrom;
                    histPlusLKG.RequestDate    = DateTime.Now;;
                    histPlusLKG.ReconRemarks   = "Transfered (" + qtyfrom + ") from " + SD.DO_Edsa;
                    _unitOfWork.History.Add(histPlusLKG);

                    _unitOfWork.Save();
                }

                _unitOfWork.Save();
            }

            return(RedirectToAction(nameof(Index)));
        }