Subversion Repositories SmartDukaan

Rev

Rev 8468 | Rev 8534 | 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);
94
							fbaSalesSnapshot.setTotalSale(fbaSalesSnapshot.getTotalSale() + itemSale);
95
							orderDateItemIdFbaSaleSnapshotMap.get(date_key).put(itemid,fbaSalesSnapshot);
96
						}
97
						else{
98
							FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
99
							fbaSalesSnapshot.setTotalOrderCount(qty);
100
							fbaSalesSnapshot.setTotalSale(itemSale);
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();
8532 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){
186
					//System.out.println(nextLine[1] +" "+ nextLine[31] +" " + nextLine[32] + " " + nextLine[34]);
8461 vikram.rag 187
					SimpleDateFormat istFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
188
					istFormatter .setLenient(false);
189
					TimeZone zone= TimeZone.getTimeZone("GMT");
190
					istFormatter.setTimeZone(zone);
191
					Date date = istFormatter.parse(nextLine[0]);
8363 vikram.rag 192
					Long item_id = Long.parseLong(nextLine[1].replaceAll("FBA",""));
8451 vikram.rag 193
					Double ourPrice = null;
194
					Double minFBAPrice= null;
195
					Double minMFNPrice= null;
196
					Double salePrice= null; 
197
					if(nextLine[30].length() >0){
198
						ourPrice = Double.parseDouble(nextLine[30]);
8532 vikram.rag 199
						if(itemIdOurPriceMap.containsKey(item_id)){
200
							if(itemIdOurPriceMap.get(item_id).getDate().getTime() < date.getTime()){
201
								PriceAtDate priceAtDate= new PriceAtDate();
202
								priceAtDate.setDate(date);
203
								priceAtDate.setPrice(ourPrice);
204
								itemIdOurPriceMap.put(item_id,priceAtDate);
205
							}
8461 vikram.rag 206
						}
207
						else{	
208
							PriceAtDate priceAtDate= new PriceAtDate();
209
							priceAtDate.setDate(date);
210
							priceAtDate.setPrice(ourPrice);
211
							itemIdOurPriceMap.put(item_id,priceAtDate);
212
						}
8451 vikram.rag 213
					}
214
					if(nextLine[31].length() >0){
215
						salePrice = Double.parseDouble(nextLine[31]);
8532 vikram.rag 216
						if(itemIdSalePriceMap.containsKey(item_id) ){
217
							if(itemIdSalePriceMap.get(item_id).getDate().getTime() < date.getTime()){
218
								PriceAtDate priceAtDate= new PriceAtDate();
219
								priceAtDate.setDate(date);
220
								priceAtDate.setPrice(salePrice);
221
								itemIdSalePriceMap.put(item_id,priceAtDate);
222
							}
8461 vikram.rag 223
						}
224
						else{	
225
							PriceAtDate priceAtDate= new PriceAtDate();
226
							priceAtDate.setDate(date);
8463 vikram.rag 227
							priceAtDate.setPrice(salePrice);
8461 vikram.rag 228
							itemIdSalePriceMap.put(item_id,priceAtDate);
229
						}
8451 vikram.rag 230
					}
231
					if(nextLine[32].length() >0){
232
						minFBAPrice = Double.parseDouble(nextLine[32]);
8532 vikram.rag 233
						if(itemIdminFBAPriceMap.containsKey(item_id)){
234
							if(itemIdminFBAPriceMap.get(item_id).getDate().getTime() < date.getTime()){
235
								PriceAtDate priceAtDate= new PriceAtDate();
236
								priceAtDate.setDate(date);
237
								priceAtDate.setPrice(minFBAPrice);
238
								itemIdminFBAPriceMap.put(item_id,priceAtDate);
239
							}
8461 vikram.rag 240
						}
241
						else{	
242
							PriceAtDate priceAtDate= new PriceAtDate();
243
							priceAtDate.setDate(date);
8463 vikram.rag 244
							priceAtDate.setPrice(minFBAPrice);
8461 vikram.rag 245
							itemIdminFBAPriceMap.put(item_id,priceAtDate);
246
						}
8451 vikram.rag 247
					}
8460 vikram.rag 248
					if(nextLine[34].length() >0){
8451 vikram.rag 249
						minMFNPrice = Double.parseDouble(nextLine[34]);
8532 vikram.rag 250
						if(itemIdminMFNPriceMap.containsKey(item_id)){
251
							if(itemIdminMFNPriceMap.get(item_id).getDate().getTime() < date.getTime()){
252
								PriceAtDate priceAtDate= new PriceAtDate();
253
								priceAtDate.setDate(date);
254
								priceAtDate.setPrice(minMFNPrice);
255
								itemIdminMFNPriceMap.put(item_id,priceAtDate);
256
							}
8461 vikram.rag 257
						}
258
						else{	
259
							PriceAtDate priceAtDate= new PriceAtDate();
260
							priceAtDate.setDate(date);
8463 vikram.rag 261
							priceAtDate.setPrice(minMFNPrice);
8461 vikram.rag 262
							itemIdminMFNPriceMap.put(item_id,priceAtDate);
263
						}
8451 vikram.rag 264
					}
8363 vikram.rag 265
				}
266
				count++;
267
			}
268
			boolean oos;
269
			for (Entry<Date, Map<Long, FbaSalesSnapshot>> entry : orderDateItemIdFbaSaleSnapshotMap.entrySet()){
270
				Date orderDate = entry.getKey();
271
				for(Entry<Long, FbaSalesSnapshot> entry1 :entry.getValue().entrySet()){
272
					System.out.println("Item ID is " + entry1.getKey());
273
					Long inventory = inventoryClient.getAmazonFbaItemInventory(entry1.getKey());
274
 
275
					if(inventory==0 && entry1.getValue().getTotalOrderCount()==0){
276
						oos=true;
277
					}
278
					else{
279
						oos=false;
280
					}
281
					Long item_id = entry1.getKey();
8438 vikram.rag 282
					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 283
					AmazonFbaSalesSnapshot amazonfbasalessnapshot = new AmazonFbaSalesSnapshot();
284
					amazonfbasalessnapshot.setDateOfSale(orderDate.getTime());
285
					amazonfbasalessnapshot.setItem_id(entry1.getKey());
286
					amazonfbasalessnapshot.setTotalOrderCount(entry1.getValue().getPromotionOrderCount());
287
					amazonfbasalessnapshot.setTotalOrderCount(entry1.getValue().getTotalOrderCount());
288
					amazonfbasalessnapshot.setTotalSale(entry1.getValue().getTotalSale());
289
					amazonfbasalessnapshot.setPromotionSale(entry1.getValue().getTotalPromotionSale());
290
					amazonfbasalessnapshot.setIsOutOfStock(oos);
291
					Amazonlisted amazon_item=null;
8532 vikram.rag 292
					if(catalogClient.getAmazonItemDetails(item_id).getItemid()!=0){
8363 vikram.rag 293
						amazon_item = catalogClient.getAmazonItemDetails(item_id);
8460 vikram.rag 294
					}
295
					else{ 
8363 vikram.rag 296
						continue;
8460 vikram.rag 297
					}
8461 vikram.rag 298
					if(itemIdSalePriceMap.containsKey(item_id) && itemIdSalePriceMap.get(item_id).getPrice()!=0){
299
						amazonfbasalessnapshot.setSalePrice(itemIdSalePriceMap.get(item_id).getPrice());
8532 vikram.rag 300
						amazonfbasalessnapshot.setSalePriceSnapshotDate(itemIdSalePriceMap.get(item_id).getDate().getTime());
8363 vikram.rag 301
					}
302
					else{
8460 vikram.rag 303
						amazonfbasalessnapshot.setSalePrice(0.0);
8532 vikram.rag 304
						amazonfbasalessnapshot.setSalePriceSnapshotDate(0);
8363 vikram.rag 305
					}
8461 vikram.rag 306
					if(itemIdminMFNPriceMap.containsKey(item_id) && itemIdminMFNPriceMap.get(item_id).getPrice()!=0){
307
						amazonfbasalessnapshot.setMinMfnPrice(itemIdminMFNPriceMap.get(item_id).getPrice());
8532 vikram.rag 308
						amazonfbasalessnapshot.setMinMfnPriceSnapshotDate(itemIdminMFNPriceMap.get(item_id).getDate().getTime());
8363 vikram.rag 309
					}
310
					else{
8460 vikram.rag 311
						amazonfbasalessnapshot.setMinMfnPrice(0.0);
8532 vikram.rag 312
						amazonfbasalessnapshot.setMinMfnPriceSnapshotDate(0);
8363 vikram.rag 313
					}
8461 vikram.rag 314
					if(itemIdminFBAPriceMap.containsKey(item_id) && itemIdminFBAPriceMap.get(item_id).getPrice()!=0){
315
						amazonfbasalessnapshot.setMinFbaPrice(itemIdminFBAPriceMap.get(item_id).getPrice());
8532 vikram.rag 316
						amazonfbasalessnapshot.setMinFbaPriceSnapshotDate(itemIdminFBAPriceMap.get(item_id).getDate().getTime());
8363 vikram.rag 317
					}
318
					else{
8460 vikram.rag 319
						amazonfbasalessnapshot.setMinFbaPrice(0.0);
8532 vikram.rag 320
						amazonfbasalessnapshot.setMinFbaPriceSnapshotDate(0);
8363 vikram.rag 321
					}
8461 vikram.rag 322
					if(itemIdOurPriceMap.containsKey(item_id) && itemIdOurPriceMap.get(item_id).getPrice()!=0){
323
						amazonfbasalessnapshot.setOurPrice(itemIdOurPriceMap.get(item_id).getPrice());
8532 vikram.rag 324
						amazonfbasalessnapshot.setOurPriceSnapshotDate(itemIdOurPriceMap.get(item_id).getDate().getTime());
8447 vikram.rag 325
					}
326
					else{
8460 vikram.rag 327
						amazonfbasalessnapshot.setOurPrice(amazon_item.getFbaPrice());
8532 vikram.rag 328
						amazonfbasalessnapshot.setOurPriceSnapshotDate(0);
8447 vikram.rag 329
					}
8363 vikram.rag 330
					amazonfbasalessnapshot.setAmazonFbaInventory(inventory);
331
					transactionClient.addOrUpdateAmazonFbaSalesSnapshot(amazonfbasalessnapshot);
332
				}
333
			}
334
		} catch (IOException e) {
335
			// TODO Auto-generated catch block
336
			e.printStackTrace();
337
		} catch (ParseException e) {
338
			// TODO Auto-generated catch block
339
			e.printStackTrace();
340
		} catch (TException e) {
341
			// TODO Auto-generated catch block
342
			e.printStackTrace();
343
		}
344
	}
345
 
346
	public static Double getPercentageDifferenceFromMinimumPrice(AmazonFbaSalesSnapshot snapshot){
347
		Double minPrice = getMinimumSalePriceOnAmazonFBA(snapshot);
8468 vikram.rag 348
		return (((minPrice - snapshot.getOurPrice())/snapshot.getOurPrice())*100);
8363 vikram.rag 349
 
350
	}
351
 
352
	public static Double getMinimumSalePriceOnAmazonFBA(AmazonFbaSalesSnapshot snapshot){
353
		if(snapshot.getMinFbaPrice() > snapshot.getMinMfnPrice()){
354
			return snapshot.getMinMfnPrice();
355
		}
356
		else{
357
			return snapshot.getMinFbaPrice();
358
		}
359
	}
360
 
361
}