CustomerController.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Threading.Tasks;
  8. using CsvHelper;
  9. using GreenTree.Strohrmann.ERP.Core.Domain.Business;
  10. using GreenTree.Strohrmann.ERP.Core.Helper;
  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.Helper;
  15. using GreenTree.Strohrmann.ERP.Web.ImportMaps;
  16. using GreenTree.Strohrmann.ERP.Web.Models.Business;
  17. using GreenTree.Strohrmann.ERP.Web.Models.Import;
  18. using GreenTree.Strohrmann.ERP.Web.Models.Rights.User;
  19. using GreenTree.Strohrmann.ERP.Web.Models.Shared;
  20. using Microsoft.AspNetCore.Http;
  21. using Microsoft.AspNetCore.Mvc;
  22. namespace GreenTree.Strohrmann.ERP.Web.Controllers
  23. {
  24. public class CustomerController : Controller
  25. {
  26. #region DI fields
  27. // The global DbContext
  28. private readonly ERPDbContext _eRPDbContext;
  29. #endregion
  30. #region Ctor
  31. /// <summary>
  32. /// Initializes a new instance of the CustomerController class
  33. /// </summary>
  34. /// <param name="eRPDbContext">Global DbContext.</param>
  35. public CustomerController(ERPDbContext eRPDbContext)
  36. {
  37. _eRPDbContext = eRPDbContext;
  38. }
  39. #endregion
  40. #region CRUD
  41. // GET: CustomerController
  42. public ActionResult Index()
  43. {
  44. var customers = _eRPDbContext.Customers
  45. .ToList()
  46. .Select(c => new CustomerModel(c));
  47. return View(customers);
  48. }
  49. // GET: CustomerController/Details/5
  50. public ActionResult Details(int id)
  51. {
  52. var customer = _eRPDbContext.Customers
  53. .FirstOrDefault(c => c.Id == id);
  54. var customerModel = new CustomerModel(customer);
  55. return View(customerModel);
  56. }
  57. // GET: CustomerController/Create
  58. public ActionResult Create()
  59. {
  60. ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);
  61. return View();
  62. }
  63. // POST: CustomerController/Create
  64. [HttpPost]
  65. [ValidateAntiForgeryToken]
  66. public ActionResult Create(CustomerModel model)
  67. {
  68. if (!ModelState.IsValid)
  69. {
  70. ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);
  71. return View(model);
  72. }
  73. var customer = new Customer
  74. {
  75. Title = _eRPDbContext.Titles.Find(model.Title.Id),
  76. Firstname = model.Firstname,
  77. Lastname = model.Lastname,
  78. CompanyName = model.CompanyName,
  79. Address = model.Address,
  80. Town = model.Town,
  81. ZipCode = model.ZipCode,
  82. Country = model.Country,
  83. IsBusiness = model.IsBusiness,
  84. CreatedBy = User.Identity.Name,
  85. CreatedOn = DateTime.Now,
  86. CreationStatus = "Created"
  87. };
  88. _eRPDbContext.Customers.Add(customer);
  89. _eRPDbContext.SaveChanges();
  90. return RedirectToAction(nameof(Index));
  91. }
  92. // GET: CustomerController/Edit/5
  93. public ActionResult Edit(int id)
  94. {
  95. ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);
  96. var customer = _eRPDbContext.Customers
  97. .FirstOrDefault(c => c.Id == id);
  98. var customerModel = new CustomerModel(customer);
  99. return View(customerModel);
  100. }
  101. // POST: CustomerController/Edit/5
  102. [HttpPost]
  103. [ValidateAntiForgeryToken]
  104. public ActionResult Edit(int id, CustomerModel model)
  105. {
  106. if (!ModelState.IsValid)
  107. {
  108. ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);
  109. return View(model);
  110. }
  111. var customer = _eRPDbContext.Customers
  112. .FirstOrDefault(u => u.Id == id);
  113. customer.Title = _eRPDbContext.Titles.Find(model.Title.Id);
  114. customer.Firstname = model.Firstname;
  115. customer.Lastname = model.Lastname;
  116. customer.CompanyName = model.CompanyName;
  117. customer.Address = model.Address;
  118. customer.Town = model.Town;
  119. customer.ZipCode = model.ZipCode;
  120. customer.Country = model.Country;
  121. customer.IsBusiness = model.IsBusiness;
  122. customer.ChangedBy = User.Identity.Name;
  123. customer.ChangedOn = DateTime.Now;
  124. _eRPDbContext.SaveChanges();
  125. return RedirectToAction(nameof(Index));
  126. }
  127. // POST: CustomerController/Delete/5
  128. [HttpPost]
  129. [ValidateAntiForgeryToken]
  130. public ActionResult Delete(int id, IFormCollection collection)
  131. {
  132. var customer = _eRPDbContext.Customers
  133. .FirstOrDefault(c => c.Id == id);
  134. _eRPDbContext.Customers.Remove(customer);
  135. _eRPDbContext.SaveChanges();
  136. return RedirectToAction(nameof(Index));
  137. }
  138. #endregion
  139. #region Import
  140. // GET: CustomerController/Import
  141. public ActionResult Import()
  142. {
  143. return View();
  144. }
  145. // POST: CustomerController/CheckImport
  146. [HttpPost]
  147. [ValidateAntiForgeryToken]
  148. public ActionResult CheckImport(ImportModel model)
  149. {
  150. try
  151. {
  152. ImportHelper.CheckImport<Customer, CustomerMap>(model);
  153. return Ok();
  154. }
  155. catch (ValidationException ex)
  156. {
  157. return Problem(ex.Message);
  158. }
  159. catch (Exception ex)
  160. {
  161. return Problem(ex.Message);
  162. }
  163. }
  164. // POST: CustomerController/Import
  165. [HttpPost]
  166. [ValidateAntiForgeryToken]
  167. public ActionResult Import(ImportModel model)
  168. {
  169. try
  170. {
  171. var customers = ImportHelper.GetFromImport<Customer, CustomerMap>(model);
  172. foreach (var customer in customers)
  173. {
  174. customer.Id = 0;
  175. customer.CreationStatus = "Imported";
  176. customer.CreatedBy = User.Identity.Name;
  177. customer.CreatedOn = DateTime.Now;
  178. customer.Title = _eRPDbContext.Titles.Find(customer.Title.Id);
  179. if (customer.Tax.Id == -1)
  180. customer.Tax = null;
  181. }
  182. _eRPDbContext.Customers.AddRange(customers);
  183. _eRPDbContext.SaveChanges();
  184. return View("~/Views/Import/ImportResult.cshtml", new ImportResultModel(customers.Count()));
  185. }
  186. catch (Exception ex)
  187. {
  188. return View("~/Views/Import/ImportResult.cshtml", new ImportResultModel(ex));
  189. }
  190. }
  191. #endregion
  192. #region Partials
  193. // POST: CustomerController/Search/*term*
  194. [HttpPost]
  195. public ActionResult Search(SearchModel search)
  196. {
  197. if (search == null)
  198. return new JsonResult(null);
  199. var result = new List<CustomerModel>();
  200. var words = search.SearchTerm.Split(' ');
  201. foreach (var word in words)
  202. {
  203. result.AddRange(
  204. _eRPDbContext.Customers
  205. .Where(c => c.Firstname.Contains(word, StringComparison.OrdinalIgnoreCase) ||
  206. c.Lastname.Contains(word, StringComparison.OrdinalIgnoreCase) ||
  207. c.CompanyName.Contains(word, StringComparison.OrdinalIgnoreCase))
  208. .ToArray()
  209. .Select(c => new CustomerModel(c)));
  210. }
  211. return new JsonResult(
  212. result
  213. .GroupBy(c => c.Id)
  214. .Select(g => g.First()));
  215. }
  216. #endregion
  217. }
  218. }