511 lines
19 KiB
C#
511 lines
19 KiB
C#
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;
|
|
using CPRNIMS.WebApps.Controllers.Base;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace CPRNIMS.WebApps.Controllers.PR
|
|
{
|
|
public class PRMgmtController : BaseMethod
|
|
{
|
|
List<PRVM>? response;
|
|
private readonly IPRequest _pRequest;
|
|
public PRMgmtController(TokenHelper tokenHelper, ErrorLogHelper errorMessageService,
|
|
IWebHostEnvironment webHostEnvironment
|
|
, IPRequest pRequest, IConfiguration configuration,IAccount account)
|
|
: base(errorMessageService, webHostEnvironment, tokenHelper, account)
|
|
{
|
|
_pRequest = pRequest;
|
|
}
|
|
#region Get
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetAllPR(
|
|
string searchPRNo = "", string searchItemName = "",
|
|
string searchDept = "", int pageNumber = 1, int pageSize = 10)
|
|
{
|
|
var dto = new PRVM
|
|
{
|
|
SearchPRNo = searchPRNo,
|
|
SearchItemName = searchItemName,
|
|
SearchDept = searchDept,
|
|
PageNumber = pageNumber,
|
|
PageSize = pageSize
|
|
};
|
|
|
|
var result = await _pRequest.GetAllPR(GetUser(),dto);
|
|
int draw = int.TryParse(Request.Query["draw"], out int d) ? d : 1;
|
|
|
|
return Json(new
|
|
{
|
|
draw = draw,
|
|
recordsTotal = result.TotalCount,
|
|
recordsFiltered = result.TotalCount,
|
|
data = result.Data
|
|
});
|
|
}
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetPRArchived(
|
|
string searchPRNo = "", string searchItemName = "",
|
|
string searchDept = "", int pageNumber = 1, int pageSize = 10)
|
|
{
|
|
var dto = new PRVM
|
|
{
|
|
SearchPRNo = searchPRNo,
|
|
SearchItemName = searchItemName,
|
|
SearchDept = searchDept,
|
|
PageNumber = pageNumber,
|
|
PageSize = pageSize
|
|
};
|
|
|
|
var result = await _pRequest.GetPRArchived(GetUser(), dto);
|
|
int draw = int.TryParse(Request.Query["draw"], out int d) ? d : 1;
|
|
|
|
return Json(new
|
|
{
|
|
draw = draw,
|
|
recordsTotal = result.TotalCount,
|
|
recordsFiltered = result.TotalCount,
|
|
data = result.Data
|
|
});
|
|
}
|
|
public async Task<IActionResult> GetDetailedPRTracking(
|
|
string searchPRNo = "",string srchItemNo ="", string searchItemName = "", string searchDept = "", string searchStatusName = "",
|
|
int pageNumber = 1, int pageSize = 10)
|
|
{
|
|
var dto = new PRVM
|
|
{
|
|
SearchPRNo = searchPRNo,
|
|
SearchItemNo = srchItemNo,
|
|
SearchItemName = searchItemName,
|
|
SearchDept = searchDept,
|
|
SearchStatusName = searchStatusName,
|
|
PageNumber = pageNumber,
|
|
PageSize = pageSize
|
|
};
|
|
|
|
var result = await _pRequest.GetDetailedPRTracking(GetUser(), dto);
|
|
int draw = int.TryParse(Request.Query["draw"], out int d) ? d : 1;
|
|
|
|
return Json(new
|
|
{
|
|
draw = draw,
|
|
recordsTotal = result.TotalCount,
|
|
recordsFiltered = result.TotalCount,
|
|
data = result.Data,
|
|
statusList = result.StatusList
|
|
});
|
|
}
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetApprovedPR(
|
|
string searchPRNo = "", string searchItemName = "", string searchDept = "", string searchStatusName = "",
|
|
int pageNumber = 1, int pageSize = 10)
|
|
{
|
|
var dto = new PRVM
|
|
{
|
|
SearchPRNo = searchPRNo,
|
|
SearchItemName = searchItemName,
|
|
SearchDept = searchDept,
|
|
SearchStatusName = searchStatusName,
|
|
PageNumber = pageNumber,
|
|
PageSize = pageSize
|
|
};
|
|
|
|
var result = await _pRequest.GetApprovedPR(GetUser(),dto);
|
|
int draw = int.TryParse(Request.Query["draw"], out int d) ? d : 1;
|
|
|
|
return Json(new
|
|
{
|
|
draw = draw,
|
|
recordsTotal = result.TotalCount,
|
|
recordsFiltered = result.TotalCount,
|
|
data = result.Data,
|
|
statusList = result.StatusList
|
|
});
|
|
}
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetDeletedPR(
|
|
bool isArchived = false, string searchPRNo = "", string searchItemName = "",
|
|
string searchDept = "", int pageNumber = 1, int pageSize = 10)
|
|
{
|
|
var dto = new PRVM
|
|
{
|
|
IsArchived = isArchived,
|
|
SearchPRNo = searchPRNo,
|
|
SearchItemName = searchItemName,
|
|
SearchDept = searchDept,
|
|
PageNumber = pageNumber,
|
|
PageSize = pageSize
|
|
};
|
|
|
|
var result = await _pRequest.GetDeletedPR(GetUser(), dto);
|
|
int draw = int.TryParse(Request.Query["draw"], out int d) ? d : 1;
|
|
|
|
return Json(new
|
|
{
|
|
draw = draw,
|
|
recordsTotal = result.TotalCount,
|
|
recordsFiltered = result.TotalCount,
|
|
data = result.Data
|
|
});
|
|
}
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetPRAttachment(string fileName)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(fileName) || fileName.Contains(".."))
|
|
{
|
|
// Return a 400 Bad Request so the frontend 'catch' or '!response.ok' triggers
|
|
return BadRequest(new { success = false, message = "File does not exist!" });
|
|
}
|
|
|
|
var uploadsPath = Path.Combine(_webHostEnvironment.WebRootPath, "Content/Uploads", "PRAttachment");
|
|
var filePath = Path.Combine(uploadsPath, fileName);
|
|
|
|
if (!System.IO.File.Exists(filePath))
|
|
{
|
|
return NotFound(new { success = false, message = "File not found on server." });
|
|
}
|
|
|
|
// Stream the file for better performance
|
|
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
var contentType = "application/octet-stream"; // Or use a provider to get actual type
|
|
|
|
return File(fileStream, contentType, fileName, enableRangeProcessing: true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, "Error retrieving file");
|
|
}
|
|
}
|
|
public async Task<IActionResult> GetProjectCodes(PRVM viewModels)
|
|
{
|
|
response = await _pRequest.GetProjectCodes(GetUser(), viewModels);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetApproverNameByPRNo(PRVM viewModels)
|
|
{
|
|
response = await _pRequest.GetApproverNameByPRNo(GetUser(), viewModels);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetApproverName(PRVM viewModels)
|
|
{
|
|
response = await _pRequest.GetApproverName(GetUser(), viewModels);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetPRDetailByPRNo(PRVM viewModels)
|
|
{
|
|
response = await _pRequest.GetPRDetailByPRNo(GetUser(), viewModels);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetPRListByPRNo(PRVM viewModels)
|
|
{
|
|
response = await _pRequest.GetPRListByPRNo(GetUser(), viewModels);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetMyPR(PRVM viewModels)
|
|
{
|
|
response = await _pRequest.GetMyPR(GetUser(), viewModels);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetForReceiving()
|
|
{
|
|
var viewModels = new PRVM();
|
|
response = await _pRequest.GetForReceiving(GetUser(), viewModels);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetDeniedItem(PRVM viewModels)
|
|
{
|
|
response = await _pRequest.GetForReceiving(GetUser(), viewModels);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetPRByRRId(PRVM viewModel)
|
|
{
|
|
response = await _pRequest.GetPRByRRId(GetUser(), viewModel);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetRRDetailByPO(PRVM viewModel)
|
|
{
|
|
response = await _pRequest.GetRRDetailByPO(GetUser(), viewModel);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetPRStatusById(PRVM viewModel)
|
|
{
|
|
response = await _pRequest.GetPRStatusById(GetUser(), viewModel);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetItemDetailForReceiving(PRVM viewModel)
|
|
{
|
|
response = await _pRequest.GetItemDetailForReceiving(GetUser(), viewModel);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetSupplierAlternativeOffer(PRVM viewModel)
|
|
{
|
|
response = await _pRequest.GetSupplierAlternativeOffer(GetUser(), viewModel);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetSupplierAlterOfferDetails(PRVM viewModel)
|
|
{
|
|
response = await _pRequest.GetSupplierAlterOfferDetails(GetUser(), viewModel);
|
|
return GetResponse(response);
|
|
}
|
|
public async Task<IActionResult> GetDashBoard()
|
|
{
|
|
response = await _pRequest.GetDashBoard(GetUser(), new PRVM());
|
|
|
|
return GetResponse(response);
|
|
}
|
|
#region Mapper
|
|
private PRList MapToPRItemList(IEnumerable<PRList> prList)
|
|
{
|
|
if (prList == null || !prList.Any())
|
|
{
|
|
return new PRList
|
|
{
|
|
PRDetailsId = new List<long>(),
|
|
ItemNo = new List<long>(),
|
|
};
|
|
}
|
|
|
|
return new PRList
|
|
{
|
|
PRDetailsId = prList.SelectMany(ic => ic.PRDetailsId).ToList(),
|
|
ItemNo = prList.SelectMany(ic => ic.ItemNo).ToList()
|
|
};
|
|
}
|
|
private PRItemListRequest MapToPRItemList(IEnumerable<PRItemListRequest> prItemListRequest)
|
|
{
|
|
if (prItemListRequest == null || !prItemListRequest.Any())
|
|
{
|
|
return new PRItemListRequest
|
|
{
|
|
ItemNo = new List<long>(),
|
|
Qty = new List<decimal>(),
|
|
};
|
|
}
|
|
|
|
return new PRItemListRequest
|
|
{
|
|
ItemNo = prItemListRequest.SelectMany(ic => ic.ItemNo).ToList(),
|
|
Qty = prItemListRequest.SelectMany(ic => ic.Qty).ToList()
|
|
};
|
|
}
|
|
#endregion
|
|
#endregion
|
|
#region POST PUT
|
|
public async Task<IActionResult?> UploadAttachment(IFormFile? file, [FromForm]
|
|
string? oldFileName,
|
|
[FromForm] long prId)
|
|
{
|
|
var uploadsPath = Path.Combine(
|
|
_webHostEnvironment.WebRootPath,
|
|
"Content", "Uploads", "PRAttachment");
|
|
|
|
Directory.CreateDirectory(uploadsPath);
|
|
|
|
// If no new file uploaded, return old filename
|
|
if (file == null)
|
|
return BadRequest(new { success = false, message = "File does not exist!" });
|
|
|
|
// 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);
|
|
|
|
var prVM = new PRVM()
|
|
{
|
|
FileName = newFileName,
|
|
OrigFileName = file.FileName,
|
|
PRId = prId
|
|
};
|
|
var prResponse = await _pRequest.PostPutAttachment(GetUser(),prVM);
|
|
if (prResponse.messCode == 0) {
|
|
return BadRequest(new { success = false, message = "File does not exist!"});
|
|
}
|
|
|
|
// Return only filename (NOT full path)
|
|
return Json(new { success = true, message = "Attachment successfully uploaded!", newFileName = 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));
|
|
}
|
|
}
|
|
public async Task<IActionResult> 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<IActionResult> ApprovedSelectedPRItem(PRVM viewModel,
|
|
List<PRList> PRList)
|
|
{
|
|
viewModel.PRList = MapToPRItemList(PRList);
|
|
|
|
var postPutItem = await _pRequest.ApprovedSelectedPRItem(GetUser(), viewModel);
|
|
|
|
if (postPutItem.messCode != 0)
|
|
{
|
|
return Json(new { success = true });
|
|
}
|
|
return Json(new { success = false, Response = postPutItem.Message });
|
|
}
|
|
public async Task<IActionResult> PostPutDeniedItem(PRVM viewModel, List<ItemReceivingList> ItemList)
|
|
{
|
|
if (ItemList.Count > 0)
|
|
{
|
|
viewModel.ItemList = new ItemReceivingList
|
|
{
|
|
PRDetailsId = ItemList.SelectMany(ic => ic.PRDetailsId).ToList(),
|
|
PRNo = ItemList.SelectMany(ic => ic.PRNo).ToList(),
|
|
ItemNo = ItemList.SelectMany(ic => ic.ItemNo).ToList(),
|
|
};
|
|
var postPutItem = await _pRequest.PostPutDeniedItem(GetUser(), viewModel);
|
|
|
|
if (postPutItem.messCode != 0)
|
|
{
|
|
return Json(new { success = true, Response = postPutItem.Message });
|
|
}
|
|
|
|
return Json(new { success = false, Response = postPutItem.Message });
|
|
}
|
|
return Json(new { success = false, Response = "EmptyArray" });
|
|
}
|
|
public async Task<IActionResult> PutItemDetail(PRVM viewModel)
|
|
{
|
|
var postPutItem = await _pRequest.PutItemDetail(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<IActionResult> PostPRApproveReject(PRVM viewModel)
|
|
{
|
|
var postPutItem = await _pRequest.PostPRApproveReject(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<IActionResult> PutSupplierAlterOffer(PRVM viewModel)
|
|
{
|
|
var postPutItem = await _pRequest.PutSupplierAlterOffer(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<IActionResult> PRItemRemoval(PRVM viewModel)
|
|
{
|
|
var postPutItem = await _pRequest.PRItemRemoval(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<IActionResult> PostItemInPR(PRVM viewModel, List<PRItemListRequest> PRItemList)
|
|
{
|
|
viewModel.PRItemListRequest = MapToPRItemList(PRItemList);
|
|
var postPutItem = await _pRequest.PostItemInPR(GetUser(), viewModel);
|
|
|
|
if (postPutItem.messCode != 0)
|
|
{
|
|
return Json(new { success = true, Response = postPutItem.Message });
|
|
}
|
|
return Json(new { success = false, Response = postPutItem.Message });
|
|
}
|
|
#endregion
|
|
#region Views
|
|
public IActionResult GetDashBoardById(int DashboardId)
|
|
{
|
|
return ViewComponent("Dashboard", new { dashboardId = DashboardId });
|
|
}
|
|
public IActionResult GetTabbedById(int TableId)
|
|
{
|
|
return ViewComponent("PRTabbed", new { tableId = TableId });
|
|
}
|
|
public async Task<IActionResult> DashBoard()
|
|
{
|
|
return await IsAuthenTicated();
|
|
}
|
|
public async Task<IActionResult> Index()
|
|
{
|
|
return await IsAuthenTicated();
|
|
}
|
|
public async Task<IActionResult> PRArchived()
|
|
{
|
|
return await IsAuthenTicated();
|
|
}
|
|
public async Task<IActionResult> DeniedItem()
|
|
{
|
|
return await IsAuthenTicated();
|
|
}
|
|
public async Task<IActionResult> PRTracking()
|
|
{
|
|
return await IsAuthenTicated();
|
|
}
|
|
public async Task<IActionResult> AlterOffer()
|
|
{
|
|
return await IsAuthenTicated();
|
|
}
|
|
public async Task<IActionResult> ProjectCode()
|
|
{
|
|
return await IsAuthenTicated();
|
|
}
|
|
#endregion
|
|
}
|
|
}
|