PageRenderTime 1995ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/Ecomerce/Ecomerce/Controllers/MVC/PurchasesController.cs

https://gitlab.com/Andersonrm28/Ecomerce
C# | 239 lines | 219 code | 20 blank | 0 comment | 49 complexity | ca0d5b19a8fb27ec56317d4796776ceb MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using Ecomerce.Models;
  10. using PagedList;
  11. using Ecomerce.Classes;
  12. namespace Ecomerce.Controllers
  13. {
  14. [Authorize(Roles = "User")]
  15. public class PurchasesController : Controller
  16. {
  17. private EcomerceContext db = new EcomerceContext();
  18. public ActionResult AddProduct()
  19. {
  20. var user = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
  21. ViewBag.ProductId = new SelectList(CombosHelper.GetProducts(user.CompanyId, true), "ProductId", "Description");
  22. return PartialView();
  23. }
  24. [HttpPost]
  25. public ActionResult AddProduct(AddProductView view)
  26. {
  27. var user = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
  28. if (ModelState.IsValid)
  29. {
  30. var purchaseDetailTmp = db.PurchaseDetailTmps.Where(odt => odt.UserName == User.Identity.Name && odt.ProductId == view.ProductId).FirstOrDefault();
  31. if (purchaseDetailTmp == null)
  32. {
  33. var product = db.Products.Find(view.ProductId);
  34. purchaseDetailTmp = new PurchaseDetailTmp
  35. {
  36. UserName = User.Identity.Name,
  37. ProductId = view.ProductId,
  38. Description = product.Description,
  39. TaxRate = product.Tax.Rate,
  40. Cost = view.Cost,
  41. Quantity = view.Quantity,
  42. };
  43. db.PurchaseDetailTmps.Add(purchaseDetailTmp);
  44. }
  45. else
  46. {
  47. purchaseDetailTmp.Quantity += view.Quantity;
  48. purchaseDetailTmp.Cost = view.Cost;
  49. db.Entry(purchaseDetailTmp).State = EntityState.Modified;
  50. }
  51. db.SaveChanges();
  52. return RedirectToAction("Create");
  53. }
  54. ViewBag.ProductId = new SelectList(CombosHelper.GetProducts(user.CompanyId), "ProductId", "Description");
  55. return PartialView(view);
  56. }
  57. public ActionResult DeleteProduct(int? id)
  58. {
  59. if (id == null)
  60. {
  61. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  62. }
  63. var purchaseDetailTmp = db.PurchaseDetailTmps.Where(odt => odt.UserName == User.Identity.Name && odt.ProductId == id).FirstOrDefault();
  64. if (purchaseDetailTmp == null)
  65. {
  66. return HttpNotFound();
  67. }
  68. db.PurchaseDetailTmps.Remove(purchaseDetailTmp);
  69. db.SaveChanges();
  70. return RedirectToAction("Create");
  71. }
  72. public ActionResult Index(int? page = null)
  73. {
  74. page = (page ?? 1);
  75. var user = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
  76. var purchases = db.Purchases.Where(p => p.CompanyId == user.CompanyId).Include(p => p.State).Include(p => p.Supplier).Include(p => p.Warehouse);
  77. return View(purchases.ToList().ToPagedList((int)page, 5));
  78. }
  79. public ActionResult Details(int? id)
  80. {
  81. if (id == null)
  82. {
  83. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  84. }
  85. var purchase = db.Purchases.Find(id);
  86. if (purchase == null)
  87. {
  88. return HttpNotFound();
  89. }
  90. return View(purchase);
  91. }
  92. public ActionResult Create()
  93. {
  94. var user = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
  95. ViewBag.SupplierId = new SelectList(CombosHelper.GetSuppliers(user.CompanyId), "SupplierId", "FullName");
  96. ViewBag.WarehouseId = new SelectList(CombosHelper.GetWarehouses(user.CompanyId), "WarehouseId", "Name");
  97. var view = new NewPurchaseView
  98. {
  99. Date = DateTime.Now,
  100. Details = db.PurchaseDetailTmps.Where(pdt => pdt.UserName == User.Identity.Name).ToList(),
  101. };
  102. return View(view);
  103. }
  104. [HttpPost]
  105. [ValidateAntiForgeryToken]
  106. public ActionResult Create(NewPurchaseView view)
  107. {
  108. if (ModelState.IsValid)
  109. {
  110. var details = db.PurchaseDetailTmps.Where(pdt => pdt.UserName == User.Identity.Name).ToList();
  111. if (details.Count() != 0)
  112. {
  113. var response = MovementsHelper.NewPurchase(view, User.Identity.Name);
  114. if (response.Succeeded)
  115. {
  116. return RedirectToAction("Index");
  117. }
  118. ModelState.AddModelError(string.Empty, response.Message);
  119. }
  120. else
  121. {
  122. ModelState.AddModelError(string.Empty, "There are no products");
  123. }
  124. }
  125. var user = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
  126. ViewBag.SupplierId = new SelectList(CombosHelper.GetSuppliers(user.CompanyId), "SupplierId", "FullName");
  127. ViewBag.WarehouseId = new SelectList(CombosHelper.GetWarehouses(user.CompanyId), "WarehouseId", "Name");
  128. view.Details = db.PurchaseDetailTmps.Where(pdt => pdt.UserName == User.Identity.Name).ToList();
  129. return View(view);
  130. }
  131. public ActionResult Edit(int? id)
  132. {
  133. if (id == null)
  134. {
  135. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  136. }
  137. var purchase = db.Purchases.Find(id);
  138. if (purchase == null)
  139. {
  140. return HttpNotFound();
  141. }
  142. var user = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
  143. ViewBag.SupplierId = new SelectList(CombosHelper.GetSuppliers(user.CompanyId), "SupplierId", "FullName", purchase.SupplierId);
  144. ViewBag.WarehouseId = new SelectList(CombosHelper.GetWarehouses(user.CompanyId), "WarehouseId", "Name", purchase.WarehouseId);
  145. return View(purchase);
  146. }
  147. [HttpPost]
  148. [ValidateAntiForgeryToken]
  149. public ActionResult Edit(Purchase purchase)
  150. {
  151. if (ModelState.IsValid)
  152. {
  153. db.Entry(purchase).State = EntityState.Modified;
  154. db.SaveChanges();
  155. return RedirectToAction("Index");
  156. }
  157. ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "Name", purchase.CompanyId);
  158. ViewBag.StateId = new SelectList(db.States, "StateId", "Description", purchase.StateId);
  159. ViewBag.SupplierId = new SelectList(db.Suppliers, "SupplierId", "UserName", purchase.SupplierId);
  160. ViewBag.WarehouseId = new SelectList(db.Warehouses, "WarehouseId", "Name", purchase.WarehouseId);
  161. return View(purchase);
  162. }
  163. public ActionResult Delete(int? id)
  164. {
  165. if (id == null)
  166. {
  167. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  168. }
  169. var purchase = db.Purchases.Find(id);
  170. if (purchase == null)
  171. {
  172. return HttpNotFound();
  173. }
  174. return View(purchase);
  175. }
  176. [HttpPost, ActionName("Delete")]
  177. [ValidateAntiForgeryToken]
  178. public ActionResult DeleteConfirmed(int id)
  179. {
  180. var purchase = db.Purchases.Find(id);
  181. var user = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
  182. var purchaseDetails = db.PurchaseDetails.Where(pd => pd.PurchaseId == purchase.PurchaseId).ToList();
  183. using (var transaction = db.Database.BeginTransaction())
  184. {
  185. foreach (var item in purchaseDetails)
  186. {
  187. var inventorie = db.Inventories.Where(i => i.WarehouseId == purchase.WarehouseId && i.ProductId == item.ProductId).FirstOrDefault();
  188. if (inventorie != null)
  189. {
  190. inventorie.Stock -= item.Quantity;
  191. db.Entry(inventorie).State = EntityState.Modified;
  192. }
  193. db.PurchaseDetails.Remove(item);
  194. }
  195. db.SaveChanges();
  196. db.Purchases.Remove(purchase);
  197. var response = DBHelper.SaveChanges(db);
  198. if (response.Succeeded)
  199. {
  200. transaction.Commit();
  201. return RedirectToAction("Index");
  202. }
  203. transaction.Rollback();
  204. ModelState.AddModelError(string.Empty, response.Message);
  205. db.Entry(purchase).State = EntityState.Added;
  206. purchase.PurchaseDetails = purchaseDetails;
  207. return View(purchase);
  208. }
  209. }
  210. protected override void Dispose(bool disposing)
  211. {
  212. if (disposing)
  213. {
  214. db.Dispose();
  215. }
  216. base.Dispose(disposing);
  217. }
  218. }
  219. }