Subversion Repositories SmartDukaan

Rev

Rev 24782 | Rev 24794 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 24782 Rev 24784
Line 291... Line 291...
291
		// throw new Exception();
291
		// throw new Exception();
292
	}
292
	}
293
 
293
 
294
	// Lets work it up for The Mobile Planet
294
	// Lets work it up for The Mobile Planet
295
	// HRFB002 - 157112773
295
	// HRFB002 - 157112773
296
	public void dailyReconciliation() throws Exception {
-
 
297
		Map<Integer, String> stores = fofoStoreRepository.getStoresMap();
-
 
298
		List<List<?>> rows = new ArrayList<>();
-
 
299
		Map<Integer, String> retailerNameMap = retailerService.getAllFofoRetailerIdNameMap(new ArrayList<>(stores.keySet()));
-
 
300
		for (int partnerId : stores.keySet()) {
-
 
301
			UserWallet uw = userWalletRepository.selectByRetailerId(partnerId);
-
 
302
			List<UserWalletHistory> walletHistory = userWalletHistoryRepository.selectByWalletId(uw.getId());
-
 
303
			Map<LocalDate, List<UserWalletHistory>> dateWiseWalletHistory = walletHistory.stream()
-
 
304
					.collect(Collectors.groupingBy(x -> x.getTimestamp().toLocalDate(), Collectors.toList()));
-
 
305
			for (Map.Entry<LocalDate, List<UserWalletHistory>> entry : dateWiseWalletHistory.entrySet()) {
-
 
306
				LocalDate dateToReconcile = entry.getKey();
-
 
307
				List<UserWalletHistory> history = entry.getValue();
-
 
308
				rows.add(reconcileOrdersAndWallet(uw.getUserId(), retailerNameMap.get(partnerId), dateToReconcile, history));
-
 
309
			}
-
 
310
		}
-
 
311
		ByteArrayOutputStream baos = FileUtil.getCSVByteStream(
-
 
312
				Arrays.asList("Store Code", "Date", "Wallet amount consumed", "Ordered Total", "Cancelled Total", "Refunded Total"), rows);
-
 
313
 
296
 
314
		Utils.sendMailWithAttachment(mailSender, new String[] { "amit.gupta@shop2020.in" }, new String[] {}, "Test",
-
 
315
				"test", "File.csv", new ByteArrayResource(baos.toByteArray()));
-
 
316
	}
-
 
317
 
-
 
318
	private List<?> reconcileOrdersAndWallet(int fofoId, String storeName, LocalDate localDate, List<UserWalletHistory> history)
-
 
319
			throws Exception {
-
 
320
		Map<Integer, Integer> transactionsOnThatDate = history.stream()
-
 
321
				.filter(x -> x.getReferenceType().equals(WalletReferenceType.PURCHASE))
-
 
322
				.collect(Collectors.groupingBy(x -> x.getReference(), Collectors.summingInt(x -> x.getAmount())));
-
 
323
 
-
 
324
		int totalWalletConsumed = 0;
-
 
325
		float cancelledAmount = 0;
-
 
326
		float returnedAmount = 0;
-
 
327
		float totalDeductedAmount = 0;
-
 
328
		for (int transactionId : transactionsOnThatDate.keySet()) {
-
 
329
			List<Order> orders = orderRepository.selectAllByTransactionId(transactionId);
-
 
330
			for (Order o : orders) {
-
 
331
				if (o.getCreateTimestamp().toLocalDate().equals(localDate)) {
-
 
332
					if (Arrays.asList(OrderStatus.PAYMENT_PENDING, OrderStatus.PAYMENT_FAILED)
-
 
333
							.contains(o.getStatus())) {
-
 
334
						cancelledAmount += o.getWalletAmount();
-
 
335
					} else if (o.getRefundTimestamp() != null
-
 
336
							&& o.getRefundTimestamp().toLocalDate().equals(localDate)) {
-
 
337
						ReturnOrder returnedOrder = returnOrderRepository.selectByOrderId(o.getId());
-
 
338
						if (returnedOrder == null) {
-
 
339
							cancelledAmount += o.getWalletAmount();
-
 
340
						} else {
-
 
341
							returnedAmount += returnedOrder.getTotalPrice();
-
 
342
						}
-
 
343
					}
-
 
344
					totalDeductedAmount += o.getWalletAmount();
-
 
345
 
-
 
346
				} else if (o.getRefundTimestamp() != null && o.getRefundTimestamp().toLocalDate().equals(localDate)) {
-
 
347
					ReturnOrder returnedOrder = returnOrderRepository.selectByOrderId(o.getId());
-
 
348
					if (returnedOrder == null) {
-
 
349
						cancelledAmount += o.getWalletAmount();
-
 
350
					} else {
-
 
351
						returnedAmount += returnedOrder.getTotalPrice();
-
 
352
					}
-
 
353
				}
-
 
354
			}
-
 
355
			totalWalletConsumed -= transactionsOnThatDate.get(transactionId);
-
 
356
 
-
 
357
		}
-
 
358
 
-
 
359
		return Arrays.asList(localDate, storeName, totalWalletConsumed, totalDeductedAmount, cancelledAmount, returnedAmount);
-
 
360
 
-
 
361
	}
-
 
362
 
-
 
363
	private void reconcileDailySchemeIn() {
-
 
364
 
-
 
365
	}
-
 
366
 
-
 
367
	private void reconcileDailySchemeOut() {
-
 
368
 
-
 
369
	}
-
 
370
}
297
}
371
298