Subversion Repositories SmartDukaan

Rev

Rev 10689 | Rev 11582 | 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;
5711 mandeep.dh 13
import in.shop2020.warehouse.InventoryAge;
10689 manish.sha 14
import in.shop2020.warehouse.Scan;
15
import in.shop2020.warehouse.ScanType;
5711 mandeep.dh 16
import in.shop2020.warehouse.WarehouseService.Client;
17
 
18
import java.io.BufferedInputStream;
19
import java.io.BufferedWriter;
20
import java.io.File;
21
import java.io.FileInputStream;
10689 manish.sha 22
import java.io.FileNotFoundException;
23
import java.io.FileReader;
5711 mandeep.dh 24
import java.io.FileWriter;
25
import java.io.InputStream;
10689 manish.sha 26
import java.util.ArrayList;
27
import java.util.HashMap;
5711 mandeep.dh 28
import java.util.List;
10689 manish.sha 29
import java.util.Map;
5711 mandeep.dh 30
 
31
import javax.servlet.ServletOutputStream;
32
 
33
import org.apache.commons.lang.StringUtils;
34
import org.apache.commons.logging.Log;
35
import org.apache.commons.logging.LogFactory;
36
 
10689 manish.sha 37
 
38
 
5711 mandeep.dh 39
/**
40
 * @author mandeep
41
 *
42
 */
43
public class InventoryAgeController extends BaseController {
10689 manish.sha 44
	private static Log logger = LogFactory.getLog(InventoryAgeController.class);
5711 mandeep.dh 45
 
10689 manish.sha 46
	public String index() {
47
		try {
48
			Client client = new WarehouseClient().getClient();
49
			List<InventoryAge> inventoryAge = client.getInventoryAge();
50
			List<InventoryAge> inventoryAgeAmazon = getAmazonInventoryAge();
51
			byte[] buffer = null;
52
			File file = createFile(inventoryAge, inventoryAgeAmazon);
53
			Thread.sleep(30000);
54
			buffer = new byte[(int) file.length()];
55
			InputStream input = null;
56
			try {
57
				int totalBytesRead = 0;
58
				input = new BufferedInputStream(new FileInputStream(file));
59
				while (totalBytesRead < buffer.length) {
60
					int bytesRemaining = buffer.length - totalBytesRead;
61
					// input.read() returns -1, 0, or more :
62
					int bytesRead = input.read(buffer, totalBytesRead,
63
							bytesRemaining);
64
					if (bytesRead > 0) {
65
						totalBytesRead = totalBytesRead + bytesRead;
66
					}
67
				}
68
				/*
69
				 * the above style is a bit tricky: it places bytes into the
70
				 * 'buffer' array; 'buffer' is an output parameter; the while
71
				 * loop usually has a single iteration only.
72
				 */
73
			} finally {
74
				input.close();
75
			}
5711 mandeep.dh 76
 
77
 
78
 
79
 
10689 manish.sha 80
			response.setContentType("application/vnd.ms-excel");
81
			response.setHeader("Content-disposition", "inline; filename="
82
					+ file.getName());
5711 mandeep.dh 83
 
10689 manish.sha 84
			ServletOutputStream sos = response.getOutputStream();
85
			sos.write(buffer);
86
			sos.flush();      
5711 mandeep.dh 87
 
10689 manish.sha 88
		}
89
		catch (Exception e) {
90
		}
91
 
92
		return null;
93
	}
94
 
95
	/**
96
	 * @param inventoryAge
97
	 * @return
98
	 */
99
	private File createFile(List<InventoryAge> inventoryAge, List<InventoryAge> inventoryAgeAmazon) {
100
		Map<Long,InventoryAge> inventoryAgeMap = new HashMap<Long,InventoryAge>();
101
 
102
		for(InventoryAge invAge : inventoryAge){
103
			inventoryAgeMap.put(invAge.getItemId(), invAge);
104
		}
105
 
106
		for(InventoryAge invAge : inventoryAgeAmazon){
107
			if(inventoryAgeMap.containsKey(invAge.getItemId())){
108
				InventoryAge invAgeObj = inventoryAgeMap.get(invAge.getItemId());
109
				invAgeObj.setFreshCount(invAgeObj.getFreshCount()+ invAge.getFreshCount());
110
				invAgeObj.setOneToTwoCount(invAgeObj.getOneToTwoCount() + invAge.getOneToTwoCount());
111
				invAgeObj.setTwoToThreeCount(invAgeObj.getTwoToThreeCount() + invAge.getTwoToThreeCount());
112
 
113
				invAgeObj.setThreeToFourCount(invAgeObj.getThreeToFourCount() + invAge.getThreeToFourCount());
114
				invAgeObj.setFourPlusCount(invAgeObj.getFourPlusCount() + invAge.getFourPlusCount());
115
				invAgeObj.setThreeMonthPlusCount(invAgeObj.getThreeMonthPlusCount() + invAge.getThreeMonthPlusCount());
116
				invAgeObj.setSixMonthPlusCount(invAgeObj.getSixMonthPlusCount() + invAge.getSixMonthPlusCount());
11219 manish.sha 117
				invAgeObj.setZeroToThreeMonthCount(invAgeObj.getZeroToThreeMonthCount() + invAge.getZeroToThreeMonthCount());
118
				invAgeObj.setThreeToSixMonthCount(invAgeObj.getThreeToSixMonthCount() + invAge.getThreeToSixMonthCount());
119
				invAgeObj.setSixToTwelveMonthCount(invAgeObj.getSixToTwelveMonthCount() + invAge.getSixToTwelveMonthCount());
120
				invAgeObj.setTwelveMonthsPlusCount(invAgeObj.getTwelveMonthsPlusCount() + invAge.getTwelveMonthsPlusCount());
10689 manish.sha 121
				invAgeObj.setZeroPlusCount(invAgeObj.getZeroPlusCount() + invAge.getZeroPlusCount());
122
				invAgeObj.setOnePlusCount(invAgeObj.getOnePlusCount() + invAge.getOnePlusCount());
123
				invAgeObj.setZeroPlusCost(invAgeObj.getZeroPlusCost() + invAge.getZeroPlusCost());
124
				invAgeObj.setOnePlusCost(invAgeObj.getOnePlusCost() + invAge.getOnePlusCost());
125
 
126
				inventoryAgeMap.put(invAge.getItemId(), invAgeObj);
127
 
128
			}
129
			else{
130
				inventoryAgeMap.put(invAge.getItemId(), invAge);
131
			}
132
		}
133
		List<InventoryAge> finalInventoryAge = new ArrayList<InventoryAge>();
134
		for(Long itemId : inventoryAgeMap.keySet()){
135
			finalInventoryAge.add(inventoryAgeMap.get(itemId));
136
		}
137
		try {
138
			File file = new File("/tmp/InventoryAge.xls");
139
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
140
			bufferedWriter.write(StringUtils.join(new String[] {
141
					"Item Id",
142
					"Category",
143
					"Brand",
144
					"Model Name",
145
					"Model Number",
146
					"Color",
147
					"Fresh",
148
					"1-2 week",
149
					"2-3 week",
150
					"3-4 week",
151
					"4+ week",
152
					"3+ month",
153
					"6+ month",
11219 manish.sha 154
					"0-3 month",
155
					"3-6 month",
156
					"6-12 month",
157
					"12+ month",
10689 manish.sha 158
					"1+ week",
159
					"1+ week cost",
160
					"All",
161
			"All cost" }, '\t'));
162
 
163
			for (InventoryAge item : finalInventoryAge) {
164
				bufferedWriter.newLine();
165
 
166
				bufferedWriter.write(StringUtils.join(
167
						new String[] {
168
								String.valueOf(item.getItemId()),
169
								item.getCategory(),
170
								item.getBrand(),
171
								item.getModelName(),
172
								item.getModelNumber(),
173
								item.getColor(),
174
								String.valueOf(item.getFreshCount()),
175
								String.valueOf(item.getOneToTwoCount()),
176
								String.valueOf(item.getTwoToThreeCount()),
177
								String.valueOf(item.getThreeToFourCount()),
178
								String.valueOf(item.getFourPlusCount()),
179
								String.valueOf(item.getThreeMonthPlusCount()),
180
								String.valueOf(item.getSixMonthPlusCount()),
11219 manish.sha 181
								String.valueOf(item.getZeroToThreeMonthCount()),
182
								String.valueOf(item.getThreeToSixMonthCount()),
183
								String.valueOf(item.getSixToTwelveMonthCount()),
184
								String.valueOf(item.getTwelveMonthsPlusCount()),
10689 manish.sha 185
								String.valueOf(item.getOnePlusCount()),
186
								String.valueOf(item.getOnePlusCost()),
187
								String.valueOf(item.getZeroPlusCount()),
188
								String.valueOf(item.getZeroPlusCost())}, '\t'));
189
			}
190
 
191
			bufferedWriter.close();
192
			return file;
193
		} catch (Exception e) {
194
			return null;
195
		}
196
	}
197
 
198
 
199
	public List<InventoryAge> getAmazonInventoryAge(){
200
		Long currentTimestamp = System.currentTimeMillis();
201
 
202
		List<InventoryAge> amazonInventoryAge = new ArrayList<InventoryAge>();
203
		try{
204
			InventoryService.Client inventoryClient = new InventoryClient().getClient();
205
			List<AmazonFbaInventorySnapshot> amazonFbaInventorySnapshot = inventoryClient.getAllAmazonFbaItemInventory();
206
 
207
			List<Long> itemIds = new ArrayList<Long>();
208
			for(AmazonFbaInventorySnapshot invSnapShot : amazonFbaInventorySnapshot)
209
				itemIds.add(invSnapShot.getItem_id());
210
			Client client = new WarehouseClient().getClient();
211
 
212
			List<AmazonTransferredSkuDetail> amazonTransferredSkuDetails = client.getAmazonTransferredSkuDetails(itemIds);
213
 
214
			Map<Long, List<AmazonTransferredSkuDetail>> skuDetailsMap = new HashMap<Long, List<AmazonTransferredSkuDetail>>();
215
 
216
			for(AmazonFbaInventorySnapshot invSnapShot : amazonFbaInventorySnapshot){
217
				Long itemId= invSnapShot.getItem_id();
218
				List<AmazonTransferredSkuDetail> mappedList = new ArrayList<AmazonTransferredSkuDetail>();
219
				for(AmazonTransferredSkuDetail detail: amazonTransferredSkuDetails){
220
					if(itemId == detail.getItemId()){
221
						mappedList.add(detail);
222
					}
223
				}
224
				skuDetailsMap.put(itemId, mappedList);
225
			}
226
 
227
			//CatalogService.Client catalogClient = new CatalogClient().getClient();
228
 
229
			for(AmazonFbaInventorySnapshot invSnapShot : amazonFbaInventorySnapshot){
230
				try{
231
 
232
					Long item_id = invSnapShot.getItem_id();
233
					List<AmazonTransferredSkuDetail> amazonTranSkuDetails = skuDetailsMap.get(item_id);
234
					long amazonFbaInvAvailability = invSnapShot.getAvailability();
235
					List<AmazonTransferredSkuDetail> toBeIncludeList = new ArrayList<AmazonTransferredSkuDetail>();
236
 
237
					long freshCount = 0;
238
					long oneToTwoCount = 0;
239
					long twoToThreeCount = 0;
240
					long threeToFourCount = 0;
241
					long fourPlusCount = 0;
242
					long onePlusCount = 0;
243
					long threeMonthPlusCount = 0;
244
					long sixMonthPlusCount = 0;
11219 manish.sha 245
					long zeroToThreeMonthCount = 0;
246
					long threeToSixMonthsCount = 0;
247
					long sixToTwelveMonthsCount = 0;
248
					long twelveMonthsPlusCount = 0;					
10689 manish.sha 249
					long onePlusCost = 0;
250
					long zeroPlusCount = 0;
251
					long zeroPlusCost = 0;
252
					String brand ="";
253
					String modelName ="";
254
					String modelNumber ="";
255
					String color ="";
256
					String category ="";
257
 
258
					if(amazonTranSkuDetails!=null && amazonTranSkuDetails.size()>0){
259
						brand = amazonTranSkuDetails.get(0).getBrand();
260
						modelName = amazonTranSkuDetails.get(0).getModelName();
261
						modelNumber = amazonTranSkuDetails.get(0).getModelNumber();
262
						color = amazonTranSkuDetails.get(0).getColor();
263
						category = amazonTranSkuDetails.get(0).getCategory();
264
					}else{
265
						continue;
266
					}
267
					double invAge = 0;
268
 
269
					for(AmazonTransferredSkuDetail amazonSkuDetail : amazonTranSkuDetails){
270
						if(amazonFbaInvAvailability == amazonSkuDetail.getQuantity()){
271
							toBeIncludeList.add(amazonSkuDetail);
272
							break;
273
						}
274
						if(amazonFbaInvAvailability > amazonSkuDetail.getQuantity()){
275
							toBeIncludeList.add(amazonSkuDetail);
276
							amazonFbaInvAvailability = amazonFbaInvAvailability- amazonSkuDetail.getQuantity();
277
							continue;
278
						}
279
						if(amazonFbaInvAvailability < amazonSkuDetail.getQuantity()){
280
							amazonSkuDetail.setQuantity(amazonFbaInvAvailability);
281
							toBeIncludeList.add(amazonSkuDetail);
282
							break;
283
						}
284
					}
285
 
286
					for(AmazonTransferredSkuDetail amazonSkuInfo : toBeIncludeList){
287
						if(!amazonSkuInfo.isSetPurchaseDate() || amazonSkuInfo.getPurchaseDate() == 0){
288
							if(!client.isAlive()){
289
								client = new WarehouseClient().getClient();
290
							}
291
							List<Scan> purchaseScans = client.getScansforPurchase(amazonSkuInfo.getPurchaseId(), ScanType.PURCHASE);
292
							amazonSkuInfo.setPurchaseDate(purchaseScans.get(0).getScannedAt());
293
						}
294
						invAge = (double)(currentTimestamp - amazonSkuInfo.getPurchaseDate())/ (24 * 60 * 60 * 1000 * 7);
295
						if(invAge < 1){
296
							freshCount = freshCount + amazonSkuInfo.getQuantity();
297
						}
298
						if(invAge >= 1 && invAge < 2){
299
							oneToTwoCount = oneToTwoCount + amazonSkuInfo.getQuantity();
300
						}
301
						if(invAge >= 2 && invAge < 3){
302
							twoToThreeCount = twoToThreeCount + amazonSkuInfo.getQuantity();
303
						}
304
						if(invAge >= 3 && invAge < 4){
305
							threeToFourCount = threeToFourCount + amazonSkuInfo.getQuantity();
306
						}
307
						if(invAge >= 4){
308
							fourPlusCount = fourPlusCount + amazonSkuInfo.getQuantity();
309
						}
310
						if(invAge >= 1){
311
							onePlusCount = onePlusCount + amazonSkuInfo.getQuantity();
312
							onePlusCost = (long)(onePlusCost + ( amazonSkuInfo.getQuantity() * amazonSkuInfo.getUnitPrice() ));
313
						}
314
						if(invAge >= 13){
315
							threeMonthPlusCount = threeMonthPlusCount + amazonSkuInfo.getQuantity();
316
						}
317
						if(invAge >= 26){
318
							sixMonthPlusCount = sixMonthPlusCount + amazonSkuInfo.getQuantity();
319
						}
11219 manish.sha 320
 
321
						if(invAge >0 && invAge <=13){
322
							zeroToThreeMonthCount = zeroToThreeMonthCount + amazonSkuInfo.getQuantity();
323
						}
324
 
325
						if(invAge >13 && invAge <=26){
326
							threeToSixMonthsCount = threeToSixMonthsCount + amazonSkuInfo.getQuantity();
327
						}
328
 
329
						if(invAge >26 && invAge <=52){
330
							sixToTwelveMonthsCount = sixToTwelveMonthsCount + amazonSkuInfo.getQuantity();
331
						}
332
 
333
						if(invAge > 52){
334
							twelveMonthsPlusCount = twelveMonthsPlusCount + amazonSkuInfo.getQuantity();
335
						}
10689 manish.sha 336
 
337
						zeroPlusCount = zeroPlusCount + amazonSkuInfo.getQuantity(); 
338
 
339
						zeroPlusCost = (long)(zeroPlusCost + ( amazonSkuInfo.getQuantity() * amazonSkuInfo.getUnitPrice() ));
340
					}
341
 
342
 
343
					InventoryAge inventAge = new InventoryAge();
344
					inventAge.setItemId(item_id);
345
					inventAge.setBrand(brand);
346
					inventAge.setModelName(modelName);
347
					inventAge.setModelNumber(modelNumber);
348
					inventAge.setColor(color);
349
					inventAge.setFreshCount(freshCount);
350
					inventAge.setOneToTwoCount(oneToTwoCount);
351
					inventAge.setTwoToThreeCount(twoToThreeCount);
352
					inventAge.setThreeToFourCount(threeToFourCount);
353
					inventAge.setFourPlusCount(fourPlusCount);
354
					inventAge.setThreeMonthPlusCount(threeMonthPlusCount);
355
					inventAge.setSixMonthPlusCount(sixMonthPlusCount);
11219 manish.sha 356
					inventAge.setZeroToThreeMonthCount(zeroToThreeMonthCount);
357
					inventAge.setThreeToSixMonthCount(threeToSixMonthsCount);
358
					inventAge.setSixToTwelveMonthCount(sixToTwelveMonthsCount);
359
					inventAge.setTwelveMonthsPlusCount(twelveMonthsPlusCount);
10689 manish.sha 360
					inventAge.setZeroPlusCount(zeroPlusCount);
361
					inventAge.setOnePlusCount(onePlusCount);
362
					inventAge.setZeroPlusCost(zeroPlusCost);
363
					inventAge.setOnePlusCost(onePlusCost);
364
					inventAge.setCategory(category);
365
 
366
					amazonInventoryAge.add(inventAge);
367
				}
368
				catch(Exception e){
369
					e.printStackTrace();				
370
				}
371
			}
372
 
373
		}
374
		catch(Exception e){
375
			e.printStackTrace();				
376
		}
377
		return amazonInventoryAge;
378
	}
379
 
380
	public File createFile(List<InventoryAge> inventoryAge, String name){
381
		try {
382
			File file = new File(name);
383
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
384
			bufferedWriter.write(StringUtils.join(new String[] {
385
					"Item Id",
386
					"Category",
387
					"Brand",
388
					"Model Name",
389
					"Model Number",
390
					"Color",
391
					"Fresh",
392
					"1-2 week",
393
					"2-3 week",
394
					"3-4 week",
395
					"4+ week",
396
					"3+ month",
397
					"6+ month",
11219 manish.sha 398
					"0-3 month",
399
					"3-6 month",
400
					"6-12 month",
401
					"12+ month",
10689 manish.sha 402
					"1+ week",
403
					"1+ week cost",
404
					"All",
405
			"All cost" }, '\t'));
406
 
407
			for (InventoryAge item : inventoryAge) {
408
				bufferedWriter.newLine();
409
 
410
				bufferedWriter.write(StringUtils.join(
411
						new String[] {
412
								String.valueOf(item.getItemId()),
413
								item.getCategory(),
414
								item.getBrand(),
415
								item.getModelName(),
416
								item.getModelNumber(),
417
								item.getColor(),
418
								String.valueOf(item.getFreshCount()),
419
								String.valueOf(item.getOneToTwoCount()),
420
								String.valueOf(item.getTwoToThreeCount()),
421
								String.valueOf(item.getThreeToFourCount()),
422
								String.valueOf(item.getFourPlusCount()),
423
								String.valueOf(item.getThreeMonthPlusCount()),
424
								String.valueOf(item.getSixMonthPlusCount()),
11219 manish.sha 425
								String.valueOf(item.getZeroToThreeMonthCount()),
426
								String.valueOf(item.getThreeToSixMonthCount()),
427
								String.valueOf(item.getSixToTwelveMonthCount()),
428
								String.valueOf(item.getTwelveMonthsPlusCount()),
10689 manish.sha 429
								String.valueOf(item.getOnePlusCount()),
430
								String.valueOf(item.getOnePlusCost()),
431
								String.valueOf(item.getZeroPlusCount()),
432
								String.valueOf(item.getZeroPlusCost())}, '\t'));
433
			}
434
 
435
			bufferedWriter.close();
436
			return file;
437
		} catch (Exception e) {
438
			return null;
439
		}
440
 
441
	}
442
 
443
	public void downloadAmazonInventoryAge(){
444
		try {
445
			List<InventoryAge> inventoryAgeAmazon = getAmazonInventoryAge();
446
			byte[] buffer = null;
447
			File file = createFile(inventoryAgeAmazon,"/tmp/AmazonInventoryAge.xls");
448
			Thread.sleep(10000);
449
			buffer = new byte[(int) file.length()];
450
			InputStream input = null;
451
			try {
452
				int totalBytesRead = 0;
453
				input = new BufferedInputStream(new FileInputStream(file));
454
				while (totalBytesRead < buffer.length) {
455
					int bytesRemaining = buffer.length - totalBytesRead;
456
					// input.read() returns -1, 0, or more :
457
					int bytesRead = input.read(buffer, totalBytesRead,
458
							bytesRemaining);
459
					if (bytesRead > 0) {
460
						totalBytesRead = totalBytesRead + bytesRead;
461
					}
462
				}
463
				/*
464
				 * the above style is a bit tricky: it places bytes into the
465
				 * 'buffer' array; 'buffer' is an output parameter; the while
466
				 * loop usually has a single iteration only.
467
				 */
468
			} finally {
469
				input.close();
470
			}
471
 
472
 
473
 
474
 
475
			response.setContentType("application/vnd.ms-excel");
476
			response.setHeader("Content-disposition", "inline; filename="
477
					+ file.getName());
478
 
479
			ServletOutputStream sos = response.getOutputStream();
480
			sos.write(buffer);
481
			sos.flush();      
482
 
483
		}
484
		catch (Exception e) {
485
		}
486
 
487
	}
488
 
489
	public void downloadSaholicInventoryAge(){
490
		try {
491
			Client client = new WarehouseClient().getClient();
492
			List<InventoryAge> inventoryAge = client.getInventoryAge();
493
			byte[] buffer = null;
494
			File file = createFile(inventoryAge,"/tmp/SaholicInventoryAge.xls");
495
			Thread.sleep(10000);
496
			buffer = new byte[(int) file.length()];
497
			InputStream input = null;
498
			try {
499
				int totalBytesRead = 0;
500
				input = new BufferedInputStream(new FileInputStream(file));
501
				while (totalBytesRead < buffer.length) {
502
					int bytesRemaining = buffer.length - totalBytesRead;
503
					// input.read() returns -1, 0, or more :
504
					int bytesRead = input.read(buffer, totalBytesRead,
505
							bytesRemaining);
506
					if (bytesRead > 0) {
507
						totalBytesRead = totalBytesRead + bytesRead;
508
					}
509
				}
510
				/*
511
				 * the above style is a bit tricky: it places bytes into the
512
				 * 'buffer' array; 'buffer' is an output parameter; the while
513
				 * loop usually has a single iteration only.
514
				 */
515
			} finally {
516
				input.close();
517
			}
518
 
519
 
520
 
521
 
522
			response.setContentType("application/vnd.ms-excel");
523
			response.setHeader("Content-disposition", "inline; filename="
524
					+ file.getName());
525
 
526
			ServletOutputStream sos = response.getOutputStream();
527
			sos.write(buffer);
528
			sos.flush();      
529
 
530
		}
531
		catch (Exception e) {
532
		}
533
 
534
	}
535
 
536
	/*public static void main(String[] args) throws Exception{
537
 
538
	}*/
5711 mandeep.dh 539
}