using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using HRM.BO; using HRM.DA; using Microsoft.AspNetCore.Authorization; using System.IO; using Newtonsoft.Json; using Microsoft.AspNetCore.StaticFiles; namespace HRM.UI.Controllers.Payroll { [Route("api/dataUpload")] [ApiController] [Authorize] public class DataUploadController : ControllerBase { private static string _filePath = @"ClientApp\Documents\"; public readonly IDataUploadColumnDefinition _columnDefination; private readonly IEmployeeBulkUploadService _employeeBulkUploadService; private readonly ISalaryMasterBulkUploadService _salaryMasterBulkUploadService; private readonly IEmployeePhotoBulkUploadService _employeePhotoBulkUploadService; private readonly ICacheManager _cashManager; private readonly IBonusProcessService _bonusProcessService; public DataUploadController(IDataUploadColumnDefinition columnDefination, IEmployeeBulkUploadService employeeBulkUploadService, ISalaryMasterBulkUploadService salaryMasterBulkUploadService, IEmployeePhotoBulkUploadService employeePhotoBulkUploadService, ICacheManager cashManager, IBonusProcessService bonusProcessService) { this._columnDefination = columnDefination; this._employeeBulkUploadService = employeeBulkUploadService; this._salaryMasterBulkUploadService = salaryMasterBulkUploadService; this._employeePhotoBulkUploadService = employeePhotoBulkUploadService; this._cashManager = cashManager; this._bonusProcessService = bonusProcessService; } // loan [HttpGet("prepareUpload/{uploadId}/{itemid}")] public ActionResult GetPrepareUpload(int uploadId, string itemid) { int? newItemid = GlobalFunctions.GetApiDefaultIntData(itemid); DataUploadColumnDefinition dataColumnDefinition = new DataUploadColumnDefinition(); try { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); dataColumnDefinition = this._columnDefination.GetColumnDefinition(uploadId, newItemid, (int)currentUser.PayrollTypeID); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return Ok(dataColumnDefinition); } //[HttpGet("uploadData/{uploadId}/{oColumnDefination}/{path}")] //public ActionResult GetUploadDataMessage(int uploadId, DataUploadColumnDefinitionVM oColumnDefination, string path) //{ // RegularDataUpload regUpload= new RegularDataUpload(); // List errorOrSuccessList = new List(); // try // { // // regUpload.UploadData(uploadId, oColumnDefination, path); // } // catch (Exception ex) // { // return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); // } // return Ok(regUpload); //} [HttpPost] [Route("regularExcelInterface")] //[AllowAnonymous] public ActionResult regularExcelInterface() { DataUploadColumnDefinition oColumnDefination = new DataUploadColumnDefinition(); RegularDataUploadService regUpload = new RegularDataUploadService(); List errorOrSuccessList = new List(); DateTime selectedDate = DateTime.Today; CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); try { int uploadID = Convert.ToInt32(Request.Form["uploadID"]); int? itemid = GlobalFunctions.GetApiDefaultIntData( Convert.ToString(Request.Form["itemID"])); int? itemidTwo = GlobalFunctions.GetApiDefaultIntData(Convert.ToString(Request.Form["itemIDTwo"])); int? payrolltypeid = currentUser.PayrollTypeID; int? userid = (int)currentUser.UserID; string date = Request.Form["selectedDate"]; if (!string.IsNullOrEmpty(date)) { selectedDate = DateTime.Parse(date); } var files = Request.Form.Files; foreach (var formFile in files) { oColumnDefination = this._columnDefination.GetColumnDefinition(uploadID, itemid, (int)payrolltypeid); if (itemid != null) { oColumnDefination.uploadItemID =(int) itemid; oColumnDefination.SelectedItemOneID =(int) itemid; } if (itemidTwo != null) { oColumnDefination.SelectedItemTwoID =(int) itemidTwo; } errorOrSuccessList = regUpload.UploadData(uploadID, oColumnDefination, formFile.OpenReadStream() , (int)payrolltypeid, (int) userid, itemid, selectedDate); break; } } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return Ok(errorOrSuccessList); } [HttpPost] [Route("uploadBonusFile")] //[AllowAnonymous] public ActionResult uploadBonusFile() { DataUploadColumnDefinition oColumnDefination = new DataUploadColumnDefinition(); RegularDataUploadService regUpload = new RegularDataUploadService(); List items = null; try { int? payrolltypeid = GlobalFunctions.GetApiDefaultIntData(Convert.ToString(Request.Form["payrolltypeid"])); var files = Request.Form.Files; foreach (var formFile in files) { oColumnDefination = this._columnDefination.GetColumnDefinition( (int)EnumRegularDataUpload.Bonus, null, (int)payrolltypeid); items = regUpload.GetBonusData((int)EnumRegularDataUpload.Bonus, oColumnDefination, formFile.OpenReadStream() , (int)payrolltypeid); break; } } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return Ok(items); } /// /// Bonus Process screen, "Upload Data": reads the bonus file (sheet EmpBonus: Employee No, Amount — the /// same reader as uploadBonusFile) and saves it in the same request, with the tax when the header asks /// for it (BonusProcessService.UploadAndSave). Form fields: excelfile, and header = the JSON of a /// BonusProcessAndSaveRequest (bonus, dates, flags). Returns the saved rows. /// [HttpPost] [Route("uploadBonusFileAndSave")] public ActionResult uploadBonusFileAndSave() { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); BonusProcessAndSaveResult result = null; try { int payrolltypeid = (int)currentUser.PayrollTypeID; string headerJson = Convert.ToString(Request.Form["header"]); if (string.IsNullOrWhiteSpace(headerJson)) return StatusCode(StatusCodes.Status500InternalServerError, "Bonus information not found."); // same JSON reading as the Process button's request (Newtonsoft defaults), so dates arrive the same way BonusProcessAndSaveRequest request = JsonConvert.DeserializeObject(headerJson); IFormFile formFile = Request.Form.Files.FirstOrDefault(); if (formFile == null || formFile.Length == 0) return StatusCode(StatusCodes.Status500InternalServerError, "Please select a file."); string extension = Path.GetExtension(formFile.FileName ?? string.Empty).ToLowerInvariant(); if (extension != ".xlsx" && extension != ".xls") return StatusCode(StatusCodes.Status500InternalServerError, "Please select an Excel file (.xlsx or .xls)."); DataUploadColumnDefinition oColumnDefination = this._columnDefination.GetColumnDefinition( (int)EnumRegularDataUpload.Bonus, null, payrolltypeid); List items; using (Stream stream = formFile.OpenReadStream()) { items = new RegularDataUploadService().GetBonusData((int)EnumRegularDataUpload.Bonus, oColumnDefination, stream, payrolltypeid); } result = _bonusProcessService.UploadAndSave(items, request, payrolltypeid, currentUser.UserID); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return Ok(result); } /// /// The Employee Bulk Upload layout — groups, sub-groups and columns — that the page shows /// as section checkboxes and in its template guide. /// [HttpGet("employee-bulk-upload-layout")] [ProducesResponseType(StatusCodes.Status200OK)] public ActionResult GetEmployeeBulkUploadLayout() { return Ok(EmployeeBulkUploadTemplate.GetGroups()); } /// /// The empty Employee Bulk Upload workbook. Generated on each request from /// EmployeeBulkUploadTemplate, so the downloaded file can never fall out of step with /// the validation rules. lists the row-2 band names to /// include (repeat the query key per name); none means the full template. /// [HttpGet("employee-bulk-upload-template")] [ProducesResponseType(StatusCodes.Status200OK)] public ActionResult GetEmployeeBulkUploadTemplate([FromQuery] List subGroups) { if (subGroups != null && subGroups.Count > 0) { List known = EmployeeBulkUploadTemplate.GetSubGroupNames(); List unknown = subGroups .Where(x => !known.Contains(x, StringComparer.OrdinalIgnoreCase)) .ToList(); if (unknown.Count > 0) { return BadRequest("Unknown section(s): " + string.Join(", ", unknown)); } } byte[] file; try { file = this._employeeBulkUploadService.GetTemplate(subGroups); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return File(file, EmployeeBulkUploadTemplate.ContentType, EmployeeBulkUploadTemplate.FileName); } /// /// Validates a filled Employee Bulk Upload workbook (multipart form: first file, plus one /// "subGroups" field per section ticked in Template sections) and returns the result of /// every validation layer, plus the sections and rows when all layers passed. All the /// validation lives in the service; a failed check is a normal 200 result, not an error. /// Nothing is saved. /// [HttpPost("employee-bulk-upload-read")] [ProducesResponseType(StatusCodes.Status200OK)] public ActionResult ReadEmployeeBulkUpload() { IFormFile file = Request.Form.Files.Count > 0 ? Request.Form.Files[0] : null; int postedPayrollTypeID; int.TryParse(Request.Form["payrollTypeId"].FirstOrDefault(), out postedPayrollTypeID); List replacements; if (!TryReadReplacements(out replacements)) { return BadRequest("The replaced master-data values could not be read. Please upload the file again."); } EmployeeBulkUploadReadResult result; try { using (Stream stream = file == null ? null : file.OpenReadStream()) { result = this._employeeBulkUploadService.ReadUpload(stream, file == null ? null : file.FileName, file == null ? 0 : file.Length, Request.Form["subGroups"].ToList(), this.CurrentPayrollTypeID(postedPayrollTypeID), replacements); } } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return Ok(result); } /// /// The optional "replacements" form field: a JSON list of the master-data values the user /// chose to swap for existing records in the master-data dialog. No field means none; /// a field that is not such a list is refused. /// private bool TryReadReplacements(out List replacements) { replacements = new List(); string json = Request.Form["replacements"].FirstOrDefault(); if (string.IsNullOrWhiteSpace(json)) { return true; } try { replacements = JsonConvert.DeserializeObject>(json) ?? new List(); return true; } catch (JsonException) { return false; } } /// /// Saves a filled Employee Bulk Upload workbook (same multipart form as the read, plus /// "closeMatchesConfirmed"). The service validates the file again and saves every row in /// one transaction — all or nothing. A file that no longer passes, or unconfirmed close /// matches, come back as Saved = false with the reason; nothing is written then. /// [HttpPost("employee-bulk-upload-save")] [ProducesResponseType(StatusCodes.Status200OK)] public ActionResult SaveEmployeeBulkUpload() { IFormFile file = Request.Form.Files.Count > 0 ? Request.Form.Files[0] : null; if (file == null) { return BadRequest("No file was received."); } int postedPayrollTypeID; int.TryParse(Request.Form["payrollTypeId"].FirstOrDefault(), out postedPayrollTypeID); bool closeMatchesConfirmed; bool.TryParse(Request.Form["closeMatchesConfirmed"].FirstOrDefault(), out closeMatchesConfirmed); List replacements; if (!TryReadReplacements(out replacements)) { return BadRequest("The replaced master-data values could not be read. Nothing was saved."); } try { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); using (Stream stream = file.OpenReadStream()) { return Ok(this._employeeBulkUploadService.SaveUpload(stream, file.FileName, file.Length, Request.Form["subGroups"].ToList(), this.CurrentPayrollTypeID(postedPayrollTypeID), currentUser.UserID, closeMatchesConfirmed, replacements)); } } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } /// /// Adds missing values to one master table (the "Add missing master data" dialog sends one /// request per table). Each value is saved on its own; the reply says, per value, whether /// it was added, already there, or why it could not be added. /// [HttpPost("employee-bulk-upload-add-master-data")] [ProducesResponseType(StatusCodes.Status200OK)] public ActionResult AddEmployeeBulkUploadMasterData(EmployeeBulkUploadMasterAddRequest request) { if (request == null || request.Items == null || request.Items.Count == 0) { return BadRequest("Nothing to add."); } try { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); return Ok(this._employeeBulkUploadService.AddMasterData(request, this.CurrentPayrollTypeID(request.PayrollTypeID), currentUser.UserID)); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } /// The active records of a master table, for the add dialog's pickers (Division, District, ...). [HttpGet("employee-bulk-upload-master-options/{masterData}/{payrollTypeId}")] [ProducesResponseType(StatusCodes.Status200OK)] public ActionResult GetEmployeeBulkUploadMasterOptions(EnumBulkUploadMasterData masterData, int payrollTypeId) { try { return Ok(this._employeeBulkUploadService.GetMasterOptions(masterData, this.CurrentPayrollTypeID(payrollTypeId))); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } /// /// The empty Salary Master Data workbook for one salary month. Generated on each request /// from SalaryMasterBulkUploadTemplate, so the file always matches the validation rules. /// [HttpGet("salary-master-bulk-upload-template/{salaryMonth}")] [ProducesResponseType(StatusCodes.Status200OK)] public ActionResult GetSalaryMasterBulkUploadTemplate(DateTime salaryMonth) { salaryMonth = GlobalFunctions.LastDateOfMonth(salaryMonth); byte[] file; try { file = this._salaryMasterBulkUploadService.GetTemplate(salaryMonth); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } return File(file, SalaryMasterBulkUploadTemplate.ContentType, SalaryMasterBulkUploadTemplate.FileName(salaryMonth)); } /// /// Validates a filled Salary Master Data workbook (multipart form: first file, plus /// "salaryMonth", the month picked on the page) and returns every validation layer, /// every problem and the rows. A failed check is a normal 200 result. Nothing is saved. /// [HttpPost("salary-master-bulk-upload-read")] [ProducesResponseType(StatusCodes.Status200OK)] public ActionResult ReadSalaryMasterBulkUpload() { IFormFile file = Request.Form.Files.Count > 0 ? Request.Form.Files[0] : null; DateTime salaryMonth; if (!TryGetPostedSalaryMonth(out salaryMonth)) { return BadRequest("Please pick a valid salary month."); } int postedPayrollTypeID; int.TryParse(Request.Form["payrollTypeId"].FirstOrDefault(), out postedPayrollTypeID); try { using (Stream stream = file == null ? null : file.OpenReadStream()) { return Ok(this._salaryMasterBulkUploadService.ReadUpload(stream, file == null ? null : file.FileName, file == null ? 0 : file.Length, salaryMonth, this.CurrentPayrollTypeID(postedPayrollTypeID))); } } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } /// /// Saves a filled Salary Master Data workbook (same multipart form as the read). The /// service validates the file again and saves every row in one transaction — all or /// nothing. A file that no longer passes comes back as Saved = false with the reason. /// [HttpPost("salary-master-bulk-upload-save")] [ProducesResponseType(StatusCodes.Status200OK)] public ActionResult SaveSalaryMasterBulkUpload() { IFormFile file = Request.Form.Files.Count > 0 ? Request.Form.Files[0] : null; if (file == null) { return BadRequest("No file was received."); } DateTime salaryMonth; if (!TryGetPostedSalaryMonth(out salaryMonth)) { return BadRequest("Please pick a valid salary month."); } int postedPayrollTypeID; int.TryParse(Request.Form["payrollTypeId"].FirstOrDefault(), out postedPayrollTypeID); try { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); using (Stream stream = file.OpenReadStream()) { return Ok(this._salaryMasterBulkUploadService.SaveUpload(stream, file.FileName, file.Length, salaryMonth, this.CurrentPayrollTypeID(postedPayrollTypeID), currentUser.UserID)); } } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } #region Employee Profile Picture Bulk Upload /// The picture folder on the server and the rules, for the instructions. [HttpGet("employee-photo-bulk-upload-info")] public ActionResult GetEmployeePhotoBulkUploadInfo() { try { return Ok(this._employeePhotoBulkUploadService.GetInfo()); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } /// Checks every picture in the folder against the login's payroll type. Nothing is saved. [HttpGet("employee-photo-bulk-upload-read")] public ActionResult ReadEmployeePhotoBulkUpload() { try { return Ok(this._employeePhotoBulkUploadService.ReadFolder(this.CurrentPayrollTypeID(0))); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } /// /// Starts saving every picture of the folder — all or nothing — and returns at once with the /// run, which the screen then follows. 15,000 pictures take minutes, far too long to hold /// one request open, so the work runs on its own thread (as mail batches do). /// [HttpPost("employee-photo-bulk-upload-save")] public ActionResult SaveEmployeePhotoBulkUpload() { try { int payrollTypeID = this.CurrentPayrollTypeID(0); EmployeePhotoBulkUploadRun run = this._employeePhotoBulkUploadService.BeginSave(payrollTypeID); IEmployeePhotoBulkUploadService service = this._employeePhotoBulkUploadService; ICacheManager cashManager = this._cashManager; EmailLogBackgroundRunner.Run(() => { service.RunSave(run.RunID, payrollTypeID); // As on approving a profile-picture request: the dashboard caches each employee's // row (with the profile-completion percent), so drop it for every changed employee. EmployeePhotoBulkUploadRun done = service.GetRun(run.RunID); if (done != null && done.Status == EmployeePhotoBulkUploadRun.Saved) { foreach (int employeeID in done.SavedEmployeeIDs) { cashManager.Remove(enumMemoryCashType.LandingPageEmployee, enumMemoryCashNature.EmployeeWise, employeeID); } } }); return Ok(run); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } /// How a save is getting on, and at the end its outcome. [HttpGet("employee-photo-bulk-upload-run/{runId}")] public ActionResult GetEmployeePhotoBulkUploadRun(string runId) { EmployeePhotoBulkUploadRun run = this._employeePhotoBulkUploadService.GetRun(runId); if (run == null) { return NotFound("The save could not be found. The application may have restarted; check the folder again."); } return Ok(run); } /// The save running now, if any, so a reopened screen follows it instead of starting another. Empty when none. [HttpGet("employee-photo-bulk-upload-active-run")] public ActionResult GetEmployeePhotoBulkUploadActiveRun() { return Ok(this._employeePhotoBulkUploadService.GetActiveRun()); } #endregion /// The "salaryMonth" form field (the month picked on the page) as the last date of that month. private bool TryGetPostedSalaryMonth(out DateTime salaryMonth) { if (!DateTime.TryParse(Request.Form["salaryMonth"].FirstOrDefault(), out salaryMonth)) { return false; } salaryMonth = GlobalFunctions.LastDateOfMonth(salaryMonth); return true; } /// /// The login's payroll type from the token; the page's own value only when the token has /// none, so a page cannot read or write another payroll type's data. /// private int CurrentPayrollTypeID(int postedPayrollTypeID) { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); if (currentUser != null && currentUser.PayrollTypeID.HasValue && currentUser.PayrollTypeID.Value > 0) { return currentUser.PayrollTypeID.Value; } return postedPayrollTypeID > 0 ? postedPayrollTypeID : 0; } //[HttpPost] //[Route("get-download-file")] //[AllowAnonymous] //[IgnoreAntiforgeryToken] //[ProducesResponseType(StatusCodes.Status200OK)] //public async Task EmployeeDownload(dynamic data) //{ // //var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "uploads"); // //var filePath = Path.Combine(uploads, file); // var item = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data)); // string tempfileName = (string) item["fileName"].ToObject(); // string tempmappingpath = (string) item["mappingPath"].ToObject(); // var fileName = _filePath + tempmappingpath; // var contentType = GetFileType(data); // string fullPath = System.IO.Path.Combine(System.Environment.CurrentDirectory, fileName); // if (!System.IO.File.Exists(fullPath)) // return NotFound(); // var memory = new MemoryStream(); // using (var stream = new FileStream(fullPath, FileMode.Open)) // { // await stream.CopyToAsync(memory); // } // memory.Position = 0; // return File(memory, GetContentType(fullPath)); //} //[HttpPost] //[Route("get-file-type")] //[AllowAnonymous] //[IgnoreAntiforgeryToken] //[ProducesResponseType(StatusCodes.Status200OK)] //public string GetFileType(dynamic data) //{ // var item = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data)); // string fileName = (string) item["fileName"].ToObject(); // string contentType; // new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType); // return contentType ?? "application/octet-stream"; //} private string GetContentType(string path) { var provider = new FileExtensionContentTypeProvider(); string contentType; if (!provider.TryGetContentType(path, out contentType)) { contentType = "application/octet-stream"; } return contentType; } } }