Subversion Repositories SmartDukaan

Rev

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