| 23755 |
amit.gupta |
1 |
package com.smartdukaan.cron.migrations;
|
|
|
2 |
|
| 24819 |
amit.gupta |
3 |
import java.io.Serializable;
|
| 23824 |
amit.gupta |
4 |
import java.time.LocalDate;
|
| 23755 |
amit.gupta |
5 |
import java.time.LocalDateTime;
|
| 23824 |
amit.gupta |
6 |
import java.time.LocalTime;
|
| 24819 |
amit.gupta |
7 |
import java.util.ArrayList;
|
| 23755 |
amit.gupta |
8 |
import java.util.Arrays;
|
| 23827 |
amit.gupta |
9 |
import java.util.Collections;
|
| 24002 |
amit.gupta |
10 |
import java.util.HashMap;
|
| 24005 |
amit.gupta |
11 |
import java.util.HashSet;
|
| 23755 |
amit.gupta |
12 |
import java.util.List;
|
| 24002 |
amit.gupta |
13 |
import java.util.Map;
|
| 24641 |
amit.gupta |
14 |
import java.util.stream.Collectors;
|
| 23755 |
amit.gupta |
15 |
|
| 24819 |
amit.gupta |
16 |
import org.apache.commons.io.output.ByteArrayOutputStream;
|
| 23755 |
amit.gupta |
17 |
import org.apache.commons.lang.StringUtils;
|
|
|
18 |
import org.apache.logging.log4j.LogManager;
|
|
|
19 |
import org.apache.logging.log4j.Logger;
|
|
|
20 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 24819 |
amit.gupta |
21 |
import org.springframework.core.io.ByteArrayResource;
|
| 24767 |
amit.gupta |
22 |
import org.springframework.mail.javamail.JavaMailSender;
|
| 23755 |
amit.gupta |
23 |
import org.springframework.stereotype.Component;
|
|
|
24 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
25 |
|
| 24962 |
amit.gupta |
26 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 24819 |
amit.gupta |
27 |
import com.spice.profitmandi.common.util.FileUtil;
|
|
|
28 |
import com.spice.profitmandi.common.util.Utils;
|
| 24716 |
amit.gupta |
29 |
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
|
| 24806 |
amit.gupta |
30 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
|
|
31 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
|
| 24005 |
amit.gupta |
32 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 24883 |
amit.gupta |
33 |
import com.spice.profitmandi.dao.entity.fofo.PartnerTargetDetails;
|
| 23898 |
amit.gupta |
34 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 24716 |
amit.gupta |
35 |
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
|
| 24883 |
amit.gupta |
36 |
import com.spice.profitmandi.dao.entity.fofo.TargetSlab;
|
| 24953 |
amit.gupta |
37 |
import com.spice.profitmandi.dao.entity.inventory.SaholicInventorySnapshot;
|
| 24962 |
amit.gupta |
38 |
import com.spice.profitmandi.dao.entity.inventory.SaholicReservationInventorySnapshot;
|
| 23824 |
amit.gupta |
39 |
import com.spice.profitmandi.dao.entity.transaction.LineItem;
|
| 23755 |
amit.gupta |
40 |
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
|
|
|
41 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 24802 |
amit.gupta |
42 |
import com.spice.profitmandi.dao.entity.transaction.SellerWarehouse;
|
| 24716 |
amit.gupta |
43 |
import com.spice.profitmandi.dao.repository.GenericRepository;
|
| 23899 |
amit.gupta |
44 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 24716 |
amit.gupta |
45 |
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
|
| 24002 |
amit.gupta |
46 |
import com.spice.profitmandi.dao.repository.fofo.DebitNoteRepository;
|
| 24806 |
amit.gupta |
47 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
|
|
48 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
| 24002 |
amit.gupta |
49 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 24806 |
amit.gupta |
50 |
import com.spice.profitmandi.dao.repository.fofo.InvoiceNumberGenerationSequenceRepository;
|
| 24883 |
amit.gupta |
51 |
import com.spice.profitmandi.dao.repository.fofo.PartnerTargetRepository;
|
| 23898 |
amit.gupta |
52 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 24716 |
amit.gupta |
53 |
import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;
|
| 24002 |
amit.gupta |
54 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
| 24883 |
amit.gupta |
55 |
import com.spice.profitmandi.dao.repository.fofo.TargetSlabRepository;
|
|
|
56 |
import com.spice.profitmandi.dao.repository.fofo.TargetSlabRepositoryImpl;
|
| 24953 |
amit.gupta |
57 |
import com.spice.profitmandi.dao.repository.inventory.SaholicInventorySnapshotRepository;
|
|
|
58 |
import com.spice.profitmandi.dao.repository.inventory.SaholicReservationInventorySnapshotRepository;
|
| 23755 |
amit.gupta |
59 |
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
|
| 23824 |
amit.gupta |
60 |
import com.spice.profitmandi.dao.repository.transaction.LineItemRepository;
|
| 23755 |
amit.gupta |
61 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
| 24772 |
amit.gupta |
62 |
import com.spice.profitmandi.dao.repository.transaction.ReturnOrderRepository;
|
| 24802 |
amit.gupta |
63 |
import com.spice.profitmandi.dao.repository.transaction.SellerWarehouseRepository;
|
| 24767 |
amit.gupta |
64 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
|
|
|
65 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
| 24002 |
amit.gupta |
66 |
import com.spice.profitmandi.dao.repository.user.UserRepository;
|
| 23899 |
amit.gupta |
67 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
| 24266 |
amit.gupta |
68 |
import com.spice.profitmandi.service.order.OrderService;
|
| 24005 |
amit.gupta |
69 |
import com.spice.profitmandi.service.pricing.PriceDropService;
|
|
|
70 |
import com.spice.profitmandi.service.scheme.SchemeService;
|
| 23899 |
amit.gupta |
71 |
import com.spice.profitmandi.service.transaction.TransactionService;
|
|
|
72 |
import com.spice.profitmandi.service.user.RetailerService;
|
|
|
73 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 23755 |
amit.gupta |
74 |
|
| 24953 |
amit.gupta |
75 |
import in.shop2020.model.v1.order.OrderStatus;
|
| 24002 |
amit.gupta |
76 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
77 |
|
| 23755 |
amit.gupta |
78 |
@Component
|
|
|
79 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
80 |
public class RunOnceTasks {
|
|
|
81 |
|
|
|
82 |
private static final Logger LOGGER = LogManager.getLogger(RunOnceTasks.class);
|
|
|
83 |
|
|
|
84 |
@Autowired
|
| 23824 |
amit.gupta |
85 |
private LineItemRepository lineItemRepository;
|
| 24711 |
amit.gupta |
86 |
|
| 23905 |
amit.gupta |
87 |
@Autowired
|
| 24953 |
amit.gupta |
88 |
private SaholicInventorySnapshotRepository saholicInventorySnapshotRepository;
|
|
|
89 |
|
|
|
90 |
@Autowired
|
|
|
91 |
private SaholicReservationInventorySnapshotRepository saholicReservationInventorySnapshotRepository;
|
|
|
92 |
|
|
|
93 |
@Autowired
|
| 24883 |
amit.gupta |
94 |
private TargetSlabRepository targetSlabRepository;
|
|
|
95 |
|
|
|
96 |
@Autowired
|
|
|
97 |
private PartnerTargetRepository partnerTargetRepository;
|
|
|
98 |
|
|
|
99 |
@Autowired
|
| 24802 |
amit.gupta |
100 |
private SellerWarehouseRepository sellerWarehouseRepository;
|
| 24814 |
amit.gupta |
101 |
|
| 24802 |
amit.gupta |
102 |
@Autowired
|
| 24806 |
amit.gupta |
103 |
private FofoOrderItemRepository fofoOrderItemRepository;
|
| 24814 |
amit.gupta |
104 |
|
| 24806 |
amit.gupta |
105 |
@Autowired
|
|
|
106 |
private FofoOrderRepository fofoOrderRepository;
|
| 24814 |
amit.gupta |
107 |
|
| 24806 |
amit.gupta |
108 |
@Autowired
|
| 24767 |
amit.gupta |
109 |
private UserWalletRepository userWalletRepository;
|
|
|
110 |
|
|
|
111 |
@Autowired
|
|
|
112 |
private UserWalletHistoryRepository userWalletHistoryRepository;
|
|
|
113 |
|
|
|
114 |
@Autowired
|
| 24002 |
amit.gupta |
115 |
private UserRepository userRepository;
|
| 24711 |
amit.gupta |
116 |
|
| 24002 |
amit.gupta |
117 |
@Autowired
|
| 23899 |
amit.gupta |
118 |
private WalletService walletService;
|
|
|
119 |
|
|
|
120 |
@Autowired
|
|
|
121 |
private InventoryService inventoryService;
|
|
|
122 |
|
|
|
123 |
@Autowired
|
|
|
124 |
private TransactionService transactionService;
|
|
|
125 |
|
| 24711 |
amit.gupta |
126 |
// Service for Tertiary/Partner Orders
|
| 24266 |
amit.gupta |
127 |
@Autowired
|
|
|
128 |
private OrderService orderService;
|
| 23767 |
amit.gupta |
129 |
|
| 23755 |
amit.gupta |
130 |
@Autowired
|
| 24772 |
amit.gupta |
131 |
private ReturnOrderRepository returnOrderRepository;
|
|
|
132 |
|
|
|
133 |
@Autowired
|
| 23899 |
amit.gupta |
134 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
135 |
|
|
|
136 |
@Autowired
|
| 23755 |
amit.gupta |
137 |
private LineItemImeisRepository lineItemImeisRepository;
|
| 24711 |
amit.gupta |
138 |
|
| 24002 |
amit.gupta |
139 |
@Autowired
|
|
|
140 |
private InventoryItemRepository inventoryItemRepository;
|
| 24814 |
amit.gupta |
141 |
|
| 24806 |
amit.gupta |
142 |
@Autowired
|
|
|
143 |
private InvoiceNumberGenerationSequenceRepository invoiceNumberGenerationSequenceRepository;
|
| 23901 |
amit.gupta |
144 |
|
| 23898 |
amit.gupta |
145 |
@Autowired
|
| 23899 |
amit.gupta |
146 |
private RetailerService retailerService;
|
| 24711 |
amit.gupta |
147 |
|
| 24002 |
amit.gupta |
148 |
@Autowired
|
|
|
149 |
private SchemeInOutRepository schemeInOutRepository;
|
| 24711 |
amit.gupta |
150 |
|
| 24002 |
amit.gupta |
151 |
@Autowired
|
|
|
152 |
private DebitNoteRepository debitNoteRepository;
|
| 23899 |
amit.gupta |
153 |
|
|
|
154 |
@Autowired
|
| 24716 |
amit.gupta |
155 |
private GenericRepository genericRepository;
|
|
|
156 |
|
|
|
157 |
@Autowired
|
| 23898 |
amit.gupta |
158 |
private PurchaseRepository purchaseRepository;
|
| 24711 |
amit.gupta |
159 |
|
| 24005 |
amit.gupta |
160 |
@Autowired
|
|
|
161 |
private PriceDropService priceDropService;
|
| 24883 |
amit.gupta |
162 |
|
| 24819 |
amit.gupta |
163 |
@Autowired
|
|
|
164 |
private JavaMailSender googleMailSender;
|
| 24711 |
amit.gupta |
165 |
|
| 24005 |
amit.gupta |
166 |
@Autowired
|
|
|
167 |
private SchemeService schemeService;
|
| 24711 |
amit.gupta |
168 |
|
| 24716 |
amit.gupta |
169 |
@Autowired
|
|
|
170 |
private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
|
|
|
171 |
|
|
|
172 |
@Autowired
|
| 24767 |
amit.gupta |
173 |
private OrderRepository orderRepository;
|
|
|
174 |
|
|
|
175 |
@Autowired
|
| 24716 |
amit.gupta |
176 |
private ScanRecordRepository scanRecordRepository;
|
|
|
177 |
|
| 24767 |
amit.gupta |
178 |
@Autowired
|
|
|
179 |
private JavaMailSender mailSender;
|
|
|
180 |
|
| 24005 |
amit.gupta |
181 |
public void dropCorrection() throws Exception {
|
|
|
182 |
|
| 24711 |
amit.gupta |
183 |
walletService.rollbackAmountFromWallet(175128034, 274, 4, WalletReferenceType.PRICE_DROP,
|
|
|
184 |
"Scheme differential for Price Drop of Rs.712 on Samsung J6 J600GG, on 01-08-2018 for missing 1pc");
|
|
|
185 |
walletService.rollbackAmountFromWallet(175128034, -259, 4, WalletReferenceType.PRICE_DROP,
|
|
|
186 |
"Scheme differential for Price Drop of Rs.712 on Samsung J6 J600GG, on 01-08-2018 for missing 1pc");
|
|
|
187 |
List<InventoryItem> iis = inventoryItemRepository.selectByIds(new HashSet<>(Arrays.asList(3518, 3516)));
|
|
|
188 |
schemeService.reverseSchemes(iis, 8,
|
|
|
189 |
"Scheme differential for Price Drop of Rs.712 on Samsung J6 J600GG, on 01-08-2018. Total 2 item(s)");
|
|
|
190 |
List<InventoryItem> iis1 = inventoryItemRepository.selectByIds(new HashSet<>(Arrays.asList(3502, 3334, 3503)));
|
|
|
191 |
schemeService.reverseSchemes(iis1, 13,
|
|
|
192 |
"Scheme differential for Price Drop of Rs.485 on Samsung Galaxy J4 J400FD, on 18-07-2018. Total 3 item(s)");
|
|
|
193 |
|
|
|
194 |
List<InventoryItem> iis2 = inventoryItemRepository.selectByIds(new HashSet<>(Arrays.asList(3319)));
|
|
|
195 |
schemeService.reverseSchemes(iis2, 13,
|
|
|
196 |
"Scheme differential for Price Drop of Rs.485 on Samsung Galaxy J4 J400FD, on 18-07-2018. Total 1 item(s)");
|
| 24005 |
amit.gupta |
197 |
}
|
| 24711 |
amit.gupta |
198 |
|
| 24002 |
amit.gupta |
199 |
public void schemeRollback() {
|
|
|
200 |
Map<Integer, Float> fofoIdAmount = new HashMap<>();
|
| 24003 |
amit.gupta |
201 |
fofoIdAmount.put(175135218, 1942f);
|
|
|
202 |
String description = "Price drop/differential rolled out as, they were already returned, Total 2pcs.";
|
| 24711 |
amit.gupta |
203 |
for (Map.Entry<Integer, Float> fofoIdAmountEntry : fofoIdAmount.entrySet()) {
|
|
|
204 |
Integer fofoId = fofoIdAmountEntry.getKey();
|
| 24002 |
amit.gupta |
205 |
Float amount = fofoIdAmountEntry.getValue();
|
| 24003 |
amit.gupta |
206 |
walletService.rollbackAmountFromWallet(fofoId, amount, 4, WalletReferenceType.PRICE_DROP, description);
|
| 24002 |
amit.gupta |
207 |
}
|
| 24004 |
amit.gupta |
208 |
fofoIdAmount.put(175135218, 438f);
|
| 24711 |
amit.gupta |
209 |
for (Map.Entry<Integer, Float> fofoIdAmountEntry : fofoIdAmount.entrySet()) {
|
|
|
210 |
Integer fofoId = fofoIdAmountEntry.getKey();
|
| 24004 |
amit.gupta |
211 |
Float amount = fofoIdAmountEntry.getValue();
|
|
|
212 |
walletService.rollbackAmountFromWallet(fofoId, amount, 4, WalletReferenceType.PRICE_DROP, description);
|
|
|
213 |
}
|
| 24002 |
amit.gupta |
214 |
}
|
| 23755 |
amit.gupta |
215 |
|
| 23898 |
amit.gupta |
216 |
public void populateGrnTimestamp() {
|
|
|
217 |
List<Purchase> allPurchases = purchaseRepository.selectAll();
|
| 23899 |
amit.gupta |
218 |
for (Purchase p : allPurchases) {
|
| 23898 |
amit.gupta |
219 |
String invoiceNumber = p.getPurchaseReference();
|
| 23899 |
amit.gupta |
220 |
if (p.getCompleteTimestamp() == null) {
|
| 23898 |
amit.gupta |
221 |
LOGGER.info("GRN for invoice {} is delivered but partially Completed.", p.getPurchaseReference());
|
|
|
222 |
} else {
|
|
|
223 |
List<Order> orders = orderRepository.selectByAirwayBillOrInvoiceNumber(invoiceNumber, p.getFofoId());
|
| 23899 |
amit.gupta |
224 |
for (Order order : orders) {
|
| 23902 |
amit.gupta |
225 |
if (order.getPartnerGrnTimestamp() == null) {
|
|
|
226 |
order.setPartnerGrnTimestamp(p.getCompleteTimestamp());
|
| 23898 |
amit.gupta |
227 |
orderRepository.persist(order);
|
|
|
228 |
}
|
|
|
229 |
}
|
|
|
230 |
}
|
|
|
231 |
}
|
| 23899 |
amit.gupta |
232 |
|
| 23898 |
amit.gupta |
233 |
}
|
| 23899 |
amit.gupta |
234 |
|
| 24716 |
amit.gupta |
235 |
public void migarateLineItemsToNewTable() throws Exception {
|
| 23755 |
amit.gupta |
236 |
LOGGER.info("Before Migrated LineItems Successfully");
|
| 23824 |
amit.gupta |
237 |
int lineItemImeiId = 0;
|
|
|
238 |
LocalDateTime startDate = null;
|
|
|
239 |
try {
|
|
|
240 |
lineItemImeiId = lineItemImeisRepository.selectMaxId();
|
| 23899 |
amit.gupta |
241 |
LineItem lineItem = lineItemRepository
|
|
|
242 |
.selectById(lineItemImeisRepository.selectById(lineItemImeiId).getLineItemId());
|
| 23824 |
amit.gupta |
243 |
Order order = orderRepository.selectById(lineItem.getOrderId());
|
|
|
244 |
startDate = order.getBillingTimestamp();
|
|
|
245 |
} catch (Exception e) {
|
|
|
246 |
LOGGER.info("Running before first time");
|
| 23826 |
amit.gupta |
247 |
startDate = LocalDateTime.of(LocalDate.of(2017, 7, 1), LocalTime.MIDNIGHT);
|
| 23824 |
amit.gupta |
248 |
}
|
|
|
249 |
List<Order> orders = orderRepository.selectAllByBillingDatesBetween(startDate, LocalDateTime.now());
|
| 23827 |
amit.gupta |
250 |
Collections.reverse(orders);
|
| 23899 |
amit.gupta |
251 |
|
| 23755 |
amit.gupta |
252 |
for (Order order : orders) {
|
| 23824 |
amit.gupta |
253 |
try {
|
| 23767 |
amit.gupta |
254 |
String serialNumbers = order.getLineItem().getSerialNumber();
|
|
|
255 |
if (!StringUtils.isEmpty(serialNumbers)) {
|
|
|
256 |
List<String> serialNumberList = Arrays.asList(serialNumbers.split(","));
|
|
|
257 |
for (String serialNumber : serialNumberList) {
|
|
|
258 |
int lineItemId = order.getLineItem().getId();
|
|
|
259 |
LineItemImei lineItemImei = new LineItemImei();
|
|
|
260 |
lineItemImei.setSerialNumber(serialNumber);
|
|
|
261 |
lineItemImei.setLineItemId(lineItemId);
|
|
|
262 |
lineItemImeisRepository.persist(lineItemImei);
|
|
|
263 |
}
|
|
|
264 |
} else {
|
|
|
265 |
LOGGER.info("Serial Numbers dont exist for Order {}", order.getId());
|
| 23755 |
amit.gupta |
266 |
}
|
| 23824 |
amit.gupta |
267 |
} catch (Exception e) {
|
| 23899 |
amit.gupta |
268 |
LOGGER.info("Error occurred while creating lineitem imei {}, because of {}", order.getId(),
|
|
|
269 |
e.getMessage());
|
| 23755 |
amit.gupta |
270 |
}
|
|
|
271 |
}
|
|
|
272 |
LOGGER.info("Migrated LineItems Successfully");
|
|
|
273 |
}
|
| 24266 |
amit.gupta |
274 |
|
|
|
275 |
public void cancelOrder(List<String> invoiceNumbers) throws Exception {
|
|
|
276 |
orderService.cancelOrder(invoiceNumbers);
|
|
|
277 |
}
|
| 24711 |
amit.gupta |
278 |
|
| 24722 |
amit.gupta |
279 |
public void migratePurchase() throws Exception {
|
| 24641 |
amit.gupta |
280 |
List<Purchase> purchases = purchaseRepository.selectPurchaseAllPurchasesLessThanZero();
|
| 24706 |
amit.gupta |
281 |
System.out.printf("Total Purchases count is %s", purchases.size());
|
| 24711 |
amit.gupta |
282 |
for (Purchase purchase : purchases) {
|
|
|
283 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchase.getId());
|
|
|
284 |
Map<Integer, List<InventoryItem>> itemIdInventoryMap = inventoryItems.stream()
|
|
|
285 |
.collect(Collectors.groupingBy(InventoryItem::getItemId));
|
|
|
286 |
List<Order> orders = orderRepository.selectByAirwayBillOrInvoiceNumber(purchase.getPurchaseReference(),
|
|
|
287 |
purchase.getFofoId());
|
|
|
288 |
Map<Integer, Integer> ourSaleItemQtyMap = orders.stream().collect(Collectors.groupingBy(
|
|
|
289 |
x -> x.getLineItem().getItemId(), Collectors.summingInt(x -> x.getLineItem().getQuantity())));
|
|
|
290 |
Map<Integer, Integer> theirPurchaseItemQtyMap = inventoryItems.stream().collect(Collectors
|
|
|
291 |
.groupingBy(InventoryItem::getItemId, Collectors.summingInt(InventoryItem::getInitialQuantity)));
|
| 24709 |
amit.gupta |
292 |
for (Map.Entry<Integer, Integer> itemQtyEntry : theirPurchaseItemQtyMap.entrySet()) {
|
| 24711 |
amit.gupta |
293 |
if (!ourSaleItemQtyMap.containsKey(itemQtyEntry.getKey())) {
|
|
|
294 |
LOGGER.info("Cannot find in Invoice {} item {}", purchase.getPurchaseReference(),
|
|
|
295 |
itemQtyEntry.getKey());
|
| 24646 |
amit.gupta |
296 |
continue;
|
| 24645 |
amit.gupta |
297 |
}
|
| 24709 |
amit.gupta |
298 |
int ourSale = ourSaleItemQtyMap.get(itemQtyEntry.getKey());
|
| 24713 |
amit.gupta |
299 |
int quantityToReduce = itemQtyEntry.getValue() - ourSale;
|
| 24716 |
amit.gupta |
300 |
List<InventoryItem> itemIis = itemIdInventoryMap.get(itemQtyEntry.getKey());
|
|
|
301 |
if (itemIdInventoryMap != null) {
|
|
|
302 |
for (InventoryItem ii : itemIis) {
|
| 24915 |
amit.gupta |
303 |
if (ii.getSerialNumber() == null && ii.getGoodQuantity() == ii.getInitialQuantity()
|
|
|
304 |
&& quantityToReduce >= ii.getInitialQuantity()) {
|
| 24734 |
amit.gupta |
305 |
LOGGER.info("Changed in inventoryItems {}, {}, {}, {}, {}, {}",
|
| 24716 |
amit.gupta |
306 |
purchase.getPurchaseReference(), ii.getId(), ii.getItemId(),
|
| 24734 |
amit.gupta |
307 |
ii.getInitialQuantity(), ii.getGoodQuantity(), quantityToReduce);
|
| 24912 |
amit.gupta |
308 |
List<ScanRecord> scanRecords = scanRecordRepository.selectByInventoryItemId(ii.getId());
|
|
|
309 |
for (ScanRecord scanRecord : scanRecords) {
|
| 24915 |
amit.gupta |
310 |
CurrentInventorySnapshot cis = currentInventorySnapshotRepository
|
|
|
311 |
.selectByItemIdAndFofoId(itemQtyEntry.getKey(), purchase.getFofoId());
|
|
|
312 |
scanRecord.setQuantity(0);
|
|
|
313 |
ii.setGoodQuantity(0);
|
|
|
314 |
quantityToReduce = quantityToReduce - ii.getInitialQuantity();
|
|
|
315 |
cis.setAvailability(cis.getAvailability() - ii.getInitialQuantity());
|
|
|
316 |
purchase.setUnfullfilledNonSerializedQuantity(
|
|
|
317 |
purchase.getUnfullfilledNonSerializedQuantity() + quantityToReduce);
|
|
|
318 |
LOGGER.info("Rectified {}, {}, {}, {}, {}, {}", purchase.getPurchaseReference(),
|
|
|
319 |
ii.getId(), ii.getItemId(), ii.getInitialQuantity(), ii.getGoodQuantity(),
|
|
|
320 |
quantityToReduce);
|
| 24711 |
amit.gupta |
321 |
}
|
| 24641 |
amit.gupta |
322 |
}
|
|
|
323 |
}
|
|
|
324 |
}
|
|
|
325 |
}
|
|
|
326 |
}
|
| 24953 |
amit.gupta |
327 |
// throw new Exception();
|
| 24641 |
amit.gupta |
328 |
}
|
| 24767 |
amit.gupta |
329 |
|
| 24802 |
amit.gupta |
330 |
public void migrateChallansToInvoices() throws Exception {
|
| 24814 |
amit.gupta |
331 |
Map<String, List<Order>> invoiceOrdersMap = orderRepository.selectAllChallans().stream()
|
|
|
332 |
.filter(x -> !x.getLineItem().getHsnCode().equals("NOGST"))
|
| 24794 |
amit.gupta |
333 |
.collect(Collectors.groupingBy(Order::getInvoiceNumber, Collectors.toList()));
|
| 24814 |
amit.gupta |
334 |
|
| 24819 |
amit.gupta |
335 |
List<List<? extends Serializable>> rows = new ArrayList<>();
|
| 24814 |
amit.gupta |
336 |
for (String invoice : invoiceOrdersMap.keySet()) {
|
| 24794 |
amit.gupta |
337 |
Order oneOrder = invoiceOrdersMap.get(invoice).get(0);
|
|
|
338 |
int totalOrders = invoiceOrdersMap.get(invoice).size();
|
|
|
339 |
LineItem lineItem = oneOrder.getLineItem();
|
| 24802 |
amit.gupta |
340 |
oneOrder.setBillingTimestamp(LocalDateTime.now());
|
|
|
341 |
oneOrder.setInvoiceNumber(getInvoiceNumber(oneOrder));
|
| 24819 |
amit.gupta |
342 |
rows.add(Arrays.asList(oneOrder.getId(), invoice, oneOrder.getInvoiceNumber(), lineItem.getQuantity()));
|
| 24883 |
amit.gupta |
343 |
LOGGER.info(invoice + "\t" + oneOrder.getInvoiceNumber() + oneOrder.getId() + "\t",
|
|
|
344 |
"\t" + totalOrders + "\t" + lineItem.getQuantity());
|
| 24814 |
amit.gupta |
345 |
Purchase p = null;
|
|
|
346 |
try {
|
|
|
347 |
p = purchaseRepository.selectByPurchaseReferenceAndFofoId(invoice, oneOrder.getRetailerId());
|
|
|
348 |
} catch (Exception e) {
|
| 24815 |
amit.gupta |
349 |
LOGGER.info("Could not find purchase for invoice {}", invoice);
|
| 24812 |
amit.gupta |
350 |
}
|
| 24814 |
amit.gupta |
351 |
if (p != null) {
|
|
|
352 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(p.getId());
|
|
|
353 |
for (InventoryItem inventoryItem : inventoryItems) {
|
| 24815 |
amit.gupta |
354 |
LOGGER.info(inventoryItem.getItemId() + " " + inventoryItem.getGoodQuantity() + " "
|
| 24883 |
amit.gupta |
355 |
+ inventoryItem.getHsnCode() + " " + inventoryItem.getSerialNumber() + " "
|
|
|
356 |
+ p.getPurchaseReference());
|
| 24814 |
amit.gupta |
357 |
}
|
|
|
358 |
}
|
|
|
359 |
|
| 24794 |
amit.gupta |
360 |
}
|
| 24818 |
amit.gupta |
361 |
changePartnerInvoices();
|
| 24883 |
amit.gupta |
362 |
ByteArrayOutputStream baos = FileUtil
|
|
|
363 |
.getCSVByteStream(Arrays.asList("Order id", "Challan", "Invoice", "Quantity"), rows);
|
|
|
364 |
|
| 24819 |
amit.gupta |
365 |
Utils.sendMailWithAttachment(googleMailSender,
|
|
|
366 |
new String[] { "amit.gupta@shop2020.in", "sunny.yadav@smartdukaan.com" }, null,
|
| 24883 |
amit.gupta |
367 |
"Challans Converted to Invoice", "PFA", "Challans-To-Invoice.csv",
|
|
|
368 |
new ByteArrayResource(baos.toByteArray()));
|
| 24916 |
amit.gupta |
369 |
throw new Exception();
|
| 24794 |
amit.gupta |
370 |
}
|
| 24767 |
amit.gupta |
371 |
|
| 24802 |
amit.gupta |
372 |
private String getInvoiceNumber(Order oneOrder) {
|
| 24805 |
amit.gupta |
373 |
String prefix = oneOrder.getInvoiceNumber().split("-")[1].replaceAll("\\d*", "");
|
| 24803 |
amit.gupta |
374 |
System.out.println("Prefix is " + prefix);
|
| 24802 |
amit.gupta |
375 |
SellerWarehouse sellerWarehouse = sellerWarehouseRepository.selectByPrefix(prefix);
|
|
|
376 |
int newSequence = sellerWarehouse.getInvoiceSequence() + 1;
|
|
|
377 |
sellerWarehouse.setInvoiceSequence(newSequence);
|
| 24814 |
amit.gupta |
378 |
return prefix + newSequence;
|
| 24802 |
amit.gupta |
379 |
}
|
| 24814 |
amit.gupta |
380 |
|
| 24806 |
amit.gupta |
381 |
private void changePartnerInvoices() throws Exception {
|
| 24814 |
amit.gupta |
382 |
List<FofoOrder> fofoOrders = fofoOrderRepository.selectByInvoiceNumberLike("%SEC%");
|
|
|
383 |
for (FofoOrder fofoOrder : fofoOrders) {
|
| 24806 |
amit.gupta |
384 |
FofoOrderItem fofoOrderItem = fofoOrderItemRepository.selectByOrderId(fofoOrder.getId()).get(0);
|
| 24814 |
amit.gupta |
385 |
if (fofoOrderItem.getBrand().equals("Vivo")) {
|
| 24806 |
amit.gupta |
386 |
String challanString = fofoOrder.getInvoiceNumber();
|
| 24814 |
amit.gupta |
387 |
String storeCode = invoiceNumberGenerationSequenceRepository.selectByFofoId(fofoOrder.getFofoId())
|
|
|
388 |
.getPrefix();
|
| 24806 |
amit.gupta |
389 |
String invoiceNumber = orderService.getInvoiceNumber(fofoOrder.getFofoId(), storeCode);
|
|
|
390 |
fofoOrder.setInvoiceNumber(invoiceNumber);
|
|
|
391 |
fofoOrder.setCreateTimestamp(LocalDateTime.now());
|
| 24818 |
amit.gupta |
392 |
LOGGER.info(challanString + "\t" + invoiceNumber + "\t" + fofoOrderItem.getQuantity());
|
| 24806 |
amit.gupta |
393 |
}
|
| 24814 |
amit.gupta |
394 |
|
| 24806 |
amit.gupta |
395 |
}
|
|
|
396 |
}
|
| 24802 |
amit.gupta |
397 |
|
| 24883 |
amit.gupta |
398 |
public void createMonthlyTargets() throws Exception {
|
|
|
399 |
Map<String, List<TargetSlab>> defaultSlabs = targetSlabRepository.getAllDefaultSlabs();
|
|
|
400 |
for (String memberType : defaultSlabs.keySet()) {
|
|
|
401 |
PartnerTargetDetails ptd = new PartnerTargetDetails();
|
|
|
402 |
ptd.setBrandName(null);
|
|
|
403 |
|
|
|
404 |
LocalDate startDate = LocalDate.now().withDayOfMonth(1);
|
|
|
405 |
int totalDays = startDate.lengthOfMonth();
|
|
|
406 |
String monthName = startDate.getMonth().toString();
|
|
|
407 |
ptd.setStartDate(startDate.atStartOfDay());
|
|
|
408 |
ptd.setEndDate(startDate.plusDays(totalDays - 1).atTime(LocalTime.MAX));
|
|
|
409 |
ptd.setTargetName(String.format("%s Sales Target for %s Partners", monthName, memberType));
|
|
|
410 |
partnerTargetRepository.persist(ptd);
|
|
|
411 |
TargetSlabRepositoryImpl.TYPE_PARTNER_MAPPING.get(memberType).forEach(x -> {
|
|
|
412 |
x.setTargetId(ptd.getId());
|
|
|
413 |
partnerTargetRepository.persist(x);
|
|
|
414 |
});
|
|
|
415 |
|
|
|
416 |
List<TargetSlab> slabs = defaultSlabs.get(memberType);
|
|
|
417 |
slabs.stream().forEach(x -> {
|
|
|
418 |
x.setTargetId(ptd.getId());
|
|
|
419 |
targetSlabRepository.persist(x);
|
|
|
420 |
});
|
|
|
421 |
}
|
|
|
422 |
}
|
|
|
423 |
|
| 24957 |
amit.gupta |
424 |
public void findMismatchesInIndent() throws Exception {
|
| 24953 |
amit.gupta |
425 |
List<SaholicInventorySnapshot> saholicInventorySnapshots = saholicInventorySnapshotRepository.selectAll();
|
|
|
426 |
Map<Integer, Integer> itemReservedMap = saholicInventorySnapshots.stream().filter(x -> x.getReserved() > 0)
|
|
|
427 |
.collect(Collectors.groupingBy(SaholicInventorySnapshot::getItemId,
|
|
|
428 |
Collectors.summingInt(SaholicInventorySnapshot::getReserved)));
|
| 24957 |
amit.gupta |
429 |
|
|
|
430 |
List<Order> allInProcessOrders = orderRepository.selectAllOrder(OrderStatus.SUBMITTED_FOR_PROCESSING);
|
|
|
431 |
|
|
|
432 |
|
|
|
433 |
Map<Integer, Integer> lineItemQtyMap = allInProcessOrders
|
| 24953 |
amit.gupta |
434 |
.stream().collect(Collectors.groupingBy(y -> y.getLineItem().getItemId(),
|
|
|
435 |
Collectors.summingInt(y -> y.getLineItem().getQuantity())));
|
|
|
436 |
|
| 24957 |
amit.gupta |
437 |
/* Map<Integer, Order> orderMap = allInProcessOrders.stream().collect(Collectors.toMap(Order::getId, x->x));
|
|
|
438 |
Map<Integer, List<Order>> itemOrdersMap = allInProcessOrders.stream()
|
|
|
439 |
.collect(Collectors.groupingBy(o->o.getLineItem().getItemId(), Collectors.toList()));
|
|
|
440 |
*/
|
| 24953 |
amit.gupta |
441 |
itemReservedMap.forEach((itemId, reserved)->{
|
|
|
442 |
if(lineItemQtyMap.containsKey(itemId)) {
|
|
|
443 |
int orderedQty = lineItemQtyMap.get(itemId);
|
| 24956 |
amit.gupta |
444 |
System.out.printf("%d\t%d\t%d\n",itemId, orderedQty, reserved);
|
| 24953 |
amit.gupta |
445 |
} else {
|
| 24957 |
amit.gupta |
446 |
saholicInventorySnapshotRepository.removeAllReservations(itemId);
|
| 24956 |
amit.gupta |
447 |
System.out.printf("%d\t%d\t%d\n",itemId, 0, reserved);
|
| 24953 |
amit.gupta |
448 |
}
|
|
|
449 |
});
|
| 24962 |
amit.gupta |
450 |
|
|
|
451 |
List<SaholicReservationInventorySnapshot> reservationInventorySnapshots = saholicReservationInventorySnapshotRepository.selectAll();
|
|
|
452 |
Map<Integer, SaholicReservationInventorySnapshot> reservationInventorySnapshotsMap =reservationInventorySnapshots.stream()
|
|
|
453 |
.collect(Collectors.toMap(x->x.getOrderId(), x->x));
|
|
|
454 |
|
|
|
455 |
List<Integer> orderIds = reservationInventorySnapshots.stream().map(x->x.getOrderId()).collect(Collectors.toList());
|
|
|
456 |
List<Order> orders = orderRepository.selectByOrderIds(orderIds);
|
|
|
457 |
orders.stream().filter(x->Arrays.asList(OrderStatus.PAYMENT_PENDING, OrderStatus.PAYMENT_FAILED).
|
|
|
458 |
contains(x.getStatus())).forEach(x->{
|
|
|
459 |
SaholicReservationInventorySnapshot snapshot = reservationInventorySnapshotsMap.get(x.getId());
|
|
|
460 |
try {
|
|
|
461 |
saholicInventorySnapshotRepository.reduceReservationCount(snapshot.getItemId(), snapshot.getWarehouseId(), snapshot.getReserved(), snapshot.getOrderId());
|
|
|
462 |
System.out.printf("%d Reduced by %d for Order Id %d\n",snapshot.getItemId(), snapshot.getReserved(), snapshot.getOrderId());
|
|
|
463 |
} catch(ProfitMandiBusinessException e) {
|
|
|
464 |
LOGGER.info("Problem whiile reducing count for Order Id - {}", snapshot.getOrderId());
|
|
|
465 |
}
|
|
|
466 |
});
|
|
|
467 |
throw new Exception();
|
| 24953 |
amit.gupta |
468 |
}
|
|
|
469 |
|
| 23755 |
amit.gupta |
470 |
}
|