From 06126796487e733cce8d872e45869079694a9697 Mon Sep 17 00:00:00 2001 From: rowell_m_soriano Date: Thu, 12 Feb 2026 10:41:44 +0800 Subject: [PATCH] Project code maintenance and pr item attachment --- CPRNIMS.Domain/Contracts/Items/IItem.cs | 6 +- CPRNIMS.Domain/Contracts/PR/IPRequest.cs | 3 + CPRNIMS.Domain/Services/ContentTypeHelper.cs | 47 + CPRNIMS.Domain/Services/Items/Item.cs | 519 +- CPRNIMS.Domain/Services/PR/PRequest.cs | 75 +- CPRNIMS.Domain/UIContracts/PR/IPRequest.cs | 10 +- CPRNIMS.Domain/UIServices/PR/PRequest.cs | 11 + .../Database/NonInventoryDbContext.cs | 4 + .../Dto/Items/AttachmentRequest.cs | 15 + .../Dto/Items/ItemCartDto.cs | 5 +- .../Dto/Items/ItemCodeDto.cs | 3 +- CPRNIMS.Infrastructure/Dto/Items/ItemDto.cs | 4 + CPRNIMS.Infrastructure/Dto/PR/PRDto.cs | 9 + .../Entities/Common/ErrorLog.cs | 23 + .../Entities/PO/PODetails.cs | 20 + .../Entities/Purchasing/PR.cs | 3 +- .../Entities/Purchasing/PRAttachments.cs | 20 + .../Entities/Purchasing/PRItemList.cs | 1 + .../Entities/Purchasing/ProjectCodes.cs | 22 + .../ViewModel/Items/ItemVM.cs | 6 + CPRNIMS.Infrastructure/ViewModel/PR/PRVM.cs | 9 + CPRNIMS.WebApi/CPRNIMS.WebApi.csproj | 5 + .../Controllers/Account/AccountController.cs | 4 +- .../Controllers/Account/AnonController.cs | 2 +- .../Controllers/Base/BaseController.cs | 4 +- .../Canvass/CanvassMgmtController.cs | 2 +- .../Controllers/Finance/RRMgmtController.cs | 6 +- .../Inventory/InventoryMgmtController.cs | 4 +- .../Controllers/Items/ItemMgmtController.cs | 152 +- .../Controllers/PO/POMgmtController.cs | 2 +- .../Controllers/PR/PRMgmtController.cs | 23 +- .../Receiving/ReceivingController.cs | 7 +- .../Controllers/SMTP/SMTPMgmtController.cs | 4 +- CPRNIMS.WebApi/Exceptions/AppException.cs | 13 + .../Middleware/GlobalExceptionMiddleware.cs | 89 + CPRNIMS.WebApi/Program.cs | 2 + CPRNIMS.WebApi/Sql/Table.sql | 20 +- CPRNIMS.WebApps/CPRNIMS.WebApps.csproj | 3 +- .../Controllers/Account/AccountController.cs | 2 +- .../Controllers/Base/BaseMethod.cs | 41 +- .../Canvass/CanvassMgmtController.cs | 7 +- .../Controllers/Finance/RRMgmtController.cs | 10 +- CPRNIMS.WebApps/Controllers/HomeController.cs | 2 +- .../Inventory/InventoryMgmtController.cs | 16 +- .../Controllers/Items/ItemMgmtController.cs | 145 +- .../Controllers/PO/POMgmtController.cs | 7 +- .../Controllers/PR/PRMgmtController.cs | 53 +- .../Receiving/ReceivingController.cs | 7 +- .../Controllers/SMTP/SMTPMgmtController.cs | 8 +- CPRNIMS.WebApps/Views/ItemMgmt/Index.cshtml | 4 +- .../Views/ItemMgmt/ItemCart.cshtml | 127 +- .../Views/PRMgmt/ProjectCode.cshtml | 105 + .../Shared/PagesView/Item/_Scripts.cshtml | 9 +- .../Shared/PagesView/PR/_PRScripts.cshtml | 10 +- .../Shared/PagesView/PR/_PRTracking.cshtml | 10 +- .../Views/Shared/PartialView/_Sidebar.cshtml | 28 - CPRNIMS.WebApps/Views/Shared/_Layout.cshtml | 2 +- .../d08072a4-5cb2-4139-843d-68d687fd03e0.csv | 6666 +++++++++++++++++ .../Items/{ItemCartV2.js => ItemCartV3.js} | 146 +- ...temManagementV6.js => ItemManagementV7.js} | 3 +- .../Items/{ItemViewV5.js => ItemViewV6.js} | 33 +- .../{PostPutItemV2.js => PostPutItemV3.js} | 264 +- .../PR/{Configv4.js => Configv5.js} | 3 + .../PR/{PRButtonv2.js => PRButtonv3.js} | 7 + .../PR/{PRColumnV6.js => PRColumnV7.js} | 22 +- .../PR/{PRPutPostV9.js => PRPutPost.js} | 62 +- .../PR/{PRViewV4.js => PRViewV5.js} | 63 +- .../wwwroot/JsFunctions/PR/ProjectCode.js | 22 + .../wwwroot/css/Item/{cart.css => cartV2.css} | 5 + 69 files changed, 8366 insertions(+), 680 deletions(-) create mode 100644 CPRNIMS.Domain/Services/ContentTypeHelper.cs create mode 100644 CPRNIMS.Infrastructure/Dto/Items/AttachmentRequest.cs create mode 100644 CPRNIMS.Infrastructure/Entities/Common/ErrorLog.cs create mode 100644 CPRNIMS.Infrastructure/Entities/PO/PODetails.cs create mode 100644 CPRNIMS.Infrastructure/Entities/Purchasing/PRAttachments.cs create mode 100644 CPRNIMS.Infrastructure/Entities/Purchasing/ProjectCodes.cs create mode 100644 CPRNIMS.WebApi/Exceptions/AppException.cs create mode 100644 CPRNIMS.WebApi/Middleware/GlobalExceptionMiddleware.cs create mode 100644 CPRNIMS.WebApps/Views/PRMgmt/ProjectCode.cshtml create mode 100644 CPRNIMS.WebApps/wwwroot/Content/Uploads/PRAttachment/d08072a4-5cb2-4139-843d-68d687fd03e0.csv rename CPRNIMS.WebApps/wwwroot/JsFunctions/Items/{ItemCartV2.js => ItemCartV3.js} (65%) rename CPRNIMS.WebApps/wwwroot/JsFunctions/Items/{ItemManagementV6.js => ItemManagementV7.js} (98%) rename CPRNIMS.WebApps/wwwroot/JsFunctions/Items/{ItemViewV5.js => ItemViewV6.js} (93%) rename CPRNIMS.WebApps/wwwroot/JsFunctions/Items/{PostPutItemV2.js => PostPutItemV3.js} (68%) rename CPRNIMS.WebApps/wwwroot/JsFunctions/PR/{Configv4.js => Configv5.js} (85%) rename CPRNIMS.WebApps/wwwroot/JsFunctions/PR/{PRButtonv2.js => PRButtonv3.js} (82%) rename CPRNIMS.WebApps/wwwroot/JsFunctions/PR/{PRColumnV6.js => PRColumnV7.js} (91%) rename CPRNIMS.WebApps/wwwroot/JsFunctions/PR/{PRPutPostV9.js => PRPutPost.js} (87%) rename CPRNIMS.WebApps/wwwroot/JsFunctions/PR/{PRViewV4.js => PRViewV5.js} (93%) create mode 100644 CPRNIMS.WebApps/wwwroot/JsFunctions/PR/ProjectCode.js rename CPRNIMS.WebApps/wwwroot/css/Item/{cart.css => cartV2.css} (91%) diff --git a/CPRNIMS.Domain/Contracts/Items/IItem.cs b/CPRNIMS.Domain/Contracts/Items/IItem.cs index 4cefed3..3ea1243 100644 --- a/CPRNIMS.Domain/Contracts/Items/IItem.cs +++ b/CPRNIMS.Domain/Contracts/Items/IItem.cs @@ -2,6 +2,7 @@ using CPRNIMS.Infrastructure.Entities.Account; using CPRNIMS.Infrastructure.Entities.Items; using CPRNIMS.Infrastructure.Entities.Purchasing; +using CPRNIMS.Infrastructure.Models.Common; using System; using System.Collections.Generic; using System.Linq; @@ -21,11 +22,12 @@ namespace CPRNIMS.Domain.Contracts.Items Task> GetItemColor(ItemDto itemDto); Task> GetItemUOM(ItemDto itemDto); Task> GetNotifUserKey(ItemDto itemDto); - Task GetPRNo(); - Task PostPurchRequest(ItemDto itemDto); + Task<(long, long)> GetPRNo(); + Task PostPurchRequest(ItemDto itemDto); Task PostPutItem(ItemCodeDto itemDto); Task PutItemDetail(ItemDto itemDto); Task PostPutItemCart(ItemDto itemDto); Task PostPutItemPath(ItemDto itemDto); + Task PostPutAttachment(AttachmentRequest attach); } } diff --git a/CPRNIMS.Domain/Contracts/PR/IPRequest.cs b/CPRNIMS.Domain/Contracts/PR/IPRequest.cs index bde3479..78d4538 100644 --- a/CPRNIMS.Domain/Contracts/PR/IPRequest.cs +++ b/CPRNIMS.Domain/Contracts/PR/IPRequest.cs @@ -2,6 +2,7 @@ using CPRNIMS.Infrastructure.Entities.Common; using CPRNIMS.Infrastructure.Entities.Purchasing; using CPRNIMS.Infrastructure.Entities.SMTP; +using CPRNIMS.Infrastructure.Models.Common; using System; using System.Collections.Generic; using System.Linq; @@ -28,6 +29,7 @@ namespace CPRNIMS.Domain.Contracts.PR Task> GetSupplierAlterOfferDetails(PRDto PRDto); Task> GetNotificationById(PRDto PRDto); Task> GetApproverName(PRDto PRDto); + Task> GetProjectCodes(PRDto pRDto); Task PRItemRemoval(PRDto pRDto); Task PostPRApproveReject(PRDto PRDto); Task PostPutReceiving(PRDto PRDto); @@ -35,5 +37,6 @@ namespace CPRNIMS.Domain.Contracts.PR Task PutItemDetail(PRDto PRDto); Task PostPutDeniedItem(PRDto PRDto); Task PutSupplierAlterOffer(PRDto pRDto); + Task PostPutProjectCode(PRDto prDto); } } diff --git a/CPRNIMS.Domain/Services/ContentTypeHelper.cs b/CPRNIMS.Domain/Services/ContentTypeHelper.cs new file mode 100644 index 0000000..5f9004f --- /dev/null +++ b/CPRNIMS.Domain/Services/ContentTypeHelper.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPRNIMS.Domain.Services +{ + public static class ContentTypeHelper + { + public static string GetContentType(string fileName) + { + var extension = Path.GetExtension(fileName).ToLowerInvariant(); + return extension switch + { + ".pdf" => "application/pdf", + ".doc" => "application/msword", + ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + ".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation", + ".xls" => "application/vnd.ms-excel", + ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + ".png" => "image/png", + ".jpg" or ".jpeg" => "image/jpeg", + ".gif" => "image/gif", + ".txt" => "text/plain", + _ => "application/octet-stream" + }; + } + public static string ValidateFile(string filePath, string uploadsPath) + { + var message = ""; + // Security: Prevent directory traversal attacks + var fullPath = Path.GetFullPath(filePath); + + if (!fullPath.StartsWith(Path.GetFullPath(uploadsPath))) + { + message = "Invalid file path"; + } + + if (!System.IO.File.Exists(filePath)) + { + message = "File not found"; + } + return message; + } + } +} diff --git a/CPRNIMS.Domain/Services/Items/Item.cs b/CPRNIMS.Domain/Services/Items/Item.cs index 4bcd635..9a6e3ce 100644 --- a/CPRNIMS.Domain/Services/Items/Item.cs +++ b/CPRNIMS.Domain/Services/Items/Item.cs @@ -1,16 +1,13 @@ -using CPRNIMS.Domain.Contracts.Account; -using CPRNIMS.Domain.Contracts.Items; +using CPRNIMS.Domain.Contracts.Items; using CPRNIMS.Infrastructure.Database; using CPRNIMS.Infrastructure.Dto.Canvass; using CPRNIMS.Infrastructure.Dto.Items; using CPRNIMS.Infrastructure.Entities.Account; -using CPRNIMS.Infrastructure.Entities.Canvass; using CPRNIMS.Infrastructure.Entities.Items; using CPRNIMS.Infrastructure.Entities.Purchasing; +using CPRNIMS.Infrastructure.Models.Common; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; @@ -26,344 +23,275 @@ namespace CPRNIMS.Domain.Services.Items } public async Task PostPutItem(ItemCodeDto itemDto) { - try + var messCodeParam = new SqlParameter("@MessCode", SqlDbType.TinyInt) { - var messCodeParam = new SqlParameter("@MessCode", SqlDbType.TinyInt) - { - Direction = ParameterDirection.Output - }; - var messageParam = new SqlParameter("@Message", SqlDbType.VarChar, 500) - { - Direction = ParameterDirection.Output - }; - var itemCodeParam = new SqlParameter("@ItemCode", SqlDbType.BigInt) - { - Direction = ParameterDirection.Output - }; - - await _dbContext.Database.ExecuteSqlRawAsync( - "EXEC PostPutItem @ItemCodeId, @ItemName, @ItemDescription, @ItemCategoryId, @Status, @UserId, " + - "@ItemCode OUTPUT, @MessCode OUTPUT, @Message OUTPUT", - new SqlParameter("@ItemCodeId", itemDto.ItemCodeId != null ? itemDto.ItemCodeId : 0L), - new SqlParameter("@ItemName", itemDto.ItemName ?? (object)DBNull.Value), - new SqlParameter("@ItemDescription", itemDto.ItemDescription ?? (object)DBNull.Value), - new SqlParameter("@ItemCategoryId", itemDto.ItemCategoryId), - new SqlParameter("@Status", itemDto.Status != null ? itemDto.Status : 4), - new SqlParameter("@UserId", itemDto.UserId ?? (object)DBNull.Value), - itemCodeParam, - messCodeParam, - messageParam - ); - - var messCode = (byte)messCodeParam.Value; - var message = messageParam.Value?.ToString(); - var itemCode = itemCodeParam.Value != DBNull.Value ? (long?)itemCodeParam.Value : null; - - return new ItemCodeDto - { - ItemCodeId = Convert.ToInt64(itemCode), - ItemName = itemDto.ItemName, - ItemDescription = itemDto.ItemDescription, - ItemCategoryId = itemDto.ItemCategoryId, - Status = itemDto.Status, - UserId = itemDto.UserId - }; - } - catch (Exception ex) + Direction = ParameterDirection.Output + }; + var messageParam = new SqlParameter("@Message", SqlDbType.VarChar, 500) { - // You could log ex.Message or rethrow with a custom message - throw new Exception("An error occurred while executing PostPutItem.", ex); - } + Direction = ParameterDirection.Output + }; + var itemCodeParam = new SqlParameter("@ItemCode", SqlDbType.BigInt) + { + Direction = ParameterDirection.Output + }; + + await _dbContext.Database.ExecuteSqlRawAsync( + "EXEC PostPutItem @ItemCodeId, @ItemName, @ItemDescription, @ItemCategoryId, @Status, @UserId, " + + "@ItemCode OUTPUT, @MessCode OUTPUT, @Message OUTPUT", + new SqlParameter("@ItemCodeId", itemDto.ItemCodeId != null ? itemDto.ItemCodeId : 0L), + new SqlParameter("@ItemName", itemDto.ItemName ?? (object)DBNull.Value), + new SqlParameter("@ItemDescription", itemDto.ItemDescription ?? (object)DBNull.Value), + new SqlParameter("@ItemCategoryId", itemDto.ItemCategoryId), + new SqlParameter("@Status", itemDto.Status != null ? itemDto.Status : 4), + new SqlParameter("@UserId", itemDto.UserId ?? (object)DBNull.Value), + itemCodeParam, + messCodeParam, + messageParam + ); + + var messCode = (byte)messCodeParam.Value; + var message = messageParam.Value?.ToString(); + var itemCode = itemCodeParam.Value != DBNull.Value ? (long?)itemCodeParam.Value : null; + + return new ItemCodeDto + { + ItemCodeId = Convert.ToInt64(itemCode), + ItemName = itemDto.ItemName, + ItemDescription = itemDto.ItemDescription, + ItemCategoryId = itemDto.ItemCategoryId, + Status = itemDto.Status, + UserId = itemDto.UserId + }; } public async Task PutItemDetail(ItemDto itemDto) { - try - { - await _dbContext.Database - .ExecuteSqlRawAsync($"EXEC PutItemDetail @ItemCodeId,@ItemLocalId,@ItemTypeId,@UOMId,@ItemColorId,@IsActive," + - $"@UserId,@ItemNo,@PRTypeId,@PackagingTypeId,@Qty,@ItemAttachId,@ItemAttachPath,@IsCommon,@ItemCategoryId," + - $"@RequestTypeId,@ItemName,@ItemDescription,@IsMDLD", - new SqlParameter("@ItemCodeId", itemDto.ItemCodeId != null ? itemDto.ItemCodeId : 0L), - new SqlParameter("@ItemLocalId", itemDto.ItemLocalId), - new SqlParameter("@ItemTypeId", itemDto.ItemTypeId), - new SqlParameter("@UOMId", itemDto.UOMId), - new SqlParameter("@ItemColorId", itemDto.ItemColorId), - new SqlParameter("@IsActive", itemDto.IsActive), - new SqlParameter("@UserId", itemDto.UserId), - new SqlParameter("@ItemNo", itemDto.ItemNo), - new SqlParameter("@PRTypeId", itemDto.PRTypeId), - new SqlParameter("@PackagingTypeId", itemDto.PackagingTypeId), - new SqlParameter("@Qty", itemDto.Qty), - new SqlParameter("@ItemAttachId", itemDto.ItemAttachId != null ? itemDto.ItemAttachId : 0L), - new SqlParameter("@ItemAttachPath", itemDto.ItemAttachPath ?? "N/A"), - new SqlParameter("@IsCommon", itemDto.IsCommon), - new SqlParameter("@ItemCategoryId", itemDto.ItemCategoryId), - new SqlParameter("@RequestTypeId", itemDto.RequestTypeId), - new SqlParameter("@ItemName", itemDto.ItemName), - new SqlParameter("@ItemDescription", itemDto.ItemDescription), - new SqlParameter("@IsMDLD", itemDto.IsMDLD)); + await _dbContext.Database + .ExecuteSqlRawAsync($"EXEC PutItemDetail @ItemCodeId,@ItemLocalId,@ItemTypeId,@UOMId,@ItemColorId,@IsActive," + + $"@UserId,@ItemNo,@PRTypeId,@PackagingTypeId,@Qty,@ItemAttachId,@ItemAttachPath,@IsCommon,@ItemCategoryId," + + $"@RequestTypeId,@ItemName,@ItemDescription,@IsMDLD", + new SqlParameter("@ItemCodeId", itemDto.ItemCodeId != null ? itemDto.ItemCodeId : 0L), + new SqlParameter("@ItemLocalId", itemDto.ItemLocalId), + new SqlParameter("@ItemTypeId", itemDto.ItemTypeId), + new SqlParameter("@UOMId", itemDto.UOMId), + new SqlParameter("@ItemColorId", itemDto.ItemColorId), + new SqlParameter("@IsActive", itemDto.IsActive), + new SqlParameter("@UserId", itemDto.UserId), + new SqlParameter("@ItemNo", itemDto.ItemNo), + new SqlParameter("@PRTypeId", itemDto.PRTypeId), + new SqlParameter("@PackagingTypeId", itemDto.PackagingTypeId), + new SqlParameter("@Qty", itemDto.Qty), + new SqlParameter("@ItemAttachId", itemDto.ItemAttachId != null ? itemDto.ItemAttachId : 0L), + new SqlParameter("@ItemAttachPath", itemDto.ItemAttachPath ?? "N/A"), + new SqlParameter("@IsCommon", itemDto.IsCommon), + new SqlParameter("@ItemCategoryId", itemDto.ItemCategoryId), + new SqlParameter("@RequestTypeId", itemDto.RequestTypeId), + new SqlParameter("@ItemName", itemDto.ItemName), + new SqlParameter("@ItemDescription", itemDto.ItemDescription), + new SqlParameter("@IsMDLD", itemDto.IsMDLD)); - return new Infrastructure.Entities.Items.Item(); - } - catch (Exception ex) - { - ex.ToString(); - throw; - } + return new Infrastructure.Entities.Items.Item(); } public async Task> GetItemLocalization(ItemDto itemDto) { - try - { - var localizations = await _dbContext.ItemLocalizations - .Where(ic => ic.IsActive == true && - EF.Functions.Like(ic.ItemLocalName, $"%{itemDto.ItemLocalName}%")) - .Take(15) - .ToListAsync(); + var localizations = await _dbContext.ItemLocalizations + .Where(ic => ic.IsActive == true && + EF.Functions.Like(ic.ItemLocalName, $"%{itemDto.ItemLocalName}%")) + .Take(15) + .ToListAsync(); - return localizations ?? new List(); - } - catch (SqlException ex) - { - ex.ToString(); - throw; - } + return localizations ?? new List(); } public async Task> GetItemCateg(ItemDto itemDto) { - try + if (itemDto.ItemCategoryId == 0 || itemDto.ItemCategoryId == null) { - if(itemDto.ItemCategoryId ==0 || itemDto.ItemCategoryId == null) - { - var categories = await _dbContext.ItemCategories - .Where(ic => ic.IsActive == true) - .ToListAsync(); - return categories ?? new List(); - } - else - { - var categories = await _dbContext.ItemCategories - .Where(ic => ic.IsActive == true && ic.ItemCategoryId == itemDto.ItemCategoryId) - .ToListAsync(); - return categories ?? new List(); - } - + var categories = await _dbContext.ItemCategories + .Where(ic => ic.IsActive == true) + .ToListAsync(); + return categories ?? new List(); } - catch (SqlException ex) + else { - ex.ToString(); - throw; + var categories = await _dbContext.ItemCategories + .Where(ic => ic.IsActive == true && ic.ItemCategoryId == itemDto.ItemCategoryId) + .ToListAsync(); + return categories ?? new List(); } } public async Task> GetItemDetail(ItemDto itemDto) { - try - { - var allItems = await _dbContext.Items - .FromSqlRaw($"EXEC GetItemDetail @ItemCodeId,@UserId", - new SqlParameter("@ItemCodeId", itemDto.ItemCodeId), - new SqlParameter("@UserId", itemDto.UserId)) - .ToListAsync(); + var allItems = await _dbContext.Items + .FromSqlRaw($"EXEC GetItemDetail @ItemCodeId,@UserId", + new SqlParameter("@ItemCodeId", itemDto.ItemCodeId), + new SqlParameter("@UserId", itemDto.UserId)) + .ToListAsync(); - return allItems ?? new List(); - } - catch (SqlException ex) - { - ex.ToString(); - throw; - } + return allItems ?? new List(); } public async Task> GetItemList(ItemCodeDto itemCode) { - try - { - var allItems = await _dbContext.ItemList - .FromSqlRaw($"EXEC GetItemList @UserId = '{itemCode.UserId}'") - .ToListAsync(); + var allItems = await _dbContext.ItemList + .FromSqlRaw($"EXEC GetItemList @UserId = '{itemCode.UserId}'") + .ToListAsync(); - return allItems ?? new List(); - } - catch (SqlException ex) - { - ex.ToString(); - throw; - } + return allItems ?? new List(); } public async Task> GetItemColor(ItemDto itemDto) { - try - { - var colors = await _dbContext.ItemColors - .Where(ic => EF.Functions.Like(ic.ItemColorName, $"%{itemDto.ItemColorName}%")) - .Take(5) - .ToListAsync(); + var colors = await _dbContext.ItemColors + .Where(ic => EF.Functions.Like(ic.ItemColorName, $"%{itemDto.ItemColorName}%")) + .Take(5) + .ToListAsync(); - return colors ?? new List(); - } - catch (SqlException ex) - { - ex.ToString(); - throw; - } + return colors ?? new List(); } public async Task> GetItemUOM(ItemDto itemDto) { - try - { - var uoms = await _dbContext.UnitOfMessures - .Where(ic => ic.IsActive == true && - EF.Functions.Like(ic.UOMName, $"%{itemDto.UOMName}%")) - .Take(150) - .ToListAsync(); + var uoms = await _dbContext.UnitOfMessures + .Where(ic => ic.IsActive == true && + EF.Functions.Like(ic.UOMName, $"%{itemDto.UOMName}%")) + .Take(150) + .ToListAsync(); - return uoms ?? new List(); - } - catch (SqlException ex) - { - ex.ToString(); - throw; - } + return uoms ?? new List(); } public async Task PostPutItemPath(ItemDto itemDto) { - try - { - var isExist = await _dbContext.ItemAttachements + var isExist = await _dbContext.ItemAttachements .FirstOrDefaultAsync(ia => ia.IsActive == true && ia.ItemNo == itemDto.ItemNo); - if(isExist != null) - { - isExist.ItemAttachPath = itemDto.ItemAttachPath; - await _dbContext.SaveChangesAsync(); - return new ItemAttachement(); - } - else - { - return new ItemAttachement(); - } - } - catch (SqlException ex) + if (isExist != null) { - ex.ToString(); - throw; + isExist.ItemAttachPath = itemDto.ItemAttachPath; + await _dbContext.SaveChangesAsync(); + return new ItemAttachement(); + } + else + { + return new ItemAttachement(); } } public async Task PostPutItemCart(ItemDto itemDto) { - try - { - await _dbContext.Database - .ExecuteSqlRawAsync("EXEC PostPutItemCart @ItemCartId,@ItemNo,@UserId,@UOMId,@ItemColorId,@ItemCategoryId,@PackagingTypeId,@ItemAttachId,@ItemLocalId", - new SqlParameter("@ItemCartId", itemDto.ItemCartId != null ? itemDto.ItemCartId : 0L), - new SqlParameter("@ItemNo", itemDto.ItemNo != null ? itemDto.ItemNo : 0L), - new SqlParameter("@UOMId", itemDto.UOMId), - new SqlParameter("@ItemColorId", itemDto.ItemColorId), - new SqlParameter("@ItemCategoryId", itemDto.ItemCategoryId), - new SqlParameter("@PackagingTypeId", itemDto.PackagingTypeId), - new SqlParameter("@ItemAttachId", itemDto.ItemAttachId), - new SqlParameter("@UserId", itemDto.UserId), - new SqlParameter("@ItemLocalId", itemDto.ItemLocalId)); - return new ItemCart(); - } - catch (SqlException ex) - { - ex.ToString(); - throw; - } + await _dbContext.Database + .ExecuteSqlRawAsync("EXEC PostPutItemCart @ItemCartId,@ItemNo,@UserId,@UOMId,@ItemColorId,@ItemCategoryId,@PackagingTypeId,@ItemAttachId,@ItemLocalId", + new SqlParameter("@ItemCartId", itemDto.ItemCartId != null ? itemDto.ItemCartId : 0L), + new SqlParameter("@ItemNo", itemDto.ItemNo != null ? itemDto.ItemNo : 0L), + new SqlParameter("@UOMId", itemDto.UOMId), + new SqlParameter("@ItemColorId", itemDto.ItemColorId), + new SqlParameter("@ItemCategoryId", itemDto.ItemCategoryId), + new SqlParameter("@PackagingTypeId", itemDto.PackagingTypeId), + new SqlParameter("@ItemAttachId", itemDto.ItemAttachId), + new SqlParameter("@UserId", itemDto.UserId), + new SqlParameter("@ItemLocalId", itemDto.ItemLocalId)); + return new ItemCart(); } public async Task> GetItemCart(ItemDto itemDto) { - try - { - var allItems = await _dbContext.ItemCarts - .FromSqlRaw($"EXEC GetItemCart @UserId,@RequestTypeId,@IsCount", - new SqlParameter("@UserId", itemDto.UserId), - new SqlParameter("@RequestTypeId", itemDto.RequestTypeId), - new SqlParameter("@IsCount", itemDto.IsCount)) - .ToListAsync(); + var allItems = await _dbContext.ItemCarts + .FromSqlRaw($"EXEC GetItemCart @UserId,@RequestTypeId,@IsCount", + new SqlParameter("@UserId", itemDto.UserId), + new SqlParameter("@RequestTypeId", itemDto.RequestTypeId), + new SqlParameter("@IsCount", itemDto.IsCount)) + .ToListAsync(); - return allItems ?? new List(); - } - catch (SqlException ex) - { - ex.ToString(); - throw; - } + return allItems ?? new List(); } - public async Task PostPurchRequest(ItemDto itemDto) + public async Task PostPurchRequest(ItemDto itemDto) + { + var (messCode, message) = OutputParamMessage.CreateOutputParams(); + + if (itemDto.RequestTypeId == 1)//Internal + { + itemDto.Status = 2; + itemDto.IsApproved = 2; + + await _dbContext.Database + .ExecuteSqlRawAsync("EXEC PostPutReqItems @UserId, @RequestItemId, @ItemNo, @QtyRequest,@Status,@IsApproved,@QtyReceived,@LotId," + + "@MessCode OUTPUT, @Message OUTPUT", + new SqlParameter("@UserId", itemDto.UserId), + new SqlParameter("@ItemNo", itemDto.ItemNo), + new SqlParameter("@RequestItemId", itemDto.RequestItemId != null ? itemDto.RequestItemId : 0L), + new SqlParameter("@QtyRequest", itemDto.Qty), + new SqlParameter("@Status", itemDto.Status), + new SqlParameter("@IsApproved", itemDto.IsApproved), + new SqlParameter("@QtyReceived", itemDto.QtyReceived), + new SqlParameter("@LotId", itemDto.LotId), + messCode,message); + } + else + { + await _dbContext.Database + .ExecuteSqlRawAsync("EXEC PostPurchRequest @ItemCartId, @IsActive, @UserId,@ItemCount," + + "@PRNo,@DateNeeded,@Qty,@ChargeTo,@Remarks,@ProjectCodeId,@MessCode OUTPUT, @Message OUTPUT", + new SqlParameter("@ItemCartId", itemDto.ItemCartId != null ? itemDto.ItemCartId : 0L), + new SqlParameter("@IsActive", 1), + new SqlParameter("@UserId", itemDto.UserId), + new SqlParameter("@ItemCount", itemDto.ItemCount), + new SqlParameter("@PRNo", itemDto.PRNo), + new SqlParameter("@DateNeeded", itemDto.DateNeeded), + new SqlParameter("@Qty", itemDto.Qty), + new SqlParameter("@ChargeTo", itemDto.ChargeTo), + new SqlParameter("@Remarks", itemDto.Remarks ?? "N/A"), + new SqlParameter("@ProjectCodeId", itemDto.ProjectCodeId), + messCode, message); + } + + return new ResponseObject() { + messCode= (byte)messCode.Value, + message= message.Value.ToString() + }; + } + public async Task PostPutAttachment(AttachmentRequest attach) + { + var existing = await _dbContext.PRAttachments + .FirstOrDefaultAsync(a => a.PRId == attach.PRId); + if (existing == null) + { + var attchmnt = new PRAttachments() + { + FileName = attach.FileName, + OrigFileName = attach.OrigFileName, + PRId = attach.PRId + }; + await _dbContext.PRAttachments.AddAsync(attchmnt); + } + else + { + existing.OrigFileName = attach.OrigFileName; + existing.FileName = Guid.NewGuid().ToString(); + } + await _dbContext.SaveChangesAsync(); + } + public async Task<(long,long)> GetPRNo() { try { - if(itemDto.RequestTypeId == 1)//Internal - { - itemDto.Status = 2; - itemDto.IsApproved = 2; - - await _dbContext.Database - .ExecuteSqlRawAsync("EXEC PostPutReqItems @UserId, @RequestItemId, @ItemNo, @QtyRequest,@Status,@IsApproved,@QtyReceived,@LotId", - new SqlParameter("@UserId", itemDto.UserId), - new SqlParameter("@ItemNo", itemDto.ItemNo), - new SqlParameter("@RequestItemId", itemDto.RequestItemId != null ? itemDto.RequestItemId : 0L), - new SqlParameter("@QtyRequest", itemDto.Qty), - new SqlParameter("@Status", itemDto.Status), - new SqlParameter("@IsApproved", itemDto.IsApproved), - new SqlParameter("@QtyReceived", itemDto.QtyReceived), - new SqlParameter("@LotId", itemDto.LotId)); - } - else - { - await _dbContext.Database - .ExecuteSqlRawAsync("EXEC PostPurchRequest @ItemCartId, @IsActive, @UserId,@ItemCount,@PRNo,@DateNeeded,@Qty,@ChargeTo,@Remarks", - new SqlParameter("@ItemCartId", itemDto.ItemCartId != null ? itemDto.ItemCartId : 0L), - new SqlParameter("@IsActive", 1), - new SqlParameter("@UserId", itemDto.UserId), - new SqlParameter("@ItemCount", itemDto.ItemCount), - new SqlParameter("@PRNo", itemDto.PRNo), - new SqlParameter("@DateNeeded", itemDto.DateNeeded), - new SqlParameter("@Qty", itemDto.Qty), - new SqlParameter("@ChargeTo", itemDto.ChargeTo), - new SqlParameter("@Remarks", itemDto.Remarks)); - } - return new ItemCart(); - } - catch (SqlException ex) - { - ex.ToString(); - throw; - } - } - public async Task GetPRNo() - { - try - { - // Query the PRs table var latestPR = await _dbContext.PRs - .OrderByDescending(ic => ic.PRNo) // Sort by PRNo in descending order - .FirstOrDefaultAsync(); // Retrieve the first record + .Where(ic => ic.PRNo != null) + .OrderByDescending(ic => ic.PRNo) + .FirstOrDefaultAsync(); if (latestPR != null) - { - return latestPR.PRNo; - } + return (latestPR.PRNo + 1,latestPR.PRId + 1); else - { - return 0; // Example: Return 0 if no records found - } + return (0,0); } - catch (SqlException ex) + catch (Exception ex) { - // Handle the exception (log, rethrow, etc.) - ex.ToString(); + throw; } + } Task IItem.PostPutItemPath(ItemDto itemDto) { @@ -372,38 +300,21 @@ namespace CPRNIMS.Domain.Services.Items public async Task > GetDepartment(ItemCodeDto itemCode) { - try - { - var departments = await _dbContext.Departments - .Where(d => d.IsActive == true) - .ToListAsync(); - return departments; - } - catch (Exception ex) - { - // Log the exception or handle it as needed - throw; - } + return await _dbContext.Departments + .Where(d => d.IsActive == true) + .ToListAsync(); } public async Task> GetNotifUserKey(ItemDto itemDto) { - try - { - var allItems = await _dbContext.NotifUserKeys - .FromSqlRaw($"EXEC GetNotifUserKey @UserId,@Status,@PRDetailsId,@PRNo", - new SqlParameter("@UserId", itemDto.UserId), - new SqlParameter("@Status", itemDto.Status), - new SqlParameter("@PRDetailsId", itemDto.PRDetailsId), - new SqlParameter("@PRNo", itemDto.PRNo)) - .ToListAsync(); + var allItems = await _dbContext.NotifUserKeys + .FromSqlRaw($"EXEC GetNotifUserKey @UserId,@Status,@PRDetailsId,@PRNo", + new SqlParameter("@UserId", itemDto.UserId), + new SqlParameter("@Status", itemDto.Status), + new SqlParameter("@PRDetailsId", itemDto.PRDetailsId), + new SqlParameter("@PRNo", itemDto.PRNo)) + .ToListAsync(); - return allItems ?? new List(); - } - catch (SqlException ex) - { - ex.ToString(); - throw; - } + return allItems ?? new List(); } } } diff --git a/CPRNIMS.Domain/Services/PR/PRequest.cs b/CPRNIMS.Domain/Services/PR/PRequest.cs index 5e9b45d..aa0aee2 100644 --- a/CPRNIMS.Domain/Services/PR/PRequest.cs +++ b/CPRNIMS.Domain/Services/PR/PRequest.cs @@ -2,7 +2,6 @@ using CPRNIMS.Infrastructure.Database; using CPRNIMS.Infrastructure.Dto.PR; using CPRNIMS.Infrastructure.Entities.Common; -using CPRNIMS.Infrastructure.Entities.PO; using CPRNIMS.Infrastructure.Entities.Purchasing; using CPRNIMS.Infrastructure.Entities.SMTP; using Microsoft.Data.SqlClient; @@ -14,6 +13,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json.Linq; +using CPRNIMS.Infrastructure.Models.Common; namespace CPRNIMS.Domain.Services.PR { @@ -297,7 +297,10 @@ namespace CPRNIMS.Domain.Services.PR return allItems ?? new List(); } - + public async Task> GetProjectCodes(PRDto pRDto) + { + return await _dbContext.ProjectCodes.ToListAsync(); + } public async Task PRItemRemoval(PRDto prDto) { var (messCode, message) = CreateOutputParams(); @@ -318,6 +321,74 @@ namespace CPRNIMS.Domain.Services.PR }; return response; } + + public async Task PostPutProjectCode(PRDto prDto) + { + if (prDto == null) throw new ArgumentNullException(nameof(prDto)); + + var existing = await _dbContext.ProjectCodes + .FirstOrDefaultAsync(p => p.ProjectCodeId == prDto.ProjectCodeId); + + if (existing == null) + { + var project = new ProjectCodes + { + ProjectCode = prDto.ProjectCode, + ProjectName = prDto.ProjectName, + DeliveryAddress = prDto.DeliveryAddress, + MaxDays = prDto.MaxDays, + IsActive = prDto.IsActive + }; + await _dbContext.ProjectCodes.AddAsync(project); + } + else + { + if (await IsUsingAsync(prDto.ProjectCodeId)) + { + return new ResponseObject() + { + message = "Cannot update this project code because it is already being used in a Purchase Order (PO).", + messCode = 0, + success = false, + }; + } + else + { + existing.ProjectCode = prDto.ProjectCode; + existing.ProjectName = prDto.ProjectName; + existing.DeliveryAddress = prDto.DeliveryAddress; + existing.MaxDays = prDto.MaxDays; + existing.IsActive = prDto.IsActive; + } + } + + await _dbContext.SaveChangesAsync(); + + return new ResponseObject() + { + message = "Project code detail successfully updated", + messCode = 1, + success = true + }; + } + private async Task IsUsingAsync(int projectCodeId) + { + try + { + return await (from pr in _dbContext.PRs + join pod in _dbContext.PODetails on pr.PRNo equals pod.PRNo + where pr.ProjectCodeId == projectCodeId + && !pod.IsRemoved + && pr.IsActive + select pr).AnyAsync(); + } + catch (Exception ex) + { + ex.ToString(); + throw; + } + } + #endregion } } diff --git a/CPRNIMS.Domain/UIContracts/PR/IPRequest.cs b/CPRNIMS.Domain/UIContracts/PR/IPRequest.cs index 2d93601..eb3770c 100644 --- a/CPRNIMS.Domain/UIContracts/PR/IPRequest.cs +++ b/CPRNIMS.Domain/UIContracts/PR/IPRequest.cs @@ -1,6 +1,4 @@ -using CPRNIMS.Infrastructure.Dto.Items; -using CPRNIMS.Infrastructure.Models.Account; -using CPRNIMS.Infrastructure.ViewModel.Items; +using CPRNIMS.Infrastructure.Models.Account; using CPRNIMS.Infrastructure.ViewModel.PR; using System; using System.Collections.Generic; @@ -12,6 +10,7 @@ namespace CPRNIMS.Domain.UIContracts.PR { public interface IPRequest { + #region Get Task?> GetApproverName(User user, PRVM viewModels); Task> GetForReceiving(User user, PRVM viewModel); Task> GetItemDetailForReceiving(User user, PRVM viewModel); @@ -27,6 +26,9 @@ namespace CPRNIMS.Domain.UIContracts.PR Task?> GetDetailedPRTracking(User user, PRVM viewModel); Task?> GetSupplierAlternativeOffer(User user, PRVM viewModel); Task?> GetSupplierAlterOfferDetails(User user, PRVM viewModel); + Task?> GetProjectCodes(User user, PRVM viewModels); + #endregion + #region Post Put Task PostPRApproveReject(User user, PRVM viewModel); Task PostPutItemReceiving(User user, PRVM viewModel); Task PutItemDetail(User user, PRVM viewModel); @@ -37,5 +39,7 @@ namespace CPRNIMS.Domain.UIContracts.PR Task PutSupplierAlterOffer(User user, PRVM viewModel); Task PRItemRemoval(User user, PRVM viewModel); Task ApprovedSelectedPRItem(User user, PRVM viewModel); + Task PostPutProjectCode(User user, PRVM viewModel); + #endregion } } diff --git a/CPRNIMS.Domain/UIServices/PR/PRequest.cs b/CPRNIMS.Domain/UIServices/PR/PRequest.cs index d93e017..fb73b6f 100644 --- a/CPRNIMS.Domain/UIServices/PR/PRequest.cs +++ b/CPRNIMS.Domain/UIServices/PR/PRequest.cs @@ -116,6 +116,11 @@ namespace CPRNIMS.Domain.UIServices.PR } #endregion #region Get + public async Task?> GetProjectCodes(User user, PRVM viewModel) + { + return await SendGetApiRequest(user, viewModel, + _configuration["LLI:NonInvent:PRMgmt:GetProjectCodes"]); + } public async Task?> GetApproverName(User user, PRVM viewModel) { return await SendGetApiRequest(user, viewModel, @@ -253,6 +258,12 @@ namespace CPRNIMS.Domain.UIServices.PR return await SendPostApiRequest(user, viewModel, _configuration["LLI:NonInvent:PRMgmt:ApprovedSelectedPRItem"]); } + + public async Task PostPutProjectCode(User user, PRVM viewModel) + { + return await SendPostApiRequest(user, viewModel, + _configuration["LLI:NonInvent:PRMgmt:PostPutProjectCode"]); + } #endregion } } \ No newline at end of file diff --git a/CPRNIMS.Infrastructure/Database/NonInventoryDbContext.cs b/CPRNIMS.Infrastructure/Database/NonInventoryDbContext.cs index 963a302..e8abb1d 100644 --- a/CPRNIMS.Infrastructure/Database/NonInventoryDbContext.cs +++ b/CPRNIMS.Infrastructure/Database/NonInventoryDbContext.cs @@ -47,6 +47,8 @@ namespace CPRNIMS.Infrastructure.Database public virtual DbSet SMTPCredentials { get; set; } public virtual DbSet PRDetails { get; set; } public virtual DbSet PRItemLists { get; set; } + public DbSet PRAttachments { get; set; } + public DbSet ProjectCodes { get; set; } public virtual DbSet NotificationByIds { get; set; } public virtual DbSet AlternativeOffers { get; set; } public virtual DbSet AlternativeOfferDetails { get; set; } @@ -84,6 +86,7 @@ namespace CPRNIMS.Infrastructure.Database public virtual DbSet ApprovedPOs { get; set; } public virtual DbSet PurchaseOrders { get; set; } public virtual DbSet POs { get; set; } + public DbSet PODetails { get; set; } public virtual DbSet PRPOSummaryCounts { get; set; } public virtual DbSet PRPOSummaryItems { get; set; } public virtual DbSet POItemDetails { get; set; } @@ -117,6 +120,7 @@ namespace CPRNIMS.Infrastructure.Database #region Automation Part public virtual DbSet AllForCanvasses { get; set; } + public DbSet ErrorLogs { get; set; } #endregion protected override void OnModelCreating(ModelBuilder modelBuilder) { diff --git a/CPRNIMS.Infrastructure/Dto/Items/AttachmentRequest.cs b/CPRNIMS.Infrastructure/Dto/Items/AttachmentRequest.cs new file mode 100644 index 0000000..f6dac34 --- /dev/null +++ b/CPRNIMS.Infrastructure/Dto/Items/AttachmentRequest.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPRNIMS.Infrastructure.Dto.Items +{ + public class AttachmentRequest + { + public long PRId { get; set; } + public string? FileName { get; set; } + public string? OrigFileName { get; set; } + } +} diff --git a/CPRNIMS.Infrastructure/Dto/Items/ItemCartDto.cs b/CPRNIMS.Infrastructure/Dto/Items/ItemCartDto.cs index 8a8d6a8..9687290 100644 --- a/CPRNIMS.Infrastructure/Dto/Items/ItemCartDto.cs +++ b/CPRNIMS.Infrastructure/Dto/Items/ItemCartDto.cs @@ -8,7 +8,8 @@ namespace CPRNIMS.Infrastructure.Dto.Items { public class ItemCartDto { - public List? ItemCartIds { get; set; } - public List? Qty { get; set; } + public long ItemCartId { get; set; } + public long ItemNo { get; set; } + public decimal Qty { get; set; } } } diff --git a/CPRNIMS.Infrastructure/Dto/Items/ItemCodeDto.cs b/CPRNIMS.Infrastructure/Dto/Items/ItemCodeDto.cs index ad5f9c5..b50b47b 100644 --- a/CPRNIMS.Infrastructure/Dto/Items/ItemCodeDto.cs +++ b/CPRNIMS.Infrastructure/Dto/Items/ItemCodeDto.cs @@ -19,7 +19,8 @@ namespace CPRNIMS.Infrastructure.Dto.Items public short ItemCategoryId { get; set; } public string? ItemName { get; set; } public string? ItemDescription { get; set; } - public string? UserId { get; set; } + public string? UserId { get; set; } + public string? FileName { get; set; } public short Status { get; set; } public bool IsActive { get; set; } } diff --git a/CPRNIMS.Infrastructure/Dto/Items/ItemDto.cs b/CPRNIMS.Infrastructure/Dto/Items/ItemDto.cs index 4c4f9b2..c39c282 100644 --- a/CPRNIMS.Infrastructure/Dto/Items/ItemDto.cs +++ b/CPRNIMS.Infrastructure/Dto/Items/ItemDto.cs @@ -115,5 +115,9 @@ namespace CPRNIMS.Infrastructure.Dto.Items public DateTime DateFrom { get; set; } public DateTime DateTo { get; set; } public bool IsSorting { get; set; }=false; + public int ProjectCodeId { get; set; } + public long PRId { get; set; } + public string? FileName { get; set; } + public string? OrigFileName { get; set; } } } diff --git a/CPRNIMS.Infrastructure/Dto/PR/PRDto.cs b/CPRNIMS.Infrastructure/Dto/PR/PRDto.cs index 17d6f77..4ae6844 100644 --- a/CPRNIMS.Infrastructure/Dto/PR/PRDto.cs +++ b/CPRNIMS.Infrastructure/Dto/PR/PRDto.cs @@ -125,5 +125,14 @@ namespace CPRNIMS.Infrastructure.Dto.PR public bool IsApproved { get; set; }=false; public int AlternativeOfferId { get; set; } public byte AppsModuleId { get; set; } + public int ProjectCodeId { get; set; } + public string? ProjectCode { get; set; } + public string? ProjectName { get; set; } + public int MaxDays { get; set; } + public string? DeliveryAddress { get; set; } + public int PRAttachmentId { get; set; } + public long PRId { get; set; } + public string? FileName { get; set; } + public string? OrigFileName { get; set; } } } diff --git a/CPRNIMS.Infrastructure/Entities/Common/ErrorLog.cs b/CPRNIMS.Infrastructure/Entities/Common/ErrorLog.cs new file mode 100644 index 0000000..d8028b7 --- /dev/null +++ b/CPRNIMS.Infrastructure/Entities/Common/ErrorLog.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPRNIMS.Infrastructure.Entities.Common +{ + public class ErrorLog + { + public Guid Id { get; set; } + public string Path { get; set; } = default!; + public string HttpMethod { get; set; } = default!; + public string? QueryString { get; set; } + public int StatusCode { get; set; } + public string ExceptionType { get; set; } = default!; + public string Message { get; set; } = default!; + public string? StackTrace { get; set; } + public string? UserId { get; set; } + public string? IpAddress { get; set; } + public DateTime CreatedAt { get; set; } + } +} diff --git a/CPRNIMS.Infrastructure/Entities/PO/PODetails.cs b/CPRNIMS.Infrastructure/Entities/PO/PODetails.cs new file mode 100644 index 0000000..ab14ba9 --- /dev/null +++ b/CPRNIMS.Infrastructure/Entities/PO/PODetails.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPRNIMS.Infrastructure.Entities.PO +{ + [Table("PODetails")] + public class PODetails + { + [Key] + public long PODetailId { get; set; } + public long POId { get; set; } + public long PRNo { get; set; } + public bool IsRemoved { get; set; } + } +} diff --git a/CPRNIMS.Infrastructure/Entities/Purchasing/PR.cs b/CPRNIMS.Infrastructure/Entities/Purchasing/PR.cs index fd74cf5..c33169f 100644 --- a/CPRNIMS.Infrastructure/Entities/Purchasing/PR.cs +++ b/CPRNIMS.Infrastructure/Entities/Purchasing/PR.cs @@ -17,10 +17,9 @@ namespace CPRNIMS.Infrastructure.Entities.Purchasing public long PRId { get; set; } public long PRNo { get; set; } public string? NewPRNo { get; set; } - //public long ItemCodeId { get; set; } + public int ProjectCodeId { get; set; } public long ItemCartId { get; set; } public string? UserId { get; set; } - public int ItemCount { get; set; } public bool IsActive { get; set; } } } diff --git a/CPRNIMS.Infrastructure/Entities/Purchasing/PRAttachments.cs b/CPRNIMS.Infrastructure/Entities/Purchasing/PRAttachments.cs new file mode 100644 index 0000000..d699c62 --- /dev/null +++ b/CPRNIMS.Infrastructure/Entities/Purchasing/PRAttachments.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPRNIMS.Infrastructure.Entities.Purchasing +{ + [Table("PRAttachments")] + public class PRAttachments + { + [Key] + public int PRAttachmentId { get; set; } + public long PRId { get; set; } + public string? FileName { get; set; } + public string? OrigFileName { get; set; } + } +} diff --git a/CPRNIMS.Infrastructure/Entities/Purchasing/PRItemList.cs b/CPRNIMS.Infrastructure/Entities/Purchasing/PRItemList.cs index 7f31302..a8ff5f7 100644 --- a/CPRNIMS.Infrastructure/Entities/Purchasing/PRItemList.cs +++ b/CPRNIMS.Infrastructure/Entities/Purchasing/PRItemList.cs @@ -37,5 +37,6 @@ namespace CPRNIMS.Infrastructure.Entities.Purchasing public bool Queue { get; set; } public string? AttestedBy { get; set; } public string? ApprovedBy { get; set; } + public string? ProjectCode { get; set; } } } diff --git a/CPRNIMS.Infrastructure/Entities/Purchasing/ProjectCodes.cs b/CPRNIMS.Infrastructure/Entities/Purchasing/ProjectCodes.cs new file mode 100644 index 0000000..7f90ed0 --- /dev/null +++ b/CPRNIMS.Infrastructure/Entities/Purchasing/ProjectCodes.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPRNIMS.Infrastructure.Entities.Purchasing +{ + [Table("ProjectCodes")] + public class ProjectCodes + { + [Key] + public int ProjectCodeId { get; set; } + public string? ProjectCode { get; set; } + public string? ProjectName { get; set; } + public int MaxDays { get; set; } + public string? DeliveryAddress { get; set; } + public bool IsActive { get; set; } + } +} diff --git a/CPRNIMS.Infrastructure/ViewModel/Items/ItemVM.cs b/CPRNIMS.Infrastructure/ViewModel/Items/ItemVM.cs index 9d0d6c9..cc9149b 100644 --- a/CPRNIMS.Infrastructure/ViewModel/Items/ItemVM.cs +++ b/CPRNIMS.Infrastructure/ViewModel/Items/ItemVM.cs @@ -44,6 +44,7 @@ namespace CPRNIMS.Infrastructure.ViewModel.Items public string? ItemAttachPath { get; set; } public int CartItemCount { get; set; } public int ItemCartId { get; set; } + public string? ItemCartIds { get; set; } public long ItemAttachId { get; set; } public decimal QtyRequest { get; set; } public byte IsApproved { get; set; } @@ -54,5 +55,10 @@ namespace CPRNIMS.Infrastructure.ViewModel.Items public bool IsMDLD { get; set; } = false; public bool CheckBox { get; set; } public bool IsCount { get; set; } + public int ProjectCodeId { get; set; } + public string? FileName { get; set; } + public long PRId { get; set; } + public string? ProjectCode { get; set; } + public string? OrigFileName { get; set; } } } diff --git a/CPRNIMS.Infrastructure/ViewModel/PR/PRVM.cs b/CPRNIMS.Infrastructure/ViewModel/PR/PRVM.cs index e1f6db6..ec25e5e 100644 --- a/CPRNIMS.Infrastructure/ViewModel/PR/PRVM.cs +++ b/CPRNIMS.Infrastructure/ViewModel/PR/PRVM.cs @@ -123,6 +123,15 @@ namespace CPRNIMS.Infrastructure.ViewModel.PR public string? Description { get; set; } public int AlternativeOfferId { get; set; } public string? AggreDescription { get; set; } + public string? ProjectCode { get; set; } + public int ProjectCodeId { get; set; } + public string? ProjectName { get; set; } + public int MaxDays { get; set; } + public string? DeliveryAddress { get; set; } + public int PRAttachmentId { get; set; } + public long PRId { get; set; } + public string? FileName { get; set; } + public string? OrigFileName { get; set; } public ItemReceivingList? ItemList { get; set; } public PRList? PRList { get; set; } } diff --git a/CPRNIMS.WebApi/CPRNIMS.WebApi.csproj b/CPRNIMS.WebApi/CPRNIMS.WebApi.csproj index 743e8ce..6ceb148 100644 --- a/CPRNIMS.WebApi/CPRNIMS.WebApi.csproj +++ b/CPRNIMS.WebApi/CPRNIMS.WebApi.csproj @@ -146,4 +146,9 @@ + + + + + diff --git a/CPRNIMS.WebApi/Controllers/Account/AccountController.cs b/CPRNIMS.WebApi/Controllers/Account/AccountController.cs index 338a743..4620ddd 100644 --- a/CPRNIMS.WebApi/Controllers/Account/AccountController.cs +++ b/CPRNIMS.WebApi/Controllers/Account/AccountController.cs @@ -31,13 +31,13 @@ namespace CPRNIMS.WebApi.Controllers.Account private readonly UserClaimsManager _userClaimsManager; private readonly RoleManager _roleManager; public AccountController(ErrorMessageService errorMessageService, - IWebHostEnvironment webHostEnvironment, SMTPHelper sMTPHelper, + IWebHostEnvironment webHostEnvironment, IConfiguration configuration, IAttachment attachment, IAccount account, IDepartment department, IControllerAccess controllerAccess, UserManager userManager, SignInManager signInManager, UserClaimsManager userClaimsManager, RoleManager roleManager ) : - base(errorMessageService, webHostEnvironment, sMTPHelper, configuration) + base(errorMessageService, webHostEnvironment, configuration) { _errorMessageService = errorMessageService; _attachment = attachment; diff --git a/CPRNIMS.WebApi/Controllers/Account/AnonController.cs b/CPRNIMS.WebApi/Controllers/Account/AnonController.cs index 55ee30e..3efef2e 100644 --- a/CPRNIMS.WebApi/Controllers/Account/AnonController.cs +++ b/CPRNIMS.WebApi/Controllers/Account/AnonController.cs @@ -28,7 +28,7 @@ namespace CPRNIMS.WebApi.Controllers.Account SignInManager signInManager, UserManager userManager ) - : base(errorMessageService, webHostEnvironment, sMTPHelper, configuration) + : base(errorMessageService, webHostEnvironment, configuration) { _config = configuration; _smtpHelper = sMTPHelper; diff --git a/CPRNIMS.WebApi/Controllers/Base/BaseController.cs b/CPRNIMS.WebApi/Controllers/Base/BaseController.cs index 7f8143e..8557666 100644 --- a/CPRNIMS.WebApi/Controllers/Base/BaseController.cs +++ b/CPRNIMS.WebApi/Controllers/Base/BaseController.cs @@ -13,11 +13,11 @@ namespace CPRNIMS.WebApi.Controllers.Base [ApiController] public class BaseController : ControllerBase { - private readonly IWebHostEnvironment _webHostEnvironment; + public readonly IWebHostEnvironment _webHostEnvironment; public readonly ErrorMessageService ErrorMessageService; public IConfiguration _configuration; public BaseController(ErrorMessageService errorMessageService, - IWebHostEnvironment webHostEnvironment, Infrastructure.Helper.SMTPHelper sMTPHelper, IConfiguration configuration) + IWebHostEnvironment webHostEnvironment, IConfiguration configuration) { ErrorMessageService = errorMessageService; _webHostEnvironment = webHostEnvironment; diff --git a/CPRNIMS.WebApi/Controllers/Canvass/CanvassMgmtController.cs b/CPRNIMS.WebApi/Controllers/Canvass/CanvassMgmtController.cs index af4e1ca..b735893 100644 --- a/CPRNIMS.WebApi/Controllers/Canvass/CanvassMgmtController.cs +++ b/CPRNIMS.WebApi/Controllers/Canvass/CanvassMgmtController.cs @@ -21,7 +21,7 @@ namespace CPRNIMS.WebApi.Controllers.Canvass public CanvassMgmtController(ErrorMessageService errorMessageService, IWebHostEnvironment webHostEnvironment, SMTPHelper sMTPHelper, IConfiguration configuration, ICanvass canvass) : - base(errorMessageService, webHostEnvironment, sMTPHelper, configuration) + base(errorMessageService, webHostEnvironment, configuration) { _canvass = canvass; _config = configuration; diff --git a/CPRNIMS.WebApi/Controllers/Finance/RRMgmtController.cs b/CPRNIMS.WebApi/Controllers/Finance/RRMgmtController.cs index 86d7ded..684ad8f 100644 --- a/CPRNIMS.WebApi/Controllers/Finance/RRMgmtController.cs +++ b/CPRNIMS.WebApi/Controllers/Finance/RRMgmtController.cs @@ -17,9 +17,9 @@ namespace CPRNIMS.WebApi.Controllers.Finance private readonly IRR _rr; public RRMgmtController(ErrorMessageService errorMessageService, - IWebHostEnvironment webHostEnvironment, SMTPHelper sMTPHelper, - IConfiguration configuration, SMTPHelper smptHelper, IRR rr) : - base(errorMessageService, webHostEnvironment, sMTPHelper, configuration) + IWebHostEnvironment webHostEnvironment, + IConfiguration configuration, IRR rr) : + base(errorMessageService, webHostEnvironment, configuration) { _rr = rr; } diff --git a/CPRNIMS.WebApi/Controllers/Inventory/InventoryMgmtController.cs b/CPRNIMS.WebApi/Controllers/Inventory/InventoryMgmtController.cs index f126061..5da8267 100644 --- a/CPRNIMS.WebApi/Controllers/Inventory/InventoryMgmtController.cs +++ b/CPRNIMS.WebApi/Controllers/Inventory/InventoryMgmtController.cs @@ -17,9 +17,9 @@ namespace CPRNIMS.WebApi.Controllers.Inventory private readonly IInventory _inventory; public InventoryMgmtController(ErrorMessageService errorMessageService, - IWebHostEnvironment webHostEnvironment, SMTPHelper sMTPHelper, IConfiguration configuration, + IWebHostEnvironment webHostEnvironment, IConfiguration configuration, IInventory inventory) : - base(errorMessageService, webHostEnvironment, sMTPHelper, configuration) + base(errorMessageService, webHostEnvironment,configuration) { _inventory = inventory; } diff --git a/CPRNIMS.WebApi/Controllers/Items/ItemMgmtController.cs b/CPRNIMS.WebApi/Controllers/Items/ItemMgmtController.cs index e2c7e64..8c26308 100644 --- a/CPRNIMS.WebApi/Controllers/Items/ItemMgmtController.cs +++ b/CPRNIMS.WebApi/Controllers/Items/ItemMgmtController.cs @@ -2,6 +2,7 @@ using CPRNIMS.Domain.Contracts.SMTP; using CPRNIMS.Domain.Services; using CPRNIMS.Infrastructure.Dto.Items; +using CPRNIMS.Infrastructure.Entities.Items; using CPRNIMS.Infrastructure.Helper; using CPRNIMS.Infrastructure.Models.Common; using CPRNIMS.Infrastructure.ViewModel.Common; @@ -17,13 +18,15 @@ namespace CPRNIMS.WebApi.Controllers.Items { private readonly IItem _item; private readonly IConfiguration _config; + private readonly SMTPHelper _smtpHelper; public ItemMgmtController(ErrorMessageService errorMessageService, - IWebHostEnvironment webHostEnvironment, SMTPHelper sMTPHelper, + IWebHostEnvironment webHostEnvironment, SMTPHelper smtpHelper, IConfiguration configuration, IItem item) : - base(errorMessageService, webHostEnvironment, sMTPHelper, configuration) + base(errorMessageService, webHostEnvironment, configuration) { _config = configuration; _item= item; + _smtpHelper = smtpHelper; } [HttpPost("PostPutItemPath")] @@ -54,76 +57,91 @@ namespace CPRNIMS.WebApi.Controllers.Items [HttpPost("PostPurchRequest")] public async Task PostPurchRequest([FromBody] ItemVM viewModel) { - try + 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) { - // 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) + return BadRequest("ItemCartId, Qty, and ItemNo length mismatch."); + } + + var result = await ProcessPurchaseRequest(viewModel); + + return Ok(new { success = result }); + } + private async Task 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 { - 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"], + 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 }; - // await _smptHelper.SendEmailAsync(messageDetails); - return Ok(new { success = true }); + + var response = await _item.PostPurchRequest(dto); + if (response.messCode == 1) + IsSuccess = true; } - catch (Exception ex) + + if (IsSuccess) { - var message = ex.InnerException?.ToString() ?? ex.Message; - await PostErrorMessage(message, "WebApi"); - return StatusCode(500, new { success = false, message }); - } + 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")] diff --git a/CPRNIMS.WebApi/Controllers/PO/POMgmtController.cs b/CPRNIMS.WebApi/Controllers/PO/POMgmtController.cs index d4e0412..32196fd 100644 --- a/CPRNIMS.WebApi/Controllers/PO/POMgmtController.cs +++ b/CPRNIMS.WebApi/Controllers/PO/POMgmtController.cs @@ -24,7 +24,7 @@ namespace CPRNIMS.WebApi.Controllers.PO public POMgmtController(ErrorMessageService errorMessageService, IWebHostEnvironment webHostEnvironment, SMTPHelper sMTPHelper, IConfiguration configuration, ISMTP sMTP, IPurchaseOrder purchaseOrder) : - base(errorMessageService, webHostEnvironment, sMTPHelper, configuration) + base(errorMessageService, webHostEnvironment, configuration) { _smtpHelper = sMTPHelper; _sMTP= sMTP; diff --git a/CPRNIMS.WebApi/Controllers/PR/PRMgmtController.cs b/CPRNIMS.WebApi/Controllers/PR/PRMgmtController.cs index 7b7f687..746a45b 100644 --- a/CPRNIMS.WebApi/Controllers/PR/PRMgmtController.cs +++ b/CPRNIMS.WebApi/Controllers/PR/PRMgmtController.cs @@ -1,10 +1,16 @@ using CPRNIMS.Domain.Contracts.PR; using CPRNIMS.Domain.Services; +using CPRNIMS.Infrastructure.Dto.PO; using CPRNIMS.Infrastructure.Dto.PR; +using CPRNIMS.Infrastructure.Entities.PO; +using CPRNIMS.Infrastructure.Entities.Purchasing; using CPRNIMS.Infrastructure.Helper; +using CPRNIMS.Infrastructure.Models.Common; using CPRNIMS.Infrastructure.ViewModel.Common; using CPRNIMS.Infrastructure.ViewModel.PR; using CPRNIMS.WebApi.Controllers.Base; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; using System.Text; @@ -21,7 +27,7 @@ namespace CPRNIMS.WebApi.Controllers.PR public PRMgmtController(ErrorMessageService errorMessageService, IWebHostEnvironment webHostEnvironment, SMTPHelper sMTPHelper, IConfiguration configuration, IPRequest pRequest) : - base(errorMessageService, webHostEnvironment, sMTPHelper, configuration) + base(errorMessageService, webHostEnvironment, configuration) { _config = configuration; _smptHelper = sMTPHelper; @@ -261,8 +267,23 @@ namespace CPRNIMS.WebApi.Controllers.PR return Ok(new { success = false, messCode = 0, message = errorMessage }); } } + [HttpPost("PostPutProjectCode")] + public async Task PostPutProjectCode([FromBody] PRDto prDto) + { + var results = await _pRequest.PostPutProjectCode(prDto); + + return Ok( new { data = results, message =results.message, messCode=results.messCode }); + } #endregion #region Get + [HttpPost("GetProjectCodes")] + public async Task GetProjectCodes(PRDto PRDto) + { + return await ExecuteWithErrorHandling( + () => _pRequest.GetProjectCodes(PRDto), + nameof(GetProjectCodes), false + ); + } [HttpPost("GetApproverName")] public async Task GetApproverName(PRDto PRDto) { diff --git a/CPRNIMS.WebApi/Controllers/Receiving/ReceivingController.cs b/CPRNIMS.WebApi/Controllers/Receiving/ReceivingController.cs index 11f8a9c..e104547 100644 --- a/CPRNIMS.WebApi/Controllers/Receiving/ReceivingController.cs +++ b/CPRNIMS.WebApi/Controllers/Receiving/ReceivingController.cs @@ -6,6 +6,7 @@ using CPRNIMS.Infrastructure.Helper; using CPRNIMS.Infrastructure.ViewModel.Receiving; using CPRNIMS.WebApi.Controllers.Base; using Microsoft.AspNetCore.Mvc; +using System.Runtime.InteropServices; using System.Text; namespace CPRNIMS.WebApi.Controllers.Receiving @@ -17,9 +18,9 @@ namespace CPRNIMS.WebApi.Controllers.Receiving private readonly IItem _item; public ReceivingController(ErrorMessageService errorMessageService, - IWebHostEnvironment webHostEnvironment, SMTPHelper sMTPHelper, + IWebHostEnvironment webHostEnvironment, IConfiguration configuration, IReceiving receiving, IItem item) : - base(errorMessageService, webHostEnvironment, sMTPHelper, configuration) + base(errorMessageService, webHostEnvironment, configuration) { _receiving = receiving; _item= item; @@ -68,7 +69,7 @@ namespace CPRNIMS.WebApi.Controllers.Receiving { try { - long prNo = await _item.GetPRNo(); + var (prNo,prId) = await _item.GetPRNo(); foreach (var items in viewModel.ItemList.PRDetailsId) { var index = viewModel.ItemList.PRDetailsId.IndexOf(items); diff --git a/CPRNIMS.WebApi/Controllers/SMTP/SMTPMgmtController.cs b/CPRNIMS.WebApi/Controllers/SMTP/SMTPMgmtController.cs index f4a1532..cff7023 100644 --- a/CPRNIMS.WebApi/Controllers/SMTP/SMTPMgmtController.cs +++ b/CPRNIMS.WebApi/Controllers/SMTP/SMTPMgmtController.cs @@ -14,9 +14,9 @@ namespace CPRNIMS.WebApi.Controllers.SMTP { private readonly ISMTP _sMTP; public SMTPMgmtController(ErrorMessageService errorMessageService, - IWebHostEnvironment webHostEnvironment, SMTPHelper sMTPHelper, + IWebHostEnvironment webHostEnvironment, IConfiguration configuration, ISMTP sMTP) : - base(errorMessageService, webHostEnvironment, sMTPHelper, configuration) + base(errorMessageService, webHostEnvironment, configuration) { _sMTP = sMTP; } diff --git a/CPRNIMS.WebApi/Exceptions/AppException.cs b/CPRNIMS.WebApi/Exceptions/AppException.cs new file mode 100644 index 0000000..28ce033 --- /dev/null +++ b/CPRNIMS.WebApi/Exceptions/AppException.cs @@ -0,0 +1,13 @@ +namespace CPRNIMS.WebApi.Exceptions +{ + public class AppException : Exception + { + public int StatusCode { get; } + + public AppException(string message, int statusCode = StatusCodes.Status400BadRequest) + : base(message) + { + StatusCode = statusCode; + } + } +} diff --git a/CPRNIMS.WebApi/Middleware/GlobalExceptionMiddleware.cs b/CPRNIMS.WebApi/Middleware/GlobalExceptionMiddleware.cs new file mode 100644 index 0000000..ebfcf86 --- /dev/null +++ b/CPRNIMS.WebApi/Middleware/GlobalExceptionMiddleware.cs @@ -0,0 +1,89 @@ +using CPRNIMS.Infrastructure.Database; +using CPRNIMS.Infrastructure.Entities.Common; +using CPRNIMS.WebApi.Exceptions; +using System.Security.Claims; +using System.Text.Json; + +namespace CPRNIMS.Middleware +{ + public sealed class GlobalExceptionMiddleware + { + private readonly RequestDelegate _next; + private readonly ILogger _logger; + private readonly IWebHostEnvironment _env; + private readonly IServiceScopeFactory _scopeFactory; + public GlobalExceptionMiddleware( + RequestDelegate next, + ILogger logger, + IWebHostEnvironment env, + IServiceScopeFactory scopeFactory) + { + _next = next; + _logger = logger; + _env = env; + _scopeFactory = scopeFactory; + } + + public async Task InvokeAsync(HttpContext context) + { + try + { + await _next(context); + } + catch (Exception ex) + { + _logger.LogError(ex, "Unhandled exception"); + + await LogToDatabaseAsync(context, ex); + await HandleResponseAsync(context, ex); + } + } + + private async Task LogToDatabaseAsync(HttpContext context, Exception ex) + { + using var scope = _scopeFactory.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + + var errorLog = new ErrorLog + { + Id = Guid.NewGuid(), + Path = context.Request.Path, + HttpMethod = context.Request.Method, + QueryString = context.Request.QueryString.Value, + StatusCode = context.Response.StatusCode, + ExceptionType = ex.GetType().FullName!, + Message = ex.Message, + StackTrace = ex.StackTrace, + UserId = context.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value, + IpAddress = context.Connection.RemoteIpAddress?.ToString(), + CreatedAt = DateTime.UtcNow + }; + + db.ErrorLogs.Add(errorLog); + await db.SaveChangesAsync(); + } + + private async Task HandleResponseAsync(HttpContext context, Exception ex) + { + context.Response.ContentType = "application/json"; + + context.Response.StatusCode = ex switch + { + AppException app => app.StatusCode, + ArgumentException => StatusCodes.Status400BadRequest, + UnauthorizedAccessException => StatusCodes.Status401Unauthorized, + _ => StatusCodes.Status500InternalServerError + }; + + var response = new + { + success = false, + message = _env.IsDevelopment() + ? ex.Message + : "An unexpected error occurred" + }; + + await context.Response.WriteAsync(JsonSerializer.Serialize(response)); + } + } +} diff --git a/CPRNIMS.WebApi/Program.cs b/CPRNIMS.WebApi/Program.cs index 1059e09..656b101 100644 --- a/CPRNIMS.WebApi/Program.cs +++ b/CPRNIMS.WebApi/Program.cs @@ -1,3 +1,4 @@ +using CPRNIMS.Middleware; using CPRNIMS.WebApi.Common; var builder = WebApplication.CreateBuilder(args); @@ -15,6 +16,7 @@ if (app.Environment.IsDevelopment()) c.SwaggerEndpoint("./v2/swagger.json", "LLOYD API V2"); //originally "./swagger/v1/swagger.json" }); } +app.UseMiddleware(); app.UseSwagger(); app.UseSwaggerUI(c => { diff --git a/CPRNIMS.WebApi/Sql/Table.sql b/CPRNIMS.WebApi/Sql/Table.sql index a977b73..3784f49 100644 --- a/CPRNIMS.WebApi/Sql/Table.sql +++ b/CPRNIMS.WebApi/Sql/Table.sql @@ -1,4 +1,22 @@ -CREATE TABLE [dbo].[ControllerAccessDesc]( +CREATE TABLE ProjectCodes( +ProjectCodeId INT PRIMARY KEY IDENTITY(1,1), +ProjectCode VARCHAR(50) NOT NULL, +ProjectName VARCHAR(200) NOT NULL, +MaxDays INT NOT NULL, +DeliveryAddress VARCHAR(300) NOT NULL, +IsActive BIT DEFAULT(1) +) +CREATE TABLE PRAttachments( +PRAttachmentId INT PRIMARY KEY IDENTITY(1,1), +PRId BIGINT, +OrigFileName VARCHAR(450), +FileName NVARCHAR(450), +FOREIGN KEY (PRId) REFERENCES PR (PRId) +); +ALTER TABLE PR +ADD ProjectCodeId INT DEFAULT 0; + +CREATE TABLE [dbo].[ControllerAccessDesc]( [ContAccId] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL, [Action] [varchar](150) NOT NULL, [Controller] [varchar](150) NOT NULL, diff --git a/CPRNIMS.WebApps/CPRNIMS.WebApps.csproj b/CPRNIMS.WebApps/CPRNIMS.WebApps.csproj index c843191..83cde2e 100644 --- a/CPRNIMS.WebApps/CPRNIMS.WebApps.csproj +++ b/CPRNIMS.WebApps/CPRNIMS.WebApps.csproj @@ -41,7 +41,7 @@ Always - + Always @@ -60,6 +60,7 @@ + diff --git a/CPRNIMS.WebApps/Controllers/Account/AccountController.cs b/CPRNIMS.WebApps/Controllers/Account/AccountController.cs index a8f2323..08c6456 100644 --- a/CPRNIMS.WebApps/Controllers/Account/AccountController.cs +++ b/CPRNIMS.WebApps/Controllers/Account/AccountController.cs @@ -13,7 +13,7 @@ namespace CPRNIMS.WebApps.Controllers.Account List? response; public AccountController(IWebHostEnvironment webHostEnvironment, IAccount account,ErrorLogHelper errorMessageService,TokenHelper tokenHelper - ) : base(errorMessageService, webHostEnvironment,tokenHelper) + ) : base(errorMessageService, webHostEnvironment,tokenHelper, account) { _account = account; } diff --git a/CPRNIMS.WebApps/Controllers/Base/BaseMethod.cs b/CPRNIMS.WebApps/Controllers/Base/BaseMethod.cs index 9fac11b..6d8eef9 100644 --- a/CPRNIMS.WebApps/Controllers/Base/BaseMethod.cs +++ b/CPRNIMS.WebApps/Controllers/Base/BaseMethod.cs @@ -1,4 +1,5 @@ using CPRNIMS.Core.Facades; +using CPRNIMS.Domain.UIContracts.Account; using CPRNIMS.Infrastructure.Constant; using CPRNIMS.Infrastructure.Helper; using CPRNIMS.Infrastructure.ViewModel; @@ -12,17 +13,19 @@ namespace CPRNIMS.WebApps.Controllers.Base public abstract class BaseMethod : BaseProperties { protected readonly ErrorLogHelper ErrorMessageService; - protected readonly IWebHostEnvironment WebHostEnvironment; + public readonly IWebHostEnvironment _webHostEnvironment; protected readonly Infrastructure.Helper.TokenHelper TokenHelper; - + private readonly IAccount _account; protected BaseMethod( ErrorLogHelper errorMessageService, IWebHostEnvironment webHostEnvironment, - Infrastructure.Helper.TokenHelper tokenHelper) + Infrastructure.Helper.TokenHelper tokenHelper, + IAccount account) { ErrorMessageService = errorMessageService; - WebHostEnvironment = webHostEnvironment; + _webHostEnvironment = webHostEnvironment; TokenHelper = tokenHelper; + _account = account; } protected Infrastructure.Models.Account.User GetUser() @@ -74,9 +77,34 @@ namespace CPRNIMS.WebApps.Controllers.Base return RedirectToAction("Index", "Home"); PopulateViewBagFromClaims(); + + if(!await PopulateSidebarAsync()) + { + return RedirectToAction("Logout", "Home"); + } return View(); } + private async Task PopulateSidebarAsync() + { + try + { + if (!string.IsNullOrEmpty(ViewBag.UserRoles)) + { + var myControllerAccess = await _account.GetLandingPageByUserId(GetUser()); + + // Group by menu name + ViewBag.TempDataElements = myControllerAccess + .GroupBy(e => e.ElementMenuName) + .ToList(); + } + return true; + } + catch (Exception) + { + return false; + } + } protected void PopulateViewBagFromClaims() { if (!User.Identity?.IsAuthenticated ?? true) @@ -90,7 +118,6 @@ namespace CPRNIMS.WebApps.Controllers.Base User.FindAll(ClaimTypes.Role).Select(c => c.Value)); ViewBag.URLAttachment = User.FindFirst("URLAttachment")?.Value ?? "Content/Images/UserProfile/404userImage.jpg"; } - protected IActionResult GetResponse(T response) { return Json(new @@ -142,12 +169,12 @@ namespace CPRNIMS.WebApps.Controllers.Base var fileName = $"{Guid.NewGuid()}.{imageFormat.Name.ToLower()}"; var filePath = Path.Combine( - WebHostEnvironment.WebRootPath, + _webHostEnvironment.WebRootPath, FileExtensionPath.GetExtensionPath(imageFormat.Name.ToLower()), fileName); var relativePath = - Path.GetRelativePath(WebHostEnvironment.WebRootPath, filePath); + Path.GetRelativePath(_webHostEnvironment.WebRootPath, filePath); return facadeAttachment.SaveAttachment( contentBytes, diff --git a/CPRNIMS.WebApps/Controllers/Canvass/CanvassMgmtController.cs b/CPRNIMS.WebApps/Controllers/Canvass/CanvassMgmtController.cs index 277ffd0..380be02 100644 --- a/CPRNIMS.WebApps/Controllers/Canvass/CanvassMgmtController.cs +++ b/CPRNIMS.WebApps/Controllers/Canvass/CanvassMgmtController.cs @@ -1,4 +1,5 @@ -using CPRNIMS.Domain.UIContracts.Canvass; +using CPRNIMS.Domain.UIContracts.Account; +using CPRNIMS.Domain.UIContracts.Canvass; using CPRNIMS.Infrastructure.Helper; using CPRNIMS.Infrastructure.ViewModel.Canvass; using CPRNIMS.WebApps.Controllers.Base; @@ -12,9 +13,9 @@ namespace CPRNIMS.WebApps.Controllers.Canvass private readonly ICanvass _canvass; public CanvassMgmtController(TokenHelper tokenHelper, ErrorLogHelper errorMessageService, IWebHostEnvironment webHostEnvironment - , ICanvass canvass + , ICanvass canvass,IAccount account ) - : base(errorMessageService, webHostEnvironment, tokenHelper) + : base(errorMessageService, webHostEnvironment, tokenHelper, account) { _canvass = canvass; } diff --git a/CPRNIMS.WebApps/Controllers/Finance/RRMgmtController.cs b/CPRNIMS.WebApps/Controllers/Finance/RRMgmtController.cs index 66bf7fe..0e89605 100644 --- a/CPRNIMS.WebApps/Controllers/Finance/RRMgmtController.cs +++ b/CPRNIMS.WebApps/Controllers/Finance/RRMgmtController.cs @@ -1,4 +1,5 @@ -using CPRNIMS.Domain.UIContracts.Finance; +using CPRNIMS.Domain.UIContracts.Account; +using CPRNIMS.Domain.UIContracts.Finance; using CPRNIMS.Infrastructure.Helper; using CPRNIMS.Infrastructure.ViewModel.Finance; using CPRNIMS.Infrastructure.ViewModel.PR; @@ -13,8 +14,8 @@ namespace CPRNIMS.WebApps.Controllers.Finance private readonly IRR _rr; public RRMgmtController(ErrorLogHelper errorMessageService, IWebHostEnvironment webHostEnvironment, TokenHelper tokenHelper - , IRR pRequest) - : base(errorMessageService, webHostEnvironment, tokenHelper) + , IRR pRequest,IAccount account) + : base(errorMessageService, webHostEnvironment, tokenHelper, account) { _rr = pRequest; } @@ -82,8 +83,7 @@ namespace CPRNIMS.WebApps.Controllers.Finance #region Views public async Task Index() { - await IsAuthenTicated(); - return View(); + return await IsAuthenTicated(); } #endregion } diff --git a/CPRNIMS.WebApps/Controllers/HomeController.cs b/CPRNIMS.WebApps/Controllers/HomeController.cs index 8199c86..554bd96 100644 --- a/CPRNIMS.WebApps/Controllers/HomeController.cs +++ b/CPRNIMS.WebApps/Controllers/HomeController.cs @@ -29,7 +29,7 @@ namespace CPRNIMS.WebApps.Controllers IWebHostEnvironment webHostEnvironment, IAttachment attachment, IAccount account, ICaptchaService captchaService) : - base(errorMessageService, webHostEnvironment,tokenHelper) + base(errorMessageService, webHostEnvironment,tokenHelper,account) { _account = account; _attachment = attachment; diff --git a/CPRNIMS.WebApps/Controllers/Inventory/InventoryMgmtController.cs b/CPRNIMS.WebApps/Controllers/Inventory/InventoryMgmtController.cs index 1ca392c..82c53a8 100644 --- a/CPRNIMS.WebApps/Controllers/Inventory/InventoryMgmtController.cs +++ b/CPRNIMS.WebApps/Controllers/Inventory/InventoryMgmtController.cs @@ -1,4 +1,5 @@ -using CPRNIMS.Domain.UIContracts.Inventory; +using CPRNIMS.Domain.UIContracts.Account; +using CPRNIMS.Domain.UIContracts.Inventory; using CPRNIMS.Infrastructure.Helper; using CPRNIMS.Infrastructure.ViewModel.Account; using CPRNIMS.Infrastructure.ViewModel.Finance; @@ -16,8 +17,8 @@ namespace CPRNIMS.WebApps.Controllers.Inventory private readonly IInventory _inventory; public InventoryMgmtController(ErrorLogHelper errorMessageService, IWebHostEnvironment webHostEnvironment, TokenHelper tokenHelper - , IInventory inventory) - : base(errorMessageService, webHostEnvironment, tokenHelper) + , IInventory inventory,IAccount account) + : base(errorMessageService, webHostEnvironment, tokenHelper, account) { _inventory = inventory; } @@ -180,18 +181,15 @@ namespace CPRNIMS.WebApps.Controllers.Inventory #region Views public async Task Inventory() { - await IsAuthenTicated(); - return View(); + return await IsAuthenTicated(); } public async Task Lot() { - await IsAuthenTicated(); - return View(); + return await IsAuthenTicated(); } public async Task RequestItem() { - await IsAuthenTicated(); - return View(); + return await IsAuthenTicated(); } #endregion } diff --git a/CPRNIMS.WebApps/Controllers/Items/ItemMgmtController.cs b/CPRNIMS.WebApps/Controllers/Items/ItemMgmtController.cs index faba42e..fcecf0b 100644 --- a/CPRNIMS.WebApps/Controllers/Items/ItemMgmtController.cs +++ b/CPRNIMS.WebApps/Controllers/Items/ItemMgmtController.cs @@ -1,21 +1,16 @@ using Azure; using CPRNIMS.Domain.Services; +using CPRNIMS.Domain.UIContracts.Account; using CPRNIMS.Domain.UIContracts.Items; using CPRNIMS.Domain.UIServices.Updater; using CPRNIMS.Infrastructure.Dto.Items; -using CPRNIMS.Infrastructure.Entities.Items; using CPRNIMS.Infrastructure.Helper; -using CPRNIMS.Infrastructure.Models.Account; -using CPRNIMS.Infrastructure.ViewModel.Account; using CPRNIMS.Infrastructure.ViewModel.Items; using CPRNIMS.WebApps.Controllers.Base; -using Google.Apis.Auth.OAuth2; -using Google.Apis.Drive.v3; -using Google.Apis.Drive.v3.Data; -using Google.Apis.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; -using System.Collections.Generic; +using System.Text.Json; + namespace CPRNIMS.WebApps.Controllers.Items { @@ -28,8 +23,8 @@ namespace CPRNIMS.WebApps.Controllers.Items private readonly IHubContext _hubContext; public ItemMgmtController(ErrorLogHelper errorMessageService, IWebHostEnvironment webHostEnvironment, IConfiguration config, TokenHelper tokenHelper, - IItem item, IHubContext hubContext) - : base(errorMessageService, webHostEnvironment,tokenHelper) + IItem item, IHubContext hubContext,IAccount account) + : base(errorMessageService, webHostEnvironment,tokenHelper, account) { _item = item; _config = config; @@ -134,32 +129,127 @@ namespace CPRNIMS.WebApps.Controllers.Items throw; } } + private async Task SaveAttachmentAsync(IFormFile? file, string? oldFileName) + { + var uploadsPath = Path.Combine( + _webHostEnvironment.WebRootPath, + "Content", "Uploads", "PRAttachment"); + + Directory.CreateDirectory(uploadsPath); + + // If no new file uploaded, return old filename + if (file == null) + return oldFileName; + + // Delete old file if exists + if (!string.IsNullOrWhiteSpace(oldFileName)) + { + await DeleteAttachmentAsync(oldFileName); + } + + // Validate file extension + var allowedExtensions = new[] { ".csv", ".xlsx", ".xls", ".pdf" }; + var fileExtension = Path.GetExtension(file.FileName).ToLowerInvariant(); + + if (!allowedExtensions.Contains(fileExtension)) + { + throw new InvalidOperationException("Invalid file type. Only CSV, Excel, and PDF files are allowed."); + } + + // Validate file size (5MB max) + if (file.Length > 5 * 1024 * 1024) + { + throw new InvalidOperationException("File size exceeds 5MB limit."); + } + + // Generate new unique filename with original extension + var newFileName = $"{Guid.NewGuid()}{fileExtension}"; + var newFilePath = Path.Combine(uploadsPath, newFileName); + + // Save new file + await using var stream = new FileStream(newFilePath, FileMode.Create); + await file.CopyToAsync(stream); + + // Return only filename (NOT full path) + return newFileName; + } + + private async Task DeleteAttachmentAsync(string fileName) + { + if (string.IsNullOrWhiteSpace(fileName)) + return; + + var uploadsPath = Path.Combine( + _webHostEnvironment.WebRootPath, + "Content", "Uploads", "PRAttachment"); + + var filePath = Path.Combine(uploadsPath, fileName); + + if (System.IO.File.Exists(filePath)) + { + await Task.Run(() => System.IO.File.Delete(filePath)); + } + } [HttpPost] - public async Task PostPurchRequest(ItemVM viewModel,List ItemCartIds) + public async Task PostPurchRequest([FromForm] string ItemCartIds, + [FromForm] DateTime DateNeeded, + [FromForm] byte RequestTypeId, + [FromForm] int ChargeTo, + [FromForm] string? Remarks, + [FromForm] string? ProjectCode, + IFormFile? file) { try { - viewModel.ItemCartVM = new ItemCartVM - { - ItemCartId = ItemCartIds.SelectMany(ic => ic.ItemCartId).ToList(), - Qty = ItemCartIds.SelectMany(ic => ic.Qty).ToList(), - ItemNo = ItemCartIds.SelectMany(ic => ic.ItemNo).ToList() - }; - var postPutItem = await _item.PostPurchRequest(GetUser(), viewModel); + // Deserialize the ItemCartIds JSON string + var itemCartList = JsonSerializer.Deserialize>(ItemCartIds); - if (postPutItem.statusResponse != "Error") + if (itemCartList == null || !itemCartList.Any()) { - return Json(new { success = true }); + return Json(new { success = false, response = "No items selected" }); } - return Json(new { success = false, Response = postPutItem.message }); + var viewModel = new ItemVM + { + DateNeeded = DateNeeded, + RequestTypeId = RequestTypeId, + ChargeTo = ChargeTo, + Remarks = Remarks, + ProjectCode = ProjectCode, + ItemCartVM = new ItemCartVM + { + ItemCartId = itemCartList.Select(ic => ic.ItemCartId).ToList(), + Qty = itemCartList.Select(ic => ic.Qty).ToList(), + ItemNo = itemCartList.Select(ic => ic.ItemNo).ToList() + } + }; + + // Save attachment if provided + string? savedFileName = await SaveAttachmentAsync(file, null); + + viewModel.FileName = savedFileName; + viewModel.OrigFileName = file.FileName; + + var postPutItem = await _item.PostPurchRequest(GetUser(), viewModel); + + if (postPutItem.messCode != 0) + { + return Json(new { success = true, message = "Purchase request created successfully" }); + } + + // Delete uploaded file if request failed + if (!string.IsNullOrWhiteSpace(savedFileName)) + { + await DeleteAttachmentAsync(savedFileName); + } + + return Json(new { success = false, response = postPutItem.message }); } catch (Exception ex) { - var message = ex.InnerException?.ToString() ?? ex.Message.ToString(); - - throw; + var message = ex.InnerException?.Message ?? ex.Message; + return Json(new { success = false, response = "An error occurred while processing your request" }); } } #endregion @@ -349,17 +439,14 @@ namespace CPRNIMS.WebApps.Controllers.Items public async Task Index() { var viewModels = new ItemVM(); - await IsAuthenTicated(); await UpdateCart(viewModels); - return View(); + return await IsAuthenTicated(); } public async Task ItemCart(byte TypeOfRequest) { - var viewModels = new ItemVM(); - await IsAuthenTicated(); ViewBag.TypeOfRequest = TypeOfRequest; - return View(); + return await IsAuthenTicated(); } #endregion #region Common diff --git a/CPRNIMS.WebApps/Controllers/PO/POMgmtController.cs b/CPRNIMS.WebApps/Controllers/PO/POMgmtController.cs index d59fe6f..d7dff48 100644 --- a/CPRNIMS.WebApps/Controllers/PO/POMgmtController.cs +++ b/CPRNIMS.WebApps/Controllers/PO/POMgmtController.cs @@ -1,4 +1,5 @@ -using CPRNIMS.Domain.UIContracts.PO; +using CPRNIMS.Domain.UIContracts.Account; +using CPRNIMS.Domain.UIContracts.PO; using CPRNIMS.Infrastructure.Entities.PO; using CPRNIMS.Infrastructure.Helper; using CPRNIMS.Infrastructure.ViewModel.PO; @@ -16,8 +17,8 @@ namespace CPRNIMS.WebApps.Controllers.PO private readonly IPurchaseOrder _purchaseOrder; public POMgmtController( ErrorLogHelper errorMessageService, IWebHostEnvironment webHostEnvironment - , IPurchaseOrder purchaseOrder, TokenHelper tokenHelper - ) : base(errorMessageService, webHostEnvironment, tokenHelper) + , IPurchaseOrder purchaseOrder, TokenHelper tokenHelper,IAccount account + ) : base(errorMessageService, webHostEnvironment, tokenHelper, account) { _purchaseOrder = purchaseOrder; } diff --git a/CPRNIMS.WebApps/Controllers/PR/PRMgmtController.cs b/CPRNIMS.WebApps/Controllers/PR/PRMgmtController.cs index 42913e5..1273d20 100644 --- a/CPRNIMS.WebApps/Controllers/PR/PRMgmtController.cs +++ b/CPRNIMS.WebApps/Controllers/PR/PRMgmtController.cs @@ -1,4 +1,6 @@ -using CPRNIMS.Domain.UIContracts.PR; +using CPRNIMS.Domain.Services; +using CPRNIMS.Domain.UIContracts.Account; +using CPRNIMS.Domain.UIContracts.PR; using CPRNIMS.Infrastructure.Dto.PR; using CPRNIMS.Infrastructure.Helper; using CPRNIMS.Infrastructure.ViewModel.PR; @@ -13,12 +15,43 @@ namespace CPRNIMS.WebApps.Controllers.PR private readonly IPRequest _pRequest; public PRMgmtController(TokenHelper tokenHelper, ErrorLogHelper errorMessageService, IWebHostEnvironment webHostEnvironment - , IPRequest pRequest, IConfiguration configuration) - : base(errorMessageService, webHostEnvironment, tokenHelper) + , IPRequest pRequest, IConfiguration configuration,IAccount account) + : base(errorMessageService, webHostEnvironment, tokenHelper, account) { _pRequest = pRequest; } #region Get + private async Task GetProductFile(string fileName) + { + try + { + // Validate filename + if (string.IsNullOrWhiteSpace(fileName) || fileName.Contains("..")) + { + return BadRequest("Invalid file name"); + } + + var uploadsPath = Path.Combine(_webHostEnvironment.WebRootPath, "Content/Uploads", "PRAttachment"); + var filePath = Path.Combine(uploadsPath, fileName); + + ContentTypeHelper.ValidateFile(filePath, uploadsPath); + + // Stream the file instead of loading entirely into memory + var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); + var contentType = ContentTypeHelper.GetContentType(fileName); + + return File(fileStream, contentType, fileName, enableRangeProcessing: true); + } + catch (Exception ex) + { + return StatusCode(500, "Error retrieving file"); + } + } + public async Task GetProjectCodes(PRVM viewModels) + { + response = await _pRequest.GetProjectCodes(GetUser(), viewModels); + return GetResponse(response); + } public async Task GetApproverName(PRVM viewModels) { response = await _pRequest.GetApproverName(GetUser(), viewModels); @@ -117,6 +150,16 @@ namespace CPRNIMS.WebApps.Controllers.PR } #endregion #region POST PUT + public async Task PostPutProjectCode([FromBody] PRVM viewModel) + { + var postPutItem = await _pRequest.PostPutProjectCode(GetUser(), viewModel); + + if (postPutItem.messCode != 0) + { + return Json(new { success = true, Response = postPutItem.Message }); + } + return Json(new { success = false, Response = postPutItem.Message }); + } public async Task ApprovedSelectedPRItem(PRVM viewModel, List PRList) { @@ -223,6 +266,10 @@ namespace CPRNIMS.WebApps.Controllers.PR { return await IsAuthenTicated(); } + public async Task ProjectCode() + { + return await IsAuthenTicated(); + } #endregion } } diff --git a/CPRNIMS.WebApps/Controllers/Receiving/ReceivingController.cs b/CPRNIMS.WebApps/Controllers/Receiving/ReceivingController.cs index 3002389..b0b873d 100644 --- a/CPRNIMS.WebApps/Controllers/Receiving/ReceivingController.cs +++ b/CPRNIMS.WebApps/Controllers/Receiving/ReceivingController.cs @@ -1,4 +1,5 @@ -using CPRNIMS.Domain.UIContracts.Receiving; +using CPRNIMS.Domain.UIContracts.Account; +using CPRNIMS.Domain.UIContracts.Receiving; using CPRNIMS.Infrastructure.Helper; using CPRNIMS.Infrastructure.ViewModel.PR; using CPRNIMS.Infrastructure.ViewModel.Receiving; @@ -14,8 +15,8 @@ namespace CPRNIMS.WebApps.Controllers.Receiving private readonly IConfiguration _configuration; public ReceivingController(ErrorLogHelper errorMessageService, IWebHostEnvironment webHostEnvironment,TokenHelper tokenHelper - , IReceiving receiving, IConfiguration configuration) - : base(errorMessageService, webHostEnvironment,tokenHelper) + , IReceiving receiving, IConfiguration configuration,IAccount account) + : base(errorMessageService, webHostEnvironment,tokenHelper, account) { _receiving = receiving; _configuration = configuration; diff --git a/CPRNIMS.WebApps/Controllers/SMTP/SMTPMgmtController.cs b/CPRNIMS.WebApps/Controllers/SMTP/SMTPMgmtController.cs index 3e3a5bf..462f088 100644 --- a/CPRNIMS.WebApps/Controllers/SMTP/SMTPMgmtController.cs +++ b/CPRNIMS.WebApps/Controllers/SMTP/SMTPMgmtController.cs @@ -1,4 +1,5 @@ using Azure; +using CPRNIMS.Domain.UIContracts.Account; using CPRNIMS.Domain.UIContracts.SMTP; using CPRNIMS.Infrastructure.Entities.Items; using CPRNIMS.Infrastructure.Helper; @@ -17,16 +18,15 @@ namespace CPRNIMS.WebApps.Controllers.SMTP private readonly ISMTP _sMTP; public SMTPMgmtController(ErrorLogHelper errorMessageService, IWebHostEnvironment webHostEnvironment, TokenHelper tokenHelper - , ISMTP sMTP + , ISMTP sMTP,IAccount account ) - : base(errorMessageService, webHostEnvironment,tokenHelper) + : base(errorMessageService, webHostEnvironment,tokenHelper, account) { _sMTP = sMTP; } public async Task Index() { - await IsAuthenTicated(); - return View(); + return await IsAuthenTicated(); } #region Get public async Task GetAllSmtp() diff --git a/CPRNIMS.WebApps/Views/ItemMgmt/Index.cshtml b/CPRNIMS.WebApps/Views/ItemMgmt/Index.cshtml index 413a127..e30e98a 100644 --- a/CPRNIMS.WebApps/Views/ItemMgmt/Index.cshtml +++ b/CPRNIMS.WebApps/Views/ItemMgmt/Index.cshtml @@ -2,7 +2,7 @@
-

Requestor Page

+

Item Management


- + @await Html.PartialAsync("PagesView/Item/_Scripts") diff --git a/CPRNIMS.WebApps/Views/ItemMgmt/ItemCart.cshtml b/CPRNIMS.WebApps/Views/ItemMgmt/ItemCart.cshtml index 3caad06..72b6e5a 100644 --- a/CPRNIMS.WebApps/Views/ItemMgmt/ItemCart.cshtml +++ b/CPRNIMS.WebApps/Views/ItemMgmt/ItemCart.cshtml @@ -15,9 +15,10 @@ - @@ -25,64 +26,122 @@ - - - - -
+ All - + ItemNo ItemNamePRTypeId Qty Action
- - Back + + Back +
- -