254 lines
9.3 KiB
C#
254 lines
9.3 KiB
C#
using CPRNIMS.Domain.Contracts.Items;
|
|
using CPRNIMS.Domain.Services;
|
|
using CPRNIMS.Infrastructure.Dto.Items;
|
|
using CPRNIMS.Infrastructure.Helper;
|
|
using CPRNIMS.Infrastructure.Models.Common;
|
|
using CPRNIMS.Infrastructure.ViewModel.Common;
|
|
using CPRNIMS.Infrastructure.ViewModel.Items;
|
|
using CPRNIMS.WebApi.Controllers.Base;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Text;
|
|
|
|
namespace CPRNIMS.WebApi.Controllers.Items
|
|
{
|
|
[Security.AuthorizeRoles("ItemMgmt")]
|
|
public class ItemMgmtController : BaseController
|
|
{
|
|
private readonly IItem _item;
|
|
private readonly IConfiguration _config;
|
|
private readonly SMTPHelper _smtpHelper;
|
|
public ItemMgmtController(ErrorMessageService errorMessageService,
|
|
IWebHostEnvironment webHostEnvironment, SMTPHelper smtpHelper,
|
|
IConfiguration configuration, IItem item) :
|
|
base(errorMessageService, webHostEnvironment, configuration)
|
|
{
|
|
_config = configuration;
|
|
_item= item;
|
|
_smtpHelper = smtpHelper;
|
|
}
|
|
|
|
[HttpPost("PostPutItemPath")]
|
|
public async Task<IActionResult> PostPutItemPath(ItemDto itemDto)
|
|
{
|
|
return await ExecuteWithErrorHandling(
|
|
() => _item.PostPutItemCart(itemDto),
|
|
nameof(PostPutItemPath), true
|
|
);
|
|
}
|
|
[HttpPost("PutItemDetail")]
|
|
public async Task<IActionResult> PutItemDetail(ItemDto itemDto)
|
|
{
|
|
return await ExecuteWithErrorHandling(
|
|
() => _item.PutItemDetail(itemDto),
|
|
nameof(PutItemDetail), true
|
|
);
|
|
}
|
|
|
|
[HttpPost("PostPutItemCart")]
|
|
public async Task<IActionResult> PostPutItemCart(ItemDto itemDto)
|
|
{
|
|
return await ExecuteWithErrorHandling(
|
|
() => _item.PostPutItemCart(itemDto),
|
|
nameof(PostPutItemCart), true
|
|
);
|
|
}
|
|
[HttpPost("PostPurchRequest")]
|
|
public async Task<IActionResult> PostPurchRequest([FromBody] ItemVM viewModel)
|
|
{
|
|
if (viewModel?.ItemCartVM == null)
|
|
return BadRequest("Invalid request.");
|
|
|
|
if (viewModel.ItemCartVM.ItemCartId.Count != viewModel.ItemCartVM.Qty.Count ||
|
|
viewModel.ItemCartVM.ItemCartId.Count != viewModel.ItemCartVM.ItemNo.Count)
|
|
{
|
|
return BadRequest("ItemCartId, Qty, and ItemNo length mismatch.");
|
|
}
|
|
|
|
var result = await ProcessPurchaseRequest(viewModel);
|
|
|
|
return Ok(new { success = result });
|
|
}
|
|
private async Task<ItemDto> ProcessPurchaseRequest(ItemVM viewModel)
|
|
{
|
|
var (prNo,prId) = await _item.GetPRNo();
|
|
bool IsSuccess = false;
|
|
int itemCount = viewModel.ItemCartVM.ItemCartId.Count;
|
|
var dto = new ItemDto();
|
|
for (int i = 0; i < itemCount; i++)
|
|
{
|
|
dto = new ItemDto
|
|
{
|
|
ItemCartId = viewModel.ItemCartVM.ItemCartId[i],
|
|
Qty = viewModel.ItemCartVM.Qty[i],
|
|
ItemNo = viewModel.ItemCartVM.ItemNo[i],
|
|
ItemCount = itemCount,
|
|
RequestTypeId = viewModel.RequestTypeId,
|
|
UserId = viewModel.UserId,
|
|
PRNo = prNo,
|
|
DateNeeded = viewModel.DateNeeded,
|
|
ChargeTo = viewModel.ChargeTo,
|
|
Remarks = viewModel.Remarks,
|
|
ProjectCodeId = viewModel.ProjectCodeId,
|
|
FileName=viewModel.FileName,
|
|
OrigFileName = viewModel.OrigFileName,
|
|
PRId= viewModel.PRId != null ? prId : 0L
|
|
};
|
|
|
|
var response = await _item.PostPurchRequest(dto);
|
|
if (response.messCode == 1)
|
|
IsSuccess = true;
|
|
}
|
|
|
|
if (IsSuccess)
|
|
{
|
|
var attachment = new AttachmentRequest()
|
|
{
|
|
FileName = dto.FileName,
|
|
OrigFileName = dto.OrigFileName,
|
|
PRId = dto.PRId
|
|
};
|
|
await _item.PostPutAttachment(attachment);
|
|
// await SendNotificationEmail(dto);
|
|
}
|
|
|
|
return dto;
|
|
}
|
|
private async Task SendNotificationEmail(ItemDto itemDto)
|
|
{
|
|
var getNotif = await _item.GetNotifUserKey(itemDto);
|
|
|
|
var baseTemplate = EMailTemplate("Content\\SMTPEmailContent", "NewPR.cshtml");
|
|
// Make a fresh copy of the template for each item
|
|
var message = new StringBuilder(baseTemplate);
|
|
|
|
message.Replace("@ViewBag.PRNo", itemDto.PRNo.ToString());
|
|
message.Replace("@ViewBag.Signature", getNotif[0].FullName);
|
|
|
|
var messageDetails = new EmailMessageDetailsVM
|
|
{
|
|
Recipient = getNotif[0].Email,
|
|
Bcc = getNotif[0].RequestorEmail,
|
|
CC = _config["SMTP:CC"] + getNotif[0].RequestorEmail,
|
|
Message = message.ToString(),
|
|
Subject = "Non-Inventory - New PR Created #: " + itemDto.PRNo.ToString(),
|
|
SenderEmail = _configuration["SMTP:SenderEmail"],
|
|
DisplayName = _configuration["SMTP:DisplayName"],
|
|
NewPassword = _configuration["SMTP:Password"],
|
|
OutGoingPort = 587,
|
|
Server = _configuration["SMTP:Server"],
|
|
UserName = _configuration["SMTP:UserName"],
|
|
};
|
|
|
|
await _smtpHelper.SendEmailAsync(messageDetails);
|
|
}
|
|
|
|
[HttpPost("PostPutItem")]
|
|
public async Task<IActionResult> PostPutItem(ItemCodeDto itemDto)
|
|
{
|
|
try
|
|
{
|
|
var item = await _item.PostPutItem(itemDto);
|
|
|
|
var response = new ResponseObject
|
|
{
|
|
itemCode = item.ItemCodeId,
|
|
messCode = 1,
|
|
};
|
|
|
|
return Ok(response);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var message = ex.InnerException?.ToString() ?? ex.Message.ToString();
|
|
await PostErrorMessage(message, "WebApi");
|
|
|
|
return StatusCode(500, new ResponseObject
|
|
{
|
|
message = "An error occurred in the API.",
|
|
messCode = 0,
|
|
IsValid = false
|
|
});
|
|
}
|
|
}
|
|
[HttpPost("GetProjectCodeByTerm")]
|
|
public async Task<IActionResult> GetProjectCodeByTerm(ItemCodeDto itemDto)
|
|
{
|
|
return await ExecuteWithErrorHandling(
|
|
() => _item.GetProjectCodeByTerm(itemDto.FileName),
|
|
nameof(GetProjectCodeByTerm), false
|
|
);
|
|
}
|
|
[HttpPost("GetProjectCode")]
|
|
public async Task<IActionResult> GetProjectCode(ItemCodeDto itemDto)
|
|
{
|
|
var results = await _item.GetProjectCode();
|
|
return Ok(new { message = "success", messCode = 1, data = results });
|
|
}
|
|
[HttpPost("GetDepartment")]
|
|
public async Task<IActionResult> GetDepartment(ItemCodeDto itemDto)
|
|
{
|
|
return await ExecuteWithErrorHandling(
|
|
() => _item.GetDepartment(itemDto),
|
|
nameof(GetDepartment), false
|
|
);
|
|
}
|
|
|
|
[HttpPost("GetItemCart")]
|
|
public async Task<IActionResult> GetItemCart(ItemDto itemDto)
|
|
{
|
|
return await ExecuteWithErrorHandling(
|
|
() => _item.GetItemCart(itemDto),
|
|
nameof(GetItemCart), false
|
|
);
|
|
}
|
|
[HttpPost("GetItemList")]
|
|
public async Task<IActionResult> GetItemList(ItemCodeDto itemDto)
|
|
{
|
|
return await ExecuteWithErrorHandling(
|
|
() => _item.GetItemList(itemDto),
|
|
nameof(GetItemList), false
|
|
);
|
|
}
|
|
[HttpPost("GetItemDetail")]
|
|
public async Task<IActionResult> GetItemDetail(ItemDto itemDto)
|
|
{
|
|
return await ExecuteWithErrorHandling(
|
|
() => _item.GetItemDetail(itemDto),
|
|
nameof(GetItemDetail), false
|
|
);
|
|
}
|
|
[HttpPost("GetItemLocalization")]
|
|
public async Task<IActionResult> GetItemLocalization(ItemDto itemDto)
|
|
{
|
|
return await ExecuteWithErrorHandling(
|
|
() => _item.GetItemLocalization(itemDto),
|
|
nameof(GetItemLocalization), false
|
|
);
|
|
}
|
|
[HttpPost("GetItemCateg")]
|
|
public async Task<IActionResult> GetItemCateg(ItemDto itemDto)
|
|
{
|
|
return await ExecuteWithErrorHandling(
|
|
() => _item.GetItemCateg(itemDto),
|
|
nameof(GetItemCateg), false
|
|
);
|
|
}
|
|
[HttpPost("GetItemColor")]
|
|
public async Task<IActionResult> GetItemColor(ItemDto itemDto)
|
|
{
|
|
return await ExecuteWithErrorHandling(
|
|
() => _item.GetItemColor(itemDto),
|
|
nameof(GetItemColor), false
|
|
);
|
|
}
|
|
[HttpPost("GetItemUOM")]
|
|
public async Task<IActionResult> GetItemUOM(ItemDto itemDto)
|
|
{
|
|
return await ExecuteWithErrorHandling(
|
|
() => _item.GetItemUOM(itemDto),
|
|
nameof(GetItemUOM), false
|
|
);
|
|
}
|
|
}
|
|
}
|