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

	$cek = mysql_query("
								select if((`belitaxdetail`.`qty` < 0),'Retur BeliTax','MasukTax') AS `jenis`,'0','Tax' as 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 'Adjust' AS `Adjust`,'0','Non'  as tax,`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 'Mutasi' AS `Mutasi`,'0'
								,'Non'  as tax,`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 'AdjustTax' AS `Adjust`,'0','Tax' as 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` 
								
							");
	$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:updatestokD2019a.php');

?>