Subversion Repositories SmartDukaan

Rev

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