All GCC vendors and employee details for IT department management
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

469 lines
20 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package com.gcc_manpower.gccadjustmentmanagement.controller;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.security.authentication.AuthenticationManager;
  11. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  12. import org.springframework.security.core.Authentication;
  13. import org.springframework.security.core.AuthenticationException;
  14. import org.springframework.security.core.context.SecurityContextHolder;
  15. import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.ui.Model;
  18. import org.springframework.web.bind.annotation.GetMapping;
  19. import org.springframework.web.bind.annotation.PostMapping;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RequestParam;
  22. import org.springframework.web.bind.annotation.ResponseBody;
  23. import org.springframework.web.multipart.MultipartFile;
  24. import com.gcc_manpower.gccadjustmentmanagement.service.AdjustmentService;
  25. import com.gcc_manpower.service.AppUserService;
  26. import jakarta.servlet.http.HttpServletRequest;
  27. import jakarta.servlet.http.HttpSession;
  28. @Controller
  29. @RequestMapping("/gccadjustmentmanagement")
  30. public class AdjustmentController {
  31. @Autowired
  32. private AdjustmentService adjustmentService;
  33. @Autowired
  34. private AppUserService appUserService;
  35. @Autowired
  36. private AuthenticationManager authenticationManager;
  37. @Value("${adjustment.upload.path:gccadjustmentmanagementfiles}")
  38. private String adjustmentUploadPathConfig;
  39. @GetMapping({ "", "/", "/dashboard" })
  40. public String viewDashboard(Model model, HttpSession session) {
  41. String username = (String) session.getAttribute("adj_usrName");
  42. if (username == null) {
  43. return "redirect:/gccadjustmentmanagement/login";
  44. }
  45. String usrRole = (String) session.getAttribute("adj_usrRole");
  46. boolean isAdmin = "ADMIN".equalsIgnoreCase(usrRole);
  47. model.addAttribute("isLoggedIn", true);
  48. model.addAttribute("isAdmin", isAdmin);
  49. model.addAttribute("usrName", username);
  50. model.addAttribute("curPage", "gccadjustmentmanagement");
  51. return "gccadjustmentmanagement/dashboard";
  52. }
  53. @GetMapping("/details")
  54. public String viewDetails(Model model, HttpSession session) {
  55. String username = (String) session.getAttribute("adj_usrName");
  56. if (username == null) {
  57. return "redirect:/gccadjustmentmanagement/login";
  58. }
  59. String usrRole = (String) session.getAttribute("adj_usrRole");
  60. boolean isAdmin = "ADMIN".equalsIgnoreCase(usrRole);
  61. model.addAttribute("isLoggedIn", true);
  62. model.addAttribute("isAdmin", isAdmin);
  63. model.addAttribute("usrName", username);
  64. model.addAttribute("curPage", "gccadjustmentmanagement");
  65. return "gccadjustmentmanagement/details";
  66. }
  67. @GetMapping("/login")
  68. public String login(HttpSession session) {
  69. return "gccadjustmentmanagement/login";
  70. }
  71. @PostMapping("/api/login")
  72. @ResponseBody
  73. public Map<String, Object> validateLogin(
  74. @RequestParam("usrName") String usrName,
  75. @RequestParam("usrPass") String usrPass,
  76. HttpSession session) {
  77. Map<String, Object> response = new HashMap<>();
  78. try {
  79. UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(usrName,
  80. usrPass);
  81. Authentication authentication = authenticationManager.authenticate(authenticationToken);
  82. SecurityContextHolder.getContext().setAuthentication(authentication);
  83. session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
  84. SecurityContextHolder.getContext());
  85. String role = authentication.getAuthorities().stream()
  86. .map(auth -> auth.getAuthority().replace("ROLE_", ""))
  87. .findFirst()
  88. .orElse("USER");
  89. session.setAttribute("adj_usrName", usrName);
  90. session.setAttribute("adj_usrRole", role);
  91. session.setMaxInactiveInterval(1800); // 30 minutes session timeout
  92. response.put("result", true);
  93. response.put("usrName", usrName);
  94. } catch (AuthenticationException e) {
  95. response.put("result", false);
  96. response.put("error", e.getMessage() != null ? e.getMessage() : "Invalid username or password!");
  97. }
  98. return response;
  99. }
  100. @GetMapping("/logout")
  101. public String logout(HttpSession session, HttpServletRequest request) {
  102. SecurityContextHolder.clearContext();
  103. if (session != null) {
  104. session.removeAttribute("adj_usrName");
  105. session.removeAttribute("adj_usrRole");
  106. session.invalidate();
  107. }
  108. request.getSession(true);
  109. return "redirect:/gccadjustmentmanagement/login";
  110. }
  111. @GetMapping("/api/advance-types")
  112. @ResponseBody
  113. public List<Map<String, Object>> getAdvanceTypes(HttpSession session) {
  114. if (session.getAttribute("adj_usrName") == null) {
  115. return Collections.emptyList();
  116. }
  117. return adjustmentService.getAdvanceTypes();
  118. }
  119. @GetMapping("/api/fund-types")
  120. @ResponseBody
  121. public List<Map<String, Object>> getFundTypes(HttpSession session) {
  122. if (session.getAttribute("adj_usrName") == null) {
  123. return Collections.emptyList();
  124. }
  125. return adjustmentService.getFundTypes();
  126. }
  127. @GetMapping("/api/file-statuses")
  128. @ResponseBody
  129. public List<Map<String, Object>> getFileStatuses(HttpSession session) {
  130. if (session.getAttribute("adj_usrName") == null) {
  131. return Collections.emptyList();
  132. }
  133. return adjustmentService.getFileStatusList();
  134. }
  135. @GetMapping("/api/list")
  136. @ResponseBody
  137. public List<Map<String, Object>> getFilesList(HttpSession session) {
  138. if (session.getAttribute("adj_usrName") == null) {
  139. return Collections.emptyList();
  140. }
  141. return adjustmentService.getAllFiles();
  142. }
  143. @GetMapping("/api/list-all")
  144. @ResponseBody
  145. public List<Map<String, Object>> getFilesListIncludingDeleted(HttpSession session) {
  146. if (session.getAttribute("adj_usrName") == null) {
  147. return Collections.emptyList();
  148. }
  149. return adjustmentService.getAllFilesIncludingDeleted();
  150. }
  151. @GetMapping("/api/pending/fi-available")
  152. @ResponseBody
  153. public List<Map<String, Object>> getPendingFIAvailable(HttpSession session) {
  154. if (session.getAttribute("adj_usrName") == null) {
  155. return Collections.emptyList();
  156. }
  157. return adjustmentService.getPendingFinalInvoiceAvailableFiles();
  158. }
  159. @GetMapping("/api/pending/fi-not-available")
  160. @ResponseBody
  161. public List<Map<String, Object>> getPendingFINotAvailable(HttpSession session) {
  162. if (session.getAttribute("adj_usrName") == null) {
  163. return Collections.emptyList();
  164. }
  165. return adjustmentService.getPendingFinalInvoiceNotAvailableFiles();
  166. }
  167. @GetMapping("/api/pending/ready-to-close")
  168. @ResponseBody
  169. public List<Map<String, Object>> getReadyToClose(HttpSession session) {
  170. if (session.getAttribute("adj_usrName") == null) {
  171. return Collections.emptyList();
  172. }
  173. return adjustmentService.getReadyToCloseFiles();
  174. }
  175. @PostMapping("/api/save")
  176. @ResponseBody
  177. public Map<String, Object> saveAdjustment(
  178. @RequestParam(value = "id", required = false) Integer id,
  179. @RequestParam(value = "vend_name", required = false) String vendName,
  180. @RequestParam(value = "fin_year", required = false) String finYear,
  181. @RequestParam(value = "subject", required = false) String subject,
  182. @RequestParam(value = "adv_type_id", required = false) String advTypeId,
  183. @RequestParam(value = "fund_type_id", required = false) String fundTypeId,
  184. @RequestParam(value = "bal_amt", required = false) Double balAmt,
  185. @RequestParam(value = "file_remark", required = false) String fileRemark,
  186. @RequestParam(value = "file_data_num", required = false) String fileDataNum,
  187. @RequestParam(value = "pi_amt", required = false) Double piAmt,
  188. @RequestParam(value = "pi_file_gen_date", required = false) String piFileGenDate,
  189. @RequestParam(value = "fi_amt", required = false) String fiAmt,
  190. @RequestParam(value = "fi_file_gen_date", required = false) String fiFileGenDate,
  191. @RequestParam(value = "tc_file_gen_date", required = false) String tcFileGenDate,
  192. @RequestParam(value = "ejv_num", required = false) String ejvNum,
  193. @RequestParam(value = "ejv_num_file_gen_date", required = false) String ejvNumFileGenDate,
  194. @RequestParam(value = "as_file_gen_date", required = false) String asFileGenDate,
  195. @RequestParam(value = "as_final_file_gen_date", required = false) String asFinalFileGenDate,
  196. @RequestParam(value = "ac_code", required = false) String acCode,
  197. @RequestParam(value = "file_available_status", required = false) String fileAvailableStatus,
  198. @RequestParam(value = "rtgs_date", required = false) String rtgsDate,
  199. @RequestParam(value = "dc_completed_date", required = false) String dcCompletedDate,
  200. @RequestParam(value = "file", required = false) MultipartFile file,
  201. @RequestParam(value = "poFile", required = false) MultipartFile poFile,
  202. @RequestParam(value = "dcFile", required = false) MultipartFile dcFile,
  203. @RequestParam(value = "irFile", required = false) MultipartFile irFile,
  204. @RequestParam(value = "srFile", required = false) MultipartFile srFile,
  205. @RequestParam(value = "piFile", required = false) MultipartFile piFile,
  206. @RequestParam(value = "fiFile", required = false) MultipartFile fiFile,
  207. @RequestParam(value = "tcFile", required = false) MultipartFile tcFile,
  208. @RequestParam(value = "rtgsFile", required = false) MultipartFile rtgsFile,
  209. @RequestParam(value = "ejvFile", required = false) MultipartFile ejvFile,
  210. @RequestParam(value = "asFile", required = false) MultipartFile asFile,
  211. @RequestParam(value = "asFinalFile", required = false) MultipartFile asFinalFile,
  212. @RequestParam(value = "file_status_id", required = false) Integer fileStatusId,
  213. HttpSession session) {
  214. Map<String, Object> response = new HashMap<>();
  215. String username = (String) session.getAttribute("adj_usrName");
  216. if (username == null) {
  217. response.put("success", false);
  218. response.put("error", "Unauthorized access. Please log in.");
  219. return response;
  220. }
  221. try {
  222. Map<String, Object> existing = null;
  223. if (id != null) {
  224. existing = adjustmentService.getAdjustmentFileById(id);
  225. if (existing == null) {
  226. response.put("success", false);
  227. response.put("error", "Record not found.");
  228. return response;
  229. }
  230. }
  231. File uploadDir = new File(adjustmentUploadPathConfig).getAbsoluteFile();
  232. if (!uploadDir.exists()) {
  233. uploadDir.mkdirs();
  234. }
  235. // Save files or keep existing
  236. String filePath = saveFile(file, "file", fileDataNum, uploadDir,
  237. existing != null ? (String) existing.get("file_path") : null);
  238. String poPath = saveFile(poFile, "po", fileDataNum, uploadDir,
  239. existing != null ? (String) existing.get("po_file_path") : null);
  240. String dcPath = saveFile(dcFile, "dc", fileDataNum, uploadDir,
  241. existing != null ? (String) existing.get("dc_file_path") : null);
  242. String irPath = saveFile(irFile, "ir", fileDataNum, uploadDir,
  243. existing != null ? (String) existing.get("ir_file_path") : null);
  244. String srPath = saveFile(srFile, "sr", fileDataNum, uploadDir,
  245. existing != null ? (String) existing.get("sr_filepath") : null);
  246. String piPath = saveFile(piFile, "pi", fileDataNum, uploadDir,
  247. existing != null ? (String) existing.get("pi_file_path") : null);
  248. String fiPath = saveFile(fiFile, "fi", fileDataNum, uploadDir,
  249. existing != null ? (String) existing.get("fi_file_path") : null);
  250. String tcPath = saveFile(tcFile, "tc", fileDataNum, uploadDir,
  251. existing != null ? (String) existing.get("tc_file_path") : null);
  252. String rtgsPath = saveFile(rtgsFile, "rtgs", fileDataNum, uploadDir,
  253. existing != null ? (String) existing.get("rtgs_file_path") : null);
  254. String ejvPath = saveFile(ejvFile, "ejv", fileDataNum, uploadDir,
  255. existing != null ? (String) existing.get("ejv_num_file_path") : null);
  256. String asPath = saveFile(asFile, "as", fileDataNum, uploadDir,
  257. existing != null ? (String) existing.get("as_file_path") : null);
  258. String asFinalPath = saveFile(asFinalFile, "as_final", fileDataNum, uploadDir,
  259. existing != null ? (String) existing.get("as_final_file_path") : null);
  260. // Build data map
  261. Map<String, Object> data = new HashMap<>();
  262. if (id != null)
  263. data.put("id", id);
  264. data.put("vend_name", vendName);
  265. data.put("fin_year", finYear);
  266. data.put("subject", subject);
  267. data.put("adv_type_id", advTypeId);
  268. data.put("fund_type_id", fundTypeId);
  269. data.put("bal_amt", balAmt);
  270. data.put("file_remark", fileRemark);
  271. data.put("file_data_num", fileDataNum);
  272. data.put("pi_amt", piAmt);
  273. data.put("pi_file_gen_date", piFileGenDate);
  274. data.put("fi_amt", fiAmt);
  275. data.put("fi_file_gen_date", fiFileGenDate);
  276. data.put("tc_file_gen_date", tcFileGenDate);
  277. data.put("ejv_num", ejvNum);
  278. data.put("ejv_num_file_gen_date", ejvNumFileGenDate);
  279. data.put("as_file_gen_date", asFileGenDate);
  280. data.put("as_final_file_gen_date", asFinalFileGenDate);
  281. data.put("ac_code", acCode);
  282. data.put("file_available_status", fileAvailableStatus);
  283. data.put("rtgs_date", rtgsDate);
  284. data.put("dc_completed_date", dcCompletedDate);
  285. data.put("file_status_id", fileStatusId);
  286. // Paths
  287. data.put("file_path", filePath);
  288. data.put("po_file_path", poPath);
  289. data.put("dc_file_path", dcPath);
  290. data.put("ir_file_path", irPath);
  291. data.put("sr_filepath", srPath);
  292. data.put("pi_file_path", piPath);
  293. data.put("fi_file_path", fiPath);
  294. data.put("tc_file_path", tcPath);
  295. data.put("rtgs_file_path", rtgsPath);
  296. data.put("ejv_num_file_path", ejvPath);
  297. data.put("as_file_path", asPath);
  298. data.put("as_final_file_path", asFinalPath);
  299. boolean saved = adjustmentService.saveAdjustmentFile(data, username);
  300. if (saved) {
  301. response.put("success", true);
  302. response.put("message", "Adjustment file details saved successfully.");
  303. } else {
  304. response.put("success", false);
  305. response.put("error", "Failed to save details in database.");
  306. }
  307. } catch (Exception e) {
  308. response.put("success", false);
  309. response.put("error", "Error occurred: " + e.getMessage());
  310. e.printStackTrace();
  311. }
  312. return response;
  313. }
  314. @PostMapping("/api/delete")
  315. @ResponseBody
  316. public Map<String, Object> deleteAdjustment(
  317. @RequestParam("id") Integer id,
  318. @RequestParam(value = "remarks", required = false) String remarks,
  319. HttpSession session) {
  320. Map<String, Object> response = new HashMap<>();
  321. String username = (String) session.getAttribute("adj_usrName");
  322. if (username == null) {
  323. response.put("success", false);
  324. response.put("error", "Unauthorized access. Please log in.");
  325. return response;
  326. }
  327. boolean deleted = adjustmentService.deleteAdjustmentFile(id, remarks != null ? remarks : "Deleted by user",
  328. username);
  329. if (deleted) {
  330. response.put("success", true);
  331. response.put("message", "Record deleted successfully.");
  332. } else {
  333. response.put("success", false);
  334. response.put("error", "Failed to delete record.");
  335. }
  336. return response;
  337. }
  338. @PostMapping("/api/update-ejv")
  339. @ResponseBody
  340. public Map<String, Object> updateEJV(
  341. @RequestParam("id") Integer id,
  342. @RequestParam("ejv_num") String ejvNum,
  343. @RequestParam(value = "ejvFile", required = false) MultipartFile ejvFile,
  344. HttpSession session) {
  345. Map<String, Object> response = new HashMap<>();
  346. String username = (String) session.getAttribute("adj_usrName");
  347. if (username == null) {
  348. response.put("success", false);
  349. response.put("error", "Unauthorized access. Please log in.");
  350. return response;
  351. }
  352. try {
  353. Map<String, Object> existing = adjustmentService.getAdjustmentFileById(id);
  354. if (existing == null) {
  355. response.put("success", false);
  356. response.put("error", "Record not found.");
  357. return response;
  358. }
  359. File uploadDir = new File(adjustmentUploadPathConfig).getAbsoluteFile();
  360. if (!uploadDir.exists()) {
  361. uploadDir.mkdirs();
  362. }
  363. String fileDataNum = (String) existing.get("file_data_num");
  364. String ejvPath = saveFile(ejvFile, "ejv", fileDataNum, uploadDir,
  365. existing != null ? (String) existing.get("ejv_num_file_path") : null);
  366. boolean updated = adjustmentService.updateEJV(id, ejvNum, ejvPath, username);
  367. if (updated) {
  368. response.put("success", true);
  369. response.put("message", "EJV details updated successfully.");
  370. } else {
  371. response.put("success", false);
  372. response.put("error", "Failed to update EJV details in database.");
  373. }
  374. } catch (Exception e) {
  375. response.put("success", false);
  376. response.put("error", "Error occurred: " + e.getMessage());
  377. e.printStackTrace();
  378. }
  379. return response;
  380. }
  381. private String saveFile(MultipartFile f, String category, String fileNum, File dir, String currentPath)
  382. throws Exception {
  383. if (f == null || f.isEmpty()) {
  384. return currentPath;
  385. }
  386. // Clean fileNum
  387. String cleanFileNum = "unknown";
  388. if (fileNum != null && !fileNum.trim().isEmpty()) {
  389. // Remove illegal characters, but allow slashes / and \ for hierarchy
  390. cleanFileNum = fileNum.trim().replaceAll("[^a-zA-Z0-9.\\/\\\\_-]", "_");
  391. // Normalize path separators to forward slash /
  392. cleanFileNum = cleanFileNum.replace('\\', '/');
  393. }
  394. // Save files inside the physical "uploads" directory in the project root
  395. File projectRoot = new File(".").getAbsoluteFile();
  396. String subPath = "uploads/file-data/" + cleanFileNum + "/" + category.toUpperCase();
  397. File folder = new File(projectRoot, subPath).getAbsoluteFile();
  398. if (!folder.exists()) {
  399. folder.mkdirs();
  400. }
  401. String orig = f.getOriginalFilename();
  402. String ext = orig != null && orig.lastIndexOf('.') > 0 ? orig.substring(orig.lastIndexOf('.')) : ".pdf";
  403. String cleanName = orig != null
  404. ? orig.substring(0, orig.lastIndexOf('.')).replaceAll("[^a-zA-Z0-9.-]", "_") + ext
  405. : (category + "_" + System.currentTimeMillis() + ext);
  406. File dest = new File(folder, cleanName).getAbsoluteFile();
  407. f.transferTo(dest);
  408. return "uploads/file-data/" + cleanFileNum + "/" + category.toUpperCase() + "/" + cleanName;
  409. }
  410. }