| 13110 |
kshitij.so |
1 |
package com.amazonaws.mws.samples;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.catalog.AmazonPromotion;
|
|
|
4 |
import in.shop2020.model.v1.inventory.AmazonFbaInventorySnapshot;
|
|
|
5 |
import in.shop2020.model.v1.order.AmazonFCWarehouseLocation;
|
|
|
6 |
import in.shop2020.model.v1.order.AmazonHourlySaleSnapshot;
|
|
|
7 |
import in.shop2020.thrift.clients.InventoryClient;
|
|
|
8 |
import in.shop2020.utils.GmailUtils;
|
|
|
9 |
|
|
|
10 |
import java.io.File;
|
|
|
11 |
import java.io.FileInputStream;
|
|
|
12 |
import java.io.FileNotFoundException;
|
|
|
13 |
import java.io.FileOutputStream;
|
|
|
14 |
import java.io.FileReader;
|
|
|
15 |
import java.io.IOException;
|
|
|
16 |
import java.io.OutputStream;
|
|
|
17 |
import java.text.ParseException;
|
|
|
18 |
import java.text.SimpleDateFormat;
|
|
|
19 |
import java.util.ArrayList;
|
|
|
20 |
import java.util.Arrays;
|
|
|
21 |
import java.util.Calendar;
|
|
|
22 |
import java.util.Date;
|
|
|
23 |
import java.util.GregorianCalendar;
|
|
|
24 |
import java.util.HashMap;
|
|
|
25 |
import java.util.List;
|
|
|
26 |
import java.util.Map;
|
|
|
27 |
import java.util.TimeZone;
|
|
|
28 |
import java.util.Map.Entry;
|
|
|
29 |
|
|
|
30 |
import javax.xml.bind.JAXBContext;
|
|
|
31 |
import javax.xml.bind.JAXBException;
|
|
|
32 |
import javax.xml.bind.Unmarshaller;
|
|
|
33 |
import javax.xml.datatype.DatatypeConfigurationException;
|
|
|
34 |
import javax.xml.datatype.DatatypeFactory;
|
|
|
35 |
import javax.xml.datatype.XMLGregorianCalendar;
|
|
|
36 |
|
|
|
37 |
import org.apache.commons.io.IOUtils;
|
|
|
38 |
import org.apache.thrift.TException;
|
|
|
39 |
import org.apache.thrift.transport.TTransportException;
|
|
|
40 |
|
|
|
41 |
import au.com.bytecode.opencsv.CSVReader;
|
|
|
42 |
|
|
|
43 |
import com.amazonaws.mws.MarketplaceWebService;
|
|
|
44 |
import com.amazonaws.mws.MarketplaceWebServiceClient;
|
|
|
45 |
import com.amazonaws.mws.MarketplaceWebServiceConfig;
|
|
|
46 |
import com.amazonaws.mws.MarketplaceWebServiceException;
|
|
|
47 |
import com.amazonaws.mws.model.AmazonEnvelope;
|
|
|
48 |
import com.amazonaws.mws.model.FulfillmentData;
|
|
|
49 |
import com.amazonaws.mws.model.GetReportListRequest;
|
|
|
50 |
import com.amazonaws.mws.model.GetReportRequest;
|
|
|
51 |
import com.amazonaws.mws.model.IdList;
|
|
|
52 |
import com.amazonaws.mws.model.Message;
|
|
|
53 |
import com.amazonaws.mws.model.Order;
|
|
|
54 |
import com.amazonaws.mws.model.RequestReportRequest;
|
|
|
55 |
|
|
|
56 |
import com.amazonaws.mws.samples.Consumer;
|
|
|
57 |
|
|
|
58 |
public class AmazonFbaSingleDaySnapshot{
|
|
|
59 |
|
|
|
60 |
public final static String accessKeyId = "AKIAII3SGRXBJDPCHSGQ";
|
|
|
61 |
public final static String secretAccessKey = "B92xTbNBTYygbGs98w01nFQUhbec1pNCkCsKVfpg";
|
|
|
62 |
|
|
|
63 |
public final static String appName = "Test";
|
|
|
64 |
public final static String appVersion = "1.0";
|
|
|
65 |
public final static String merchantId = "AF6E3O0VE0X4D";
|
|
|
66 |
|
| 13132 |
kshitij.so |
67 |
public final static String FILE_PATH = "/tmp/amazonorderreport-hourly.xml";
|
| 13110 |
kshitij.so |
68 |
public final static long now = System.currentTimeMillis();
|
|
|
69 |
public static Map<String, AmazonFbaInventorySnapshot> fbaInventoryMap = new HashMap<String,AmazonFbaInventorySnapshot>();
|
|
|
70 |
public static Map<String,AmazonHourlySaleSnapshot> skuSaleMap = new HashMap<String,AmazonHourlySaleSnapshot>();
|
|
|
71 |
|
|
|
72 |
public static void terminate(String msg){
|
|
|
73 |
String emailFromAddress = "build@shop2020.in";
|
|
|
74 |
String password = "cafe@nes";
|
|
|
75 |
String[] sendTo = new String[]{ "kshitij.sood@saholic.com","anikendra.das@shop2020.in"};
|
|
|
76 |
String emailSubjectTxt = msg;
|
|
|
77 |
try {
|
|
|
78 |
GmailUtils mailer = new GmailUtils();
|
|
|
79 |
mailer.sendSSLMessage(sendTo, emailSubjectTxt, "", emailFromAddress, password,"");
|
|
|
80 |
}
|
|
|
81 |
catch (Exception ex) {
|
|
|
82 |
ex.printStackTrace();
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
System.exit(1);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
public static String invokeServiceRequest() throws InterruptedException{
|
|
|
89 |
|
|
|
90 |
/* Returns request id of the OrderReport */
|
|
|
91 |
|
|
|
92 |
MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
|
|
|
93 |
config.setServiceURL("https://mws.amazonservices.in");
|
|
|
94 |
|
|
|
95 |
MarketplaceWebService service = new MarketplaceWebServiceClient(accessKeyId, secretAccessKey, appName, appVersion, config);
|
|
|
96 |
|
|
|
97 |
IdList marketplaces = new IdList(Arrays.asList("A21TJRUUN4KGV"));
|
|
|
98 |
RequestReportRequest orderreportrequest = new RequestReportRequest()
|
|
|
99 |
.withMerchant(merchantId)
|
|
|
100 |
.withMarketplaceIdList(marketplaces)
|
|
|
101 |
.withReportType("_GET_XML_ALL_ORDERS_DATA_BY_ORDER_DATE_")
|
|
|
102 |
.withReportOptions("ShowSalesChannel=true");
|
|
|
103 |
|
|
|
104 |
DatatypeFactory df = null;
|
|
|
105 |
try {
|
|
|
106 |
df = DatatypeFactory.newInstance();
|
|
|
107 |
} catch (DatatypeConfigurationException e) {
|
|
|
108 |
e.printStackTrace();
|
|
|
109 |
throw new RuntimeException(e);
|
|
|
110 |
}
|
|
|
111 |
Calendar cal = Calendar.getInstance();
|
|
|
112 |
cal.set(Calendar.HOUR_OF_DAY, 00);cal.set(Calendar.MINUTE,00);cal.set(Calendar.SECOND,00);cal.set(Calendar.MILLISECOND,00);
|
|
|
113 |
GregorianCalendar ost = new GregorianCalendar();
|
|
|
114 |
ost.setTimeInMillis(cal.getTimeInMillis());
|
|
|
115 |
XMLGregorianCalendar orderStartDate = df.newXMLGregorianCalendar(ost);
|
|
|
116 |
XMLGregorianCalendar orderEndDate = df.newXMLGregorianCalendar(new GregorianCalendar());
|
|
|
117 |
orderreportrequest.setStartDate(orderStartDate);
|
|
|
118 |
orderreportrequest.setEndDate(orderEndDate);
|
|
|
119 |
System.out.println(orderStartDate);
|
|
|
120 |
System.out.println(orderEndDate);
|
|
|
121 |
String orderreportrequestId = null;
|
|
|
122 |
boolean retry=true;
|
|
|
123 |
int retryCount =1;
|
|
|
124 |
while(retry){
|
|
|
125 |
if(retryCount==10){
|
|
|
126 |
terminate("Fba Hourly Sales - Unable to get request id");
|
|
|
127 |
}
|
|
|
128 |
try {
|
|
|
129 |
orderreportrequestId = RequestReportSample.invokeRequestReport(service, orderreportrequest);
|
|
|
130 |
retry = false;
|
|
|
131 |
} catch (MarketplaceWebServiceException e) {
|
|
|
132 |
e.printStackTrace();
|
|
|
133 |
Thread.sleep(1*60*1000);
|
|
|
134 |
retryCount++;
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
return orderreportrequestId;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
public static void writeDataToDisk(String requestId) throws FileNotFoundException, IOException, InterruptedException{
|
|
|
141 |
|
|
|
142 |
/* Write OrderXml file to disk */
|
|
|
143 |
|
|
|
144 |
int retryCount = 1;
|
|
|
145 |
while(true){
|
|
|
146 |
GetReportListRequest requestreportlist = new GetReportListRequest();
|
|
|
147 |
requestreportlist.setMerchant( merchantId );
|
|
|
148 |
final IdList requestIdList = new IdList(Arrays.asList(requestId));
|
|
|
149 |
requestreportlist.setReportRequestIdList(requestIdList);
|
|
|
150 |
MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
|
|
|
151 |
config.setServiceURL("https://mws.amazonservices.in");
|
|
|
152 |
MarketplaceWebService service = new MarketplaceWebServiceClient(accessKeyId, secretAccessKey, appName, appVersion, config);
|
|
|
153 |
Map<String, String> requestIdreportIdmap = GetReportListSample.invokeGetReportList(service, requestreportlist);
|
|
|
154 |
if(requestIdreportIdmap.get(requestId)!=null){
|
|
|
155 |
GetReportRequest requestorderreport = new GetReportRequest();
|
|
|
156 |
requestorderreport.setMerchant( merchantId );
|
|
|
157 |
requestorderreport.setReportId( requestIdreportIdmap.get(requestId));
|
|
|
158 |
OutputStream orderreport=null;
|
|
|
159 |
try {
|
|
|
160 |
orderreport = new FileOutputStream( "/tmp/amazonorderreport.xml" );
|
|
|
161 |
} catch (FileNotFoundException e) {
|
|
|
162 |
e.printStackTrace();
|
|
|
163 |
}
|
|
|
164 |
requestorderreport.setReportOutputStream(orderreport);
|
|
|
165 |
GetReportSample.invokeGetReport(service, requestorderreport);
|
|
|
166 |
System.out.println("Order Report is ready please check");
|
|
|
167 |
|
|
|
168 |
String toFind = "<AmazonEnvelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"amzn-envelope.xsd\">";
|
|
|
169 |
String toReplace = "<AmazonEnvelope xmlns=\"http://mws.amazonaws.com/doc/2009-01-01/\">";
|
|
|
170 |
File orderReportFile = new File(FILE_PATH);
|
|
|
171 |
String content = "";
|
|
|
172 |
try {
|
|
|
173 |
content = IOUtils.toString(new FileInputStream(orderReportFile));
|
|
|
174 |
} catch (FileNotFoundException e1) {
|
|
|
175 |
// TODO Auto-generated catch block
|
|
|
176 |
e1.printStackTrace();
|
|
|
177 |
} catch (IOException e1) {
|
|
|
178 |
// TODO Auto-generated catch block
|
|
|
179 |
e1.printStackTrace();
|
|
|
180 |
}
|
|
|
181 |
content = content.replaceAll(toFind, toReplace);
|
|
|
182 |
IOUtils.write(content, new FileOutputStream(orderReportFile));
|
|
|
183 |
break;
|
|
|
184 |
}
|
|
|
185 |
else{
|
|
|
186 |
if (retryCount == 5){
|
|
|
187 |
terminate("Unable to pull FBA Hourly sales report");
|
|
|
188 |
}
|
|
|
189 |
retryCount++;
|
|
|
190 |
Thread.sleep(1*60*1000);
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
public static void populateInventory() throws TException{
|
|
|
196 |
|
|
|
197 |
/* Populate fba inventory by fcLocation */
|
|
|
198 |
|
|
|
199 |
InventoryClient inventoryServiceClient = new InventoryClient();
|
|
|
200 |
in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
|
|
|
201 |
List<AmazonFbaInventorySnapshot> fbaInventory = inventoryClient.getAllAvailableAmazonFbaItemInventory();
|
|
|
202 |
for(AmazonFbaInventorySnapshot inventory : fbaInventory){
|
|
|
203 |
String prefix = "";
|
|
|
204 |
if (inventory.getLocation().getValue()==AmazonFCWarehouseLocation.Mumbai.getValue()){
|
|
|
205 |
prefix = "FBA";
|
|
|
206 |
}
|
|
|
207 |
else if (inventory.getLocation().getValue()==AmazonFCWarehouseLocation.Bangalore.getValue()){
|
|
|
208 |
prefix = "FBB";
|
|
|
209 |
}
|
|
|
210 |
else if (inventory.getLocation().getValue()==AmazonFCWarehouseLocation.Gurgaon.getValue()){
|
|
|
211 |
prefix = "FBG";
|
|
|
212 |
}
|
|
|
213 |
else{
|
|
|
214 |
continue;
|
|
|
215 |
}
|
|
|
216 |
fbaInventoryMap.put(prefix+String.valueOf(inventory.getItem_id()), inventory);
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
public static void populateAndCommitSale() throws ParseException{
|
|
|
221 |
JAXBContext jc = null;
|
|
|
222 |
Unmarshaller unmarshaller = null;
|
|
|
223 |
AmazonEnvelope amazonOrderData = null;
|
|
|
224 |
try {
|
|
|
225 |
jc = JAXBContext.newInstance(AmazonEnvelope.class);
|
|
|
226 |
unmarshaller = jc.createUnmarshaller();
|
|
|
227 |
amazonOrderData = (AmazonEnvelope)unmarshaller.unmarshal(new File(FILE_PATH));
|
|
|
228 |
} catch (JAXBException e1) {
|
|
|
229 |
e1.printStackTrace();
|
|
|
230 |
}
|
|
|
231 |
List<Message> orderMessageList = amazonOrderData.getMessage();
|
|
|
232 |
System.out.println("Amazon Order List ... "+orderMessageList.size());
|
|
|
233 |
|
|
|
234 |
for(Message orderMessage : orderMessageList){
|
|
|
235 |
Order amazonOrder = orderMessage.getOrder();
|
|
|
236 |
FulfillmentData orderFullfillmentData = amazonOrder.getFulfillmentData();
|
|
|
237 |
if("Amazon.in".equalsIgnoreCase(amazonOrder.getSalesChannel()) && "Amazon".equalsIgnoreCase(orderFullfillmentData.getFulfillmentChannel())
|
|
|
238 |
&& !("Cancelled".equalsIgnoreCase(amazonOrder.getOrderStatus()) || "Cancelled".equalsIgnoreCase(amazonOrder.getOrderItem().get(0).getItemStatus()))){
|
|
|
239 |
SimpleDateFormat istFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
|
|
240 |
istFormatter.setLenient(false);
|
|
|
241 |
TimeZone zone= TimeZone.getTimeZone("GMT");
|
|
|
242 |
istFormatter.setTimeZone(zone);
|
|
|
243 |
Date date = istFormatter.parse(amazonOrder.getPurchaseDate().toString());
|
|
|
244 |
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
|
|
|
245 |
Date date_key = dateFormat.parse(dateFormat.format(date));
|
|
|
246 |
Long itemid;
|
|
|
247 |
AmazonFCWarehouseLocation fcLocation;
|
|
|
248 |
String prefix = "";
|
|
|
249 |
if(amazonOrder.getOrderItem().get(0).getSKU().startsWith("FBA")){
|
|
|
250 |
try{
|
|
|
251 |
itemid = Long.parseLong(amazonOrder.getOrderItem().get(0).getSKU().replaceAll("FBA",""));
|
|
|
252 |
}
|
|
|
253 |
catch(Exception ex){
|
|
|
254 |
continue;
|
|
|
255 |
}
|
|
|
256 |
fcLocation = AmazonFCWarehouseLocation.Mumbai;
|
|
|
257 |
prefix = "FBA";
|
|
|
258 |
}
|
|
|
259 |
else if(amazonOrder.getOrderItem().get(0).getSKU().startsWith("FBB")){
|
|
|
260 |
try{
|
|
|
261 |
itemid = Long.parseLong(amazonOrder.getOrderItem().get(0).getSKU().replaceAll("FBB",""));
|
|
|
262 |
}
|
|
|
263 |
catch(Exception ex){
|
|
|
264 |
continue;
|
|
|
265 |
}
|
|
|
266 |
fcLocation = AmazonFCWarehouseLocation.Bangalore;
|
|
|
267 |
prefix = "FBB";
|
|
|
268 |
}
|
|
|
269 |
else if(amazonOrder.getOrderItem().get(0).getSKU().startsWith("FBG")){
|
|
|
270 |
try{
|
|
|
271 |
itemid = Long.parseLong(amazonOrder.getOrderItem().get(0).getSKU().replaceAll("FBG",""));
|
|
|
272 |
}
|
|
|
273 |
catch(Exception ex){
|
|
|
274 |
continue;
|
|
|
275 |
}
|
|
|
276 |
fcLocation = AmazonFCWarehouseLocation.Gurgaon;
|
|
|
277 |
prefix = "FBG";
|
|
|
278 |
}
|
|
|
279 |
else{
|
|
|
280 |
continue;
|
|
|
281 |
}
|
|
|
282 |
Integer qty=0;
|
|
|
283 |
if(amazonOrder.getOrderItem().get(0).getQuantity()!=0){
|
|
|
284 |
qty = new Integer(amazonOrder.getOrderItem().get(0).getQuantity());
|
|
|
285 |
}
|
|
|
286 |
Float itemSale = null;
|
|
|
287 |
if(amazonOrder.getOrderItem().get(0).getItemPrice()!=null && amazonOrder.getOrderItem().get(0).getItemPrice().getComponent()!=null && amazonOrder.getOrderItem().get(0).getItemPrice().getComponent().size()>0){
|
|
|
288 |
if(amazonOrder.getOrderItem().get(0).getItemPrice().getComponent().get(0).getAmount().getValue()!=0){
|
|
|
289 |
itemSale = new Float(amazonOrder.getOrderItem().get(0).getItemPrice().getComponent().get(0).getAmount().getValue());
|
|
|
290 |
}
|
|
|
291 |
else{
|
|
|
292 |
itemSale = (float) 0;
|
|
|
293 |
}
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
else{
|
|
|
297 |
itemSale = (float) 0;
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
Float itemDiscount;
|
|
|
301 |
if(amazonOrder.getOrderItem().get(0).getPromotion()!=null){
|
|
|
302 |
if(amazonOrder.getOrderItem().get(0).getPromotion().getItemPromotionDiscount()!=null && amazonOrder.getOrderItem().get(0).getPromotion().getItemPromotionDiscount().floatValue()!=0.0f){
|
|
|
303 |
itemDiscount = amazonOrder.getOrderItem().get(0).getPromotion().getItemPromotionDiscount();
|
|
|
304 |
}
|
|
|
305 |
else{
|
|
|
306 |
itemDiscount = new Float(0);
|
|
|
307 |
}
|
|
|
308 |
}
|
|
|
309 |
else{
|
|
|
310 |
itemDiscount = new Float(0);
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
AmazonHourlySaleSnapshot fbaSalesSnapshot;
|
|
|
314 |
if(skuSaleMap.containsKey(prefix+String.valueOf(itemid))){
|
|
|
315 |
fbaSalesSnapshot = skuSaleMap.get(prefix+String.valueOf(itemid));
|
|
|
316 |
if(itemDiscount!=0){
|
|
|
317 |
fbaSalesSnapshot.setPromotionOrderCount(fbaSalesSnapshot.getPromotionOrderCount()+qty);
|
|
|
318 |
fbaSalesSnapshot.setPromotionSale(fbaSalesSnapshot.getPromotionSale() + (itemSale - itemDiscount));
|
|
|
319 |
}
|
|
|
320 |
fbaSalesSnapshot.setTotalOrderCount(fbaSalesSnapshot.getTotalOrderCount() + qty);
|
|
|
321 |
fbaSalesSnapshot.setTotalSale(fbaSalesSnapshot.getTotalSale() + itemSale - itemDiscount);
|
|
|
322 |
}
|
|
|
323 |
else{
|
|
|
324 |
fbaSalesSnapshot = new AmazonHourlySaleSnapshot();
|
|
|
325 |
fbaSalesSnapshot.setTotalOrderCount(qty);
|
|
|
326 |
fbaSalesSnapshot.setTotalSale(itemSale - itemDiscount);
|
|
|
327 |
if(itemDiscount!=0){
|
|
|
328 |
fbaSalesSnapshot.setPromotionOrderCount(qty);
|
|
|
329 |
fbaSalesSnapshot.setPromotionSale(itemSale - itemDiscount);
|
|
|
330 |
}
|
|
|
331 |
else{
|
|
|
332 |
fbaSalesSnapshot.setPromotionOrderCount(0);
|
|
|
333 |
fbaSalesSnapshot.setPromotionSale((float) 0);
|
|
|
334 |
}
|
|
|
335 |
}
|
|
|
336 |
fbaSalesSnapshot.setItem_id(itemid);
|
|
|
337 |
if (fbaInventoryMap.containsKey(prefix+String.valueOf(itemid))){
|
|
|
338 |
fbaSalesSnapshot.setAmazonFbaInventory(fbaInventoryMap.get(prefix+String.valueOf(itemid)).getAvailability());
|
|
|
339 |
fbaSalesSnapshot.setIsOutOfStock(false);
|
|
|
340 |
}
|
|
|
341 |
else{
|
|
|
342 |
fbaSalesSnapshot.setAmazonFbaInventory(0);
|
|
|
343 |
fbaSalesSnapshot.setIsOutOfStock(true);
|
|
|
344 |
}
|
|
|
345 |
fbaSalesSnapshot.setSnapshotTime(now);
|
|
|
346 |
fbaSalesSnapshot.setFcLocation(fcLocation);
|
|
|
347 |
skuSaleMap.put(prefix+String.valueOf(itemid),fbaSalesSnapshot);
|
|
|
348 |
}
|
|
|
349 |
}
|
|
|
350 |
List<AmazonHourlySaleSnapshot> sales = new ArrayList<AmazonHourlySaleSnapshot>(skuSaleMap.values());
|
|
|
351 |
boolean result = Consumer.addHourlySnapshot(sales);
|
|
|
352 |
System.out.println("Data Committed ? " + result);
|
|
|
353 |
if (!result){
|
|
|
354 |
terminate("Unable to commit hourly FBA sales data to mongo");
|
|
|
355 |
}
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
public static void main (String[] args) throws InterruptedException, FileNotFoundException, IOException, TException, ParseException{
|
|
|
360 |
String requestId= invokeServiceRequest();
|
|
|
361 |
writeDataToDisk(requestId);
|
|
|
362 |
populateInventory();
|
|
|
363 |
populateAndCommitSale();
|
|
|
364 |
}
|
|
|
365 |
}
|