<?php session_name('CAKEPHP'); session_start();	
$PageSecurity = 1;
$tabindex=0;
//include('includes/DefinePaymentClass.php');

include('includes/SQL_CommonFunctions.inc');
include('CRM/functions.php');
$s=new db_query();
$deptid=  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'mst_dpt_heirarchy');
$LowerDeptList=GETLowerDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);
$lowerlist=  UserDeptList($LowerDeptList);
if(count($lowerlist)>0)
    $lowerlist=implode(",",$lowerlist).",".$_SESSION['UserID'];
else
    $lowerlist="'".$_SESSION['UserID']."'";

$appsql=DB_query("select * from perf_approval_list",$db);
$appnum=DB_num_rows($appsql);
?>
<div>
    <table width="100%" align="left">
        <tr>
            <th>Task No</th>
            <th>Task Date</th>
            <th colspan="<?php echo $appnum ?>">Task Approval</th>
        </tr>
        <tr>
            <th colspan="2"></th>
            <?php while($appres=DB_fetch_assoc($appsql))
            { ?>
            <th><?php echo $appres['approval'] ?></th>
            <?php } ?>
        </tr>
        <?php $tsksql=DB_query("Select * from perf_task_assign where CompanyId='".$_SESSION['ACGCompanyId']."' and approval_status!='1'",$db);  
        if(DB_num_rows($tsksql)!=0){
        while($tsk_res=DB_fetch_assoc($tsksql))
        { ?>
          <tr>
            <td><?php echo $tsk_res['doc_no']; ?></td>  
            <td><?php echo ConvertSqlDate($tsk_res['task_date']); ?></td>
            <?php            
            $mastsql=DB_query("select * from perf_approval_list",$db); 
            $i=0;
            while($mastres=DB_fetch_assoc($mastsql))
            {  
                $det_task=DB_query("select * from perf_task_assign_details where task_id='".$tsk_res['id']."' and status='close' ",$db);  $appdet=DB_query("select * from perf_taskwise_approve where task_id='".$tsk_res['id']."' and app_list_id='".$mastres['id']."' ",$db);
                $appby=explode(",",$mastres['approved_by']);
                if($mastres['seq_no']==1) {
                if(in_array($_SESSION['UserID'],$appby))
                { 
                    if(DB_num_rows($det_task)>DB_num_rows($appdet)) { ?>            
            <td>
            <a href="index.php?route=TaskMgt/perf_task_assign_app&modid=<?php echo $_REQUEST['modid'] ?>&frmid=<?php echo $_REQUEST['frmid'] ?>&tid=<?php echo $tsk_res['id'] ?>&appid=<?php echo $mastres['id'] ?>"><input type="button" value="Approve" ></a>
            </td>
                <?php }  else { ?>
                   <td><input type="button" value="Approved" disabled ></td>
<?php } }
                else{?>
            <td><input type="button" value="No Permission" disabled ></td>
                <?php     
                }
                }
          else { ?>
            <?php $prev_seq=$mastres['seq_no']-1; 
          $res=DB_fetch_assoc(DB_query("select * from perf_approval_list where seq_no='".$prev_seq."' ",$db));  $count=DB_query("select * from perf_taskwise_approve where task_id='".$tsk_res['id']."' and app_list_id='".$res['id']."' ",$db); 
            if(DB_num_rows($count)>0) {  $appby=explode(",",$mastres['approved_by']);  if(in_array($_SESSION['UserID'],$appby)) {   if(DB_num_rows($det_task)>DB_num_rows($appdet)) { ?>   <td> <a href="index.php?route=TaskMgt/perf_task_assign_app&modid=<?php echo $_REQUEST['modid'] ?>&frmid=<?php echo $_REQUEST['frmid'] ?>&tid=<?php echo $tsk_res['id'] ?>&appid=<?php echo $mastres['id'] ?>"><input type="button" value="Approve"></a></td>  <?php }else { ?> <td><input type="button" value="Approved" disabled></td> <?php  }  } else{ ?><td><input type="button" value="No Permission" disabled></td> <?php  }  }else{?><td><input type="button" value="<?php echo $prev_seq." approval not done"?>" disabled></td><?php } ?>
         <?php }
        $i++; }?>
        </tr>
 <?php }
        }else {?>
        <tr>
            <td colspan="<?php  echo $appnum+2; ?>"><strong>No Task to approved.</strong></td>
        </tr>
        <?php } ?>
    </table>    
</div>