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.
864 lines
45 KiB
864 lines
45 KiB
|
|
package com.gcc.restapiservices.communityHall.service;
|
|
|
|
import com.gcc.restapiservices.communityHall.configuration.PaymentConfig;
|
|
import com.gcc.restapiservices.communityHall.util.TransactionNumberGenerator;
|
|
import com.gcc.restapiservices.configuration.AppConfig;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.core.env.Environment;
|
|
import org.springframework.dao.DataAccessException;
|
|
import org.springframework.dao.EmptyResultDataAccessException;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
|
import org.springframework.jdbc.support.KeyHolder;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.util.Random;
|
|
|
|
import com.gcc.restapiservices.configuration.AppConfig;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.servlet.http.HttpSession;
|
|
|
|
import javax.sql.DataSource;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Random;
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.math.BigInteger;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.SecureRandom;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.Statement;
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
|
|
@Service
|
|
|
|
public class communityHallService {
|
|
private JdbcTemplate jdbcTemplate;
|
|
@Autowired
|
|
private AppConfig appConfig;
|
|
@Autowired
|
|
private Environment environment;
|
|
|
|
@Autowired
|
|
private PaymentConfig paymentConfig;
|
|
|
|
@Autowired
|
|
private TransactionNumberGenerator transactionNumberGenerator;
|
|
|
|
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
private static final int STRING_LENGTH = 15;
|
|
private static final Random RANDOM = new SecureRandom();
|
|
|
|
@Value("${file.upload.directory.communityhall}")
|
|
private String uploadDirectory;
|
|
|
|
@Value("${file.fullpath.communityhall}")
|
|
private String serverPath;
|
|
|
|
@Autowired
|
|
public void setDataSource(@Qualifier("mysqlCommunityHallDataSource") DataSource dataSource) {
|
|
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
|
}
|
|
|
|
public List<Map<String, Object>> getHallList() {
|
|
return jdbcTemplate.queryForList("SELECT * FROM hall_details WHERE is_active = 1 order by hall_name asc");
|
|
}
|
|
|
|
public List<Map<String, Object>> getReasonMaster() {
|
|
String sql = "SELECT reason_id,reason,maxdays,reasoncode FROM reason_master";
|
|
return jdbcTemplate.queryForList(sql);
|
|
}
|
|
|
|
public List<Map<String, Object>> getDocumentsByReasonId(Long reasonId) {
|
|
String sql = "SELECT doc_field_name,field_name FROM reason_document_master WHERE reason_id = ?";
|
|
return jdbcTemplate.queryForList(sql, reasonId);
|
|
}
|
|
|
|
public List<Map<String, String>> getRelationTypesAsMap() {
|
|
List<Map<String, String>> relationList = new ArrayList<>();
|
|
|
|
String[] relations = {"Self", "Son", "Daughter", "Granddaughter", "Grandson", "Brother", "Sister"};
|
|
|
|
for (String rel : relations) {
|
|
Map<String, String> map = new HashMap<>();
|
|
map.put("relation_name", rel);
|
|
map.put("value", rel);
|
|
relationList.add(map);
|
|
}
|
|
|
|
return relationList;
|
|
}
|
|
|
|
public List<Map<String, Object>> getUserDetails(Long Mobilenumber) {
|
|
String sql = "SELECT name,email,mobile_no,doorno,area,location,pin_code,user_status,usertype,profilephoto FROM users WHERE mobile_no = ?";
|
|
return jdbcTemplate.queryForList(sql, Mobilenumber);
|
|
}
|
|
|
|
public Map<String, Object> getHallDetailsById(int hallId) {
|
|
String sql = "SELECT * FROM hall_details WHERE hall_id = ?";
|
|
List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, hallId);
|
|
|
|
if (result.isEmpty()) {
|
|
return null; // or Collections.emptyMap() if you prefer
|
|
}
|
|
|
|
return result.get(0);
|
|
}
|
|
|
|
public Map<String, Object> getHallConcessionByHallId(int hallId) {
|
|
String sql = "SELECT * FROM hall_details_concession WHERE hall_id = ?";
|
|
List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, hallId);
|
|
|
|
if (result.isEmpty()) {
|
|
return null; // or Collections.emptyMap()
|
|
}
|
|
|
|
return result.get(0); // ✅ safe return of first result
|
|
}
|
|
|
|
public List<Map<String, Object>> checkMobileExits(Long Mobilenumber) {
|
|
String sql = "SELECT * FROM users WHERE mobile_no = ?";
|
|
return jdbcTemplate.queryForList(sql, Mobilenumber);
|
|
}
|
|
|
|
public List<Map<String, Object>> checkEmployeeExits(String EmployeeID) {
|
|
String sql = "SELECT * FROM users WHERE government_id= ?";
|
|
return jdbcTemplate.queryForList(sql, EmployeeID);
|
|
}
|
|
|
|
/*
|
|
* public Map<String, Object> getBookingDetailsById(int bookingId) { String sql
|
|
* = "SELECT " +
|
|
* "bd.booking_id, bd.booking_date, bd.start_date, bd.end_date, bd.comments, bd.booking_status, "
|
|
* +
|
|
* "bd.starting_slot, bd.ending_slot, bd.no_of_days, bd.reason, bd.gst, bd.deposit_amount, "
|
|
* + "bd.hall_amount, bd.amount_received AS booking_amount_received, " +
|
|
* "u.name, u.usertype, u.mobile_no, u.email, " +
|
|
* "hd.zone, hd.division, hd.hall_incharge_name, hd.hall_incharge_no, hd.hall_name, "
|
|
* + "CONCAT_WS(', ', hd.street, hd.area, hd.pin_code) AS address, " +
|
|
* "p.transaction_id, p.payment_status, p.amount_received AS payment_amount_received "
|
|
* + "FROM booking_details bd " +
|
|
* "INNER JOIN users u ON bd.users_user_id = u.user_id " +
|
|
* "INNER JOIN hall_details hd ON bd.hall_details_hall_id = hd.hall_id " +
|
|
* "INNER JOIN payments p ON bd.booking_id = p.booking_details_booking_id " +
|
|
* "WHERE bd.booking_id = ?";
|
|
*
|
|
* return jdbcTemplate.queryForMap(sql, bookingId); }
|
|
*/
|
|
public Map<String, Object> getBookingDetailsById(int bookingId) {
|
|
String sql = "SELECT "
|
|
+ "bd.booking_id, DATE_FORMAT(bd.booking_date, '%d-%m-%Y') AS booking_date, "
|
|
+ " DATE_FORMAT(bd.start_date, '%d-%m-%Y') AS start_date, "
|
|
+ " DATE_FORMAT(bd.end_date, '%d-%m-%Y') AS end_date, "
|
|
+ " bd.comments, bd.booking_status, "
|
|
+ "bd.starting_slot, bd.ending_slot, bd.no_of_days, bd.reason, bd.gst, bd.deposit_amount, "
|
|
+ "bd.hall_amount, bd.amount_received AS total_amount, " + "u.name, u.usertype, u.mobile_no, u.email, "
|
|
+ "hd.zone, hd.division, hd.hall_incharge_name as caretaker_name, hd.hall_incharge_no as caretaker_mobileno, hd.hall_name, "
|
|
+ "CONCAT_WS(', ', hd.street, hd.area, hd.pin_code) AS hall_address, "
|
|
+ "p.transaction_id, p.payment_status, p.amount_received AS payment_amount_received "
|
|
+ "FROM booking_details bd " + "INNER JOIN users u ON bd.users_user_id = u.user_id "
|
|
+ "INNER JOIN hall_details hd ON bd.hall_details_hall_id = hd.hall_id "
|
|
+ "INNER JOIN payments p ON bd.booking_id = p.booking_details_booking_id " + "WHERE bd.booking_id = ?";
|
|
|
|
Map<String, Object> result = jdbcTemplate.queryForMap(sql, bookingId);
|
|
|
|
// Add the acknowledgement URL to the result
|
|
String acknowledgementUrl = "https://gccservices.chennaicorporation.gov.in/communityhall/user/acknowledgement?bookingId=" + bookingId;
|
|
result.put("acknowledgement_url", acknowledgementUrl);
|
|
|
|
return result;
|
|
}
|
|
|
|
// pdf
|
|
|
|
/*
|
|
* public List<Map<String, Object>> getBookingsByMobile(Long mobileNumber) {
|
|
* String sql =
|
|
* "SELECT bd.booking_id, bd.booking_date, bd.start_date, bd.end_date, " +
|
|
* "bd.starting_slot, bd.ending_slot, bd.reason, bd.booking_status, " +
|
|
* "p.payment_status AS payment_status " +
|
|
* "FROM testcommunityhall.booking_details bd " +
|
|
* "INNER JOIN testcommunityhall.users u ON bd.users_user_id = u.user_id " +
|
|
* "INNER JOIN testcommunityhall.payments p ON bd.booking_id = p.booking_details_booking_id "
|
|
* + "WHERE u.mobile_no = ? " + "AND bd.booking_status NOT IN ('PENDING')";
|
|
*
|
|
* return jdbcTemplate.queryForList(sql, mobileNumber); }
|
|
*/
|
|
public List<Map<String, Object>> getBookingsByMobile(Long mobileNumber) {
|
|
String sql = "SELECT bd.booking_id , DATE_FORMAT(bd.booking_date, '%d-%m-%Y') AS booking_date,"
|
|
+ " DATE_FORMAT(bd.start_date, '%d-%m-%Y') AS start_date,"
|
|
+ " DATE_FORMAT(bd.end_date, '%d-%m-%Y') AS end_date, "
|
|
+ "bd.starting_slot, bd.ending_slot, bd.reason, bd.booking_status, "
|
|
+ "p.payment_status AS payment_status " + "FROM booking_details bd "
|
|
+ "INNER JOIN users u ON bd.users_user_id = u.user_id "
|
|
+ "INNER JOIN payments p ON bd.booking_id = p.booking_details_booking_id "
|
|
+ "WHERE u.mobile_no = ? " + "AND bd.booking_status NOT IN ('PENDING')";
|
|
|
|
List<Map<String, Object>> bookings = jdbcTemplate.queryForList(sql, mobileNumber);
|
|
|
|
// Add acknowledgement URL to each booking
|
|
for (Map<String, Object> booking : bookings) {
|
|
Object bookingId = booking.get("booking_id");
|
|
if (bookingId != null) {
|
|
String acknowledgementUrl = "https://gccservices.chennaicorporation.gov.in/communityhall/user/acknowledgement?bookingId="
|
|
+ bookingId;
|
|
booking.put("acknowledgement_url", acknowledgementUrl);
|
|
}
|
|
}
|
|
|
|
return bookings;
|
|
}
|
|
|
|
public List<Map<String, Object>> checkPenionerExits(String PensionerID) {
|
|
String sql = "SELECT * FROM users WHERE government_id= ?";
|
|
return jdbcTemplate.queryForList(sql, PensionerID);
|
|
}
|
|
|
|
//
|
|
public List<Map<String, Object>> findCounsellorByMobile(long mobileNo) {
|
|
String sql = "SELECT * FROM counsellor_details WHERE mobile1 = ? OR mobile2 = ?";
|
|
return jdbcTemplate.queryForList(sql, mobileNo, mobileNo);
|
|
}
|
|
|
|
public void registerUser(String userType, String name, long mobileNumber, String aadharNumber,
|
|
MultipartFile profilePhoto, String emailId, String doorNo, String street, String area, String location,
|
|
String pincode, String pensionerID, String pensionerDepartment, String employeeID,
|
|
String employeeDepartment) throws IOException {
|
|
|
|
String uploadPhotoUrl = null;
|
|
if (profilePhoto != null && !profilePhoto.isEmpty()) {
|
|
uploadPhotoUrl = saveFile(profilePhoto);
|
|
}
|
|
|
|
// Default values for government_id and department
|
|
String governmentId = null;
|
|
String department = null;
|
|
|
|
String userstatus = "APPROVED";
|
|
|
|
// Logic for conditional insertion based on userType
|
|
if ("GCC PENSIONER".equalsIgnoreCase(userType)) {
|
|
|
|
governmentId = pensionerID;
|
|
department = pensionerDepartment;
|
|
userstatus = "PENDING";
|
|
} else if ("GCC EMPLOYEE".equalsIgnoreCase(userType)) {
|
|
governmentId = employeeID;
|
|
department = employeeDepartment;
|
|
userstatus = "PENDING";
|
|
}
|
|
|
|
String sql = "INSERT INTO users (usertype, name, mobile_no, aadhar_number, profilephoto, email, "
|
|
+ "doorno, street, area, location, pin_code, government_id, department,user_status) "
|
|
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
|
|
|
|
jdbcTemplate.update(sql, userType, name, mobileNumber, aadharNumber, uploadPhotoUrl, emailId, doorNo, street,
|
|
area, location, pincode, governmentId, department, userstatus);
|
|
}
|
|
|
|
public String saveFile(MultipartFile file) throws IOException {
|
|
String order_copy_url = "";
|
|
|
|
// Handle file upload if a file is provided
|
|
if (file == null || file.isEmpty()) {
|
|
return order_copy_url;
|
|
}
|
|
|
|
// Get the base upload directory from properties
|
|
String uploadDirectory = environment.getProperty("file.upload.directory"); // e.g., /opt/myapp/uploads
|
|
|
|
// Define the subdirectory structure
|
|
String serviceFolderName = "images/userPhoto"; // You can also get from properties if needed
|
|
|
|
// Build the full folder path
|
|
String fullUploadPath = uploadDirectory + File.separator + serviceFolderName;
|
|
Path directoryPath = Paths.get(fullUploadPath);
|
|
if (!Files.exists(directoryPath)) {
|
|
Files.createDirectories(directoryPath);
|
|
}
|
|
|
|
// Generate unique file name
|
|
// String datetimetxt = DateTimeUtil.getCurrentDateTime();
|
|
String fileName = generateRandomStringForFile(10) + "_" + file.getOriginalFilename();
|
|
|
|
// Physical file path
|
|
Path filePath = directoryPath.resolve(fileName);
|
|
|
|
// Write the file to the physical path
|
|
Files.write(filePath, file.getBytes());
|
|
|
|
// Relative path for storing in DB
|
|
String filepath_txt = "/" + serviceFolderName + "/" + fileName;
|
|
|
|
return filepath_txt;
|
|
}
|
|
|
|
public static String generateRandomStringForFile(int String_Lenth) {
|
|
StringBuilder result = new StringBuilder(String_Lenth);
|
|
for (int i = 0; i < STRING_LENGTH; i++) {
|
|
result.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length())));
|
|
}
|
|
return result.toString();
|
|
|
|
}
|
|
|
|
@Transactional("mysqlCommunityHallTransactionManager")
|
|
public Map<String, Object> saveBookingDetails(Map<String, Object> formData) {
|
|
Map<String, Object> hs = new HashMap<>();
|
|
|
|
Map<String, Object> userMaster = jdbcTemplate.queryForMap("SELECT * FROM users WHERE mobile_no = ?",
|
|
(formData.get("mobNo").toString()));
|
|
|
|
Integer userId = (Integer) userMaster.get("user_id");
|
|
|
|
Map<String, Object> reasonMaster = jdbcTemplate.queryForMap("SELECT * FROM reason_master WHERE reason_id = ?",
|
|
Integer.parseInt(formData.get("reasonId").toString()));
|
|
|
|
Map<String, Object> hallDetails = jdbcTemplate.queryForMap("SELECT * FROM hall_details WHERE hall_id = ?",
|
|
Integer.parseInt(formData.get("hallId").toString()));
|
|
|
|
// Fetch User
|
|
Map<String, Object> user = jdbcTemplate.queryForMap("SELECT * FROM users WHERE user_id = ?", userId);
|
|
|
|
String bookingId = formData.get("bookingId").toString();
|
|
Map<String, Object> bookingDetails = null;
|
|
|
|
if (bookingId != null && bookingId.equals("0")) {
|
|
} else {
|
|
List<Map<String, Object>> bookings = jdbcTemplate
|
|
.queryForList("SELECT * FROM booking_details WHERE booking_id = ?", Integer.parseInt(bookingId));
|
|
if (!bookings.isEmpty()) {
|
|
bookingDetails = bookings.get(0);
|
|
jdbcTemplate.update("DELETE FROM booking_documents WHERE booking_id = ?", Integer.parseInt(bookingId));
|
|
}
|
|
}
|
|
|
|
if (user != null) {
|
|
String startDate = formData.get("startDate").toString();
|
|
String endDate = formData.get("endDate").toString();
|
|
String relation = formData.get("relation").toString();
|
|
double days = Double.parseDouble(formData.get("days").toString());
|
|
int daysrange = Integer.parseInt(formData.get("daysrange").toString());
|
|
String usertype = user.get("usertype").toString();
|
|
String reason = reasonMaster.get("reason").toString();
|
|
int startingSlot = Integer.parseInt(formData.get("startingSlot").toString());
|
|
int endingSlot = Integer.parseInt(formData.get("endingSlot").toString());
|
|
String startingSlot_txt = formData.get("startingSlot").toString();
|
|
String endingSlot_txt = formData.get("endingSlot").toString();
|
|
|
|
Map<String, Object> discountrange = jdbcTemplate
|
|
.queryForMap("SELECT * FROM discount_range WHERE usertype = ?", usertype);
|
|
float discount = Float.parseFloat(discountrange.get("discount_range").toString());
|
|
|
|
double hallAmount = calcHallAmountJdbc(hallDetails, daysrange, startingSlot, endingSlot);
|
|
hs.put("actualhallamount", hallAmount);
|
|
|
|
double depositeamount = Double.parseDouble(hallDetails.get("deposit_amount").toString());
|
|
double halfDepositAmount = Double.parseDouble(hallDetails.get("half_deposit_amount").toString());
|
|
double Totaldepositeamount = calculateDepositAmountJdbc(daysrange, startingSlot_txt, endingSlot_txt,
|
|
depositeamount, halfDepositAmount);
|
|
depositeamount = Totaldepositeamount;
|
|
|
|
double discountAmount = 0;
|
|
double gstAmount = 0.00;
|
|
String reasoncode = reasonMaster.get("reasoncode") != null ? reasonMaster.get("reasoncode").toString() : "";
|
|
hs.put("reasoncode", reasoncode);
|
|
|
|
/*Map<String, Object> governmentevents = jdbcTemplate.queryForMap(
|
|
"SELECT * FROM hall_details_concession WHERE hall_id = ?",
|
|
Integer.parseInt(hallDetails.get("hall_id").toString()));*/
|
|
|
|
List<Map<String, Object>> rows = jdbcTemplate.queryForList(
|
|
"SELECT * FROM hall_details_concession WHERE hall_id = ?",
|
|
Integer.parseInt(hallDetails.get("hall_id").toString())
|
|
);
|
|
|
|
Map<String, Object> governmentevents = rows.isEmpty()
|
|
? Collections.emptyMap() // or null, your call
|
|
: rows.get(0);
|
|
|
|
if (reasoncode.equals("SGE")) {
|
|
if (startingSlot == 1 && endingSlot == 2) {
|
|
discountAmount = (Double.parseDouble(governmentevents.get("fulldayhallamount").toString())
|
|
* Double.parseDouble(hallDetails.get("sgediscount").toString())) / 100;
|
|
hallAmount = Double.parseDouble(governmentevents.get("fulldayhallamount").toString());
|
|
hs.put("discount", hallDetails.get("sgediscount"));
|
|
double cleaningCharges = Double
|
|
.parseDouble(governmentevents.get("full_day_cleaningcharges").toString());
|
|
double PASystem = Double.parseDouble(governmentevents.get("fulldaypasystem").toString());
|
|
double EB = Double.parseDouble(governmentevents.get("fulldayebcharges").toString());
|
|
double additionalcharges = cleaningCharges + PASystem + EB;
|
|
gstAmount = Math.round(additionalcharges * Double.parseDouble(hallDetails.get("gst").toString()))
|
|
/ 100.0;
|
|
double depositAmount = Double.parseDouble(hallDetails.get("deposit_amount").toString()) * daysrange;
|
|
double totalAmount = additionalcharges + gstAmount + depositAmount;
|
|
double truncatedTotalAmount = Math.round(totalAmount * 100.0) / 100.0;
|
|
hs.put("actualhallamount", hallAmount);
|
|
hs.put("additionalcharges", additionalcharges);
|
|
hs.put("totalAmount", truncatedTotalAmount);
|
|
|
|
bookingId = insertOrUpdateBookingDetailsJdbc(jdbcTemplate, bookingId, user, hallDetails, startDate,
|
|
endDate, days, startingSlot, endingSlot, depositeamount, additionalcharges, gstAmount,
|
|
truncatedTotalAmount, reason, relation, "WAITING", "Booking Slot not Confirmed.", "ONLINE");
|
|
} else {
|
|
hallAmount = Double.parseDouble(governmentevents.get("halfdayhallamount").toString());
|
|
discountAmount = (hallAmount * Double.parseDouble(hallDetails.get("sgediscount").toString())) / 100;
|
|
hs.put("discount", hallDetails.get("sgediscount"));
|
|
double cleaningCharges = Double
|
|
.parseDouble(governmentevents.get("halfdaycleaningcharges").toString());
|
|
double PASystem = Double.parseDouble(governmentevents.get("halfdaypasystem").toString());
|
|
double EB = Double.parseDouble(governmentevents.get("halfdayebcharges").toString());
|
|
double additionalcharges = cleaningCharges + PASystem + EB;
|
|
gstAmount = Math.round(additionalcharges * Double.parseDouble(hallDetails.get("gst").toString()))
|
|
/ 100.0;
|
|
double depositAmount = Double.parseDouble(hallDetails.get("deposit_amount").toString()) * daysrange;
|
|
double totalAmount = additionalcharges + gstAmount + depositAmount;
|
|
double truncatedTotalAmount = Math.round(totalAmount * 100.0) / 100.0;
|
|
hs.put("actualhallamount", hallAmount);
|
|
hs.put("additionalcharges", additionalcharges);
|
|
hs.put("totalAmount", truncatedTotalAmount);
|
|
bookingId = insertOrUpdateBookingDetailsJdbc(jdbcTemplate, bookingId, user, hallDetails, startDate,
|
|
endDate, days, startingSlot, endingSlot, depositeamount, additionalcharges, gstAmount,
|
|
truncatedTotalAmount, reason, relation, "WAITING", "Booking Slot not Confirmed.", "ONLINE");
|
|
}
|
|
} else if (reasoncode.equals("CGE")) {
|
|
double rentAmount, netAmount, depositAmount, totalAmount, truncatedTotalAmount, additionalcharges;
|
|
if (startingSlot == 1 && endingSlot == 2) {
|
|
discountAmount = (Double.parseDouble(hallDetails.get("full_day_rent").toString())
|
|
* Double.parseDouble(hallDetails.get("cgediscount").toString())) / 100;
|
|
hallAmount = Double.parseDouble(hallDetails.get("full_day_rent").toString());
|
|
hs.put("discount", hallDetails.get("cgediscount"));
|
|
double cleaningCharges = Double
|
|
.parseDouble(governmentevents.get("full_day_cleaningcharges").toString());
|
|
double PASystem = Double.parseDouble(governmentevents.get("fulldaypasystem").toString());
|
|
double EB = Double.parseDouble(governmentevents.get("fulldayebcharges").toString());
|
|
additionalcharges = cleaningCharges + PASystem + EB;
|
|
rentAmount = hallAmount - discountAmount;
|
|
netAmount = rentAmount + additionalcharges;
|
|
gstAmount = Math.round(netAmount * Double.parseDouble(hallDetails.get("gst").toString())) / 100.0;
|
|
depositAmount = Double.parseDouble(hallDetails.get("deposit_amount").toString()) * daysrange;
|
|
totalAmount = netAmount + gstAmount + depositAmount;
|
|
truncatedTotalAmount = Math.round(totalAmount * 100.0) / 100.0;
|
|
hs.put("actualhallamount", hallAmount);
|
|
hs.put("rentAmount", rentAmount);
|
|
hs.put("gstAmount", gstAmount);
|
|
hs.put("netAmount", netAmount);
|
|
hs.put("additionalcharges", additionalcharges);
|
|
hs.put("totalAmount", truncatedTotalAmount);
|
|
bookingId = insertOrUpdateBookingDetailsJdbc(jdbcTemplate, bookingId, user, hallDetails, startDate,
|
|
endDate, days, startingSlot, endingSlot, depositeamount, netAmount, gstAmount,
|
|
truncatedTotalAmount, reason, relation, "WAITING", "Booking Slot not Confirmed.", "ONLINE");
|
|
} else {
|
|
discountAmount = (Double.parseDouble(hallDetails.get("half_day_rent").toString())
|
|
* Double.parseDouble(hallDetails.get("cge_discount").toString())) / 100;
|
|
hallAmount = Double.parseDouble(hallDetails.get("half_day_rent").toString());
|
|
hs.put("discount", hallDetails.get("cgediscount"));
|
|
double cleaningCharges = Double
|
|
.parseDouble(governmentevents.get("half_day_cleaningcharges").toString());
|
|
double PASystem = Double.parseDouble(governmentevents.get("halfdaypasystem").toString());
|
|
double EB = Double.parseDouble(governmentevents.get("halfdayebcharges").toString());
|
|
additionalcharges = cleaningCharges + PASystem + EB;
|
|
rentAmount = hallAmount - discountAmount;
|
|
netAmount = rentAmount + additionalcharges;
|
|
gstAmount = Math.round(netAmount * Double.parseDouble(hallDetails.get("gst").toString())) / 100.0;
|
|
depositAmount = Double.parseDouble(hallDetails.get("deposit_amount").toString()) * daysrange;
|
|
totalAmount = netAmount + gstAmount + depositAmount;
|
|
truncatedTotalAmount = Math.round(totalAmount * 100.0) / 100.0;
|
|
hs.put("actualhallamount", hallAmount);
|
|
hs.put("rentAmount", rentAmount);
|
|
hs.put("gstAmount", gstAmount);
|
|
hs.put("netAmount", netAmount);
|
|
hs.put("additionalcharges", additionalcharges);
|
|
hs.put("totalAmount", truncatedTotalAmount);
|
|
bookingId = insertOrUpdateBookingDetailsJdbc(jdbcTemplate, bookingId, user, hallDetails, startDate,
|
|
endDate, days, startingSlot, endingSlot, depositeamount, netAmount, gstAmount,
|
|
truncatedTotalAmount, reason, relation, "WAITING", "Booking Slot not Confirmed.", "ONLINE");
|
|
}
|
|
} else {
|
|
|
|
if (usertype.equals("COUNCILLOR") || usertype.equals("PENSIONER") || usertype.equals("GCC_EMPLOYEE")
|
|
|| usertype.equals("PUBLIC")) {
|
|
discountAmount = (hallAmount * discount) / 100;
|
|
hallAmount = hallAmount - discountAmount;
|
|
gstAmount = Math.round(hallAmount * Double.parseDouble(hallDetails.get("gst").toString())) / 100.0;
|
|
double totalAmount = hallAmount + gstAmount + depositeamount;
|
|
double truncatedTotalAmount = Math.round(totalAmount * 100.0) / 100.0;
|
|
hs.put("totalAmount", truncatedTotalAmount);
|
|
bookingId = insertOrUpdateBookingDetailsJdbc(jdbcTemplate, bookingId, user, hallDetails, startDate,
|
|
endDate, days, startingSlot, endingSlot, depositeamount, hallAmount, gstAmount,
|
|
truncatedTotalAmount, reason, relation, "WAITING", "Booking Slot not Confirmed.", "ONLINE");
|
|
}
|
|
}
|
|
|
|
System.out.println("bookingId...." + bookingId);
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
LocalDate start = LocalDate.parse(startDate, formatter);
|
|
LocalDate end = LocalDate.parse(endDate, formatter);
|
|
int hallId = Integer.parseInt(hallDetails.get("hall_id").toString());
|
|
int i = 0;
|
|
for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) {
|
|
Map<String, Object> bookingCalendar = null;
|
|
List<Map<String, Object>> calList = jdbcTemplate.queryForList(
|
|
"SELECT * FROM booking_calendar WHERE bdate = ? AND hall_id = ?", date.toString(), hallId);
|
|
if (!calList.isEmpty())
|
|
bookingCalendar = calList.get(0);
|
|
|
|
if (bookingCalendar != null) {
|
|
boolean morningSlot = Boolean.TRUE.equals(bookingCalendar.get("morning_slot"));
|
|
boolean eveningSlot = Boolean.TRUE.equals(bookingCalendar.get("evening_slot"));
|
|
if ((date.equals(start) && startingSlot == 1 && morningSlot)
|
|
|| (date.equals(start) && startingSlot == 2 && eveningSlot)
|
|
|| (date.equals(end) && endingSlot == 1 && morningSlot)
|
|
|| (date.equals(end) && endingSlot == 2 && eveningSlot)) {
|
|
return Collections.singletonMap("status", "slotOccupied");
|
|
} else if (morningSlot && eveningSlot) {
|
|
return Collections.singletonMap("status", "slotOccupied");
|
|
} else if (!date.equals(end) && i != 0 && i != Math.round(days - 1)
|
|
&& (morningSlot || eveningSlot)) {
|
|
return Collections.singletonMap("status", "slotOccupied");
|
|
} else if (date.equals(end) && endingSlot == 2 && morningSlot && i != 0
|
|
&& i == Math.round(days - 1)) {
|
|
return Collections.singletonMap("status", "slotOccupied");
|
|
}
|
|
}
|
|
i++;
|
|
}
|
|
|
|
List<Map<String, Object>> reasonDocMaster = jdbcTemplate.queryForList(
|
|
"SELECT * FROM reason_document_master WHERE reason_id = ?",
|
|
Integer.parseInt(formData.get("reasonId").toString()));
|
|
List<Map<String, Object>> uploadedDocs = new ArrayList<>();
|
|
for (Map<String, Object> rm : reasonDocMaster) {
|
|
Map<String, Object> docMap = new HashMap<>();
|
|
String fieldName = "";
|
|
String value = "";
|
|
String fileName = "";
|
|
String fileUrl = "";
|
|
String filePath = "";
|
|
if (Boolean.TRUE.equals(rm.get("add_text_field"))) {
|
|
fieldName = rm.get("field_name").toString();
|
|
value = formData.get(fieldName).toString();
|
|
jdbcTemplate.update(
|
|
"INSERT INTO booking_documents (booking_details_booking_id, booking_label, name) VALUES (?, ?, ?)",
|
|
Integer.parseInt(bookingId), fieldName, value);
|
|
docMap.put("booking_label", fieldName);
|
|
docMap.put("name", value);
|
|
}
|
|
if (Boolean.TRUE.equals(rm.get("add_document_field"))) {
|
|
String docFieldName = rm.get("doc_field_name").toString();
|
|
MultipartFile file = (MultipartFile) formData.get(docFieldName);
|
|
fileName = bookingId + "_" + file.getOriginalFilename();
|
|
fileUrl = serverPath + "images/" + reason + "/" + fileName;
|
|
filePath = uploadDirectory + "/" + reason + "/" + fileName;
|
|
|
|
// Create directory if it doesn't exist
|
|
File directory = new File(uploadDirectory + "/" + reason);
|
|
if (!directory.exists()) {
|
|
directory.mkdirs(); // Creates parent directories if needed
|
|
}
|
|
|
|
docMap.put("documentName", file.getOriginalFilename());
|
|
docMap.put("documentType", file.getContentType());
|
|
docMap.put("bookingDocUrl", fileUrl);
|
|
|
|
// Save file to disk
|
|
try (FileOutputStream fos = new FileOutputStream(filePath)) {
|
|
byte[] bytes = file.getBytes();
|
|
fos.write(bytes);
|
|
} catch (Exception e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
|
|
jdbcTemplate.update(
|
|
"INSERT INTO booking_documents (booking_details_booking_id, booking_doc_url, document_name, document_type, booking_label, name) VALUES (?, ?, ?, ?, ?, ?)",
|
|
Integer.parseInt(bookingId), fileUrl, file.getOriginalFilename(), file.getContentType(),
|
|
fieldName, value);
|
|
}
|
|
uploadedDocs.add(docMap);
|
|
}
|
|
|
|
hs.put("bookingId", bookingId);
|
|
hs.put("hallName", hallDetails.get("hall_name"));
|
|
hs.put("NoOfDay", days);
|
|
hs.put("startDate", startDate);
|
|
hs.put("endDate", endDate);
|
|
hs.put("startingSlot", startingSlot);
|
|
hs.put("endingSlot", endingSlot);
|
|
hs.put("hallAmount", hallAmount);
|
|
hs.put("gst", gstAmount);
|
|
hs.put("depositAmount", depositeamount);
|
|
hs.put("username", user.get("name"));
|
|
hs.put("userId", user.get("user_id"));
|
|
hs.put("mobileNo", user.get("mobile_no"));
|
|
hs.put("userType", user.get("usertype"));
|
|
hs.put("reason", reason);
|
|
hs.put("bookingId", bookingId);
|
|
hs.put("uploadedDocs", uploadedDocs);
|
|
}
|
|
|
|
return hs;
|
|
}
|
|
|
|
private double calcHallAmountJdbc(Map<String, Object> hallDetails, int daysrange, int startPeriod, int endPeriod) {
|
|
double fullDayRent = Double.parseDouble(hallDetails.get("full_day_rent").toString());
|
|
double halfDayRent = Double.parseDouble(hallDetails.get("half_day_rent").toString());
|
|
if (daysrange == 1) {
|
|
if (startPeriod == 1 && endPeriod == 2)
|
|
return fullDayRent;
|
|
else if (startPeriod == 1 && endPeriod == 1)
|
|
return halfDayRent;
|
|
else if (startPeriod == 2 && endPeriod == 2)
|
|
return halfDayRent;
|
|
else if (startPeriod == 2 && endPeriod == 1)
|
|
return fullDayRent;
|
|
} else {
|
|
if (startPeriod == 1 && endPeriod == 2)
|
|
return fullDayRent * daysrange;
|
|
else if (startPeriod == 1 && endPeriod == 1)
|
|
return fullDayRent * (daysrange - 1) + halfDayRent;
|
|
else if (startPeriod == 2 && endPeriod == 2)
|
|
return fullDayRent * (daysrange - 1) + halfDayRent;
|
|
else if (startPeriod == 2 && endPeriod == 1)
|
|
return fullDayRent * (daysrange - 2) + halfDayRent * 2;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
private double calculateDepositAmountJdbc(double numDays, String startPeriod, String endPeriod,
|
|
double depositAmount, double HalfDepositAmount) {
|
|
double damount = 0;
|
|
if (numDays == 1) {
|
|
if ("1".equals(startPeriod) && "2".equals(endPeriod))
|
|
damount = depositAmount;
|
|
else if ("1".equals(startPeriod) && "1".equals(endPeriod))
|
|
damount = HalfDepositAmount;
|
|
else if ("2".equals(startPeriod) && "2".equals(endPeriod))
|
|
damount = HalfDepositAmount;
|
|
else
|
|
damount = numDays;
|
|
} else {
|
|
if ("1".equals(startPeriod) && "2".equals(endPeriod))
|
|
damount = depositAmount * numDays;
|
|
else if ("1".equals(startPeriod) && "1".equals(endPeriod))
|
|
damount = depositAmount * (numDays - 1) + HalfDepositAmount;
|
|
else if ("2".equals(startPeriod) && "2".equals(endPeriod))
|
|
damount = depositAmount * (numDays - 1) + HalfDepositAmount;
|
|
else if ("2".equals(startPeriod) && "1".equals(endPeriod))
|
|
damount = depositAmount * (numDays - 2) + depositAmount;
|
|
else
|
|
damount = numDays;
|
|
}
|
|
return damount;
|
|
}
|
|
|
|
private String insertOrUpdateBookingDetailsJdbc(JdbcTemplate jdbcTemplate, String bookingId,
|
|
Map<String, Object> user, Map<String, Object> hallDetails, String startDate, String endDate, double days,
|
|
int startingSlot, int endingSlot, double depositAmount, double hallAmount, double gst, double totalAmount,
|
|
String reason, String relation, String refundStatus, String comments, String bookingType) {
|
|
|
|
if (bookingId == null || bookingId.equals("0")) {
|
|
// Insert with KeyHolder
|
|
KeyHolder keyHolder = new GeneratedKeyHolder();
|
|
jdbcTemplate.update(connection -> {
|
|
PreparedStatement ps = connection.prepareStatement(
|
|
"INSERT INTO booking_details (users_user_id, hall_details_hall_id, start_date, end_date, no_of_days, starting_slot, ending_slot, deposit_amount, hall_amount, gst, amount_received, reason, refund_status, comments, booking_type, relationship, booking_date, schedule_count, source) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
Statement.RETURN_GENERATED_KEYS);
|
|
ps.setObject(1, user.get("user_id"));
|
|
ps.setObject(2, hallDetails.get("hall_id"));
|
|
ps.setString(3, startDate);
|
|
ps.setString(4, endDate);
|
|
ps.setDouble(5, days);
|
|
ps.setInt(6, startingSlot);
|
|
ps.setInt(7, endingSlot);
|
|
ps.setDouble(8, depositAmount);
|
|
ps.setDouble(9, hallAmount);
|
|
ps.setDouble(10, gst);
|
|
ps.setDouble(11, totalAmount);
|
|
ps.setString(12, reason);
|
|
ps.setString(13, refundStatus);
|
|
ps.setString(14, comments);
|
|
ps.setString(15, bookingType);
|
|
ps.setString(16, relation);
|
|
ps.setString(17, java.time.LocalDate.now().toString());
|
|
ps.setInt(18, 0);
|
|
ps.setInt(19, 2);
|
|
return ps;
|
|
}, keyHolder);
|
|
|
|
Number generatedId = keyHolder.getKey();
|
|
return generatedId != null ? generatedId.toString() : null;
|
|
} else {
|
|
// Update
|
|
jdbcTemplate.update(
|
|
"UPDATE booking_details SET users_user_id=?, hall_details_hall_id=?, start_date=?, end_date=?, no_of_days=?, starting_slot=?, ending_slot=?, deposit_amount=?, hall_amount=?, gst=?, amount_received=?, reason=?, refund_status=?, comments=?, booking_type=?, relationship=?, booking_date=?, schedule_count=? WHERE booking_id=?",
|
|
user.get("user_id"), hallDetails.get("hall_id"), startDate, endDate, days, startingSlot, endingSlot,
|
|
depositAmount, hallAmount, gst, totalAmount, reason, refundStatus, comments, bookingType, relation,
|
|
java.time.LocalDate.now().toString(), 0, Integer.parseInt(bookingId));
|
|
return bookingId;
|
|
}
|
|
}
|
|
|
|
public HashMap<String, Object> startPayment(int bookingId) {
|
|
HashMap<String, Object> hs = new HashMap<>();
|
|
|
|
String bookingSql = "SELECT b.amount_received, b.users_user_id, b.hall_details_hall_id, "
|
|
+ " u.user_id, u.mobile_no, u.email " + " FROM booking_details b "
|
|
+ " JOIN users u ON b.users_user_id = u.user_id " + " WHERE b.booking_id = ?";
|
|
|
|
try {
|
|
Map<String, Object> data = jdbcTemplate.queryForMap(bookingSql, bookingId);
|
|
|
|
double amountReceived = (double) data.get("amount_received");
|
|
int UserId = (int) data.get("user_id");
|
|
long mobileNo = (long) data.get("mobile_no");
|
|
String email = (String) data.get("email");
|
|
int hallId = (int) data.get("hall_details_hall_id");
|
|
|
|
String txnId = "CHB" + transactionNumberGenerator.generateTransactionNumber()
|
|
+ String.format("%05d", UserId);
|
|
|
|
String totalAmt = String.valueOf(amountReceived);
|
|
|
|
String insertSql = "INSERT INTO transaction_response (txansaction_id, hall_id, user_id, status_code, booking_id) "
|
|
+ " VALUES (?, ?, ?, ?, ?)";
|
|
jdbcTemplate.update(insertSql, txnId, hallId, String.valueOf(UserId), "temp", bookingId);
|
|
|
|
String merchantId = paymentConfig.getMerchantId();
|
|
String consumerId = String.format("%05d", UserId);
|
|
String encryptedToken = getToken(merchantId + "|", txnId + "|", totalAmt + "|", "|", consumerId + "|",
|
|
mobileNo + "|", email + "|", "|", "|", "|", "|", "|", "|", "|", "|", "|",
|
|
paymentConfig.getEncryptionKey());
|
|
|
|
hs.put("txansactionId", txnId);
|
|
hs.put("token", encryptedToken);
|
|
hs.put("amount", totalAmt);
|
|
/*
|
|
* hs.put("merchantId", merchantId); hs.put("consumerMobileNo", mobileNo);
|
|
* hs.put("consumerEmailId", email); hs.put("consumerId", consumerId);
|
|
* hs.put("error", false);
|
|
*/
|
|
|
|
} catch (EmptyResultDataAccessException | NoSuchAlgorithmException e) {
|
|
hs.put("error", true);
|
|
hs.put("message", "Invalid booking ID or no user data found. ");
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return hs;
|
|
}
|
|
|
|
public String getToken(String merchantId, String txnId, String totalAmt, String accountNo, String consumerId,
|
|
String consumerMobileNo, String consumerEmailId, String debitStartDate, String debitEndDate,
|
|
String maxAmount, String amountType, String frequency, String cardNumber, String expMonth, String expYear,
|
|
String cvvCode, String salt) throws NoSuchAlgorithmException {
|
|
String token = merchantId + txnId + totalAmt + accountNo + consumerId + consumerMobileNo + consumerEmailId
|
|
+ debitStartDate + debitEndDate + maxAmount + amountType + frequency + cardNumber + expMonth + expYear
|
|
+ cvvCode + salt;
|
|
|
|
return generateToken(token);
|
|
}
|
|
|
|
public String generateToken(String token) throws NoSuchAlgorithmException {
|
|
// System.out.println("Token:-" + token);
|
|
MessageDigest md = MessageDigest.getInstance("SHA-512");
|
|
md.update(token.getBytes());
|
|
|
|
byte[] bytes = md.digest();
|
|
|
|
BigInteger bigInt = new BigInteger(1, bytes);
|
|
String encryptedtoken = bigInt.toString(16);
|
|
|
|
while (encryptedtoken.length() < 128) {
|
|
encryptedtoken = "0" + encryptedtoken;
|
|
}
|
|
// System.out.println("Encryption :- " + encryptedtoken);
|
|
return encryptedtoken;
|
|
}
|
|
|
|
public Map<String, Object> checkSlotAvailability(int hallId, String startDate, String endDate, int startingSlot, int endingSlot) {
|
|
|
|
// Validate slot numbers
|
|
if (startingSlot < 1 || startingSlot > 2 || endingSlot < 1 || endingSlot > 2) {
|
|
throw new IllegalArgumentException("Slot values must be 1 (morning) or 2 (evening).");
|
|
}
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
LocalDate start;
|
|
LocalDate end;
|
|
|
|
try {
|
|
start = LocalDate.parse(startDate, formatter);
|
|
end = LocalDate.parse(endDate, formatter);
|
|
} catch (Exception e) {
|
|
throw new IllegalArgumentException("Invalid date format. Use yyyy-MM-dd.");
|
|
}
|
|
|
|
if (end.isBefore(start)) {
|
|
throw new IllegalArgumentException("End date cannot be before start date.");
|
|
}
|
|
|
|
long days = java.time.temporal.ChronoUnit.DAYS.between(start, end) + 1;
|
|
int i = 0;
|
|
|
|
try {
|
|
for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) {
|
|
|
|
List<Map<String, Object>> calList = jdbcTemplate.queryForList(
|
|
"SELECT * FROM booking_calendar WHERE bdate = ? AND hall_id = ?",
|
|
date.toString(), hallId
|
|
);
|
|
|
|
if (!calList.isEmpty()) {
|
|
Map<String, Object> bookingCalendar = calList.get(0);
|
|
|
|
boolean morningSlot = Boolean.TRUE.equals(bookingCalendar.get("morning_slot"));
|
|
boolean eveningSlot = Boolean.TRUE.equals(bookingCalendar.get("evening_slot"));
|
|
|
|
if ((date.equals(start) && startingSlot == 1 && morningSlot)
|
|
|| (date.equals(start) && startingSlot == 2 && eveningSlot)
|
|
|| (date.equals(end) && endingSlot == 1 && morningSlot)
|
|
|| (date.equals(end) && endingSlot == 2 && eveningSlot)) {
|
|
return Map.of("status", "slotOccupied");
|
|
} else if (morningSlot && eveningSlot) {
|
|
return Map.of("status", "slotOccupied");
|
|
} else if (!date.equals(end) && i != 0 && i != Math.round(days - 1)
|
|
&& (morningSlot || eveningSlot)) {
|
|
return Map.of("status", "slotOccupied");
|
|
} else if (date.equals(end) && endingSlot == 2 && morningSlot && i != 0
|
|
&& i == Math.round(days - 1)) {
|
|
return Map.of("status", "slotOccupied");
|
|
}
|
|
}
|
|
i++;
|
|
}
|
|
} catch (DataAccessException e) {
|
|
e.printStackTrace();
|
|
throw new RuntimeException("Database error occurred while checking slot availability.");
|
|
}
|
|
|
|
return Map.of("status", "slotAvailable");
|
|
}
|
|
}
|