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.

466 lines
18 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package com.gcc_manpower.gccservicepaymenttracker.controller;
  2. import com.gcc_manpower.gccservicepaymenttracker.service.GccServicePaymentTrackerService;
  3. import com.gcc_manpower.service.AppUserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.Model;
  8. import org.springframework.web.bind.annotation.*;
  9. import org.springframework.web.multipart.MultipartFile;
  10. import jakarta.servlet.http.HttpSession;
  11. import jakarta.servlet.http.HttpServletRequest;
  12. import org.springframework.http.CacheControl;
  13. import org.springframework.http.ResponseEntity;
  14. import java.io.File;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18. import org.springframework.security.authentication.AuthenticationManager;
  19. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  20. import org.springframework.security.core.Authentication;
  21. import org.springframework.security.core.AuthenticationException;
  22. import org.springframework.security.core.context.SecurityContextHolder;
  23. import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
  24. @Controller
  25. @RequestMapping("/gccservicepaymenttracker")
  26. public class GccServicePaymentTrackerController {
  27. @Autowired
  28. private GccServicePaymentTrackerService paymentTrackerService;
  29. @Autowired
  30. private AppUserService appUserService;
  31. @Autowired
  32. private AuthenticationManager authenticationManager;
  33. @Value("${payment.tracker.upload.path:gccpaymenttrackerfiles}")
  34. private String paymentTrackerUploadPathConfig;
  35. @GetMapping({ "", "/", "/dashboard" })
  36. public String viewDashboard(Model model, HttpSession session) {
  37. String username = (String) session.getAttribute("pay_usrName");
  38. if (username == null) {
  39. return "redirect:/gccservicepaymenttracker/login";
  40. }
  41. String usrRole = (String) session.getAttribute("pay_usrRole");
  42. boolean isAdmin = "ADMIN".equalsIgnoreCase(usrRole);
  43. List<Map<String, Object>> vendorDetails = appUserService.getVendorDetailsWithCounts();
  44. model.addAttribute("isLoggedIn", true);
  45. model.addAttribute("isAdmin", isAdmin);
  46. model.addAttribute("usrName", username);
  47. model.addAttribute("curPage", "gccservicepaymenttracker");
  48. model.addAttribute("vendorDetails", vendorDetails);
  49. return "gccservicepaymenttracker/dashboard";
  50. }
  51. @GetMapping("/details")
  52. public String viewDetails(HttpSession session, Model model) {
  53. String username = (String) session.getAttribute("pay_usrName");
  54. if (username == null) {
  55. return "redirect:/gccservicepaymenttracker/login";
  56. }
  57. String usrRole = (String) session.getAttribute("pay_usrRole");
  58. boolean isAdmin = "ADMIN".equalsIgnoreCase(usrRole);
  59. model.addAttribute("isLoggedIn", true);
  60. model.addAttribute("isAdmin", isAdmin);
  61. model.addAttribute("usrName", username);
  62. model.addAttribute("curPage", "gccservicepaymenttracker");
  63. return "gccservicepaymenttracker/details";
  64. }
  65. @GetMapping("/login")
  66. public String login(HttpSession session) {
  67. return "gccservicepaymenttracker/login";
  68. }
  69. @PostMapping("/api/login")
  70. @ResponseBody
  71. public Map<String, Object> validateLogin(
  72. @RequestParam("usrName") String usrName,
  73. @RequestParam("usrPass") String usrPass,
  74. HttpSession session) {
  75. Map<String, Object> response = new HashMap<>();
  76. try {
  77. UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(usrName,
  78. usrPass);
  79. Authentication authentication = authenticationManager.authenticate(authenticationToken);
  80. SecurityContextHolder.getContext().setAuthentication(authentication);
  81. session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
  82. SecurityContextHolder.getContext());
  83. String role = authentication.getAuthorities().stream()
  84. .map(auth -> auth.getAuthority().replace("ROLE_", ""))
  85. .findFirst()
  86. .orElse("USER");
  87. session.setAttribute("pay_usrName", usrName);
  88. session.setAttribute("pay_usrRole", role);
  89. session.setMaxInactiveInterval(1800); // 30 minutes session timeout
  90. response.put("result", true);
  91. response.put("usrName", usrName);
  92. } catch (AuthenticationException e) {
  93. response.put("result", false);
  94. response.put("error", e.getMessage() != null ? e.getMessage() : "Invalid username or password!");
  95. }
  96. return response;
  97. }
  98. @PostMapping("/logout")
  99. public String logout(HttpSession session, HttpServletRequest request) {
  100. SecurityContextHolder.clearContext();
  101. if (session != null) {
  102. session.removeAttribute("pay_usrName");
  103. session.removeAttribute("pay_usrRole");
  104. session.invalidate();
  105. }
  106. request.getSession(true);
  107. return "redirect:/gccservicepaymenttracker/login";
  108. }
  109. @GetMapping("/logout")
  110. public String logoutGet(HttpSession session, HttpServletRequest request) {
  111. SecurityContextHolder.clearContext();
  112. if (session != null) {
  113. session.removeAttribute("pay_usrName");
  114. session.removeAttribute("pay_usrRole");
  115. session.invalidate();
  116. }
  117. request.getSession(true);
  118. return "redirect:/gccservicepaymenttracker/login";
  119. }
  120. @GetMapping("/api/counts")
  121. @ResponseBody
  122. public ResponseEntity<Map<String, Integer>> getCounts() {
  123. return ResponseEntity.ok()
  124. .cacheControl(CacheControl.noCache().mustRevalidate())
  125. .body(paymentTrackerService.getCountsByCategory());
  126. }
  127. @GetMapping("/api/list")
  128. @ResponseBody
  129. public ResponseEntity<List<Map<String, Object>>> listDetails(
  130. @RequestParam(value = "category", required = false) String category,
  131. @RequestParam(value = "fromDate", required = false) String fromDate,
  132. @RequestParam(value = "toDate", required = false) String toDate,
  133. @RequestParam(value = "status", required = false) String status) {
  134. List<Map<String, Object>> details = paymentTrackerService.searchDetails(category, fromDate, toDate, status);
  135. return ResponseEntity.ok()
  136. .cacheControl(CacheControl.noCache().mustRevalidate())
  137. .body(details);
  138. }
  139. @PostMapping("/api/save")
  140. @ResponseBody
  141. public Map<String, Object> saveDetail(
  142. @RequestBody Map<String, Object> payload,
  143. HttpSession session) {
  144. Map<String, Object> response = new HashMap<>();
  145. String username = (String) session.getAttribute("pay_usrName");
  146. if (username == null) {
  147. username = (String) session.getAttribute("usrName");
  148. }
  149. if (username == null) {
  150. response.put("success", false);
  151. response.put("error", "Unauthorized access! Please log in.");
  152. return response;
  153. }
  154. String usrRole = (String) session.getAttribute("pay_usrRole");
  155. if (usrRole == null) {
  156. usrRole = (String) session.getAttribute("usrRole");
  157. }
  158. if (!"ADMIN".equalsIgnoreCase(usrRole)) {
  159. response.put("success", false);
  160. response.put("error", "Access denied! Only admins can save or update records.");
  161. return response;
  162. }
  163. try {
  164. boolean saved = paymentTrackerService.saveOrUpdateDetail(payload, username);
  165. response.put("success", saved);
  166. if (!saved) {
  167. response.put("error", "Failed to save details to database.");
  168. }
  169. } catch (Exception e) {
  170. response.put("success", false);
  171. response.put("error", "Error: " + e.getMessage());
  172. }
  173. return response;
  174. }
  175. @PostMapping("/api/delete")
  176. @ResponseBody
  177. public Map<String, Object> deleteDetail(
  178. @RequestParam("id") int id,
  179. @RequestParam(value = "remarks", required = false, defaultValue = "") String remarks,
  180. HttpSession session) {
  181. Map<String, Object> response = new HashMap<>();
  182. String username = (String) session.getAttribute("pay_usrName");
  183. if (username == null) {
  184. username = (String) session.getAttribute("usrName");
  185. }
  186. if (username == null) {
  187. response.put("success", false);
  188. response.put("error", "Unauthorized access! Please log in.");
  189. return response;
  190. }
  191. String usrRole = (String) session.getAttribute("pay_usrRole");
  192. if (usrRole == null) {
  193. usrRole = (String) session.getAttribute("usrRole");
  194. }
  195. if (!"ADMIN".equalsIgnoreCase(usrRole)) {
  196. response.put("success", false);
  197. response.put("error", "Access denied! Only admins can delete records.");
  198. return response;
  199. }
  200. try {
  201. boolean deleted = paymentTrackerService.deleteDetail(id, remarks, username);
  202. response.put("success", deleted);
  203. if (!deleted) {
  204. response.put("error", "Failed to delete record.");
  205. }
  206. } catch (Exception e) {
  207. response.put("success", false);
  208. response.put("error", "Error: " + e.getMessage());
  209. }
  210. return response;
  211. }
  212. @PostMapping("/api/upload-pdf")
  213. @ResponseBody
  214. public Map<String, Object> uploadPdf(
  215. @RequestParam("file") MultipartFile file,
  216. HttpSession session) {
  217. Map<String, Object> response = new HashMap<>();
  218. String username = (String) session.getAttribute("pay_usrName");
  219. if (username == null) {
  220. username = (String) session.getAttribute("usrName");
  221. }
  222. if (username == null) {
  223. response.put("success", false);
  224. response.put("error", "Unauthorized access! Please log in.");
  225. return response;
  226. }
  227. String usrRole = (String) session.getAttribute("pay_usrRole");
  228. if (usrRole == null) {
  229. usrRole = (String) session.getAttribute("usrRole");
  230. }
  231. if (!"ADMIN".equalsIgnoreCase(usrRole)) {
  232. response.put("success", false);
  233. response.put("error", "Access denied! Only admins can upload files.");
  234. return response;
  235. }
  236. if (file.isEmpty()) {
  237. response.put("success", false);
  238. response.put("error", "Please select a file to upload.");
  239. return response;
  240. }
  241. try {
  242. File uploadDir = new File(paymentTrackerUploadPathConfig).getAbsoluteFile();
  243. if (!uploadDir.exists()) {
  244. uploadDir.mkdirs();
  245. }
  246. String originalFilename = file.getOriginalFilename();
  247. String nameWithoutExt = "pdf";
  248. String ext = ".pdf";
  249. if (originalFilename != null && originalFilename.contains(".")) {
  250. int dotIdx = originalFilename.lastIndexOf(".");
  251. nameWithoutExt = originalFilename.substring(0, dotIdx);
  252. ext = originalFilename.substring(dotIdx);
  253. }
  254. nameWithoutExt = nameWithoutExt.replaceAll("[^a-zA-Z0-9-_]", "_");
  255. String savedFilename = nameWithoutExt + "_" + System.currentTimeMillis() + ext;
  256. File destFile = new File(uploadDir, savedFilename).getAbsoluteFile();
  257. file.transferTo(destFile);
  258. String fileWebUrl = "/gccpaymenttrackerfiles/" + savedFilename;
  259. response.put("success", true);
  260. response.put("filePath", fileWebUrl);
  261. } catch (Exception e) {
  262. response.put("success", false);
  263. response.put("error", "File upload failed: " + e.getMessage());
  264. }
  265. return response;
  266. }
  267. @PostMapping("/api/upload-screenshot")
  268. @ResponseBody
  269. public Map<String, Object> uploadScreenshot(
  270. @RequestParam("file") MultipartFile file,
  271. HttpSession session) {
  272. Map<String, Object> response = new HashMap<>();
  273. String username = (String) session.getAttribute("pay_usrName");
  274. if (username == null) {
  275. username = (String) session.getAttribute("usrName");
  276. }
  277. if (username == null) {
  278. response.put("success", false);
  279. response.put("error", "Unauthorized access! Please log in.");
  280. return response;
  281. }
  282. String usrRole = (String) session.getAttribute("pay_usrRole");
  283. if (usrRole == null) {
  284. usrRole = (String) session.getAttribute("usrRole");
  285. }
  286. if (!"ADMIN".equalsIgnoreCase(usrRole)) {
  287. response.put("success", false);
  288. response.put("error", "Access denied! Only admins can upload files.");
  289. return response;
  290. }
  291. if (file.isEmpty()) {
  292. response.put("success", false);
  293. response.put("error", "Please select a file to upload.");
  294. return response;
  295. }
  296. try {
  297. File parentDir = new File(paymentTrackerUploadPathConfig).getAbsoluteFile();
  298. File screenshotDir = new File(parentDir, "screenshots");
  299. if (!screenshotDir.exists()) {
  300. screenshotDir.mkdirs();
  301. }
  302. String originalFilename = file.getOriginalFilename();
  303. String nameWithoutExt = "screenshot";
  304. String ext = ".png";
  305. if (originalFilename != null && originalFilename.contains(".")) {
  306. int dotIdx = originalFilename.lastIndexOf(".");
  307. nameWithoutExt = originalFilename.substring(0, dotIdx);
  308. ext = originalFilename.substring(dotIdx);
  309. }
  310. nameWithoutExt = nameWithoutExt.replaceAll("[^a-zA-Z0-9-_]", "_");
  311. String savedFilename = nameWithoutExt + "_" + System.currentTimeMillis() + ext;
  312. File destFile = new File(screenshotDir, savedFilename).getAbsoluteFile();
  313. file.transferTo(destFile);
  314. String fileWebUrl = "/gccpaymenttrackerfiles/screenshots/" + savedFilename;
  315. response.put("success", true);
  316. response.put("filePath", fileWebUrl);
  317. } catch (Exception e) {
  318. response.put("success", false);
  319. response.put("error", "Screenshot upload failed: " + e.getMessage());
  320. }
  321. return response;
  322. }
  323. @GetMapping("/api/types")
  324. @ResponseBody
  325. public List<Map<String, Object>> getActiveTypes() {
  326. return paymentTrackerService.getActiveTypes();
  327. }
  328. @GetMapping("/api/vendors")
  329. @ResponseBody
  330. public List<Map<String, Object>> getActiveVendors() {
  331. return paymentTrackerService.getActiveVendors();
  332. }
  333. @GetMapping("/api/statuslist")
  334. @ResponseBody
  335. public List<Map<String, Object>> getStatusList() {
  336. return paymentTrackerService.getStatusList();
  337. }
  338. @GetMapping("/api/history")
  339. @ResponseBody
  340. public ResponseEntity<List<Map<String, Object>>> getHistory(
  341. @RequestParam("id") int id,
  342. HttpSession session) {
  343. String username = (String) session.getAttribute("pay_usrName");
  344. if (username == null) {
  345. username = (String) session.getAttribute("usrName");
  346. }
  347. if (username == null) {
  348. return ResponseEntity.status(401).body(null);
  349. }
  350. return ResponseEntity.ok()
  351. .cacheControl(CacheControl.noCache().mustRevalidate())
  352. .body(paymentTrackerService.getHistory(id));
  353. }
  354. @GetMapping("/api/deleted-list")
  355. @ResponseBody
  356. public ResponseEntity<List<Map<String, Object>>> getDeletedList(HttpSession session) {
  357. String username = (String) session.getAttribute("pay_usrName");
  358. if (username == null) {
  359. username = (String) session.getAttribute("usrName");
  360. }
  361. if (username == null) {
  362. return ResponseEntity.status(401).body(null);
  363. }
  364. return ResponseEntity.ok()
  365. .cacheControl(CacheControl.noCache().mustRevalidate())
  366. .body(paymentTrackerService.getDeletedDetails());
  367. }
  368. @PostMapping("/api/restore")
  369. @ResponseBody
  370. public Map<String, Object> restoreDetail(
  371. @RequestParam("id") int id,
  372. HttpSession session) {
  373. Map<String, Object> response = new HashMap<>();
  374. String username = (String) session.getAttribute("pay_usrName");
  375. if (username == null) {
  376. username = (String) session.getAttribute("usrName");
  377. }
  378. if (username == null) {
  379. response.put("success", false);
  380. response.put("error", "Unauthorized access! Please log in.");
  381. return response;
  382. }
  383. String usrRole = (String) session.getAttribute("pay_usrRole");
  384. if (usrRole == null) {
  385. usrRole = (String) session.getAttribute("usrRole");
  386. }
  387. if (!"ADMIN".equalsIgnoreCase(usrRole)) {
  388. response.put("success", false);
  389. response.put("error", "Access denied! Only admins can restore records.");
  390. return response;
  391. }
  392. try {
  393. boolean restored = paymentTrackerService.restoreDetail(id, username);
  394. response.put("success", restored);
  395. if (!restored) {
  396. response.put("error", "Failed to restore record.");
  397. }
  398. } catch (Exception e) {
  399. response.put("success", false);
  400. response.put("error", "Error: " + e.getMessage());
  401. }
  402. return response;
  403. }
  404. }