Subversion Repositories SmartDukaan

Rev

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