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

session_start();

date_default_timezone_set('Asia/Jakarta');

$kode		=$_POST['kode'];
$kodesop	=$_POST['kodesop'];
$tanggal	=$_POST['tanggal'];
$kodeplg	=$_POST['kodeplg'];
$namaplg	=$_POST['namaplg'];
$tanggaljt	=$_POST['tanggaljt'];
$total		=$_POST['total'];
$potong		=$_POST['potong'];
$bayar		=$_POST['bayar'];
$kurang 	=$_POST['kurang'];
$tothpp 	=$_POST['tothpp'];
$laba		=$total-$potong-$tothpp;
$user		=$_SESSION['username'];
$tgllap		=date('d-m-Y');
$jam		=date('H:i:s');
$tanggal	=jin_date_str($tanggal);

$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 faktur_pjg WHERE kode_pj='$kode' AND batal='' ORDER BY kode_pj");
	
$row = mysql_num_rows($sql);
if($row>0){
	$namast='';
	while($r=mysql_fetch_array($sql)){	
		$nourut =$r['no_urut'];
		$kodest	=$r['kode_st'];
		$jmlunit=$r['jml_unit'];
		$hgjual	=$r['hg_jual'];
		$namast	=$namast.'. '.$r['nama_st'];
		
		mysql_query("UPDATE stock SET jml_stok=jml_stok+$jmlunit WHERE kode_st='$kodest'");
		mysql_query("UPDATE sop SET jml_muat=jml_muat-$jmlunit,tanggal_ft='' WHERE kode_sop='$kodesop' AND no_urut='$nourut'");
	}
	// --->kas debet
	if(!$bayar==0) mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$bayar WHERE noakun='$kas'");

	// --->piutang debet
	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 nofak='$kode'");		
	}
	if(!($potong==0)) mysql_query("UPDATE akun_bb SET mutasi_db=mutasi_db-$potong WHERE noakun='$potjual'");		

	// --->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'");	
	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'");
	mysql_query("UPDATE akun_bb SET mutasi_kd=mutasi_kd-$laba WHERE noakun='$labapb'");
	
	mysql_query("UPDATE sop SET tanggal_ft='' WHERE kode_sop='$kodesop'");
	
	// --->menandai bahwa data faktur telah diproses
	mysql_query("UPDATE faktur_pjg SET batal='BATAL' WHERE kode_pj='$kode'");
	mysql_query("DELETE FROM rekapjur WHERE kode_tr='$kode'");
	mysql_query("DELETE FROM rekap_mbr WHERE kode_tr='$kode'");
	
	mysql_query("INSERT INTO log_history (user,tanggal,jam,aktivitas,no_bukti,nama_st,keterangan) VALUES('$user','$tgllap','$jam','BATAL IMPORT PENJUALAN','$kode','$namast','Tanggal faktur $tanggal, pelanggan $namaplg')");
	echo "Faktur penjualan nomor $kode telah dibatalkan.";
}
?>