Subversion Repositories SmartDukaan

Rev

Rev 2066 | Rev 2105 | 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 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_MOP = 2, TABLE_INDEX_DP = 3, TABLE_INDEX_TP = 4;
    private final int TABLE_INDEX_WAREHOUSE_ID = 0, TABLE_INDEX_WAREHOUSE_DESC = 1, TABLE_INDEX_WAREHOUSE_INV = 2;
    private Item newItem;

    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;

    @UiField ItemDetailStyle style;
    @UiField Label itemId;
    @UiField TextBox productGroup, brand, modelNumber, modelName, color;
    @UiField Label category, catalogItemId;
    @UiField TextBox comments;
    @UiField TextBox sellingPrice, mrp, /*mop, dealerPrice, transferPrice,*/ weight;
    @UiField Label addedOn, /*startDate,*/ retireDate, updatedOn;
    @UiField Label itemStatus;
    @UiField TextBox bestDealsText, bestDealsValue; 
    @UiField FlexTable headerAvailability, availabilityTable;
    @UiField FlexTable headerPricing, pricingTable;
    @UiField TextBox bestSellingRank;
    @UiField CheckBox defaultForEntity;
    @UiField Button submit;
    @UiField DateBox startDate;

    public ItemDetails(Item item){
        initWidget(uiBinder.createAndBindUi(this));
        initAvailabilityHeader();
        initPricingHeader();
        setItemDetails(item);
    }

    public ItemDetails() {
        this.item = null;
        initWidget(uiBinder.createAndBindUi(this));
        initAvailabilityHeader();
        initPricingHeader();
    }

    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());

        category.setText(item.getCategory()+"");
        comments.setText(item.getComments());
        catalogItemId.setText(item.getCatalogItemId() + "");

        mrp.setText(item.getMrp()+"");
        sellingPrice.setText(item.getSellingPrice()+"");
        /*mop.setText(item.getMop()+"");
                dealerPrice.setText(item.getDealerPrice()+"");
                transferPrice.setText(item.getTransferPrice()+"");*/
        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.getVendorPricings());
    }

    private void initAvailabilityHeader(){
        // Initialize the header.
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_ID, "128px");
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_DESC, "128px");
        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.
        headerPricing.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
        headerPricing.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
        headerPricing.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
        headerPricing.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
        headerPricing.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");

        headerPricing.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
        headerPricing.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor Desc");
        headerPricing.setText(0, TABLE_INDEX_MOP, "MOP");
        headerPricing.setText(0, TABLE_INDEX_DP, "Dealer Price");
        headerPricing.setText(0, TABLE_INDEX_TP, "Transfer Price");
    }

    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, "128px");
        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, double[]> pricings){
        pricingTable.removeAllRows();
        if(pricings == null || pricings.isEmpty()) {
            return;
        }
        pricingTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
        pricingTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
        pricingTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
        pricingTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
        pricingTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");


        int i=0;
        for(Entry<Long, double[]> pricing : pricings.entrySet()){
            pricingTable.setText(i, TABLE_INDEX_VENDORID, pricing.getKey() + "");
            pricingTable.setText(i, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(pricing.getKey()));
            pricingTable.setText(i, TABLE_INDEX_MOP, pricing.getValue()[Item.INDEX_MOP] + "");
            pricingTable.setText(i, TABLE_INDEX_DP, pricing.getValue()[Item.INDEX_DP] + "");
            pricingTable.setText(i, TABLE_INDEX_TP, pricing.getValue()[Item.INDEX_TP] + "");
            Button editButton = new Button("Edit");
            pricingTable.setWidget(i, TABLE_INDEX_TP + 1, editButton);
            i++;
            editButton.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    Cell cell = pricingTable.getCellForEvent(event);
                    int row = cell.getRowIndex();
                    long vendorId = Long.parseLong(pricingTable.getText(row, TABLE_INDEX_VENDORID));
                    updateVendorPrices(vendorId, row);
                }
            });
        }
    }


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

    @UiHandler("submit")
    void onSubmitPressed(ClickEvent clickEvent) {
        //long id = Long.parseLong(this.itemId.getText());
        try {
            updateItem();
            if(!isItemChanged()) {
                Window.alert("Nothing to update. Please change intended item parameters and try again.");
                return;
            }
            
            if(!validatePrices()) {
                return;
            }
            //updateItem();
        } 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.setCategory(Long.parseLong(category.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, double[]> vendorPrices = new HashMap<Long, double[]>();
        double[] arr;
        for(int row = 0; row < pricingTable.getRowCount(); row++) {
            arr = new double[3];
            arr[Item.INDEX_MOP] = Double.parseDouble(pricingTable.getText(row, TABLE_INDEX_MOP));
            arr[Item.INDEX_DP] = Double.parseDouble(pricingTable.getText(row, TABLE_INDEX_DP));
            arr[Item.INDEX_TP] = Double.parseDouble(pricingTable.getText(row, TABLE_INDEX_TP));
            vendorPrices.put(Long.parseLong(pricingTable.getText(row, TABLE_INDEX_VENDORID)), arr);
            newItem.setMop(arr[Item.INDEX_MOP]);
            newItem.setDealerPrice(arr[Item.INDEX_DP]);
            newItem.setTransferPrice(arr[Item.INDEX_TP]);
        }
        newItem.setVendorPricings(vendorPrices);
        
        /*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 = pricingTable.getText(row, TABLE_INDEX_MOP);
        String dp = pricingTable.getText(row, TABLE_INDEX_DP);
        String tp = pricingTable.getText(row, TABLE_INDEX_TP);
        VendorPricesDialog pricesDialog = new VendorPricesDialog(mop, dp, tp);
        pricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
            @Override
            public void onPriceUpdate(double mop, double dp, double tp) {
                pricingTable.setText(row, TABLE_INDEX_MOP, mop + "");
                pricingTable.setText(row, TABLE_INDEX_DP, dp + "");
                pricingTable.setText(row, TABLE_INDEX_TP, 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);*/
            }
        });
        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;
        }
                
        double[] vendorPrices;
        long vendorId;
        for(int row = 0; row < pricingTable.getRowCount(); row++) {
            vendorId = Long.parseLong(pricingTable.getText(row, TABLE_INDEX_VENDORID));
            vendorPrices = item.getVendorPricings().get(vendorId);
            if(vendorPrices[Item.INDEX_MOP] != Double.parseDouble(pricingTable.getText(row, TABLE_INDEX_MOP))) {
                return true;
            }
            if(vendorPrices[Item.INDEX_DP] != Double.parseDouble(pricingTable.getText(row, TABLE_INDEX_DP))) {
                return true;
            }
            if(vendorPrices[Item.INDEX_TP] != Double.parseDouble(pricingTable.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(Entry<Long, double[]> v : newItem.getVendorPricings().entrySet()) {
            if(newItem.getMrp() < v.getValue()[Item.INDEX_MOP]) {
                Window.alert("MRP cannot be less than MOP. Vendor: " + v.getKey());
                return false;
            }
            if(v.getValue()[Item.INDEX_TP] > v.getValue()[Item.INDEX_MOP]) {
                Window.alert("Transfer Price cannot be more than MOP. Vendor: " + v.getKey());
                return false;
            }
        }
        return true;
    }

    /*
        @UiHandler("sellingPrice")
        void onSPUpdate(KeyPressEvent keyPressEvent) {
            if(Double.parseDouble(sellingPrice.getText()) != item.getSellingPrice()) {
                sellingPrice.getElement().addClassName(style.fieldChanged());
            } else {
                sellingPrice.getElement().addClassName(style.greenLabel());
            }
        }

        @UiHandler("mop")
    void onMOPUpdate(KeyPressEvent keyPressEvent) {
        if(Double.parseDouble(mop.getText()) != item.getMop()) {
            mop.getElement().addClassName(style.fieldChanged());
        } else {
            mop.getElement().addClassName(style.greenLabel());
        }
    }

        @UiHandler("dealerPrice")
    void onDPUpdate(KeyPressEvent keyPressEvent) {
        if(Double.parseDouble(dealerPrice.getText()) != item.getDealerPrice()) {
            dealerPrice.getElement().addClassName(style.fieldChanged());
        } else {
            dealerPrice.getElement().addClassName(style.greenLabel());
        }
    }

        @UiHandler("transferPrice")
    void onTPUpdate(KeyPressEvent keyPressEvent) {
        if(Double.parseDouble(transferPrice.getText()) != item.getTransferPrice()) {
            transferPrice.getElement().addClassName(style.fieldChanged());
        } else {
            transferPrice.getElement().addClassName(style.greenLabel());
        }
    }

        @UiHandler("weight")
    void onWeightUpdate(KeyPressEvent keyPressEvent) {
        if(Double.parseDouble(weight.getText()) != item.getWeight()) {
            weight.getElement().addClassName(style.fieldChanged());
        } else {
            weight.getElement().addClassName(style.greenLabel());
        }
    }

        void applyDefaultStyles() {
            sellingPrice.getElement().addClassName(style.greenLabel());
            mop.getElement().addClassName(style.greenLabel());
            dealerPrice.getElement().addClassName(style.greenLabel());
            transferPrice.getElement().addClassName(style.greenLabel());
            weight.getElement().addClassName(style.greenLabel());
        }*/

}