| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Threading.Tasks;
- using GreenTree.Strohrmann.ERP.Core.Domain.Business;
- using GreenTree.Strohrmann.ERP.Core.Extension;
- using GreenTree.Strohrmann.ERP.Core.Helper;
- using GreenTree.Strohrmann.ERP.Domain.Extension;
- using GreenTree.Strohrmann.ERP.Domain.Model;
- using GreenTree.Strohrmann.ERP.Services.Geolocator;
- using GreenTree.Strohrmann.ERP.Web.Extension;
- using GreenTree.Strohrmann.ERP.Web.Models.Business;
- using GreenTree.Strohrmann.ERP.Web.Models.Rights.User;
- using GreenTree.Strohrmann.ERP.Web.Validators;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- namespace GreenTree.Strohrmann.ERP.Web.Controllers
- {
- public class CraftController : Controller
- {
- #region DI fields
- // The global DbContext
- private readonly ERPDbContext _eRPDbContext;
- #endregion
- #region Ctor
- /// <summary>
- /// Initializes a new instance of the CraftController class
- /// </summary>
- /// <param name="eRPDbContext">Global DbContext.</param>
- public CraftController(ERPDbContext eRPDbContext)
- {
- _eRPDbContext = eRPDbContext;
- }
- #endregion
- #region Actions
- // GET: CraftController
- public ActionResult Index()
- {
- var crafts = _eRPDbContext.Crafts
- .ToList()
- .Select(c => new CraftModel(c));
- return View(crafts);
- }
- // GET: CraftController/Details/5
- public ActionResult Details(int id)
- {
- var craft = _eRPDbContext.Crafts
- .FirstOrDefault(c => c.Id == id);
- var craftModel = new CraftModel(craft);
- return View(craftModel);
- }
- // GET: CraftController/Create
- public ActionResult Create()
- {
- AddEmployeeListForSelection();
- AddMaterialListForSelection();
- return View();
- }
- // POST: CraftController/Create
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Create(CraftModel model)
- {
- if (!ModelState.IsValid)
- {
- AddEmployeeListForSelection();
- AddMaterialListForSelection();
- foreach (var item in model.CraftMaterials)
- {
- if (item.Value.Amount % 1 != 0)
- item.Value.Amount /= 10;
- item.Value.CalculationFactor = item.Value.CalculationFactor / 100;
- }
- return View(model);
- }
- var craft = new Craft
- {
- Name = model.Name,
- CreationDate = model.CreationDate,
- Customer = _eRPDbContext.Customers.Find(model.CustomerId),
- Comment = model.Comment,
- CreatedBy = User.Identity.Name,
- CreatedOn = DateTime.Now
- };
- craft.CraftEmployees = new List<CraftEmployee>(
- model.CraftEmployees
- .Select(ce => new CraftEmployee
- {
- Craft = craft,
- Employee = _eRPDbContext.Employees.Find(ce.Value.EmployeeId),
- Amount = ce.Value.Amount,
- Value = Convert.ToDecimal(ce.Value.Value),
- Comment = ce.Value.Comment
- }));
- craft.CraftMaterials = new List<CraftMaterial>(
- model.CraftMaterials
- .GroupBy(cm => cm.Value.MaterialId)
- .Select(cm => new CraftMaterial
- {
- Craft = craft,
- Material = _eRPDbContext.Materials.Find(cm.Key),
- Amount = cm.Sum(v => v.Value.Amount),
- CalculationFactor = cm.Average(v => v.Value.CalculationFactor / 100),
- Value = cm.Sum(v => Convert.ToDecimal(v.Value.Value))
- }));
- _eRPDbContext.Crafts.Add(craft);
- _eRPDbContext.SaveChanges();
- return RedirectToAction(nameof(Index));
- }
- // GET: CraftController/Edit/5
- public ActionResult Edit(int id)
- {
- AddEmployeeListForSelection();
- AddMaterialListForSelection();
- var craft = _eRPDbContext.Crafts
- .FirstOrDefault(c => c.Id == id);
- var craftModel = new CraftModel(craft);
- return View(craftModel);
- }
- // POST: CraftController/Edit/5
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Edit(int id, CraftModel model)
- {
- if (!ModelState.IsValid)
- {
- AddEmployeeListForSelection();
- AddMaterialListForSelection();
- foreach (var item in model.CraftMaterials)
- {
- if (item.Value.Amount % 1 != 0)
- item.Value.Amount /= 10;
- item.Value.CalculationFactor = item.Value.CalculationFactor / 100;
- }
- return View(model);
- }
- var craft = _eRPDbContext.Crafts
- .FirstOrDefault(u => u.Id == id);
- craft.Name = model.Name;
- craft.CreationDate = model.CreationDate;
- craft.Customer = _eRPDbContext.Customers.Find(model.CustomerId);
- craft.Comment = model.Comment;
- craft.ChangedBy = User.Identity.Name;
- craft.ChangedOn = DateTime.Now;
- craft.CraftEmployees.Clear();
- craft.CraftMaterials.Clear();
- craft.CraftEmployees = new List<CraftEmployee>(
- model.CraftEmployees
- .Select(ce => new CraftEmployee
- {
- Craft = craft,
- Employee = _eRPDbContext.Employees.Find(ce.Value.EmployeeId),
- Amount = ce.Value.Amount,
- Value = Convert.ToDecimal(ce.Value.Value),
- Comment = ce.Value.Comment
- }));
- craft.CraftMaterials = new List<CraftMaterial>(
- model.CraftMaterials
- .GroupBy(cm => cm.Value.MaterialId)
- .Select(cm => new CraftMaterial
- {
- Craft = craft,
- Material = _eRPDbContext.Materials.Find(cm.Key),
- Amount = cm.Sum(v => v.Value.Amount),
- CalculationFactor = cm.Average(v => v.Value.CalculationFactor / 100),
- Value = cm.Sum(v => Convert.ToDecimal(v.Value.Value))
- }));
- _eRPDbContext.SaveChanges();
- return RedirectToAction(nameof(Index));
- }
- // POST: CraftController/Delete/5
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Delete(int id, IFormCollection collection)
- {
- var craft = _eRPDbContext.Crafts
- .FirstOrDefault(c => c.Id == id);
- _eRPDbContext.Crafts.Remove(craft);
- _eRPDbContext.SaveChanges();
- return RedirectToAction(nameof(Index));
- }
- #endregion
- #region Partials
- // GET: CraftController/AddCraftEmployeePartial
- public ActionResult AddCraftEmployeePartial(CraftEmployeeModel craftEmployeeModel)
- {
- AddEmployeeListForSelection();
- ModelState.Clear();
- return PartialView("~/Views/Craft/_CraftEmployeePartial.cshtml", craftEmployeeModel);
- }
- // GET: CraftController/AddCraftMaterialPartial
- public ActionResult AddCraftMaterialPartial(CraftMaterialModel craftMaterialModel)
- {
- AddMaterialListForSelection();
- craftMaterialModel.CalculationFactor = 1.2m;
- return PartialView("~/Views/Craft/_CraftMaterialPartial.cshtml", craftMaterialModel);
- }
- #endregion
- #region Values
- // POST: CraftController/CalculateEmployeeValue
- [HttpPost]
- public ActionResult CalculateEmployeeValue(CraftEmployeeModel craftEmployeeModel)
- {
- if (craftEmployeeModel == null)
- return Ok(0);
- if (craftEmployeeModel.EmployeeId == default)
- return Ok(0);
- var employee = _eRPDbContext.Employees.Find(craftEmployeeModel.EmployeeId);
- var result = employee.EmployeeDegree.Value * craftEmployeeModel.Amount;
- return Ok(result);
- }
- // POST: CraftController/CalculateMaterialValue
- [HttpPost]
- public ActionResult CalculateMaterialValue(CraftMaterialModel craftMaterialModel)
- {
- if (craftMaterialModel == null)
- return Ok(0);
- if (craftMaterialModel.MaterialId == default)
- return Ok(0);
- var material = _eRPDbContext.Materials.Find(craftMaterialModel.MaterialId);
- var result = material.NetValue * craftMaterialModel.Amount * (craftMaterialModel.CalculationFactor / 100);
- return Ok(result);
- }
- // POST: CraftController/CalculateMaterialCalculationFactor
- [HttpPost]
- public ActionResult CalculateMaterialCalculationFactor(CraftMaterialModel craftMaterialModel)
- {
- if (craftMaterialModel == null)
- return Ok(0);
- if (craftMaterialModel.MaterialId == default)
- return Ok(0);
- var material = _eRPDbContext.Materials.Find(craftMaterialModel.MaterialId);
- var result = Convert.ToDecimal(craftMaterialModel.Value) / (material.NetValue * craftMaterialModel.Amount) * 100;
- return Ok(result);
- }
- #endregion
- #region Helper
- /// <summary>
- /// Adds the employee SelectListItem collection to the current ViewData dictionary
- /// </summary>
- private void AddEmployeeListForSelection()
- {
- ViewData.AddSelectList("AvailableEmployees",
- _eRPDbContext.Employees
- .ToList()
- .Select(e => new EmployeeModel(e)),
- e => e.Id, e => String.Format("{0} {1} ({2} - {3:n0}€ / Std.)",
- e.Firstname, e.Lastname, e.EmployeeDegree.Name, e.EmployeeDegree.Value));
- }
- /// <summary>
- /// Adds the material SelectListItem collection to the current ViewData dictionary
- /// </summary>
- private void AddMaterialListForSelection()
- {
- ViewData.AddSelectList("AvailableMaterials",
- _eRPDbContext.Materials,
- m => m.Id, e => String.Format("{0} ({1})", e.Name, e.ItemNumber));
- }
- #endregion
- }
- }
|