Subversion Repositories SmartDukaan

Rev

Rev 8423 | Rev 8449 | 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 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 FileParser {
        final public static String AMAZON_FBA_SHEET = "/home/vikram/Desktop/FBA-Stock-File.xls";
        public static void main(String... args){
                CSVReader orderreportreader = null;
                CSVReader inventoryhealthreportreader = null;
                try {
                        orderreportreader = new CSVReader(new FileReader("/home/amazonorderreport.csv"),'\t');
                        inventoryhealthreportreader = new CSVReader(new FileReader("/home/inventoryhealthreport.csv"),'\t');
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                String [] nextLine;
                try {
                        int count =1;
                        Map<Date,Map<Long,FbaSalesSnapshot>> orderDateItemIdFbaSaleSnapshotMap = new HashMap<Date,Map<Long,FbaSalesSnapshot>>();
                        while ((nextLine = orderreportreader.readNext()) != null) {
                                if(count!=1 && nextLine[5].equalsIgnoreCase("Amazon") && nextLine[6].equalsIgnoreCase("Amazon.in") && (nextLine[13].equalsIgnoreCase("Unshipped") || nextLine[13].equalsIgnoreCase("Shipped"))){
                                        SimpleDateFormat istFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                                        istFormatter .setLenient(false);
                                        TimeZone zone= TimeZone.getTimeZone("GMT");
                                        istFormatter.setTimeZone(zone);
                                        Date date = istFormatter.parse(nextLine[2]);
                                        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
                                        Date date_key = dateFormat.parse(dateFormat.format(date));
                                        System.out.println(nextLine[0]+" "+date_key+" "+ nextLine[11] +" " + nextLine[13] + " " + nextLine[14]);
                                        Long itemid = Long.parseLong(nextLine[11].replaceAll("FBA",""));
                                        
                                        Integer qty = new Integer(nextLine[14]);
                                        Float itemSale = null;
                                        if(nextLine[16].length()!=0){
                                                itemSale = new Float(nextLine[16]);
                                        }
                                        else{
                                                continue;
                                        }
                                        Float itemDiscount; 
                                        if(nextLine[22].length()!=0){
                                                itemDiscount = new Float(nextLine[22]);
                                        }
                                        else{
                                                itemDiscount = new Float(0);
                                        }
                                        if(orderDateItemIdFbaSaleSnapshotMap.containsKey(date_key)){
                                                if(orderDateItemIdFbaSaleSnapshotMap.get(date_key).containsKey(itemid)){
                                                        FbaSalesSnapshot fbaSalesSnapshot = orderDateItemIdFbaSaleSnapshotMap.get(date_key).get(itemid);
                                                        if(itemDiscount!=0){
                                                                fbaSalesSnapshot.setPromotionOrderCount(fbaSalesSnapshot.getPromotionOrderCount()+qty);
                                                                fbaSalesSnapshot.setTotalPromotionSale(fbaSalesSnapshot.getTotalPromotionSale() + (itemSale - itemDiscount));
                                                        }
                                                        fbaSalesSnapshot.setTotalOrderCount(fbaSalesSnapshot.getTotalOrderCount() + qty);
                                                        fbaSalesSnapshot.setTotalSale(fbaSalesSnapshot.getTotalSale() + itemSale);
                                                        orderDateItemIdFbaSaleSnapshotMap.get(date_key).put(itemid,fbaSalesSnapshot);
                                                }
                                                else{
                                                        FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
                                                        fbaSalesSnapshot.setTotalOrderCount(qty);
                                                        fbaSalesSnapshot.setTotalSale(itemSale);
                                                        if(itemDiscount!=0){
                                                                fbaSalesSnapshot.setPromotionOrderCount(qty);
                                                                fbaSalesSnapshot.setTotalPromotionSale(itemSale - itemDiscount);
                                                        }
                                                        else{
                                                                fbaSalesSnapshot.setPromotionOrderCount(0);
                                                                fbaSalesSnapshot.setTotalPromotionSale((float) 0);
                                                        }
                                                        orderDateItemIdFbaSaleSnapshotMap.get(date_key).put(itemid,fbaSalesSnapshot);
                                                }
                                        }
                                        else{
                                                Map<Long,FbaSalesSnapshot> ItemIdFbaSaleSnapshotMap = new HashMap<Long,FbaSalesSnapshot>();
                                                FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
                                                fbaSalesSnapshot.setTotalOrderCount(qty);
                                                fbaSalesSnapshot.setTotalSale(itemSale);
                                                if(itemDiscount!=0){
                                                        fbaSalesSnapshot.setTotalPromotionSale(itemSale - itemDiscount);
                                                        fbaSalesSnapshot.setPromotionOrderCount(qty);
                                                }
                                                else{
                                                        fbaSalesSnapshot.setTotalPromotionSale((float) 0);
                                                        fbaSalesSnapshot.setPromotionOrderCount(0);
                                                }
                                                ItemIdFbaSaleSnapshotMap.put(itemid,fbaSalesSnapshot);
                                                orderDateItemIdFbaSaleSnapshotMap.put(date_key,ItemIdFbaSaleSnapshotMap);
                                        }
                                } 
                                count++;
                        }
                        InventoryClient inventoryServiceClient = null;
                        TransactionClient transactionServiceClient = null;
                        CatalogClient catalogServiceClient = null;
                        try {
                                inventoryServiceClient = new InventoryClient();
                                transactionServiceClient = new TransactionClient();
                                //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.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
                        in.shop2020.model.v1.order.TransactionService.Client transactionClient   = transactionServiceClient.getClient();
                        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient   = catalogServiceClient.getClient();
                        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
                        Date date_today = dateFormat.parse(dateFormat.format(new Date()));
                        List<AmazonFbaInventorySnapshot> nonzeroFbaInventorySnapshotlist =  inventoryClient.getAllAmazonFbaItemInventory();
                        if(nonzeroFbaInventorySnapshotlist!=null){
                                for(AmazonFbaInventorySnapshot amazonFbaInventory:nonzeroFbaInventorySnapshotlist){
                                        if(orderDateItemIdFbaSaleSnapshotMap.containsKey(date_today) ){ 
                                                if(!orderDateItemIdFbaSaleSnapshotMap.get(date_today).containsKey(amazonFbaInventory.getItem_id())){
                                                        Map<Long,FbaSalesSnapshot> ItemIdFbaSaleSnapshotMap = new HashMap<Long,FbaSalesSnapshot>();
                                                        FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
                                                        fbaSalesSnapshot.setTotalOrderCount(0);
                                                        fbaSalesSnapshot.setPromotionOrderCount(0);
                                                        fbaSalesSnapshot.setTotalPromotionSale((float) 0);
                                                        fbaSalesSnapshot.setTotalSale((float) 0);
                                                        ItemIdFbaSaleSnapshotMap.put(amazonFbaInventory.getItem_id(),fbaSalesSnapshot);
                                                        orderDateItemIdFbaSaleSnapshotMap.get(date_today).put(amazonFbaInventory.getItem_id(),fbaSalesSnapshot);
                                                }

                                        }
                                        else{
                                                Map<Long,FbaSalesSnapshot> ItemIdFbaSaleSnapshotMap = new HashMap<Long,FbaSalesSnapshot>();
                                                FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
                                                fbaSalesSnapshot.setTotalOrderCount(0);
                                                fbaSalesSnapshot.setPromotionOrderCount(0);
                                                fbaSalesSnapshot.setTotalPromotionSale((float) 0);
                                                fbaSalesSnapshot.setTotalSale((float) 0);
                                                ItemIdFbaSaleSnapshotMap.put(amazonFbaInventory.getItem_id(),fbaSalesSnapshot);
                                                orderDateItemIdFbaSaleSnapshotMap.put(date_today,ItemIdFbaSaleSnapshotMap);
                                        }
                                }
                        }
                        else{
                                System.out.println("No inventory in FBA");
                        }
                        Map<Long,Double> itemIdSalePriceMap = new HashMap<Long,Double>();
                        Map<Long,Double> itemIdminFBAPriceMap = new HashMap<Long,Double>();
                        Map<Long,Double> itemIdminMFNPriceMap = new HashMap<Long,Double>();
                        count=1;
                        while ((nextLine = inventoryhealthreportreader.readNext()) != null ) {
                                if(count!=1){
                                        //System.out.println(nextLine[1] +" "+ nextLine[31] +" " + nextLine[32] + " " + nextLine[34]);
                                        Long item_id = Long.parseLong(nextLine[1].replaceAll("FBA",""));
                                        Double salePrice = Double.parseDouble(nextLine[31]);
                                        Double minFBAPrice = Double.parseDouble(nextLine[32]);
                                        Double minMFNPrice = Double.parseDouble(nextLine[34]);
                                        itemIdSalePriceMap.put(item_id,salePrice);
                                        itemIdminFBAPriceMap.put(item_id,minFBAPrice);
                                        itemIdminMFNPriceMap.put(item_id,minMFNPrice);
                                }
                                count++;
                        }
                        boolean oos;
                        for (Entry<Date, Map<Long, FbaSalesSnapshot>> entry : orderDateItemIdFbaSaleSnapshotMap.entrySet()){
                                Date orderDate = entry.getKey();
                                for(Entry<Long, FbaSalesSnapshot> entry1 :entry.getValue().entrySet()){
                                        System.out.println("Item ID is " + entry1.getKey());
                                        Long inventory = inventoryClient.getAmazonFbaItemInventory(entry1.getKey());

                                        if(inventory==0 && entry1.getValue().getTotalOrderCount()==0){
                                                oos=true;
                                        }
                                        else{
                                                oos=false;
                                        }
                                        Long item_id = entry1.getKey();
                                        System.out.println(orderDate +","+entry1.getKey()+","+entry1.getValue()+","+ inventory +","+ oos+","+itemIdSalePriceMap.get(item_id)+","+itemIdminFBAPriceMap.get(item_id)+","+itemIdminMFNPriceMap.get(item_id));
                                        AmazonFbaSalesSnapshot amazonfbasalessnapshot = new AmazonFbaSalesSnapshot();
                                        amazonfbasalessnapshot.setDateOfSale(orderDate.getTime());
                                        amazonfbasalessnapshot.setItem_id(entry1.getKey());
                                        amazonfbasalessnapshot.setTotalOrderCount(entry1.getValue().getPromotionOrderCount());
                                        amazonfbasalessnapshot.setTotalOrderCount(entry1.getValue().getTotalOrderCount());
                                        amazonfbasalessnapshot.setTotalSale(entry1.getValue().getTotalSale());
                                        amazonfbasalessnapshot.setPromotionSale(entry1.getValue().getTotalPromotionSale());
                                        amazonfbasalessnapshot.setIsOutOfStock(oos);
                                        Amazonlisted amazon_item=null;
                                        if(catalogClient.getAmazonItemDetails(item_id)!=null)
                                                amazon_item = catalogClient.getAmazonItemDetails(item_id);
                                        else 
                                                continue;
                                        if(itemIdSalePriceMap.containsKey(item_id) && itemIdSalePriceMap.get(item_id)!=0){
                                                amazonfbasalessnapshot.setSalePrice(itemIdSalePriceMap.get(item_id));
                                        }
                                        else{
                                                amazonfbasalessnapshot.setSalePrice(amazon_item.getFbaPrice());
                                        }
                                        if(itemIdminMFNPriceMap.containsKey(item_id) && itemIdminMFNPriceMap.get(item_id)!=0){
                                                amazonfbasalessnapshot.setMinFbaPrice(itemIdminMFNPriceMap.get(item_id));
                                        }
                                        else{
                                                amazonfbasalessnapshot.setMinFbaPrice(0.0);
                                        }
                                        if(itemIdminMFNPriceMap.containsKey(item_id) && itemIdminMFNPriceMap.get(item_id)!=0){
                                                amazonfbasalessnapshot.setMinMfnPrice(itemIdminMFNPriceMap.get(item_id));
                                        }
                                        else{
                                                amazonfbasalessnapshot.setMinMfnPrice(0.0);
                                        }
                                        amazonfbasalessnapshot.setAmazonFbaInventory(inventory);
                                        transactionClient.addOrUpdateAmazonFbaSalesSnapshot(amazonfbasalessnapshot);
                                }
                        }
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.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();
                }
        }

}