HRM-10/HRM.DA/Service/TrainingS/TrainingScheduleAttnService.cs

544 lines
17 KiB
C#
Raw Normal View History

2026-11-03 13:06:00 +06:00
using System;
using Ease.Core.Model;
using Ease.Core.DataAccess;
using Ease.Core.Utility;
using System.Collections.Generic;
using HRM.BO;
using System.Data;
using Ease.Core;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
namespace HRM.DA
{
#region Training Service
public class TrainingScheduleAttnService : ServiceTemplate, ITrainingScheduleAttnService
{
#region Private functions and declaration
private void MapObject(TrainingScheduleAttn oTrainingScheduleAttn, DataReader oReader)
{
base.SetObjectID(oTrainingScheduleAttn, (oReader.GetInt32("TSAttnID").Value));
oTrainingScheduleAttn.TrainingScheduleID = oReader.GetInt32("TrainingScheduleID", 0);
oTrainingScheduleAttn.TSDateID = oReader.GetInt32("TSDateID", 0);
oTrainingScheduleAttn.EmployeeID = oReader.GetInt32("EmployeeID", 0);
this.SetObjectState(oTrainingScheduleAttn, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
TrainingScheduleAttn oTrainingScheduleAttng = new TrainingScheduleAttn();
MapObject(oTrainingScheduleAttng, oReader);
return oTrainingScheduleAttng as T;
}
#endregion
#region Implementation of ITrainingScheduleAttnService
#region Get TrainingScheduleAttn
public TrainingScheduleAttn Get(int trainingScheduleAttnID)
{
TrainingScheduleAttn otrainingScheduleAttn = null;
TransactionContext tc = null;
try
{
#region Retrieving data
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(TrainingScheduleAttnDA.Get(tc, trainingScheduleAttnID));
oreader.Read();
otrainingScheduleAttn = CreateObject<TrainingScheduleAttn>(oreader);
tc.End();
#endregion
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message, e);
#endregion
}
return otrainingScheduleAttn;
}
#endregion
#region Get List<TrainingScheduleAttn>
public List<TrainingScheduleAttn> Get()
{
List<TrainingScheduleAttn> trainingScheduleAttns = null;
TransactionContext tc = null;
try
{
#region Retrieving data
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(TrainingScheduleAttnDA.Get(tc));
trainingScheduleAttns = CreateObjects<TrainingScheduleAttn>(oreader);
tc.End();
#endregion
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message, e);
#endregion
}
return trainingScheduleAttns;
}
public List<TrainingScheduleAttn> GetByTrainingScheduleID(int trainingScheduleID)
{
List<TrainingScheduleAttn> trainingScheduleAttns = null;
TransactionContext tc = null;
try
{
#region Retrieving data
tc = TransactionContext.Begin();
DataReader oreader =
new DataReader(TrainingScheduleAttnDA.GetByTrainingScheduleID(tc, trainingScheduleID));
trainingScheduleAttns = CreateObjects<TrainingScheduleAttn>(oreader);
tc.End();
#endregion
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message, e);
#endregion
}
return trainingScheduleAttns;
}
public List<TrainingScheduleAttn> GetByTrainingScheduleIDAndEmployeeID(int trainingScheduleID, int employeeID)
{
List<TrainingScheduleAttn> trainingScheduleAttns = null;
TransactionContext tc = null;
try
{
#region Retrieving data
tc = TransactionContext.Begin();
DataReader oreader =
new DataReader(TrainingScheduleAttnDA.GetByTrainingScheduleIDAndEmployeeID(tc, trainingScheduleID, employeeID));
trainingScheduleAttns = CreateObjects<TrainingScheduleAttn>(oreader);
tc.End();
#endregion
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message, e);
#endregion
}
return trainingScheduleAttns;
}
public List<TrainingScheduleAttn> GetByTrainingScheduleDateID(int trainingScheduleDateID)
{
List<TrainingScheduleAttn> trainingScheduleAttns = null;
TransactionContext tc = null;
try
{
#region Retrieving data
tc = TransactionContext.Begin();
DataReader oreader =
new DataReader(TrainingScheduleAttnDA.GetByTrainingScheduleDateID(tc, trainingScheduleDateID));
trainingScheduleAttns = CreateObjects<TrainingScheduleAttn>(oreader);
tc.End();
#endregion
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message, e);
#endregion
}
return trainingScheduleAttns;
}
#endregion
#endregion
#region Insert TrainingScheduleAttn
public int Save(TrainingScheduleAttn oTrainingScheduleAttn)
{
try
{
TransactionContext tc = null;
try
{
#region Saving data
tc = TransactionContext.Begin(true);
if (oTrainingScheduleAttn.IsNew)
{
int newID = tc.GenerateID("TrainingScheduleAttn", "TSAttnID");
base.SetObjectID(oTrainingScheduleAttn, (newID));
TrainingScheduleAttnDA.Insert(tc, oTrainingScheduleAttn);
}
else
TrainingScheduleAttnDA.Update(tc, oTrainingScheduleAttn);
tc.End();
#endregion
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message, e);
#endregion
}
}
catch (Exception e)
{
//throw new ServiceException(e.Message, e);
throw new Exception(e.Message, e);
}
return oTrainingScheduleAttn.ID;
}
public void saveTrainingAttendance(int trainingScheduleID, int trainingScheduleDateID,
int userid, List<TrainingScheduleEmployee> oTrainingScheduleEmployees)
{
TransactionContext tc = null;
try
{
#region Saving data
TrainingSchedule oTrainingSchedule = new TrainingScheduleService().Get(trainingScheduleID);
tc = TransactionContext.Begin(true);
try
{
if (oTrainingSchedule.EnrolledTrainingEmployees == null)
oTrainingSchedule.EnrolledTrainingEmployees = new List<TrainingScheduleEmployee>();
foreach (TrainingScheduleEmployee newEmps in oTrainingScheduleEmployees)
{
var existItem = oTrainingSchedule.EnrolledTrainingEmployees.FirstOrDefault(
x => x.EmployeeID == newEmps.EmployeeID);
if (existItem == null)
{
TrainingScheduleEmployee item = new TrainingScheduleEmployee();
base.SetObjectID(item,
(tc.GenerateID("TrainingScheduleEmployee", "TrainingScheduleEmployeeID")));
item.EmployeeID = newEmps.EmployeeID;
item.TrainingScheduleID = newEmps.TrainingScheduleID;
item.ScheduleEmpStatus = EnumTSStatus.Enrolled;
item.Status = EnumStatus.Active;
item.CreatedDate = DateTime.Today;
item.CreatedBy = userid;
item.ProposedDate = DateTime.Today;
item.ProposedByID = userid;
TrainingScheduleDA.InsertTrainingEmployee(tc, item);
oTrainingSchedule.EnrolledTrainingEmployees.Add(item);
}
}
int newID = tc.GenerateID("TrainingScheduleAttn", "TSAttnID");
TrainingScheduleAttnDA.Delete(tc, trainingScheduleID, trainingScheduleDateID);
foreach (TrainingScheduleEmployee x in oTrainingScheduleEmployees)
{
// if (x.Present == false) continue;
newID = newID + 1;
TrainingScheduleAttn item = new TrainingScheduleAttn();
item.ID = newID;
item.TrainingScheduleID = trainingScheduleID;
item.EmployeeID = x.EmployeeID;
item.TSDateID = trainingScheduleDateID;
item.CreatedBy = userid;
item.CreatedDate = DateTime.Today;
TrainingScheduleAttnDA.Insert(tc, item);
}
tc.End();
#endregion
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message, e);
#endregion
}
}
catch (Exception e)
{
//throw new ServiceException(e.Message, e);
throw new Exception(e.Message, e);
}
}
public void saveTrainingAttendance(int trainingScheduleID, int trainingScheduleDateID,
int userid, List<TrainingScheduleAttn> oTrainingScheduleAttns)
{
TransactionContext tc = null;
try
{
#region Saving data
TrainingSchedule oTrainingSchedule = new TrainingScheduleService().Get(trainingScheduleID);
tc = TransactionContext.Begin(true);
try
{
if (oTrainingSchedule.EnrolledTrainingEmployees == null)
oTrainingSchedule.EnrolledTrainingEmployees = new List<TrainingScheduleEmployee>();
foreach (TrainingScheduleAttn newEmps in oTrainingScheduleAttns)
{
var existItem = oTrainingSchedule.EnrolledTrainingEmployees.FirstOrDefault(
x => x.EmployeeID == newEmps.EmployeeID);
if (existItem == null)
{
TrainingScheduleEmployee item = new TrainingScheduleEmployee();
base.SetObjectID(item,
(tc.GenerateID("TrainingScheduleEmployee", "TrainingScheduleEmployeeID")));
item.EmployeeID = newEmps.EmployeeID;
item.TrainingScheduleID = newEmps.TrainingScheduleID;
item.ScheduleEmpStatus = EnumTSStatus.Enrolled;
item.Status = EnumStatus.Active;
item.CreatedDate = DateTime.Today;
item.CreatedBy = userid;
item.ProposedDate = DateTime.Today;
item.ProposedByID = userid;
TrainingScheduleDA.InsertTrainingEmployee(tc, item);
oTrainingSchedule.EnrolledTrainingEmployees.Add(item);
}
}
int newID = tc.GenerateID("TrainingScheduleAttn", "TSAttnID");
TrainingScheduleAttnDA.Delete(tc, trainingScheduleID, trainingScheduleDateID);
foreach (TrainingScheduleAttn x in oTrainingScheduleAttns)
{
newID = newID + 1;
TrainingScheduleAttnDA.Insert(tc, x);
}
tc.End();
#endregion
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message, e);
#endregion
}
}
catch (Exception e)
{
//throw new ServiceException(e.Message, e);
throw new Exception(e.Message, e);
}
}
public int Save(TransactionContext tc, TrainingScheduleAttn oTrainingScheduleAttn)
{
try
{
try
{
#region Saving data
if (oTrainingScheduleAttn.IsNew)
{
int newID = tc.GenerateID("TrainingScheduleAttn", "TSAttnID");
base.SetObjectID(oTrainingScheduleAttn, (newID));
TrainingScheduleAttnDA.Insert(tc, oTrainingScheduleAttn);
}
else
TrainingScheduleAttnDA.Update(tc, oTrainingScheduleAttn);
#endregion
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message, e);
#endregion
}
}
catch (Exception e)
{
//throw new ServiceException(e.Message, e);
throw new Exception(e.Message, e);
}
return oTrainingScheduleAttn.ID;
}
#endregion
#region Delete
public void Delete(int id)
{
try
{
TransactionContext tc = null;
try
{
#region Deleting data
tc = TransactionContext.Begin(true);
TrainingScheduleAttnDA.Delete(tc, id);
tc.End();
#endregion
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message, e);
#endregion
}
}
catch (Exception e)
{
throw new ServiceException(e.Message, e);
}
}
public void DeletebyEmployeeIdAndTrainingScheduleID(int employeeID, int scheduleDateID)
{
try
{
TransactionContext tc = null;
try
{
#region Deleting data
tc = TransactionContext.Begin(true);
TrainingScheduleAttnDA.DeletebyEmployeeIdAndTrainingScheduleID(tc, employeeID, scheduleDateID);
tc.End();
#endregion
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message, e);
#endregion
}
}
catch (Exception e)
{
throw new ServiceException(e.Message, e);
}
}
#endregion
public bool DoesTrainingScheduleAttnExists(int trainingScheduleDateID)
{
try
{
TransactionContext tc = null;
tc = TransactionContext.Begin();
bool result = false;
result = TrainingScheduleAttnDA.DoesTrainingScheduleAttnExists(tc, trainingScheduleDateID);
tc.End();
return result;
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
}
#endregion
}
}