| Line 1... |
Line 1... |
| 1 |
package com.smartdukaan.cron.migrations;
|
1 |
package com.smartdukaan.cron.migrations;
|
| 2 |
|
2 |
|
| 3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 4 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
4 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
| 5 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
5 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| - |
|
6 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 6 |
import com.spice.profitmandi.dao.entity.warehouse.Supplier;
|
7 |
import com.spice.profitmandi.dao.entity.warehouse.Supplier;
|
| 7 |
import com.spice.profitmandi.dao.entity.warehouse.WarehousePurchaseOrder;
|
8 |
import com.spice.profitmandi.dao.entity.warehouse.WarehousePurchaseOrder;
|
| 8 |
import com.spice.profitmandi.dao.model.CreateWarehousePurchaseOrderModel;
|
9 |
import com.spice.profitmandi.dao.model.CreateWarehousePurchaseOrderModel;
|
| 9 |
import com.spice.profitmandi.dao.model.InternalMovementAllocationModel;
|
10 |
import com.spice.profitmandi.dao.model.InternalMovementAllocationModel;
|
| 10 |
import com.spice.profitmandi.dao.model.InternalMovementAvailabilityModel;
|
11 |
import com.spice.profitmandi.dao.model.InternalMovementAvailabilityModel;
|
| 11 |
import com.spice.profitmandi.dao.model.ItemWisePurchaseModel;
|
12 |
import com.spice.profitmandi.dao.model.ItemWisePurchaseModel;
|
| 12 |
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
|
13 |
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
|
| 13 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
14 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| - |
|
15 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
| 14 |
import com.spice.profitmandi.dao.repository.warehouse.SupplierRepository;
|
16 |
import com.spice.profitmandi.dao.repository.warehouse.SupplierRepository;
|
| 15 |
import com.spice.profitmandi.dao.repository.warehouse.WarehousePurchaseOrderRepository;
|
17 |
import com.spice.profitmandi.dao.repository.warehouse.WarehousePurchaseOrderRepository;
|
| 16 |
import com.spice.profitmandi.service.warehouse.InternalMovementPricingService;
|
18 |
import com.spice.profitmandi.service.warehouse.InternalMovementPricingService;
|
| 17 |
import com.spice.profitmandi.service.warehouse.PurchaseOrderService;
|
19 |
import com.spice.profitmandi.service.warehouse.PurchaseOrderService;
|
| - |
|
20 |
import in.shop2020.model.v1.order.OrderStatus;
|
| - |
|
21 |
import in.shop2020.purchase.POStatus;
|
| 18 |
import org.apache.poi.ss.usermodel.CellType;
|
22 |
import org.apache.poi.ss.usermodel.CellType;
|
| 19 |
import org.apache.poi.ss.usermodel.Row;
|
23 |
import org.apache.poi.ss.usermodel.Row;
|
| 20 |
import org.apache.poi.xssf.usermodel.XSSFRow;
|
24 |
import org.apache.poi.xssf.usermodel.XSSFRow;
|
| 21 |
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
25 |
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
| 22 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
26 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
| Line 25... |
Line 29... |
| 25 |
import org.springframework.transaction.annotation.Transactional;
|
29 |
import org.springframework.transaction.annotation.Transactional;
|
| 26 |
|
30 |
|
| 27 |
import java.io.FileInputStream;
|
31 |
import java.io.FileInputStream;
|
| 28 |
import java.io.InputStream;
|
32 |
import java.io.InputStream;
|
| 29 |
import java.time.LocalDate;
|
33 |
import java.time.LocalDate;
|
| - |
|
34 |
import java.time.LocalDateTime;
|
| 30 |
import java.util.ArrayList;
|
35 |
import java.util.ArrayList;
|
| - |
|
36 |
import java.util.Collections;
|
| 31 |
import java.util.Comparator;
|
37 |
import java.util.Comparator;
|
| 32 |
import java.util.LinkedHashMap;
|
38 |
import java.util.LinkedHashMap;
|
| 33 |
import java.util.List;
|
39 |
import java.util.List;
|
| 34 |
import java.util.Map;
|
40 |
import java.util.Map;
|
| 35 |
import java.util.function.Function;
|
41 |
import java.util.function.Function;
|
| Line 57... |
Line 63... |
| 57 |
private ItemRepository itemRepository;
|
63 |
private ItemRepository itemRepository;
|
| 58 |
@Autowired
|
64 |
@Autowired
|
| 59 |
private WarehousePurchaseOrderRepository warehousePurchaseOrderRepository;
|
65 |
private WarehousePurchaseOrderRepository warehousePurchaseOrderRepository;
|
| 60 |
@Autowired
|
66 |
@Autowired
|
| 61 |
private AuthRepository authRepository;
|
67 |
private AuthRepository authRepository;
|
| - |
|
68 |
@Autowired
|
| - |
|
69 |
private OrderRepository orderRepository;
|
| 62 |
|
70 |
|
| 63 |
public static class Preview {
|
71 |
public static class Preview {
|
| 64 |
public String supplierName;
|
72 |
public String supplierName;
|
| 65 |
public final List<String> problems = new ArrayList<>();
|
73 |
public final List<String> problems = new ArrayList<>();
|
| 66 |
/** One entry per PO that would be raised, each its priced lines. */
|
74 |
/** One entry per PO that would be raised, each its priced lines. */
|
| Line 170... |
Line 178... |
| 170 |
+ " PO(s) but found " + raised.size() + " near PO id " + firstPoId + " - rolled back, check by hand");
|
178 |
+ " PO(s) but found " + raised.size() + " near PO id " + firstPoId + " - rolled back, check by hand");
|
| 171 |
}
|
179 |
}
|
| 172 |
return raised.stream().map(WarehousePurchaseOrder::getPoNumber).collect(Collectors.toList());
|
180 |
return raised.stream().map(WarehousePurchaseOrder::getPoNumber).collect(Collectors.toList());
|
| 173 |
}
|
181 |
}
|
| 174 |
|
182 |
|
| - |
|
183 |
/**
|
| - |
|
184 |
* Closes a movement PO the way the PO screen's Close does (PurchaseOrderController.closePuchaseOrder): refuses if
|
| - |
|
185 |
* any order has moved past submitted for processing, otherwise refunds every live order and pre-closes the PO.
|
| - |
|
186 |
* Returns what it did, or would do when {@code live} is false.
|
| - |
|
187 |
*/
|
| - |
|
188 |
@Transactional(rollbackFor = Throwable.class)
|
| - |
|
189 |
public String close(String poNumber, String closedBy, boolean live) throws Exception {
|
| - |
|
190 |
List<WarehousePurchaseOrder> found = warehousePurchaseOrderRepository.selectAllByPONumbers(Collections.singletonList(poNumber));
|
| - |
|
191 |
if (found.size() != 1) {
|
| - |
|
192 |
throw new ProfitMandiBusinessException("po", poNumber, "No single PO numbered " + poNumber);
|
| - |
|
193 |
}
|
| - |
|
194 |
WarehousePurchaseOrder po = found.get(0);
|
| - |
|
195 |
if (po.getTransactionId() == null || po.getTransactionId() <= 0) {
|
| - |
|
196 |
throw new ProfitMandiBusinessException("po", poNumber, poNumber + " raised no movement order");
|
| - |
|
197 |
}
|
| - |
|
198 |
List<Order> orders = orderRepository.selectAllByTransactionId(po.getTransactionId());
|
| - |
|
199 |
int liveOrders = 0;
|
| - |
|
200 |
int units = 0;
|
| - |
|
201 |
for (Order order : orders) {
|
| - |
|
202 |
if (order.getRefundTimestamp() != null) {
|
| - |
|
203 |
continue;
|
| - |
|
204 |
}
|
| - |
|
205 |
if (!OrderStatus.SUBMITTED_FOR_PROCESSING.equals(order.getStatus())) {
|
| - |
|
206 |
throw new ProfitMandiBusinessException("po", poNumber, "Order " + order.getId() + " is already "
|
| - |
|
207 |
+ order.getStatus() + " - its stock has left, so " + poNumber + " cannot be closed");
|
| - |
|
208 |
}
|
| - |
|
209 |
liveOrders++;
|
| - |
|
210 |
units += order.getLineItem().getQuantity();
|
| - |
|
211 |
}
|
| - |
|
212 |
String summary = poNumber + " (txn " + po.getTransactionId() + ", status " + po.getStatus() + "): " + liveOrders
|
| - |
|
213 |
+ " order(s), " + units + " unit(s) to refund, then PRECLOSED";
|
| - |
|
214 |
if (!live) {
|
| - |
|
215 |
return "WOULD CLOSE " + summary;
|
| - |
|
216 |
}
|
| - |
|
217 |
for (Order order : orders) {
|
| - |
|
218 |
if (order.getRefundTimestamp() == null) {
|
| - |
|
219 |
orderRepository.refundOrder(order.getId(), closedBy, "Movement purchase order " + poNumber + " closed",
|
| - |
|
220 |
OrderStatus.CANCELLED_DUE_TO_LOW_INVENTORY);
|
| - |
|
221 |
}
|
| - |
|
222 |
}
|
| - |
|
223 |
po.setStatus(POStatus.PRECLOSED);
|
| - |
|
224 |
po.setUpdatedAt(LocalDateTime.now());
|
| - |
|
225 |
return "CLOSED " + summary;
|
| - |
|
226 |
}
|
| - |
|
227 |
|
| 175 |
private static List<ItemWisePurchaseModel> toPurchaseItems(Map<Integer, Integer> quantities) {
|
228 |
private static List<ItemWisePurchaseModel> toPurchaseItems(Map<Integer, Integer> quantities) {
|
| 176 |
List<ItemWisePurchaseModel> items = new ArrayList<>();
|
229 |
List<ItemWisePurchaseModel> items = new ArrayList<>();
|
| 177 |
for (Map.Entry<Integer, Integer> line : quantities.entrySet()) {
|
230 |
for (Map.Entry<Integer, Integer> line : quantities.entrySet()) {
|
| 178 |
ItemWisePurchaseModel item = new ItemWisePurchaseModel();
|
231 |
ItemWisePurchaseModel item = new ItemWisePurchaseModel();
|
| 179 |
item.setItemId(line.getKey());
|
232 |
item.setItemId(line.getKey());
|