Subversion Repositories SmartDukaan

Rev

Rev 8438 | Rev 8449 | 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",""));
8438 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
			}
8447 vikram.rag 173
			Map<Long,Double> itemIdOurPriceMap = new HashMap<Long,Double>();
8363 vikram.rag 174
			Map<Long,Double> itemIdSalePriceMap = new HashMap<Long,Double>();
175
			Map<Long,Double> itemIdminFBAPriceMap = new HashMap<Long,Double>();
176
			Map<Long,Double> itemIdminMFNPriceMap = new HashMap<Long,Double>();
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]);
181
					Long item_id = Long.parseLong(nextLine[1].replaceAll("FBA",""));
8447 vikram.rag 182
					Double ourPrice = Double.parseDouble(nextLine[30]);
8363 vikram.rag 183
					Double salePrice = Double.parseDouble(nextLine[31]);
184
					Double minFBAPrice = Double.parseDouble(nextLine[32]);
185
					Double minMFNPrice = Double.parseDouble(nextLine[34]);
8447 vikram.rag 186
					itemIdOurPriceMap.put(item_id,ourPrice); 
8363 vikram.rag 187
					itemIdSalePriceMap.put(item_id,salePrice);
188
					itemIdminFBAPriceMap.put(item_id,minFBAPrice);
189
					itemIdminMFNPriceMap.put(item_id,minMFNPrice);
190
				}
191
				count++;
192
			}
193
			boolean oos;
194
			for (Entry<Date, Map<Long, FbaSalesSnapshot>> entry : orderDateItemIdFbaSaleSnapshotMap.entrySet()){
195
				Date orderDate = entry.getKey();
196
				for(Entry<Long, FbaSalesSnapshot> entry1 :entry.getValue().entrySet()){
197
					System.out.println("Item ID is " + entry1.getKey());
198
					Long inventory = inventoryClient.getAmazonFbaItemInventory(entry1.getKey());
199
 
200
					if(inventory==0 && entry1.getValue().getTotalOrderCount()==0){
201
						oos=true;
202
					}
203
					else{
204
						oos=false;
205
					}
206
					Long item_id = entry1.getKey();
8438 vikram.rag 207
					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 208
					AmazonFbaSalesSnapshot amazonfbasalessnapshot = new AmazonFbaSalesSnapshot();
209
					amazonfbasalessnapshot.setDateOfSale(orderDate.getTime());
210
					amazonfbasalessnapshot.setItem_id(entry1.getKey());
211
					amazonfbasalessnapshot.setTotalOrderCount(entry1.getValue().getPromotionOrderCount());
212
					amazonfbasalessnapshot.setTotalOrderCount(entry1.getValue().getTotalOrderCount());
213
					amazonfbasalessnapshot.setTotalSale(entry1.getValue().getTotalSale());
214
					amazonfbasalessnapshot.setPromotionSale(entry1.getValue().getTotalPromotionSale());
215
					amazonfbasalessnapshot.setIsOutOfStock(oos);
216
					Amazonlisted amazon_item=null;
217
					if(catalogClient.getAmazonItemDetails(item_id)!=null)
218
						amazon_item = catalogClient.getAmazonItemDetails(item_id);
219
					else 
220
						continue;
221
					if(itemIdSalePriceMap.containsKey(item_id) && itemIdSalePriceMap.get(item_id)!=0){
222
						amazonfbasalessnapshot.setSalePrice(itemIdSalePriceMap.get(item_id));
223
					}
224
					else{
225
						amazonfbasalessnapshot.setSalePrice(amazon_item.getFbaPrice());
226
					}
227
					if(itemIdminMFNPriceMap.containsKey(item_id) && itemIdminMFNPriceMap.get(item_id)!=0){
228
						amazonfbasalessnapshot.setMinFbaPrice(itemIdminMFNPriceMap.get(item_id));
229
					}
230
					else{
231
						amazonfbasalessnapshot.setMinFbaPrice(0.0);
232
					}
233
					if(itemIdminMFNPriceMap.containsKey(item_id) && itemIdminMFNPriceMap.get(item_id)!=0){
234
						amazonfbasalessnapshot.setMinMfnPrice(itemIdminMFNPriceMap.get(item_id));
235
					}
236
					else{
237
						amazonfbasalessnapshot.setMinMfnPrice(0.0);
238
					}
8447 vikram.rag 239
					if(itemIdOurPriceMap.containsKey(item_id) && itemIdOurPriceMap.get(item_id)!=0){
240
						amazonfbasalessnapshot.setOurPrice(itemIdOurPriceMap.get(item_id));
241
					}
242
					else{
243
						amazonfbasalessnapshot.setOurPrice(itemIdOurPriceMap.get(item_id));
244
					}
8363 vikram.rag 245
					amazonfbasalessnapshot.setAmazonFbaInventory(inventory);
246
					transactionClient.addOrUpdateAmazonFbaSalesSnapshot(amazonfbasalessnapshot);
247
				}
248
			}
249
		} catch (IOException e) {
250
			// TODO Auto-generated catch block
251
			e.printStackTrace();
252
		} catch (ParseException e) {
253
			// TODO Auto-generated catch block
254
			e.printStackTrace();
255
		} catch (TException e) {
256
			// TODO Auto-generated catch block
257
			e.printStackTrace();
258
		}
259
	}
260
 
261
 
262
	public static Double getPercentageDifferenceFromMinimumPrice(AmazonFbaSalesSnapshot snapshot){
263
		Double minPrice = getMinimumSalePriceOnAmazonFBA(snapshot);
264
		return (snapshot.getSalePrice() - minPrice)/snapshot.getSalePrice();
265
 
266
	}
267
 
268
	public static Double getMinimumSalePriceOnAmazonFBA(AmazonFbaSalesSnapshot snapshot){
269
		if(snapshot.getMinFbaPrice() > snapshot.getMinMfnPrice()){
270
			return snapshot.getMinMfnPrice();
271
		}
272
		else{
273
			return snapshot.getMinFbaPrice();
274
		}
275
	}
276
 
277
}