diff --git a/BMA.EHR.Application/Repositories/Commands/CommandReportRepository.cs b/BMA.EHR.Application/Repositories/Commands/CommandReportRepository.cs index 2d833fb6..518d95dc 100644 --- a/BMA.EHR.Application/Repositories/Commands/CommandReportRepository.cs +++ b/BMA.EHR.Application/Repositories/Commands/CommandReportRepository.cs @@ -1,6 +1,4 @@ -using Amazon.Runtime.Internal.Transform; -using Amazon.S3.Model.Internal.MarshallTransformations; -using BMA.EHR.Application.Common.Interfaces; +using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Responses.Reports; using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.Models.Commands.Core; @@ -278,7 +276,7 @@ namespace BMA.EHR.Application.Repositories.Commands } var report_data = (from r in raw_data - join p in _dbContext.Set() + join p in _dbContext.Set() .Include(x => x.OrganizationPosition) .ThenInclude(x => x.Organization) .Include(x => x.PositionPath) diff --git a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs index c963a9ba..814ea9ce 100644 --- a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs +++ b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs @@ -1458,12 +1458,21 @@ namespace BMA.EHR.Application.Repositories.Commands case "C-PM-02": await ExecuteCommand01_02Async(command); break; + case "C-PM-10": + await ExecuteCommand10Async(command); + break; case "C-PM-11": await ExecuteCommand11Async(command); break; case "C-PM-12": await ExecuteCommand12Async(command); break; + case "C-PM-15": + await ExecuteCommand15Async(command); + break; + case "C-PM-16": + await ExecuteCommand16Async(command); + break; case "C-PM-17": await ExecuteCommand17Async(command); break; @@ -1861,6 +1870,162 @@ namespace BMA.EHR.Application.Repositories.Commands } } + // + /// C-PM-10 - คำสั่งแต่งตั้งคณะกรรมการประเมินผลการทดลองปฏิบัติหน้าที่ราชการ + /// + /// object ของรายการคำสั่ง + /// + private async Task ExecuteCommand10Async(Command command) + { + try + { + foreach (var recv in command.Receivers) + { + var data = await _dbContext.Set() + .Include(x => x.Salaries) + .ThenInclude(x => x.PositionLevel) + .FirstOrDefaultAsync(x => x.Id == recv.RefPlacementProfileId); + + if (data == null) + throw new Exception(GlobalMessages.DataNotFound); + + var lastSarary = data.Salaries.OrderByDescending(x => x.Order).FirstOrDefault(); + var order = 1; + if (lastSarary.Order != null) + order = lastSarary.Order.Value + 1; + + var salary = new ProfileSalary + { + Order = order, + Date = command.CommandAffectDate, + Amount = lastSarary.Amount, + PositionSalaryAmount = lastSarary.PositionSalaryAmount, + MouthSalaryAmount = lastSarary.MouthSalaryAmount, + SalaryClass = "", + SalaryRef = "คำสั่งแต่งตั้งคณะกรรมการประเมินผลการทดลองปฏิบัติหน้าที่ราชการ คำสั่ง" + command.IssuerOrganizationName + "ที่ " + $"{command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate!.Value.ToThaiFullDate3()}", + + OcId = lastSarary.OcId, + + + PositionLevel = lastSarary.PositionLevel, + PositionLineId = lastSarary.PositionLineId, + PositionTypeId = lastSarary.PositionTypeId, + OrganizationShortNameId = lastSarary.OrganizationShortNameId, + PosNoId = lastSarary.PosNoId, + + CommandNo = $"{command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()}", + CommandTypeName = command.CommandType.Name, + + + PositionEmployeeGroupId = null, + PositionEmployeeLevelId = null, + PositionEmployeePositionId = null, + PositionEmployeePositionSideId = null, + PosNoEmployee = "", + + + //PositionPathSideId = lastSarary.PositionPathSideId == null, + PositionExecutiveId = lastSarary.PositionExecutiveId, + //PositionExecutiveSideId = lastSarary.PositionExecutiveSideId, + + IsActive = true, + CreatedAt = DateTime.Now, + CreatedFullName = FullName ?? "System Administrator", + CreatedUserId = UserId ?? "", + LastUpdatedAt = DateTime.Now, + LastUpdateFullName = FullName ?? "System Administrator", + LastUpdateUserId = UserId ?? "", + + }; + + if (lastSarary.PositionPathSideId != null) salary.PositionPathSideId = lastSarary.PositionPathSideId; + if (lastSarary.PositionExecutiveSideId != null) salary.PositionExecutiveSideId = lastSarary.PositionExecutiveSideId; + + data.Salaries.Add(salary); + + // update placementstatus + //data.Status = "DONE"; + + await _dbContext.SaveChangesAsync(); + + // TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่ + // Send noti inbox and email + var subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + var body = $"คำสั่งแต่งตั้งคณะกรรมการประเมินผลการทดลองปฏิบัติหน้าที่ราชการ เลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + _emailSenderService.SendMail(subject, body, "dev@frappet.com"); + + + var inbox = new Inbox + { + Subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + Body = $"คำสั่งแต่งตั้งคณะกรรมการประเมินผลการทดลองปฏิบัติหน้าที่ราชการ เลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = data.Id, + Payload = "", + }; + _dbContext.Set().Add(inbox); + + var noti = new Notification + { + Body = $"คำสั่งแต่งตั้งคณะกรรมการประเมินผลการทดลองปฏิบัติหน้าที่ราชการ เลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = data.Id, + Type = "", + Payload = "", + }; + _dbContext.Set().Add(noti); + await _dbContext.SaveChangesAsync(); + } + + // send cc noti inbox + foreach (var cc in command.Deployments) + { + var pf = await _dbContext.Set().FirstOrDefaultAsync(x => x.CitizenId == cc.CitizenId); + if (pf != null) + { + if (cc.IsSendInbox) + { + var inbox = new Inbox + { + Subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + Body = $"คำสั่งแต่งตั้งคณะกรรมการประเมินผลการทดลองปฏิบัติหน้าที่ราชการ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = pf.Id, + Payload = "", + }; + _dbContext.Set().Add(inbox); + } + + if (cc.IsSendMail) + { + // TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่ + // Send noti inbox and email + var subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + var body = $"คำสั่งแต่งตั้งคณะกรรมการประเมินผลการทดลองปฏิบัติหน้าที่ราชการ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + _emailSenderService.SendMail(subject, body, "dev@frappet.com"); + } + + + var noti = new Notification + { + Body = $"คำสั่งแต่งตั้งคณะกรรมการประเมินผลการทดลองปฏิบัติหน้าที่ราชการ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = pf.Id, + Type = "", + Payload = "", + }; + _dbContext.Set().Add(noti); + } + } + + // change command status + var cmdStatus = await _dbContext.Set().FirstOrDefaultAsync(x => x.Sequence == 5); + command.CommandStatusId = cmdStatus!.Id; + + await _dbContext.SaveChangesAsync(); + } + catch + { + throw; + } + } + /// /// C-PM-11 - คำสั่งให้ข้าราชการที่มีผลการทดลองปฏิบัติหน้าที่ราชการไม่ต่ำกว่ามาตรฐานที่กำหนดรับราชการต่อไป /// @@ -2020,7 +2185,170 @@ namespace BMA.EHR.Application.Repositories.Commands } /// - /// C-PM-12 - คำสั่งให้ข้าราชการออกจากราชการเพราะผลการทดลองปฏิบัติหน้าที่ราชการต่ำกว่ามาตรฐานที่กำหนด + /// C-PM-12 - คำสั่งให้โอนข้าราชการกรุงเทพมหานครสามัญ + /// + /// object ของรายการคำสั่ง + /// + private async Task ExecuteCommand13Async(Command command) + { + try + { + foreach (var recv in command.Receivers) + { + var data = await _dbContext.Set() + .Include(x => x.Profile) + .ThenInclude(x => x.Salaries) + .Include(x => x.Profile) + .ThenInclude(x => x.PositionLevel) + .FirstOrDefaultAsync(x => x.Id == recv.RefPlacementProfileId); + + if (data == null) + throw new Exception(GlobalMessages.DataNotFound); + + data.Profile.IsActive = false; + data.Profile.IsLeave = true; + data.Profile.LeaveReason = "คำสั่งให้โอนข้าราชการกรุงเทพมหานครสามัญ"; + data.Profile.LeaveDate = command.CommandAffectDate; + + var lastSarary = data.Profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault(); + var order = 1; + if (lastSarary.Order != null) + order = lastSarary.Order.Value + 1; + + var salary = new ProfileSalary + { + Order = order, + Date = command.CommandAffectDate, + Amount = lastSarary.Amount, + PositionSalaryAmount = lastSarary.PositionSalaryAmount, + MouthSalaryAmount = lastSarary.MouthSalaryAmount, + SalaryClass = "", + SalaryRef = "คำสั่งให้โอนข้าราชการกรุงเทพมหานครสามัญ คำสั่ง" + command.IssuerOrganizationName + "ที่ " + $"{command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate!.Value.ToThaiFullDate3()}", + + OcId = lastSarary.OcId, + + + PositionLevel = lastSarary.PositionLevel, + PositionLineId = lastSarary.PositionLineId, + PositionTypeId = lastSarary.PositionTypeId, + OrganizationShortNameId = lastSarary.OrganizationShortNameId, + PosNoId = lastSarary.PosNoId, + + CommandNo = $"{command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()}", + CommandTypeName = command.CommandType.Name, + + + PositionEmployeeGroupId = null, + PositionEmployeeLevelId = null, + PositionEmployeePositionId = null, + PositionEmployeePositionSideId = null, + PosNoEmployee = "", + + + //PositionPathSideId = lastSarary.PositionPathSideId == null, + PositionExecutiveId = lastSarary.PositionExecutiveId, + //PositionExecutiveSideId = lastSarary.PositionExecutiveSideId, + + IsActive = true, + CreatedAt = DateTime.Now, + CreatedFullName = FullName ?? "System Administrator", + CreatedUserId = UserId ?? "", + LastUpdatedAt = DateTime.Now, + LastUpdateFullName = FullName ?? "System Administrator", + LastUpdateUserId = UserId ?? "", + + }; + + if (lastSarary.PositionPathSideId != null) salary.PositionPathSideId = lastSarary.PositionPathSideId; + if (lastSarary.PositionExecutiveSideId != null) salary.PositionExecutiveSideId = lastSarary.PositionExecutiveSideId; + + data.Profile.Salaries.Add(salary); + + // update placementstatus + //data.Status = "DONE"; + + await _dbContext.SaveChangesAsync(); + + // TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่ + // Send noti inbox and email + var subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + var body = $"คำสั่งให้โอนข้าราชการกรุงเทพมหานครสามัญ เลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + _emailSenderService.SendMail(subject, body, "dev@frappet.com"); + + + var inbox = new Inbox + { + Subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + Body = $"คำสั่งให้โอนข้าราชการกรุงเทพมหานครสามัญ เลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = data.Id, + Payload = "", + }; + _dbContext.Set().Add(inbox); + + var noti = new Notification + { + Body = $"คำสั่งให้โอนข้าราชการกรุงเทพมหานครสามัญ เลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = data.Id, + Type = "", + Payload = "", + }; + _dbContext.Set().Add(noti); + await _dbContext.SaveChangesAsync(); + } + + // send cc noti inbox + foreach (var cc in command.Deployments) + { + var pf = await _dbContext.Set().FirstOrDefaultAsync(x => x.CitizenId == cc.CitizenId); + if (pf != null) + { + if (cc.IsSendInbox) + { + var inbox = new Inbox + { + Subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + Body = $"คำสั่งให้โอนข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = pf.Id, + Payload = "", + }; + _dbContext.Set().Add(inbox); + } + + if (cc.IsSendMail) + { + // TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่ + // Send noti inbox and email + var subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + var body = $"คำสั่งให้โอนข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + _emailSenderService.SendMail(subject, body, "dev@frappet.com"); + } + + + var noti = new Notification + { + Body = $"คำสั่งให้โอนข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = pf.Id, + Type = "", + Payload = "", + }; + _dbContext.Set().Add(noti); + } + } + + // change command status + var cmdStatus = await _dbContext.Set().FirstOrDefaultAsync(x => x.Sequence == 5); + command.CommandStatusId = cmdStatus!.Id; + + await _dbContext.SaveChangesAsync(); + } + catch + { + throw; + } + } + + /// + /// C-PM-13 - คำสั่งให้ข้าราชการออกจากราชการเพราะผลการทดลองปฏิบัติหน้าที่ราชการต่ำกว่ามาตรฐานที่กำหนด /// /// object ของรายการคำสั่ง /// @@ -2180,6 +2508,330 @@ namespace BMA.EHR.Application.Repositories.Commands } } + /// + /// C-PM-15 - คำสั่งให้ช่วยราชการ + /// + /// object ของรายการคำสั่ง + /// + private async Task ExecuteCommand15Async(Command command) + { + try + { + foreach (var recv in command.Receivers) + { + var data = await _dbContext.Set() + .Include(x => x.Profile) + .ThenInclude(x => x.Salaries) + .ThenInclude(x => x.PositionLevel) + + .FirstOrDefaultAsync(x => x.Id == recv.RefPlacementProfileId); + + if (data == null) + throw new Exception(GlobalMessages.DataNotFound); + + //data.Profile.IsActive = false; + //data.Profile.IsLeave = true; + //data.Profile.LeaveReason = "ออกจากราชการ"; + //data.Profile.LeaveDate = command.CommandAffectDate; + + var lastSarary = data.Profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault(); + var order = 1; + if (lastSarary.Order != null) + order = lastSarary.Order.Value + 1; + + var salary = new ProfileSalary + { + Order = order, + Date = command.CommandAffectDate, + Amount = lastSarary.Amount, + PositionSalaryAmount = lastSarary.PositionSalaryAmount, + MouthSalaryAmount = lastSarary.MouthSalaryAmount, + SalaryClass = "", + SalaryRef = "คำสั่งให้ช่วยราชการ คำสั่ง" + command.IssuerOrganizationName + "ที่ " + $"{command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate!.Value.ToThaiFullDate3()}", + + OcId = lastSarary.OcId, + + PositionLevel = lastSarary.PositionLevel, + PositionLineId = lastSarary.PositionLineId, + PositionTypeId = lastSarary.PositionTypeId, + OrganizationShortNameId = lastSarary.OrganizationShortNameId, + PosNoId = lastSarary.PosNoId, + + CommandNo = $"{command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()}", + CommandTypeName = command.CommandType.Name, + + + PositionEmployeeGroupId = null, + PositionEmployeeLevelId = null, + PositionEmployeePositionId = null, + PositionEmployeePositionSideId = null, + PosNoEmployee = "", + + + //PositionPathSideId = lastSarary.PositionPathSideId == null, + PositionExecutiveId = lastSarary.PositionExecutiveId, + //PositionExecutiveSideId = lastSarary.PositionExecutiveSideId, + + IsActive = true, + CreatedAt = DateTime.Now, + CreatedFullName = FullName ?? "System Administrator", + CreatedUserId = UserId ?? "", + LastUpdatedAt = DateTime.Now, + LastUpdateFullName = FullName ?? "System Administrator", + LastUpdateUserId = UserId ?? "", + + }; + + if (lastSarary.PositionPathSideId != null) salary.PositionPathSideId = lastSarary.PositionPathSideId; + if (lastSarary.PositionExecutiveSideId != null) salary.PositionExecutiveSideId = lastSarary.PositionExecutiveSideId; + + data.Profile.Salaries.Add(salary); + + // update placementstatus + data.Status = "DONE"; + + await _dbContext.SaveChangesAsync(); + + // TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่ + // Send noti inbox and email + var subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + var body = $"คำสั่งให้ช่วยราชการ เลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + _emailSenderService.SendMail(subject, body, "dev@frappet.com"); + + + var inbox = new Inbox + { + Subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + Body = $"คำสั่งให้ช่วยราชการ เลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = data.Profile.Id, + Payload = "", + }; + _dbContext.Set().Add(inbox); + + var noti = new Notification + { + Body = $"คำสั่งให้ช่วยราชการ เลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = data.Profile.Id, + Type = "", + Payload = "", + }; + _dbContext.Set().Add(noti); + await _dbContext.SaveChangesAsync(); + } + + // send cc noti inbox + foreach (var cc in command.Deployments) + { + var pf = await _dbContext.Set().FirstOrDefaultAsync(x => x.CitizenId == cc.CitizenId); + if (pf != null) + { + if (cc.IsSendInbox) + { + var inbox = new Inbox + { + Subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + Body = $"คำสั่งให้ช่วยราชการ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = pf.Id, + Payload = "", + }; + _dbContext.Set().Add(inbox); + } + + if (cc.IsSendMail) + { + // TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่ + // Send noti inbox and email + var subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + var body = $"คำสั่งให้ช่วยราชการ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + _emailSenderService.SendMail(subject, body, "dev@frappet.com"); + } + + + var noti = new Notification + { + Body = $"คำสั่งให้ช่วยราชการ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = pf.Id, + Type = "", + Payload = "", + }; + _dbContext.Set().Add(noti); + } + } + + // change command status + var cmdStatus = await _dbContext.Set().FirstOrDefaultAsync(x => x.Sequence == 5); + command.CommandStatusId = cmdStatus!.Id; + + await _dbContext.SaveChangesAsync(); + } + catch + { + throw; + } + } + + /// + /// C-PM-16 - คำสั่งส่งตัวกลับ + /// + /// object ของรายการคำสั่ง + /// + private async Task ExecuteCommand16Async(Command command) + { + try + { + foreach (var recv in command.Receivers) + { + var data = await _dbContext.Set() + .Include(x => x.Profile) + .ThenInclude(x => x.Salaries) + .ThenInclude(x => x.PositionLevel) + + .FirstOrDefaultAsync(x => x.Id == recv.RefPlacementProfileId); + + if (data == null) + throw new Exception(GlobalMessages.DataNotFound); + + //data.Profile.IsActive = false; + //data.Profile.IsLeave = true; + //data.Profile.LeaveReason = "ออกจากราชการ"; + //data.Profile.LeaveDate = command.CommandAffectDate; + + var lastSarary = data.Profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault(); + var order = 1; + if (lastSarary.Order != null) + order = lastSarary.Order.Value + 1; + + var salary = new ProfileSalary + { + Order = order, + Date = command.CommandAffectDate, + Amount = lastSarary.Amount, + PositionSalaryAmount = lastSarary.PositionSalaryAmount, + MouthSalaryAmount = lastSarary.MouthSalaryAmount, + SalaryClass = "", + SalaryRef = "คำสั่งส่งตัวกลับ คำสั่ง" + command.IssuerOrganizationName + "ที่ " + $"{command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate!.Value.ToThaiFullDate3()}", + + OcId = lastSarary.OcId, + + PositionLevel = lastSarary.PositionLevel, + PositionLineId = lastSarary.PositionLineId, + PositionTypeId = lastSarary.PositionTypeId, + OrganizationShortNameId = lastSarary.OrganizationShortNameId, + PosNoId = lastSarary.PosNoId, + + CommandNo = $"{command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()}", + CommandTypeName = command.CommandType.Name, + + + PositionEmployeeGroupId = null, + PositionEmployeeLevelId = null, + PositionEmployeePositionId = null, + PositionEmployeePositionSideId = null, + PosNoEmployee = "", + + + //PositionPathSideId = lastSarary.PositionPathSideId == null, + PositionExecutiveId = lastSarary.PositionExecutiveId, + //PositionExecutiveSideId = lastSarary.PositionExecutiveSideId, + + IsActive = true, + CreatedAt = DateTime.Now, + CreatedFullName = FullName ?? "System Administrator", + CreatedUserId = UserId ?? "", + LastUpdatedAt = DateTime.Now, + LastUpdateFullName = FullName ?? "System Administrator", + LastUpdateUserId = UserId ?? "", + + }; + + if (lastSarary.PositionPathSideId != null) salary.PositionPathSideId = lastSarary.PositionPathSideId; + if (lastSarary.PositionExecutiveSideId != null) salary.PositionExecutiveSideId = lastSarary.PositionExecutiveSideId; + + data.Profile.Salaries.Add(salary); + + // update placementstatus + data.Status = "DONE"; + + await _dbContext.SaveChangesAsync(); + + // TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่ + // Send noti inbox and email + var subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + var body = $"คำสั่งส่งตัวกลับ เลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + _emailSenderService.SendMail(subject, body, "dev@frappet.com"); + + + var inbox = new Inbox + { + Subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + Body = $"คำสั่งส่งตัวกลับ เลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = data.Profile.Id, + Payload = "", + }; + _dbContext.Set().Add(inbox); + + var noti = new Notification + { + Body = $"คำสั่งส่งตัวกลับ เลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = data.Profile.Id, + Type = "", + Payload = "", + }; + _dbContext.Set().Add(noti); + await _dbContext.SaveChangesAsync(); + } + + // send cc noti inbox + foreach (var cc in command.Deployments) + { + var pf = await _dbContext.Set().FirstOrDefaultAsync(x => x.CitizenId == cc.CitizenId); + if (pf != null) + { + if (cc.IsSendInbox) + { + var inbox = new Inbox + { + Subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + Body = $"คำสั่งส่งตัวกลับ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = pf.Id, + Payload = "", + }; + _dbContext.Set().Add(inbox); + } + + if (cc.IsSendMail) + { + // TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่ + // Send noti inbox and email + var subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + var body = $"คำสั่งส่งตัวกลับ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + _emailSenderService.SendMail(subject, body, "dev@frappet.com"); + } + + + var noti = new Notification + { + Body = $"คำสั่งส่งตัวกลับ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + ReceiverUserId = pf.Id, + Type = "", + Payload = "", + }; + _dbContext.Set().Add(noti); + } + } + + // change command status + var cmdStatus = await _dbContext.Set().FirstOrDefaultAsync(x => x.Sequence == 5); + command.CommandStatusId = cmdStatus!.Id; + + await _dbContext.SaveChangesAsync(); + } + catch + { + throw; + } + } + /// /// C-PM-17 - คำสั่งอนุญาตให้ข้าราชการลาออกจากราชการ ///