Subversion Repositories SmartDukaan

Rev

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