EchoTex_Payroll/HRM.DA/Service/Bonus/ProductionBonusSetupService.cs
2024-10-14 10:01:49 +06:00

662 lines
23 KiB
C#

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<T>(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<ProdBonusLine> CreateProdBonusLineObjects(DataReader oReader)
{
List<ProdBonusLine> oItems = new List<ProdBonusLine>();
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<ProdBonusWorkSchedule> CreateScheduleObjects(DataReader oReader)
{
List<ProdBonusWorkSchedule> oItems = new List<ProdBonusWorkSchedule>();
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<ProductionBonusSetup>(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<ProductionBonusSetup>(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<ProductionBonusSetup> Get(DateTime dtSalaryMonth)
{
List<ProductionBonusSetup> ProductionBonusSetups = null;
List<ProdBonusLine> oProdBonusLines = new List<ProdBonusLine>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(ProductionBonusSetupDA.Get(tc, dtSalaryMonth));
ProductionBonusSetups = this.CreateObjects<ProductionBonusSetup>(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<ProdBonusLine>();
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<ProductionBonusSetup> Get()
{
List<ProductionBonusSetup> ProductionBonusSetups = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(ProductionBonusSetupDA.Get(tc));
ProductionBonusSetups = this.CreateObjects<ProductionBonusSetup>(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<ProductionBonusSetup> ProductionBonusSetups = null;
TransactionContext tc = null;
DataTable dt = null;
try
{
tc = TransactionContext.Begin();
//DataReader dr = new DataReader(ProductionBonusSetupDA.GetLayout(tc, dtCreatedDate));
//ProductionBonusSetups = this.CreateObjects<ProductionBonusSetup>(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<ProductionBonusSetup> ProductionBonusSetups = null;
TransactionContext tc = null;
DataTable dt = null;
try
{
tc = TransactionContext.Begin();
//DataReader dr = new DataReader(ProductionBonusSetupDA.GetLayout(tc, dtCreatedDate));
//ProductionBonusSetups = this.CreateObjects<ProductionBonusSetup>(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<ProductionBonusSetup> Get(EnumBonusStatus status)
{
List<ProductionBonusSetup> ProductionBonusSetups = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(ProductionBonusSetupDA.Get(tc, status));
ProductionBonusSetups = this.CreateObjects<ProductionBonusSetup>(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<ProdBonusLine> GetProductionBonusLine(int id)
{
List<ProdBonusLine> oProdBonusLine = new List<ProdBonusLine>();
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<ProdBonusParameter> GetProdBonusParameters(int iD)
{
List<ProdBonusParameter> ProdBonusParameters = new List<ProdBonusParameter>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(ProductionBonusSetupDA.GetProdBonusParameters(tc, iD));
ProdBonusParameters = this.CreateObjects<ProdBonusParameter>(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<ProdBonusWorkSchedule> GetProdBonusWorkSchedules(int iD)
{
List<ProdBonusWorkSchedule> ProdBonusWorkSchedules = new List<ProdBonusWorkSchedule>();
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<ProductionBonusSetup> 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<ProdBonusLine> 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<ProdBonusWorkSchedule> 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
}