EchoTex_Payroll/HRM.DA/Service/Assets/AssetSerialTranService.cs

480 lines
18 KiB
C#
Raw Permalink Normal View History

2024-10-14 10:01:49 +06:00
using System;
using Ease.Core.Model;
using Ease.Core.DataAccess;
using Ease.Core.Utility;
using System.Collections.Generic;
using HRM.BO;
using HRM.BO.Configuration;
using Microsoft.Extensions.Configuration;
using System.Threading;
using System.IO;
using HRM.DA.DA.Assets;
namespace HRM.DA
{
#region Asset Service
public class AssetSerialTranService : ServiceTemplate, IAssetSerialTranService
{
public AssetSerialTranService()
{
}
#region MapObject For Asset
private void MapObject(AssetSerialTran oAssetSerialtran, DataReader oReader)
{
base.SetObjectID(oAssetSerialtran, oReader.GetInt32("AssetSerialTranID").Value);
oAssetSerialtran.AssetId = oReader.GetInt32("AssetId").Value;
oAssetSerialtran.SerialId = oReader.GetInt32("SerialId").Value;
oAssetSerialtran.TranDate = oReader.GetDateTime("TranDate").Value;
oAssetSerialtran.VendorId = oReader.GetInt32("VendorId").Value;
oAssetSerialtran.StoreId = oReader.GetInt32("StoreId").Value;
oAssetSerialtran.BatchNo = oReader.GetString("BatchNo", null);
oAssetSerialtran.AssetReceiverId = oReader.GetInt32("AssetReceiverId", 0);
oAssetSerialtran.AssetReceiverType = (EnumAssetReceiverType)oReader.GetInt32("AssetReceiverType");
oAssetSerialtran.UserId = oReader.GetInt32("UserId", 0);
oAssetSerialtran.CreatedBy = oReader.GetInt32("CreatedBy", 0);
oAssetSerialtran.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
oAssetSerialtran.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
oAssetSerialtran.ModifiedDate = oReader.GetDateTime("ModifiedDate");
oAssetSerialtran.damageAmount = oReader.GetDouble("damageAmount", 0);
oAssetSerialtran.damageRemarks = oReader.GetString("damageRemarks");
oAssetSerialtran.isFinalClerance = oReader.GetBoolean("isFinalClerance", false);
oAssetSerialtran.faultybyEmpID = oReader.GetInt32("faultybyEmpID");
this.SetObjectState(oAssetSerialtran, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
AssetSerialTran oAssetSerialtran = new AssetSerialTran();
MapObject(oAssetSerialtran, oReader);
return oAssetSerialtran as T;
}
#endregion
#region Service Implemantation
public AssetSerialTran Get(int id)
{
AssetSerialTran oAsset = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(AssetSerialTranDA.Get(tc, id));
if (oreader.Read())
{
oAsset = this.CreateObject<AssetSerialTran>(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 double GetDamageAmount(int id)
{
TransactionContext tc = null;
double nAmount = 0;
try
{
tc = TransactionContext.Begin();
nAmount = AssetSerialTranDA.GetDamageAmount(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
}
return nAmount;
}
public List<AssetSerialTran> Get()
{
List<AssetSerialTran> AssetSerials = new List<AssetSerialTran>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(AssetSerialTranDA.Get(tc));
AssetSerials = this.CreateObjects<AssetSerialTran>(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(AssetSerialTran oAssetSerial)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oAssetSerial.IsNew)
{
int id = tc.GenerateID("AssetSerialTran", "AssetSerialTranID");
base.SetObjectID(oAssetSerial, (id));
AssetSerialTranDA.Insert(tc, oAssetSerial);
}
else
{
AssetSerialTranDA.Update(tc, oAssetSerial);
}
if (oAssetSerial.faultybyEmpID != null && oAssetSerial.TranType == EnumAssetTranType.HandOver ||
oAssetSerial.TranType == EnumAssetTranType.Receive)
AssetSerialDA.UpdateFaultyEmp(tc, oAssetSerial.SerialId, oAssetSerial.faultybyEmpID);
else AssetSerialDA.UpdateFaultyEmp(tc, oAssetSerial.SerialId, null);
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 void SaveAssetSerialTranList(List<AssetSerialTran> Items)
{
TransactionContext tc = null;
try
{
AssetSerialTran oAsset = new AssetSerialTran();
int batchID = 0;
tc = TransactionContext.Begin(true);
DataReader oreader = new DataReader(AssetSerialTranDA.GetLastBatchNo(tc));
if (oreader.Read())
{
oAsset = this.CreateObject<AssetSerialTran>(oreader);
}
if (string.IsNullOrEmpty(oAsset.BatchNo))
{
batchID = 1;
}
else
{
if (Convert.ToInt32(oAsset.BatchNo) <= 0)
{
batchID = 1;
}
else
{
batchID = Convert.ToInt32(oAsset.BatchNo) + 1;
}
}
oreader.Close();
foreach (var oAssetSerialtran in Items)
{
int oID = 0;
if (oAssetSerialtran.IsNew)
{
int id = tc.GenerateID("AssetSerialTran", "ASSETSERIALTRANID");
oID = (id);
oAssetSerialtran.BatchNo = batchID.ToString();
base.SetObjectID(oAssetSerialtran, (id));
AssetSerialTranDA.Insert(tc, oAssetSerialtran);
if (oAssetSerialtran.AssetSerialTranAttachment != null)
{
oAssetSerialtran.AssetSerialTranAttachment.AssetSerailTranID = oID;
if (oAssetSerialtran.AssetSerialTranAttachment.ID < 0 && oAssetSerialtran.AssetSerialTranAttachment.PreviousFileTobase64 != null && !String.IsNullOrEmpty(oAssetSerialtran.AssetSerialTranAttachment.ConnectionString))
{
oAssetSerialtran.AssetSerialTranAttachment.AssetSerailTranID = oAssetSerialtran.ID;
//AssetSerialTranAttachmentDA.Insert(oAssetSerialtran.AssetSerialTranAttachment, oAssetSerialtran.AssetSerialTranAttachment.ConnectionString);
}
}
if (oAssetSerialtran.TranType == EnumAssetTranType.Assign || oAssetSerialtran.TranType == EnumAssetTranType.Repaired)
AssetSerialTranDA.UpdateAssetSerialStatus(oAssetSerialtran, tc);
if (oAssetSerialtran.TranType == EnumAssetTranType.HandOver)
AssetSerialTranDA.UpdateAssetSerialHandoverStatus(oAssetSerialtran, tc);
if (oAssetSerialtran.TranType == EnumAssetTranType.Receive)
AssetSerialTranDA.UpdateAssetSerialStatus(oAssetSerialtran, tc);
}
else
{
oID = oAssetSerialtran.ID;
AssetSerialTranDA.Update(tc, oAssetSerialtran);
}
if (oAssetSerialtran.TranType == EnumAssetTranType.HandOver ||
oAssetSerialtran.TranType == EnumAssetTranType.Receive)
AssetSerialDA.UpdateFaultyEmp(tc, oAssetSerialtran.SerialId, oAssetSerialtran.faultybyEmpID);
}
tc.End();
foreach (var oAssetSerialtran in Items)
{
if (oAssetSerialtran.AssetSerialTranAttachment != null)
{
if (oAssetSerialtran.AssetSerialTranAttachment.ID < 0 && oAssetSerialtran.AssetSerialTranAttachment.PreviousFileTobase64 != null && !String.IsNullOrEmpty(oAssetSerialtran.AssetSerialTranAttachment.ConnectionString))
{
AssetSerialTranAttachmentDA.Insert(oAssetSerialtran.AssetSerialTranAttachment, oAssetSerialtran.AssetSerialTranAttachment.ConnectionString);
}
}
}
SendMailInThread(Items);
}
catch (Exception e)
{
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
}
}
public void SaveAssetSerialTranList(TransactionContext tc, List<AssetSerialTran> Items)
{
// TransactionContext tc = null;
try
{
AssetSerialTran oAsset = new AssetSerialTran();
int batchID = 0;
// tc = TransactionContext.Begin(true);
DataReader oreader = new DataReader(AssetSerialTranDA.GetLastBatchNo(tc));
if (oreader.Read())
{
oAsset = this.CreateObject<AssetSerialTran>(oreader);
}
if (string.IsNullOrEmpty(oAsset.BatchNo))
{
batchID = 1;
}
else
{
if (Convert.ToInt32(oAsset.BatchNo) <= 0)
{
batchID = 1;
}
else
{
batchID = Convert.ToInt32(oAsset.BatchNo) + 1;
}
}
oreader.Close();
foreach (var oAssetSerialtran in Items)
{
int oID = 0;
if (oAssetSerialtran.IsNew)
{
int id = tc.GenerateID("AssetSerialTran", "ASSETSERIALTRANID");
oID = (id);
oAssetSerialtran.BatchNo = batchID.ToString();
base.SetObjectID(oAssetSerialtran, (id));
AssetSerialTranDA.Insert(tc, oAssetSerialtran);
if (oAssetSerialtran.TranType == EnumAssetTranType.Assign || oAssetSerialtran.TranType == EnumAssetTranType.Repaired)
AssetSerialTranDA.UpdateAssetSerialStatus(oAssetSerialtran, tc);
if (oAssetSerialtran.TranType == EnumAssetTranType.HandOver)
AssetSerialTranDA.UpdateAssetSerialHandoverStatus(oAssetSerialtran, tc);
if (oAssetSerialtran.TranType == EnumAssetTranType.Receive)
AssetSerialTranDA.UpdateAssetSerialStatus(oAssetSerialtran, tc);
}
else
{
oID = oAssetSerialtran.ID;
AssetSerialTranDA.Update(tc, oAssetSerialtran);
}
if (oAssetSerialtran.TranType == EnumAssetTranType.HandOver ||
oAssetSerialtran.TranType == EnumAssetTranType.Receive)
AssetSerialDA.UpdateFaultyEmp(tc, oAssetSerialtran.SerialId, oAssetSerialtran.faultybyEmpID);
}
// tc.End();
SendMailInThread(Items);
foreach (var oAssetSerialTran in Items)
AssetSerialTranDA.UpdateMailStatus(oAssetSerialTran, tc);
}
catch (Exception e)
{
//if (tc != null)
// tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
}
}
public void SendMailInThread(List<AssetSerialTran> Items)
{
Thread myNewThread = new Thread(() => mailAssetSerial(Items));
myNewThread.Start();
}
public void mailAssetSerial(List<AssetSerialTran> Items)
{
// TransactionContext tc = null;
try
{
foreach (var oAssetSerialTran in Items)
{
if (oAssetSerialTran.TranType == EnumAssetTranType.Assign && oAssetSerialTran.AssetReceiverType == EnumAssetReceiverType.Employee)
{
Employee emp = new EmployeeService().Get((int)oAssetSerialTran.AssetReceiverId);
if (emp == null)
{
throw new Exception("No Employee Found for Assignment");
}
var serialNo = new AssetSerialService().Get(oAssetSerialTran.SerialId).SerialNo;
bool isMailSent = SendEmail(emp, serialNo);
if (isMailSent == false)
{
new Exception("Mail has not been sent");
}
//if (isMailSent == true)
//{
// tc = TransactionContext.Begin(true);
// AssetSerialTranDA.UpdateMailStatus(oAssetSerialTran, tc);
// tc.End();
//}
}
}
}
catch (Exception e)
{
#region Handle Exception
//if (tc != null)
// tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public bool SendEmail(Employee mailEmployee, string serialNo)
{
var isMailSent = false;
MailSender sender = null;
EmailSettings st = new EmailSettings();
try
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
IConfiguration Configuration = builder.Build();
Configuration.GetSection("EmailSettings").Bind(st);
sender = new MailSender();
Thread.Sleep(200);
string sbody = "";
if (mailEmployee.EmailAddress == string.Empty || mailEmployee.EmailAddress == null)
{
throw new Exception("no mail address found");
}
var description = "Notification of Asset Assignment";
sender = new MailSender();
sender.AddTo(mailEmployee.EmailAddress.Trim());
sbody = "Dear " + mailEmployee.Name + "," + "<br/><br/>" + " You have Assigned for Asset Serial No: " + serialNo + " .Please Proceed Acccordingly.";
sender.Body = sbody;
sender.Link = st.WebAddress;
sender.Subject = description;
sender.SendMail(st);
isMailSent = true;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return isMailSent;
}
public void Delete(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
AssetSerialTranDA.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();
}
}
#endregion
}
#endregion
}