Subversion Repositories SmartDukaan

Rev

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

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