| 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;
|
| 23899 |
amit.gupta |
29 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
| 23898 |
amit.gupta |
30 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 23824 |
amit.gupta |
31 |
import com.spice.profitmandi.dao.entity.transaction.LineItem;
|
| 23755 |
amit.gupta |
32 |
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
|
|
|
33 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 23899 |
amit.gupta |
34 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 23898 |
amit.gupta |
35 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 23755 |
amit.gupta |
36 |
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
|
| 23824 |
amit.gupta |
37 |
import com.spice.profitmandi.dao.repository.transaction.LineItemRepository;
|
| 23755 |
amit.gupta |
38 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
| 23899 |
amit.gupta |
39 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
|
|
40 |
import com.spice.profitmandi.service.transaction.TransactionService;
|
|
|
41 |
import com.spice.profitmandi.service.user.RetailerService;
|
|
|
42 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 23755 |
amit.gupta |
43 |
|
|
|
44 |
@Component
|
|
|
45 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
46 |
public class RunOnceTasks {
|
|
|
47 |
|
|
|
48 |
private static final Logger LOGGER = LogManager.getLogger(RunOnceTasks.class);
|
|
|
49 |
|
|
|
50 |
@Autowired
|
| 23824 |
amit.gupta |
51 |
private LineItemRepository lineItemRepository;
|
| 23899 |
amit.gupta |
52 |
|
| 23824 |
amit.gupta |
53 |
@Autowired
|
| 23905 |
amit.gupta |
54 |
private JavaMailSender mailSender;
|
|
|
55 |
|
|
|
56 |
@Autowired
|
| 23899 |
amit.gupta |
57 |
private WalletService walletService;
|
|
|
58 |
|
|
|
59 |
@Autowired
|
|
|
60 |
private InventoryService inventoryService;
|
|
|
61 |
|
|
|
62 |
@Autowired
|
|
|
63 |
private TransactionService transactionService;
|
|
|
64 |
|
|
|
65 |
@Autowired
|
| 23755 |
amit.gupta |
66 |
private OrderRepository orderRepository;
|
| 23767 |
amit.gupta |
67 |
|
| 23755 |
amit.gupta |
68 |
@Autowired
|
| 23899 |
amit.gupta |
69 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
70 |
|
|
|
71 |
@Autowired
|
| 23755 |
amit.gupta |
72 |
private LineItemImeisRepository lineItemImeisRepository;
|
| 23901 |
amit.gupta |
73 |
|
| 23898 |
amit.gupta |
74 |
@Autowired
|
| 23899 |
amit.gupta |
75 |
private RetailerService retailerService;
|
|
|
76 |
|
|
|
77 |
@Autowired
|
| 23898 |
amit.gupta |
78 |
private PurchaseRepository purchaseRepository;
|
| 23755 |
amit.gupta |
79 |
|
| 23898 |
amit.gupta |
80 |
public void populateGrnTimestamp() {
|
|
|
81 |
List<Purchase> allPurchases = purchaseRepository.selectAll();
|
| 23899 |
amit.gupta |
82 |
for (Purchase p : allPurchases) {
|
| 23898 |
amit.gupta |
83 |
String invoiceNumber = p.getPurchaseReference();
|
| 23899 |
amit.gupta |
84 |
if (p.getCompleteTimestamp() == null) {
|
| 23898 |
amit.gupta |
85 |
LOGGER.info("GRN for invoice {} is delivered but partially Completed.", p.getPurchaseReference());
|
|
|
86 |
} else {
|
|
|
87 |
List<Order> orders = orderRepository.selectByAirwayBillOrInvoiceNumber(invoiceNumber, p.getFofoId());
|
| 23899 |
amit.gupta |
88 |
for (Order order : orders) {
|
| 23902 |
amit.gupta |
89 |
if (order.getPartnerGrnTimestamp() == null) {
|
|
|
90 |
order.setPartnerGrnTimestamp(p.getCompleteTimestamp());
|
| 23898 |
amit.gupta |
91 |
orderRepository.persist(order);
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
}
|
| 23899 |
amit.gupta |
96 |
|
| 23898 |
amit.gupta |
97 |
}
|
| 23899 |
amit.gupta |
98 |
|
| 23755 |
amit.gupta |
99 |
public void migarateLineItemsToNewTable() {
|
|
|
100 |
LOGGER.info("Before Migrated LineItems Successfully");
|
| 23824 |
amit.gupta |
101 |
int lineItemImeiId = 0;
|
|
|
102 |
LocalDateTime startDate = null;
|
|
|
103 |
try {
|
|
|
104 |
lineItemImeiId = lineItemImeisRepository.selectMaxId();
|
| 23899 |
amit.gupta |
105 |
LineItem lineItem = lineItemRepository
|
|
|
106 |
.selectById(lineItemImeisRepository.selectById(lineItemImeiId).getLineItemId());
|
| 23824 |
amit.gupta |
107 |
Order order = orderRepository.selectById(lineItem.getOrderId());
|
|
|
108 |
startDate = order.getBillingTimestamp();
|
|
|
109 |
} catch (Exception e) {
|
|
|
110 |
LOGGER.info("Running before first time");
|
| 23826 |
amit.gupta |
111 |
startDate = LocalDateTime.of(LocalDate.of(2017, 7, 1), LocalTime.MIDNIGHT);
|
| 23824 |
amit.gupta |
112 |
}
|
|
|
113 |
List<Order> orders = orderRepository.selectAllByBillingDatesBetween(startDate, LocalDateTime.now());
|
| 23827 |
amit.gupta |
114 |
Collections.reverse(orders);
|
| 23899 |
amit.gupta |
115 |
|
| 23755 |
amit.gupta |
116 |
for (Order order : orders) {
|
| 23824 |
amit.gupta |
117 |
try {
|
| 23767 |
amit.gupta |
118 |
String serialNumbers = order.getLineItem().getSerialNumber();
|
|
|
119 |
if (!StringUtils.isEmpty(serialNumbers)) {
|
|
|
120 |
List<String> serialNumberList = Arrays.asList(serialNumbers.split(","));
|
|
|
121 |
for (String serialNumber : serialNumberList) {
|
|
|
122 |
int lineItemId = order.getLineItem().getId();
|
|
|
123 |
LineItemImei lineItemImei = new LineItemImei();
|
|
|
124 |
lineItemImei.setSerialNumber(serialNumber);
|
|
|
125 |
lineItemImei.setLineItemId(lineItemId);
|
|
|
126 |
lineItemImeisRepository.persist(lineItemImei);
|
|
|
127 |
}
|
|
|
128 |
} else {
|
|
|
129 |
LOGGER.info("Serial Numbers dont exist for Order {}", order.getId());
|
| 23755 |
amit.gupta |
130 |
}
|
| 23824 |
amit.gupta |
131 |
} catch (Exception e) {
|
| 23899 |
amit.gupta |
132 |
LOGGER.info("Error occurred while creating lineitem imei {}, because of {}", order.getId(),
|
|
|
133 |
e.getMessage());
|
| 23755 |
amit.gupta |
134 |
}
|
|
|
135 |
}
|
|
|
136 |
LOGGER.info("Migrated LineItems Successfully");
|
|
|
137 |
}
|
| 23899 |
amit.gupta |
138 |
|
| 23901 |
amit.gupta |
139 |
public void getInvestmentDetails() throws Exception {
|
| 23909 |
amit.gupta |
140 |
File f = new File("/tmp/InvestmentSummary-" + FormattingUtils.formatDate(LocalDateTime.now()) + ".csv");
|
| 23905 |
amit.gupta |
141 |
List<String> lines = new ArrayList<>();
|
|
|
142 |
lines.add(
|
| 23901 |
amit.gupta |
143 |
"Code\tStoreName\tEmail\tMobile\tminimumInvestment\twalletAmount\tinStockAmount\tunbilledStockAmount\tgrnPendingStockAmount\tTotalInvested");
|
| 23899 |
amit.gupta |
144 |
List<FofoStore> fofoStores = fofoStoreRepository.selectAll();
|
| 23901 |
amit.gupta |
145 |
Map<Integer, CustomRetailer> customRetailerMap = retailerService
|
|
|
146 |
.getFofoRetailers(fofoStores.stream().map(x -> x.getId()).collect(Collectors.toList()));
|
|
|
147 |
for (FofoStore fofoStore : fofoStores) {
|
| 23903 |
amit.gupta |
148 |
try {
|
|
|
149 |
CustomRetailer retailer = customRetailerMap.get(fofoStore.getId());
|
|
|
150 |
float walletAmount = walletService.getUserWallet(fofoStore.getId()).getAmount();
|
|
|
151 |
float inStockAmount = inventoryService.getTotalAmountInStock(fofoStore.getId());
|
| 23901 |
amit.gupta |
152 |
|
| 23903 |
amit.gupta |
153 |
float unbilledStockAmount = 0;
|
|
|
154 |
List<Order> unbilledOrders = transactionService.getInTransitOrders(fofoStore.getId());
|
|
|
155 |
for (Order unBilledOrder : unbilledOrders) {
|
|
|
156 |
unbilledStockAmount += unBilledOrder.getTotalAmount();
|
|
|
157 |
}
|
| 23901 |
amit.gupta |
158 |
|
| 23903 |
amit.gupta |
159 |
float grnPendingStockAmount = 0;
|
| 23905 |
amit.gupta |
160 |
List<Order> grnPendingOrders = orderRepository.selectPendingGrnOrders(fofoStore.getId());
|
| 23903 |
amit.gupta |
161 |
for (Order grnPendingOrder : grnPendingOrders) {
|
|
|
162 |
grnPendingStockAmount += grnPendingOrder.getTotalAmount();
|
|
|
163 |
}
|
|
|
164 |
float totalInvestedAmount = walletAmount + inStockAmount + unbilledStockAmount + grnPendingStockAmount;
|
| 23905 |
amit.gupta |
165 |
lines.add(String.join("\t", fofoStore.getCode(), retailer.getBusinessName(), retailer.getMobileNumber(),
|
|
|
166 |
String.valueOf(fofoStore.getMinimumInvestment()), String.valueOf(walletAmount),
|
|
|
167 |
String.valueOf(inStockAmount), String.valueOf(unbilledStockAmount),
|
|
|
168 |
String.valueOf(grnPendingStockAmount), String.valueOf(totalInvestedAmount)));
|
| 23901 |
amit.gupta |
169 |
} catch (Exception e) {
|
| 23903 |
amit.gupta |
170 |
e.printStackTrace();
|
| 23899 |
amit.gupta |
171 |
continue;
|
|
|
172 |
}
|
| 23905 |
amit.gupta |
173 |
FileUtils.writeLines(f, lines);
|
|
|
174 |
this.sendMailWithAttachments("amit.gupta@shop2020.in", "Partner Investment Summary", "PFA", Arrays.asList(f));
|
| 23899 |
amit.gupta |
175 |
}
|
| 23901 |
amit.gupta |
176 |
|
| 23899 |
amit.gupta |
177 |
}
|
| 23905 |
amit.gupta |
178 |
|
|
|
179 |
private void sendMailWithAttachments(String email, String subject, String body, List<File> attachments) throws Exception {
|
|
|
180 |
|
|
|
181 |
MimeMessage message = mailSender.createMimeMessage();
|
|
|
182 |
MimeMessageHelper helper = new MimeMessageHelper(message,true);
|
|
|
183 |
helper.setSubject(subject);
|
|
|
184 |
helper.setText(body);
|
| 23909 |
amit.gupta |
185 |
//String[] cc = {"backup@shop2020.in", "gadgetcops.01@gmail.com"};
|
| 23905 |
amit.gupta |
186 |
//String[] cc = {"amit.gupta@shop2020.in"};
|
| 23909 |
amit.gupta |
187 |
//helper.setCc(cc);
|
| 23905 |
amit.gupta |
188 |
helper.setTo(email);
|
|
|
189 |
InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "ProfitMandi Admin");
|
| 23909 |
amit.gupta |
190 |
//helper.setTo("help@smartdukaan.com");
|
| 23905 |
amit.gupta |
191 |
helper.setFrom(senderAddress);
|
|
|
192 |
for (File file : attachments) {
|
|
|
193 |
helper.addAttachment(file.getName(), file);
|
|
|
194 |
}
|
|
|
195 |
mailSender.send(message);
|
|
|
196 |
|
|
|
197 |
}
|
| 23755 |
amit.gupta |
198 |
}
|