| 32817 |
amit.gupta |
1 |
package com.smartdukaan.cron.scheduled;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
4 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
|
|
5 |
import com.spice.profitmandi.dao.entity.warehouse.WarehouseInventoryItem;
|
|
|
6 |
import com.spice.profitmandi.dao.entity.warehouse.WarehouseScan;
|
|
|
7 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
8 |
import com.spice.profitmandi.dao.repository.warehouse.WarehouseScanRepository;
|
|
|
9 |
import com.spice.profitmandi.service.transaction.ReturnService;
|
|
|
10 |
import com.spice.profitmandi.service.wallet.WalletService;
|
|
|
11 |
import com.spice.profitmandi.service.warehouse.WarehouseInventoryService;
|
|
|
12 |
import in.shop2020.model.v1.order.OrderStatus;
|
|
|
13 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
14 |
import in.shop2020.warehouse.Scan;
|
|
|
15 |
import in.shop2020.warehouse.ScanType;
|
|
|
16 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
17 |
import org.springframework.stereotype.Component;
|
|
|
18 |
|
|
|
19 |
import java.time.LocalDateTime;
|
|
|
20 |
import java.util.HashMap;
|
|
|
21 |
import java.util.List;
|
|
|
22 |
import java.util.Map;
|
|
|
23 |
import java.util.stream.Collectors;
|
|
|
24 |
|
|
|
25 |
@Component
|
|
|
26 |
public class TransactionRelatedTasks {
|
|
|
27 |
@Autowired
|
|
|
28 |
private ReturnService returnService;
|
|
|
29 |
@Autowired
|
|
|
30 |
private OrderRepository orderRepository;
|
|
|
31 |
|
|
|
32 |
@Autowired
|
|
|
33 |
WarehouseScanRepository warehouseScanRepository;
|
|
|
34 |
|
|
|
35 |
@Autowired
|
|
|
36 |
WarehouseInventoryService warehouseInventoryService;
|
|
|
37 |
|
|
|
38 |
@Autowired
|
|
|
39 |
WalletService walletService;
|
|
|
40 |
|
|
|
41 |
public void reverseRTO(List<String> invoiceNumbers) throws ProfitMandiBusinessException {
|
| 35424 |
amit |
42 |
// Fetch with pessimistic lock to prevent concurrent modifications
|
|
|
43 |
List<Order> lockedOrders = orderRepository.selectByInvoiceNumbersForUpdate(invoiceNumbers);
|
|
|
44 |
Map<String, List<Order>> invoiceOrderMap = lockedOrders.stream().collect(Collectors.groupingBy(x -> x.getInvoiceNumber()));
|
|
|
45 |
List<Integer> rtoOrderIds = lockedOrders.stream().map(x -> x.getId()).collect(Collectors.toList());
|
|
|
46 |
|
| 32817 |
amit.gupta |
47 |
Map<Integer, List<WarehouseScan>> orderIdWarehouseScanMap = warehouseScanRepository.selectAllByOrderIds(rtoOrderIds).stream().filter(x -> x.getType().equals(ScanType.SALE_RET)).collect(Collectors.groupingBy(x -> x.getOrderId()));
|
|
|
48 |
|
|
|
49 |
for (Map.Entry<String, List<Order>> invoiceOrdersEntry : invoiceOrderMap.entrySet()) {
|
|
|
50 |
String invoiceNumber = invoiceOrdersEntry.getKey();
|
|
|
51 |
List<Order> orders = invoiceOrdersEntry.getValue();
|
|
|
52 |
int transactionId = orders.get(0).getTransactionId();
|
|
|
53 |
int fofoId = orders.get(0).getRetailerId();
|
|
|
54 |
Float walletAmount = 0f;
|
|
|
55 |
for (Order order : orders) {
|
|
|
56 |
order.setRefundBy(null);
|
|
|
57 |
order.setRefundTimestamp(null);
|
|
|
58 |
order.setRefundReason(null);
|
|
|
59 |
order.setStatusDescription("");
|
|
|
60 |
order.setReceiverReturnTimestamp(null);
|
|
|
61 |
order.getWalletAmount();
|
|
|
62 |
if (order.getShippingTimestamp() != null) {
|
|
|
63 |
order.setStatus(OrderStatus.SHIPPED_FROM_WH);
|
|
|
64 |
} else {
|
|
|
65 |
order.setStatus(OrderStatus.BILLED);
|
|
|
66 |
}
|
|
|
67 |
walletAmount += order.getWalletAmount();
|
|
|
68 |
List<WarehouseScan> warehouseScans = orderIdWarehouseScanMap.get(order.getId());
|
|
|
69 |
for (WarehouseScan warehouseScan : warehouseScans) {
|
|
|
70 |
warehouseInventoryService.addQuantity(warehouseScan.getInventoryItemId(), warehouseScan.getQuantity());
|
|
|
71 |
warehouseScanRepository.delete(warehouseScan);
|
|
|
72 |
}
|
|
|
73 |
//Find inventory items and remove return entries
|
|
|
74 |
}
|
|
|
75 |
walletService.rollbackAmountFromWallet(fofoId, walletAmount, transactionId, WalletReferenceType.PURCHASE, "RTO reversed for invoice - " + invoiceNumber, LocalDateTime.now());
|
|
|
76 |
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
//RTOs should also come in curtomer return report
|