Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
2066 ankur.sing 1
package in.shop2020.catalog.dashboard.client;
2
 
2105 ankur.sing 3
import in.shop2020.catalog.dashboard.shared.Item;
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;
5217 amit.gupta 7
import java.util.Date;
2105 ankur.sing 8
import java.util.HashMap;
9
import java.util.Map;
4725 phani.kuma 10
import java.util.Map.Entry;
2105 ankur.sing 11
import com.google.gwt.core.client.GWT;
12
import com.google.gwt.event.dom.client.ClickEvent;
13
import com.google.gwt.event.dom.client.ClickHandler;
14
import com.google.gwt.uibinder.client.UiBinder;
15
import com.google.gwt.uibinder.client.UiField;
16
import com.google.gwt.uibinder.client.UiHandler;
17
import com.google.gwt.user.client.Window;
18
import com.google.gwt.user.client.rpc.AsyncCallback;
19
import com.google.gwt.user.client.ui.Button;
20
import com.google.gwt.user.client.ui.CheckBox;
21
import com.google.gwt.user.client.ui.DialogBox;
22
import com.google.gwt.user.client.ui.FlexTable;
23
import com.google.gwt.user.client.ui.ListBox;
24
import com.google.gwt.user.client.ui.TextBox;
25
import com.google.gwt.user.client.ui.Widget;
26
import com.google.gwt.user.datepicker.client.DateBox;
27
 
2427 ankur.sing 28
/**
29
 * ItemForm is a DialogBox which is created on the click of Add Item button in ItemActions.
30
 * This contains UI fields to fill up item attributes. It also contains tables for adding
31
 * vendor prices and vendor mapping keys.
32
 *
33
 */
5217 amit.gupta 34
public class ItemForm extends DialogBox implements ComingSoon{
2105 ankur.sing 35
 
8901 vikram.rag 36
	interface ItemFormUiBinder extends UiBinder<Widget, ItemForm> {}
37
	private static ItemFormUiBinder uiBinder = GWT.create(ItemFormUiBinder.class);
18048 kshitij.so 38
 
2105 ankur.sing 39
 
8901 vikram.rag 40
	private final static CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
41
	private final int TABLE_INDEX_VENDORID = 0,
42
	TABLE_INDEX_VENDOR_DESC = 1, 
43
	TABLE_INDEX_ITEM_KEY = 2,
44
	TABLE_INDEX_MOP = 2,
45
	TABLE_INDEX_DP = 3,
46
	TABLE_INDEX_TP = 4,
47
	TABLE_INDEX_NLC = 5;
2119 ankur.sing 48
 
8901 vikram.rag 49
	private static boolean entityIdMandatory = Utils.isEntityIdMandatory();
2105 ankur.sing 50
 
8901 vikram.rag 51
	@UiField TextBox productGroup, catalogItemId,existingItemId;
52
	@UiField TextBox brand, modelNumber, modelName, color, comments,holdInventory,defaultInventory;
18048 kshitij.so 53
	@UiField TextBox mrp, sellingPrice, weight, bestDealText, bestDealValue, bestSellingRank,bestDealsDetailsText,bestDealsDetailsLink, minStockLevel, numOfDaysStock, freebieItemId,asin,packQuantity;
8901 vikram.rag 54
	@UiField DateBox startDate, retireDate, comingSoonStartDate, expectedArrivalDate;
55
	@UiField Button addButton, cancelButton, comingSoonButton,searchButton;
56
	@UiField CheckBox defaultForEntity, risky, itemType, hasItemNo, clearance, showSellingPrice;
57
	@UiField FlexTable headerVendor, vendorTable;
58
	@UiField FlexTable headerVendorM, vendorTableM;
59
	@UiField ListBox preferredVendor,preferredInsurer;
60
 
61
	public ItemForm(){
62
		setText("Add New Item");
63
		setWidget(uiBinder.createAndBindUi(this));
64
		initVendorKeysHeader();
65
		initVendorPricesHeader();
66
		preferredVendor.addItem("select");
67
		for(Entry<Long, String> e : Utils.getAllVendors().entrySet()){
68
			preferredVendor.addItem(e.getValue());
69
		}
70
		if(!entityIdMandatory) {
71
			catalogItemId.setEnabled(false);
72
		}
73
		preferredInsurer.addItem("select", "0");
74
		for(Entry<Long, String> e : Utils.getAllInsurers().entrySet()){
75
			preferredInsurer.addItem(e.getValue(),e.getKey().toString());
76
		}
77
 
78
	}
79
 
80
	/**
81
	 * initialises vendor item key table header. Creates an Add button and
82
	 * adds click event listener to it to create and pop up a dialog for adding 
83
	 * a new vendor item key. 
84
	 */
85
	private void initVendorKeysHeader(){
86
		headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
87
		headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
88
		headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "400px");
89
		headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY + 1, "50px");
90
 
91
		headerVendorM.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
92
		headerVendorM.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor");
93
		headerVendorM.setText(0, TABLE_INDEX_ITEM_KEY, "Item Key");
94
 
95
		headerVendorM.getCellFormatter().setVisible(0, TABLE_INDEX_VENDORID, false);
96
 
97
		Button addButton = new Button("Add");
98
		headerVendorM.setWidget(0, TABLE_INDEX_ITEM_KEY + 1, addButton);
99
		addButton.addClickHandler(new ClickHandler() {
100
			@Override
101
			public void onClick(ClickEvent event) {
102
				VendorMappingDialog vendorMappingDialog = new VendorMappingDialog(productGroup.getText().trim(), brand.getText().trim(), 
103
						modelNumber.getText().trim(), color.getText().trim());
104
				vendorMappingDialog.updateButton.setText("Add");
105
				vendorMappingDialog.setVendorMappingUpdateListener(new VendorMappingDialog.VendorMappingUpdateListener() {
106
					@Override
107
					public boolean onUpdate(String key, long vendorId) {
108
						final int row = vendorTableM.getRowCount();
109
						vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
110
						vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
111
						vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "400px");
112
						vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY + 1, "50px");
113
 
114
						vendorTableM.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
115
						vendorTableM.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
116
						vendorTableM.setText(row, TABLE_INDEX_ITEM_KEY, key);
117
						Button removeButton = new Button("X");
118
						vendorTableM.setWidget(row, TABLE_INDEX_ITEM_KEY + 1, removeButton);
119
 
120
						vendorTableM.getCellFormatter().setVisible(row, TABLE_INDEX_VENDORID, false);
121
						removeButton.addClickHandler(new ClickHandler() {
122
							@Override
123
							public void onClick(ClickEvent event) {
124
								vendorTableM.removeRow(row);
125
							}
126
						});
127
						return true;
128
					}
129
				});
130
				vendorMappingDialog.show();
131
			}
132
		});
133
	}
134
 
135
	/**
136
	 * initialises vendor prices table header. Creates an Add button and
137
	 * adds click event listener to it to create and pop up a dialog for adding 
138
	 * a prices for a new vendor 
139
	 */
140
	private void initVendorPricesHeader(){
141
		headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
142
		headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
143
		headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
144
		headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
145
		headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
146
		headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_NLC, "128px");
147
		headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_NLC + 1, "50px");
148
 
149
		headerVendor.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
150
		headerVendor.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor");
151
		headerVendor.setText(0, TABLE_INDEX_MOP, "MOP");
152
		headerVendor.setText(0, TABLE_INDEX_DP, "Dealer Price");
153
		headerVendor.setText(0, TABLE_INDEX_TP, "Transfer Price");
154
		headerVendor.setText(0, TABLE_INDEX_NLC, "NLC");
155
 
156
		headerVendor.getCellFormatter().setVisible(0, TABLE_INDEX_VENDORID, false);
157
 
158
		Button addButton = new Button("Add");
159
		headerVendor.setWidget(0, TABLE_INDEX_NLC + 1, addButton);
160
		addButton.addClickHandler(new ClickHandler() {
161
			@Override
162
			public void onClick(ClickEvent event) {
163
				VendorPricesDialog vendorPricesDialog = new VendorPricesDialog();
164
				vendorPricesDialog.updateButton.setText("Add");
165
				vendorPricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
166
					@Override
167
					public boolean onUpdate(double mop, double dp, double tp, double nlc, long vendorId) {
168
						if(!vendorExists(vendorId)) {
169
							Window.alert("Vendor already exists");
170
							return false;
171
						}
172
						/*if(!validateVendorPrices(mop, dp, tp)) {
2119 ankur.sing 173
                            return false;
174
                        }*/
8901 vikram.rag 175
						final int row = vendorTable.getRowCount();
176
						vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
177
						vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
178
						vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
179
						vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
180
						vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
181
						vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_NLC, "128px");
182
						vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_NLC + 1, "50px");
2105 ankur.sing 183
 
8901 vikram.rag 184
						vendorTable.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
185
						vendorTable.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
186
						vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
187
						vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
188
						vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
189
						vendorTable.setText(row, TABLE_INDEX_NLC, nlc + "");
190
 
191
						vendorTable.getCellFormatter().setVisible(row, TABLE_INDEX_VENDORID, false);
192
 
193
						Button removeButton = new Button("X");
194
						vendorTable.setWidget(row, TABLE_INDEX_NLC + 1, removeButton);
195
						removeButton.addClickHandler(new ClickHandler() {
196
							@Override
197
							public void onClick(ClickEvent event) {
198
								vendorTable.removeRow(row);
199
							}
200
						});
201
						return true;
202
					}
203
				});
204
				vendorPricesDialog.show();
205
			}
206
		});
207
	}
208
	@UiHandler("searchButton")
209
	/**
210
	 * On search button click the item id is searched if it exists then the attributes of the item id are filled into the item form.
211
	 * Else an alert is displayed showing item id does not exists.
212
	 */
213
	void searchItem(ClickEvent event){
214
		preferredVendor.clear();
215
		preferredInsurer.clear();  
216
		catalogItemId.setText(null);
217
		productGroup.setText(null);
218
		brand.setText(null);
219
		modelNumber.setText(null);
220
		modelName.setText(null);
221
		comments.setText(null);
222
		mrp.setText(null);
223
		sellingPrice.setText(null);
224
		weight.setText(null);
225
		hasItemNo.setValue(null);
226
		itemType.setValue(null);
227
		showSellingPrice.setValue(null);
228
		startDate.setValue(null);
229
		this.comingSoonStartDate.setValue(null);
230
		this.expectedArrivalDate.setValue(null);
231
		bestDealText.setText(null);
232
		bestDealsDetailsText.setText(null);
233
		bestDealsDetailsLink.setText(null);
234
		bestDealValue.setText(null);
235
		showSellingPrice.setValue(null);
236
		bestSellingRank.setText(null);
237
		minStockLevel.setText(null);
238
		numOfDaysStock.setText(null);
239
		defaultForEntity.setValue(null);
240
		freebieItemId.setText(null);
241
		asin.setText(null);
242
		holdInventory.setText(null);
243
		defaultInventory.setText(null);
244
		color.setText(null);
18048 kshitij.so 245
		vendorTable.removeAllRows();
246
		packQuantity.setText(null);
8901 vikram.rag 247
		if(!existingItemId.getText().trim().isEmpty()){
248
			long id = Long.parseLong(existingItemId.getText().trim());
249
 
250
			if(id <= 0) {
251
				throw new NumberFormatException("Invalid value of Item Id");
252
			}
253
			else{
254
				catalogService.getItem(id, new AsyncCallback<Item>() {
255
					@Override
256
					public void onFailure(Throwable caught) {
257
						Window.alert("Unable to fetch item details.");
258
					}
259
					@Override
260
					public void onSuccess(Item result) {
261
						if(result !=null){
262
							fillItemForm(result);
263
						}
264
						else{
265
							Window.alert("Invalid Item Id not found");
266
						}
267
					}
268
				});
269
 
270
			}
271
		}
272
		else{
273
			Window.alert("Please enter item id");
274
			GWT.log("Please enter an item id ");
275
		}
276
	}
277
 
278
 
279
	/**
280
	 * This method is called if the valid item is returned and it populates the item entries in Item Form.
281
	 * 
282
	 */
283
	public void fillItemForm(Item item){
284
		int index = 0;
285
		preferredVendor.addItem("select");
286
        if (item.getPreferredVendor() == null) {
287
        	preferredVendor.setSelectedIndex(index);
288
        }
2105 ankur.sing 289
 
8901 vikram.rag 290
        if((item.getVendorPricesMap() == null || item.getVendorPricesMap().isEmpty())) {
291
        	if(item.getPreferredVendor() != null) {
292
        		index++;
293
        		preferredVendor.addItem(Utils.getVendorDesc(item.getPreferredVendor()));
294
        	}
4762 phani.kuma 295
        }
296
        else {
8901 vikram.rag 297
        	for(VendorPricings vendorDetail : item.getVendorPricesMap().values()){
298
        		index++;
299
        		preferredVendor.addItem(Utils.getVendorDesc(vendorDetail.getVendorId()));
300
        		if(item.getPreferredVendor() != null && item.getPreferredVendor() == vendorDetail.getVendorId()) {
301
	        		preferredVendor.setSelectedIndex(index);
302
	        	}
303
        	}
304
 
305
        	if(item.getPreferredVendor() != null && !item.getVendorPricesMap().containsKey(item.getPreferredVendor())){
306
        		index++;
307
            	preferredVendor.addItem(Utils.getVendorDesc(item.getPreferredVendor()));
308
            	preferredVendor.setSelectedIndex(index);
309
        	}
4762 phani.kuma 310
        }
8901 vikram.rag 311
        index=0;
312
        preferredInsurer.addItem("select","0");
313
        preferredInsurer.setSelectedIndex(0);
314
    	for (Map.Entry<Long, String> entry : Utils.getAllInsurers().entrySet()){
315
    		index++;
316
    		preferredInsurer.addItem(entry.getValue(),entry.getKey().toString());
317
    		if(item.getPreferredInsurer()==entry.getKey()){
318
    			preferredInsurer.setSelectedIndex(index);  
319
    		}
4762 phani.kuma 320
    	}
8901 vikram.rag 321
		catalogItemId.setText(Long.toString(item.getCatalogItemId()));
322
		productGroup.setText(item.getProductGroup());
323
		brand.setText(item.getBrand());
324
		modelNumber.setText(item.getModelNumber());
325
		modelName.setText(item.getModelName());
326
		comments.setText(item.getComments());
327
		mrp.setText(item.getMrp() != null ? item.getMrp()+"" : "");
18048 kshitij.so 328
		packQuantity.setText(item.getPackQuantity().toString());
8901 vikram.rag 329
		sellingPrice.setText(item.getSellingPrice() != null ? item.getSellingPrice()+"" : "");
330
		weight.setText(item.getWeight() != null ? item.getWeight()+"" : "");
331
		hasItemNo.setValue(item.isHasItemNo());
332
		itemType.setValue(item.isItemType());
333
		showSellingPrice.setValue(item.isShowSellingPrice());
334
		startDate.setValue(new Date(item.getStartDate()));
335
		if(item.getComingSoonStartDate() != null){
336
			this.comingSoonStartDate.setValue(new Date(item.getComingSoonStartDate()));
337
		}else {
338
			this.comingSoonStartDate.setValue(null);
339
		}
340
		if(item.getExpectedArrivalDate() != null){
341
			this.expectedArrivalDate.setValue(new Date(item.getExpectedArrivalDate()));
342
		}else {
343
			this.expectedArrivalDate.setValue(null);
344
		}
345
		bestDealText.setText(item.getBestDealsText());
346
		bestDealsDetailsText.setText(item.getBestDealsDetailsText());
347
		bestDealsDetailsLink.setText(item.getBestDealsDetailsLink());
348
		bestDealValue.setText(item.getBestDealsValue() != null ? item.getBestDealsValue()+"" : "");
349
		showSellingPrice.setValue(item.isShowSellingPrice());
350
		bestSellingRank.setText(item.getBestSellingRank() != null ? item.getBestSellingRank()+"" : "");
351
		minStockLevel.setText(item.getMinStockLevel() != null ? item.getMinStockLevel()+"" : "");
352
		numOfDaysStock.setText(item.getNumOfDaysStock() != null ? item.getNumOfDaysStock()+"" : "");
353
		defaultForEntity.setValue(item.isDefaultForEntity());
354
		freebieItemId.setText(item.getFreebieItemId().toString());
355
		holdInventory.setText(Long.toString(item.getHoldInventory()));
356
		defaultInventory.setText(Long.toString(item.getDefaultInventory()));
357
		Map<Long, VendorPricings> map = item.getVendorPricesMap();
358
		for (Map.Entry<Long, VendorPricings> vendorItem : map.entrySet()) {
359
			final int row = vendorTable.getRowCount();
360
			long vendorId = vendorItem.getKey();
361
			double mop = vendorItem.getValue().getMop();
362
			double dp  = vendorItem.getValue().getDealerPrice();
363
			double tp =  vendorItem.getValue().getTransferPrice();
364
			double nlc = vendorItem.getValue().getNlc();
365
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
366
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
367
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
368
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
369
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
370
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_NLC, "128px");
371
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_NLC + 1, "50px");
372
			vendorTable.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
373
			vendorTable.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
374
			vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
375
			vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
376
			vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
377
			vendorTable.setText(row, TABLE_INDEX_NLC, nlc + "");
378
			vendorTable.getCellFormatter().setVisible(row, TABLE_INDEX_VENDORID, false);
379
			Button removeButton = new Button("X");
380
			vendorTable.setWidget(row, TABLE_INDEX_NLC + 1, removeButton);
381
			removeButton.addClickHandler(new ClickHandler() {
382
				@Override
383
				public void onClick(ClickEvent event) {
384
					vendorTable.removeRow(row);
385
				}
386
			});
387
		}
388
	}
389
 
390
 
391
	@UiHandler("addButton")
392
	/**
393
	 * On add button click a new Item instance is created and all the information from UI fields is dumped in this item instance.
394
	 * This item instance is then passed to the service to be added to the database.
395
	 */
396
	void addItem(ClickEvent event) {
397
		if(entityIdMandatory) {
398
			long entityId = 0;
399
			try {
400
				if(!catalogItemId.getText().trim().isEmpty()) {
401
					entityId = Long.parseLong(catalogItemId.getText().trim());
402
					if(entityId <= 0) {
403
						throw new NumberFormatException("Invalid value of Catalog Item Id");
404
					}
405
				}
406
				else {
407
					Window.alert("Catalog Item Id can not be empty");
408
					return;
409
				}
410
			} catch(NumberFormatException ex) {
411
				Window.alert("Invalid value of Catalog Item Id");
412
				return;
413
			}
414
			catalogService.checkEntityId(entityId, new AsyncCallback<Boolean>() {
415
				@Override
416
				public void onFailure(Throwable caught) {
417
					GWT.log("Error checking Catalog Item Id");
418
					Window.alert("Error checking Catalog Item Id");
419
					return;
420
				}
421
				@Override
422
				public void onSuccess(Boolean result) {
423
					if(!result) {
424
						Window.alert("Invalid value of Catalog Item Id");
425
						return;
426
					}
427
					addNewItem();
428
				}
429
			});
430
		}
431
		else {
432
			catalogService.checkSimilarItem(brand.getText().trim(), modelNumber.getText().trim(),
433
					modelName.getText().trim(), color.getText().trim(), new AsyncCallback<Long>() {
434
				@Override
435
				public void onFailure(Throwable caught) {
436
					GWT.log("Error checking similar item");
437
					Window.alert("Error checking similar item");
438
					return;
439
				}
440
				@Override
441
				public void onSuccess(Long result) {
442
					if(result != 0) {
443
						Window.alert("Similar product exists with Item Id " + result);
444
						return;
445
					}
446
					addNewItem();
447
				}
448
			});
449
		}
450
	}
451
 
452
	private void addNewItem() {
453
		Item item = new Item();
454
		if(!productGroup.getText().trim().isEmpty()) {
455
			item.setProductGroup(productGroup.getText().trim());
456
		}
457
		else {
458
			Window.alert("Product Group can not be empty.");
459
			return;
460
		}
461
 
462
		if(entityIdMandatory) {
463
			try {
464
				if(!catalogItemId.getText().trim().isEmpty()) {
465
					long entityId = Long.parseLong(catalogItemId.getText().trim());
466
					if(entityId <= 0) {
467
						throw new NumberFormatException("Invalid value of Catalog Item Id");
468
					}
469
					item.setCatalogItemId(entityId);
470
				}
471
				else {
472
					Window.alert("Catalog Item Id can not be empty");
473
					return;
474
				}
475
			} catch(NumberFormatException ex) {
476
				Window.alert("Invalid value of Catalog Item Id");
477
				return;
478
			}
479
		}
480
		item.setBrand(brand.getText().trim());
481
		item.setModelNumber(modelNumber.getText().trim());
482
		item.setModelName(modelName.getText().trim());
483
		item.setColor(color.getText().trim());
484
		item.setComments(comments.getText().trim());
485
		try {
486
			if(!mrp.getText().trim().isEmpty()) {
487
				double mrpValue = Double.parseDouble(mrp.getText().trim());
488
				if(mrpValue <= 0) {
489
					throw new NumberFormatException("Negative value of MRP");
490
				}
491
				item.setMrp(mrpValue);
492
			}
493
		} catch(NumberFormatException ex) {
494
			Window.alert("Invalid MRP format/value. Value shoule be greater than zero");
495
			return;
496
		}
497
		try {
498
			if(!sellingPrice.getText().trim().isEmpty()) {
499
				double spValue = Double.parseDouble(sellingPrice.getText().trim());
500
				if(spValue <= 0) {
501
					throw new NumberFormatException("Negative value of Selling price");
502
				}
503
				item.setSellingPrice(spValue);
504
			}
505
		} catch(NumberFormatException ex) {
506
			Window.alert("Invalid Selling Price format/value. Value shoule be greater than zero");
507
			return;
508
		}
509
		try {
510
			if(!weight.getText().trim().isEmpty()) {
511
				double wtValue = Double.parseDouble(weight.getText().trim());
512
				if(wtValue <= 0) {
513
					throw new NumberFormatException("Negative value of Weight");
514
				}
515
				item.setWeight(wtValue);
516
			}
517
		} catch(NumberFormatException ex) {
518
			Window.alert("Invalid weight format/value. Value shoule be greater than zero");
519
			return;
520
		}
521
		try {
18048 kshitij.so 522
			if(!packQuantity.getText().trim().isEmpty()) {
523
				long packQuantityValue = Long.parseLong(packQuantity.getText().trim());
524
				if(packQuantityValue < 1) {
525
					throw new NumberFormatException("Illegal value of PackQuantity");
526
				}
527
				item.setPackQuantity(packQuantityValue);
528
			}
529
			else{
530
				Window.alert("Invalid PackQuantity format/value. Value should can't be empty");
531
				return;
532
			}
533
		} catch(NumberFormatException ex) {
534
			Window.alert("Invalid PackQuantity format/value. Value should be greater than zero");
535
			return;
536
		}
537
		try {
8901 vikram.rag 538
			if(!startDate.getTextBox().getText().trim().equals("")) {
539
				item.setStartDate(startDate.getValue().getTime());
540
			}
541
		} catch(Exception ex) {
542
			Window.alert("Invalid start date format");
543
			return;
544
		}
545
		if(comingSoonStartDate.getValue()==null){
546
			item.setComingSoonStartDate(null);
547
		}else {
548
			item.setComingSoonStartDate(comingSoonStartDate.getValue().getTime());
549
		}
550
		if(expectedArrivalDate.getValue()==null){
551
			item.setExpectedArrivalDate(null);
552
		}else {
553
			item.setExpectedArrivalDate(expectedArrivalDate.getValue().getTime());
554
		}
555
		item.setBestDealsText(bestDealText.getText().trim());
556
		item.setBestDealsDetailsText(bestDealsDetailsText.getText().trim());
557
		item.setBestDealsDetailsLink(bestDealsDetailsLink.getText().trim());
558
		try {
559
			if(!bestDealValue.getText().trim().equals("")) {
560
				double bdValue = Double.parseDouble(bestDealValue.getText().trim());
561
				if(bdValue < 0) {
562
					throw new NumberFormatException("Negative value of BestDealValue");
563
				}
564
				item.setBestDealsValue(bdValue);
565
			}
566
		} catch(NumberFormatException ex) {
567
			Window.alert("Invalid best deal value format");
568
			return;
569
		}
570
		try {
571
			if(!bestSellingRank.getText().trim().equals("")) {
572
				long bsrValue = Long.parseLong(bestSellingRank.getText().trim());
573
				if(bsrValue < 0) {
574
					throw new NumberFormatException("Negative value of Best Selling Rank");
575
				}
576
				item.setBestSellingRank(bsrValue);
577
			}
578
		} catch(NumberFormatException ex) {
579
			Window.alert("Invalid best selling rank format");
580
			return;
581
		}
582
 
583
		try {
584
			if(!minStockLevel.getText().trim().equals("")) {
585
				long msl = Long.parseLong(minStockLevel.getText().trim());
586
				if(msl < 0) {
587
					throw new NumberFormatException("Negative value of Min Stock Value");
588
				}
589
				item.setMinStockLevel(msl);
590
			}else {
591
				item.setMinStockLevel(0L);
592
			}
593
		} catch(NumberFormatException ex) {
594
			item.setMinStockLevel(0L);
595
			Window.alert("Invalid min Stock Level. Setting it to zero(0)");
596
			return;
597
		}
598
 
599
		try {
600
			if(!numOfDaysStock.getText().trim().equals("")) {
601
				Long nds = Long.parseLong(numOfDaysStock.getText().trim());
602
				if(nds < 0) {
603
					throw new NumberFormatException("Negative value of Num of Days Of Stock");
604
				}
605
				item.setNumOfDaysStock(nds.intValue());
606
			} else {
607
				item.setNumOfDaysStock(0);
608
			}
609
		} catch(NumberFormatException ex) {
610
			item.setNumOfDaysStock(0);
611
			Window.alert("Invalid min Stock Level. Setting it to zero(0)");
612
			return;
613
		}
614
 
615
		try {
616
			if(!freebieItemId.getText().trim().equals("")) {
617
				long freeItemId = Long.parseLong(freebieItemId.getText().trim());
618
				if(freeItemId < 0) {
619
					throw new NumberFormatException("Negative value of freebieItemId ");
620
				}
621
				item.setFreebieItemId(new Long(freeItemId));
622
			}
623
		} catch(NumberFormatException ex) {
624
			Window.alert("Invalid freebie ItemId");
625
			return;
626
		}
627
		item.setAsin(asin.getText().trim());
628
 
629
		try {
630
			if(!holdInventory.getText().trim().equals("")) {
631
				long hold_inventory = Long.parseLong(holdInventory.getText().trim());
632
				if(hold_inventory < 0) {
633
					throw new NumberFormatException("Negative value of Hold Inventory");
634
				}
635
				item.setHoldInventory(hold_inventory);
636
			}
637
		} catch(NumberFormatException ex) {
638
			Window.alert("Invalid Hold Inventory Value");
639
			return;
640
		}
641
 
642
		try {
643
			if(!defaultInventory.getText().trim().equals("")) {
644
				long default_inventory = Long.parseLong(defaultInventory.getText().trim());
645
				if(default_inventory < 0) {
646
					throw new NumberFormatException("Negative value of Default Inventory");
647
				}
648
				item.setDefaultInventory(default_inventory);
649
			}
650
		} catch(NumberFormatException ex) {
651
			Window.alert("Invalid Default Inventory Value");
652
			return;
653
		}
654
		item.setDefaultForEntity(defaultForEntity.getValue());
655
		item.setRisky(risky.getValue());
656
		item.setHasItemNo(hasItemNo.getValue());
657
		item.setItemType(itemType.getValue());
658
		if(preferredVendor.getSelectedIndex() == 0) {
659
			Window.alert("Invalid Vendor Selected");
660
			return;
661
		}
662
		else {
663
			item.setPreferredVendor(Utils.getVendorId(preferredVendor.getItemText(preferredVendor.getSelectedIndex())));
664
		}
665
		item.setPreferredInsurer(Long.parseLong(preferredInsurer.getValue(preferredInsurer.getSelectedIndex())));
666
		/*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
2105 ankur.sing 667
          Add the instance to map and set the map to the item instance created above.*/
8901 vikram.rag 668
		Map<Long, VendorPricings> vendorPrices = new HashMap<Long, VendorPricings>();
669
		VendorPricings v;
670
		for(int row = 0; row < vendorTable.getRowCount(); row++) {
671
			v = new VendorPricings();
672
			v.setMop(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP)));
673
			v.setDealerPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP)));
674
			v.setTransferPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP)));
675
			v.setVendorId(Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID)));
676
			v.setNlc(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_NLC)));
677
			vendorPrices.put(v.getVendorId(), v);
678
		}
679
		item.setVendorPricesMap(vendorPrices);
680
 
681
		/*Create an instance of VendorKeys for each row in vendor pricing table. Set the vendor prices to the instance.
2119 ankur.sing 682
        Add the instance to map and set the map to the item instance created above.*/
8901 vikram.rag 683
		Map<String, VendorItemMapping> vendorMappings = new HashMap<String, VendorItemMapping>();
684
		VendorItemMapping vMapping;
685
		for(int row = 0; row < vendorTableM.getRowCount(); row++) {
686
			vMapping = new VendorItemMapping();
687
			vMapping.setItemKey(vendorTableM.getText(row, TABLE_INDEX_ITEM_KEY));
688
			vMapping.setVendorId(Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_VENDORID)));
689
			vendorMappings.put(vMapping.getVendorId() + Item.KEY_SEPARATOR + vMapping.getItemKey(), vMapping);
690
		}
691
		item.setVendorKeysMap(vendorMappings);
692
		if(!Utils.validateItem(item)) {
693
			return;
694
		}
2427 ankur.sing 695
 
8901 vikram.rag 696
		/*Service method to add item. */
697
		catalogService.addItem(item, new AsyncCallback<Long>() {
698
			@Override
699
			public void onSuccess(Long result) {
700
				if(result == null || result == 0) {
701
					Window.alert("Error while adding item");
702
					return;
703
				}
704
				Window.alert("Item added successfully. Id = " + result);
705
				hide();
706
			}
707
			@Override
708
			public void onFailure(Throwable caught) {
709
				caught.printStackTrace();
710
				Window.alert("Error while adding item");
711
			}
712
		});
713
	}
714
 
715
	@UiHandler("cancelButton")
716
	void closeForm(ClickEvent event) {
717
		this.hide();
718
	}
719
 
720
	/**
721
	 * Checks if vendor details already exists corresponding to the vendor Id parameter. 
722
	 */
723
	private boolean vendorExists(long vendorId) {
724
		long id;
725
		for(int i = 0; i < vendorTable.getRowCount(); i++) {
726
			id = Long.parseLong(vendorTable.getText(i, TABLE_INDEX_VENDORID));
727
			if(vendorId == id) {
728
				return false;
729
			}
730
		}
731
		return true;
732
	}
733
 
734
	/**
735
	 * validate vendor prices (MOP, DealerPrice, TransferPrice)
736
	 */
737
	@SuppressWarnings("unused")
2566 chandransh 738
	private boolean validateVendorPrices(double mop, double dp, double tp) {
8901 vikram.rag 739
		double mrpValue;
740
		try {
741
			mrpValue = Double.parseDouble(mrp.getText().trim());
742
		} catch (NumberFormatException ex) {
743
			Window.alert("Invalid MRP value.");
744
			return false;
745
		}
746
		if(mrpValue < mop) {
747
			Window.alert("MOP cannot be more than MRP.");
748
			return false;
749
		}
750
		if(tp > mop) {
751
			Window.alert("Transfer Price cannot be more than MOP.");
752
			return false;
753
		}
754
		return true;
755
	}
756
 
5217 amit.gupta 757
	@UiHandler("comingSoonButton")
758
	void onComingSoonButtonClick(ClickEvent event) {
759
		ComingSoonDialog cd = new ComingSoonDialog(this);
760
		cd.show();
761
	}
762
 
763
	public void setComingSoonStartDate(Date date){
764
		this.comingSoonStartDate.setValue(date);
765
	}
766
	public void setBestDealsText(String bestDealsText){
767
		this.bestDealText.setText(bestDealsText);
768
	}
769
 
770
	public void setExpectedArrivalDate(Date date){
771
		this.expectedArrivalDate.setValue(date);
772
	}
773
 
6241 amit.gupta 774
	public void setShowPrice(boolean b){
775
		this.showSellingPrice.setValue(b);
776
	}
777
 
778
	public boolean isShowPrice(){
779
		return this.showSellingPrice.getValue();
780
	}
781
 
5217 amit.gupta 782
	@Override
783
	public String getBestDealsText() {
784
		return this.bestDealText.getValue();
785
	}
786
 
787
	@Override
788
	public Date getComingSoonStartDate() {
789
		return this.comingSoonStartDate.getValue();
790
	}
791
 
792
	@Override
793
	public Date getExpectedArrivalDate() {
6241 amit.gupta 794
		return this.expectedArrivalDate.getValue();
5217 amit.gupta 795
	}
6777 vikram.rag 796
 
797
	public TextBox getBestDealsDetailsText() {
798
		return bestDealsDetailsText;
799
	}
800
 
801
	public void setBestDealsDetailsText(TextBox bestDealsDetailsText) {
802
		this.bestDealsDetailsText = bestDealsDetailsText;
803
	}
804
 
805
	public TextBox getBestDealsDetailsLink() {
806
		return bestDealsDetailsLink;
807
	}
808
 
809
	public void setBestDealsDetailsLink(TextBox bestDealsDetailsLink) {
810
		this.bestDealsDetailsLink = bestDealsDetailsLink;
811
	}
2066 ankur.sing 812
}