Subversion Repositories SmartDukaan

Rev

Rev 8423 | Go to most recent revision | Details | 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 {
43
			orderreportreader = new CSVReader(new FileReader("/home/vikram/Desktop/amazonorderreport.csv"),'\t');
44
			inventoryhealthreportreader = new CSVReader(new FileReader("/home/vikram/Desktop/inventoryhealthreport.csv"),'\t');
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",""));
64
					Integer qty = new Integer(nextLine[14]);
65
					Float itemSale=null; 
66
					if(nextLine[16].length()!=0){
67
						itemSale = new Float(nextLine[16]);
68
						continue;
69
					}
70
					System.out.println("Item Sale  " + itemSale );
71
					Float itemDiscount; 
72
					if(nextLine[22].length()!=0){
73
						itemDiscount = new Float(nextLine[22]);
74
					}
75
					else{
76
						itemDiscount = new Float(0);
77
					}
78
					if(orderDateItemIdFbaSaleSnapshotMap.containsKey(date_key)){
79
						if(orderDateItemIdFbaSaleSnapshotMap.get(date_key).containsKey(itemid)){
80
							FbaSalesSnapshot fbaSalesSnapshot = orderDateItemIdFbaSaleSnapshotMap.get(date_key).get(itemid);
81
							if(itemDiscount!=0){
82
								fbaSalesSnapshot.setPromotionOrderCount(qty);
83
								fbaSalesSnapshot.setTotalPromotionSale(fbaSalesSnapshot.totalPromotionSale + (itemSale - itemDiscount));
84
							}
85
							else{
86
								fbaSalesSnapshot.setPromotionOrderCount(0);
87
								fbaSalesSnapshot.setTotalPromotionSale((float) 0);
88
							}
89
							fbaSalesSnapshot.setTotalOrderCount(fbaSalesSnapshot.getTotalOrderCount() + qty);
90
							fbaSalesSnapshot.setTotalSale(fbaSalesSnapshot.getTotalSale() + itemSale);
91
							orderDateItemIdFbaSaleSnapshotMap.get(date_key).put(itemid,fbaSalesSnapshot);
92
						}
93
						else{
94
							FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
95
							fbaSalesSnapshot.setTotalOrderCount(qty);
96
							fbaSalesSnapshot.setTotalSale(itemSale);
97
							if(itemDiscount!=0){
98
								fbaSalesSnapshot.setPromotionOrderCount(qty);
99
								fbaSalesSnapshot.setTotalPromotionSale(itemSale - itemDiscount);
100
							}
101
							else{
102
								fbaSalesSnapshot.setPromotionOrderCount(0);
103
								fbaSalesSnapshot.setTotalPromotionSale((float) 0);
104
							}
105
							orderDateItemIdFbaSaleSnapshotMap.get(date_key).put(itemid,fbaSalesSnapshot);
106
						}
107
					}
108
					else{
109
						Map<Long,FbaSalesSnapshot> ItemIdFbaSaleSnapshotMap = new HashMap<Long,FbaSalesSnapshot>();
110
						FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
111
						fbaSalesSnapshot.setTotalOrderCount(qty);
112
						fbaSalesSnapshot.setTotalSale(itemSale);
113
						if(itemDiscount!=0){
114
							fbaSalesSnapshot.setTotalPromotionSale(itemSale - itemDiscount);
115
							fbaSalesSnapshot.setPromotionOrderCount(qty);
116
						}
117
						else{
118
							fbaSalesSnapshot.setTotalPromotionSale((float) 0);
119
							fbaSalesSnapshot.setPromotionOrderCount(0);
120
						}
121
						ItemIdFbaSaleSnapshotMap.put(itemid,fbaSalesSnapshot);
122
						orderDateItemIdFbaSaleSnapshotMap.put(date_key,ItemIdFbaSaleSnapshotMap);
123
					}
124
				} 
125
				count++;
126
			}
127
			InventoryClient inventoryServiceClient = null;
128
			TransactionClient transactionServiceClient = null;
129
			CatalogClient catalogServiceClient = null;
130
			try {
131
				inventoryServiceClient = new InventoryClient();
132
				transactionServiceClient = new TransactionClient();
133
				catalogServiceClient = new CatalogClient();
134
				//catalogServiceClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");
135
			} catch (Exception e) {
136
				// TODO Auto-generated catch block
137
				e.printStackTrace();
138
			}	
139
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
140
			in.shop2020.model.v1.order.TransactionService.Client transactionClient   = transactionServiceClient.getClient();
141
			in.shop2020.model.v1.catalog.CatalogService.Client catalogClient   = catalogServiceClient.getClient();
142
			SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
143
			Date date_today = dateFormat.parse(dateFormat.format(new Date()));
144
			List<AmazonFbaInventorySnapshot> nonzeroFbaInventorySnapshotlist =  inventoryClient.getAllAmazonFbaItemInventory();
145
			if(nonzeroFbaInventorySnapshotlist!=null){
146
				for(AmazonFbaInventorySnapshot amazonFbaInventory:nonzeroFbaInventorySnapshotlist){
147
					if(orderDateItemIdFbaSaleSnapshotMap.containsKey(date_today) ){ 
148
						if(!orderDateItemIdFbaSaleSnapshotMap.get(date_today).containsKey(amazonFbaInventory.getItem_id())){
149
							Map<Long,FbaSalesSnapshot> ItemIdFbaSaleSnapshotMap = new HashMap<Long,FbaSalesSnapshot>();
150
							FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
151
							fbaSalesSnapshot.setTotalOrderCount(0);
152
							fbaSalesSnapshot.setPromotionOrderCount(0);
153
							fbaSalesSnapshot.setTotalPromotionSale((float) 0);
154
							fbaSalesSnapshot.setTotalSale((float) 0);
155
							ItemIdFbaSaleSnapshotMap.put(amazonFbaInventory.getItem_id(),fbaSalesSnapshot);
156
							orderDateItemIdFbaSaleSnapshotMap.get(date_today).put(amazonFbaInventory.getItem_id(),fbaSalesSnapshot);
157
						}
158
 
159
					}
160
					else{
161
						Map<Long,FbaSalesSnapshot> ItemIdFbaSaleSnapshotMap = new HashMap<Long,FbaSalesSnapshot>();
162
						FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
163
						fbaSalesSnapshot.setTotalOrderCount(0);
164
						fbaSalesSnapshot.setPromotionOrderCount(0);
165
						fbaSalesSnapshot.setTotalPromotionSale((float) 0);
166
						fbaSalesSnapshot.setTotalSale((float) 0);
167
						ItemIdFbaSaleSnapshotMap.put(amazonFbaInventory.getItem_id(),fbaSalesSnapshot);
168
						orderDateItemIdFbaSaleSnapshotMap.put(date_today,ItemIdFbaSaleSnapshotMap);
169
					}
170
				}
171
			}
172
			else{
173
				System.out.println("No inventory in FBA");
174
			}
175
			Map<Long,Double> itemIdSalePriceMap = new HashMap<Long,Double>();
176
			Map<Long,Double> itemIdminFBAPriceMap = new HashMap<Long,Double>();
177
			Map<Long,Double> itemIdminMFNPriceMap = new HashMap<Long,Double>();
178
			count=1;
179
			while ((nextLine = inventoryhealthreportreader.readNext()) != null ) {
180
				if(count!=1){
181
					//System.out.println(nextLine[1] +" "+ nextLine[31] +" " + nextLine[32] + " " + nextLine[34]);
182
					Long item_id = Long.parseLong(nextLine[1].replaceAll("FBA",""));
183
					Double salePrice = Double.parseDouble(nextLine[31]);
184
					Double minFBAPrice = Double.parseDouble(nextLine[32]);
185
					Double minMFNPrice = Double.parseDouble(nextLine[34]);
186
					itemIdSalePriceMap.put(item_id,salePrice);
187
					itemIdminFBAPriceMap.put(item_id,minFBAPrice);
188
					itemIdminMFNPriceMap.put(item_id,minMFNPrice);
189
				}
190
				count++;
191
			}
192
			boolean oos;
193
			for (Entry<Date, Map<Long, FbaSalesSnapshot>> entry : orderDateItemIdFbaSaleSnapshotMap.entrySet()){
194
				Date orderDate = entry.getKey();
195
				for(Entry<Long, FbaSalesSnapshot> entry1 :entry.getValue().entrySet()){
196
					System.out.println("Item ID is " + entry1.getKey());
197
					Long inventory = inventoryClient.getAmazonFbaItemInventory(entry1.getKey());
198
 
199
					if(inventory==0 && entry1.getValue().getTotalOrderCount()==0){
200
						oos=true;
201
					}
202
					else{
203
						oos=false;
204
					}
205
					Long item_id = entry1.getKey();
206
					System.out.println(orderDate +","+entry1.getKey()+","+entry1.getValue()+","+ inventory +","+ oos+","+itemIdSalePriceMap.get(item_id)+","+itemIdminFBAPriceMap.get(item_id)+","+itemIdminMFNPriceMap.get(item_id));
207
					AmazonFbaSalesSnapshot amazonfbasalessnapshot = new AmazonFbaSalesSnapshot();
208
					amazonfbasalessnapshot.setDateOfSale(orderDate.getTime());
209
					amazonfbasalessnapshot.setItem_id(entry1.getKey());
210
					amazonfbasalessnapshot.setTotalOrderCount(entry1.getValue().getPromotionOrderCount());
211
					amazonfbasalessnapshot.setTotalOrderCount(entry1.getValue().getTotalOrderCount());
212
					amazonfbasalessnapshot.setTotalSale(entry1.getValue().getTotalSale());
213
					amazonfbasalessnapshot.setPromotionSale(entry1.getValue().getTotalPromotionSale());
214
					amazonfbasalessnapshot.setIsOutOfStock(oos);
215
					Amazonlisted amazon_item=null;
216
					if(catalogClient.getAmazonItemDetails(item_id)!=null)
217
						amazon_item = catalogClient.getAmazonItemDetails(item_id);
218
					else 
219
						continue;
220
					if(itemIdSalePriceMap.containsKey(item_id) && itemIdSalePriceMap.get(item_id)!=0){
221
						amazonfbasalessnapshot.setSalePrice(itemIdSalePriceMap.get(item_id));
222
					}
223
					else{
224
						amazonfbasalessnapshot.setSalePrice(amazon_item.getFbaPrice());
225
					}
226
					if(itemIdminMFNPriceMap.containsKey(item_id) && itemIdminMFNPriceMap.get(item_id)!=0){
227
						amazonfbasalessnapshot.setMinFbaPrice(itemIdminMFNPriceMap.get(item_id));
228
					}
229
					else{
230
						amazonfbasalessnapshot.setMinFbaPrice(0.0);
231
					}
232
					if(itemIdminMFNPriceMap.containsKey(item_id) && itemIdminMFNPriceMap.get(item_id)!=0){
233
						amazonfbasalessnapshot.setMinMfnPrice(itemIdminMFNPriceMap.get(item_id));
234
					}
235
					else{
236
						amazonfbasalessnapshot.setMinMfnPrice(0.0);
237
					}
238
					amazonfbasalessnapshot.setAmazonFbaInventory(inventory);
239
					System.out.println("Item ID is " + amazonfbasalessnapshot.getItem_id() + " Min Price is  " + amazonfbasalessnapshot.getMinFbaPrice());
240
					transactionClient.addOrUpdateAmazonFbaSalesSnapshot(amazonfbasalessnapshot);
241
				}
242
			}
243
		} catch (IOException e) {
244
			// TODO Auto-generated catch block
245
			e.printStackTrace();
246
		} catch (ParseException e) {
247
			// TODO Auto-generated catch block
248
			e.printStackTrace();
249
		} catch (TException e) {
250
			// TODO Auto-generated catch block
251
			e.printStackTrace();
252
		}
253
	}
254
 
255
 
256
	public static Double getPercentageDifferenceFromMinimumPrice(AmazonFbaSalesSnapshot snapshot){
257
		Double minPrice = getMinimumSalePriceOnAmazonFBA(snapshot);
258
		return (snapshot.getSalePrice() - minPrice)/snapshot.getSalePrice();
259
 
260
	}
261
 
262
	public static Double getMinimumSalePriceOnAmazonFBA(AmazonFbaSalesSnapshot snapshot){
263
		if(snapshot.getMinFbaPrice() > snapshot.getMinMfnPrice()){
264
			return snapshot.getMinMfnPrice();
265
		}
266
		else{
267
			return snapshot.getMinFbaPrice();
268
		}
269
	}
270
 
271
}