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