Subversion Repositories SmartDukaan

Rev

Rev 28822 | Rev 29510 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

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.SellerWarehouse;
import com.spice.profitmandi.dao.entity.transaction.WarehouseAddressMapping;
import com.spice.profitmandi.dao.entity.transaction.WarehouseAddressMaster;
import com.spice.profitmandi.dao.enumuration.inventory.InventoryType;
import com.spice.profitmandi.dao.enumuration.inventory.WarehouseType;
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
import com.spice.profitmandi.dao.repository.inventory.WarehouseRepository;
import com.spice.profitmandi.dao.repository.transaction.SellerRepository;
import com.spice.profitmandi.dao.repository.transaction.SellerWarehouseRepository;
import com.spice.profitmandi.dao.repository.transaction.WareHouseAddressMappingRepository;
import com.spice.profitmandi.dao.repository.transaction.WareHouseAddressMasterRepository;


@Controller
@Transactional(rollbackFor = Throwable.class)
public class SellerController {

        @Autowired
        private SellerRepository sellerRepository;
        
        @Autowired
        private StateRepository stateRepository;
        

        @Autowired
        WarehouseRepository warehouseRepository;

        @Autowired
        WareHouseAddressMasterRepository wareHouseAddressMasterRepository;

        @Autowired
        private SellerWarehouseRepository sellerWarehouseRepository;

        @Autowired
        private WareHouseAddressMappingRepository wareHouseAddressMappingRepository;

        private static final Logger LOGGER = LogManager.getLogger(SellerController.class);

        @RequestMapping(value = "/createNewWarehouse", method = RequestMethod.POST)
        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)

                        throws Exception {

                Seller seller = sellerRepository.selectById(sellerId);
                WarehouseAddressMaster wam = wareHouseAddressMasterRepository.selectByAddressId(address);

                Warehouse warehouse = new Warehouse();
                warehouse.setDisplayName(warehouseName);
                warehouse.setLocation(wam.getAddress());
                warehouse.setGstin(seller.getGstin());
                warehouse.setStateId(seller.getStateId());
                warehouse.setStatus(3);
                warehouse.setAddedOn(LocalDateTime.now());
                warehouse.setLastCheckedOn(LocalDateTime.now());
                warehouse.setTinNumber(null);
        warehouse.setPincode(pinCode);
                warehouse.setLogisticsLocation(0);
                warehouse.setVendor(1);
                warehouse.setBillingType(0);
                warehouse.setInventoryType(InventoryType.GOOD);
                warehouse.setWarehouseType(WarehouseType.OURS);
                warehouse.setIsAvailabilityMonitored(1);
                warehouse.setSource(0);
                warehouseRepository.persist(warehouse);
                warehouse.setBillingWarehouseId(warehouse.getId());
                warehouse.setShippingWarehouseId(warehouse.getId());

                LOGGER.info("warehouseId" + warehouse.getId());
                
                SellerWarehouse sellerWarehouse = new SellerWarehouse();
                sellerWarehouse.setActive(true);
                sellerWarehouse.setWarehouseId(warehouse.getId());
                sellerWarehouse.setChallanSequence(0);
                sellerWarehouse.setCreateTimestamp(LocalDateTime.now());
                sellerWarehouse.setInvoiceSequence(0);
                sellerWarehouse.setPrefix(prefix);
                sellerWarehouse.setOrderType(0);
                sellerWarehouse.setSellerId(sellerId);
                sellerWarehouseRepository.persist(sellerWarehouse);

                WarehouseAddressMapping warehouseAddressMapping = new WarehouseAddressMapping();
                warehouseAddressMapping.setAddressId(address);
                warehouseAddressMapping.setCreated(LocalDateTime.now());
                warehouseAddressMapping.setUpdated(null);
                warehouseAddressMapping.setWarehouseId(warehouse.getId());
                warehouseAddressMapping.setAddressId(address);
                wareHouseAddressMappingRepository.persist(warehouseAddressMapping);

                
                return "seller";

        }

        @RequestMapping(value = "/createNewSeller", method = RequestMethod.POST)
        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 {

                Seller seller = new Seller();

                seller.setAddress(address);
                seller.setLabel(label);
                seller.setGstin(gst);
                seller.setStateId(state);
                seller.setOrganisationId(7);
                sellerRepository.persist(seller);

                return "seller";

        }

        @RequestMapping(value = "/createNewAddress", method = RequestMethod.POST)
        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 {

                WarehouseAddressMaster wham = new WarehouseAddressMaster();
                wham.setAddress(address);
                wham.setPin(pin);
                wham.setStateId(stateId);
                wham.setContacNumber("18002700273");
                wham.setContactPerson("Customer Care");
                wham.setCreated(LocalDateTime.now());
                wareHouseAddressMasterRepository.persist(wham);

                return "seller";

        }

        @RequestMapping(value = "/getAllSeller", method = RequestMethod.GET)
        public String getAllSeller(HttpServletRequest request, Model model) throws Exception {

                List<Seller> sellers = sellerRepository.selectAllSeller().stream().filter(x->x.getOrganisationId()==7).collect(Collectors.toList());

                List<WarehouseAddressMaster> warehouseAddressMaster = sellerRepository.selectAllWarehouseAddressMaster();

                List<State> state = stateRepository.selectAll();
                LOGGER.info("statemaster" + state);
                model.addAttribute("state", state);

                LOGGER.info("sellerLabel" + sellers);
                LOGGER.info("warehouseAddressMaster" + warehouseAddressMaster);
                model.addAttribute("sellerLabel", sellers);
                model.addAttribute("warehouseAddressMaster", warehouseAddressMaster);

                return "seller";
        }

        @RequestMapping(value = "/getSellerWareHouse", method = RequestMethod.GET)
        public String getSellerWareHouse(HttpServletRequest request,
                        @RequestParam(name = "sellerId", required = true, defaultValue = "0") int sellerId, Model model)
                        throws Exception {

                List<SellerWarehouse> sellerWarehouse = sellerWarehouseRepository.selectBySellerId(sellerId);

                List<WarehouseAddressMaster> allWarehouseAddressMaster = sellerRepository.selectAllWarehouseAddressMaster();

                Seller seller = sellerRepository.selectById(sellerId);


                List<Integer> sellerwarehouseIds = sellerWarehouse.stream().map(x -> x.getWarehouseId())
                                .collect(Collectors.toList());

                Map<Integer, WarehouseAddressMaster> wam = null;
                if(sellerwarehouseIds.size() > 0) {
                        List<WarehouseAddressMapping> warehouseAddressMapping = wareHouseAddressMappingRepository
                                        .selectAllByIds(sellerwarehouseIds);
                        wam = mapWarhouseIdAndAddress(warehouseAddressMapping);
                } else {
                        wam = new HashMap<>();
                }

                model.addAttribute("allWhm", allWarehouseAddressMaster);

                model.addAttribute("wam", wam);
                model.addAttribute("seller", seller);
                model.addAttribute("sellerId", sellerId);
                model.addAttribute("sellerWarehouse", sellerWarehouse);
                return "seller-warehouse-container";
        }

        private Map<Integer, WarehouseAddressMaster> mapWarhouseIdAndAddress(
                        List<WarehouseAddressMapping> warehouseAddressMapping) {
                Map<Integer, WarehouseAddressMaster> wam = new HashMap<>();
                for (WarehouseAddressMapping wa : warehouseAddressMapping) {
                        WarehouseAddressMaster wm = wareHouseAddressMasterRepository.selectByAddressId(wa.getAddressId());
                        wam.put(wa.getWarehouseId(), wm);
                }

                return wam;
        }

}