<?php
include "../include/config.php";
include "../function/inc_function.php";
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="laporan_exbc_bc_'.date('Y-m-d H:i:s').'.xls"');
header('Cache-Control: max-age=0');

$start_datec = $_REQUEST['fromdate'];
$end_datec   = $_REQUEST['todate'];
$doc_type    = $_REQUEST['doc_type'];
$month 		 = substr($start_datec,5,2);
$year		 = substr($start_datec,0,4);
#tanggal stock awal
if($month=='01' || $month=='02' || $month=='03'){
	$start_date = "$year-01-01";
	$end_date   = "$year-03-31";
}elseif($month=='04' || $month=='05' || $month=='06'){
	$start_date = "$year-04-01";
	$end_date   = "$year-06-30";
}elseif($month=='07' || $month=='08' || $month=='09'){
	$start_date = "$year-07-01";
	$end_date   = "$year-09-30";
}elseif($month=='10' || $month=='11' || $month=='12'){
	$start_date = "$year-10-01";
	$end_date   = "$year-12-31";
}
#insert temp exbc
$q0 = mysql_query("SELECT a.id oute_hid, b.id oute_did, b.outd_prodid, b.outd_qty, outh_docdate, c.prod_cat FROM outgoing a
	  INNER JOIN outgoing_detail b ON b.outd_hid=a.id
	  INNER JOIN product c ON c.id=b.outd_prodid
	  WHERE a.outh_status=20 AND a.outh_doctype='$doc_type' AND a.outh_docdate BETWEEN '$start_datec' AND '$end_datec' 
	  ORDER BY a.outh_docdate ASC"); 
while ($d0=mysql_fetch_array($q0)){
	if($d0['prod_cat']==3){
		$cek = mysql_fetch_array(mysql_query("SELECT id FROM outgoing_exbc WHERE oute_hid='$d0[oute_hid]' AND oute_did='$d0[oute_did]' AND oute_status=10"));
		if(empty($cek['id'])){
			# cek bom master
			$cekb = mysql_fetch_array(mysql_query("SELECT id FROM bom WHERE bomh_status=10 AND bomh_prodid='$d0[outd_prodid]'"));
			if(!empty($cekb['id'])){
				$q1 = mysql_query("SELECT b.bomd_qty, b.bomd_prodid FROM bom a
				  INNER JOIN bom_detail b ON b.bomd_hid=a.id
				  WHERE a.bomh_status=10 AND b.bomd_status=10 AND a.bomh_prodid='$d0[outd_prodid]'"); 
				while ($d1=mysql_fetch_array($q1)){	
					$qtybom1 = $d0['outd_qty'] * $d1['bomd_qty'];		
					mysql_query("INSERT INTO outgoing_exbc_temp SET oute_docdate='$d0[outh_docdate]', oute_hid='$d0[oute_hid]', oute_did='$d0[oute_did]', oute_prodid='$d1[bomd_prodid]', 
					oute_qty_orig='$qtybom1', oute_qty_exbc='$qtybom1', oute_status=10");
				}
			}else{ #jika tidak ada bom master dari WO terakhir
				$woid = mysql_fetch_array(mysql_query("SELECT id FROM workorder WHERE wo_status=20 AND wo_prodid='$d0[outd_prodid]' ORDER BY id DESC LIMIT 0,1"));
				if(!empty($woid['id'])){
					$q2 = mysql_query("SELECT c.prod_code, c.prod_name, c.prod_sat, (b.wod_qty/a.wo_qty) bomd_qty, b.wod_prodid bomd_prodid, b.wod_qty FROM workorder a
						  INNER JOIN workorder_detail b ON b.wod_hid=a.id
						  INNER JOIN product c ON c.id=b.wod_prodid
						  WHERE a.wo_status=20 AND a.wo_level='Level2' AND a.wo_prodid='$d0[outd_prodid]' AND a.id='$woid[id]'");
					while ($d2=mysql_fetch_array($q2)){
						$qtybom2 = $d0['outd_qty'] * $d2['bomd_qty'];	
						mysql_query("INSERT INTO outgoing_exbc_temp SET oute_docdate='$d0[outh_docdate]', oute_hid='$d0[oute_hid]', oute_did='$d0[oute_did]', oute_prodid='$d2[bomd_prodid]', 
						oute_qty_orig='$qtybom2', oute_qty_exbc='$qtybom2', oute_status=10");
					}
				}else{
					mysql_query("INSERT INTO outgoing_exbc_temp SET oute_docdate='$d0[outh_docdate]', oute_hid='$d0[oute_hid]', oute_did='$d0[oute_did]', oute_prodid='$d0[outd_prodid]', 
					oute_qty_orig='$d0[outd_qty]', oute_qty_exbc='$d0[outd_qty]', oute_status=10");
				}
			}
		} #sudah ada exbc lanjut
	}elseif($d0['prod_cat']==1 || $d0['prod_cat']==6){  
		$cek = mysql_fetch_array(mysql_query("SELECT id FROM outgoing_exbc WHERE oute_hid='$d0[oute_hid]' AND oute_did='$d0[oute_did]' AND oute_status=10"));
		if(empty($cek['id'])){
			mysql_query("INSERT INTO outgoing_exbc_temp SET oute_docdate='$d0[outh_docdate]', oute_hid='$d0[oute_hid]', oute_did='$d0[oute_did]', oute_prodid='$d0[outd_prodid]', 
			oute_qty_orig='$d0[outd_qty]', oute_qty_exbc='$d0[outd_qty]', oute_status=10");
		}
	}elseif($d0['prod_cat']==5){
		$cek = mysql_fetch_array(mysql_query("SELECT id FROM outgoing_exbc WHERE oute_hid='$d0[oute_hid]' AND oute_did='$d0[oute_did]' AND oute_status=10"));
		if(empty($cek['id'])){
			# cek bom master
			$cekb = mysql_fetch_array(mysql_query("SELECT id FROM bom WHERE bomh_status=10 AND bomh_prodid='$d0[outd_prodid]'"));
			if(!empty($cekb['id'])){
				$q1 = mysql_query("SELECT b.bomd_qty, b.bomd_prodid FROM bom a
				  INNER JOIN bom_detail b ON b.bomd_hid=a.id
				  WHERE a.bomh_status=10 AND b.bomd_status=10 AND a.bomh_prodid='$d0[outd_prodid]'"); 
				while ($d1=mysql_fetch_array($q1)){	
					$qtybom1 = $d0['outd_qty'] * $d1['bomd_qty'];		
					mysql_query("INSERT INTO outgoing_exbc_temp SET oute_docdate='$d0[outh_docdate]', oute_hid='$d0[oute_hid]', oute_did='$d0[oute_did]', oute_prodid='$d1[bomd_prodid]', 
					oute_qty_orig='$qtybom1', oute_qty_exbc='$qtybom1', oute_status=10");
				}
			}else{ #jika tidak ada bom master dari WO terakhir
				$woid = mysql_fetch_array(mysql_query("SELECT id FROM workorder WHERE wo_status=20 AND wo_prodid='$d0[outd_prodid]' ORDER BY id DESC LIMIT 0,1"));
				if(!empty($woid['id'])){
					$q2 = mysql_query("SELECT c.prod_code, c.prod_name, c.prod_sat, (b.wod_qty/a.wo_qty) bomd_qty, b.wod_prodid bomd_prodid, b.wod_qty FROM workorder a
						  INNER JOIN workorder_detail b ON b.wod_hid=a.id
						  INNER JOIN product c ON c.id=b.wod_prodid
						  WHERE a.wo_status=20 AND a.wo_level='Level1' AND a.wo_prodid='$d0[outd_prodid]' AND a.id='$woid[id]'");
					while ($d2=mysql_fetch_array($q2)){
						$qtybom2 = $d0['outd_qty'] * $d2['bomd_qty'];	
						mysql_query("INSERT INTO outgoing_exbc_temp SET oute_docdate='$d0[outh_docdate]', oute_hid='$d0[oute_hid]', oute_did='$d0[oute_did]', oute_prodid='$d2[bomd_prodid]', 
						oute_qty_orig='$qtybom2', oute_qty_exbc='$qtybom2', oute_status=10");
					}
				}else{
					mysql_query("INSERT INTO outgoing_exbc_temp SET oute_docdate='$d0[outh_docdate]', oute_hid='$d0[oute_hid]', oute_did='$d0[oute_did]', oute_prodid='$d0[outd_prodid]', 
					oute_qty_orig='$d0[outd_qty]', oute_qty_exbc='$d0[outd_qty]', oute_status=10");
				}	
			}
		} #sudah ada exbc lanjut
	} 
}
#insert exbc 1
$qt1 = mysql_query("SELECT * FROM outgoing_exbc_temp WHERE oute_docdate BETWEEN '$start_datec' AND '$end_datec' AND oute_status=10 AND oute_qty_exbc > 0 ORDER BY  oute_hid, oute_did ASC");
while($dt1=mysql_fetch_array($qt1)){
	$exbc1 = mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
			b.prod_code, b.prod_name, b.prod_sat FROM
			(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
			(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
			(stock_price/stock_qty) price 
			FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
			UNION ALL
			SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
			(b.incd_price/b.incd_qty) price 
			FROM incoming a
			INNER JOIN incoming_detail b ON b.incd_hid=a.id
			WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
			UNION ALL
			SELECT a.oute_prodid prodid, a.oute_doctype doctype , a.oute_docno docno, a.oute_docdate docdate, 0 inqty, a.oute_qty outqty, 0 price 
			FROM outgoing_exbc a INNER JOIN outgoing b ON b.id=a.oute_hid WHERE a.oute_status=10 AND b.outh_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
			INNER JOIN product b ON b.id=a.prodid
			WHERE b.prod_cat IN(1,3,5,6) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
			WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt1[oute_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
	if(!empty($exbc1['docno'])){		
		if($exbc1['qty_bal'] >= $dt1['oute_qty_exbc']){
			$qty1   = $dt1['oute_qty_exbc'];
			$price1 = $qty1 * $exbc1['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt1[oute_hid]', oute_did='$dt1[oute_did]', oute_prodid='$dt1[oute_prodid]', 
						oute_qty='$qty1', oute_status=10, oute_doctype='$exbc1[doctype]', oute_docno='$exbc1[docno]', oute_docdate='$exbc1[docdate]'");
			#update qty temp
			mysql_query("DELETE FROM outgoing_exbc_temp WHERE oute_prodid='$dt1[oute_prodid]' AND oute_hid='$dt1[oute_hid]' AND oute_did='$dt1[oute_did]'");			 
		}else{
			$qty1   = $dt1['oute_qty_exbc'] - $exbc1['qty_bal'];
			$price1 = $exbc1['qty_bal'] * $exbc1['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt1[oute_hid]', oute_did='$dt1[oute_did]', oute_prodid='$dt1[oute_prodid]', 
						oute_qty='$exbc1[qty_bal]', oute_status=10, oute_doctype='$exbc1[doctype]', oute_docno='$exbc1[docno]', oute_docdate='$exbc1[docdate]'");			 
			#update qty temp
			mysql_query("UPDATE outgoing_exbc_temp SET oute_qty_exbc='$qty1' WHERE oute_prodid='$dt1[oute_prodid]' AND oute_hid='$dt1[oute_hid]' AND oute_did='$dt1[oute_did]'");
		}
	}		
}	

#insert exbc 2
$qt2 = mysql_query("SELECT * FROM outgoing_exbc_temp WHERE oute_docdate BETWEEN '$start_datec' AND '$end_datec' AND oute_status=10 AND oute_qty_exbc > 0 ORDER BY  oute_hid, oute_did ASC");
while($dt2=mysql_fetch_array($qt2)){
	$exbc2 = mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
			b.prod_code, b.prod_name, b.prod_sat FROM
			(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
			(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
			(stock_price/stock_qty) price 
			FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
			UNION ALL
			SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
			(b.incd_price/b.incd_qty) price 
			FROM incoming a
			INNER JOIN incoming_detail b ON b.incd_hid=a.id
			WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
			UNION ALL
			SELECT a.oute_prodid prodid, a.oute_doctype doctype , a.oute_docno docno, a.oute_docdate docdate, 0 inqty, a.oute_qty outqty, 0 price 
			FROM outgoing_exbc a INNER JOIN outgoing b ON b.id=a.oute_hid WHERE a.oute_status=10 AND b.outh_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
			INNER JOIN product b ON b.id=a.prodid
			WHERE b.prod_cat IN(1,3,5,6) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
			WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt2[oute_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
	if(!empty($exbc2['docno'])){
		if($exbc2['qty_bal'] >= $dt2['oute_qty_exbc']){
			$qty2   = $dt2['oute_qty_exbc'];
			$price2 = $qty2 * $exbc2['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt2[oute_hid]', oute_did='$dt2[oute_did]', oute_prodid='$dt2[oute_prodid]', 
						oute_qty='$qty2', oute_status=10, oute_doctype='$exbc2[doctype]', oute_docno='$exbc2[docno]', oute_docdate='$exbc2[docdate]'");
			#update qty temp
			mysql_query("DELETE FROM outgoing_exbc_temp WHERE oute_prodid='$dt2[oute_prodid]' AND oute_hid='$dt2[oute_hid]' AND oute_did='$dt2[oute_did]'");			 
		}else{
			$qty2   = $dt2['oute_qty_exbc'] - $exbc2['qty_bal'];
			$price2 = $exbc2['qty_bal'] * $exbc2['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt2[oute_hid]', oute_did='$dt2[oute_did]', oute_prodid='$dt2[oute_prodid]', 
						oute_qty='$exbc2[qty_bal]', oute_status=10, oute_doctype='$exbc2[doctype]', oute_docno='$exbc2[docno]', oute_docdate='$exbc2[docdate]'");			 
			#update qty temp
			mysql_query("UPDATE outgoing_exbc_temp SET oute_qty_exbc='$qty2' WHERE oute_prodid='$dt2[oute_prodid]' AND oute_hid='$dt2[oute_hid]' AND oute_did='$dt2[oute_did]'");
		}
	}		
}

#insert exbc 3
$qt3 = mysql_query("SELECT * FROM outgoing_exbc_temp WHERE oute_docdate BETWEEN '$start_datec' AND '$end_datec' AND oute_status=10 AND oute_qty_exbc > 0 ORDER BY  oute_hid, oute_did ASC");
while($dt3=mysql_fetch_array($qt3)){
	$exbc3 = mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
			b.prod_code, b.prod_name, b.prod_sat FROM
			(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
			(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
			(stock_price/stock_qty) price 
			FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
			UNION ALL
			SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
			(b.incd_price/b.incd_qty) price 
			FROM incoming a
			INNER JOIN incoming_detail b ON b.incd_hid=a.id
			WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
			UNION ALL
			SELECT a.oute_prodid prodid, a.oute_doctype doctype , a.oute_docno docno, a.oute_docdate docdate, 0 inqty, a.oute_qty outqty, 0 price 
			FROM outgoing_exbc a INNER JOIN outgoing b ON b.id=a.oute_hid WHERE a.oute_status=10 AND b.outh_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
			INNER JOIN product b ON b.id=a.prodid
			WHERE b.prod_cat IN(1,3,5,6) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
			WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt3[oute_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
	if(!empty($exbc3['docno'])){
		if($exbc3['qty_bal'] >= $dt3['oute_qty_exbc']){
			$qty3   = $dt3['oute_qty_exbc'];
			$price3 = $qty3 * $exbc3['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt3[oute_hid]', oute_did='$dt3[oute_did]', oute_prodid='$dt3[oute_prodid]', 
						oute_qty='$qty3', oute_status=10, oute_doctype='$exbc3[doctype]', oute_docno='$exbc3[docno]', oute_docdate='$exbc3[docdate]'");
			#update qty temp
			mysql_query("DELETE FROM outgoing_exbc_temp WHERE oute_prodid='$dt3[oute_prodid]' AND oute_hid='$dt3[oute_hid]' AND oute_did='$dt3[oute_did]'");			 
		}else{
			$qty3   = $dt3['oute_qty_exbc'] - $exbc3['qty_bal'];
			$price3 = $exbc3['qty_bal'] * $exbc3['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt3[oute_hid]', oute_did='$dt3[oute_did]', oute_prodid='$dt3[oute_prodid]', 
						oute_qty='$exbc3[qty_bal]', oute_status=10, oute_doctype='$exbc3[doctype]', oute_docno='$exbc3[docno]', oute_docdate='$exbc3[docdate]'");			 
			#update qty temp
			mysql_query("UPDATE outgoing_exbc_temp SET oute_qty_exbc='$qty3' WHERE oute_prodid='$dt3[oute_prodid]' AND oute_hid='$dt3[oute_hid]' AND oute_did='$dt3[oute_did]'");
		}	
	}	
}

#insert exbc 4
$qt4 = mysql_query("SELECT * FROM outgoing_exbc_temp WHERE oute_docdate BETWEEN '$start_datec' AND '$end_datec' AND oute_status=10 AND oute_qty_exbc > 0 ORDER BY  oute_hid, oute_did ASC");
while($dt4=mysql_fetch_array($qt4)){
	$exbc4 = mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
			b.prod_code, b.prod_name, b.prod_sat FROM
			(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
			(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
			(stock_price/stock_qty) price 
			FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
			UNION ALL
			SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
			(b.incd_price/b.incd_qty) price 
			FROM incoming a
			INNER JOIN incoming_detail b ON b.incd_hid=a.id
			WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
			UNION ALL
			SELECT a.oute_prodid prodid, a.oute_doctype doctype , a.oute_docno docno, a.oute_docdate docdate, 0 inqty, a.oute_qty outqty, 0 price 
			FROM outgoing_exbc a INNER JOIN outgoing b ON b.id=a.oute_hid WHERE a.oute_status=10 AND b.outh_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
			INNER JOIN product b ON b.id=a.prodid
			WHERE b.prod_cat IN(1,3,5,6) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
			WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt4[oute_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
	if(!empty($exbc4['docno'])){
		if($exbc4['qty_bal'] >= $dt4['oute_qty_exbc']){
			$qty4   = $dt4['oute_qty_exbc'];
			$price4 = $qty4 * $exbc4['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt4[oute_hid]', oute_did='$dt4[oute_did]', oute_prodid='$dt4[oute_prodid]', 
						oute_qty='$qty4', oute_status=10, oute_doctype='$exbc4[doctype]', oute_docno='$exbc4[docno]', oute_docdate='$exbc4[docdate]'");
			#update qty temp
			mysql_query("DELETE FROM outgoing_exbc_temp WHERE oute_prodid='$dt4[oute_prodid]' AND oute_hid='$dt4[oute_hid]' AND oute_did='$dt4[oute_did]'");			 
		}else{
			$qty4   = $dt4['oute_qty_exbc'] - $exbc4['qty_bal'];
			$price4 = $exbc4['qty_bal'] * $exbc4['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt4[oute_hid]', oute_did='$dt4[oute_did]', oute_prodid='$dt4[oute_prodid]', 
						oute_qty='$exbc4[qty_bal]', oute_status=10, oute_doctype='$exbc4[doctype]', oute_docno='$exbc4[docno]', oute_docdate='$exbc4[docdate]'");			 
			#update qty temp
			mysql_query("UPDATE outgoing_exbc_temp SET oute_qty_exbc='$qty4' WHERE oute_prodid='$dt4[oute_prodid]' AND oute_hid='$dt4[oute_hid]' AND oute_did='$dt4[oute_did]'");
		}	
	}	
}

#insert exbc 5
$qt5 = mysql_query("SELECT * FROM outgoing_exbc_temp WHERE oute_docdate BETWEEN '$start_datec' AND '$end_datec' AND oute_status=10 AND oute_qty_exbc > 0 ORDER BY  oute_hid, oute_did ASC");
while($dt5=mysql_fetch_array($qt5)){
	$exbc5 = mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
			b.prod_code, b.prod_name, b.prod_sat FROM
			(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
			(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
			(stock_price/stock_qty) price 
			FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
			UNION ALL
			SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
			(b.incd_price/b.incd_qty) price 
			FROM incoming a
			INNER JOIN incoming_detail b ON b.incd_hid=a.id
			WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
			UNION ALL
			SELECT a.oute_prodid prodid, a.oute_doctype doctype , a.oute_docno docno, a.oute_docdate docdate, 0 inqty, a.oute_qty outqty, 0 price 
			FROM outgoing_exbc a INNER JOIN outgoing b ON b.id=a.oute_hid WHERE a.oute_status=10 AND b.outh_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
			INNER JOIN product b ON b.id=a.prodid
			WHERE b.prod_cat IN(1,3,5,6) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
			WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt5[oute_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
	if(!empty($exbc5['docno'])){
		if($exbc5['qty_bal'] >= $dt5['oute_qty_exbc']){
			$qty5   = $dt5['oute_qty_exbc'];
			$price5 = $qty5 * $exbc5['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt5[oute_hid]', oute_did='$dt5[oute_did]', oute_prodid='$dt5[oute_prodid]', 
						oute_qty='$qty5', oute_status=10, oute_doctype='$exbc5[doctype]', oute_docno='$exbc5[docno]', oute_docdate='$exbc5[docdate]'");
			#update qty temp
			mysql_query("DELETE FROM outgoing_exbc_temp WHERE oute_prodid='$dt5[oute_prodid]' AND oute_hid='$dt5[oute_hid]' AND oute_did='$dt5[oute_did]'");			 
		}else{
			$qty5   = $dt5['oute_qty_exbc'] - $exbc5['qty_bal'];
			$price5 = $exbc5['qty_bal'] * $exbc5['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt5[oute_hid]', oute_did='$dt5[oute_did]', oute_prodid='$dt5[oute_prodid]', 
						oute_qty='$exbc5[qty_bal]', oute_status=10, oute_doctype='$exbc5[doctype]', oute_docno='$exbc5[docno]', oute_docdate='$exbc5[docdate]'");			 
			#update qty temp
			mysql_query("UPDATE outgoing_exbc_temp SET oute_qty_exbc='$qty5' WHERE oute_prodid='$dt5[oute_prodid]' AND oute_hid='$dt5[oute_hid]' AND oute_did='$dt5[oute_did]'");
		}
	}		
}

#insert exbc 6
$qt6 = mysql_query("SELECT * FROM outgoing_exbc_temp WHERE oute_docdate BETWEEN '$start_datec' AND '$end_datec' AND oute_status=10 AND oute_qty_exbc > 0 ORDER BY  oute_hid, oute_did ASC");
while($dt6=mysql_fetch_array($qt6)){
	$exbc6 = mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
			b.prod_code, b.prod_name, b.prod_sat FROM
			(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
			(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
			(stock_price/stock_qty) price 
			FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
			UNION ALL
			SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
			(b.incd_price/b.incd_qty) price 
			FROM incoming a
			INNER JOIN incoming_detail b ON b.incd_hid=a.id
			WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
			UNION ALL
			SELECT a.oute_prodid prodid, a.oute_doctype doctype , a.oute_docno docno, a.oute_docdate docdate, 0 inqty, a.oute_qty outqty, 0 price 
			FROM outgoing_exbc a INNER JOIN outgoing b ON b.id=a.oute_hid WHERE a.oute_status=10 AND b.outh_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
			INNER JOIN product b ON b.id=a.prodid
			WHERE b.prod_cat IN(1,3,5,6) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
			WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt6[oute_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
	if(!empty($exbc6['docno'])){
		if($exbc6['qty_bal'] >= $dt6['oute_qty_exbc']){
			$qty6   = $dt6['oute_qty_exbc'];
			$price6 = $qty6 * $exbc6['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt6[oute_hid]', oute_did='$dt6[oute_did]', oute_prodid='$dt6[oute_prodid]', 
						oute_qty='$qty6', oute_status=10, oute_doctype='$exbc6[doctype]', oute_docno='$exbc6[docno]', oute_docdate='$exbc6[docdate]'");
			#update qty temp
			mysql_query("DELETE FROM outgoing_exbc_temp WHERE oute_prodid='$dt6[oute_prodid]' AND oute_hid='$dt6[oute_hid]' AND oute_did='$dt6[oute_did]'");			 
		}else{
			$qty6   = $dt6['oute_qty_exbc'] - $exbc6['qty_bal'];
			$price6 = $exbc6['qty_bal'] * $exbc6['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt6[oute_hid]', oute_did='$dt6[oute_did]', oute_prodid='$dt6[oute_prodid]', 
						oute_qty='$exbc6[qty_bal]', oute_status=10, oute_doctype='$exbc6[doctype]', oute_docno='$exbc6[docno]', oute_docdate='$exbc6[docdate]'");			 
			#update qty temp
			mysql_query("UPDATE outgoing_exbc_temp SET oute_qty_exbc='$qty6' WHERE oute_prodid='$dt6[oute_prodid]' AND oute_hid='$dt6[oute_hid]' AND oute_did='$dt6[oute_did]'");
		}
	}		
}

#insert exbc 7
$qt7 = mysql_query("SELECT * FROM outgoing_exbc_temp WHERE oute_docdate BETWEEN '$start_datec' AND '$end_datec' AND oute_status=10 AND oute_qty_exbc > 0 ORDER BY  oute_hid, oute_did ASC");
while($dt7=mysql_fetch_array($qt7)){
	$exbc7 = mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
			b.prod_code, b.prod_name, b.prod_sat FROM
			(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
			(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
			(stock_price/stock_qty) price 
			FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
			UNION ALL
			SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
			(b.incd_price/b.incd_qty) price 
			FROM incoming a
			INNER JOIN incoming_detail b ON b.incd_hid=a.id
			WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
			UNION ALL
			SELECT a.oute_prodid prodid, a.oute_doctype doctype , a.oute_docno docno, a.oute_docdate docdate, 0 inqty, a.oute_qty outqty, 0 price 
			FROM outgoing_exbc a INNER JOIN outgoing b ON b.id=a.oute_hid WHERE a.oute_status=10 AND b.outh_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
			INNER JOIN product b ON b.id=a.prodid
			WHERE b.prod_cat IN(1,3,5,6) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
			WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt7[oute_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
	if(!empty($exbc7['docno'])){
		if($exbc7['qty_bal'] >= $dt7['oute_qty_exbc']){
			$qty7   = $dt7['oute_qty_exbc'];
			$price7 = $qty7 * $exbc7['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt7[oute_hid]', oute_did='$dt7[oute_did]', oute_prodid='$dt7[oute_prodid]', 
						oute_qty='$qty7', oute_status=10, oute_doctype='$exbc7[doctype]', oute_docno='$exbc7[docno]', oute_docdate='$exbc7[docdate]'");
			#update qty temp
			mysql_query("DELETE FROM outgoing_exbc_temp WHERE oute_prodid='$dt7[oute_prodid]' AND oute_hid='$dt7[oute_hid]' AND oute_did='$dt7[oute_did]'");			 
		}else{
			$qty7   = $dt7['oute_qty_exbc'] - $exbc7['qty_bal'];
			$price7 = $exbc6['qty_bal'] * $exbc7['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt7[oute_hid]', oute_did='$dt7[oute_did]', oute_prodid='$dt7[oute_prodid]', 
						oute_qty='$exbc7[qty_bal]', oute_status=10, oute_doctype='$exbc7[doctype]', oute_docno='$exbc7[docno]', oute_docdate='$exbc7[docdate]'");			 
			#update qty temp
			mysql_query("UPDATE outgoing_exbc_temp SET oute_qty_exbc='$qty7' WHERE oute_prodid='$dt7[oute_prodid]' AND oute_hid='$dt7[oute_hid]' AND oute_did='$dt7[oute_did]'");
		}	
	}	
}
#insert exbc 8
$qt8 = mysql_query("SELECT * FROM outgoing_exbc_temp WHERE oute_docdate BETWEEN '$start_datec' AND '$end_datec' AND oute_status=10 AND oute_qty_exbc > 0 ORDER BY  oute_hid, oute_did ASC");
while($dt8=mysql_fetch_array($qt8)){
	$exbc8 = mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
			b.prod_code, b.prod_name, b.prod_sat FROM
			(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
			(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
			(stock_price/stock_qty) price 
			FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
			UNION ALL
			SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
			(b.incd_price/b.incd_qty) price 
			FROM incoming a
			INNER JOIN incoming_detail b ON b.incd_hid=a.id
			WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
			UNION ALL
			SELECT a.oute_prodid prodid, a.oute_doctype doctype , a.oute_docno docno, a.oute_docdate docdate, 0 inqty, a.oute_qty outqty, 0 price 
			FROM outgoing_exbc a INNER JOIN outgoing b ON b.id=a.oute_hid WHERE a.oute_status=10 AND b.outh_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
			INNER JOIN product b ON b.id=a.prodid
			WHERE b.prod_cat IN(1,3,5,6) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
			WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt8[oute_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
	if(!empty($exbc8['docno'])){
		if($exbc8['qty_bal'] >= $dt8['oute_qty_exbc']){
			$qty8   = $dt8['oute_qty_exbc'];
			$price8 = $qty8 * $exbc8['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt8[oute_hid]', oute_did='$dt8[oute_did]', oute_prodid='$dt8[oute_prodid]', 
						oute_qty='$qty8', oute_status=10, oute_doctype='$exbc8[doctype]', oute_docno='$exbc8[docno]', oute_docdate='$exbc8[docdate]'");
			#update qty temp
			mysql_query("DELETE FROM outgoing_exbc_temp WHERE oute_prodid='$dt8[oute_prodid]' AND oute_hid='$dt8[oute_hid]' AND oute_did='$dt8[oute_did]'");			 
		}else{
			$qty8   = $dt8['oute_qty_exbc'] - $exbc8['qty_bal'];
			$price8 = $exbc8['qty_bal'] * $exbc8['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt8[oute_hid]', oute_did='$dt8[oute_did]', oute_prodid='$dt8[oute_prodid]', 
						oute_qty='$exbc8[qty_bal]', oute_status=10, oute_doctype='$exbc8[doctype]', oute_docno='$exbc8[docno]', oute_docdate='$exbc8[docdate]'");			 
			#update qty temp
			mysql_query("UPDATE outgoing_exbc_temp SET oute_qty_exbc='$qty8' WHERE oute_prodid='$dt8[oute_prodid]' AND oute_hid='$dt8[oute_hid]' AND oute_did='$dt8[oute_did]'");
		}	
	}	
}

#insert exbc 9
$qt9 = mysql_query("SELECT * FROM outgoing_exbc_temp WHERE oute_docdate BETWEEN '$start_datec' AND '$end_datec' AND oute_status=10 AND oute_qty_exbc > 0 ORDER BY  oute_hid, oute_did ASC");
while($dt9=mysql_fetch_array($qt9)){
	$exbc9 = mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
			b.prod_code, b.prod_name, b.prod_sat FROM
			(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
			(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
			(stock_price/stock_qty) price 
			FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
			UNION ALL
			SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
			(b.incd_price/b.incd_qty) price 
			FROM incoming a
			INNER JOIN incoming_detail b ON b.incd_hid=a.id
			WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
			UNION ALL
			SELECT a.oute_prodid prodid, a.oute_doctype doctype , a.oute_docno docno, a.oute_docdate docdate, 0 inqty, a.oute_qty outqty, 0 price 
			FROM outgoing_exbc a INNER JOIN outgoing b ON b.id=a.oute_hid WHERE a.oute_status=10 AND b.outh_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
			INNER JOIN product b ON b.id=a.prodid
			WHERE b.prod_cat IN(1,3,5,6) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
			WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt9[oute_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
	if(!empty($exbc9['docno'])){
		if($exbc9['qty_bal'] >= $dt9['oute_qty_exbc']){
			$qty9   = $dt9['oute_qty_exbc'];
			$price9 = $qty9 * $exbc9['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt9[oute_hid]', oute_did='$dt9[oute_did]', oute_prodid='$dt9[oute_prodid]', 
						oute_qty='$qty9', oute_status=10, oute_doctype='$exbc9[doctype]', oute_docno='$exbc9[docno]', oute_docdate='$exbc9[docdate]'");
			#update qty temp
			mysql_query("DELETE FROM outgoing_exbc_temp WHERE oute_prodid='$dt9[oute_prodid]' AND oute_hid='$dt9[oute_hid]' AND oute_did='$dt9[oute_did]'");			 
		}else{
			$qty9   = $dt9['oute_qty_exbc'] - $exbc9['qty_bal'];
			$price9 = $exbc9['qty_bal'] * $exbc9['pricein'];
			#insert detail
			mysql_query("INSERT INTO outgoing_exbc SET oute_hid='$dt9[oute_hid]', oute_did='$dt9[oute_did]', oute_prodid='$dt9[oute_prodid]', 
						oute_qty='$exbc9[qty_bal]', oute_status=10, oute_doctype='$exbc9[doctype]', oute_docno='$exbc9[docno]', oute_docdate='$exbc9[docdate]'");			 
			#update qty temp
			mysql_query("UPDATE outgoing_exbc_temp SET oute_qty_exbc='$qty9' WHERE oute_prodid='$dt9[oute_prodid]' AND oute_hid='$dt9[oute_hid]' AND oute_did='$dt9[oute_did]'");
		}	
	}	
}
#delete temp
mysql_query("DELETE FROM outgoing_exbc_temp WHERE oute_docdate BETWEEN '$start_datec' AND '$end_datec'");
#delete qty 0 
mysql_query("DELETE FROM outgoing_exbc WHERE oute_qty <=0");

$prof 	= mysql_fetch_array(mysql_query("SELECT prof_name FROM profil"));


?>
<table width="100%" border="0">
    <tr>
  		<td colspan="15" align="center">LAPORAN PENGELUARAN&nbsp;<?=$doc_type;?></td>
    </tr>
    <tr>
  		<td colspan="15" align="center">DETAIL EXBC</td>
    </tr>
</table>	
<table width="100%" border="1">	
  <tr>
    <th align="center" bgcolor="#9df990" rowspan="2">NO</th>
    <th align="center" bgcolor="#9df990" colspan="6">DETAIL BARANG JADI</th>
	<th align="center" bgcolor="#f790f9" colspan="8">DETAIL BAHAN BAKU</th>
  </tr>
  <tr>
	<th align="center" bgcolor="#9df990">NOMOR</th>
	<th align="center" bgcolor="#9df990">TANGGAL</th>
	<th align="center" bgcolor="#9df990">KODE</th>
	<th align="center" bgcolor="#9df990">NAMA</th>
	<th align="center" bgcolor="#9df990">SATUAN</th>
	<th align="center" bgcolor="#9df990">JUMLAH</th>	
	<th align="center" bgcolor="#f790f9">NO</th>
	<th align="center" bgcolor="#f790f9">JENIS</th>
    <th align="center" bgcolor="#f790f9">NOMOR</th>
    <th align="center" bgcolor="#f790f9">TANGGAL</th>
	<th align="center" bgcolor="#f790f9">KODE</th>
	<th align="center" bgcolor="#f790f9">NAMA</th>
	<th align="center" bgcolor="#f790f9">SATUAN</th>
	<th align="center" bgcolor="#f790f9">JUMLAH</th>
  </tr>
  <?php $no1=1;
	$q1 = mysql_query("SELECT a.id hid, b.id did, a.outh_docno, a.outh_docdate, c.prod_code, c.prod_name, c.prod_sat, b.outd_qty, b.outd_prodid, c.prod_cat FROM outgoing a
		  INNER JOIN outgoing_detail b ON b.outd_hid=a.id
		  INNER JOIN product c ON c.id=b.outd_prodid
		  WHERE a.outh_status=20 AND a.outh_doctype='$doc_type' AND a.outh_docdate BETWEEN '$start_datec' AND '$end_datec' 
		  ORDER BY a.outh_docdate ASC"); 
	while ($d1=mysql_fetch_array($q1)){
  ?>	
	<tr>
		<td align="center"><?=$no1;?></td>
		<td align="center">'<?=$d1['outh_docno'];?></td>
		<td align="center"><?=$d1['outh_docdate'];?></td>
		<td><?=$d1['prod_code'];?></td>
		<td><?=$d1['prod_name'];?></td>
		<td align="center"><?=$d1['prod_sat'];?></td>
		<td align="right"><?=$d1['outd_qty'];?></td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>	
		<td>&nbsp;</td>	
	</tr>
  <?php $no2=1;
    #cek
	$q2 = mysql_query("SELECT a.*, b.prod_code, b.prod_name, b.prod_sat FROM outgoing_exbc a INNER JOIN product b ON b.id=a.oute_prodid WHERE a.oute_hid='$d1[hid]' AND a.oute_did='$d1[did]' ORDER BY a.id ASC"); 
	while ($d2=mysql_fetch_array($q2)){	
  ?>  
	<tr>		
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td align="center"><?=$no2;?></td>
		<td align="center"><?=$d2['oute_doctype'];?></td>
		<td align="center">'<?=$d2['oute_docno'];?></td>
		<td align="center"><?=$d2['oute_docdate'];?></td>
		<td><?=$d2['prod_code'];?></td>
		<td><?=$d2['prod_name'];?></td>
		<td align="center"><?=$d2['prod_sat'];?></td>
		<td align="right"><?=$d2['oute_qty'];?></td>
	</tr> 	
  <?php $no2++; } $no1++; } ?>		
</table>

