Subversion Repositories SmartDukaan

Rev

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

Rev 24597 Rev 24876
Line 4... Line 4...
4
import java.time.LocalDateTime;
4
import java.time.LocalDateTime;
5
import java.util.ArrayList;
5
import java.util.ArrayList;
6
import java.util.Collections;
6
import java.util.Collections;
7
import java.util.List;
7
import java.util.List;
8
import java.util.Map;
8
import java.util.Map;
-
 
9
import java.util.stream.Collectors;
9
 
10
 
10
import javax.mail.internet.InternetAddress;
11
import javax.mail.internet.InternetAddress;
11
import javax.mail.internet.MimeMessage;
12
import javax.mail.internet.MimeMessage;
12
import javax.servlet.http.HttpServletRequest;
13
import javax.servlet.http.HttpServletRequest;
13
 
14
 
14
import org.apache.logging.log4j.LogManager;
15
import org.apache.logging.log4j.LogManager;
15
import org.apache.logging.log4j.Logger;
16
import org.apache.logging.log4j.Logger;
-
 
17
import org.json.JSONObject;
16
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.mail.javamail.JavaMailSender;
19
import org.springframework.mail.javamail.JavaMailSender;
18
import org.springframework.mail.javamail.MimeMessageHelper;
20
import org.springframework.mail.javamail.MimeMessageHelper;
19
import org.springframework.stereotype.Controller;
21
import org.springframework.stereotype.Controller;
20
import org.springframework.transaction.annotation.Transactional;
22
import org.springframework.transaction.annotation.Transactional;
Line 27... Line 29...
27
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
29
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
28
import com.spice.profitmandi.common.model.CustomRetailer;
30
import com.spice.profitmandi.common.model.CustomRetailer;
29
import com.spice.profitmandi.common.model.ProfitMandiConstants;
31
import com.spice.profitmandi.common.model.ProfitMandiConstants;
30
import com.spice.profitmandi.common.model.UnsettledPaymentModel;
32
import com.spice.profitmandi.common.model.UnsettledPaymentModel;
31
import com.spice.profitmandi.common.util.StringUtils;
33
import com.spice.profitmandi.common.util.StringUtils;
-
 
34
import com.spice.profitmandi.dao.entity.dtr.Retailer;
32
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
35
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
-
 
36
import com.spice.profitmandi.dao.entity.transaction.ManualPaymentType;
33
import com.spice.profitmandi.dao.entity.transaction.UnsettledPayment;
37
import com.spice.profitmandi.dao.entity.transaction.UnsettledPayment;
34
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
38
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
35
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
39
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
36
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
40
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
-
 
41
import com.spice.profitmandi.dao.enumuration.transaction.TransactionType;
37
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
42
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
-
 
43
import com.spice.profitmandi.dao.repository.catalog.ManualPaymentRequestRepository;
38
import com.spice.profitmandi.dao.repository.catalog.UnsettledPaymentsRepository;
44
import com.spice.profitmandi.dao.repository.catalog.UnsettledPaymentsRepository;
-
 
45
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
-
 
46
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
39
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
47
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
-
 
48
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
40
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
49
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
41
import com.spice.profitmandi.service.authentication.RoleManager;
50
import com.spice.profitmandi.service.authentication.RoleManager;
42
import com.spice.profitmandi.service.user.RetailerService;
51
import com.spice.profitmandi.service.user.RetailerService;
43
import com.spice.profitmandi.service.wallet.WalletService;
52
import com.spice.profitmandi.service.wallet.WalletService;
44
import com.spice.profitmandi.web.model.LoginDetails;
53
import com.spice.profitmandi.web.model.LoginDetails;
Line 59... Line 68...
59
 
68
 
60
	@Autowired
69
	@Autowired
61
	private UserWalletRepository userWalletRepository;
70
	private UserWalletRepository userWalletRepository;
62
 
71
 
63
	@Autowired
72
	@Autowired
-
 
73
	private UserWalletHistoryRepository userWalletHistoryRepository;
-
 
74
 
-
 
75
	@Autowired
64
	private MVCResponseSender mvcResponseSender;
76
	private MVCResponseSender mvcResponseSender;
65
 
77
 
66
	@Autowired
78
	@Autowired
67
	private UserAccountRepository userAccountRepository;
79
	private UserAccountRepository userAccountRepository;
68
 
80
 
Line 72... Line 84...
72
	@Autowired
84
	@Autowired
73
	private UnsettledPaymentsRepository unsettledPaymentsRepository;
85
	private UnsettledPaymentsRepository unsettledPaymentsRepository;
74
 
86
 
75
	@Autowired
87
	@Autowired
76
	private RetailerService retailerService;
88
	private RetailerService retailerService;
77
	
89
 
78
	@Autowired
90
	@Autowired
79
	private RoleManager roleManager;
91
	private RoleManager roleManager;
80
 
92
 
81
	@Autowired
93
	@Autowired
-
 
94
	private FofoStoreRepository fofoStoreRepository;
-
 
95
 
-
 
96
	@Autowired
-
 
97
	private ManualPaymentRequestRepository manualPaymentRequestRepository;
-
 
98
 
-
 
99
	@Autowired
82
	AddWalletRequestRepository addWalletRequestRepository;
100
	AddWalletRequestRepository addWalletRequestRepository;
-
 
101
 
-
 
102
	@Autowired
-
 
103
	private RetailerRepository retailerRepository;
83
	private static final Logger LOGGER = LogManager.getLogger(WalletController.class);
104
	private static final Logger LOGGER = LogManager.getLogger(WalletController.class);
84
 
105
 
85
	@RequestMapping(value = "/walletDetails", method = RequestMethod.GET)
106
	@RequestMapping(value = "/walletDetails", method = RequestMethod.GET)
86
	public String dashboard(HttpServletRequest request,
107
	public String dashboard(HttpServletRequest request,
87
			@RequestParam(name = ProfitMandiConstants.START_TIME, required = false) String startTimeString,
108
			@RequestParam(name = ProfitMandiConstants.START_TIME, required = false) String startTimeString,
88
			@RequestParam(name = ProfitMandiConstants.END_TIME, required = false) String endTimeString,
109
			@RequestParam(name = ProfitMandiConstants.END_TIME, required = false) String endTimeString,
89
			@RequestParam(name = "offset", defaultValue = "0") int offset,
110
			@RequestParam(name = "offset", defaultValue = "0") int offset,
90
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
111
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
91
			throws ProfitMandiBusinessException {
112
			throws ProfitMandiBusinessException {
-
 
113
		/*
92
		/*boolean underMaintainance = true;
114
		 * boolean underMaintainance = true; if(underMaintainance) { throw new
93
		if(underMaintainance) {
115
		 * ProfitMandiBusinessException("Wallet", "Wallet",
94
			throw new ProfitMandiBusinessException("Wallet", "Wallet", "Wallet is under Maintenance");
116
		 * "Wallet is under Maintenance"); }
95
		}*/
117
		 */
96
		LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
118
		LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
97
		boolean isAdmin = roleManager.isAdmin(fofoDetails.getRoleIds());
119
		boolean isAdmin = roleManager.isAdmin(fofoDetails.getRoleIds());
98
		UserWallet userWallet = null;
120
		UserWallet userWallet = null;
99
		try {
121
		try {
100
			userWallet = userWalletRepository.selectByRetailerId(fofoDetails.getFofoId());
122
			userWallet = userWalletRepository.selectByRetailerId(fofoDetails.getFofoId());
Line 501... Line 523...
501
		return "response";
523
		return "response";
502
	}
524
	}
503
 
525
 
504
	@RequestMapping(value = "/wallet/statement", method = RequestMethod.GET)
526
	@RequestMapping(value = "/wallet/statement", method = RequestMethod.GET)
505
	public String getWalletStatement(HttpServletRequest request, @RequestParam LocalDateTime startDate,
527
	public String getWalletStatement(HttpServletRequest request, @RequestParam LocalDateTime startDate,
506
			@RequestParam LocalDateTime endDate, Model model, @RequestParam(defaultValue="0") int fofoId) throws Exception {
528
			@RequestParam LocalDateTime endDate, Model model, @RequestParam(defaultValue = "0") int fofoId)
-
 
529
			throws Exception {
507
		boolean isAdmin = roleManager.isAdmin(cookiesProcessor.getCookiesObject(request).getRoleIds());
530
		boolean isAdmin = roleManager.isAdmin(cookiesProcessor.getCookiesObject(request).getRoleIds());
508
		if(fofoId > 0) {
531
		if (fofoId > 0) {
509
			if(!isAdmin) {
532
			if (!isAdmin) {
510
				throw new ProfitMandiBusinessException("Unauthorised access", "PartnerId", "Permission Denied");
533
				throw new ProfitMandiBusinessException("Unauthorised access", "PartnerId", "Permission Denied");
511
			}
534
			}
512
		} else {
535
		} else {
513
			fofoId = cookiesProcessor.getCookiesObject(request).getFofoId();
536
			fofoId = cookiesProcessor.getCookiesObject(request).getFofoId();
514
		}
537
		}
515
		float openingBalance = walletService.getOpeningTill(fofoId, startDate);
538
		float openingBalance = walletService.getOpeningTill(fofoId, startDate);
516
		float closingBalance = walletService.getOpeningTill(fofoId, endDate);
539
		float closingBalance = walletService.getOpeningTill(fofoId, endDate);
517
		List<UserWalletHistory> uwhList = walletService.getPaginatedUserWalletHistoryByRetailerId(
540
		List<UserWalletHistory> uwhList = walletService.getPaginatedUserWalletHistoryByRetailerId(fofoId, startDate,
518
				fofoId, startDate, endDate, 0, 0);
541
				endDate, 0, 0);
519
		Collections.reverse(uwhList);
542
		Collections.reverse(uwhList);
-
 
543
		/*
520
		/*HttpHeaders headers = new HttpHeaders();
544
		 * HttpHeaders headers = new HttpHeaders(); InputStreamResource is = new
521
		InputStreamResource is = new InputStreamResource();
545
		 * InputStreamResource(); headers.set("Content-Type",
522
		headers.set("Content-Type", "application/vnd.ms-excel");
546
		 * "application/vnd.ms-excel"); headers.set("Content-disposition",
523
		headers.set("Content-disposition", "inline; filename=report-" + fileName + ".csv");
547
		 * "inline; filename=report-" + fileName + ".csv");
524
		headers.setContentLength(response.getEntity().getContentLength());*/
548
		 * headers.setContentLength(response.getEntity().getContentLength());
-
 
549
		 */
525
		model.addAttribute("opening", openingBalance);
550
		model.addAttribute("opening", openingBalance);
526
		model.addAttribute("closing", closingBalance);
551
		model.addAttribute("closing", closingBalance);
527
		model.addAttribute("history", uwhList);
552
		model.addAttribute("history", uwhList);
528
		model.addAttribute("startDate", startDate);
553
		model.addAttribute("startDate", startDate);
529
		model.addAttribute("endDate", endDate);
554
		model.addAttribute("endDate", endDate);
530
		return "walletStatement";
555
		return "walletStatement";
531
	}
556
	}
532
 
557
 
-
 
558
	@RequestMapping(value = "/manualPayment", method = RequestMethod.GET)
-
 
559
	public String ManualPayment(HttpServletRequest request, Model model) throws Exception {
-
 
560
 
-
 
561
		List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
-
 
562
				.collect(Collectors.toList());
-
 
563
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
-
 
564
		String customRetailers = JSONObject.valueToString(customRetailersMap.values());
-
 
565
		model.addAttribute("customRetailers", customRetailers);
-
 
566
 
-
 
567
		model.addAttribute("referenceTypes", WalletReferenceType.referenceType);
-
 
568
		model.addAttribute("transactionTypes", TransactionType.values());
-
 
569
		return "wallet-edit";
-
 
570
 
-
 
571
	}
-
 
572
 
-
 
573
	@RequestMapping(value = "/getWalletHistory", method = RequestMethod.GET)
-
 
574
	public String getWalletHistory(HttpServletRequest request,
-
 
575
			@RequestParam(name = "reference", defaultValue = "0") int reference,
-
 
576
			@RequestParam WalletReferenceType referenceType, Model model) throws Exception {
-
 
577
 
-
 
578
		LOGGER.info("type" + referenceType);
-
 
579
		List<UserWalletHistory> userWalletHistory = userWalletHistoryRepository
-
 
580
				.selectAllByreferenceIdandreferenceType(reference, referenceType);
-
 
581
 
-
 
582
		model.addAttribute("wallethistory", userWalletHistory);
-
 
583
 
-
 
584
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
-
 
585
		return "wallet-history";
-
 
586
 
-
 
587
	}
-
 
588
 
-
 
589
	@RequestMapping(value = "/getPartnerName", method = RequestMethod.GET)
-
 
590
	public String getPartnerName(HttpServletRequest request,
-
 
591
			@RequestParam(name = "reference", defaultValue = "0") int reference,
-
 
592
			@RequestParam WalletReferenceType referenceType, Model model) throws Exception {
-
 
593
 
-
 
594
		List<UserWalletHistory> userWalletHistory = userWalletHistoryRepository
-
 
595
				.selectAllByreferenceIdandreferenceType(reference, referenceType);
-
 
596
		if(userWalletHistory.isEmpty()){
-
 
597
			throw new ProfitMandiBusinessException("RefrenceId", reference, "Reference Id not found");
-
 
598
		}
-
 
599
		UserWallet userWallet = userWalletRepository.selectById(userWalletHistory.get(0).getWalletId());
-
 
600
		
-
 
601
		CustomRetailer retailer = retailerService.getFofoRetailer(userWallet.getUserId());
-
 
602
   
-
 
603
		model.addAttribute("response", mvcResponseSender.createResponseString(retailer));
-
 
604
		
-
 
605
		return "response";
-
 
606
 
-
 
607
	}
-
 
608
 
-
 
609
	@RequestMapping(value = "/walletUpdate", method = RequestMethod.POST)
-
 
610
	public String walletUpdate(HttpServletRequest request,
-
 
611
			@RequestParam(name = "reference", defaultValue = "0") int reference,
-
 
612
			@RequestParam WalletReferenceType referenceType, @RequestParam TransactionType transactiontype,
-
 
613
			@RequestParam int amount, @RequestParam String description, @RequestParam int retailerId, Model model)
-
 
614
			throws Exception {
-
 
615
 
-
 
616
		List<UserWalletHistory> userWalletHistory = userWalletHistoryRepository
-
 
617
				.selectAllByreferenceIdandreferenceType(reference, referenceType);
-
 
618
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
-
 
619
		if (!userWalletHistory.isEmpty()) {
-
 
620
 
-
 
621
			UserWalletHistory history = new UserWalletHistory();
-
 
622
			if (TransactionType.CREDIT.equals(transactiontype)) {
-
 
623
				userWallet.setAmount(userWallet.getAmount() + amount);
-
 
624
				history.setAmount(amount);
-
 
625
				history.setDescription(description);
-
 
626
				history.setReference(reference);
-
 
627
				history.setWalletId(userWallet.getId());
-
 
628
				history.setReferenceType(referenceType);
-
 
629
				history.setTimestamp(LocalDateTime.now());
-
 
630
 
-
 
631
			} else if (TransactionType.DEBIT.equals(transactiontype)) {
-
 
632
				userWallet.setAmount(userWallet.getAmount() - amount);
-
 
633
				history.setAmount(-amount);
-
 
634
				history.setDescription(description);
-
 
635
				history.setWalletId(userWallet.getId());
-
 
636
				history.setReference(reference);
-
 
637
				history.setReferenceType(referenceType);
-
 
638
				history.setTimestamp(LocalDateTime.now());
-
 
639
 
-
 
640
			}
-
 
641
			userWalletHistoryRepository.persit(history);
-
 
642
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
-
 
643
		} else {
-
 
644
 
-
 
645
			ManualPaymentType paymentType = manualPaymentRequestRepository.selectByReferenceType(referenceType);
-
 
646
 
-
 
647
			if (paymentType == null) {
-
 
648
				paymentType = new ManualPaymentType();
-
 
649
			}
-
 
650
 
-
 
651
			paymentType.setReferenceType(referenceType);
-
 
652
			try {
-
 
653
				paymentType.setCounter(paymentType.getCounter() + 1);
-
 
654
			} catch (Exception e) {
-
 
655
				paymentType.setCounter(1);
-
 
656
			}
-
 
657
			manualPaymentRequestRepository.persist(paymentType);
-
 
658
 
-
 
659
			UserWalletHistory history = new UserWalletHistory();
-
 
660
 
-
 
661
			if (TransactionType.CREDIT.equals(transactiontype)) {
-
 
662
				userWallet.setAmount(userWallet.getAmount() + amount);
-
 
663
				history.setAmount(amount);
-
 
664
				history.setDescription(description);
-
 
665
				history.setReference(paymentType.getCounter());
-
 
666
				history.setWalletId(userWallet.getId());
-
 
667
				history.setReferenceType(referenceType);
-
 
668
				history.setTimestamp(LocalDateTime.now());
-
 
669
 
-
 
670
			} else if (TransactionType.DEBIT.equals(transactiontype)) {
-
 
671
				userWallet.setAmount(userWallet.getAmount() - amount);
-
 
672
				history.setAmount(-amount);
-
 
673
				history.setDescription(description);
-
 
674
				history.setWalletId(userWallet.getId());
-
 
675
				history.setReference(paymentType.getCounter());
-
 
676
				history.setReferenceType(referenceType);
-
 
677
				history.setTimestamp(LocalDateTime.now());
-
 
678
 
-
 
679
			}
-
 
680
			userWalletHistoryRepository.persit(history);
-
 
681
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
-
 
682
		}
-
 
683
		return "response";
-
 
684
 
-
 
685
	}
533
}
686
}