Subversion Repositories SmartDukaan

Rev

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

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