Subversion Repositories SmartDukaan

Rev

Rev 30316 | Rev 33256 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
27561 amit.gupta 1
package com.smartdukaan.cron.scheduled;
2
 
28531 amit.gupta 3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
4
import com.spice.profitmandi.common.util.FormattingUtils;
27561 amit.gupta 5
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
6
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
7
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
8
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
28531 amit.gupta 9
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
27561 amit.gupta 10
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
11
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
12
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
30767 amit.gupta 13
import com.spice.profitmandi.dao.repository.transaction.TransactionRepository;
28531 amit.gupta 14
import com.spice.profitmandi.service.user.RetailerService;
15
import com.spice.profitmandi.service.wallet.WalletService;
30316 amit.gupta 16
import in.shop2020.model.v1.order.WalletReferenceType;
17
import org.apache.logging.log4j.LogManager;
18
import org.apache.logging.log4j.Logger;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.stereotype.Component;
21
import org.springframework.transaction.annotation.Transactional;
27561 amit.gupta 22
 
30316 amit.gupta 23
import java.time.LocalDate;
24
import java.time.LocalDateTime;
25
import java.time.LocalTime;
26
import java.util.List;
27
import java.util.Map;
28
import java.util.stream.Collectors;
28531 amit.gupta 29
 
30316 amit.gupta 30
;
31
 
27561 amit.gupta 32
@Component
33
@Transactional(rollbackFor = Throwable.class)
34
public class InvestmentRelatedTasks {
35
	@Autowired
36
	RetailerService retailerService;
37
	@Autowired
38
	SchemeInOutRepository schemeInOutRepository;
28531 amit.gupta 39
 
27571 amit.gupta 40
	private static final Logger LOGGER = LogManager.getLogger(InvestmentRelatedTasks.class);
27561 amit.gupta 41
 
42
	@Autowired
43
	InventoryItemRepository inventoryItemRepository;
28531 amit.gupta 44
 
27561 amit.gupta 45
	@Autowired
46
	PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
30767 amit.gupta 47
 
48
 
28531 amit.gupta 49
	@Autowired
30767 amit.gupta 50
	TransactionRepository transactionRepository;
51
 
52
	@Autowired
28531 amit.gupta 53
	WalletService walletService;
54
 
55
	public void payMonthlyInvestment() throws ProfitMandiBusinessException {
27561 amit.gupta 56
		LocalDate firstDateOfCurrentMonth = LocalDateTime.now().withDayOfMonth(1).toLocalDate();
57
		LocalDate startOfPreviousMonth = firstDateOfCurrentMonth.minusMonths(1);
28531 amit.gupta 58
		int referenceId = Integer.parseInt(FormattingUtils.getYearMonth(startOfPreviousMonth.atStartOfDay()));
27561 amit.gupta 59
		LocalDate lastOfPreviousMonth = firstDateOfCurrentMonth.minusDays(1);
28531 amit.gupta 60
		List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository
61
				.selectAll(startOfPreviousMonth, lastOfPreviousMonth);
62
		Map<Integer, Long> investmentMaintainedDaysMap = partnerDailyInvestments.stream()
63
				.filter(x -> x.getShortPercentage() <= 10)
64
				.collect(Collectors.groupingBy(x -> x.getFofoId(), Collectors.counting()));
27571 amit.gupta 65
		LOGGER.info("investmentMaintainedDaysMap {}", investmentMaintainedDaysMap);
28531 amit.gupta 66
		List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectAllPending(SchemeType.INVESTMENT,
67
				startOfPreviousMonth.atStartOfDay(), firstDateOfCurrentMonth.atStartOfDay());
68
		Map<Integer, List<SchemeInOut>> inventoryItemIdSchemeMap = schemeInOuts.stream()
69
				.collect(Collectors.groupingBy(x -> x.getInventoryItemId()));
27561 amit.gupta 70
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIdSchemeMap.keySet());
28531 amit.gupta 71
		Map<Integer, List<Integer>> retailerInventoryItemIdMap = inventoryItems.stream().collect(
72
				Collectors.groupingBy(x -> x.getFofoId(), Collectors.mapping(x -> x.getId(), Collectors.toList())));
28738 amit.gupta 73
		System.out.println("Fofo Id\tInvestment Maintained Days\tEligible payout");
28531 amit.gupta 74
		for (Map.Entry<Integer, List<Integer>> retailerEntry : retailerInventoryItemIdMap.entrySet()) {
27561 amit.gupta 75
			int fofoId = retailerEntry.getKey();
28531 amit.gupta 76
			long investmentMaintainedDays = investmentMaintainedDaysMap.get(fofoId) == null ? 0
77
					: investmentMaintainedDaysMap.get(fofoId);
78
 
79
			List<SchemeInOut> schemeInouts = retailerEntry.getValue().stream().map(x -> inventoryItemIdSchemeMap.get(x))
80
					.flatMap(List::stream).filter(x -> x.getRolledBackTimestamp() == null).collect(Collectors.toList());
81
			float totalAmount = 0;
30767 amit.gupta 82
			LocalDateTime firstBillingDate = transactionRepository.getFirstBillingDate(fofoId);
83
			boolean sameYearMonth = firstBillingDate.getMonth() == startOfPreviousMonth.getMonth() && firstBillingDate.getYear() == startOfPreviousMonth.getYear();
84
			for (SchemeInOut sio : schemeInouts) {
85
				if (sameYearMonth) {
86
					sio.setStatusDescription("Investment payout fully disbursed for first month");
28531 amit.gupta 87
					sio.setStatus(SchemePayoutStatus.CREDITED);
88
					sio.setCreditTimestamp(LocalDateTime.now());
89
					totalAmount += sio.getAmount();
90
				} else {
30767 amit.gupta 91
					if (investmentMaintainedDays < 8) {
92
						sio.setStatus(SchemePayoutStatus.REJECTED);
93
						//sio.setRolledBackTimestamp(LocalDateTime.now());
94
						sio.setStatusDescription("Investment maintained for " + investmentMaintainedDays + "(< 8) days");
95
					} else if (investmentMaintainedDays < 12) {
96
						sio.setStatus(SchemePayoutStatus.CREDITED);
97
						sio.setAmount(sio.getAmount() / 2);
98
						sio.setCreditTimestamp(LocalDateTime.now());
99
						sio.setStatusDescription("Investment maintained for " + investmentMaintainedDays + "(< 12) days");
100
						totalAmount += sio.getAmount();
101
					} else {
102
						sio.setStatus(SchemePayoutStatus.CREDITED);
103
						sio.setCreditTimestamp(LocalDateTime.now());
104
						totalAmount += sio.getAmount();
105
					}
28531 amit.gupta 106
				}
107
			}
30767 amit.gupta 108
			if (totalAmount > 0) {
29274 amit.gupta 109
				String description = "Investment margin paid for " + FormattingUtils.formatYearMonth(startOfPreviousMonth.atStartOfDay());
30767 amit.gupta 110
				if (investmentMaintainedDays < 12) {
111
					description += ", as maintained for " + investmentMaintainedDays + "(< 12) days";
28531 amit.gupta 112
				}
30767 amit.gupta 113
				walletService.addAmountToWallet(fofoId, referenceId, WalletReferenceType.INVESTMENT_PAYOUT, description, totalAmount, lastOfPreviousMonth.atTime(LocalTime.MAX));
28531 amit.gupta 114
			}
115
			System.out.printf("%d\t%d\t%f%n", fofoId, investmentMaintainedDays, totalAmount);
116
		}
117
	}
118
 
119
	public void evaluateActualInvestmentPayout() {
120
		LocalDate firstDateOfCurrentMonth = LocalDateTime.now().withDayOfMonth(1).toLocalDate();
121
		LocalDate startOfPreviousMonth = firstDateOfCurrentMonth.minusMonths(1);
122
		LocalDate lastOfPreviousMonth = firstDateOfCurrentMonth.minusDays(1);
123
		List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository
124
				.selectAll(startOfPreviousMonth, lastOfPreviousMonth);
125
		Map<Integer, Long> investmentMaintainedDaysMap = partnerDailyInvestments.stream()
126
				.filter(x -> x.getShortPercentage() <= 10)
127
				.collect(Collectors.groupingBy(x -> x.getFofoId(), Collectors.counting()));
128
		LOGGER.info("investmentMaintainedDaysMap {}", investmentMaintainedDaysMap);
129
		List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectAllPending(SchemeType.INVESTMENT,
130
				startOfPreviousMonth.atStartOfDay(), firstDateOfCurrentMonth.atStartOfDay());
131
		Map<Integer, List<SchemeInOut>> inventoryItemIdSchemeMap = schemeInOuts.stream()
132
				.collect(Collectors.groupingBy(x -> x.getInventoryItemId()));
133
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIdSchemeMap.keySet());
134
		Map<Integer, List<Integer>> retailerInventoryItemIdMap = inventoryItems.stream().collect(
135
				Collectors.groupingBy(x -> x.getFofoId(), Collectors.mapping(x -> x.getId(), Collectors.toList())));
28738 amit.gupta 136
		System.out.println("Fofo Id\tInvestment Maintained Days\tEligible payout");
28531 amit.gupta 137
		for (Map.Entry<Integer, List<Integer>> retailerEntry : retailerInventoryItemIdMap.entrySet()) {
138
			int fofoId = retailerEntry.getKey();
139
			List<SchemeInOut> schemeInouts = retailerEntry.getValue().stream().map(x -> inventoryItemIdSchemeMap.get(x))
140
					.flatMap(List::stream).collect(Collectors.toList());
141
			double totalAmount = schemeInouts.stream().filter(x -> x.getRolledBackTimestamp() == null)
142
					.collect(Collectors.summingDouble(x -> x.getAmount()));
143
			long investmentMaintainedDays = investmentMaintainedDaysMap.get(fofoId) == null ? 0
144
					: investmentMaintainedDaysMap.get(fofoId);
145
			if (investmentMaintainedDays < 8) {
27577 amit.gupta 146
				totalAmount = 0;
28531 amit.gupta 147
			} else if (investmentMaintainedDays < 12) {
148
				totalAmount = totalAmount / 2;
27561 amit.gupta 149
			}
27568 amit.gupta 150
			System.out.printf("%d\t%d\t%f%n", fofoId, investmentMaintainedDays, totalAmount);
27561 amit.gupta 151
		}
152
	}
30316 amit.gupta 153
 
27561 amit.gupta 154
}