Rev 37823 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.dao.entity.inventory.State;import com.spice.profitmandi.dao.entity.inventory.Warehouse;import com.spice.profitmandi.dao.entity.transaction.Seller;import com.spice.profitmandi.dao.entity.transaction.WarehouseAddressMaster;import com.spice.profitmandi.dao.enumuration.warehouse.WarehouseStatus;import com.spice.profitmandi.dao.model.warehouse.NewPhysicalWarehouseRequest;import com.spice.profitmandi.dao.model.warehouse.NewSellerRequest;import com.spice.profitmandi.dao.model.warehouse.NewWarehouseAddressRequest;import com.spice.profitmandi.dao.model.warehouse.PhysicalWarehouseView;import com.spice.profitmandi.dao.repository.inventory.StateRepository;import com.spice.profitmandi.dao.repository.transaction.SellerRepository;import com.spice.profitmandi.dao.repository.transaction.SellerWarehouseRepository;import com.spice.profitmandi.service.SellerService;import com.spice.profitmandi.service.warehouse.SuspendedWarehouseStockAlertService;import com.spice.profitmandi.service.warehouse.WarehouseSetupService;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.transaction.annotation.Transactional;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import java.util.EnumMap;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.stream.Collectors;/*** 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.*/@Controller@Transactional(rollbackFor = Throwable.class)public class SellerController {@Autowiredprivate SellerRepository sellerRepository;@Autowiredprivate StateRepository stateRepository;@Autowiredprivate SellerWarehouseRepository sellerWarehouseRepository;@Autowiredprivate SellerService sellerService;@Autowiredprivate WarehouseSetupService warehouseSetupService;@Autowiredprivate SuspendedWarehouseStockAlertService suspendedWarehouseStockAlertService;private static final Logger LOGGER = LogManager.getLogger(SellerController.class);/*** Realign the INTERNAL stores of a warehouse with the address currently mapped to it.** The mapping had no edit screen - it got repointed straight in the database - so nothing* carried a warehouse's move through to the internal store's user.address, and orders kept* billing to an address the branch had left years earlier. Remapping from the setup screen now* syncs on its own; this stays for mappings changed outside it.*/@RequestMapping(value = "/syncWarehouseInternalAddress", method = RequestMethod.POST)@ResponseBodypublic ResponseEntity<?> syncWarehouseInternalAddress(@RequestParam(name = "warehouseId") int warehouseId)throws Exception {int synced = sellerService.syncInternalStoreAddresses(warehouseId);LOGGER.info("Synced {} internal store address(es) for warehouse {}", synced, warehouseId);Map<String, Object> response = new HashMap<>();response.put("warehouseId", warehouseId);response.put("storesSynced", synced);return ResponseEntity.ok(response);}@RequestMapping(value = "/getAllSeller", method = RequestMethod.GET)public String getAllSeller(Model model) throws Exception {Map<Integer, Long> warehouseCounts = sellerWarehouseRepository.selectAll().stream().collect(Collectors.groupingBy(x -> x.getSellerId(), Collectors.counting()));List<PhysicalWarehouseView> warehouses = warehouseSetupService.getAllPhysicalWarehouses();Map<WarehouseStatus, Long> statusCounts = new EnumMap<>(WarehouseStatus.class);for (WarehouseStatus status : WarehouseStatus.values()) {statusCounts.put(status, warehouses.stream().filter(x -> x.getStatus() == status).count());}model.addAttribute("warehouses", warehouses);model.addAttribute("statusCounts", statusCounts);model.addAttribute("statuses", WarehouseStatus.values());model.addAttribute("sellers", warehouseSetupService.getSellers());model.addAttribute("warehouseCounts", warehouseCounts);model.addAttribute("stateNames", this.stateNames());model.addAttribute("states", stateRepository.selectAll());model.addAttribute("addresses", warehouseSetupService.getAddresses());return "seller";}@RequestMapping(value = "/getSellerWareHouse", method = RequestMethod.GET)public String getSellerWareHouse(@RequestParam(name = "sellerId") int sellerId, Model model) throws Exception {Seller seller = sellerRepository.selectById(sellerId);model.addAttribute("seller", seller);model.addAttribute("stateNames", this.stateNames());model.addAttribute("warehouses", warehouseSetupService.getPhysicalWarehouses(sellerId));model.addAttribute("statuses", WarehouseStatus.values());model.addAttribute("assignableAddresses", warehouseSetupService.getAssignableAddresses(seller.getStateId()));return "seller-warehouse-container";}@RequestMapping(value = "/createNewAddress", method = RequestMethod.POST)@ResponseBodypublic ResponseEntity<?> createNewAddress(@RequestBody NewWarehouseAddressRequest request) throws Exception {WarehouseAddressMaster address = warehouseSetupService.createAddress(request);return ResponseEntity.ok(this.idResponse("addressId", address.getId()));}@RequestMapping(value = "/warehouseAddress/{addressId}/active", method = RequestMethod.POST)@ResponseBodypublic ResponseEntity<?> setAddressActive(@PathVariable int addressId, @RequestParam boolean active)throws Exception {warehouseSetupService.setAddressActive(addressId, active);return ResponseEntity.ok(this.idResponse("addressId", addressId));}@RequestMapping(value = "/createNewSeller", method = RequestMethod.POST)@ResponseBodypublic ResponseEntity<?> createNewSeller(@RequestBody NewSellerRequest request) throws Exception {Seller seller = warehouseSetupService.createSeller(request);return ResponseEntity.ok(this.idResponse("sellerId", seller.getId()));}@RequestMapping(value = "/createNewWarehouse", method = RequestMethod.POST)@ResponseBodypublic ResponseEntity<?> createNewWarehouse(@RequestBody NewPhysicalWarehouseRequest request) throws Exception {Warehouse warehouse = warehouseSetupService.createPhysicalWarehouse(request);return ResponseEntity.ok(this.idResponse("warehouseId", warehouse.getId()));}@RequestMapping(value = "/physicalWarehouse/{warehouseId}/status", method = RequestMethod.POST)@ResponseBodypublic ResponseEntity<?> setWarehouseStatus(@PathVariable int warehouseId, @RequestParam WarehouseStatus status)throws Exception {warehouseSetupService.setWarehouseStatus(warehouseId, status);return ResponseEntity.ok(this.idResponse("warehouseId", warehouseId));}@RequestMapping(value = "/physicalWarehouse/{warehouseId}/displayName", method = RequestMethod.POST)@ResponseBodypublic ResponseEntity<?> setWarehouseDisplayName(@PathVariable int warehouseId, @RequestParam String displayName)throws Exception {warehouseSetupService.setWarehouseDisplayName(warehouseId, displayName);return ResponseEntity.ok(this.idResponse("warehouseId", warehouseId));}// Same mail the daily cron sends; from the screen it goes out at once.@RequestMapping(value = "/warehouseStockAlert/send", method = RequestMethod.POST)@ResponseBodypublic ResponseEntity<?> sendWarehouseStockAlert() throws Exception {Map<String, Object> response = new HashMap<>();response.put("summary", suspendedWarehouseStockAlertService.sendAlert());return ResponseEntity.ok(response);}@RequestMapping(value = "/physicalWarehouse/{warehouseId}/address", method = RequestMethod.POST)@ResponseBodypublic ResponseEntity<?> remapAddress(@PathVariable int warehouseId, @RequestParam int addressId)throws Exception {warehouseSetupService.remapAddress(warehouseId, addressId);return ResponseEntity.ok(this.idResponse("warehouseId", warehouseId));}// Raised after the transaction has rolled back; the screen shows the rule that was broken.@ExceptionHandler(ProfitMandiBusinessException.class)@ResponseBodypublic ResponseEntity<String> handleBusinessException(ProfitMandiBusinessException e) {LOGGER.info("Seller setup refused: {} = {} - {}", e.getRejectedType(), e.getRejectedValue(), e.getCode());return ResponseEntity.badRequest().body(e.getCode());}private Map<Integer, String> stateNames() {return stateRepository.selectAll().stream().collect(Collectors.toMap(State::getId, State::getName, (a, b) -> a));}private Map<String, Object> idResponse(String key, int id) {Map<String, Object> response = new HashMap<>();response.put(key, id);return response;}}