Subversion Repositories SmartDukaan

Rev

Rev 8288 | Rev 8363 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8285 kshitij.so 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
import in.shop2020.utils.GmailUtils;
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
 
38
public class CreateSendFBAStockEstimation {
8288 kshitij.so 39
	final public static String AMAZON_FBA_SHEET = "/home/FBA-Stock-File.xls";
8293 kshitij.so 40
 
8288 kshitij.so 41
	public static void main(String... args){
8285 kshitij.so 42
		TransactionClient transactionServiceClient = null;
43
		try {
44
			transactionServiceClient = new TransactionClient();
45
		} catch (Exception e) {
46
			// TODO Auto-generated catch block
47
			e.printStackTrace();
48
		}	
8293 kshitij.so 49
 
8285 kshitij.so 50
		in.shop2020.model.v1.order.TransactionService.Client transactionClient   = transactionServiceClient.getClient();
51
		List<AmazonFbaSalesSnapshot> salessnapshots = null;
52
		try {
53
			salessnapshots = transactionClient.getAmazonFbaSalesSnapshotForDays(5);
54
		} catch (TException e) {
55
			// TODO Auto-generated catch block
56
			e.printStackTrace();
57
		}
58
		Map<Long,List<Integer>> itemId5daysSale = new HashMap<Long,List<Integer>>();
59
		AmazonFbaSalesSnapshot snapshot;
60
		for(AmazonFbaSalesSnapshot salessnapshot:salessnapshots){
61
			try {
62
				if(itemId5daysSale.containsKey(salessnapshot.getItem_id())){
63
					if(salessnapshot.getAmazonFbaInventory()!=0)
64
						itemId5daysSale.get(salessnapshot.getItem_id()).add(salessnapshot.getOrderCount());
65
					else
66
						itemId5daysSale.get(salessnapshot.getItem_id()).add(-1);
67
				}
68
				else{
69
					List<Integer> last5daysSale = new ArrayList<Integer>();
70
					if(salessnapshot.getAmazonFbaInventory()!=0){
71
						last5daysSale.add(salessnapshot.getOrderCount());
72
					}
73
					else{ 
74
						last5daysSale.add(-1);
75
					}
76
					itemId5daysSale.put(salessnapshot.getItem_id(),last5daysSale);
77
				}
8293 kshitij.so 78
			} catch (Exception e) {
8285 kshitij.so 79
				// TODO Auto-generated catch block
80
				e.printStackTrace();
81
			}
8293 kshitij.so 82
		}	
83
 
84
		//System.out.println("ItemID , Last Five Days Sale , Product Name , TP , NLC , STICKY , Preferred Vendor , Qty , Min AFN , Min MFN , SP ,Total Amount");
85
		CatalogClient catalogServiceClient = null;
86
		InventoryClient inventoryServiceClient = null;
87
		try {
88
			inventoryServiceClient = new InventoryClient();
89
			//catalogServiceClient = new CatalogClient();
90
			catalogServiceClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");
91
		} catch (Exception e) {
92
			// TODO Auto-generated catch block
93
			e.printStackTrace();
94
		}	
95
		in.shop2020.model.v1.catalog.CatalogService.Client catalogClient   = catalogServiceClient.getClient();
96
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
97
		HSSFWorkbook hwb=new HSSFWorkbook();
98
		HSSFSheet sheet =  hwb.createSheet("FBA-STOCK-SHEET");
99
		HSSFRow rowhead=   sheet.createRow((short)0);
100
		rowhead.createCell((short) 0).setCellValue("ITEM-ID");
101
		rowhead.createCell((short) 1).setCellValue("LAST 5 DAYS SALE");
102
		rowhead.createCell((short) 2).setCellValue("PRODUCT-NAME");
103
		rowhead.createCell((short) 3).setCellValue("TRANSFER-PRICE");
104
		rowhead.createCell((short) 4).setCellValue("NLC");
105
		rowhead.createCell((short) 5).setCellValue("IS-STICKY");
106
		rowhead.createCell((short) 6).setCellValue("PREFERRED-VENDOR");
107
		rowhead.createCell((short) 7).setCellValue("QUANTITY");
108
		rowhead.createCell((short) 8).setCellValue("MIN-AFN-PRICE");
109
		rowhead.createCell((short) 9).setCellValue("MIN-MFN-PRICE");
110
		rowhead.createCell((short) 10).setCellValue("OUR-PRICE");
111
		rowhead.createCell((short) 11).setCellValue("TOTAL-AMOUNT");
112
		rowhead.createCell((short) 12).setCellValue("DAYS-OF-STOCK");
113
		int iterator=1;
114
		for(Map.Entry<Long,List<Integer>> entry : itemId5daysSale.entrySet()){
115
			int count =1;
116
			int avg_sale=0;
117
			int sale_days=0;
118
			StringBuilder days_sale = new StringBuilder();
119
			for(Integer sale:entry.getValue()){
120
				if(sale!=-1){
121
					days_sale.append(sale.toString());
122
					avg_sale=avg_sale+sale;
123
					sale_days++;
8285 kshitij.so 124
				}
8293 kshitij.so 125
				else{
126
					days_sale.append("X");
8285 kshitij.so 127
				}
8293 kshitij.so 128
				if(count != entry.getValue().size()){
129
					days_sale.append("-");
8285 kshitij.so 130
				}
8293 kshitij.so 131
				count++;
8285 kshitij.so 132
			}
8293 kshitij.so 133
			if(sale_days!=0){
134
				avg_sale=avg_sale/sale_days;
135
			}
136
			Item item = null;
137
			VendorItemPricing vip=null;
138
			Vendor vendor = null;
8285 kshitij.so 139
			try {
8293 kshitij.so 140
				//System.out.println("Item ID is " + entry.getKey());
141
				item = catalogClient.getItem(entry.getKey());
142
				vip = inventoryClient.getItemPricing(item.getId(),item.getPreferredVendor());
143
				vendor = inventoryClient.getVendor(item.getPreferredVendor());
144
			} catch (CatalogServiceException e) {
8285 kshitij.so 145
				// TODO Auto-generated catch block
146
				e.printStackTrace();
8293 kshitij.so 147
			} catch (TException e) {
8285 kshitij.so 148
				// TODO Auto-generated catch block
149
				e.printStackTrace();
8293 kshitij.so 150
			} catch (InventoryServiceException e) {
151
				// TODO Auto-generated catch block
152
				e.printStackTrace();
8285 kshitij.so 153
			}
8293 kshitij.so 154
			AmazonFbaSalesSnapshot latest_snapshot = null;
8285 kshitij.so 155
			try {
8293 kshitij.so 156
				latest_snapshot = transactionClient.getAmazonFbaSalesLatestSnapshotForItem(entry.getKey());
157
			} catch (TException e) {
8285 kshitij.so 158
				// TODO Auto-generated catch block
159
				e.printStackTrace();
160
			}
8293 kshitij.so 161
			int days_of_stock=0;
162
			if(latest_snapshot.getMinFbaPrice()==0 ||  latest_snapshot.getMinMfnPrice()==0){
163
				days_of_stock=0;
164
			}
165
			else{
166
				double percent_diff = getPercentageDifferenceFromMinimumPrice(latest_snapshot);
167
				if(percent_diff >= 3){
168
					days_of_stock = 15;
169
				}
170
				else if(percent_diff > 0 && percent_diff < 3){
171
					days_of_stock = 10;
172
				}
173
				else if(percent_diff > 0 && percent_diff < 1){
174
					days_of_stock = 4;
175
				}
176
				else if(latest_snapshot.getSalePrice() < latest_snapshot.getMinFbaPrice() &&  latest_snapshot.getSalePrice() > latest_snapshot.getMinMfnPrice() && percent_diff <= 2){
177
					days_of_stock = 2;
178
				}
179
				else{
180
					days_of_stock = 0;
181
				}
182
			}
8285 kshitij.so 183
 
8293 kshitij.so 184
			HSSFRow row=   sheet.createRow((short)iterator);
185
			row.createCell((short) 0).setCellValue(entry.getKey());
186
			row.createCell((short) 1).setCellValue(days_sale.toString());
187
			row.createCell((short) 2).setCellValue(item.getBrand() + " " + item.getModelName()+" " + item.getModelNumber() + " " +
188
					item.getColor());
189
			row.createCell((short) 3).setCellValue(vip.getTransferPrice());
190
			row.createCell((short) 4).setCellValue(vip.getNlc());
191
			if(item.isIsWarehousePreferenceSticky())
192
				row.createCell((short) 5).setCellValue("YES");
193
			else
194
				row.createCell((short) 5).setCellValue("NO");
195
			row.createCell((short) 6).setCellValue(vendor.getName());
196
			row.createCell((short) 7).setCellValue(avg_sale*days_of_stock);
197
			row.createCell((short) 8).setCellValue(latest_snapshot.getMinFbaPrice());
198
			row.createCell((short) 9).setCellValue(latest_snapshot.getMinMfnPrice());
199
			row.createCell((short) 10).setCellValue(latest_snapshot.getSalePrice());
200
			double amount = avg_sale*days_of_stock*latest_snapshot.getSalePrice();
201
			row.createCell((short) 11).setCellValue(amount);
202
			row.createCell((short) 12).setCellValue(days_of_stock);
203
			iterator++;
204
 
8285 kshitij.so 205
		}
8293 kshitij.so 206
 
207
		FileOutputStream fileOut = null;
208
		try {
209
			fileOut = new FileOutputStream(AMAZON_FBA_SHEET);
210
		} catch (FileNotFoundException e) {
211
			// TODO Auto-generated catch block
212
			e.printStackTrace();
213
		}
214
		try {
215
			hwb.write(fileOut);
216
		} catch (IOException e) {
217
			// TODO Auto-generated catch block
218
			e.printStackTrace();
219
		}
220
		try {
221
			fileOut.close();
222
		} catch (IOException e) {
223
			// TODO Auto-generated catch block
224
			e.printStackTrace();
225
		}
226
 
8285 kshitij.so 227
		String emailFromAddress = "build@shop2020.in";
228
		String password = "cafe@nes";
8293 kshitij.so 229
		String[] sendTo = new String[]{ "vikram.raghav@shop2020.in", "rajveer.singh@shop2020.in" };
230
		String emailSubjectTxt = "FBA Stock Estimation Sheet";
231
 
8285 kshitij.so 232
		try {
8293 kshitij.so 233
			GmailUtils mailer = new GmailUtils();
8288 kshitij.so 234
			mailer.sendSSLMessage(sendTo, emailSubjectTxt, "", emailFromAddress, password, "/home/FBA-Stock-File.xls");
8285 kshitij.so 235
		}
236
		catch (Exception ex) {
237
			ex.printStackTrace();
238
		}
239
 
240
	}
8293 kshitij.so 241
 
8285 kshitij.so 242
	public static Double getPercentageDifferenceFromMinimumPrice(AmazonFbaSalesSnapshot snapshot){
243
		Double minPrice = getMinimumSalePriceOnAmazonFBA(snapshot);
8293 kshitij.so 244
		return (((minPrice - snapshot.getSalePrice())/snapshot.getSalePrice())*100);
245
 
8285 kshitij.so 246
	}
8293 kshitij.so 247
 
8285 kshitij.so 248
	public static Double getMinimumSalePriceOnAmazonFBA(AmazonFbaSalesSnapshot snapshot){
249
		if(snapshot.getMinFbaPrice() > snapshot.getMinMfnPrice()){
250
			return snapshot.getMinMfnPrice();
251
		}
252
		else{
253
			return snapshot.getMinFbaPrice();
254
		}
255
	}
256
 
257
}