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

$kode	=$_POST[kode];
$tanggal=$_POST[tanggal];
$kodepl =$_POST[kodepl];
$namapl =$_POST[namapl];
$tglaw  =$_POST[tglaw];
$tglak  =$_POST[tglak];
$tbayar =$_POST[tbayar];
$tgaw	=.jin_date_str($tglaw);
$tgak	=.jin_date_str($tglak);

$sql	= mysql_query("SELECT * FROM interface");
$r 		= mysql_fetch_assoc($sql);
$kas 	= $r[kas];
$piut	= $r[piutang];
$nosubks= substr($nosubks,0,3);
$nosubpi= substr($piut,0,3);
$nobppi	= $piut.$kodepl;

$query = mysql_query("SELECT * FROM jurppi_tmp WHERE batal=''");

$tbayar=0;

while($data=mysql_fetch_array($query)){
	$nofak	= $data[nofak];
	$tglfak	= $data[tgl_fak];
	$nopol	= $data[nopol];
	$jmlpiut= $data[jml_piut];
	$dibayar= $data[dibayar];
	$sisa 	= $data[sisa];
	
	mysql_query("INSERT INTO jurppi (kode_ppi,tanggal,kodepl,namapl,tanggal_aw,tanggal_ak,nofak,tgl_fak,nopol,jml_piut,dibayar,sisa,user) VALUES('$kode','$tanggal','$kodepl','$namapl','$tanggal_aw','$tanggal_ak','$nofak','$tgl_fak','$nopol','$jml_piut','$dibayar','$sisa','$user')");
	
	$sql1 = mysql_query("SELECT kode_pl,nofak,jumlah FROM piutang WHERE kode_pl='$kodepl' AND nofak='$nofak'");
	$r = mysql_fetch_assoc($sql1);
	$saldo = $r[jumlah]-$dibayar;
		
	$input	= "UPDATE piutang SET jumlah='$saldo' WHERE kode_pl='$kodepl' AND nofak='$nofak'";
	mysql_query($input);
}

// --->debet kas bertambah
$sql = mysql_query("SELECT noakun,mutasi_db FROM akun_bb WHERE noakun='$kas' ");
$r = mysql_fetch_assoc($sql);
$debet = $r[mutasi_db]+$tbayar;
		
$input	= "UPDATE akun_bb SET mutasi_db='$debet' WHERE noakun='$kas'";
mysql_query($input);
	
mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,debet,keterangan,user) VALUES('$kode','PPI','$tanggal','$kode','$kas','$nosubks','$tbayar','Penerimaan piutang $namapl $tgaw s/d $tgak','$user')");
	
// --->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]+$tbayar;
		
$input	= "UPDATE akun_bb SET mutasi_kd='$kredit' WHERE noakun='$piut'";
mysql_query($input);
		
$sql = mysql_query("SELECT noakun,mutasi_kd FROM akun_bp WHERE noakun='$nobppi' ");
$r = mysql_fetch_assoc($sql);	
$kredit = $r[mutasi_kd]+$tbayar;
		
$input	= "UPDATE akun_bp SET mutasi_kd='$kredit' WHERE noakun='$nobppi'";
mysql_query($input);
	
mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,nobp,kredit,keterangan,user) VALUES('$kode','PPI','$tanggal','$kode','$piut','$nosubpi','$nobppi','$tbayar','Penerimaan piutang $namapl $tgaw s/d $tgak','$user')");
?>
