| 10049 |
vikram.rag |
1 |
package in.shop2020;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.catalog.FlipkartItem;
|
|
|
4 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
5 |
import in.shop2020.model.v1.catalog.SnapdealItem;
|
|
|
6 |
import in.shop2020.model.v1.inventory.FlipkartInventorySnapshot;
|
|
|
7 |
import in.shop2020.model.v1.inventory.InventoryService.Client;
|
|
|
8 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
9 |
import in.shop2020.thrift.clients.InventoryClient;
|
|
|
10 |
import in.shop2020.utils.GmailUtils;
|
|
|
11 |
|
|
|
12 |
import java.io.BufferedReader;
|
|
|
13 |
import java.io.FileInputStream;
|
|
|
14 |
import java.io.FileNotFoundException;
|
|
|
15 |
import java.io.FileOutputStream;
|
|
|
16 |
import java.io.IOException;
|
|
|
17 |
import java.io.InputStream;
|
|
|
18 |
import java.io.InputStreamReader;
|
|
|
19 |
import java.io.PrintStream;
|
|
|
20 |
import java.io.UnsupportedEncodingException;
|
|
|
21 |
import java.text.SimpleDateFormat;
|
|
|
22 |
import java.util.ArrayList;
|
|
|
23 |
import java.util.HashMap;
|
|
|
24 |
import java.util.List;
|
|
|
25 |
import java.util.Map;
|
|
|
26 |
|
|
|
27 |
import javax.mail.MessagingException;
|
|
|
28 |
|
|
|
29 |
import org.apache.http.HttpResponse;
|
|
|
30 |
import org.apache.http.NameValuePair;
|
|
|
31 |
import org.apache.http.client.ClientProtocolException;
|
|
|
32 |
import org.apache.http.client.HttpClient;
|
|
|
33 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
34 |
import org.apache.http.client.methods.HttpGet;
|
|
|
35 |
import org.apache.http.client.methods.HttpPost;
|
|
|
36 |
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
37 |
import org.apache.http.message.BasicNameValuePair;
|
|
|
38 |
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
39 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
40 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
41 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
42 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
43 |
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
44 |
import org.apache.thrift.TException;
|
|
|
45 |
import org.apache.thrift.transport.TTransportException;
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
public class FlipkartInventoryReconcillation {
|
|
|
49 |
|
| 10061 |
vikram.rag |
50 |
|
| 10049 |
vikram.rag |
51 |
public static void main(String... args) throws ClientProtocolException, IOException, TException{
|
|
|
52 |
SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
|
|
|
53 |
String emailFromAddress = "build@shop2020.in";
|
|
|
54 |
String password = "cafe@nes";
|
|
|
55 |
GmailUtils mailer = new GmailUtils();
|
|
|
56 |
String[] sendTo = new String[]{"vikram.raghav@shop2020.in"};
|
|
|
57 |
/*String[] sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "vikram.raghav@shop2020.in", "rajneesh.arora@shop2020.in",
|
|
|
58 |
"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
|
|
|
59 |
"yukti.jain@shop2020.in","manoj.pal@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","amar.kumar@shop2020.in"};
|
|
|
60 |
*/
|
| 10058 |
vikram.rag |
61 |
Map<String,Long> itemInventoryMap = FetchInventoryAtFlipkart();
|
| 10049 |
vikram.rag |
62 |
in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = new CatalogClient().getClient();
|
|
|
63 |
List<FlipkartItem> flipkartItems = catalogClient.getAllFlipkartItems();
|
| 10069 |
vikram.rag |
64 |
catalogClient = new CatalogClient().getClient();
|
|
|
65 |
List<Item> aliveItems = catalogClient.getAllAliveItems();
|
|
|
66 |
/*try{
|
| 10053 |
vikram.rag |
67 |
aliveItems = catalogClient.getAllAliveItems();
|
|
|
68 |
}
|
| 10066 |
vikram.rag |
69 |
catch(Exception e){
|
| 10053 |
vikram.rag |
70 |
catalogClient = new CatalogClient().getClient();
|
|
|
71 |
aliveItems = catalogClient.getAllAliveItems();
|
| 10069 |
vikram.rag |
72 |
}*/
|
| 10066 |
vikram.rag |
73 |
Map<Long,FlipkartItem> flipkartItemsMap = new HashMap<Long,FlipkartItem>();
|
| 10049 |
vikram.rag |
74 |
Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>();
|
|
|
75 |
for(Item item:aliveItems){
|
|
|
76 |
aliveItemsMap.put(item.getId(),item);
|
|
|
77 |
}
|
|
|
78 |
for(FlipkartItem flipkartItem:flipkartItems){
|
|
|
79 |
if(flipkartItem.isIsListedOnFlipkart() && !flipkartItem.isSetSuppressInventoryFeed()){
|
|
|
80 |
flipkartItemsMap.put(flipkartItem.getItem_id(), flipkartItem);
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
Client inventoryServiceClient = new InventoryClient("inventory_service_server_host","inventory_service_server_port").getClient();
|
|
|
84 |
List<FlipkartInventorySnapshot> flipkartInventorySnapshot;
|
|
|
85 |
try {
|
|
|
86 |
flipkartInventorySnapshot = inventoryServiceClient.getFlipkartInventorySnapshot();
|
|
|
87 |
} catch (TException e) {
|
|
|
88 |
e.printStackTrace();
|
|
|
89 |
inventoryServiceClient = new InventoryClient("inventory_service_server_host","inventory_service_server_port").getClient();
|
|
|
90 |
flipkartInventorySnapshot = inventoryServiceClient.getFlipkartInventorySnapshot();
|
|
|
91 |
}
|
|
|
92 |
FlipkartItem flipkartItem;
|
|
|
93 |
Long availabilityAtFlipkart = 0L;
|
|
|
94 |
Item item;
|
|
|
95 |
String tableHeader = "<html><table border=\"1\" align=\"center\"><tr>"
|
|
|
96 |
+ "<td><b>" + "Product Name" +"</b></td>"
|
|
|
97 |
+"<td><b>" + "Item ID" + "</b></td>"
|
|
|
98 |
+"<td><b>" + "Inventory Sent" + "</b></td>"
|
|
|
99 |
+"<td><b>" + "Inventory Updated" + "</b></td>"
|
|
|
100 |
+"<td><b>" + "Orders Added" + "</b></td>"
|
|
|
101 |
+"<td><b>" + "Order Held" + "</b></td>"
|
|
|
102 |
+"<td><b>" + "Difference" + "</b></td>"
|
|
|
103 |
+"</tr>";
|
|
|
104 |
String tableFooter = "</table></html>";
|
|
|
105 |
StringBuffer reconcilledItems = new StringBuffer();
|
|
|
106 |
for(FlipkartInventorySnapshot snapshot:flipkartInventorySnapshot){
|
|
|
107 |
System.out.println("Item ID " + snapshot.getItem_id());
|
|
|
108 |
System.out.println("Sent Inventory " + snapshot.getAvailability());
|
|
|
109 |
System.out.println("Held Orders " + snapshot.getHeldOrders());
|
|
|
110 |
System.out.println("Created Orders " + snapshot.getCreatedOrders());
|
|
|
111 |
flipkartItem = flipkartItemsMap.get(snapshot.getItem_id());
|
|
|
112 |
availabilityAtFlipkart = itemInventoryMap.get(flipkartItem.getSkuAtFlipkart());
|
|
|
113 |
item = aliveItemsMap.get(snapshot.getItem_id());
|
|
|
114 |
snapshot.getItem_id();
|
|
|
115 |
snapshot.getAvailability();
|
|
|
116 |
snapshot.getCreatedOrders();
|
|
|
117 |
snapshot.getHeldOrders();
|
|
|
118 |
if(snapshot.getAvailability() - snapshot.getCreatedOrders() - snapshot.getHeldOrders() !=0){
|
|
|
119 |
reconcilledItems .append("<tr>"
|
|
|
120 |
+ "<td>" +getProductName(item)+"</td>"
|
|
|
121 |
+"<td>" + snapshot.getItem_id() + "</td>"
|
|
|
122 |
+"<td>" + snapshot.getAvailability() + "</td>"
|
|
|
123 |
+"<td>" + availabilityAtFlipkart + "</td>"
|
|
|
124 |
+"<td>" + snapshot.getCreatedOrders() + "</td>"
|
|
|
125 |
+"<td>" + snapshot.getHeldOrders() + "</td>"
|
|
|
126 |
+"<td>" + (snapshot.getAvailability() - snapshot.getCreatedOrders() - snapshot.getHeldOrders()) + "</td>"
|
|
|
127 |
+"</tr>");
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
String text = tableHeader+reconcilledItems.toString()+tableFooter;
|
|
|
131 |
try {
|
|
|
132 |
mailer.sendSSLMessage(sendTo,"Flipkart Inventory Reconcillation ", emailFromAddress, password, text);
|
|
|
133 |
} catch (MessagingException e) {
|
|
|
134 |
e.printStackTrace();
|
|
|
135 |
}
|
|
|
136 |
}
|
| 10058 |
vikram.rag |
137 |
public static Map<String, Long> FetchInventoryAtFlipkart() throws ClientProtocolException, IOException{
|
|
|
138 |
Map<String,Long> itemInventoryMap = new HashMap<String,Long>();
|
| 10049 |
vikram.rag |
139 |
HttpClient client = new DefaultHttpClient();
|
|
|
140 |
HttpPost post = new HttpPost("https://seller.flipkart.com/login");
|
|
|
141 |
BufferedReader rd= null;
|
|
|
142 |
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
|
|
|
143 |
nameValuePairs.add(new BasicNameValuePair("authName",
|
|
|
144 |
"flipkart"));
|
|
|
145 |
nameValuePairs.add(new BasicNameValuePair("username",
|
|
|
146 |
"flipkart-support@saholic.com"));
|
|
|
147 |
nameValuePairs.add(new BasicNameValuePair("password",
|
|
|
148 |
"076c27ee24d7596b06608a8ed2559f87"));
|
|
|
149 |
post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
|
|
|
150 |
HttpResponse response = client.execute(post);
|
|
|
151 |
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
152 |
String line ="";
|
|
|
153 |
while ((line = rd.readLine()) != null) {
|
|
|
154 |
//System.out.println(line);
|
|
|
155 |
}
|
|
|
156 |
HttpGet get = new HttpGet("https://seller.flipkart.com/sellerListing/listing/downloadTemplateForExistingProducts?category=Mobiles+%26+Cameras&sellerId=m2z93iskuj81qiid&feedType=PRICING_STOCK&useDefaultTemplate=false");
|
|
|
157 |
response = client.execute(get);
|
|
|
158 |
InputStream in = response.getEntity().getContent();
|
|
|
159 |
Workbook wb = new HSSFWorkbook(in);
|
|
|
160 |
Sheet sheet = wb.getSheetAt(0);
|
|
|
161 |
Row firstRow = sheet.getRow(0);
|
|
|
162 |
Row SecondRow = sheet.getRow(1);
|
|
|
163 |
for (Row row : sheet) {
|
|
|
164 |
if(row.equals(firstRow) || row.equals(SecondRow))
|
|
|
165 |
continue;
|
|
|
166 |
row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
167 |
row.getCell(15).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
168 |
System.out.println("SKU " + row.getCell(6).getStringCellValue() + " " + "Quantity "+ row.getCell(15).getStringCellValue());
|
|
|
169 |
if(!row.getCell(15).getStringCellValue().equals("")){
|
| 10058 |
vikram.rag |
170 |
itemInventoryMap.put(row.getCell(6).getStringCellValue(), Long.parseLong(row.getCell(15).getStringCellValue()));
|
| 10049 |
vikram.rag |
171 |
}
|
|
|
172 |
else{
|
| 10058 |
vikram.rag |
173 |
itemInventoryMap.put(row.getCell(6).getStringCellValue(),0L);
|
| 10049 |
vikram.rag |
174 |
}
|
|
|
175 |
}
|
|
|
176 |
return itemInventoryMap;
|
|
|
177 |
}
|
|
|
178 |
static String getProductName(Item item){
|
|
|
179 |
return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor());
|
|
|
180 |
|
|
|
181 |
}
|
|
|
182 |
static String getName(String name){
|
|
|
183 |
if(name==null || name.length()==0){
|
|
|
184 |
return "";
|
|
|
185 |
}
|
|
|
186 |
else{
|
|
|
187 |
return name;
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
}
|