| 23724 |
amit.gupta |
1 |
package com.smartdukaan.cron.scheduled;
|
| 23723 |
amit.gupta |
2 |
|
| 24121 |
govind |
3 |
import java.io.IOException;
|
|
|
4 |
import java.io.InputStream;
|
| 23739 |
amit.gupta |
5 |
import java.sql.Timestamp;
|
| 24174 |
govind |
6 |
import java.text.MessageFormat;
|
| 23724 |
amit.gupta |
7 |
import java.time.LocalDate;
|
|
|
8 |
import java.time.LocalDateTime;
|
| 24256 |
amit.gupta |
9 |
import java.time.LocalTime;
|
| 23724 |
amit.gupta |
10 |
import java.time.temporal.ChronoUnit;
|
| 24256 |
amit.gupta |
11 |
import java.time.temporal.TemporalAmount;
|
|
|
12 |
import java.time.temporal.TemporalUnit;
|
| 23724 |
amit.gupta |
13 |
import java.util.ArrayList;
|
|
|
14 |
import java.util.Arrays;
|
| 23723 |
amit.gupta |
15 |
import java.util.HashMap;
|
| 24241 |
amit.gupta |
16 |
import java.util.HashSet;
|
| 23724 |
amit.gupta |
17 |
import java.util.List;
|
| 23723 |
amit.gupta |
18 |
import java.util.Map;
|
| 24242 |
amit.gupta |
19 |
import java.util.Optional;
|
| 23724 |
amit.gupta |
20 |
import java.util.stream.Collectors;
|
| 23723 |
amit.gupta |
21 |
|
| 24121 |
govind |
22 |
import javax.mail.MessagingException;
|
|
|
23 |
import javax.mail.internet.InternetAddress;
|
|
|
24 |
import javax.mail.internet.MimeMessage;
|
|
|
25 |
|
|
|
26 |
import org.apache.commons.codec.CharEncoding;
|
|
|
27 |
import org.apache.commons.io.IOUtils;
|
| 23929 |
amit.gupta |
28 |
import org.apache.commons.io.output.ByteArrayOutputStream;
|
| 23755 |
amit.gupta |
29 |
import org.apache.logging.log4j.LogManager;
|
|
|
30 |
import org.apache.logging.log4j.Logger;
|
| 24184 |
govind |
31 |
import org.apache.poi.EncryptedDocumentException;
|
|
|
32 |
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
| 23723 |
amit.gupta |
33 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 23933 |
amit.gupta |
34 |
import org.springframework.beans.factory.annotation.Qualifier;
|
| 23724 |
amit.gupta |
35 |
import org.springframework.beans.factory.annotation.Value;
|
| 23929 |
amit.gupta |
36 |
import org.springframework.core.io.ByteArrayResource;
|
|
|
37 |
import org.springframework.mail.javamail.JavaMailSender;
|
| 24121 |
govind |
38 |
import org.springframework.mail.javamail.MimeMessageHelper;
|
| 23723 |
amit.gupta |
39 |
import org.springframework.stereotype.Component;
|
| 23724 |
amit.gupta |
40 |
import org.springframework.transaction.annotation.Transactional;
|
| 23723 |
amit.gupta |
41 |
|
| 23724 |
amit.gupta |
42 |
import com.spice.profitmandi.common.enumuration.RechargeStatus;
|
| 24121 |
govind |
43 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 23929 |
amit.gupta |
44 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
| 23724 |
amit.gupta |
45 |
import com.spice.profitmandi.common.model.RechargeCredential;
|
| 24002 |
amit.gupta |
46 |
import com.spice.profitmandi.common.util.FileUtil;
|
| 23929 |
amit.gupta |
47 |
import com.spice.profitmandi.common.util.FormattingUtils;
|
|
|
48 |
import com.spice.profitmandi.common.util.Utils;
|
| 23724 |
amit.gupta |
49 |
import com.spice.profitmandi.dao.entity.dtr.DailyRecharge;
|
|
|
50 |
import com.spice.profitmandi.dao.entity.dtr.RechargeProvider;
|
|
|
51 |
import com.spice.profitmandi.dao.entity.dtr.RechargeProviderCreditWalletHistory;
|
|
|
52 |
import com.spice.profitmandi.dao.entity.dtr.RechargeTransaction;
|
|
|
53 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
| 23929 |
amit.gupta |
54 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
| 24249 |
amit.gupta |
55 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 24174 |
govind |
56 |
import com.spice.profitmandi.dao.entity.fofo.PartnerTargetDetails;
|
| 23724 |
amit.gupta |
57 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 24174 |
govind |
58 |
import com.spice.profitmandi.dao.entity.fofo.SaleHeadDetails;
|
| 24242 |
amit.gupta |
59 |
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
|
| 24241 |
amit.gupta |
60 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
|
| 23929 |
amit.gupta |
61 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 24250 |
amit.gupta |
62 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
| 24242 |
amit.gupta |
63 |
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
|
| 24249 |
amit.gupta |
64 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 24241 |
amit.gupta |
65 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
| 23724 |
amit.gupta |
66 |
import com.spice.profitmandi.dao.repository.dtr.DailyRechargeRepository;
|
| 23929 |
amit.gupta |
67 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 23724 |
amit.gupta |
68 |
import com.spice.profitmandi.dao.repository.dtr.RechargeProviderCreditWalletHistoryRepository;
|
|
|
69 |
import com.spice.profitmandi.dao.repository.dtr.RechargeProviderRepository;
|
|
|
70 |
import com.spice.profitmandi.dao.repository.dtr.RechargeTransactionRepository;
|
|
|
71 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
| 24249 |
amit.gupta |
72 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 24174 |
govind |
73 |
import com.spice.profitmandi.dao.repository.fofo.PartnerTargetRepository;
|
| 23724 |
amit.gupta |
74 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 24242 |
amit.gupta |
75 |
import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;
|
| 24241 |
amit.gupta |
76 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
| 23929 |
amit.gupta |
77 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
78 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
| 23724 |
amit.gupta |
79 |
import com.spice.profitmandi.service.recharge.provider.OxigenRechargeProviderService;
|
|
|
80 |
import com.spice.profitmandi.service.recharge.provider.ThinkWalnutDigitalRechargeProviderService;
|
|
|
81 |
import com.spice.profitmandi.service.scheme.SchemeService;
|
| 24121 |
govind |
82 |
import com.spice.profitmandi.service.slab.TargetSlabService;
|
| 23929 |
amit.gupta |
83 |
import com.spice.profitmandi.service.transaction.TransactionService;
|
|
|
84 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 23739 |
amit.gupta |
85 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 23723 |
amit.gupta |
86 |
|
| 24197 |
amit.gupta |
87 |
import in.shop2020.model.v1.order.WalletReferenceType;;
|
| 23739 |
amit.gupta |
88 |
|
| 23723 |
amit.gupta |
89 |
@Component
|
| 23724 |
amit.gupta |
90 |
@Transactional(rollbackFor = Throwable.class)
|
| 23723 |
amit.gupta |
91 |
public class ScheduledTasks {
|
|
|
92 |
|
| 23724 |
amit.gupta |
93 |
@Value("${oxigen.recharge.transaction.url}")
|
|
|
94 |
private String oxigenRechargeTransactionUrl;
|
| 23723 |
amit.gupta |
95 |
|
| 23724 |
amit.gupta |
96 |
@Value("${oxigen.recharge.enquiry.url}")
|
|
|
97 |
private String oxigenRechargeEnquiryUrl;
|
| 23723 |
amit.gupta |
98 |
|
| 23724 |
amit.gupta |
99 |
@Value("${oxigen.recharge.auth.key}")
|
|
|
100 |
private String oxigenRechargeAuthKey;
|
|
|
101 |
|
|
|
102 |
@Value("${oxigen.recharge.validation.url}")
|
|
|
103 |
private String oxigenRechargeValidationUrl;
|
|
|
104 |
|
|
|
105 |
@Value("${oxigen.recharge.validation.auth.key}")
|
|
|
106 |
private String oxigenRechargeValidationAuthKey;
|
|
|
107 |
|
|
|
108 |
@Value("${think.walnut.digital.recharge.transaction.mobile.url}")
|
|
|
109 |
private String thinkWalnutDigitalRechargeTransactionMobileUrl;
|
|
|
110 |
|
|
|
111 |
@Value("${think.walnut.digital.recharge.transaction.dth.url}")
|
|
|
112 |
private String thinkWalnutDigitalRechargeTransactionDthUrl;
|
|
|
113 |
|
|
|
114 |
@Value("${think.walnut.digital.recharge.enquiry.url}")
|
|
|
115 |
private String thinkWalnutDigitalRechargeEnquiryUrl;
|
|
|
116 |
|
|
|
117 |
@Value("${think.walnut.digital.recharge.balance.url}")
|
|
|
118 |
private String thinkWalnutDigitalRechargeBalanceUrl;
|
|
|
119 |
|
|
|
120 |
@Value("${think.walnut.digital.recharge.username}")
|
|
|
121 |
private String thinkWalnutDigitalRechargeUserName;
|
|
|
122 |
|
|
|
123 |
@Value("${think.walnut.digital.recharge.password}")
|
|
|
124 |
private String thinkWalnutDigitalRechargePassword;
|
|
|
125 |
|
|
|
126 |
@Value("${think.walnut.digital.recharge.auth.key}")
|
|
|
127 |
private String thinkWalnutDigitalRechargeAuthKey;
|
|
|
128 |
|
| 23723 |
amit.gupta |
129 |
@Autowired
|
| 23724 |
amit.gupta |
130 |
private PurchaseRepository purchaseRepository;
|
|
|
131 |
|
|
|
132 |
@Autowired
|
|
|
133 |
private SchemeService schemeService;
|
|
|
134 |
|
|
|
135 |
@Autowired
|
| 24241 |
amit.gupta |
136 |
private SchemeInOutRepository schemeInOutRepository;
|
|
|
137 |
|
|
|
138 |
@Autowired
|
| 23724 |
amit.gupta |
139 |
private RechargeTransactionRepository rechargeTransactionRepository;
|
|
|
140 |
|
|
|
141 |
@Autowired
|
|
|
142 |
private RechargeProviderCreditWalletHistoryRepository rechargeProviderCreditWalletHistoryRepository;
|
|
|
143 |
|
|
|
144 |
@Autowired
|
|
|
145 |
private FofoOrderRepository fofoOrderRepository;
|
| 24250 |
amit.gupta |
146 |
|
| 24249 |
amit.gupta |
147 |
@Autowired
|
|
|
148 |
private InventoryItemRepository inventoryItemRepository;
|
| 23929 |
amit.gupta |
149 |
|
| 23739 |
amit.gupta |
150 |
@Autowired
|
|
|
151 |
private WalletService walletService;
|
| 23724 |
amit.gupta |
152 |
|
|
|
153 |
@Autowired
|
|
|
154 |
private ThinkWalnutDigitalRechargeProviderService thinkWalnutDigitalRechargeProviderService;
|
|
|
155 |
|
|
|
156 |
@Autowired
|
|
|
157 |
private OxigenRechargeProviderService oxigenRechargeProviderService;
|
|
|
158 |
|
|
|
159 |
@Autowired
|
|
|
160 |
private RechargeProviderRepository rechargeProviderRepository;
|
|
|
161 |
|
|
|
162 |
@Autowired
|
| 24242 |
amit.gupta |
163 |
private ScanRecordRepository scanRecordRepository;
|
|
|
164 |
|
|
|
165 |
@Autowired
|
| 23724 |
amit.gupta |
166 |
private DailyRechargeRepository dailyRechargeRepository;
|
|
|
167 |
|
| 23929 |
amit.gupta |
168 |
@Autowired
|
|
|
169 |
private FofoStoreRepository fofoStoreRepository;
|
| 24177 |
govind |
170 |
|
| 24121 |
govind |
171 |
@Autowired
|
|
|
172 |
private TargetSlabService targetService;
|
| 23929 |
amit.gupta |
173 |
|
| 23724 |
amit.gupta |
174 |
@Value("${prod}")
|
|
|
175 |
private boolean prod;
|
|
|
176 |
|
| 23929 |
amit.gupta |
177 |
@Autowired
|
|
|
178 |
private RetailerService retailerService;
|
|
|
179 |
|
|
|
180 |
@Autowired
|
|
|
181 |
private TransactionService transactionService;
|
| 24250 |
amit.gupta |
182 |
|
| 24249 |
amit.gupta |
183 |
@Autowired
|
|
|
184 |
private ItemRepository itemRepository;
|
| 23929 |
amit.gupta |
185 |
|
|
|
186 |
@Autowired
|
|
|
187 |
private OrderRepository orderRepository;
|
|
|
188 |
|
|
|
189 |
@Autowired
|
| 24241 |
amit.gupta |
190 |
private SchemeRepository schemeRepository;
|
|
|
191 |
|
|
|
192 |
@Autowired
|
| 23929 |
amit.gupta |
193 |
private JavaMailSender mailSender;
|
| 24177 |
govind |
194 |
|
| 24174 |
govind |
195 |
@Autowired
|
|
|
196 |
private PartnerTargetRepository partnerTargetRepository;
|
| 24002 |
amit.gupta |
197 |
|
|
|
198 |
@Autowired
|
|
|
199 |
@Qualifier(value = "googleMailSender")
|
| 23932 |
amit.gupta |
200 |
private JavaMailSender googleMailSender;
|
| 23929 |
amit.gupta |
201 |
|
|
|
202 |
@Autowired
|
|
|
203 |
private InventoryService inventoryService;
|
|
|
204 |
|
| 23755 |
amit.gupta |
205 |
private static final Logger LOGGER = LogManager.getLogger(ScheduledTasks.class);
|
| 23724 |
amit.gupta |
206 |
|
|
|
207 |
public void generateDailyRecharge() {
|
|
|
208 |
List<RechargeProviderCreditWalletHistory> allCreditHistory = rechargeProviderCreditWalletHistoryRepository
|
|
|
209 |
.selectAll(0, 2000);
|
|
|
210 |
List<RechargeProvider> rechargeProviders = rechargeProviderRepository.selectAll();
|
|
|
211 |
rechargeProviders.stream().forEach(x -> x.setAmount(0));
|
|
|
212 |
|
|
|
213 |
rechargeProviders.stream().forEach(x -> {
|
|
|
214 |
Map<LocalDate, List<RechargeProviderCreditWalletHistory>> dateWiseProviderCreditsMap = allCreditHistory
|
|
|
215 |
.stream().filter(z -> z.getProviderId() == x.getId())
|
|
|
216 |
.collect(Collectors.groupingBy(x1 -> x1.getReceiveTimestamp().toLocalDate()));
|
|
|
217 |
|
|
|
218 |
LOGGER.info("dateWiseProviderCreditsMap -- {}", dateWiseProviderCreditsMap);
|
|
|
219 |
LocalDate endDate = LocalDate.now().plusDays(1);
|
|
|
220 |
float previousDayClosing = 0;
|
|
|
221 |
LocalDate date = LocalDate.of(2018, 4, 6);
|
|
|
222 |
while (date.isBefore(endDate)) {
|
|
|
223 |
List<RechargeTransaction> dateWiseRechargeTransactions = rechargeTransactionRepository
|
|
|
224 |
.selectAllBetweenTimestamp(Arrays.asList(RechargeStatus.values()), date.atStartOfDay(),
|
|
|
225 |
date.plusDays(1).atStartOfDay());
|
|
|
226 |
|
|
|
227 |
List<RechargeTransaction> successfulTransactions = dateWiseRechargeTransactions.stream()
|
|
|
228 |
.filter(y -> y.getStatus().equals(RechargeStatus.SUCCESS)).collect(Collectors.toList());
|
|
|
229 |
|
|
|
230 |
float dailyAmount = 0;
|
|
|
231 |
float totalCommission = 0;
|
|
|
232 |
for (RechargeTransaction rechargeTransaction : successfulTransactions) {
|
|
|
233 |
if (rechargeTransaction.getProviderId() == x.getId()) {
|
|
|
234 |
dailyAmount += rechargeTransaction.getAmount();
|
|
|
235 |
totalCommission += rechargeTransaction.getCommission();
|
|
|
236 |
}
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
List<RechargeProviderCreditWalletHistory> rechargeHistoryList = dateWiseProviderCreditsMap.get(date);
|
|
|
240 |
float dailyWalletRecharge = 0;
|
|
|
241 |
if (rechargeHistoryList != null) {
|
|
|
242 |
for (RechargeProviderCreditWalletHistory rechargeProviderCreditWalletHistory : rechargeHistoryList) {
|
|
|
243 |
if (rechargeProviderCreditWalletHistory.getProviderId() == x.getId()) {
|
|
|
244 |
dailyWalletRecharge += rechargeProviderCreditWalletHistory.getAmount();
|
|
|
245 |
}
|
|
|
246 |
}
|
|
|
247 |
}
|
|
|
248 |
if (dailyAmount > 0 || dailyWalletRecharge > 0) {
|
|
|
249 |
DailyRecharge dailyRecharge = null;
|
|
|
250 |
try {
|
|
|
251 |
dailyRecharge = dailyRechargeRepository.selectByProviderIdAndCreateDate(x.getId(), date);
|
|
|
252 |
} catch (Exception e) {
|
|
|
253 |
LOGGER.info("Could not find Recharge entry");
|
|
|
254 |
}
|
|
|
255 |
if (dailyRecharge == null) {
|
|
|
256 |
dailyRecharge = new DailyRecharge();
|
|
|
257 |
dailyRecharge.setCreateDate(date);
|
|
|
258 |
}
|
|
|
259 |
dailyRecharge.setOpeningBalance(previousDayClosing);
|
|
|
260 |
dailyRecharge.setProviderId(x.getId());
|
|
|
261 |
dailyRecharge.setWalletRechargeAmount(dailyWalletRecharge);
|
|
|
262 |
dailyRecharge.setTotalAmount(dailyAmount);
|
|
|
263 |
dailyRecharge.setTotalCommission(totalCommission);
|
|
|
264 |
float closingBalance = dailyRecharge.getOpeningBalance() + dailyWalletRecharge - dailyAmount;
|
|
|
265 |
dailyRecharge.setClosingBalance(closingBalance);
|
|
|
266 |
dailyRechargeRepository.persist(dailyRecharge);
|
|
|
267 |
x.setAmount(x.getAmount() + dailyRecharge.getClosingBalance() - dailyRecharge.getOpeningBalance());
|
|
|
268 |
previousDayClosing = dailyRecharge.getClosingBalance();
|
|
|
269 |
}
|
|
|
270 |
date = date.plusDays(1);
|
|
|
271 |
}
|
|
|
272 |
rechargeProviderRepository.persist(x);
|
|
|
273 |
});
|
| 23761 |
amit.gupta |
274 |
LOGGER.info("finished generating daily recharge");
|
| 23724 |
amit.gupta |
275 |
}
|
|
|
276 |
|
| 23738 |
amit.gupta |
277 |
public void reconcileRecharge() throws Exception {
|
| 23724 |
amit.gupta |
278 |
LocalDateTime fromDate = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS).minusDays(30);
|
|
|
279 |
LocalDateTime toDate = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS);
|
|
|
280 |
List<RechargeStatus> nonSuccessRechargeStatuses = new ArrayList<>(Arrays.asList(RechargeStatus.values()));
|
|
|
281 |
LOGGER.info("nonSuccessRechargeStatuses {} ", nonSuccessRechargeStatuses);
|
|
|
282 |
nonSuccessRechargeStatuses.remove(RechargeStatus.SUCCESS);
|
|
|
283 |
nonSuccessRechargeStatuses.remove(RechargeStatus.FAILED);
|
|
|
284 |
RechargeCredential thinkWalnutDigitalRechargeEnquiryCredential = new RechargeCredential();
|
|
|
285 |
thinkWalnutDigitalRechargeEnquiryCredential.setRechargeUrl(thinkWalnutDigitalRechargeEnquiryUrl);
|
|
|
286 |
thinkWalnutDigitalRechargeEnquiryCredential.setRechargeUserName(thinkWalnutDigitalRechargeUserName);
|
|
|
287 |
thinkWalnutDigitalRechargeEnquiryCredential.setRechargePassword(thinkWalnutDigitalRechargePassword);
|
|
|
288 |
thinkWalnutDigitalRechargeEnquiryCredential.setRechargeAuthKey(thinkWalnutDigitalRechargeAuthKey);
|
|
|
289 |
Map<String, RechargeStatus> requestRechargeStatusChanged = new HashMap<>();
|
|
|
290 |
List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository
|
|
|
291 |
.selectAllBetweenTimestamp(nonSuccessRechargeStatuses, fromDate, toDate);
|
|
|
292 |
for (RechargeTransaction rechargeTransaction : rechargeTransactions) {
|
|
|
293 |
try {
|
|
|
294 |
int providerId = rechargeTransaction.getProviderId();
|
|
|
295 |
if (providerId == 1) {
|
|
|
296 |
oxigenRechargeProviderService.doCheckStatusRequest(oxigenRechargeEnquiryUrl, oxigenRechargeAuthKey,
|
|
|
297 |
rechargeTransaction);
|
|
|
298 |
} else if (providerId == 2) {
|
|
|
299 |
thinkWalnutDigitalRechargeProviderService
|
|
|
300 |
.doCheckStatusRequest(thinkWalnutDigitalRechargeEnquiryCredential, rechargeTransaction);
|
|
|
301 |
}
|
|
|
302 |
if (rechargeTransaction.getStatus().equals(RechargeStatus.SUCCESS)
|
|
|
303 |
|| rechargeTransaction.getStatus().equals(RechargeStatus.FAILED)) {
|
|
|
304 |
requestRechargeStatusChanged.put(rechargeTransaction.getRequestId(),
|
|
|
305 |
rechargeTransaction.getStatus());
|
|
|
306 |
}
|
|
|
307 |
} catch (Exception e) {
|
|
|
308 |
LOGGER.info("Could not check status for Request {}", rechargeTransaction.getRequestId());
|
|
|
309 |
}
|
|
|
310 |
}
|
| 23738 |
amit.gupta |
311 |
LOGGER.info("Reconcile recharge ran successfully");
|
| 23724 |
amit.gupta |
312 |
}
|
| 24240 |
amit.gupta |
313 |
|
|
|
314 |
// TemporaryMethod
|
| 24237 |
amit.gupta |
315 |
public void migrateInvoice() {
|
|
|
316 |
List<FofoOrder> fofoOrders = fofoOrderRepository.selectFromSaleDate(LocalDateTime.now().minusDays(3));
|
|
|
317 |
Map<Integer, List<FofoOrder>> partnerOrdersMap = new HashMap<>();
|
| 24241 |
amit.gupta |
318 |
partnerOrdersMap = fofoOrders.stream()
|
|
|
319 |
.collect(Collectors.groupingBy(FofoOrder::getFofoId, Collectors.toList()));
|
| 24237 |
amit.gupta |
320 |
for (List<FofoOrder> orderList : partnerOrdersMap.values()) {
|
| 24240 |
amit.gupta |
321 |
int sequence = 0;
|
|
|
322 |
String prefix = "";
|
| 24241 |
amit.gupta |
323 |
List<FofoOrder> sortedList = orderList.stream().sorted((x1, x2) -> x1.getId() - x2.getId())
|
|
|
324 |
.collect(Collectors.toList());
|
|
|
325 |
for (FofoOrder order : sortedList) {
|
|
|
326 |
|
| 24240 |
amit.gupta |
327 |
LOGGER.info("Order Id is {}, partner Id is {}", order.getId(), order.getFofoId());
|
| 24241 |
amit.gupta |
328 |
if (!order.getInvoiceNumber().contains("SEC")) {
|
| 24240 |
amit.gupta |
329 |
sequence = Integer.parseInt(order.getInvoiceNumber().split("/")[1]);
|
|
|
330 |
prefix = order.getInvoiceNumber().split("/")[0];
|
|
|
331 |
} else {
|
|
|
332 |
sequence += 1;
|
| 24241 |
amit.gupta |
333 |
String invoiceNumber = prefix + "/" + sequence;
|
| 24240 |
amit.gupta |
334 |
order.setInvoiceNumber(invoiceNumber);
|
|
|
335 |
fofoOrderRepository.persist(order);
|
|
|
336 |
}
|
|
|
337 |
}
|
| 24241 |
amit.gupta |
338 |
|
| 24237 |
amit.gupta |
339 |
}
|
|
|
340 |
}
|
| 23724 |
amit.gupta |
341 |
|
| 24241 |
amit.gupta |
342 |
// Temporary Method
|
| 24252 |
amit.gupta |
343 |
public void evaluateExcessSchemeOut() throws Exception {
|
| 24244 |
amit.gupta |
344 |
Map<Integer, String> userNameMap = retailerService.getAllFofoRetailerIdNameMap();
|
|
|
345 |
Map<Integer, Float> userAmountMap = new HashMap<>();
|
| 24252 |
amit.gupta |
346 |
|
|
|
347 |
List<List<Object>> rows = new ArrayList<>();
|
| 24253 |
amit.gupta |
348 |
List<String> headers = Arrays.asList("Scheme", "Item","Partner", "Amount", "Credited On",
|
|
|
349 |
"Invoice Number", "Sale On", "Scheme Start", "Scheme End", "Active On", "Expired On");
|
| 24241 |
amit.gupta |
350 |
schemeRepository.selectAll().stream().forEach(x -> {
|
| 24250 |
amit.gupta |
351 |
if (x.getType().equals(SchemeType.OUT)) {
|
|
|
352 |
List<SchemeInOut> sioList = schemeInOutRepository
|
|
|
353 |
.selectBySchemeIds(new HashSet<>(Arrays.asList(x.getId())));
|
|
|
354 |
if (x.getActiveTimestamp() != null) {
|
|
|
355 |
LocalDateTime endDateTime = x.getEndDateTime();
|
|
|
356 |
if (x.getExpireTimestamp() != null && x.getExpireTimestamp().isBefore(x.getEndDateTime())) {
|
|
|
357 |
endDateTime = x.getExpireTimestamp();
|
| 24249 |
amit.gupta |
358 |
}
|
| 24250 |
amit.gupta |
359 |
for (SchemeInOut sio : sioList) {
|
|
|
360 |
InventoryItem inventoryItem = null;
|
| 24253 |
amit.gupta |
361 |
FofoOrder fofoOrder = null;
|
| 24250 |
amit.gupta |
362 |
try {
|
|
|
363 |
inventoryItem = inventoryItemRepository.selectById(sio.getInventoryItemId());
|
| 24253 |
amit.gupta |
364 |
fofoOrder = fofoOrderRepository.selectByFofoIdAndSerialNumber(inventoryItem.getFofoId(), inventoryItem.getSerialNumber(),
|
|
|
365 |
null, null, 0, 1).get(0);
|
| 24250 |
amit.gupta |
366 |
} catch (ProfitMandiBusinessException e) {
|
|
|
367 |
// TODO Auto-generated catch block
|
|
|
368 |
e.printStackTrace();
|
|
|
369 |
}
|
|
|
370 |
Optional<ScanRecord> record = scanRecordRepository
|
|
|
371 |
.selectByInventoryItemId(sio.getInventoryItemId()).stream()
|
|
|
372 |
.filter(y -> y.getType().equals(ScanType.SALE)).findFirst();
|
|
|
373 |
if (record.isPresent()) {
|
|
|
374 |
int fofoId = record.get().getFofoId();
|
|
|
375 |
if (record.get().getCreateTimestamp().isAfter(endDateTime)
|
|
|
376 |
|| record.get().getCreateTimestamp().isBefore(x.getStartDateTime())) {
|
|
|
377 |
if (!userAmountMap.containsKey(fofoId)) {
|
|
|
378 |
userAmountMap.put(fofoId, 0f);
|
|
|
379 |
}
|
|
|
380 |
userAmountMap.put(fofoId, sio.getAmount() + userAmountMap.get(fofoId));
|
|
|
381 |
try {
|
| 24252 |
amit.gupta |
382 |
rows.add(Arrays.asList(x.getDescription(),
|
| 24250 |
amit.gupta |
383 |
itemRepository.selectById(inventoryItem.getItemId()).getItemDescription(),
|
| 24252 |
amit.gupta |
384 |
userNameMap.get(fofoId), sio.getAmount(),
|
| 24253 |
amit.gupta |
385 |
FormattingUtils.formatDate(sio.getCreateTimestamp()),
|
|
|
386 |
fofoOrder.getInvoiceNumber(),
|
|
|
387 |
FormattingUtils.formatDate(record.get().getCreateTimestamp()),
|
|
|
388 |
FormattingUtils.formatDate(x.getStartDateTime()),
|
|
|
389 |
FormattingUtils.formatDate(x.getEndDateTime()),
|
|
|
390 |
FormattingUtils.formatDate(x.getActiveTimestamp()),
|
|
|
391 |
FormattingUtils.formatDate(x.getExpireTimestamp())));
|
| 24250 |
amit.gupta |
392 |
} catch (Exception e) {
|
|
|
393 |
e.printStackTrace();
|
|
|
394 |
}
|
| 24242 |
amit.gupta |
395 |
}
|
| 24241 |
amit.gupta |
396 |
}
|
|
|
397 |
}
|
|
|
398 |
}
|
|
|
399 |
}
|
|
|
400 |
});
|
| 24246 |
amit.gupta |
401 |
userAmountMap.entrySet().stream()
|
|
|
402 |
.forEach(x -> LOGGER.info("{} to be deducted from {}({}) for wrongly disbursed due to technical error.",
|
|
|
403 |
x.getValue(), userNameMap.get(x.getKey())));
|
| 24241 |
amit.gupta |
404 |
|
| 24252 |
amit.gupta |
405 |
ByteArrayOutputStream baos = FileUtil.getCSVByteStream(headers, rows);
|
|
|
406 |
Utils.sendMailWithAttachment(googleMailSender, "adeel.yazdani@smartdukaan.com",
|
|
|
407 |
new String[] { "amit.gupta@shop2020.in" }, "Partner Excess Amount", "PFA"
|
| 24256 |
amit.gupta |
408 |
, "ListofSchemes.csv", new ByteArrayResource(baos.toByteArray()));
|
| 24252 |
amit.gupta |
409 |
|
| 24241 |
amit.gupta |
410 |
}
|
|
|
411 |
|
| 24256 |
amit.gupta |
412 |
public void processScheme(int offset) throws Exception {
|
|
|
413 |
LocalDateTime startDate = LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT).minusDays(30*offset);
|
| 24259 |
amit.gupta |
414 |
LocalDateTime endDate = startDate.plusDays(30);
|
| 24258 |
amit.gupta |
415 |
processScheme(startDate, endDate);
|
| 24256 |
amit.gupta |
416 |
}
|
|
|
417 |
|
| 23738 |
amit.gupta |
418 |
public void processScheme() throws Exception {
|
| 24256 |
amit.gupta |
419 |
LocalDateTime fromDate = LocalDateTime.now().minusDays(30);
|
|
|
420 |
processScheme(fromDate, LocalDateTime.now());
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
public void processScheme(LocalDateTime startDate, LocalDateTime endDate) throws Exception {
|
| 23724 |
amit.gupta |
424 |
LOGGER.info("Started execution at {}", LocalDateTime.now());
|
| 24256 |
amit.gupta |
425 |
List<Purchase> purchases = purchaseRepository.selectAllBetweenPurchaseDate(startDate, endDate);
|
| 23724 |
amit.gupta |
426 |
for (Purchase purchase : purchases) {
|
|
|
427 |
try {
|
|
|
428 |
schemeService.processSchemeIn(purchase.getId(), purchase.getFofoId());
|
|
|
429 |
} catch (Exception e) {
|
|
|
430 |
LOGGER.error("Error while processing purchase {} for scheme In ", purchase.getId());
|
|
|
431 |
e.printStackTrace();
|
| 24256 |
amit.gupta |
432 |
|
| 23724 |
amit.gupta |
433 |
}
|
|
|
434 |
}
|
| 24256 |
amit.gupta |
435 |
List<FofoOrder> fofoOrders = fofoOrderRepository.selectBetweenSaleDate(startDate, endDate);
|
| 23724 |
amit.gupta |
436 |
for (FofoOrder fofoOrder : fofoOrders) {
|
|
|
437 |
try {
|
|
|
438 |
schemeService.processSchemeOut(fofoOrder.getId(), fofoOrder.getFofoId());
|
|
|
439 |
} catch (Exception e) {
|
|
|
440 |
LOGGER.error("Error while processing sale order {} for scheme Out", fofoOrder.getId());
|
|
|
441 |
}
|
|
|
442 |
}
|
|
|
443 |
LOGGER.info("Schemes process successfully.");
|
|
|
444 |
}
|
| 23929 |
amit.gupta |
445 |
|
| 23739 |
amit.gupta |
446 |
public void processRechargeCashback() throws Throwable {
|
| 23761 |
amit.gupta |
447 |
LocalDateTime cashbackTime = LocalDateTime.now();
|
| 23929 |
amit.gupta |
448 |
int referenceId = (int) Timestamp.valueOf(cashbackTime).getTime() / 1000;
|
|
|
449 |
List<RechargeTransaction> pendingTransactions = rechargeTransactionRepository
|
|
|
450 |
.getPendingCashBackRehargeTransactions();
|
|
|
451 |
Map<Object, Double> totalRetailerCashbacks = pendingTransactions.stream().collect(
|
|
|
452 |
Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingDouble(x -> x.getCommission())));
|
|
|
453 |
for (Map.Entry<Object, Double> totalRetailerCashback : totalRetailerCashbacks.entrySet()) {
|
|
|
454 |
int retailerId = (Integer) totalRetailerCashback.getKey();
|
| 23739 |
amit.gupta |
455 |
float amount = totalRetailerCashback.getValue().floatValue();
|
| 23929 |
amit.gupta |
456 |
if (Math.round(amount) > 0) {
|
|
|
457 |
walletService.addAmountToWallet(retailerId, referenceId, WalletReferenceType.CASHBACK,
|
|
|
458 |
"Recharge Cashback", Math.round(amount));
|
| 23762 |
amit.gupta |
459 |
}
|
| 23739 |
amit.gupta |
460 |
}
|
| 23929 |
amit.gupta |
461 |
for (RechargeTransaction rt : pendingTransactions) {
|
| 23761 |
amit.gupta |
462 |
rt.setCashbackTimestamp(cashbackTime);
|
|
|
463 |
rt.setCashbackReference(referenceId);
|
|
|
464 |
rechargeTransactionRepository.persist(rt);
|
|
|
465 |
}
|
| 23739 |
amit.gupta |
466 |
LOGGER.info("Cashbacks for Recharge processed Successfully");
|
|
|
467 |
}
|
| 23724 |
amit.gupta |
468 |
|
| 23929 |
amit.gupta |
469 |
public void sendPartnerInvestmentDetails() throws Exception {
|
| 23934 |
amit.gupta |
470 |
String fileName = "InvestmentSummary-" + FormattingUtils.formatDate(LocalDateTime.now()) + ".csv";
|
| 23929 |
amit.gupta |
471 |
|
|
|
472 |
List<FofoStore> fofoStores = fofoStoreRepository.selectAll();
|
|
|
473 |
Map<Integer, CustomRetailer> customRetailerMap = retailerService
|
|
|
474 |
.getFofoRetailers(fofoStores.stream().map(x -> x.getId()).collect(Collectors.toList()));
|
|
|
475 |
|
| 24002 |
amit.gupta |
476 |
List<String> headers = Arrays.asList("Code", "StoreName", "Email", "Mobile", "WalletAmount", "InStockAmount",
|
|
|
477 |
"UnbilledStockAmount", "GrnPendingStockAmount", "MinimumInvestment", "TotalInvested", "ShortAmount");
|
|
|
478 |
List<List<Object>> rows = new ArrayList<>();
|
|
|
479 |
for (FofoStore fofoStore : fofoStores) {
|
|
|
480 |
CustomRetailer retailer = customRetailerMap.get(fofoStore.getId());
|
|
|
481 |
if (retailer == null) {
|
|
|
482 |
LOGGER.info("Could not find retailer with retailer Id {}", fofoStore.getId());
|
|
|
483 |
continue;
|
|
|
484 |
}
|
|
|
485 |
float walletAmount = walletService.getUserWallet(fofoStore.getId()).getAmount();
|
|
|
486 |
float inStockAmount = inventoryService.getTotalAmountInStock(fofoStore.getId());
|
| 23929 |
amit.gupta |
487 |
|
| 24002 |
amit.gupta |
488 |
float unbilledStockAmount = 0;
|
|
|
489 |
List<Order> unbilledOrders = transactionService.getInTransitOrders(fofoStore.getId());
|
|
|
490 |
for (Order unBilledOrder : unbilledOrders) {
|
|
|
491 |
unbilledStockAmount += unBilledOrder.getTotalAmount();
|
| 23929 |
amit.gupta |
492 |
}
|
|
|
493 |
|
| 24002 |
amit.gupta |
494 |
float grnPendingStockAmount = 0;
|
|
|
495 |
List<Order> grnPendingOrders = orderRepository.selectPendingGrnOrders(fofoStore.getId());
|
|
|
496 |
for (Order grnPendingOrder : grnPendingOrders) {
|
|
|
497 |
LOGGER.info("Grn pending for Order {} and partner {}", grnPendingOrder.getId(),
|
|
|
498 |
grnPendingOrder.getRetailerEmailId());
|
|
|
499 |
grnPendingStockAmount += grnPendingOrder.getTotalAmount();
|
|
|
500 |
}
|
|
|
501 |
float totalInvestedAmount = walletAmount + inStockAmount + unbilledStockAmount + grnPendingStockAmount;
|
|
|
502 |
List<Object> row = Arrays.asList(fofoStore.getCode(), retailer.getBusinessName(), retailer.getEmail(),
|
|
|
503 |
retailer.getMobileNumber(), walletAmount, inStockAmount, unbilledStockAmount, grnPendingStockAmount,
|
|
|
504 |
fofoStore.getMinimumInvestment(), totalInvestedAmount,
|
|
|
505 |
fofoStore.getMinimumInvestment() - totalInvestedAmount);
|
|
|
506 |
rows.add(row);
|
|
|
507 |
|
| 23929 |
amit.gupta |
508 |
}
|
| 24002 |
amit.gupta |
509 |
ByteArrayOutputStream baos = FileUtil.getCSVByteStream(headers, rows);
|
|
|
510 |
Utils.sendMailWithAttachment(googleMailSender, "adeel.yazdani@smartdukaan.com",
|
|
|
511 |
new String[] { "amandeep.singh@smartdukaan.com", "tarun.verma@smartdukaan.com",
|
| 24052 |
amit.gupta |
512 |
"kamini.sharma@smartdukaan.com", "dhruv.kathpal@smartdukaan.com" },
|
| 24002 |
amit.gupta |
513 |
"Partner Investment Summary", "PFA", fileName, new ByteArrayResource(baos.toByteArray()));
|
| 23929 |
amit.gupta |
514 |
}
|
| 24177 |
govind |
515 |
|
|
|
516 |
private void sendMailWithAttachments(String[] email, String[] ccEmails, String body, String subject,
|
| 24187 |
govind |
517 |
Map<String, InputStream> inputStreams)
|
| 24177 |
govind |
518 |
throws MessagingException, ProfitMandiBusinessException, IOException {
|
| 24121 |
govind |
519 |
MimeMessage message = mailSender.createMimeMessage();
|
| 24177 |
govind |
520 |
MimeMessageHelper helper = new MimeMessageHelper(message, true, CharEncoding.UTF_8);
|
|
|
521 |
helper.setSubject(subject);
|
|
|
522 |
helper.setText(body);
|
|
|
523 |
if (ccEmails != null) {
|
|
|
524 |
helper.setCc(ccEmails);
|
|
|
525 |
}
|
| 24240 |
amit.gupta |
526 |
for (String attachment : inputStreams.keySet()) {
|
|
|
527 |
helper.addAttachment(attachment + "_" + LocalDate.now() + ".xls",
|
|
|
528 |
new ByteArrayResource(IOUtils.toByteArray(inputStreams.get(attachment))));
|
| 24187 |
govind |
529 |
}
|
| 24177 |
govind |
530 |
helper.setTo(email);
|
| 24195 |
govind |
531 |
InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "Smart Dukaan");
|
| 24177 |
govind |
532 |
helper.setFrom(senderAddress);
|
|
|
533 |
mailSender.send(message);
|
| 24135 |
govind |
534 |
}
|
| 24240 |
amit.gupta |
535 |
|
|
|
536 |
private void sendMailWithAttachmentsForPartners(String email, String body, String subject, InputStream is)
|
| 24184 |
govind |
537 |
throws MessagingException, ProfitMandiBusinessException, IOException {
|
|
|
538 |
MimeMessage message = mailSender.createMimeMessage();
|
|
|
539 |
MimeMessageHelper helper = new MimeMessageHelper(message, true, CharEncoding.UTF_8);
|
|
|
540 |
helper.setSubject(subject);
|
|
|
541 |
helper.setText(body);
|
| 24240 |
amit.gupta |
542 |
helper.addAttachment("DailySaleTargetReports_" + LocalDate.now() + ".xls",
|
|
|
543 |
new ByteArrayResource(IOUtils.toByteArray(is)));
|
| 24184 |
govind |
544 |
helper.setTo(email);
|
| 24195 |
govind |
545 |
InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "Smart Dukaan");
|
| 24184 |
govind |
546 |
helper.setFrom(senderAddress);
|
|
|
547 |
mailSender.send(message);
|
|
|
548 |
}
|
| 24177 |
govind |
549 |
|
|
|
550 |
public void sendMailToSalesHeadAboutTargetAndSales(SaleHeadDetails salesHeadDetail)
|
|
|
551 |
throws MessagingException, ProfitMandiBusinessException, IOException {
|
|
|
552 |
List<PartnerTargetDetails> partnerTargetDetails = partnerTargetRepository
|
|
|
553 |
.selectAllGeEqAndLeEqStartDateAndEndDate(LocalDateTime.now());
|
|
|
554 |
Map<String, InputStream> inputStreams = new HashMap<>();
|
|
|
555 |
for (PartnerTargetDetails partnerTargetDetail : partnerTargetDetails) {
|
|
|
556 |
InputStream is = targetService.getDailySaleReportVsTargetForSaleHead(partnerTargetDetail,
|
|
|
557 |
salesHeadDetail.getName());
|
| 24240 |
amit.gupta |
558 |
|
| 24177 |
govind |
559 |
inputStreams.put(partnerTargetDetail.getTargetName(), is);
|
|
|
560 |
|
| 24174 |
govind |
561 |
}
|
| 24240 |
amit.gupta |
562 |
// String[] ccEmails = null;
|
| 24255 |
govind |
563 |
if (partnerTargetDetails.size()>0) {
|
| 24254 |
govind |
564 |
String[] ccEmails = { "Tarun.verma@smartdukaan.com", "Kamini.sharma@smartdukaan.com",
|
|
|
565 |
"chaitnaya.vats@smartdukaan.com", "dhruv.kathpal@smartdukaan.com" };
|
|
|
566 |
String subject = "Daily Sales Report ";
|
|
|
567 |
String message = MessageFormat.format("Sale Target summary for {0}", LocalDate.now());
|
|
|
568 |
LOGGER.info("message" + message);
|
|
|
569 |
if (salesHeadDetail.getName().equals("Kamal")) {
|
|
|
570 |
LOGGER.info("salesHeadDetail.getName()" + salesHeadDetail.getName());
|
|
|
571 |
String[] to = { salesHeadDetail.getEmail(), "mohinder.mutreja@smartdukaan.com" };
|
| 24177 |
govind |
572 |
|
| 24187 |
govind |
573 |
this.sendMailWithAttachments(to, ccEmails, message, subject, inputStreams);
|
| 24177 |
govind |
574 |
|
|
|
575 |
} else {
|
|
|
576 |
LOGGER.info("salesHeadDetail.getName()" + salesHeadDetail.getName());
|
|
|
577 |
String[] to = { salesHeadDetail.getEmail() };
|
| 24187 |
govind |
578 |
this.sendMailWithAttachments(to, ccEmails, message, subject, inputStreams);
|
| 24177 |
govind |
579 |
}
|
| 24254 |
govind |
580 |
}
|
| 24121 |
govind |
581 |
}
|
| 24177 |
govind |
582 |
|
| 24240 |
amit.gupta |
583 |
public void sendMailToPartnerAboutTargetAndSales() throws ProfitMandiBusinessException, MessagingException,
|
|
|
584 |
IOException, EncryptedDocumentException, InvalidFormatException {
|
| 24177 |
govind |
585 |
List<Integer> fofoIds = targetService.getfofoIdsFromfofoStore();
|
|
|
586 |
Map<Integer, CustomRetailer> fofoIdsAndCustomRetailer = retailerService
|
|
|
587 |
.getFofoRetailers(new ArrayList<>(fofoIds));
|
| 24240 |
amit.gupta |
588 |
List<InputStream> input = new ArrayList<>();
|
| 24177 |
govind |
589 |
List<PartnerTargetDetails> partnerTargetDetails = partnerTargetRepository
|
|
|
590 |
.selectAllGeEqAndLeEqStartDateAndEndDate(LocalDateTime.now());
|
|
|
591 |
for (Integer fofoId : fofoIds) {
|
| 24240 |
amit.gupta |
592 |
InputStream is = targetService.getDailySaleReportVsTargetForPartner(fofoId, partnerTargetDetails);
|
|
|
593 |
LOGGER.info("is" + is);
|
|
|
594 |
if (is == null) {
|
| 24192 |
govind |
595 |
continue;
|
|
|
596 |
}
|
| 24240 |
amit.gupta |
597 |
LOGGER.info("fofoId" + fofoId);
|
|
|
598 |
|
| 24177 |
govind |
599 |
LOGGER.info(fofoIdsAndCustomRetailer.get(fofoId).getEmail());
|
| 24240 |
amit.gupta |
600 |
String subject = "Daily Sales Report";
|
|
|
601 |
String message = MessageFormat.format("Sale Target summary for {0}", LocalDate.now());
|
| 24177 |
govind |
602 |
LOGGER.info(message);
|
| 24240 |
amit.gupta |
603 |
this.sendMailWithAttachmentsForPartners(fofoIdsAndCustomRetailer.get(fofoId).getEmail(), message, subject,
|
|
|
604 |
is);
|
|
|
605 |
|
| 24174 |
govind |
606 |
}
|
|
|
607 |
}
|
| 23929 |
amit.gupta |
608 |
|
| 23724 |
amit.gupta |
609 |
}
|