<script type="text/javascript">
    $(function() {
        $("#theList tr:even").addClass("stripe1");
        $("#theList tr:odd").addClass("stripe2");

        $("#theList tr").hover(
            function() {
                $(this).toggleClass("highlight");
            },
            function() {
                $(this).toggleClass("highlight");
            }
        );
    });
</script>

<?php
include '../../inc/inc.koneksi.php';
include '../../inc/fungsi_tanggal.php';

session_start();

$bulanaw= $_GET['bulanaw'];
$bulanak= $_GET['bulanak'];
$tahun  = $_GET['tahun'];
$kodesp = $_GET['kodesp'];
$kodegr = $_GET['kodegr'];
$cari   = $_GET['cari'];
$user   = $_SESSION['namauser'];
$nafile = 'pbpj_'.$user;

mysql_query("CREATE TABLE $nafile LIKE anal_pbpj");
mysql_query("DELETE FROM $nafile WHERE 1");

$kabisat=$tahun;
while($kabisat>4){
    $kabisat=$kabisat-4;
}

if($bulanak=='01') $tanggal='31';
else if($bulanak=='02'){
    if($kabisat==0)
        $tanggal='29';
    else
        $tanggal='28';
}
else if($bulanak=='03') $tanggal='31';
else if($bulanak=='04') $tanggal='30';
else if($bulanak=='05') $tanggal='31';
else if($bulanak=='06') $tanggal='30';
else if($bulanak=='07') $tanggal='31';
else if($bulanak=='08') $tanggal='31';
else if($bulanak=='09') $tanggal='30';
else if($bulanak=='10') $tanggal='31';
else if($bulanak=='11') $tanggal='30';
else if($bulanak=='12') $tanggal='31';

$tglawal = $tahun.'-'.$bulanaw.'-01';
$tglakhir= $tahun.'-'.$bulanak.'-'.$tanggal;

echo "<table id='theList' width='100%'>
	<tr>
		<th>No.</th>
		<th>Bulan</th>
		<th>Pembelian</th>
		<th>Penjualan</th>
		<th>Surplus/ Devisit</th>
		<th>Prosen</th>
	</tr>";
	if(!$cari==''){
	    $query	= "SELECT month(tanggal) as bulan,sum(jumlah) as tjumlah,sum(potongan) as tpotong FROM faktur_pb WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND (nama_st LIKE '%$cari%' OR kode_st LIKE '%$cari%') AND batal='' AND posting='P' GROUP BY month(tanggal) ";
    }
    else{
        if(!$kodegr=='')
            $query= "SELECT month(tanggal) as bulan,sum(jumlah) as tjumlah,sum(potongan) as tpotong FROM faktur_pb WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND concat(substr(kode_st,1,3))='$kodegr' AND batal='' AND posting='P' GROUP BY month(tanggal) ";
        else{
            if(!$kodesp=='')
                $query= "SELECT month(tanggal) as bulan,sum(jumlah) as tjumlah,sum(potongan) as tpotong FROM faktur_pb WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND kode_sup='$kodesp' AND batal='' AND posting='P' GROUP BY month(tanggal) ";
            else{
                $query= "SELECT month(tanggal) as bulan,sum(jumlah) as tjumlah,sum(potongan) as tpotong FROM faktur_pb WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND batal='' AND posting='P' GROUP BY month(tanggal) ";
            }
        }
    }
	$data= mysql_query($query);
	
	while($r1=mysql_fetch_array($data)){
	    $bulan=$r1['bulan'];
	    $pembelian=$r1['tjumlah']-$r1['tpotong'];
	    
	    if($bulan<10) $bulan='0'.$bulan;
	    
	    mysql_query("INSERT INTO $nafile (bulan,tahun,pembelian,penjualan,surpdev,prosen) VALUES('$bulan','$tahun','$pembelian','0','0','0')");
	}
	
	if(!$cari==''){
	    $query	= "SELECT month(tanggal) as bulan,sum(jumlah) as tjumlah FROM faktur_pj WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND (nama_st LIKE '%$cari%' OR kode_st LIKE '%$cari%') AND batal='' AND posting='P' GROUP BY month(tanggal) ";
    }
    else{
        if(!$kodegr=='')
            $query= "SELECT month(tanggal) as bulan,sum(jumlah) as tjumlah FROM faktur_pj WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND concat(substr(kode_st,1,3))='$kodegr' AND batal='' AND posting='P' GROUP BY month(tanggal) ";
        else{
            if(!$kodesp=='')
                $query= "SELECT month(tanggal) as bulan,sum(jumlah) as tjumlah FROM faktur_pj WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND kode_sp='$kodesp' AND batal='' AND posting='P' GROUP BY month(tanggal) ";
            else{
                $query= "SELECT month(tanggal) as bulan,sum(jumlah) as tjumlah FROM faktur_pj WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND batal='' AND posting='P' GROUP BY month(tanggal) ";
            }
        }
    }
	$data= mysql_query($query);
	
	while($r1=mysql_fetch_array($data)){
	    $bulan=$r1['bulan'];
	    $penjualan=$r1['tjumlah'];
	    
	    if($bulan<10) $bulan='0'.$bulan;
	    
	    $sql2=mysql_query("SELECT * FROM $nafile WHERE bulan='$bulan' AND tahun='$tahun'");
	    $row2=mysql_num_rows($sql2);
	    
	    if($row2>0){
	        $r2=mysql_fetch_assoc($sql2);
	        $pembelian=$r2['pembelian'];
	        $surpdev=$penjualan-$pembelian;
	        $prosen=round((($surpdev/$pembelian)*100),2);
	        
	        mysql_query("UPDATE $nafile SET penjualan='$penjualan',surpdev='$surpdev',prosen='$prosen' WHERE bulan='$bulan'");
	    }else
	        mysql_query("INSERT INTO $nafile (bulan,tahun,pembelian,penjualan,surpdev,prosen) VALUES('$bulan','$tahun','0','$penjualan','0','0')");
	}
	
	$sql=mysql_query("SELECT * FROM $nafile WHERE 1");
	    
	$tbeli=0;$tjual=0;$tsurpdev=0;
	$no=1;
	while($r=mysql_fetch_array($sql)){
	    echo "<tr>
    		<td align='center'>$no</td>
    		<td align='right'>$r[bulan]</td>
		    <td align='right'>".number_format($r['pembelian'])."</td>
		    <td align='right'>".number_format($r['penjualan'])."</td>
		    <td align='right'>".number_format($r['surpdev'])."</td>
    		<td align='right'>".number_format($r['prosen'],2)."</td>
		</tr>";
		$tbeli=$tbeli+$r['pembelian'];
		$tjual=$tjual+$r['penjualan'];
		$tsurpdev=$tsurpdev+$r['surpdev'];
		$no++;
	}
	$tprosen=$tsurpdev/$tbeli;
	
	echo "<tr>
		<td></td>
	    <td align='right'>Total</td>
		<td align='right'>".number_format($tbeli)."</td>
		<td align='right'>".number_format($tjual)."</td>
		<td align='right'>".number_format($tsurpdev)."</td>
    	<td align='right'>".number_format($tprosen,2)."</td>
	</tr>
</table>";
?>