<?php

// ==================
// Export Permits
// ==================

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

class Export_AllPermitsByBuilderExcel extends Base {

    // ==================
    // Class Constructor.
    // ==================

    public function __construct() {

        set_error_handler(array($this, "displayError"));

        date_default_timezone_set(self::TIMEZONE);
    }

    // ===============
    // Export the Data
    // ===============

    public function export() {

        set_time_limit(0);

        $subscription_serial = filter_input(INPUT_POST, "subscription_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;

        $subscription = (new Subscriptions())->getSubscription($subscription_serial);

        $permit_area = $subscription->record->subscription_area;
        $projecttype = $subscription->record->subscription_projecttype;

        $permits = (new PermitsByBuilder())->getAllPermits($permit_area, $projecttype);

        // Create the Spreadsheet.

        $spreadsheet = new Spreadsheet();

        $spreadsheet->getProperties()->setCreator("DEC International")
                ->setTitle("Permits")
                ->setSubject("Permits")
                ->setDescription("Permits")
                ->setKeywords("Permits")
                ->setCategory("Permits");

        // Create the Base Sheet.

        $spreadsheet->setActiveSheetIndex(0);
        $spreadsheet->getActiveSheet()->setTitle("Permits");

        // Format the Cells and Create the Headings.

        $spreadsheet->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("E")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("F")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("G")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("H")->setAutoSize(true);

        $spreadsheet->getActiveSheet()->getStyle("A1:H1")->getFont()->setBold(true);
        $spreadsheet->getActiveSheet()->getStyle("A1:H1")->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB("D3D3D3");

        $spreadsheet->setActiveSheetIndex(0)->setCellValue("A1", "Builder/Company")
                ->setCellValue("B1", "Permits")
                ->setCellValue("C1", "Value")
                ->setCellValue("D1", "Current Year YTD")
                ->setCellValue("E1", "Current Year YTD Value")
                ->setCellValue("F1", "Last 12 Months")
                ->setCellValue("G1", "Address")
                ->setCellValue("H1", "Phone");

        $spreadsheet->getActiveSheet()->getStyle("A:A")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER);
        $spreadsheet->getActiveSheet()->getStyle("C:C")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("D:D")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("E:E")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("F:F")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("G:G")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("H:H")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);

        $spreadsheet->getActiveSheet()->freezePane("A2");

        // Create the Detail Rows.

        $row = 2;

        $permit_ytd_value = 0.00;
        $permit_value = 0.00;

        foreach ($permits as $permit) {

            $address = "{$permit->permit_address} {$permit->permit_city}, {$permit->permit_state} {$permit->permit_zip}";

            $permit_ytd_value = "$" . number_format((int) $permit->ytd_value, 2);
            $permit_value = "$" . number_format((int) $permit->permit_value_verbose, 2);

            $spreadsheet->getActiveSheet()->setCellValue("A" . $row, (string) $permit->permit_company);
            $spreadsheet->getActiveSheet()->setCellValue("B" . $row, (string) $permit->permit_permit_count);
            $spreadsheet->getActiveSheet()->setCellValue("C" . $row, (string) $permit_value);
            $spreadsheet->getActiveSheet()->setCellValue("D" . $row, (string) $permit->ytd_permits);
            $spreadsheet->getActiveSheet()->setCellValue("E" . $row, (string) $permit_ytd_value);
            $spreadsheet->getActiveSheet()->setCellValue("F" . $row, (string) $permit->previous_ytd_permits);
            $spreadsheet->getActiveSheet()->setCellValue("G" . $row, (string) $address);
            $spreadsheet->getActiveSheet()->setCellValue("H" . $row, (string) $permit->permit_phone_verbose);

            $row++;
        }

        $spreadsheet->setActiveSheetIndex(0);

        // Download the spreadsheet.

        header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        header("Content-disposition: attachment; filename=AllPermitsByBuilder.xlsx");
        header("Cache-Control: max-age=0");

        $excelWriter = new Xlsx($spreadsheet);

        @ob_clean();

        $excelWriter->save('php://output');

        exit();
    }

    // ===============
    // Debug an Object
    // ===============

    public function debug($object = "") {

        echo "<pre>";
        print_r($object);
        exit();
    }
}

?>
