| 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;
|
| 24005 |
amit.gupta |
9 |
import java.util.HashSet;
|
| 23755 |
amit.gupta |
10 |
import java.util.List;
|
| 24002 |
amit.gupta |
11 |
import java.util.Map;
|
| 24641 |
amit.gupta |
12 |
import java.util.stream.Collectors;
|
| 23755 |
amit.gupta |
13 |
|
|
|
14 |
import org.apache.commons.lang.StringUtils;
|
|
|
15 |
import org.apache.logging.log4j.LogManager;
|
|
|
16 |
import org.apache.logging.log4j.Logger;
|
|
|
17 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
18 |
import org.springframework.stereotype.Component;
|
|
|
19 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
20 |
|
| 24005 |
amit.gupta |
21 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 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;
|
| 24002 |
amit.gupta |
27 |
import com.spice.profitmandi.dao.repository.fofo.DebitNoteRepository;
|
|
|
28 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 23898 |
amit.gupta |
29 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 24002 |
amit.gupta |
30 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
| 23755 |
amit.gupta |
31 |
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
|
| 23824 |
amit.gupta |
32 |
import com.spice.profitmandi.dao.repository.transaction.LineItemRepository;
|
| 23755 |
amit.gupta |
33 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
| 24002 |
amit.gupta |
34 |
import com.spice.profitmandi.dao.repository.user.UserRepository;
|
| 23899 |
amit.gupta |
35 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
| 24266 |
amit.gupta |
36 |
import com.spice.profitmandi.service.order.OrderService;
|
| 24005 |
amit.gupta |
37 |
import com.spice.profitmandi.service.pricing.PriceDropService;
|
|
|
38 |
import com.spice.profitmandi.service.scheme.SchemeService;
|
| 23899 |
amit.gupta |
39 |
import com.spice.profitmandi.service.transaction.TransactionService;
|
|
|
40 |
import com.spice.profitmandi.service.user.RetailerService;
|
|
|
41 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 23755 |
amit.gupta |
42 |
|
| 24002 |
amit.gupta |
43 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
44 |
|
| 23755 |
amit.gupta |
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;
|
| 23905 |
amit.gupta |
53 |
|
|
|
54 |
@Autowired
|
| 24002 |
amit.gupta |
55 |
private UserRepository userRepository;
|
|
|
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;
|
| 24266 |
amit.gupta |
68 |
|
|
|
69 |
//Service for Tertiary/Partner Orders
|
|
|
70 |
@Autowired
|
|
|
71 |
private OrderService orderService;
|
| 23767 |
amit.gupta |
72 |
|
| 23755 |
amit.gupta |
73 |
@Autowired
|
| 23899 |
amit.gupta |
74 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
75 |
|
|
|
76 |
@Autowired
|
| 23755 |
amit.gupta |
77 |
private LineItemImeisRepository lineItemImeisRepository;
|
| 24002 |
amit.gupta |
78 |
|
|
|
79 |
@Autowired
|
|
|
80 |
private InventoryItemRepository inventoryItemRepository;
|
| 23901 |
amit.gupta |
81 |
|
| 23898 |
amit.gupta |
82 |
@Autowired
|
| 23899 |
amit.gupta |
83 |
private RetailerService retailerService;
|
| 24002 |
amit.gupta |
84 |
|
|
|
85 |
@Autowired
|
|
|
86 |
private SchemeInOutRepository schemeInOutRepository;
|
|
|
87 |
|
|
|
88 |
@Autowired
|
|
|
89 |
private DebitNoteRepository debitNoteRepository;
|
| 23899 |
amit.gupta |
90 |
|
|
|
91 |
@Autowired
|
| 23898 |
amit.gupta |
92 |
private PurchaseRepository purchaseRepository;
|
| 24002 |
amit.gupta |
93 |
|
| 24005 |
amit.gupta |
94 |
|
|
|
95 |
@Autowired
|
|
|
96 |
private PriceDropService priceDropService;
|
|
|
97 |
|
|
|
98 |
@Autowired
|
|
|
99 |
private SchemeService schemeService;
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
public void dropCorrection() throws Exception {
|
|
|
103 |
|
|
|
104 |
walletService.rollbackAmountFromWallet(175128034, 274, 4, WalletReferenceType.PRICE_DROP, "Scheme differential for Price Drop of Rs.712 on Samsung J6 J600GG, on 01-08-2018 for missing 1pc");
|
|
|
105 |
walletService.rollbackAmountFromWallet(175128034, -259, 4, WalletReferenceType.PRICE_DROP, "Scheme differential for Price Drop of Rs.712 on Samsung J6 J600GG, on 01-08-2018 for missing 1pc");
|
|
|
106 |
List<InventoryItem> iis = inventoryItemRepository.selectByIds(new HashSet<>(Arrays.asList(3518, 3516)));
|
|
|
107 |
schemeService.reverseSchemes(iis, 8, "Scheme differential for Price Drop of Rs.712 on Samsung J6 J600GG, on 01-08-2018. Total 2 item(s)");
|
|
|
108 |
List<InventoryItem> iis1 = inventoryItemRepository.selectByIds(new HashSet<>(Arrays.asList(3502, 3334,3503)));
|
|
|
109 |
schemeService.reverseSchemes(iis1, 13, "Scheme differential for Price Drop of Rs.485 on Samsung Galaxy J4 J400FD, on 18-07-2018. Total 3 item(s)");
|
|
|
110 |
|
|
|
111 |
List<InventoryItem> iis2 = inventoryItemRepository.selectByIds(new HashSet<>(Arrays.asList(3319)));
|
|
|
112 |
schemeService.reverseSchemes(iis2, 13, "Scheme differential for Price Drop of Rs.485 on Samsung Galaxy J4 J400FD, on 18-07-2018. Total 1 item(s)");
|
|
|
113 |
}
|
|
|
114 |
|
| 24002 |
amit.gupta |
115 |
public void schemeRollback() {
|
|
|
116 |
Map<Integer, Float> fofoIdAmount = new HashMap<>();
|
| 24003 |
amit.gupta |
117 |
fofoIdAmount.put(175135218, 1942f);
|
|
|
118 |
String description = "Price drop/differential rolled out as, they were already returned, Total 2pcs.";
|
| 24002 |
amit.gupta |
119 |
for(Map.Entry<Integer, Float> fofoIdAmountEntry: fofoIdAmount.entrySet()) {
|
|
|
120 |
Integer fofoId=fofoIdAmountEntry.getKey();
|
|
|
121 |
Float amount = fofoIdAmountEntry.getValue();
|
| 24003 |
amit.gupta |
122 |
walletService.rollbackAmountFromWallet(fofoId, amount, 4, WalletReferenceType.PRICE_DROP, description);
|
| 24002 |
amit.gupta |
123 |
}
|
| 24004 |
amit.gupta |
124 |
fofoIdAmount.put(175135218, 438f);
|
|
|
125 |
for(Map.Entry<Integer, Float> fofoIdAmountEntry: fofoIdAmount.entrySet()) {
|
|
|
126 |
Integer fofoId=fofoIdAmountEntry.getKey();
|
|
|
127 |
Float amount = fofoIdAmountEntry.getValue();
|
|
|
128 |
walletService.rollbackAmountFromWallet(fofoId, amount, 4, WalletReferenceType.PRICE_DROP, description);
|
|
|
129 |
}
|
| 24002 |
amit.gupta |
130 |
}
|
| 23755 |
amit.gupta |
131 |
|
| 23898 |
amit.gupta |
132 |
public void populateGrnTimestamp() {
|
|
|
133 |
List<Purchase> allPurchases = purchaseRepository.selectAll();
|
| 23899 |
amit.gupta |
134 |
for (Purchase p : allPurchases) {
|
| 23898 |
amit.gupta |
135 |
String invoiceNumber = p.getPurchaseReference();
|
| 23899 |
amit.gupta |
136 |
if (p.getCompleteTimestamp() == null) {
|
| 23898 |
amit.gupta |
137 |
LOGGER.info("GRN for invoice {} is delivered but partially Completed.", p.getPurchaseReference());
|
|
|
138 |
} else {
|
|
|
139 |
List<Order> orders = orderRepository.selectByAirwayBillOrInvoiceNumber(invoiceNumber, p.getFofoId());
|
| 23899 |
amit.gupta |
140 |
for (Order order : orders) {
|
| 23902 |
amit.gupta |
141 |
if (order.getPartnerGrnTimestamp() == null) {
|
|
|
142 |
order.setPartnerGrnTimestamp(p.getCompleteTimestamp());
|
| 23898 |
amit.gupta |
143 |
orderRepository.persist(order);
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
}
|
| 23899 |
amit.gupta |
148 |
|
| 23898 |
amit.gupta |
149 |
}
|
| 23899 |
amit.gupta |
150 |
|
| 23755 |
amit.gupta |
151 |
public void migarateLineItemsToNewTable() {
|
|
|
152 |
LOGGER.info("Before Migrated LineItems Successfully");
|
| 23824 |
amit.gupta |
153 |
int lineItemImeiId = 0;
|
|
|
154 |
LocalDateTime startDate = null;
|
|
|
155 |
try {
|
|
|
156 |
lineItemImeiId = lineItemImeisRepository.selectMaxId();
|
| 23899 |
amit.gupta |
157 |
LineItem lineItem = lineItemRepository
|
|
|
158 |
.selectById(lineItemImeisRepository.selectById(lineItemImeiId).getLineItemId());
|
| 23824 |
amit.gupta |
159 |
Order order = orderRepository.selectById(lineItem.getOrderId());
|
|
|
160 |
startDate = order.getBillingTimestamp();
|
|
|
161 |
} catch (Exception e) {
|
|
|
162 |
LOGGER.info("Running before first time");
|
| 23826 |
amit.gupta |
163 |
startDate = LocalDateTime.of(LocalDate.of(2017, 7, 1), LocalTime.MIDNIGHT);
|
| 23824 |
amit.gupta |
164 |
}
|
|
|
165 |
List<Order> orders = orderRepository.selectAllByBillingDatesBetween(startDate, LocalDateTime.now());
|
| 23827 |
amit.gupta |
166 |
Collections.reverse(orders);
|
| 23899 |
amit.gupta |
167 |
|
| 23755 |
amit.gupta |
168 |
for (Order order : orders) {
|
| 23824 |
amit.gupta |
169 |
try {
|
| 23767 |
amit.gupta |
170 |
String serialNumbers = order.getLineItem().getSerialNumber();
|
|
|
171 |
if (!StringUtils.isEmpty(serialNumbers)) {
|
|
|
172 |
List<String> serialNumberList = Arrays.asList(serialNumbers.split(","));
|
|
|
173 |
for (String serialNumber : serialNumberList) {
|
|
|
174 |
int lineItemId = order.getLineItem().getId();
|
|
|
175 |
LineItemImei lineItemImei = new LineItemImei();
|
|
|
176 |
lineItemImei.setSerialNumber(serialNumber);
|
|
|
177 |
lineItemImei.setLineItemId(lineItemId);
|
|
|
178 |
lineItemImeisRepository.persist(lineItemImei);
|
|
|
179 |
}
|
|
|
180 |
} else {
|
|
|
181 |
LOGGER.info("Serial Numbers dont exist for Order {}", order.getId());
|
| 23755 |
amit.gupta |
182 |
}
|
| 23824 |
amit.gupta |
183 |
} catch (Exception e) {
|
| 23899 |
amit.gupta |
184 |
LOGGER.info("Error occurred while creating lineitem imei {}, because of {}", order.getId(),
|
|
|
185 |
e.getMessage());
|
| 23755 |
amit.gupta |
186 |
}
|
|
|
187 |
}
|
|
|
188 |
LOGGER.info("Migrated LineItems Successfully");
|
|
|
189 |
}
|
| 24266 |
amit.gupta |
190 |
|
|
|
191 |
public void cancelOrder(List<String> invoiceNumbers) throws Exception {
|
|
|
192 |
orderService.cancelOrder(invoiceNumbers);
|
|
|
193 |
}
|
| 24641 |
amit.gupta |
194 |
|
|
|
195 |
|
|
|
196 |
public void migratePurchase() {
|
|
|
197 |
List<Purchase> purchases = purchaseRepository.selectPurchaseAllPurchasesLessThanZero();
|
|
|
198 |
for(Purchase purchase : purchases) {
|
|
|
199 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchase.getId());
|
|
|
200 |
List<Order> orders = orderRepository.selectByAirwayBillOrInvoiceNumber(purchase.getPurchaseReference(), purchase.getFofoId());
|
|
|
201 |
Map<Integer, Integer> ourSaleItemQtyMap = orders.stream().collect(Collectors.groupingBy(x->x.getLineItem().getItemId(),
|
|
|
202 |
Collectors.summingInt(x->x.getLineItem().getQuantity())));
|
|
|
203 |
Map<Integer, Integer> theirPurchaseItemQtyMap = inventoryItems.stream().collect(Collectors.groupingBy(InventoryItem::getItemId,
|
|
|
204 |
Collectors.summingInt(InventoryItem::getInitialQuantity)));
|
|
|
205 |
for (Map.Entry<Integer, Integer> partnerPurchaseEntry : theirPurchaseItemQtyMap.entrySet()) {
|
| 24645 |
amit.gupta |
206 |
if(!ourSaleItemQtyMap.containsKey(partnerPurchaseEntry.getKey())) {
|
|
|
207 |
LOGGER.info("Cannot find in Invoice {} item {}", purchase.getPurchaseReference(), partnerPurchaseEntry.getKey());
|
|
|
208 |
}
|
| 24641 |
amit.gupta |
209 |
int ourSale = ourSaleItemQtyMap.get(partnerPurchaseEntry.getKey());
|
|
|
210 |
if(partnerPurchaseEntry.getValue() - ourSale > 0) {
|
|
|
211 |
int quantityToReduce = partnerPurchaseEntry.getValue() - ourSale;
|
|
|
212 |
for(InventoryItem ii : inventoryItems) {
|
|
|
213 |
if(ii.getGoodQuantity() >= quantityToReduce && ii.getInitialQuantity() > quantityToReduce) {
|
|
|
214 |
LOGGER.info("Invoice {} item {} can be reduced to {}", purchase.getPurchaseReference(), ii.getItemId(), ii.getGoodQuantity() - quantityToReduce);
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
}
|
|
|
220 |
}
|
| 23755 |
amit.gupta |
221 |
}
|