<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

?>

<html>
<head>
	<title>Tarik BC</title>
</head>
<body style="Font-family:arial">
<center style="margin-top:50; font-family:arial;"><h3>TARIK DATA BILLING COLLECTIVE</h3></center>
<table border='10' height="180" align="center" width="600">
	<tr >
		<td align='center' ><form action="" method="post">
		<center><h4 style="Font-family:arial; margin-bottom:20;">PILIH TANGGAL</h4></center>
			Tanggal : <input type="date" name='tanggal_awal'> s/d <input type="date" name='tanggal_akhir'>
			<button type="submit" name="submit">CEK</button>
		</form>
		</td >
</table>
<?php
$conn = false;
$kueri = false;
$Hari = false;

if(isset($_POST['submit'])){
	if(isset($_POST['tanggal_awal']) && isset($_POST['tanggal_akhir'])){
		$tanggal_awal = $_POST['tanggal_awal'];
		$tanggal_akr = $_POST['tanggal_akhir'];
		$conn = mysqli_connect('localhost','admin', 'admin','billing_collective');
		$kueri = mysqli_query($conn, "SELECT * FROM bc_manifest_detil WHERE Tanggal_Pembayaran between '$tanggal_awal' and '$tanggal_akr' ");
		$Hari = $tanggal_awal.'-'.$tanggal_akr;
	}
	
	/***
	else{
		$bulan = $_POST['tahun']."-".$_POST['bulan'];
		$conn = mysqli_connect('172.19.23.68','bill', 'bill','billing_collective');
		$kueri = mysqli_query($conn, "SELECT * FROM bc_manifest_detil WHERE Tanggal_Pembayaran LIKE '$bulan%'");
		$Hari = $bulan;
	}
	***/
	//getData($tanggal);
	
		if($conn->affected_rows >= 1){
				$spreadsheet = new Spreadsheet();
				$sheet = $spreadsheet->getActiveSheet();
				$sheet->setCellValue('A1', 'NOP');
				$sheet->setCellValue('B1', 'NAMA');
				$sheet->setCellValue('C1', 'TAHUN PAJAK');
				$sheet->setCellValue('D1', 'ID BILLING');
				$sheet->setCellValue('E1', 'POKOK');
				$sheet->setCellValue('F1', 'DENDA');
				$sheet->setCellValue('G1', 'TANGGAL PEMBAYARAN');

				$nomor = 1;
				while($row = mysqli_fetch_row($kueri)){
					$nomor++;
					
					$sheet->setCellValue('A'.$nomor, "'".$row[1].$row[2].$row[3].$row[4].$row[5].$row[6].$row[7]);
					$sheet->getStyle('A'.$nomor)->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
					$sheet->setCellValue('B'.$nomor, $row[9]);
					$sheet->setCellValue('C'.$nomor, $row[8]);
					$sheet->setCellValue('D'.$nomor, "'".$row[13]);
					$sheet->getStyle('D'.$nomor)->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
					$sheet->setCellValue('E'.$nomor, $row[11]);
					$sheet->setCellValue('F'.$nomor, $row[12]);
					$sheet->setCellValue('G'.$nomor, $row[21]);
				}

				$writer = new Xlsx($spreadsheet);
				
				$writer->save('generate/billing_'.$Hari.'.xlsx');
				
				print "<center><br><b><a href='generate/billing_$Hari.xlsx'>Download Data BC $Hari</a>";
		}else{
			print "<center><br><b>Data Tidak Ada !";
		}
}
?>
	
</body>
</html>