Subversion Repositories SmartDukaan

Rev

Rev 19999 | Rev 23313 | 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
 
150
	public String loadUploadTaxRateDiv(){
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 "upload-tax-rate";
156
	}
19253 kshitij.so 157
 
19988 kshitij.so 158
	public String loadDownloadVirtualDiv(){
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
		return "download-virtual-div";
164
	}
165
 
19248 kshitij.so 166
	public String uploadItemsDtr() throws IOException, CatalogServiceException, TException{
167
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
168
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
169
			return "authfail";
170
		}
171
		File fileToCreate = new File("/tmp/", "dtr-items.xls");
172
		FileUtils.copyFile(this.file, fileToCreate);
173
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
174
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
175
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 176
 
19248 kshitij.so 177
		List<BulkItems> bulkItemsList= new ArrayList<BulkItems>();
19253 kshitij.so 178
 
19248 kshitij.so 179
		List<Long> items = new ArrayList<Long>();
180
 
181
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
182
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
183
			items.add(item_id);
184
		}
185
 
186
		Client cc = new CatalogClient().getClient();
187
		Map<Long, Item> itemMap = cc.getItems(items);
188
		StringBuilder sb = new StringBuilder(); 
189
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
190
			long itemId = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
191
			Item t_item = itemMap.get(itemId);
192
			if (t_item==null){
193
				sb.append("Item is not valid "+itemId+"\n");
194
				continue;
195
			}
196
			BulkItems b = new BulkItems();
197
			b.setItem_id(itemId);
19253 kshitij.so 198
 
199
			if (checkEmptyString(sheet.getRow(iterator).getCell(1))){
200
				b.setShowMrpFlag(false);
201
			}
202
			else{
203
				int show_mrp_flag = (int) sheet.getRow(iterator).getCell(1).getNumericCellValue();
204
				b.setShowMrpFlag(show_mrp_flag ==1 ? true : false);
205
			}
206
			if (checkEmptyString(sheet.getRow(iterator).getCell(2))){
207
				b.setTagline("");
208
			}
209
			else{
210
				String tagline = sheet.getRow(iterator).getCell(2).getStringCellValue();
211
				b.setTagline(tagline);
212
			}
213
			if (checkEmptyString(sheet.getRow(iterator).getCell(3))){
214
				b.setOffer("");
215
			}
216
			else{
217
				String offer = sheet.getRow(iterator).getCell(3).getStringCellValue();
218
				b.setOffer(offer);
219
			}
19248 kshitij.so 220
			long category_id = (long) sheet.getRow(iterator).getCell(4).getNumericCellValue();
221
			if (category_id!=6L){
222
				sb.append("Category Id is not valid "+itemId+"\n");
223
			}
224
			b.setCategory_id(category_id);
225
			long sub_category_id = (long) sheet.getRow(iterator).getCell(5).getNumericCellValue();
226
			if (sub_category_id==0){
227
				sb.append("SubCategory Id is not valid "+itemId+"\n");
228
			}
229
			b.setSubCategoryId(sub_category_id);
230
			int show_net_price = (int) sheet.getRow(iterator).getCell(6).getNumericCellValue();
231
			b.setShowNetPrice(show_net_price ==1 ? true : false);
232
			long brand_id = (long) sheet.getRow(iterator).getCell(7).getNumericCellValue();
233
			if (brand_id==0){
234
				sb.append("Brand Id is not valid "+itemId+"\n");
235
			}
19588 kshitij.so 236
			long internalRank = (long) sheet.getRow(iterator).getCell(8).getNumericCellValue();
237
			b.setInternalRank(internalRank);
19248 kshitij.so 238
			b.setBrand_id(brand_id);
239
			b.setAvailable_price(t_item.getSellingPrice());
240
			b.setMrp(t_item.getMrp());
241
			b.setBrand(t_item.getBrand());
242
			b.setIdentifier(String.valueOf(t_item.getCatalogItemId()));
243
			b.setModel_name(t_item.getModelName()+" "+t_item.getModelNumber());
244
			b.setProduct_name(t_item.getBrand()+" "+t_item.getModelName()+" "+t_item.getModelNumber());
245
			b.setSource_product_name(t_item.getBrand()+" "+t_item.getModelName()+" "+t_item.getModelNumber());
246
			b.setQuantity(t_item.getPackQuantity());
247
			bulkItemsList.add(b);
248
		}
249
		if (sb.length()>0){
250
			setResult("Please correct error \n"+sb.toString());
251
			return "item-details-json";
252
		}
253
		in.shop2020.model.v1.dtr.DtrService.Client dc = new DtrClient("dtr_service_server_host","dtr_service_server_port").getClient(); 
254
		List<String> res = dc.addItemsInBulk(bulkItemsList);
255
		if (res.size()>0){
256
			for(String s : res){
257
				sb.append("Catalog ItemId not added "+s+"\n");
258
			}
259
			setResult(sb.toString());
260
		}else{
261
			setResult("Items added successfully");
262
		}
263
		return "item-details-json";
264
	}
23304 ashik.ali 265
 
266
	private static class ItemIdStateId{
267
		private int itemId;
268
		private int stateId;
269
		public int getItemId() {
270
			return itemId;
271
		}
272
		public void setItemId(int itemId) {
273
			this.itemId = itemId;
274
		}
275
		public int getStateId() {
276
			return stateId;
277
		}
278
		public void setStateId(int stateId) {
279
			this.stateId = stateId;
280
		}
281
	}
282
 
283
	public String uploadTaxRateSheet() throws IOException, CatalogServiceException, TException{
284
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
285
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
286
			return "authfail";
287
		}
288
		File fileToCreate = new File("/tmp/", "tax-rate-sheet.xls");
289
		FileUtils.copyFile(this.file, fileToCreate);
290
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
291
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
292
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 293
 
23304 ashik.ali 294
		List<Long> itemIdsTaxRates = new ArrayList<Long>();
295
 
296
		Map<Long, Double> itemIdIgstTaxRate = new HashMap<Long, Double>();
297
		Map<ItemIdStateId, GstRate> itemIdStateIdStateRateMap = new HashMap<BulkAddController.ItemIdStateId, GstRate>(); 
298
 
299
		for (int iterator = sheet.getFirstRowNum() + 1; iterator <= sheet.getLastRowNum(); iterator++){
300
			long itemId = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
301
			long stateId = (long)sheet.getRow(iterator).getCell(1).getNumericCellValue();
302
			double igstRate = sheet.getRow(iterator).getCell(2).getNumericCellValue();
303
			if(stateId == -1){
304
 
305
				itemIdIgstTaxRate.put(itemId, igstRate);
306
			}else{
307
				double cgstRate = sheet.getRow(iterator).getCell(3).getNumericCellValue();
308
				double sgstRate = sheet.getRow(iterator).getCell(4).getNumericCellValue();
309
				ItemIdStateId itemIdStateId = new ItemIdStateId();
310
				itemIdStateId.setItemId(Long.valueOf(itemId).intValue());
311
				itemIdStateId.setStateId(Long.valueOf(stateId).intValue());
312
				GstRate gstRate = new GstRate();
313
				gstRate.setCgstRate(cgstRate);
314
				gstRate.setSgstRate(sgstRate);
315
				itemIdStateIdStateRateMap.put(itemIdStateId, gstRate);
316
				itemIdsTaxRates.add(itemId);
317
			}
318
		}
319
 
320
		Client cc = new CatalogClient().getClient();
321
		if(!itemIdIgstTaxRate.keySet().isEmpty()){
322
 
323
			Map<Long, Item> itemMap = cc.getItems(new ArrayList<Long>(itemIdIgstTaxRate.keySet()));
324
			StringBuilder sb = new StringBuilder(); 
325
 
326
			for(Map.Entry<Long, Double> itemIdIgstTaxRateEntry : itemIdIgstTaxRate.entrySet()){
327
				if(itemMap.get(itemIdIgstTaxRateEntry.getKey()) == null){
328
					sb.append("Item is not valid "+itemIdIgstTaxRateEntry.getKey()+"\n");
329
				}
330
			}
331
			if (sb.length()>0){
332
				setResult("Please correct error \n"+sb.toString());
333
				return "item-details-json";
334
			}
335
		}
336
 
337
		if(!itemIdsTaxRates.isEmpty()){
338
			Map<Long, Item> itemMap = cc.getItems(new ArrayList<Long>(itemIdsTaxRates));
339
			StringBuilder sb = new StringBuilder(); 
340
			for(long itemId : itemIdsTaxRates){
341
				if(itemMap.get(itemId) == null){
342
					sb.append("Item is not valid "+itemId+"\n");
343
				}
344
			}
345
			if (sb.length()>0){
346
				setResult("Please correct error \n"+sb.toString());
347
				return "item-details-json";
348
			}
349
		}
350
 
351
		for(Map.Entry<Long, Double> itemIdIgstTaxRateEntry : itemIdIgstTaxRate.entrySet()){
352
			StateGstRate stateGstRate = new StateGstRate();
353
			stateGstRate.setItemId(itemIdIgstTaxRateEntry.getKey());
354
			stateGstRate.setStateId(-1);
355
			stateGstRate.setIgstRate(itemIdIgstTaxRateEntry.getValue());
356
			stateGstRate.setCgstRate(0);
357
			stateGstRate.setSgstRate(0);
358
			cc.persistStateGstRate(stateGstRate);
359
		}
360
 
361
		for(Map.Entry<ItemIdStateId, GstRate> itemIdStateIdStateRateEntry : itemIdStateIdStateRateMap.entrySet()){
362
			StateGstRate stateGstRate = new StateGstRate();
363
			stateGstRate.setItemId(itemIdStateIdStateRateEntry.getKey().getItemId());
364
			stateGstRate.setStateId(itemIdStateIdStateRateEntry.getKey().getStateId());
365
			stateGstRate.setIgstRate(0);
366
			stateGstRate.setCgstRate(itemIdStateIdStateRateEntry.getValue().getCgstRate());
367
			stateGstRate.setSgstRate(itemIdStateIdStateRateEntry.getValue().getSgstRate());
368
			cc.persistStateGstRate(stateGstRate);
369
		}
370
 
371
		setResult("Sheet uploaded successfully");
372
		return "item-details-json";
373
	}
374
 
19248 kshitij.so 375
	public String updateVirtualInventory() throws IOException, CatalogServiceException, TException, InventoryServiceException{
376
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
377
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
378
			return "authfail";
379
		}
19253 kshitij.so 380
 
19248 kshitij.so 381
		File fileToCreate = new File("/tmp/", "vendor-inventory.xls");
382
		FileUtils.copyFile(this.file, fileToCreate);
383
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
384
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
385
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 386
 
19248 kshitij.so 387
		List<BulkAddInventory> vendorInventory= new ArrayList<BulkAddInventory>();
19253 kshitij.so 388
 
19248 kshitij.so 389
		List<Long> items = new ArrayList<Long>();
390
 
391
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
392
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
393
			items.add(item_id);
394
		}
395
		Client cc = new CatalogClient().getClient();
396
		Map<Long, Item> itemMap = cc.getItems(items);
19253 kshitij.so 397
 
19248 kshitij.so 398
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
399
			long itemId = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
400
			if (itemMap.get(itemId)==null){
401
				continue;
402
			}
403
			BulkAddInventory b = new BulkAddInventory();
404
			b.setItem_id(itemId);
405
			long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
406
			b.setWarehouse_id(warehouseId);
407
			long inventory = (long) sheet.getRow(iterator).getCell(2).getNumericCellValue();
408
			b.setInventory(inventory);
409
			vendorInventory.add(b);
410
		}
19253 kshitij.so 411
 
19248 kshitij.so 412
		InventoryClient inventoryServiceClient = new InventoryClient();
413
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
414
		inventoryClient.addInventoryInBulk(vendorInventory);
415
		setResult("Inventory updated successfully");
416
		return "item-details-json";
417
	}
19253 kshitij.so 418
 
19248 kshitij.so 419
	public String addVendorItemPricing() throws IOException, TException, CatalogServiceException{
420
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
421
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
422
			return "authfail";
423
		}
424
		File fileToCreate = new File("/tmp/", "vendor-pricing.xls");
425
		FileUtils.copyFile(this.file, fileToCreate);
426
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
427
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
428
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 429
 
19248 kshitij.so 430
		List<VendorItemPricing> vendorItemPricingList= new ArrayList<VendorItemPricing>();
431
		List<Long> items = new ArrayList<Long>();
19253 kshitij.so 432
 
19248 kshitij.so 433
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
434
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
435
			items.add(item_id);
436
		}
437
		Client cc = new CatalogClient().getClient();
438
		Map<Long, Item> itemMap = cc.getItems(items);
19253 kshitij.so 439
 
19248 kshitij.so 440
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
441
			long itemId = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
442
			if (itemMap.get(itemId)==null){
443
				continue;
444
			}
445
			VendorItemPricing v = new VendorItemPricing();
446
			v.setItemId(itemId);
447
			long vendorId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
448
			v.setVendorId(vendorId);
449
			double mop = (double) sheet.getRow(iterator).getCell(2).getNumericCellValue();
450
			v.setMop(mop);
451
			double dp = (double) sheet.getRow(iterator).getCell(3).getNumericCellValue();
452
			v.setDealerPrice(dp);
453
			double tp = (double) sheet.getRow(iterator).getCell(4).getNumericCellValue();
454
			v.setTransferPrice(tp);
455
			double nlc = (double) sheet.getRow(iterator).getCell(5).getNumericCellValue();
456
			v.setNlc(nlc);
457
			vendorItemPricingList.add(v);
19253 kshitij.so 458
		}
459
 
19248 kshitij.so 460
		InventoryClient inventoryServiceClient = new InventoryClient();
461
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
462
		List<Long> notUpdated = inventoryClient.addVendorItemPricingInBulk(vendorItemPricingList);
19253 kshitij.so 463
 
19248 kshitij.so 464
		if (notUpdated.size() > 0){
465
			StringBuilder sb = new StringBuilder();
466
			for (Long x : notUpdated){
467
				sb.append("Vendor Pricing not updated for "+x+"\n");
468
			}
469
			setResult(sb.toString());
470
		}
471
		else{
472
			setResult("Vendor item pricing updated.");
473
		}
474
		return "item-details-json";
475
	}
19253 kshitij.so 476
 
19248 kshitij.so 477
	public String updateItemPricing() throws IOException, CatalogServiceException, TException{
478
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
479
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
480
			return "authfail";
481
		}
482
		File fileToCreate = new File("/tmp/", "item-pricing.xls");
483
		FileUtils.copyFile(this.file, fileToCreate);
484
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
485
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
486
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 487
 
19248 kshitij.so 488
		List<ItemPricing> itemPricingList= new ArrayList<ItemPricing>();
489
		List<Long> items = new ArrayList<Long>();
19253 kshitij.so 490
 
19248 kshitij.so 491
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
492
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
493
			items.add(item_id);
494
		}
495
		Client cc = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
496
		Map<Long, Item> itemMap = cc.getItems(items);
19253 kshitij.so 497
 
19248 kshitij.so 498
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
499
			ItemPricing i = new ItemPricing();
500
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
501
			i.setItem_id(item_id);
502
			if (itemMap.get(item_id)!=null){
503
				if (checkEmptyString(sheet.getRow(iterator).getCell(1))){
504
					i.setSelling_price(itemMap.get(item_id).getSellingPrice());
505
				}
506
				else{
507
					double sellingPrice = (double) sheet.getRow(iterator).getCell(1).getNumericCellValue();
508
					i.setSelling_price(sellingPrice);
509
				}
510
				if (checkEmptyString(sheet.getRow(iterator).getCell(2))){
511
					i.setMrp(itemMap.get(item_id).getMrp());
512
				}
513
				else{
514
					double mrp = (double) sheet.getRow(iterator).getCell(2).getNumericCellValue();
515
					i.setMrp(mrp);
516
				}
517
				if (checkEmptyString(sheet.getRow(iterator).getCell(3))){
518
					i.setPreferred_vendor(itemMap.get(item_id).getPreferredVendor());
519
				}
520
				else{
521
					long preferred_vendor = (long) sheet.getRow(iterator).getCell(3).getNumericCellValue();
522
					i.setPreferred_vendor(preferred_vendor);
523
				}
524
				if (checkEmptyString(sheet.getRow(iterator).getCell(4))){
525
					i.setPrivate_deal_price(0);
526
				}
527
				else{
528
					double private_deal_price = (double) sheet.getRow(iterator).getCell(4).getNumericCellValue();
529
					i.setPrivate_deal_price(private_deal_price);
530
				}
19249 kshitij.so 531
				if (checkEmptyString(sheet.getRow(iterator).getCell(5))){
532
					i.setWeight(itemMap.get(item_id).getWeight());
533
				}
534
				else{
535
					double weight = (double) sheet.getRow(iterator).getCell(5).getNumericCellValue();
19252 kshitij.so 536
					i.setWeight(weight);
19249 kshitij.so 537
				}
19253 kshitij.so 538
				itemPricingList.add(i);
19248 kshitij.so 539
			}
540
		}
541
		boolean res = cc.updateItemPricing(itemPricingList);
542
		if (res){
543
			setResult("Pricing updated successfully");
544
		}
545
		else{
546
			setResult("Unable to update pricing");
547
		}
548
		return "item-details-json";
549
	}
19253 kshitij.so 550
 
19248 kshitij.so 551
	public String downloadVendors() throws TException, IOException{
552
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
553
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
554
			return "authfail";
555
		}
556
		InventoryClient inventoryServiceClient = new InventoryClient();
557
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
558
		List<Vendor> vendors = inventoryClient.getAllVendors();
19253 kshitij.so 559
 
19248 kshitij.so 560
		File file = new File("/tmp/vendors.xls");
561
		HSSFWorkbook hwb=new HSSFWorkbook();
562
		HSSFSheet sheet =  hwb.createSheet("Vendors");
563
		HSSFRow rowhead=   sheet.createRow((short)0);
564
		rowhead.createCell((short) 0).setCellValue("VENDOR_ID");
565
		rowhead.createCell((short) 1).setCellValue("VENDOR_NAME");
19253 kshitij.so 566
 
19248 kshitij.so 567
		int iterator= 1;
568
		for (Vendor v : vendors){
569
			HSSFRow row = sheet.createRow((short)iterator);
570
			row.createCell((short) 0).setCellValue(v.getId());
571
			row.createCell((short) 1).setCellValue(v.getName());
572
			iterator++;
573
		}
19253 kshitij.so 574
 
19248 kshitij.so 575
		FileOutputStream fileOut = null;
576
		try {
577
			fileOut = new FileOutputStream(file);
578
		} catch (FileNotFoundException e) {
579
			// TODO Auto-generated catch block
580
			e.printStackTrace();
581
		}
582
		try {
583
			hwb.write(fileOut);
584
		} catch (IOException e) {
585
			// TODO Auto-generated catch block
586
			e.printStackTrace();
587
		}
588
		try {
589
			fileOut.close();
590
		} catch (IOException e) {
591
			// TODO Auto-generated catch block
592
			e.printStackTrace();
593
		}
594
		byte[] buffer = new byte[(int)file.length()];
595
		InputStream input = null;
596
		try {
597
			int totalBytesRead = 0;
598
			input = new BufferedInputStream(new FileInputStream(file));
599
			while(totalBytesRead < buffer.length){
600
				int bytesRemaining = buffer.length - totalBytesRead;
601
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
602
				if (bytesRead > 0){
603
					totalBytesRead = totalBytesRead + bytesRead;
604
				}
605
			}
606
		}
607
		finally {
608
			input.close();
609
			file.delete();
610
		}
611
 
612
		response.setHeader("Content-Disposition", "attachment; filename=\"Vendors.xls\"");
613
		response.setContentType("application/octet-stream");
614
		ServletOutputStream sos;
615
		try {
616
			sos = response.getOutputStream();
617
			sos.write(buffer);
618
			sos.flush();
619
		} catch (IOException e) {
620
			System.out.println("Unable to stream the manifest file");
621
		}
622
		setResult("Vendor Streaming done");
623
		return "item-details-json";
19253 kshitij.so 624
 
19248 kshitij.so 625
	}
19253 kshitij.so 626
 
19248 kshitij.so 627
	public String downloadWarehouses() throws TException, IOException{
628
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
629
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
630
			return "authfail";
631
		}
632
		InventoryClient inventoryServiceClient = new InventoryClient();
633
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
634
		List<Warehouse> warehouses = inventoryClient.getWarehouses(null, null, 0, 0, 0);
19253 kshitij.so 635
 
19248 kshitij.so 636
		File file = new File("/tmp/warehouses.xls");
637
		HSSFWorkbook hwb=new HSSFWorkbook();
638
		HSSFSheet sheet =  hwb.createSheet("Warehouses");
639
		HSSFRow rowhead=   sheet.createRow((short)0);
640
		rowhead.createCell((short) 0).setCellValue("warehouse_id");
641
		rowhead.createCell((short) 1).setCellValue("display_name");
642
		rowhead.createCell((short) 2).setCellValue("location");
643
		rowhead.createCell((short) 3).setCellValue("vendor_id");
644
		rowhead.createCell((short) 4).setCellValue("vendor_name");
645
		rowhead.createCell((short) 5).setCellValue("inventory_type");
646
		rowhead.createCell((short) 6).setCellValue("warehouse_type");
647
		rowhead.createCell((short) 7).setCellValue("logistics_location");
648
		rowhead.createCell((short) 8).setCellValue("state_id");
19253 kshitij.so 649
 
19248 kshitij.so 650
		int iterator= 1;
651
		for (Warehouse w : warehouses){
652
			HSSFRow row = sheet.createRow((short)iterator);
653
			row.createCell((short) 0).setCellValue(w.getId());
654
			row.createCell((short) 1).setCellValue(w.getDisplayName());
655
			row.createCell((short) 2).setCellValue(w.getLocation());
656
			row.createCell((short) 3).setCellValue(w.getVendor().getId());
657
			row.createCell((short) 4).setCellValue(w.getVendor().getName());
658
			row.createCell((short) 5).setCellValue(w.getInventoryType().toString());
659
			row.createCell((short) 6).setCellValue(w.getWarehouseType().toString());
660
			row.createCell((short) 7).setCellValue(w.getLogisticsLocation().toString());
661
			row.createCell((short) 7).setCellValue(w.getStateId());
662
			iterator++;
663
		}
19253 kshitij.so 664
 
19248 kshitij.so 665
		FileOutputStream fileOut = null;
666
		try {
667
			fileOut = new FileOutputStream(file);
668
		} catch (FileNotFoundException e) {
669
			// TODO Auto-generated catch block
670
			e.printStackTrace();
671
		}
672
		try {
673
			hwb.write(fileOut);
674
		} catch (IOException e) {
675
			// TODO Auto-generated catch block
676
			e.printStackTrace();
677
		}
678
		try {
679
			fileOut.close();
680
		} catch (IOException e) {
681
			// TODO Auto-generated catch block
682
			e.printStackTrace();
683
		}
684
		byte[] buffer = new byte[(int)file.length()];
685
		InputStream input = null;
686
		try {
687
			int totalBytesRead = 0;
688
			input = new BufferedInputStream(new FileInputStream(file));
689
			while(totalBytesRead < buffer.length){
690
				int bytesRemaining = buffer.length - totalBytesRead;
691
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
692
				if (bytesRead > 0){
693
					totalBytesRead = totalBytesRead + bytesRead;
694
				}
695
			}
696
		}
697
		finally {
698
			input.close();
699
			file.delete();
700
		}
701
 
702
		response.setHeader("Content-Disposition", "attachment; filename=\"Warehouses.xls\"");
703
		response.setContentType("application/octet-stream");
704
		ServletOutputStream sos;
705
		try {
706
			sos = response.getOutputStream();
707
			sos.write(buffer);
708
			sos.flush();
709
		} catch (IOException e) {
710
			System.out.println("Unable to stream the manifest file");
711
		}
712
		setResult("Warehouse Streaming done");
713
		return "item-details-json";
19253 kshitij.so 714
 
19248 kshitij.so 715
	}
716
 
19988 kshitij.so 717
	public String downloadVirtualInventoryForWarehouse() throws NumberFormatException, TException, IOException{
718
		try{
719
			InventoryClient inventoryServiceClient = new InventoryClient();
720
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
721
			Map<Long, ItemInventory> inventoryMap = inventoryClient.getInventorySnapshot(Long.valueOf(virtualWarehouseId));
19997 kshitij.so 722
			List<Long> itemIds = new ArrayList<Long>();
19988 kshitij.so 723
			File file = new File("/tmp/currentinventory.xls");
724
			HSSFWorkbook hwb=new HSSFWorkbook();
725
			HSSFSheet sheet =  hwb.createSheet("Inventory");
726
			HSSFRow rowhead=   sheet.createRow((short)0);
727
			rowhead.createCell((short) 0).setCellValue("item_id");
19997 kshitij.so 728
			rowhead.createCell((short) 1).setCellValue("brand");
729
			rowhead.createCell((short) 2).setCellValue("model_name");
730
			rowhead.createCell((short) 3).setCellValue("model_number");
731
			rowhead.createCell((short) 4).setCellValue("color");
732
			rowhead.createCell((short) 5).setCellValue("Availability");
733
			rowhead.createCell((short) 6).setCellValue("Reserved");
734
			rowhead.createCell((short) 7).setCellValue("Held");
19253 kshitij.so 735
 
19988 kshitij.so 736
			int iterator= 1;
737
			for (Map.Entry<Long,ItemInventory> entry : inventoryMap.entrySet()) {
738
				HSSFRow row = sheet.createRow((short)iterator);
19997 kshitij.so 739
				itemIds.add(entry.getKey());
19988 kshitij.so 740
				row.createCell((short) 0).setCellValue(entry.getKey());
19997 kshitij.so 741
				row.createCell((short) 5).setCellValue(entry.getValue().getAvailability().get(Long.valueOf(virtualWarehouseId)));
742
				row.createCell((short) 6).setCellValue(entry.getValue().getReserved().get(Long.valueOf(virtualWarehouseId)));
743
				row.createCell((short) 7).setCellValue(entry.getValue().getHeld().get(Long.valueOf(virtualWarehouseId)));
19988 kshitij.so 744
				iterator++;
745
			}
19997 kshitij.so 746
			Client cc = new CatalogClient().getClient();
747
			Map<Long, Item> itemMap = cc.getItems(itemIds);
748
 
749
			iterator--;
750
			Item d_item;
751
			while(iterator!=0){
19999 kshitij.so 752
				long item_id = (long) (sheet.getRow(iterator).getCell(0).getNumericCellValue());
19997 kshitij.so 753
				d_item = itemMap.get(item_id);
754
				if (d_item == null){
755
					iterator--;
756
					continue;
757
				}
758
				sheet.getRow(iterator).createCell((short) 1).setCellValue(d_item.getBrand());
759
				sheet.getRow(iterator).createCell((short) 2).setCellValue(d_item.getModelName());
760
				sheet.getRow(iterator).createCell((short) 3).setCellValue(d_item.getModelNumber());
761
				sheet.getRow(iterator).createCell((short) 4).setCellValue(d_item.getColor());
762
				iterator--;
763
			}
764
 
19988 kshitij.so 765
			FileOutputStream fileOut = null;
766
			try {
767
				fileOut = new FileOutputStream(file);
768
			} catch (FileNotFoundException e) {
769
				// TODO Auto-generated catch block
770
				e.printStackTrace();
771
			}
772
			try {
773
				hwb.write(fileOut);
774
			} catch (IOException e) {
775
				// TODO Auto-generated catch block
776
				e.printStackTrace();
777
			}
778
			try {
779
				fileOut.close();
780
			} catch (IOException e) {
781
				// TODO Auto-generated catch block
782
				e.printStackTrace();
783
			}
784
			byte[] buffer = new byte[(int)file.length()];
785
			InputStream input = null;
786
			try {
787
				int totalBytesRead = 0;
788
				input = new BufferedInputStream(new FileInputStream(file));
789
				while(totalBytesRead < buffer.length){
790
					int bytesRemaining = buffer.length - totalBytesRead;
791
					int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
792
					if (bytesRead > 0){
793
						totalBytesRead = totalBytesRead + bytesRead;
794
					}
795
				}
796
			}
797
			finally {
798
				input.close();
799
				file.delete();
800
			}
801
 
802
			response.setHeader("Content-Disposition", "attachment; filename=\"Inventory.xls\"");
803
			response.setContentType("application/octet-stream");
804
			ServletOutputStream sos;
805
			try {
806
				sos = response.getOutputStream();
807
				sos.write(buffer);
808
				sos.flush();
809
			} catch (IOException e) {
810
				System.out.println("Unable to stream the manifest file");
811
			}
812
		}
813
		catch(Exception e){
19999 kshitij.so 814
			logger.error("Issue wile downloading virtual inventory"+e);
19988 kshitij.so 815
			setResult("Something went wrong");
816
			return "item-details-json";
817
		}
818
		setResult("Streaming done");
819
		return "item-details-json";
820
	}
821
 
822
 
19248 kshitij.so 823
	public boolean checkEmptyString(Cell cell){
824
		if (cell==null || cell.getCellType() == Cell.CELL_TYPE_BLANK){
825
			return true;
826
		}
827
		return false;
828
	}
19253 kshitij.so 829
 
19248 kshitij.so 830
	public String getItemDetails(){
831
		return result;
832
	}
833
 
834
	public void setId(String id) {
835
		this.id = id;
836
	}
837
 
838
	public String getUserName(){
839
		return session.getAttribute(ReportsUtils.USER_NAME).toString();
840
	}
841
 
842
	public HttpServletRequest getRequest() {
843
		logger.info("set request"+request.toString());
844
		return request;
845
	}
846
 
847
	public void setRequest(HttpServletRequest request) {
848
		this.request = request;
849
	}
850
 
851
	public HttpServletResponse getResponse() {
852
		return response;
853
	}
854
 
855
	public void setResponse(HttpServletResponse response) {
856
		this.response = response;
857
	}
858
 
859
	public HttpSession getSession() {
860
		return session;
861
	}
862
 
863
	public void setSession(HttpSession session) {
864
		this.session = session;
865
	}
866
 
867
	public ServletContext getContext() {
868
		return context;
869
	}
870
 
871
	public void setContext(ServletContext context) {
872
		this.context = context;
873
	}
874
 
19253 kshitij.so 875
 
19248 kshitij.so 876
	public void setServletRequest(HttpServletRequest req) {
877
		this.request = req;
878
		this.session = req.getSession();        
879
	}
880
 
881
	public void setServletContext(ServletContext arg0) {
882
		// TODO Auto-generated method stub
883
 
884
	}
885
 
886
	public void setServletResponse(HttpServletResponse response) {
887
		this.response = response;
888
	}
19253 kshitij.so 889
 
19248 kshitij.so 890
	public String getResult() {
891
		return result;
892
	}
893
 
894
	public void setResult(String result) {
895
		this.result = result;
896
	}
19253 kshitij.so 897
 
19248 kshitij.so 898
	public File getFile() {
899
		return file;
900
	}
901
 
902
	public void setFile(File file) {
903
		this.file = file;
904
	}
905
 
19988 kshitij.so 906
	public static void main(String args[]) throws NumberFormatException, TException, IOException{
907
		BulkAddController b =  new BulkAddController();
908
		b.setVirtualWarehouseId("3295");
909
		b.downloadVirtualInventoryForWarehouse();
910
	}
19253 kshitij.so 911
 
19988 kshitij.so 912
 
19248 kshitij.so 913
}