Subversion Repositories SmartDukaan

Rev

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