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