Subversion Repositories SmartDukaan

Rev

Rev 8288 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.amazonaws.mws.samples;

import in.shop2020.model.v1.catalog.Amazonlisted;
import in.shop2020.model.v1.catalog.CatalogServiceException;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.inventory.AmazonFbaInventorySnapshot;
import in.shop2020.model.v1.inventory.InventoryServiceException;
import in.shop2020.model.v1.inventory.Vendor;
import in.shop2020.model.v1.inventory.VendorItemPricing;
import in.shop2020.model.v1.order.AmazonFbaSalesSnapshot;
import in.shop2020.model.v1.order.TransactionService.Client;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.InventoryClient;
import in.shop2020.thrift.clients.TransactionClient;
import in.shop2020.utils.GmailUtils;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.thrift.TException;

import au.com.bytecode.opencsv.CSVReader;


public class CreateSendFBAStockEstimation {
        final public static String AMAZON_FBA_SHEET = "/home/vikram/Desktop/FBA-Stock-File.xls";
        
        public static void main(){
                TransactionClient transactionServiceClient = null;
                try {
                        transactionServiceClient = new TransactionClient();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }       
                
                in.shop2020.model.v1.order.TransactionService.Client transactionClient   = transactionServiceClient.getClient();
                List<AmazonFbaSalesSnapshot> salessnapshots = null;
                try {
                        salessnapshots = transactionClient.getAmazonFbaSalesSnapshotForDays(5);
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                Map<Long,Integer> itemIdDaysOfStockMap = new HashMap<Long,Integer>();
                Map<Long,List<Integer>> itemId5daysSale = new HashMap<Long,List<Integer>>();
                AmazonFbaSalesSnapshot snapshot;
                for(AmazonFbaSalesSnapshot salessnapshot:salessnapshots){
                        try {
                                snapshot = transactionClient.getAmazonFbaSalesLatestSnapshotForItem(salessnapshot.getItem_id());
                                if (snapshot.getMinFbaPrice()!=0 && snapshot.getMinMfnPrice()!=0){
                                        if(!itemIdDaysOfStockMap.containsKey(snapshot.getItem_id())){
                                                if(getPercentageDifferenceFromMinimumPrice(snapshot) >= -3){
                                                        itemIdDaysOfStockMap.put(snapshot.getItem_id(),15);
                                                }
                                                else if(getPercentageDifferenceFromMinimumPrice(snapshot) > -3){
                                                        itemIdDaysOfStockMap.put(snapshot.getItem_id(),10);
                                                }
                                                else if(getPercentageDifferenceFromMinimumPrice(snapshot) < 1){
                                                        itemIdDaysOfStockMap.put(snapshot.getItem_id(),4);
                                                }       
                                                else if(snapshot.getSalePrice() > snapshot.getMinMfnPrice() && snapshot.getSalePrice() < snapshot.getMinFbaPrice()){
                                                        if(getPercentageDifferenceFromMinimumPrice(snapshot) <= 2)
                                                                itemIdDaysOfStockMap.put(snapshot.getItem_id(),2);
                                                }
                                                else{
                                                        itemIdDaysOfStockMap.put(snapshot.getItem_id(),0);
                                                }
                                        }
                                }
                                else{
                                        itemIdDaysOfStockMap.put(snapshot.getItem_id(),0);
                                }
                                if(itemId5daysSale.containsKey(salessnapshot.getItem_id())){
                                        if(salessnapshot.getAmazonFbaInventory()!=0)
                                                itemId5daysSale.get(salessnapshot.getItem_id()).add(salessnapshot.getOrderCount());
                                        else
                                                itemId5daysSale.get(salessnapshot.getItem_id()).add(-1);
                                }
                                else{
                                        List<Integer> last5daysSale = new ArrayList<Integer>();
                                        if(salessnapshot.getAmazonFbaInventory()!=0){
                                                last5daysSale.add(salessnapshot.getOrderCount());
                                        }
                                        else{ 
                                                last5daysSale.add(-1);
                                        }
                                        itemId5daysSale.put(salessnapshot.getItem_id(),last5daysSale);
                                }
                        } catch (TException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        System.out.println("ItemID , Last Five Days Sale , Product Name , TP , NLC , STICKY , Preferred Vendor , Qty , Min AFN , Min MFN , SP ,Total Amount");
                        CatalogClient catalogServiceClient = null;
                        InventoryClient inventoryServiceClient = null;
                        try {
                                catalogServiceClient = new CatalogClient();
                                inventoryServiceClient = new InventoryClient();
                                //catalogServiceClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }       
                        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient   = catalogServiceClient.getClient();
                        in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
                        HSSFWorkbook hwb=new HSSFWorkbook();
                        HSSFSheet sheet =  hwb.createSheet("FBA-STOCK-SHEET");
                        HSSFRow rowhead=   sheet.createRow((short)0);
                        rowhead.createCell((short) 0).setCellValue("ITEM-ID");
                        rowhead.createCell((short) 1).setCellValue("LAST 5 DAYS SALE");
                        rowhead.createCell((short) 2).setCellValue("PRODUCT-NAME");
                        rowhead.createCell((short) 3).setCellValue("TRANSFER-PRICE");
                        rowhead.createCell((short) 4).setCellValue("NLC");
                        rowhead.createCell((short) 5).setCellValue("IS-STICKY");
                        rowhead.createCell((short) 6).setCellValue("PREFERRED-VENDOR");
                        rowhead.createCell((short) 7).setCellValue("QUANTITY");
                        rowhead.createCell((short) 8).setCellValue("MIN-AFN-PRICE");
                        rowhead.createCell((short) 9).setCellValue("MIN-MFN-PRICE");
                        rowhead.createCell((short) 10).setCellValue("OUR-PRICE");
                        rowhead.createCell((short) 11).setCellValue("TOTAL-AMOUNT");
                        int iterator=1;
                        for(Map.Entry<Long,List<Integer>> entry : itemId5daysSale.entrySet()){
                                int count =1;
                                int avg_sale=0;
                                int sale_days=0;
                                String days_sale = new String();
                                for(Integer sale:entry.getValue()){
                                        if(sale!=-1){
                                                days_sale.concat(sale.toString());
                                                avg_sale=avg_sale+sale;
                                                sale_days++;
                                        }
                                        else{
                                                days_sale.concat("X");
                                        }
                                        if(count != entry.getValue().size()){
                                                days_sale.concat("-");
                                        }
                                }
                                avg_sale=avg_sale*sale_days;
                                Item item = null;
                                VendorItemPricing vip=null;
                                Vendor vendor = null;
                                try {
                                        System.out.println("Item ID is " + entry.getKey());
                                        item = catalogClient.getItem(entry.getKey());
                                        vip = inventoryClient.getItemPricing(item.getId(),item.getPreferredVendor());
                                        vendor = inventoryClient.getVendor(item.getPreferredVendor());
                                } catch (CatalogServiceException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                } catch (TException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                } catch (InventoryServiceException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                                System.out.println(entry.getKey() + "\t  "+ days_sale.toString() + "\t" + item.getBrand() + " " + item.getModelName() + item.getModelNumber() +
                                                item.getColor() + "\t" + vip.getTransferPrice() + "\t" + vip.getNlc() + "\t" + item.isIsWarehousePreferenceSticky() + "\t" 
                                                + vendor.getName() + avg_sale*itemIdDaysOfStockMap.get(entry.getKey()));
                                HSSFRow row=   sheet.createRow((short)iterator);
                                row.createCell((short) 0).setCellValue(entry.getKey());
                                row.createCell((short) 1).setCellValue(days_sale);
                                row.createCell((short) 2).setCellValue(item.getBrand() + " " + item.getModelName()+" " + item.getModelNumber() + " " +
                                                item.getColor());
                                row.createCell((short) 3).setCellValue(vip.getTransferPrice());
                                row.createCell((short) 4).setCellValue(vip.getNlc());
                                if(item.isIsWarehousePreferenceSticky())
                                        row.createCell((short) 5).setCellValue("YES");
                                else
                                        row.createCell((short) 5).setCellValue("NO");
                                row.createCell((short) 6).setCellValue(vendor.getName());
                                row.createCell((short) 7).setCellValue(avg_sale*itemIdDaysOfStockMap.get(entry.getKey()));
                                AmazonFbaSalesSnapshot latest_snapshot = null;
                                try {
                                        latest_snapshot = transactionClient.getAmazonFbaSalesLatestSnapshotForItem(entry.getKey());
                                } catch (TException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                                row.createCell((short) 8).setCellValue(latest_snapshot.getMinFbaPrice());
                                row.createCell((short) 9).setCellValue(latest_snapshot.getMinMfnPrice());
                                row.createCell((short) 10).setCellValue(latest_snapshot.getSalePrice());
                                row.createCell((short) 11).setCellValue(avg_sale*itemIdDaysOfStockMap.get(entry.getKey())*latest_snapshot.getSalePrice());
                                iterator++;

                        }
                        FileOutputStream fileOut = null;
                        try {
                                fileOut = new FileOutputStream(AMAZON_FBA_SHEET);
                        } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        try {
                                hwb.write(fileOut);
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        try {
                                fileOut.close();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }

                }
                String emailFromAddress = "build@shop2020.in";
                String password = "cafe@nes";
                String[] sendTo = new String[]{ "vikram.raghav@shop2020.in", "kshitij.sood@shop2020.in" };
                String emailSubjectTxt = "FBA Stock Estimation Sheet ";
                GmailUtils mailer = new GmailUtils();
                try {
                        mailer.sendSSLMessage(sendTo, emailSubjectTxt, "", emailFromAddress, password, "/home/amazon/FBA-Stock-File.xls");
                }
                catch (Exception ex) {
                        ex.printStackTrace();
                }

        }
        
        public static Double getPercentageDifferenceFromMinimumPrice(AmazonFbaSalesSnapshot snapshot){
                Double minPrice = getMinimumSalePriceOnAmazonFBA(snapshot);
                return (snapshot.getSalePrice() - minPrice)/snapshot.getSalePrice();
                
        }
        
        public static Double getMinimumSalePriceOnAmazonFBA(AmazonFbaSalesSnapshot snapshot){
                if(snapshot.getMinFbaPrice() > snapshot.getMinMfnPrice()){
                        return snapshot.getMinMfnPrice();
                }
                else{
                        return snapshot.getMinFbaPrice();
                }
        }

}