Subversion Repositories SmartDukaan

Rev

Rev 2138 | Rev 2359 | 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;
2066 ankur.sing 4
import in.shop2020.catalog.dashboard.shared.Utils;
2119 ankur.sing 5
import in.shop2020.catalog.dashboard.shared.VendorItemMapping;
6
import in.shop2020.catalog.dashboard.shared.VendorPricings;
1961 ankur.sing 7
 
8
import java.util.Date;
2066 ankur.sing 9
import java.util.HashMap;
1992 ankur.sing 10
import java.util.Map;
11
import java.util.Map.Entry;
1961 ankur.sing 12
 
13
import com.google.gwt.core.client.GWT;
14
import com.google.gwt.event.dom.client.ClickEvent;
1992 ankur.sing 15
import com.google.gwt.event.dom.client.ClickHandler;
16
import com.google.gwt.resources.client.CssResource;
1961 ankur.sing 17
import com.google.gwt.uibinder.client.UiBinder;
18
import com.google.gwt.uibinder.client.UiField;
19
import com.google.gwt.uibinder.client.UiHandler;
20
import com.google.gwt.user.client.Window;
2126 ankur.sing 21
import com.google.gwt.user.client.rpc.AsyncCallback;
1961 ankur.sing 22
import com.google.gwt.user.client.ui.Button;
2066 ankur.sing 23
import com.google.gwt.user.client.ui.CheckBox;
1992 ankur.sing 24
import com.google.gwt.user.client.ui.FlexTable;
25
import com.google.gwt.user.client.ui.HTMLTable.Cell;
1961 ankur.sing 26
import com.google.gwt.user.client.ui.Label;
27
import com.google.gwt.user.client.ui.ResizeComposite;
28
import com.google.gwt.user.client.ui.TextBox;
29
import com.google.gwt.user.client.ui.Widget;
2068 ankur.sing 30
import com.google.gwt.user.datepicker.client.DateBox;
1961 ankur.sing 31
 
32
public class ItemDetails extends ResizeComposite {
33
 
2119 ankur.sing 34
    private final int TABLE_INDEX_VENDORID = 0, 
35
                      TABLE_INDEX_VENDOR_DESC = 1, 
36
                      TABLE_INDEX_ITEM_KEY = 2,
37
                      TABLE_INDEX_MOP = 2,
38
                      TABLE_INDEX_DP = 3,
39
                      TABLE_INDEX_TP = 4;
40
    private final int TABLE_INDEX_WAREHOUSE_ID = 0,
41
                      TABLE_INDEX_WAREHOUSE_DESC = 1,
42
                      TABLE_INDEX_WAREHOUSE_INV = 2;
2066 ankur.sing 43
 
44
    interface ItemDetailsUiBinder extends UiBinder<Widget, ItemDetails> {}
45
    private static ItemDetailsUiBinder uiBinder = GWT.create(ItemDetailsUiBinder.class);
2126 ankur.sing 46
    private final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
2066 ankur.sing 47
 
1961 ankur.sing 48
 
2066 ankur.sing 49
    interface ItemDetailStyle extends CssResource{
50
        String greenLabel();
51
        String fieldChanged();
52
    }
1961 ankur.sing 53
 
2105 ankur.sing 54
    private Item item, newItem;
2066 ankur.sing 55
 
56
    @UiField ItemDetailStyle style;
57
    @UiField Label itemId;
58
    @UiField TextBox productGroup, brand, modelNumber, modelName, color;
2105 ankur.sing 59
    @UiField Label contentCategory, catalogItemId;
2066 ankur.sing 60
    @UiField TextBox comments;
2105 ankur.sing 61
    @UiField TextBox sellingPrice, mrp, weight;
62
    @UiField Label addedOn, retireDate, updatedOn;
2066 ankur.sing 63
    @UiField Label itemStatus;
64
    @UiField TextBox bestDealsText, bestDealsValue; 
65
    @UiField FlexTable headerAvailability, availabilityTable;
2105 ankur.sing 66
    @UiField FlexTable headerVendorM, vendorTableM;
67
    @UiField FlexTable headerVendor, vendorTable;
2066 ankur.sing 68
    @UiField TextBox bestSellingRank;
2252 ankur.sing 69
    @UiField CheckBox defaultForEntity, risky;
2126 ankur.sing 70
    //@UiField Button submit;
2068 ankur.sing 71
    @UiField DateBox startDate;
2066 ankur.sing 72
 
73
    public ItemDetails(Item item){
2105 ankur.sing 74
        this();
2066 ankur.sing 75
        setItemDetails(item);
76
    }
77
 
78
    public ItemDetails() {
79
        initWidget(uiBinder.createAndBindUi(this));
80
        initAvailabilityHeader();
2119 ankur.sing 81
        initMappingHeader();
2066 ankur.sing 82
        initPricingHeader();
2119 ankur.sing 83
 
2105 ankur.sing 84
        headerAvailability.setVisible(false);
85
        availabilityTable.setVisible(false);
2066 ankur.sing 86
    }
87
 
88
    public void setItemDetails(Item item){
89
        this.item = item;
90
        itemId.setText(item.getId()+"");
91
        productGroup.setText(item.getProductGroup());
92
        brand.setText(item.getBrand());
93
        modelNumber.setText(item.getModelNumber());
94
        modelName.setText(item.getModelName());
95
        color.setText(item.getColor());
96
 
2105 ankur.sing 97
        contentCategory.setText(item.getContentCategory()+"");
2066 ankur.sing 98
        comments.setText(item.getComments());
99
        catalogItemId.setText(item.getCatalogItemId() + "");
100
 
2126 ankur.sing 101
        mrp.setText(item.getMrp() != -1 ? item.getMrp()+"" : "");
102
        sellingPrice.setText(item.getSellingPrice() != -1 ? item.getSellingPrice()+"" : "");
2138 ankur.sing 103
        weight.setText(item.getWeight() != -1 ? item.getWeight()+"" : "");
2068 ankur.sing 104
 
105
        startDate.setValue(new Date(item.getStartDate()));
106
        addedOn.setText(Utils.getDisplayableDate(item.getAddedOn()));
107
        retireDate.setText(Utils.getDisplayableDate(item.getRetireDate()));
108
        updatedOn.setText(Utils.getDisplayableDate(item.getUpdatedOn()));
1992 ankur.sing 109
 
2066 ankur.sing 110
        bestDealsText.setText(item.getBestDealsText());
2126 ankur.sing 111
        bestDealsValue.setText(item.getBestDealsValue() != -1 ? item.getBestDealsValue()+"" : "");
112
        bestSellingRank.setText(item.getBestSellingRank() != -1 ? item.getBestSellingRank()+"" : "");
2066 ankur.sing 113
        defaultForEntity.setValue(item.isDefaultForEntity());
2252 ankur.sing 114
        risky.setValue(item.isRisky());
1992 ankur.sing 115
 
2066 ankur.sing 116
        itemStatus.setText(item.getItemStatus());
117
 
118
        updateAvailabilityTable(item.getAvailability());
2119 ankur.sing 119
        updateMappingTable(item.getVendorMappingsMap());
120
        updatePricingTable(item.getVendorPricesMap());
1992 ankur.sing 121
    }
2066 ankur.sing 122
 
123
    private void initAvailabilityHeader(){
1992 ankur.sing 124
        // Initialize the header.
2066 ankur.sing 125
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_ID, "128px");
2105 ankur.sing 126
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_DESC, "300px");
2066 ankur.sing 127
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_INV, "128px");
128
 
129
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_ID, "Warehouse Id");
130
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_DESC, "Warehouse Desc");
131
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_INV, "Availability");
1992 ankur.sing 132
 
133
    }
2066 ankur.sing 134
 
2119 ankur.sing 135
    private void initMappingHeader(){
136
        // Initialize the header.
137
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
138
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
139
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "250px");
140
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY + 1, "50px");
141
 
142
        headerVendorM.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
143
        headerVendorM.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor Desc");
144
        headerVendorM.setText(0, TABLE_INDEX_ITEM_KEY, "Item Key");
145
 
146
        Button addButton = new Button("Add");
147
        headerVendorM.setWidget(0, TABLE_INDEX_ITEM_KEY + 1, addButton);
148
        addButton.addClickHandler(new ClickHandler() {
149
            @Override
150
            public void onClick(ClickEvent event) {
2126 ankur.sing 151
                VendorMappingDialog vendorMappingDialog = new VendorMappingDialog(productGroup.getText().trim(), brand.getText().trim(), 
152
                        modelNumber.getText().trim(), color.getText().trim());
2119 ankur.sing 153
                vendorMappingDialog.updateButton.setText("Add");
154
                vendorMappingDialog.setVendorMappingUpdateListener(new VendorMappingDialog.VendorMappingUpdateListener() {
155
                    @Override
156
                    public boolean onUpdate(String key, long vendorId) {
157
                        int row = vendorTableM.getRowCount();
158
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
159
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
160
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "250px");
161
 
162
                        vendorTableM.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
163
                        vendorTableM.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
164
                        vendorTableM.setText(row, TABLE_INDEX_ITEM_KEY, key);
165
                        return true;
166
                    }
167
                });
168
                vendorMappingDialog.show();
169
            }
170
        });
171
    }
172
 
173
 
2066 ankur.sing 174
    private void initPricingHeader(){
175
        // Initialize the header.
2105 ankur.sing 176
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
177
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
178
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "250px");
179
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
180
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
181
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
2119 ankur.sing 182
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_TP + 1, "50px");
2066 ankur.sing 183
 
2105 ankur.sing 184
        headerVendor.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
185
        headerVendor.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor Desc");
186
        headerVendor.setText(0, TABLE_INDEX_ITEM_KEY, "Item Key");
187
        headerVendor.setText(0, TABLE_INDEX_MOP, "MOP");
188
        headerVendor.setText(0, TABLE_INDEX_DP, "Dealer Price");
189
        headerVendor.setText(0, TABLE_INDEX_TP, "Transfer Price");
190
 
191
        Button addButton = new Button("Add");
192
        headerVendor.setWidget(0, TABLE_INDEX_TP + 1, addButton);
193
        addButton.addClickHandler(new ClickHandler() {
194
            @Override
195
            public void onClick(ClickEvent event) {
2119 ankur.sing 196
                VendorPricesDialog vendorPricesDialog = new VendorPricesDialog();
197
                vendorPricesDialog.updateButton.setText("Add");
198
                vendorPricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
2105 ankur.sing 199
                    @Override
2119 ankur.sing 200
                    public boolean onUpdate(double mop, double dp, double tp, long vendorId) {
2105 ankur.sing 201
                        if(!vendorExists(vendorId)) {
202
                            Window.alert("Vendor already exists");
203
                            return false;
204
                        }
2119 ankur.sing 205
                        /*if(!validateVendorPrices(mop, dp, tp)) {
2105 ankur.sing 206
                            return false;
2119 ankur.sing 207
                        }*/
2105 ankur.sing 208
                        int row = vendorTable.getRowCount();
209
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
210
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
211
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
212
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
213
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
214
 
215
                        vendorTable.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
216
                        vendorTable.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
217
                        vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
218
                        vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
219
                        vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
220
                        return true;
221
                    }
222
                });
2119 ankur.sing 223
                vendorPricesDialog.show();
2105 ankur.sing 224
            }
225
        });
2066 ankur.sing 226
    }
2105 ankur.sing 227
 
1992 ankur.sing 228
    private void updateAvailabilityTable(Map<Long, Long> availabilityMap){
229
        availabilityTable.removeAllRows();
230
        if(availabilityMap == null) {
231
            return;
232
        }
2066 ankur.sing 233
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_ID, "128px");
2105 ankur.sing 234
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_DESC, "300px");
2066 ankur.sing 235
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_INV, "128px");
236
 
237
 
1992 ankur.sing 238
        int i=0;
239
        for(Entry<Long, Long> availability : availabilityMap.entrySet()){
2066 ankur.sing 240
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_ID, availability.getKey() + "");
241
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_DESC, Utils.getWarehouseDesc(availability.getKey()));
242
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_INV, availability.getValue() + "");
1992 ankur.sing 243
            i++;
244
        }
245
    }
2119 ankur.sing 246
 
247
    private void updateMappingTable(Map<Long, VendorItemMapping> vendorMappingsMap){
248
        vendorTableM.removeAllRows();
249
 
250
        if(vendorMappingsMap == null || vendorMappingsMap.isEmpty()) {
251
            return;
252
        }
253
        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
254
        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
255
        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "250px");
256
        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY + 1, "50px");
2066 ankur.sing 257
 
2119 ankur.sing 258
        int i=0;
259
        for(VendorItemMapping vendorMapping : vendorMappingsMap.values()){
260
            vendorTableM.setText(i, TABLE_INDEX_VENDORID, vendorMapping.getVendorId() + "");
261
            vendorTableM.setText(i, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorMapping.getVendorId()));
262
            vendorTableM.setText(i, TABLE_INDEX_ITEM_KEY, vendorMapping.getItemKey());
263
            Button editButton = new Button("Edit");
264
            vendorTableM.setWidget(i, TABLE_INDEX_ITEM_KEY + 1, editButton);
265
            i++;
266
            editButton.addClickHandler(new ClickHandler() {
267
                @Override
268
                public void onClick(ClickEvent event) {
269
                    Cell cell = vendorTableM.getCellForEvent(event);
270
                    int row = cell.getRowIndex();
271
                    long vendorId = Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_VENDORID));
272
                    updateVendorMapping(vendorId, row);
273
                }
274
            });
275
        }
276
    }
277
 
278
    private void updatePricingTable(Map<Long, VendorPricings> vendorPricingMap){
2105 ankur.sing 279
        vendorTable.removeAllRows();
280
 
2119 ankur.sing 281
        if(vendorPricingMap == null || vendorPricingMap.isEmpty()) {
1992 ankur.sing 282
            return;
283
        }
2105 ankur.sing 284
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
285
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
286
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
287
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
288
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
2119 ankur.sing 289
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP + 1, "50px");
2066 ankur.sing 290
 
291
 
1992 ankur.sing 292
        int i=0;
2119 ankur.sing 293
        for(VendorPricings vendorDetail : vendorPricingMap.values()){
2105 ankur.sing 294
            vendorTable.setText(i, TABLE_INDEX_VENDORID, vendorDetail.getVendorId() + "");
295
            vendorTable.setText(i, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorDetail.getVendorId()));
296
            vendorTable.setText(i, TABLE_INDEX_MOP, vendorDetail.getMop() + "");
297
            vendorTable.setText(i, TABLE_INDEX_DP, vendorDetail.getDealerPrice() + "");
298
            vendorTable.setText(i, TABLE_INDEX_TP, vendorDetail.getTransferPrice() + "");
1992 ankur.sing 299
            Button editButton = new Button("Edit");
2105 ankur.sing 300
            vendorTable.setWidget(i, TABLE_INDEX_TP + 1, editButton);
1992 ankur.sing 301
            i++;
302
            editButton.addClickHandler(new ClickHandler() {
303
                @Override
304
                public void onClick(ClickEvent event) {
2105 ankur.sing 305
                    Cell cell = vendorTable.getCellForEvent(event);
1992 ankur.sing 306
                    int row = cell.getRowIndex();
2105 ankur.sing 307
                    long vendorId = Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID));
1992 ankur.sing 308
                    updateVendorPrices(vendorId, row);
309
                }
310
            });
311
        }
312
    }
2066 ankur.sing 313
 
2126 ankur.sing 314
 
315
    void updateItem() {
316
        if(item == null) {
317
            Window.alert("Please select an item to update.");
318
            return;
319
        }
2066 ankur.sing 320
        try {
2126 ankur.sing 321
            if(!createNewItem()) {
322
                return;
323
            }
2252 ankur.sing 324
            String paramsChanged = isItemChanged();
325
            if(paramsChanged.equals("")) {
2066 ankur.sing 326
                Window.alert("Nothing to update. Please change intended item parameters and try again.");
327
                return;
2252 ankur.sing 328
            } else {
329
                paramsChanged = "You have changed following items.\n" + paramsChanged;
330
                Window.alert(paramsChanged);
2066 ankur.sing 331
            }
332
        } catch(NumberFormatException ex) {
2126 ankur.sing 333
            ex.printStackTrace();
334
            GWT.log("Number format exception");
335
        }
336
        if(!Utils.validateItem(newItem)) {
2066 ankur.sing 337
            return;
338
        }
2126 ankur.sing 339
 
340
        /*if(!validatePrices()) {
341
            return;
342
        }*/
343
        //updateItem();  -- Calling this method above. A new item is created with updated data.
344
        catalogService.updateItem(newItem, new AsyncCallback<Boolean>() {
345
            @Override
346
            public void onSuccess(Boolean result) {
347
                if(result) {
348
                    item = newItem;
349
                    GWT.log("Item updated. Id = " + item.getId());
350
                    Window.alert("Item updated successfully.");
351
                }
352
                else {
353
                    GWT.log("Error updating item");
354
                    Window.alert("Error updating item");
355
                }
356
            }
357
            @Override
358
            public void onFailure(Throwable caught) {
359
                caught.printStackTrace();
360
                Window.alert("Error while updating item");
361
            }
362
        });
2066 ankur.sing 363
    }
2126 ankur.sing 364
 
365
    private boolean createNewItem() {
2066 ankur.sing 366
        newItem = new Item();
367
        newItem.setId(Long.parseLong(itemId.getText()));
368
        newItem.setProductGroup(productGroup.getText().trim());
369
        newItem.setBrand(brand.getText().trim());
370
        newItem.setModelNumber(modelNumber.getText().trim());
371
        newItem.setModelName(modelName.getText().trim());
372
        newItem.setColor(color.getText().trim());
2119 ankur.sing 373
        newItem.setContentCategory(contentCategory.getText());
2066 ankur.sing 374
        newItem.setComments(comments.getText().trim());
375
        newItem.setCatalogItemId(Long.parseLong(catalogItemId.getText()));
2126 ankur.sing 376
 
2068 ankur.sing 377
        try {
2126 ankur.sing 378
            if(!mrp.getText().trim().isEmpty()) {
379
                double mrpValue = Double.parseDouble(mrp.getText().trim());
380
                if(mrpValue <= 0) {
381
                    throw new NumberFormatException("Negative value of MRP");
382
                }
383
                newItem.setMrp(mrpValue);
384
            } else {
385
                newItem.setMrp(-1);
386
            }
2068 ankur.sing 387
        } catch(NumberFormatException ex) {
2126 ankur.sing 388
            Window.alert("Invalid MRP format/value. Value shoule be greater than zero");
389
            return false;
2068 ankur.sing 390
        }
391
        try {
2126 ankur.sing 392
            if(!sellingPrice.getText().trim().isEmpty()) {
393
            double spValue = Double.parseDouble(sellingPrice.getText().trim());
394
            if(spValue <= 0) {
395
                throw new NumberFormatException("Negative value of Selling price");
396
            }
397
            newItem.setSellingPrice(spValue);
398
            } else {
399
                newItem.setSellingPrice(-1);
400
            }
401
        } catch(NumberFormatException ex) {
402
            Window.alert("Invalid Selling Price format/value. Value shoule be greater than zero");
403
            return false;
2068 ankur.sing 404
        }
405
        try {
2126 ankur.sing 406
            if(!weight.getText().trim().isEmpty()) {
407
                double wtValue = Double.parseDouble(weight.getText().trim());
408
                if(wtValue <= 0) {
409
                    throw new NumberFormatException("Negative value of Weight");
410
                }
411
                newItem.setWeight(wtValue);
412
            } else {
413
                newItem.setWeight(-1);
414
            }
2068 ankur.sing 415
        } catch(NumberFormatException ex) {
2126 ankur.sing 416
            Window.alert("Invalid weight format/value. Value shoule be greater than zero");
417
            return false;
2068 ankur.sing 418
        }
2126 ankur.sing 419
        try {
420
            if(!startDate.getTextBox().getText().trim().equals("")) {
421
                newItem.setStartDate(startDate.getValue().getTime());
422
            }
423
        } catch(Exception ex) {
424
            Window.alert("Invalid start date format");
425
            return false;
426
        }
2066 ankur.sing 427
        newItem.setBestDealsText(bestDealsText.getText().trim());
2068 ankur.sing 428
        try {
2126 ankur.sing 429
            if(!bestDealsValue.getText().trim().equals("")) {
430
                double bdValue = Double.parseDouble(bestDealsValue.getText().trim());
431
                if(bdValue < 0) {
432
                    throw new NumberFormatException("Negative value of BestDealValue");
433
                }
434
                newItem.setBestDealsValue(bdValue);
435
            } else {
436
                newItem.setBestDealsValue(-1);
437
            }
2068 ankur.sing 438
        } catch(NumberFormatException ex) {
2126 ankur.sing 439
            Window.alert("Invalid best deal value format");
440
            return false;
2068 ankur.sing 441
        }
442
        try {
2126 ankur.sing 443
            if(!bestSellingRank.getText().trim().equals("")) {
444
                long bsrValue = Long.parseLong(bestSellingRank.getText().trim());
445
                if(bsrValue < 0) {
446
                    throw new NumberFormatException("Negative value of Best Selling Rank");
447
                }
448
                newItem.setBestSellingRank(bsrValue);
449
            } else {
450
                newItem.setBestSellingRank(-1);
451
            }
452
 
453
        } catch(NumberFormatException ex) {
454
            Window.alert("Invalid best selling rank format");
455
            return false;
2068 ankur.sing 456
        }
2066 ankur.sing 457
        newItem.setDefaultForEntity(defaultForEntity.getValue());
2252 ankur.sing 458
        newItem.setRisky(risky.getValue());
2119 ankur.sing 459
 
2126 ankur.sing 460
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
461
          Add the instance to map and set the map to the item instance created above.*/
462
        Map<Long, VendorPricings> vendorPrices = new HashMap<Long, VendorPricings>();
2119 ankur.sing 463
        VendorPricings v;
2105 ankur.sing 464
        for(int row = 0; row < vendorTable.getRowCount(); row++) {
2119 ankur.sing 465
            v = new VendorPricings();
2105 ankur.sing 466
            v.setMop(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP)));
467
            v.setDealerPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP)));
468
            v.setTransferPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP)));
469
            v.setVendorId(Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID)));
2126 ankur.sing 470
            vendorPrices.put(v.getVendorId(), v);
2105 ankur.sing 471
            newItem.setMop(v.getMop());
472
            newItem.setDealerPrice(v.getDealerPrice());
473
            newItem.setTransferPrice(v.getTransferPrice());
2066 ankur.sing 474
        }
2126 ankur.sing 475
        newItem.setVendorPricesMap(vendorPrices);
2027 ankur.sing 476
 
2126 ankur.sing 477
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
478
        Add the instance to map and set the map to the item instance created above.*/
479
        Map<Long, VendorItemMapping> vendorMappings = new HashMap<Long, VendorItemMapping>();
480
        VendorItemMapping vMapping;
2119 ankur.sing 481
        for(int row = 0; row < vendorTableM.getRowCount(); row++) {
2126 ankur.sing 482
            vMapping = new VendorItemMapping();
483
            vMapping.setItemKey(vendorTableM.getText(row, TABLE_INDEX_ITEM_KEY));
484
            vMapping.setVendorId(Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_VENDORID)));
485
            vendorMappings.put(vMapping.getVendorId(), vMapping);
2119 ankur.sing 486
        }
2126 ankur.sing 487
        newItem.setVendorMappingsMap(vendorMappings);
2119 ankur.sing 488
 
2126 ankur.sing 489
        newItem.setContentCategoryId(item.getContentCategoryId());
490
        newItem.setFeatureId(item.getFeatureId());
491
        newItem.setFeatureDescription(item.getFeatureDescription());
492
        newItem.setAddedOn(item.getAddedOn());
493
        newItem.setRetireDate(item.getRetireDate());
494
        newItem.setUpdatedOn(item.getUpdatedOn());
495
        newItem.setItemStatus(item.getItemStatus());
496
        newItem.setOtherInfo(item.getOtherInfo());
497
        newItem.setAvailability(item.getAvailability());
2119 ankur.sing 498
 
2126 ankur.sing 499
        return true;
2066 ankur.sing 500
    }
501
 
502
    private void updateVendorPrices(final long vendorId, final int row) {
2105 ankur.sing 503
        String mop = vendorTable.getText(row, TABLE_INDEX_MOP);
504
        String dp = vendorTable.getText(row, TABLE_INDEX_DP);
505
        String tp = vendorTable.getText(row, TABLE_INDEX_TP);
506
        String key = vendorTable.getText(row, TABLE_INDEX_ITEM_KEY);
2119 ankur.sing 507
        VendorPricesDialog pricesDialog = new VendorPricesDialog(mop, dp, tp);
2105 ankur.sing 508
        pricesDialog.updateButton.setText("Update");
2119 ankur.sing 509
        pricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
2066 ankur.sing 510
            @Override
2119 ankur.sing 511
            public boolean onUpdate(double mop, double dp, double tp, long vendorId) {
512
                if(!validateVendorPrices(mop, dp, tp)) {
2105 ankur.sing 513
                    return false;
514
                }
515
                vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
516
                vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
517
                vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
518
                return true;
2066 ankur.sing 519
            }
520
        });
521
        pricesDialog.show();
522
    }
2119 ankur.sing 523
 
524
    private void updateVendorMapping(final long vendorId, final int row) {
525
        String key = vendorTableM.getText(row, TABLE_INDEX_ITEM_KEY);
2126 ankur.sing 526
        VendorMappingDialog mappingDialog = new VendorMappingDialog(productGroup.getText().trim(), brand.getText().trim(), 
527
                modelNumber.getText().trim(), color.getText().trim(), key);
2119 ankur.sing 528
        mappingDialog.updateButton.setText("Update");
529
        mappingDialog.setVendorMappingUpdateListener(new VendorMappingDialog.VendorMappingUpdateListener() {
530
            @Override
531
            public boolean onUpdate(String itemKey, long vendorId) {
532
                if(itemKey == null || itemKey.equals("")) {
533
                    Window.alert("Item key cannot be empty.");
534
                    return false;
535
                }
536
                vendorTableM.setText(row, TABLE_INDEX_ITEM_KEY, itemKey);
537
                return true;
538
            }
539
        });
540
        mappingDialog.show();
541
    }
2066 ankur.sing 542
 
2252 ankur.sing 543
    private String isItemChanged() {
544
        StringBuilder sb = new StringBuilder("");
2066 ankur.sing 545
        if(!productGroup.getText().trim().equals(item.getProductGroup())) {
2252 ankur.sing 546
            sb.append("\nProduct Group");
2066 ankur.sing 547
        }
548
        if(!brand.getText().trim().equals(item.getBrand())) {
2252 ankur.sing 549
            sb.append("\nBrand");
2066 ankur.sing 550
        }
551
        if(!modelNumber.getText().trim().equals(item.getModelNumber())) {
2252 ankur.sing 552
            sb.append("\nModel Number");
2066 ankur.sing 553
        }
554
        if(!modelName.getText().trim().equals(item.getModelName())) {
2252 ankur.sing 555
            sb.append("\nModel Name");
2066 ankur.sing 556
        }
557
        if(!color.getText().trim().equals(item.getColor())) {
2252 ankur.sing 558
            sb.append("\nColor");
2066 ankur.sing 559
        }
560
        if(!comments.getText().trim().equals(item.getComments())) {
2252 ankur.sing 561
            sb.append("\nComments");
2066 ankur.sing 562
        }
2126 ankur.sing 563
        if(newItem.getMrp() != item.getMrp()) {
2252 ankur.sing 564
            sb.append("\nMRP");
2066 ankur.sing 565
        }
2126 ankur.sing 566
        if(newItem.getSellingPrice() != item.getSellingPrice()) {
2252 ankur.sing 567
            sb.append("\nSelling Price");
2027 ankur.sing 568
        }
2126 ankur.sing 569
        if(newItem.getWeight() != item.getWeight()) {
2252 ankur.sing 570
            sb.append("\nWeight");
2027 ankur.sing 571
        }
2066 ankur.sing 572
        if(!bestDealsText.getText().trim().equals(item.getBestDealsText())) {
2252 ankur.sing 573
            sb.append("\nBest Deal Text");
2066 ankur.sing 574
        }
2126 ankur.sing 575
        if(newItem.getBestDealsValue() != item.getBestDealsValue()) {
2252 ankur.sing 576
            sb.append("\nBest Deal Value");
2027 ankur.sing 577
        }
2126 ankur.sing 578
        if(newItem.getBestSellingRank() != item.getBestSellingRank()) {
2252 ankur.sing 579
            sb.append("\nBest Selling Rank");
2066 ankur.sing 580
        }
581
        if(item.isDefaultForEntity() != defaultForEntity.getValue()) {
2252 ankur.sing 582
            sb.append("\nDefault For Entity Flag");
2066 ankur.sing 583
        }
2252 ankur.sing 584
        if(item.isRisky() != risky.getValue()) {
585
            sb.append("\nRisky Flag");
586
        }
2068 ankur.sing 587
        if(newItem.getStartDate() != item.getStartDate()) {
2252 ankur.sing 588
            sb.append("\nStart Date");
2068 ankur.sing 589
        }
2126 ankur.sing 590
        VendorPricings vendorPricings;
2066 ankur.sing 591
        long vendorId;
2105 ankur.sing 592
        for(int row = 0; row < vendorTable.getRowCount(); row++) {
593
            vendorId = Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID));
2126 ankur.sing 594
            vendorPricings = item.getVendorPricesMap().get(vendorId);
595
            if(vendorPricings.getMop() != Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP))) {
2252 ankur.sing 596
                sb.append("\nMOP (Vendor:" + vendorId + ")");
2066 ankur.sing 597
            }
2126 ankur.sing 598
            if(vendorPricings.getDealerPrice() != Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP))) {
2252 ankur.sing 599
                sb.append("\nDealer Price (Vendor:" + vendorId + ")");
2066 ankur.sing 600
            }
2126 ankur.sing 601
            if(vendorPricings.getTransferPrice() != Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP))) {
2252 ankur.sing 602
                sb.append("\nTransfer Price (Vendor:" + vendorId + ")");
2066 ankur.sing 603
            }
604
        }
2252 ankur.sing 605
        return sb.toString();
2066 ankur.sing 606
    }
607
 
608
    private boolean validatePrices() {
609
        if(newItem.getSellingPrice() > newItem.getMrp()) {
610
            Window.alert("Selling price cannot be more than MRP");
611
            return false;
612
        }
2119 ankur.sing 613
        for(VendorPricings v : newItem.getVendorPricesMap().values()) {
2105 ankur.sing 614
            if(newItem.getMrp() < v.getMop()) {
615
                Window.alert("MRP cannot be less than MOP. Vendor: " + v.getVendorId());
2066 ankur.sing 616
                return false;
617
            }
2105 ankur.sing 618
            if(v.getTransferPrice() > v.getMop()) {
619
                Window.alert("Transfer Price cannot be more than MOP. Vendor: " + v.getVendorId());
2066 ankur.sing 620
                return false;
621
            }
622
        }
623
        return true;
624
    }
2105 ankur.sing 625
 
2119 ankur.sing 626
    private boolean validateVendorPrices(double mop, double dp, double tp) {
2126 ankur.sing 627
        if(item.getMrp() != -1 && item.getMrp() < mop) {
2105 ankur.sing 628
            Window.alert("MOP cannot be more than MRP.");
629
            return false;
1992 ankur.sing 630
        }
2105 ankur.sing 631
        if(tp > mop) {
632
            Window.alert("Transfer Price cannot be more than MOP.");
633
            return false;
1992 ankur.sing 634
        }
2105 ankur.sing 635
        return true;
1992 ankur.sing 636
    }
2105 ankur.sing 637
 
638
    public long getItemId() {
639
        return item == null ? 0 : item.getId();
640
    }
641
 
642
    public Item getItem() {
643
        return item;
644
    }
645
 
646
    private boolean vendorExists(long vendorId) {
647
        long id;
648
        for(int i = 0; i < vendorTable.getRowCount(); i++) {
649
            id = Long.parseLong(vendorTable.getText(i, TABLE_INDEX_VENDORID));
650
            if(vendorId == id) {
651
                return false;
652
            }
1992 ankur.sing 653
        }
2105 ankur.sing 654
        return true;
1992 ankur.sing 655
    }
1961 ankur.sing 656
}