Subversion Repositories SmartDukaan

Rev

Rev 8288 | Rev 8363 | 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/FBA-Stock-File.xls";

        public static void main(String... args){
                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,List<Integer>> itemId5daysSale = new HashMap<Long,List<Integer>>();
                AmazonFbaSalesSnapshot snapshot;
                for(AmazonFbaSalesSnapshot salessnapshot:salessnapshots){
                        try {
                                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 (Exception 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 {
                        inventoryServiceClient = new InventoryClient();
                        //catalogServiceClient = new CatalogClient();
                        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");
                rowhead.createCell((short) 12).setCellValue("DAYS-OF-STOCK");
                int iterator=1;
                for(Map.Entry<Long,List<Integer>> entry : itemId5daysSale.entrySet()){
                        int count =1;
                        int avg_sale=0;
                        int sale_days=0;
                        StringBuilder days_sale = new StringBuilder();
                        for(Integer sale:entry.getValue()){
                                if(sale!=-1){
                                        days_sale.append(sale.toString());
                                        avg_sale=avg_sale+sale;
                                        sale_days++;
                                }
                                else{
                                        days_sale.append("X");
                                }
                                if(count != entry.getValue().size()){
                                        days_sale.append("-");
                                }
                                count++;
                        }
                        if(sale_days!=0){
                                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();
                        }
                        AmazonFbaSalesSnapshot latest_snapshot = null;
                        try {
                                latest_snapshot = transactionClient.getAmazonFbaSalesLatestSnapshotForItem(entry.getKey());
                        } catch (TException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        int days_of_stock=0;
                        if(latest_snapshot.getMinFbaPrice()==0 ||  latest_snapshot.getMinMfnPrice()==0){
                                days_of_stock=0;
                        }
                        else{
                                double percent_diff = getPercentageDifferenceFromMinimumPrice(latest_snapshot);
                                if(percent_diff >= 3){
                                        days_of_stock = 15;
                                }
                                else if(percent_diff > 0 && percent_diff < 3){
                                        days_of_stock = 10;
                                }
                                else if(percent_diff > 0 && percent_diff < 1){
                                        days_of_stock = 4;
                                }
                                else if(latest_snapshot.getSalePrice() < latest_snapshot.getMinFbaPrice() &&  latest_snapshot.getSalePrice() > latest_snapshot.getMinMfnPrice() && percent_diff <= 2){
                                        days_of_stock = 2;
                                }
                                else{
                                        days_of_stock = 0;
                                }
                        }

                        HSSFRow row=   sheet.createRow((short)iterator);
                        row.createCell((short) 0).setCellValue(entry.getKey());
                        row.createCell((short) 1).setCellValue(days_sale.toString());
                        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*days_of_stock);
                        row.createCell((short) 8).setCellValue(latest_snapshot.getMinFbaPrice());
                        row.createCell((short) 9).setCellValue(latest_snapshot.getMinMfnPrice());
                        row.createCell((short) 10).setCellValue(latest_snapshot.getSalePrice());
                        double amount = avg_sale*days_of_stock*latest_snapshot.getSalePrice();
                        row.createCell((short) 11).setCellValue(amount);
                        row.createCell((short) 12).setCellValue(days_of_stock);
                        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", "rajveer.singh@shop2020.in" };
                String emailSubjectTxt = "FBA Stock Estimation Sheet";

                try {
                        GmailUtils mailer = new GmailUtils();
                        mailer.sendSSLMessage(sendTo, emailSubjectTxt, "", emailFromAddress, password, "/home/FBA-Stock-File.xls");
                }
                catch (Exception ex) {
                        ex.printStackTrace();
                }

        }

        public static Double getPercentageDifferenceFromMinimumPrice(AmazonFbaSalesSnapshot snapshot){
                Double minPrice = getMinimumSalePriceOnAmazonFBA(snapshot);
                return (((minPrice - snapshot.getSalePrice())/snapshot.getSalePrice())*100);

        }

        public static Double getMinimumSalePriceOnAmazonFBA(AmazonFbaSalesSnapshot snapshot){
                if(snapshot.getMinFbaPrice() > snapshot.getMinMfnPrice()){
                        return snapshot.getMinMfnPrice();
                }
                else{
                        return snapshot.getMinFbaPrice();
                }
        }

}