Subversion Repositories SmartDukaan

Rev

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