Subversion Repositories SmartDukaan

Rev

Rev 8288 | Rev 8363 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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