<?php
	include '../../inc/inc.koneksi.php';	
	include '../../inc/fungsi_tanggal.php';

	$tgl_awal	= $_GET['tgl_awal'];
	$tgl_akhir= $_GET['tgl_akhir'];	
	$tjumlah=0;
	$tdiskon=0;

	$content = "<b><li>DAFTAR PENJUALAN BARANG DAGANGAN</li>
		<li>TANGGAL : ".jin_date_str($tgl_awal)." S/D ".jin_date_str($tgl_akhir)."</li></b>
		<br>
	
		<table width='390' border='1' style='border-collapse:collapse'>
		<tr>
			<th>No.</th>
			<th>Kode</th>
			<th>Nama Barang</th>
			<th>Satuan</th>
			<th>Jumlah Unit</th>
			<th>Harga Bruto</th>
			<th>Potongan</th>
			<th>Harga Netto</th>
			<th>Total Harga</th>			
		</tr>";
		
		$query2	= "SELECT kode_pj,tanggal,kode_plg,nama_plg,count(kode_st) as totitem FROM faktur_pj 
			WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir'	AND batal='' GROUP BY kode_pj";
		$data2	= mysql_query($query2);
		while($r_data2=mysql_fetch_array($data2)){
			$kodepj=$r_data2[kode_pj];

		$content .= "<tr>	
				<td align='center'><b>Faktur</b></td>
				<td><b>$r_data2[kode_pj]</b></td>								
				<td><b>$r_data2[kode_plg] - $r_data2[nama_plg]</b></td>
				<td align='center'><b>".jin_date_str($r_data2[tanggal])."</b></td>
			</tr>";
			
		$sql = "SELECT * FROM faktur_pj WHERE kode_pj='$kodepj' ";		
		$query = mysql_query($sql);
		
		$no=1+$hal;
		$sjumlah=0;
		$sdiskon=0;
		while($r_data=mysql_fetch_array($query)){
			$hgnet=$r_data[hg_jual]-$r_data[potongan];
				
			$content .= "<tr>
				<td align='center'>$no</td>
				<td align='center'>$r_data[kode_st]</td>
				<td>$r_data[nama_st]</td>
				<td align='center'>$r_data[satuan]</td>
				<td align='right'>$r_data[jml_unit]</td>
				<td align='right'>$r_data[hg_jual]</td>
				<td align='right'>$r_data[potongan]</td>
				<td align='right'>$hgnet</td>
				<td align='right'>".number_format($r_data[jumlah])."</td>
			</tr>";
			$sjumlah=$sjumlah+$r_data[jumlah];
			$sdiskon=$sdiskon+$r_data[diskon];
			$no++;			
		}
		$netto=$sjumlah-$sdiskon;
		
		$content .= "<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Jumlah</td>
				<td align='right'>".number_format($sjumlah)."</td>
			</tr>
			<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Diskon</td>
				<td align='right'>".number_format($sdiskon)."</td>
			</tr>
			<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Netto</td>
				<td align='right'>".number_format($netto)."</td>
			</tr>";
		$tjumlah=$tjumlah+$sjumlah;
		$tdiskon=$tdiskon+$sdiskon;
	}
	$netto=$tjumlah-$tdiskon;
	
	$content .= "<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Total </td>
			<td align='right'>".number_format($tjumlah)."</td>
		</tr>
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Total </td>
			<td align='right'>".number_format($tdiskon)."</td>
		</tr>
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Total </td>
			<td align='right'>".number_format($netto)."</td>
		</tr>";
		$content .= "</table>";
		
		header("Content-type: application/x-msdownload");
		header("Content-Disposition: attachment; filename=daftar_pjl.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		echo $content;
?>