using CPRNIMS.Domain.Contracts.Items; using CPRNIMS.Domain.Contracts.SMTP; 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; public ItemMgmtController(ErrorMessageService errorMessageService, IWebHostEnvironment webHostEnvironment, SMTPHelper sMTPHelper, IConfiguration configuration, IItem item) : base(errorMessageService, webHostEnvironment, sMTPHelper, configuration) { _config = configuration; _item= item; } [HttpPost("PostPutItemPath")] public async Task PostPutItemPath(ItemDto itemDto) { return await ExecuteWithErrorHandling( () => _item.PostPutItemCart(itemDto), nameof(PostPutItemPath), true ); } [HttpPost("PutItemDetail")] public async Task PutItemDetail(ItemDto itemDto) { return await ExecuteWithErrorHandling( () => _item.PutItemDetail(itemDto), nameof(PutItemDetail), true ); } [HttpPost("PostPutItemCart")] public async Task PostPutItemCart(ItemDto itemDto) { return await ExecuteWithErrorHandling( () => _item.PostPutItemCart(itemDto), nameof(PostPutItemCart), true ); } [HttpPost("PostPurchRequest")] public async Task PostPurchRequest([FromBody] ItemVM viewModel) { try { // Check if ItemCartId, Qty, and ItemNo are null or not of the same length if (viewModel.ItemCartVM.ItemCartId == null || viewModel.ItemCartVM.Qty == null || viewModel.ItemCartVM.ItemNo == null) { throw new ArgumentNullException("One or more lists in ItemCartVM are null"); } if (viewModel.ItemCartVM.ItemCartId.Count != viewModel.ItemCartVM.Qty.Count || viewModel.ItemCartVM.ItemCartId.Count != viewModel.ItemCartVM.ItemNo.Count) { throw new ArgumentException("The lengths of ItemCartId, Qty, and ItemNo must be the same"); } viewModel.ItemCount = viewModel.ItemCartVM.ItemCartId.Count; long prNo = await _item.GetPRNo(); var itemDto = new ItemDto(); // Process each item in the ItemCartVM foreach (var itemCartId in viewModel.ItemCartVM.ItemCartId) { var index = viewModel.ItemCartVM.ItemCartId.IndexOf(itemCartId); itemDto.ItemCartId = itemCartId; itemDto.Qty = viewModel.ItemCartVM.Qty[index]; itemDto.ItemNo = viewModel.ItemCartVM.ItemNo[index]; itemDto.ItemCount = viewModel.ItemCount; itemDto.RequestTypeId = viewModel.RequestTypeId; itemDto.UserId = viewModel.UserId; itemDto.PRNo = prNo + 1; itemDto.DateNeeded = viewModel.DateNeeded; itemDto.ChargeTo = viewModel.ChargeTo; itemDto.Remarks = viewModel.Remarks; await _item.PostPurchRequest(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 _smptHelper.SendEmailAsync(messageDetails); return Ok(new { success = true }); } catch (Exception ex) { var message = ex.InnerException?.ToString() ?? ex.Message; await PostErrorMessage(message, "WebApi"); return StatusCode(500, new { success = false, message }); } } [HttpPost("PostPutItem")] public async Task 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("GetDepartment")] public async Task GetDepartment(ItemCodeDto itemDto) { return await ExecuteWithErrorHandling( () => _item.GetDepartment(itemDto), nameof(GetDepartment), false ); } [HttpPost("GetItemCart")] public async Task GetItemCart(ItemDto itemDto) { return await ExecuteWithErrorHandling( () => _item.GetItemCart(itemDto), nameof(GetItemCart), false ); } [HttpPost("GetItemList")] public async Task GetItemList(ItemCodeDto itemDto) { return await ExecuteWithErrorHandling( () => _item.GetItemList(itemDto), nameof(GetItemList), false ); } [HttpPost("GetItemDetail")] public async Task GetItemDetail(ItemDto itemDto) { return await ExecuteWithErrorHandling( () => _item.GetItemDetail(itemDto), nameof(GetItemDetail), false ); } [HttpPost("GetItemLocalization")] public async Task GetItemLocalization(ItemDto itemDto) { return await ExecuteWithErrorHandling( () => _item.GetItemLocalization(itemDto), nameof(GetItemLocalization), false ); } [HttpPost("GetItemCateg")] public async Task GetItemCateg(ItemDto itemDto) { return await ExecuteWithErrorHandling( () => _item.GetItemCateg(itemDto), nameof(GetItemCateg), false ); } [HttpPost("GetItemColor")] public async Task GetItemColor(ItemDto itemDto) { return await ExecuteWithErrorHandling( () => _item.GetItemColor(itemDto), nameof(GetItemColor), false ); } [HttpPost("GetItemUOM")] public async Task GetItemUOM(ItemDto itemDto) { return await ExecuteWithErrorHandling( () => _item.GetItemUOM(itemDto), nameof(GetItemUOM), false ); } } }