CraftController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. Comment = model.Comment,
  83. CreatedBy = User.Identity.Name,
  84. CreatedOn = DateTime.Now
  85. };
  86. craft.CraftEmployees = new List<CraftEmployee>(
  87. model.CraftEmployees
  88. .Select(ce => new CraftEmployee
  89. {
  90. Craft = craft,
  91. Employee = _eRPDbContext.Employees.Find(ce.Value.EmployeeId),
  92. Amount = ce.Value.Amount,
  93. Value = Convert.ToDecimal(ce.Value.Value),
  94. Comment = ce.Value.Comment
  95. }));
  96. craft.CraftMaterials = new List<CraftMaterial>(
  97. model.CraftMaterials
  98. .GroupBy(cm => cm.Value.MaterialId)
  99. .Select(cm => new CraftMaterial
  100. {
  101. Craft = craft,
  102. Material = _eRPDbContext.Materials.Find(cm.Key),
  103. Amount = cm.Sum(v => v.Value.Amount),
  104. CalculationFactor = cm.Average(v => v.Value.CalculationFactor / 100),
  105. Value = cm.Sum(v => Convert.ToDecimal(v.Value.Value))
  106. }));
  107. _eRPDbContext.Crafts.Add(craft);
  108. _eRPDbContext.SaveChanges();
  109. return RedirectToAction(nameof(Index));
  110. }
  111. // GET: CraftController/Edit/5
  112. public ActionResult Edit(int id)
  113. {
  114. AddEmployeeListForSelection();
  115. AddMaterialListForSelection();
  116. var craft = _eRPDbContext.Crafts
  117. .FirstOrDefault(c => c.Id == id);
  118. var craftModel = new CraftModel(craft);
  119. return View(craftModel);
  120. }
  121. // POST: CraftController/Edit/5
  122. [HttpPost]
  123. [ValidateAntiForgeryToken]
  124. public ActionResult Edit(int id, CraftModel model)
  125. {
  126. if (!ModelState.IsValid)
  127. {
  128. AddEmployeeListForSelection();
  129. AddMaterialListForSelection();
  130. foreach (var item in model.CraftMaterials)
  131. {
  132. if (item.Value.Amount % 1 != 0)
  133. item.Value.Amount /= 10;
  134. item.Value.CalculationFactor = item.Value.CalculationFactor / 100;
  135. }
  136. return View(model);
  137. }
  138. var craft = _eRPDbContext.Crafts
  139. .FirstOrDefault(u => u.Id == id);
  140. craft.Name = model.Name;
  141. craft.CreationDate = model.CreationDate;
  142. craft.Customer = _eRPDbContext.Customers.Find(model.CustomerId);
  143. craft.Comment = model.Comment;
  144. craft.ChangedBy = User.Identity.Name;
  145. craft.ChangedOn = DateTime.Now;
  146. craft.CraftEmployees.Clear();
  147. craft.CraftMaterials.Clear();
  148. craft.CraftEmployees = new List<CraftEmployee>(
  149. model.CraftEmployees
  150. .Select(ce => new CraftEmployee
  151. {
  152. Craft = craft,
  153. Employee = _eRPDbContext.Employees.Find(ce.Value.EmployeeId),
  154. Amount = ce.Value.Amount,
  155. Value = Convert.ToDecimal(ce.Value.Value),
  156. Comment = ce.Value.Comment
  157. }));
  158. craft.CraftMaterials = new List<CraftMaterial>(
  159. model.CraftMaterials
  160. .GroupBy(cm => cm.Value.MaterialId)
  161. .Select(cm => new CraftMaterial
  162. {
  163. Craft = craft,
  164. Material = _eRPDbContext.Materials.Find(cm.Key),
  165. Amount = cm.Sum(v => v.Value.Amount),
  166. CalculationFactor = cm.Average(v => v.Value.CalculationFactor / 100),
  167. Value = cm.Sum(v => Convert.ToDecimal(v.Value.Value))
  168. }));
  169. _eRPDbContext.SaveChanges();
  170. return RedirectToAction(nameof(Index));
  171. }
  172. // POST: CraftController/Delete/5
  173. [HttpPost]
  174. [ValidateAntiForgeryToken]
  175. public ActionResult Delete(int id, IFormCollection collection)
  176. {
  177. var craft = _eRPDbContext.Crafts
  178. .FirstOrDefault(c => c.Id == id);
  179. _eRPDbContext.Crafts.Remove(craft);
  180. _eRPDbContext.SaveChanges();
  181. return RedirectToAction(nameof(Index));
  182. }
  183. #endregion
  184. #region Partials
  185. // GET: CraftController/AddCraftEmployeePartial
  186. public ActionResult AddCraftEmployeePartial(CraftEmployeeModel craftEmployeeModel)
  187. {
  188. AddEmployeeListForSelection();
  189. ModelState.Clear();
  190. return PartialView("~/Views/Craft/_CraftEmployeePartial.cshtml", craftEmployeeModel);
  191. }
  192. // GET: CraftController/AddCraftMaterialPartial
  193. public ActionResult AddCraftMaterialPartial(CraftMaterialModel craftMaterialModel)
  194. {
  195. AddMaterialListForSelection();
  196. craftMaterialModel.CalculationFactor = 1.2m;
  197. return PartialView("~/Views/Craft/_CraftMaterialPartial.cshtml", craftMaterialModel);
  198. }
  199. #endregion
  200. #region Values
  201. // POST: CraftController/CalculateEmployeeValue
  202. [HttpPost]
  203. public ActionResult CalculateEmployeeValue(CraftEmployeeModel craftEmployeeModel)
  204. {
  205. if (craftEmployeeModel == null)
  206. return Ok(0);
  207. if (craftEmployeeModel.EmployeeId == default)
  208. return Ok(0);
  209. var employee = _eRPDbContext.Employees.Find(craftEmployeeModel.EmployeeId);
  210. var result = employee.EmployeeDegree.Value * craftEmployeeModel.Amount;
  211. return Ok(result);
  212. }
  213. // POST: CraftController/CalculateMaterialValue
  214. [HttpPost]
  215. public ActionResult CalculateMaterialValue(CraftMaterialModel craftMaterialModel)
  216. {
  217. if (craftMaterialModel == null)
  218. return Ok(0);
  219. if (craftMaterialModel.MaterialId == default)
  220. return Ok(0);
  221. var material = _eRPDbContext.Materials.Find(craftMaterialModel.MaterialId);
  222. var result = material.NetValue * craftMaterialModel.Amount * (craftMaterialModel.CalculationFactor / 100);
  223. return Ok(result);
  224. }
  225. // POST: CraftController/CalculateMaterialCalculationFactor
  226. [HttpPost]
  227. public ActionResult CalculateMaterialCalculationFactor(CraftMaterialModel craftMaterialModel)
  228. {
  229. if (craftMaterialModel == null)
  230. return Ok(0);
  231. if (craftMaterialModel.MaterialId == default)
  232. return Ok(0);
  233. var material = _eRPDbContext.Materials.Find(craftMaterialModel.MaterialId);
  234. var result = Convert.ToDecimal(craftMaterialModel.Value) / (material.NetValue * craftMaterialModel.Amount) * 100;
  235. return Ok(result);
  236. }
  237. #endregion
  238. #region Helper
  239. /// <summary>
  240. /// Adds the employee SelectListItem collection to the current ViewData dictionary
  241. /// </summary>
  242. private void AddEmployeeListForSelection()
  243. {
  244. ViewData.AddSelectList("AvailableEmployees",
  245. _eRPDbContext.Employees
  246. .ToList()
  247. .Select(e => new EmployeeModel(e)),
  248. e => e.Id, e => String.Format("{0} {1} ({2} - {3:n0}€ / Std.)",
  249. e.Firstname, e.Lastname, e.EmployeeDegree.Name, e.EmployeeDegree.Value));
  250. }
  251. /// <summary>
  252. /// Adds the material SelectListItem collection to the current ViewData dictionary
  253. /// </summary>
  254. private void AddMaterialListForSelection()
  255. {
  256. ViewData.AddSelectList("AvailableMaterials",
  257. _eRPDbContext.Materials,
  258. m => m.Id, e => String.Format("{0} ({1})", e.Name, e.ItemNumber));
  259. }
  260. #endregion
  261. }
  262. }