<?php
$last=date('t',strtotime($this->input->post('p')));
         $colawal=$wk=$week=$wktarget=$wkpersen=$dynamic=0;
         $default=$this->db->query("SELECT SUM(target) as target FROM sv_customer_plan_default WHERE sales='$sales' AND activity='$activity'")->row_array();  
         
?>

<body>
<table width="100%" border="1" cellspacing="0" cellpadding="10">
  <tbody>
    <tr>
        <td colspan="<?php echo $last+1?>" align="center"><h3><?php echo GetMonthFull(substr($this->input->post('p'),5,2)).' - '.substr($this->input->post('p'),0,4);?></h3> </td>
    </tr>
    <tr>
      <td align="center">TGL</td>
    <?php for($a=1;$a<=$last;$a++){ 
        $hari=date('D',strtotime(''.$this->input->post('p').'-'.$a));
        $color='';
        if($hari=='Sun' || $hari=='Sat'){
        $color='background-color:red;color:white;text-height:bold;';}
        ?>
      <td align="center" style="<?php echo $color?>" ><?php echo $a.'<br/> '.$hari ?></td>
    <?php }?>
    </tr>
    <tr>
      <td align="center">ACTIVITY</td>
    <?php
        for($a=1;$a<=$last;$a++){
        $tanggal=$tahun.'-'.$bulan.'-'.$a;
        $hariaktivity=date('D',strtotime(''.$this->input->post('p').'-'.$a));
        $akt=gethasilsummary($sales,$activity,$tanggal);
        if($a==1 && $hariaktivity!='Sun' && $hariaktivity!='Sat'){
            $week++;
        }
        elseif($hariaktivity=='Mon' ){
            $week++;
        }
        
           $aksum[$week]+=$akt; 
        ?>
      <td align="center"><?php echo $akt ?></td>
    <?php }?>
    </tr>
    <?php ?>
    <tr>
      <td align="center">ACTIVITY SUMMARY</td>
    <?php for($c=1;$c<=$last;$c++){
        $cols='';
        $hari=date('D',strtotime(''.$this->input->post('p').'-'.$c));
        if($hari!='Sat' && $hari!='Sun'){$cols='border-style: solid;border-width: 1px 0px 1px 0px;';}?>
     <!-- <td align="center" style="<?php echo $cols?>"><?php echo $target;?></td> -->
     <?php
     if($c==1 && $hari!='Sun' && $hari!='Sat'){
         $wk++;
        $color='background-color:green;color:white;text-height:bold;';
         for($d=1;$d<=7;$d++){
            $hariawal=date('D',strtotime(''.$this->input->post('p').'-'.$d));
             if($hariawal!='Sat'){
                 $colawal+=1;
             }else{ break;}
         }
         
         ?>
      <td align="center" colspan="<?php echo $colawal?>" style="<?php echo $color?>" ><?php echo $aksum[$wk]?></td>
     <?php }
     elseif($hari=='Mon'){
         $wk++;
        $color='background-color:green;color:white;text-height:bold;';
     ?>
      <td align="center" colspan="5" style="<?php echo $color?>"  ><?php echo $aksum[$wk]?></td>
     <?php }elseif($hari=='Sat' || $hari=='Sun'){?>
      <td align="center" ></td>
     <?php }?>
    <?php }?>
    </tr>
    <tr>
      <td align="center">TARGET</td>
    <?php
    
         $colawal=0;
    for($a=1;$a<=$last;$a++){
        $cols='';
        $datez=$this->input->post('p').'-'.$a;
        $hari=date('D',strtotime(''.$datez));
        if($hari!='Sat' && $hari!='Sun'){$cols='border-style: solid;border-width: 1px 0px 1px 0px;';}?>
     <!-- <td align="center" style="<?php echo $cols?>"><?php echo $target;?></td> -->
     <?php if($hari=='Mon'){
        $wktarget++; 
        $color='background-color:yellow;text-height:bold;';
        $dynamic=$this->db->query("SELECT target FROM sv_customer_plan WHERE sales='$sales' AND activity='$activity' AND `from`='$datez'")->row_array();  
         $target=$default['target'];
         $today=strtotime(''.$this->input->post('p').'-'.$a);
         $endmonth=strtotime(date('Y-m-t',strtotime(''.$this->input->post('p').'-01')));
         $jarak=round(round(($endmonth-$today)/(60*60*24)));
         if($jarak<5){
             $target=round(floor(($jarak+1)/5*$default['target']));
         }
         
         $target=($dynamic['target']>0 ? $dynamic['target'] : $target);
         $targetnya[$wktarget]=$target;
     ?>
      <td align="center" colspan="5" style="<?php echo $color?>"  ><?php echo $target?></td>
     <?php }
    
     else if($a==1 && $hari!='Sun' && $hari!='Sat'){
        $wktarget++; 
        $color='background-color:yellow;text-height:bold;';
        $dynamic=$this->db->query("SELECT target FROM sv_customer_plan WHERE sales='$sales' AND activity='$activity' AND `from`='$datez'")->row_array(); 
        //echo $this->db->last_query();
         for($b=1;$b<=7;$b++){
            $hariawal=date('D',strtotime(''.$this->input->post('p').'-'.$b));
             if($hariawal!='Sat'){
                 $colawal+=1;
             }else{ break;}
         }
         $target=round(floor($colawal/5*$default['target']));
         $target=($dynamic['target']>0 ? $dynamic['target'] : $target);
         $targetnya[$wktarget]=$target;
         ?>
      <td align="center" colspan="<?php echo $colawal?>" style="<?php echo $color?>" ><?php echo $target ?></td>
     <?php }
         
         elseif($hari=='Sat' || $hari=='Sun'){?>
      <td align="center" ></td>
     <?php }?>
    <?php }?>
    </tr>
    <?php 
    
    
    ?>
    <tr>
      <td align="center">% Weekly</td>
    <?php
    
         $colawal=0;
    for($a=1;$a<=$last;$a++){
        $cols='';
        $hari=date('D',strtotime(''.$this->input->post('p').'-'.$a));
        if($hari!='Sat' && $hari!='Sun'){$cols='border-style: solid;border-width: 1px 0px 1px 0px;';}?>
     <!-- <td align="center" style="<?php echo $cols?>"><?php echo $target;?></td> -->
     <?php if($hari=='Mon'){
        $wkpersen++; 
        $color='background-color:blue;color:white;text-height:bold;';
        
         $target[$week]=$default['target'];
     ?>
      <td align="center" colspan="5" style="<?php echo $color?>"  ><?php echo round($aksum[$wkpersen]/$targetnya[$wkpersen]*100)?>%</td>
     <?php }
    
     else if($a==1 && $hari!='Sun' && $hari!='Sat'){
        $wkpersen++;  
        $color='background-color:blue;color:white;text-height:bold;';
         for($b=1;$b<=7;$b++){
            $hariawal=date('D',strtotime(''.$this->input->post('p').'-'.$b));
             if($hariawal!='Sat'){
                 $colawal+=1;
             }else{ break;}
         }
         $target[$week]=$default['target'];
         ?>
      <td align="center" colspan="<?php echo $colawal?>" style="<?php echo $color?>" ><?php echo round($aksum[$wkpersen]/$targetnya[$wkpersen]*100)?>% </td>
     <?php }
         
         elseif($hari=='Sat' || $hari=='Sun'){?>
      <td align="center" ></td>
     <?php }?>
    <?php }?>
    </tr>
    <?php
    $monthly=round(array_sum($aksum)/array_sum($targetnya)*100);
    ?>
     <tr>
      <td align="center">% Monthly</td>
      <td align="center" colspan="<?php echo $last?>" style="<?php echo $color?>" ><?php echo $monthly?>%</td>
    </tr>
  </tbody>
</table>
</body>
</html>