Subversion Repositories SmartDukaan

Rev

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