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