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

$tglawal =$_POST['tglawal'];
$tglakhir=$_POST['tglakhir'];

$sql= mysql_query("SELECT * FROM interface");
$r 	= mysql_fetch_assoc($sql);
$kas 	 	= '11103';
$persed		= $r['persed'];
$labapb		= $r['laba_pb'];
$jual 	 	= $r['penjualan'];
$pendedc	= $r['pendap_edc'];
$potjual 	= $r['pot_jual'];
$nohpp 		= $r['hpp'];
$labrug  	= $r['laba_rugi'];
$nosubks	= substr($kas,0,3);
$nosubps	= substr($persed,0,3);
$nosublpb	= substr($labapb,0,3);
$nosubjl 	= substr($jual,0,3);
$nosubped	= substr($pendedc,0,3);
$nosubpj 	= substr($potjual,0,3);
$nosubhpp	= substr($nohpp,0,3);
$nosublr	= substr($labrug,0,3);

mysql_query("DELETE FROM rekapjur WHERE jurnal='PJL' AND kode_cab='03' AND tanggal BETWEEN '$tglawal' AND '$tglakhir'");

$sql= mysql_query("SELECT kode_pj,tanggal,kode_cab,kode_pl,nama_pl,sum(jumlah) as tjumlah,sum(hpp) as thpp,sum(potongan) as tpotong,sum(charge_edc) as tcharge,jenis_by,nobank,user,jam FROM faktur_pj WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND kode_cab='03' AND posting='P' AND batal='' GROUP BY kode_pj");
 
while($r=mysql_fetch_array($sql)){
	$kode	=$r['kode_pj'];
	$tanggal=$r['tanggal'];
	$kodecab=$r['kode_cab'];
	$kodepl	=$r['kode_pl'];
	$namapl	=$r['nama_pl'];
	$total	=$r['tjumlah'];
	$thpp 	=$r['thpp'];
	$potong	=$r['tpotong'];
	$charge =$r['tcharge'];
	$jenisby=$r['jenis_by'];
	$nobank =$r['nobank'];
	$user 	=$r['user'];
	$jam  	=$r['jam'];
	$netto	=$total-$potong;
	$laba	=$netto-$thpp;
	
	if($jenisby==''){
	    $sql1= mysql_query("SELECT kode_pj,jenis_by,nobank FROM faktur_pj WHERE kode_pj='$kode' AND !(jenis_by='')");
	    $r1  = mysql_fetch_assoc($sql1);
	    $jenisby=$r1['jenis_by'];
	    $nobank =$r1['nobank'];
	}
	$nosubbk=substr($nobank,1,3);
	
    if($jenisby=='TUNAI')
		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','PJL','$tanggal','$kode','$kodecab','1','$kas','$nosubks','','','$netto','0','Penjualan retail','$user','$jam')");
	else{
		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','PJL','$tanggal','$kode','$kodecab','1','$nobank','$nosubbk','','','$netto','0','Penjualan retail','$user','$jam')");
		
		if(!$charge=='')
			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','PJL','$tanggal','$kode','$kodecab','1','$pendedc','$nosubped','','','0','$charge','Charge EDC Penjualan retail','$user','$jam')");
	}
	
	if($potong>0) 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','PJL','$tanggal','$kode','$kodecab','1','$potjual','$nosubpj','','','$potong','0','Potongan penjualan retail','$user','$jam')");
	
	// --->hpp debet	
	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','PJL','$tanggal','$kode','$kodecab','1','$nohpp','$nosubhpp','','','$thpp','0','Penjualan ke $namapl','$user','$jam')");
	// --->penjualan kredit
	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','PJL','$tanggal','$kode','$kodecab','1','$jual','$nosubjl','','','0','$total','Penjualan retail','$user','$jam')");
	// --->persediaan kredit
	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','PJL','$tanggal','$kode','$kodecab','1','$persed','$nosubps','','','0','$thpp','Penjualan ke $namapl','$user','$jam')");
	// --->laba rugi debet
	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','PJL','$tanggal','$kode','$kodecab','1','$labrug','$nosublr','','','$laba','0','Laba penjualan ke $namapl','$user','$jam')");
	// --->laba periode berjalan kredit
	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','PJL','$tanggal','$kode','$kodecab','1','$labapb','$nosublpb','','','0','$laba','Laba penjualan ke $namapl','$user','$jam')");
}
echo "Data penjualan tanggal $tglawal s/d $tglakhir telah diposting ulang.";
?>