Subversion Repositories SmartDukaan

Rev

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