using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using HRM.BO; using HRM.DA.DA.Tax; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HRM.DA.Service.Tax { public class TaxReturnService: ServiceTemplate, ITaxReturnService { public TaxReturnService() { } private void MapObject(TaxReturn oTaxReturn, DataReader oReader) { base.SetObjectID(oTaxReturn, oReader.GetInt32("TaxReturnID").Value); oTaxReturn.EmployeeID = oReader.GetInt32("EmployeeID", 0); oTaxReturn.TaxParamID = oReader.GetInt32("TaxParamID", 0); oTaxReturn.Circle = oReader.GetString("Circle"); oTaxReturn.TaxZone = oReader.GetString("TaxZone"); oTaxReturn.TaxUnit = oReader.GetString("TaxUnit"); oTaxReturn.Serial = oReader.GetString("Serial"); oTaxReturn.ReturnStatus= (EnumReturnStatus)oReader.GetInt32("ReturnStatus", true, 0); oTaxReturn.CreatedBy = oReader.GetInt32("CreatedBy", 0); oTaxReturn.CreatedDate = oReader.GetDateTime("CreationDate").Value; oTaxReturn.ModifiedBy = oReader.GetInt32("ModifiedBy", 0); oTaxReturn.ModifiedDate = oReader.GetDateTime("ModifiedDate"); oTaxReturn.ReturnSubmissionDate = (DateTime)oReader.GetDateTime("ReturnSubmissionDate").GetValueOrDefault(DateTime.MinValue); oTaxReturn.TimeExtension = (DateTime)oReader.GetDateTime("TimeExtensionDate").GetValueOrDefault(DateTime.MinValue); oTaxReturn.Reason = oReader.GetString("reason", null); oTaxReturn.AcknowledgedBy = oReader.GetInt32("AcknowledgedBy", 0); oTaxReturn.AcknowledgedDate = oReader.GetDateTime("AcknowledgedDate"); this.SetObjectState(oTaxReturn, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { TaxReturn oTaxReturn = new TaxReturn(); MapObject(oTaxReturn, oReader); return oTaxReturn as T; } protected TaxReturn CreateObject(DataReader oReader) { TaxReturn oTaxReturn = new TaxReturn(); MapObject(oTaxReturn, oReader); return oTaxReturn; } #region Service implementation public TaxReturn Get(int id) { TaxReturn oTaxReturn = new TaxReturn(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(TaxReturnDA.Get(tc, id)); if (oreader.Read()) { oTaxReturn = this.CreateObject(oreader); } oreader.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oTaxReturn; } public TaxReturn Get(int empId, int taxParamId) { TaxReturn oTaxReturn = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(TaxReturnDA.Get(tc, empId, taxParamId)); if (oreader.Read()) { oTaxReturn = this.CreateObject(oreader); } oreader.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oTaxReturn; } public List Get() { List taxReturns = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(TaxReturnDA.Get(tc)); taxReturns = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return taxReturns; } public DataSet GetTaxReturnByEmpId(int empId) { DataSet ds = new DataSet(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); ds = TaxReturnDA.GetTaxReturnByEmpId(tc, empId); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return ds; } public DataSet GetByTaxParamId(int taxParamId) { DataSet ds = new DataSet(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); ds = TaxReturnDA.GetByTaxParamId(tc, taxParamId); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return ds; } public DataSet GetByTaxParamId(int taxParamId, EnumReturnStatus returnStatus) { DataSet ds = new DataSet(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); ds = TaxReturnDA.GetByTaxParamId(tc, taxParamId, returnStatus); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return ds; } public int Save(TaxReturn oTaxReturn) { TransactionContext tc = null; try { tc = TransactionContext.Begin(); if (oTaxReturn.IsNew) { //TaxReturnDA.Delete(tc, oTaxReturn.EmployeeID); TaxReturnDA.DeleteByEmployeeAndTaxParamId(tc, oTaxReturn.EmployeeID, oTaxReturn.TaxParamID); int id = tc.GenerateID("TaxReturn", "TaxReturnID"); base.SetObjectID(oTaxReturn, id); oTaxReturn.CreatedBy = oTaxReturn.EmployeeID; oTaxReturn.CreatedDate = DateTime.Today; TaxReturnDA.Insert(tc, oTaxReturn); } else { TaxReturnDA.Update(tc, oTaxReturn); } if (oTaxReturn.TaxAttachment != null) { TaxReturnAttachment attch = oTaxReturn.TaxAttachment; string base64String = ""; var fdata = oTaxReturn.TaxAttachment.FileData; //Upload Image attch.EmployeeID = oTaxReturn.TaxAttachment.EmployeeID; attch.ReferenceID = oTaxReturn.ID; attch.FileType = (EnumTaxAttachmentType)oTaxReturn.TaxAttachment.FileType; //attch.FileTobase64 = string[] items = oTaxReturn.TaxAttachment.FileTobase64.Split(new char[] { ',', ' ' }, StringSplitOptions.None); byte[] newBytes = Convert.FromBase64String(items[1]); TaxReturnDA.UploadFile(tc,attch, newBytes); } tc.End(); return oTaxReturn.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void UploadFile(TaxReturnAttachment item) { TransactionContext tc = null; try { tc = TransactionContext.Begin(); TaxReturnDA.UploadFile(tc, item); tc.End(); } catch (Exception ex) { if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException("Failed to save file", ex); } } public TaxReturnAttachment GetFile(int empId, int referenceId, EnumTaxAttachmentType type) { TaxReturnAttachment attachment = new TaxReturnAttachment(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(TaxReturnDA.GetFile(tc, empId, referenceId, type)); if (oreader.Read()) { attachment.ID = oreader.GetInt32("taxReturnAttachmentId").Value; attachment.EmployeeID = oreader.GetInt32("empId", 0); attachment.ReferenceID = oreader.GetInt32("ReferenceID", 0); attachment.FileData = oreader.GetLob("FileData"); attachment.FileName = oreader.GetString("fileName"); attachment.FileType = (EnumTaxAttachmentType)oreader.GetInt32("FileType").GetValueOrDefault(); } else { attachment = null; } oreader.Close(); tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException("Failed to Get IRNotifications: " + ex.Message, ex); #endregion } return attachment; } public List GetAllFileByReferenceIds(string referenceIds) { TaxReturnAttachment attachment = new TaxReturnAttachment(); List allAttachment = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(TaxReturnDA.GetAllFileByReferenceIds(tc, referenceIds)); while (oreader.Read()) { attachment = new TaxReturnAttachment(); attachment.ID = oreader.GetInt32("taxReturnAttachmentId").Value; attachment.EmployeeID = oreader.GetInt32("empId", 0); attachment.ReferenceID = oreader.GetInt32("ReferenceID", 0); attachment.FileData = oreader.GetLob("FileData"); attachment.FileName = oreader.GetString("fileName"); attachment.FileType = (EnumTaxAttachmentType)oreader.GetInt32("FileType").GetValueOrDefault(); allAttachment.Add(attachment); } oreader.Close(); tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException("Failed to Get IRNotifications: " + ex.Message, ex); #endregion } return allAttachment; } public void Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); TaxReturnDA.Delete(tc, id); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void DeleteByEmployeeAndTaxParamId(int empId, int taxParamId) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); TaxReturnDA.DeleteByEmployeeAndTaxParamId(tc, empId, taxParamId); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public DataTable Get108AReport(string sEmpNo, int taxParamID) { DataTable dt = new DataTable(); TransactionContext tc = null; try { tc = TransactionContext.Begin(true); dt = TaxReturnDA.Get108AReport(tc, sEmpNo, taxParamID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return dt; } public int UpdateTaxReturnStatus(TaxReturn oTaxReturn) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (!oTaxReturn.IsNew) { TaxReturnDA.UpdateTaxReturnStatus(tc, oTaxReturn); } tc.End(); return oTaxReturn.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } #endregion } }