Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
19248 kshitij.so 1
package in.shop2020.support.controllers;
2
 
3
 
4
import java.io.BufferedInputStream;
5
import java.io.File;
6
import java.io.FileInputStream;
7
import java.io.FileNotFoundException;
8
import java.io.FileOutputStream;
9
import java.io.IOException;
10
import java.io.InputStream;
11
import java.util.ArrayList;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15
 
16
import javax.servlet.ServletContext;
17
import javax.servlet.ServletOutputStream;
18
import javax.servlet.http.HttpServletRequest;
19
import javax.servlet.http.HttpServletResponse;
20
import javax.servlet.http.HttpSession;
21
 
23304 ashik.ali 22
import org.apache.commons.io.FileUtils;
23
import org.apache.commons.lang.xwork.StringUtils;
19248 kshitij.so 24
import org.apache.poi.hssf.usermodel.HSSFRow;
25
import org.apache.poi.hssf.usermodel.HSSFSheet;
26
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
27
import org.apache.poi.ss.usermodel.Cell;
28
import org.apache.struts2.convention.annotation.InterceptorRef;
29
import org.apache.struts2.convention.annotation.InterceptorRefs;
30
import org.apache.struts2.interceptor.ServletRequestAware;
31
import org.apache.struts2.interceptor.ServletResponseAware;
32
import org.apache.struts2.util.ServletContextAware;
33
import org.apache.thrift.TException;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36
 
37
import com.opensymphony.xwork2.ValidationAwareSupport;
38
 
23304 ashik.ali 39
import in.shop2020.model.v1.catalog.CatalogService.Client;
40
import in.shop2020.model.v1.catalog.CatalogServiceException;
41
import in.shop2020.model.v1.catalog.GstRate;
42
import in.shop2020.model.v1.catalog.Item;
43
import in.shop2020.model.v1.catalog.ItemPricing;
44
import in.shop2020.model.v1.catalog.StateGstRate;
45
import in.shop2020.model.v1.dtr.BulkItems;
46
import in.shop2020.model.v1.inventory.BulkAddInventory;
47
import in.shop2020.model.v1.inventory.InventoryServiceException;
48
import in.shop2020.model.v1.inventory.ItemInventory;
49
import in.shop2020.model.v1.inventory.Vendor;
50
import in.shop2020.model.v1.inventory.VendorItemPricing;
51
import in.shop2020.model.v1.inventory.Warehouse;
52
import in.shop2020.support.utils.ReportsUtils;
53
import in.shop2020.thrift.clients.CatalogClient;
54
import in.shop2020.thrift.clients.DtrClient;
55
import in.shop2020.thrift.clients.InventoryClient;
56
 
19248 kshitij.so 57
@SuppressWarnings({"unused","deprecation"})
58
 
59
@InterceptorRefs({
60
	@InterceptorRef("defaultStack"),
61
	@InterceptorRef("login")
62
})
63
 
64
public class BulkAddController extends ValidationAwareSupport implements ServletRequestAware ,ServletResponseAware, ServletContextAware{
65
	/**
66
	 * 
67
	 */
68
	private static final long serialVersionUID = 1L;
69
 
70
 
71
	private static Logger logger = LoggerFactory.getLogger(BulkAddController.class);
72
 
73
 
74
	private HttpServletRequest request;
75
	private HttpServletResponse response;
76
	private HttpSession session;
77
	private ServletContext context;
78
	private String id;
79
	private String result;
80
	private File file;
19988 kshitij.so 81
	private String virtualWarehouseId;
19248 kshitij.so 82
 
19988 kshitij.so 83
	public String getVirtualWarehouseId() {
84
		return virtualWarehouseId;
85
	}
86
 
87
	public void setVirtualWarehouseId(String virtualWarehouseId) {
88
		this.virtualWarehouseId = virtualWarehouseId;
89
	}
90
 
19248 kshitij.so 91
	public String index() {
92
		if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE),request.getServletPath())) {
93
			return "authfail";
94
		}
95
		return "index";
96
	}
97
 
98
	public String edit() {
99
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
100
			return "authfail";
101
		}
102
		return "edit";
103
	}
104
 
19253 kshitij.so 105
 
19248 kshitij.so 106
	public String show() {
107
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
108
			return "authfail";
109
		}
110
		logger.info("Inside show for "+id);
19253 kshitij.so 111
 
19248 kshitij.so 112
		if (StringUtils.equals(id, "bulk-add-options")){
113
			return "bulk-add-options";
114
		}
115
		return "id";
116
	}
19253 kshitij.so 117
 
19248 kshitij.so 118
	public String loadVendorDiv(){
119
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
120
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
121
			return "authfail";
122
		}
123
		return "vendor-item-pricing-upload";
124
	}
19253 kshitij.so 125
 
19248 kshitij.so 126
	public String loadVirtualDiv(){
127
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
128
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
129
			return "authfail";
130
		}
131
		return "virtual-bulk-add";
132
	}
19253 kshitij.so 133
 
19248 kshitij.so 134
	public String loadCatalogDiv(){
135
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
136
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
137
			return "authfail";
138
		}
139
		return "catalog-bulk-add";
140
	}
19253 kshitij.so 141
 
19248 kshitij.so 142
	public String loadDtrDiv(){
143
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
144
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
145
			return "authfail";
146
		}
147
		return "dtr-bulk-add";
148
	}
23304 ashik.ali 149
 
19988 kshitij.so 150
	public String loadDownloadVirtualDiv(){
151
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
152
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
153
			return "authfail";
154
		}
155
		return "download-virtual-div";
156
	}
157
 
19248 kshitij.so 158
	public String uploadItemsDtr() throws IOException, CatalogServiceException, TException{
159
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
160
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
161
			return "authfail";
162
		}
163
		File fileToCreate = new File("/tmp/", "dtr-items.xls");
164
		FileUtils.copyFile(this.file, fileToCreate);
165
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
166
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
167
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 168
 
19248 kshitij.so 169
		List<BulkItems> bulkItemsList= new ArrayList<BulkItems>();
19253 kshitij.so 170
 
19248 kshitij.so 171
		List<Long> items = new ArrayList<Long>();
172
 
173
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
174
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
175
			items.add(item_id);
176
		}
177
 
178
		Client cc = new CatalogClient().getClient();
179
		Map<Long, Item> itemMap = cc.getItems(items);
180
		StringBuilder sb = new StringBuilder(); 
181
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
182
			long itemId = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
183
			Item t_item = itemMap.get(itemId);
184
			if (t_item==null){
185
				sb.append("Item is not valid "+itemId+"\n");
186
				continue;
187
			}
188
			BulkItems b = new BulkItems();
189
			b.setItem_id(itemId);
19253 kshitij.so 190
 
191
			if (checkEmptyString(sheet.getRow(iterator).getCell(1))){
192
				b.setShowMrpFlag(false);
193
			}
194
			else{
195
				int show_mrp_flag = (int) sheet.getRow(iterator).getCell(1).getNumericCellValue();
196
				b.setShowMrpFlag(show_mrp_flag ==1 ? true : false);
197
			}
198
			if (checkEmptyString(sheet.getRow(iterator).getCell(2))){
199
				b.setTagline("");
200
			}
201
			else{
202
				String tagline = sheet.getRow(iterator).getCell(2).getStringCellValue();
203
				b.setTagline(tagline);
204
			}
205
			if (checkEmptyString(sheet.getRow(iterator).getCell(3))){
206
				b.setOffer("");
207
			}
208
			else{
209
				String offer = sheet.getRow(iterator).getCell(3).getStringCellValue();
210
				b.setOffer(offer);
211
			}
19248 kshitij.so 212
			long category_id = (long) sheet.getRow(iterator).getCell(4).getNumericCellValue();
213
			if (category_id!=6L){
214
				sb.append("Category Id is not valid "+itemId+"\n");
215
			}
216
			b.setCategory_id(category_id);
217
			long sub_category_id = (long) sheet.getRow(iterator).getCell(5).getNumericCellValue();
218
			if (sub_category_id==0){
219
				sb.append("SubCategory Id is not valid "+itemId+"\n");
220
			}
221
			b.setSubCategoryId(sub_category_id);
222
			int show_net_price = (int) sheet.getRow(iterator).getCell(6).getNumericCellValue();
223
			b.setShowNetPrice(show_net_price ==1 ? true : false);
224
			long brand_id = (long) sheet.getRow(iterator).getCell(7).getNumericCellValue();
225
			if (brand_id==0){
226
				sb.append("Brand Id is not valid "+itemId+"\n");
227
			}
19588 kshitij.so 228
			long internalRank = (long) sheet.getRow(iterator).getCell(8).getNumericCellValue();
229
			b.setInternalRank(internalRank);
19248 kshitij.so 230
			b.setBrand_id(brand_id);
231
			b.setAvailable_price(t_item.getSellingPrice());
232
			b.setMrp(t_item.getMrp());
233
			b.setBrand(t_item.getBrand());
234
			b.setIdentifier(String.valueOf(t_item.getCatalogItemId()));
235
			b.setModel_name(t_item.getModelName()+" "+t_item.getModelNumber());
236
			b.setProduct_name(t_item.getBrand()+" "+t_item.getModelName()+" "+t_item.getModelNumber());
237
			b.setSource_product_name(t_item.getBrand()+" "+t_item.getModelName()+" "+t_item.getModelNumber());
238
			b.setQuantity(t_item.getPackQuantity());
239
			bulkItemsList.add(b);
240
		}
241
		if (sb.length()>0){
242
			setResult("Please correct error \n"+sb.toString());
243
			return "item-details-json";
244
		}
245
		in.shop2020.model.v1.dtr.DtrService.Client dc = new DtrClient("dtr_service_server_host","dtr_service_server_port").getClient(); 
246
		List<String> res = dc.addItemsInBulk(bulkItemsList);
247
		if (res.size()>0){
248
			for(String s : res){
249
				sb.append("Catalog ItemId not added "+s+"\n");
250
			}
251
			setResult(sb.toString());
252
		}else{
253
			setResult("Items added successfully");
254
		}
255
		return "item-details-json";
256
	}
23304 ashik.ali 257
 
258
	public String uploadTaxRateSheet() throws IOException, CatalogServiceException, TException{
259
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
260
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
261
			return "authfail";
262
		}
263
		File fileToCreate = new File("/tmp/", "tax-rate-sheet.xls");
264
		FileUtils.copyFile(this.file, fileToCreate);
265
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
266
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
267
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 268
 
23304 ashik.ali 269
		List<Long> itemIdsTaxRates = new ArrayList<Long>();
270
 
271
		Map<Long, Double> itemIdIgstTaxRate = new HashMap<Long, Double>();
23313 amit.gupta 272
		List<StateGstRate> stateGstRates = new ArrayList<StateGstRate>();
23304 ashik.ali 273
 
274
		for (int iterator = sheet.getFirstRowNum() + 1; iterator <= sheet.getLastRowNum(); iterator++){
275
			long itemId = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
23313 amit.gupta 276
			double taxRate = sheet.getRow(iterator).getCell(1).getNumericCellValue();
277
			StateGstRate sgr = new StateGstRate();
278
			sgr.setItemId(itemId);
279
			sgr.setIgstRate(taxRate);
280
 
23304 ashik.ali 281
		}
282
 
283
		Client cc = new CatalogClient().getClient();
23317 amit.gupta 284
		cc.persistGstRate(stateGstRates);
23304 ashik.ali 285
 
286
		setResult("Sheet uploaded successfully");
287
		return "item-details-json";
288
	}
289
 
19248 kshitij.so 290
	public String updateVirtualInventory() throws IOException, CatalogServiceException, TException, InventoryServiceException{
291
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
292
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
293
			return "authfail";
294
		}
19253 kshitij.so 295
 
19248 kshitij.so 296
		File fileToCreate = new File("/tmp/", "vendor-inventory.xls");
297
		FileUtils.copyFile(this.file, fileToCreate);
298
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
299
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
300
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 301
 
19248 kshitij.so 302
		List<BulkAddInventory> vendorInventory= new ArrayList<BulkAddInventory>();
19253 kshitij.so 303
 
19248 kshitij.so 304
		List<Long> items = new ArrayList<Long>();
305
 
306
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
307
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
308
			items.add(item_id);
309
		}
310
		Client cc = new CatalogClient().getClient();
311
		Map<Long, Item> itemMap = cc.getItems(items);
19253 kshitij.so 312
 
19248 kshitij.so 313
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
314
			long itemId = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
315
			if (itemMap.get(itemId)==null){
316
				continue;
317
			}
318
			BulkAddInventory b = new BulkAddInventory();
319
			b.setItem_id(itemId);
320
			long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
321
			b.setWarehouse_id(warehouseId);
322
			long inventory = (long) sheet.getRow(iterator).getCell(2).getNumericCellValue();
323
			b.setInventory(inventory);
324
			vendorInventory.add(b);
325
		}
19253 kshitij.so 326
 
19248 kshitij.so 327
		InventoryClient inventoryServiceClient = new InventoryClient();
328
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
329
		inventoryClient.addInventoryInBulk(vendorInventory);
330
		setResult("Inventory updated successfully");
331
		return "item-details-json";
332
	}
19253 kshitij.so 333
 
19248 kshitij.so 334
	public String addVendorItemPricing() throws IOException, TException, CatalogServiceException{
335
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
336
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
337
			return "authfail";
338
		}
339
		File fileToCreate = new File("/tmp/", "vendor-pricing.xls");
340
		FileUtils.copyFile(this.file, fileToCreate);
341
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
342
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
343
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 344
 
19248 kshitij.so 345
		List<VendorItemPricing> vendorItemPricingList= new ArrayList<VendorItemPricing>();
346
		List<Long> items = new ArrayList<Long>();
19253 kshitij.so 347
 
19248 kshitij.so 348
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
349
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
350
			items.add(item_id);
351
		}
352
		Client cc = new CatalogClient().getClient();
353
		Map<Long, Item> itemMap = cc.getItems(items);
19253 kshitij.so 354
 
19248 kshitij.so 355
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
356
			long itemId = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
357
			if (itemMap.get(itemId)==null){
358
				continue;
359
			}
360
			VendorItemPricing v = new VendorItemPricing();
361
			v.setItemId(itemId);
362
			long vendorId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
363
			v.setVendorId(vendorId);
364
			double mop = (double) sheet.getRow(iterator).getCell(2).getNumericCellValue();
365
			v.setMop(mop);
366
			double dp = (double) sheet.getRow(iterator).getCell(3).getNumericCellValue();
367
			v.setDealerPrice(dp);
368
			double tp = (double) sheet.getRow(iterator).getCell(4).getNumericCellValue();
369
			v.setTransferPrice(tp);
370
			double nlc = (double) sheet.getRow(iterator).getCell(5).getNumericCellValue();
371
			v.setNlc(nlc);
372
			vendorItemPricingList.add(v);
19253 kshitij.so 373
		}
374
 
19248 kshitij.so 375
		InventoryClient inventoryServiceClient = new InventoryClient();
376
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
377
		List<Long> notUpdated = inventoryClient.addVendorItemPricingInBulk(vendorItemPricingList);
19253 kshitij.so 378
 
19248 kshitij.so 379
		if (notUpdated.size() > 0){
380
			StringBuilder sb = new StringBuilder();
381
			for (Long x : notUpdated){
382
				sb.append("Vendor Pricing not updated for "+x+"\n");
383
			}
384
			setResult(sb.toString());
385
		}
386
		else{
387
			setResult("Vendor item pricing updated.");
388
		}
389
		return "item-details-json";
390
	}
19253 kshitij.so 391
 
19248 kshitij.so 392
	public String updateItemPricing() throws IOException, CatalogServiceException, TException{
393
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
394
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
395
			return "authfail";
396
		}
397
		File fileToCreate = new File("/tmp/", "item-pricing.xls");
398
		FileUtils.copyFile(this.file, fileToCreate);
399
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
400
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
401
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 402
 
19248 kshitij.so 403
		List<ItemPricing> itemPricingList= new ArrayList<ItemPricing>();
404
		List<Long> items = new ArrayList<Long>();
19253 kshitij.so 405
 
19248 kshitij.so 406
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
407
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
408
			items.add(item_id);
409
		}
410
		Client cc = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
411
		Map<Long, Item> itemMap = cc.getItems(items);
19253 kshitij.so 412
 
19248 kshitij.so 413
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
414
			ItemPricing i = new ItemPricing();
415
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
416
			i.setItem_id(item_id);
417
			if (itemMap.get(item_id)!=null){
418
				if (checkEmptyString(sheet.getRow(iterator).getCell(1))){
419
					i.setSelling_price(itemMap.get(item_id).getSellingPrice());
420
				}
421
				else{
422
					double sellingPrice = (double) sheet.getRow(iterator).getCell(1).getNumericCellValue();
423
					i.setSelling_price(sellingPrice);
424
				}
425
				if (checkEmptyString(sheet.getRow(iterator).getCell(2))){
426
					i.setMrp(itemMap.get(item_id).getMrp());
427
				}
428
				else{
429
					double mrp = (double) sheet.getRow(iterator).getCell(2).getNumericCellValue();
430
					i.setMrp(mrp);
431
				}
432
				if (checkEmptyString(sheet.getRow(iterator).getCell(3))){
433
					i.setPreferred_vendor(itemMap.get(item_id).getPreferredVendor());
434
				}
435
				else{
436
					long preferred_vendor = (long) sheet.getRow(iterator).getCell(3).getNumericCellValue();
437
					i.setPreferred_vendor(preferred_vendor);
438
				}
439
				if (checkEmptyString(sheet.getRow(iterator).getCell(4))){
440
					i.setPrivate_deal_price(0);
441
				}
442
				else{
443
					double private_deal_price = (double) sheet.getRow(iterator).getCell(4).getNumericCellValue();
444
					i.setPrivate_deal_price(private_deal_price);
445
				}
19249 kshitij.so 446
				if (checkEmptyString(sheet.getRow(iterator).getCell(5))){
447
					i.setWeight(itemMap.get(item_id).getWeight());
448
				}
449
				else{
450
					double weight = (double) sheet.getRow(iterator).getCell(5).getNumericCellValue();
19252 kshitij.so 451
					i.setWeight(weight);
19249 kshitij.so 452
				}
19253 kshitij.so 453
				itemPricingList.add(i);
19248 kshitij.so 454
			}
455
		}
456
		boolean res = cc.updateItemPricing(itemPricingList);
457
		if (res){
458
			setResult("Pricing updated successfully");
459
		}
460
		else{
461
			setResult("Unable to update pricing");
462
		}
463
		return "item-details-json";
464
	}
19253 kshitij.so 465
 
19248 kshitij.so 466
	public String downloadVendors() throws TException, IOException{
467
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
468
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
469
			return "authfail";
470
		}
471
		InventoryClient inventoryServiceClient = new InventoryClient();
472
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
473
		List<Vendor> vendors = inventoryClient.getAllVendors();
19253 kshitij.so 474
 
19248 kshitij.so 475
		File file = new File("/tmp/vendors.xls");
476
		HSSFWorkbook hwb=new HSSFWorkbook();
477
		HSSFSheet sheet =  hwb.createSheet("Vendors");
478
		HSSFRow rowhead=   sheet.createRow((short)0);
479
		rowhead.createCell((short) 0).setCellValue("VENDOR_ID");
480
		rowhead.createCell((short) 1).setCellValue("VENDOR_NAME");
19253 kshitij.so 481
 
19248 kshitij.so 482
		int iterator= 1;
483
		for (Vendor v : vendors){
484
			HSSFRow row = sheet.createRow((short)iterator);
485
			row.createCell((short) 0).setCellValue(v.getId());
486
			row.createCell((short) 1).setCellValue(v.getName());
487
			iterator++;
488
		}
19253 kshitij.so 489
 
19248 kshitij.so 490
		FileOutputStream fileOut = null;
491
		try {
492
			fileOut = new FileOutputStream(file);
493
		} catch (FileNotFoundException e) {
494
			// TODO Auto-generated catch block
495
			e.printStackTrace();
496
		}
497
		try {
498
			hwb.write(fileOut);
499
		} catch (IOException e) {
500
			// TODO Auto-generated catch block
501
			e.printStackTrace();
502
		}
503
		try {
504
			fileOut.close();
505
		} catch (IOException e) {
506
			// TODO Auto-generated catch block
507
			e.printStackTrace();
508
		}
509
		byte[] buffer = new byte[(int)file.length()];
510
		InputStream input = null;
511
		try {
512
			int totalBytesRead = 0;
513
			input = new BufferedInputStream(new FileInputStream(file));
514
			while(totalBytesRead < buffer.length){
515
				int bytesRemaining = buffer.length - totalBytesRead;
516
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
517
				if (bytesRead > 0){
518
					totalBytesRead = totalBytesRead + bytesRead;
519
				}
520
			}
521
		}
522
		finally {
523
			input.close();
524
			file.delete();
525
		}
526
 
527
		response.setHeader("Content-Disposition", "attachment; filename=\"Vendors.xls\"");
528
		response.setContentType("application/octet-stream");
529
		ServletOutputStream sos;
530
		try {
531
			sos = response.getOutputStream();
532
			sos.write(buffer);
533
			sos.flush();
534
		} catch (IOException e) {
535
			System.out.println("Unable to stream the manifest file");
536
		}
537
		setResult("Vendor Streaming done");
538
		return "item-details-json";
19253 kshitij.so 539
 
19248 kshitij.so 540
	}
19253 kshitij.so 541
 
19248 kshitij.so 542
	public String downloadWarehouses() throws TException, IOException{
543
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
544
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
545
			return "authfail";
546
		}
547
		InventoryClient inventoryServiceClient = new InventoryClient();
548
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
549
		List<Warehouse> warehouses = inventoryClient.getWarehouses(null, null, 0, 0, 0);
19253 kshitij.so 550
 
19248 kshitij.so 551
		File file = new File("/tmp/warehouses.xls");
552
		HSSFWorkbook hwb=new HSSFWorkbook();
553
		HSSFSheet sheet =  hwb.createSheet("Warehouses");
554
		HSSFRow rowhead=   sheet.createRow((short)0);
555
		rowhead.createCell((short) 0).setCellValue("warehouse_id");
556
		rowhead.createCell((short) 1).setCellValue("display_name");
557
		rowhead.createCell((short) 2).setCellValue("location");
558
		rowhead.createCell((short) 3).setCellValue("vendor_id");
559
		rowhead.createCell((short) 4).setCellValue("vendor_name");
560
		rowhead.createCell((short) 5).setCellValue("inventory_type");
561
		rowhead.createCell((short) 6).setCellValue("warehouse_type");
562
		rowhead.createCell((short) 7).setCellValue("logistics_location");
563
		rowhead.createCell((short) 8).setCellValue("state_id");
19253 kshitij.so 564
 
19248 kshitij.so 565
		int iterator= 1;
566
		for (Warehouse w : warehouses){
567
			HSSFRow row = sheet.createRow((short)iterator);
568
			row.createCell((short) 0).setCellValue(w.getId());
569
			row.createCell((short) 1).setCellValue(w.getDisplayName());
570
			row.createCell((short) 2).setCellValue(w.getLocation());
571
			row.createCell((short) 3).setCellValue(w.getVendor().getId());
572
			row.createCell((short) 4).setCellValue(w.getVendor().getName());
573
			row.createCell((short) 5).setCellValue(w.getInventoryType().toString());
574
			row.createCell((short) 6).setCellValue(w.getWarehouseType().toString());
575
			row.createCell((short) 7).setCellValue(w.getLogisticsLocation().toString());
576
			row.createCell((short) 7).setCellValue(w.getStateId());
577
			iterator++;
578
		}
19253 kshitij.so 579
 
19248 kshitij.so 580
		FileOutputStream fileOut = null;
581
		try {
582
			fileOut = new FileOutputStream(file);
583
		} catch (FileNotFoundException e) {
584
			// TODO Auto-generated catch block
585
			e.printStackTrace();
586
		}
587
		try {
588
			hwb.write(fileOut);
589
		} catch (IOException e) {
590
			// TODO Auto-generated catch block
591
			e.printStackTrace();
592
		}
593
		try {
594
			fileOut.close();
595
		} catch (IOException e) {
596
			// TODO Auto-generated catch block
597
			e.printStackTrace();
598
		}
599
		byte[] buffer = new byte[(int)file.length()];
600
		InputStream input = null;
601
		try {
602
			int totalBytesRead = 0;
603
			input = new BufferedInputStream(new FileInputStream(file));
604
			while(totalBytesRead < buffer.length){
605
				int bytesRemaining = buffer.length - totalBytesRead;
606
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
607
				if (bytesRead > 0){
608
					totalBytesRead = totalBytesRead + bytesRead;
609
				}
610
			}
611
		}
612
		finally {
613
			input.close();
614
			file.delete();
615
		}
616
 
617
		response.setHeader("Content-Disposition", "attachment; filename=\"Warehouses.xls\"");
618
		response.setContentType("application/octet-stream");
619
		ServletOutputStream sos;
620
		try {
621
			sos = response.getOutputStream();
622
			sos.write(buffer);
623
			sos.flush();
624
		} catch (IOException e) {
625
			System.out.println("Unable to stream the manifest file");
626
		}
627
		setResult("Warehouse Streaming done");
628
		return "item-details-json";
19253 kshitij.so 629
 
19248 kshitij.so 630
	}
631
 
19988 kshitij.so 632
	public String downloadVirtualInventoryForWarehouse() throws NumberFormatException, TException, IOException{
633
		try{
634
			InventoryClient inventoryServiceClient = new InventoryClient();
635
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
636
			Map<Long, ItemInventory> inventoryMap = inventoryClient.getInventorySnapshot(Long.valueOf(virtualWarehouseId));
19997 kshitij.so 637
			List<Long> itemIds = new ArrayList<Long>();
19988 kshitij.so 638
			File file = new File("/tmp/currentinventory.xls");
639
			HSSFWorkbook hwb=new HSSFWorkbook();
640
			HSSFSheet sheet =  hwb.createSheet("Inventory");
641
			HSSFRow rowhead=   sheet.createRow((short)0);
642
			rowhead.createCell((short) 0).setCellValue("item_id");
19997 kshitij.so 643
			rowhead.createCell((short) 1).setCellValue("brand");
644
			rowhead.createCell((short) 2).setCellValue("model_name");
645
			rowhead.createCell((short) 3).setCellValue("model_number");
646
			rowhead.createCell((short) 4).setCellValue("color");
647
			rowhead.createCell((short) 5).setCellValue("Availability");
648
			rowhead.createCell((short) 6).setCellValue("Reserved");
649
			rowhead.createCell((short) 7).setCellValue("Held");
19253 kshitij.so 650
 
19988 kshitij.so 651
			int iterator= 1;
652
			for (Map.Entry<Long,ItemInventory> entry : inventoryMap.entrySet()) {
653
				HSSFRow row = sheet.createRow((short)iterator);
19997 kshitij.so 654
				itemIds.add(entry.getKey());
19988 kshitij.so 655
				row.createCell((short) 0).setCellValue(entry.getKey());
19997 kshitij.so 656
				row.createCell((short) 5).setCellValue(entry.getValue().getAvailability().get(Long.valueOf(virtualWarehouseId)));
657
				row.createCell((short) 6).setCellValue(entry.getValue().getReserved().get(Long.valueOf(virtualWarehouseId)));
658
				row.createCell((short) 7).setCellValue(entry.getValue().getHeld().get(Long.valueOf(virtualWarehouseId)));
19988 kshitij.so 659
				iterator++;
660
			}
19997 kshitij.so 661
			Client cc = new CatalogClient().getClient();
662
			Map<Long, Item> itemMap = cc.getItems(itemIds);
663
 
664
			iterator--;
665
			Item d_item;
666
			while(iterator!=0){
19999 kshitij.so 667
				long item_id = (long) (sheet.getRow(iterator).getCell(0).getNumericCellValue());
19997 kshitij.so 668
				d_item = itemMap.get(item_id);
669
				if (d_item == null){
670
					iterator--;
671
					continue;
672
				}
673
				sheet.getRow(iterator).createCell((short) 1).setCellValue(d_item.getBrand());
674
				sheet.getRow(iterator).createCell((short) 2).setCellValue(d_item.getModelName());
675
				sheet.getRow(iterator).createCell((short) 3).setCellValue(d_item.getModelNumber());
676
				sheet.getRow(iterator).createCell((short) 4).setCellValue(d_item.getColor());
677
				iterator--;
678
			}
679
 
19988 kshitij.so 680
			FileOutputStream fileOut = null;
681
			try {
682
				fileOut = new FileOutputStream(file);
683
			} catch (FileNotFoundException e) {
684
				// TODO Auto-generated catch block
685
				e.printStackTrace();
686
			}
687
			try {
688
				hwb.write(fileOut);
689
			} catch (IOException e) {
690
				// TODO Auto-generated catch block
691
				e.printStackTrace();
692
			}
693
			try {
694
				fileOut.close();
695
			} catch (IOException e) {
696
				// TODO Auto-generated catch block
697
				e.printStackTrace();
698
			}
699
			byte[] buffer = new byte[(int)file.length()];
700
			InputStream input = null;
701
			try {
702
				int totalBytesRead = 0;
703
				input = new BufferedInputStream(new FileInputStream(file));
704
				while(totalBytesRead < buffer.length){
705
					int bytesRemaining = buffer.length - totalBytesRead;
706
					int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
707
					if (bytesRead > 0){
708
						totalBytesRead = totalBytesRead + bytesRead;
709
					}
710
				}
711
			}
712
			finally {
713
				input.close();
714
				file.delete();
715
			}
716
 
717
			response.setHeader("Content-Disposition", "attachment; filename=\"Inventory.xls\"");
718
			response.setContentType("application/octet-stream");
719
			ServletOutputStream sos;
720
			try {
721
				sos = response.getOutputStream();
722
				sos.write(buffer);
723
				sos.flush();
724
			} catch (IOException e) {
725
				System.out.println("Unable to stream the manifest file");
726
			}
727
		}
728
		catch(Exception e){
19999 kshitij.so 729
			logger.error("Issue wile downloading virtual inventory"+e);
19988 kshitij.so 730
			setResult("Something went wrong");
731
			return "item-details-json";
732
		}
733
		setResult("Streaming done");
734
		return "item-details-json";
735
	}
736
 
737
 
19248 kshitij.so 738
	public boolean checkEmptyString(Cell cell){
739
		if (cell==null || cell.getCellType() == Cell.CELL_TYPE_BLANK){
740
			return true;
741
		}
742
		return false;
743
	}
19253 kshitij.so 744
 
19248 kshitij.so 745
	public String getItemDetails(){
746
		return result;
747
	}
748
 
749
	public void setId(String id) {
750
		this.id = id;
751
	}
752
 
753
	public String getUserName(){
754
		return session.getAttribute(ReportsUtils.USER_NAME).toString();
755
	}
756
 
757
	public HttpServletRequest getRequest() {
758
		logger.info("set request"+request.toString());
759
		return request;
760
	}
761
 
762
	public void setRequest(HttpServletRequest request) {
763
		this.request = request;
764
	}
765
 
766
	public HttpServletResponse getResponse() {
767
		return response;
768
	}
769
 
770
	public void setResponse(HttpServletResponse response) {
771
		this.response = response;
772
	}
773
 
774
	public HttpSession getSession() {
775
		return session;
776
	}
777
 
778
	public void setSession(HttpSession session) {
779
		this.session = session;
780
	}
781
 
782
	public ServletContext getContext() {
783
		return context;
784
	}
785
 
786
	public void setContext(ServletContext context) {
787
		this.context = context;
788
	}
789
 
19253 kshitij.so 790
 
19248 kshitij.so 791
	public void setServletRequest(HttpServletRequest req) {
792
		this.request = req;
793
		this.session = req.getSession();        
794
	}
795
 
796
	public void setServletContext(ServletContext arg0) {
797
		// TODO Auto-generated method stub
798
 
799
	}
800
 
801
	public void setServletResponse(HttpServletResponse response) {
802
		this.response = response;
803
	}
19253 kshitij.so 804
 
19248 kshitij.so 805
	public String getResult() {
806
		return result;
807
	}
808
 
809
	public void setResult(String result) {
810
		this.result = result;
811
	}
19253 kshitij.so 812
 
19248 kshitij.so 813
	public File getFile() {
814
		return file;
815
	}
816
 
817
	public void setFile(File file) {
818
		this.file = file;
819
	}
820
 
19988 kshitij.so 821
	public static void main(String args[]) throws NumberFormatException, TException, IOException{
822
		BulkAddController b =  new BulkAddController();
823
		b.setVirtualWarehouseId("3295");
824
		b.downloadVirtualInventoryForWarehouse();
825
	}
19253 kshitij.so 826
 
19988 kshitij.so 827
 
19248 kshitij.so 828
}