Subversion Repositories SmartDukaan

Rev

Rev 32565 | 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.model.ProfitMandiConstants;
import com.spice.profitmandi.dao.entity.inventory.State;
import com.spice.profitmandi.dao.entity.inventory.Vendor;
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.entity.warehouse.Supplier;
import com.spice.profitmandi.dao.enumuration.inventory.InventoryType;
import com.spice.profitmandi.dao.enumuration.inventory.WarehouseType;
import com.spice.profitmandi.dao.hrms.CreateNewSupplier;
import com.spice.profitmandi.dao.hrms.WarehouseIdPrefixModel;
import com.spice.profitmandi.dao.model.hdfc.SupplierFormData;
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
import com.spice.profitmandi.dao.repository.inventory.VendorRepository;
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;
import com.spice.profitmandi.dao.repository.warehouse.SupplierRepository;
import com.spice.profitmandi.service.warehouse.WarehouseService;
import com.spice.profitmandi.web.util.MVCResponseSender;
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.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

    @Autowired
    private SupplierRepository supplierRepository;

    @Autowired
    private MVCResponseSender mvcResponseSender;


    @Autowired
    private SellerRepository sellerRepository;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private SellerWarehouseRepository sellerWarehouseRepository;

    @Autowired
    private VendorRepository vendorRepository;

    @Autowired
    private WarehouseAddressMappingRepository warehouseAddressMappingRepository;

    @Autowired
    private WarehouseAddressMasterRepository warehouseAddressMasterRepository;

    @Autowired
    private StateRepository stateRepository;

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

    @Autowired
    WarehouseRepository warehouseRepository;

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

        List<Supplier> supplierInactives = supplierRepository.selectByStatus(false);

        model.addAttribute("supplierInactives", supplierInactives);

        LOGGER.info("supplierInactives" + supplierInactives);

        return "supplier-in-active";

    }

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

        List<Supplier> supplierAcvtives = supplierRepository.selectByStatus(true);

        List<SellerWarehouse> sellerWarehouses = sellerWarehouseRepository.selectAll();
        List<Integer> sellerwarehouseIds = sellerWarehouses.stream().map(x -> x.getWarehouseId()).collect(Collectors.toList());
        LOGGER.info("sellerWarehouses" + sellerWarehouses);
        Map<Integer, WarehouseIdPrefixModel> warehouseIdAndState = new HashMap<>();

        for (SellerWarehouse sw : sellerWarehouses) {

            WarehouseAddressMapping warehouseAddressMapping = warehouseAddressMappingRepository.selectByWarehouseId(sw.getWarehouseId());
            LOGGER.info("warehouseAddressMapping" + warehouseAddressMapping);
            if (warehouseAddressMapping != null) {
                WarehouseAddressMaster WarehouseAddressMaster = warehouseAddressMasterRepository.selectById(warehouseAddressMapping.getAddressId());
                LOGGER.info("warehouseAddressMappingAddreesId" + warehouseAddressMapping.getAddressId());
                LOGGER.info("WarehouseAddressMaster" + WarehouseAddressMaster);
                State state = stateRepository.selectById(WarehouseAddressMaster.getStateId());
                WarehouseIdPrefixModel wp = new WarehouseIdPrefixModel();
                wp.setPrefix(sw.getPrefix());
                wp.setStateName(state.getName());
                warehouseIdAndState.put(sw.getWarehouseId(), wp);
                LOGGER.info("state" + state);
            }
        }
        LOGGER.info("warehouseIdAndState" + warehouseIdAndState);
        model.addAttribute("warehouseIdAndState", warehouseIdAndState);

        List<State> state = stateRepository.selectAll();

        model.addAttribute("suppliers", supplierAcvtives);
        model.addAttribute("response1", mvcResponseSender.createResponseString(supplierAcvtives));
        model.addAttribute("state", state);

        return "supplier";

    }


    @RequestMapping(value = "/getCreateNewSupplier", method = RequestMethod.GET)
    public String getCreateNewSupplier(HttpServletRequest request, Model model) throws Exception {
        List<SellerWarehouse> sellerWarehouses = sellerWarehouseRepository.selectAll();
        Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
        sellerWarehouses = sellerWarehouses.stream().filter(x -> warehouseMap.containsKey(x.getWarehouseId())).collect(Collectors.toList());

        Map<Integer, WarehouseIdPrefixModel> warehouseIdAndState = new HashMap<>();

        for (SellerWarehouse sw : sellerWarehouses) {
            WarehouseAddressMapping warehouseAddressMapping = warehouseAddressMappingRepository.selectByWarehouseId(sw.getWarehouseId());
            LOGGER.info("warehouseAddressMapping" + warehouseAddressMapping);
            if (warehouseAddressMapping != null) {
                WarehouseAddressMaster WarehouseAddressMaster = warehouseAddressMasterRepository.selectById(warehouseAddressMapping.getAddressId());
                LOGGER.info("warehouseAddressMappingAddreesId" + warehouseAddressMapping.getAddressId());
                LOGGER.info("WarehouseAddressMaster" + WarehouseAddressMaster);
                State state = stateRepository.selectById(WarehouseAddressMaster.getStateId());
                WarehouseIdPrefixModel wp = new WarehouseIdPrefixModel();
                wp.setPrefix(sw.getPrefix());
                wp.setStateName(state.getName());
                warehouseIdAndState.put(sw.getWarehouseId(), wp);
                LOGGER.info("state" + state);
            }
        }
        LOGGER.info("warehouseIdAndState" + warehouseIdAndState);
        model.addAttribute("warehouseIdAndState", warehouseIdAndState);

        List<State> state = stateRepository.selectAll();

        model.addAttribute("state", state);
        model.addAttribute("warehouseMap", ProfitMandiConstants.WAREHOUSE_MAP);

        return "new-supplier";
    }

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

        Supplier supplier = supplierRepository.selectById(id);

        supplier.setStatus(false);

        model.addAttribute("response1", mvcResponseSender.createResponseString(true));

        return "response";
    }

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

        Supplier supplier = supplierRepository.selectById(id);

        supplier.setStatus(true);

        model.addAttribute("response1", mvcResponseSender.createResponseString(true));

        return "response";

    }

    @RequestMapping(value = "/submitNewSupplier", method = RequestMethod.POST)
    public String getCreateNewSupplier(HttpServletRequest request, @RequestBody CreateNewSupplier createNewSupplier, Model model) throws Exception {

        Supplier supplier = new Supplier();

        supplier.setName(createNewSupplier.getName());
        supplier.setCommunicationAddress(createNewSupplier.getCommunicationAddress());
        supplier.setContactEmail(createNewSupplier.getContactEmail());
        supplier.setContactFax(createNewSupplier.getContactFax());
        supplier.setContactName(createNewSupplier.getContactPerson());

        supplier.setContactPhone(createNewSupplier.getContactPhone());
        supplier.setGstin(createNewSupplier.getGst());
        supplier.setHeadDesignation(createNewSupplier.getHeadDesign());
        supplier.setHeadEmail(createNewSupplier.getHeadEmail());
        supplier.setHeadName(createNewSupplier.getHeadName());
        supplier.setPan(createNewSupplier.getPan());
        supplier.setPhone(createNewSupplier.getPhone());
        supplier.setPoValidityLimit(createNewSupplier.getpOValidityLimit());
        supplier.setRegisteredAddress(createNewSupplier.getRegisteredAddress());
        supplier.setStateId(createNewSupplier.getState());
        supplier.setInternalWarehouseId(createNewSupplier.getInternalWarehouseId());
        supplierRepository.persist(supplier);

        Vendor vendor = new Vendor();
        vendor.setName(supplier.getName());
        vendor.setId(supplier.getId());
        vendorRepository.persist(vendor);
        LOGGER.info("supplierId" + supplier.getId());
        LOGGER.info("vendorId" + vendor.getId());

        List<WarehouseAddressMapping> warehouseAddressMappings = warehouseAddressMappingRepository.selectAllByIds(createNewSupplier.getWarehouseId());
        LOGGER.info("warehouseAddressMappings" + warehouseAddressMappings);

        for (WarehouseAddressMapping wamp : warehouseAddressMappings) {
            List<SellerWarehouse> sellerWarehouse = sellerWarehouseRepository.selectBywarehouseId(wamp.getWarehouseId());
            for (SellerWarehouse sw : sellerWarehouse) {
                Seller seller = sellerRepository.selectById(sw.getSellerId());

                WarehouseAddressMaster wam = warehouseAddressMasterRepository.selectById(wamp.getAddressId());

                // GOOD - WarehouseType.OURS, InventoryType.GOOD
                warehouseService.createVendorWarehouse(WarehouseType.OURS, InventoryType.GOOD, seller.getLabel(), wamp.getWarehouseId(), wam, vendor.getId());

                // BAD - WarehouseType.OURS, InventoryType.BAD
                warehouseService.createVendorWarehouse(WarehouseType.OURS, InventoryType.BAD, seller.getLabel(), wamp.getWarehouseId(), wam, vendor.getId());

                // VIRTUAL - WarehouseType.THIRD_PARTY, InventoryType.GOOD
                warehouseService.createVendorWarehouse(WarehouseType.THIRD_PARTY, InventoryType.GOOD, seller.getLabel(), wamp.getWarehouseId(), wam, vendor.getId());

            }

        }

        //model.addAttribute("response1", mvcResponseSender.createResponseString(true));

        return getShowInActiveSupplier(request, model);

    }

    @RequestMapping(value = "/supplier/edit/{supplierId}", method = RequestMethod.GET)
    public String getEditSupplier(HttpServletRequest request, Model model, @PathVariable int supplierId) throws Exception {
        Supplier supplier  = supplierRepository.selectById(supplierId);
        List<SellerWarehouse> sellerWarehouses = sellerWarehouseRepository.selectAll();
        Map<Integer, WarehouseIdPrefixModel> warehouseIdAndState = new HashMap<>();

        List<Warehouse> supplierWarehouses = warehouseRepository.selectVendorWarehouses(supplierId, WarehouseType.OURS, InventoryType.GOOD);
        List<Integer> supplierPhysicalWarehouseIds = supplierWarehouses.stream().map(x -> x.getShippingWarehouseId()).collect(Collectors.toList());
        LOGGER.info("supplierPhysicalWarehouseIds" + supplierPhysicalWarehouseIds);
        for (SellerWarehouse sw : sellerWarehouses) {
            WarehouseAddressMapping warehouseAddressMapping = warehouseAddressMappingRepository.selectByWarehouseId(sw.getWarehouseId());
            if (warehouseAddressMapping != null) {
                WarehouseAddressMaster WarehouseAddressMaster = warehouseAddressMasterRepository.selectById(warehouseAddressMapping.getAddressId());
                State state = stateRepository.selectById(WarehouseAddressMaster.getStateId());
                WarehouseIdPrefixModel wp = new WarehouseIdPrefixModel();
                wp.setPrefix(sw.getPrefix());
                wp.setStateName(state.getName());
                warehouseIdAndState.put(sw.getWarehouseId(), wp);
            }
        }
        LOGGER.info("warehouseIdAndState" + warehouseIdAndState);
        model.addAttribute("warehouseIdAndState", warehouseIdAndState);

        List<State> state = stateRepository.selectAll();

        model.addAttribute("state", state);
        model.addAttribute("warehouseMap", ProfitMandiConstants.WAREHOUSE_MAP);
        model.addAttribute("supplier", supplier);
        model.addAttribute("supplierPhysicalWarehouseIds", supplierPhysicalWarehouseIds);
        return "edit-supplier";
    }

    @RequestMapping(value = "/supplier/update/{supplierId}", method = RequestMethod.POST)
    public String updateSupplier(@PathVariable("supplierId") int supplierId,
                                 @RequestBody SupplierFormData supplierFormData,
                                 Model model) throws Exception {
        LOGGER.info("supplierFormDataaaa - " + supplierFormData);

        Supplier supplier = supplierRepository.selectById(supplierId);
        supplier.setContactPhone(supplierFormData.getInputPhone());
        supplier.setGstin(supplierFormData.getInputGst());
        supplier.setPan(supplierFormData.getInputPan());
        supplier.setFax(supplierFormData.getInputFax());
        supplier.setHeadName(supplierFormData.getInputHeadName());
        supplier.setHeadDesignation(supplierFormData.getInputHeadDesign());
        supplier.setHeadEmail(supplierFormData.getInputHeadEmail());
        supplier.setContactName(supplierFormData.getInputContactPerson());
        supplier.setContactPhone(supplierFormData.getInputContactPhone());
        supplier.setContactEmail(supplierFormData.getInputContactEmail());
        supplier.setFax((supplierFormData.getInputFax()));
        supplier.setRegisteredAddress(supplierFormData.getInputRegisteredAddress());
        supplier.setCommunicationAddress(supplierFormData.getInputCommunicationAddress());
        supplier.setTnc(supplierFormData.getInputTermConditions());
        supplier.setInternalWarehouseId(supplierFormData.getInternalWarehouseId());
        supplier.setStateId(supplierFormData.getStateId());
        supplier.setPoValidityLimit(supplierFormData.getInputPOValidityLimit());

        List<Integer> newWarehouseIds = supplierFormData.getInputWarehouseLocation();

        // Only run the warehouse update logic if new warehouse IDs are provided
        if (newWarehouseIds != null && !newWarehouseIds.isEmpty()) {
            List<Warehouse> existingWarehouses = warehouseRepository.selectVendorWarehouses(supplierId, WarehouseType.OURS, InventoryType.GOOD);
            List<Integer> existingWarehouseIds = existingWarehouses.stream().map(Warehouse::getShippingWarehouseId).collect(Collectors.toList());

            // Filter out any IDs that are already present
            List<Integer> warehousesToAdd = newWarehouseIds.stream()
                    .filter(id -> !existingWarehouseIds.contains(id))
                    .collect(Collectors.toList());

            if (!warehousesToAdd.isEmpty()) {
                for (Integer warehouseId : warehousesToAdd) {
                    WarehouseAddressMapping warehouseAddressMapping = warehouseAddressMappingRepository.selectByWarehouseId(warehouseId);
                    if (warehouseAddressMapping != null) {
                        List<SellerWarehouse> sellerWarehouses = sellerWarehouseRepository.selectBywarehouseId(warehouseAddressMapping.getWarehouseId());
                        for (SellerWarehouse sellerWarehouse : sellerWarehouses) {
                            Seller seller = sellerRepository.selectById(sellerWarehouse.getSellerId());
                            WarehouseAddressMaster wam = warehouseAddressMasterRepository.selectById(warehouseAddressMapping.getAddressId());

                            // GOOD - WarehouseType.OURS, InventoryType.GOOD
                            warehouseService.createVendorWarehouse(WarehouseType.OURS, InventoryType.GOOD, seller.getLabel(), warehouseId, wam, supplierId);

                            // BAD - WarehouseType.OURS, InventoryType.BAD
                            warehouseService.createVendorWarehouse(WarehouseType.OURS, InventoryType.BAD, seller.getLabel(), warehouseId, wam, supplierId);

                            // VIRTUAL - WarehouseType.THIRD_PARTY, InventoryType.GOOD
                            warehouseService.createVendorWarehouse(WarehouseType.THIRD_PARTY, InventoryType.GOOD, seller.getLabel(), warehouseId, wam, supplierId);
                        }
                    }
                }
            }
        }

        return "edit-supplier";
    }





}