<?php
session_start();
error_reporting(0);
if($_SESSION['id_user']){
    require_once('inc/dbcon.php');
	require_once('inc/function.php');
    require_once('inc/PHPExcel/PHPExcel.php');
    ob_start();
    $excel= new PHPExcel();
    $excel->setActiveSheetIndex(0)->setCellValue('A1','TOKO');
    $excel->setActiveSheetIndex(0)->setCellValue('B1','KASIR');
    $excel->setActiveSheetIndex(0)->setCellValue('C1','STRUK');
    $excel->setActiveSheetIndex(0)->setCellValue('D1','ITEM');
    $excel->setActiveSheetIndex(0)->setCellValue('E1','QTY');
    $excel->setActiveSheetIndex(0)->setCellValue('F1','JUMLAH');
    $excel->setActiveSheetIndex(0)->setCellValue('G1','SUBTOTAL');
    $excel->setActiveSheetIndex(0)->setCellValue('H1','DISKON');
    $excel->setActiveSheetIndex(0)->setCellValue('I1','PPN');
    $excel->setActiveSheetIndex(0)->setCellValue('J1','GRANDTOTAL');
    $excel->setActiveSheetIndex(0)->setCellValue('K1','TANGGAL');
    $excel->getActiveSheet()->getStyle('A1:I1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $i=1;
    $idu = $_SESSION['id_user'];
    $q=mysqli_query($connect, "select a.*,b.nama_toko from transaksi a, toko b where b.ID_user=a.ID_user and a.ID_user='$idu' order by a.id_ desc");
    while($b=mysqli_fetch_array($q)){
        $i++;
        $q_item=mysqli_query($connect, "select a.*,b.nama_produk from transaksi_detail a, produk b where b.ID=a.ID_produk and a.ID_transaksi='$b[id_]'");
        $ii=($i-1);
        while($b_item=mysqli_fetch_array($q_item)){
            $ii++;
            $excel->setActiveSheetIndex(0)->setCellValue('D'.$ii,$b_item['nama_produk']);
            $excel->setActiveSheetIndex(0)->setCellValue('E'.$ii,$b_item['qty']);
            $excel->setActiveSheetIndex(0)->setCellValue('F'.$ii,$b_item['total']);
        }
        $kar=mysqli_fetch_array(mysqli_query($connect, "select nama from karyawan where ID_user='$idu' and email='$b[email_karyawan]'"));
        if($kar[0]){
            $kasir=$kar[0];
        }else{
            $kasir=$_SESSION['nama_user'];
        }
        $excel->setActiveSheetIndex(0)->setCellValue('A'.$i,$b['nama_toko']);
        $excel->setActiveSheetIndex(0)->setCellValue('B'.$i,$kasir);
        $excel->setActiveSheetIndex(0)->setCellValue('C'.$i,$b['no_struk']);
        $excel->setActiveSheetIndex(0)->setCellValue('G'.$i,$b['subtotal']);
        $excel->setActiveSheetIndex(0)->setCellValue('H'.$i,$b['diskon']);
        $excel->setActiveSheetIndex(0)->setCellValue('I'.$i,(($b['pajak']==1)?'Ya':'Tidak'));
        $excel->setActiveSheetIndex(0)->setCellValue('J'.$i,$b['grandtotal']);
        $excel->setActiveSheetIndex(0)->setCellValue('K'.$i,$b['waktu']);
        $excel->setActiveSheetIndex(0)->getStyle('B'.$i)->setQuotePrefix(true);
        $excel->setActiveSheetIndex(0)->mergeCells('A'.$i.':A'.$ii);
        $excel->setActiveSheetIndex(0)->mergeCells('B'.$i.':B'.$ii);
        $excel->setActiveSheetIndex(0)->mergeCells('C'.$i.':C'.$ii);
        $excel->setActiveSheetIndex(0)->mergeCells('G'.$i.':G'.$ii);
        $excel->setActiveSheetIndex(0)->mergeCells('H'.$i.':H'.$ii);
        $excel->setActiveSheetIndex(0)->mergeCells('I'.$i.':I'.$ii);
        $excel->setActiveSheetIndex(0)->mergeCells('J'.$i.':J'.$ii);
        $excel->setActiveSheetIndex(0)->mergeCells('K'.$i.':K'.$ii);
        $excel->setActiveSheetIndex(0)->getStyle('A'.$i.':A'.$ii)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $excel->setActiveSheetIndex(0)->getStyle('A'.$i.':A'.$ii)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $excel->setActiveSheetIndex(0)->getStyle('B'.$i.':B'.$ii)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $excel->setActiveSheetIndex(0)->getStyle('B'.$i.':B'.$ii)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $excel->setActiveSheetIndex(0)->getStyle('C'.$i.':C'.$ii)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $excel->setActiveSheetIndex(0)->getStyle('C'.$i.':C'.$ii)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $excel->setActiveSheetIndex(0)->getStyle('G'.$i.':G'.$ii)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $excel->setActiveSheetIndex(0)->getStyle('H'.$i.':H'.$ii)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $excel->setActiveSheetIndex(0)->getStyle('I'.$i.':I'.$ii)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $excel->setActiveSheetIndex(0)->getStyle('J'.$i.':J'.$ii)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $excel->setActiveSheetIndex(0)->getStyle('K'.$i.':K'.$ii)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        
    }
    $excel->getActiveSheet(0)->setTitle("DATA TRANSAKSI");
    $excel->setActiveSheetIndex(0);
    $write =new PHPExcel_Writer_Excel5($excel);
    $write->save('php://output');
    $data = ob_get_contents();
    ob_end_clean();
    ob_flush();
    header('Connection: Keep-alive');
	header('Access-control-allow-origin: *');
	header('Content-Description: File Transfer');
	header('Content-Disposition: attachment; filename="Bluepos_Trx_'.date('YmdHis').'.xls"');
	header('Content-type: application/vnd.ms-excel');
	header('Expires: 0');
	header('Pragma: public');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    set_time_limit(0);
    echo $data;
    exit();
}else{
    header("HTTP/1.0 404 Not Found");
}