Subversion Repositories SmartDukaan

Rev

Rev 9755 | Rev 9757 | 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>>();
9756 vikram.rag 54
			if(count!=1 && nextLine[5].equalsIgnoreCase("Amazon") && nextLine[6].equalsIgnoreCase("Amazon.in")){
55
				SimpleDateFormat istFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
56
				istFormatter .setLenient(false);
57
				TimeZone zone= TimeZone.getTimeZone("GMT");
58
				istFormatter.setTimeZone(zone);
59
				Date date = istFormatter.parse(nextLine[2]);
60
				SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
61
				Date date_key = dateFormat.parse(dateFormat.format(date));
62
				System.out.println("Order Details " + nextLine[0]+" "+date_key+" "+ nextLine[11] +" " + nextLine[13] + " " + nextLine[14]);
63
				Long itemid = Long.parseLong(nextLine[11].replaceAll("FBA",""));
64
				Integer qty=0;
65
				if(nextLine[14].length()!=0){
66
					qty = new Integer(nextLine[14]);
9752 vikram.rag 67
				}
9756 vikram.rag 68
				Float itemSale = null;
69
				if(nextLine[16].length()!=0){
70
					itemSale = new Float(nextLine[16]);
9755 vikram.rag 71
				}
9756 vikram.rag 72
				else{
73
					continue;
74
				}
75
				Float itemDiscount; 
76
				if(nextLine[22].length()!=0){
77
					itemDiscount = new Float(nextLine[22]);
78
				}
79
				else{
80
					itemDiscount = new Float(0);
81
				}
82
				if(nextLine[4].equalsIgnoreCase("Cancelled") || nextLine[13].equalsIgnoreCase("Cancelled")){
83
					itemSale = (float) 0; 
84
					itemDiscount = (float) 0;
85
					qty = 0;
86
				}
87
				if(orderDateItemIdFbaSaleSnapshotMap.containsKey(date_key)){
88
					Map<Long,FbaSalesSnapshot> ItemIdFbaSaleSnapshotMap;
89
					if(orderDateItemIdFbaSaleSnapshotMap.get(date_key).containsKey(itemid)){
90
						FbaSalesSnapshot fbaSalesSnapshot = orderDateItemIdFbaSaleSnapshotMap.get(date_key).get(itemid);
91
						if(itemDiscount!=0){
92
							fbaSalesSnapshot.setPromotionOrderCount(fbaSalesSnapshot.getPromotionOrderCount()+qty);
93
							fbaSalesSnapshot.setTotalPromotionSale(fbaSalesSnapshot.getTotalPromotionSale() + (itemSale - itemDiscount));
8825 vikram.rag 94
						}
9756 vikram.rag 95
						fbaSalesSnapshot.setTotalOrderCount(fbaSalesSnapshot.getTotalOrderCount() + qty);
96
						fbaSalesSnapshot.setTotalSale(fbaSalesSnapshot.getTotalSale() + itemSale - itemDiscount);
97
						ItemIdFbaSaleSnapshotMap = orderDateItemIdFbaSaleSnapshotMap.get(date_key);
98
						ItemIdFbaSaleSnapshotMap.put(itemid,fbaSalesSnapshot); 
99
						orderDateItemIdFbaSaleSnapshotMap.put(date_key,ItemIdFbaSaleSnapshotMap);
100
					}
101
					else{
102
						FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
103
						fbaSalesSnapshot.setTotalOrderCount(qty);
104
						fbaSalesSnapshot.setTotalSale(itemSale - itemDiscount);
105
						if(itemDiscount!=0){
106
							fbaSalesSnapshot.setPromotionOrderCount(qty);
107
							fbaSalesSnapshot.setTotalPromotionSale(itemSale - itemDiscount);
108
						}
8825 vikram.rag 109
						else{
8363 vikram.rag 110
							fbaSalesSnapshot.setPromotionOrderCount(0);
111
							fbaSalesSnapshot.setTotalPromotionSale((float) 0);
112
						}
9756 vikram.rag 113
						ItemIdFbaSaleSnapshotMap = orderDateItemIdFbaSaleSnapshotMap.get(date_key);
114
						ItemIdFbaSaleSnapshotMap.put(itemid,fbaSalesSnapshot); 
115
						orderDateItemIdFbaSaleSnapshotMap.put(date_key,ItemIdFbaSaleSnapshotMap);
8363 vikram.rag 116
					}
117
				}
8825 vikram.rag 118
				else{
9756 vikram.rag 119
					Map<Long,FbaSalesSnapshot> ItemIdFbaSaleSnapshotMap = new HashMap<Long,FbaSalesSnapshot>();
120
					FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
121
					fbaSalesSnapshot.setTotalOrderCount(qty);
122
					fbaSalesSnapshot.setTotalSale(itemSale);
123
					if(itemDiscount!=0){
124
						fbaSalesSnapshot.setTotalPromotionSale(itemSale - itemDiscount);
125
						fbaSalesSnapshot.setPromotionOrderCount(qty);
126
					}
127
					else{
128
						fbaSalesSnapshot.setTotalPromotionSale((float) 0);
129
						fbaSalesSnapshot.setPromotionOrderCount(0);
130
					}
131
					ItemIdFbaSaleSnapshotMap.put(itemid,fbaSalesSnapshot);
132
					orderDateItemIdFbaSaleSnapshotMap.put(date_key,ItemIdFbaSaleSnapshotMap);
8825 vikram.rag 133
				}
8363 vikram.rag 134
			}
9756 vikram.rag 135
		}
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();
144
		catalogServiceClient = new CatalogClient();
145
		//catalogServiceClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");
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");
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)){
165
		cal.setTime(d);
166
		cal.add(Calendar.DATE,1);
167
		d = cal.getTime();
168
		dates.add(d);
169
	}
170
	List<AmazonFbaInventorySnapshot> nonzeroFbaInventorySnapshotlist =  inventoryClient.getAllAmazonFbaItemInventory();
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);
8451 vikram.rag 184
					}
9756 vikram.rag 185
 
8363 vikram.rag 186
				}
9756 vikram.rag 187
				else{
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);
195
					orderDateItemIdFbaSaleSnapshotMap.put(date,ItemIdFbaSaleSnapshotMap);
8825 vikram.rag 196
				}
8363 vikram.rag 197
			}
9756 vikram.rag 198
		}
199
		else{
200
			System.out.println("No inventory in FBA");
201
		}
202
	}
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>();
207
	count=1;
208
	while ((nextLine = inventoryhealthreportreader.readNext()) != null ) {
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
						}
8363 vikram.rag 231
					}
9756 vikram.rag 232
					else{	
233
						PriceAtDate priceAtDate= new PriceAtDate();
234
						priceAtDate.setDate(date);
235
						priceAtDate.setPrice(ourPrice);
236
						itemIdOurPriceMap.put(item_id,priceAtDate);
8363 vikram.rag 237
					}
9756 vikram.rag 238
				}
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
						}
8534 vikram.rag 248
					}
9756 vikram.rag 249
					else{	
250
						PriceAtDate priceAtDate= new PriceAtDate();
251
						priceAtDate.setDate(date);
252
						priceAtDate.setPrice(salePrice);
253
						itemIdSalePriceMap.put(item_id,priceAtDate);
8363 vikram.rag 254
					}
9756 vikram.rag 255
				}
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
						}
8363 vikram.rag 265
					}
9756 vikram.rag 266
					else{	
267
						PriceAtDate priceAtDate= new PriceAtDate();
268
						priceAtDate.setDate(date);
269
						priceAtDate.setPrice(minFBAPrice);
270
						itemIdminFBAPriceMap.put(item_id,priceAtDate);
8363 vikram.rag 271
					}
9756 vikram.rag 272
				}
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
						}
8363 vikram.rag 282
					}
9756 vikram.rag 283
					else{	
284
						PriceAtDate priceAtDate= new PriceAtDate();
285
						priceAtDate.setDate(date);
286
						priceAtDate.setPrice(minMFNPrice);
287
						itemIdminMFNPriceMap.put(item_id,priceAtDate);
8363 vikram.rag 288
					}
289
				}
290
			}
9256 vikram.rag 291
		}
9756 vikram.rag 292
		catch(Exception e){
8825 vikram.rag 293
			e.printStackTrace();
8363 vikram.rag 294
		}
9756 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());
8825 vikram.rag 303
 
9756 vikram.rag 304
			if(inventory==0 && entry1.getValue().getTotalOrderCount()==0){
305
				oos=true;
306
			}
307
			else{
308
				oos=false;
309
			}
310
			Long item_id = entry1.getKey();
311
			Amazonlisted amazon_item=catalogClient.getAmazonItemDetails(item_id);
312
			if(amazon_item.getItemid()==0){
313
				continue;
314
			}
315
			System.out.println(orderDate +","+entry1.getKey()+","+entry1.getValue()+","+ inventory +","+ oos+","+itemIdSalePriceMap.get(item_id)+","+itemIdminFBAPriceMap.get(item_id)+","+itemIdminMFNPriceMap.get(item_id));
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);
324
			if(itemIdSalePriceMap.containsKey(item_id) && itemIdSalePriceMap.get(item_id).getPrice()!=0){
325
				amazonfbasalessnapshot.setSalePrice(itemIdSalePriceMap.get(item_id).getPrice());
326
				amazonfbasalessnapshot.setSalePriceSnapshotDate(itemIdSalePriceMap.get(item_id).getDate().getTime());
327
			}
328
			else{
329
				amazonfbasalessnapshot.setSalePrice(0.0);
330
				amazonfbasalessnapshot.setSalePriceSnapshotDate(0);
331
			}
332
			if(itemIdminMFNPriceMap.containsKey(item_id) && itemIdminMFNPriceMap.get(item_id).getPrice()!=0){
333
				amazonfbasalessnapshot.setMinMfnPrice(itemIdminMFNPriceMap.get(item_id).getPrice());
334
				amazonfbasalessnapshot.setMinMfnPriceSnapshotDate(itemIdminMFNPriceMap.get(item_id).getDate().getTime());
335
			}
336
			else{
337
				amazonfbasalessnapshot.setMinMfnPrice(0.0);
338
				amazonfbasalessnapshot.setMinMfnPriceSnapshotDate(0);
339
			}
340
			if(itemIdminFBAPriceMap.containsKey(item_id) && itemIdminFBAPriceMap.get(item_id).getPrice()!=0){
341
				amazonfbasalessnapshot.setMinFbaPrice(itemIdminFBAPriceMap.get(item_id).getPrice());
342
				amazonfbasalessnapshot.setMinFbaPriceSnapshotDate(itemIdminFBAPriceMap.get(item_id).getDate().getTime());
343
			}
344
			else{
345
				amazonfbasalessnapshot.setMinFbaPrice(0.0);
346
				amazonfbasalessnapshot.setMinFbaPriceSnapshotDate(0);
347
			}
348
			if(itemIdOurPriceMap.containsKey(item_id) && itemIdOurPriceMap.get(item_id).getPrice()!=0){
349
				amazonfbasalessnapshot.setOurPrice(itemIdOurPriceMap.get(item_id).getPrice());
350
				amazonfbasalessnapshot.setOurPriceSnapshotDate(itemIdOurPriceMap.get(item_id).getDate().getTime());
351
			}
352
			else{
353
				amazonfbasalessnapshot.setOurPrice(amazon_item.getFbaPrice());
354
				amazonfbasalessnapshot.setOurPriceSnapshotDate(0);
355
			}
356
			amazonfbasalessnapshot.setAmazonFbaInventory(inventory);
357
			transactionClient.addOrUpdateAmazonFbaSalesSnapshot(amazonfbasalessnapshot);
358
		}
8363 vikram.rag 359
	}
9756 vikram.rag 360
} catch (IOException e) {
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();
369
}
370
catch (Exception e) {
371
	// TODO Auto-generated catch block
372
	e.printStackTrace();
373
}
8363 vikram.rag 374
 
9756 vikram.rag 375
}
8363 vikram.rag 376
 
9756 vikram.rag 377
public static Double getPercentageDifferenceFromMinimumPrice(AmazonFbaSalesSnapshot snapshot){
378
	Double minPrice = getMinimumSalePriceOnAmazonFBA(snapshot);
379
	return (((minPrice - snapshot.getOurPrice())/snapshot.getOurPrice())*100);
8363 vikram.rag 380
 
9756 vikram.rag 381
}
382
 
383
public static Double getMinimumSalePriceOnAmazonFBA(AmazonFbaSalesSnapshot snapshot){
384
	if(snapshot.getMinFbaPrice() > snapshot.getMinMfnPrice()){
385
		return snapshot.getMinMfnPrice();
8363 vikram.rag 386
	}
9756 vikram.rag 387
	else{
388
		return snapshot.getMinFbaPrice();
389
	}
390
}
8363 vikram.rag 391
 
392
}