<?php
require '../../vendor/autoload.php';
include("../../packages/require.php");
include("controller/controller_export.php");
date_default_timezone_set("Asia/Jakarta");
$files_name = "Report Purchase Order ".$date_view;

function convertStatus($param){
    $result = "";

    if($param == "open"){
        $result = "Open";
    } else if($param == "progress") {
        $result = "In Progress";
    } else if($param == "prepared") {
        $result = "Prepared";
    } else if($param == "complete") {
        $result = "Complete";
    } else if($param == "cancel") {
        $result = "Cancel";
    }

    return $result;
}

function get_ppn_amount($total, $number)
{
    if ( $total > 0 ) {
        return round($number * ($total / 100),0);
    } else {
        return 0;
    }
}

function get_total_amount($subtotal, $ppn_amount)
{
    $result = 0;
    $result = $subtotal + $ppn_amount;
    return $result;
}

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$sheet->setCellValue('A1', 'PO ID');
$sheet->setCellValue('B1', 'Purchase Order No');
$sheet->setCellValue('C1', 'Document Date');
$sheet->setCellValue('D1', 'Status');
$sheet->setCellValue('E1', 'Region');
$sheet->setCellValue('F1', 'Client Name');
$sheet->setCellValue('G1', 'Sub Client');
$sheet->setCellValue('H1', 'Client Email');
$sheet->setCellValue('I1', 'Client Phone');
$sheet->setCellValue('J1', 'PIC');
$sheet->setCellValue('K1', 'Description');
$sheet->setCellValue('L1', 'Product');
$sheet->setCellValue('M1', 'Qty');
$sheet->setCellValue('N1', 'Unit');
if($acsp_hide_amount != 1){
$sheet->setCellValue('O1', 'Price');
$sheet->setCellValue('P1', 'Subtotal');
$sheet->setCellValue('Q1', 'PPN');
$sheet->setCellValue('R1', 'Total');
$sheet->setCellValue('S1', 'Invoice No');
$sheet->setCellValue('T1', 'PO Notes');
} else {
$sheet->setCellValue('O1', 'Invoice No');
$sheet->setCellValue('P1', 'PO Notes');  
}

if(is_array($datas)){ 
    $i = 2; 
    foreach($datas as $data){

        $spreadsheet->getActiveSheet()->getStyle('I'.$i.'')->getNumberFormat()
    ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER);

        $spreadsheet->getActiveSheet()->getStyle('M'.$i.'')->getNumberFormat()
    ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER);

        // $sum_cell = $data['po_item_total'];

        // if($data['po_item_total'] >= 1){
        //     $sum_cell = ( $i + $data['po_item_total'] ) - 1;
        // } else {
        //     $sum_cell = $i + $data['po_item_total'];
        // }

        // $spreadsheet->getActiveSheet()->mergeCells('P'.$i.':P'.$sum_cell.'');
        // $spreadsheet->getActiveSheet()->mergeCells('Q'.$i.':Q'.$sum_cell.'');
        // $spreadsheet->getActiveSheet()->mergeCells('R'.$i.':R'.$sum_cell.'');

        $ppn_amount = get_ppn_amount($data['atpo_price'], $data['po_ppn']);
        $total_amount = get_total_amount($data['atpo_price'], $ppn_amount);

        $sheet->setCellValue('A'.$i, $data['po_id']);
        $sheet->setCellValue('B'.$i, correctDisplay($data['po_order_num']));
        $sheet->setCellValue('C'.$i, date("d/m/Y", strtotime($data['po_date'])));
        $sheet->setCellValue('D'.$i, convertStatus($data['po_status']));
        $sheet->setCellValue('E'.$i, ucfirst($data['region_title']));
        $sheet->setCellValue('F'.$i, ucfirst($data['client_name']));
        $sheet->setCellValue('G'.$i, ucfirst($data['po_subclient']));
        $sheet->setCellValue('H'.$i, $data['client_email']);
        $sheet->setCellValue('I'.$i, $data['client_phone']);
        $sheet->setCellValue('J'.$i, ucfirst($data['admin_name']));
        $sheet->setCellValue('K'.$i, correctDisplay($data['atpo_desc']));
        $sheet->setCellValue('L'.$i, correctDisplay($data['atpo_product']));
        $sheet->setCellValue('M'.$i, $data['atpo_qty']);
        $sheet->setCellValue('N'.$i, ucfirst($data['atpo_unit']));
        if($acsp_hide_amount != 1){
            $sheet->setCellValue('O'.$i, $data['atpo_price']);
            $sheet->setCellValue('P'.$i, $data['atpo_price']);
            $sheet->setCellValue('Q'.$i, $ppn_amount);
            $sheet->setCellValue('R'.$i, $total_amount);
            if($data['po_invoice'] != NULL && $data['po_invoice'] != ""){
            $sheet->setCellValue('S'.$i, correctDisplay($data['po_invoice']));
            } else {
            $sheet->setCellValue('S'.$i, "-");
            }
            $sheet->setCellValue('T'.$i, correctDisplay($data['po_notes']));
        } else {
            if($data['po_invoice'] != NULL && $data['po_invoice'] != ""){
            $sheet->setCellValue('O'.$i, correctDisplay($data['po_invoice']));
            } else {
            $sheet->setCellValue('O'.$i, "-");
            }
            $sheet->setCellValue('P'.$i, correctDisplay($data['po_notes']));
        }
        $i++;
    }
} else {
    $i = 2;
    if($acsp_hide_amount != 1){
        $spreadsheet->getActiveSheet()->mergeCells('A2:T2');
    } else {
        $spreadsheet->getActiveSheet()->mergeCells('A2:P2');
    }
    $sheet->setCellValue('A2', 'There is no data right now');
}

$styleArrayHeader = [
    'font' => [
        'bold' => true,
    ],
    'alignment' => [
        'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
    ],
];
if($acsp_hide_amount != 1){
    $sheet->getStyle('A1:T1')->applyFromArray($styleArrayHeader);
} else {
    $sheet->getStyle('A1:P1')->applyFromArray($styleArrayHeader);
}
$styleArray = [
    'borders' => [
        'allBorders' => [
            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
        ],
    ],
    'alignment' => [
        'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
    ],
];
$i = $i - 1;
if($acsp_hide_amount != 1){
    $sheet->getStyle('A1:T'.$i)->applyFromArray($styleArray);

    foreach (range('A','T') as $col) {
        $spreadsheet->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
    }
} else {
    $sheet->getStyle('A1:P'.$i)->applyFromArray($styleArray);

    foreach (range('A','P') as $col) {
        $spreadsheet->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
    }
}

$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$files_name.'.xlsx"');
ob_end_clean();
$writer->save("php://output");
die();
?>