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

$proses = mysql_result(mysql_query("select setup from setup where id = 1"),0);

if ($proses == "N"){
	mysql_query("Update setup set setup = 'Y' where id = 1");

	mysql_query("TRUNCATE  qqtmpproduk");
	//$cek = mysql_query("select if((belidetail.qty < 0),'Retur Beli','Masuk') AS jenis,beli.billno AS billno,beli.billdate AS billdate,beli.cust AS cust,(select supplier.nama from supplier where (supplier.id = beli.cust)) AS custname,beli.dateadded AS dateadded,belidetail.produk AS produk,belidetail.qty AS qty,belidetail.harga AS harga,beli.status from (beli join belidetail on(((belidetail.billno = beli.billno) ))) ");

	$cek = mysql_query("select if((`belidetail`.`qty` < 0),'Retur Beli','Masuk') AS `jenis`
								,'0' as ucode,'Non' as tax,`beli`.`billno` AS `billno`
								,`beli`.`billdate` AS `billdate`,`beli`.`cust` AS `cust`
								,(select `supplier`.`nama` from `supplier` where (`supplier`.`id` = `beli`.`cust`)) AS `custname`
								,`beli`.`dateadded` AS `dateadded`,`belidetail`.`produk` AS `produk`,`belidetail`.`qty` AS `qty`
								,`belidetail`.`harga` AS `harga` from (`beli` join `belidetail` on(((`belidetail`.`billno` = `beli`.`billno`) and (`beli`.`status` <> 'D')))) 
								");
	$i = 0;
	while ($r=mysql_fetch_array($cek)){	
			$i = $i + 1;
			// if ($i == 8220){
				// echo"$r[jenis], $r[billno],$r[billdate],$r[cust],$r[custname],$r[produk], $r[qty], $r[harga]";
			// }
			$custname = str_replace("'","",$r['custname']);
			mysql_query("INSERT INTO qqtmpproduk(jenis,billno,billdate,cust,custname,dateadded,produk,qty,harga,status,ucode,tax) VALUES('$r[jenis]','$r[billno]','$r[billdate]','$r[cust]','$custname','$r[dateadded]','$r[produk]','$r[qty]','$r[harga]','$i','$r[ucode]','$r[tax]')");


	}
	



	header('location:updatestokB2019.php');
}
?>