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

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


			$beli = 0;
			$belitax = 0;
			$jual = 0;
			$jualtax = 0;
			$adjust = 0;
			$adjusttax = 0;
			$mutasi = 0;
			$mutasitax = 0;



			$cek = mysql_query("select * from beli where status = 'N' and billno not in (select billno from qqtmpproduk where  (jenis = 'Masuk' or jenis = 'Retur Beli' ) and tax = 'Non')");
			while ($r=mysql_fetch_array($cek)){	

					$cek2 = mysql_query("select * from belidetail where billno = $r[billno]");
					while ($r2=mysql_fetch_array($cek2)){	
						$beli = $beli + 1;

						$custname = str_replace("'","",mysql_result(mysql_query("select nama from supplier where id =  $r[cust]"),0));
						if($r['isretur'] == "N"){
							$qty = $r2['qty'];
							$jenis = 'Masuk';
						}else{
							$qty = $r2['qty'] * -1;
							$jenis = 'Retur Beli';
						}
						mysql_query("INSERT INTO qqtmpproduk(jenis,billno,billdate,cust,custname,dateadded,produk,qty,harga,status,ucode,tax) VALUES('$jenis','$r[billno]','$r[billdate]','$r[cust]','$custname','$r[dateadded]','$r2[produk]','$qty','$r2[harga]','$i','0','Non')");
					}

			}




			$cek = mysql_query("select * from belitax where status = 'N' and billno not in (select billno from qqtmpproduk where (jenis = 'Masuk' or jenis = 'Retur Beli' ) and tax = 'Tax')");
			while ($r=mysql_fetch_array($cek)){	

					$cek2 = mysql_query("select * from belitaxdetail where billno = $r[billno]");
					while ($r2=mysql_fetch_array($cek2)){	
						$belitax = $belitax + 1;

						$custname = str_replace("'","",mysql_result(mysql_query("select nama from supplier where id =  $r[cust]"),0));
						if($r['isretur'] == "N"){
							$qty = $r2['qty'];
							$jenis = 'Masuk';
						}else{
							$qty = $r2['qty'] * -1;
							$jenis = 'Retur Beli';
						}
						mysql_query("INSERT INTO qqtmpproduk(jenis,billno,billdate,cust,custname,dateadded,produk,qty,harga,status,ucode,tax) VALUES('$jenis','$r[billno]','$r[billdate]','$r[cust]','$custname','$r[dateadded]','$r2[produk]','$qty','$r2[harga]','$i','0','Tax')");
					}

			}

			$cek = mysql_query("select * from jualcs where status = 'N' and (billno,ucode) not in (select billno,ucode from qqtmpproduk where (jenis = 'Keluar' or jenis = 'Retur Jual' ) and tax = 'Non')");
			while ($r=mysql_fetch_array($cek)){	
					$cek2 = mysql_query("select * from jualcsdetail where billno = $r[billno] and ucode = $r[ucode]");
					while ($r2=mysql_fetch_array($cek2)){	
						$jual = $jual + 1;
						
						$custname = str_replace("'","",mysql_result(mysql_query("select nama from customer where id =  $r[cust]"),0));
						if($r['isretur'] == "N"){
							$qty = $r2['qty'] * -1;
							$jenis = 'Keluar';
						}else{
							$qty = $r2['qty'] ;
							$jenis = 'Retur Jual';
						}
						mysql_query("INSERT INTO qqtmpproduk(jenis,billno,billdate,cust,custname,dateadded,produk,qty,harga,status,ucode,tax) VALUES('$jenis','$r[billno]','$r[billdate]','$r[cust]','$custname','$r[dateadded]','$r2[produk]','$qty','$r2[harga]','$i','$r[ucode]','Non')");
					}

			}

			$cek = mysql_query("select * from jualcstax where status = 'N' and (billno,ucode) not in (select billno,ucode from qqtmpproduk where (jenis = 'KeluarTax' or jenis = 'Retur JualTax' ) and tax = 'Tax')");
			while ($r=mysql_fetch_array($cek)){	

					$cek2 = mysql_query("select * from jualcstaxdetail where billno = $r[billno] and ucode = $r[ucode]");
					while ($r2=mysql_fetch_array($cek2)){	
						$jualtax = $jualtax + 1;
				
						$custname = str_replace("'","",mysql_result(mysql_query("select nama from customer where id =  $r[cust]"),0));
						if($r['isretur'] == "N"){
							$qty = $r2['qty'] * -1;
							$jenis = 'KeluarTax';
						}else{
							$qty = $r2['qty'] ;
							$jenis = 'Retur JualTax';
						}
						mysql_query("INSERT INTO qqtmpproduk(jenis,billno,billdate,cust,custname,dateadded,produk,qty,harga,status,ucode,tax) VALUES('$jenis','$r[billno]','$r[billdate]','$r[cust]','$custname','$r[dateadded]','$r2[produk]','$qty','$r2[harga]','$i','$r[ucode]','Tax')");
						//echo"INSERT INTO qqtmpproduk(jenis,billno,billdate,cust,custname,dateadded,produk,qty,harga,status,ucode,tax) VALUES('$jenis','$r[billno]','$r[billdate]','$r[cust]','$custname','$r[dateadded]','$r2[produk]','$qty','$r2[harga]','$i','$r[ucode]','Non')  ===";
					}

			}





			$cek = mysql_query("select * from adjust where pk not in (select billno from qqtmpproduk where jenis = 'Adjust'  and tax = 'Non')");
			while ($r=mysql_fetch_array($cek)){	

						$adjust = $adjust + 1;

						$custname = "";
						$qty = $r['qty'] * -1;
						mysql_query("INSERT INTO qqtmpproduk(jenis,billno,billdate,cust,custname,dateadded,produk,qty,harga,status,ucode,tax) VALUES('Adjust','$r[pk]','$r[billdate]','0','$custname','$r[dateadded]','$r[produk]','$qty','0','$i','0','Non')");
						echo"INSERT INTO qqtmpproduk(jenis,billno,billdate,cust,custname,dateadded,produk,qty,harga,status,ucode,tax) VALUES('Adjust','$r[pk]','$r[billdate]','0','$custname','$r[dateadded]','$r[produk]','$qty','0','$i','0','Non')";
			}



			$cek = mysql_query("select * from adjusttax where pk not in (select billno from qqtmpproduk where jenis = 'AdjustTax'  and tax = 'Tax')");
			while ($r=mysql_fetch_array($cek)){	

						$adjusttax = $adjusttax + 1;

						$custname = "";
						$qty = $r['qty'] * -1;
						mysql_query("INSERT INTO qqtmpproduk(jenis,billno,billdate,cust,custname,dateadded,produk,qty,harga,status,ucode,tax) VALUES('AdjustTax','$r[pk]','$r[billdate]','0','$custname','$r[dateadded]','$r[produk]','$qty','0','$i','0','Tax')");

			}



			$cek = mysql_query("select * from mutasim where status = 'Y' and billno not in (select billno from qqtmpproduk where jenis = 'Mutasi'  and tax = 'Non')");
			while ($r=mysql_fetch_array($cek)){	
					$cek2 = mysql_query("select * from mutasi where mutasi = $r[billno]");
					while ($r2=mysql_fetch_array($cek2)){
						$mutasi = $mutasi + 1;
						
						$custname = "";
						if($r['gudang2'] ==1){
							$qty = $r['qty'] * -1;
						}
						else{
							$qty = $r['qty'] ;
						}
						mysql_query("INSERT INTO qqtmpproduk(jenis,billno,billdate,cust,custname,dateadded,produk,qty,harga,status,ucode,tax) VALUES('Mutasi','$r[billno]','$r[billdate]','0','$custname','$r[dateadded]','$r2[produk]','$qty','0','$i','0','Non')");
					}	
			}




			$cek = mysql_query("select * from mutasitaxm where status = 'Y' and billno not in (select billno from qqtmpproduk where jenis = 'MutasiTax'  and tax = 'Tax')");
			while ($r=mysql_fetch_array($cek)){	
					$cek2 = mysql_query("select * from mutasitax where mutasi = $r[billno]");
					while ($r2=mysql_fetch_array($cek2)){
						$mutasitax = $mutasitax + 1;
						
						$custname = "";
						if($r['gudang2'] ==1){
							$qty = $r['qty'] * -1;
						}
						else{
							$qty = $r['qty'] ;
						}
						mysql_query("INSERT INTO qqtmpproduk(jenis,billno,billdate,cust,custname,dateadded,produk,qty,harga,status,ucode,tax) VALUES('MutasiTax','$r[billno]','$r[billdate]','0','$custname','$r[dateadded]','$r2[produk]','$qty','0','$i','0','Tax')");
					}	
			}



}
//echo"Beli : $beli<br>Beli Tax : $belitax<br>Jual = $jual<br>Jual Tax : $jualtax<br>Adjust : $adjust<br>Adjust Tax : $adjusttax<br>Mutasi : $mutasi<br>Mutasi Tax : $mutasitax";
?>