<?php
session_start();
$companyname=$_SESSION['companyname'];
$adminemailid=$_SESSION['emailid'];
if(($adminemailid !="")&&($companyname!="")){
$companytype=$_SESSION['companytype'];
if(isset($_POST['type1'])){
    $type=$_POST['type1'];
    $filter_is=$_POST['type2'];
    $filter_select=$_POST['type3'];
    $filter_name=$_POST['type4'];
    //Date Created, Call Date, Deal create Date, Deal Close Date, Status, Tag, Owner, Lead Source
    if($type=="Date Created"){
        if($companytype=="admin"){
            $sql='SELECT `c_companyname`, `c_emailid`, `c_id`, `c_title`, `c_fname`, `c_lname`, `c_star`, `c_jobtitle`, `c_a_line`, `c_a_city`, `c_a_state`, `c_a_country`, `c_a_pincode`, `c_background`, `c_owner_id`, `c_owner_name`, `c_private`, `c_dob`, `c_dtime`, `c_create_by`, `c_create_type`, `o_id`, `o_name`, `o_desc`, `o_phone`, `o_url`, `o_line`, `o_city`, `o_state`, `o_country`, `o_pincode`, `o_create_by`, `o_create_type`, `s_name`, `s_desc`, `s_color`, `t_name`, `t_desc`, `l_name`, `status_id`, `tag_id`, `lead_id`, `c_code` FROM `sales_contact_organization_status_tag_leadsource` WHERE date(`c_dtime`)="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and sales_contact_organization_status_tag_leadsource.c_delete="no"';
        }else{
            $sql='SELECT `c_companyname`, `c_emailid`, `c_id`, `c_title`, `c_fname`, `c_lname`, `c_star`, `c_jobtitle`, `c_a_line`, `c_a_city`, `c_a_state`, `c_a_country`, `c_a_pincode`, `c_background`, `c_owner_id`, `c_owner_name`, `c_private`, `c_dob`, `c_dtime`, `c_create_by`, `c_create_type`, `o_id`, `o_name`, `o_desc`, `o_phone`, `o_url`, `o_line`, `o_city`, `o_state`, `o_country`, `o_pincode`, `o_create_by`, `o_create_type`, `s_name`, `s_desc`, `s_color`, `t_name`, `t_desc`, `l_name`, `status_id`, `tag_id`, `lead_id`, `c_code` FROM `sales_contact_organization_status_tag_leadsource` WHERE date(`c_dtime`)="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and `c_emailid`="'.$adminemailid.'" and sales_contact_organization_status_tag_leadsource.c_delete="no"';
        }
    }else if($type=="Call Date"){
        if($companytype=="admin"){
            $sql='SELECT `c_companyname`, `c_emailid`, `c_id`, `c_title`, `c_fname`, `c_lname`, `c_star`, `c_jobtitle`, `c_a_line`, `c_a_city`, `c_a_state`, `c_a_country`, `c_a_pincode`, `c_background`, `c_owner_id`, `c_owner_name`, `c_private`, `c_dob`, `c_dtime`, `c_create_by`, `c_create_type`, `o_id`, `o_name`, `o_desc`, `o_phone`, `o_url`, `o_line`, `o_city`, `o_state`, `o_country`, `o_pincode`, `o_create_by`, `o_create_type`, `s_name`, `s_desc`, `s_color`, `t_name`, `t_desc`, `l_name`, `status_id`, `tag_id`, `lead_id`, `c_code` FROM `sales_contact_organization_status_tag_leadsource` INNER join sales_call on sales_contact_organization_status_tag_leadsource.c_id=sales_call.cont_id and sales_call.c_date="'.$filter_select.'" and sales_contact_organization_status_tag_leadsource.c_companyname="'.$companyname.'" and sales_contact_organization_status_tag_leadsource.c_delete="no" and sales_call.c_delete="no"';
        }else{
            $sql='SELECT * FROM `SELECT `c_companyname`, `c_emailid`, `c_id`, `c_title`, `c_fname`, `c_lname`, `c_star`, `c_jobtitle`, `c_a_line`, `c_a_city`, `c_a_state`, `c_a_country`, `c_a_pincode`, `c_background`, `c_owner_id`, `c_owner_name`, `c_private`, `c_dob`, `c_dtime`, `c_create_by`, `c_create_type`, `o_id`, `o_name`, `o_desc`, `o_phone`, `o_url`, `o_line`, `o_city`, `o_state`, `o_country`, `o_pincode`, `o_create_by`, `o_create_type`, `s_name`, `s_desc`, `s_color`, `t_name`, `t_desc`, `l_name`, `status_id`, `tag_id`, `lead_id`, `c_code` FROM `sales_contact_organization_status_tag_leadsource` INNER join sales_call on sales_contact_organization_status_tag_leadsource.c_id=sales_call.cont_id and sales_call.c_date="'.$filter_select.'" and sales_contact_organization_status_tag_leadsource.c_companyname="'.$companyname.'" and sales_contact_organization_status_tag_leadsource.c_delete="no" and sales_call.c_delete="no" and `c_emailid`="'.$adminemailid.'" ';
        }
    }else if($type=="Deal create Date"){
        if($companytype=="admin"){
            $sql='SELECT `c_companyname`, `c_emailid`, `c_id`, `c_title`, `c_fname`, `c_lname`, `c_star`, `c_jobtitle`, `c_a_line`, `c_a_city`, `c_a_state`, `c_a_country`, `c_a_pincode`, `c_background`, `c_owner_id`, `c_owner_name`, `c_private`, `c_dob`, `c_dtime`, `c_create_by`, `c_create_type`, `o_id`, `o_name`, `o_desc`, `o_phone`, `o_url`, `o_line`, `o_city`, `o_state`, `o_country`, `o_pincode`, `o_create_by`, `o_create_type`, `s_name`, `s_desc`, `s_color`, `t_name`, `t_desc`, `l_name`, `status_id`, `tag_id`, `lead_id`, `c_code` FROM `sales_contact_organization_status_tag_leadsource` INNER join sales_deal on sales_contact_organization_status_tag_leadsource.c_id=sales_deal.cont_id and sales_deal.d_date ="'.$filter_select.'" and sales_contact_organization_status_tag_leadsource.c_companyname="'.$adminemailid.'" and sales_contact_organization_status_tag_leadsource.c_delete="no" and sales_deal.d_delete="no"';
        }else{
            $sql='SELECT `c_companyname`, `c_emailid`, `c_id`, `c_title`, `c_fname`, `c_lname`, `c_star`, `c_jobtitle`, `c_a_line`, `c_a_city`, `c_a_state`, `c_a_country`, `c_a_pincode`, `c_background`, `c_owner_id`, `c_owner_name`, `c_private`, `c_dob`, `c_dtime`, `c_create_by`, `c_create_type`, `o_id`, `o_name`, `o_desc`, `o_phone`, `o_url`, `o_line`, `o_city`, `o_state`, `o_country`, `o_pincode`, `o_create_by`, `o_create_type`, `s_name`, `s_desc`, `s_color`, `t_name`, `t_desc`, `l_name`, `status_id`, `tag_id`, `lead_id`, `c_code` FROM `sales_contact_organization_status_tag_leadsource` INNER join sales_deal on sales_contact_organization_status_tag_leadsource.c_id=sales_deal.cont_id and sales_deal.d_date ="'.$filter_select.'" and sales_contact_organization_status_tag_leadsource.c_companyname="'.$adminemailid.'" and sales_contact_organization_status_tag_leadsource.c_delete="no" and sales_deal.d_delete="no" and `c_emailid`="'.$adminemailid.'" ';
        }
    }else if($type=="Deal Close Date"){
        if($companytype=="admin"){
            $sql='SELECT `c_companyname`, `c_emailid`, `c_id`, `c_title`, `c_fname`, `c_lname`, `c_star`, `c_jobtitle`, `c_a_line`, `c_a_city`, `c_a_state`, `c_a_country`, `c_a_pincode`, `c_background`, `c_owner_id`, `c_owner_name`, `c_private`, `c_dob`, `c_dtime`, `c_create_by`, `c_create_type`, `o_id`, `o_name`, `o_desc`, `o_phone`, `o_url`, `o_line`, `o_city`, `o_state`, `o_country`, `o_pincode`, `o_create_by`, `o_create_type`, `s_name`, `s_desc`, `s_color`, `t_name`, `t_desc`, `l_name`, `status_id`, `tag_id`, `lead_id`, `c_code` FROM `sales_contact_organization_status_tag_leadsource` INNER join sales_deal on sales_contact_organization_status_tag_leadsource.c_id=sales_deal.cont_id and sales_deal.d_closedate ="'.$filter_select.'" and sales_contact_organization_status_tag_leadsource.c_companyname="'.$adminemailid.'" and sales_contact_organization_status_tag_leadsource.c_delete="no" and sales_deal.d_delete="no" ';
        }else{
            $sql='SELECT `c_companyname`, `c_emailid`, `c_id`, `c_title`, `c_fname`, `c_lname`, `c_star`, `c_jobtitle`, `c_a_line`, `c_a_city`, `c_a_state`, `c_a_country`, `c_a_pincode`, `c_background`, `c_owner_id`, `c_owner_name`, `c_private`, `c_dob`, `c_dtime`, `c_create_by`, `c_create_type`, `o_id`, `o_name`, `o_desc`, `o_phone`, `o_url`, `o_line`, `o_city`, `o_state`, `o_country`, `o_pincode`, `o_create_by`, `o_create_type`, `s_name`, `s_desc`, `s_color`, `t_name`, `t_desc`, `l_name`, `status_id`, `tag_id`, `lead_id`, `c_code` FROM `sales_contact_organization_status_tag_leadsource` INNER join sales_deal on sales_contact_organization_status_tag_leadsource.c_id=sales_deal.cont_id and sales_deal.d_closedate ="'.$filter_select.'" and sales_contact_organization_status_tag_leadsource.c_companyname="'.$adminemailid.'" and sales_contact_organization_status_tag_leadsource.c_delete="no" and sales_deal.d_delete="no" and `c_emailid`="'.$adminemailid.'" ';
        }
    }else if($type=="Status"){
        if($filter_is=="is"){
            if($companytype=="admin"){
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `status_id`="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }else{
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `status_id`="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and `c_emailid`="'.$adminemailid.'"  and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }
        }else{
            if($companytype=="admin"){
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `status_id`!="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }else{
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `status_id`!="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and `c_emailid`="'.$adminemailid.'"  and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }
        }
    }else if($type=="Tag"){
        if($filter_is=="is"){
            if($companytype=="admin"){
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `tag_id`="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }else{
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `tag_id`="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and `c_emailid`="'.$adminemailid.'"  and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }
        }else{
            if($companytype=="admin"){
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `tag_id`!="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }else{
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `tag_id`!="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and `c_emailid`="'.$adminemailid.'"  and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }
        }
    }else if($type=="Owner"){
        if($filter_is=="is"){
            if($companytype=="admin"){
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `c_owner_id`="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }else{
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `c_owner_id`="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and `c_emailid`="'.$adminemailid.'"  and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }
        }else{
            if($companytype=="admin"){
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `c_owner_id`!="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }else{
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `c_owner_id`!="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and `c_emailid`="'.$adminemailid.'"  and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }
        }
    }else if($type=="Lead Source"){
        if($filter_is=="is"){
            if($companytype=="admin"){
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `lead_id`="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }else{
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `lead_id`="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and `c_emailid`="'.$adminemailid.'"  and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }
        }else{
            if($companytype=="admin"){
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `lead_id`!="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }else{
                $sql='SELECT * FROM `sales_contact_organization_status_tag_leadsource` WHERE `lead_id`!="'.$filter_select.'" and `c_companyname`="'.$companyname.'" and `c_emailid`="'.$adminemailid.'"  and sales_contact_organization_status_tag_leadsource.c_delete="no"';
            }
        }
    }
    
    
require_once('../Control/filter.php');
$objfilter=new filterctrl();
date_default_timezone_set("Asia/Kolkata");
$contdatetime=date("Y-m-d H:i:s");
//echo $sql;
$result=$objfilter->salesfilterinsert($filter_name, addslashes($sql), $adminemailid, $companytype, $contdatetime, $companyname);

}
?>
<?php
}else{
echo "<script> location.replace('http://fieldcloud.in/Login.php') </script>";
}?>