Exemplo n.º 1
0
        protected void btnAddProd_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("procedInsertProd", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@PName", txtProdName.Text);
                cmd.Parameters.AddWithValue("@PPrice", txtProdPrice.Text);
                cmd.Parameters.AddWithValue("@PDescription", txtDesc.Text);
                con.Open();
                Int64 PID = Convert.ToInt64(cmd.ExecuteScalar());


                // Insert Image to db
                if (ProdImg.HasFile)
                {
                    string path = Server.MapPath("~/Images/Products/") + PID;
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    string extention = Path.GetExtension(ProdImg.PostedFile.FileName);
                    ProdImg.SaveAs(path + "\\" + txtProdName.Text.ToString().Trim() + PID + extention);

                    SqlCommand cmd2 = new SqlCommand("procedInsertPic", con);
                    cmd2.CommandType = CommandType.StoredProcedure;
                    cmd2.Parameters.AddWithValue("@PID", PID);
                    cmd2.Parameters.AddWithValue("@Name", txtProdName.Text.ToString().Trim());
                    cmd2.Parameters.AddWithValue("@Extention", extention);
                    cmd2.ExecuteNonQuery();
                }
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> OnPost()
        {
            bool check = _InveTrans.CheckProdCodeExist(ProductInfo.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;

            if (!ModelState.IsValid)
            {
                // return Page();
            }
            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(Page());
                }
            }

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

                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 == ProductInfo.ProdCode);
            bool CreateProdInWh = _InveTrans.CreateProdInWh(Prod.Id, SelectedWarehouse, OpenQty);

            StatusMessage = "تمت اضافة مادة جديدة";
            return(RedirectToPage("Create"));
        }
        public async Task <IActionResult> OnPost()
        {
            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(Page());
                }
            }

            var ProductFromDB = await _db.ProdInfo.FirstOrDefaultAsync(m => m.Id == ProdInfo.Id);

            if (ProductFromDB == null)
            {
                return(NotFound());
            }
            ProductFromDB.ProdCode        = ProdInfo.ProdCode;
            ProductFromDB.ProdName        = ProdInfo.ProdName;
            ProductFromDB.ProdCategory    = ProdInfo.ProdCategory;
            ProductFromDB.ProdDescription = ProdInfo.ProdDescription;
            ProductFromDB.CreatedById     = ProdInfo.CreatedById;
            // ProductFromDB.CreatedDateTime = ProdInfo.CreatedDateTime;
            // ProductFromDB.ImgFile = img.FileName.ToString(); // ProdInfo.ImgFile;
            ProductFromDB.CostPrice   = ProdInfo.CostPrice;
            ProductFromDB.RetailPrice = ProdInfo.RetailPrice;
            ProductFromDB.WholePrice  = ProdInfo.WholePrice;

            TempProdImg v = new TempProdImg
            {
                ImgFile = newfileName
            };

            if (v.ImgFile != null)
            {
                _db.TempProdImg.Add(v);
                await _db.SaveChangesAsync();
            }


            // ImgFile = newfileName;
            AddPhoto = await _db.TempProdImg.ToListAsync();


            AddPhoto = await _db.TempProdImg.ToListAsync();

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

                ProductFromDB.ImgFile = AddPhoto.ImgFile;
            }

            _db.TempProdImg.RemoveRange(AddPhoto);

            await _db.SaveChangesAsync();

            StatusMessage = "تم التعديل على المادة";
            return(RedirectToPage("Index"));
        }
Exemplo n.º 4
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
            }));
        }