| 23755 |
amit.gupta |
1 |
package com.smartdukaan.cron.migrations;
|
|
|
2 |
|
| 23905 |
amit.gupta |
3 |
import java.io.File;
|
| 23824 |
amit.gupta |
4 |
import java.time.LocalDate;
|
| 23755 |
amit.gupta |
5 |
import java.time.LocalDateTime;
|
| 23824 |
amit.gupta |
6 |
import java.time.LocalTime;
|
| 23905 |
amit.gupta |
7 |
import java.util.ArrayList;
|
| 23755 |
amit.gupta |
8 |
import java.util.Arrays;
|
| 23827 |
amit.gupta |
9 |
import java.util.Collections;
|
| 23755 |
amit.gupta |
10 |
import java.util.List;
|
| 23899 |
amit.gupta |
11 |
import java.util.Map;
|
|
|
12 |
import java.util.stream.Collectors;
|
| 23755 |
amit.gupta |
13 |
|
| 23905 |
amit.gupta |
14 |
import javax.mail.internet.InternetAddress;
|
|
|
15 |
import javax.mail.internet.MimeMessage;
|
|
|
16 |
|
|
|
17 |
import org.apache.commons.io.FileUtils;
|
| 23755 |
amit.gupta |
18 |
import org.apache.commons.lang.StringUtils;
|
|
|
19 |
import org.apache.logging.log4j.LogManager;
|
|
|
20 |
import org.apache.logging.log4j.Logger;
|
|
|
21 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 23905 |
amit.gupta |
22 |
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
23 |
import org.springframework.mail.javamail.MimeMessageHelper;
|
| 23755 |
amit.gupta |
24 |
import org.springframework.stereotype.Component;
|
|
|
25 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
26 |
|
| 23899 |
amit.gupta |
27 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
| 23905 |
amit.gupta |
28 |
import com.spice.profitmandi.common.util.FormattingUtils;
|
| 23910 |
amit.gupta |
29 |
import com.spice.profitmandi.common.util.Utils;
|
| 23899 |
amit.gupta |
30 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
| 23898 |
amit.gupta |
31 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 23824 |
amit.gupta |
32 |
import com.spice.profitmandi.dao.entity.transaction.LineItem;
|
| 23755 |
amit.gupta |
33 |
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
|
|
|
34 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 23899 |
amit.gupta |
35 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 23898 |
amit.gupta |
36 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 23755 |
amit.gupta |
37 |
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
|
| 23824 |
amit.gupta |
38 |
import com.spice.profitmandi.dao.repository.transaction.LineItemRepository;
|
| 23755 |
amit.gupta |
39 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
| 23899 |
amit.gupta |
40 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
|
|
41 |
import com.spice.profitmandi.service.transaction.TransactionService;
|
|
|
42 |
import com.spice.profitmandi.service.user.RetailerService;
|
|
|
43 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 23755 |
amit.gupta |
44 |
|
|
|
45 |
@Component
|
|
|
46 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
47 |
public class RunOnceTasks {
|
|
|
48 |
|
|
|
49 |
private static final Logger LOGGER = LogManager.getLogger(RunOnceTasks.class);
|
|
|
50 |
|
|
|
51 |
@Autowired
|
| 23824 |
amit.gupta |
52 |
private LineItemRepository lineItemRepository;
|
| 23899 |
amit.gupta |
53 |
|
| 23824 |
amit.gupta |
54 |
@Autowired
|
| 23905 |
amit.gupta |
55 |
private JavaMailSender mailSender;
|
|
|
56 |
|
|
|
57 |
@Autowired
|
| 23899 |
amit.gupta |
58 |
private WalletService walletService;
|
|
|
59 |
|
|
|
60 |
@Autowired
|
|
|
61 |
private InventoryService inventoryService;
|
|
|
62 |
|
|
|
63 |
@Autowired
|
|
|
64 |
private TransactionService transactionService;
|
|
|
65 |
|
|
|
66 |
@Autowired
|
| 23755 |
amit.gupta |
67 |
private OrderRepository orderRepository;
|
| 23767 |
amit.gupta |
68 |
|
| 23755 |
amit.gupta |
69 |
@Autowired
|
| 23899 |
amit.gupta |
70 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
71 |
|
|
|
72 |
@Autowired
|
| 23755 |
amit.gupta |
73 |
private LineItemImeisRepository lineItemImeisRepository;
|
| 23901 |
amit.gupta |
74 |
|
| 23898 |
amit.gupta |
75 |
@Autowired
|
| 23899 |
amit.gupta |
76 |
private RetailerService retailerService;
|
|
|
77 |
|
|
|
78 |
@Autowired
|
| 23898 |
amit.gupta |
79 |
private PurchaseRepository purchaseRepository;
|
| 23755 |
amit.gupta |
80 |
|
| 23898 |
amit.gupta |
81 |
public void populateGrnTimestamp() {
|
|
|
82 |
List<Purchase> allPurchases = purchaseRepository.selectAll();
|
| 23899 |
amit.gupta |
83 |
for (Purchase p : allPurchases) {
|
| 23898 |
amit.gupta |
84 |
String invoiceNumber = p.getPurchaseReference();
|
| 23899 |
amit.gupta |
85 |
if (p.getCompleteTimestamp() == null) {
|
| 23898 |
amit.gupta |
86 |
LOGGER.info("GRN for invoice {} is delivered but partially Completed.", p.getPurchaseReference());
|
|
|
87 |
} else {
|
|
|
88 |
List<Order> orders = orderRepository.selectByAirwayBillOrInvoiceNumber(invoiceNumber, p.getFofoId());
|
| 23899 |
amit.gupta |
89 |
for (Order order : orders) {
|
| 23902 |
amit.gupta |
90 |
if (order.getPartnerGrnTimestamp() == null) {
|
|
|
91 |
order.setPartnerGrnTimestamp(p.getCompleteTimestamp());
|
| 23898 |
amit.gupta |
92 |
orderRepository.persist(order);
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
}
|
| 23899 |
amit.gupta |
97 |
|
| 23898 |
amit.gupta |
98 |
}
|
| 23899 |
amit.gupta |
99 |
|
| 23755 |
amit.gupta |
100 |
public void migarateLineItemsToNewTable() {
|
|
|
101 |
LOGGER.info("Before Migrated LineItems Successfully");
|
| 23824 |
amit.gupta |
102 |
int lineItemImeiId = 0;
|
|
|
103 |
LocalDateTime startDate = null;
|
|
|
104 |
try {
|
|
|
105 |
lineItemImeiId = lineItemImeisRepository.selectMaxId();
|
| 23899 |
amit.gupta |
106 |
LineItem lineItem = lineItemRepository
|
|
|
107 |
.selectById(lineItemImeisRepository.selectById(lineItemImeiId).getLineItemId());
|
| 23824 |
amit.gupta |
108 |
Order order = orderRepository.selectById(lineItem.getOrderId());
|
|
|
109 |
startDate = order.getBillingTimestamp();
|
|
|
110 |
} catch (Exception e) {
|
|
|
111 |
LOGGER.info("Running before first time");
|
| 23826 |
amit.gupta |
112 |
startDate = LocalDateTime.of(LocalDate.of(2017, 7, 1), LocalTime.MIDNIGHT);
|
| 23824 |
amit.gupta |
113 |
}
|
|
|
114 |
List<Order> orders = orderRepository.selectAllByBillingDatesBetween(startDate, LocalDateTime.now());
|
| 23827 |
amit.gupta |
115 |
Collections.reverse(orders);
|
| 23899 |
amit.gupta |
116 |
|
| 23755 |
amit.gupta |
117 |
for (Order order : orders) {
|
| 23824 |
amit.gupta |
118 |
try {
|
| 23767 |
amit.gupta |
119 |
String serialNumbers = order.getLineItem().getSerialNumber();
|
|
|
120 |
if (!StringUtils.isEmpty(serialNumbers)) {
|
|
|
121 |
List<String> serialNumberList = Arrays.asList(serialNumbers.split(","));
|
|
|
122 |
for (String serialNumber : serialNumberList) {
|
|
|
123 |
int lineItemId = order.getLineItem().getId();
|
|
|
124 |
LineItemImei lineItemImei = new LineItemImei();
|
|
|
125 |
lineItemImei.setSerialNumber(serialNumber);
|
|
|
126 |
lineItemImei.setLineItemId(lineItemId);
|
|
|
127 |
lineItemImeisRepository.persist(lineItemImei);
|
|
|
128 |
}
|
|
|
129 |
} else {
|
|
|
130 |
LOGGER.info("Serial Numbers dont exist for Order {}", order.getId());
|
| 23755 |
amit.gupta |
131 |
}
|
| 23824 |
amit.gupta |
132 |
} catch (Exception e) {
|
| 23899 |
amit.gupta |
133 |
LOGGER.info("Error occurred while creating lineitem imei {}, because of {}", order.getId(),
|
|
|
134 |
e.getMessage());
|
| 23755 |
amit.gupta |
135 |
}
|
|
|
136 |
}
|
|
|
137 |
LOGGER.info("Migrated LineItems Successfully");
|
|
|
138 |
}
|
| 23899 |
amit.gupta |
139 |
|
| 23901 |
amit.gupta |
140 |
public void getInvestmentDetails() throws Exception {
|
| 23909 |
amit.gupta |
141 |
File f = new File("/tmp/InvestmentSummary-" + FormattingUtils.formatDate(LocalDateTime.now()) + ".csv");
|
| 23905 |
amit.gupta |
142 |
List<String> lines = new ArrayList<>();
|
|
|
143 |
lines.add(
|
| 23901 |
amit.gupta |
144 |
"Code\tStoreName\tEmail\tMobile\tminimumInvestment\twalletAmount\tinStockAmount\tunbilledStockAmount\tgrnPendingStockAmount\tTotalInvested");
|
| 23899 |
amit.gupta |
145 |
List<FofoStore> fofoStores = fofoStoreRepository.selectAll();
|
| 23901 |
amit.gupta |
146 |
Map<Integer, CustomRetailer> customRetailerMap = retailerService
|
|
|
147 |
.getFofoRetailers(fofoStores.stream().map(x -> x.getId()).collect(Collectors.toList()));
|
|
|
148 |
for (FofoStore fofoStore : fofoStores) {
|
| 23903 |
amit.gupta |
149 |
try {
|
|
|
150 |
CustomRetailer retailer = customRetailerMap.get(fofoStore.getId());
|
|
|
151 |
float walletAmount = walletService.getUserWallet(fofoStore.getId()).getAmount();
|
|
|
152 |
float inStockAmount = inventoryService.getTotalAmountInStock(fofoStore.getId());
|
| 23901 |
amit.gupta |
153 |
|
| 23903 |
amit.gupta |
154 |
float unbilledStockAmount = 0;
|
|
|
155 |
List<Order> unbilledOrders = transactionService.getInTransitOrders(fofoStore.getId());
|
|
|
156 |
for (Order unBilledOrder : unbilledOrders) {
|
|
|
157 |
unbilledStockAmount += unBilledOrder.getTotalAmount();
|
|
|
158 |
}
|
| 23901 |
amit.gupta |
159 |
|
| 23903 |
amit.gupta |
160 |
float grnPendingStockAmount = 0;
|
| 23905 |
amit.gupta |
161 |
List<Order> grnPendingOrders = orderRepository.selectPendingGrnOrders(fofoStore.getId());
|
| 23903 |
amit.gupta |
162 |
for (Order grnPendingOrder : grnPendingOrders) {
|
| 23917 |
amit.gupta |
163 |
LOGGER.info("Grn pending for Order {} and partner {}", grnPendingOrder.getId(), grnPendingOrder.getRetailerEmailId());
|
| 23903 |
amit.gupta |
164 |
grnPendingStockAmount += grnPendingOrder.getTotalAmount();
|
|
|
165 |
}
|
|
|
166 |
float totalInvestedAmount = walletAmount + inStockAmount + unbilledStockAmount + grnPendingStockAmount;
|
| 23915 |
amit.gupta |
167 |
lines.add(String.join("\t", fofoStore.getCode(), retailer.getBusinessName(), retailer.getEmail(), retailer.getMobileNumber(),
|
| 23905 |
amit.gupta |
168 |
String.valueOf(fofoStore.getMinimumInvestment()), String.valueOf(walletAmount),
|
|
|
169 |
String.valueOf(inStockAmount), String.valueOf(unbilledStockAmount),
|
|
|
170 |
String.valueOf(grnPendingStockAmount), String.valueOf(totalInvestedAmount)));
|
| 23901 |
amit.gupta |
171 |
} catch (Exception e) {
|
| 23903 |
amit.gupta |
172 |
e.printStackTrace();
|
| 23899 |
amit.gupta |
173 |
continue;
|
|
|
174 |
}
|
| 23905 |
amit.gupta |
175 |
FileUtils.writeLines(f, lines);
|
| 23899 |
amit.gupta |
176 |
}
|
| 23911 |
amit.gupta |
177 |
Utils.sendMailWithAttachments(mailSender,"amit.gupta@shop2020.in", null, "Partner Investment Summary", "PFA", Arrays.asList(f));
|
| 23899 |
amit.gupta |
178 |
}
|
| 23905 |
amit.gupta |
179 |
|
| 23755 |
amit.gupta |
180 |
}
|