Subversion Repositories SmartDukaan

Rev

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

package in.shop2020;

import in.shop2020.model.v1.catalog.FlipkartItem;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.catalog.SnapdealItemDetails;
import in.shop2020.model.v1.inventory.InventoryType;
import in.shop2020.model.v1.inventory.ItemInventory;
import in.shop2020.model.v1.inventory.Vendor;
import in.shop2020.model.v1.inventory.Warehouse;
import in.shop2020.model.v1.inventory.WarehouseLocation;
import in.shop2020.model.v1.inventory.WarehouseType;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.InventoryClient;
import in.shop2020.thrift.clients.TransactionClient;
import in.shop2020.utils.GmailUtils;
//import inventory.Inventory;
import inventory.FlipkartInventoryItem;
import inventory.NlcLeftOutInventory;
//import inventory.SnapdealItemForInventory;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.mail.MessagingException;

import org.apache.commons.lang.StringUtils;
import org.apache.http.Header;
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.http.params.HttpParams;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;


public class NlcLeftOutInventoryDetailScript{

        //private static ArrayList<Inventory> currentInventoryItemList;
        private static List<SnapdealItemDetails> snapdealItems;
        private static List<FlipkartItem> flipkartItems;
        private static List<FlipkartItem> flipkartInventoryItems = new ArrayList<FlipkartItem>();
        static Map<Long,SnapdealItemDetails> snapdealItemMap = new HashMap<Long,SnapdealItemDetails>();
        static Map<Long,FlipkartItem> flipkartItemMap = new HashMap<Long,FlipkartItem>();
        //private static Map<Long, SnapdealItemForInventory> allItemsInventoryMap = new HashMap<Long, SnapdealItemForInventory>();
        private static Map<Long, String> vendorDataMap = new HashMap<Long, String>();
        //static java.text.SimpleDateFormat sdf;
        static String emailFromAddress;
        static String password;
        static GmailUtils mailer;
        static String sendTo[];
        private static Map<String, Map<Long, NlcLeftOutInventory>> nlcLoiItemsMapSD = new HashMap<String, Map<Long, NlcLeftOutInventory>>();
        private static Map<String, Map<Long, NlcLeftOutInventory>> nlcLoiItemsMapFK = new HashMap<String, Map<Long, NlcLeftOutInventory>>();

        static{
                emailFromAddress = "build-staging@shop2020.in";
                password = "shop2020";
                mailer = new GmailUtils();
                sendTo = new String[]{"vikram.raghav@shop2020.in", "manish.sharma@shop2020.in"};
                /*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","manisha.sharma@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in"};*/
                CatalogClient catalogServiceClient = null;
                try {
                        catalogServiceClient = new CatalogClient();
                } catch (TTransportException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }
                in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();

                try {
                        snapdealItems = catalogClient.getAllSnapdealItems();
                        flipkartItems = catalogClient.getAllFlipkartItems();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                for(FlipkartItem flipkartItem:flipkartItems){
                        if(flipkartItem.isIsListedOnFlipkart() && !flipkartItem.isSuppressInventoryFeed())
                                flipkartInventoryItems.add(flipkartItem);
                }

                for(FlipkartItem flipkartItem:flipkartItems){
                        if(flipkartItem.isIsListedOnFlipkart() && !flipkartItem.isSuppressInventoryFeed()){
                                flipkartItemMap.put(flipkartItem.getItem_id(), flipkartItem);
                        }
                }

                for(SnapdealItemDetails snapdealItem:snapdealItems){
                        snapdealItemMap.put(snapdealItem.getItem_id(),snapdealItem);
                }

                if(flipkartItemMap.size()==0){
                        String text = "";
                        try {
                                mailer.sendSSLMessage(sendTo,"No items to check not sent Inventory Data ", emailFromAddress, password, text);
                                System.exit(0);
                        } catch (MessagingException e) {
                                e.printStackTrace();
                        }
                }

                if(snapdealItemMap.size()==0){
                        String text = "";
                        try {
                                mailer.sendSSLMessage(sendTo,"No items to check not sent Inventory Data ", emailFromAddress, password, text);
                                System.exit(0);
                        } catch (MessagingException e) {
                                e.printStackTrace();
                        }
                }
        }

        private static void calculateinventory(){
                CatalogClient catalogServiceClient = null;
                Map<Long,ItemInventory> availability= new HashMap<Long,ItemInventory>();
                try {
                        catalogServiceClient = new CatalogClient();
                } catch (TTransportException e) {
                        e.printStackTrace();
                }
                in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
                try {
                        List<Item> aliveItems = catalogClient.getAllAliveItems();
                        Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>(); 
                        for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
                                aliveItemsMap.put(thriftItem.getId(), thriftItem);

                        }
                        InventoryClient inventoryServiceClient = new InventoryClient();
                        try {
                                inventoryServiceClient = new InventoryClient();
                        } catch (TTransportException e) {
                                e.printStackTrace();
                        }
                        in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
                        List<Vendor> allVendors = inventoryClient.getAllVendors();
                        for(Vendor v: allVendors){
                                vendorDataMap.put(v.getId(), v.getName());
                        }
                        List<in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems> ignoreItems = inventoryClient.getIgnoredWarehouseidsAndItemids();

                        availability = inventoryClient.getInventorySnapshot(0);
                        List<Warehouse> allwarehouses = inventoryClient.getWarehouses(null,null,0,0,0);
                        Map<Long,Warehouse> allWarehousesMap = new HashMap<Long,Warehouse>();
                        for(Warehouse warehouse:allwarehouses){
                                allWarehousesMap.put(warehouse.getId(),warehouse);
                        }
                        List<Warehouse> warehouses = inventoryClient.getWarehouses(WarehouseType.OURS_THIRDPARTY,null,0,0,0);
                        List<Warehouse> ours_warehouses = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, 0, 0, 0);
                        List<Long> thirdpartywarehouseids = new ArrayList<Long>();
                        List<Long> oursGoodWarehouse = new ArrayList<Long>();
                        for(Warehouse warehouse:warehouses){
                                thirdpartywarehouseids.add(warehouse.getId());
                        }
                        for (Warehouse warehouse:ours_warehouses){
                                oursGoodWarehouse.add(warehouse.getId());
                        }
                        /*long available=0;
                        long reserve=0;
                        long total_warehouse_held=0;
                        long heldForSource=0;
                        long total_held=0;*/
                        double nlc=0;
                        double maxnlcSD=0;
                        double maxnlcFK=0;

                        Map<String, Map<Long, NlcLeftOutInventory>> snapdealLeftInvDataMap = new HashMap<String, Map<Long, NlcLeftOutInventory>>();
                        Map<String, Map<Long, NlcLeftOutInventory>> flipkartLeftInvDataMap = new HashMap<String, Map<Long, NlcLeftOutInventory>>();


                        for(Item thriftItem : aliveItems){
                                SnapdealItemDetails snapdealItem = null;
                                FlipkartItem flipkartItem = null;
                                NlcLeftOutInventory nlcLOISD;
                                NlcLeftOutInventory nlcLOIFK;
                                maxnlcSD=0;
                                maxnlcFK=0;
                                Map<Long, NlcLeftOutInventory> nlcLoiMapSD = new HashMap<Long, NlcLeftOutInventory>();
                                Map<Long, NlcLeftOutInventory> nlcLoiMapFK = new HashMap<Long, NlcLeftOutInventory>();
                                Set<NlcLeftOutInventory> nlcLoiSetSD = new HashSet<NlcLeftOutInventory>();
                                Set<NlcLeftOutInventory> nlcLoiSetFK = new HashSet<NlcLeftOutInventory>();
                                if(snapdealItemMap.containsKey(thriftItem.getId())){
                                        snapdealItem = snapdealItemMap.get(thriftItem.getId());
                                        maxnlcSD = snapdealItem.getMaxNlc();
                                }
                                if(flipkartItemMap.containsKey(thriftItem.getId())){
                                        flipkartItem = flipkartItemMap.get(thriftItem.getId());
                                        maxnlcFK = flipkartItem.getMaxNlc();
                                }
                                nlc=0;
                                List<Warehouse> vendor_warehouses=null; 
                                if(availability.get(thriftItem.getId())!=null){ 
                                        ItemInventory iteminventory = availability.get(thriftItem.getId());
                                        Map<Long,Long> itemavailability = new HashMap<Long,Long>();
                                        itemavailability = iteminventory.getAvailability();
                                        if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
                                                //System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating availability)"+thriftItem.getPreferredVendor());                                         
                                                vendor_warehouses = inventoryClient.getWarehouses(WarehouseType.THIRD_PARTY,InventoryType.GOOD , thriftItem.getPreferredVendor(), 0, 0);
                                                vendor_warehouses.addAll(ours_warehouses);
                                                for (Warehouse warehouse:vendor_warehouses){
                                                        if(warehouse.getBillingWarehouseId()!=7){
                                                                if(warehouse.getLogisticsLocation().equals(WarehouseLocation.Delhi) && warehouse.getWarehouseType().equals(WarehouseType.THIRD_PARTY) && warehouse.getVendor().getId()!=1){

                                                                }
                                                                else{
                                                                        continue;
                                                                }
                                                        }
                                                        in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
                                                        ignoredItem.setItemId(thriftItem.getId());
                                                        ignoredItem.setWarehouseId(warehouse.getId());
                                                        if (itemavailability.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(ignoredItem)){
                                                                try{
                                                                        nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());

                                                                }
                                                                catch(TTransportException e){
                                                                        inventoryClient = inventoryServiceClient.getClient(); 
                                                                        nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
                                                                }

                                                                //maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();

                                                                if(snapdealItem!=null){
                                                                        if(nlc !=0 && (maxnlcSD >= nlc)){
                                                                                /*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
                                                                                heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 7);
                                                                                available = available + itemavailability.get(warehouse.getId());
                                                                                total_held = total_held + total_warehouse_held;*/ 
                                                                                //continue;
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
                                                                        }
                                                                        else if(maxnlcSD==0){
                                                                                /*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
                                                                                heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 7);
                                                                                available = available + itemavailability.get(warehouse.getId());
                                                                                total_held = total_held + total_warehouse_held;*/
                                                                                //continue;
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
                                                                        }
                                                                        else{
                                                                                long available = itemavailability.get(warehouse.getId());
                                                                                if(available>0){
                                                                                        nlcLOISD= new NlcLeftOutInventory();
                                                                                        nlcLOISD.setItemId(snapdealItem.getItem_id());
                                                                                        nlcLOISD.setProductName(snapdealItem.getBrand()+" " +snapdealItem.getModel_name() + " "+ snapdealItem.getModel_number() + " " +  snapdealItem.getColor());
                                                                                        nlcLOISD.setOurNlc(maxnlcSD);
                                                                                        nlcLOISD.setSkuAtSource(snapdealItem.getSkuAtSnapdeal());
                                                                                        nlcLOISD.setVendorName(warehouse.getVendor().getName());
                                                                                        nlcLOISD.setVendorAvailability(available);
                                                                                        nlcLOISD.setVendorNlc(nlc);
                                                                                        nlcLOISD.setVendorWarehouseId(warehouse.getId());
                                                                                        nlcLOISD.setWarehouseType(warehouse.getWarehouseType().toString());
                                                                                        if(!nlcLoiMapSD.containsKey(warehouse.getId()))
                                                                                                nlcLoiMapSD.put(warehouse.getId(), nlcLOISD);

                                                                                }
                                                                        }
                                                                }

                                                                if(flipkartItem!=null){
                                                                        if(nlc !=0 && (maxnlcFK >= nlc)){
                                                                                /*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
                                                                                heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 8);
                                                                                available = available + itemavailability.get(warehouse.getId());
                                                                                total_held = total_held + total_warehouse_held; */
                                                                                //continue;
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
                                                                        }
                                                                        else if(maxnlcFK==0){
                                                                                /*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
                                                                                heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 8);
                                                                                available = available + itemavailability.get(warehouse.getId());
                                                                                total_held = total_held + total_warehouse_held;*/
                                                                                //continue;
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
                                                                        }
                                                                        else{
                                                                                long available = itemavailability.get(warehouse.getId());
                                                                                if(available>0){
                                                                                        nlcLOIFK = new NlcLeftOutInventory();
                                                                                        nlcLOIFK.setItemId(flipkartItem.getItem_id());
                                                                                        nlcLOIFK.setProductName(thriftItem.getBrand()+" " +thriftItem.getModelName() + " "+ thriftItem.getModelNumber() + " " +  thriftItem.getColor());
                                                                                        nlcLOIFK.setOurNlc(maxnlcFK);
                                                                                        nlcLOIFK.setSkuAtSource(flipkartItem.getSkuAtFlipkart());
                                                                                        nlcLOIFK.setVendorName(warehouse.getVendor().getName());
                                                                                        nlcLOIFK.setVendorAvailability(available);
                                                                                        nlcLOIFK.setVendorNlc(nlc);
                                                                                        nlcLOIFK.setVendorWarehouseId(warehouse.getId());
                                                                                        nlcLOIFK.setWarehouseType(warehouse.getWarehouseType().toString());
                                                                                        if(!nlcLoiMapFK.containsKey(warehouse.getId()))
                                                                                                nlcLoiMapFK.put(warehouse.getId(), nlcLOIFK);
                                                                                }
                                                                        }
                                                                }
                                                                //System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + warehouse.getId() );

                                                        }
                                                }
                                                if(nlcLoiMapSD!=null && nlcLoiMapSD.size()>0){
                                                        snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiMapSD);
                                                }
                                                if(nlcLoiMapFK!=null && nlcLoiMapFK.size()>0){
                                                        flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiMapFK);
                                                }
                                        }
                                        else{
                                                for (Map.Entry<Long,Long> entry :  itemavailability.entrySet()) {
                                                        if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
                                                                if(allWarehousesMap.get(entry.getKey()).getLogisticsLocation().equals(WarehouseLocation.Delhi) && allWarehousesMap.get(entry.getKey()).getWarehouseType().equals(WarehouseType.THIRD_PARTY) && allWarehousesMap.get(entry.getKey()).getVendor().getId()!=1){

                                                                }
                                                                else{
                                                                        continue;
                                                                }
                                                        }
                                                        in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
                                                        ignoredItem.setItemId(thriftItem.getId());
                                                        ignoredItem.setWarehouseId(entry.getKey());
                                                        if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){

                                                                nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());

                                                                //System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + entry.getKey() );
                                                                if(snapdealItem!=null){
                                                                        if(nlc !=0 && (maxnlcSD >= nlc)){
                                                                                /*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
                                                                                heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 7);
                                                                                available =  available + entry.getValue();
                                                                                total_held = total_held + total_warehouse_held;*/
                                                                                //continue;
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
                                                                        }
                                                                        else if(maxnlcSD==0){
                                                                                /*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
                                                                                heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 7);
                                                                                available = available + itemavailability.get(entry.getKey());
                                                                                total_held = total_held + total_warehouse_held;*/

                                                                                //continue;
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
                                                                        }
                                                                        else{
                                                                                long available = itemavailability.get(entry.getKey());
                                                                                if(available>0){
                                                                                        nlcLOISD = new NlcLeftOutInventory();
                                                                                        nlcLOISD.setItemId(snapdealItem.getItem_id());
                                                                                        nlcLOISD.setProductName(snapdealItem.getBrand()+" " +snapdealItem.getModel_name() + " "+ snapdealItem.getModel_number() + " " +  snapdealItem.getColor());
                                                                                        nlcLOISD.setOurNlc(maxnlcSD);
                                                                                        nlcLOISD.setSkuAtSource(snapdealItem.getSkuAtSnapdeal());
                                                                                        nlcLOISD.setVendorName(vendorDataMap.get(allWarehousesMap.get(entry.getKey()).getVendor().getId()));
                                                                                        nlcLOISD.setVendorAvailability(available);
                                                                                        nlcLOISD.setVendorNlc(nlc);
                                                                                        nlcLOISD.setVendorWarehouseId(entry.getKey());
                                                                                        nlcLOISD.setWarehouseType(allWarehousesMap.get(entry.getKey()).getWarehouseType().toString());
                                                                                        if(!nlcLoiMapSD.containsKey(entry.getKey()))
                                                                                                nlcLoiMapSD.put(entry.getKey(), nlcLOISD);
                                                                                }
                                                                        }
                                                                }

                                                                if(flipkartItem!=null){
                                                                        if(nlc !=0 && (maxnlcFK >= nlc)){
                                                                                /*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
                                                                                heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 8);
                                                                                available =  available + entry.getValue();
                                                                                total_held = total_held + total_warehouse_held;*/
                                                                                //continue;
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
                                                                        }
                                                                        else if(maxnlcFK==0){
                                                                                /*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
                                                                                heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 8);
                                                                                available = available + itemavailability.get(entry.getKey());
                                                                                total_held = total_held + total_warehouse_held;*/
                                                                                //continue;
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
                                                                        }
                                                                        else{
                                                                                long available = itemavailability.get(entry.getKey());
                                                                                if(available>0){
                                                                                        nlcLOIFK = new NlcLeftOutInventory();
                                                                                        nlcLOIFK.setItemId(flipkartItem.getItem_id());
                                                                                        nlcLOIFK.setProductName(thriftItem.getBrand()+" " +thriftItem.getModelName() + " "+ thriftItem.getModelNumber() + " " +  thriftItem.getColor());
                                                                                        nlcLOIFK.setOurNlc(maxnlcFK);
                                                                                        nlcLOIFK.setSkuAtSource(flipkartItem.getSkuAtFlipkart());
                                                                                        nlcLOIFK.setVendorName(vendorDataMap.get(allWarehousesMap.get(entry.getKey()).getVendor().getId()));
                                                                                        nlcLOIFK.setVendorAvailability(available);
                                                                                        nlcLOIFK.setVendorNlc(nlc);
                                                                                        nlcLOIFK.setVendorWarehouseId(entry.getKey());
                                                                                        nlcLOIFK.setWarehouseType(allWarehousesMap.get(entry.getKey()).getWarehouseType().toString());
                                                                                        if(!nlcLoiMapFK.containsKey(entry.getKey()))
                                                                                                nlcLoiMapFK.put(entry.getKey(), nlcLOIFK);
                                                                                }
                                                                        }
                                                                }


                                                        }
                                                }
                                                if(nlcLoiMapSD!=null && nlcLoiMapSD.size()>0){
                                                        snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiMapSD);
                                                }
                                                if(nlcLoiMapFK!=null && nlcLoiMapFK.size()>0){
                                                        flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiMapFK);
                                                }
                                        }

                                        Map<Long,Long> itemreserve = new HashMap<Long,Long>();
                                        itemreserve = iteminventory.getReserved();
                                        if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
                                                //System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating Reserve)"+thriftItem.getPreferredVendor());
                                                for (Warehouse warehouse:vendor_warehouses){
                                                        if(warehouse.getBillingWarehouseId()!=7){
                                                                if(warehouse.getLogisticsLocation().equals(WarehouseLocation.Delhi) && warehouse.getWarehouseType().equals(WarehouseType.THIRD_PARTY) && warehouse.getVendor().getId()!=1){

                                                                }
                                                                else{
                                                                        continue;
                                                                }
                                                        }
                                                        in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
                                                        ignoredItem.setItemId(thriftItem.getId());
                                                        ignoredItem.setWarehouseId(warehouse.getId());
                                                        if (itemreserve.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(warehouse.getId())){
                                                                nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
                                                                //maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
                                                                //System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + warehouse.getId() );
                                                                if(snapdealItem!=null){
                                                                        if(nlc !=0 && (maxnlcSD >= nlc)){
                                                                                //reserve = reserve + itemreserve.get(warehouse.getId());
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
                                                                                //continue;
                                                                        }
                                                                        else if(maxnlcSD==0){
                                                                                //reserve = reserve + itemreserve.get(warehouse.getId());
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
                                                                                //continue;
                                                                        }
                                                                        else{
                                                                                if(snapdealLeftInvDataMap.containsKey(snapdealItem.getSkuAtSnapdeal())){
                                                                                        Map<Long, NlcLeftOutInventory> newNlcObjMap = snapdealLeftInvDataMap.get(snapdealItem.getSkuAtSnapdeal());
                                                                                        if(newNlcObjMap.containsKey(warehouse.getId())){
                                                                                                NlcLeftOutInventory newObj = newNlcObjMap.get(warehouse.getId());
                                                                                                newObj.setReserved(itemreserve.get(warehouse.getId()));
                                                                                                newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(warehouse.getId()));
                                                                                                newNlcObjMap.remove(warehouse.getId());
                                                                                                newNlcObjMap.put(warehouse.getId(), newObj);

                                                                                        }
                                                                                        nlcLoiMapSD = newNlcObjMap;
                                                                                }
                                                                        }
                                                                }

                                                                if(flipkartItem!=null){
                                                                        if(nlc !=0 && (maxnlcFK >= nlc)){
                                                                                //reserve = reserve + itemreserve.get(warehouse.getId());
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
                                                                                //continue;
                                                                        }
                                                                        else if(maxnlcFK==0){
                                                                                //reserve = reserve + itemreserve.get(warehouse.getId());
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
                                                                                //continue;
                                                                        }
                                                                        else{
                                                                                if(flipkartLeftInvDataMap.containsKey(flipkartItem.getSkuAtFlipkart())){
                                                                                        Map<Long, NlcLeftOutInventory> newNlcObjMap = flipkartLeftInvDataMap.get(flipkartItem.getSkuAtFlipkart());
                                                                                        if(newNlcObjMap.containsKey(warehouse.getId())){
                                                                                                NlcLeftOutInventory newObj = newNlcObjMap.get(warehouse.getId());
                                                                                                newObj.setReserved(itemreserve.get(warehouse.getId()));
                                                                                                newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(warehouse.getId()));
                                                                                                newNlcObjMap.remove(warehouse.getId());
                                                                                                newNlcObjMap.put(warehouse.getId(), newObj);

                                                                                        }
                                                                                        nlcLoiMapFK = newNlcObjMap;
                                                                                }
                                                                        }
                                                                }


                                                        }
                                                }

                                                if(nlcLoiMapSD!=null && nlcLoiMapSD.size()>0){
                                                        snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiMapSD);
                                                }
                                                if(nlcLoiMapFK!=null && nlcLoiMapFK.size()>0){
                                                        flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiMapFK);
                                                }

                                        }else{
                                                for (Map.Entry<Long,Long> entry : itemreserve.entrySet()) {
                                                        if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
                                                                if(allWarehousesMap.get(entry.getKey()).getLogisticsLocation().equals(WarehouseLocation.Delhi) && allWarehousesMap.get(entry.getKey()).getWarehouseType().equals(WarehouseType.THIRD_PARTY) && allWarehousesMap.get(entry.getKey()).getVendor().getId()!=1){

                                                                }
                                                                else{
                                                                        continue;
                                                                }
                                                        }
                                                        in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
                                                        ignoredItem.setItemId(thriftItem.getId());
                                                        ignoredItem.setWarehouseId(entry.getKey());
                                                        if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
                                                                nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
                                                                //System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + entry.getKey() );
                                                                if(snapdealItem!=null){
                                                                        if(nlc !=0 && (maxnlcSD >= nlc)){
                                                                                //reserve =  reserve + entry.getValue();
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
                                                                                //continue;
                                                                        }
                                                                        else if(maxnlcSD==0){
                                                                                //reserve =  reserve + entry.getValue();
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
                                                                                //continue;
                                                                        }
                                                                        else{
                                                                                if(snapdealLeftInvDataMap.containsKey(snapdealItem.getSkuAtSnapdeal())){
                                                                                        Map<Long, NlcLeftOutInventory> newNlcObjMap = snapdealLeftInvDataMap.get(snapdealItem.getSkuAtSnapdeal());
                                                                                        if(newNlcObjMap.containsKey(entry.getKey())){
                                                                                                NlcLeftOutInventory newObj = newNlcObjMap.get(entry.getKey());
                                                                                                newObj.setReserved(itemreserve.get(entry.getKey()));
                                                                                                newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(entry.getKey()));
                                                                                                newNlcObjMap.remove(entry.getKey());
                                                                                                newNlcObjMap.put(entry.getKey(), newObj);

                                                                                        }
                                                                                        nlcLoiMapSD = newNlcObjMap;
                                                                                }
                                                                        }
                                                                }
                                                                if(flipkartItem!=null){
                                                                        if(nlc !=0 && (maxnlcFK >= nlc)){
                                                                                //reserve =  reserve + entry.getValue();
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
                                                                                //continue;
                                                                        }
                                                                        else if(maxnlcFK==0){
                                                                                //reserve =  reserve + entry.getValue();
                                                                                //System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
                                                                                //continue;
                                                                        }
                                                                        else{
                                                                                if(flipkartLeftInvDataMap.containsKey(flipkartItem.getSkuAtFlipkart())){
                                                                                        Map<Long, NlcLeftOutInventory> newNlcObjMap = flipkartLeftInvDataMap.get(flipkartItem.getSkuAtFlipkart());
                                                                                        if(newNlcObjMap.containsKey(entry.getKey())){
                                                                                                NlcLeftOutInventory newObj = newNlcObjMap.get(entry.getKey());
                                                                                                newObj.setReserved(itemreserve.get(entry.getKey()));
                                                                                                newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(entry.getKey()));
                                                                                                newNlcObjMap.remove(entry.getKey());
                                                                                                newNlcObjMap.put(entry.getKey(), newObj);

                                                                                        }
                                                                                        nlcLoiMapFK = newNlcObjMap;
                                                                                }
                                                                        }
                                                                }

                                                        }

                                                        else{
                                                                //System.out.println("skipping inventory for warehouse id " + entry.getKey());
                                                        }
                                                }

                                                if(nlcLoiMapSD!=null && nlcLoiMapSD.size()>0){
                                                        snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiMapSD);
                                                }
                                                if(nlcLoiMapFK!=null && nlcLoiMapFK.size()>0){
                                                        flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiMapFK);
                                                }
                                        }


                                }

                                nlcLoiItemsMapSD = snapdealLeftInvDataMap;
                                nlcLoiItemsMapFK = flipkartLeftInvDataMap;
                                /*for (Map.Entry<String,Map<Long, NlcLeftOutInventory>> entry : snapdealLeftInvDataMap.entrySet()){
                                        for(NlcLeftOutInventory nlcLOI : entry.getValue().values()){
                                                nlcLeftOutInventoryItemsSD.add(nlcLOI);
                                        }
                                }
                                for (Map.Entry<String,Map<Long, NlcLeftOutInventory>> entry : flipkartLeftInvDataMap.entrySet()){
                                        for(NlcLeftOutInventory nlcLOI : entry.getValue().values()){
                                                nlcLeftOutInventoryItemsFK.add(nlcLOI);
                                        }
                                }*/

                        }
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

        }

        private static void createAndSendLeftOutInventoryFile(Map<String, Map<Long, NlcLeftOutInventory>> nlcLeftoutInventoryItems, String fileName){
                try {
                        File file = new File("/tmp/" + fileName +"-leftOutInv.xls");
                        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
                        bufferedWriter.write(StringUtils.join(new String[] {
                                        "ItemId", 
                                        "Product Name",
                                        "SkuAtSource",
                                        "Vendor Name",
                                        "Vendor Warehouse Id",
                                        "Warehouse Type",
                                        "Max Our NLC",
                                        "Vendor NLC",
                                        "Availability Count",
                                        "Reserved Count",
                                        "Net Availabilty"
                        }, '\t'));

                        //System.out.println(fileName+ " Main Map Size "+nlcLeftoutInventoryItems.size());
                        
                        for(Map<Long, NlcLeftOutInventory> nlcLOImap : nlcLeftoutInventoryItems.values()){
                                //System.out.println("Inner Map Size "+nlcLOImap.size());
                                for(NlcLeftOutInventory nlcLOI : nlcLOImap.values()){
                                        bufferedWriter.newLine();

                                        bufferedWriter.write(StringUtils.join(
                                                        new String[] {
                                                                        String.valueOf(nlcLOI.getItemId()),
                                                                        nlcLOI.getProductName(),
                                                                        nlcLOI.getSkuAtSource(),
                                                                        nlcLOI.getVendorName(),
                                                                        String.valueOf(nlcLOI.getVendorWarehouseId()),
                                                                        nlcLOI.getWarehouseType(),
                                                                        String.valueOf(nlcLOI.getOurNlc()),
                                                                        String.valueOf(nlcLOI.getVendorNlc()),
                                                                        String.valueOf(nlcLOI.getVendorAvailability()),
                                                                        String.valueOf(nlcLOI.getReserved()),
                                                                        String.valueOf(nlcLOI.getTotalAvailability())}, '\t'));

                                }
                        }
                        bufferedWriter.close();

                        List<File> files = new ArrayList<File>();
                        files.add(file);

                        mailer.sendSSLMessage(sendTo, "Inventory not Sent due to NLC Limitaion in case of - "+ fileName , "Please find attached file.", emailFromAddress, password, files);
                } catch (Exception e) {
                        e.printStackTrace();
                        try {
                                mailer.sendSSLMessage(sendTo,"Error while creating file from data ", emailFromAddress, password, "");
                        } catch (MessagingException e1) {
                                e1.printStackTrace();
                        }
                }
        }

        //private static DefaultHttpClient client = new DefaultHttpClient();

        public static void main(String[] args) throws Exception {
                calculateinventory();
                createAndSendLeftOutInventoryFile(nlcLoiItemsMapSD, "SNAPDEAL");
                createAndSendLeftOutInventoryFile(nlcLoiItemsMapFK, "FLIPKART");


                /*HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/keymaker/login");
                HttpGet get = new HttpGet("http://seller.snapdeal.com/pricing/search?gridType=normal&_search=false&nd=1392188583467&rows=30&page=1&sidx=&sord=asc");
                BufferedReader rd= null;
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("username",
                "khushal.bhatia@saholic.com"));
                nameValuePairs.add(new BasicNameValuePair("password",
                "sonline"));
                nameValuePairs.add(new BasicNameValuePair("lt",
                "LT-14717-oFCLyRTRMWfMDeczgXm0aaufUjfwPV"));
                nameValuePairs.add(new BasicNameValuePair("execution",
                "e1s1"));
                nameValuePairs.add(new BasicNameValuePair("_eventId",
                "submit"));
                nameValuePairs.add(new BasicNameValuePair("submit",
                "LOGIN"));
                //post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));

                HttpResponse response = null;
                try {
                        response = client.execute(post);
                } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

                try {
                        rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                } catch (IllegalStateException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }
                String line = "";
                try {
                        while ((line = rd.readLine()) != null) {
                                System.out.println(line);
                        }
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

                for(Header h: response.getAllHeaders()){
                        System.out.println(h.getName()+" "+h.getValue());
                }


                try {
                        response = client.execute(get);
                } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

                try {
                        rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                } catch (IllegalStateException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }

                System.out.println("Get Response for Get Request");

                try {
                        while ((line = rd.readLine()) != null) {
                                System.out.println(line);
                        }
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }*/

        }
}