Subversion Repositories SmartDukaan

Rev

Rev 37653 | 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
 
28499 amit.gupta 3
import com.spice.profitmandi.dao.entity.inventory.State;
4
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.SellerWarehouse;
7
import com.spice.profitmandi.dao.entity.transaction.WarehouseAddressMapping;
8
import com.spice.profitmandi.dao.entity.transaction.WarehouseAddressMaster;
9
import com.spice.profitmandi.dao.enumuration.inventory.InventoryType;
10
import com.spice.profitmandi.dao.enumuration.inventory.WarehouseType;
11
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
28493 tejbeer 12
import com.spice.profitmandi.dao.repository.inventory.WarehouseRepository;
28300 tejbeer 13
import com.spice.profitmandi.dao.repository.transaction.SellerRepository;
14
import com.spice.profitmandi.dao.repository.transaction.SellerWarehouseRepository;
32383 amit.gupta 15
import com.spice.profitmandi.dao.repository.transaction.WarehouseAddressMappingRepository;
16
import com.spice.profitmandi.dao.repository.transaction.WarehouseAddressMasterRepository;
37653 amit 17
import com.spice.profitmandi.service.SellerService;
32383 amit.gupta 18
import org.apache.logging.log4j.LogManager;
19
import org.apache.logging.log4j.Logger;
20
import org.springframework.beans.factory.annotation.Autowired;
37653 amit 21
import org.springframework.http.ResponseEntity;
32383 amit.gupta 22
import org.springframework.stereotype.Controller;
23
import org.springframework.transaction.annotation.Transactional;
24
import org.springframework.ui.Model;
25
import org.springframework.web.bind.annotation.RequestMapping;
26
import org.springframework.web.bind.annotation.RequestMethod;
27
import org.springframework.web.bind.annotation.RequestParam;
37653 amit 28
import org.springframework.web.bind.annotation.ResponseBody;
28300 tejbeer 29
 
32383 amit.gupta 30
import javax.servlet.http.HttpServletRequest;
31
import java.time.LocalDateTime;
32
import java.util.HashMap;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.stream.Collectors;
28499 amit.gupta 36
 
32383 amit.gupta 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
 
28300 tejbeer 48
 
32075 tejbeer 49
    @Autowired
50
    WarehouseRepository warehouseRepository;
28300 tejbeer 51
 
32075 tejbeer 52
    @Autowired
32383 amit.gupta 53
    WarehouseAddressMasterRepository warehouseAddressMasterRepository;
28300 tejbeer 54
 
32075 tejbeer 55
    @Autowired
56
    private SellerWarehouseRepository sellerWarehouseRepository;
28300 tejbeer 57
 
32075 tejbeer 58
    @Autowired
32383 amit.gupta 59
    private WarehouseAddressMappingRepository warehouseAddressMappingRepository;
28300 tejbeer 60
 
37653 amit 61
    @Autowired
62
    private SellerService sellerService;
63
 
32075 tejbeer 64
    private static final Logger LOGGER = LogManager.getLogger(SellerController.class);
28300 tejbeer 65
 
37653 amit 66
    /**
67
     * Realign the INTERNAL stores of a warehouse with the address currently mapped to it.
68
     *
69
     * The mapping has no edit screen - it gets repointed straight in the database - so nothing
70
     * 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
72
     * after changing a warehouse's address mapping.
73
     */
74
    @RequestMapping(value = "/syncWarehouseInternalAddress", method = RequestMethod.POST)
75
    @ResponseBody
76
    public ResponseEntity<?> syncWarehouseInternalAddress(@RequestParam(name = "warehouseId") int warehouseId)
77
            throws Exception {
78
        int synced = sellerService.syncInternalStoreAddresses(warehouseId);
79
        LOGGER.info("Synced {} internal store address(es) for warehouse {}", synced, warehouseId);
80
        Map<String, Object> response = new HashMap<>();
81
        response.put("warehouseId", warehouseId);
82
        response.put("storesSynced", synced);
83
        return ResponseEntity.ok(response);
84
    }
85
 
32075 tejbeer 86
    @RequestMapping(value = "/createNewWarehouse", method = RequestMethod.POST)
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)
28493 tejbeer 88
 
32075 tejbeer 89
            throws Exception {
28493 tejbeer 90
 
32075 tejbeer 91
        Seller seller = sellerRepository.selectById(sellerId);
32383 amit.gupta 92
        WarehouseAddressMaster wam = warehouseAddressMasterRepository.selectById(address);
28300 tejbeer 93
 
32075 tejbeer 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());
28300 tejbeer 114
 
32075 tejbeer 115
        LOGGER.info("warehouseId" + warehouse.getId());
28300 tejbeer 116
 
32075 tejbeer 117
        SellerWarehouse sellerWarehouse = new SellerWarehouse();
118
        sellerWarehouse.setActive(true);
119
        sellerWarehouse.setWarehouseId(warehouse.getId());
120
        sellerWarehouse.setChallanSequence(0);
121
        sellerWarehouse.setCreateTimestamp(LocalDateTime.now());
122
        sellerWarehouse.setInvoiceSequence(0);
123
        sellerWarehouse.setPrefix(prefix);
124
        sellerWarehouse.setOrderType(0);
125
        sellerWarehouse.setSellerId(sellerId);
126
        sellerWarehouseRepository.persist(sellerWarehouse);
28300 tejbeer 127
 
32075 tejbeer 128
        WarehouseAddressMapping warehouseAddressMapping = new WarehouseAddressMapping();
129
        warehouseAddressMapping.setAddressId(address);
130
        warehouseAddressMapping.setCreated(LocalDateTime.now());
131
        warehouseAddressMapping.setUpdated(null);
132
        warehouseAddressMapping.setWarehouseId(warehouse.getId());
133
        warehouseAddressMapping.setAddressId(address);
32383 amit.gupta 134
        warehouseAddressMappingRepository.persist(warehouseAddressMapping);
28300 tejbeer 135
 
37654 amit 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());
28300 tejbeer 141
 
32075 tejbeer 142
        return "seller";
28300 tejbeer 143
 
32075 tejbeer 144
    }
28300 tejbeer 145
 
32075 tejbeer 146
    @RequestMapping(value = "/createNewSeller", method = RequestMethod.POST)
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 {
28300 tejbeer 148
 
32075 tejbeer 149
        Seller seller = new Seller();
28300 tejbeer 150
 
32075 tejbeer 151
        seller.setAddress(address);
152
        seller.setLabel(label);
153
        seller.setGstin(gst);
154
        seller.setStateId(state);
155
        seller.setOrganisationId(7);
156
        sellerRepository.persist(seller);
28300 tejbeer 157
 
32075 tejbeer 158
        return "seller";
28300 tejbeer 159
 
32075 tejbeer 160
    }
28300 tejbeer 161
 
32075 tejbeer 162
    @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 {
28300 tejbeer 164
 
32075 tejbeer 165
        WarehouseAddressMaster wham = new WarehouseAddressMaster();
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());
32383 amit.gupta 172
        warehouseAddressMasterRepository.persist(wham);
28300 tejbeer 173
 
32075 tejbeer 174
        return "seller";
28300 tejbeer 175
 
32075 tejbeer 176
    }
28300 tejbeer 177
 
32075 tejbeer 178
    @RequestMapping(value = "/getAllSeller", method = RequestMethod.GET)
179
    public String getAllSeller(HttpServletRequest request, Model model) throws Exception {
28300 tejbeer 180
 
32075 tejbeer 181
        List<Seller> sellers = sellerRepository.selectAll().stream().filter(x -> x.getOrganisationId() == 7).collect(Collectors.toList());
28300 tejbeer 182
 
32075 tejbeer 183
        List<WarehouseAddressMaster> warehouseAddressMaster = sellerRepository.selectAllWarehouseAddressMaster();
28300 tejbeer 184
 
32075 tejbeer 185
        List<State> state = stateRepository.selectAll();
186
        LOGGER.info("statemaster" + state);
187
        model.addAttribute("state", state);
28300 tejbeer 188
 
32075 tejbeer 189
        LOGGER.info("sellerLabel" + sellers);
190
        LOGGER.info("warehouseAddressMaster" + warehouseAddressMaster);
191
        model.addAttribute("sellerLabel", sellers);
192
        model.addAttribute("warehouseAddressMaster", warehouseAddressMaster);
28300 tejbeer 193
 
32075 tejbeer 194
        return "seller";
195
    }
28300 tejbeer 196
 
32075 tejbeer 197
    @RequestMapping(value = "/getSellerWareHouse", method = RequestMethod.GET)
198
    public String getSellerWareHouse(HttpServletRequest request, @RequestParam(name = "sellerId", required = true, defaultValue = "0") int sellerId, Model model) throws Exception {
28300 tejbeer 199
 
32075 tejbeer 200
        List<SellerWarehouse> sellerWarehouse = sellerWarehouseRepository.selectBySellerId(sellerId);
28300 tejbeer 201
 
32075 tejbeer 202
        List<WarehouseAddressMaster> allWarehouseAddressMaster = sellerRepository.selectAllWarehouseAddressMaster();
28300 tejbeer 203
 
32075 tejbeer 204
        Seller seller = sellerRepository.selectById(sellerId);
28300 tejbeer 205
 
206
 
32075 tejbeer 207
        List<Integer> sellerwarehouseIds = sellerWarehouse.stream().map(x -> x.getWarehouseId()).collect(Collectors.toList());
28300 tejbeer 208
 
32075 tejbeer 209
        Map<Integer, WarehouseAddressMaster> wam = null;
210
        if (sellerwarehouseIds.size() > 0) {
32383 amit.gupta 211
            List<WarehouseAddressMapping> warehouseAddressMapping = warehouseAddressMappingRepository.selectAllByIds(sellerwarehouseIds);
32075 tejbeer 212
            wam = mapWarhouseIdAndAddress(warehouseAddressMapping);
213
        } else {
214
            wam = new HashMap<>();
215
        }
28300 tejbeer 216
 
32075 tejbeer 217
        model.addAttribute("allWhm", allWarehouseAddressMaster);
218
 
219
        model.addAttribute("wam", wam);
220
        model.addAttribute("seller", seller);
221
        model.addAttribute("sellerId", sellerId);
222
        model.addAttribute("sellerWarehouse", sellerWarehouse);
223
        return "seller-warehouse-container";
224
    }
225
 
226
    private Map<Integer, WarehouseAddressMaster> mapWarhouseIdAndAddress(
227
                                                                         List<WarehouseAddressMapping> warehouseAddressMapping) {
228
        Map<Integer, WarehouseAddressMaster> wam = new HashMap<>();
229
        for (WarehouseAddressMapping wa : warehouseAddressMapping) {
32383 amit.gupta 230
            WarehouseAddressMaster wm = warehouseAddressMasterRepository.selectById(wa.getAddressId());
32075 tejbeer 231
            wam.put(wa.getWarehouseId(), wm);
232
        }
233
 
234
        return wam;
235
    }
236
 
28300 tejbeer 237
}