Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq.Dynamic;
- using System.Reflection;
- using System.Web.UI.WebControls;
- using Nexus.NException;
- using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
- using OfficeOpenXml.FormulaParsing.ExpressionGraph;
- using Payroll.AppEnum;
- using Payroll.Contract.Repository.Administration;
- using Payroll.Contract.Repository.Configuration.ConfigurationClient;
- using Payroll.Contract.Repository.Security;
- using Payroll.DAL;
- using Payroll.Model;
- using Payroll.Model.Configuration;
- using Payroll.Model.Configuration.ConfigurationClient;
- using Payroll.Model.Security;
- using Payroll.PayrollException;
- using Payroll.ResultObject;
- using Payroll.Validator;
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Payroll.Extensions;
- using Expression = System.Linq.Expressions.Expression;
- using Payroll.API.Extensions;
- namespace Payroll.Server.Repository.Configuration.ConfigurationClient
- {
- public class TeamMemberRepository :
- GenericRepository<TeamMember>,
- ITeamMemberRepository
- {
- private readonly List<string> _gtAccessList;
- public TeamMemberRepository(DbContext context) : base(context)
- {
- _gtAccessList = new List<string>
- {
- "Checker",
- "Preparer",
- "Reviewer"
- };
- }
- public override TeamMember Update(TeamMember entity)
- {
- using (PayrollContext _context = new PayrollContext(CurrentLoggedIn, true))
- {
- var _teamMember = _context.tbl_TeamMember.SingleOrDefault(a => a.TeamMemberUID == entity.UID);
- tbl_ConfClient _confClient = null;
- if (entity.ConfigClient != null)
- {
- if (entity.ConfigClient.Client != null)
- {
- _confClient = (from _tcc in _context.tbl_ConfClient
- join _tc in _context.tbl_Client on _tcc.ClientID equals _tc.ClientID
- where _tc.ClientUID == entity.ConfigClient.Client.UID
- select _tcc).SingleOrDefault();
- }
- }
- if (_teamMember != null)
- {
- _teamMember.EmpID = GetRepo<IEmployeeRepository>().UID2ID(entity.Employee.UID);
- _teamMember.AccessID = GetRepo<IAccessRepository>().UID2ID(entity.Access.UID);
- if (_confClient != null)
- {
- _teamMember.TeamMemberCollID = _confClient.TeamMemberCollID.Value;
- }
- _context.SaveChanges();
- }
- }
- return RetrieveByUID(entity.UID);
- }
- public override TeamMember RetrieveByUID(Guid uid)
- {
- TeamMember _retVal = null;
- //using (PayrollContext _context = new PayrollContext(CurrentLoggedIn))
- //{
- var _q = from _t in Context.vw__TeamMember_StatusDetails
- join _cc
- in Context.tbl_ConfClient
- on _t.TeamMemberCollID equals _cc.TeamMemberCollID
- join _c
- in Context.tbl_Client
- on _cc.ClientID equals _c.ClientID
- join _e
- in Context.vw__Employee_ClientAndGT
- on _t.EmpID equals _e.EmpID
- join _a
- in Context.tbl_Access
- on _t.AccessID equals _a.AccessID
- join _as
- in Context.tbl_AccessScope
- on _t.AccessScopeID equals _as.AccessScopeID
- where _t.TeamMemberUID == uid
- select new TeamMember
- {
- UID = _t.TeamMemberUID,
- Employee = new EmployeeSummary
- {
- UID = _e.EmpUID,
- Code = _e.EmpCode,
- FullName = _e.FullName
- },
- Access = new Access
- {
- UID = _a.AccessUID,
- Name = _a.AccessName
- },
- AccessScope = new AccessScope
- {
- UID = _as.AccessScopeUID,
- Name = _as.AccessScopeName,
- Description = _as.Description
- },
- Scope = (Scope)_e.Source,
- Status = (TeamMemberStatus)_t.Status,
- StatusChangedOn = _t.StatusChangedOn,
- StatusChangedBy = _t.StatusChangedByID == null
- ? null
- : new Employee
- {
- ID = _t.StatusChangedByID.Value,
- EmployeeInfo = new EmployeeInfo
- {
- ID = _t.StatusChangedByID.Value,
- NickName = _t.StatusChangedByNickName
- }
- },
- ConfigClient = new ConfigClient
- {
- Client = new Client
- {
- UID = _cc.tbl_Client.ClientUID,
- ID = _cc.ClientID,
- Name = _cc.tbl_Client.ClientName
- }
- },
- CanUpdate = (_t.Status == (byte)TeamMemberStatus.Pending) || CurrentUser.IsSuperAdmin,
- CanDelete = (_t.Status == (byte)TeamMemberStatus.Pending) || CurrentUser.IsSuperAdmin
- };
- _retVal = _q.SingleOrDefault();
- //}
- return _retVal;
- }
- public TeamMember CreateByClientUID(Guid cuid, TeamMember value, bool saveNow = true)
- {
- value.UID = Guid.NewGuid();
- using (PayrollContext _context = value.Access.Name.In(_gtAccessList)
- ? new PayrollContext(CurrentLoggedIn, true)
- : new PayrollContext(CurrentLoggedIn))
- {
- var _q = from _cc in _context.tbl_ConfClient
- join _c
- in _context.tbl_Client
- on _cc.ClientID equals _c.ClientID
- where _c.ClientUID == cuid
- select _cc;
- var _confClient = _q.SingleOrDefault();
- if (_confClient != null)
- {
- if (_confClient.tbl_TeamMemberColl == null)
- {
- _confClient.tbl_TeamMemberColl = new tbl_TeamMemberColl
- {
- TeamMemberCollUID = Guid.NewGuid()
- };
- }
- tbl_TeamMember _teamMember = new tbl_TeamMember
- {
- TeamMemberUID = value.UID,
- EmpID = GetRepo<IEmployeeRepository>().UID2ID(value.Employee.UID),
- AccessID = GetRepo<IAccessRepository>().UID2ID(value.Access.UID),
- Status = (byte)value.Status,
- StatusChangedOn = DateTime.Now,
- tbl_AccessScope = new tbl_AccessScope
- {
- AccessScopeUID = Guid.NewGuid(),
- AccessScopeName = Guid.NewGuid().ToString()
- }
- };
- //if (value.StatusChangedBy != null)
- //{
- _teamMember.StatusChangedByID = CurrentLoggedIn.EmployeeID;
- //}
- _confClient.tbl_TeamMemberColl.tbl_TeamMember.Add(_teamMember);
- }
- _context.SaveChanges();
- }
- return RetrieveByUID(value.UID);
- }
- public ResultSummary<TeamMember> GetByClientUID(Guid cuid, JSFilter filtering = null, JSPaging paging = null,
- JSSort sorting = null)
- {
- //using (var _context = new PayrollContext(CurrentLoggedIn))
- //{
- IQueryable<TeamMember> result = null;
- result = Context.vw_ClientTeamMembers.Where(x => x.ClientUID == cuid).Select(x => new TeamMember
- {
- UID = x.TeamMemberUID,
- Employee = new EmployeeSummary
- {
- UID = x.EmpUID,
- Code = x.EmpCode,
- FullName = x.FullName
- },
- Access = new Access
- {
- UID = x.AccessUID,
- Name = x.AccessName
- },
- AccessScope = new AccessScope
- {
- UID = x.AccessScopeUID,
- Name = x.AccessScopeName,
- Description = x.Description
- },
- Scope = (Scope)x.SOURCE,
- Status = (TeamMemberStatus)x.STATUS,
- StatusChangedOn = x.StatusChangedOn,
- StatusChangedBy = x.StatusChangedByID == null ? null : new Employee
- {
- ID = x.StatusChangedByID.Value,
- EmployeeInfo = new EmployeeInfo
- {
- ID = x.StatusChangedByID.Value,
- NickName = x.StatusChangedByNickName
- }
- }
- });
- ResultSummary<TeamMember> _retVal = new ResultSummary<TeamMember>
- {
- Result = result
- };
- #region Commented Out
- //ResultSummary<TeamMember> _retVal = new ResultSummary<TeamMember>
- //{
- // Result = from _vt in Context.vw__CCTeamMember
- // join _t
- // in Context.vw__TeamMember_StatusDetails
- // on _vt.TeamMemberID equals _t.TeamMemberID
- // join _e
- // in Context.vw__Employee_ClientAndGT
- // on _t.EmpID equals _e.EmpID
- // join _a
- // in Context.tbl_Access
- // on _t.AccessID equals _a.AccessID
- // join _as
- // in Context.tbl_AccessScope
- // on _t.AccessScopeID equals _as.AccessScopeID
- // orderby _e.FullName
- // where _vt.ClientUID == cuid
- // select new TeamMember
- // {
- // UID = _t.TeamMemberUID,
- // Employee = new EmployeeSummary
- // {
- // UID = _e.EmpUID,
- // Code = _e.EmpCode,
- // FullName = _e.FullName
- // },
- // Access = new Access
- // {
- // UID = _a.AccessUID,
- // Name = _a.AccessName
- // },
- // AccessScope = new AccessScope
- // {
- // UID = _as.AccessScopeUID,
- // Name = _as.AccessScopeName,
- // Description = _as.Description
- // },
- // Scope = (Scope)_e.Source,
- // Status = (TeamMemberStatus)_t.Status,
- // StatusChangedOn = _t.StatusChangedOn,
- // StatusChangedBy = _t.StatusChangedByID == null ? null : new Employee
- // {
- // ID = _t.StatusChangedByID.Value,
- // EmployeeInfo = new EmployeeInfo
- // {
- // ID = _t.StatusChangedByID.Value,
- // NickName = _t.StatusChangedByNickName
- // }
- // }
- // }
- //};
- #endregion
- if (filtering != null)
- {
- foreach (var _jsFilter in filtering.Filters)
- {
- string _field = _jsFilter.Field.ToLower();
- string _value = _jsFilter.Value;
- switch (_field)
- {
- case "scope":
- Scope _scope;
- if (Enum.TryParse(_value, true, out _scope))
- {
- _retVal.Result = _retVal.Result.Where(a => a.Scope == _scope);
- }
- break;
- case "search":
- _retVal.Result = _retVal.Result.Where(a => a.Employee.FullName.Contains(_value));
- break;
- }
- }
- }
- _retVal.TotalRows = _retVal.Result.Count();
- if (paging != null)
- {
- _retVal.Result = _retVal.Result.OrderBy(x => x.Employee.FullName).Skip(paging.Skip).Take(paging.Take);
- }
- return _retVal;
- //}
- }
- public TeamMember ChangeStatusByTeamMemberUID(Guid TeamMemberUID, TeamMemberStatus Status)
- {
- using (PayrollContext _context = new PayrollContext(CurrentLoggedIn))
- {
- var teamMember = _context.tbl_TeamMember.SingleOrDefault(a => a.TeamMemberUID == TeamMemberUID);
- if (teamMember == null)
- {
- throw new System.ArgumentException("Team Member not exist!");
- }
- teamMember.Status = (byte)Status;
- teamMember.StatusChangedOn = DateTime.Now;
- teamMember.StatusChangedByID = CurrentLoggedIn.EmployeeID;
- _context.SaveChanges();
- }
- return RetrieveByUID(TeamMemberUID);
- }
- public TeamMember OverrideFromClient(Guid cuid, TeamMember entity)
- {
- throw new NotImplementedException();
- }
- public ResultSummary<TeamMember> GetByEmpUID(JSFilter filtering = null, JSPaging paging = null, JSSort sorting = null)
- {
- ResultSummary<TeamMember> _retVal = new ResultSummary<TeamMember>
- {
- Result = from _e in Context.tbl_Emp
- join _t
- in Context.vw__TeamMember_StatusDetails
- on _e.EmpID equals _t.EmpID
- join _cc
- in Context.tbl_ConfClient
- on _t.TeamMemberCollID equals _cc.TeamMemberCollID
- join _a
- in Context.tbl_Access
- on _t.AccessID equals _a.AccessID
- join _as
- in Context.tbl_AccessScope
- on _t.AccessScopeID equals _as.AccessScopeID
- orderby _e.FullName
- where _e.EmpUID == CurrentLoggedIn.EmployeeUID
- select new TeamMember
- {
- UID = _t.TeamMemberUID,
- Employee = new EmployeeSummary
- {
- UID = _e.EmpUID,
- Code = _e.EmpCode,
- FullName = _e.FullName
- },
- Access = new Access
- {
- UID = _a.AccessUID,
- Name = _a.AccessName
- },
- AccessScope = new AccessScope
- {
- UID = _as.AccessScopeUID,
- Name = _as.AccessScopeName,
- Description = _as.Description
- },
- Scope = (Scope)_t.Status,
- Status = (TeamMemberStatus)_t.Status,
- StatusChangedOn = _t.StatusChangedOn,
- StatusChangedBy = _t.StatusChangedByID == null
- ? null
- : new Employee
- {
- ID = _t.StatusChangedByID.Value,
- EmployeeInfo = new EmployeeInfo
- {
- ID = _t.StatusChangedByID.Value,
- NickName = _t.StatusChangedByNickName
- }
- },
- ConfigClient = new ConfigClient
- {
- Client = new Client
- {
- UID = _cc.tbl_Client.ClientUID,
- ID = _cc.ClientID,
- Name = _cc.tbl_Client.ClientName
- }
- },
- CanUpdate = (_t.Status == (byte)TeamMemberStatus.Pending) || CurrentUser.IsSuperAdmin,
- CanDelete = (_t.Status == (byte)TeamMemberStatus.Pending) || CurrentUser.IsSuperAdmin
- }
- };
- if (filtering != null)
- {
- foreach (var _jsFilter in filtering.Filters)
- {
- string _field = _jsFilter.Field.ToLower();
- string _value = _jsFilter.Value;
- switch (_field)
- {
- //case "scope":
- // Scope _scope;
- // if (Enum.TryParse(_value, true, out _scope))
- // {
- // _retVal.Result = _retVal.Result.Where(a => a.Scope == _scope);
- // }
- // break;
- case "search":
- _retVal.Result = _retVal.Result.Where(a => a.ConfigClient.Client.Name.Contains(_value)
- || a.Access.Name.Contains(_value));
- break;
- }
- }
- }
- if (sorting != null)
- {
- _retVal.Result = _retVal.Result.OrderBy(sorting.Field + (sorting.Direction == SortingDirection.Descending ? " descending" : ""));
- }
- _retVal.TotalRows = _retVal.Result.Count();
- if (paging != null)
- {
- _retVal.Result = _retVal.Result.Skip(paging.Skip).Take(paging.Take);
- }
- return _retVal;
- }
- public TeamMember CreateByEmpUID(TeamMember value, bool saveNow = true)
- {
- TeamMember _retVal = null;
- value.UID = Guid.NewGuid();
- using (PayrollContext _context = new PayrollContext(CurrentLoggedIn))
- {
- var _q = from _cc in _context.tbl_ConfClient
- join _c
- in _context.tbl_Client
- on _cc.ClientID equals _c.ClientID
- where _c.ClientUID == value.ConfigClient.Client.UID
- select _cc;
- var _confClient = _q.SingleOrDefault();
- if (_confClient != null)
- {
- if (_confClient.tbl_TeamMemberColl == null)
- {
- _confClient.tbl_TeamMemberColl = new tbl_TeamMemberColl
- {
- TeamMemberCollUID = Guid.NewGuid()
- };
- }
- tbl_TeamMember _teamMember = new tbl_TeamMember
- {
- TeamMemberUID = value.UID,
- EmpID = GetRepo<IEmployeeRepository>().UID2ID(CurrentLoggedIn.EmployeeUID),
- AccessID = GetRepo<IAccessRepository>().UID2ID(value.Access.UID),
- Status = (byte)value.Status,
- StatusChangedOn = DateTime.Now,
- tbl_AccessScope = new tbl_AccessScope
- {
- AccessScopeUID = Guid.NewGuid(),
- AccessScopeName = Guid.NewGuid().ToString()
- }
- };
- //if (value.StatusChangedBy != null)
- //{
- _teamMember.StatusChangedByID = CurrentLoggedIn.EmployeeID;
- //}
- _confClient.tbl_TeamMemberColl.tbl_TeamMember.Add(_teamMember);
- _context.SaveChanges();
- }
- }
- _retVal = RetrieveByUID(value.UID);
- _retVal.CanUpdate = _retVal.Status == (byte)TeamMemberStatus.Pending;
- _retVal.CanDelete = _retVal.Status == (byte)TeamMemberStatus.Pending;
- return _retVal;
- }
- public override void SoftDelete(params Guid[] uid)
- {
- using (PayrollContext _context = new PayrollContext(CurrentLoggedIn, true))
- {
- var _modelPair = ModelMapping.GetModelPair<TeamMember>();
- if (_modelPair == null)
- {
- throw new Exception("Unable to delete entry, " + typeof(TeamMember).Name + " is not mapped.");
- }
- string _efFull = _modelPair.ToString(true);
- if (!string.IsNullOrEmpty(_efFull))
- {
- //throw new NotImplementedException();
- var _result = _context.usp_DeleteEntries(CurrentLoggedIn.EmployeeUID,
- _efFull,
- string.Join(",", uid),
- false
- )
- .ToList();
- if (_result.Any(a => a.StartsWith("DELETE ERROR")))
- {
- throw new Exception("DEL_Err " + _result.First());
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement