| 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;
|
| 24002 |
amit.gupta |
8 |
import java.util.HashMap;
|
| 23755 |
amit.gupta |
9 |
import java.util.List;
|
| 24002 |
amit.gupta |
10 |
import java.util.Map;
|
| 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 |
|
| 23898 |
amit.gupta |
19 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 23824 |
amit.gupta |
20 |
import com.spice.profitmandi.dao.entity.transaction.LineItem;
|
| 23755 |
amit.gupta |
21 |
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
|
|
|
22 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 23899 |
amit.gupta |
23 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 24002 |
amit.gupta |
24 |
import com.spice.profitmandi.dao.repository.fofo.DebitNoteRepository;
|
|
|
25 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 23898 |
amit.gupta |
26 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 24002 |
amit.gupta |
27 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
| 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;
|
| 24002 |
amit.gupta |
31 |
import com.spice.profitmandi.dao.repository.user.UserRepository;
|
| 23899 |
amit.gupta |
32 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
|
|
33 |
import com.spice.profitmandi.service.transaction.TransactionService;
|
|
|
34 |
import com.spice.profitmandi.service.user.RetailerService;
|
|
|
35 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 23755 |
amit.gupta |
36 |
|
| 24002 |
amit.gupta |
37 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
38 |
|
| 23755 |
amit.gupta |
39 |
@Component
|
|
|
40 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
41 |
public class RunOnceTasks {
|
|
|
42 |
|
|
|
43 |
private static final Logger LOGGER = LogManager.getLogger(RunOnceTasks.class);
|
|
|
44 |
|
|
|
45 |
@Autowired
|
| 23824 |
amit.gupta |
46 |
private LineItemRepository lineItemRepository;
|
| 23905 |
amit.gupta |
47 |
|
|
|
48 |
@Autowired
|
| 24002 |
amit.gupta |
49 |
private UserRepository userRepository;
|
|
|
50 |
|
|
|
51 |
@Autowired
|
| 23899 |
amit.gupta |
52 |
private WalletService walletService;
|
|
|
53 |
|
|
|
54 |
@Autowired
|
|
|
55 |
private InventoryService inventoryService;
|
|
|
56 |
|
|
|
57 |
@Autowired
|
|
|
58 |
private TransactionService transactionService;
|
|
|
59 |
|
|
|
60 |
@Autowired
|
| 23755 |
amit.gupta |
61 |
private OrderRepository orderRepository;
|
| 23767 |
amit.gupta |
62 |
|
| 23755 |
amit.gupta |
63 |
@Autowired
|
| 23899 |
amit.gupta |
64 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
65 |
|
|
|
66 |
@Autowired
|
| 23755 |
amit.gupta |
67 |
private LineItemImeisRepository lineItemImeisRepository;
|
| 24002 |
amit.gupta |
68 |
|
|
|
69 |
@Autowired
|
|
|
70 |
private InventoryItemRepository inventoryItemRepository;
|
| 23901 |
amit.gupta |
71 |
|
| 23898 |
amit.gupta |
72 |
@Autowired
|
| 23899 |
amit.gupta |
73 |
private RetailerService retailerService;
|
| 24002 |
amit.gupta |
74 |
|
|
|
75 |
@Autowired
|
|
|
76 |
private SchemeInOutRepository schemeInOutRepository;
|
|
|
77 |
|
|
|
78 |
@Autowired
|
|
|
79 |
private DebitNoteRepository debitNoteRepository;
|
| 23899 |
amit.gupta |
80 |
|
|
|
81 |
@Autowired
|
| 23898 |
amit.gupta |
82 |
private PurchaseRepository purchaseRepository;
|
| 24002 |
amit.gupta |
83 |
|
|
|
84 |
public void schemeRollback() {
|
|
|
85 |
Map<Integer, Float> fofoIdAmount = new HashMap<>();
|
|
|
86 |
fofoIdAmount.put(157112773, 21f);
|
|
|
87 |
fofoIdAmount.put(175134343, 11787f);
|
|
|
88 |
fofoIdAmount.put(175135214, 43f);
|
|
|
89 |
fofoIdAmount.put(175135218, 1752f);
|
|
|
90 |
fofoIdAmount.put(175135597, 274f);
|
|
|
91 |
String description = "Scheme IN rolled out for returned items, Now reversed";
|
|
|
92 |
for(Map.Entry<Integer, Float> fofoIdAmountEntry: fofoIdAmount.entrySet()) {
|
|
|
93 |
Integer fofoId=fofoIdAmountEntry.getKey();
|
|
|
94 |
Float amount = fofoIdAmountEntry.getValue();
|
|
|
95 |
walletService.rollbackAmountFromWallet(fofoId, amount, -1, WalletReferenceType.SCHEME_IN, description);
|
|
|
96 |
}
|
|
|
97 |
}
|
| 23755 |
amit.gupta |
98 |
|
| 23898 |
amit.gupta |
99 |
public void populateGrnTimestamp() {
|
|
|
100 |
List<Purchase> allPurchases = purchaseRepository.selectAll();
|
| 23899 |
amit.gupta |
101 |
for (Purchase p : allPurchases) {
|
| 23898 |
amit.gupta |
102 |
String invoiceNumber = p.getPurchaseReference();
|
| 23899 |
amit.gupta |
103 |
if (p.getCompleteTimestamp() == null) {
|
| 23898 |
amit.gupta |
104 |
LOGGER.info("GRN for invoice {} is delivered but partially Completed.", p.getPurchaseReference());
|
|
|
105 |
} else {
|
|
|
106 |
List<Order> orders = orderRepository.selectByAirwayBillOrInvoiceNumber(invoiceNumber, p.getFofoId());
|
| 23899 |
amit.gupta |
107 |
for (Order order : orders) {
|
| 23902 |
amit.gupta |
108 |
if (order.getPartnerGrnTimestamp() == null) {
|
|
|
109 |
order.setPartnerGrnTimestamp(p.getCompleteTimestamp());
|
| 23898 |
amit.gupta |
110 |
orderRepository.persist(order);
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
}
|
| 23899 |
amit.gupta |
115 |
|
| 23898 |
amit.gupta |
116 |
}
|
| 23899 |
amit.gupta |
117 |
|
| 23755 |
amit.gupta |
118 |
public void migarateLineItemsToNewTable() {
|
|
|
119 |
LOGGER.info("Before Migrated LineItems Successfully");
|
| 23824 |
amit.gupta |
120 |
int lineItemImeiId = 0;
|
|
|
121 |
LocalDateTime startDate = null;
|
|
|
122 |
try {
|
|
|
123 |
lineItemImeiId = lineItemImeisRepository.selectMaxId();
|
| 23899 |
amit.gupta |
124 |
LineItem lineItem = lineItemRepository
|
|
|
125 |
.selectById(lineItemImeisRepository.selectById(lineItemImeiId).getLineItemId());
|
| 23824 |
amit.gupta |
126 |
Order order = orderRepository.selectById(lineItem.getOrderId());
|
|
|
127 |
startDate = order.getBillingTimestamp();
|
|
|
128 |
} catch (Exception e) {
|
|
|
129 |
LOGGER.info("Running before first time");
|
| 23826 |
amit.gupta |
130 |
startDate = LocalDateTime.of(LocalDate.of(2017, 7, 1), LocalTime.MIDNIGHT);
|
| 23824 |
amit.gupta |
131 |
}
|
|
|
132 |
List<Order> orders = orderRepository.selectAllByBillingDatesBetween(startDate, LocalDateTime.now());
|
| 23827 |
amit.gupta |
133 |
Collections.reverse(orders);
|
| 23899 |
amit.gupta |
134 |
|
| 23755 |
amit.gupta |
135 |
for (Order order : orders) {
|
| 23824 |
amit.gupta |
136 |
try {
|
| 23767 |
amit.gupta |
137 |
String serialNumbers = order.getLineItem().getSerialNumber();
|
|
|
138 |
if (!StringUtils.isEmpty(serialNumbers)) {
|
|
|
139 |
List<String> serialNumberList = Arrays.asList(serialNumbers.split(","));
|
|
|
140 |
for (String serialNumber : serialNumberList) {
|
|
|
141 |
int lineItemId = order.getLineItem().getId();
|
|
|
142 |
LineItemImei lineItemImei = new LineItemImei();
|
|
|
143 |
lineItemImei.setSerialNumber(serialNumber);
|
|
|
144 |
lineItemImei.setLineItemId(lineItemId);
|
|
|
145 |
lineItemImeisRepository.persist(lineItemImei);
|
|
|
146 |
}
|
|
|
147 |
} else {
|
|
|
148 |
LOGGER.info("Serial Numbers dont exist for Order {}", order.getId());
|
| 23755 |
amit.gupta |
149 |
}
|
| 23824 |
amit.gupta |
150 |
} catch (Exception e) {
|
| 23899 |
amit.gupta |
151 |
LOGGER.info("Error occurred while creating lineitem imei {}, because of {}", order.getId(),
|
|
|
152 |
e.getMessage());
|
| 23755 |
amit.gupta |
153 |
}
|
|
|
154 |
}
|
|
|
155 |
LOGGER.info("Migrated LineItems Successfully");
|
|
|
156 |
}
|
|
|
157 |
}
|