Untitled

mail@pastecode.io avatar
unknown
java
14 days ago
1.6 kB
2
Indexable
Never
public Map<String, Number> getPaymentMethodCount(){
		Map<String , Number> data = new HashMap<>();
		Number totalMethodCount = 0;
		LocalDate now = LocalDate.now();
		LocalDate firstDayOfMonth = now.withDayOfMonth(1);
		LocalDate lastDayOfMonth = now.withDayOfMonth(now.lengthOfMonth());

		try {
			// get query data from database range one month
			String stringQuery = "select paymentMethod, count(*) AS method_count FROM " + PnrSalesReportView.class.getName()
					+ " WHERE bookingDate >= :startDate AND bookingDate <= :endDate" + " GROUP BY paymentMethod";


			Query query = getSession().createQuery(stringQuery);

			query.setParameter("startDate", java.sql.Date.valueOf(firstDayOfMonth));
			query.setParameter("endDate", java.sql.Date.valueOf(lastDayOfMonth));

			List<Object[]> results = query.list();

			// get total of method count
			for (Object[] row : results) {
				Number methodCount = (Number) row[1];
				totalMethodCount = totalMethodCount.longValue() + methodCount.longValue();
			}

			// Mapping paymentMethod and methodCount to data and convert data to percentage
			for (Object[] row : results) {
				String paymentMethod = (String) row[0];
				Number methodCount = (Number) row[1];
				Number percentage = (methodCount.doubleValue() / totalMethodCount.doubleValue()) * 100;
				data.put(paymentMethod, percentage);
			}

			return data;

		} catch (Exception e) {
			System.err.println("Error executing PaymentMethodCount query: " + e.getMessage());
			throw new RuntimeException("Error executing PaymentMethodCount query", e);

		}

	}
Leave a Comment