Subversion Repositories SmartDukaan

Rev

Rev 6099 | Rev 6241 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1961 ankur.sing 1
package in.shop2020.catalog.dashboard.client;
2
 
3
import in.shop2020.catalog.dashboard.shared.Item;
4431 phani.kuma 4
import in.shop2020.catalog.dashboard.shared.ItemInventory;
3558 rajveer 5
import in.shop2020.catalog.dashboard.shared.SourcePricings;
2066 ankur.sing 6
import in.shop2020.catalog.dashboard.shared.Utils;
2119 ankur.sing 7
import in.shop2020.catalog.dashboard.shared.VendorItemMapping;
8
import in.shop2020.catalog.dashboard.shared.VendorPricings;
5504 phani.kuma 9
import in.shop2020.catalog.dashboard.shared.VoucherItemMapping;
1961 ankur.sing 10
 
5427 amit.gupta 11
import java.util.ArrayList;
1961 ankur.sing 12
import java.util.Date;
2066 ankur.sing 13
import java.util.HashMap;
5427 amit.gupta 14
import java.util.Iterator;
15
import java.util.List;
1992 ankur.sing 16
import java.util.Map;
17
import java.util.Map.Entry;
1961 ankur.sing 18
 
19
import com.google.gwt.core.client.GWT;
5427 amit.gupta 20
import com.google.gwt.dom.client.Style.Unit;
1961 ankur.sing 21
import com.google.gwt.event.dom.client.ClickEvent;
1992 ankur.sing 22
import com.google.gwt.event.dom.client.ClickHandler;
23
import com.google.gwt.resources.client.CssResource;
1961 ankur.sing 24
import com.google.gwt.uibinder.client.UiBinder;
25
import com.google.gwt.uibinder.client.UiField;
5427 amit.gupta 26
import com.google.gwt.uibinder.client.UiHandler;
1961 ankur.sing 27
import com.google.gwt.user.client.Window;
2126 ankur.sing 28
import com.google.gwt.user.client.rpc.AsyncCallback;
1961 ankur.sing 29
import com.google.gwt.user.client.ui.Button;
2066 ankur.sing 30
import com.google.gwt.user.client.ui.CheckBox;
5427 amit.gupta 31
import com.google.gwt.user.client.ui.DialogBox;
1992 ankur.sing 32
import com.google.gwt.user.client.ui.FlexTable;
33
import com.google.gwt.user.client.ui.HTMLTable.Cell;
1961 ankur.sing 34
import com.google.gwt.user.client.ui.Label;
4506 phani.kuma 35
import com.google.gwt.user.client.ui.ListBox;
1961 ankur.sing 36
import com.google.gwt.user.client.ui.ResizeComposite;
37
import com.google.gwt.user.client.ui.TextBox;
5427 amit.gupta 38
import com.google.gwt.user.client.ui.VerticalPanel;
1961 ankur.sing 39
import com.google.gwt.user.client.ui.Widget;
2068 ankur.sing 40
import com.google.gwt.user.datepicker.client.DateBox;
1961 ankur.sing 41
 
2427 ankur.sing 42
/**
43
 * Panel contains fields for item details. Some of these fields are editable.
44
 * It also contains vendor item pricings, vendor item keys, and item availability in tabular format.
45
 * Item availability is made invisible for time being 
46
 */
5217 amit.gupta 47
public class ItemDetails extends ResizeComposite implements ComingSoon {
1961 ankur.sing 48
 
2359 ankur.sing 49
    private final int TABLE_INDEX_MAPPING_VENDOR_DESC = 0, 
50
                      TABLE_INDEX_MAPPING_ITEM_KEY = 1,
51
                      TABLE_INDEX_MAPPING_BUTTON = 2,
52
                      TABLE_INDEX_MAPPING_VENDORID = 3, 
53
                      TABLE_INDEX_MAPPING_ITEM_KEY_OLD = 4;
54
 
55
    private final int TABLE_INDEX_PRICING_VENDOR_DESC = 0, 
3558 rajveer 56
    				  TABLE_INDEX_PRICING_MOP = 1,
2359 ankur.sing 57
                      TABLE_INDEX_PRICING_DP = 2,
58
                      TABLE_INDEX_PRICING_TP = 3,
59
                      TABLE_INDEX_PRICING_BUTTON = 4,
60
                      TABLE_INDEX_PRICING_VENDORID = 5;
3558 rajveer 61
 
62
    private final int TABLE_INDEX_SOURCE_PRICING_SOURCE_DESC = 0, 
63
    				  TABLE_INDEX_SOURCE_PRICING_MRP = 1,
64
    				  TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE = 2,
65
    				  TABLE_INDEX_SOURCE_PRICING_BUTTON = 3,
66
    				  TABLE_INDEX_SOURCE_PRICING_SOURCE_ID = 4;
67
 
2119 ankur.sing 68
    private final int TABLE_INDEX_WAREHOUSE_ID = 0,
69
                      TABLE_INDEX_WAREHOUSE_DESC = 1,
4431 phani.kuma 70
                      TABLE_INDEX_WAREHOUSE_INVA = 2,
71
                      TABLE_INDEX_WAREHOUSE_INVR = 3,
72
                      TABLE_INDEX_WAREHOUSE_GET_BUTTON = 4,
73
                      TABLE_INDEX_WAREHOUSE_UPDATE_BUTTON = 5;
2359 ankur.sing 74
 
4423 phani.kuma 75
    private final int TABLE_INDEX_SIMILAR_ITEMS_CATALOG_ITEM_ID = 0,
76
                      TABLE_INDEX_SIMILAR_ITEMS_PRODUCT_GROUP = 1,
77
                      TABLE_INDEX_SIMILAR_ITEMS_BRAND = 2,
78
                      TABLE_INDEX_SIMILAR_ITEMS_MODEL_NUMBER = 3,
79
                      TABLE_INDEX_SIMILAR_ITEMS_MODEL_NAME = 4,
80
                      TABLE_INDEX_SIMILAR_ITEMS_CATEGORY = 5,
81
                      TABLE_INDEX_SIMILAR_ITEMS_BUTTON = 6;
82
 
5504 phani.kuma 83
    private final int TABLE_INDEX_VOUCHERS_TYPE = 0,
84
                      TABLE_INDEX_VOUCHERS_AMOUNT = 1,
85
                      TABLE_INDEX_VOUCHERS_ADD_EDIT_BUTTON = 2,
86
                      TABLE_INDEX_VOUCHERS_DEL_BUTTON = 3;
87
 
5427 amit.gupta 88
    private final String MANDATORY_MRP = "MRP",
89
					  MANDATORY_WEIGHT = "Weight",
90
					  MANDATORY_BEST_DEAL_TEXT = "Best Deal Text",
91
					  MANDATORY_BRAND = "Brand",
92
					  MANDATORY_MODEL_NO = "Model Number",
93
					  MANDATORY_MODEL_NAME = "Model Name";
94
 
95
    private final String OPTIONAL_SELLING_PRICE = "Selling Price",	
96
    				  OPTIONAL_VENDOR_PRICING = "Vendor Pricing",
97
    				  OPTIONAL_PREFERRED_VENDOR = "Preferred Vendor",
98
    				  OPTIONAL_SOURCE_PRICING = "Source Pricing";
99
 
2359 ankur.sing 100
    private final String PRICE_WIDTH = "100px", VENDOR_DESC_WIDTH = "130px",
101
                         VENDOR_ID_WIDTH = "100px", ITEM_KEY_WIDTH = "200px",
102
                         BUTTON_WIDTH = "50px";
2066 ankur.sing 103
 
4649 phani.kuma 104
    private Map<String, String> ConfigMap;
2066 ankur.sing 105
    interface ItemDetailsUiBinder extends UiBinder<Widget, ItemDetails> {}
106
    private static ItemDetailsUiBinder uiBinder = GWT.create(ItemDetailsUiBinder.class);
2126 ankur.sing 107
    private final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
5427 amit.gupta 108
    Map<String, Object> mandatoryChangedValMap;
109
    Map<String, Object> optionalChangedValMap;
2066 ankur.sing 110
 
1961 ankur.sing 111
 
2066 ankur.sing 112
    interface ItemDetailStyle extends CssResource{
113
        String greenLabel();
114
        String fieldChanged();
115
    }
1961 ankur.sing 116
 
2105 ankur.sing 117
    private Item item, newItem;
2489 ankur.sing 118
    private CatalogDashboard catalogDashboardPanel;
2066 ankur.sing 119
 
120
    @UiField ItemDetailStyle style;
121
    @UiField Label itemId;
4762 phani.kuma 122
    @UiField TextBox brand, modelNumber, modelName, color;
123
    @UiField Label contentCategory, catalogItemId, productGroup;
2359 ankur.sing 124
    @UiField TextBox statusDesc, comments;
2105 ankur.sing 125
    @UiField TextBox sellingPrice, mrp, weight;
126
    @UiField Label addedOn, retireDate, updatedOn;
2066 ankur.sing 127
    @UiField Label itemStatus;
128
    @UiField TextBox bestDealsText, bestDealsValue; 
129
    @UiField FlexTable headerAvailability, availabilityTable;
2427 ankur.sing 130
    @UiField FlexTable headerVendorItemKey, tableVendorItemKey;
131
    @UiField FlexTable headerVendorPrices, tableVendorPrices;
3558 rajveer 132
    @UiField FlexTable headerSourcePrices, tableSourcePrices;
2066 ankur.sing 133
    @UiField TextBox bestSellingRank;
3359 chandransh 134
    @UiField TextBox expectedDelay;
5460 phani.kuma 135
    @UiField CheckBox defaultForEntity, risky, warehouseStickiness, itemType, hasItemNo, clearance;
5217 amit.gupta 136
    @UiField DateBox startDate, expectedArrivalDate, comingSoonStartDate;
4423 phani.kuma 137
    @UiField FlexTable headerSimilarItems, tableSimilarItems;
5384 phani.kuma 138
    @UiField ListBox preferredVendor;
5217 amit.gupta 139
    @UiField Button comingSoonButton;
5504 phani.kuma 140
    @UiField FlexTable headerVouchers, tableVouchers;
2066 ankur.sing 141
 
142
    public ItemDetails(Item item){
2105 ankur.sing 143
        this();
2066 ankur.sing 144
        setItemDetails(item);
145
    }
146
 
147
    public ItemDetails() {
148
        initWidget(uiBinder.createAndBindUi(this));
149
        initAvailabilityHeader();
2427 ankur.sing 150
        initVendorKeysHeader();
151
        initVendorPricingHeader();
3558 rajveer 152
        initSourcePricingHeader();
4423 phani.kuma 153
        initSimilarItemList();
5504 phani.kuma 154
        initVoucherHeader();
4506 phani.kuma 155
        preferredVendor.addItem("null");
4649 phani.kuma 156
        getConfigdataforPriceCompare();
2066 ankur.sing 157
    }
158
 
4649 phani.kuma 159
    public void getConfigdataforPriceCompare(){
160
    	catalogService.getConfigdataforPriceCompare(new AsyncCallback<Map<String,String>>() {
161
            @Override
162
            public void onSuccess(Map<String, String> result) {
163
            	ConfigMap = result;
164
            }
165
            @Override
166
            public void onFailure(Throwable caught) {
167
                caught.printStackTrace();
168
            }
169
        });
170
    }
171
 
2427 ankur.sing 172
    /**
173
     * Sets the UI fields with item object attributes
174
     * Also populates tables for vendor prices, keys and item availability
175
     * @param item
176
     */
2066 ankur.sing 177
    public void setItemDetails(Item item){
178
        this.item = item;
179
        itemId.setText(item.getId()+"");
180
        productGroup.setText(item.getProductGroup());
181
        brand.setText(item.getBrand());
182
        modelNumber.setText(item.getModelNumber());
183
        modelName.setText(item.getModelName());
184
        color.setText(item.getColor());
185
 
2359 ankur.sing 186
        statusDesc.setText(item.getItemStatusDesc());
2105 ankur.sing 187
        contentCategory.setText(item.getContentCategory()+"");
2066 ankur.sing 188
        comments.setText(item.getComments());
189
        catalogItemId.setText(item.getCatalogItemId() + "");
190
 
4583 phani.kuma 191
        preferredVendor.clear();
192
        int index = -1;
193
        if (item.getPreferredVendor() == null) {
194
        	index++;
195
        	preferredVendor.addItem("null");
196
        }
197
 
198
        if((item.getVendorPricesMap() == null || item.getVendorPricesMap().isEmpty())) {
199
        	if(item.getPreferredVendor() != null) {
200
        		index++;
201
        		preferredVendor.addItem(Utils.getVendorDesc(item.getPreferredVendor()));
202
        	}
203
        }
204
        else {
205
        	for(VendorPricings vendorDetail : item.getVendorPricesMap().values()){
206
        		index++;
207
        		preferredVendor.addItem(Utils.getVendorDesc(vendorDetail.getVendorId()));
208
        		if(item.getPreferredVendor() != null && item.getPreferredVendor() == vendorDetail.getVendorId()) {
4506 phani.kuma 209
	        		preferredVendor.setSelectedIndex(index);
210
	        	}
4583 phani.kuma 211
        	}
212
 
213
        	if(item.getPreferredVendor() != null && !item.getVendorPricesMap().containsKey(item.getPreferredVendor())){
214
        		index++;
215
            	preferredVendor.addItem(Utils.getVendorDesc(item.getPreferredVendor()));
216
            	preferredVendor.setSelectedIndex(index);
217
        	}
4506 phani.kuma 218
        }
5384 phani.kuma 219
 
2489 ankur.sing 220
        mrp.setText(item.getMrp() != null ? item.getMrp()+"" : "");
221
        sellingPrice.setText(item.getSellingPrice() != null ? item.getSellingPrice()+"" : "");
222
        weight.setText(item.getWeight() != null ? item.getWeight()+"" : "");
3359 chandransh 223
        expectedDelay.setValue(item.getExpectedDelay()+"");
4413 anupam.sin 224
        warehouseStickiness.setValue(item.isWarehouseStickiness());
2068 ankur.sing 225
 
5384 phani.kuma 226
        hasItemNo.setValue(item.isHasItemNo());
227
        itemType.setValue(item.isItemType());
5460 phani.kuma 228
        clearance.setValue(item.isClearance());
5384 phani.kuma 229
 
2068 ankur.sing 230
        startDate.setValue(new Date(item.getStartDate()));
5217 amit.gupta 231
        if(item.getComingSoonStartDate() != null){
232
        	comingSoonStartDate.setValue(new Date(item.getComingSoonStartDate()));
233
        }else {
234
        	comingSoonStartDate.setValue(null);
235
        }
236
 
237
        if(item.getExpectedArrivalDate() != null){
238
        	expectedArrivalDate.setValue(new Date(item.getExpectedArrivalDate()));
239
        }else {
240
        	expectedArrivalDate.setValue(null);
241
        }
2068 ankur.sing 242
        addedOn.setText(Utils.getDisplayableDate(item.getAddedOn()));
243
        retireDate.setText(Utils.getDisplayableDate(item.getRetireDate()));
244
        updatedOn.setText(Utils.getDisplayableDate(item.getUpdatedOn()));
1992 ankur.sing 245
 
2066 ankur.sing 246
        bestDealsText.setText(item.getBestDealsText());
2489 ankur.sing 247
        bestDealsValue.setText(item.getBestDealsValue() != null ? item.getBestDealsValue()+"" : "");
248
        bestSellingRank.setText(item.getBestSellingRank() != null ? item.getBestSellingRank()+"" : "");
2066 ankur.sing 249
        defaultForEntity.setValue(item.isDefaultForEntity());
2252 ankur.sing 250
        risky.setValue(item.isRisky());
1992 ankur.sing 251
 
2066 ankur.sing 252
        itemStatus.setText(item.getItemStatus());
253
 
4431 phani.kuma 254
        updateAvailabilityTable(item.getItemInventory());
2427 ankur.sing 255
        updateVendorKeysTable(item.getVendorKeysMap());
256
        updateVendorPricingTable(item.getVendorPricesMap());
3558 rajveer 257
        updateSourcePricingTable(item.getSourcePricesMap());
4423 phani.kuma 258
        updateSimilarItemsTable(item.getSimilarItems());
5504 phani.kuma 259
        updateVoucherTable(item.getVouchersMap());
1992 ankur.sing 260
    }
2066 ankur.sing 261
 
2427 ankur.sing 262
    /**
263
     * initialise item availability table header.
264
     */
2066 ankur.sing 265
    private void initAvailabilityHeader(){
4431 phani.kuma 266
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_ID, "100px");
2105 ankur.sing 267
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_DESC, "300px");
4431 phani.kuma 268
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_INVA, "100px");
269
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_INVR, "100px");
270
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_GET_BUTTON, "100px");
271
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_UPDATE_BUTTON, "100px");
2066 ankur.sing 272
 
273
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_ID, "Warehouse Id");
274
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_DESC, "Warehouse Desc");
4431 phani.kuma 275
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_INVA, "Availability");
276
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_INVR, "Reserved");
277
 
278
        Button getLiveDataButton = new Button("Get Live Data");
279
        headerAvailability.setWidget(0, TABLE_INDEX_WAREHOUSE_GET_BUTTON, getLiveDataButton);
280
        getLiveDataButton.addClickHandler(new ClickHandler() {
281
            @Override
282
            public void onClick(ClickEvent event) {
283
            	catalogService.getProdItemInventory(item.getId(), new AsyncCallback<Map<Long, ItemInventory>>() {
284
                    @Override
285
                    public void onSuccess(Map<Long, ItemInventory> itemInventoryMap) {
286
                        if(itemInventoryMap != null) {
287
                            GWT.log("Inventory Data fetched");
288
                            item.setItemInventory(itemInventoryMap);
289
                            updateAvailabilityTable(item.getItemInventory());
290
                        }
291
                        else {
292
                            GWT.log("Error fetching Inventory Data");
293
                            Window.alert("Error fetching Inventory Data");
294
                        }
295
                    }
296
                    @Override
297
                    public void onFailure(Throwable caught) {
298
                        caught.printStackTrace();
299
                        Window.alert("Error fetching Inventory Data");
300
                    }
301
                });
302
            }
303
        });
304
 
305
        Button updateLiveDataButton = new Button("Update Live Data");
306
        headerAvailability.setWidget(0, TABLE_INDEX_WAREHOUSE_UPDATE_BUTTON, updateLiveDataButton);
307
        /* code to be rewritten later
308
        updateLiveDataButton.addClickHandler(new ClickHandler() {
309
            @Override
310
            public void onClick(ClickEvent event) {
311
            	catalogService.getProdItemInventory(item.getId(), new AsyncCallback<Map<Long, ItemInventory>>() {
312
                    @Override
313
                    public void onSuccess(Map<Long, ItemInventory> itemInventoryMap) {
314
                        if(itemInventoryMap != null) {
315
                            GWT.log("Inventory Data fetched");
316
                            item.setItemInventory(itemInventoryMap);
317
                            updateAvailabilityTable(item.getItemInventory());
318
                        }
319
                        else {
320
                            GWT.log("Error fetching Inventory Data");
321
                            Window.alert("Error fetching Inventory Data");
322
                        }
323
                    }
324
                    @Override
325
                    public void onFailure(Throwable caught) {
326
                        caught.printStackTrace();
327
                        Window.alert("Error fetching Inventory Data");
328
                    }
329
                });
330
            }
331
        });
332
        */
333
        headerAvailability.getCellFormatter().setVisible(0, TABLE_INDEX_WAREHOUSE_UPDATE_BUTTON, false);
1992 ankur.sing 334
 
335
    }
2066 ankur.sing 336
 
2427 ankur.sing 337
    /**
338
     * initialises vendor item key table header. Creates an Add button and
339
     * adds click event listener to it to create and pop up a dialog for adding 
340
     * a new vendor item key. 
341
     */
342
    private void initVendorKeysHeader(){
343
        headerVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_VENDORID, VENDOR_ID_WIDTH);
344
        headerVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_VENDOR_DESC, VENDOR_DESC_WIDTH);
345
        headerVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_ITEM_KEY, ITEM_KEY_WIDTH);
346
        headerVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_ITEM_KEY_OLD, ITEM_KEY_WIDTH);
347
        headerVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_BUTTON, BUTTON_WIDTH);
2119 ankur.sing 348
 
2427 ankur.sing 349
        headerVendorItemKey.setText(0, TABLE_INDEX_MAPPING_VENDORID, "Vendor Id");
350
        headerVendorItemKey.setText(0, TABLE_INDEX_MAPPING_VENDOR_DESC, "Vendor");
351
        headerVendorItemKey.setText(0, TABLE_INDEX_MAPPING_ITEM_KEY, "Item Key");
352
        headerVendorItemKey.setText(0, TABLE_INDEX_MAPPING_ITEM_KEY_OLD, "Prev Item Key");
2119 ankur.sing 353
 
2427 ankur.sing 354
        headerVendorItemKey.getCellFormatter().setVisible(0, TABLE_INDEX_MAPPING_VENDORID, false);
355
        headerVendorItemKey.getCellFormatter().setVisible(0, TABLE_INDEX_MAPPING_ITEM_KEY_OLD, false);
2359 ankur.sing 356
 
2119 ankur.sing 357
        Button addButton = new Button("Add");
2427 ankur.sing 358
        headerVendorItemKey.setWidget(0, TABLE_INDEX_MAPPING_BUTTON, addButton);
2119 ankur.sing 359
        addButton.addClickHandler(new ClickHandler() {
360
            @Override
361
            public void onClick(ClickEvent event) {
2126 ankur.sing 362
                VendorMappingDialog vendorMappingDialog = new VendorMappingDialog(productGroup.getText().trim(), brand.getText().trim(), 
363
                        modelNumber.getText().trim(), color.getText().trim());
2119 ankur.sing 364
                vendorMappingDialog.updateButton.setText("Add");
365
                vendorMappingDialog.setVendorMappingUpdateListener(new VendorMappingDialog.VendorMappingUpdateListener() {
366
                    @Override
367
                    public boolean onUpdate(String key, long vendorId) {
2427 ankur.sing 368
                        int row = tableVendorItemKey.getRowCount();
369
                        tableVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_VENDORID, VENDOR_ID_WIDTH);
370
                        tableVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_VENDOR_DESC, VENDOR_DESC_WIDTH);
371
                        tableVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_ITEM_KEY, ITEM_KEY_WIDTH);
372
                        tableVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_ITEM_KEY_OLD, ITEM_KEY_WIDTH);
2119 ankur.sing 373
 
2427 ankur.sing 374
                        tableVendorItemKey.setText(row, TABLE_INDEX_MAPPING_VENDORID, vendorId + "");
375
                        tableVendorItemKey.setText(row, TABLE_INDEX_MAPPING_VENDOR_DESC, Utils.getVendorDesc(vendorId));
376
                        tableVendorItemKey.setText(row, TABLE_INDEX_MAPPING_ITEM_KEY, key);
377
                        tableVendorItemKey.setText(row, TABLE_INDEX_MAPPING_ITEM_KEY_OLD, key);
2359 ankur.sing 378
 
2427 ankur.sing 379
                        tableVendorItemKey.getCellFormatter().setVisible(row, TABLE_INDEX_MAPPING_VENDORID, false);
380
                        tableVendorItemKey.getCellFormatter().setVisible(row, TABLE_INDEX_MAPPING_ITEM_KEY_OLD, false);
2119 ankur.sing 381
                        return true;
382
                    }
383
                });
384
                vendorMappingDialog.show();
385
            }
386
        });
387
    }
388
 
2427 ankur.sing 389
    /**
390
     * initialises vendor prices table header. Creates an Add button and
391
     * adds click event listener to it to create and pop up a dialog for adding 
392
     * a prices for a new vendor 
393
     */
394
    private void initVendorPricingHeader(){
395
        headerVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_VENDORID, VENDOR_ID_WIDTH);
396
        headerVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_VENDOR_DESC, VENDOR_DESC_WIDTH);
397
        headerVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_MOP, PRICE_WIDTH);
398
        headerVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_DP, PRICE_WIDTH);
399
        headerVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_TP, PRICE_WIDTH);
400
        headerVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_BUTTON, BUTTON_WIDTH);
2066 ankur.sing 401
 
2427 ankur.sing 402
        headerVendorPrices.setText(0, TABLE_INDEX_PRICING_VENDORID, "Vendor Id");
403
        headerVendorPrices.setText(0, TABLE_INDEX_PRICING_VENDOR_DESC, "Vendor");
404
        headerVendorPrices.setText(0, TABLE_INDEX_PRICING_MOP, "MOP");
405
        headerVendorPrices.setText(0, TABLE_INDEX_PRICING_DP, "Dealer Price");
406
        headerVendorPrices.setText(0, TABLE_INDEX_PRICING_TP, "Transfer Price");
2105 ankur.sing 407
 
2427 ankur.sing 408
        headerVendorPrices.getCellFormatter().setVisible(0, TABLE_INDEX_PRICING_VENDORID, false);
2359 ankur.sing 409
 
2105 ankur.sing 410
        Button addButton = new Button("Add");
2427 ankur.sing 411
        headerVendorPrices.setWidget(0, TABLE_INDEX_PRICING_BUTTON, addButton);
2105 ankur.sing 412
        addButton.addClickHandler(new ClickHandler() {
413
            @Override
414
            public void onClick(ClickEvent event) {
2119 ankur.sing 415
                VendorPricesDialog vendorPricesDialog = new VendorPricesDialog();
416
                vendorPricesDialog.updateButton.setText("Add");
417
                vendorPricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
2105 ankur.sing 418
                    @Override
2119 ankur.sing 419
                    public boolean onUpdate(double mop, double dp, double tp, long vendorId) {
2105 ankur.sing 420
                        if(!vendorExists(vendorId)) {
421
                            Window.alert("Vendor already exists");
422
                            return false;
423
                        }
2119 ankur.sing 424
                        /*if(!validateVendorPrices(mop, dp, tp)) {
2105 ankur.sing 425
                            return false;
2119 ankur.sing 426
                        }*/
2427 ankur.sing 427
                        int row = tableVendorPrices.getRowCount();
428
                        tableVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_VENDORID, VENDOR_ID_WIDTH);
429
                        tableVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_VENDOR_DESC, VENDOR_DESC_WIDTH);
430
                        tableVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_MOP, PRICE_WIDTH);
431
                        tableVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_DP, PRICE_WIDTH);
432
                        tableVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_TP, PRICE_WIDTH);
2105 ankur.sing 433
 
2427 ankur.sing 434
                        tableVendorPrices.setText(row, TABLE_INDEX_PRICING_VENDORID, vendorId + "");
435
                        tableVendorPrices.setText(row, TABLE_INDEX_PRICING_VENDOR_DESC, Utils.getVendorDesc(vendorId));
436
                        tableVendorPrices.setText(row, TABLE_INDEX_PRICING_MOP, mop + "");
437
                        tableVendorPrices.setText(row, TABLE_INDEX_PRICING_DP, dp + "");
438
                        tableVendorPrices.setText(row, TABLE_INDEX_PRICING_TP, tp + "");
2359 ankur.sing 439
 
2427 ankur.sing 440
                        tableVendorPrices.getCellFormatter().setVisible(row, TABLE_INDEX_PRICING_VENDORID, false);
2105 ankur.sing 441
                        return true;
442
                    }
443
                });
2119 ankur.sing 444
                vendorPricesDialog.show();
2105 ankur.sing 445
            }
446
        });
2066 ankur.sing 447
    }
2105 ankur.sing 448
 
2427 ankur.sing 449
    /**
3558 rajveer 450
     * initialises source prices table header. Creates an Add button and
451
     * adds click event listener to it to create and pop up a dialog for adding 
452
     * a prices for a new source 
453
     */
454
    private void initSourcePricingHeader(){
455
        headerSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_SOURCE_ID, VENDOR_ID_WIDTH);
456
        headerSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_SOURCE_DESC, VENDOR_DESC_WIDTH);
457
        headerSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_MRP, PRICE_WIDTH);
458
        headerSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE, PRICE_WIDTH);
459
        headerSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_BUTTON, BUTTON_WIDTH);
460
 
461
        headerSourcePrices.setText(0, TABLE_INDEX_SOURCE_PRICING_SOURCE_ID, "Source Id");
462
        headerSourcePrices.setText(0, TABLE_INDEX_SOURCE_PRICING_SOURCE_DESC, "Source");
463
        headerSourcePrices.setText(0, TABLE_INDEX_SOURCE_PRICING_MRP, "MRP");
464
        headerSourcePrices.setText(0, TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE, "Selling Price");
465
 
466
        headerSourcePrices.getCellFormatter().setVisible(0, TABLE_INDEX_SOURCE_PRICING_SOURCE_ID, false);
467
 
468
        Button addButton = new Button("Add");
469
        headerSourcePrices.setWidget(0, TABLE_INDEX_SOURCE_PRICING_BUTTON, addButton);
470
        addButton.addClickHandler(new ClickHandler() {
471
            @Override
472
            public void onClick(ClickEvent event) {
3567 rajveer 473
                SourcePricesDialog sourcePricesDialog = new SourcePricesDialog(mrp.getText().trim(), sellingPrice.getText().trim());
474
                //SourcePricesDialog sourcePricesDialog = new SourcePricesDialog();
3558 rajveer 475
                sourcePricesDialog.updateButton.setText("Add");
476
                sourcePricesDialog.setSourcePriceUpdateListener(new SourcePricesDialog.SourcePriceUpdateListener() {
477
                    @Override
478
                    public boolean onUpdate(double mrp, double sellingPrice, long sourceId) {
479
                        if(!sourceExists(sourceId)) {
480
                            Window.alert("Source already exists");
481
                            return false;
482
                        }
483
 
484
                        int row = tableSourcePrices.getRowCount();
485
                        tableSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_SOURCE_ID, VENDOR_ID_WIDTH);
486
                        tableSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_SOURCE_DESC, VENDOR_DESC_WIDTH);
487
                        tableSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_MRP, PRICE_WIDTH);
488
                        tableSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE, PRICE_WIDTH);
489
 
490
                        tableSourcePrices.setText(row, TABLE_INDEX_SOURCE_PRICING_SOURCE_ID, sourceId + "");
491
                        tableSourcePrices.setText(row, TABLE_INDEX_SOURCE_PRICING_SOURCE_DESC, Utils.getSourceDesc(sourceId));
492
                        tableSourcePrices.setText(row, TABLE_INDEX_SOURCE_PRICING_MRP, mrp + "");
493
                        tableSourcePrices.setText(row, TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE, sellingPrice + "");
494
 
495
                        tableSourcePrices.getCellFormatter().setVisible(row, TABLE_INDEX_SOURCE_PRICING_SOURCE_ID, false);
496
                        return true;
497
                    }
498
                });
499
                sourcePricesDialog.show();
500
            }
501
        });
502
    }
503
 
504
    /**
4423 phani.kuma 505
     * initialises similar items table header. Creates an Add button and
506
     * adds click event listener to it to create and pop up a dialog for adding 
507
     * a new similar item. 
508
     */
509
    private void initSimilarItemList() {
510
    	headerSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_CATALOG_ITEM_ID, PRICE_WIDTH);
511
    	headerSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_PRODUCT_GROUP, PRICE_WIDTH);
512
    	headerSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_BRAND, PRICE_WIDTH);
513
    	headerSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_MODEL_NUMBER, PRICE_WIDTH);
514
    	headerSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_MODEL_NAME, PRICE_WIDTH);
515
    	headerSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_CATEGORY, "150px");
516
    	headerSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_BUTTON, BUTTON_WIDTH);
517
 
518
    	headerSimilarItems.setText(0, TABLE_INDEX_SIMILAR_ITEMS_CATALOG_ITEM_ID, "Catalog ItemId");
519
    	headerSimilarItems.setText(0, TABLE_INDEX_SIMILAR_ITEMS_PRODUCT_GROUP, "Product Group");
520
    	headerSimilarItems.setText(0, TABLE_INDEX_SIMILAR_ITEMS_BRAND, "Brand");
521
    	headerSimilarItems.setText(0, TABLE_INDEX_SIMILAR_ITEMS_MODEL_NUMBER, "Model Number");
522
    	headerSimilarItems.setText(0, TABLE_INDEX_SIMILAR_ITEMS_MODEL_NAME, "Model Name");
523
    	headerSimilarItems.setText(0, TABLE_INDEX_SIMILAR_ITEMS_CATEGORY, "Category");
524
 
525
        Button addButton = new Button("Add");
526
        headerSimilarItems.setWidget(0, TABLE_INDEX_SIMILAR_ITEMS_BUTTON, addButton);
527
        addButton.addClickHandler(new ClickHandler() {
528
            @Override
529
            public void onClick(ClickEvent event) {
530
            	AddSimilarItemDialog addSimilarItemDialog = new AddSimilarItemDialog();
531
            	addSimilarItemDialog.setAddSimilarItemUpdateListener(new AddSimilarItemDialog.AddSimilarItemUpdateListener() {
532
					@Override
533
					public boolean onUpdate(long catalogItemId) {
534
						if (item.getSimilarItems().containsKey(catalogItemId)) {
535
							Window.alert("Similar Item exists.");
536
							return false;
537
						}
538
						else {
539
		                    catalogService.addSimilarItem(item.getId(), catalogItemId, new AsyncCallback<Item>() {
540
		                        @Override
541
		                        public void onSuccess(Item similarItem) {
542
		                            if(similarItem != null) {
543
		                                GWT.log("Similar Item Added");
544
		                                Map<Long, Item> newsimilarItems = item.getSimilarItems();
545
		                                newsimilarItems.put(similarItem.getCatalogItemId(), similarItem);
546
		                                item.setSimilarItems(newsimilarItems);
547
		                                updateSimilarItemsTable(item.getSimilarItems());
548
		                                Window.alert("Similar Item Added successfully.");
549
		                            }
550
		                            else {
551
		                                GWT.log("Error Adding Similar Item");
552
		                                Window.alert("Error Adding Similar Item");
553
		                            }
554
		                        }
555
		                        @Override
556
		                        public void onFailure(Throwable caught) {
557
		                            caught.printStackTrace();
558
		                            Window.alert("Error deleting Similar Item");
559
		                        }
560
		                    });
561
	                        return true;
562
						}
563
					}
564
				});
565
            	addSimilarItemDialog.show();
566
            }
567
        });
568
    }
569
 
5504 phani.kuma 570
    private void initVoucherHeader(){
571
    	headerVouchers.getColumnFormatter().setWidth(TABLE_INDEX_VOUCHERS_TYPE, ITEM_KEY_WIDTH);
572
    	headerVouchers.getColumnFormatter().setWidth(TABLE_INDEX_VOUCHERS_AMOUNT, VENDOR_DESC_WIDTH);
573
    	headerVouchers.getColumnFormatter().setWidth(TABLE_INDEX_VOUCHERS_ADD_EDIT_BUTTON, BUTTON_WIDTH);
574
    	headerVouchers.getColumnFormatter().setWidth(TABLE_INDEX_VOUCHERS_DEL_BUTTON, BUTTON_WIDTH);
575
 
576
    	headerVouchers.setText(0, TABLE_INDEX_VOUCHERS_TYPE, "Voucher Type");
577
    	headerVouchers.setText(0, TABLE_INDEX_VOUCHERS_AMOUNT, "Voucher Amount");
578
        headerVouchers.getCellFormatter().setVisible(0, TABLE_INDEX_VOUCHERS_DEL_BUTTON, false);
579
        Button addButton = new Button("Add");
580
        headerVouchers.setWidget(0, TABLE_INDEX_VOUCHERS_ADD_EDIT_BUTTON, addButton);
581
        addButton.addClickHandler(new ClickHandler() {
582
            @Override
583
            public void onClick(ClickEvent event) {
584
            	VoucherItemDialog voucherItemDialog = new VoucherItemDialog();
585
            	voucherItemDialog.updateButton.setText("Add");
586
            	voucherItemDialog.setVoucherUpdateListener(new VoucherItemDialog.VoucherUpdateListener() {
587
                    @Override
588
                    public boolean onUpdate(final String voucherType, final long voucherAmount) {
589
                        if(!voucherExists(voucherType)) {
590
                            Window.alert("Voucher already exists");
591
                            return false;
592
                        }
5516 phani.kuma 593
                        Long voucherTypeValue = Utils.getVoucherTypeId(voucherType);;
594
                        catalogService.addVoucher(item.getCatalogItemId(), voucherTypeValue, voucherAmount, new AsyncCallback<Boolean>() {
5504 phani.kuma 595
	                        @Override
596
	                        public void onSuccess(Boolean result) {
597
	                        	if(result) {
598
	                                GWT.log("Voucher Added");
599
	                                VoucherItemMapping newvoucher = new VoucherItemMapping(voucherAmount, voucherType);
600
	                                Map<String, VoucherItemMapping> newvouchersMap = item.getVouchersMap();
601
	                                newvouchersMap.put(voucherType, newvoucher);
602
	                                item.setVouchersMap(newvouchersMap);
603
	                                updateVoucherTable(item.getVouchersMap());
604
	                                Window.alert("Voucher Added successfully.");
605
	                            }
606
	                            else {
607
	                                GWT.log("Error Adding Voucher");
608
	                                Window.alert("Error Adding Voucher");
609
	                            }
610
	                        }
611
	                        @Override
612
	                        public void onFailure(Throwable caught) {
613
	                            caught.printStackTrace();
614
	                            Window.alert("Error deleting voucher");
615
	                        }
616
	                    });
617
                        return true;
618
                    }
619
                });
620
            	voucherItemDialog.show();
621
            }
622
        });
623
    }
624
 
4423 phani.kuma 625
    /**
2427 ankur.sing 626
     * Clear and populate item availability table.
627
     * @param availabilityMap
628
     */
4431 phani.kuma 629
    private void updateAvailabilityTable(Map<Long, ItemInventory> itemInventoryMap){
1992 ankur.sing 630
        availabilityTable.removeAllRows();
4431 phani.kuma 631
        if(itemInventoryMap == null || itemInventoryMap.isEmpty()) {
1992 ankur.sing 632
            return;
633
        }
4431 phani.kuma 634
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_ID, "100px");
2105 ankur.sing 635
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_DESC, "300px");
4431 phani.kuma 636
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_INVA, "100px");
637
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_INVR, "100px");
638
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_GET_BUTTON, "100px");
639
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_UPDATE_BUTTON, "100px");
2066 ankur.sing 640
 
1992 ankur.sing 641
        int i=0;
4431 phani.kuma 642
        for(ItemInventory warehousedata : itemInventoryMap.values()){
643
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_ID, warehousedata.getWarehouseId() + "");
644
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_DESC, Utils.getWarehouseDesc(warehousedata.getWarehouseId()) + "");
645
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_INVA, warehousedata.getAvailability() + "");
646
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_INVR, warehousedata.getReserved() + "");
1992 ankur.sing 647
            i++;
648
        }
649
    }
4423 phani.kuma 650
 
2427 ankur.sing 651
    /**
652
     * Clear and populate vendor item key table with keys in the passed argument.
653
     * With each row in the table, an edit button is created and click event listener 
654
     * is added to it to edit that vendor item key row.
655
     * @param vendorKeysMap
656
     */
657
    private void updateVendorKeysTable(Map<String, VendorItemMapping> vendorKeysMap){
658
        tableVendorItemKey.removeAllRows();
2119 ankur.sing 659
 
2427 ankur.sing 660
        if(vendorKeysMap == null || vendorKeysMap.isEmpty()) {
2119 ankur.sing 661
            return;
662
        }
2427 ankur.sing 663
        tableVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_VENDORID, VENDOR_ID_WIDTH);
664
        tableVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_VENDOR_DESC, VENDOR_DESC_WIDTH);
665
        tableVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_ITEM_KEY, ITEM_KEY_WIDTH);
666
        tableVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_ITEM_KEY_OLD, ITEM_KEY_WIDTH);
667
        tableVendorItemKey.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_BUTTON, BUTTON_WIDTH);
2066 ankur.sing 668
 
2119 ankur.sing 669
        int i=0;
2427 ankur.sing 670
        for(Entry<String, VendorItemMapping> e : vendorKeysMap.entrySet()){
2359 ankur.sing 671
            VendorItemMapping vendorMapping = e.getValue();
2427 ankur.sing 672
            tableVendorItemKey.setText(i, TABLE_INDEX_MAPPING_VENDORID, vendorMapping.getVendorId() + "");
673
            tableVendorItemKey.setText(i, TABLE_INDEX_MAPPING_VENDOR_DESC, Utils.getVendorDesc(vendorMapping.getVendorId()));
674
            tableVendorItemKey.setText(i, TABLE_INDEX_MAPPING_ITEM_KEY, vendorMapping.getItemKey());
675
            tableVendorItemKey.setText(i, TABLE_INDEX_MAPPING_ITEM_KEY_OLD, e.getKey().substring(e.getKey().indexOf(Item.KEY_SEPARATOR)+1));
2119 ankur.sing 676
            Button editButton = new Button("Edit");
2427 ankur.sing 677
            tableVendorItemKey.setWidget(i, TABLE_INDEX_MAPPING_BUTTON, editButton);
2119 ankur.sing 678
            editButton.addClickHandler(new ClickHandler() {
679
                @Override
680
                public void onClick(ClickEvent event) {
2427 ankur.sing 681
                    Cell cell = tableVendorItemKey.getCellForEvent(event);
2119 ankur.sing 682
                    int row = cell.getRowIndex();
2427 ankur.sing 683
                    long vendorId = Long.parseLong(tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_VENDORID));
684
                    editVendorKey(vendorId, row);
2119 ankur.sing 685
                }
686
            });
2427 ankur.sing 687
            tableVendorItemKey.getCellFormatter().setVisible(i, TABLE_INDEX_MAPPING_VENDORID, false);
688
            tableVendorItemKey.getCellFormatter().setVisible(i, TABLE_INDEX_MAPPING_ITEM_KEY_OLD, false);
2359 ankur.sing 689
            i++;
2119 ankur.sing 690
        }
691
    }
692
 
2427 ankur.sing 693
    /**
694
     * Clear and populate vendor prices table with prices in the passed argument.
695
     * With each row in the table, an edit button is created and click event listener 
696
     * is added to it to edit that vendor prices row.
697
     * @param vendorPricingMap
698
     */
699
    private void updateVendorPricingTable(Map<Long, VendorPricings> vendorPricingMap){
700
        tableVendorPrices.removeAllRows();
2105 ankur.sing 701
 
2119 ankur.sing 702
        if(vendorPricingMap == null || vendorPricingMap.isEmpty()) {
1992 ankur.sing 703
            return;
704
        }
2427 ankur.sing 705
        tableVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_VENDORID, VENDOR_ID_WIDTH);
706
        tableVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_VENDOR_DESC, VENDOR_DESC_WIDTH);
707
        tableVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_MOP, PRICE_WIDTH);
708
        tableVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_DP, PRICE_WIDTH);
709
        tableVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_TP, PRICE_WIDTH);
710
        tableVendorPrices.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_BUTTON, BUTTON_WIDTH);
2066 ankur.sing 711
 
712
 
1992 ankur.sing 713
        int i=0;
2119 ankur.sing 714
        for(VendorPricings vendorDetail : vendorPricingMap.values()){
2427 ankur.sing 715
            tableVendorPrices.setText(i, TABLE_INDEX_PRICING_VENDORID, vendorDetail.getVendorId() + "");
716
            tableVendorPrices.setText(i, TABLE_INDEX_PRICING_VENDOR_DESC, Utils.getVendorDesc(vendorDetail.getVendorId()));
717
            tableVendorPrices.setText(i, TABLE_INDEX_PRICING_MOP, vendorDetail.getMop() + "");
718
            tableVendorPrices.setText(i, TABLE_INDEX_PRICING_DP, vendorDetail.getDealerPrice() + "");
719
            tableVendorPrices.setText(i, TABLE_INDEX_PRICING_TP, vendorDetail.getTransferPrice() + "");
1992 ankur.sing 720
            Button editButton = new Button("Edit");
2427 ankur.sing 721
            tableVendorPrices.setWidget(i, TABLE_INDEX_PRICING_BUTTON, editButton);
1992 ankur.sing 722
            editButton.addClickHandler(new ClickHandler() {
723
                @Override
724
                public void onClick(ClickEvent event) {
2427 ankur.sing 725
                    Cell cell = tableVendorPrices.getCellForEvent(event);
1992 ankur.sing 726
                    int row = cell.getRowIndex();
2427 ankur.sing 727
                    long vendorId = Long.parseLong(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_VENDORID));
728
                    editVendorPrices(vendorId, row);
1992 ankur.sing 729
                }
730
            });
2427 ankur.sing 731
            tableVendorPrices.getCellFormatter().setVisible(i, TABLE_INDEX_PRICING_VENDORID, false);
2359 ankur.sing 732
            i++;
1992 ankur.sing 733
        }
734
    }
2066 ankur.sing 735
 
2427 ankur.sing 736
    /**
3558 rajveer 737
     * Clear and populate vendor prices table with prices in the passed argument.
738
     * With each row in the table, an edit button is created and click event listener 
739
     * is added to it to edit that vendor prices row.
740
     * @param sourcePricingMap
741
     */
742
    private void updateSourcePricingTable(Map<Long, SourcePricings> sourcePricingMap){
743
        tableSourcePrices.removeAllRows();
744
 
745
        if(sourcePricingMap == null || sourcePricingMap.isEmpty()) {
746
            return;
747
        }
748
        tableSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_SOURCE_ID, VENDOR_ID_WIDTH);
749
        tableSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_SOURCE_DESC, VENDOR_DESC_WIDTH);
750
        tableSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_MRP, PRICE_WIDTH);
751
        tableSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE, PRICE_WIDTH);
752
        tableSourcePrices.getColumnFormatter().setWidth(TABLE_INDEX_SOURCE_PRICING_BUTTON, BUTTON_WIDTH);
753
 
754
        int i=0;
755
        for(SourcePricings sourceDetail : sourcePricingMap.values()){
756
            tableSourcePrices.setText(i, TABLE_INDEX_SOURCE_PRICING_SOURCE_ID, sourceDetail.getSourceId() + "");
757
            tableSourcePrices.setText(i, TABLE_INDEX_SOURCE_PRICING_SOURCE_DESC, Utils.getSourceDesc(sourceDetail.getSourceId()));
758
            tableSourcePrices.setText(i, TABLE_INDEX_SOURCE_PRICING_MRP, sourceDetail.getMrp() + "");
759
            tableSourcePrices.setText(i, TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE, sourceDetail.getSellingPrice() + "");
760
 
761
            Button editButton = new Button("Edit");
762
            tableSourcePrices.setWidget(i, TABLE_INDEX_SOURCE_PRICING_BUTTON, editButton);
763
            editButton.addClickHandler(new ClickHandler() {
764
                @Override
765
                public void onClick(ClickEvent event) {
766
                    Cell cell = tableSourcePrices.getCellForEvent(event);
767
                    int row = cell.getRowIndex();
768
                    long sourceId = Long.parseLong(tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_SOURCE_ID));
769
                    editSourcePrices(sourceId, row);
770
                }
771
            });
772
            tableSourcePrices.getCellFormatter().setVisible(i, TABLE_INDEX_SOURCE_PRICING_SOURCE_ID, false);
773
            i++;
774
        }
775
    }
776
 
777
    /**
4423 phani.kuma 778
     * Clear and populate similar items table with items in the passed argument.
779
     * With each row in the table, an delete button is created and click event listener 
780
     * is added to it to delete that similar item row.
781
     * @param similarItems
782
     */
783
    private void updateSimilarItemsTable(Map<Long, Item> similarItems){
784
    	tableSimilarItems.removeAllRows();
785
 
786
        if(similarItems == null || similarItems.isEmpty()) {
787
            return;
788
        }
789
        tableSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_CATALOG_ITEM_ID, PRICE_WIDTH);
790
        tableSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_PRODUCT_GROUP, PRICE_WIDTH);
791
        tableSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_BRAND, PRICE_WIDTH);
792
        tableSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_MODEL_NUMBER, PRICE_WIDTH);
793
        tableSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_MODEL_NAME, PRICE_WIDTH);
794
        tableSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_CATEGORY, "150px");
795
        tableSimilarItems.getColumnFormatter().setWidth(TABLE_INDEX_SIMILAR_ITEMS_BUTTON, BUTTON_WIDTH);
796
 
797
 
798
        int i=0;
799
        for(Item similarItemDetail : similarItems.values()){
800
        	tableSimilarItems.setText(i, TABLE_INDEX_SIMILAR_ITEMS_CATALOG_ITEM_ID, similarItemDetail.getCatalogItemId() + "");
801
        	tableSimilarItems.setText(i, TABLE_INDEX_SIMILAR_ITEMS_PRODUCT_GROUP, similarItemDetail.getProductGroup() + "");
802
        	tableSimilarItems.setText(i, TABLE_INDEX_SIMILAR_ITEMS_BRAND, similarItemDetail.getBrand() + "");
803
        	tableSimilarItems.setText(i, TABLE_INDEX_SIMILAR_ITEMS_MODEL_NUMBER, similarItemDetail.getModelNumber() + "");
804
        	tableSimilarItems.setText(i, TABLE_INDEX_SIMILAR_ITEMS_MODEL_NAME, similarItemDetail.getModelName() + "");
805
        	tableSimilarItems.setText(i, TABLE_INDEX_SIMILAR_ITEMS_CATEGORY, similarItemDetail.getContentCategory() + "");
806
 
807
        	Button deleteButton = new Button("Delete");
808
            tableSimilarItems.setWidget(i, TABLE_INDEX_SIMILAR_ITEMS_BUTTON, deleteButton);
809
            deleteButton.addClickHandler(new ClickHandler() {
810
                @Override
811
                public void onClick(ClickEvent event) {
812
                    Cell cell = tableSimilarItems.getCellForEvent(event);
813
                    final int row = cell.getRowIndex();
814
                    long catalogItemId = Long.parseLong(tableSimilarItems.getText(row, TABLE_INDEX_SIMILAR_ITEMS_CATALOG_ITEM_ID));
815
 
816
                    catalogService.deleteSimilarItem(item.getId(), catalogItemId, new AsyncCallback<Boolean>() {
817
                        @Override
818
                        public void onSuccess(Boolean result) {
819
                            if(result) {
820
                                GWT.log("Similar Item deleted");
821
                                long catalogItemId = Long.parseLong(tableSimilarItems.getText(row, TABLE_INDEX_SIMILAR_ITEMS_CATALOG_ITEM_ID)); 
822
                                tableSimilarItems.removeRow(row);
823
                                Map<Long, Item> newsimilarItems = item.getSimilarItems();
824
                                newsimilarItems.remove(catalogItemId);
825
                                item.setSimilarItems(newsimilarItems);
826
                            }
827
                            else {
828
                                GWT.log("Error deleting Similar Item");
829
                                Window.alert("Error deleting Similar Item");
830
                            }
831
                        }
832
                        @Override
833
                        public void onFailure(Throwable caught) {
834
                            caught.printStackTrace();
835
                            Window.alert("Error deleting Similar Item");
836
                        }
837
                    });
838
                }
839
            });
840
            i++;
841
        }
842
    }
843
 
5504 phani.kuma 844
    private void updateVoucherTable(Map<String, VoucherItemMapping> vouchersMap){
845
    	tableVouchers.removeAllRows();
846
 
847
        if(vouchersMap == null || vouchersMap.isEmpty()) {
848
            return;
849
        }
850
        tableVouchers.getColumnFormatter().setWidth(TABLE_INDEX_VOUCHERS_TYPE, ITEM_KEY_WIDTH);
851
        tableVouchers.getColumnFormatter().setWidth(TABLE_INDEX_VOUCHERS_AMOUNT, VENDOR_DESC_WIDTH);
852
        tableVouchers.getColumnFormatter().setWidth(TABLE_INDEX_VOUCHERS_ADD_EDIT_BUTTON, BUTTON_WIDTH);
853
        tableVouchers.getColumnFormatter().setWidth(TABLE_INDEX_VOUCHERS_DEL_BUTTON, BUTTON_WIDTH);
854
 
855
        int i=0;
856
        for(VoucherItemMapping voucher : vouchersMap.values()){
857
        	tableVouchers.setText(i, TABLE_INDEX_VOUCHERS_TYPE, voucher.getVoucherType());
858
        	tableVouchers.setText(i, TABLE_INDEX_VOUCHERS_AMOUNT, voucher.getAmount() + "");
859
 
860
            Button editButton = new Button("Edit");
861
            tableVouchers.setWidget(i, TABLE_INDEX_VOUCHERS_ADD_EDIT_BUTTON, editButton);
862
            editButton.addClickHandler(new ClickHandler() {
863
                @Override
864
                public void onClick(ClickEvent event) {
865
                    Cell cell = tableVouchers.getCellForEvent(event);
866
                    final int row = cell.getRowIndex();
867
                    String voucherAmount = tableVouchers.getText(row, TABLE_INDEX_VOUCHERS_AMOUNT);
868
                    VoucherItemDialog voucherDialog = new VoucherItemDialog(voucherAmount);
869
                    voucherDialog.updateButton.setText("Update");
870
                    voucherDialog.setVoucherUpdateListener(new VoucherItemDialog.VoucherUpdateListener() {
871
                        @Override
872
                        public boolean onUpdate(final String voucherType, final long voucherAmount) {
5516 phani.kuma 873
                        	Long voucherTypeValue = Utils.getVoucherTypeId(voucherType);
874
                        	catalogService.addVoucher(item.getCatalogItemId(), voucherTypeValue, voucherAmount, new AsyncCallback<Boolean>() {
5504 phani.kuma 875
    	                        @Override
876
    	                        public void onSuccess(Boolean result) {
877
    	                        	if(result) {
878
    	                                GWT.log("Voucher Updated");
879
    	                                VoucherItemMapping newvoucher = new VoucherItemMapping(voucherAmount, voucherType);
880
    	                                Map<String, VoucherItemMapping> newvouchersMap = item.getVouchersMap();
881
    	                                newvouchersMap.remove(voucherType);
882
    	                                newvouchersMap.put(voucherType, newvoucher);
883
    	                                item.setVouchersMap(newvouchersMap);
884
    	                                updateVoucherTable(item.getVouchersMap());
885
    	                                Window.alert("Voucher Updated successfully.");
886
    	                            }
887
    	                            else {
888
    	                                GWT.log("Error Updating Voucher");
889
    	                                Window.alert("Error Updating Voucher");
890
    	                            }
891
    	                        }
892
    	                        @Override
893
    	                        public void onFailure(Throwable caught) {
894
    	                            caught.printStackTrace();
895
    	                            Window.alert("Error Updating voucher");
896
    	                        }
897
    	                    });
898
                            return true;
899
                        }
900
                    });
901
                    voucherDialog.show();
902
                }
903
            });
904
 
905
            Button deleteButton = new Button("Delete");
906
            tableVouchers.setWidget(i, TABLE_INDEX_VOUCHERS_DEL_BUTTON, deleteButton);
907
            deleteButton.addClickHandler(new ClickHandler() {
908
                @Override
909
                public void onClick(ClickEvent event) {
910
                    Cell cell = tableVouchers.getCellForEvent(event);
911
                    final int row = cell.getRowIndex();
912
                    String voucherType = tableVouchers.getText(row, TABLE_INDEX_VOUCHERS_TYPE);
5516 phani.kuma 913
                    Long voucherTypeValue = Utils.getVoucherTypeId(voucherType);
914
                    catalogService.deleteVoucher(item.getCatalogItemId(), voucherTypeValue, new AsyncCallback<Boolean>() {
5504 phani.kuma 915
                        @Override
916
                        public void onSuccess(Boolean result) {
917
                            if(result) {
918
                                GWT.log("Voucher deleted");
919
                                String voucherType = tableVouchers.getText(row, TABLE_INDEX_VOUCHERS_TYPE);
920
                                tableVouchers.removeRow(row);
921
                                Map<String, VoucherItemMapping> newvouchersMap = item.getVouchersMap();
922
                                newvouchersMap.remove(voucherType);
923
                                item.setVouchersMap(newvouchersMap);
924
                            }
925
                            else {
926
                                GWT.log("Error deleting Voucher");
927
                                Window.alert("Error deleting Voucher");
928
                            }
929
                        }
930
                        @Override
931
                        public void onFailure(Throwable caught) {
932
                            caught.printStackTrace();
933
                            Window.alert("Error deleting Voucher");
934
                        }
935
                    });
936
                }
937
            });
938
            i++;
939
        }
940
    }
941
 
4423 phani.kuma 942
    /**
2427 ankur.sing 943
     * called on the click event of update item button in ItemActions
944
     */
2126 ankur.sing 945
    void updateItem() {
946
        if(item == null) {
947
            Window.alert("Please select an item to update.");
948
            return;
949
        }
2066 ankur.sing 950
        try {
2126 ankur.sing 951
            if(!createNewItem()) {
952
                return;
953
            }
2066 ankur.sing 954
        } catch(NumberFormatException ex) {
2126 ankur.sing 955
            ex.printStackTrace();
956
            GWT.log("Number format exception");
957
        }
5427 amit.gupta 958
        String paramsChanged = isItemChanged();
959
        if(paramsChanged.equals("")) {
960
        	Window.alert("Nothing to update. Please change intended item parameters and try again.");
961
        	return;
962
        } else {
963
        	if(item.getSameItemsWithDifferentColors().size()>0 && optionalChangedValMap.size()>0 ){
964
        		createDialog(paramsChanged);
965
        	} else {
6096 amit.gupta 966
        		String paramsChanged1  = "";
967
        		paramsChanged1 = "You have changed following items.\n" + paramsChanged;
968
        		Window.alert(paramsChanged1);
5427 amit.gupta 969
        		validateNUpdate();
970
        	}
6096 amit.gupta 971
        	if("\n-Expected Delay".equals(paramsChanged)){
6099 amit.gupta 972
        		if(Window.confirm("Would You like to update Expected delay on prod?")){
6105 amit.gupta 973
	        		catalogService.updateExpectedDelayOnProd(newItem, new AsyncCallback<String>() {
6099 amit.gupta 974
	                    @Override
975
	                    public void onSuccess(String result) {
976
	                        Window.alert(result);
977
	                    }
978
	                    @Override
979
	                    public void onFailure(Throwable caught) {
980
	                        Window.alert("Error while updating item on production");
981
	                    }
982
	                });
983
        		}
6096 amit.gupta 984
        	}
5427 amit.gupta 985
        }
986
    }
987
 
988
 
989
    private void validateNUpdate() {
990
    	if(!Utils.validateItem(newItem)) {
2066 ankur.sing 991
            return;
992
        }
2126 ankur.sing 993
 
4649 phani.kuma 994
        final String[] messageList = new String[2];
995
 
996
    	String sellingpricemessage = compareSellingPricewithBreakEven();
997
    	if(sellingpricemessage.equals("false")) {
998
    		return;
999
    	}
1000
    	else if(!sellingpricemessage.equals("true")) {
1001
    		messageList[0] = sellingpricemessage;
1002
    	}
1003
 
1004
        String preferredvendormessage = checkTransferPriceforPreferredVendor();
1005
        if(preferredvendormessage.equals("false")) {
1006
        	return;
1007
        }
1008
        else if(!preferredvendormessage.equals("true")) {
1009
        	messageList[1] = preferredvendormessage;
1010
        }
1011
 
2126 ankur.sing 1012
        /*if(!validatePrices()) {
1013
            return;
1014
        }*/
1015
        catalogService.updateItem(newItem, new AsyncCallback<Boolean>() {
1016
            @Override
1017
            public void onSuccess(Boolean result) {
1018
                if(result) {
4649 phani.kuma 1019
                	for(int i = 0; i < messageList.length; i++) {
1020
                		if(messageList[i] != null) {
1021
                			logAuthorization(messageList[i]);
1022
                		}
1023
                	}
5427 amit.gupta 1024
                	if(optionalChangedValMap.size()>0 || mandatoryChangedValMap.size()>0){
1025
	                	for(Long itemId : item.getSameItemsWithDifferentColors()){
1026
	                		updateItem(itemId);
1027
	                	}
1028
                	}
2126 ankur.sing 1029
                    item = newItem;
1030
                    GWT.log("Item updated. Id = " + item.getId());
2489 ankur.sing 1031
                    catalogDashboardPanel.getItemListWidget().updateItem(item);
1032
                    getFreshItemFromDB(item.getId());
2126 ankur.sing 1033
                    Window.alert("Item updated successfully.");
1034
                }
1035
                else {
1036
                    GWT.log("Error updating item");
1037
                    Window.alert("Error updating item");
1038
                }
1039
            }
1040
            @Override
1041
            public void onFailure(Throwable caught) {
1042
                caught.printStackTrace();
1043
                Window.alert("Error while updating item");
1044
            }
1045
        });
2066 ankur.sing 1046
    }
2126 ankur.sing 1047
 
2489 ankur.sing 1048
    private void getFreshItemFromDB(long id) {
1049
        catalogService.getItem(id, new AsyncCallback<Item>() {
1050
            @Override
1051
            public void onSuccess(Item result) {
1052
                setItemDetails(result);
1053
            }
1054
            @Override
1055
            public void onFailure(Throwable caught) {
1056
                caught.printStackTrace();
1057
                Window.alert("Unable to fetch item details.");
1058
            }
1059
        });
1060
    }
1061
 
2427 ankur.sing 1062
    /**
1063
     * This method is called while updating item.<br> It will create a new Item object and set 
1064
     * its editable attributes with UI fields values and non-editable attributes with old Item
1065
     * object attributes. This new Item object is then passed to the service to update item in the database.
1066
     * <br>If update is successful, the old Item object is replaced with the new Item object. 
1067
     * @return true if new Item object is created successfully
1068
     *     <br>false if some error occurs due to NumberFormatException
1069
     */
2126 ankur.sing 1070
    private boolean createNewItem() {
2066 ankur.sing 1071
        newItem = new Item();
2489 ankur.sing 1072
        newItem.setId(item.getId());
2066 ankur.sing 1073
        newItem.setProductGroup(productGroup.getText().trim());
1074
        newItem.setBrand(brand.getText().trim());
1075
        newItem.setModelNumber(modelNumber.getText().trim());
1076
        newItem.setModelName(modelName.getText().trim());
1077
        newItem.setColor(color.getText().trim());
2119 ankur.sing 1078
        newItem.setContentCategory(contentCategory.getText());
2066 ankur.sing 1079
        newItem.setComments(comments.getText().trim());
1080
        newItem.setCatalogItemId(Long.parseLong(catalogItemId.getText()));
2126 ankur.sing 1081
 
2068 ankur.sing 1082
        try {
2126 ankur.sing 1083
            if(!mrp.getText().trim().isEmpty()) {
1084
                double mrpValue = Double.parseDouble(mrp.getText().trim());
1085
                if(mrpValue <= 0) {
1086
                    throw new NumberFormatException("Negative value of MRP");
1087
                }
1088
                newItem.setMrp(mrpValue);
1089
            }
2068 ankur.sing 1090
        } catch(NumberFormatException ex) {
2126 ankur.sing 1091
            Window.alert("Invalid MRP format/value. Value shoule be greater than zero");
1092
            return false;
2068 ankur.sing 1093
        }
1094
        try {
2126 ankur.sing 1095
            if(!sellingPrice.getText().trim().isEmpty()) {
1096
            double spValue = Double.parseDouble(sellingPrice.getText().trim());
1097
            if(spValue <= 0) {
1098
                throw new NumberFormatException("Negative value of Selling price");
1099
            }
1100
            newItem.setSellingPrice(spValue);
1101
            }
1102
        } catch(NumberFormatException ex) {
1103
            Window.alert("Invalid Selling Price format/value. Value shoule be greater than zero");
1104
            return false;
2068 ankur.sing 1105
        }
1106
        try {
2126 ankur.sing 1107
            if(!weight.getText().trim().isEmpty()) {
1108
                double wtValue = Double.parseDouble(weight.getText().trim());
1109
                if(wtValue <= 0) {
1110
                    throw new NumberFormatException("Negative value of Weight");
1111
                }
1112
                newItem.setWeight(wtValue);
1113
            }
2068 ankur.sing 1114
        } catch(NumberFormatException ex) {
2126 ankur.sing 1115
            Window.alert("Invalid weight format/value. Value shoule be greater than zero");
1116
            return false;
2068 ankur.sing 1117
        }
2126 ankur.sing 1118
        try {
1119
            if(!startDate.getTextBox().getText().trim().equals("")) {
1120
                newItem.setStartDate(startDate.getValue().getTime());
1121
            }
1122
        } catch(Exception ex) {
1123
            Window.alert("Invalid start date format");
1124
            return false;
1125
        }
2066 ankur.sing 1126
        newItem.setBestDealsText(bestDealsText.getText().trim());
5217 amit.gupta 1127
        Date comingSoonStartDt  = comingSoonStartDate.getValue();
1128
        Date expectedArrivalDt  = expectedArrivalDate.getValue();
1129
        if(comingSoonStartDt == null){
1130
        	newItem.setComingSoonStartDate(null);
1131
        }else {
1132
        	newItem.setComingSoonStartDate(comingSoonStartDt.getTime());
1133
        }
1134
        if(expectedArrivalDt == null){
1135
        	newItem.setExpectedArrivalDate(null);
1136
        }else {
1137
        	newItem.setExpectedArrivalDate(expectedArrivalDt.getTime());
1138
        }
2068 ankur.sing 1139
        try {
2126 ankur.sing 1140
            if(!bestDealsValue.getText().trim().equals("")) {
1141
                double bdValue = Double.parseDouble(bestDealsValue.getText().trim());
1142
                if(bdValue < 0) {
1143
                    throw new NumberFormatException("Negative value of BestDealValue");
1144
                }
1145
                newItem.setBestDealsValue(bdValue);
1146
            }
2068 ankur.sing 1147
        } catch(NumberFormatException ex) {
2126 ankur.sing 1148
            Window.alert("Invalid best deal value format");
1149
            return false;
2068 ankur.sing 1150
        }
3363 chandransh 1151
 
2068 ankur.sing 1152
        try {
2126 ankur.sing 1153
            if(!bestSellingRank.getText().trim().equals("")) {
1154
                long bsrValue = Long.parseLong(bestSellingRank.getText().trim());
1155
                if(bsrValue < 0) {
1156
                    throw new NumberFormatException("Negative value of Best Selling Rank");
1157
                }
1158
                newItem.setBestSellingRank(bsrValue);
1159
            }
1160
        } catch(NumberFormatException ex) {
1161
            Window.alert("Invalid best selling rank format");
1162
            return false;
2068 ankur.sing 1163
        }
2066 ankur.sing 1164
        newItem.setDefaultForEntity(defaultForEntity.getValue());
2252 ankur.sing 1165
        newItem.setRisky(risky.getValue());
2119 ankur.sing 1166
 
3363 chandransh 1167
        try {
1168
            String expectedDelayText = expectedDelay.getText().trim();
1169
            if(!expectedDelayText.equals("")){
1170
                newItem.setExpectedDelay(Integer.parseInt(expectedDelayText));
1171
            }
1172
        } catch(NumberFormatException nfe) {
1173
            Window.alert("Invalid expected delay");
1174
            return false;
1175
        }
1176
 
4583 phani.kuma 1177
        if((item.getPreferredVendor() == null || item.getVendorPricesMap() == null || item.getVendorPricesMap().isEmpty()) && preferredVendor.getSelectedIndex() == 0) {
1178
        	newItem.setPreferredVendor(item.getPreferredVendor());
1179
        }
1180
    	else {
4506 phani.kuma 1181
    		long vendorId = Utils.getVendorId(preferredVendor.getItemText(preferredVendor.getSelectedIndex()));
1182
            newItem.setPreferredVendor(vendorId);
1183
    	}
1184
 
4413 anupam.sin 1185
        newItem.setWarehouseStickiness(warehouseStickiness.getValue());
5384 phani.kuma 1186
        newItem.setHasItemNo(hasItemNo.getValue());
1187
        newItem.setItemType(itemType.getValue());
5460 phani.kuma 1188
        newItem.setClearance(clearance.getValue());
4413 anupam.sin 1189
 
2126 ankur.sing 1190
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
1191
          Add the instance to map and set the map to the item instance created above.*/
1192
        Map<Long, VendorPricings> vendorPrices = new HashMap<Long, VendorPricings>();
2119 ankur.sing 1193
        VendorPricings v;
2427 ankur.sing 1194
        for(int row = 0; row < tableVendorPrices.getRowCount(); row++) {
2119 ankur.sing 1195
            v = new VendorPricings();
2427 ankur.sing 1196
            v.setMop(Double.parseDouble(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_MOP)));
1197
            v.setDealerPrice(Double.parseDouble(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_DP)));
1198
            v.setTransferPrice(Double.parseDouble(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_TP)));
1199
            v.setVendorId(Long.parseLong(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_VENDORID)));
2126 ankur.sing 1200
            vendorPrices.put(v.getVendorId(), v);
2066 ankur.sing 1201
        }
2126 ankur.sing 1202
        newItem.setVendorPricesMap(vendorPrices);
2359 ankur.sing 1203
        newItem.setItemStatusDesc(statusDesc.getText().trim());
2427 ankur.sing 1204
 
2126 ankur.sing 1205
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
1206
        Add the instance to map and set the map to the item instance created above.*/
2359 ankur.sing 1207
        Map<String, VendorItemMapping> vendorMappings = new HashMap<String, VendorItemMapping>();
2126 ankur.sing 1208
        VendorItemMapping vMapping;
2427 ankur.sing 1209
        for(int row = 0; row < tableVendorItemKey.getRowCount(); row++) {
2126 ankur.sing 1210
            vMapping = new VendorItemMapping();
2427 ankur.sing 1211
            vMapping.setItemKey(tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY));
1212
            vMapping.setVendorId(Long.parseLong(tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_VENDORID)));
1213
            vendorMappings.put(vMapping.getVendorId() + Item.KEY_SEPARATOR + tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY_OLD), vMapping);
2119 ankur.sing 1214
        }
2359 ankur.sing 1215
        newItem.setVendorKeysMap(vendorMappings);
2119 ankur.sing 1216
 
3558 rajveer 1217
        /*Create an instance of SourcePricings for each row in source pricing table. Set the source prices to the instance.
1218
        Add the instance to map and set the map to the item instance created above.*/
1219
		  Map<Long, SourcePricings> sourcePrices = new HashMap<Long, SourcePricings>();
1220
		  SourcePricings s;
1221
		  for(int row = 0; row < tableSourcePrices.getRowCount(); row++) {
1222
		      s = new SourcePricings();
1223
		      s.setMrp(Double.parseDouble(tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_MRP)));
1224
		      s.setSellingPrice(Double.parseDouble(tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE)));
1225
		      s.setSourceId(Long.parseLong(tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_SOURCE_ID)));
1226
		      sourcePrices.put(s.getSourceId(), s);
1227
		  }
1228
		newItem.setSourcePricesMap(sourcePrices);
1229
 
2126 ankur.sing 1230
        newItem.setContentCategoryId(item.getContentCategoryId());
1231
        newItem.setFeatureId(item.getFeatureId());
1232
        newItem.setFeatureDescription(item.getFeatureDescription());
1233
        newItem.setAddedOn(item.getAddedOn());
1234
        newItem.setRetireDate(item.getRetireDate());
1235
        newItem.setUpdatedOn(item.getUpdatedOn());
1236
        newItem.setItemStatus(item.getItemStatus());
4431 phani.kuma 1237
        newItem.setItemInventory(item.getItemInventory());
4423 phani.kuma 1238
        newItem.setSimilarItems(item.getSimilarItems());
5504 phani.kuma 1239
        newItem.setVouchersMap(item.getVouchersMap());
2119 ankur.sing 1240
 
2126 ankur.sing 1241
        return true;
2066 ankur.sing 1242
    }
1243
 
2427 ankur.sing 1244
    /**
1245
     * This method is called when Edit button is clicked corresponding to a row in 
1246
     * vendor prices table. It will pop up a form to edit the vendor prices.
1247
     * @param vendorId
1248
     * @param row
1249
     */
1250
    private void editVendorPrices(final long vendorId, final int row) {
1251
        String mop = tableVendorPrices.getText(row, TABLE_INDEX_PRICING_MOP);
1252
        String dp = tableVendorPrices.getText(row, TABLE_INDEX_PRICING_DP);
1253
        String tp = tableVendorPrices.getText(row, TABLE_INDEX_PRICING_TP);
2119 ankur.sing 1254
        VendorPricesDialog pricesDialog = new VendorPricesDialog(mop, dp, tp);
2105 ankur.sing 1255
        pricesDialog.updateButton.setText("Update");
2119 ankur.sing 1256
        pricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
2066 ankur.sing 1257
            @Override
2119 ankur.sing 1258
            public boolean onUpdate(double mop, double dp, double tp, long vendorId) {
1259
                if(!validateVendorPrices(mop, dp, tp)) {
2105 ankur.sing 1260
                    return false;
1261
                }
2427 ankur.sing 1262
                tableVendorPrices.setText(row, TABLE_INDEX_PRICING_MOP, mop + "");
1263
                tableVendorPrices.setText(row, TABLE_INDEX_PRICING_DP, dp + "");
1264
                tableVendorPrices.setText(row, TABLE_INDEX_PRICING_TP, tp + "");
2105 ankur.sing 1265
                return true;
2066 ankur.sing 1266
            }
1267
        });
1268
        pricesDialog.show();
1269
    }
2119 ankur.sing 1270
 
2427 ankur.sing 1271
    /**
1272
     * This method is called when Edit button is clicked corresponding to a row in 
3558 rajveer 1273
     * vendor prices table. It will pop up a form to edit the vendor prices.
1274
     * @param vendorId
1275
     * @param row
1276
     */
1277
    private void editSourcePrices(final long sourceId, final int row) {
1278
        String mrp = tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_MRP);
1279
        String sellingPrice = tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE);
1280
        SourcePricesDialog pricesDialog = new SourcePricesDialog(mrp, sellingPrice);
1281
        pricesDialog.updateButton.setText("Update");
1282
        pricesDialog.setSourcePriceUpdateListener(new SourcePricesDialog.SourcePriceUpdateListener() {
1283
            @Override
1284
            public boolean onUpdate(double mrp, double sellingPrice, long sourceId) {
1285
                if(!validateSourcePrices(mrp, sellingPrice)) {
1286
                    return false;
1287
                }
1288
                tableSourcePrices.setText(row, TABLE_INDEX_SOURCE_PRICING_MRP, mrp + "");
1289
                tableSourcePrices.setText(row, TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE, sellingPrice + "");
1290
                return true;
1291
            }
1292
        });
1293
        pricesDialog.show();
1294
    }
1295
 
1296
 
1297
    /**
1298
     * This method is called when Edit button is clicked corresponding to a row in 
2427 ankur.sing 1299
     * vendor item key table. It will pop up a form to edit the item key.
1300
     * @param vendorId
1301
     * @param row
1302
     */
1303
    private void editVendorKey(final long vendorId, final int row) {
1304
        String key = tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY);
2126 ankur.sing 1305
        VendorMappingDialog mappingDialog = new VendorMappingDialog(productGroup.getText().trim(), brand.getText().trim(), 
1306
                modelNumber.getText().trim(), color.getText().trim(), key);
2119 ankur.sing 1307
        mappingDialog.updateButton.setText("Update");
1308
        mappingDialog.setVendorMappingUpdateListener(new VendorMappingDialog.VendorMappingUpdateListener() {
1309
            @Override
1310
            public boolean onUpdate(String itemKey, long vendorId) {
1311
                if(itemKey == null || itemKey.equals("")) {
1312
                    Window.alert("Item key cannot be empty.");
1313
                    return false;
1314
                }
2427 ankur.sing 1315
                tableVendorItemKey.setText(row, TABLE_INDEX_MAPPING_ITEM_KEY, itemKey);
2119 ankur.sing 1316
                return true;
1317
            }
1318
        });
1319
        mappingDialog.show();
1320
    }
2066 ankur.sing 1321
 
2427 ankur.sing 1322
    /**
1323
     * This method compares all the editable UI fields values with attributes in the item object.
1324
     * If they differ, the attribute name is appended to a string.
1325
     * @return String showing attributes which are changed by the user for confirmation.
1326
     *      <br>Empty string if nothing is changed.
1327
     */
2252 ankur.sing 1328
    private String isItemChanged() {
1329
        StringBuilder sb = new StringBuilder("");
5427 amit.gupta 1330
        mandatoryChangedValMap = new HashMap<String, Object>();
1331
        optionalChangedValMap = new HashMap<String, Object>();
2489 ankur.sing 1332
        if(!checkParameterIfEqual(brand.getText().trim(), item.getBrand())) {
5427 amit.gupta 1333
        	mandatoryChangedValMap.put(MANDATORY_BRAND, brand.getText().trim());
2387 ankur.sing 1334
            sb.append("\n-Brand");
2066 ankur.sing 1335
        }
2489 ankur.sing 1336
        if(!checkParameterIfEqual(modelNumber.getText().trim(), item.getModelNumber())) {
5427 amit.gupta 1337
        	mandatoryChangedValMap.put(MANDATORY_MODEL_NO, modelNumber.getText().trim());
2387 ankur.sing 1338
            sb.append("\n-Model Number");
2066 ankur.sing 1339
        }
2489 ankur.sing 1340
        if(!checkParameterIfEqual(modelName.getText().trim(), item.getModelName())) {
5427 amit.gupta 1341
        	mandatoryChangedValMap.put(MANDATORY_MODEL_NAME, modelName.getText().trim());
2387 ankur.sing 1342
            sb.append("\n-Model Name");
2066 ankur.sing 1343
        }
2489 ankur.sing 1344
        if(!checkParameterIfEqual(color.getText().trim(), item.getColor())) {
2387 ankur.sing 1345
            sb.append("\n-Color");
2066 ankur.sing 1346
        }
2489 ankur.sing 1347
        if(!checkParameterIfEqual(statusDesc.getText().trim(), item.getItemStatusDesc())) {
2387 ankur.sing 1348
            sb.append("\n-Status Description");
2359 ankur.sing 1349
        }
2489 ankur.sing 1350
        if(!checkParameterIfEqual(comments.getText().trim(), item.getComments())) {
2387 ankur.sing 1351
            sb.append("\n-Comments");
2066 ankur.sing 1352
        }
2489 ankur.sing 1353
        if(!checkParameterIfEqual(newItem.getMrp(), item.getMrp())) {
5427 amit.gupta 1354
        	mandatoryChangedValMap.put(MANDATORY_MRP, newItem.getMrp());
2387 ankur.sing 1355
            sb.append("\n-MRP");
2066 ankur.sing 1356
        }
2489 ankur.sing 1357
        if(!checkParameterIfEqual(newItem.getSellingPrice(), item.getSellingPrice())) {
5427 amit.gupta 1358
        	optionalChangedValMap.put(OPTIONAL_SELLING_PRICE, newItem.getSellingPrice());
2387 ankur.sing 1359
            sb.append("\n-Selling Price");
2027 ankur.sing 1360
        }
2489 ankur.sing 1361
        if(!checkParameterIfEqual(newItem.getWeight(), item.getWeight())) {
5427 amit.gupta 1362
        	mandatoryChangedValMap.put(MANDATORY_WEIGHT, newItem.getWeight());
2387 ankur.sing 1363
            sb.append("\n-Weight");
2027 ankur.sing 1364
        }
2489 ankur.sing 1365
        if(!checkParameterIfEqual(bestDealsText.getText().trim(), item.getBestDealsText())) {
5427 amit.gupta 1366
        	mandatoryChangedValMap.put(MANDATORY_BEST_DEAL_TEXT, bestDealsText.getText().trim());
2387 ankur.sing 1367
            sb.append("\n-Best Deal Text");
2066 ankur.sing 1368
        }
2489 ankur.sing 1369
        if(!checkParameterIfEqual(newItem.getBestDealsValue(), item.getBestDealsValue())) {
2387 ankur.sing 1370
            sb.append("\n-Best Deal Value");
2027 ankur.sing 1371
        }
2489 ankur.sing 1372
        if(!checkParameterIfEqual(newItem.getBestSellingRank(), item.getBestSellingRank())) {
2387 ankur.sing 1373
            sb.append("\n-Best Selling Rank");
2066 ankur.sing 1374
        }
1375
        if(item.isDefaultForEntity() != defaultForEntity.getValue()) {
2387 ankur.sing 1376
            sb.append("\n-Default For Entity Flag");
2066 ankur.sing 1377
        }
2252 ankur.sing 1378
        if(item.isRisky() != risky.getValue()) {
2387 ankur.sing 1379
            sb.append("\n-Risky Flag");
2252 ankur.sing 1380
        }
2489 ankur.sing 1381
        if(!checkParameterIfEqual(newItem.getStartDate(), item.getStartDate())) {
2387 ankur.sing 1382
            sb.append("\n-Start Date");
2068 ankur.sing 1383
        }
5217 amit.gupta 1384
        if(!checkParameterIfEqual(newItem.getExpectedArrivalDate(), item.getExpectedArrivalDate())) {
1385
        	sb.append("\n-Expected Arrival Date");
1386
        }
1387
        if(!checkParameterIfEqual(newItem.getComingSoonStartDate(), item.getComingSoonStartDate())) {
1388
        	sb.append("\n-Coming Soon Start Date");
1389
        }
3524 chandransh 1390
        if(!checkParameterIfEqual(newItem.getExpectedDelay(), item.getExpectedDelay())) {
3362 chandransh 1391
            sb.append("\n-Expected Delay");
1392
        }
4413 anupam.sin 1393
        if(item.isWarehouseStickiness() != warehouseStickiness.getValue()) {
1394
            sb.append("\n-Warehouse Stickiness Flag");
1395
        }
4506 phani.kuma 1396
        if(!checkParameterIfEqual(newItem.getPreferredVendor(), item.getPreferredVendor())) {
5427 amit.gupta 1397
        	optionalChangedValMap.put(OPTIONAL_PREFERRED_VENDOR, newItem.getPreferredVendor());
4506 phani.kuma 1398
            sb.append("\n-Preferred Vendor");
1399
        }
5384 phani.kuma 1400
        if(item.isHasItemNo() != hasItemNo.getValue()) {
1401
            sb.append("\n-Has Item Number Flag");
1402
        }
1403
        if(item.isItemType() != itemType.getValue()) {
1404
            sb.append("\n-Has Serial Number Flag");
1405
        }
5460 phani.kuma 1406
        if(item.isClearance() != clearance.getValue()) {
1407
            sb.append("\n-Marked For Clearence Sale");
1408
        }
3362 chandransh 1409
 
2126 ankur.sing 1410
        VendorPricings vendorPricings;
2066 ankur.sing 1411
        long vendorId;
5427 amit.gupta 1412
        boolean vendorPricingsChanged = false;
2427 ankur.sing 1413
        for(int row = 0; row < tableVendorPrices.getRowCount(); row++) {
1414
            vendorId = Long.parseLong(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_VENDORID));
2126 ankur.sing 1415
            vendorPricings = item.getVendorPricesMap().get(vendorId);
2359 ankur.sing 1416
            if(vendorPricings == null) {
2387 ankur.sing 1417
                sb.append("\n-Vendor Prices (Vendor:" + vendorId + ")");
5427 amit.gupta 1418
                vendorPricingsChanged = true;
2359 ankur.sing 1419
                continue;
1420
            }
2427 ankur.sing 1421
            if(vendorPricings.getMop() != Double.parseDouble(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_MOP))) {
5427 amit.gupta 1422
            	vendorPricingsChanged = true;
2387 ankur.sing 1423
                sb.append("\n-MOP (Vendor:" + vendorId + ")");
2066 ankur.sing 1424
            }
2427 ankur.sing 1425
            if(vendorPricings.getDealerPrice() != Double.parseDouble(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_DP))) {
5427 amit.gupta 1426
            	vendorPricingsChanged = true;
2387 ankur.sing 1427
                sb.append("\n-Dealer Price (Vendor:" + vendorId + ")");
2066 ankur.sing 1428
            }
2427 ankur.sing 1429
            if(vendorPricings.getTransferPrice() != Double.parseDouble(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_TP))) {
5427 amit.gupta 1430
            	vendorPricingsChanged = true;
2066 ankur.sing 1431
            }
1432
        }
5427 amit.gupta 1433
        if(vendorPricingsChanged){
1434
        	sb.append("\n-Vendor Pricing");
1435
        	optionalChangedValMap.put(OPTIONAL_VENDOR_PRICING, newItem.getVendorPricesMap());
1436
        }
2387 ankur.sing 1437
 
3558 rajveer 1438
        SourcePricings sourcePricings;
5427 amit.gupta 1439
        boolean sourcePricingsChanged = false;
3558 rajveer 1440
        long sourceId;
1441
        for(int row = 0; row < tableSourcePrices.getRowCount(); row++) {
1442
            sourceId = Long.parseLong(tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_SOURCE_ID));
1443
            sourcePricings = item.getSourcePricesMap().get(sourceId);
1444
            if(sourcePricings == null) {
5427 amit.gupta 1445
            	sourcePricingsChanged = true;
3558 rajveer 1446
                sb.append("\n-Source Prices (Source:" + sourceId + ")");
1447
                continue;
1448
            }
1449
            if(sourcePricings.getMrp() != Double.parseDouble(tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_MRP))) {
5427 amit.gupta 1450
            	sourcePricingsChanged = true;
3558 rajveer 1451
                sb.append("\n-MRP (Source:" + sourceId + ")");
1452
            }
1453
            if(sourcePricings.getSellingPrice() != Double.parseDouble(tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE))) {
5427 amit.gupta 1454
            	sourcePricingsChanged = true;
3558 rajveer 1455
                sb.append("\n-Selling Price (Source:" + sourceId + ")");
1456
            }
1457
        }
5427 amit.gupta 1458
        if(sourcePricingsChanged){
1459
        	sb.append("\n-Source Pricing");
1460
        	optionalChangedValMap.put(OPTIONAL_SOURCE_PRICING, newItem.getSourcePricesMap());
1461
        }
3558 rajveer 1462
 
5427 amit.gupta 1463
 
2387 ankur.sing 1464
        VendorItemMapping mapping;
1465
        String old_key, new_key;
2427 ankur.sing 1466
        for(int row = 0; row < tableVendorItemKey.getRowCount(); row++) {
1467
            vendorId = Long.parseLong(tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_VENDORID));
1468
            old_key = tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY_OLD);
1469
            new_key = tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY);
2387 ankur.sing 1470
            mapping = item.getVendorKeysMap().get(vendorId + Item.KEY_SEPARATOR + old_key);
1471
            if(mapping == null || !old_key.equals(new_key)) {
1472
                sb.append("\n-Vendor Key (Vendor:" + vendorId + ",Key: = " + old_key + ")");
1473
                continue;
1474
            }
1475
        }
2252 ankur.sing 1476
        return sb.toString();
2066 ankur.sing 1477
    }
1478
 
2566 chandransh 1479
    @SuppressWarnings("unused")
1480
	private boolean validatePrices() {
2066 ankur.sing 1481
        if(newItem.getSellingPrice() > newItem.getMrp()) {
1482
            Window.alert("Selling price cannot be more than MRP");
1483
            return false;
1484
        }
2119 ankur.sing 1485
        for(VendorPricings v : newItem.getVendorPricesMap().values()) {
2105 ankur.sing 1486
            if(newItem.getMrp() < v.getMop()) {
1487
                Window.alert("MRP cannot be less than MOP. Vendor: " + v.getVendorId());
2066 ankur.sing 1488
                return false;
1489
            }
2105 ankur.sing 1490
            if(v.getTransferPrice() > v.getMop()) {
1491
                Window.alert("Transfer Price cannot be more than MOP. Vendor: " + v.getVendorId());
2066 ankur.sing 1492
                return false;
1493
            }
1494
        }
1495
        return true;
1496
    }
2105 ankur.sing 1497
 
2119 ankur.sing 1498
    private boolean validateVendorPrices(double mop, double dp, double tp) {
2489 ankur.sing 1499
        if(item.getMrp() != null && item.getMrp() < mop) {
2105 ankur.sing 1500
            Window.alert("MOP cannot be more than MRP.");
1501
            return false;
1992 ankur.sing 1502
        }
2105 ankur.sing 1503
        if(tp > mop) {
1504
            Window.alert("Transfer Price cannot be more than MOP.");
1505
            return false;
1992 ankur.sing 1506
        }
2105 ankur.sing 1507
        return true;
1992 ankur.sing 1508
    }
3558 rajveer 1509
 
1510
    private boolean validateSourcePrices(double mrp, double sellingPrice) {
1511
        if(sellingPrice > mrp) {
1512
            Window.alert("Selling Price cannot be more than MRP.");
1513
            return false;
1514
        }
1515
        return true;
1516
    }
1517
 
2105 ankur.sing 1518
 
1519
    public long getItemId() {
1520
        return item == null ? 0 : item.getId();
1521
    }
1522
 
1523
    public Item getItem() {
1524
        return item;
1525
    }
1526
 
5504 phani.kuma 1527
    private boolean voucherExists(String voucherType) {
1528
        for(int i = 0; i < tableVouchers.getRowCount(); i++) {
1529
            if(voucherType.equals(tableVouchers.getText(i, TABLE_INDEX_VOUCHERS_TYPE))) {
1530
                return false;
1531
            }
1532
        }
1533
        return true;
1534
    }
1535
 
2427 ankur.sing 1536
    /**
1537
     * This method is used while adding vendor prices to ensure that there is only one row in the table for a vendor.
1538
     * @param vendorId
1539
     * @return true if parameter vendor Id is already added to vendor prices table.
1540
     *      <br>else false
1541
     */
2105 ankur.sing 1542
    private boolean vendorExists(long vendorId) {
1543
        long id;
2427 ankur.sing 1544
        for(int i = 0; i < tableVendorPrices.getRowCount(); i++) {
1545
            id = Long.parseLong(tableVendorPrices.getText(i, TABLE_INDEX_PRICING_VENDORID));
2105 ankur.sing 1546
            if(vendorId == id) {
1547
                return false;
1548
            }
1992 ankur.sing 1549
        }
2105 ankur.sing 1550
        return true;
1992 ankur.sing 1551
    }
2387 ankur.sing 1552
 
2427 ankur.sing 1553
    /**
3558 rajveer 1554
     * This method is used while adding source prices to ensure that there is only one row in the table for a source.
1555
     * @param sourceId
1556
     * @return true if parameter vendor Id is already added to source prices table.
1557
     *      <br>else false
1558
     */
1559
    private boolean sourceExists(long sourceId) {
1560
        long id;
1561
        for(int i = 0; i < tableSourcePrices.getRowCount(); i++) {
1562
            id = Long.parseLong(tableSourcePrices.getText(i, TABLE_INDEX_SOURCE_PRICING_SOURCE_ID));
1563
            if(sourceId == id) {
1564
                return false;
1565
            }
1566
        }
1567
        return true;
1568
    }
1569
 
1570
    /**
2427 ankur.sing 1571
     * This method is used to check if any of the string item attributes is changed by the user.
2489 ankur.sing 1572
     * @param o1
1573
     * @param o2
2427 ankur.sing 1574
     * @return true if two strings are equal
1575
     *      <br>true if one of them is null and another is empty.
1576
     *      <br>false otherwise
1577
     */
2489 ankur.sing 1578
    private boolean checkParameterIfEqual(Object o1, Object o2) {
1579
        if(o1 == o2) {
2387 ankur.sing 1580
            return true;
1581
        }
2489 ankur.sing 1582
        if(o1 != null && o2 != null && o1.equals(o2)) {
2387 ankur.sing 1583
            return true;
1584
        }
2489 ankur.sing 1585
        if((o1 == null && o2.equals("")) || (o2 == null && o1.equals(""))) {
2387 ankur.sing 1586
            return true;
1587
        }
1588
        return false;
1589
    }
2489 ankur.sing 1590
 
1591
    public void setCatalogDashboardPanel(CatalogDashboard catalogDashboardPanel) {
1592
        this.catalogDashboardPanel = catalogDashboardPanel;
1593
    }
4649 phani.kuma 1594
 
1595
    private String compareSellingPricewithBreakEven() {
1596
    	String message = "false";
1597
    	if(newItem.getWeight() == null) {
1598
    		Window.alert("Weight is empty.");
1599
            return message;
1600
    	}
1601
    	if(newItem.getSellingPrice() == null) {
1602
    		Window.alert("Selling Price is empty.");
1603
            return message;
1604
    	}
1605
 
1606
    	double transferPrice;
1607
    	if(newItem.getPreferredVendor() == null && !newItem.getVendorPricesMap().isEmpty()) {
1608
    		transferPrice = -1;
1609
			for(VendorPricings vendorDetail : newItem.getVendorPricesMap().values()){
1610
				if(transferPrice > vendorDetail.getTransferPrice() || transferPrice == -1){
1611
					transferPrice = vendorDetail.getTransferPrice();
1612
				}
1613
			}
1614
    	}
1615
    	else if(!newItem.getVendorPricesMap().isEmpty() && newItem.getVendorPricesMap().containsKey(newItem.getPreferredVendor())){
1616
    		transferPrice = newItem.getVendorPricesMap().get(newItem.getPreferredVendor()).getTransferPrice();    		
1617
    	}
1618
    	else{
1619
    		Window.alert("Add vendor to Vendor Prices and then change the Selling Price.");
1620
            return message;
1621
    	}
1622
 
1623
    	double weightfactor = Math.ceil((newItem.getWeight() * 1000)/Double.parseDouble(ConfigMap.get("courier_weight_factor")));
1624
		double couriercost = Double.parseDouble(ConfigMap.get("courier_cost_factor")) * weightfactor;
1625
		double costfactor = (Double.parseDouble(ConfigMap.get("transfer_price_percentage")) * transferPrice)/100;
1626
		double breakeven;
1627
		if(costfactor < Double.parseDouble(ConfigMap.get("transfer_price_factor"))){
1628
			breakeven = transferPrice + couriercost + Double.parseDouble(ConfigMap.get("breakeven_additon_factor"));
1629
		}
1630
		else{
1631
			breakeven = (transferPrice + couriercost)/Double.parseDouble(ConfigMap.get("breakeven_divisor"));
1632
		}
1633
 
1634
		if(breakeven > newItem.getSellingPrice()) {
1635
			message = "Selling Price("+newItem.getSellingPrice()+") is less than Breakeven Price("+breakeven+").";
1636
			if(Window.confirm(message)){
1637
				return message;
1638
			}
1639
			else{
1640
				message = "false";
1641
				Window.alert("Updation of Item is canceled.");
1642
				return message;
1643
			}
1644
		}
1645
		else {
1646
			message = "true";
1647
	        return message;
1648
		}
1649
    }
1650
 
1651
    private String checkTransferPriceforPreferredVendor() {
1652
    	String message = "false";
1653
    	if(newItem.getPreferredVendor() == null) {
1654
    		message = "true";
1655
    		return message;
1656
    	}
1657
    	else if(!newItem.getVendorPricesMap().isEmpty() && newItem.getVendorPricesMap().containsKey(newItem.getPreferredVendor())){
1658
    		double transferPrice = newItem.getVendorPricesMap().get(newItem.getPreferredVendor()).getTransferPrice();
1659
        	double mintransferPrice = -1;
1660
        	String minvendor = "";
1661
        	boolean compareTransferPrices = false;
1662
			for(VendorPricings vendorDetail : newItem.getVendorPricesMap().values()){
1663
				if(mintransferPrice > vendorDetail.getTransferPrice() || mintransferPrice == -1){
1664
					mintransferPrice = vendorDetail.getTransferPrice();
1665
					minvendor = Utils.getVendorDesc(vendorDetail.getVendorId());
1666
				}
1667
			}
1668
			if(!checkParameterIfEqual(newItem.getPreferredVendor(), item.getPreferredVendor()) || item.getVendorPricesMap().isEmpty()) {
1669
				compareTransferPrices = true;
1670
			}
1671
			else {
1672
				double oldmintransferPrice = -1;
1673
				for(VendorPricings vendorDetail : item.getVendorPricesMap().values()){
1674
					if(oldmintransferPrice > vendorDetail.getTransferPrice() || oldmintransferPrice == -1){
1675
						oldmintransferPrice = vendorDetail.getTransferPrice();
1676
					}
1677
				}
1678
				if(mintransferPrice < oldmintransferPrice){
1679
					compareTransferPrices = true;
1680
				}
1681
			}
1682
			if(compareTransferPrices && transferPrice > mintransferPrice){
1683
				message = "Transfer Price("+transferPrice+") of Preferred Vendor("+Utils.getVendorDesc(newItem.getPreferredVendor())+") is more than Transfer Price("+mintransferPrice+") of "+minvendor+".";
1684
				if(Window.confirm(message)){
1685
					return message;
1686
				}
1687
				else{
1688
					message = "false";
1689
					Window.alert("Updation of Item is canceled.");
1690
					return message;
1691
				}
1692
			}
1693
			else {
1694
				message = "true";
1695
		        return message;
1696
			}
1697
    	}
1698
    	else{
1699
    		Window.alert("Add vendor to Vendor Prices and then change the Preferred Vendor.");
1700
            return message;
1701
    	}
1702
    }
1703
 
1704
    public void logAuthorization(String message) {
1705
    	String username = catalogDashboardPanel.uname;
1706
    	catalogService.addAuthorizationLog(newItem.getId(), username, message, new AsyncCallback<Boolean>() {
1707
            @Override
1708
            public void onSuccess(Boolean result) {
1709
                if(result) {
1710
                    GWT.log("Event is added");
1711
                }
1712
                else {
1713
                    GWT.log("Error adding the event");
1714
                    Window.alert("Error adding the event");
1715
                }
1716
            }
1717
            @Override
1718
            public void onFailure(Throwable caught) {
1719
                caught.printStackTrace();
1720
                Window.alert("Error while adding the event");
1721
            }
1722
        });
1723
    }
5217 amit.gupta 1724
 
1725
	@Override
1726
	public void setComingSoonStartDate(Date date) {
1727
		this.comingSoonStartDate.setValue(date);
1728
 
1729
	}
1730
 
1731
	@Override
1732
	public void setBestDealsText(String bestDealsText) {
1733
		this.bestDealsText.setValue(bestDealsText);
1734
 
1735
	}
1736
 
1737
	@Override
1738
	public void setExpectedArrivalDate(Date date) {
1739
		this.expectedArrivalDate.setValue(date);
1740
 
1741
	}
1742
 
1743
	@Override
1744
	public String getBestDealsText() {
1745
		return this.bestDealsText.getValue();
1746
	}
1747
 
1748
	@Override
1749
	public Date getComingSoonStartDate() {
1750
		return this.comingSoonStartDate.getValue();
1751
	}
1752
 
1753
	@Override
1754
	public Date getExpectedArrivalDate() {
1755
		return this.expectedArrivalDate.getValue();
1756
	}
1757
	@UiHandler("comingSoonButton")
1758
	void onComingSoonButtonClick(ClickEvent event) {
1759
		ComingSoonDialog cd = new ComingSoonDialog(this);
1760
		cd.show();
1761
	}
5427 amit.gupta 1762
 
1763
	private void createDialog(String changedString) {
1764
        VerticalPanel vp = new VerticalPanel();
1765
        final DialogBox db = new DialogBox();
1766
        db.setText("Changed fields");
1767
 
1768
        vp.add(new Label("Check the fields to update all SKUs with similar entity."));
1769
        vp.add(new Label("--------------------------------------------------------"));
1770
        CheckBox allChecked = new CheckBox("Select all");
1771
        if(optionalChangedValMap.size()>1){
1772
	        allChecked.setName("all");
1773
	        allChecked.addClickHandler(new ClickHandler() {
1774
				@Override
1775
				public void onClick(ClickEvent event) {
1776
					CheckBox cb = (CheckBox)event.getSource();
1777
					VerticalPanel vp = (VerticalPanel)cb.getParent();
1778
					Iterator<Widget> iterator = vp.iterator();
1779
					while(iterator.hasNext()){
1780
						Widget w = iterator.next();
1781
						if(w instanceof CheckBox){
1782
							CheckBox cbox = (CheckBox)w;
1783
							if(cbox.getName()!="all"){
1784
								cbox.setValue(cb.getValue());
1785
							}
1786
						}
1787
					}
1788
					if(cb.getValue()){
1789
						cb.setText("Deselect all");
1790
					}else {
1791
						cb.setText("Select all");
1792
					}
1793
				}
1794
			});
1795
	        vp.add(allChecked);
1796
        }
1797
        String[] changedFields = changedString.split("\n-");
1798
        for(String changeField : changedFields){
1799
        	if(optionalChangedValMap.containsKey(changeField)){
1800
        		CheckBox ch = new CheckBox(changeField); 
1801
        		ch.getElement().getStyle().setPadding(10d, Unit.PX);
1802
        		vp.add(ch);
1803
        	} else {
1804
        		Label l = new Label(changeField);
1805
        		l.getElement().getStyle().setMarginLeft(10d, Unit.PX);
1806
        		l.getElement().getStyle().setPadding(10d, Unit.PX);
1807
        		vp.add(l);
1808
        	}
1809
        }
1810
 
1811
        Button submitButton = new Button();
1812
        submitButton.setText("Update");
1813
        submitButton.addClickHandler(new ClickHandler() {
1814
 
1815
			@Override
1816
			public void onClick(ClickEvent event) {
1817
				Button b  = (Button)event.getSource();
1818
				VerticalPanel vp = (VerticalPanel)b.getParent();
1819
				Iterator<Widget> iterator = vp.iterator();
1820
				while(iterator.hasNext()){
1821
					Widget w = iterator.next();
1822
					if(w instanceof CheckBox){
1823
						CheckBox cbox = (CheckBox)w;
1824
						if(cbox.getName()!="all"){
1825
							if(!cbox.getValue()){
1826
								optionalChangedValMap.remove(cbox.getText());
1827
							}
1828
						}
1829
					}
1830
				}
1831
				db.hide();
1832
				List<Item> items = new ArrayList<Item>();
1833
				items.add(newItem);
1834
				validateNUpdate();
1835
				/*for(Long id : item.getSameItemsWithDifferentColors()){
1836
					catalogService.getItem(id, new AsyncCallback<Item>() {
1837
			            @Override
1838
			            public void onSuccess(Item result) {
1839
			                items.add(e)
1840
			            }
1841
			            @Override
1842
			            public void onFailure(Throwable caught) {
1843
			                caught.printStackTrace();
1844
			                Window.alert("Unable to fetch item details.");
1845
			            }
1846
			        });
1847
				}*/
1848
			}
1849
 
1850
		});
1851
     vp.add(submitButton);
1852
     db.add(vp); 
1853
     db.center();
1854
     db.show();   
1855
    }
1856
 
1857
	private void updateItem(Long itemId) {
1858
		catalogService.getItem(itemId, new AsyncCallback<Item>() {
1859
            @Override
1860
            public void onSuccess(Item result) {
1861
            	final Item res = result;
1862
                if(mandatoryChangedValMap.containsKey(MANDATORY_BRAND)){
1863
                	result.setBrand((String)mandatoryChangedValMap.get(MANDATORY_BRAND));
1864
                }
1865
                if(mandatoryChangedValMap.containsKey(MANDATORY_BEST_DEAL_TEXT)){
1866
                	result.setBestDealsText((String)mandatoryChangedValMap.get(MANDATORY_BEST_DEAL_TEXT));
1867
                }
1868
                if(mandatoryChangedValMap.containsKey(MANDATORY_MODEL_NAME)){
1869
                	result.setModelName((String)mandatoryChangedValMap.get(MANDATORY_MODEL_NAME));
1870
                }
1871
                if(mandatoryChangedValMap.containsKey(MANDATORY_MODEL_NO)){
1872
                	result.setModelNumber((String)mandatoryChangedValMap.get(MANDATORY_MODEL_NO));
1873
                }
1874
                if(mandatoryChangedValMap.containsKey(MANDATORY_WEIGHT)){
1875
                	result.setWeight((Double)mandatoryChangedValMap.get(MANDATORY_WEIGHT));
1876
                }
1877
                if(mandatoryChangedValMap.containsKey(MANDATORY_MRP)){
1878
                	result.setMrp((Double)mandatoryChangedValMap.get(MANDATORY_MRP));
1879
                }
1880
                if(optionalChangedValMap.containsKey(OPTIONAL_PREFERRED_VENDOR)){
1881
                	result.setPreferredVendor((Long)optionalChangedValMap.get(OPTIONAL_PREFERRED_VENDOR));
1882
                }
1883
                if(optionalChangedValMap.containsKey(OPTIONAL_SELLING_PRICE)){
1884
                	result.setSellingPrice((Double)optionalChangedValMap.get(OPTIONAL_SELLING_PRICE));
1885
                }
1886
                if(optionalChangedValMap.containsKey(OPTIONAL_SOURCE_PRICING)){
1887
                	result.setSourcePricesMap((Map<Long, SourcePricings>) optionalChangedValMap.get(OPTIONAL_SOURCE_PRICING));
1888
                }
1889
                if(optionalChangedValMap.containsKey(OPTIONAL_VENDOR_PRICING)){
1890
                	result.setVendorPricesMap((Map<Long, VendorPricings>) optionalChangedValMap.get(OPTIONAL_VENDOR_PRICING));
1891
                }
1892
                catalogService.updateItem(result, new AsyncCallback<Boolean>() {
1893
 
1894
					@Override
1895
					public void onSuccess(Boolean result1) {
1896
						if(result1) {
1897
							Window.alert(res.getId() + " updated Successfully");
1898
						}
1899
 
1900
					}
1901
 
1902
					@Override
1903
					public void onFailure(Throwable caught) {
1904
						caught.printStackTrace();
1905
						Window.alert("Error updating item " + res.getId());
1906
 
1907
					}
1908
				});
1909
            }
1910
            @Override
1911
            public void onFailure(Throwable caught) {
1912
                caught.printStackTrace();
1913
                Window.alert("Unable to fetch item details.");
1914
            }
1915
        });
1916
	}
1961 ankur.sing 1917
}