Subversion Repositories SmartDukaan

Rev

Rev 12847 | Rev 12849 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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