<?php
ob_start();
session_name('CAKEPHP'); session_start();
error_reporting(FALSE);

	$PageSecurity = 3;
	include ('includes/session.inc');
	include ('includes/function_lib.php');
	include('includes/fun-php.php');
	include('includes/fun-HRClass.php');

 	global $db;?>
<table width="100%" border="1" cellpadding="1" cellspacing="1" style="border-collapse:collapse">
    <tr>
        <th>S.No</th>
        <th>NAME OF MEMBERS</th>
        <th>ASSIGNED TASK</th>
        <th>TARGET DATE</th>
        <th colspan="3">% of Completion</th> 
    </tr>
    <tr>
        <th>&nbsp;</th>
         <th>&nbsp;</th>
         <th>&nbsp;</th>
         <th>&nbsp;</th>
        <th>Int.</th>
         <th>S.Int.</th>
          <th>Ext.</th>
    </tr>
<?php 
/*$tsksql=DB_query("SELECT COUNT(t.user_id) AS cnt,t.user_id FROM perf_task_assign t WHERE  
 CompanyId='".$_SESSION['ACGCompanyId']."' GROUP BY t.user_id HAVING COUNT(t.user_id)>=1 ",$db);*/
$tsksql=DB_query("SELECT t.user_id FROM perf_task_assign t,perf_task_assign_details td  WHERE CompanyId='".$_SESSION['ACGCompanyId']."' and  t.id=td.task_id AND td.status='close' GROUP BY t.user_id ",$db);
$usrnum=DB_num_rows($tsksql);
if(DB_num_rows($tsksql))
{
 $i=1;
 $tot_int=0;
 while($tskres=DB_fetch_assoc($tsksql))
    { ?>
    <tr>
        <td style="text-align: center"><?php echo $i; ?></td>
        <td style="text-align: center"><?php if($tskres['user_id']!=0){ echo $s->GetFieldValue('www_users'," id='".$tskres['user_id']."' " ,'realname'); } else echo "Team"; ?></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        
    </tr> 
    <?php // echo "select * from perf_task_assign t, perf_task_assign_details td where t.id=td.task_id AND user_id='".$tskres['user_id']."'  " ;$docsql=DB_query("select * from perf_task_assign where user_id='".$tskres['user_id']."' ",$db);
    
$docsql=DB_query("SELECT t.doc_no,t.id FROM perf_task_assign t, perf_task_assign_details td WHERE t.id=td.task_id AND td.status='close' AND t.user_id='".$tskres['user_id']."' GROUP BY t.id ",$db);
        while($dbres=DB_fetch_assoc($docsql))
        { $int=0;$s_int=0;$ext=0;?>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td style="text-align:center"><strong><?php echo $dbres['doc_no'] ?></strong></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
     <?php $task_det=DB_query("SELECT t.doc_no,td.* FROM perf_task_assign t,perf_task_assign_details td WHERE
 t.id=td.task_id AND td.task_id='".$dbres['id']."' AND td.status='close' AND t.CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
           while($task_res=DB_fetch_assoc($task_det)){
               ?>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td style="text-align:left"><?php echo $task_res['task_description']; ?></td>
        <td style="text-align:left"><?php echo ConvertSqlDate($task_res['end_date']); ?></td>
        <td style="text-align:left"><?php echo $task_res['int_com']; $int+= $task_res['int_com']; ?></td>
        <td style="text-align:left"><?php echo $task_res['semi_com']; $s_int+=$task_res['semi_com']; ?></td>
        <td style="text-align:left"><?php echo $task_res['ext_com']; $ext+=$task_res['ext_com'];?></td>
    </tr>
        <?php } ?>
    <tr>
        <td colspan="4" style="text-align:right;padding-right:10px"><strong>Total</strong></td>
        <td><?php $count=DB_num_rows($task_det); $int=$int/$count; $tot_int+=round($int,2); echo round($int,2)."%" ?></td>
        <td><?php $semi=$s_int/$count; echo round($semi,2)."%" ?></td>
        <td><?php $ext=$ext/$count; echo round($ext,2)."%" ?></td>
    </tr>
        
        <?php    } $i++;  } ?> 
    <tr>
        <td colspan="4" style="text-align:right;padding-right:10px"><strong>Overall departmental % of Completion </strong></td>
        <td><?php echo round($tot_int/$usrnum,2)."%" ?></td>
        <td></td>
        <td></td>
    </tr>
    <?php }else{ ?>
    <tr>
        <td colspan="7">No Result Found.</td>
    </tr>
<?php }  ?>
</table>
<?php 
date_default_timezone_set('Asia/Kolkata');
$filename = "taskreport".date('y-m-d h:i:s'); 
header("Content-Type: application/x-msdownload");	
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
?>
