Subversion Repositories SmartDukaan

Rev

Rev 18457 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.catalog.dashboard.shared;

import in.shop2020.catalog.dashboard.client.CatalogService;
import in.shop2020.catalog.dashboard.client.CatalogServiceAsync;
import in.shop2020.model.v1.catalog.CategoryHsnCode;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Utility methods goes here
 *
 */
public class Utils {
    // item status values. These should be analogous to status enum in status.java
    // These values are used in the client side to check if transition from one status to another is 
    // valid. Also see validateStatusChange method
    // FIXME: These should be read from the ThriftConfig file and not hard-coded here.
    public static final int PHASED_OUT = 0,
                            DELETED = 1,
                            PAUSED = 2,
                            ACTIVE = 3,
                            IN_PROCESS = 4,
                            CONTENT_COMPLETE = 5;
    
    // role column in helper.catalogdashboarduser table. role specifies 
    // the username-pwd pair for staging or for production.
    // production password is needed while updating item on production server.
    public static final int ROLE_STAGING = 0, ROLE_READONLY = 1, ROLE_PRODUCTION = 2;
    private static boolean entityIdMandatory = false;
    
    private static Map<Long, String> vendors, warehouses, sources, voucherType, categoriesMap, stateMap;
    private static List<String> categories, brands;
    
    private static final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
    
    private static List<ItemWarehouse> ignoredInventoryUpdateItemsIdsWarehouseIds;
    
    private static List<CategoryHsnCode> categoryHsnCodes;
    
    private static Map<Long,String> allInsurers;
    
    static {
        catalogService.getAllInsurers(new AsyncCallback<Map<Long,String>>() {
            @Override
            public void onSuccess(Map<Long, String> result) {
                allInsurers = result;
            }
            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
            }
        });
        catalogService.getConfigforentityIdMandatory(new AsyncCallback<Boolean>() {
                @Override
                public void onSuccess(Boolean result) {
                        setEntityIdMandatory(result);
                }
                @Override
                public void onFailure(Throwable caught) {
                    caught.printStackTrace();
                }
            });
        catalogService.getignoredInventoryUpdateItemsIdsWarehouseIds(new AsyncCallback<List<ItemWarehouse>>() {
                @Override
                public void onSuccess(List<ItemWarehouse> result) {
                        ignoredInventoryUpdateItemsIdsWarehouseIds=result;
                }
                @Override
                public void onFailure(Throwable caught) {
                    caught.printStackTrace();
                }
            });

        
        catalogService.getAllVendors(new AsyncCallback<Map<Long,String>>() {
            @Override
            public void onSuccess(Map<Long, String> result) {
                vendors = result;
            }
            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
            }
        });
        

        
        catalogService.getAllStatesMap(new AsyncCallback<Map<Long,String>>() {
            @Override
            public void onSuccess(Map<Long, String> result) {
                stateMap = result;
            }
            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
            }
        });
        
        
        catalogService.getAllWarehouses(new AsyncCallback<Map<Long,String>>() {
            @Override
            public void onSuccess(Map<Long, String> result) {
                warehouses = result;
            }
            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
            }
        });
        
        catalogService.getAllSources(new AsyncCallback<Map<Long,String>>() {
            @Override
            public void onSuccess(Map<Long, String> result) {
                sources = result;
            }
            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
            }
        });
        
        catalogService.getAllCategories(new AsyncCallback<List<String>>() {
            @Override
            public void onSuccess(List<String> result) {
                categories = result; 
            }
            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
            }
        });
        

        catalogService.getAllCategoriesMap(new AsyncCallback<Map<Long, String>>() {
            @Override
            public void onSuccess(Map<Long, String> result) {
                categoriesMap = result;
            }
            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
            }
        });
        
        catalogService.getAllBrands(new AsyncCallback<List<String>>() {
            @Override
            public void onSuccess(List<String> result) {
                brands = result;
            }
            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
            }
        });
        
        catalogService.getvoucherTypes(new AsyncCallback<Map<Long,String>>() {
            @Override
            public void onSuccess(Map<Long,String> result) {
                setVoucherType(result);
            }
            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
            }
        });
        


    }
    
    public static void setEntityIdMandatory(boolean entityIdMandatory) {
                Utils.entityIdMandatory = entityIdMandatory;
        }

        public static boolean isEntityIdMandatory() {
                return entityIdMandatory;
        }

        public static Map<Long, String> getAllVendors() {
        return vendors;
    }
        public static Map<Long, String> getAllInsurers() {
        return allInsurers;
    }

    public static Map<Long, String> getAllSources() {
        return sources;
    }
    
    public static Map<Long, String> getAllWarehouses() {
        return warehouses;
    }

    public static List<String> getAllCategories() {
        return categories;
    }
    

    public static Map<Long, String> getAllCategoriesMap() {
        return categoriesMap;
    }
    
    public static List<String> getAllBrands() {
        return brands;
    }
    
    public static String getVendorDesc(long id) {
        if(vendors == null) {
            return null;
        }
        return vendors.get(id);
    }
    
    public static List<ItemWarehouse> getignoredInventoryUpdateItemsIdsWarehouseIds(){
        return ignoredInventoryUpdateItemsIdsWarehouseIds;
    }
    
    public static String getSourceDesc(long id) {
        if(sources == null) {
            return null;
        }
        return sources.get(id);
    }
    
    public static String getWarehouseDesc(long id) {
        if(warehouses == null) {
            return null;
        }
        return warehouses.get(id);
    }
    
    public static long getVoucherTypeId(String voucherDesc) {
        if(voucherType == null) {
            return 0;
        }
        for(Entry<Long, String> v : voucherType.entrySet()) {
            if(v.getValue().equals(voucherDesc)) {
                return v.getKey();
            }
        }
        return 0;
    }
    
    public static String getDisplayableDate(Long millis){
        Date date = new Date();
        date.setTime(millis);
        String dateString = date.toString();
        dateString = dateString.substring(0, dateString.lastIndexOf(" "));
        dateString = dateString.substring(0, dateString.lastIndexOf(" "));
        return dateString;      
    }
    
    /**
     * Since in vendor combo box, vendor name is populated, this method is used to get Id from name
     * @param vendor name
     * @return vendor Id corresponding to the name
     */
    public static long getVendorId(String vendorDesc) {
        if(vendors == null) {
            return 0;
        }
        for(Entry<Long, String> v : vendors.entrySet()) {
            if(v.getValue().equals(vendorDesc)) {
                return v.getKey();
            }
        }
        return 0;
    }
    
    /**
     * Since in warehouse combo box, warehouse name is populated, this method is used to get Id from name
     * @param warehouse name
     * @return warehouse Id corresponding to the name
     */
    public static long getWarehouseId(String warehouseDesc) {
        if(warehouses == null) {
            return 0;
        }
        for(Entry<Long, String> w : warehouses.entrySet()) {
            if(w.getValue().equals(warehouseDesc)) {
                return w.getKey();
            }
        }
        return 0;
    }
    
    /**
     * Since in source combo box, source name is populated, this method is used to get Id from name
     * @param source name
     * @return source Id corresponding to the name
     */
    public static long getSourceId(String sourceDesc) {
        if(sources == null) {
            return 0;
        }
        for(Entry<Long, String> s : sources.entrySet()) {
            if(s.getValue().equals(sourceDesc)) {
                return s.getKey();
            }
        }
        return 0;
    }
    
    /**
     * validates item object. ProductGroup, brand, modelNumber, modelName should not be empty or null.
     * Also does item and vendor prices validations (MRP >= SP, MRP >= MOP, TP <= MOP)
     * Vendor Item Key should not be empty or null
     * @param item
     * @return true if validation is successful, false otherwise
     */
    public static boolean validateItem(Item item) {
        if(item.getProductGroup() == null || item.getProductGroup().isEmpty()) {
            Window.alert("Product Group cannot be empty.");
            return false;
        }
        if(item.getBrand() == null || item.getBrand().isEmpty()) {
            Window.alert("Brand cannot be empty.");
            return false;
        }
        if(item.getModelNumber() == null || item.getModelNumber().isEmpty()) {
            Window.alert("Model Number cannot be empty.");
            return false;
        }
        
        if(item.getSellingPrice() != null && item.getMrp() != null && item.getSellingPrice() > item.getMrp()) {
            Window.alert("Selling price cannot be more than MRP");
            return false;
        }
        if(item.getVendorPricesMap() != null && !item.getVendorPricesMap().isEmpty()) {
            for(VendorPricings v : item.getVendorPricesMap().values()) {
                if(item.getMrp() != null && item.getMrp() < v.getMop()) {
                    Window.alert("MRP cannot be less than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
                    return false;
                }
//                if(v.getTransferPrice() > v.getMop()) {
//                    Window.alert("Transfer Price cannot be more than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
//                    return false;
//                }
            }
        }
        if(item.getVendorKeysMap() != null && !item.getVendorKeysMap().isEmpty()) {
            for(VendorItemMapping v : item.getVendorKeysMap().values()) {
                if(v.getItemKey() == null || v.getItemKey().isEmpty()) {
                    Window.alert("Item Key cannot be empty. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
                    return false;
                }
            }
        }
        return true;
    }
    
    
    public static boolean validateStatusChange(int fromStatus, int toStatus) {
        switch(toStatus) {
        case PHASED_OUT: 
            switch(fromStatus) {
            case IN_PROCESS: return true;
            case CONTENT_COMPLETE: return true;
            case ACTIVE: return true;
            default: return false;
            }
        case IN_PROCESS:
            switch(fromStatus) {
            case PHASED_OUT: return true;
            default: return false;
            }
        case PAUSED:
            switch(fromStatus) {
            case ACTIVE: return true;
            default: return false;
            }
        case ACTIVE:
            switch(fromStatus) {
            case PAUSED: return true;
            default: return false;
            }
        }
        return true;
    }

        public static void setVoucherType(Map<Long, String> voucherType) {
                Utils.voucherType = voucherType;
        }

        public static Map<Long, String> getVoucherType() {
                return voucherType;
        }
        
        public static Map<Long, String> getStateMap() {
                return stateMap;
        }
}