Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
5711 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.inventory.controllers;
5
 
10689 manish.sha 6
import in.shop2020.model.v1.catalog.CatalogService;
7
import in.shop2020.model.v1.inventory.AmazonFbaInventorySnapshot;
8
import in.shop2020.model.v1.inventory.InventoryService;
9
import in.shop2020.thrift.clients.CatalogClient;
10
import in.shop2020.thrift.clients.InventoryClient;
5711 mandeep.dh 11
import in.shop2020.thrift.clients.WarehouseClient;
10689 manish.sha 12
import in.shop2020.warehouse.AmazonTransferredSkuDetail;
12800 manish.sha 13
import in.shop2020.warehouse.InvAgeConsiderItems;
5711 mandeep.dh 14
import in.shop2020.warehouse.InventoryAge;
12800 manish.sha 15
import in.shop2020.warehouse.InventoryAvailability;
10689 manish.sha 16
import in.shop2020.warehouse.Scan;
17
import in.shop2020.warehouse.ScanType;
5711 mandeep.dh 18
import in.shop2020.warehouse.WarehouseService.Client;
19
 
20
import java.io.BufferedInputStream;
21
import java.io.BufferedWriter;
22
import java.io.File;
23
import java.io.FileInputStream;
10689 manish.sha 24
import java.io.FileNotFoundException;
25
import java.io.FileReader;
5711 mandeep.dh 26
import java.io.FileWriter;
27
import java.io.InputStream;
10689 manish.sha 28
import java.util.ArrayList;
12800 manish.sha 29
import java.util.Collections;
10689 manish.sha 30
import java.util.HashMap;
5711 mandeep.dh 31
import java.util.List;
10689 manish.sha 32
import java.util.Map;
5711 mandeep.dh 33
 
34
import javax.servlet.ServletOutputStream;
35
 
36
import org.apache.commons.lang.StringUtils;
37
import org.apache.commons.logging.Log;
38
import org.apache.commons.logging.LogFactory;
39
 
10689 manish.sha 40
 
41
 
5711 mandeep.dh 42
/**
43
 * @author mandeep
44
 *
45
 */
46
public class InventoryAgeController extends BaseController {
10689 manish.sha 47
	private static Log logger = LogFactory.getLog(InventoryAgeController.class);
5711 mandeep.dh 48
 
10689 manish.sha 49
	public String index() {
50
		try {
12800 manish.sha 51
			List<InventoryAge> inventoryAge = getSorplInventoryAge();
10689 manish.sha 52
			List<InventoryAge> inventoryAgeAmazon = getAmazonInventoryAge();
53
			byte[] buffer = null;
54
			File file = createFile(inventoryAge, inventoryAgeAmazon);
55
			Thread.sleep(30000);
56
			buffer = new byte[(int) file.length()];
57
			InputStream input = null;
58
			try {
59
				int totalBytesRead = 0;
60
				input = new BufferedInputStream(new FileInputStream(file));
61
				while (totalBytesRead < buffer.length) {
62
					int bytesRemaining = buffer.length - totalBytesRead;
63
					// input.read() returns -1, 0, or more :
64
					int bytesRead = input.read(buffer, totalBytesRead,
65
							bytesRemaining);
66
					if (bytesRead > 0) {
67
						totalBytesRead = totalBytesRead + bytesRead;
68
					}
69
				}
70
				/*
71
				 * the above style is a bit tricky: it places bytes into the
72
				 * 'buffer' array; 'buffer' is an output parameter; the while
73
				 * loop usually has a single iteration only.
74
				 */
75
			} finally {
76
				input.close();
77
			}
5711 mandeep.dh 78
 
79
 
80
 
81
 
10689 manish.sha 82
			response.setContentType("application/vnd.ms-excel");
83
			response.setHeader("Content-disposition", "inline; filename="
84
					+ file.getName());
5711 mandeep.dh 85
 
10689 manish.sha 86
			ServletOutputStream sos = response.getOutputStream();
87
			sos.write(buffer);
88
			sos.flush();      
5711 mandeep.dh 89
 
10689 manish.sha 90
		}
91
		catch (Exception e) {
92
		}
93
 
94
		return null;
95
	}
96
 
12800 manish.sha 97
 
98
	public List<InventoryAge> getSorplInventoryAge(){
99
		List<InventoryAge> inventoryAge = new ArrayList<InventoryAge>();
100
		List<InventoryAvailability> serializedInventoryAvailability = new ArrayList<InventoryAvailability>();
101
		List<InventoryAvailability> nonSerializedInventoryAvailability = new ArrayList<InventoryAvailability>();
102
		Map<Long, InventoryAvailability> inventoryAvailabilityMap = new HashMap<Long, InventoryAvailability>();
103
 
104
		try {
105
			WarehouseClient whClient = new WarehouseClient();
106
			Client client = whClient.getClient();
107
 
108
			for(Long physicalWarehouseId : PHYSICAL_WAREHOUSES ){
109
				if(!client.isAlive()){
110
					client = whClient.getClient();
111
				}
112
				serializedInventoryAvailability = client.getCurrentSerializedInventoryByScans(physicalWarehouseId);
113
				nonSerializedInventoryAvailability  = client.getCurrentNonSerializedInventoryByScans(physicalWarehouseId);
114
 
115
				for(InventoryAvailability availability : serializedInventoryAvailability){
116
					if(inventoryAvailabilityMap.containsKey(availability.getItemId())){
117
						InventoryAvailability invAvailbility = inventoryAvailabilityMap.get(availability.getItemId());
118
						invAvailbility.setQuantity(invAvailbility.getQuantity()+availability.getQuantity());
119
						inventoryAvailabilityMap.put(invAvailbility.getItemId(), invAvailbility);
120
					}else{
121
						inventoryAvailabilityMap.put(availability.getItemId(), availability);
122
					}
123
				}
124
				for(InventoryAvailability availability : nonSerializedInventoryAvailability){
125
					if(inventoryAvailabilityMap.containsKey(availability.getItemId())){
126
						InventoryAvailability invAvailbility = inventoryAvailabilityMap.get(availability.getItemId());
127
						invAvailbility.setQuantity(invAvailbility.getQuantity()+availability.getQuantity());
128
						inventoryAvailabilityMap.put(invAvailbility.getItemId(), invAvailbility);
129
					}else{
130
						inventoryAvailabilityMap.put(availability.getItemId(), availability);
131
					}
132
				}
133
			}
134
 
135
			for(Long itemId : inventoryAvailabilityMap.keySet()){
136
				InventoryAvailability stockAvailability = inventoryAvailabilityMap.get(itemId);
137
				long stockQuantity = stockAvailability.getQuantity();
138
 
139
				if(!client.isAlive()){
140
					client = whClient.getClient();
141
				}
142
 
143
				List<InvAgeConsiderItems> invAgeConsiderItems = client.getInventoryAgeConsideredItems(itemId);
144
 
145
				long freshCount = 0;
146
				long oneToTwoCount = 0;
147
				long twoToThreeCount = 0;
148
				long threeToFourCount = 0;
149
				long fourPlusCount = 0;
150
				long onePlusCount = 0;
151
				long threeMonthPlusCount = 0;
152
				long sixMonthPlusCount = 0;
153
				long zeroToThreeMonthCount = 0;
154
				long threeToSixMonthsCount = 0;
155
				long sixToTwelveMonthsCount = 0;
156
				long twelveMonthsPlusCount = 0;					
157
				long onePlusCost = 0;
158
				long zeroPlusCount = 0;
159
				long zeroPlusCost = 0;
160
				String brand =stockAvailability.getBrand();
161
				String modelName =stockAvailability.getModelName();
162
				String modelNumber =stockAvailability.getModelNumber();
163
				String color =stockAvailability.getColor();
164
				String category ="";
165
 
166
				if(invAgeConsiderItems!=null && invAgeConsiderItems.size()>0){
167
					category = invAgeConsiderItems.get(0).getCategory();
168
				}
169
 
170
				if(invAgeConsiderItems==null || invAgeConsiderItems.size()==0){
171
					continue;
172
				}
173
				double invAge = 0.0;
174
 
175
 
176
 
177
				for(InvAgeConsiderItems invItem : invAgeConsiderItems){
178
					invAge = invItem.getAge();
179
					boolean breakInner = false;
180
					if(stockQuantity == invItem.getCurrentQuantity()){
181
						breakInner = true;
182
					}
183
					if(stockQuantity > invItem.getCurrentQuantity()){
184
						stockQuantity = stockQuantity - invItem.getCurrentQuantity();
185
					}
186
					if(stockQuantity < invItem.getCurrentQuantity()){
187
						invItem.setCurrentQuantity(stockQuantity);
188
						breakInner = true;
189
					}
190
 
191
					if(invAge < 1){
192
						freshCount = freshCount + invItem.getCurrentQuantity();
193
					}
194
					if(invAge >= 1 && invAge < 2){
195
						oneToTwoCount = oneToTwoCount + invItem.getCurrentQuantity();
196
					}
197
					if(invAge >= 2 && invAge < 3){
198
						twoToThreeCount = twoToThreeCount + invItem.getCurrentQuantity();
199
					}
200
					if(invAge >= 3 && invAge < 4){
201
						threeToFourCount = threeToFourCount + invItem.getCurrentQuantity();
202
					}
203
					if(invAge >= 4){
204
						fourPlusCount = fourPlusCount + invItem.getCurrentQuantity();
205
					}
206
					if(invAge >= 1){
207
						onePlusCount = onePlusCount + invItem.getCurrentQuantity();
208
						onePlusCost = (long)(onePlusCost + invItem.getCost());
209
					}
210
					if(invAge >= 13){
211
						threeMonthPlusCount = threeMonthPlusCount + invItem.getCurrentQuantity();
212
					}
213
					if(invAge >= 26){
214
						sixMonthPlusCount = sixMonthPlusCount + invItem.getCurrentQuantity();
215
					}
216
 
217
					if(invAge >=0 && invAge <=13){
218
						zeroToThreeMonthCount = zeroToThreeMonthCount + invItem.getCurrentQuantity();
219
					}
220
 
221
					if(invAge >13 && invAge <=26){
222
						threeToSixMonthsCount = threeToSixMonthsCount + invItem.getCurrentQuantity();
223
					}
224
 
225
					if(invAge >26 && invAge <=52){
226
						sixToTwelveMonthsCount = sixToTwelveMonthsCount + invItem.getCurrentQuantity();
227
					}
228
 
229
					if(invAge > 52){
230
						twelveMonthsPlusCount = twelveMonthsPlusCount + invItem.getCurrentQuantity();
231
					}
232
 
233
					zeroPlusCount = zeroPlusCount + invItem.getCurrentQuantity();
234
 
235
					zeroPlusCost = (long)(zeroPlusCost + invItem.getCost());
236
 
237
					if(breakInner)
238
						break;
239
				}
240
 
241
				InventoryAge inventAge = new InventoryAge();
242
				inventAge.setItemId(itemId);
243
				inventAge.setBrand(brand);
244
				inventAge.setModelName(modelName);
245
				inventAge.setModelNumber(modelNumber);
246
				inventAge.setColor(color);
247
				inventAge.setFreshCount(freshCount);
248
				inventAge.setOneToTwoCount(oneToTwoCount);
249
				inventAge.setTwoToThreeCount(twoToThreeCount);
250
				inventAge.setThreeToFourCount(threeToFourCount);
251
				inventAge.setFourPlusCount(fourPlusCount);
252
				inventAge.setThreeMonthPlusCount(threeMonthPlusCount);
253
				inventAge.setSixMonthPlusCount(sixMonthPlusCount);
254
				inventAge.setZeroToThreeMonthCount(zeroToThreeMonthCount);
255
				inventAge.setThreeToSixMonthCount(threeToSixMonthsCount);
256
				inventAge.setSixToTwelveMonthCount(sixToTwelveMonthsCount);
257
				inventAge.setTwelveMonthsPlusCount(twelveMonthsPlusCount);
258
				inventAge.setZeroPlusCount(zeroPlusCount);
259
				inventAge.setOnePlusCount(onePlusCount);
260
				inventAge.setZeroPlusCost(zeroPlusCost);
261
				inventAge.setOnePlusCost(onePlusCost);
262
				inventAge.setCategory(category);
263
				inventoryAge.add(inventAge);
264
			}
265
 
266
 
267
		}
268
		catch (Exception e) {
269
			e.printStackTrace();
270
		}
271
		return inventoryAge;
272
	}
10689 manish.sha 273
	/**
274
	 * @param inventoryAge
275
	 * @return
276
	 */
277
	private File createFile(List<InventoryAge> inventoryAge, List<InventoryAge> inventoryAgeAmazon) {
278
		Map<Long,InventoryAge> inventoryAgeMap = new HashMap<Long,InventoryAge>();
11639 anikendra 279
 
10689 manish.sha 280
		for(InventoryAge invAge : inventoryAge){
281
			inventoryAgeMap.put(invAge.getItemId(), invAge);
282
		}
11639 anikendra 283
 
10689 manish.sha 284
		for(InventoryAge invAge : inventoryAgeAmazon){
285
			if(inventoryAgeMap.containsKey(invAge.getItemId())){
286
				InventoryAge invAgeObj = inventoryAgeMap.get(invAge.getItemId());
287
				invAgeObj.setFreshCount(invAgeObj.getFreshCount()+ invAge.getFreshCount());
288
				invAgeObj.setOneToTwoCount(invAgeObj.getOneToTwoCount() + invAge.getOneToTwoCount());
289
				invAgeObj.setTwoToThreeCount(invAgeObj.getTwoToThreeCount() + invAge.getTwoToThreeCount());
11639 anikendra 290
 
10689 manish.sha 291
				invAgeObj.setThreeToFourCount(invAgeObj.getThreeToFourCount() + invAge.getThreeToFourCount());
292
				invAgeObj.setFourPlusCount(invAgeObj.getFourPlusCount() + invAge.getFourPlusCount());
293
				invAgeObj.setThreeMonthPlusCount(invAgeObj.getThreeMonthPlusCount() + invAge.getThreeMonthPlusCount());
294
				invAgeObj.setSixMonthPlusCount(invAgeObj.getSixMonthPlusCount() + invAge.getSixMonthPlusCount());
11219 manish.sha 295
				invAgeObj.setZeroToThreeMonthCount(invAgeObj.getZeroToThreeMonthCount() + invAge.getZeroToThreeMonthCount());
296
				invAgeObj.setThreeToSixMonthCount(invAgeObj.getThreeToSixMonthCount() + invAge.getThreeToSixMonthCount());
297
				invAgeObj.setSixToTwelveMonthCount(invAgeObj.getSixToTwelveMonthCount() + invAge.getSixToTwelveMonthCount());
298
				invAgeObj.setTwelveMonthsPlusCount(invAgeObj.getTwelveMonthsPlusCount() + invAge.getTwelveMonthsPlusCount());
10689 manish.sha 299
				invAgeObj.setZeroPlusCount(invAgeObj.getZeroPlusCount() + invAge.getZeroPlusCount());
300
				invAgeObj.setOnePlusCount(invAgeObj.getOnePlusCount() + invAge.getOnePlusCount());
301
				invAgeObj.setZeroPlusCost(invAgeObj.getZeroPlusCost() + invAge.getZeroPlusCost());
302
				invAgeObj.setOnePlusCost(invAgeObj.getOnePlusCost() + invAge.getOnePlusCost());
11639 anikendra 303
 
10689 manish.sha 304
				inventoryAgeMap.put(invAge.getItemId(), invAgeObj);
11639 anikendra 305
 
10689 manish.sha 306
			}
307
			else{
308
				inventoryAgeMap.put(invAge.getItemId(), invAge);
309
			}
310
		}
311
		List<InventoryAge> finalInventoryAge = new ArrayList<InventoryAge>();
312
		for(Long itemId : inventoryAgeMap.keySet()){
313
			finalInventoryAge.add(inventoryAgeMap.get(itemId));
314
		}
315
		try {
316
			File file = new File("/tmp/InventoryAge.xls");
317
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
318
			bufferedWriter.write(StringUtils.join(new String[] {
319
					"Item Id",
320
					"Category",
321
					"Brand",
322
					"Model Name",
323
					"Model Number",
324
					"Color",
325
					"Fresh",
326
					"1-2 week",
327
					"2-3 week",
328
					"3-4 week",
329
					"4+ week",
330
					"3+ month",
331
					"6+ month",
11219 manish.sha 332
					"0-3 month",
333
					"3-6 month",
334
					"6-12 month",
335
					"12+ month",
10689 manish.sha 336
					"1+ week",
337
					"1+ week cost",
338
					"All",
339
			"All cost" }, '\t'));
340
 
341
			for (InventoryAge item : finalInventoryAge) {
342
				bufferedWriter.newLine();
343
 
344
				bufferedWriter.write(StringUtils.join(
345
						new String[] {
346
								String.valueOf(item.getItemId()),
347
								item.getCategory(),
348
								item.getBrand(),
349
								item.getModelName(),
350
								item.getModelNumber(),
351
								item.getColor(),
352
								String.valueOf(item.getFreshCount()),
353
								String.valueOf(item.getOneToTwoCount()),
354
								String.valueOf(item.getTwoToThreeCount()),
355
								String.valueOf(item.getThreeToFourCount()),
356
								String.valueOf(item.getFourPlusCount()),
357
								String.valueOf(item.getThreeMonthPlusCount()),
358
								String.valueOf(item.getSixMonthPlusCount()),
11219 manish.sha 359
								String.valueOf(item.getZeroToThreeMonthCount()),
360
								String.valueOf(item.getThreeToSixMonthCount()),
361
								String.valueOf(item.getSixToTwelveMonthCount()),
362
								String.valueOf(item.getTwelveMonthsPlusCount()),
10689 manish.sha 363
								String.valueOf(item.getOnePlusCount()),
364
								String.valueOf(item.getOnePlusCost()),
365
								String.valueOf(item.getZeroPlusCount()),
366
								String.valueOf(item.getZeroPlusCost())}, '\t'));
367
			}
368
 
369
			bufferedWriter.close();
370
			return file;
371
		} catch (Exception e) {
372
			return null;
373
		}
374
	}
375
 
376
 
377
	public List<InventoryAge> getAmazonInventoryAge(){
378
		Long currentTimestamp = System.currentTimeMillis();
379
 
380
		List<InventoryAge> amazonInventoryAge = new ArrayList<InventoryAge>();
381
		try{
382
			InventoryService.Client inventoryClient = new InventoryClient().getClient();
383
			List<AmazonFbaInventorySnapshot> amazonFbaInventorySnapshot = inventoryClient.getAllAmazonFbaItemInventory();
11639 anikendra 384
 
10689 manish.sha 385
			List<Long> itemIds = new ArrayList<Long>();
11639 anikendra 386
 
387
			for(AmazonFbaInventorySnapshot invSnapShot : amazonFbaInventorySnapshot){
11643 vikram.rag 388
				//logger.info("AmazonFbaInventorySnapshot entry " + invSnapShot.getItem_id());
11640 vikram.rag 389
				itemIds.add(invSnapShot.getItem_id());
11639 anikendra 390
			}
391
 
10689 manish.sha 392
			Client client = new WarehouseClient().getClient();
11639 anikendra 393
 
10689 manish.sha 394
			List<AmazonTransferredSkuDetail> amazonTransferredSkuDetails = client.getAmazonTransferredSkuDetails(itemIds);
11582 manish.sha 395
			Map<Long, AmazonFbaInventorySnapshot> amazonFbaSnapshotMap = new HashMap<Long, AmazonFbaInventorySnapshot> ();
11639 anikendra 396
 
10689 manish.sha 397
			Map<Long, List<AmazonTransferredSkuDetail>> skuDetailsMap = new HashMap<Long, List<AmazonTransferredSkuDetail>>();
11639 anikendra 398
 
10689 manish.sha 399
			for(AmazonFbaInventorySnapshot invSnapShot : amazonFbaInventorySnapshot){
11642 vikram.rag 400
				Long itemId = invSnapShot.getItem_id();
11582 manish.sha 401
				if(!amazonFbaSnapshotMap.containsKey(itemId)){
402
					amazonFbaSnapshotMap.put(itemId, invSnapShot);
11643 vikram.rag 403
					//logger.info("Adding Availabity " + itemId + " Availability " +invSnapShot.getAvailability());
11582 manish.sha 404
				} else {
405
					AmazonFbaInventorySnapshot invSS = amazonFbaSnapshotMap.get(itemId);
406
					invSnapShot.setAvailability(invSnapShot.getAvailability() + invSS.getAvailability());
407
					amazonFbaSnapshotMap.put(itemId, invSnapShot);
11643 vikram.rag 408
					//logger.info("Updating Availabity " + itemId + " Availability " +invSnapShot.getAvailability());
11582 manish.sha 409
				}
11639 anikendra 410
 
10689 manish.sha 411
				List<AmazonTransferredSkuDetail> mappedList = new ArrayList<AmazonTransferredSkuDetail>();
412
				for(AmazonTransferredSkuDetail detail: amazonTransferredSkuDetails){
11641 vikram.rag 413
					if(itemId.longValue() == detail.getItemId()){
11642 vikram.rag 414
						logger.info("Amazon Transferred Sku Details Detail found for ITEM ID  " + itemId.longValue()+" " + itemId);
10689 manish.sha 415
						mappedList.add(detail);
416
					}
11639 anikendra 417
					else{
418
					}
10689 manish.sha 419
				}
11641 vikram.rag 420
				skuDetailsMap.put(itemId, mappedList);
10689 manish.sha 421
			}
422
 
423
			//CatalogService.Client catalogClient = new CatalogClient().getClient();
11639 anikendra 424
 
11582 manish.sha 425
			for(AmazonFbaInventorySnapshot invSnapShot : amazonFbaSnapshotMap.values()){
10689 manish.sha 426
				try{
427
 
428
					Long item_id = invSnapShot.getItem_id();
429
					List<AmazonTransferredSkuDetail> amazonTranSkuDetails = skuDetailsMap.get(item_id);
430
					long amazonFbaInvAvailability = invSnapShot.getAvailability();
431
					List<AmazonTransferredSkuDetail> toBeIncludeList = new ArrayList<AmazonTransferredSkuDetail>();
432
 
433
					long freshCount = 0;
434
					long oneToTwoCount = 0;
435
					long twoToThreeCount = 0;
436
					long threeToFourCount = 0;
437
					long fourPlusCount = 0;
438
					long onePlusCount = 0;
439
					long threeMonthPlusCount = 0;
440
					long sixMonthPlusCount = 0;
11219 manish.sha 441
					long zeroToThreeMonthCount = 0;
442
					long threeToSixMonthsCount = 0;
443
					long sixToTwelveMonthsCount = 0;
444
					long twelveMonthsPlusCount = 0;					
10689 manish.sha 445
					long onePlusCost = 0;
446
					long zeroPlusCount = 0;
447
					long zeroPlusCost = 0;
448
					String brand ="";
449
					String modelName ="";
450
					String modelNumber ="";
451
					String color ="";
452
					String category ="";
453
 
454
					if(amazonTranSkuDetails!=null && amazonTranSkuDetails.size()>0){
455
						brand = amazonTranSkuDetails.get(0).getBrand();
456
						modelName = amazonTranSkuDetails.get(0).getModelName();
457
						modelNumber = amazonTranSkuDetails.get(0).getModelNumber();
458
						color = amazonTranSkuDetails.get(0).getColor();
459
						category = amazonTranSkuDetails.get(0).getCategory();
11639 anikendra 460
					}else{	
461
						logger.info("Amazon Sku Transfer details  skipped " + item_id);
10689 manish.sha 462
						continue;
463
					}
464
					double invAge = 0;
465
 
466
					for(AmazonTransferredSkuDetail amazonSkuDetail : amazonTranSkuDetails){
467
						if(amazonFbaInvAvailability == amazonSkuDetail.getQuantity()){
468
							toBeIncludeList.add(amazonSkuDetail);
469
							break;
470
						}
471
						if(amazonFbaInvAvailability > amazonSkuDetail.getQuantity()){
472
							toBeIncludeList.add(amazonSkuDetail);
473
							amazonFbaInvAvailability = amazonFbaInvAvailability- amazonSkuDetail.getQuantity();
474
							continue;
475
						}
476
						if(amazonFbaInvAvailability < amazonSkuDetail.getQuantity()){
477
							amazonSkuDetail.setQuantity(amazonFbaInvAvailability);
478
							toBeIncludeList.add(amazonSkuDetail);
479
							break;
480
						}
481
					}
482
 
483
					for(AmazonTransferredSkuDetail amazonSkuInfo : toBeIncludeList){
484
						if(!amazonSkuInfo.isSetPurchaseDate() || amazonSkuInfo.getPurchaseDate() == 0){
485
							if(!client.isAlive()){
486
								client = new WarehouseClient().getClient();
487
							}
488
							List<Scan> purchaseScans = client.getScansforPurchase(amazonSkuInfo.getPurchaseId(), ScanType.PURCHASE);
489
							amazonSkuInfo.setPurchaseDate(purchaseScans.get(0).getScannedAt());
490
						}
491
						invAge = (double)(currentTimestamp - amazonSkuInfo.getPurchaseDate())/ (24 * 60 * 60 * 1000 * 7);
492
						if(invAge < 1){
493
							freshCount = freshCount + amazonSkuInfo.getQuantity();
494
						}
495
						if(invAge >= 1 && invAge < 2){
496
							oneToTwoCount = oneToTwoCount + amazonSkuInfo.getQuantity();
497
						}
498
						if(invAge >= 2 && invAge < 3){
499
							twoToThreeCount = twoToThreeCount + amazonSkuInfo.getQuantity();
500
						}
501
						if(invAge >= 3 && invAge < 4){
502
							threeToFourCount = threeToFourCount + amazonSkuInfo.getQuantity();
503
						}
504
						if(invAge >= 4){
505
							fourPlusCount = fourPlusCount + amazonSkuInfo.getQuantity();
506
						}
507
						if(invAge >= 1){
508
							onePlusCount = onePlusCount + amazonSkuInfo.getQuantity();
509
							onePlusCost = (long)(onePlusCost + ( amazonSkuInfo.getQuantity() * amazonSkuInfo.getUnitPrice() ));
510
						}
511
						if(invAge >= 13){
512
							threeMonthPlusCount = threeMonthPlusCount + amazonSkuInfo.getQuantity();
513
						}
514
						if(invAge >= 26){
515
							sixMonthPlusCount = sixMonthPlusCount + amazonSkuInfo.getQuantity();
516
						}
11639 anikendra 517
 
12800 manish.sha 518
						if(invAge >=0 && invAge <=13){
11219 manish.sha 519
							zeroToThreeMonthCount = zeroToThreeMonthCount + amazonSkuInfo.getQuantity();
520
						}
11639 anikendra 521
 
11219 manish.sha 522
						if(invAge >13 && invAge <=26){
523
							threeToSixMonthsCount = threeToSixMonthsCount + amazonSkuInfo.getQuantity();
524
						}
11639 anikendra 525
 
11219 manish.sha 526
						if(invAge >26 && invAge <=52){
527
							sixToTwelveMonthsCount = sixToTwelveMonthsCount + amazonSkuInfo.getQuantity();
528
						}
11639 anikendra 529
 
11219 manish.sha 530
						if(invAge > 52){
531
							twelveMonthsPlusCount = twelveMonthsPlusCount + amazonSkuInfo.getQuantity();
532
						}
10689 manish.sha 533
 
534
						zeroPlusCount = zeroPlusCount + amazonSkuInfo.getQuantity(); 
11639 anikendra 535
 
10689 manish.sha 536
						zeroPlusCost = (long)(zeroPlusCost + ( amazonSkuInfo.getQuantity() * amazonSkuInfo.getUnitPrice() ));
537
					}
538
 
11639 anikendra 539
 
10689 manish.sha 540
					InventoryAge inventAge = new InventoryAge();
541
					inventAge.setItemId(item_id);
542
					inventAge.setBrand(brand);
543
					inventAge.setModelName(modelName);
544
					inventAge.setModelNumber(modelNumber);
545
					inventAge.setColor(color);
546
					inventAge.setFreshCount(freshCount);
547
					inventAge.setOneToTwoCount(oneToTwoCount);
548
					inventAge.setTwoToThreeCount(twoToThreeCount);
549
					inventAge.setThreeToFourCount(threeToFourCount);
550
					inventAge.setFourPlusCount(fourPlusCount);
551
					inventAge.setThreeMonthPlusCount(threeMonthPlusCount);
552
					inventAge.setSixMonthPlusCount(sixMonthPlusCount);
11219 manish.sha 553
					inventAge.setZeroToThreeMonthCount(zeroToThreeMonthCount);
554
					inventAge.setThreeToSixMonthCount(threeToSixMonthsCount);
555
					inventAge.setSixToTwelveMonthCount(sixToTwelveMonthsCount);
556
					inventAge.setTwelveMonthsPlusCount(twelveMonthsPlusCount);
10689 manish.sha 557
					inventAge.setZeroPlusCount(zeroPlusCount);
558
					inventAge.setOnePlusCount(onePlusCount);
559
					inventAge.setZeroPlusCost(zeroPlusCost);
560
					inventAge.setOnePlusCost(onePlusCost);
561
					inventAge.setCategory(category);
562
 
563
					amazonInventoryAge.add(inventAge);
564
				}
565
				catch(Exception e){
566
					e.printStackTrace();				
567
				}
568
			}
569
 
570
		}
571
		catch(Exception e){
572
			e.printStackTrace();				
573
		}
574
		return amazonInventoryAge;
575
	}
11639 anikendra 576
 
10689 manish.sha 577
	public File createFile(List<InventoryAge> inventoryAge, String name){
578
		try {
579
			File file = new File(name);
580
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
581
			bufferedWriter.write(StringUtils.join(new String[] {
582
					"Item Id",
583
					"Category",
584
					"Brand",
585
					"Model Name",
586
					"Model Number",
587
					"Color",
588
					"Fresh",
589
					"1-2 week",
590
					"2-3 week",
591
					"3-4 week",
592
					"4+ week",
593
					"3+ month",
594
					"6+ month",
11219 manish.sha 595
					"0-3 month",
596
					"3-6 month",
597
					"6-12 month",
598
					"12+ month",
10689 manish.sha 599
					"1+ week",
600
					"1+ week cost",
601
					"All",
602
			"All cost" }, '\t'));
603
 
604
			for (InventoryAge item : inventoryAge) {
605
				bufferedWriter.newLine();
606
 
607
				bufferedWriter.write(StringUtils.join(
608
						new String[] {
609
								String.valueOf(item.getItemId()),
610
								item.getCategory(),
611
								item.getBrand(),
612
								item.getModelName(),
613
								item.getModelNumber(),
614
								item.getColor(),
615
								String.valueOf(item.getFreshCount()),
616
								String.valueOf(item.getOneToTwoCount()),
617
								String.valueOf(item.getTwoToThreeCount()),
618
								String.valueOf(item.getThreeToFourCount()),
619
								String.valueOf(item.getFourPlusCount()),
620
								String.valueOf(item.getThreeMonthPlusCount()),
621
								String.valueOf(item.getSixMonthPlusCount()),
11219 manish.sha 622
								String.valueOf(item.getZeroToThreeMonthCount()),
623
								String.valueOf(item.getThreeToSixMonthCount()),
624
								String.valueOf(item.getSixToTwelveMonthCount()),
625
								String.valueOf(item.getTwelveMonthsPlusCount()),
10689 manish.sha 626
								String.valueOf(item.getOnePlusCount()),
627
								String.valueOf(item.getOnePlusCost()),
628
								String.valueOf(item.getZeroPlusCount()),
629
								String.valueOf(item.getZeroPlusCost())}, '\t'));
630
			}
631
 
632
			bufferedWriter.close();
633
			return file;
634
		} catch (Exception e) {
635
			return null;
636
		}
11639 anikendra 637
 
10689 manish.sha 638
	}
11639 anikendra 639
 
10689 manish.sha 640
	public void downloadAmazonInventoryAge(){
641
		try {
642
			List<InventoryAge> inventoryAgeAmazon = getAmazonInventoryAge();
643
			byte[] buffer = null;
644
			File file = createFile(inventoryAgeAmazon,"/tmp/AmazonInventoryAge.xls");
645
			Thread.sleep(10000);
646
			buffer = new byte[(int) file.length()];
647
			InputStream input = null;
648
			try {
649
				int totalBytesRead = 0;
650
				input = new BufferedInputStream(new FileInputStream(file));
651
				while (totalBytesRead < buffer.length) {
652
					int bytesRemaining = buffer.length - totalBytesRead;
653
					// input.read() returns -1, 0, or more :
654
					int bytesRead = input.read(buffer, totalBytesRead,
655
							bytesRemaining);
656
					if (bytesRead > 0) {
657
						totalBytesRead = totalBytesRead + bytesRead;
658
					}
659
				}
660
				/*
661
				 * the above style is a bit tricky: it places bytes into the
662
				 * 'buffer' array; 'buffer' is an output parameter; the while
663
				 * loop usually has a single iteration only.
664
				 */
665
			} finally {
666
				input.close();
667
			}
668
 
669
 
670
 
671
 
672
			response.setContentType("application/vnd.ms-excel");
673
			response.setHeader("Content-disposition", "inline; filename="
674
					+ file.getName());
675
 
676
			ServletOutputStream sos = response.getOutputStream();
677
			sos.write(buffer);
678
			sos.flush();      
679
 
680
		}
681
		catch (Exception e) {
682
		}
683
 
684
	}
11639 anikendra 685
 
10689 manish.sha 686
	public void downloadSaholicInventoryAge(){
687
		try {
12800 manish.sha 688
			List<InventoryAge> inventoryAge = getSorplInventoryAge();
10689 manish.sha 689
			byte[] buffer = null;
690
			File file = createFile(inventoryAge,"/tmp/SaholicInventoryAge.xls");
691
			Thread.sleep(10000);
692
			buffer = new byte[(int) file.length()];
693
			InputStream input = null;
694
			try {
695
				int totalBytesRead = 0;
696
				input = new BufferedInputStream(new FileInputStream(file));
697
				while (totalBytesRead < buffer.length) {
698
					int bytesRemaining = buffer.length - totalBytesRead;
699
					// input.read() returns -1, 0, or more :
700
					int bytesRead = input.read(buffer, totalBytesRead,
701
							bytesRemaining);
702
					if (bytesRead > 0) {
703
						totalBytesRead = totalBytesRead + bytesRead;
704
					}
705
				}
706
				/*
707
				 * the above style is a bit tricky: it places bytes into the
708
				 * 'buffer' array; 'buffer' is an output parameter; the while
709
				 * loop usually has a single iteration only.
710
				 */
711
			} finally {
712
				input.close();
713
			}
714
 
715
 
716
 
717
 
718
			response.setContentType("application/vnd.ms-excel");
719
			response.setHeader("Content-disposition", "inline; filename="
720
					+ file.getName());
721
 
722
			ServletOutputStream sos = response.getOutputStream();
723
			sos.write(buffer);
724
			sos.flush();      
725
 
726
		}
727
		catch (Exception e) {
728
		}
729
 
730
	}
731
 
732
	/*public static void main(String[] args) throws Exception{
11639 anikendra 733
 
10689 manish.sha 734
	}*/
5711 mandeep.dh 735
}