Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.catalog.dashboard.client;

import in.shop2020.catalog.dashboard.shared.Item;
import in.shop2020.catalog.dashboard.shared.Utils;
import in.shop2020.catalog.dashboard.shared.VendorDetails;

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

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class ItemDetails extends ResizeComposite {

    private final int TABLE_INDEX_VENDORID = 0, TABLE_INDEX_VENDOR_DESC = 1, 
        TABLE_INDEX_ITEM_KEY = 2, TABLE_INDEX_MOP = 3, TABLE_INDEX_DP = 4, TABLE_INDEX_TP = 5;
    private final int TABLE_INDEX_WAREHOUSE_ID = 0, TABLE_INDEX_WAREHOUSE_DESC = 1, TABLE_INDEX_WAREHOUSE_INV = 2;

    interface ItemDetailsUiBinder extends UiBinder<Widget, ItemDetails> {}
    private static ItemDetailsUiBinder uiBinder = GWT.create(ItemDetailsUiBinder.class);

    public interface ItemUpdateListener {
        void onItemUpdate(Item item);
    }

    interface ItemDetailStyle extends CssResource{
        String greenLabel();
        String fieldChanged();
    }

    private ItemUpdateListener itemUpdateListener;

    private Item item, newItem;

    @UiField ItemDetailStyle style;
    @UiField Label itemId;
    @UiField TextBox productGroup, brand, modelNumber, modelName, color;
    @UiField Label contentCategory, catalogItemId;
    @UiField TextBox comments;
    @UiField TextBox sellingPrice, mrp, weight;
    @UiField Label addedOn, retireDate, updatedOn;
    @UiField Label itemStatus;
    @UiField TextBox bestDealsText, bestDealsValue; 
    @UiField FlexTable headerAvailability, availabilityTable;
    @UiField FlexTable headerVendorM, vendorTableM;
    @UiField FlexTable headerVendor, vendorTable;
    @UiField TextBox bestSellingRank;
    @UiField CheckBox defaultForEntity;
    @UiField Button submit;
    @UiField DateBox startDate;

    public ItemDetails(Item item){
        this();
        setItemDetails(item);
    }

    public ItemDetails() {
        initWidget(uiBinder.createAndBindUi(this));
        initAvailabilityHeader();
        initPricingHeader();
        headerAvailability.setVisible(false);
        availabilityTable.setVisible(false);
    }

    public void setItemDetails(Item item){
        this.item = item;
        itemId.setText(item.getId()+"");
        productGroup.setText(item.getProductGroup());
        brand.setText(item.getBrand());
        modelNumber.setText(item.getModelNumber());
        modelName.setText(item.getModelName());
        color.setText(item.getColor());

        contentCategory.setText(item.getContentCategory()+"");
        comments.setText(item.getComments());
        catalogItemId.setText(item.getCatalogItemId() + "");

        mrp.setText(item.getMrp()+"");
        sellingPrice.setText(item.getSellingPrice()+"");
        weight.setText(item.getWeight()+"");
        
        startDate.setValue(new Date(item.getStartDate()));
        
        addedOn.setText(Utils.getDisplayableDate(item.getAddedOn()));
        retireDate.setText(Utils.getDisplayableDate(item.getRetireDate()));
        updatedOn.setText(Utils.getDisplayableDate(item.getUpdatedOn()));

        bestDealsText.setText(item.getBestDealsText());
        bestDealsValue.setText(item.getBestDealsValue()+"");

        bestSellingRank.setText(item.getBestSellingRank()+"");
        defaultForEntity.setValue(item.isDefaultForEntity());
        
        itemStatus.setText(item.getItemStatus());

        updateAvailabilityTable(item.getAvailability());

        updatePricingTable(item.getVendorDetails());
    }

    private void initAvailabilityHeader(){
        // Initialize the header.
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_ID, "128px");
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_DESC, "300px");
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_INV, "128px");
        
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_ID, "Warehouse Id");
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_DESC, "Warehouse Desc");
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_INV, "Availability");

    }

    private void initPricingHeader(){
        // Initialize the header.
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "250px");
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");

        headerVendor.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
        headerVendor.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor Desc");
        headerVendor.setText(0, TABLE_INDEX_ITEM_KEY, "Item Key");
        headerVendor.setText(0, TABLE_INDEX_MOP, "MOP");
        headerVendor.setText(0, TABLE_INDEX_DP, "Dealer Price");
        headerVendor.setText(0, TABLE_INDEX_TP, "Transfer Price");
        
        Button addButton = new Button("Add");
        headerVendor.setWidget(0, TABLE_INDEX_TP + 1, addButton);
        addButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                VendorPricesDialog vendorDialog = new VendorPricesDialog();
                vendorDialog.updateButton.setText("Add");
                vendorDialog.setVendorDetailsUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
                    @Override
                    public boolean onUpdate(String key, double mop, double dp, double tp, long vendorId) {
                        if(!vendorExists(vendorId)) {
                            Window.alert("Vendor already exists");
                            return false;
                        }
                        if(!validateVendorDetails(key, mop, dp, tp)) {
                            return false;
                        }
                        int row = vendorTable.getRowCount();
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "250px");
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
                        
                        vendorTable.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
                        vendorTable.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
                        vendorTable.setText(row, TABLE_INDEX_ITEM_KEY, key);
                        vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
                        vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
                        vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
                        return true;
                    }
                });
                vendorDialog.show();
            }
        });
    }

    
    private void updateAvailabilityTable(Map<Long, Long> availabilityMap){
        availabilityTable.removeAllRows();
        if(availabilityMap == null) {
            return;
        }
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_ID, "128px");
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_DESC, "300px");
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_INV, "128px");


        int i=0;
        for(Entry<Long, Long> availability : availabilityMap.entrySet()){
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_ID, availability.getKey() + "");
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_DESC, Utils.getWarehouseDesc(availability.getKey()));
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_INV, availability.getValue() + "");
            i++;
        }
    }

    private void updatePricingTable(Map<Long, VendorDetails> vendorDetailsMap){
        vendorTable.removeAllRows();
        
        if(vendorDetailsMap == null || vendorDetailsMap.isEmpty()) {
            return;
        }
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "250px");
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");


        int i=0;
        for(VendorDetails vendorDetail : vendorDetailsMap.values()){
            vendorTable.setText(i, TABLE_INDEX_VENDORID, vendorDetail.getVendorId() + "");
            vendorTable.setText(i, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorDetail.getVendorId()));
            vendorTable.setText(i, TABLE_INDEX_ITEM_KEY, vendorDetail.getItemKey());
            vendorTable.setText(i, TABLE_INDEX_MOP, vendorDetail.getMop() + "");
            vendorTable.setText(i, TABLE_INDEX_DP, vendorDetail.getDealerPrice() + "");
            vendorTable.setText(i, TABLE_INDEX_TP, vendorDetail.getTransferPrice() + "");
            Button editButton = new Button("Edit");
            vendorTable.setWidget(i, TABLE_INDEX_TP + 1, editButton);
            i++;
            editButton.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    Cell cell = vendorTable.getCellForEvent(event);
                    int row = cell.getRowIndex();
                    long vendorId = Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID));
                    updateVendorPrices(vendorId, row);
                }
            });
        }
    }

    public void setItemUpdateListener(ItemUpdateListener itemUpdatelistener) {
        this.itemUpdateListener = itemUpdatelistener;
    }

    @UiHandler("submit")
    void onSubmitPressed(ClickEvent clickEvent) {
        try {
            updateItem();
            if(!isItemChanged()) {
                Window.alert("Nothing to update. Please change intended item parameters and try again.");
                return;
            }
            
            if(!validatePrices()) {
                return;
            }
            //updateItem();  -- Calling this method above. A new item is created with updated data.
        } catch(NumberFormatException ex) {
            return;
        }
        itemUpdateListener.onItemUpdate(newItem);
        GWT.log("Item updated. Id = " + item.getId());
        item = newItem;
    }

    private void updateItem() throws NumberFormatException {
        newItem = new Item();
        newItem.setId(Long.parseLong(itemId.getText()));
        newItem.setProductGroup(productGroup.getText().trim());
        newItem.setBrand(brand.getText().trim());
        newItem.setModelNumber(modelNumber.getText().trim());
        newItem.setModelName(modelName.getText().trim());
        newItem.setColor(color.getText().trim());
        newItem.setContentCategory(Long.parseLong(contentCategory.getText()));
        newItem.setComments(comments.getText().trim());
        newItem.setCatalogItemId(Long.parseLong(catalogItemId.getText()));
        newItem.setFeatureId(item.getFeatureId());
        newItem.setFeatureDescription(item.getFeatureDescription());
        try {
            newItem.setMrp(Double.parseDouble(mrp.getText().trim()));
        } catch(NumberFormatException ex) {
            Window.alert("Invalid MRP format");
            throw new NumberFormatException();
        }
        try {
            newItem.setSellingPrice(Double.parseDouble(sellingPrice.getText().trim()));
        }
        catch(NumberFormatException ex) {
            Window.alert("Invalid Selling Price format");
            throw new NumberFormatException();
        }
        try {
            newItem.setWeight(Double.parseDouble(weight.getText().trim()));
        } catch(NumberFormatException ex) {
            Window.alert("Invalid Weight format");
            throw new NumberFormatException();
        }
        newItem.setAddedOn(item.getAddedOn());
        newItem.setStartDate(startDate.getValue().getTime());
        newItem.setRetireDate(item.getRetireDate());
        newItem.setUpdatedOn(item.getUpdatedOn());
        newItem.setItemStatus(item.getItemStatus());
        newItem.setOtherInfo(item.getOtherInfo());
        newItem.setBestDealsText(bestDealsText.getText().trim());
        try {
            newItem.setBestDealsValue(Double.parseDouble(bestDealsValue.getText().trim()));
        } catch(NumberFormatException ex) {
            Window.alert("Invalid Best Deals Value format");
            throw new NumberFormatException();
        }
        try {
            newItem.setBestSellingRank(Long.parseLong(bestSellingRank.getText().trim()));
        } catch (NumberFormatException ex) {
            Window.alert("Invalid Best Selling Rank format");
            throw new NumberFormatException();
        }

        newItem.setDefaultForEntity(defaultForEntity.getValue());
        newItem.setAvailability(item.getAvailability());
        Map<Long, VendorDetails> vendorDetails = new HashMap<Long, VendorDetails>();
        VendorDetails v;
        for(int row = 0; row < vendorTable.getRowCount(); row++) {
            v = new VendorDetails();
            v.setItemKey(vendorTable.getText(row, TABLE_INDEX_ITEM_KEY));
            v.setMop(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP)));
            v.setDealerPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP)));
            v.setTransferPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP)));
            v.setVendorId(Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID)));
            vendorDetails.put(v.getVendorId(), v);
            newItem.setMop(v.getMop());
            newItem.setDealerPrice(v.getDealerPrice());
            newItem.setTransferPrice(v.getTransferPrice());
        }
        newItem.setVendorDetails(vendorDetails);
        
        /*item.setProductGroup(productGroup.getText().trim());
        item.setBrand(brand.getText().trim());
        item.setModelName(modelName.getText().trim());
        item.setModelNumber(modelNumber.getText().trim());
        item.setColor(color.getText().trim());
        item.setComments(comments.getText().trim());
        item.setMrp(Double.parseDouble(this.mrp.getText()));
        item.setSellingPrice(Double.parseDouble(this.sellingPrice.getText()));
        item.setWeight(Double.parseDouble(this.weight.getText()));
        item.setBestDealsText(bestDealsText.getText().trim());
        item.setBestDealsValue(Double.parseDouble(bestDealsValue.getText().trim()));
        item.setBestSellingRank(Long.parseLong(bestSellingRank.getText().trim()));
        item.setDefaultForEntity(defaultForEntity.getValue());
        for(int row = 0; row < pricingTable.getRowCount(); row++) {
            long vendorId = Long.parseLong(pricingTable.getText(row, TABLE_INDEX_VENDORID));
            double mop = Double.parseDouble(pricingTable.getText(row, TABLE_INDEX_MOP));
            double dp = Double.parseDouble(pricingTable.getText(row, TABLE_INDEX_DP));
            double tp = Double.parseDouble(pricingTable.getText(row, TABLE_INDEX_TP));
            item.getVendorPricings().get(vendorId)[Item.INDEX_MOP] = mop;
            item.getVendorPricings().get(vendorId)[Item.INDEX_DP] = dp;
            item.getVendorPricings().get(vendorId)[Item.INDEX_TP] = tp;
            item.setMop(mop);
            item.setDealerPrice(dp);
            item.setTransferPrice(tp);
        }*/
    }

    private void updateVendorPrices(final long vendorId, final int row) {
        String mop = vendorTable.getText(row, TABLE_INDEX_MOP);
        String dp = vendorTable.getText(row, TABLE_INDEX_DP);
        String tp = vendorTable.getText(row, TABLE_INDEX_TP);
        String key = vendorTable.getText(row, TABLE_INDEX_ITEM_KEY);
        VendorPricesDialog pricesDialog = new VendorPricesDialog(key, mop, dp, tp);
        pricesDialog.updateButton.setText("Update");
        pricesDialog.setVendorDetailsUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
            @Override
            public boolean onUpdate(String itemKey, double mop, double dp, double tp, long vendorId) {
                if(!validateVendorDetails(itemKey, mop, dp, tp)) {
                    return false;
                }
                vendorTable.setText(row, TABLE_INDEX_ITEM_KEY, itemKey);
                vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
                vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
                vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
                return true;
            }
        });
        pricesDialog.show();
    }

    private boolean isItemChanged() {
        if(!productGroup.getText().trim().equals(item.getProductGroup())) {
            return true;
        }
        if(!brand.getText().trim().equals(item.getBrand())) {
            return true;
        }
        if(!modelNumber.getText().trim().equals(item.getModelNumber())) {
            return true;
        }
        if(!modelName.getText().trim().equals(item.getModelName())) {
            return true;
        }
        if(!color.getText().trim().equals(item.getColor())) {
            return true;
        }
        if(!comments.getText().trim().equals(item.getComments())) {
            return true;
        }
        try {
            if(item.getMrp() != Double.parseDouble(mrp.getText().trim())) {
                return true;
            }
        } catch(NumberFormatException ex) {
            Window.alert("Invalid MRP format");
            throw new NumberFormatException();
        }
        try {
            if(item.getSellingPrice() != Double.parseDouble(sellingPrice.getText().trim())) {
                return true;
            }
        } catch(NumberFormatException ex) {
            Window.alert("Invalid Selling Price format");
            throw new NumberFormatException();
        }
        try {
            if(item.getWeight() != Double.parseDouble(weight.getText().trim())) {
                return true;
            }
        } catch(NumberFormatException ex) {
            Window.alert("Invalid Weight format");
            throw new NumberFormatException();
        }
        if(!bestDealsText.getText().trim().equals(item.getBestDealsText())) {
            return true;
        }
        try {
            if(item.getBestDealsValue() != Double.parseDouble(bestDealsValue.getText().trim())) {
                return true;
            }
        } catch(NumberFormatException ex) {
            Window.alert("Invalid Best Deals Value format");
            throw new NumberFormatException();
        }
        try{
            if(item.getBestSellingRank() != Long.parseLong(bestSellingRank.getText().trim())) {
                return true;
            }
        } catch (NumberFormatException ex) {
            Window.alert("Invalid Best Selling Rank format");
            throw new NumberFormatException();
        }
        if(item.isDefaultForEntity() != defaultForEntity.getValue()) {
            return true;
        }
        
        if(newItem.getStartDate() != item.getStartDate()) {
            return true;
        }
                
        VendorDetails vendorDetails;
        long vendorId;
        for(int row = 0; row < vendorTable.getRowCount(); row++) {
            vendorId = Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID));
            vendorDetails = item.getVendorDetails().get(vendorId);
            if(vendorDetails.getMop() != Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP))) {
                return true;
            }
            if(vendorDetails.getDealerPrice() != Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP))) {
                return true;
            }
            if(vendorDetails.getTransferPrice() != Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP))) {
                return true;
            }
        }
        return false;
    }
    
    private boolean validatePrices() {
        if(newItem.getSellingPrice() > newItem.getMrp()) {
            Window.alert("Selling price cannot be more than MRP");
            return false;
        }
        for(VendorDetails v : newItem.getVendorDetails().values()) {
            if(newItem.getMrp() < v.getMop()) {
                Window.alert("MRP cannot be less than MOP. Vendor: " + v.getVendorId());
                return false;
            }
            if(v.getTransferPrice() > v.getMop()) {
                Window.alert("Transfer Price cannot be more than MOP. Vendor: " + v.getVendorId());
                return false;
            }
        }
        return true;
    }
    
    private boolean validateVendorDetails(String key, double mop, double dp, double tp) {
        if(key == null || key.isEmpty()) {
            Window.alert("Item key cannot be empty");
            return false;
        }
        if(item.getMrp() < mop) {
            Window.alert("MOP cannot be more than MRP.");
            return false;
        }
        if(tp > mop) {
            Window.alert("Transfer Price cannot be more than MOP.");
            return false;
        }
        return true;
    }
    
    public long getItemId() {
        return item == null ? 0 : item.getId();
    }
    
    public Item getItem() {
        return item;
    }
    
    private boolean vendorExists(long vendorId) {
        long id;
        for(int i = 0; i < vendorTable.getRowCount(); i++) {
            id = Long.parseLong(vendorTable.getText(i, TABLE_INDEX_VENDORID));
            if(vendorId == id) {
                return false;
            }
        }
        return true;
    }
}