public ActionResult OnPostCreateProduct()
        {
            bool check = _InveTrans.CheckProdCodeExist(ProdInfo.ProdCode).GetAwaiter().GetResult();

            if (check == false)
            {
                StatusMessage = "Error!رمز المادة مضافة سابقا";
                return(Page());
            }

            var    ClaimId = (ClaimsIdentity)User.Identity;
            var    Claim   = ClaimId.FindFirst(ClaimTypes.NameIdentifier);
            string UserId  = Claim.Value;


            ProdInfo.CreatedDateTime = DateTime.Now;
            var NewProd = new ProdInfo
            {
                CreatedById     = UserId,
                ProdCode        = ProdInfo.ProdCode,
                ProdDescription = ProdInfo.ProdDescription,
                ProdName        = ProdInfo.ProdName,
                ProdCategory    = ProdInfo.ProdCategory,
                CostPrice       = ProdInfo.CostPrice,
                RetailPrice     = ProdInfo.RetailPrice,
                WholePrice      = ProdInfo.WholePrice,
                CreatedDateTime = ProdInfo.CreatedDateTime
            };

            _db.ProdInfo.Add(NewProd);

            _db.SaveChanges();
            var  Prod           = _db.ProdInfo.FirstOrDefault(pr => pr.ProdCode == ProdInfo.ProdCode);
            bool CreateProdInWh = _InveTrans.CreateProdInWh(Prod.Id, 0, 0);

            StatusMessage = "تمت اضافة مادة جديدة";
            // return new JsonResult(StatusMessage);
            //return RedirectToPage("/Inventory/IncomingGoods/Create");
            return(Page());
        }
Example #2
0
        public async Task <IActionResult> OnPostProduct(ProdInfo prodInfo)
        {
            DateTime InDateTime       = DateTime.Now;
            string   sqlFormattedDate = InDateTime.ToString("yyyy-MM-dd HH:mm:ss");
            bool     check            = _InveTrans.CheckProdCodeExist(prodInfo.ProdCode).GetAwaiter().GetResult();

            if (check == false)
            {
                return(new JsonResult(new
                {
                    success = false,
                    error = "Error!رمز المادة مضافة سابقا"
                }));
            }

            var    ClaimId = (ClaimsIdentity)User.Identity;
            var    Claim   = ClaimId.FindFirst(ClaimTypes.NameIdentifier);
            string UserId  = Claim.Value;

            prodInfo.CreatedById = UserId;

            var  context           = new ValidationContext(prodInfo, serviceProvider: null, items: null);
            var  validationResults = new List <ValidationResult>();
            bool isValid           = Validator.TryValidateObject(prodInfo, context, validationResults, true);

            if (!isValid)
            {
                return(new JsonResult(new
                {
                    success = false,
                    error = validationResults.First().ErrorMessage
                }));
            }

            string newfileName = string.Empty;

            if (img != null && img.Length > 0)
            {
                string fn = img.FileName;
                if (IsImagValidate(fn))
                {
                    string extension = Path.GetExtension(fn);
                    newfileName = Guid.NewGuid().ToString() + extension;
                    string filename = Path.Combine(_host.WebRootPath + "/img/", newfileName);
                    await img.CopyToAsync(new FileStream(filename, FileMode.Create));
                }
                else
                {
                    return(new JsonResult(new
                    {
                        success = false,
                        error = "Invalid Image"
                    }));
                }
            }

            //  prodInfo.CreatedDateTime = DateTime.Now;
            var Information = new ProdInfo
            {
                CreatedById     = UserId,
                ProdCode        = prodInfo.ProdCode,
                ProdDescription = prodInfo.ProdDescription,
                ProdName        = prodInfo.ProdName,
                ProdCategory    = prodInfo.ProdCategory,
                CostPrice       = prodInfo.CostPrice,
                RetailPrice     = prodInfo.RetailPrice,
                WholePrice      = prodInfo.WholePrice,
                CreatedDateTime = InDateTime,

                ImgFile = newfileName
            };

            AddPhoto = await _db.TempProdImg.ToListAsync();

            foreach (var item in AddPhoto)
            {
                var AddPhoto = new ProdImg
                {
                    ImgFile = item.ImgFile,
                    ProdId  = Information.Id
                };
                _db.ProdImg.Add(AddPhoto);
            }
            _db.TempProdImg.RemoveRange(AddPhoto);
            _db.ProdInfo.Add(Information);

            await _db.SaveChangesAsync();

            var  Prod           = _db.ProdInfo.FirstOrDefault(pr => pr.ProdCode == prodInfo.ProdCode);
            bool CreateProdInWh = _InveTrans.CreateProdInWh(Prod.Id, SelectedWarehouse, OpenQty);

            StatusMessage = "تمت اضافة مادة جديدة";
            return(new JsonResult(new
            {
                success = true
            }));
        }