CraftController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Threading.Tasks;
  6. using GreenTree.Strohrmann.ERP.Core.Domain.Business;
  7. using GreenTree.Strohrmann.ERP.Core.Extension;
  8. using GreenTree.Strohrmann.ERP.Core.Helper;
  9. using GreenTree.Strohrmann.ERP.Domain.Extension;
  10. using GreenTree.Strohrmann.ERP.Domain.Model;
  11. using GreenTree.Strohrmann.ERP.Services.Geolocator;
  12. using GreenTree.Strohrmann.ERP.Web.Extension;
  13. using GreenTree.Strohrmann.ERP.Web.Models.Business;
  14. using GreenTree.Strohrmann.ERP.Web.Models.Rights.User;
  15. using GreenTree.Strohrmann.ERP.Web.Validators;
  16. using Microsoft.AspNetCore.Http;
  17. using Microsoft.AspNetCore.Mvc;
  18. namespace GreenTree.Strohrmann.ERP.Web.Controllers
  19. {
  20. public class CraftController : Controller
  21. {
  22. #region DI fields
  23. // The global DbContext
  24. private readonly ERPDbContext _eRPDbContext;
  25. #endregion
  26. #region Ctor
  27. /// <summary>
  28. /// Initializes a new instance of the CraftController class
  29. /// </summary>
  30. /// <param name="eRPDbContext">Global DbContext.</param>
  31. public CraftController(ERPDbContext eRPDbContext)
  32. {
  33. _eRPDbContext = eRPDbContext;
  34. }
  35. #endregion
  36. #region Actions
  37. // GET: CraftController
  38. public ActionResult Index()
  39. {
  40. var crafts = _eRPDbContext.Crafts
  41. .ToList()
  42. .Select(c => new CraftModel(c));
  43. return View(crafts);
  44. }
  45. // GET: CraftController/Details/5
  46. public ActionResult Details(int id)
  47. {
  48. var craft = _eRPDbContext.Crafts
  49. .FirstOrDefault(c => c.Id == id);
  50. var craftModel = new CraftModel(craft);
  51. return View(craftModel);
  52. }
  53. // GET: CraftController/Create
  54. public ActionResult Create()
  55. {
  56. AddEmployeeListForSelection();
  57. AddMaterialListForSelection();
  58. return View();
  59. }
  60. // POST: CraftController/Create
  61. [HttpPost]
  62. [ValidateAntiForgeryToken]
  63. public ActionResult Create(CraftModel model)
  64. {
  65. if (!ModelState.IsValid)
  66. {
  67. AddEmployeeListForSelection();
  68. AddMaterialListForSelection();
  69. foreach (var item in model.CraftMaterials)
  70. {
  71. if (item.Value.Amount % 1 != 0)
  72. item.Value.Amount /= 10;
  73. item.Value.CalculationFactor = item.Value.CalculationFactor / 100;
  74. }
  75. return View(model);
  76. }
  77. var craft = new Craft
  78. {
  79. Name = model.Name,
  80. CreationDate = model.CreationDate,
  81. Customer = _eRPDbContext.Customers.Find(model.CustomerId),
  82. CreatedBy = User.Identity.Name,
  83. CreatedOn = DateTime.Now
  84. };
  85. craft.CraftEmployees = new List<CraftEmployee>(
  86. model.CraftEmployees
  87. .Select(ce => new CraftEmployee
  88. {
  89. Craft = craft,
  90. Employee = _eRPDbContext.Employees.Find(ce.Value.EmployeeId),
  91. Amount = ce.Value.Amount,
  92. Value = Convert.ToDecimal(ce.Value.Value),
  93. Comment = ce.Value.Comment
  94. }));
  95. craft.CraftMaterials = new List<CraftMaterial>(
  96. model.CraftMaterials
  97. .GroupBy(cm => cm.Value.MaterialId)
  98. .Select(cm => new CraftMaterial
  99. {
  100. Craft = craft,
  101. Material = _eRPDbContext.Materials.Find(cm.Key),
  102. Amount = cm.Sum(v => v.Value.Amount),
  103. CalculationFactor = cm.Average(v => v.Value.CalculationFactor / 100),
  104. Value = cm.Sum(v => Convert.ToDecimal(v.Value.Value))
  105. }));
  106. _eRPDbContext.Crafts.Add(craft);
  107. _eRPDbContext.SaveChanges();
  108. return RedirectToAction(nameof(Index));
  109. }
  110. // GET: CraftController/Edit/5
  111. public ActionResult Edit(int id)
  112. {
  113. AddEmployeeListForSelection();
  114. AddMaterialListForSelection();
  115. var craft = _eRPDbContext.Crafts
  116. .FirstOrDefault(c => c.Id == id);
  117. var craftModel = new CraftModel(craft);
  118. return View(craftModel);
  119. }
  120. // POST: CraftController/Edit/5
  121. [HttpPost]
  122. [ValidateAntiForgeryToken]
  123. public ActionResult Edit(int id, CraftModel model)
  124. {
  125. if (!ModelState.IsValid)
  126. {
  127. AddEmployeeListForSelection();
  128. AddMaterialListForSelection();
  129. foreach (var item in model.CraftMaterials)
  130. {
  131. if (item.Value.Amount % 1 != 0)
  132. item.Value.Amount /= 10;
  133. item.Value.CalculationFactor = item.Value.CalculationFactor / 100;
  134. }
  135. return View(model);
  136. }
  137. var craft = _eRPDbContext.Crafts
  138. .FirstOrDefault(u => u.Id == id);
  139. craft.Name = model.Name;
  140. craft.CreationDate = model.CreationDate;
  141. craft.Customer = _eRPDbContext.Customers.Find(model.CustomerId);
  142. craft.ChangedBy = User.Identity.Name;
  143. craft.ChangedOn = DateTime.Now;
  144. craft.CraftEmployees.Clear();
  145. craft.CraftMaterials.Clear();
  146. craft.CraftEmployees = new List<CraftEmployee>(
  147. model.CraftEmployees
  148. .Select(ce => new CraftEmployee
  149. {
  150. Craft = craft,
  151. Employee = _eRPDbContext.Employees.Find(ce.Value.EmployeeId),
  152. Amount = ce.Value.Amount,
  153. Value = Convert.ToDecimal(ce.Value.Value),
  154. Comment = ce.Value.Comment
  155. }));
  156. craft.CraftMaterials = new List<CraftMaterial>(
  157. model.CraftMaterials
  158. .GroupBy(cm => cm.Value.MaterialId)
  159. .Select(cm => new CraftMaterial
  160. {
  161. Craft = craft,
  162. Material = _eRPDbContext.Materials.Find(cm.Key),
  163. Amount = cm.Sum(v => v.Value.Amount),
  164. CalculationFactor = cm.Average(v => v.Value.CalculationFactor / 100),
  165. Value = cm.Sum(v => Convert.ToDecimal(v.Value.Value))
  166. }));
  167. _eRPDbContext.SaveChanges();
  168. return RedirectToAction(nameof(Index));
  169. }
  170. // POST: CraftController/Delete/5
  171. [HttpPost]
  172. [ValidateAntiForgeryToken]
  173. public ActionResult Delete(int id, IFormCollection collection)
  174. {
  175. var craft = _eRPDbContext.Crafts
  176. .FirstOrDefault(c => c.Id == id);
  177. _eRPDbContext.Crafts.Remove(craft);
  178. _eRPDbContext.SaveChanges();
  179. return RedirectToAction(nameof(Index));
  180. }
  181. #endregion
  182. #region Partials
  183. // GET: CraftController/AddCraftEmployeePartial
  184. public ActionResult AddCraftEmployeePartial(CraftEmployeeModel craftEmployeeModel)
  185. {
  186. AddEmployeeListForSelection();
  187. ModelState.Clear();
  188. return PartialView("~/Views/Craft/_CraftEmployeePartial.cshtml", craftEmployeeModel);
  189. }
  190. // GET: CraftController/AddCraftMaterialPartial
  191. public ActionResult AddCraftMaterialPartial(CraftMaterialModel craftMaterialModel)
  192. {
  193. AddMaterialListForSelection();
  194. craftMaterialModel.CalculationFactor = 1.2m;
  195. return PartialView("~/Views/Craft/_CraftMaterialPartial.cshtml", craftMaterialModel);
  196. }
  197. #endregion
  198. #region Values
  199. // POST: CraftController/CalculateEmployeeValue
  200. [HttpPost]
  201. public ActionResult CalculateEmployeeValue(CraftEmployeeModel craftEmployeeModel)
  202. {
  203. if (craftEmployeeModel == null)
  204. return Ok(0);
  205. if (craftEmployeeModel.EmployeeId == default)
  206. return Ok(0);
  207. var employee = _eRPDbContext.Employees.Find(craftEmployeeModel.EmployeeId);
  208. var result = employee.EmployeeDegree.Value * craftEmployeeModel.Amount;
  209. return Ok(result);
  210. }
  211. // POST: CraftController/CalculateMaterialValue
  212. [HttpPost]
  213. public ActionResult CalculateMaterialValue(CraftMaterialModel craftMaterialModel)
  214. {
  215. if (craftMaterialModel == null)
  216. return Ok(0);
  217. if (craftMaterialModel.MaterialId == default)
  218. return Ok(0);
  219. var material = _eRPDbContext.Materials.Find(craftMaterialModel.MaterialId);
  220. var result = material.NetValue * craftMaterialModel.Amount * (craftMaterialModel.CalculationFactor / 100);
  221. return Ok(result);
  222. }
  223. // POST: CraftController/CalculateMaterialCalculationFactor
  224. [HttpPost]
  225. public ActionResult CalculateMaterialCalculationFactor(CraftMaterialModel craftMaterialModel)
  226. {
  227. if (craftMaterialModel == null)
  228. return Ok(0);
  229. if (craftMaterialModel.MaterialId == default)
  230. return Ok(0);
  231. var material = _eRPDbContext.Materials.Find(craftMaterialModel.MaterialId);
  232. var result = Convert.ToDecimal(craftMaterialModel.Value) / (material.NetValue * craftMaterialModel.Amount) * 100;
  233. return Ok(result);
  234. }
  235. #endregion
  236. #region Helper
  237. /// <summary>
  238. /// Adds the employee SelectListItem collection to the current ViewData dictionary
  239. /// </summary>
  240. private void AddEmployeeListForSelection()
  241. {
  242. ViewData.AddSelectList("AvailableEmployees",
  243. _eRPDbContext.Employees
  244. .ToList()
  245. .Select(e => new EmployeeModel(e)),
  246. e => e.Id, e => String.Format("{0} {1} ({2} - {3:n0}€ / Std.)",
  247. e.Firstname, e.Lastname, e.EmployeeDegree.Name, e.EmployeeDegree.Value));
  248. }
  249. /// <summary>
  250. /// Adds the material SelectListItem collection to the current ViewData dictionary
  251. /// </summary>
  252. private void AddMaterialListForSelection()
  253. {
  254. ViewData.AddSelectList("AvailableMaterials",
  255. _eRPDbContext.Materials,
  256. m => m.Id, e => String.Format("{0} ({1})", e.Name, e.ItemNumber));
  257. }
  258. #endregion
  259. }
  260. }