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

$kode		=$_POST[kode];
$tanggal	=$_POST[tanggal];
$kodepl 	=$_POST[kodepl];

$sql	= mysql_query("SELECT * FROM interface");
$r 	= mysql_fetch_assoc($sql);
$kas 			= $r[kas];
$piut			= $r[piutang];
$pibg			= $r[piut_bg];
$potjual		= $r[pot_jual];
$nobppi		= $piut.$kodepl;
$nobppbg	= $pibg.$kodepl;

$query = mysql_query("SELECT * FROM jurppi WHERE kode_ppi='$kode' AND batal='' ");
$row	= mysql_num_rows($query);
if($row>0){
	$ttunai =0;
	$tjbg  	=0;
	$tpot  	=0;

	while($data=mysql_fetch_array($query)){
		$nofak		= $data[nofak];
		$tunai  		= $data[tunai];
		$nomorbg	= $data[nomor_bg];
		$jumlahbg	= $data[jumlah_bg];
		$potongan	=$data[potongan];
		$ttunai 		= $ttunai+$data[tunai];
		$tjbg			= $tjbg+$data[jumlah_bg];
		$tpot			= $tpot+$data[potongan];
			
		mysql_query("UPDATE piutang SET jumlah=jumlah+$tunai+$jumlahbg+$potongan WHERE kode_pl='$kodepl' AND nofak='$nofak'");
	
		if(!($nomorbg.length==0)){
			mysql_query("UPDATE piut_bg SET jumlah=jumlah-$jumlahbg WHERE kode_pl='$kodepl' AND nobg='$nomorbg'");
		}
	}

	// --->kredit piutang berkurang
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$ttunai-$tjbg-$tpot WHERE noakun='$piut'");
	mysql_query("UPDATE akun_bp SET mutasi_kd=mutasi_kd-$ttunai-$tjbg-$tpot WHERE noakun='$nobppi'");

	if(!($ttunai==0))	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$ttunai WHERE noakun='$kas'");

	if(!($tjbg==0)){
		// --->debet piutang berkurang
		mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$tjbg WHERE noakun='$pibg'");
		mysql_query("UPDATE akun_bp SET mutasi_db=mutasi_db-$tjbg WHERE noakun='$nobppbg'");
	}

	if(!($tpot==0)) mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$tpot WHERE noakun='$potjual'");

	mysql_query("UPDATE jurppi SET batal='BATAL' WHERE kode_ppi='$kode'");
	mysql_query("DELETE FROM rekapjur WHERE kode_tr='$kode'");
}
?>