public ActionResult AllocatedProductsBoxEdit(int? productId, string DateAccountability, string amount, int vendorId = 0) { Users LoggedUser = Login.GetLoggedUser(); if (LoggedUser == null) { return View("NoPermission"); } if (LoggedUser.Role == Roles.Vendedor) { return View("NoPermission"); } #region Validation Validation validation = new Validation(); bool error = false; string DataError = null; try { validation.DateNotToday(DateAccountability); } catch (Exception ex) { DataError = ex.Message; error = true; } string AmountError = null; try { validation.Amount(amount); } catch (Exception ex) { AmountError = ex.Message; error = true; } string InventoryError = null; ProductsAllocated productToEdite = ProductsAllocated.TryFind(productId); if (productToEdite.Product.Amount - (int.Parse(amount) - productToEdite.Amount) < 0) { InventoryError = "Item indisponivel no estoque"; error = true; } #endregion Validation if (!error) { productToEdite.Product.Amount = productToEdite.Product.Amount - (int.Parse(amount) - productToEdite.Amount); productToEdite.DateAccountability = DateTime.Parse(DateAccountability); productToEdite.Product.Save(); productToEdite.Amount = int.Parse(amount); productToEdite.Save(); ViewBag.SaveSuccess = "Salvo com Sucesso"; } ViewBag.DataError = DataError; ViewBag.AmountError = AmountError; ViewBag.InventoryError = InventoryError; ViewBag.vendors = Users.Queryable.Where(i => i.UserBoss == LoggedUser).ToList(); ViewBag.vendorId = vendorId; return View("EditAllocatedProducts", new { vendorId = vendorId }); }
public ActionResult AllocateProducts(string vendorid, List<AlocatteProductForm> alocateForm) { Users LoggedUser = Login.GetLoggedUser(); if (LoggedUser == null) { return View("NoPermission"); } if (LoggedUser.Role == Roles.Vendedor) { return View("NoPermission"); } Validation validattion = new Validation(); List<string> productError = new List<string>(); string vendorError = null; List<string> AmountError = new List<string>(); List<string> ComissionError = new List<string>(); List<string> DataError = new List<string>(); List<string> formError = new List<string>(); List<int> boxErrorNumber = new List<int>(); for (int i = 0; i < alocateForm.Count; i++) { productError.Add(""); AmountError.Add(""); ComissionError.Add(""); DataError.Add(""); formError.Add(""); } alocateForm.RemoveAll(i => i.DateAccountability <= DateTime.MinValue || i.Amount == 0 || i.ProductId == 0 || i.Comission == ""); foreach (var item in alocateForm) { if (Products.Queryable.Where(i => i.User == LoggedUser && i.Id == item.ProductId).FirstOrDefault() == null) { productError[item.BoxNumber] = "Produto não encontrado"; boxErrorNumber.Add(item.BoxNumber); } try { validattion.Commission(item.Comission.ToString(CultureInfo.InvariantCulture).Replace(',', '.')); } catch (Exception ex) { ComissionError[item.BoxNumber] = ex.Message; } if (!string.IsNullOrEmpty(ComissionError[item.BoxNumber])) boxErrorNumber.Add(item.BoxNumber); else { item.Comission = item.Comission.ToString(CultureInfo.InvariantCulture).Replace(',', '.'); } try { validattion.DateNotToday(item.DateAccountability.ToString()); } catch (Exception ex) { DataError[item.BoxNumber] = ex.Message; } if (!string.IsNullOrEmpty(DataError[item.BoxNumber])) boxErrorNumber.Add(item.BoxNumber); item.Product = Products.Queryable.Where(i => i.User == LoggedUser && i.Id == item.ProductId).FirstOrDefault(); } Users vendor = new Users(); if (string.IsNullOrEmpty(vendorid)) vendorError = "Vendedor Invalido"; else { vendor = Users.Queryable.Where(i => i.UserBoss == LoggedUser && i.Id == int.Parse(vendorid)).FirstOrDefault(); if (vendor == null) { vendorError = "Vendedor Invalido"; } } foreach (var productItem in alocateForm) { if (productItem.Product.Amount < productItem.Amount || productItem.Amount <= 0) { AmountError[productItem.BoxNumber] = "Quantidade indisponivel: " + productItem.Product.Name + " Estq: " + productItem.Product.Amount; boxErrorNumber.Add(productItem.BoxNumber); } if (string.IsNullOrEmpty(productError[productItem.BoxNumber]) && string.IsNullOrEmpty(vendorError) && string.IsNullOrEmpty(AmountError[productItem.BoxNumber]) && string.IsNullOrEmpty(formError[productItem.BoxNumber]) && string.IsNullOrEmpty(DataError[productItem.BoxNumber])) { ProductsAllocated existingAlocate = ProductsAllocated.Queryable.Where(i => i.Product == productItem.Product && i.Vendor == vendor && i.Price == productItem.Product.Price && i.Cost == productItem.Product.Cost && i.DateAccountability == productItem.DateAccountability && i.Commision == double.Parse(productItem.Comission, CultureInfo.InvariantCulture)).FirstOrDefault(); productItem.Product.Amount -= productItem.Amount; if (existingAlocate == null) { ProductsAllocated productAlocate = new ProductsAllocated { Product = productItem.Product, Vendor = vendor, Amount = productItem.Amount, Cost = productItem.Product.Cost, Price = productItem.Product.Price, DateAccountability = productItem.DateAccountability, Commision = double.Parse(productItem.Comission, CultureInfo.InvariantCulture) }; productAlocate.Save(); } else { existingAlocate.Amount += productItem.Amount; existingAlocate.Update(); } ViewBag.SaveSuccess = "Produto alocado com sucesso"; productItem.Product.Save(); } } List<Products> listproducts = Products.Queryable.Where(i => i.User == LoggedUser).ToList(); List<Users> vendors = Users.Queryable.Where(i => i.UserBoss == LoggedUser).ToList(); ViewBag.Products = listproducts; ViewBag.Vendors = vendors; List<AlocatteProductForm> boxWithError = new List<AlocatteProductForm>(); //errors foreach (var item in boxErrorNumber.Distinct()) { boxWithError.Add(alocateForm.Where(i => i.BoxNumber == item).FirstOrDefault()); } ViewBag.boxWithError = boxWithError; ViewBag.productError = productError; ViewBag.vendorError = vendorError; ViewBag.AmountError = AmountError; ViewBag.FormError = formError; ViewBag.DataError = DataError; ViewBag.ComissionError = ComissionError; if (boxWithError.Count() > 0) { ViewBag.Vendor = vendor; } return View("AllocateProduct"); }