<?php

$div=divisinya();
$anaknya=  anaknya(); 
$sales=$this->input->post('sales');
            if($sales!=NULL){
                $cust="AND sales='$sales'";
                $custb="AND sales_id='$sales'";
            }else{
                $cust='';
                $custb='';
            }
            $aktf=" AND b.status='Aktif'";
            $aktfb=" AND b.status='Aktif'";
            
$month=  str_replace('Actual','',$this->input->post('m'));
$year=$this->input->post('year');
$q="SELECT a.id id,b.nama_lengkap nama_lengkap, DATE(a.created_on) created_on,a.pelanggan pelanggan,a.sales sales,a.nilai_po nilai_po,a.catatan catatan FROM sv_activity_sales a LEFT JOIN sv_master_sales b ON a.sales=b.id WHERE  (a.`jenis_aktivitas`='24' OR `a`.`jenis_aktivitas`='25' OR `a`.`jenis_aktivitas`='37' ) AND MONTH(a.created_on)='$month' AND YEAR(a.created_on)='$year' AND a.nilai_po>0 $aktf $cust";
            if(!diaadmin()){$q.=" AND (b.leader IN (".implode(',',$anaknya).") OR a.created_by ='".$this->session->userdata('webmaster_id')."')";}
            if($this->session->userdata('divisi')){
                $q.=" AND b.divisi='".$this->session->userdata('divisi')."'";
            }
            if($this->session->userdata('groups')){
                $q.=" AND b.groups='".$this->session->userdata('groups')."'";
            }
            $q.=" ORDER BY a.created_on,b.nama_lengkap ASC";
$zzz=$this->db->query($q)->result_array();
?>

<table border="1" cellpadding="4" style="width:100%; padding:5px;">
    <tr  style="background-color: #E2574C!important; color:white!important;">
        <th align="center" style="padding-left:5px;text-align: center;" colspan="7">PO <?php echo GetMonthFull($month).'  '.$year?></th>
    </tr>
    <tr  style="background-color: #E2574C!important; color:white!important;">
        <th align="center" width="2%" style="padding-left:5px">No</th>
        <th align="center" width="8%" style="padding-left:5px">Tanggal</th>
        <th align="center" width="12%" style="padding-left:5px">Customer</th>
        <th align="center" width="10%" style="padding-left:5px">Sales</th>
        <th align="center" width="12%" style="padding-left:5px">Produk</th>
        <th align="center" width="44%" style="padding-left:5px">Catatan</th>
        <th align="center" width="12%" style="padding-left:5px">Nilai PO (IDR)</th>
    </tr>
    <?php
    $total=0;
    $no=1;
    foreach($zzz as $z){
       $total+= $z['nilai_po'];
        $produk=GetAll('sv_activity_sales_detail',array('activity'=>'where/'.$z['id']))->result_array();
        $dijual=array();
        foreach($produk as $pr){
            $dijual[]=GetValue('title','master_produk',array('id'=>'where/'.$pr['produk']));
        }
        $kejual=implode(',',$dijual);
        ?>
    <tr style="background-color: #A9FF96!important;cursor:pointer;">
        <td style="padding-left:5px;padding-right:7px"><?php echo $no;?></td>
        <td style="padding-left:5px;padding-right:7px"><?php echo $z['created_on']?></td>
        <td style="padding-left:5px;padding-right:7px"><?php echo GetValue('title','master_pelanggan',array('id'=>'where/'.$z['pelanggan']))?></td>
        <td style="padding-left:5px;padding-right:7px"><?php echo $z['nama_lengkap']?></td>
        <td style="padding-left:5px;padding-right:7px"><?php echo $kejual?></td>
        <td style="padding-left:5px;padding-right:7px"><?php echo $z['catatan']?></td>
        <td align="right" style="padding-right:7px"><?php echo number_format($z['nilai_po'],0,",",".")?></td>
    </tr>
    <?php $no++; }?>
    <tr style="background-color: #A9FF96!important;cursor:pointer;">
        <td style="padding-left:5px;padding-right:7px" colspan='6'><span style='font-weight: bold;font-size:16px;'>TOTAL</span></td>
        <td align='right' style="padding-left:5px;padding-right:7px" ><span style='font-weight: bold;font-size:16px;'><?php echo number_format($total,0,",",".")?></span></td>
    </tr>
</table> 