Subversion Repositories SmartDukaan

Rev

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