using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using Ease.CoreV35.Caching; using Ease.CoreV35.Model; using System.Globalization; namespace Payroll.BO { #region AttnRawData [Serializable] public class AttnRawData : ObjectTemplate { #region Cache Store private static Cache _cache = new Cache(typeof(AttnRawData)); #endregion #region Constructor public AttnRawData() { _cardID = null; _employeeID = null; _cardNo = string.Empty; _punchTime = DateTime.MinValue; _entryMode = EnumEntryMode.Unknown; } #endregion #region Properties #region cardID : ID private ID _cardID; public ID CardID { get { return _cardID; } set { base.OnPropertyChange("cardID", _cardID, value); _cardID = value; } } #endregion #region employeeID : ID private ID _employeeID; public ID EmployeeID { get { return _employeeID; } set { base.OnPropertyChange("employeeID", _employeeID, value); _employeeID = value; } } #endregion #region cardNo : string private string _cardNo; public string CardNo { get { return _cardNo; } set { base.OnPropertyChange("cardNo", _cardNo, value); _cardNo = value; } } #endregion #region punchTime : string private DateTime _punchTime; public DateTime PunchTime { get { return _punchTime; } set { base.OnPropertyChange("punchTime", _punchTime, value); _punchTime = value; } } #endregion #region entryMode : EnumEntryMode private EnumEntryMode _entryMode; public EnumEntryMode EntryMode { get { return _entryMode; } set { base.OnPropertyChange("entryMode", (short)_entryMode, (short)value); _entryMode = value; } } #endregion #region AccessCard : AccessCard private AccessCard _accessCard; public AccessCard AccessCard { get { if (_cardID.Integer > 0 && _accessCard == null) { _accessCard = new AccessCard(); _accessCard = AccessCard.Get(_cardID); } return this._accessCard; } set { _accessCard = value; } } #endregion #region DeviceIPAddress : string private string _deviceIPAddress; public string DeviceIPAddress { get { return _deviceIPAddress; } set { base.OnPropertyChange("DeviceIPAddress", _deviceIPAddress, value); _deviceIPAddress = value; } } #endregion #region cardNo : string private string _deviceNo; public string DeviceNo { get { return _deviceNo; } set { base.OnPropertyChange("DeviceNo", _deviceNo, value); _deviceNo = value; } } #endregion #region Service Factory IAttnRawDataService : IAttnRawDataService internal static IAttnRawDataService Service { get { return Services.Factory.CreateService(typeof(IAttnRawDataService)); } } #endregion #endregion #region Functions #region Get public static AttnRawData Get(ID nID) { AttnRawData oAttnRawData = null; #region Cache Header oAttnRawData = (AttnRawData)_cache["Get", nID]; if (oAttnRawData != null) return oAttnRawData; #endregion oAttnRawData = AttnRawData.Service.Get(nID); #region Cache Footer _cache.Add(oAttnRawData, "Get", nID); #endregion return oAttnRawData; } public static ObjectsTemplate Get() { #region Cache Header ObjectsTemplate attnRawDatas = _cache["Get"] as ObjectsTemplate; if (attnRawDatas != null) return attnRawDatas; #endregion try { attnRawDatas = Service.Get(); } catch (ServiceException e) { throw new Exception(e.Message, e); } #region Cache Footer _cache.Add(attnRawDatas, "Get"); #endregion return attnRawDatas; } public static ObjectsTemplate Get(DateTime fromDate, DateTime toDate) { #region Cache Header ObjectsTemplate attnRawDatas = _cache["Get", fromDate, toDate] as ObjectsTemplate; if (attnRawDatas != null) return attnRawDatas; #endregion try { attnRawDatas = Service.Get(fromDate, toDate); } catch (ServiceException e) { throw new Exception(e.Message, e); } #region Cache Footer _cache.Add(attnRawDatas, "Get", fromDate, toDate); #endregion return attnRawDatas; } public static DataTable GetInOutData(DateTime startDate, DateTime endDate, int employeeID) { try { return Service.GetInOutData(startDate, endDate, employeeID); } catch (ServiceException e) { throw new Exception(e.Message, e); } } public static bool IsDataExist(DateTime fromDate, DateTime toDate) { try { return Service.IsDataExist(fromDate, toDate); } catch (ServiceException e) { throw new Exception(e.Message, e); } } #endregion #region Save public ID Save() { return AttnRawData.Service.Save(this); } public void Save(DataTable rawData, ref List _errorCardsNo, ref ObjectsTemplate _errorAccCards) { #region DataPreparation ObjectsTemplate attnRawDatas = new ObjectsTemplate(); ObjectsTemplate employees = Employee.Get(); ObjectsTemplate accessCards = AccessCard.Get(); List employeeList = employees.ToList(); AttnRawData at = new AttnRawData(); Employee employee = new Employee(); foreach (DataRow dr in rawData.Rows) { string cardNo = Convert.ToString(dr["CardNo"]); AccessCard accCard = accessCards.Find(delegate(AccessCard item) { return item.CardNumber == cardNo; }); if (accCard != null) { var em = from tempEmployee in employeeList.ToList() where tempEmployee.CardID == accCard.ID select tempEmployee; employee = em.FirstOrDefault(); if (employee != null) { at = new AttnRawData(); at.CardID = accCard.ID; at.CardNo = accCard.CardNumber; at.EmployeeID = employee.ID; at.PunchTime = Convert.ToDateTime(dr["Time"]); attnRawDatas.Add(at); } else { _errorAccCards.Add(accCard); } } else { _errorCardsNo.Add(cardNo); } } #endregion AttnRawData.Service.Save(attnRawDatas); } public void SaveWithEmpID(DataTable rawData, ref List _errorCardsNo, ref ObjectsTemplate _errorAccCards) { #region DataPreparation ObjectsTemplate attnRawDatas = new ObjectsTemplate(); ObjectsTemplate employees = Employee.Get(); //ObjectsTemplate accessCards = AccessCard.Get(); List employeeList = employees.ToList(); AttnRawData at = new AttnRawData(); Employee employee = new Employee(); foreach (DataRow dr in rawData.Rows) { string cardNo = Convert.ToString(dr["EmpNo"]); //AccessCard accCard = accessCards.Find(delegate(AccessCard item) { return item.CardNumber == cardNo; }); var em = from tempEmployee in employeeList.ToList() where tempEmployee.EmployeeNo == cardNo select tempEmployee; employee = em.FirstOrDefault(); if (employee != null) { at = new AttnRawData(); //at.CardID = accCard.ID; //at.CardNo = accCard.CardNumber; at.EmployeeID = employee.ID; at.PunchTime = Convert.ToDateTime(dr["Time"]); at.DeviceIPAddress = "1"; at.DeviceNo = Convert.ToString(dr["DeviceNo"]); ; attnRawDatas.Add(at); } } #endregion AttnRawData.Service.Save(attnRawDatas); } #endregion #region Delete public static void Delete(DateTime fromTime) { AttnRawData.Service.Delete(fromTime); } #endregion #region For Attendance Process public static void CollectRawData() { bool isInOutApplicable = ConfigurationManager.GetBoolValue("attendence", "inoutapplicable", EnumConfigurationType.Logic); CollectRawData(new AttnProcessRunSummary(), isInOutApplicable); } public static void CollectRawData(AttnProcessRunSummary oRunSummary) { bool isInOutApplicable = ConfigurationManager.GetBoolValue("attendence", "inoutapplicable", EnumConfigurationType.Logic); CollectRawData(oRunSummary, isInOutApplicable); } // This is for ECho(Running Script Manually). Do not Delete public static void RunScriptCollectRawData() { Service.RunScriptCollectRawData(); } private static void CollectRawData(AttnProcessRunSummary oRunSummary, bool isInOutApplicable) { ObjectsTemplate attnRawDatas = new ObjectsTemplate(); DataTable dTable = AttnRawData.Service.GetCoatsData(); ObjectsTemplate oAccessCard = AccessCard.Get(EnumCardStatus.Attached, EnumCardStatus.Temporary, EnumCardStatus.Found); ObjectsTemplate oEmployees = Employee.GetAllEmps(); bool isEmpnoAndCardnoSame = ConfigurationManager.GetBoolValue("attendence", "isempnoandcardnosame", EnumConfigurationType.Logic); AccessCard accessCard = null; foreach (DataRow item in dTable.Rows) { string sEmployeeNo = item["EmployeeID"].ToString(); Employee employee = oEmployees.Where(o => o.EmployeeNo.ToUpper() == sEmployeeNo.ToUpper().Trim()) .FirstOrDefault(); if (employee == null) { oRunSummary.AddError(EnumErrorType.Others, "Raw Data Contains Employee's Information which is not found in system", new Employee() { EmployeeNo = sEmployeeNo }); continue; } if (!isEmpnoAndCardnoSame) { accessCard = oAccessCard.Where(o => o.CardNumber.ToUpper() == sEmployeeNo.ToUpper().Trim()) .FirstOrDefault(); if (accessCard == null) { oRunSummary.AddError(EnumErrorType.Others, "Raw Data Contains an Employee's information who's card is not assigned in system", employee); continue; } } AttnRawData oAttnRaw = new AttnRawData(); oAttnRaw.EmployeeID = employee.ID; oAttnRaw.PunchTime = Convert.ToDateTime(item["PunchTime"].ToString()); if (isEmpnoAndCardnoSame) { oAttnRaw.CardNo = employee.EmployeeNo; } else { oAttnRaw.CardID = accessCard.ID; oAttnRaw.CardNo = accessCard.CardNumber; } oAttnRaw.DeviceIPAddress = item["DeviceIPAddress"].ToString(); oAttnRaw.DeviceNo = item["DeviceNo"].ToString(); if (isInOutApplicable) { oAttnRaw.EntryMode = (EnumEntryMode)Convert.ToInt32(item["InOut"].ToString()); } else { oAttnRaw.EntryMode = EnumEntryMode.Unknown; } attnRawDatas.Add(oAttnRaw); } AttnRawData.Service.Save(attnRawDatas); } public static DataSet GetEmployeeNo(DateTime workdate) { DataSet employeeNo = null; try { employeeNo = Service.GetEmployeeNo(workdate); } catch (ServiceException e) { throw new Exception(e.Message, e); } return employeeNo; } #endregion #endregion } #endregion #region IAttnRawData Service public interface IAttnRawDataService { AttnRawData Get(ID id); ObjectsTemplate Get(); ObjectsTemplate Get(DateTime fromDate, DateTime toDate); bool IsDataExist(DateTime fromdate, DateTime todate); ID Save(AttnRawData item); void Save(ObjectsTemplate attnRawDatas); void Save(ObjectsTemplate attnRawDatas, DataTable rawData, ObjectsTemplate employees); void Delete(ID id); void Delete(DateTime fromTime); DataSet GetEmployeeNo(DateTime workdate); DataTable GetCoatsData(); DataTable GetInOutData(DateTime startDate, DateTime endDate, int employeeID); void RunScriptCollectRawData(); } #endregion }