<?php
session_start();
$companyname=$_SESSION['companyname'];//;
if(isset($_POST['filtebyemp'])){
    $adminemailid=$_POST['filtebyemp'];
}else{
    $adminemailid=$_SESSION['emailid'];
}
$companymailid=$_SESSION['companymailid'];
include('../Model/configuration.php');
require_once('../Control/sales.php');
$objsales=new salesctrl();
require_once('../Control/pipeline.php');
$objpipeline=new pipelinectrl();
$pipelinetype_res=$objsales->salesselectall($companyname, $companymailid, "sales_pipe_type", "no");
$i=0;
$pipe_name=array();
$pipe_percentage=array();
foreach($pipelinetype_res as $pipelinetype_row){
    $pipe_id[$i]=$pipelinetype_row['id'];
    $pipe_name[$i]=$pipelinetype_row['pipe_name'];
    $pipe_percentage[$i]=$pipelinetype_row['pipe_percentage'];
    //echo $pipelinetype_row['pipe_percentage'];
    $i++;
}

if(isset($_POST['expected_won'])){
    $startmonth=$_POST['expected_won'];
    for($iiiii=0;$iiiii<6;$iiiii++){
        $months= date('m', strtotime($startmonth.'+'.$iiiii.' month'));
        $year=date('Y', strtotime($startmonth.'+'.$iiiii.' month')); 
        $sqlans1='SELECT sum(d_amount) as total_amount FROM `sales_deal_pipeline` left join sales_pipeline_type on sales_pipeline_type.id=sales_deal_pipeline.d_stage WHERE sales_deal_pipeline.`companyname`="'.$companyname.'" and Month(d_date)="'.$months.'" and YEAR(`d_date`)="'.$year.'" and `d_stage`!="loss" and  `d_stage`="Won" and d_deal_by="'.$adminemailid.'"';
        $resultans1 = $pdo->query($sqlans1);
        $total_amount="";
        foreach($resultans1 as $rowans1){
            $total_amount=$rowans1['total_amount'];
        }
        $total_amount=round($total_amount);
    ?>
    
    <div class="col-md-2" style="color:black;cursor:pointer;padding-top:15px;height:50px;background-color:#fff;border-right:1px solid #ddd;">
                 <?php if($total_amount>0){ ?>
                <a style="color:black;font-size:14px;" data-toggle="modal" data-target="#myModal_view_pipeline_deal" onclick="show_pipeline_total_deal_month('<?php echo $companyname;?>','<?php echo $adminemailid;?>','<?php echo $months;?>','<?php echo $year;?>')"><center>&#8360&nbsp;<b><?php echo $total_amount; ?></b></center></a>
                <?php  } else { ?>
                <center><span style="color: #ccc;text-decoration: none;">&#8360&nbsp;0</span></center>
                <?php } ?>
    </div> 
    
    <?php
    }
}



if(isset($_POST['expected_won_pending'])){
    $startmonth=$_POST['expected_won_pending'];
    for($iiiii=0;$iiiii<6;$iiiii++){
        $months= date('m', strtotime($startmonth.'+'.$iiiii.' month'));
        $year=date('Y', strtotime($startmonth.'+'.$iiiii.' month')); 
        $sqlans1='SELECT sum( case when `d_stage`="Won" then `d_amount` else (d_amount * cast(REPLACE(sales_pipeline_type.pipe_percentage, "%", "") as UNSIGNED INTEGER) / 100) end) as total_amount FROM `sales_deal_pipeline` left join sales_pipeline_type on sales_pipeline_type.id=sales_deal_pipeline.d_stage WHERE sales_deal_pipeline.`companyname`="'.$companyname.'" and Month(d_date)="'.$months.'" and YEAR(`d_date`)="'.$year.'" and `d_stage`!="loss" and d_deal_by="'.$adminemailid.'"';
        $resultans1 = $pdo->query($sqlans1);
        $total_amount="";
        foreach($resultans1 as $rowans1){
            $total_amount=$rowans1['total_amount'];
        }
        $total_amount=round($total_amount);
    ?>
    
    <div class="col-md-2" style="color:black;cursor:pointer;padding-top:15px;height:50px;background-color:#fff;border-right:1px solid #ddd;">
                 <?php if($total_amount>0){ ?>
                <a style="color:black;font-size:14px;" data-toggle="modal" data-target="#myModal_view_pipeline_deal" onclick="show_pipeline_total_deal_month('<?php echo $companyname;?>','<?php echo $adminemailid;?>','<?php echo $months;?>','<?php echo $year;?>')"><center>&#8360&nbsp;<b><?php echo $total_amount; ?></b></center></a>
                <?php  } else { ?>
                <center><span style="color: #ccc;text-decoration: none;">&#8360&nbsp;0</span></center>
                <?php } ?>
    </div> 
    
    <?php
    }
}



if(isset($_POST['monthly_target'])){
    $startmonth=$_POST['monthly_target'];
    for($iiiii=0;$iiiii<6;$iiiii++){
        $months= date('m', strtotime($startmonth.'+'.$iiiii.' month'));
        $year=date('Y', strtotime($startmonth.'+'.$iiiii.' month')); 
        $sql21='SELECT count(*) as count1 FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and t_emp_id="'.$adminemailid.'"';
        $resultans21 = $pdo->query($sql21);
        foreach($resultans21 as $rowans21){
            $count1=$rowans21['count1'];
        }
        if(intval($count1)<1){
            for($rft=0;$rft<count($email_array);$rft++){
                $mail=$email_array[$rft];
                $sqlfun="select sales_generate_employee_target('".$companyname."','".$mail."','".$year."')";
                $resultansfun = $pdo->query($sqlfun);
            }
        }
        if(intval($months)==1){
            $sqlans1='SELECT sum(t_1) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and t_emp_id="'.$adminemailid.'"';
        }else if(intval($months)==2){
            $sqlans1='SELECT sum(t_2) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and t_emp_id="'.$adminemailid.'"';
        }else if(intval($months)==3){
            $sqlans1='SELECT sum(t_3) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and t_emp_id="'.$adminemailid.'"';
        }else if(intval($months)==4){
            $sqlans1='SELECT sum(t_4) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and t_emp_id="'.$adminemailid.'"';
        }else if(intval($months)==5){
            $sqlans1='SELECT sum(t_5) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and t_emp_id="'.$adminemailid.'"';
        }else if(intval($months)==6){
            $sqlans1='SELECT sum(t_6) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and t_emp_id="'.$adminemailid.'"';
        }else if(intval($months)==7){
            $sqlans1='SELECT sum(t_7) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and t_emp_id="'.$adminemailid.'"';
        }else if(intval($months)==8){
            $sqlans1='SELECT sum(t_8) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and t_emp_id="'.$adminemailid.'"';
        }else if(intval($months)==9){
            $sqlans1='SELECT sum(t_9) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and t_emp_id="'.$adminemailid.'"';
        }else if(intval($months)==10){
            $sqlans1='SELECT sum(t_10) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and t_emp_id="'.$adminemailid.'"';
        }else if(intval($months)==11){
            $sqlans1='SELECT sum(t_11) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and t_emp_id="'.$adminemailid.'"';
        }else{
            $sqlans1='SELECT sum(t_12) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and t_emp_id="'.$adminemailid.'"';
        } 
        $resultans1 = $pdo->query($sqlans1);
        $traget_amount="";
        foreach($resultans1 as $rowans1){
            $traget_amount=$rowans1['amount'];
        }
        $traget_amount=round($traget_amount);
    ?>
        <div class="col-md-2" style="color:black;cursor:pointer;padding-top:10px;height:40px;background-color:#fff;border-right:1px solid #ddd;">
            <span style="color:grey;font-size:14px;"><center>&#8360&nbsp;<b><?php echo $traget_amount;?></b></center></span>
        </div>
    
    <?php
    }
}


if(isset($_POST['monthly_body'])){
    $startdate=$_POST['monthly_body'];
    for($i1=0;$i1<count($pipe_percentage);$i1++){
            $j=100-($i1*10)."%";
            $stage=$pipe_id[$i1];
            $percentage=$pipe_percentage[$i1];
            $p_name=$pipe_name[$i1];
    ?>
                
        <div class="row" style="height:40px;">
            <div class="col-md-3" style="background-color:#f6f6f6;">
                <center><span id="pipeline1" style="width:<?php echo $j;?>"><span ><?php echo $percentage;?><br><span style="font-size:12px;"><?php echo $p_name;?></span></span></span></center>
            </div>
            <div class="col-md-9"  style="height:40px;">
                
                
                <?php
                
                for($iiiii=0;$iiiii<6;$iiiii++){
                    $months= date('m', strtotime($startdate.'+'.$iiiii.' month'));
                    $year=date('Y', strtotime($startdate.'+'.$iiiii.' month')); 
                    $sqlans1='SELECT sum( case when `d_stage`="Won" then `d_amount` else (d_amount * cast(REPLACE(sales_pipeline_type.pipe_percentage, "%", "") as UNSIGNED INTEGER) / 100) end) as total_amount FROM `sales_deal_pipeline` left join sales_pipeline_type on sales_pipeline_type.id=sales_deal_pipeline.d_stage WHERE sales_deal_pipeline.`companyname`="'.$companyname.'" and Month(d_date)="'.$months.'" and YEAR(`d_date`)="'.$year.'" and `d_stage`!="loss" and `d_stage`="'.$stage.'" and d_deal_by="'.$adminemailid.'"';
                    $resultans1 = $pdo->query($sqlans1);
                    $total_amount="";
                    foreach($resultans1 as $rowans1){
                        $total_amount=$rowans1['total_amount'];
                    }
                    $total_amount=round($total_amount);
                ?>
                
                <div class="col-md-2" style="color:black;cursor:pointer;padding-top:15px;height:50px;background-color:#fff;border-right:1px solid #ddd;">
                             <?php if($total_amount>0){ ?>
                            <a style="color:black;font-size:14px;" data-toggle="modal" data-target="#myModal_view_pipeline_deal" onclick="show_pipeline_deal_stage_month('<?php echo $companyname;?>','<?php echo $adminemailid;?>','<?php echo $months;?>','<?php echo $year;?>','<?php echo $stage;?>')"><center>&#8360&nbsp;<b><?php echo $total_amount; ?></b></center></a>
                            <?php  } else { ?>
                            <center><span style="color: #ccc;text-decoration: none;">&#8360&nbsp;0</span></center>
                            <?php } ?>
                </div> 
                
                <?php
                }
                ?>
            </div>
        </div>
        <br>
                
<?php
    }
}


if(isset($_POST['monthly_tot_pending'])){
    $startdate=$_POST['monthly_tot_pending'];
    for($iiiii=0;$iiiii<6;$iiiii++){
        $months= date('m', strtotime($startdate.'+'.$iiiii.' month'));
        $year=date('Y', strtotime($startdate.'+'.$iiiii.' month')); 
        $sqlans1='SELECT sum( case when `d_stage`="Won" then `d_amount` else (d_amount * cast(REPLACE(sales_pipeline_type.pipe_percentage, "%", "") as UNSIGNED INTEGER) / 100) end) as total_amount FROM `sales_deal_pipeline` left join sales_pipeline_type on sales_pipeline_type.id=sales_deal_pipeline.d_stage WHERE sales_deal_pipeline.`companyname`="'.$companyname.'" and Month(d_date)="'.$months.'" and YEAR(`d_date`)="'.$year.'" and `d_stage`!="loss" and `d_stage`!="won" and d_deal_by="'.$adminemailid.'"';
        $resultans1 = $pdo->query($sqlans1);
        $total_amount="";
        foreach($resultans1 as $rowans1){
            $total_amount=$rowans1['total_amount'];
        }
        $total_amount=round($total_amount);
    ?>
    
    <div class="col-md-2" style="color:black;cursor:pointer;padding-top:15px;height:50px;background-color:#fff;border-right:1px solid #ddd;">
        <?php if($total_amount>0){ ?>
        <a style="color:#db1b56;font-size:14px;"><center>&#8360&nbsp;<b><?php echo $total_amount; ?></b></center></a>
        <?php  } else { ?>
        <center><span style="color: #db1b56;text-decoration: none;">&#8360&nbsp;0</span></center>
        <?php } ?>
    </div> 
    
    <?php
    }
    ?>
                
<?php
}
?>