Subversion Repositories SmartDukaan

Rev

Rev 37654 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 37654 Rev 37823
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
-
 
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
3
import com.spice.profitmandi.dao.entity.inventory.State;
4
import com.spice.profitmandi.dao.entity.inventory.State;
4
import com.spice.profitmandi.dao.entity.inventory.Warehouse;
5
import com.spice.profitmandi.dao.entity.inventory.Warehouse;
5
import com.spice.profitmandi.dao.entity.transaction.Seller;
6
import com.spice.profitmandi.dao.entity.transaction.Seller;
6
import com.spice.profitmandi.dao.entity.transaction.SellerWarehouse;
-
 
7
import com.spice.profitmandi.dao.entity.transaction.WarehouseAddressMapping;
-
 
8
import com.spice.profitmandi.dao.entity.transaction.WarehouseAddressMaster;
7
import com.spice.profitmandi.dao.entity.transaction.WarehouseAddressMaster;
-
 
8
import com.spice.profitmandi.dao.model.warehouse.NewPhysicalWarehouseRequest;
9
import com.spice.profitmandi.dao.enumuration.inventory.InventoryType;
9
import com.spice.profitmandi.dao.model.warehouse.NewSellerRequest;
10
import com.spice.profitmandi.dao.enumuration.inventory.WarehouseType;
10
import com.spice.profitmandi.dao.model.warehouse.NewWarehouseAddressRequest;
11
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
11
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
12
import com.spice.profitmandi.dao.repository.inventory.WarehouseRepository;
-
 
13
import com.spice.profitmandi.dao.repository.transaction.SellerRepository;
12
import com.spice.profitmandi.dao.repository.transaction.SellerRepository;
14
import com.spice.profitmandi.dao.repository.transaction.SellerWarehouseRepository;
13
import com.spice.profitmandi.dao.repository.transaction.SellerWarehouseRepository;
15
import com.spice.profitmandi.dao.repository.transaction.WarehouseAddressMappingRepository;
-
 
16
import com.spice.profitmandi.dao.repository.transaction.WarehouseAddressMasterRepository;
-
 
17
import com.spice.profitmandi.service.SellerService;
14
import com.spice.profitmandi.service.SellerService;
-
 
15
import com.spice.profitmandi.service.warehouse.WarehouseSetupService;
18
import org.apache.logging.log4j.LogManager;
16
import org.apache.logging.log4j.LogManager;
19
import org.apache.logging.log4j.Logger;
17
import org.apache.logging.log4j.Logger;
20
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.http.ResponseEntity;
19
import org.springframework.http.ResponseEntity;
22
import org.springframework.stereotype.Controller;
20
import org.springframework.stereotype.Controller;
23
import org.springframework.transaction.annotation.Transactional;
21
import org.springframework.transaction.annotation.Transactional;
24
import org.springframework.ui.Model;
22
import org.springframework.ui.Model;
-
 
23
import org.springframework.web.bind.annotation.ExceptionHandler;
-
 
24
import org.springframework.web.bind.annotation.PathVariable;
-
 
25
import org.springframework.web.bind.annotation.RequestBody;
25
import org.springframework.web.bind.annotation.RequestMapping;
26
import org.springframework.web.bind.annotation.RequestMapping;
26
import org.springframework.web.bind.annotation.RequestMethod;
27
import org.springframework.web.bind.annotation.RequestMethod;
27
import org.springframework.web.bind.annotation.RequestParam;
28
import org.springframework.web.bind.annotation.RequestParam;
28
import org.springframework.web.bind.annotation.ResponseBody;
29
import org.springframework.web.bind.annotation.ResponseBody;
29
 
30
 
30
import javax.servlet.http.HttpServletRequest;
-
 
31
import java.time.LocalDateTime;
-
 
32
import java.util.HashMap;
31
import java.util.HashMap;
33
import java.util.List;
-
 
34
import java.util.Map;
32
import java.util.Map;
35
import java.util.stream.Collectors;
33
import java.util.stream.Collectors;
36
 
34
 
-
 
35
/**
-
 
36
 * Seller / physical warehouse setup screen. Rules live in WarehouseSetupService; this only maps requests to it.
37
 
37
 */
38
@Controller
38
@Controller
39
@Transactional(rollbackFor = Throwable.class)
39
@Transactional(rollbackFor = Throwable.class)
40
public class SellerController {
40
public class SellerController {
41
 
41
 
42
    @Autowired
42
    @Autowired
43
    private SellerRepository sellerRepository;
43
    private SellerRepository sellerRepository;
44
 
44
 
45
    @Autowired
45
    @Autowired
46
    private StateRepository stateRepository;
46
    private StateRepository stateRepository;
47
 
47
 
48
 
-
 
49
    @Autowired
-
 
50
    WarehouseRepository warehouseRepository;
-
 
51
 
-
 
52
    @Autowired
-
 
53
    WarehouseAddressMasterRepository warehouseAddressMasterRepository;
-
 
54
 
-
 
55
    @Autowired
48
    @Autowired
56
    private SellerWarehouseRepository sellerWarehouseRepository;
49
    private SellerWarehouseRepository sellerWarehouseRepository;
57
 
50
 
58
    @Autowired
51
    @Autowired
59
    private WarehouseAddressMappingRepository warehouseAddressMappingRepository;
52
    private SellerService sellerService;
60
 
53
 
61
    @Autowired
54
    @Autowired
62
    private SellerService sellerService;
55
    private WarehouseSetupService warehouseSetupService;
63
 
56
 
64
    private static final Logger LOGGER = LogManager.getLogger(SellerController.class);
57
    private static final Logger LOGGER = LogManager.getLogger(SellerController.class);
65
 
58
 
66
    /**
59
    /**
67
     * Realign the INTERNAL stores of a warehouse with the address currently mapped to it.
60
     * Realign the INTERNAL stores of a warehouse with the address currently mapped to it.
68
     *
61
     *
69
     * The mapping has no edit screen - it gets repointed straight in the database - so nothing
62
     * The mapping had no edit screen - it got repointed straight in the database - so nothing
70
     * carried a warehouse's move through to the internal store's user.address, and orders kept
63
     * carried a warehouse's move through to the internal store's user.address, and orders kept
71
     * billing to an address the branch had left years earlier. This is that missing hop; run it
64
     * billing to an address the branch had left years earlier. Remapping from the setup screen now
72
     * after changing a warehouse's address mapping.
65
     * syncs on its own; this stays for mappings changed outside it.
73
     */
66
     */
74
    @RequestMapping(value = "/syncWarehouseInternalAddress", method = RequestMethod.POST)
67
    @RequestMapping(value = "/syncWarehouseInternalAddress", method = RequestMethod.POST)
75
    @ResponseBody
68
    @ResponseBody
76
    public ResponseEntity<?> syncWarehouseInternalAddress(@RequestParam(name = "warehouseId") int warehouseId)
69
    public ResponseEntity<?> syncWarehouseInternalAddress(@RequestParam(name = "warehouseId") int warehouseId)
77
            throws Exception {
70
            throws Exception {
Line 81... Line 74...
81
        response.put("warehouseId", warehouseId);
74
        response.put("warehouseId", warehouseId);
82
        response.put("storesSynced", synced);
75
        response.put("storesSynced", synced);
83
        return ResponseEntity.ok(response);
76
        return ResponseEntity.ok(response);
84
    }
77
    }
85
 
78
 
86
    @RequestMapping(value = "/createNewWarehouse", method = RequestMethod.POST)
79
    @RequestMapping(value = "/getAllSeller", method = RequestMethod.GET)
87
    public String getViewOurSales(HttpServletRequest request, @RequestParam(name = "address", required = true, defaultValue = "0") int address, @RequestParam(name = "prefix", required = true, defaultValue = "0") String prefix, @RequestParam(name = "sellerId", required = true, defaultValue = "0") int sellerId, @RequestParam(name = "warehouseName", required = true, defaultValue = "0") String warehouseName, @RequestParam(name = "pin", required = true, defaultValue = "0") String pinCode, Model model)
80
    public String getAllSeller(Model model) throws Exception {
88
 
-
 
89
            throws Exception {
-
 
90
 
-
 
91
        Seller seller = sellerRepository.selectById(sellerId);
-
 
92
        WarehouseAddressMaster wam = warehouseAddressMasterRepository.selectById(address);
81
        Map<Integer, Long> warehouseCounts = sellerWarehouseRepository.selectAll().stream()
93
 
-
 
94
        Warehouse warehouse = new Warehouse();
-
 
95
        warehouse.setDisplayName(warehouseName);
-
 
96
        warehouse.setLocation(wam.getAddress());
-
 
97
        warehouse.setGstin(seller.getGstin());
-
 
98
        warehouse.setStateId(seller.getStateId());
-
 
99
        warehouse.setStatus(3);
-
 
100
        warehouse.setAddedOn(LocalDateTime.now());
-
 
101
        warehouse.setLastCheckedOn(LocalDateTime.now());
-
 
102
        warehouse.setTinNumber(null);
-
 
103
        warehouse.setPincode(pinCode);
-
 
104
        warehouse.setLogisticsLocation(0);
-
 
105
        warehouse.setVendor(1);
-
 
106
        warehouse.setBillingType(0);
-
 
107
        warehouse.setInventoryType(InventoryType.GOOD);
-
 
108
        warehouse.setWarehouseType(WarehouseType.OURS);
-
 
109
        warehouse.setIsAvailabilityMonitored(1);
-
 
110
        warehouse.setSource(0);
-
 
111
        warehouseRepository.persist(warehouse);
-
 
112
        warehouse.setBillingWarehouseId(warehouse.getId());
-
 
113
        warehouse.setShippingWarehouseId(warehouse.getId());
82
                .collect(Collectors.groupingBy(x -> x.getSellerId(), Collectors.counting()));
114
 
-
 
115
        LOGGER.info("warehouseId" + warehouse.getId());
-
 
116
 
-
 
117
        SellerWarehouse sellerWarehouse = new SellerWarehouse();
83
        model.addAttribute("sellers", warehouseSetupService.getSellers());
118
        sellerWarehouse.setActive(true);
-
 
119
        sellerWarehouse.setWarehouseId(warehouse.getId());
84
        model.addAttribute("warehouseCounts", warehouseCounts);
120
        sellerWarehouse.setChallanSequence(0);
-
 
121
        sellerWarehouse.setCreateTimestamp(LocalDateTime.now());
85
        model.addAttribute("stateNames", this.stateNames());
122
        sellerWarehouse.setInvoiceSequence(0);
-
 
123
        sellerWarehouse.setPrefix(prefix);
-
 
124
        sellerWarehouse.setOrderType(0);
-
 
125
        sellerWarehouse.setSellerId(sellerId);
-
 
126
        sellerWarehouseRepository.persist(sellerWarehouse);
-
 
127
 
-
 
128
        WarehouseAddressMapping warehouseAddressMapping = new WarehouseAddressMapping();
-
 
129
        warehouseAddressMapping.setAddressId(address);
-
 
130
        warehouseAddressMapping.setCreated(LocalDateTime.now());
86
        model.addAttribute("states", stateRepository.selectAll());
131
        warehouseAddressMapping.setUpdated(null);
-
 
132
        warehouseAddressMapping.setWarehouseId(warehouse.getId());
87
        model.addAttribute("addresses", warehouseSetupService.getAddresses());
133
        warehouseAddressMapping.setAddressId(address);
-
 
134
        warehouseAddressMappingRepository.persist(warehouseAddressMapping);
-
 
135
 
-
 
136
        // Setting the mapping is what gives a warehouse its address, so the internal stores that bill
-
 
137
        // from it are realigned in the same transaction. A brand new warehouse has none yet and this
-
 
138
        // is a no-op, but anchoring the rule at the write means no later path can set a mapping and
-
 
139
        // leave an internal store on the previous address - which is how the Delhi stores drifted.
-
 
140
        sellerService.syncInternalStoreAddresses(warehouse.getId());
-
 
141
 
-
 
142
        return "seller";
88
        return "seller";
143
 
-
 
144
    }
89
    }
145
 
90
 
146
    @RequestMapping(value = "/createNewSeller", method = RequestMethod.POST)
91
    @RequestMapping(value = "/getSellerWareHouse", method = RequestMethod.GET)
147
    public String getNewSeller(HttpServletRequest request, @RequestParam(name = "label", required = true, defaultValue = "0") String label, @RequestParam(name = "address", required = true, defaultValue = "0") String address, @RequestParam(name = "gst", required = true, defaultValue = "0") String gst, @RequestParam(name = "state", required = true, defaultValue = "0") int state, Model model) throws Exception {
92
    public String getSellerWareHouse(@RequestParam(name = "sellerId") int sellerId, Model model) throws Exception {
148
 
-
 
149
        Seller seller = new Seller();
93
        Seller seller = sellerRepository.selectById(sellerId);
150
 
-
 
151
        seller.setAddress(address);
94
        model.addAttribute("seller", seller);
152
        seller.setLabel(label);
-
 
153
        seller.setGstin(gst);
-
 
154
        seller.setStateId(state);
95
        model.addAttribute("stateNames", this.stateNames());
155
        seller.setOrganisationId(7);
96
        model.addAttribute("warehouses", warehouseSetupService.getPhysicalWarehouses(sellerId));
156
        sellerRepository.persist(seller);
97
        model.addAttribute("assignableAddresses", warehouseSetupService.getAssignableAddresses(seller.getStateId()));
157
 
-
 
158
        return "seller";
98
        return "seller-warehouse-container";
159
 
-
 
160
    }
99
    }
161
 
100
 
162
    @RequestMapping(value = "/createNewAddress", method = RequestMethod.POST)
101
    @RequestMapping(value = "/createNewAddress", method = RequestMethod.POST)
163
    public String getNewSeller(HttpServletRequest request, @RequestParam(name = "newAddress", required = true, defaultValue = "0") String address, @RequestParam(name = "pin", required = true, defaultValue = "0") String pin, @RequestParam(name = "stateId", required = true, defaultValue = "0") int stateId, Model model) throws Exception {
102
    @ResponseBody
164
 
-
 
165
        WarehouseAddressMaster wham = new WarehouseAddressMaster();
103
    public ResponseEntity<?> createNewAddress(@RequestBody NewWarehouseAddressRequest request) throws Exception {
166
        wham.setAddress(address);
-
 
167
        wham.setPin(pin);
-
 
168
        wham.setStateId(stateId);
-
 
169
        wham.setContacNumber("18002700273");
-
 
170
        wham.setContactPerson("Customer Care");
-
 
171
        wham.setCreated(LocalDateTime.now());
104
        WarehouseAddressMaster address = warehouseSetupService.createAddress(request);
172
        warehouseAddressMasterRepository.persist(wham);
105
        return ResponseEntity.ok(this.idResponse("addressId", address.getId()));
173
 
-
 
174
        return "seller";
-
 
175
 
-
 
176
    }
106
    }
177
 
107
 
178
    @RequestMapping(value = "/getAllSeller", method = RequestMethod.GET)
108
    @RequestMapping(value = "/warehouseAddress/{addressId}/active", method = RequestMethod.POST)
179
    public String getAllSeller(HttpServletRequest request, Model model) throws Exception {
-
 
180
 
-
 
181
        List<Seller> sellers = sellerRepository.selectAll().stream().filter(x -> x.getOrganisationId() == 7).collect(Collectors.toList());
-
 
182
 
-
 
183
        List<WarehouseAddressMaster> warehouseAddressMaster = sellerRepository.selectAllWarehouseAddressMaster();
-
 
184
 
-
 
185
        List<State> state = stateRepository.selectAll();
-
 
186
        LOGGER.info("statemaster" + state);
109
    @ResponseBody
187
        model.addAttribute("state", state);
110
    public ResponseEntity<?> setAddressActive(@PathVariable int addressId, @RequestParam boolean active)
188
 
-
 
189
        LOGGER.info("sellerLabel" + sellers);
111
            throws Exception {
190
        LOGGER.info("warehouseAddressMaster" + warehouseAddressMaster);
112
        warehouseSetupService.setAddressActive(addressId, active);
191
        model.addAttribute("sellerLabel", sellers);
-
 
192
        model.addAttribute("warehouseAddressMaster", warehouseAddressMaster);
113
        return ResponseEntity.ok(this.idResponse("addressId", addressId));
193
 
-
 
194
        return "seller";
-
 
195
    }
114
    }
196
 
115
 
197
    @RequestMapping(value = "/getSellerWareHouse", method = RequestMethod.GET)
116
    @RequestMapping(value = "/createNewSeller", method = RequestMethod.POST)
-
 
117
    @ResponseBody
198
    public String getSellerWareHouse(HttpServletRequest request, @RequestParam(name = "sellerId", required = true, defaultValue = "0") int sellerId, Model model) throws Exception {
118
    public ResponseEntity<?> createNewSeller(@RequestBody NewSellerRequest request) throws Exception {
199
 
-
 
200
        List<SellerWarehouse> sellerWarehouse = sellerWarehouseRepository.selectBySellerId(sellerId);
119
        Seller seller = warehouseSetupService.createSeller(request);
201
 
-
 
202
        List<WarehouseAddressMaster> allWarehouseAddressMaster = sellerRepository.selectAllWarehouseAddressMaster();
-
 
203
 
-
 
204
        Seller seller = sellerRepository.selectById(sellerId);
120
        return ResponseEntity.ok(this.idResponse("sellerId", seller.getId()));
205
 
121
    }
206
 
-
 
207
        List<Integer> sellerwarehouseIds = sellerWarehouse.stream().map(x -> x.getWarehouseId()).collect(Collectors.toList());
-
 
208
 
122
 
209
        Map<Integer, WarehouseAddressMaster> wam = null;
123
    @RequestMapping(value = "/createNewWarehouse", method = RequestMethod.POST)
210
        if (sellerwarehouseIds.size() > 0) {
124
    @ResponseBody
211
            List<WarehouseAddressMapping> warehouseAddressMapping = warehouseAddressMappingRepository.selectAllByIds(sellerwarehouseIds);
125
    public ResponseEntity<?> createNewWarehouse(@RequestBody NewPhysicalWarehouseRequest request) throws Exception {
212
            wam = mapWarhouseIdAndAddress(warehouseAddressMapping);
126
        Warehouse warehouse = warehouseSetupService.createPhysicalWarehouse(request);
213
        } else {
-
 
214
            wam = new HashMap<>();
127
        return ResponseEntity.ok(this.idResponse("warehouseId", warehouse.getId()));
215
        }
128
    }
216
 
129
 
-
 
130
    @RequestMapping(value = "/physicalWarehouse/{warehouseId}/active", method = RequestMethod.POST)
-
 
131
    @ResponseBody
-
 
132
    public ResponseEntity<?> setWarehouseActive(@PathVariable int warehouseId, @RequestParam boolean active)
-
 
133
            throws Exception {
217
        model.addAttribute("allWhm", allWarehouseAddressMaster);
134
        warehouseSetupService.setWarehouseActive(warehouseId, active);
-
 
135
        return ResponseEntity.ok(this.idResponse("warehouseId", warehouseId));
-
 
136
    }
218
 
137
 
-
 
138
    @RequestMapping(value = "/physicalWarehouse/{warehouseId}/address", method = RequestMethod.POST)
219
        model.addAttribute("wam", wam);
139
    @ResponseBody
220
        model.addAttribute("seller", seller);
140
    public ResponseEntity<?> remapAddress(@PathVariable int warehouseId, @RequestParam int addressId)
221
        model.addAttribute("sellerId", sellerId);
141
            throws Exception {
222
        model.addAttribute("sellerWarehouse", sellerWarehouse);
142
        warehouseSetupService.remapAddress(warehouseId, addressId);
223
        return "seller-warehouse-container";
143
        return ResponseEntity.ok(this.idResponse("warehouseId", warehouseId));
224
    }
144
    }
225
 
145
 
226
    private Map<Integer, WarehouseAddressMaster> mapWarhouseIdAndAddress(
146
    // Raised after the transaction has rolled back; the screen shows the rule that was broken.
227
                                                                         List<WarehouseAddressMapping> warehouseAddressMapping) {
-
 
228
        Map<Integer, WarehouseAddressMaster> wam = new HashMap<>();
147
    @ExceptionHandler(ProfitMandiBusinessException.class)
-
 
148
    @ResponseBody
229
        for (WarehouseAddressMapping wa : warehouseAddressMapping) {
149
    public ResponseEntity<String> handleBusinessException(ProfitMandiBusinessException e) {
230
            WarehouseAddressMaster wm = warehouseAddressMasterRepository.selectById(wa.getAddressId());
150
        LOGGER.info("Seller setup refused: {} = {} - {}", e.getRejectedType(), e.getRejectedValue(), e.getCode());
231
            wam.put(wa.getWarehouseId(), wm);
151
        return ResponseEntity.badRequest().body(e.getCode());
232
        }
152
    }
233
 
153
 
-
 
154
    private Map<Integer, String> stateNames() {
234
        return wam;
155
        return stateRepository.selectAll().stream()
-
 
156
                .collect(Collectors.toMap(State::getId, State::getName, (a, b) -> a));
235
    }
157
    }
236
 
158
 
237
}
-
 
238
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;
-
 
163
    }
-
 
164
}
-
 
165