Rev 11675 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020;import in.shop2020.model.v1.catalog.CatalogServiceException;import in.shop2020.model.v1.catalog.FlipkartItem;import in.shop2020.model.v1.catalog.Item;import in.shop2020.model.v1.catalog.SnapdealItem;import in.shop2020.model.v1.inventory.FlipkartInventorySnapshot;import in.shop2020.model.v1.inventory.InventoryService.Client;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.thrift.clients.InventoryClient;import in.shop2020.utils.GmailUtils;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintStream;import java.io.UnsupportedEncodingException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.mail.MessagingException;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;public class FlipkartInventoryReconcillation {public static void main(String... args) throws ClientProtocolException, IOException, TException{SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");String emailFromAddress = "build@shop2020.in";String password = "cafe@nes";GmailUtils mailer = new GmailUtils();//String[] sendTo = new String[]{"vikram.raghav@shop2020.in"};String[] sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "vikram.raghav@shop2020.in", "rajneesh.arora@shop2020.in","khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com","yukti.jain@shop2020.in","manoj.pal@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","amar.kumar@shop2020.in"};Map<String,Long> itemInventoryMap = FetchInventoryAtFlipkart();in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = null;List<Item> aliveItems = null;List<FlipkartItem> flipkartItems = null;boolean ex = true;while(ex){try {catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();flipkartItems = catalogClient.getAllFlipkartItems();ex =false;} catch (Exception e) {e.printStackTrace();}}ex=true;while(ex){try {aliveItems = catalogClient.getAllAliveItems();ex =false;} catch (Exception e) {try {catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();} catch (Exception e1) {e1.printStackTrace();}}}Map<Long,FlipkartItem> flipkartItemsMap = new HashMap<Long,FlipkartItem>();Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>();for(Item item:aliveItems){aliveItemsMap.put(item.getId(),item);}for(FlipkartItem flipkartItem:flipkartItems){flipkartItemsMap.put(flipkartItem.getItem_id(), flipkartItem);}Client inventoryServiceClient = new InventoryClient().getClient();List<FlipkartInventorySnapshot> flipkartInventorySnapshot;try {flipkartInventorySnapshot = inventoryServiceClient.getFlipkartInventorySnapshot();} catch (TException e) {e.printStackTrace();inventoryServiceClient = new InventoryClient().getClient();flipkartInventorySnapshot = inventoryServiceClient.getFlipkartInventorySnapshot();}FlipkartItem flipkartItem;Long availabilityAtFlipkart = 0L;Item item;String tableHeader = "<html><table border=\"1\" align=\"center\"><tr>"+ "<td><b>" + "Product Name" +"</b></td>"+"<td><b>" + "Item ID" + "</b></td>"+"<td><b>" + "Inventory Sent" + "</b></td>"+"<td><b>" + "Orders Added" + "</b></td>"+"<td><b>" + "Order Held" + "</b></td>"+"<td><b>" + "Inventory Updated" + "</b></td>"+"<td><b>" + "Difference" + "</b></td>"+"</tr>";String tableFooter = "</table></html>";StringBuffer reconcilledItems = new StringBuffer();for(FlipkartInventorySnapshot snapshot:flipkartInventorySnapshot){System.out.println("Item ID " + snapshot.getItem_id());System.out.println("Sent Inventory " + snapshot.getAvailability());System.out.println("Held Orders " + snapshot.getHeldOrders());System.out.println("Created Orders " + snapshot.getCreatedOrders());if(flipkartItemsMap.containsKey(snapshot.getItem_id())){flipkartItem = flipkartItemsMap.get(snapshot.getItem_id());}else{continue;}System.out.println("SKU at Flipkart " + flipkartItem.getSkuAtFlipkart());if(itemInventoryMap.containsKey(flipkartItem.getSkuAtFlipkart())){availabilityAtFlipkart = itemInventoryMap.get(flipkartItem.getSkuAtFlipkart());}else{availabilityAtFlipkart = 0L;}item = aliveItemsMap.get(snapshot.getItem_id());System.out.println("Availability at Flipkart " + availabilityAtFlipkart);if(snapshot.getAvailability()==25){snapshot.setCreatedOrders(0);}if((snapshot.getAvailability() - snapshot.getCreatedOrders() - snapshot.getHeldOrders() - availabilityAtFlipkart)!=0){reconcilledItems .append("<tr>"+ "<td>" +getProductName(item)+"</td>"+"<td>" + snapshot.getItem_id() + "</td>"+"<td>" + snapshot.getAvailability() + "</td>"+"<td>" + snapshot.getCreatedOrders() + "</td>"+"<td>" + snapshot.getHeldOrders() + "</td>"+"<td>" + availabilityAtFlipkart + "</td>"+"<td>" + (snapshot.getAvailability() - snapshot.getCreatedOrders() - availabilityAtFlipkart) + "</td>"+"</tr>");}}String text = tableHeader+reconcilledItems.toString()+tableFooter;try {mailer.sendSSLMessage(sendTo,"Flipkart Inventory Reconcillation "+sdf.format(System.currentTimeMillis()), emailFromAddress, password, text);} catch (MessagingException e) {e.printStackTrace();}}public static Map<String, Long> FetchInventoryAtFlipkart() throws ClientProtocolException, IOException{Map<String,Long> itemInventoryMap = new HashMap<String,Long>();HttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost("https://seller.flipkart.com/login");BufferedReader rd= null;List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();nameValuePairs.add(new BasicNameValuePair("authName","flipkart"));nameValuePairs.add(new BasicNameValuePair("username","flipkart-support@saholic.com"));nameValuePairs.add(new BasicNameValuePair("password","076c27ee24d7596b06608a8ed2559f87"));post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));HttpResponse response = client.execute(post);rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));String line ="";while ((line = rd.readLine()) != null) {//System.out.println(line);}HttpGet get = new HttpGet("https://seller.flipkart.com/sellerListing/listing/downloadTemplateForExistingProducts?category=Computers+%26+Gaming&sellerId=m2z93iskuj81qiid&feedType=PRICING_STOCK&useDefaultTemplate=false&requestId=default");response = client.execute(get);InputStream in = response.getEntity().getContent();Workbook wb = new HSSFWorkbook(in);Sheet sheet = wb.getSheetAt(0);Row firstRow = sheet.getRow(0);Row SecondRow = sheet.getRow(1);for (Row row : sheet) {if(row.equals(firstRow) || row.equals(SecondRow))continue;row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);row.getCell(15).setCellType(Cell.CELL_TYPE_STRING);System.out.println("SKU " + row.getCell(6).getStringCellValue() + " " + "Quantity "+ row.getCell(15).getStringCellValue());if(!row.getCell(15).getStringCellValue().equals("")){itemInventoryMap.put(row.getCell(6).getStringCellValue(), Long.parseLong(row.getCell(15).getStringCellValue()));}else{itemInventoryMap.put(row.getCell(6).getStringCellValue(),0L);}}get = new HttpGet("https://seller.flipkart.com/sellerListing/listing/downloadTemplateForExistingProducts?category=Mobiles+%26+Cameras&sellerId=m2z93iskuj81qiid&feedType=PRICING_STOCK&useDefaultTemplate=false&requestId=default");response = client.execute(get);in = response.getEntity().getContent();wb = new HSSFWorkbook(in);sheet = wb.getSheetAt(0);firstRow = sheet.getRow(0);SecondRow = sheet.getRow(1);for (Row row : sheet) {if(row.equals(firstRow) || row.equals(SecondRow))continue;row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);row.getCell(15).setCellType(Cell.CELL_TYPE_STRING);System.out.println("SKU " + row.getCell(6).getStringCellValue() + " " + "Quantity "+ row.getCell(15).getStringCellValue());if(!row.getCell(15).getStringCellValue().equals("")){itemInventoryMap.put(row.getCell(6).getStringCellValue(), Long.parseLong(row.getCell(15).getStringCellValue()));}else{itemInventoryMap.put(row.getCell(6).getStringCellValue(),0L);}}return itemInventoryMap;}static String getProductName(Item item){return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor());}static String getName(String name){if(name==null || name.length()==0){return "";}else{return name;}}}