Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.84 KB | None | 0 0
  1. using System.Linq.Dynamic;
  2. using System.Reflection;
  3. using System.Web.UI.WebControls;
  4. using Nexus.NException;
  5. using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
  6. using OfficeOpenXml.FormulaParsing.ExpressionGraph;
  7. using Payroll.AppEnum;
  8. using Payroll.Contract.Repository.Administration;
  9. using Payroll.Contract.Repository.Configuration.ConfigurationClient;
  10. using Payroll.Contract.Repository.Security;
  11. using Payroll.DAL;
  12. using Payroll.Model;
  13. using Payroll.Model.Configuration;
  14. using Payroll.Model.Configuration.ConfigurationClient;
  15. using Payroll.Model.Security;
  16. using Payroll.PayrollException;
  17. using Payroll.ResultObject;
  18. using Payroll.Validator;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Data.Entity;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Threading.Tasks;
  25. using Payroll.Extensions;
  26. using Expression = System.Linq.Expressions.Expression;
  27. using Payroll.API.Extensions;
  28.  
  29. namespace Payroll.Server.Repository.Configuration.ConfigurationClient
  30. {
  31. public class TeamMemberRepository :
  32. GenericRepository<TeamMember>,
  33. ITeamMemberRepository
  34. {
  35.  
  36. private readonly List<string> _gtAccessList;
  37.  
  38. public TeamMemberRepository(DbContext context) : base(context)
  39. {
  40. _gtAccessList = new List<string>
  41. {
  42. "Checker",
  43. "Preparer",
  44. "Reviewer"
  45. };
  46. }
  47.  
  48. public override TeamMember Update(TeamMember entity)
  49. {
  50. using (PayrollContext _context = new PayrollContext(CurrentLoggedIn, true))
  51. {
  52. var _teamMember = _context.tbl_TeamMember.SingleOrDefault(a => a.TeamMemberUID == entity.UID);
  53.  
  54. tbl_ConfClient _confClient = null;
  55. if (entity.ConfigClient != null)
  56. {
  57. if (entity.ConfigClient.Client != null)
  58. {
  59. _confClient = (from _tcc in _context.tbl_ConfClient
  60. join _tc in _context.tbl_Client on _tcc.ClientID equals _tc.ClientID
  61. where _tc.ClientUID == entity.ConfigClient.Client.UID
  62. select _tcc).SingleOrDefault();
  63. }
  64. }
  65.  
  66. if (_teamMember != null)
  67. {
  68. _teamMember.EmpID = GetRepo<IEmployeeRepository>().UID2ID(entity.Employee.UID);
  69. _teamMember.AccessID = GetRepo<IAccessRepository>().UID2ID(entity.Access.UID);
  70. if (_confClient != null)
  71. {
  72. _teamMember.TeamMemberCollID = _confClient.TeamMemberCollID.Value;
  73. }
  74. _context.SaveChanges();
  75. }
  76. }
  77.  
  78. return RetrieveByUID(entity.UID);
  79. }
  80.  
  81. public override TeamMember RetrieveByUID(Guid uid)
  82. {
  83. TeamMember _retVal = null;
  84.  
  85. //using (PayrollContext _context = new PayrollContext(CurrentLoggedIn))
  86. //{
  87. var _q = from _t in Context.vw__TeamMember_StatusDetails
  88. join _cc
  89. in Context.tbl_ConfClient
  90. on _t.TeamMemberCollID equals _cc.TeamMemberCollID
  91. join _c
  92. in Context.tbl_Client
  93. on _cc.ClientID equals _c.ClientID
  94. join _e
  95. in Context.vw__Employee_ClientAndGT
  96. on _t.EmpID equals _e.EmpID
  97. join _a
  98. in Context.tbl_Access
  99. on _t.AccessID equals _a.AccessID
  100. join _as
  101. in Context.tbl_AccessScope
  102. on _t.AccessScopeID equals _as.AccessScopeID
  103. where _t.TeamMemberUID == uid
  104. select new TeamMember
  105. {
  106. UID = _t.TeamMemberUID,
  107. Employee = new EmployeeSummary
  108. {
  109. UID = _e.EmpUID,
  110. Code = _e.EmpCode,
  111. FullName = _e.FullName
  112. },
  113. Access = new Access
  114. {
  115. UID = _a.AccessUID,
  116. Name = _a.AccessName
  117. },
  118. AccessScope = new AccessScope
  119. {
  120. UID = _as.AccessScopeUID,
  121. Name = _as.AccessScopeName,
  122. Description = _as.Description
  123. },
  124. Scope = (Scope)_e.Source,
  125. Status = (TeamMemberStatus)_t.Status,
  126. StatusChangedOn = _t.StatusChangedOn,
  127. StatusChangedBy = _t.StatusChangedByID == null
  128. ? null
  129. : new Employee
  130. {
  131. ID = _t.StatusChangedByID.Value,
  132. EmployeeInfo = new EmployeeInfo
  133. {
  134. ID = _t.StatusChangedByID.Value,
  135. NickName = _t.StatusChangedByNickName
  136. }
  137. },
  138. ConfigClient = new ConfigClient
  139. {
  140. Client = new Client
  141. {
  142. UID = _cc.tbl_Client.ClientUID,
  143. ID = _cc.ClientID,
  144. Name = _cc.tbl_Client.ClientName
  145. }
  146. },
  147. CanUpdate = (_t.Status == (byte)TeamMemberStatus.Pending) || CurrentUser.IsSuperAdmin,
  148. CanDelete = (_t.Status == (byte)TeamMemberStatus.Pending) || CurrentUser.IsSuperAdmin
  149. };
  150.  
  151. _retVal = _q.SingleOrDefault();
  152. //}
  153.  
  154. return _retVal;
  155. }
  156.  
  157. public TeamMember CreateByClientUID(Guid cuid, TeamMember value, bool saveNow = true)
  158. {
  159. value.UID = Guid.NewGuid();
  160.  
  161. using (PayrollContext _context = value.Access.Name.In(_gtAccessList)
  162. ? new PayrollContext(CurrentLoggedIn, true)
  163. : new PayrollContext(CurrentLoggedIn))
  164. {
  165. var _q = from _cc in _context.tbl_ConfClient
  166. join _c
  167. in _context.tbl_Client
  168. on _cc.ClientID equals _c.ClientID
  169. where _c.ClientUID == cuid
  170. select _cc;
  171.  
  172. var _confClient = _q.SingleOrDefault();
  173.  
  174. if (_confClient != null)
  175. {
  176. if (_confClient.tbl_TeamMemberColl == null)
  177. {
  178. _confClient.tbl_TeamMemberColl = new tbl_TeamMemberColl
  179. {
  180. TeamMemberCollUID = Guid.NewGuid()
  181. };
  182. }
  183.  
  184. tbl_TeamMember _teamMember = new tbl_TeamMember
  185. {
  186. TeamMemberUID = value.UID,
  187. EmpID = GetRepo<IEmployeeRepository>().UID2ID(value.Employee.UID),
  188. AccessID = GetRepo<IAccessRepository>().UID2ID(value.Access.UID),
  189. Status = (byte)value.Status,
  190. StatusChangedOn = DateTime.Now,
  191. tbl_AccessScope = new tbl_AccessScope
  192. {
  193. AccessScopeUID = Guid.NewGuid(),
  194. AccessScopeName = Guid.NewGuid().ToString()
  195. }
  196. };
  197.  
  198. //if (value.StatusChangedBy != null)
  199. //{
  200. _teamMember.StatusChangedByID = CurrentLoggedIn.EmployeeID;
  201. //}
  202. _confClient.tbl_TeamMemberColl.tbl_TeamMember.Add(_teamMember);
  203. }
  204.  
  205. _context.SaveChanges();
  206. }
  207.  
  208. return RetrieveByUID(value.UID);
  209. }
  210.  
  211. public ResultSummary<TeamMember> GetByClientUID(Guid cuid, JSFilter filtering = null, JSPaging paging = null,
  212. JSSort sorting = null)
  213. {
  214. //using (var _context = new PayrollContext(CurrentLoggedIn))
  215. //{
  216. IQueryable<TeamMember> result = null;
  217. result = Context.vw_ClientTeamMembers.Where(x => x.ClientUID == cuid).Select(x => new TeamMember
  218. {
  219. UID = x.TeamMemberUID,
  220. Employee = new EmployeeSummary
  221. {
  222. UID = x.EmpUID,
  223. Code = x.EmpCode,
  224. FullName = x.FullName
  225. },
  226. Access = new Access
  227. {
  228. UID = x.AccessUID,
  229. Name = x.AccessName
  230. },
  231. AccessScope = new AccessScope
  232. {
  233. UID = x.AccessScopeUID,
  234. Name = x.AccessScopeName,
  235. Description = x.Description
  236. },
  237. Scope = (Scope)x.SOURCE,
  238. Status = (TeamMemberStatus)x.STATUS,
  239. StatusChangedOn = x.StatusChangedOn,
  240. StatusChangedBy = x.StatusChangedByID == null ? null : new Employee
  241. {
  242. ID = x.StatusChangedByID.Value,
  243. EmployeeInfo = new EmployeeInfo
  244. {
  245. ID = x.StatusChangedByID.Value,
  246. NickName = x.StatusChangedByNickName
  247. }
  248. }
  249. });
  250.  
  251. ResultSummary<TeamMember> _retVal = new ResultSummary<TeamMember>
  252. {
  253. Result = result
  254. };
  255. #region Commented Out
  256. //ResultSummary<TeamMember> _retVal = new ResultSummary<TeamMember>
  257. //{
  258. // Result = from _vt in Context.vw__CCTeamMember
  259. // join _t
  260. // in Context.vw__TeamMember_StatusDetails
  261. // on _vt.TeamMemberID equals _t.TeamMemberID
  262. // join _e
  263. // in Context.vw__Employee_ClientAndGT
  264. // on _t.EmpID equals _e.EmpID
  265. // join _a
  266. // in Context.tbl_Access
  267. // on _t.AccessID equals _a.AccessID
  268. // join _as
  269. // in Context.tbl_AccessScope
  270. // on _t.AccessScopeID equals _as.AccessScopeID
  271. // orderby _e.FullName
  272. // where _vt.ClientUID == cuid
  273. // select new TeamMember
  274. // {
  275. // UID = _t.TeamMemberUID,
  276. // Employee = new EmployeeSummary
  277. // {
  278. // UID = _e.EmpUID,
  279. // Code = _e.EmpCode,
  280. // FullName = _e.FullName
  281. // },
  282. // Access = new Access
  283. // {
  284. // UID = _a.AccessUID,
  285. // Name = _a.AccessName
  286. // },
  287. // AccessScope = new AccessScope
  288. // {
  289. // UID = _as.AccessScopeUID,
  290. // Name = _as.AccessScopeName,
  291. // Description = _as.Description
  292. // },
  293. // Scope = (Scope)_e.Source,
  294. // Status = (TeamMemberStatus)_t.Status,
  295. // StatusChangedOn = _t.StatusChangedOn,
  296. // StatusChangedBy = _t.StatusChangedByID == null ? null : new Employee
  297. // {
  298. // ID = _t.StatusChangedByID.Value,
  299. // EmployeeInfo = new EmployeeInfo
  300. // {
  301. // ID = _t.StatusChangedByID.Value,
  302. // NickName = _t.StatusChangedByNickName
  303. // }
  304. // }
  305. // }
  306. //};
  307. #endregion
  308.  
  309. if (filtering != null)
  310. {
  311. foreach (var _jsFilter in filtering.Filters)
  312. {
  313. string _field = _jsFilter.Field.ToLower();
  314. string _value = _jsFilter.Value;
  315.  
  316. switch (_field)
  317. {
  318. case "scope":
  319. Scope _scope;
  320.  
  321. if (Enum.TryParse(_value, true, out _scope))
  322. {
  323. _retVal.Result = _retVal.Result.Where(a => a.Scope == _scope);
  324. }
  325.  
  326. break;
  327. case "search":
  328. _retVal.Result = _retVal.Result.Where(a => a.Employee.FullName.Contains(_value));
  329. break;
  330. }
  331. }
  332. }
  333.  
  334. _retVal.TotalRows = _retVal.Result.Count();
  335.  
  336. if (paging != null)
  337. {
  338. _retVal.Result = _retVal.Result.OrderBy(x => x.Employee.FullName).Skip(paging.Skip).Take(paging.Take);
  339. }
  340. return _retVal;
  341. //}
  342.  
  343. }
  344.  
  345. public TeamMember ChangeStatusByTeamMemberUID(Guid TeamMemberUID, TeamMemberStatus Status)
  346. {
  347. using (PayrollContext _context = new PayrollContext(CurrentLoggedIn))
  348. {
  349. var teamMember = _context.tbl_TeamMember.SingleOrDefault(a => a.TeamMemberUID == TeamMemberUID);
  350.  
  351. if (teamMember == null)
  352. {
  353. throw new System.ArgumentException("Team Member not exist!");
  354. }
  355.  
  356. teamMember.Status = (byte)Status;
  357. teamMember.StatusChangedOn = DateTime.Now;
  358. teamMember.StatusChangedByID = CurrentLoggedIn.EmployeeID;
  359. _context.SaveChanges();
  360. }
  361. return RetrieveByUID(TeamMemberUID);
  362. }
  363.  
  364. public TeamMember OverrideFromClient(Guid cuid, TeamMember entity)
  365. {
  366. throw new NotImplementedException();
  367. }
  368.  
  369. public ResultSummary<TeamMember> GetByEmpUID(JSFilter filtering = null, JSPaging paging = null, JSSort sorting = null)
  370. {
  371. ResultSummary<TeamMember> _retVal = new ResultSummary<TeamMember>
  372. {
  373. Result = from _e in Context.tbl_Emp
  374. join _t
  375. in Context.vw__TeamMember_StatusDetails
  376. on _e.EmpID equals _t.EmpID
  377. join _cc
  378. in Context.tbl_ConfClient
  379. on _t.TeamMemberCollID equals _cc.TeamMemberCollID
  380. join _a
  381. in Context.tbl_Access
  382. on _t.AccessID equals _a.AccessID
  383. join _as
  384. in Context.tbl_AccessScope
  385. on _t.AccessScopeID equals _as.AccessScopeID
  386. orderby _e.FullName
  387. where _e.EmpUID == CurrentLoggedIn.EmployeeUID
  388. select new TeamMember
  389. {
  390. UID = _t.TeamMemberUID,
  391. Employee = new EmployeeSummary
  392. {
  393. UID = _e.EmpUID,
  394. Code = _e.EmpCode,
  395. FullName = _e.FullName
  396. },
  397. Access = new Access
  398. {
  399. UID = _a.AccessUID,
  400. Name = _a.AccessName
  401. },
  402. AccessScope = new AccessScope
  403. {
  404. UID = _as.AccessScopeUID,
  405. Name = _as.AccessScopeName,
  406. Description = _as.Description
  407. },
  408. Scope = (Scope)_t.Status,
  409. Status = (TeamMemberStatus)_t.Status,
  410. StatusChangedOn = _t.StatusChangedOn,
  411. StatusChangedBy = _t.StatusChangedByID == null
  412. ? null
  413. : new Employee
  414. {
  415. ID = _t.StatusChangedByID.Value,
  416. EmployeeInfo = new EmployeeInfo
  417. {
  418. ID = _t.StatusChangedByID.Value,
  419. NickName = _t.StatusChangedByNickName
  420. }
  421. },
  422. ConfigClient = new ConfigClient
  423. {
  424. Client = new Client
  425. {
  426. UID = _cc.tbl_Client.ClientUID,
  427. ID = _cc.ClientID,
  428. Name = _cc.tbl_Client.ClientName
  429. }
  430. },
  431. CanUpdate = (_t.Status == (byte)TeamMemberStatus.Pending) || CurrentUser.IsSuperAdmin,
  432. CanDelete = (_t.Status == (byte)TeamMemberStatus.Pending) || CurrentUser.IsSuperAdmin
  433.  
  434. }
  435. };
  436.  
  437. if (filtering != null)
  438. {
  439. foreach (var _jsFilter in filtering.Filters)
  440. {
  441. string _field = _jsFilter.Field.ToLower();
  442. string _value = _jsFilter.Value;
  443.  
  444. switch (_field)
  445. {
  446. //case "scope":
  447. // Scope _scope;
  448.  
  449. // if (Enum.TryParse(_value, true, out _scope))
  450. // {
  451. // _retVal.Result = _retVal.Result.Where(a => a.Scope == _scope);
  452. // }
  453.  
  454. // break;
  455. case "search":
  456. _retVal.Result = _retVal.Result.Where(a => a.ConfigClient.Client.Name.Contains(_value)
  457. || a.Access.Name.Contains(_value));
  458. break;
  459. }
  460. }
  461. }
  462.  
  463. if (sorting != null)
  464. {
  465. _retVal.Result = _retVal.Result.OrderBy(sorting.Field + (sorting.Direction == SortingDirection.Descending ? " descending" : ""));
  466. }
  467.  
  468. _retVal.TotalRows = _retVal.Result.Count();
  469.  
  470. if (paging != null)
  471. {
  472. _retVal.Result = _retVal.Result.Skip(paging.Skip).Take(paging.Take);
  473. }
  474.  
  475. return _retVal;
  476. }
  477.  
  478. public TeamMember CreateByEmpUID(TeamMember value, bool saveNow = true)
  479. {
  480. TeamMember _retVal = null;
  481. value.UID = Guid.NewGuid();
  482.  
  483. using (PayrollContext _context = new PayrollContext(CurrentLoggedIn))
  484. {
  485. var _q = from _cc in _context.tbl_ConfClient
  486. join _c
  487. in _context.tbl_Client
  488. on _cc.ClientID equals _c.ClientID
  489. where _c.ClientUID == value.ConfigClient.Client.UID
  490. select _cc;
  491.  
  492. var _confClient = _q.SingleOrDefault();
  493.  
  494. if (_confClient != null)
  495. {
  496. if (_confClient.tbl_TeamMemberColl == null)
  497. {
  498. _confClient.tbl_TeamMemberColl = new tbl_TeamMemberColl
  499. {
  500. TeamMemberCollUID = Guid.NewGuid()
  501. };
  502. }
  503.  
  504. tbl_TeamMember _teamMember = new tbl_TeamMember
  505. {
  506. TeamMemberUID = value.UID,
  507. EmpID = GetRepo<IEmployeeRepository>().UID2ID(CurrentLoggedIn.EmployeeUID),
  508. AccessID = GetRepo<IAccessRepository>().UID2ID(value.Access.UID),
  509. Status = (byte)value.Status,
  510. StatusChangedOn = DateTime.Now,
  511. tbl_AccessScope = new tbl_AccessScope
  512. {
  513. AccessScopeUID = Guid.NewGuid(),
  514. AccessScopeName = Guid.NewGuid().ToString()
  515. }
  516. };
  517.  
  518. //if (value.StatusChangedBy != null)
  519. //{
  520. _teamMember.StatusChangedByID = CurrentLoggedIn.EmployeeID;
  521. //}
  522. _confClient.tbl_TeamMemberColl.tbl_TeamMember.Add(_teamMember);
  523.  
  524. _context.SaveChanges();
  525.  
  526. }
  527.  
  528. }
  529.  
  530.  
  531. _retVal = RetrieveByUID(value.UID);
  532. _retVal.CanUpdate = _retVal.Status == (byte)TeamMemberStatus.Pending;
  533. _retVal.CanDelete = _retVal.Status == (byte)TeamMemberStatus.Pending;
  534.  
  535. return _retVal;
  536. }
  537.  
  538. public override void SoftDelete(params Guid[] uid)
  539. {
  540. using (PayrollContext _context = new PayrollContext(CurrentLoggedIn, true))
  541. {
  542. var _modelPair = ModelMapping.GetModelPair<TeamMember>();
  543.  
  544. if (_modelPair == null)
  545. {
  546. throw new Exception("Unable to delete entry, " + typeof(TeamMember).Name + " is not mapped.");
  547. }
  548.  
  549. string _efFull = _modelPair.ToString(true);
  550.  
  551. if (!string.IsNullOrEmpty(_efFull))
  552. {
  553. //throw new NotImplementedException();
  554.  
  555. var _result = _context.usp_DeleteEntries(CurrentLoggedIn.EmployeeUID,
  556. _efFull,
  557. string.Join(",", uid),
  558. false
  559. )
  560. .ToList();
  561.  
  562. if (_result.Any(a => a.StartsWith("DELETE ERROR")))
  563. {
  564. throw new Exception("DEL_Err " + _result.First());
  565. }
  566. }
  567. }
  568. }
  569. }
  570. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement