Subversion Repositories SmartDukaan

Rev

Rev 10533 | 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.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
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 {
        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")){
                                        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("Order Details " + nextLine[0]+" "+date_key+" "+ nextLine[11] +" " + nextLine[14] + " " + nextLine[4] + " " + nextLine[13] );
                                        Long itemid = Long.parseLong(nextLine[11].replaceAll("FBA",""));
                                        Integer qty=0;
                                        if(nextLine[14].length()!=0){
                                                qty = new Integer(nextLine[14]);
                                        }
                                        Float itemSale = null;
                                        if(nextLine[16].length()!=0){
                                                itemSale = new Float(nextLine[16]);
                                        }
                                        else{
                                                itemSale = (float) 0;
                                        }
                                        Float itemDiscount; 
                                        if(nextLine[22].length()!=0){
                                                itemDiscount = new Float(nextLine[22]);
                                        }
                                        else{
                                                itemDiscount = new Float(0);
                                        }
                                        if(nextLine[4].equalsIgnoreCase("Cancelled") || nextLine[13].equalsIgnoreCase("Cancelled")){
                                                itemSale = (float) 0; 
                                                itemDiscount = (float) 0;
                                                qty = 0;
                                        }
                                        System.out.println("Order Details after parsing " + nextLine[0]+" "+date_key+" "+ itemid +" " + qty);
                                        if(orderDateItemIdFbaSaleSnapshotMap.containsKey(date_key)){
                                                Map<Long,FbaSalesSnapshot> ItemIdFbaSaleSnapshotMap;
                                                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 - itemDiscount);
                                                        ItemIdFbaSaleSnapshotMap = orderDateItemIdFbaSaleSnapshotMap.get(date_key);
                                                        ItemIdFbaSaleSnapshotMap.put(itemid,fbaSalesSnapshot); 
                                                        orderDateItemIdFbaSaleSnapshotMap.put(date_key,ItemIdFbaSaleSnapshotMap);
                                                }
                                                else{
                                                        FbaSalesSnapshot fbaSalesSnapshot = new FbaSalesSnapshot();
                                                        fbaSalesSnapshot.setTotalOrderCount(qty);
                                                        fbaSalesSnapshot.setTotalSale(itemSale - itemDiscount);
                                                        if(itemDiscount!=0){
                                                                fbaSalesSnapshot.setPromotionOrderCount(qty);
                                                                fbaSalesSnapshot.setTotalPromotionSale(itemSale - itemDiscount);
                                                        }
                                                        else{
                                                                fbaSalesSnapshot.setPromotionOrderCount(0);
                                                                fbaSalesSnapshot.setTotalPromotionSale((float) 0);
                                                        }
                                                        ItemIdFbaSaleSnapshotMap = orderDateItemIdFbaSaleSnapshotMap.get(date_key);
                                                        ItemIdFbaSaleSnapshotMap.put(itemid,fbaSalesSnapshot); 
                                                        orderDateItemIdFbaSaleSnapshotMap.put(date_key,ItemIdFbaSaleSnapshotMap);
                                                }
                                        }
                                        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<Date> dates = new ArrayList<Date>();
                        Calendar cal = Calendar.getInstance();
                        cal.add(Calendar.DATE, -1);
                        Date date_end = dateFormat.parse(dateFormat.format(cal.getTime()));
                        cal.add(Calendar.DATE, -5);
                        Date date_start = dateFormat.parse(dateFormat.format(cal.getTime()));
                        //System.out.println("Start Date = " + date_start);
                        //System.out.println("End Date = " + date_end);
                        Date d = date_start;
                        while(!d.equals(date_end)){
                                cal.setTime(d);
                                cal.add(Calendar.DATE,1);
                                d = cal.getTime();
                                dates.add(d);
                        }
                        List<AmazonFbaInventorySnapshot> nonzeroFbaInventorySnapshotlist =  inventoryClient.getAllAmazonFbaItemInventory();
                        for(Date date:dates){
                                if(nonzeroFbaInventorySnapshotlist!=null){
                                        for(AmazonFbaInventorySnapshot amazonFbaInventory:nonzeroFbaInventorySnapshotlist){
                                                if(orderDateItemIdFbaSaleSnapshotMap.containsKey(date) ){ 
                                                        if(!orderDateItemIdFbaSaleSnapshotMap.get(date).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).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,ItemIdFbaSaleSnapshotMap);
                                                }
                                        }
                                }
                                else{
                                        System.out.println("No inventory in FBA");
                                }
                        }
                        Map<Long,PriceAtDate> itemIdOurPriceMap = new HashMap<Long,PriceAtDate>();
                        Map<Long,PriceAtDate> itemIdSalePriceMap = new HashMap<Long,PriceAtDate>();
                        Map<Long,PriceAtDate> itemIdminFBAPriceMap = new HashMap<Long,PriceAtDate>();
                        Map<Long,PriceAtDate> itemIdminMFNPriceMap = new HashMap<Long,PriceAtDate>();
                        count=1;
                        while ((nextLine = inventoryhealthreportreader.readNext()) != null ) {
                                try{
                                        if(count!=1){
                                                //System.out.println(nextLine[1] +" "+ nextLine[31] +" " + nextLine[32] + " " + nextLine[34]);
                                                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[0]);
                                                Long item_id = Long.parseLong(nextLine[1].replaceAll("FBA",""));
                                                Double ourPrice = null;
                                                Double minFBAPrice= null;
                                                Double minMFNPrice= null;
                                                Double salePrice= null; 
                                                if(nextLine[30].length() >0){
                                                        ourPrice = Double.parseDouble(nextLine[30]);
                                                        if(itemIdOurPriceMap.containsKey(item_id)){
                                                                if(itemIdOurPriceMap.get(item_id).getDate().getTime() < date.getTime()){
                                                                        PriceAtDate priceAtDate= new PriceAtDate();
                                                                        priceAtDate.setDate(date);
                                                                        priceAtDate.setPrice(ourPrice);
                                                                        itemIdOurPriceMap.put(item_id,priceAtDate);
                                                                }
                                                        }
                                                        else{   
                                                                PriceAtDate priceAtDate= new PriceAtDate();
                                                                priceAtDate.setDate(date);
                                                                priceAtDate.setPrice(ourPrice);
                                                                itemIdOurPriceMap.put(item_id,priceAtDate);
                                                        }
                                                }
                                                if(nextLine[31].length() >0){
                                                        salePrice = Double.parseDouble(nextLine[31]);
                                                        if(itemIdSalePriceMap.containsKey(item_id) ){
                                                                if(itemIdSalePriceMap.get(item_id).getDate().getTime() < date.getTime()){
                                                                        PriceAtDate priceAtDate= new PriceAtDate();
                                                                        priceAtDate.setDate(date);
                                                                        priceAtDate.setPrice(salePrice);
                                                                        itemIdSalePriceMap.put(item_id,priceAtDate);
                                                                }
                                                        }
                                                        else{   
                                                                PriceAtDate priceAtDate= new PriceAtDate();
                                                                priceAtDate.setDate(date);
                                                                priceAtDate.setPrice(salePrice);
                                                                itemIdSalePriceMap.put(item_id,priceAtDate);
                                                        }
                                                }
                                                if(nextLine[32].length() >0){
                                                        minFBAPrice = Double.parseDouble(nextLine[32]);
                                                        if(itemIdminFBAPriceMap.containsKey(item_id)){
                                                                if(itemIdminFBAPriceMap.get(item_id).getDate().getTime() < date.getTime()){
                                                                        PriceAtDate priceAtDate= new PriceAtDate();
                                                                        priceAtDate.setDate(date);
                                                                        priceAtDate.setPrice(minFBAPrice);
                                                                        itemIdminFBAPriceMap.put(item_id,priceAtDate);
                                                                }
                                                        }
                                                        else{   
                                                                PriceAtDate priceAtDate= new PriceAtDate();
                                                                priceAtDate.setDate(date);
                                                                priceAtDate.setPrice(minFBAPrice);
                                                                itemIdminFBAPriceMap.put(item_id,priceAtDate);
                                                        }
                                                }
                                                if(nextLine[34].length() >0){
                                                        minMFNPrice = Double.parseDouble(nextLine[34]);
                                                        if(itemIdminMFNPriceMap.containsKey(item_id)){
                                                                if(itemIdminMFNPriceMap.get(item_id).getDate().getTime() < date.getTime()){
                                                                        PriceAtDate priceAtDate= new PriceAtDate();
                                                                        priceAtDate.setDate(date);
                                                                        priceAtDate.setPrice(minMFNPrice);
                                                                        itemIdminMFNPriceMap.put(item_id,priceAtDate);
                                                                }
                                                        }
                                                        else{   
                                                                PriceAtDate priceAtDate= new PriceAtDate();
                                                                priceAtDate.setDate(date);
                                                                priceAtDate.setPrice(minMFNPrice);
                                                                itemIdminMFNPriceMap.put(item_id,priceAtDate);
                                                        }
                                                }
                                        }
                                }
                                catch(Exception e){
                                        e.printStackTrace();
                                }
                                count++;
                        }
                        boolean oos;
                        List<AmazonFbaSalesSnapshot> fbaSalesSnapShotList = new ArrayList<AmazonFbaSalesSnapshot>();
                        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 = 0l;

                                        if(inventory==0 && entry1.getValue().getTotalOrderCount()==0){
                                                oos=true;
                                        }
                                        else{
                                                oos=false;
                                        }
                                        Long item_id = entry1.getKey();
                                        Amazonlisted amazon_item=catalogClient.getAmazonItemDetails(item_id);
                                        if(amazon_item.getItemid()==0){
                                                continue;
                                        }
                                        //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);
                                        if(itemIdSalePriceMap.containsKey(item_id) && itemIdSalePriceMap.get(item_id).getPrice()!=0){
                                                amazonfbasalessnapshot.setSalePrice(itemIdSalePriceMap.get(item_id).getPrice());
                                                amazonfbasalessnapshot.setSalePriceSnapshotDate(itemIdSalePriceMap.get(item_id).getDate().getTime());
                                        }
                                        else{
                                                amazonfbasalessnapshot.setSalePrice(0.0);
                                                amazonfbasalessnapshot.setSalePriceSnapshotDate(0);
                                        }
                                        if(itemIdminMFNPriceMap.containsKey(item_id) && itemIdminMFNPriceMap.get(item_id).getPrice()!=0){
                                                amazonfbasalessnapshot.setMinMfnPrice(itemIdminMFNPriceMap.get(item_id).getPrice());
                                                amazonfbasalessnapshot.setMinMfnPriceSnapshotDate(itemIdminMFNPriceMap.get(item_id).getDate().getTime());
                                        }
                                        else{
                                                amazonfbasalessnapshot.setMinMfnPrice(0.0);
                                                amazonfbasalessnapshot.setMinMfnPriceSnapshotDate(0);
                                        }
                                        if(itemIdminFBAPriceMap.containsKey(item_id) && itemIdminFBAPriceMap.get(item_id).getPrice()!=0){
                                                amazonfbasalessnapshot.setMinFbaPrice(itemIdminFBAPriceMap.get(item_id).getPrice());
                                                amazonfbasalessnapshot.setMinFbaPriceSnapshotDate(itemIdminFBAPriceMap.get(item_id).getDate().getTime());
                                        }
                                        else{
                                                amazonfbasalessnapshot.setMinFbaPrice(0.0);
                                                amazonfbasalessnapshot.setMinFbaPriceSnapshotDate(0);
                                        }
                                        if(itemIdOurPriceMap.containsKey(item_id) && itemIdOurPriceMap.get(item_id).getPrice()!=0){
                                                amazonfbasalessnapshot.setOurPrice(itemIdOurPriceMap.get(item_id).getPrice());
                                                amazonfbasalessnapshot.setOurPriceSnapshotDate(itemIdOurPriceMap.get(item_id).getDate().getTime());
                                        }
                                        else{
                                                amazonfbasalessnapshot.setOurPrice(amazon_item.getFbaPrice());
                                                amazonfbasalessnapshot.setOurPriceSnapshotDate(0);
                                        }
                                        if(amazonfbasalessnapshot.getTotalOrderCount() > 0){
                                                Date date =  new Date(amazonfbasalessnapshot.getDateOfSale());
                                                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
                                                System.out.println(" Item details are " + sdf.format(date) + " "+ amazonfbasalessnapshot.getItem_id() + " " + amazonfbasalessnapshot.getTotalOrderCount());
                                        }
                                        amazonfbasalessnapshot.setAmazonFbaInventory(inventory);
                                        fbaSalesSnapShotList.add(amazonfbasalessnapshot);
                                }
                        }
                        transactionServiceClient.getClient().bulkAddOrUpdateAmazonFbaSalesSnapshot(fbaSalesSnapShotList);
                } 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();
                }
                catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

        }

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

        }

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

}