using System; using System.Data; using Ease.Core.Model; using Ease.Core.DataAccess; using Ease.Core; using System.Collections.Generic; using Ease.Core.Utility; using System.Linq; using HRM.BO; namespace HRM.DA { #region ProductionBonusSetup Service public class ProductionBonusSetupService : ServiceTemplate, IProductionBonusSetupService { public ProductionBonusSetupService() { } private void MapObject(ProductionBonusSetup oProductionBonusSetup, DataReader oReader) { //oProductionBonusSetup.int = oReader.GetInt32("ProductionBonusSetupID"); base.SetObjectID(oProductionBonusSetup, (oReader.GetInt32("ProductionBonusSetupID").Value)); oProductionBonusSetup.DesignNo = oReader.GetString("DesignNo"); oProductionBonusSetup.ProgramName = oReader.GetString("ProgramName"); oProductionBonusSetup.AchivedPercent = oReader.GetDouble("AchivedPercent").GetValueOrDefault(); oProductionBonusSetup.OTHour = oReader.GetDouble("OTHour").GetValueOrDefault(); oProductionBonusSetup.FromDate = oReader.GetDateTime("FromDate").Value; oProductionBonusSetup.ToDate = oReader.GetDateTime("ToDate").Value; oProductionBonusSetup.SalaryMonth = oReader.GetDateTime("SalaryMonth").Value; oProductionBonusSetup.MaxPerson = oReader.GetInt32("MaxPerson").Value; oProductionBonusSetup.ProductionBonusType = (EnumProductionBonusType)oReader.GetInt32("ProductionBonusType").Value; oProductionBonusSetup.Status = (EnumBonusStatus)oReader.GetInt32("Status").Value; oProductionBonusSetup.CreatedBy = oReader.GetInt32("CreatedBy", 0); oProductionBonusSetup.CreatedDate = oReader.GetDateTime("CreationDate").HasValue ? oReader.GetDateTime("CreationDate").Value : DateTime.MinValue; this.SetObjectState(oProductionBonusSetup, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { ProductionBonusSetup oProductionBonusSetup = new ProductionBonusSetup(); MapObject(oProductionBonusSetup, oReader); return oProductionBonusSetup as T; } protected ProductionBonusSetup CreateObject(DataReader oReader) { ProductionBonusSetup oProductionBonusSetup = new ProductionBonusSetup(); MapObject(oProductionBonusSetup, oReader); return oProductionBonusSetup; } private void MapLineObject(ProdBonusLine oProdBonusLine, DataReader oReader) { //oProdBonusLine.int = oReader.GetInt32("ProdBonusLineID"); base.SetObjectID(oProdBonusLine, (oReader.GetInt32("ProdBonusLineID").Value)); oProdBonusLine.ProdBonusSetupID = oReader.GetInt32("ProdBonusSetupID", 0); // oProdBonusLine.EmployeeID = oReader.GetInt32("EmployeeID"); oProdBonusLine.LineName = oReader.GetString("LineName"); oProdBonusLine.LineAchievedPercent = oReader.GetDouble("LineAchievedPercent").GetValueOrDefault(); oProdBonusLine.LineOTHour = oReader.GetDouble("LineOTHour").GetValueOrDefault(); oProdBonusLine.ScheduledHour = oReader.GetDouble("ScheduledHour").GetValueOrDefault(); this.SetObjectState(oProdBonusLine, Ease.Core.ObjectState.Saved); } private ProdBonusLine CreateProdBonusLineObject(DataReader oReader) { ProdBonusLine item = new ProdBonusLine(); MapLineObject(item, oReader); return item; } protected List CreateProdBonusLineObjects(DataReader oReader) { List oItems = new List(); while (oReader.Read()) { ProdBonusLine item = CreateProdBonusLineObject(oReader); oItems.Add(item); } return oItems; } private void MapScheduleObject(ProdBonusWorkSchedule oProdBonusWorkSchedule, DataReader oReader) { //oProdBonusWorkSchedule.int = oReader.GetInt32("ProdBonusWorkScheduleID"); base.SetObjectID(oProdBonusWorkSchedule, (oReader.GetInt32("ProdBonusWorkScheduleID").Value)); oProdBonusWorkSchedule.ProdBonusSetupID = oReader.GetInt32("ProdBonusSetupID", 0); oProdBonusWorkSchedule.StartDateTime = oReader.GetDateTime("StartDateTime").Value; oProdBonusWorkSchedule.EndDateTime = oReader.GetDateTime("EndDateTime").Value; this.SetObjectState(oProdBonusWorkSchedule, Ease.Core.ObjectState.Saved); } private ProdBonusWorkSchedule CreateScheduleObject(DataReader oReader) { ProdBonusWorkSchedule item = new ProdBonusWorkSchedule(); MapScheduleObject(item, oReader); return item; } protected List CreateScheduleObjects(DataReader oReader) { List oItems = new List(); while (oReader.Read()) { ProdBonusWorkSchedule item = CreateScheduleObject(oReader); oItems.Add(item); } return oItems; } #region Service implementation public ProductionBonusSetup Get(int id) { ProductionBonusSetup oProductionBonusSetup = new ProductionBonusSetup(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(ProductionBonusSetupDA.Get(tc, id)); if (oreader.Read()) { oProductionBonusSetup = 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 oProductionBonusSetup; } public ProductionBonusSetup Get(string DesignNo) { ProductionBonusSetup oProductionBonusSetup = new ProductionBonusSetup(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(ProductionBonusSetupDA.Get(tc, DesignNo)); if (oreader.Read()) { oProductionBonusSetup = 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 oProductionBonusSetup; } public List Get(DateTime dtSalaryMonth) { List ProductionBonusSetups = null; List oProdBonusLines = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(ProductionBonusSetupDA.Get(tc, dtSalaryMonth)); ProductionBonusSetups = this.CreateObjects(dr); dr.Close(); if (ProductionBonusSetups.Count > 0) { dr = new DataReader(ProductionBonusSetupDA.GetProductionBonusLine(tc, ProductionBonusSetups.CommaSeparatedIDs())); oProdBonusLines = this.CreateProdBonusLineObjects(dr); dr.Close(); int check = 0; foreach (ProductionBonusSetup ProductionBonusSetup in ProductionBonusSetups) { check = 0; ProductionBonusSetup.ProductionBonusLinesNew = new List(); foreach (ProdBonusLine line in oProdBonusLines) { if (ProductionBonusSetup.ID == line.ProdBonusSetupID) { ProductionBonusSetup.ProductionBonusLinesNew.Add(line); check = 1; } else { if (check == 1) break; } } } } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return ProductionBonusSetups; } public List Get() { List ProductionBonusSetups = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(ProductionBonusSetupDA.Get(tc)); ProductionBonusSetups = 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 ProductionBonusSetups; } public DataTable GetLayout(DateTime dtCreatedDate) { //List ProductionBonusSetups = null; TransactionContext tc = null; DataTable dt = null; try { tc = TransactionContext.Begin(); //DataReader dr = new DataReader(ProductionBonusSetupDA.GetLayout(tc, dtCreatedDate)); //ProductionBonusSetups = this.CreateObjects(dr); //dr.Close(); dt = ProductionBonusSetupDA.GetLayout(tc, dtCreatedDate); 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 DataTable GetLayoutMonthly(DateTime dtSalaryMonth) { //List ProductionBonusSetups = null; TransactionContext tc = null; DataTable dt = null; try { tc = TransactionContext.Begin(); //DataReader dr = new DataReader(ProductionBonusSetupDA.GetLayout(tc, dtCreatedDate)); //ProductionBonusSetups = this.CreateObjects(dr); //dr.Close(); dt = ProductionBonusSetupDA.GetLayoutMonthly(tc, dtSalaryMonth); 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 List Get(EnumBonusStatus status) { List ProductionBonusSetups = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(ProductionBonusSetupDA.Get(tc, status)); ProductionBonusSetups = 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 ProductionBonusSetups; } public List GetProductionBonusLine(int id) { List oProdBonusLine = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(ProductionBonusSetupDA.GetProductionBonusLine(tc, id)); oProdBonusLine = this.CreateProdBonusLineObjects(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 oProdBonusLine; } public List GetProdBonusParameters(int iD) { List ProdBonusParameters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(ProductionBonusSetupDA.GetProdBonusParameters(tc, iD)); ProdBonusParameters = 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 ProdBonusParameters; } public List GetProdBonusWorkSchedules(int iD) { List ProdBonusWorkSchedules = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(ProductionBonusSetupDA.GetProdBonusWorkSchedules(tc, iD)); ProdBonusWorkSchedules = this.CreateScheduleObjects(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 ProdBonusWorkSchedules; } public int Save(ProductionBonusSetup oProductionBonusSetup) { TransactionContext tc = null; try { #region Get Uncalled Properties foreach (var item in oProductionBonusSetup.ProductionBonusLines) { var temp = item.ProdBonusWorkSchedules; var temp1 = item.ProdBonusSupervisors; var temp2 = item.ProdBonusParameters; } #endregion if (oProductionBonusSetup.IsNew) { #region New tc = TransactionContext.Begin(true); int id = tc.GenerateID("ProductionBonusSetup", "ProductionBonusSetupID"); base.SetObjectID(oProductionBonusSetup, (id)); List pbs = new ProductionBonusSetupService().Get(); if (pbs.Count > 0) { ProductionBonusSetup pbsdesign = pbs.Find(x=> x.DesignNo.ToLower() == oProductionBonusSetup.DesignNo.ToLower()); if(pbsdesign != null) { throw new Exception("The Layout No already exixts. Layout No must be Unique"); } //if (x.DesignNo == oProductionBonusSetup.DesignNo) // throw new InvalidOperationException("The Layout No already exixts. Layout No must be Unique"); } ProductionBonusSetupDA.Insert(tc, oProductionBonusSetup); #endregion } else { #region Update #region Get Prev ProdLines List oldLines = GetProductionBonusLine(oProductionBonusSetup.ID); foreach (var item in oldLines) { item.ProdBonusWorkSchedules = (new ProdBonusLineService()).GetProdBonusWorkSchedules(item.ID); } #endregion tc = TransactionContext.Begin(true); ProductionBonusSetupDA.Update(tc, oProductionBonusSetup); #region Delete Normal Childs //ProductionBonusSetupDA.DeleteAttn(tc, oProductionBonusSetup.ID); ProductionBonusSetupDA.DeleteSupervisor(tc, oProductionBonusSetup.ID); ProductionBonusSetupDA.DeleteParameter(tc, oProductionBonusSetup.ID); #endregion #region Delete Prod Lines with Schedule foreach (var line in oldLines) { if (!oProductionBonusSetup.ProductionBonusLines.Any(x => x.ID == line.ID)) { foreach (var shedule in line.ProdBonusWorkSchedules) { ProductionBonusSetupDA.DeleteSchedule(tc, shedule.ID); } ProductionBonusSetupDA.DeleteLine(tc, line.ID); } } #endregion #endregion } #region Insert Or Update Prod Lines foreach (ProdBonusLine oProdBonusLine in oProductionBonusSetup.ProductionBonusLines) { //ProdBonusLineDA.DeleteChiled(tc, oProdBonusLine.ID); if (oProdBonusLine.IsNew) { #region New oProdBonusLine.ProdBonusSetupID = oProductionBonusSetup.ID; base.SetObjectID(oProdBonusLine, (tc.GenerateID("ProdBonusLine", "ProdBonusLineID"))); ProdBonusLineDA.Insert(tc, oProdBonusLine); #endregion } else { #region Update ProdBonusLineDA.Update(tc, oProdBonusLine); #region Delete WorkSchedule List oldWorkSchedule = (new ProdBonusLineService()).GetProdBonusWorkSchedules(tc, oProdBonusLine.ID); foreach (var wSchedule in oldWorkSchedule) { if (!oProdBonusLine.ProdBonusWorkSchedules.Any(x => x.ID == wSchedule.ID)) { ProductionBonusSetupDA.DeleteSchedule(tc, wSchedule.ID); } } #endregion #endregion } #region Insert Normal Childs foreach (ProdBonusSupervisor supervisor in oProdBonusLine.ProdBonusSupervisors) { base.SetObjectID(supervisor, (tc.GenerateID("ProdBonusSupervisor", "ProdBonusSupervisorID"))); supervisor.ProdBonusSetupID = oProductionBonusSetup.ID; supervisor.ProdBonusLineID = oProdBonusLine.ID; ProdBonusSupervisorDA.Insert(tc, supervisor); } foreach (ProdBonusParameter param in oProdBonusLine.ProdBonusParameters) { base.SetObjectID(param, (tc.GenerateID("ProdBonusParameter", "ProdBonusParameterID"))); param.ProdBonusSetupID = oProductionBonusSetup.ID; param.ProdBonusLineID = oProdBonusLine.ID; ProdBonusParameterDA.Insert(tc, param); } #endregion #region Workschedule Insert Or Update foreach (ProdBonusWorkSchedule oProdBonusWorkSchedule in oProdBonusLine.ProdBonusWorkSchedules) { if (oProdBonusWorkSchedule.IsNew) { base.SetObjectID(oProdBonusWorkSchedule, (tc.GenerateID("ProdBonusWorkSchedule", "ProdBonusWorkScheduleID"))); oProdBonusWorkSchedule.ProdBonusSetupID = oProductionBonusSetup.ID; oProdBonusWorkSchedule.ProdBonusLineID = oProdBonusLine.ID; ProdBonusWorkScheduleDA.Insert(tc, oProdBonusWorkSchedule); } else { ProdBonusWorkScheduleDA.Update(tc, oProdBonusWorkSchedule); } } #endregion } #endregion tc.End(); return oProductionBonusSetup.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); ProductionBonusSetupDA.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 } } #endregion #region IProductionBonusSetupService Members #endregion } #endregion }