CraftController.cs 11 KB

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