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.

280 lines
9.3 KiB

2 years ago
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
  1. package com.gcc.communityHall.service.Impl;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.time.Instant;
  6. import java.time.temporal.ChronoUnit;
  7. import java.util.List;
  8. import java.util.Optional;
  9. import java.util.Date;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import com.gcc.communityHall.entity.BookingDetails;
  15. import com.gcc.communityHall.entity.BookingHistory;
  16. import com.gcc.communityHall.entity.BookingDetails.RefundStatus;
  17. import com.gcc.communityHall.entity.Settlement;
  18. import com.gcc.communityHall.entity.SettlementHistory;
  19. import com.gcc.communityHall.repository.BookingDetailsRepository;
  20. import com.gcc.communityHall.repository.BookingHistoryRepository;
  21. import com.gcc.communityHall.repository.SettlementHistoryRepository;
  22. import com.gcc.communityHall.repository.SettlementRepository;
  23. import com.gcc.communityHall.service.SettlementService;
  24. import java.time.Instant;
  25. import java.time.temporal.ChronoUnit;
  26. import java.util.Optional;
  27. import jakarta.servlet.http.HttpSession;
  28. @Service
  29. public class SettlementServiceImpl implements SettlementService {
  30. // ak
  31. @Autowired
  32. private SettlementHistoryRepository settlementHistoryRepository;
  33. @Autowired
  34. SettlementRepository settlementRepository;
  35. @Autowired
  36. BookingDetailsRepository bookingDetailsRepository;
  37. @Autowired
  38. private BookingHistoryRepository bookingHistoryRepository;
  39. @Autowired
  40. private HttpSession session;
  41. @Value("${file.upload.directory}")
  42. private String UploadDirectory;
  43. @Value("${file.fullpath}")
  44. private String serverPath;
  45. private final String settlementDirectory;
  46. private final String settlementPhotoUrl;
  47. @Autowired
  48. SettlementServiceImpl(SettlementRepository settlementRepository,
  49. @Value("${file.settlement.upload.directory}") String settlementDirectory,
  50. @Value("${file.settlement.fullpath}") String settlementPhotoUrl) {
  51. this.settlementDirectory = settlementDirectory;
  52. this.settlementPhotoUrl = settlementPhotoUrl;
  53. }
  54. // @Override
  55. // public Settlement getSettlementById(int settlementId) {
  56. // Optional<Settlement> settlement= settlementRepository.findById(settlementId);
  57. // if(settlement.isPresent()){
  58. // return settlement.get();
  59. // }
  60. // return null;
  61. // }
  62. // @Override
  63. // public Settlement getSettlementById(int settlementId) {
  64. // return settlementRepository.findByIdWithBookingDetails(settlementId);
  65. // }
  66. @Override
  67. public Settlement getSettlementById(int settlementId) {
  68. Optional<Settlement> settlementOptional = settlementRepository.findById(settlementId);
  69. if (settlementOptional.isPresent()) {
  70. Settlement settlement = settlementOptional.get();
  71. // Initialize the BookingDetails object
  72. settlement.getBookingDetails();
  73. // System.out.println("✅ Approved flag reset after 4 days for Settlement ID: " +
  74. // settlementId);
  75. return settlement;
  76. }
  77. return null;
  78. }
  79. // ak
  80. /*
  81. * @Override
  82. * public Settlement getSettlementById(int settlementId) {
  83. * Optional<Settlement> settlementOptional =
  84. * settlementRepository.findById(settlementId);
  85. *
  86. * if (settlementOptional.isPresent()) {
  87. * Settlement settlement = settlementOptional.get();
  88. *
  89. * // Initialize BookingDetails
  90. * settlement.getBookingDetails();
  91. * System.out.println("settlement.getBookingDetails(): " +
  92. * settlement.getBookingDetails());
  93. *
  94. * // ✅ Auto-reset logic: if approved for 4+ days, reset flag
  95. * if (settlement.getApprovedFlag() == 1 && settlement.getApprovedFlagDate() !=
  96. * null) {
  97. * long millisDiff = System.currentTimeMillis() -
  98. * settlement.getApprovedFlagDate().getTime();
  99. * long daysDiff = millisDiff / (1000 * 60 * 60 * 24);
  100. * System.out.println("Approved Flag Date: " +
  101. * settlement.getApprovedFlagDate());
  102. * System.out.println("Current Java Date: " + new Date());
  103. * System.out.println("Days Difference: " + daysDiff);
  104. *
  105. *
  106. * if (daysDiff >= 4) {
  107. * settlement.setApprovedFlag(0);
  108. * settlement.setApprovedFlagDate(null);
  109. * settlementRepository.save(settlement);
  110. * System.out.println("✅ Approved flag reset after 4 days for Settlement ID: " +
  111. * settlementId);
  112. * }
  113. * }
  114. *
  115. * return settlement;
  116. * }
  117. *
  118. * return null;
  119. * }
  120. */
  121. @Override
  122. public List<Settlement> getSettlementListWithWaitingRefund() {
  123. return settlementRepository.findByBookingDetailsRefundStatus(RefundStatus.WAITING);
  124. }
  125. @Override
  126. public Settlement updateSettlementStatus(int settlementId, BookingDetails.RefundStatus status, String comments) {
  127. Optional<Settlement> settlementOptional = settlementRepository.findById(settlementId);
  128. String adminname = (String) session.getAttribute("usertype");
  129. if (settlementOptional.isPresent()) {
  130. Settlement settlement = settlementOptional.get();
  131. System.out.println("settlement@12345" + settlement);
  132. BookingDetails bookingDetails = settlement.getBookingDetails(); // Fetch associated BookingDetails
  133. // Update BookingDetails with new RefundStatus
  134. // bookingDetails.setRefundStatus(status);
  135. // If the refund status is COMPLETED, update the BookingStatus to COMPLETED
  136. // if (status == BookingDetails.RefundStatus.COMPLETED) {
  137. // bookingDetails.setBookingStatus(BookingDetails.BookingStatus.COMPLETED);
  138. // }else {
  139. // bookingDetails.setBookingStatus(BookingDetails.BookingStatus.APPROVED);
  140. // }
  141. // to maintain history for settlement
  142. bookingDetailsRepository.save(bookingDetails);
  143. //ak
  144. //settlement histoty insert
  145. if (settlementOptional.isPresent()) {
  146. SettlementHistory history = new SettlementHistory();
  147. history.setSettlementId(settlement.getSettlementId());
  148. history.setComments(settlement.getComments());
  149. history.setCdate(settlement.getCdate());
  150. history.setDamageAmount(settlement.getDamageAmount());
  151. history.setEbAmount(settlement.getEbAmount());
  152. history.setDepositAmount(settlement.getDepositAmount());
  153. history.setRefundedAmount(settlement.getRefundedAmount());
  154. history.setUpdatedDate(new Date());
  155. history.setUpdatedId(adminname);
  156. history.setChallon(settlement.getChallon());
  157. history.setDddate(settlement.getDddate());
  158. history.setDdnumber(settlement.getDdnumber());
  159. history.setBookingDetails(settlement.getBookingDetails());
  160. history.setCollectionPending(settlement.getCollectionPending());
  161. history.setDdamount(settlement.getDdamount());
  162. history.setChallonnumber(settlement.getChallonnumber());
  163. history.setCollectionStatus(settlement.getCollectionStatus());
  164. history.setChallonamount(settlement.getChallonamount());
  165. history.setApprovedFlag(settlement.getApprovedFlag());
  166. history.setApprovedFlagDate(settlement.getApprovedFlagDate());
  167. history.setShDate(new Date());
  168. settlementHistoryRepository.save(history);
  169. System.out.println("✅ Record inserted into SettlementHistory for Settlement ID: " + settlementId);
  170. }
  171. // checking ak
  172. //settlement.setApprovedFlag(1);
  173. //settlement.setApprovedFlagDate(new Date());
  174. settlement.setUpdatedDate(new Date());
  175. // Update Settlement comments
  176. settlement.setComments(comments);
  177. // Save and return updated Settlement
  178. return settlementRepository.save(settlement);
  179. }
  180. return null;
  181. }
  182. // ak
  183. @Override
  184. public Settlement updateRejectSettlementStatus(int settlementId, BookingDetails.RefundStatus status,
  185. String comments) {
  186. Optional<Settlement> settlementOptional = settlementRepository.findById(settlementId);
  187. if (settlementOptional.isPresent()) {
  188. Settlement settlement = settlementOptional.get();
  189. BookingDetails bookingDetails = settlement.getBookingDetails(); // Fetch associated BookingDetails
  190. bookingDetails.setRefundStatus(status);
  191. bookingDetailsRepository.save(bookingDetails);
  192. // Update Settlement comments
  193. settlement.setComments(comments);
  194. settlement.setUpdatedDate(new Date());
  195. // Save and return updated Settlement
  196. return settlementRepository.save(settlement);
  197. }
  198. return null;
  199. }
  200. @Override
  201. public Settlement settlement(Settlement settlement) {
  202. // TODO Auto-generated method stub
  203. return null;
  204. }
  205. @Override
  206. public Settlement uploadChallonDetails(int settlementId, long ddNumber, String ddDate, long ddamount,
  207. MultipartFile challonimage, long challonnumber, long challonamount) {
  208. Optional<Settlement> settlementOptional = settlementRepository.findById(settlementId);
  209. if (settlementOptional.isPresent()) {
  210. Settlement updateSettlement = settlementOptional.get();
  211. updateSettlement.setDdnumber(ddNumber);
  212. updateSettlement.setDddate(ddDate);
  213. updateSettlement.setChallonamount(challonamount);
  214. updateSettlement.setChallonnumber(challonnumber);
  215. updateSettlement.setDdamount(ddamount);
  216. if (challonimage != null && !challonimage.isEmpty()) {
  217. String fileName = settlementId + "_" + challonimage.getOriginalFilename();
  218. String filePath = settlementDirectory + "/" + fileName;
  219. // Save the file to the specified directory
  220. try (FileOutputStream fos = new FileOutputStream(filePath)) {
  221. byte[] bytes = challonimage.getBytes();
  222. fos.write(bytes);
  223. updateSettlement.setChallon(settlementPhotoUrl + "challonDetails/" + fileName);
  224. } catch (Exception e) {
  225. throw new RuntimeException("Error saving challon image", e);
  226. }
  227. }
  228. return settlementRepository.save(updateSettlement);
  229. } else {
  230. return null;
  231. }
  232. }
  233. }