Subversion Repositories SmartDukaan

Rev

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