Subversion Repositories SmartDukaan

Rev

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