/MContracts/MContracts/ViewModel/AddOrderViewModel.cs

# · C# · 327 lines · 249 code · 52 blank · 26 comment · 42 complexity · 591467cb0e66165ef429ab9821f2c10a MD5 · raw file

  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using MCDomain.DataAccess;
  6. using MCDomain.Model;
  7. using UIShared.Commands;
  8. using MContracts.Classes;
  9. using CommonBase;
  10. using CommonBase;
  11. using McUIBase.ViewModel;
  12. namespace MContracts.ViewModel
  13. {
  14. public class ResponsibleForOrderEntity: IDataErrorInfo
  15. {
  16. #region Nested type: ErrorHandlers
  17. private class DepartmentErrorHandler : IDataErrorHandler<ResponsibleForOrderEntity>
  18. {
  19. public string GetError(ResponsibleForOrderEntity source, string propertyName, ref bool handled)
  20. {
  21. if (propertyName == "Department")
  22. {
  23. return HandleCurrencyError(source, out handled);
  24. }
  25. return string.Empty;
  26. }
  27. private static string HandleCurrencyError(ResponsibleForOrderEntity source, out bool handled)
  28. {
  29. handled = false;
  30. if (source.Department == null)
  31. {
  32. handled = true;
  33. return "Выберите отдел";
  34. }
  35. handled = true;
  36. return string.Empty;
  37. }
  38. }
  39. private class EmployeeErrorHandler : IDataErrorHandler<ResponsibleForOrderEntity>
  40. {
  41. public string GetError(ResponsibleForOrderEntity source, string propertyName, ref bool handled)
  42. {
  43. if (propertyName == "Employee")
  44. {
  45. return HandleCurrencyError(source, out handled);
  46. }
  47. return string.Empty;
  48. }
  49. private static string HandleCurrencyError(ResponsibleForOrderEntity source, out bool handled)
  50. {
  51. handled = false;
  52. if (source.Employee == null)
  53. {
  54. handled = true;
  55. return "Выберите сотрудника";
  56. }
  57. handled = true;
  58. return string.Empty;
  59. }
  60. }
  61. #endregion
  62. private readonly DataErrorHandlers<ResponsibleForOrderEntity> _errorHandlers = new DataErrorHandlers<ResponsibleForOrderEntity>
  63. {
  64. new DepartmentErrorHandler(),
  65. new EmployeeErrorHandler()
  66. };
  67. public double Id { get; set; }
  68. public Department Department { get; set; }
  69. public Employee Employee { get; set; }
  70. public Contracttype Contracttype { get; set; }
  71. public string this[string columnName]
  72. {
  73. get { return _errorHandlers.HandleError(this, columnName); }
  74. }
  75. public string Error
  76. {
  77. get { return this.Validate(); }
  78. }
  79. }
  80. public class AddOrderViewModel : WorkspaceViewModel
  81. {
  82. public RelayCommand OkPressedAction
  83. {
  84. get { return _okpressedaction ?? (_okpressedaction = new RelayCommand(PressOK, CanPressOk)); }
  85. }
  86. private bool CanPressOk(object obj)
  87. {
  88. return (Order.Validate() == string.Empty)&&(ResponsibleForOrderEntities.Cast<ResponsibleForOrderEntity>().Select(p => p.Validate()).Where(s => s != string.Empty).Count() == 0);
  89. }
  90. private void PressOK(object obj)
  91. {
  92. }
  93. public AddOrderViewModel(IContractRepository repository) : base(repository)
  94. {
  95. }
  96. public override bool IsClosable
  97. {
  98. get { return true; }
  99. }
  100. protected override void Save()
  101. {
  102. // удаляем из респонсиблов каждый удаленный из DTO коллекции
  103. ResponsibleForOrderEntity re = null;
  104. Responsiblefororder r = null;
  105. for (int i = Order.Responsiblefororders.Count - 1; i >= 0; i--)
  106. {
  107. r = Order.Responsiblefororders[i];
  108. re = ResponsibleForOrderEntities.Where<ResponsibleForOrderEntity>(p => p.Id == r.Id).FirstOrDefault() as ResponsibleForOrderEntity;
  109. if (re == null) Order.Responsiblefororders.Remove(r);
  110. }
  111. // добавляем или обновляем каждый добавленный
  112. Responsiblefororder ru = null;
  113. foreach (ResponsibleForOrderEntity reu in ResponsibleForOrderEntities)
  114. {
  115. ru = Order.Responsiblefororders.FirstOrDefault(p => (p.Id == reu.Id && reu.Id != 0));
  116. if (ru == null)
  117. {
  118. ru = new Responsiblefororder() { Responsibleassignmentorder = Order, Employee = reu.Employee, Department = reu.Department, Contracttype = reu.Contracttype};
  119. Order.Responsiblefororders.Add(ru);
  120. }
  121. else
  122. {
  123. ru.Employee = reu.Employee;
  124. ru.Department = reu.Department;
  125. ru.Contracttype = reu.Contracttype;
  126. }
  127. }
  128. }
  129. protected override bool CanSave()
  130. {
  131. return true;
  132. }
  133. private Responsibleassignmentorder _order;
  134. /// <summary>
  135. /// редактируемый приказ
  136. /// </summary>
  137. public Responsibleassignmentorder Order
  138. {
  139. get { return _order; }
  140. set
  141. {
  142. if (_order == value) return;
  143. _order = value;
  144. OnPropertyChanged("Order");
  145. }
  146. }
  147. private IBindingList _responsiblefororderentities;
  148. private ResponsibleForOrderEntity _selectedresponsiblefororder;
  149. /// <summary>
  150. /// набор ответственных по приказу (DTO)
  151. /// </summary>
  152. public IBindingList ResponsibleForOrderEntities
  153. {
  154. get
  155. {
  156. if (_responsiblefororderentities == null)
  157. {
  158. _responsiblefororderentities = new BindingList<ResponsibleForOrderEntity>();
  159. if (_order != null)
  160. {
  161. foreach (Responsiblefororder r in _order.Responsiblefororders)
  162. {
  163. var rr = new ResponsibleForOrderEntity { Id = r.Id, Department = r.Department, Employee = r.Employee, Contracttype = r.Contracttype};
  164. _responsiblefororderentities.Add(rr);
  165. }
  166. }
  167. OnPropertyChanged("ResponsibleForOrderEntities");
  168. }
  169. return _responsiblefororderentities;
  170. }
  171. }
  172. /// <summary>
  173. /// выбранный ответственный
  174. /// </summary>
  175. public ResponsibleForOrderEntity SelectedResponsibleForOrder
  176. {
  177. get { return _selectedresponsiblefororder; }
  178. set
  179. {
  180. if (_selectedresponsiblefororder == value) return;
  181. _selectedresponsiblefororder = value;
  182. OnPropertyChanged("SelectedResponsibleForOrder");
  183. OnPropertyChanged("SelectedDepartment");
  184. OnPropertyChanged("DepartmentEmployees");
  185. }
  186. }
  187. private RelayCommand _createResponsibleForOrderCommand;
  188. /// <summary>
  189. /// добавить ответственного по приказу
  190. /// </summary>
  191. public RelayCommand CreateResponsibleForOrderCommand
  192. {
  193. get
  194. {
  195. return _createResponsibleForOrderCommand ??
  196. (_createResponsibleForOrderCommand = new RelayCommand(CreateResponsibleForOrder, x => true));
  197. }
  198. }
  199. public void CreateResponsibleForOrder(object o)
  200. {
  201. var r = new ResponsibleForOrderEntity() {Contracttype = Contracttypes.GetReservedUndefined()};
  202. ResponsibleForOrderEntities.Add(r);
  203. SelectedResponsibleForOrder = r;
  204. OnPropertyChanged("ResponsibleForOrderEntities");
  205. }
  206. private RelayCommand _deleteResponsibleForOrderCommand;
  207. private RelayCommand _okpressedaction;
  208. /// <summary>
  209. /// Удалить ответственного по приказу
  210. /// </summary>
  211. public RelayCommand DeleteResponsibleForOrderCommand
  212. {
  213. get
  214. {
  215. return _deleteResponsibleForOrderCommand ?? (_deleteResponsibleForOrderCommand = new RelayCommand(DeleteResponsibleForOrder, x => CanDeleteResponsibleForOrder));
  216. }
  217. }
  218. public void DeleteResponsibleForOrder(object o)
  219. {
  220. ResponsibleForOrderEntities.Remove(SelectedResponsibleForOrder);
  221. SelectedResponsibleForOrder = null;
  222. }
  223. public bool CanDeleteResponsibleForOrder
  224. {
  225. get
  226. {
  227. return (SelectedResponsibleForOrder != null);
  228. }
  229. }
  230. /// <summary>
  231. /// отделы, по которым еще не определены ответственные
  232. /// </summary>
  233. public IEnumerable<Department> Departments
  234. {
  235. get
  236. {
  237. return Repository.Departments.Where(d => (ResponsibleForOrderEntities.Where<ResponsibleForOrderEntity>(p => p.Department == d&&p.Contracttype.Id == EntityBase.ReservedUndifinedOid).Count() == 0) || d == SelectedDepartment);
  238. }
  239. }
  240. public IEnumerable<Contracttype> Contracttypes
  241. {
  242. get { return Repository.Contracttypes.Where(c => (ResponsibleForOrderEntities.Where<ResponsibleForOrderEntity>(p => p.Contracttype == c&&p.Department == SelectedDepartment).Count() == 0) || c == SelectedContracttype);}
  243. }
  244. /// <summary>
  245. /// выбранный департамент
  246. /// </summary>
  247. public Department SelectedDepartment
  248. {
  249. get
  250. {
  251. return SelectedResponsibleForOrder != null ? SelectedResponsibleForOrder.Department : null;
  252. }
  253. }
  254. public Contracttype SelectedContracttype
  255. {
  256. get
  257. {
  258. return SelectedResponsibleForOrder != null?SelectedResponsibleForOrder.Contracttype : null;
  259. }
  260. }
  261. /// <summary>
  262. /// сотрудники выбранного департамента
  263. /// </summary>
  264. public IEnumerable<Employee> DepartmentEmployees
  265. {
  266. get {
  267. return SelectedDepartment != null ? SelectedDepartment.RealEmployees : null;
  268. }
  269. }
  270. public void SendPropertyChanged(string propertyName)
  271. {
  272. OnPropertyChanged(propertyName);
  273. }
  274. }
  275. }