Subversion Repositories SmartDukaan

Rev

Rev 37654 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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