Subversion Repositories SmartDukaan

Rev

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

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

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 tin;
    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 String communicationAddress;
    private List<Long> billingWarehouseIds;
    
    private String id;
    private List<Supplier> suppliers;
    private String errorMessage = "";
    private Supplier supplier;

    public String index() {
        try {
            Client purchaseClient = new PurchaseClient().getClient();
            suppliers = purchaseClient.getSuppliers();
        } catch (Exception e) {
            logger.error("Error loading suppliers", e);
        }

        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 {
            Supplier supplier = new Supplier();
            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);
            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.setTinNumber(tin);
                    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);
            }
        } 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.setPhone(phone);
            supplier.setRegisteredAddress(registeredAddress);
            supplier.setTin(tin);
            purchaseClient.updateSupplier(supplier);
        } 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 getTin() {
        return tin;
    }
    public void setTin(String tin) {
        this.tin = tin;
    }
    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 suppliers;
    }

    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;
        }
    
}