<?php
include "../functions/dblink.php" ; 
if(isset($_POST['begin']) AND isset($_POST['finish'])){
	$begin=$_POST['begin'];
	$finish=$_POST['finish'];
    $sql = "SELECT * FROM quot002 
	        WHERE (QDATE BETWEEN '$begin' AND '$finish') AND (QLOCK=0 OR QLOCK='' OR QLOCK IS NULL)";
	$prev="href='quotExportForm.php' ";
	$string="Quotation Date : $begin to $finish";
}
//$sql = "SELECT * FROM member";
//$result = mysqli_query($con, $sql);

require_once '../Classes/PHPExcel.php';
$spreadsheet = new PHPExcel();

// Set the column headers
$sheet = $spreadsheet->createSheet();
$sheet->setTitle('DB to XLSX');
$sheet->setCellValue('A1', 'Date');
$sheet->setCellValue('B1', 'Quot.No.');
$sheet->setCellValue('C1', 'Cust.Name');
$sheet->setCellValue('D1', 'ส่วนลด');
$sheet->setCellValue('E1', 'สินค้า1');
$sheet->setCellValue('F1', 'จำนวน');
$sheet->setCellValue('G1', 'สินค้า2');
$sheet->setCellValue('H1', 'จำนวน');
$sheet->setCellValue('I1', 'สินค้า3');
$sheet->setCellValue('J1', 'จำนวน');
$sheet->setCellValue('K1', 'สินค้า4');
$sheet->setCellValue('L1', 'จำนวน');
$sheet->setCellValue('M1', 'สินค้า5');
$sheet->setCellValue('N1', 'จำนวน');
$sheet->setCellValue('O1', 'สินค้า6');
$sheet->setCellValue('P1', 'จำนวน');
$sheet->setCellValue('Q1', 'สินค้า7');
$sheet->setCellValue('R1', 'จำนวน');
$sheet->setCellValue('S1', 'สินค้า8');
$sheet->setCellValue('T1', 'จำนวน');
$sheet->setCellValue('U1', 'สินค้า9');
$sheet->setCellValue('V1', 'จำนวน');
$sheet->setCellValue('W1', 'สินค้า10');
$sheet->setCellValue('X1', 'จำนวน');
$sheet->setCellValue('Y1', 'ผู้ขาย');
$sheet->setCellValue('Z1', 'เงิน(บาท)');

// Fill in the data to the sheet
$row = 2;
$result = mysqli_query($con,$sql);
while ($row_data = mysqli_fetch_array($result)) {
    $sheet->setCellValue('A' . $row, $row_data['QDATE']);
    $sheet->setCellValue('B' . $row, $row_data['QNO']);
    $sheet->setCellValue('C' . $row, $row_data['QC_NAME']);
    $sheet->setCellValue('D' . $row, $row_data['QC_DISC']);
    $sheet->setCellValue('E' . $row, $row_data['QPDNAME1']);
    $sheet->setCellValue('F' . $row, $row_data['QQTY1']);
    $sheet->setCellValue('G' . $row, $row_data['QPDNAME2']);
    $sheet->setCellValue('H' . $row, $row_data['QQTY2']);
    $sheet->setCellValue('I' . $row, $row_data['QPDNAME3']);
    $sheet->setCellValue('J' . $row, $row_data['QQTY3']);
    $sheet->setCellValue('K' . $row, $row_data['QPDNAME4']);
    $sheet->setCellValue('L' . $row, $row_data['QQTY4']);
    $sheet->setCellValue('M' . $row, $row_data['QPDNAME5']);
    $sheet->setCellValue('N' . $row, $row_data['QQTY5']);
    $sheet->setCellValue('O' . $row, $row_data['QPDNAME6']);
    $sheet->setCellValue('P' . $row, $row_data['QQTY6']);
    $sheet->setCellValue('Q' . $row, $row_data['QPDNAME7']);
    $sheet->setCellValue('R' . $row, $row_data['QQTY7']);
    $sheet->setCellValue('S' . $row, $row_data['QPDNAME8']);
    $sheet->setCellValue('T' . $row, $row_data['QQTY8']);
    $sheet->setCellValue('U' . $row, $row_data['QPDNAME9']);
    $sheet->setCellValue('V' . $row, $row_data['QQTY9']);
    $sheet->setCellValue('W' . $row, $row_data['QPDNAME10']);
    $sheet->setCellValue('X' . $row, $row_data['QQTY10']);
    $sheet->setCellValue('Y' . $row, $row_data['QSMFNAME']);
    $sheet->setCellValue('Z' . $row, $row_data['QSUM']);
    $row++;
}

$writer = PHPExcel_IOFactory::createWriter($spreadsheet, 'Excel2007');
$writer->save('/Users/applemacintosh/Downloads/QuotExport.xlsx');
//$writer->save('php://output');
include "quotExportForm.php";
?>
<br>
<div style="border: 1px solid red; margin-left: 332px; width: 530px; height: 30px;">
    <p style="text-align: center; margin-top: 3px;">Export DB to QuotExport.xlsx Complete !</p>
</div>
<?php
mysqli_close($con);
?>