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

session_start();
date_default_timezone_set('Asia/Jakarta');

$kode	=$_POST['kode'];
$tanggal=$_POST['tanggal'];
$user	=$_SESSION['namauser'];
$jam	=date('H:i:s');

$sql	= mysql_query("SELECT * FROM interface");
$r 	= mysql_fetch_assoc($sql);
$kas 		= $r['kas'];
$piut		= $r['piutang'];
$potjual	= $r['pot_jual'];
$labapb		= $r['laba_pb'];
$labrug		= $r['laba_rugi'];
$nosubks	= substr($nosubks,0,3);
$nosubpi	= substr($piut,0,3);
$nosubpot	= substr($potjual,0,3);
$nosublpb	= substr($labapb,0,3);
$nosublr 	= substr($labrug,0,3);

$query = mysql_query("SELECT * FROM jurppi WHERE kode_ppi='$kode'");

$ttunai=0;
$tpot  =0;
while($data=mysql_fetch_array($query)){
	$nourut		= $data['no_urut'];
	$kodepl 	= $data['kode_pl'];
	$namapl 	= $data['nama_pl'];
	$kodecab 	= $data['kode_cab'];
	$nobppi	    = $piut.$kodepl;
	$nofak		= $data['nofak'];
	$tglfak		= $data['tgl_fak'];
	$jmlpiut	= $data['jml_piut'];
	$tunai  	= $data['tunai'];
	$potongan	= $data['potongan'];
	$sisa 		= $data['sisa'];
	$bayar		= $data['tunai']+$data['jumlah_bg']+$data['potongan'];
	$ttunai 	= $ttunai+$data['tunai'];
	$tpot		= $tpot+$data['potongan'];
	
	$sql1 	= mysql_query("SELECT * FROM piutang WHERE kode_pl='$kodepl' AND nofak='$nofak'");
	$r1		= mysql_fetch_assoc($sql1);
	$saldo 	= $r1['jumlah']-$tunai-$potongan;
	
	mysql_query("UPDATE piutang SET jumlah='$saldo' WHERE kode_pl='$kodepl' AND nofak='$nofak'");
	
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,kode_cab,no_urut,noakun,nosub,nobp,nofak,debet,kredit,keterangan,user,jam) VALUES('$kode','PPI','$tanggal','$kode','$kodecab','$nourut','$piut','$nosubpi','$nobppi','','0','$bayar','Terima dari $namapl faktur $nofak','$user','$jam')");
}

// --->kredit piutang bertambah
$sql		= mysql_query("SELECT noakun,mutasi_kd FROM akun_bb WHERE noakun='$piut'");
$r 	 	= mysql_fetch_assoc($sql);	
$kredit = $r['mutasi_kd']+$ttunai+$tpot;
		
mysql_query("UPDATE akun_bb SET mutasi_kd='$kredit' WHERE noakun='$piut'");
		
$sql 	= mysql_query("SELECT noakun,mutasi_kd FROM akun_bp WHERE noakun='$nobppi'");
$r 	 	= mysql_fetch_assoc($sql);
$kredit	= $r['mutasi_kd']+$ttunai+$tpot;
		
mysql_query("UPDATE akun_bp SET mutasi_kd='$kredit' WHERE noakun='$nobppi'");
	
if(!($ttunai==0)){
	// --->debet kas bertambah
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$ttunai WHERE noakun='$kas'");	
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,kode_cab,no_urut,noakun,nosub,nobp,nofak,debet,kredit,keterangan,user,jam) VALUES('$kode','PPI','$tanggal','$kode','$kodecab','$nourut','$kas','$nosubks','','','$ttunai','0','Penerimaan piutang $namapl','$user','$jam')");
}

if(!($tpot==0)){
	// --->debet potongan penjualan
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$tpot WHERE noakun='$potjual'");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,kode_cab,no_urut,noakun,nosub,nobp,nofak,debet,kredit,keterangan,user,jam) VALUES('$kode','PPI','$tanggal','$kode','$kodecab','$nourut','$potjual','$nosubpot','','','$tpot','0','Potongan pembayaran $namapl','$user','$jam')");
	
	mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db+$tpot WHERE noakun='$labapb'");	
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,kode_cab,no_urut,noakun,nosub,nobp,nofak,debet,kredit,keterangan,user,jam) VALUES('$kode','PPI','$tanggal','$kode','$kodecab','$nourut','$labapb','$nosublpb','','','$tpot','0','Potongan pembayaran $namapl','$user','$jam')");
	
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd+$tpot WHERE noakun='$labrug' ");
	mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,kode_cab,no_urut,noakun,nosub,nobp,nofak,debet,kredit,keterangan,user,jam) VALUES('$kode','PPI','$tanggal','$kode','$kodecab','$nourut','$labrug','$nosublr','','','0','$tpot','Potongan pembayaran $namapl','$user','$jam')");
}

mysql_query("UPDATE jurppi SET posting='P' WHERE kode_ppi='$kode'");
?>