<?php
include "../../inc/inc.koneksi.php";

$table	="faktur_pj";

$kode		=$_POST[kode];
$kodeplg    =$_POST[kodeplg];
$total		=$_POST[total];
$bayar	    =$_POST[tbayar];
$thpp 		=$_POST[thpp];
$laba		=$total-$thpp;
$kurang 	=$total-$bayar;

$sql	    = mysql_query("SELECT * FROM interface");
$r 		 	= mysql_fetch_assoc($sql);
$kas 	 	= $r[kas];
$piut 	 	= $r[piutang];
$persed	    = $r[persed];
$labapb	    = $r[laba_pb];
$jual 	 	= $r[penjualan];
$potjual 	= $r[pot_jual];
$nohpp 	    = $r[hpp];
$labrug  	= $r[laba_rugi];
$nobppi	    = $piut.$kodeplg;

$sql 	= mysql_query("SELECT * FROM $table WHERE kode_pj='$kode' AND batal='' ORDER BY kode_pj");
$row	= mysql_num_rows($sql);
if($row>0){
	while ($r=mysql_fetch_array($sql)){
		$kodest=$r[kode_st];
		$junit=$r[jml_unit];
		mysql_query("UPDATE stock SET jml_stok=jml_stok+$junit WHERE kode_st='$kodest'");
	}

	if(!$kurang==0){	
		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$kurang WHERE noakun='$piut'");
		mysql_query("UPDATE akun_bp SET mutasi_db=mutasi_db-$kurang WHERE noakun='$nobppi'");
		mysql_query("DELETE FROM piutang WHERE kode_pl='$kodeplg' AND nofak='$kode' ");		
	}

	// --->hpp debet	
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$tothpp WHERE noakun='$nohpp'");
	
	// --->penjualan kredit
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$total WHERE noakun='$jual'");
	
	// --->persediaan barang jadi kredit
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$tothpp WHERE noakun='$persed'");
	
	// --->laba rugi debet	
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$laba WHERE noakun='$labrug'");
	
	// --->laba periode berjalan kredit	
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$laba WHERE noakun='$labapb'");
	
	mysql_query("UPDATE $table SET batal='BATAL' WHERE kode_pj='$kode' ORDER BY kode_pj");
	
	mysql_query("DELETE FROM rekap_mbr WHERE kode_tr='$kode' ORDER BY kode_tr");
	mysql_query("DELETE FROM rekapjur WHERE kode_tr='$kode'");
	// --->menandai bahwa data faktur telah diproses
	
	echo "Faktur penjualan kode $kode telah dibatalkan.";
}
?>