Subversion Repositories SmartDukaan

Rev

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

Rev 27345 Rev 28515
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
-
 
3
import java.io.ByteArrayInputStream;
-
 
4
import java.io.ByteArrayOutputStream;
-
 
5
import java.io.InputStream;
3
import java.text.MessageFormat;
6
import java.text.MessageFormat;
4
import java.time.LocalDateTime;
7
import java.time.LocalDateTime;
-
 
8
import java.time.ZoneId;
5
import java.util.ArrayList;
9
import java.util.ArrayList;
6
import java.util.Collections;
10
import java.util.Collections;
-
 
11
import java.util.Date;
-
 
12
import java.util.HashMap;
7
import java.util.List;
13
import java.util.List;
8
import java.util.Map;
14
import java.util.Map;
9
import java.util.stream.Collectors;
15
import java.util.stream.Collectors;
10
 
16
 
11
import javax.mail.internet.InternetAddress;
17
import javax.mail.internet.InternetAddress;
Line 14... Line 20...
14
import javax.swing.SortOrder;
20
import javax.swing.SortOrder;
15
import javax.transaction.Transactional;
21
import javax.transaction.Transactional;
16
 
22
 
17
import org.apache.logging.log4j.LogManager;
23
import org.apache.logging.log4j.LogManager;
18
import org.apache.logging.log4j.Logger;
24
import org.apache.logging.log4j.Logger;
-
 
25
import org.apache.poi.ss.usermodel.Cell;
-
 
26
import org.apache.poi.ss.usermodel.CellStyle;
-
 
27
import org.apache.poi.ss.usermodel.CreationHelper;
-
 
28
import org.apache.poi.ss.usermodel.Sheet;
-
 
29
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
19
import org.json.JSONObject;
30
import org.json.JSONObject;
20
import org.springframework.beans.factory.annotation.Autowired;
31
import org.springframework.beans.factory.annotation.Autowired;
-
 
32
import org.springframework.core.io.InputStreamResource;
-
 
33
import org.springframework.http.HttpHeaders;
-
 
34
import org.springframework.http.HttpStatus;
-
 
35
import org.springframework.http.ResponseEntity;
21
import org.springframework.mail.javamail.JavaMailSender;
36
import org.springframework.mail.javamail.JavaMailSender;
22
import org.springframework.mail.javamail.MimeMessageHelper;
37
import org.springframework.mail.javamail.MimeMessageHelper;
23
import org.springframework.stereotype.Controller;
38
import org.springframework.stereotype.Controller;
24
import org.springframework.ui.Model;
39
import org.springframework.ui.Model;
25
import org.springframework.web.bind.annotation.PostMapping;
40
import org.springframework.web.bind.annotation.PostMapping;
Line 42... Line 57...
42
import com.spice.profitmandi.dao.entity.transaction.UnsettledPayment;
57
import com.spice.profitmandi.dao.entity.transaction.UnsettledPayment;
43
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
58
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
44
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
59
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
45
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
60
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
46
import com.spice.profitmandi.dao.enumuration.transaction.TransactionType;
61
import com.spice.profitmandi.dao.enumuration.transaction.TransactionType;
47
import com.spice.profitmandi.dao.model.ContentPojo;
62
import com.spice.profitmandi.dao.model.AccountStatementModel;
48
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
63
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
49
import com.spice.profitmandi.dao.repository.catalog.ManualPaymentRequestRepository;
64
import com.spice.profitmandi.dao.repository.catalog.ManualPaymentRequestRepository;
50
import com.spice.profitmandi.dao.repository.catalog.UnsettledPaymentsRepository;
65
import com.spice.profitmandi.dao.repository.catalog.UnsettledPaymentsRepository;
51
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
66
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
52
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
67
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
53
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
68
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
54
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
69
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
-
 
70
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
-
 
71
import com.spice.profitmandi.dao.repository.transaction.StatementDetailModel;
55
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
72
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
56
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
73
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
57
import com.spice.profitmandi.service.authentication.RoleManager;
74
import com.spice.profitmandi.service.authentication.RoleManager;
58
import com.spice.profitmandi.service.user.RetailerService;
75
import com.spice.profitmandi.service.user.RetailerService;
59
import com.spice.profitmandi.service.wallet.WalletService;
76
import com.spice.profitmandi.service.wallet.WalletService;
Line 65... Line 82...
65
 
82
 
66
@Controller
83
@Controller
67
@Transactional(rollbackOn = Throwable.class)
84
@Transactional(rollbackOn = Throwable.class)
68
public class WalletController {
85
public class WalletController {
69
 
86
 
-
 
87
	private static final int IDX_PERIOD = 1;
-
 
88
	private static final int OPENING_BALANCE = 2;
-
 
89
	private static final int ACTIVATION_SCHEME = 4;
-
 
90
	private static final int ADVANCE_AMOUNT = 5;
-
 
91
	private static final int AUTOMATED_ADVANCE = 6;
-
 
92
	private static final int BRAND_PAYOUT = 7;
-
 
93
	private static final int INVESTMENT_PAYOUT = 8;
-
 
94
	private static final int OTHERS = 9;
-
 
95
	private static final int PREBOOKING_ORDER = 10;
-
 
96
	private static final int PRICE_DROP = 11;
-
 
97
	private static final int PURCHASE = 12;
-
 
98
	private static final int PURCHASE_BILLED = 13;
-
 
99
	private static final int PURCHASE_PENDING_BILLING = 14;
-
 
100
	private static final int PURCHASE_CANCELLED = 15;
-
 
101
	private static final int RECHARGE = 16;
-
 
102
	private static final int REFUND = 17;
-
 
103
	private static final int SCHEME_IN = 18;
-
 
104
	private static final int SCHEME_OUT = 19;
-
 
105
	private static final Map<WalletReferenceType, Integer> walletReferenceMap = new HashMap<>();
-
 
106
	static {
-
 
107
		walletReferenceMap.put(WalletReferenceType.ACTIVATION_SCHEME, ACTIVATION_SCHEME);
-
 
108
		walletReferenceMap.put(WalletReferenceType.ADVANCE_AMOUNT, ADVANCE_AMOUNT);
-
 
109
		walletReferenceMap.put(WalletReferenceType.AUTOMATED_ADVANCE, AUTOMATED_ADVANCE);
-
 
110
		walletReferenceMap.put(WalletReferenceType.BRAND_PAYOUT, BRAND_PAYOUT);
-
 
111
		walletReferenceMap.put(WalletReferenceType.INVESTMENT_PAYOUT, INVESTMENT_PAYOUT);
-
 
112
		walletReferenceMap.put(WalletReferenceType.OTHERS, OTHERS);
-
 
113
		walletReferenceMap.put(WalletReferenceType.PREBOOKING_ORDER, PREBOOKING_ORDER);
-
 
114
		walletReferenceMap.put(WalletReferenceType.PRICE_DROP, PRICE_DROP);
-
 
115
		walletReferenceMap.put(WalletReferenceType.PURCHASE, PURCHASE);
-
 
116
		walletReferenceMap.put(WalletReferenceType.RECHARGE, RECHARGE);
-
 
117
		walletReferenceMap.put(WalletReferenceType.REFUND, REFUND);
-
 
118
		walletReferenceMap.put(WalletReferenceType.SCHEME_IN, SCHEME_IN);
-
 
119
		walletReferenceMap.put(WalletReferenceType.SCHEME_OUT, SCHEME_OUT);
-
 
120
 
-
 
121
	}
-
 
122
	private static final int GRAND_TOTAL = 20;
-
 
123
 
-
 
124
	private static final int CLOSING_BALANCE = 22;
-
 
125
	private static final int PENDING_GRN = 23;
-
 
126
	private static final int ACTIVATED_IMEIS = 24;
-
 
127
 
70
	@Autowired
128
	@Autowired
71
	private CookiesProcessor cookiesProcessor;
129
	private CookiesProcessor cookiesProcessor;
72
 
130
 
73
	@Autowired
131
	@Autowired
74
	private WalletService walletService;
132
	private WalletService walletService;
Line 84... Line 142...
84
 
142
 
85
	@Autowired
143
	@Autowired
86
	private UserAccountRepository userAccountRepository;
144
	private UserAccountRepository userAccountRepository;
87
 
145
 
88
	@Autowired
146
	@Autowired
-
 
147
	private OrderRepository orderRepository;
-
 
148
 
-
 
149
	@Autowired
89
	JavaMailSender mailSender;
150
	JavaMailSender mailSender;
90
 
151
 
91
	@Autowired
152
	@Autowired
92
	private UnsettledPaymentsRepository unsettledPaymentsRepository;
153
	private UnsettledPaymentsRepository unsettledPaymentsRepository;
93
 
154
 
Line 110... Line 171...
110
	private RetailerRepository retailerRepository;
171
	private RetailerRepository retailerRepository;
111
 
172
 
112
	@Autowired
173
	@Autowired
113
	private UserRepository userRepository;
174
	private UserRepository userRepository;
114
	private static final Logger LOGGER = LogManager.getLogger(WalletController.class);
175
	private static final Logger LOGGER = LogManager.getLogger(WalletController.class);
115
	
-
 
116
	
176
 
117
	@PostMapping(value = "/wallet/upload")
177
	@PostMapping(value = "/wallet/upload")
118
	public String uploadWalletBulk(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model)
178
	public String uploadWalletBulk(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model)
119
			throws Exception {
179
			throws Exception {
120
		List<WalletHistoryModel> walletHistoryModelList = ExcelUtils.parseWalletBulkCredit(file.getInputStream());
180
		List<WalletHistoryModel> walletHistoryModelList = ExcelUtils.parseWalletBulkCredit(file.getInputStream());
121
		for(WalletHistoryModel walletHistoryModel : walletHistoryModelList) {
181
		for (WalletHistoryModel walletHistoryModel : walletHistoryModelList) {
122
			if(walletHistoryModel.getReference()==0) {
182
			if (walletHistoryModel.getReference() == 0) {
123
				ManualPaymentType paymentType =
183
				ManualPaymentType paymentType = manualPaymentRequestRepository
124
						manualPaymentRequestRepository.selectByReferenceType(walletHistoryModel.getWalletReferenceType());
184
						.selectByReferenceType(walletHistoryModel.getWalletReferenceType());
125
				if (paymentType == null) {
185
				if (paymentType == null) {
126
					paymentType = new ManualPaymentType();
186
					paymentType = new ManualPaymentType();
127
					paymentType.setReferenceType(walletHistoryModel.getWalletReferenceType());
187
					paymentType.setReferenceType(walletHistoryModel.getWalletReferenceType());
128
				}
188
				}
129
				paymentType.setCounter(paymentType.getCounter() + 1);
189
				paymentType.setCounter(paymentType.getCounter() + 1);
130
				manualPaymentRequestRepository.persist(paymentType);
190
				manualPaymentRequestRepository.persist(paymentType);
131
				int reference = paymentType.getCounter();
191
				int reference = paymentType.getCounter();
132
				walletService.addAmountToWallet(walletHistoryModel.getFofoId(), reference, walletHistoryModel.getWalletReferenceType(), 
192
				walletService.addAmountToWallet(walletHistoryModel.getFofoId(), reference,
-
 
193
						walletHistoryModel.getWalletReferenceType(), walletHistoryModel.getDescription(),
133
						walletHistoryModel.getDescription(), (float)walletHistoryModel.getAmount(), walletHistoryModel.getBusinessDate());
194
						(float) walletHistoryModel.getAmount(), walletHistoryModel.getBusinessDate());
134
			}  else {
195
			} else {
135
				walletService.addAmountToWallet(walletHistoryModel.getFofoId(), walletHistoryModel.getReference(), walletHistoryModel.getWalletReferenceType(), 
196
				walletService.addAmountToWallet(walletHistoryModel.getFofoId(), walletHistoryModel.getReference(),
-
 
197
						walletHistoryModel.getWalletReferenceType(), walletHistoryModel.getDescription(),
136
						walletHistoryModel.getDescription(), (float)walletHistoryModel.getAmount(), walletHistoryModel.getBusinessDate());
198
						(float) walletHistoryModel.getAmount(), walletHistoryModel.getBusinessDate());
137
			}
199
			}
138
		}
200
		}
139
 
201
 
140
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
202
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
141
		return "response";
203
		return "response";
Line 395... Line 457...
395
			throws Exception {
457
			throws Exception {
396
 
458
 
397
		AddWalletRequest addWalletRequest = addWalletRequestRepository.selectById(walletRequestid);
459
		AddWalletRequest addWalletRequest = addWalletRequestRepository.selectById(walletRequestid);
398
		if (addWalletRequest.getStatus().equals(AddWalletRequestStatus.pending)) {
460
		if (addWalletRequest.getStatus().equals(AddWalletRequestStatus.pending)) {
399
			walletService.addAmountToWallet(addWalletRequest.getRetailerId(), walletRequestid,
461
			walletService.addAmountToWallet(addWalletRequest.getRetailerId(), walletRequestid,
400
					WalletReferenceType.ADVANCE_AMOUNT, "ntfs/rgfs", addWalletRequest.getAmount(), addWalletRequest.getCreateTimestamp());
462
					WalletReferenceType.ADVANCE_AMOUNT, "ntfs/rgfs", addWalletRequest.getAmount(),
-
 
463
					addWalletRequest.getCreateTimestamp());
401
			addWalletRequest.setStatus(AddWalletRequestStatus.approved);
464
			addWalletRequest.setStatus(AddWalletRequestStatus.approved);
402
			addWalletRequest.setUpdateTimestamp(LocalDateTime.now());
465
			addWalletRequest.setUpdateTimestamp(LocalDateTime.now());
403
			addWalletRequestRepository.persist(addWalletRequest);
466
			addWalletRequestRepository.persist(addWalletRequest);
404
			unsettledPaymentsRepository.deleteById(id);
467
			unsettledPaymentsRepository.deleteById(id);
405
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
468
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
Line 578... Line 641...
578
		model.addAttribute("startDate", startDate);
641
		model.addAttribute("startDate", startDate);
579
		model.addAttribute("endDate", endDate);
642
		model.addAttribute("endDate", endDate);
580
		return "walletStatement";
643
		return "walletStatement";
581
	}
644
	}
582
 
645
 
-
 
646
	@RequestMapping(value = "/account/statement", method = RequestMethod.GET)
-
 
647
	public ResponseEntity<?> getAccountStatement(HttpServletRequest request, @RequestParam LocalDateTime startDate,
-
 
648
			@RequestParam LocalDateTime endDate, Model model, @RequestParam(defaultValue = "0") int fofoId)
-
 
649
			throws Exception {
-
 
650
		boolean isAdmin = roleManager.isAdmin(cookiesProcessor.getCookiesObject(request).getRoleIds());
-
 
651
		if (fofoId > 0) {
-
 
652
			if (!isAdmin) {
-
 
653
				throw new ProfitMandiBusinessException("Unauthorised access", "PartnerId", "Permission Denied");
-
 
654
			}
-
 
655
		} else {
-
 
656
			fofoId = cookiesProcessor.getCookiesObject(request).getFofoId();
-
 
657
		}
-
 
658
		float openingBalance = walletService.getOpeningTillExcludingPurchase(fofoId, startDate);
-
 
659
		float closingBalance = walletService.getOpeningTillExcludingPurchase(fofoId, endDate);
-
 
660
		List<AccountStatementModel> accountModels = userWalletRepository.getStatement(fofoId, startDate, endDate)
-
 
661
				.stream().filter(x -> !x.getWalletReferenceType().equals(WalletReferenceType.PURCHASE))
-
 
662
				.collect(Collectors.toList());
-
 
663
		Map<WalletReferenceType, AccountStatementModel> accountModelsMap = accountModels.stream()
-
 
664
				.collect(Collectors.toMap(AccountStatementModel::getWalletReferenceType, x -> x));
-
 
665
		InputStream is = getClass().getClassLoader().getResourceAsStream("account-statement.xlsx");
-
 
666
		String partnerName = retailerService.getAllFofoRetailerIdNameMap().get(fofoId);
-
 
667
		List<StatementDetailModel> details = orderRepository.selectDetailsBetween(fofoId, startDate, endDate);
-
 
668
		float openingAmount = orderRepository.selectOpeningAmount(fofoId, startDate);
-
 
669
		float closingAmount = orderRepository.selectOpeningAmount(fofoId, endDate);
-
 
670
		LOGGER.info("Opening - {}, Closing - {}", openingAmount, closingAmount);
-
 
671
		ByteArrayOutputStream byteArrayOutputStream = this.populateData(is, openingBalance - openingAmount,
-
 
672
				closingBalance - closingAmount, partnerName, accountModelsMap, startDate, endDate, details);
-
 
673
 
-
 
674
		final HttpHeaders headers = new HttpHeaders();
-
 
675
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
-
 
676
		headers.set("Content-disposition",
-
 
677
				"inline; filename=account-statement." + StringUtils.toHyphenatedString(startDate.toLocalDate()) + "-"
-
 
678
						+ StringUtils.toHyphenatedString(endDate.toLocalDate()) + ".xlsx");
-
 
679
		headers.setContentLength(byteArrayOutputStream.toByteArray().length);
-
 
680
 
-
 
681
		final InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
-
 
682
		final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
-
 
683
 
-
 
684
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
-
 
685
 
-
 
686
	}
-
 
687
 
-
 
688
	private ByteArrayOutputStream populateData(InputStream is, float openingBalance, float closingBalance,
-
 
689
			String partnerName, Map<WalletReferenceType, AccountStatementModel> accountModels, LocalDateTime startDate,
-
 
690
			LocalDateTime endDate, List<StatementDetailModel> invoiceDetails) throws Exception {
-
 
691
		XSSFWorkbook workbook = new XSSFWorkbook(is);
-
 
692
		CreationHelper creationHelper = workbook.getCreationHelper();
-
 
693
		CellStyle style2 = workbook.createCellStyle();
-
 
694
		style2.setDataFormat(creationHelper.createDataFormat().getFormat(
-
 
695
				"mm/dd/yyyy hh:mm:ss"));
-
 
696
		
-
 
697
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
-
 
698
		Sheet sheet = workbook.getSheetAt(0);
-
 
699
		sheet.getRow(0).getCell(0).setCellValue(partnerName);
-
 
700
		sheet.getRow(1).getCell(1)
-
 
701
				.setCellValue(new Date(startDate.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()));
-
 
702
		sheet.getRow(1).getCell(2)
-
 
703
				.setCellValue(new Date(endDate.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()));
-
 
704
		sheet.getRow(2).getCell(2).setCellValue(openingBalance);
-
 
705
		long grandTotalDebit = 0l;
-
 
706
		long grandTotalCredit = 0l;
-
 
707
		int row = 4;
-
 
708
		for (Map.Entry<WalletReferenceType, AccountStatementModel> walletRow : accountModels.entrySet()) {
-
 
709
			sheet.createRow(row).createCell(0).setCellValue(walletRow.getKey().toString());
-
 
710
			sheet.getRow(row).createCell(1).setCellValue(walletRow.getValue().getDebit());
-
 
711
			sheet.getRow(row).createCell(2).setCellValue(walletRow.getValue().getCredit());
-
 
712
			grandTotalDebit += walletRow.getValue().getDebit();
-
 
713
			grandTotalCredit += walletRow.getValue().getCredit();
-
 
714
			row++;
-
 
715
		}
-
 
716
		for (StatementDetailModel statementDetailModel : invoiceDetails) {
-
 
717
			sheet.createRow(row).createCell(0).setCellValue(statementDetailModel.getInvoiceNumber());
-
 
718
			if (statementDetailModel.getAmount() > 0) {
-
 
719
				sheet.getRow(row).createCell(1).setCellValue(statementDetailModel.getAmount());
-
 
720
				sheet.getRow(row).createCell(2).setCellValue(0);
-
 
721
				grandTotalDebit += statementDetailModel.getAmount(); 
-
 
722
			} else {
-
 
723
				sheet.getRow(row).createCell(1).setCellValue(0);
-
 
724
				sheet.getRow(row).createCell(2).setCellValue(-statementDetailModel.getAmount());
-
 
725
				grandTotalCredit += -statementDetailModel.getAmount(); 
-
 
726
			}
-
 
727
			Cell dateCell = sheet.getRow(row).createCell(3);
-
 
728
			dateCell.setCellValue(statementDetailModel.getOnDate());
-
 
729
			dateCell.setCellStyle(style2);
-
 
730
			
-
 
731
			
-
 
732
 
-
 
733
			row += 1;
-
 
734
		}
-
 
735
		sheet.createRow(row).createCell(0).setCellValue("Grand Total");
-
 
736
		sheet.getRow(row).createCell(1).setCellValue(grandTotalDebit);
-
 
737
		sheet.getRow(row).createCell(2).setCellValue(grandTotalCredit);
-
 
738
		row += 2;
-
 
739
		sheet.createRow(row).createCell(0).setCellValue("Closing Balance");
-
 
740
		sheet.getRow(row).createCell(2).setCellValue(closingBalance);
-
 
741
		row++;
-
 
742
		/*
-
 
743
		 * sheet.createRow(row).createCell(0).setCellValue("Pending Grns");
-
 
744
		 * sheet.getRow(row).createCell(2).setCellValue(closingBalance); row++;
-
 
745
		 */
-
 
746
		try {
-
 
747
			workbook.write(bos);
-
 
748
		} finally {
-
 
749
			workbook.close();
-
 
750
			bos.close();
-
 
751
		}
-
 
752
 
-
 
753
		return bos;
-
 
754
	}
-
 
755
 
583
	@RequestMapping(value = "/manualPayment", method = RequestMethod.GET)
756
	@RequestMapping(value = "/manualPayment", method = RequestMethod.GET)
584
	public String ManualPayment(HttpServletRequest request, Model model) throws Exception {
757
	public String ManualPayment(HttpServletRequest request, Model model) throws Exception {
585
 
758
 
586
		List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
759
		List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
587
				.collect(Collectors.toList());
760
				.collect(Collectors.toList());
Line 665... Line 838...
665
 
838
 
666
	}
839
	}
667
 
840
 
668
	@RequestMapping(value = "/walletUpdate", method = RequestMethod.POST)
841
	@RequestMapping(value = "/walletUpdate", method = RequestMethod.POST)
669
	public String walletUpdate(HttpServletRequest request,
842
	public String walletUpdate(HttpServletRequest request,
670
			@RequestParam(name = "reference", defaultValue = "0") int reference,
843
			@RequestParam(name = "reference", defaultValue = "0") int reference, @RequestParam int referenceTypeValue,
671
			@RequestParam int referenceTypeValue, @RequestParam TransactionType transactiontype,
844
			@RequestParam TransactionType transactiontype, @RequestParam int amount, @RequestParam String description,
672
			@RequestParam int amount, @RequestParam String description, @RequestParam int retailerId, Model model,
845
			@RequestParam int retailerId, Model model, @RequestParam LocalDateTime businessTimestamp) throws Exception {
673
			@RequestParam LocalDateTime businessTimestamp)
-
 
674
			throws Exception {
-
 
675
		WalletReferenceType referenceType = WalletReferenceType.findByValue(referenceTypeValue);
846
		WalletReferenceType referenceType = WalletReferenceType.findByValue(referenceTypeValue);
676
		if (reference==0 && referenceType.getValue()>= WalletReferenceType.INCENTIVES.getValue()) {
847
		if (reference == 0 && referenceType.getValue() >= WalletReferenceType.INCENTIVES.getValue()) {
677
			LOGGER.error("referenceType: " + referenceType);
848
			LOGGER.error("referenceType: " + referenceType);
678
			ManualPaymentType paymentType = manualPaymentRequestRepository.selectByReferenceType(referenceType);
849
			ManualPaymentType paymentType = manualPaymentRequestRepository.selectByReferenceType(referenceType);
679
 
850
 
680
			if (paymentType == null) {
851
			if (paymentType == null) {
681
				paymentType = new ManualPaymentType();
852
				paymentType = new ManualPaymentType();
682
				paymentType.setReferenceType(referenceType);
853
				paymentType.setReferenceType(referenceType);
683
			}
854
			}
684
			paymentType.setCounter(paymentType.getCounter() + 1);
855
			paymentType.setCounter(paymentType.getCounter() + 1);
685
			manualPaymentRequestRepository.persist(paymentType);
856
			manualPaymentRequestRepository.persist(paymentType);
686
			reference = paymentType.getCounter();
857
			reference = paymentType.getCounter();
687
			
858
 
688
		} else if(reference==0){
859
		} else if (reference == 0) {
689
			throw new ProfitMandiBusinessException("RefrenceId", reference,
860
			throw new ProfitMandiBusinessException("RefrenceId", reference,
690
					"System specific reference type cant be used manually");
861
					"System specific reference type cant be used manually");
691
		}
862
		}
692
			
863
 
693
		List<UserWalletHistory> userWalletHistoryList = userWalletHistoryRepository
864
		List<UserWalletHistory> userWalletHistoryList = userWalletHistoryRepository
694
				.selectAllByreferenceIdandreferenceType(reference, referenceType);
865
				.selectAllByreferenceIdandreferenceType(reference, referenceType);
695
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
866
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
696
		int walletId = userWallet.getId();
867
		int walletId = userWallet.getId();
697
		int walletAmount = walletService.getWalletAmount(retailerId);
868
		int walletAmount = walletService.getWalletAmount(retailerId);
698
 
869
 
699
		int returnReference = 0;
870
		int returnReference = 0;
700
		UserWalletHistory newUserWalletHistory = new UserWalletHistory();
871
		UserWalletHistory newUserWalletHistory = new UserWalletHistory();
701
 
872
 
702
		if (!userWalletHistoryList.isEmpty()) {
873
		if (!userWalletHistoryList.isEmpty()) {
703
			long validRetailerEntries = userWalletHistoryList.stream()
874
			long validRetailerEntries = userWalletHistoryList.stream().filter(x -> x.getWalletId() == walletId).count();
704
					.filter(x-> x.getWalletId() == walletId)
-
 
705
					.count();
-
 
706
			if(validRetailerEntries == 0) {
875
			if (validRetailerEntries == 0) {
707
				throw new ProfitMandiBusinessException("RefrenceId", reference,
876
				throw new ProfitMandiBusinessException("RefrenceId", reference,
708
						"Reference Id assign to Other  partner");
877
						"Reference Id assign to Other  partner");
709
			}
878
			}
710
		}
879
		}
711
		
880
 
712
		if (TransactionType.DEBIT.equals(transactiontype)) {
881
		if (TransactionType.DEBIT.equals(transactiontype)) {
713
			amount = -amount;
882
			amount = -amount;
714
		}
883
		}
715
		
884
 
716
		userWallet.setAmount(walletAmount + amount);
885
		userWallet.setAmount(walletAmount + amount);
717
		newUserWalletHistory.setAmount(amount);
886
		newUserWalletHistory.setAmount(amount);
718
		newUserWalletHistory.setBusinessTimestamp(businessTimestamp);
887
		newUserWalletHistory.setBusinessTimestamp(businessTimestamp);
719
		newUserWalletHistory.setDescription(description);
888
		newUserWalletHistory.setDescription(description);
720
		newUserWalletHistory.setReference(reference);
889
		newUserWalletHistory.setReference(reference);