Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
8363 vikram.rag 1
package com.amazonaws.mws.samples;
2
 
3
import in.shop2020.model.v1.catalog.Amazonlisted;
4
import in.shop2020.model.v1.catalog.CatalogServiceException;
5
import in.shop2020.model.v1.catalog.Item;
6
import in.shop2020.model.v1.inventory.AmazonFbaInventorySnapshot;
7
import in.shop2020.model.v1.inventory.InventoryServiceException;
8
import in.shop2020.model.v1.inventory.Vendor;
9
import in.shop2020.model.v1.inventory.VendorItemPricing;
10
import in.shop2020.model.v1.order.AmazonFbaSalesSnapshot;
11
import in.shop2020.model.v1.order.TransactionService.Client;
12
import in.shop2020.thrift.clients.CatalogClient;
13
import in.shop2020.thrift.clients.InventoryClient;
14
import in.shop2020.thrift.clients.TransactionClient;
15
 
16
import java.io.FileNotFoundException;
17
import java.io.FileOutputStream;
18
import java.io.FileReader;
19
import java.io.IOException;
8825 vikram.rag 20
import java.io.OutputStream;
8363 vikram.rag 21
import java.text.ParseException;
22
import java.text.SimpleDateFormat;
23
import java.util.ArrayList;
8825 vikram.rag 24
import java.util.Calendar;
8363 vikram.rag 25
import java.util.Date;
26
import java.util.HashMap;
27
import java.util.List;
28
import java.util.Map;
29
import java.util.Map.Entry;
30
import java.util.TimeZone;
31
 
32
import org.apache.poi.hssf.usermodel.HSSFRow;
33
import org.apache.poi.hssf.usermodel.HSSFSheet;
34
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
35
import org.apache.thrift.TException;
36
 
37
import au.com.bytecode.opencsv.CSVReader;
38
 
39
public class FileParser {
40
	public static void main(String... args){
41
		CSVReader orderreportreader = null;
42
		CSVReader inventoryhealthreportreader = null;
43
		try {
8438 vikram.rag 44
			orderreportreader = new CSVReader(new FileReader("/home/amazonorderreport.csv"),'\t');
45
			inventoryhealthreportreader = new CSVReader(new FileReader("/home/inventoryhealthreport.csv"),'\t');
8363 vikram.rag 46
		} catch (FileNotFoundException e) {
47
			// TODO Auto-generated catch block
48
			e.printStackTrace();
49
		}
50
		String [] nextLine;
51
		try {
52
			int count =1;
53
			Map<Date,Map<Long,FbaSalesSnapshot>> orderDateItemIdFbaSaleSnapshotMap = new HashMap<Date,Map<Long,FbaSalesSnapshot>>();
54
			while ((nextLine = orderreportreader.readNext()) != null) {
8826 vikram.rag 55
				try{
56
					if(count!=1 && nextLine[5].equalsIgnoreCase("Amazon") && nextLine[6].equalsIgnoreCase("Amazon.in")){
57
						SimpleDateFormat istFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
58
						istFormatter .setLenient(false);
59
						TimeZone zone= TimeZone.getTimeZone("GMT");
60
						istFormatter.setTimeZone(zone);
61
						Date date = istFormatter.parse(nextLine[2]);
62
						SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
63
						Date date_key = dateFormat.parse(dateFormat.format(date));
9256 vikram.rag 64
						System.out.println("Order Details " + nextLine[0]+" "+date_key+" "+ nextLine[11] +" " + nextLine[13] + " " + nextLine[14]);
8826 vikram.rag 65
						Long itemid = Long.parseLong(nextLine[11].replaceAll("FBA",""));
66
						Integer qty=0;
67
						if(nextLine[14].length()!=0){
68
							qty = new Integer(nextLine[14]);
69
						}
70
						Float itemSale = null;
71
						if(nextLine[16].length()!=0){
72
							itemSale = new Float(nextLine[16]);
73
						}
74
						else{
75
							continue;
76
						}
77
						Float itemDiscount; 
78
						if(nextLine[22].length()!=0){
79
							itemDiscount = new Float(nextLine[22]);
80
						}
81
						else{
82
							itemDiscount = new Float(0);
83
						}
84
						if(nextLine[4].equalsIgnoreCase("Cancelled") || nextLine[4].equalsIgnoreCase("Cancelled")){
85
							itemSale = (float) 0; 
86
							itemDiscount = (float) 0;
87
							qty = 0;
88
						}
89
						if(orderDateItemIdFbaSaleSnapshotMap.containsKey(date_key)){
90
							if(orderDateItemIdFbaSaleSnapshotMap.get(date_key).containsKey(itemid)){
91
								FbaSalesSnapshot fbaSalesSnapshot = orderDateItemIdFbaSaleSnapshotMap.get(date_key).get(itemid);
92
								if(itemDiscount!=0){
93
									fbaSalesSnapshot.setPromotionOrderCount(fbaSalesSnapshot.getPromotionOrderCount()+qty);
94
									fbaSalesSnapshot.setTotalPromotionSale(fbaSalesSnapshot.getTotalPromotionSale() + (itemSale - itemDiscount));
95
								}
96
								fbaSalesSnapshot.setTotalOrderCount(fbaSalesSnapshot.getTotalOrderCount() + qty);
97
								fbaSalesSnapshot.setTotalSale(fbaSalesSnapshot.getTotalSale() + itemSale - itemDiscount);
98
								orderDateItemIdFbaSaleSnapshotMap.get(date_key).put(itemid,fbaSalesSnapshot);
8363 vikram.rag 99
							}
8826 vikram.rag 100
							else{
101
								FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
102
								fbaSalesSnapshot.setTotalOrderCount(qty);
103
								fbaSalesSnapshot.setTotalSale(itemSale - itemDiscount);
104
								if(itemDiscount!=0){
105
									fbaSalesSnapshot.setPromotionOrderCount(qty);
106
									fbaSalesSnapshot.setTotalPromotionSale(itemSale - itemDiscount);
107
								}
108
								else{
109
									fbaSalesSnapshot.setPromotionOrderCount(0);
110
									fbaSalesSnapshot.setTotalPromotionSale((float) 0);
111
								}
112
								orderDateItemIdFbaSaleSnapshotMap.get(date_key).put(itemid,fbaSalesSnapshot);
113
							}
8363 vikram.rag 114
						}
115
						else{
8826 vikram.rag 116
							Map<Long,FbaSalesSnapshot> ItemIdFbaSaleSnapshotMap = new HashMap<Long,FbaSalesSnapshot>();
8363 vikram.rag 117
							FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
118
							fbaSalesSnapshot.setTotalOrderCount(qty);
8826 vikram.rag 119
							fbaSalesSnapshot.setTotalSale(itemSale);
8363 vikram.rag 120
							if(itemDiscount!=0){
8826 vikram.rag 121
								fbaSalesSnapshot.setTotalPromotionSale(itemSale - itemDiscount);
8363 vikram.rag 122
								fbaSalesSnapshot.setPromotionOrderCount(qty);
123
							}
124
							else{
8826 vikram.rag 125
								fbaSalesSnapshot.setTotalPromotionSale((float) 0);
8363 vikram.rag 126
								fbaSalesSnapshot.setPromotionOrderCount(0);
127
							}
8826 vikram.rag 128
							ItemIdFbaSaleSnapshotMap.put(itemid,fbaSalesSnapshot);
129
							orderDateItemIdFbaSaleSnapshotMap.put(date_key,ItemIdFbaSaleSnapshotMap);
8363 vikram.rag 130
						}
131
					}
8826 vikram.rag 132
				}
133
				catch(Exception e){
134
					e.printStackTrace();
135
				}
8363 vikram.rag 136
				count++;
137
			}
138
			InventoryClient inventoryServiceClient = null;
139
			TransactionClient transactionServiceClient = null;
140
			CatalogClient catalogServiceClient = null;
141
			try {
142
				inventoryServiceClient = new InventoryClient();
143
				transactionServiceClient = new TransactionClient();
8825 vikram.rag 144
				catalogServiceClient = new CatalogClient();
145
				//catalogServiceClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");
8363 vikram.rag 146
			} catch (Exception e) {
147
				// TODO Auto-generated catch block
148
				e.printStackTrace();
149
			}	
150
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
151
			in.shop2020.model.v1.order.TransactionService.Client transactionClient   = transactionServiceClient.getClient();
152
			in.shop2020.model.v1.catalog.CatalogService.Client catalogClient   = catalogServiceClient.getClient();
153
			SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
8825 vikram.rag 154
			//Date date_today = dateFormat.parse(dateFormat.format(new Date()));
155
			List<Date> dates = new ArrayList<Date>();
156
			Calendar cal = Calendar.getInstance();
157
			cal.add(Calendar.DATE, -1);
158
			Date date_end = dateFormat.parse(dateFormat.format(cal.getTime()));
159
			cal.add(Calendar.DATE, -5);
160
			Date date_start = dateFormat.parse(dateFormat.format(cal.getTime()));
161
			System.out.println("Start Date = " + date_start);
162
			System.out.println("End Date = " + date_end);
163
			Date d = date_start;
164
			while(!d.equals(date_end)){
8826 vikram.rag 165
				cal.setTime(d);
166
				cal.add(Calendar.DATE,1);
8825 vikram.rag 167
				d = cal.getTime();
168
				dates.add(d);
169
			}
8363 vikram.rag 170
			List<AmazonFbaInventorySnapshot> nonzeroFbaInventorySnapshotlist =  inventoryClient.getAllAmazonFbaItemInventory();
8825 vikram.rag 171
			for(Date date:dates){
172
				if(nonzeroFbaInventorySnapshotlist!=null){
173
					for(AmazonFbaInventorySnapshot amazonFbaInventory:nonzeroFbaInventorySnapshotlist){
174
						if(orderDateItemIdFbaSaleSnapshotMap.containsKey(date) ){ 
175
							if(!orderDateItemIdFbaSaleSnapshotMap.get(date).containsKey(amazonFbaInventory.getItem_id())){
176
								Map<Long,FbaSalesSnapshot> ItemIdFbaSaleSnapshotMap = new HashMap<Long,FbaSalesSnapshot>();
177
								FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
178
								fbaSalesSnapshot.setTotalOrderCount(0);
179
								fbaSalesSnapshot.setPromotionOrderCount(0);
180
								fbaSalesSnapshot.setTotalPromotionSale((float) 0);
181
								fbaSalesSnapshot.setTotalSale((float) 0);
182
								ItemIdFbaSaleSnapshotMap.put(amazonFbaInventory.getItem_id(),fbaSalesSnapshot);
183
								orderDateItemIdFbaSaleSnapshotMap.get(date).put(amazonFbaInventory.getItem_id(),fbaSalesSnapshot);
184
							}
185
 
186
						}
187
						else{
8363 vikram.rag 188
							Map<Long,FbaSalesSnapshot> ItemIdFbaSaleSnapshotMap = new HashMap<Long,FbaSalesSnapshot>();
189
							FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
190
							fbaSalesSnapshot.setTotalOrderCount(0);
191
							fbaSalesSnapshot.setPromotionOrderCount(0);
192
							fbaSalesSnapshot.setTotalPromotionSale((float) 0);
193
							fbaSalesSnapshot.setTotalSale((float) 0);
194
							ItemIdFbaSaleSnapshotMap.put(amazonFbaInventory.getItem_id(),fbaSalesSnapshot);
8825 vikram.rag 195
							orderDateItemIdFbaSaleSnapshotMap.put(date,ItemIdFbaSaleSnapshotMap);
8363 vikram.rag 196
						}
197
					}
198
				}
8825 vikram.rag 199
				else{
200
					System.out.println("No inventory in FBA");
201
				}
8363 vikram.rag 202
			}
8461 vikram.rag 203
			Map<Long,PriceAtDate> itemIdOurPriceMap = new HashMap<Long,PriceAtDate>();
204
			Map<Long,PriceAtDate> itemIdSalePriceMap = new HashMap<Long,PriceAtDate>();
205
			Map<Long,PriceAtDate> itemIdminFBAPriceMap = new HashMap<Long,PriceAtDate>();
206
			Map<Long,PriceAtDate> itemIdminMFNPriceMap = new HashMap<Long,PriceAtDate>();
8363 vikram.rag 207
			count=1;
208
			while ((nextLine = inventoryhealthreportreader.readNext()) != null ) {
8825 vikram.rag 209
				try{
210
					if(count!=1){
211
						//System.out.println(nextLine[1] +" "+ nextLine[31] +" " + nextLine[32] + " " + nextLine[34]);
212
						SimpleDateFormat istFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
213
						istFormatter .setLenient(false);
214
						TimeZone zone= TimeZone.getTimeZone("GMT");
215
						istFormatter.setTimeZone(zone);
216
						Date date = istFormatter.parse(nextLine[0]);
217
						Long item_id = Long.parseLong(nextLine[1].replaceAll("FBA",""));
218
						Double ourPrice = null;
219
						Double minFBAPrice= null;
220
						Double minMFNPrice= null;
221
						Double salePrice= null; 
222
						if(nextLine[30].length() >0){
223
							ourPrice = Double.parseDouble(nextLine[30]);
224
							if(itemIdOurPriceMap.containsKey(item_id)){
225
								if(itemIdOurPriceMap.get(item_id).getDate().getTime() < date.getTime()){
226
									PriceAtDate priceAtDate= new PriceAtDate();
227
									priceAtDate.setDate(date);
228
									priceAtDate.setPrice(ourPrice);
229
									itemIdOurPriceMap.put(item_id,priceAtDate);
230
								}
231
							}
232
							else{	
8532 vikram.rag 233
								PriceAtDate priceAtDate= new PriceAtDate();
234
								priceAtDate.setDate(date);
235
								priceAtDate.setPrice(ourPrice);
236
								itemIdOurPriceMap.put(item_id,priceAtDate);
237
							}
8461 vikram.rag 238
						}
8825 vikram.rag 239
						if(nextLine[31].length() >0){
240
							salePrice = Double.parseDouble(nextLine[31]);
241
							if(itemIdSalePriceMap.containsKey(item_id) ){
242
								if(itemIdSalePriceMap.get(item_id).getDate().getTime() < date.getTime()){
243
									PriceAtDate priceAtDate= new PriceAtDate();
244
									priceAtDate.setDate(date);
245
									priceAtDate.setPrice(salePrice);
246
									itemIdSalePriceMap.put(item_id,priceAtDate);
247
								}
248
							}
249
							else{	
8532 vikram.rag 250
								PriceAtDate priceAtDate= new PriceAtDate();
251
								priceAtDate.setDate(date);
252
								priceAtDate.setPrice(salePrice);
253
								itemIdSalePriceMap.put(item_id,priceAtDate);
254
							}
8461 vikram.rag 255
						}
8825 vikram.rag 256
						if(nextLine[32].length() >0){
257
							minFBAPrice = Double.parseDouble(nextLine[32]);
258
							if(itemIdminFBAPriceMap.containsKey(item_id)){
259
								if(itemIdminFBAPriceMap.get(item_id).getDate().getTime() < date.getTime()){
260
									PriceAtDate priceAtDate= new PriceAtDate();
261
									priceAtDate.setDate(date);
262
									priceAtDate.setPrice(minFBAPrice);
263
									itemIdminFBAPriceMap.put(item_id,priceAtDate);
264
								}
265
							}
266
							else{	
8532 vikram.rag 267
								PriceAtDate priceAtDate= new PriceAtDate();
268
								priceAtDate.setDate(date);
269
								priceAtDate.setPrice(minFBAPrice);
270
								itemIdminFBAPriceMap.put(item_id,priceAtDate);
271
							}
8461 vikram.rag 272
						}
8825 vikram.rag 273
						if(nextLine[34].length() >0){
274
							minMFNPrice = Double.parseDouble(nextLine[34]);
275
							if(itemIdminMFNPriceMap.containsKey(item_id)){
276
								if(itemIdminMFNPriceMap.get(item_id).getDate().getTime() < date.getTime()){
277
									PriceAtDate priceAtDate= new PriceAtDate();
278
									priceAtDate.setDate(date);
279
									priceAtDate.setPrice(minMFNPrice);
280
									itemIdminMFNPriceMap.put(item_id,priceAtDate);
281
								}
282
							}
283
							else{	
8532 vikram.rag 284
								PriceAtDate priceAtDate= new PriceAtDate();
285
								priceAtDate.setDate(date);
286
								priceAtDate.setPrice(minMFNPrice);
287
								itemIdminMFNPriceMap.put(item_id,priceAtDate);
288
							}
8461 vikram.rag 289
						}
8451 vikram.rag 290
					}
8363 vikram.rag 291
				}
8825 vikram.rag 292
				catch(Exception e){
293
					e.printStackTrace();
294
				}
8363 vikram.rag 295
				count++;
296
			}
297
			boolean oos;
298
			for (Entry<Date, Map<Long, FbaSalesSnapshot>> entry : orderDateItemIdFbaSaleSnapshotMap.entrySet()){
299
				Date orderDate = entry.getKey();
300
				for(Entry<Long, FbaSalesSnapshot> entry1 :entry.getValue().entrySet()){
301
					System.out.println("Item ID is " + entry1.getKey());
302
					Long inventory = inventoryClient.getAmazonFbaItemInventory(entry1.getKey());
303
 
304
					if(inventory==0 && entry1.getValue().getTotalOrderCount()==0){
305
						oos=true;
306
					}
307
					else{
308
						oos=false;
309
					}
310
					Long item_id = entry1.getKey();
8534 vikram.rag 311
					Amazonlisted amazon_item=catalogClient.getAmazonItemDetails(item_id);
312
					if(amazon_item.getItemid()==0){
313
						continue;
314
					}
8438 vikram.rag 315
					System.out.println(orderDate +","+entry1.getKey()+","+entry1.getValue()+","+ inventory +","+ oos+","+itemIdSalePriceMap.get(item_id)+","+itemIdminFBAPriceMap.get(item_id)+","+itemIdminMFNPriceMap.get(item_id));
8363 vikram.rag 316
					AmazonFbaSalesSnapshot amazonfbasalessnapshot = new AmazonFbaSalesSnapshot();
317
					amazonfbasalessnapshot.setDateOfSale(orderDate.getTime());
318
					amazonfbasalessnapshot.setItem_id(entry1.getKey());
319
					amazonfbasalessnapshot.setTotalOrderCount(entry1.getValue().getPromotionOrderCount());
320
					amazonfbasalessnapshot.setTotalOrderCount(entry1.getValue().getTotalOrderCount());
321
					amazonfbasalessnapshot.setTotalSale(entry1.getValue().getTotalSale());
322
					amazonfbasalessnapshot.setPromotionSale(entry1.getValue().getTotalPromotionSale());
323
					amazonfbasalessnapshot.setIsOutOfStock(oos);
8461 vikram.rag 324
					if(itemIdSalePriceMap.containsKey(item_id) && itemIdSalePriceMap.get(item_id).getPrice()!=0){
325
						amazonfbasalessnapshot.setSalePrice(itemIdSalePriceMap.get(item_id).getPrice());
8532 vikram.rag 326
						amazonfbasalessnapshot.setSalePriceSnapshotDate(itemIdSalePriceMap.get(item_id).getDate().getTime());
8363 vikram.rag 327
					}
328
					else{
8460 vikram.rag 329
						amazonfbasalessnapshot.setSalePrice(0.0);
8532 vikram.rag 330
						amazonfbasalessnapshot.setSalePriceSnapshotDate(0);
8363 vikram.rag 331
					}
8461 vikram.rag 332
					if(itemIdminMFNPriceMap.containsKey(item_id) && itemIdminMFNPriceMap.get(item_id).getPrice()!=0){
333
						amazonfbasalessnapshot.setMinMfnPrice(itemIdminMFNPriceMap.get(item_id).getPrice());
8532 vikram.rag 334
						amazonfbasalessnapshot.setMinMfnPriceSnapshotDate(itemIdminMFNPriceMap.get(item_id).getDate().getTime());
8363 vikram.rag 335
					}
336
					else{
8460 vikram.rag 337
						amazonfbasalessnapshot.setMinMfnPrice(0.0);
8532 vikram.rag 338
						amazonfbasalessnapshot.setMinMfnPriceSnapshotDate(0);
8363 vikram.rag 339
					}
8461 vikram.rag 340
					if(itemIdminFBAPriceMap.containsKey(item_id) && itemIdminFBAPriceMap.get(item_id).getPrice()!=0){
341
						amazonfbasalessnapshot.setMinFbaPrice(itemIdminFBAPriceMap.get(item_id).getPrice());
8532 vikram.rag 342
						amazonfbasalessnapshot.setMinFbaPriceSnapshotDate(itemIdminFBAPriceMap.get(item_id).getDate().getTime());
8363 vikram.rag 343
					}
344
					else{
8460 vikram.rag 345
						amazonfbasalessnapshot.setMinFbaPrice(0.0);
8532 vikram.rag 346
						amazonfbasalessnapshot.setMinFbaPriceSnapshotDate(0);
8363 vikram.rag 347
					}
8461 vikram.rag 348
					if(itemIdOurPriceMap.containsKey(item_id) && itemIdOurPriceMap.get(item_id).getPrice()!=0){
349
						amazonfbasalessnapshot.setOurPrice(itemIdOurPriceMap.get(item_id).getPrice());
8532 vikram.rag 350
						amazonfbasalessnapshot.setOurPriceSnapshotDate(itemIdOurPriceMap.get(item_id).getDate().getTime());
8447 vikram.rag 351
					}
352
					else{
8460 vikram.rag 353
						amazonfbasalessnapshot.setOurPrice(amazon_item.getFbaPrice());
8532 vikram.rag 354
						amazonfbasalessnapshot.setOurPriceSnapshotDate(0);
8447 vikram.rag 355
					}
8363 vikram.rag 356
					amazonfbasalessnapshot.setAmazonFbaInventory(inventory);
357
					transactionClient.addOrUpdateAmazonFbaSalesSnapshot(amazonfbasalessnapshot);
358
				}
359
			}
9256 vikram.rag 360
		} catch (IOException e) {
8363 vikram.rag 361
			// TODO Auto-generated catch block
362
			e.printStackTrace();
363
		} catch (ParseException e) {
364
			// TODO Auto-generated catch block
365
			e.printStackTrace();
366
		} catch (TException e) {
367
			// TODO Auto-generated catch block
368
			e.printStackTrace();
9256 vikram.rag 369
		}
370
		catch (Exception e) {
8825 vikram.rag 371
			// TODO Auto-generated catch block
372
			e.printStackTrace();
8363 vikram.rag 373
		}
8825 vikram.rag 374
 
8363 vikram.rag 375
	}
376
 
377
	public static Double getPercentageDifferenceFromMinimumPrice(AmazonFbaSalesSnapshot snapshot){
378
		Double minPrice = getMinimumSalePriceOnAmazonFBA(snapshot);
8468 vikram.rag 379
		return (((minPrice - snapshot.getOurPrice())/snapshot.getOurPrice())*100);
8363 vikram.rag 380
 
381
	}
382
 
383
	public static Double getMinimumSalePriceOnAmazonFBA(AmazonFbaSalesSnapshot snapshot){
384
		if(snapshot.getMinFbaPrice() > snapshot.getMinMfnPrice()){
385
			return snapshot.getMinMfnPrice();
386
		}
387
		else{
388
			return snapshot.getMinFbaPrice();
389
		}
390
	}
391
 
392
}