Subversion Repositories SmartDukaan

Rev

Rev 21878 | Rev 23085 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.inventory.controllers;

import in.shop2020.inventory.utils.Utils;
import in.shop2020.model.v1.inventory.BillingType;
import in.shop2020.model.v1.inventory.InventoryType;
import in.shop2020.model.v1.inventory.Vendor;
import in.shop2020.model.v1.inventory.Warehouse;
import in.shop2020.model.v1.inventory.WarehouseType;
import in.shop2020.purchase.PurchaseService.Client;
import in.shop2020.purchase.Supplier;
import in.shop2020.thrift.clients.InventoryClient;
import in.shop2020.thrift.clients.PurchaseClient;
import in.shop2020.utils.ConfigClientKeys;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author mandeep
 *
 */
public class SupplierController extends BaseController {
    private static Log logger = LogFactory.getLog(SupplierController.class);
    private String name;
    private String phone;
    private String fax;
    private String gstin;
    public String getGstin() {
                return gstin;
        }

        public void setGstin(String gstin) {
                this.gstin = gstin;
        }

        private String pan;
    private String headName;
    private String headDesignation;
    private String headEmail;
    private String contactName;
    private String contactPhone;
    private String contactFax;
    private String contactEmail;
    private String registeredAddress;
    private long stateId;
    private String stateIdVal;
    private long poValidityLimit;
    private String communicationAddress;
    private List<Long> billingWarehouseIds;
    
    private String id;
    private List<Supplier> suppliers;
    private String errorMessage = "";
    private Supplier supplier;

    public String index() {
        return INDEX;
    }
    
    /* (non-Javadoc)
     * @see in.shop2020.inventory.controllers.BaseController#edit()
     */
    @Override
    public String edit() {
        try {
            Client purchaseClient = new PurchaseClient().getClient();
            supplier = purchaseClient.getSupplier(Long.valueOf(id));
        } catch (Exception e) {
            errorMessage = "Error finding supplier: " + id + ": " + e.getMessage();
            logger.error("Could not find supplier: " + id, e);
        }

        return super.edit();
    }

    public String create() {
        try {
                if(name==null || ("").equalsIgnoreCase(name)){
                        throw new Exception("Supplier Name is Mandatory.");
                }
                if((phone==null || ("").equalsIgnoreCase(phone)) && !StringUtils.isNumeric(phone)){
                        throw new Exception("Supplier Phone is Mandatory and Should be in digits only");
                }
                if(fax==null || ("").equalsIgnoreCase(fax)){
                        throw new Exception("Supplier Fax is Mandatory.");
                }
                if(gstin==null || ("").equalsIgnoreCase(gstin)){
                        throw new Exception("Supplier GSTIN is Mandatory.");
                }
                if(pan==null || ("").equalsIgnoreCase(pan)){
                        throw new Exception("Supplier PAN is Mandatory.");
                }
                if(headName==null || ("").equalsIgnoreCase(headName)){
                        throw new Exception("Supplier Head Name is Mandatory.");
                }
                if(headEmail==null || ("").equalsIgnoreCase(headEmail)){
                        throw new Exception("Supplier Head Email is Mandatory.");
                }
                if(contactName==null || ("").equalsIgnoreCase(contactName)){
                        throw new Exception("Supplier Contact Name is Mandatory.");
                }
                if(contactFax==null || ("").equalsIgnoreCase(contactFax)){
                        throw new Exception("Supplier Name is Mandatory.");
                }
                if(contactPhone==null || ("").equalsIgnoreCase(contactPhone)){
                        throw new Exception("Supplier Contact Phone is Mandatory.");
                }
                if(registeredAddress==null || ("").equalsIgnoreCase(registeredAddress)){
                        throw new Exception("Supplier Registered Address is Mandatory.");
                }
                if(communicationAddress==null || ("").equalsIgnoreCase(communicationAddress)){
                        throw new Exception("Supplier Communication Address is Mandatory.");
                }
                if(billingWarehouseIds==null || billingWarehouseIds.size()==0){
                        throw new Exception("Atleast One Billing Warehouse is Mandatory.");
                }
                if(stateId == -1){
                        throw new Exception("State is Mandatory. Please select state");
                }
                if(poValidityLimit < 1){
                        throw new Exception("PO Validity Limit Must be greater than equal to 1.");
                }
            Supplier supplier = new Supplier();
            name = name.trim();
            supplier.setName(name);
            supplier.setCommunicationAddress(communicationAddress);
            supplier.setContactEmail(contactEmail);
            supplier.setContactFax(contactFax);
            supplier.setContactName(contactName);
            supplier.setContactPhone(contactPhone);
            supplier.setFax(fax);
            supplier.setHeadDesignation(headDesignation);
            supplier.setHeadEmail(headEmail);
            supplier.setHeadName(headName);
            supplier.setPan(pan);
            supplier.setPhone(phone);
            supplier.setRegisteredAddress(registeredAddress);
            //supplier.setTin(tin);
            supplier.setGstin(gstin);
            supplier.setStateId(stateId);
            supplier.setPoValidityLimit(poValidityLimit);
            Client purchaseClient = new PurchaseClient().getClient();
            supplier = purchaseClient.addSupplier(supplier);
            
            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = new InventoryClient(ConfigClientKeys.inventory_service_server_host.toString(),
                    ConfigClientKeys.inventory_service_server_port.toString()).getClient();
            Vendor vendor = new Vendor();
            vendor.setId(supplier.getId());
            vendor.setName(name);
            inventoryClient.addVendor(vendor);
            for(Long billingWarehouseId : billingWarehouseIds) {
                    Warehouse billingWarehouse = inventoryClient.getWarehouse(billingWarehouseId);
                    Warehouse warehouse = new Warehouse();
                    warehouse.setVendor(vendor);
                    warehouse.setStateId(billingWarehouse.getStateId());
                    warehouse.setBillingType(BillingType.OURS);
                    warehouse.setBillingWarehouseId(billingWarehouse.getId());
                    warehouse.setDisplayName(StringUtils.join(new String[]{billingWarehouse.getDisplayName(), name, "G"}, '/'));
                    warehouse.setInventoryType(InventoryType.GOOD);
                    warehouse.setIsAvailabilityMonitored(false);
                    warehouse.setLocation(billingWarehouse.getLocation());
                    warehouse.setPincode(billingWarehouse.getPincode());
                    warehouse.setLogisticsLocation(billingWarehouse.getLogisticsLocation());
                    warehouse.setShippingWarehouseId(billingWarehouse.getId());
                    warehouse.setGstin(gstin);
                    warehouse.setTransferDelayInHours(0);
                    warehouse.setWarehouseType(WarehouseType.OURS);
                    inventoryClient.addWarehouse(warehouse);
                    
                    warehouse.setDisplayName(StringUtils.join(new String[]{billingWarehouse.getDisplayName(), name, "B"}, '/'));
                    warehouse.setInventoryType(InventoryType.BAD);
                    inventoryClient.addWarehouse(warehouse);
                    
                    warehouse.setWarehouseType(WarehouseType.THIRD_PARTY);
                    warehouse.setDisplayName(StringUtils.join(new String[]{name, "G"}, '/'));
                    warehouse.setInventoryType(InventoryType.GOOD);
                    warehouse.setTransferDelayInHours(24);
                    warehouse.setBillingType(null);
                    warehouse.setBillingWarehouseId(0);
                    warehouse.setShippingWarehouseId(0);
                    inventoryClient.addWarehouse(warehouse);
                    Utils.resetSuppliers();
            }
        } catch (Exception e) {
            errorMessage = "Error creating supplier: " + e.getMessage();
            logger.error("Error creating supplier", e);
        }

        return index();
    }

    public String update() {
        String supplierName = id;
        try {
            Client purchaseClient = new PurchaseClient().getClient();
            Supplier supplier = purchaseClient.getSupplier(Long.valueOf(id));
            supplierName = supplier.getName();
            supplier.setName(name);
            supplier.setCommunicationAddress(communicationAddress);
            supplier.setContactEmail(contactEmail);
            supplier.setContactFax(contactFax);
            supplier.setContactName(contactName);
            supplier.setContactPhone(contactPhone);
            supplier.setFax(fax);
            supplier.setHeadDesignation(headDesignation);
            supplier.setHeadEmail(headEmail);
            supplier.setHeadName(headName);
            supplier.setPan(pan);
            supplier.setRegisteredAddress(registeredAddress);
            supplier.setGstin(gstin);
            supplier.setStateId(supplier.getStateId());
            supplier.setPoValidityLimit(poValidityLimit);
            purchaseClient.updateSupplier(supplier);
            Utils.resetSuppliers();
        } catch (Exception e) {
            errorMessage = "Error updating supplier: " + supplierName + ": " + e.getMessage();
            logger.error("Error updating supplier", e);
        }

        return index();
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getFax() {
        return fax;
    }
    public void setFax(String fax) {
        this.fax = fax;
    }
    public String getPan() {
        return pan;
    }
    public void setPan(String pan) {
        this.pan = pan;
    }
    public String getHeadName() {
        return headName;
    }
    public void setHeadName(String headName) {
        this.headName = headName;
    }
    public String getHeadDesignation() {
        return headDesignation;
    }
    public void setHeadDesignation(String headDesignation) {
        this.headDesignation = headDesignation;
    }
    public String getHeadEmail() {
        return headEmail;
    }
    public void setHeadEmail(String headEmail) {
        this.headEmail = headEmail;
    }
    public String getContactName() {
        return contactName;
    }
    public void setContactName(String contactName) {
        this.contactName = contactName;
    }
    public String getContactPhone() {
        return contactPhone;
    }
    public void setContactPhone(String contactPhone) {
        this.contactPhone = contactPhone;
    }
    public String getContactFax() {
        return contactFax;
    }
    public void setContactFax(String contactFax) {
        this.contactFax = contactFax;
    }
    public String getContactEmail() {
        return contactEmail;
    }
    public void setContactEmail(String contactEmail) {
        this.contactEmail = contactEmail;
    }
    public String getRegisteredAddress() {
        return registeredAddress;
    }
    public void setRegisteredAddress(String registeredAddress) {
        this.registeredAddress = registeredAddress;
    }
    public String getCommunicationAddress() {
        return communicationAddress;
    }
    public void setCommunicationAddress(String communicationAddress) {
        this.communicationAddress = communicationAddress;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Supplier> getSuppliers() {
        return Utils.getSuppliers();
    }

    public void setSuppliers(List<Supplier> suppliers) {
        this.suppliers = suppliers;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

        public List<Long> getBillingWarehouseIds() {
                return billingWarehouseIds;
        }

        public void setBillingWarehouseIds(List<Long> billingWarehouseIds) {
                this.billingWarehouseIds = billingWarehouseIds;
        }

    public long getStateId() {
                return stateId;
        }
    
    public String getStateName(long stateId){
        return Utils.getStateIdMap().get(stateId).getStateName();
    }

        public void setStateId(long stateId) {
                this.stateId = stateId;
        }

        public long getPoValidityLimit() {
                return poValidityLimit;
        }

        public void setPoValidityLimit(long poValidityLimit) {
                this.poValidityLimit = poValidityLimit;
        }

        public String getStateIdVal() {
                return stateIdVal;
        }

        public void setStateIdVal(String stateIdVal) {
                this.stateIdVal = stateIdVal;
        }
    
}