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

595 lines
19 KiB
C#

using System;
using Ease.Core.Model;
using Ease.Core.DataAccess;
using Ease.Core.Utility;
using System.Collections.Generic;
using HRM.BO;
using System.Data;
namespace HRM.DA
{
#region Asset Service
public class AssetSerialService : ServiceTemplate, IAssetSerialService
{
public AssetSerialService()
{
}
#region MapObject For Asset
private void MapObject(AssetSerial oAssetSerial, DataReader oReader)
{
base.SetObjectID(oAssetSerial, oReader.GetInt32("AssetSerialID").Value);
oAssetSerial.AssetId = oReader.GetInt32("AssetId").Value;
oAssetSerial.ReceivedDate = oReader.GetDateTime("ReceivedDate").Value;
oAssetSerial.VendorId = oReader.GetInt32("VendorId",0);
oAssetSerial.StoreId = oReader.GetInt32("StoreId",0);
oAssetSerial.Specification = oReader.GetString("Specification");
oAssetSerial.UniqueIndentifier = oReader.GetString("UniqueIndentifier");
oAssetSerial.Price = oReader.GetDouble("Price", 0);
oAssetSerial.ExpiryDays = oReader.GetInt32("ExpiryDays", 0);
oAssetSerial.BatchNo = oReader.GetString("BatchNo",null);
oAssetSerial.SerialNo = oReader.GetString("SerialNo");
oAssetSerial.Remarks = oReader.GetString("Remarks",null);
oAssetSerial.AssetStatus = (EnumAssetStatus)oReader.GetInt32("AssetStatus",0);
oAssetSerial.AssetStatusString = oReader.GetInt32("AssetStatus", 0) == 0 ? null :((EnumAssetStatus)oReader.GetInt32("AssetStatus")).ToString();
oAssetSerial.AssetReceiverType = (EnumAssetReceiverType)oReader.GetInt32("AssetReceiverType",0);
oAssetSerial.AssetReceiverId = oReader.GetInt32("AssetReceiverId", 0);
oAssetSerial.CreatedBy = oReader.GetInt32("CreatedBy", 0);
oAssetSerial.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
oAssetSerial.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
oAssetSerial.ModifiedDate = oReader.GetDateTime("ModifiedDate");
oAssetSerial.RepairedDate = oReader.GetDateTime("RepairDate",true,DateTime.MinValue);
oAssetSerial.VendorName = oReader.GetString("VendorName", true, null);
oAssetSerial.Amount = oReader.GetDouble("Amount", true, 0);
oAssetSerial.IsTemporaryItem = oReader.GetBoolean("IsTemporary", true,false);
oAssetSerial.TillDate = oReader.GetDateTime("TillDate") == null ? null : oReader.GetDateTime("TillDate");
oAssetSerial.AssignDate = oReader.GetDateTime("AssignDate", DateTime.MinValue);
oAssetSerial.EmployeeNo= oReader.GetString("EMPLOYEENO", true, null);
oAssetSerial.EmployeeName = oReader.GetString("EmployeeName", true, null);
oAssetSerial.Designation = oReader.GetString("Designation", true, null);
oAssetSerial.Vendor = oReader.GetString("Vendor", true, null);
oAssetSerial.Store = oReader.GetString("Store", true, null);
oAssetSerial.EmployeeId = oReader.GetInt32("employeeId", true,0);
oAssetSerial.FaultybyEmpID = oReader.GetInt32("FaultybyEmpID");
oAssetSerial.AssetName = oReader.GetString("AssetName",true, null);
this.SetObjectState(oAssetSerial, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
AssetSerial oAssetSerial = new AssetSerial();
MapObject(oAssetSerial, oReader);
return oAssetSerial as T;
}
#endregion
#region Service Implemantation
public AssetSerial Get(int id)
{
AssetSerial oAsset = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(AssetSerialDA.Get(tc, id));
if (oreader.Read())
{
oAsset = this.CreateObject<AssetSerial>(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 oAsset;
}
public List<AssetSerial> Get()
{
List<AssetSerial> AssetSerials = new List<AssetSerial>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(AssetSerialDA.Get(tc));
AssetSerials = this.CreateObjects<AssetSerial>(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 AssetSerials;
}
public int Save(AssetSerial oAssetSerial)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oAssetSerial.IsNew)
{
int id = tc.GenerateID("AssetSerial", "AssetSerialID");
base.SetObjectID(oAssetSerial, (id));
AssetSerialDA.Insert(tc, oAssetSerial);
}
else
{
AssetSerialDA.Update(tc, oAssetSerial);
}
return oAssetSerial.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
finally
{
tc.End();
}
}
public DataSet GetFormData(int serialId)
{
DataSet oGetFormData = new DataSet();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
oGetFormData = AssetSerialDA.GetFormData(tc, serialId);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oGetFormData;
}
public DataSet GetFormSerialData(int employeeId)
{
DataSet oGetFormData = new DataSet();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
oGetFormData = AssetSerialDA.GetFormSerialData(tc, employeeId);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oGetFormData;
}
public void Delete(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
AssetSerialDA.Delete(tc, id);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
finally
{
tc.End();
}
}
public List<AssetSerial> SaveAssetSerialList(List<AssetSerial> Items)
{
TransactionContext tc = null;
try
{
AssetSerial oAsset = new AssetSerial();
int batchID = 0;
tc = TransactionContext.Begin(true);
DataReader oreader = new DataReader(AssetSerialDA.GetLastAssetSerialBatch(tc));
if (oreader.Read())
{
oAsset = this.CreateObject<AssetSerial>(oreader);
}
oreader.Close();
if (oAsset.BatchNo == null)
{
batchID = 1;
}
else
{
if (Convert.ToInt32(oAsset.BatchNo) <= 0)
{
batchID = 1;
}
else
{
batchID = Convert.ToInt32(oAsset.BatchNo) + 1;
}
}
foreach (var oAssetSerial in Items)
{
int oID = 0;
if (oAssetSerial.IsNew)
{
int id = tc.GenerateID("AssetSerial", "ASSETSERIALID");
oAssetSerial.ID = oID = (id);
base.SetObjectID(oAssetSerial, (id));
oAssetSerial.isNewAssetTran = true;
//if (oAsset.BatchNo == null)
//{
// batchID = 1;
//}
//else
//{
// if (Convert.ToInt32(batchID) <= 0)
// {
// batchID = 1;
// }
// else
// {
// batchID = Convert.ToInt32(batchID) + 1;
// }
//}
oAssetSerial.BatchNo = batchID.ToString();
AssetSerialDA.Insert(tc, oAssetSerial);
}
else
{
oID = oAssetSerial.ID;
AssetSerialDA.Update(tc, oAssetSerial);
}
}
tc.End();
}
catch (Exception ex)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(ex);
if (ex.Message.Contains("UC_Asset_SerialNo"))
{
throw new ServiceException("Cannot insert duplicate serial data", ex);
}
else
{
throw new ServiceException(ex.Message, ex);
}
#endregion
}
return Items;
}
public List<AssetSerial> SaveAssetSerialList(TransactionContext tc,List<AssetSerial> Items)
{
//TransactionContext tc = null;
try
{
AssetSerial oAsset = new AssetSerial();
int batchID = 0;
//tc = TransactionContext.Begin(true);
DataReader oreader = new DataReader(AssetSerialDA.GetLastAssetSerialBatch(tc));
if (oreader.Read())
{
oAsset = this.CreateObject<AssetSerial>(oreader);
}
oreader.Close();
if (oAsset.BatchNo == null)
{
batchID = 1;
}
else
{
if (Convert.ToInt32(oAsset.BatchNo) <= 0)
{
batchID = 1;
}
else
{
batchID = Convert.ToInt32(oAsset.BatchNo) + 1;
}
}
foreach (var oAssetSerial in Items)
{
int oID = 0;
if (oAssetSerial.IsNew)
{
int id = tc.GenerateID("AssetSerial", "ASSETSERIALID");
oAssetSerial.ID = oID = (id);
base.SetObjectID(oAssetSerial, (id));
oAssetSerial.isNewAssetTran = true;
oAssetSerial.BatchNo = batchID.ToString();
AssetSerialDA.Insert(tc, oAssetSerial);
}
else
{
oID = oAssetSerial.ID;
AssetSerialDA.Update(tc, oAssetSerial);
}
}
// tc.End();
}
catch (Exception ex)
{
#region Handle Exception
//if (tc != null)
// tc.HandleError();
ExceptionLog.Write(ex);
if (ex.Message.Contains("UC_Asset_SerialNo"))
{
throw new ServiceException("Cannot insert duplicate serial data", ex);
}
else
{
throw new ServiceException(ex.Message, ex);
}
#endregion
}
return Items;
}
public AssetSerial GetLastAssetSerial(int assetId)
{
AssetSerial oAsset = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(AssetSerialDA.GetLastAssetSerial(tc, assetId));
if (oreader.Read())
{
oAsset = new AssetSerial();
oAsset = this.CreateObject<AssetSerial>(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 oAsset;
}
public List<AssetSerial> GetAssetSerials(DateTime? fromDate, DateTime? toDate, int assetid, int vendorid, int storid, EnumAssetStatus? status, EnumAssetReceiverType? receiverType, string receiverIds)
{
List<AssetSerial> AssetSerials = new List<AssetSerial>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(AssetSerialDA.GetAssetSerials(tc, fromDate, toDate, assetid, vendorid,storid, status, receiverType, receiverIds));
AssetSerials = this.CreateObjects<AssetSerial>(dr);
dr.Close();
tc.End();
foreach(var item in AssetSerials)
{
item.TempStatus = item.IsTemporaryItem == true ? "Yes" : "No";
item.ReceivedDateString= item.ReceivedDate.ToString("dd/MM/yyyy");
item.AssignDateString=item.AssignDate==DateTime.MinValue ? null : item.AssignDate.ToString("dd/MM/yyyy");
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return AssetSerials;
}
public List<AssetSerial> GetReceivedAssetSerials(DateTime? receiveDate, EnumAssetReceiverType? receiverType, int receiverId)
{
List<AssetSerial> AssetSerials = new List<AssetSerial>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(AssetSerialDA.GetReceivedAssetSerials(tc, receiveDate, receiverType, receiverId));
AssetSerials = this.CreateObjects<AssetSerial>(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 AssetSerials;
}
public List<AssetSerial> GetRepairedAssetSerials()
{
List<AssetSerial> AssetSerials = new List<AssetSerial>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(AssetSerialDA.GetRepairedAssetSerials(tc));
AssetSerials = this.CreateObjects<AssetSerial>(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 AssetSerials;
}
public List<AssetSerial> GetAssetSerialPicker(int assetid, int categoryid, EnumAssetStatus? status)
{
List<AssetSerial> AssetSerials = new List<AssetSerial>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(AssetSerialDA.GetAssetSerialPicker(tc, assetid, categoryid, status));
AssetSerials = this.CreateObjects<AssetSerial>(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 AssetSerials;
}
public List<AssetSerial> GetAssignSerialsByEmployeeId(EnumAssetReceiverType? receiverType, int receiverId)
{
List<AssetSerial> AssetSerials = new List<AssetSerial>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(AssetSerialDA.GetAssignSerialsByEmployeeId(tc, receiverType, receiverId));
AssetSerials = this.CreateObjects<AssetSerial>(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 AssetSerials;
}
#endregion
}
#endregion
}