Subversion Repositories SmartDukaan

Rev

Rev 5460 | Rev 5516 | 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
                        }
593
 
594
                        catalogService.addVoucher(item.getCatalogItemId(), voucherType, voucherAmount, new AsyncCallback<Boolean>() {
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) {
873
                        	catalogService.addVoucher(item.getCatalogItemId(), voucherType, voucherAmount, new AsyncCallback<Boolean>() {
874
    	                        @Override
875
    	                        public void onSuccess(Boolean result) {
876
    	                        	if(result) {
877
    	                                GWT.log("Voucher Updated");
878
    	                                VoucherItemMapping newvoucher = new VoucherItemMapping(voucherAmount, voucherType);
879
    	                                Map<String, VoucherItemMapping> newvouchersMap = item.getVouchersMap();
880
    	                                newvouchersMap.remove(voucherType);
881
    	                                newvouchersMap.put(voucherType, newvoucher);
882
    	                                item.setVouchersMap(newvouchersMap);
883
    	                                updateVoucherTable(item.getVouchersMap());
884
    	                                Window.alert("Voucher Updated successfully.");
885
    	                            }
886
    	                            else {
887
    	                                GWT.log("Error Updating Voucher");
888
    	                                Window.alert("Error Updating Voucher");
889
    	                            }
890
    	                        }
891
    	                        @Override
892
    	                        public void onFailure(Throwable caught) {
893
    	                            caught.printStackTrace();
894
    	                            Window.alert("Error Updating voucher");
895
    	                        }
896
    	                    });
897
                            return true;
898
                        }
899
                    });
900
                    voucherDialog.show();
901
                }
902
            });
903
 
904
            Button deleteButton = new Button("Delete");
905
            tableVouchers.setWidget(i, TABLE_INDEX_VOUCHERS_DEL_BUTTON, deleteButton);
906
            deleteButton.addClickHandler(new ClickHandler() {
907
                @Override
908
                public void onClick(ClickEvent event) {
909
                    Cell cell = tableVouchers.getCellForEvent(event);
910
                    final int row = cell.getRowIndex();
911
                    String voucherType = tableVouchers.getText(row, TABLE_INDEX_VOUCHERS_TYPE);
912
 
913
                    catalogService.deleteVoucher(item.getCatalogItemId(), voucherType, new AsyncCallback<Boolean>() {
914
                        @Override
915
                        public void onSuccess(Boolean result) {
916
                            if(result) {
917
                                GWT.log("Voucher deleted");
918
                                String voucherType = tableVouchers.getText(row, TABLE_INDEX_VOUCHERS_TYPE);
919
                                tableVouchers.removeRow(row);
920
                                Map<String, VoucherItemMapping> newvouchersMap = item.getVouchersMap();
921
                                newvouchersMap.remove(voucherType);
922
                                item.setVouchersMap(newvouchersMap);
923
                            }
924
                            else {
925
                                GWT.log("Error deleting Voucher");
926
                                Window.alert("Error deleting Voucher");
927
                            }
928
                        }
929
                        @Override
930
                        public void onFailure(Throwable caught) {
931
                            caught.printStackTrace();
932
                            Window.alert("Error deleting Voucher");
933
                        }
934
                    });
935
                }
936
            });
937
            i++;
938
        }
939
    }
940
 
4423 phani.kuma 941
    /**
2427 ankur.sing 942
     * called on the click event of update item button in ItemActions
943
     */
2126 ankur.sing 944
    void updateItem() {
945
        if(item == null) {
946
            Window.alert("Please select an item to update.");
947
            return;
948
        }
2066 ankur.sing 949
        try {
2126 ankur.sing 950
            if(!createNewItem()) {
951
                return;
952
            }
2066 ankur.sing 953
        } catch(NumberFormatException ex) {
2126 ankur.sing 954
            ex.printStackTrace();
955
            GWT.log("Number format exception");
956
        }
5427 amit.gupta 957
        String paramsChanged = isItemChanged();
958
        if(paramsChanged.equals("")) {
959
        	Window.alert("Nothing to update. Please change intended item parameters and try again.");
960
        	return;
961
        } else {
962
        	if(item.getSameItemsWithDifferentColors().size()>0 && optionalChangedValMap.size()>0 ){
963
        		createDialog(paramsChanged);
964
        	} else {
965
        		paramsChanged = "You have changed following items.\n" + paramsChanged;
966
        		Window.alert(paramsChanged);
967
        		validateNUpdate();
968
        	}
969
        }
970
    }
971
 
972
 
973
    private void validateNUpdate() {
974
    	if(!Utils.validateItem(newItem)) {
2066 ankur.sing 975
            return;
976
        }
2126 ankur.sing 977
 
4649 phani.kuma 978
        final String[] messageList = new String[2];
979
 
980
    	String sellingpricemessage = compareSellingPricewithBreakEven();
981
    	if(sellingpricemessage.equals("false")) {
982
    		return;
983
    	}
984
    	else if(!sellingpricemessage.equals("true")) {
985
    		messageList[0] = sellingpricemessage;
986
    	}
987
 
988
        String preferredvendormessage = checkTransferPriceforPreferredVendor();
989
        if(preferredvendormessage.equals("false")) {
990
        	return;
991
        }
992
        else if(!preferredvendormessage.equals("true")) {
993
        	messageList[1] = preferredvendormessage;
994
        }
995
 
2126 ankur.sing 996
        /*if(!validatePrices()) {
997
            return;
998
        }*/
999
        catalogService.updateItem(newItem, new AsyncCallback<Boolean>() {
1000
            @Override
1001
            public void onSuccess(Boolean result) {
1002
                if(result) {
4649 phani.kuma 1003
                	for(int i = 0; i < messageList.length; i++) {
1004
                		if(messageList[i] != null) {
1005
                			logAuthorization(messageList[i]);
1006
                		}
1007
                	}
5427 amit.gupta 1008
                	if(optionalChangedValMap.size()>0 || mandatoryChangedValMap.size()>0){
1009
	                	for(Long itemId : item.getSameItemsWithDifferentColors()){
1010
	                		updateItem(itemId);
1011
	                	}
1012
                	}
2126 ankur.sing 1013
                    item = newItem;
1014
                    GWT.log("Item updated. Id = " + item.getId());
2489 ankur.sing 1015
                    catalogDashboardPanel.getItemListWidget().updateItem(item);
1016
                    getFreshItemFromDB(item.getId());
2126 ankur.sing 1017
                    Window.alert("Item updated successfully.");
1018
                }
1019
                else {
1020
                    GWT.log("Error updating item");
1021
                    Window.alert("Error updating item");
1022
                }
1023
            }
1024
            @Override
1025
            public void onFailure(Throwable caught) {
1026
                caught.printStackTrace();
1027
                Window.alert("Error while updating item");
1028
            }
1029
        });
2066 ankur.sing 1030
    }
2126 ankur.sing 1031
 
2489 ankur.sing 1032
    private void getFreshItemFromDB(long id) {
1033
        catalogService.getItem(id, new AsyncCallback<Item>() {
1034
            @Override
1035
            public void onSuccess(Item result) {
1036
                setItemDetails(result);
1037
            }
1038
            @Override
1039
            public void onFailure(Throwable caught) {
1040
                caught.printStackTrace();
1041
                Window.alert("Unable to fetch item details.");
1042
            }
1043
        });
1044
    }
1045
 
2427 ankur.sing 1046
    /**
1047
     * This method is called while updating item.<br> It will create a new Item object and set 
1048
     * its editable attributes with UI fields values and non-editable attributes with old Item
1049
     * object attributes. This new Item object is then passed to the service to update item in the database.
1050
     * <br>If update is successful, the old Item object is replaced with the new Item object. 
1051
     * @return true if new Item object is created successfully
1052
     *     <br>false if some error occurs due to NumberFormatException
1053
     */
2126 ankur.sing 1054
    private boolean createNewItem() {
2066 ankur.sing 1055
        newItem = new Item();
2489 ankur.sing 1056
        newItem.setId(item.getId());
2066 ankur.sing 1057
        newItem.setProductGroup(productGroup.getText().trim());
1058
        newItem.setBrand(brand.getText().trim());
1059
        newItem.setModelNumber(modelNumber.getText().trim());
1060
        newItem.setModelName(modelName.getText().trim());
1061
        newItem.setColor(color.getText().trim());
2119 ankur.sing 1062
        newItem.setContentCategory(contentCategory.getText());
2066 ankur.sing 1063
        newItem.setComments(comments.getText().trim());
1064
        newItem.setCatalogItemId(Long.parseLong(catalogItemId.getText()));
2126 ankur.sing 1065
 
2068 ankur.sing 1066
        try {
2126 ankur.sing 1067
            if(!mrp.getText().trim().isEmpty()) {
1068
                double mrpValue = Double.parseDouble(mrp.getText().trim());
1069
                if(mrpValue <= 0) {
1070
                    throw new NumberFormatException("Negative value of MRP");
1071
                }
1072
                newItem.setMrp(mrpValue);
1073
            }
2068 ankur.sing 1074
        } catch(NumberFormatException ex) {
2126 ankur.sing 1075
            Window.alert("Invalid MRP format/value. Value shoule be greater than zero");
1076
            return false;
2068 ankur.sing 1077
        }
1078
        try {
2126 ankur.sing 1079
            if(!sellingPrice.getText().trim().isEmpty()) {
1080
            double spValue = Double.parseDouble(sellingPrice.getText().trim());
1081
            if(spValue <= 0) {
1082
                throw new NumberFormatException("Negative value of Selling price");
1083
            }
1084
            newItem.setSellingPrice(spValue);
1085
            }
1086
        } catch(NumberFormatException ex) {
1087
            Window.alert("Invalid Selling Price format/value. Value shoule be greater than zero");
1088
            return false;
2068 ankur.sing 1089
        }
1090
        try {
2126 ankur.sing 1091
            if(!weight.getText().trim().isEmpty()) {
1092
                double wtValue = Double.parseDouble(weight.getText().trim());
1093
                if(wtValue <= 0) {
1094
                    throw new NumberFormatException("Negative value of Weight");
1095
                }
1096
                newItem.setWeight(wtValue);
1097
            }
2068 ankur.sing 1098
        } catch(NumberFormatException ex) {
2126 ankur.sing 1099
            Window.alert("Invalid weight format/value. Value shoule be greater than zero");
1100
            return false;
2068 ankur.sing 1101
        }
2126 ankur.sing 1102
        try {
1103
            if(!startDate.getTextBox().getText().trim().equals("")) {
1104
                newItem.setStartDate(startDate.getValue().getTime());
1105
            }
1106
        } catch(Exception ex) {
1107
            Window.alert("Invalid start date format");
1108
            return false;
1109
        }
2066 ankur.sing 1110
        newItem.setBestDealsText(bestDealsText.getText().trim());
5217 amit.gupta 1111
        Date comingSoonStartDt  = comingSoonStartDate.getValue();
1112
        Date expectedArrivalDt  = expectedArrivalDate.getValue();
1113
        if(comingSoonStartDt == null){
1114
        	newItem.setComingSoonStartDate(null);
1115
        }else {
1116
        	newItem.setComingSoonStartDate(comingSoonStartDt.getTime());
1117
        }
1118
        if(expectedArrivalDt == null){
1119
        	newItem.setExpectedArrivalDate(null);
1120
        }else {
1121
        	newItem.setExpectedArrivalDate(expectedArrivalDt.getTime());
1122
        }
2068 ankur.sing 1123
        try {
2126 ankur.sing 1124
            if(!bestDealsValue.getText().trim().equals("")) {
1125
                double bdValue = Double.parseDouble(bestDealsValue.getText().trim());
1126
                if(bdValue < 0) {
1127
                    throw new NumberFormatException("Negative value of BestDealValue");
1128
                }
1129
                newItem.setBestDealsValue(bdValue);
1130
            }
2068 ankur.sing 1131
        } catch(NumberFormatException ex) {
2126 ankur.sing 1132
            Window.alert("Invalid best deal value format");
1133
            return false;
2068 ankur.sing 1134
        }
3363 chandransh 1135
 
2068 ankur.sing 1136
        try {
2126 ankur.sing 1137
            if(!bestSellingRank.getText().trim().equals("")) {
1138
                long bsrValue = Long.parseLong(bestSellingRank.getText().trim());
1139
                if(bsrValue < 0) {
1140
                    throw new NumberFormatException("Negative value of Best Selling Rank");
1141
                }
1142
                newItem.setBestSellingRank(bsrValue);
1143
            }
1144
        } catch(NumberFormatException ex) {
1145
            Window.alert("Invalid best selling rank format");
1146
            return false;
2068 ankur.sing 1147
        }
2066 ankur.sing 1148
        newItem.setDefaultForEntity(defaultForEntity.getValue());
2252 ankur.sing 1149
        newItem.setRisky(risky.getValue());
2119 ankur.sing 1150
 
3363 chandransh 1151
        try {
1152
            String expectedDelayText = expectedDelay.getText().trim();
1153
            if(!expectedDelayText.equals("")){
1154
                newItem.setExpectedDelay(Integer.parseInt(expectedDelayText));
1155
            }
1156
        } catch(NumberFormatException nfe) {
1157
            Window.alert("Invalid expected delay");
1158
            return false;
1159
        }
1160
 
4583 phani.kuma 1161
        if((item.getPreferredVendor() == null || item.getVendorPricesMap() == null || item.getVendorPricesMap().isEmpty()) && preferredVendor.getSelectedIndex() == 0) {
1162
        	newItem.setPreferredVendor(item.getPreferredVendor());
1163
        }
1164
    	else {
4506 phani.kuma 1165
    		long vendorId = Utils.getVendorId(preferredVendor.getItemText(preferredVendor.getSelectedIndex()));
1166
            newItem.setPreferredVendor(vendorId);
1167
    	}
1168
 
4413 anupam.sin 1169
        newItem.setWarehouseStickiness(warehouseStickiness.getValue());
5384 phani.kuma 1170
        newItem.setHasItemNo(hasItemNo.getValue());
1171
        newItem.setItemType(itemType.getValue());
5460 phani.kuma 1172
        newItem.setClearance(clearance.getValue());
4413 anupam.sin 1173
 
2126 ankur.sing 1174
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
1175
          Add the instance to map and set the map to the item instance created above.*/
1176
        Map<Long, VendorPricings> vendorPrices = new HashMap<Long, VendorPricings>();
2119 ankur.sing 1177
        VendorPricings v;
2427 ankur.sing 1178
        for(int row = 0; row < tableVendorPrices.getRowCount(); row++) {
2119 ankur.sing 1179
            v = new VendorPricings();
2427 ankur.sing 1180
            v.setMop(Double.parseDouble(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_MOP)));
1181
            v.setDealerPrice(Double.parseDouble(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_DP)));
1182
            v.setTransferPrice(Double.parseDouble(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_TP)));
1183
            v.setVendorId(Long.parseLong(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_VENDORID)));
2126 ankur.sing 1184
            vendorPrices.put(v.getVendorId(), v);
2066 ankur.sing 1185
        }
2126 ankur.sing 1186
        newItem.setVendorPricesMap(vendorPrices);
2359 ankur.sing 1187
        newItem.setItemStatusDesc(statusDesc.getText().trim());
2427 ankur.sing 1188
 
2126 ankur.sing 1189
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
1190
        Add the instance to map and set the map to the item instance created above.*/
2359 ankur.sing 1191
        Map<String, VendorItemMapping> vendorMappings = new HashMap<String, VendorItemMapping>();
2126 ankur.sing 1192
        VendorItemMapping vMapping;
2427 ankur.sing 1193
        for(int row = 0; row < tableVendorItemKey.getRowCount(); row++) {
2126 ankur.sing 1194
            vMapping = new VendorItemMapping();
2427 ankur.sing 1195
            vMapping.setItemKey(tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY));
1196
            vMapping.setVendorId(Long.parseLong(tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_VENDORID)));
1197
            vendorMappings.put(vMapping.getVendorId() + Item.KEY_SEPARATOR + tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY_OLD), vMapping);
2119 ankur.sing 1198
        }
2359 ankur.sing 1199
        newItem.setVendorKeysMap(vendorMappings);
2119 ankur.sing 1200
 
3558 rajveer 1201
        /*Create an instance of SourcePricings for each row in source pricing table. Set the source prices to the instance.
1202
        Add the instance to map and set the map to the item instance created above.*/
1203
		  Map<Long, SourcePricings> sourcePrices = new HashMap<Long, SourcePricings>();
1204
		  SourcePricings s;
1205
		  for(int row = 0; row < tableSourcePrices.getRowCount(); row++) {
1206
		      s = new SourcePricings();
1207
		      s.setMrp(Double.parseDouble(tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_MRP)));
1208
		      s.setSellingPrice(Double.parseDouble(tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE)));
1209
		      s.setSourceId(Long.parseLong(tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_SOURCE_ID)));
1210
		      sourcePrices.put(s.getSourceId(), s);
1211
		  }
1212
		newItem.setSourcePricesMap(sourcePrices);
1213
 
2126 ankur.sing 1214
        newItem.setContentCategoryId(item.getContentCategoryId());
1215
        newItem.setFeatureId(item.getFeatureId());
1216
        newItem.setFeatureDescription(item.getFeatureDescription());
1217
        newItem.setAddedOn(item.getAddedOn());
1218
        newItem.setRetireDate(item.getRetireDate());
1219
        newItem.setUpdatedOn(item.getUpdatedOn());
1220
        newItem.setItemStatus(item.getItemStatus());
4431 phani.kuma 1221
        newItem.setItemInventory(item.getItemInventory());
4423 phani.kuma 1222
        newItem.setSimilarItems(item.getSimilarItems());
5504 phani.kuma 1223
        newItem.setVouchersMap(item.getVouchersMap());
2119 ankur.sing 1224
 
2126 ankur.sing 1225
        return true;
2066 ankur.sing 1226
    }
1227
 
2427 ankur.sing 1228
    /**
1229
     * This method is called when Edit button is clicked corresponding to a row in 
1230
     * vendor prices table. It will pop up a form to edit the vendor prices.
1231
     * @param vendorId
1232
     * @param row
1233
     */
1234
    private void editVendorPrices(final long vendorId, final int row) {
1235
        String mop = tableVendorPrices.getText(row, TABLE_INDEX_PRICING_MOP);
1236
        String dp = tableVendorPrices.getText(row, TABLE_INDEX_PRICING_DP);
1237
        String tp = tableVendorPrices.getText(row, TABLE_INDEX_PRICING_TP);
2119 ankur.sing 1238
        VendorPricesDialog pricesDialog = new VendorPricesDialog(mop, dp, tp);
2105 ankur.sing 1239
        pricesDialog.updateButton.setText("Update");
2119 ankur.sing 1240
        pricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
2066 ankur.sing 1241
            @Override
2119 ankur.sing 1242
            public boolean onUpdate(double mop, double dp, double tp, long vendorId) {
1243
                if(!validateVendorPrices(mop, dp, tp)) {
2105 ankur.sing 1244
                    return false;
1245
                }
2427 ankur.sing 1246
                tableVendorPrices.setText(row, TABLE_INDEX_PRICING_MOP, mop + "");
1247
                tableVendorPrices.setText(row, TABLE_INDEX_PRICING_DP, dp + "");
1248
                tableVendorPrices.setText(row, TABLE_INDEX_PRICING_TP, tp + "");
2105 ankur.sing 1249
                return true;
2066 ankur.sing 1250
            }
1251
        });
1252
        pricesDialog.show();
1253
    }
2119 ankur.sing 1254
 
2427 ankur.sing 1255
    /**
1256
     * This method is called when Edit button is clicked corresponding to a row in 
3558 rajveer 1257
     * vendor prices table. It will pop up a form to edit the vendor prices.
1258
     * @param vendorId
1259
     * @param row
1260
     */
1261
    private void editSourcePrices(final long sourceId, final int row) {
1262
        String mrp = tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_MRP);
1263
        String sellingPrice = tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE);
1264
        SourcePricesDialog pricesDialog = new SourcePricesDialog(mrp, sellingPrice);
1265
        pricesDialog.updateButton.setText("Update");
1266
        pricesDialog.setSourcePriceUpdateListener(new SourcePricesDialog.SourcePriceUpdateListener() {
1267
            @Override
1268
            public boolean onUpdate(double mrp, double sellingPrice, long sourceId) {
1269
                if(!validateSourcePrices(mrp, sellingPrice)) {
1270
                    return false;
1271
                }
1272
                tableSourcePrices.setText(row, TABLE_INDEX_SOURCE_PRICING_MRP, mrp + "");
1273
                tableSourcePrices.setText(row, TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE, sellingPrice + "");
1274
                return true;
1275
            }
1276
        });
1277
        pricesDialog.show();
1278
    }
1279
 
1280
 
1281
    /**
1282
     * This method is called when Edit button is clicked corresponding to a row in 
2427 ankur.sing 1283
     * vendor item key table. It will pop up a form to edit the item key.
1284
     * @param vendorId
1285
     * @param row
1286
     */
1287
    private void editVendorKey(final long vendorId, final int row) {
1288
        String key = tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY);
2126 ankur.sing 1289
        VendorMappingDialog mappingDialog = new VendorMappingDialog(productGroup.getText().trim(), brand.getText().trim(), 
1290
                modelNumber.getText().trim(), color.getText().trim(), key);
2119 ankur.sing 1291
        mappingDialog.updateButton.setText("Update");
1292
        mappingDialog.setVendorMappingUpdateListener(new VendorMappingDialog.VendorMappingUpdateListener() {
1293
            @Override
1294
            public boolean onUpdate(String itemKey, long vendorId) {
1295
                if(itemKey == null || itemKey.equals("")) {
1296
                    Window.alert("Item key cannot be empty.");
1297
                    return false;
1298
                }
2427 ankur.sing 1299
                tableVendorItemKey.setText(row, TABLE_INDEX_MAPPING_ITEM_KEY, itemKey);
2119 ankur.sing 1300
                return true;
1301
            }
1302
        });
1303
        mappingDialog.show();
1304
    }
2066 ankur.sing 1305
 
2427 ankur.sing 1306
    /**
1307
     * This method compares all the editable UI fields values with attributes in the item object.
1308
     * If they differ, the attribute name is appended to a string.
1309
     * @return String showing attributes which are changed by the user for confirmation.
1310
     *      <br>Empty string if nothing is changed.
1311
     */
2252 ankur.sing 1312
    private String isItemChanged() {
1313
        StringBuilder sb = new StringBuilder("");
5427 amit.gupta 1314
        mandatoryChangedValMap = new HashMap<String, Object>();
1315
        optionalChangedValMap = new HashMap<String, Object>();
2489 ankur.sing 1316
        if(!checkParameterIfEqual(brand.getText().trim(), item.getBrand())) {
5427 amit.gupta 1317
        	mandatoryChangedValMap.put(MANDATORY_BRAND, brand.getText().trim());
2387 ankur.sing 1318
            sb.append("\n-Brand");
2066 ankur.sing 1319
        }
2489 ankur.sing 1320
        if(!checkParameterIfEqual(modelNumber.getText().trim(), item.getModelNumber())) {
5427 amit.gupta 1321
        	mandatoryChangedValMap.put(MANDATORY_MODEL_NO, modelNumber.getText().trim());
2387 ankur.sing 1322
            sb.append("\n-Model Number");
2066 ankur.sing 1323
        }
2489 ankur.sing 1324
        if(!checkParameterIfEqual(modelName.getText().trim(), item.getModelName())) {
5427 amit.gupta 1325
        	mandatoryChangedValMap.put(MANDATORY_MODEL_NAME, modelName.getText().trim());
2387 ankur.sing 1326
            sb.append("\n-Model Name");
2066 ankur.sing 1327
        }
2489 ankur.sing 1328
        if(!checkParameterIfEqual(color.getText().trim(), item.getColor())) {
2387 ankur.sing 1329
            sb.append("\n-Color");
2066 ankur.sing 1330
        }
2489 ankur.sing 1331
        if(!checkParameterIfEqual(statusDesc.getText().trim(), item.getItemStatusDesc())) {
2387 ankur.sing 1332
            sb.append("\n-Status Description");
2359 ankur.sing 1333
        }
2489 ankur.sing 1334
        if(!checkParameterIfEqual(comments.getText().trim(), item.getComments())) {
2387 ankur.sing 1335
            sb.append("\n-Comments");
2066 ankur.sing 1336
        }
2489 ankur.sing 1337
        if(!checkParameterIfEqual(newItem.getMrp(), item.getMrp())) {
5427 amit.gupta 1338
        	mandatoryChangedValMap.put(MANDATORY_MRP, newItem.getMrp());
2387 ankur.sing 1339
            sb.append("\n-MRP");
2066 ankur.sing 1340
        }
2489 ankur.sing 1341
        if(!checkParameterIfEqual(newItem.getSellingPrice(), item.getSellingPrice())) {
5427 amit.gupta 1342
        	optionalChangedValMap.put(OPTIONAL_SELLING_PRICE, newItem.getSellingPrice());
2387 ankur.sing 1343
            sb.append("\n-Selling Price");
2027 ankur.sing 1344
        }
2489 ankur.sing 1345
        if(!checkParameterIfEqual(newItem.getWeight(), item.getWeight())) {
5427 amit.gupta 1346
        	mandatoryChangedValMap.put(MANDATORY_WEIGHT, newItem.getWeight());
2387 ankur.sing 1347
            sb.append("\n-Weight");
2027 ankur.sing 1348
        }
2489 ankur.sing 1349
        if(!checkParameterIfEqual(bestDealsText.getText().trim(), item.getBestDealsText())) {
5427 amit.gupta 1350
        	mandatoryChangedValMap.put(MANDATORY_BEST_DEAL_TEXT, bestDealsText.getText().trim());
2387 ankur.sing 1351
            sb.append("\n-Best Deal Text");
2066 ankur.sing 1352
        }
2489 ankur.sing 1353
        if(!checkParameterIfEqual(newItem.getBestDealsValue(), item.getBestDealsValue())) {
2387 ankur.sing 1354
            sb.append("\n-Best Deal Value");
2027 ankur.sing 1355
        }
2489 ankur.sing 1356
        if(!checkParameterIfEqual(newItem.getBestSellingRank(), item.getBestSellingRank())) {
2387 ankur.sing 1357
            sb.append("\n-Best Selling Rank");
2066 ankur.sing 1358
        }
1359
        if(item.isDefaultForEntity() != defaultForEntity.getValue()) {
2387 ankur.sing 1360
            sb.append("\n-Default For Entity Flag");
2066 ankur.sing 1361
        }
2252 ankur.sing 1362
        if(item.isRisky() != risky.getValue()) {
2387 ankur.sing 1363
            sb.append("\n-Risky Flag");
2252 ankur.sing 1364
        }
2489 ankur.sing 1365
        if(!checkParameterIfEqual(newItem.getStartDate(), item.getStartDate())) {
2387 ankur.sing 1366
            sb.append("\n-Start Date");
2068 ankur.sing 1367
        }
5217 amit.gupta 1368
        if(!checkParameterIfEqual(newItem.getExpectedArrivalDate(), item.getExpectedArrivalDate())) {
1369
        	sb.append("\n-Expected Arrival Date");
1370
        }
1371
        if(!checkParameterIfEqual(newItem.getComingSoonStartDate(), item.getComingSoonStartDate())) {
1372
        	sb.append("\n-Coming Soon Start Date");
1373
        }
3524 chandransh 1374
        if(!checkParameterIfEqual(newItem.getExpectedDelay(), item.getExpectedDelay())) {
3362 chandransh 1375
            sb.append("\n-Expected Delay");
1376
        }
4413 anupam.sin 1377
        if(item.isWarehouseStickiness() != warehouseStickiness.getValue()) {
1378
            sb.append("\n-Warehouse Stickiness Flag");
1379
        }
4506 phani.kuma 1380
        if(!checkParameterIfEqual(newItem.getPreferredVendor(), item.getPreferredVendor())) {
5427 amit.gupta 1381
        	optionalChangedValMap.put(OPTIONAL_PREFERRED_VENDOR, newItem.getPreferredVendor());
4506 phani.kuma 1382
            sb.append("\n-Preferred Vendor");
1383
        }
5384 phani.kuma 1384
        if(item.isHasItemNo() != hasItemNo.getValue()) {
1385
            sb.append("\n-Has Item Number Flag");
1386
        }
1387
        if(item.isItemType() != itemType.getValue()) {
1388
            sb.append("\n-Has Serial Number Flag");
1389
        }
5460 phani.kuma 1390
        if(item.isClearance() != clearance.getValue()) {
1391
            sb.append("\n-Marked For Clearence Sale");
1392
        }
3362 chandransh 1393
 
2126 ankur.sing 1394
        VendorPricings vendorPricings;
2066 ankur.sing 1395
        long vendorId;
5427 amit.gupta 1396
        boolean vendorPricingsChanged = false;
2427 ankur.sing 1397
        for(int row = 0; row < tableVendorPrices.getRowCount(); row++) {
1398
            vendorId = Long.parseLong(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_VENDORID));
2126 ankur.sing 1399
            vendorPricings = item.getVendorPricesMap().get(vendorId);
2359 ankur.sing 1400
            if(vendorPricings == null) {
2387 ankur.sing 1401
                sb.append("\n-Vendor Prices (Vendor:" + vendorId + ")");
5427 amit.gupta 1402
                vendorPricingsChanged = true;
2359 ankur.sing 1403
                continue;
1404
            }
2427 ankur.sing 1405
            if(vendorPricings.getMop() != Double.parseDouble(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_MOP))) {
5427 amit.gupta 1406
            	vendorPricingsChanged = true;
2387 ankur.sing 1407
                sb.append("\n-MOP (Vendor:" + vendorId + ")");
2066 ankur.sing 1408
            }
2427 ankur.sing 1409
            if(vendorPricings.getDealerPrice() != Double.parseDouble(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_DP))) {
5427 amit.gupta 1410
            	vendorPricingsChanged = true;
2387 ankur.sing 1411
                sb.append("\n-Dealer Price (Vendor:" + vendorId + ")");
2066 ankur.sing 1412
            }
2427 ankur.sing 1413
            if(vendorPricings.getTransferPrice() != Double.parseDouble(tableVendorPrices.getText(row, TABLE_INDEX_PRICING_TP))) {
5427 amit.gupta 1414
            	vendorPricingsChanged = true;
2066 ankur.sing 1415
            }
1416
        }
5427 amit.gupta 1417
        if(vendorPricingsChanged){
1418
        	sb.append("\n-Vendor Pricing");
1419
        	optionalChangedValMap.put(OPTIONAL_VENDOR_PRICING, newItem.getVendorPricesMap());
1420
        }
2387 ankur.sing 1421
 
3558 rajveer 1422
        SourcePricings sourcePricings;
5427 amit.gupta 1423
        boolean sourcePricingsChanged = false;
3558 rajveer 1424
        long sourceId;
1425
        for(int row = 0; row < tableSourcePrices.getRowCount(); row++) {
1426
            sourceId = Long.parseLong(tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_SOURCE_ID));
1427
            sourcePricings = item.getSourcePricesMap().get(sourceId);
1428
            if(sourcePricings == null) {
5427 amit.gupta 1429
            	sourcePricingsChanged = true;
3558 rajveer 1430
                sb.append("\n-Source Prices (Source:" + sourceId + ")");
1431
                continue;
1432
            }
1433
            if(sourcePricings.getMrp() != Double.parseDouble(tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_MRP))) {
5427 amit.gupta 1434
            	sourcePricingsChanged = true;
3558 rajveer 1435
                sb.append("\n-MRP (Source:" + sourceId + ")");
1436
            }
1437
            if(sourcePricings.getSellingPrice() != Double.parseDouble(tableSourcePrices.getText(row, TABLE_INDEX_SOURCE_PRICING_SELLING_PRICE))) {
5427 amit.gupta 1438
            	sourcePricingsChanged = true;
3558 rajveer 1439
                sb.append("\n-Selling Price (Source:" + sourceId + ")");
1440
            }
1441
        }
5427 amit.gupta 1442
        if(sourcePricingsChanged){
1443
        	sb.append("\n-Source Pricing");
1444
        	optionalChangedValMap.put(OPTIONAL_SOURCE_PRICING, newItem.getSourcePricesMap());
1445
        }
3558 rajveer 1446
 
5427 amit.gupta 1447
 
2387 ankur.sing 1448
        VendorItemMapping mapping;
1449
        String old_key, new_key;
2427 ankur.sing 1450
        for(int row = 0; row < tableVendorItemKey.getRowCount(); row++) {
1451
            vendorId = Long.parseLong(tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_VENDORID));
1452
            old_key = tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY_OLD);
1453
            new_key = tableVendorItemKey.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY);
2387 ankur.sing 1454
            mapping = item.getVendorKeysMap().get(vendorId + Item.KEY_SEPARATOR + old_key);
1455
            if(mapping == null || !old_key.equals(new_key)) {
1456
                sb.append("\n-Vendor Key (Vendor:" + vendorId + ",Key: = " + old_key + ")");
1457
                continue;
1458
            }
1459
        }
2252 ankur.sing 1460
        return sb.toString();
2066 ankur.sing 1461
    }
1462
 
2566 chandransh 1463
    @SuppressWarnings("unused")
1464
	private boolean validatePrices() {
2066 ankur.sing 1465
        if(newItem.getSellingPrice() > newItem.getMrp()) {
1466
            Window.alert("Selling price cannot be more than MRP");
1467
            return false;
1468
        }
2119 ankur.sing 1469
        for(VendorPricings v : newItem.getVendorPricesMap().values()) {
2105 ankur.sing 1470
            if(newItem.getMrp() < v.getMop()) {
1471
                Window.alert("MRP cannot be less than MOP. Vendor: " + v.getVendorId());
2066 ankur.sing 1472
                return false;
1473
            }
2105 ankur.sing 1474
            if(v.getTransferPrice() > v.getMop()) {
1475
                Window.alert("Transfer Price cannot be more than MOP. Vendor: " + v.getVendorId());
2066 ankur.sing 1476
                return false;
1477
            }
1478
        }
1479
        return true;
1480
    }
2105 ankur.sing 1481
 
2119 ankur.sing 1482
    private boolean validateVendorPrices(double mop, double dp, double tp) {
2489 ankur.sing 1483
        if(item.getMrp() != null && item.getMrp() < mop) {
2105 ankur.sing 1484
            Window.alert("MOP cannot be more than MRP.");
1485
            return false;
1992 ankur.sing 1486
        }
2105 ankur.sing 1487
        if(tp > mop) {
1488
            Window.alert("Transfer Price cannot be more than MOP.");
1489
            return false;
1992 ankur.sing 1490
        }
2105 ankur.sing 1491
        return true;
1992 ankur.sing 1492
    }
3558 rajveer 1493
 
1494
    private boolean validateSourcePrices(double mrp, double sellingPrice) {
1495
        if(sellingPrice > mrp) {
1496
            Window.alert("Selling Price cannot be more than MRP.");
1497
            return false;
1498
        }
1499
        return true;
1500
    }
1501
 
2105 ankur.sing 1502
 
1503
    public long getItemId() {
1504
        return item == null ? 0 : item.getId();
1505
    }
1506
 
1507
    public Item getItem() {
1508
        return item;
1509
    }
1510
 
5504 phani.kuma 1511
    private boolean voucherExists(String voucherType) {
1512
        for(int i = 0; i < tableVouchers.getRowCount(); i++) {
1513
            if(voucherType.equals(tableVouchers.getText(i, TABLE_INDEX_VOUCHERS_TYPE))) {
1514
                return false;
1515
            }
1516
        }
1517
        return true;
1518
    }
1519
 
2427 ankur.sing 1520
    /**
1521
     * This method is used while adding vendor prices to ensure that there is only one row in the table for a vendor.
1522
     * @param vendorId
1523
     * @return true if parameter vendor Id is already added to vendor prices table.
1524
     *      <br>else false
1525
     */
2105 ankur.sing 1526
    private boolean vendorExists(long vendorId) {
1527
        long id;
2427 ankur.sing 1528
        for(int i = 0; i < tableVendorPrices.getRowCount(); i++) {
1529
            id = Long.parseLong(tableVendorPrices.getText(i, TABLE_INDEX_PRICING_VENDORID));
2105 ankur.sing 1530
            if(vendorId == id) {
1531
                return false;
1532
            }
1992 ankur.sing 1533
        }
2105 ankur.sing 1534
        return true;
1992 ankur.sing 1535
    }
2387 ankur.sing 1536
 
2427 ankur.sing 1537
    /**
3558 rajveer 1538
     * This method is used while adding source prices to ensure that there is only one row in the table for a source.
1539
     * @param sourceId
1540
     * @return true if parameter vendor Id is already added to source prices table.
1541
     *      <br>else false
1542
     */
1543
    private boolean sourceExists(long sourceId) {
1544
        long id;
1545
        for(int i = 0; i < tableSourcePrices.getRowCount(); i++) {
1546
            id = Long.parseLong(tableSourcePrices.getText(i, TABLE_INDEX_SOURCE_PRICING_SOURCE_ID));
1547
            if(sourceId == id) {
1548
                return false;
1549
            }
1550
        }
1551
        return true;
1552
    }
1553
 
1554
    /**
2427 ankur.sing 1555
     * This method is used to check if any of the string item attributes is changed by the user.
2489 ankur.sing 1556
     * @param o1
1557
     * @param o2
2427 ankur.sing 1558
     * @return true if two strings are equal
1559
     *      <br>true if one of them is null and another is empty.
1560
     *      <br>false otherwise
1561
     */
2489 ankur.sing 1562
    private boolean checkParameterIfEqual(Object o1, Object o2) {
1563
        if(o1 == o2) {
2387 ankur.sing 1564
            return true;
1565
        }
2489 ankur.sing 1566
        if(o1 != null && o2 != null && o1.equals(o2)) {
2387 ankur.sing 1567
            return true;
1568
        }
2489 ankur.sing 1569
        if((o1 == null && o2.equals("")) || (o2 == null && o1.equals(""))) {
2387 ankur.sing 1570
            return true;
1571
        }
1572
        return false;
1573
    }
2489 ankur.sing 1574
 
1575
    public void setCatalogDashboardPanel(CatalogDashboard catalogDashboardPanel) {
1576
        this.catalogDashboardPanel = catalogDashboardPanel;
1577
    }
4649 phani.kuma 1578
 
1579
    private String compareSellingPricewithBreakEven() {
1580
    	String message = "false";
1581
    	if(newItem.getWeight() == null) {
1582
    		Window.alert("Weight is empty.");
1583
            return message;
1584
    	}
1585
    	if(newItem.getSellingPrice() == null) {
1586
    		Window.alert("Selling Price is empty.");
1587
            return message;
1588
    	}
1589
 
1590
    	double transferPrice;
1591
    	if(newItem.getPreferredVendor() == null && !newItem.getVendorPricesMap().isEmpty()) {
1592
    		transferPrice = -1;
1593
			for(VendorPricings vendorDetail : newItem.getVendorPricesMap().values()){
1594
				if(transferPrice > vendorDetail.getTransferPrice() || transferPrice == -1){
1595
					transferPrice = vendorDetail.getTransferPrice();
1596
				}
1597
			}
1598
    	}
1599
    	else if(!newItem.getVendorPricesMap().isEmpty() && newItem.getVendorPricesMap().containsKey(newItem.getPreferredVendor())){
1600
    		transferPrice = newItem.getVendorPricesMap().get(newItem.getPreferredVendor()).getTransferPrice();    		
1601
    	}
1602
    	else{
1603
    		Window.alert("Add vendor to Vendor Prices and then change the Selling Price.");
1604
            return message;
1605
    	}
1606
 
1607
    	double weightfactor = Math.ceil((newItem.getWeight() * 1000)/Double.parseDouble(ConfigMap.get("courier_weight_factor")));
1608
		double couriercost = Double.parseDouble(ConfigMap.get("courier_cost_factor")) * weightfactor;
1609
		double costfactor = (Double.parseDouble(ConfigMap.get("transfer_price_percentage")) * transferPrice)/100;
1610
		double breakeven;
1611
		if(costfactor < Double.parseDouble(ConfigMap.get("transfer_price_factor"))){
1612
			breakeven = transferPrice + couriercost + Double.parseDouble(ConfigMap.get("breakeven_additon_factor"));
1613
		}
1614
		else{
1615
			breakeven = (transferPrice + couriercost)/Double.parseDouble(ConfigMap.get("breakeven_divisor"));
1616
		}
1617
 
1618
		if(breakeven > newItem.getSellingPrice()) {
1619
			message = "Selling Price("+newItem.getSellingPrice()+") is less than Breakeven Price("+breakeven+").";
1620
			if(Window.confirm(message)){
1621
				return message;
1622
			}
1623
			else{
1624
				message = "false";
1625
				Window.alert("Updation of Item is canceled.");
1626
				return message;
1627
			}
1628
		}
1629
		else {
1630
			message = "true";
1631
	        return message;
1632
		}
1633
    }
1634
 
1635
    private String checkTransferPriceforPreferredVendor() {
1636
    	String message = "false";
1637
    	if(newItem.getPreferredVendor() == null) {
1638
    		message = "true";
1639
    		return message;
1640
    	}
1641
    	else if(!newItem.getVendorPricesMap().isEmpty() && newItem.getVendorPricesMap().containsKey(newItem.getPreferredVendor())){
1642
    		double transferPrice = newItem.getVendorPricesMap().get(newItem.getPreferredVendor()).getTransferPrice();
1643
        	double mintransferPrice = -1;
1644
        	String minvendor = "";
1645
        	boolean compareTransferPrices = false;
1646
			for(VendorPricings vendorDetail : newItem.getVendorPricesMap().values()){
1647
				if(mintransferPrice > vendorDetail.getTransferPrice() || mintransferPrice == -1){
1648
					mintransferPrice = vendorDetail.getTransferPrice();
1649
					minvendor = Utils.getVendorDesc(vendorDetail.getVendorId());
1650
				}
1651
			}
1652
			if(!checkParameterIfEqual(newItem.getPreferredVendor(), item.getPreferredVendor()) || item.getVendorPricesMap().isEmpty()) {
1653
				compareTransferPrices = true;
1654
			}
1655
			else {
1656
				double oldmintransferPrice = -1;
1657
				for(VendorPricings vendorDetail : item.getVendorPricesMap().values()){
1658
					if(oldmintransferPrice > vendorDetail.getTransferPrice() || oldmintransferPrice == -1){
1659
						oldmintransferPrice = vendorDetail.getTransferPrice();
1660
					}
1661
				}
1662
				if(mintransferPrice < oldmintransferPrice){
1663
					compareTransferPrices = true;
1664
				}
1665
			}
1666
			if(compareTransferPrices && transferPrice > mintransferPrice){
1667
				message = "Transfer Price("+transferPrice+") of Preferred Vendor("+Utils.getVendorDesc(newItem.getPreferredVendor())+") is more than Transfer Price("+mintransferPrice+") of "+minvendor+".";
1668
				if(Window.confirm(message)){
1669
					return message;
1670
				}
1671
				else{
1672
					message = "false";
1673
					Window.alert("Updation of Item is canceled.");
1674
					return message;
1675
				}
1676
			}
1677
			else {
1678
				message = "true";
1679
		        return message;
1680
			}
1681
    	}
1682
    	else{
1683
    		Window.alert("Add vendor to Vendor Prices and then change the Preferred Vendor.");
1684
            return message;
1685
    	}
1686
    }
1687
 
1688
    public void logAuthorization(String message) {
1689
    	String username = catalogDashboardPanel.uname;
1690
    	catalogService.addAuthorizationLog(newItem.getId(), username, message, new AsyncCallback<Boolean>() {
1691
            @Override
1692
            public void onSuccess(Boolean result) {
1693
                if(result) {
1694
                    GWT.log("Event is added");
1695
                }
1696
                else {
1697
                    GWT.log("Error adding the event");
1698
                    Window.alert("Error adding the event");
1699
                }
1700
            }
1701
            @Override
1702
            public void onFailure(Throwable caught) {
1703
                caught.printStackTrace();
1704
                Window.alert("Error while adding the event");
1705
            }
1706
        });
1707
    }
5217 amit.gupta 1708
 
1709
	@Override
1710
	public void setComingSoonStartDate(Date date) {
1711
		this.comingSoonStartDate.setValue(date);
1712
 
1713
	}
1714
 
1715
	@Override
1716
	public void setBestDealsText(String bestDealsText) {
1717
		this.bestDealsText.setValue(bestDealsText);
1718
 
1719
	}
1720
 
1721
	@Override
1722
	public void setExpectedArrivalDate(Date date) {
1723
		this.expectedArrivalDate.setValue(date);
1724
 
1725
	}
1726
 
1727
	@Override
1728
	public String getBestDealsText() {
1729
		return this.bestDealsText.getValue();
1730
	}
1731
 
1732
	@Override
1733
	public Date getComingSoonStartDate() {
1734
		return this.comingSoonStartDate.getValue();
1735
	}
1736
 
1737
	@Override
1738
	public Date getExpectedArrivalDate() {
1739
		return this.expectedArrivalDate.getValue();
1740
	}
1741
	@UiHandler("comingSoonButton")
1742
	void onComingSoonButtonClick(ClickEvent event) {
1743
		ComingSoonDialog cd = new ComingSoonDialog(this);
1744
		cd.show();
1745
	}
5427 amit.gupta 1746
 
1747
	private void createDialog(String changedString) {
1748
        VerticalPanel vp = new VerticalPanel();
1749
        final DialogBox db = new DialogBox();
1750
        db.setText("Changed fields");
1751
 
1752
        vp.add(new Label("Check the fields to update all SKUs with similar entity."));
1753
        vp.add(new Label("--------------------------------------------------------"));
1754
        CheckBox allChecked = new CheckBox("Select all");
1755
        if(optionalChangedValMap.size()>1){
1756
	        allChecked.setName("all");
1757
	        allChecked.addClickHandler(new ClickHandler() {
1758
				@Override
1759
				public void onClick(ClickEvent event) {
1760
					CheckBox cb = (CheckBox)event.getSource();
1761
					VerticalPanel vp = (VerticalPanel)cb.getParent();
1762
					Iterator<Widget> iterator = vp.iterator();
1763
					while(iterator.hasNext()){
1764
						Widget w = iterator.next();
1765
						if(w instanceof CheckBox){
1766
							CheckBox cbox = (CheckBox)w;
1767
							if(cbox.getName()!="all"){
1768
								cbox.setValue(cb.getValue());
1769
							}
1770
						}
1771
					}
1772
					if(cb.getValue()){
1773
						cb.setText("Deselect all");
1774
					}else {
1775
						cb.setText("Select all");
1776
					}
1777
				}
1778
			});
1779
	        vp.add(allChecked);
1780
        }
1781
        String[] changedFields = changedString.split("\n-");
1782
        for(String changeField : changedFields){
1783
        	if(optionalChangedValMap.containsKey(changeField)){
1784
        		CheckBox ch = new CheckBox(changeField); 
1785
        		ch.getElement().getStyle().setPadding(10d, Unit.PX);
1786
        		vp.add(ch);
1787
        	} else {
1788
        		Label l = new Label(changeField);
1789
        		l.getElement().getStyle().setMarginLeft(10d, Unit.PX);
1790
        		l.getElement().getStyle().setPadding(10d, Unit.PX);
1791
        		vp.add(l);
1792
        	}
1793
        }
1794
 
1795
        Button submitButton = new Button();
1796
        submitButton.setText("Update");
1797
        submitButton.addClickHandler(new ClickHandler() {
1798
 
1799
			@Override
1800
			public void onClick(ClickEvent event) {
1801
				Button b  = (Button)event.getSource();
1802
				VerticalPanel vp = (VerticalPanel)b.getParent();
1803
				Iterator<Widget> iterator = vp.iterator();
1804
				while(iterator.hasNext()){
1805
					Widget w = iterator.next();
1806
					if(w instanceof CheckBox){
1807
						CheckBox cbox = (CheckBox)w;
1808
						if(cbox.getName()!="all"){
1809
							if(!cbox.getValue()){
1810
								optionalChangedValMap.remove(cbox.getText());
1811
							}
1812
						}
1813
					}
1814
				}
1815
				db.hide();
1816
				List<Item> items = new ArrayList<Item>();
1817
				items.add(newItem);
1818
				validateNUpdate();
1819
				/*for(Long id : item.getSameItemsWithDifferentColors()){
1820
					catalogService.getItem(id, new AsyncCallback<Item>() {
1821
			            @Override
1822
			            public void onSuccess(Item result) {
1823
			                items.add(e)
1824
			            }
1825
			            @Override
1826
			            public void onFailure(Throwable caught) {
1827
			                caught.printStackTrace();
1828
			                Window.alert("Unable to fetch item details.");
1829
			            }
1830
			        });
1831
				}*/
1832
			}
1833
 
1834
		});
1835
     vp.add(submitButton);
1836
     db.add(vp); 
1837
     db.center();
1838
     db.show();   
1839
    }
1840
 
1841
	private void updateItem(Long itemId) {
1842
		catalogService.getItem(itemId, new AsyncCallback<Item>() {
1843
            @Override
1844
            public void onSuccess(Item result) {
1845
            	final Item res = result;
1846
                if(mandatoryChangedValMap.containsKey(MANDATORY_BRAND)){
1847
                	result.setBrand((String)mandatoryChangedValMap.get(MANDATORY_BRAND));
1848
                }
1849
                if(mandatoryChangedValMap.containsKey(MANDATORY_BEST_DEAL_TEXT)){
1850
                	result.setBestDealsText((String)mandatoryChangedValMap.get(MANDATORY_BEST_DEAL_TEXT));
1851
                }
1852
                if(mandatoryChangedValMap.containsKey(MANDATORY_MODEL_NAME)){
1853
                	result.setModelName((String)mandatoryChangedValMap.get(MANDATORY_MODEL_NAME));
1854
                }
1855
                if(mandatoryChangedValMap.containsKey(MANDATORY_MODEL_NO)){
1856
                	result.setModelNumber((String)mandatoryChangedValMap.get(MANDATORY_MODEL_NO));
1857
                }
1858
                if(mandatoryChangedValMap.containsKey(MANDATORY_WEIGHT)){
1859
                	result.setWeight((Double)mandatoryChangedValMap.get(MANDATORY_WEIGHT));
1860
                }
1861
                if(mandatoryChangedValMap.containsKey(MANDATORY_MRP)){
1862
                	result.setMrp((Double)mandatoryChangedValMap.get(MANDATORY_MRP));
1863
                }
1864
                if(optionalChangedValMap.containsKey(OPTIONAL_PREFERRED_VENDOR)){
1865
                	result.setPreferredVendor((Long)optionalChangedValMap.get(OPTIONAL_PREFERRED_VENDOR));
1866
                }
1867
                if(optionalChangedValMap.containsKey(OPTIONAL_SELLING_PRICE)){
1868
                	result.setSellingPrice((Double)optionalChangedValMap.get(OPTIONAL_SELLING_PRICE));
1869
                }
1870
                if(optionalChangedValMap.containsKey(OPTIONAL_SOURCE_PRICING)){
1871
                	result.setSourcePricesMap((Map<Long, SourcePricings>) optionalChangedValMap.get(OPTIONAL_SOURCE_PRICING));
1872
                }
1873
                if(optionalChangedValMap.containsKey(OPTIONAL_VENDOR_PRICING)){
1874
                	result.setVendorPricesMap((Map<Long, VendorPricings>) optionalChangedValMap.get(OPTIONAL_VENDOR_PRICING));
1875
                }
1876
                catalogService.updateItem(result, new AsyncCallback<Boolean>() {
1877
 
1878
					@Override
1879
					public void onSuccess(Boolean result1) {
1880
						if(result1) {
1881
							Window.alert(res.getId() + " updated Successfully");
1882
						}
1883
 
1884
					}
1885
 
1886
					@Override
1887
					public void onFailure(Throwable caught) {
1888
						caught.printStackTrace();
1889
						Window.alert("Error updating item " + res.getId());
1890
 
1891
					}
1892
				});
1893
            }
1894
            @Override
1895
            public void onFailure(Throwable caught) {
1896
                caught.printStackTrace();
1897
                Window.alert("Unable to fetch item details.");
1898
            }
1899
        });
1900
	}
1961 ankur.sing 1901
}