<?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')))) 
								
								union all 
								select if((`jualcsdetail`.`qty` < 0),'Retur Jual','Keluar') AS `jenis`,jualcs.ucode,'Non',`jualcs`.`billno` AS `billno`,`jualcs`.`billdate` AS `billdate`
								,`jualcs`.`cust` AS `cust`,(select `customer`.`nama` from `customer` where (`customer`.`id` = `jualcs`.`cust`)) AS `custname`,`jualcs`.`dateadded` AS `dateadded`,`jualcsdetail`.`produk` AS `produk`
								,(`jualcsdetail`.`qty` * -(1)) AS `(jualcsdetail.qty * -1)`,`jualcsdetail`.`harga` AS `harga` 
								from (`jualcs` join `jualcsdetail` on(((`jualcsdetail`.`billno` = `jualcs`.`billno`) 
								and (convert(`jualcsdetail`.`ucode` using utf8) = convert(`jualcs`.`ucode` using utf8))
								and (`jualcs`.`status` <> 'D')))) union all select 'Adjust' AS `Adjust`,'0','Non',`adjust`.`pk` AS `pk`,`adjust`.`billdate` AS `billdate`,0 AS `0`
								,'' AS `Name_exp_23`,`adjust`.`dateadded` AS `dateadded`,`adjust`.`produk` AS `produk`,`adjust`.`qty` AS `qty`,0 AS `0` from `adjust` 
								
								union all 
								select if((`belitaxdetail`.`qty` < 0),'Retur BeliTax','MasukTax') AS `jenis`,'0','Tax',`belitax`.`billno` AS `billno`,`belitax`.`billdate` AS `billdate`
								,`belitax`.`cust` AS `cust`,(select `supplier`.`nama` from `supplier` where (`supplier`.`id` = `belitax`.`cust`)) AS `custname`,`belitax`.`dateadded` AS `dateadded`
								,`belitaxdetail`.`produk` AS `produk`,`belitaxdetail`.`qty` AS `qty`,`belitaxdetail`.`harga` AS `harga` 
								from (`belitax` join `belitaxdetail` on(((`belitaxdetail`.`billno` = `belitax`.`billno`) and (`belitax`.`status` <> 'D')))) 
								
								union all 
								select if((`jualcstaxdetail`.`qty` < 0),'Retur JualTax','KeluarTax') AS `jenis`,jualcstax.ucode,'Tax',`jualcstax`.`billno` AS `billno`,`jualcstax`.`billdate` AS `billdate`
								,`jualcstax`.`cust` AS `cust`,(select `customer`.`nama` from `customer` where (`customer`.`id` = `jualcstax`.`cust`)) AS `custname`,`jualcstax`.`dateadded` AS `dateadded`
								,`jualcstaxdetail`.`produk` AS `produk`,(`jualcstaxdetail`.`qty` * -(1)) AS `(jualcstaxdetail.qty * -1)`,`jualcstaxdetail`.`harga` AS `harga`
								from (`jualcstax` join `jualcstaxdetail` on(((`jualcstaxdetail`.`billno` = `jualcstax`.`billno`) and (convert(`jualcstaxdetail`.`ucode` using utf8) = convert(`jualcstax`.`ucode` using utf8)) 
								and (`jualcstax`.`status` <> 'D')))) union all select 'AdjustTax' AS `Adjust`,'0','Tax',`adjusttax`.`pk` AS `pk`,`adjusttax`.`billdate` AS `billdate`,0 AS `0`,'' AS `Name_exp_50`
								,`adjusttax`.`dateadded` AS `dateadded`,`adjusttax`.`produk` AS `produk`,`adjusttax`.`qty` AS `qty`,0 AS `0` from `adjusttax` union all select 'Mutasi' AS `Mutasi`,'0'
								,'Non',`mutasim`.`billno` AS `pk`,`mutasim`.`billdate` AS `billdate`,0 AS `0`,'' AS `Name_exp_50`,`mutasim`.`dateadded` AS `dateadded`,`mutasi`.`produk` AS `produk`
								,if((`mutasim`.`gudang2` = 1),`mutasi`.`qty`,(-(1) * `mutasi`.`qty`)) AS `qty`,0 AS `0` from (`mutasim` join `mutasi` on(((`mutasi`.`mutasi` = `mutasim`.`billno`) and (`mutasim`.`status` = 'Y')))) 
									
								union all 
								select 'MutasiTax' AS `Mutasi`,'0','Tax',`mutasitaxm`.`billno` AS `pk`,`mutasitaxm`.`billdate` AS `billdate`,0 AS `0`,'' AS `Name_exp_50`,`mutasitaxm`.`dateadded` AS `dateadded`
								,`mutasitax`.`produk` AS `produk`,if((`mutasitaxm`.`gudang2` = 1),`mutasitax`.`qty`,(-(1) * `mutasitax`.`qty`)) AS `qty`,0 AS `0` 
								from (`mutasitaxm` join `mutasitax` on(((`mutasitax`.`mutasi` = `mutasitaxm`.`billno`) and (`mutasitaxm`.`status` = 'Y')))) ");
	$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]')");


	}
	

	mysql_query("Update setup set setup = 'N' where id = 1");
	//echo $i;
	header('location:data.php');
}
?>