| 28300 |
tejbeer |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
| 37823 |
amit |
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 28499 |
amit.gupta |
4 |
import com.spice.profitmandi.dao.entity.inventory.State;
|
|
|
5 |
import com.spice.profitmandi.dao.entity.inventory.Warehouse;
|
|
|
6 |
import com.spice.profitmandi.dao.entity.transaction.Seller;
|
|
|
7 |
import com.spice.profitmandi.dao.entity.transaction.WarehouseAddressMaster;
|
| 37846 |
amit |
8 |
import com.spice.profitmandi.dao.enumuration.warehouse.WarehouseStatus;
|
| 37823 |
amit |
9 |
import com.spice.profitmandi.dao.model.warehouse.NewPhysicalWarehouseRequest;
|
|
|
10 |
import com.spice.profitmandi.dao.model.warehouse.NewSellerRequest;
|
|
|
11 |
import com.spice.profitmandi.dao.model.warehouse.NewWarehouseAddressRequest;
|
| 37846 |
amit |
12 |
import com.spice.profitmandi.dao.model.warehouse.PhysicalWarehouseView;
|
| 28499 |
amit.gupta |
13 |
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
|
| 28300 |
tejbeer |
14 |
import com.spice.profitmandi.dao.repository.transaction.SellerRepository;
|
|
|
15 |
import com.spice.profitmandi.dao.repository.transaction.SellerWarehouseRepository;
|
| 37653 |
amit |
16 |
import com.spice.profitmandi.service.SellerService;
|
| 37846 |
amit |
17 |
import com.spice.profitmandi.service.warehouse.SuspendedWarehouseStockAlertService;
|
| 37823 |
amit |
18 |
import com.spice.profitmandi.service.warehouse.WarehouseSetupService;
|
| 32383 |
amit.gupta |
19 |
import org.apache.logging.log4j.LogManager;
|
|
|
20 |
import org.apache.logging.log4j.Logger;
|
|
|
21 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 37653 |
amit |
22 |
import org.springframework.http.ResponseEntity;
|
| 32383 |
amit.gupta |
23 |
import org.springframework.stereotype.Controller;
|
|
|
24 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
25 |
import org.springframework.ui.Model;
|
| 37823 |
amit |
26 |
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
27 |
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
28 |
import org.springframework.web.bind.annotation.RequestBody;
|
| 32383 |
amit.gupta |
29 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
30 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
31 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 37653 |
amit |
32 |
import org.springframework.web.bind.annotation.ResponseBody;
|
| 28300 |
tejbeer |
33 |
|
| 37846 |
amit |
34 |
import java.util.EnumMap;
|
| 32383 |
amit.gupta |
35 |
import java.util.HashMap;
|
| 37846 |
amit |
36 |
import java.util.List;
|
| 32383 |
amit.gupta |
37 |
import java.util.Map;
|
|
|
38 |
import java.util.stream.Collectors;
|
| 28499 |
amit.gupta |
39 |
|
| 37823 |
amit |
40 |
/**
|
| 37846 |
amit |
41 |
* Seller / physical warehouse setup screen, including each warehouse's status (active / suspended / inactive) and label. Rules live in WarehouseSetupService; this only maps requests to it.
|
| 37823 |
amit |
42 |
*/
|
| 28300 |
tejbeer |
43 |
@Controller
|
|
|
44 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
45 |
public class SellerController {
|
|
|
46 |
|
| 32075 |
tejbeer |
47 |
@Autowired
|
|
|
48 |
private SellerRepository sellerRepository;
|
| 28499 |
amit.gupta |
49 |
|
| 32075 |
tejbeer |
50 |
@Autowired
|
|
|
51 |
private StateRepository stateRepository;
|
| 28493 |
tejbeer |
52 |
|
| 32075 |
tejbeer |
53 |
@Autowired
|
|
|
54 |
private SellerWarehouseRepository sellerWarehouseRepository;
|
| 28300 |
tejbeer |
55 |
|
| 32075 |
tejbeer |
56 |
@Autowired
|
| 37823 |
amit |
57 |
private SellerService sellerService;
|
| 28300 |
tejbeer |
58 |
|
| 37653 |
amit |
59 |
@Autowired
|
| 37823 |
amit |
60 |
private WarehouseSetupService warehouseSetupService;
|
| 37653 |
amit |
61 |
|
| 37846 |
amit |
62 |
@Autowired
|
|
|
63 |
private SuspendedWarehouseStockAlertService suspendedWarehouseStockAlertService;
|
|
|
64 |
|
| 32075 |
tejbeer |
65 |
private static final Logger LOGGER = LogManager.getLogger(SellerController.class);
|
| 28300 |
tejbeer |
66 |
|
| 37653 |
amit |
67 |
/**
|
|
|
68 |
* Realign the INTERNAL stores of a warehouse with the address currently mapped to it.
|
|
|
69 |
*
|
| 37823 |
amit |
70 |
* The mapping had no edit screen - it got repointed straight in the database - so nothing
|
| 37653 |
amit |
71 |
* carried a warehouse's move through to the internal store's user.address, and orders kept
|
| 37823 |
amit |
72 |
* billing to an address the branch had left years earlier. Remapping from the setup screen now
|
|
|
73 |
* syncs on its own; this stays for mappings changed outside it.
|
| 37653 |
amit |
74 |
*/
|
|
|
75 |
@RequestMapping(value = "/syncWarehouseInternalAddress", method = RequestMethod.POST)
|
|
|
76 |
@ResponseBody
|
|
|
77 |
public ResponseEntity<?> syncWarehouseInternalAddress(@RequestParam(name = "warehouseId") int warehouseId)
|
|
|
78 |
throws Exception {
|
|
|
79 |
int synced = sellerService.syncInternalStoreAddresses(warehouseId);
|
|
|
80 |
LOGGER.info("Synced {} internal store address(es) for warehouse {}", synced, warehouseId);
|
|
|
81 |
Map<String, Object> response = new HashMap<>();
|
|
|
82 |
response.put("warehouseId", warehouseId);
|
|
|
83 |
response.put("storesSynced", synced);
|
|
|
84 |
return ResponseEntity.ok(response);
|
|
|
85 |
}
|
|
|
86 |
|
| 37823 |
amit |
87 |
@RequestMapping(value = "/getAllSeller", method = RequestMethod.GET)
|
|
|
88 |
public String getAllSeller(Model model) throws Exception {
|
|
|
89 |
Map<Integer, Long> warehouseCounts = sellerWarehouseRepository.selectAll().stream()
|
|
|
90 |
.collect(Collectors.groupingBy(x -> x.getSellerId(), Collectors.counting()));
|
| 37846 |
amit |
91 |
List<PhysicalWarehouseView> warehouses = warehouseSetupService.getAllPhysicalWarehouses();
|
|
|
92 |
Map<WarehouseStatus, Long> statusCounts = new EnumMap<>(WarehouseStatus.class);
|
|
|
93 |
for (WarehouseStatus status : WarehouseStatus.values()) {
|
|
|
94 |
statusCounts.put(status, warehouses.stream().filter(x -> x.getStatus() == status).count());
|
|
|
95 |
}
|
|
|
96 |
model.addAttribute("warehouses", warehouses);
|
|
|
97 |
model.addAttribute("statusCounts", statusCounts);
|
|
|
98 |
model.addAttribute("statuses", WarehouseStatus.values());
|
| 37823 |
amit |
99 |
model.addAttribute("sellers", warehouseSetupService.getSellers());
|
|
|
100 |
model.addAttribute("warehouseCounts", warehouseCounts);
|
|
|
101 |
model.addAttribute("stateNames", this.stateNames());
|
|
|
102 |
model.addAttribute("states", stateRepository.selectAll());
|
|
|
103 |
model.addAttribute("addresses", warehouseSetupService.getAddresses());
|
|
|
104 |
return "seller";
|
|
|
105 |
}
|
| 28493 |
tejbeer |
106 |
|
| 37823 |
amit |
107 |
@RequestMapping(value = "/getSellerWareHouse", method = RequestMethod.GET)
|
|
|
108 |
public String getSellerWareHouse(@RequestParam(name = "sellerId") int sellerId, Model model) throws Exception {
|
| 32075 |
tejbeer |
109 |
Seller seller = sellerRepository.selectById(sellerId);
|
| 37823 |
amit |
110 |
model.addAttribute("seller", seller);
|
|
|
111 |
model.addAttribute("stateNames", this.stateNames());
|
|
|
112 |
model.addAttribute("warehouses", warehouseSetupService.getPhysicalWarehouses(sellerId));
|
| 37846 |
amit |
113 |
model.addAttribute("statuses", WarehouseStatus.values());
|
| 37823 |
amit |
114 |
model.addAttribute("assignableAddresses", warehouseSetupService.getAssignableAddresses(seller.getStateId()));
|
|
|
115 |
return "seller-warehouse-container";
|
|
|
116 |
}
|
| 28300 |
tejbeer |
117 |
|
| 37823 |
amit |
118 |
@RequestMapping(value = "/createNewAddress", method = RequestMethod.POST)
|
|
|
119 |
@ResponseBody
|
|
|
120 |
public ResponseEntity<?> createNewAddress(@RequestBody NewWarehouseAddressRequest request) throws Exception {
|
|
|
121 |
WarehouseAddressMaster address = warehouseSetupService.createAddress(request);
|
|
|
122 |
return ResponseEntity.ok(this.idResponse("addressId", address.getId()));
|
|
|
123 |
}
|
| 28300 |
tejbeer |
124 |
|
| 37823 |
amit |
125 |
@RequestMapping(value = "/warehouseAddress/{addressId}/active", method = RequestMethod.POST)
|
|
|
126 |
@ResponseBody
|
|
|
127 |
public ResponseEntity<?> setAddressActive(@PathVariable int addressId, @RequestParam boolean active)
|
|
|
128 |
throws Exception {
|
|
|
129 |
warehouseSetupService.setAddressActive(addressId, active);
|
|
|
130 |
return ResponseEntity.ok(this.idResponse("addressId", addressId));
|
| 32075 |
tejbeer |
131 |
}
|
| 28300 |
tejbeer |
132 |
|
| 32075 |
tejbeer |
133 |
@RequestMapping(value = "/createNewSeller", method = RequestMethod.POST)
|
| 37823 |
amit |
134 |
@ResponseBody
|
|
|
135 |
public ResponseEntity<?> createNewSeller(@RequestBody NewSellerRequest request) throws Exception {
|
|
|
136 |
Seller seller = warehouseSetupService.createSeller(request);
|
|
|
137 |
return ResponseEntity.ok(this.idResponse("sellerId", seller.getId()));
|
|
|
138 |
}
|
| 28300 |
tejbeer |
139 |
|
| 37823 |
amit |
140 |
@RequestMapping(value = "/createNewWarehouse", method = RequestMethod.POST)
|
|
|
141 |
@ResponseBody
|
|
|
142 |
public ResponseEntity<?> createNewWarehouse(@RequestBody NewPhysicalWarehouseRequest request) throws Exception {
|
|
|
143 |
Warehouse warehouse = warehouseSetupService.createPhysicalWarehouse(request);
|
|
|
144 |
return ResponseEntity.ok(this.idResponse("warehouseId", warehouse.getId()));
|
|
|
145 |
}
|
| 28300 |
tejbeer |
146 |
|
| 37846 |
amit |
147 |
@RequestMapping(value = "/physicalWarehouse/{warehouseId}/status", method = RequestMethod.POST)
|
| 37823 |
amit |
148 |
@ResponseBody
|
| 37846 |
amit |
149 |
public ResponseEntity<?> setWarehouseStatus(@PathVariable int warehouseId, @RequestParam WarehouseStatus status)
|
| 37823 |
amit |
150 |
throws Exception {
|
| 37846 |
amit |
151 |
warehouseSetupService.setWarehouseStatus(warehouseId, status);
|
| 37823 |
amit |
152 |
return ResponseEntity.ok(this.idResponse("warehouseId", warehouseId));
|
| 32075 |
tejbeer |
153 |
}
|
| 28300 |
tejbeer |
154 |
|
| 37846 |
amit |
155 |
@RequestMapping(value = "/physicalWarehouse/{warehouseId}/displayName", method = RequestMethod.POST)
|
|
|
156 |
@ResponseBody
|
|
|
157 |
public ResponseEntity<?> setWarehouseDisplayName(@PathVariable int warehouseId, @RequestParam String displayName)
|
|
|
158 |
throws Exception {
|
|
|
159 |
warehouseSetupService.setWarehouseDisplayName(warehouseId, displayName);
|
|
|
160 |
return ResponseEntity.ok(this.idResponse("warehouseId", warehouseId));
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
// Same mail the daily cron sends; from the screen it goes out at once.
|
|
|
164 |
@RequestMapping(value = "/warehouseStockAlert/send", method = RequestMethod.POST)
|
|
|
165 |
@ResponseBody
|
|
|
166 |
public ResponseEntity<?> sendWarehouseStockAlert() throws Exception {
|
|
|
167 |
Map<String, Object> response = new HashMap<>();
|
|
|
168 |
response.put("summary", suspendedWarehouseStockAlertService.sendAlert());
|
|
|
169 |
return ResponseEntity.ok(response);
|
|
|
170 |
}
|
|
|
171 |
|
| 37823 |
amit |
172 |
@RequestMapping(value = "/physicalWarehouse/{warehouseId}/address", method = RequestMethod.POST)
|
|
|
173 |
@ResponseBody
|
|
|
174 |
public ResponseEntity<?> remapAddress(@PathVariable int warehouseId, @RequestParam int addressId)
|
|
|
175 |
throws Exception {
|
|
|
176 |
warehouseSetupService.remapAddress(warehouseId, addressId);
|
|
|
177 |
return ResponseEntity.ok(this.idResponse("warehouseId", warehouseId));
|
| 32075 |
tejbeer |
178 |
}
|
| 28300 |
tejbeer |
179 |
|
| 37823 |
amit |
180 |
// Raised after the transaction has rolled back; the screen shows the rule that was broken.
|
|
|
181 |
@ExceptionHandler(ProfitMandiBusinessException.class)
|
|
|
182 |
@ResponseBody
|
|
|
183 |
public ResponseEntity<String> handleBusinessException(ProfitMandiBusinessException e) {
|
|
|
184 |
LOGGER.info("Seller setup refused: {} = {} - {}", e.getRejectedType(), e.getRejectedValue(), e.getCode());
|
|
|
185 |
return ResponseEntity.badRequest().body(e.getCode());
|
| 32075 |
tejbeer |
186 |
}
|
| 28300 |
tejbeer |
187 |
|
| 37823 |
amit |
188 |
private Map<Integer, String> stateNames() {
|
|
|
189 |
return stateRepository.selectAll().stream()
|
|
|
190 |
.collect(Collectors.toMap(State::getId, State::getName, (a, b) -> a));
|
| 32075 |
tejbeer |
191 |
}
|
|
|
192 |
|
| 37823 |
amit |
193 |
private Map<String, Object> idResponse(String key, int id) {
|
|
|
194 |
Map<String, Object> response = new HashMap<>();
|
|
|
195 |
response.put(key, id);
|
|
|
196 |
return response;
|
| 32075 |
tejbeer |
197 |
}
|
| 37823 |
amit |
198 |
}
|