<?php

// ============================
// Export Permit Facts to Excel
// ============================

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

class Export_PermitFactsToExcel 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() {

        $subscription_serial = $_SESSION['subscription_serial'];
        $usersubscription_serial = $_SESSION['usersubscription_serial'];

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

        if ($subscription->count() === 0) {
            $this->logError("Invalid Subscription ({$subscription_serial}): " . __METHOD__);
        }

        $userssubscription = (new Users())->getUserSubscription($usersubscription_serial);

        $user_subscription_start_date = $userssubscription->record->usersubscription_start_date;
        $user_subscription_end_date = $userssubscription->record->usersubscription_end_date;

        $permitfacts = (new PermitFacts())->getPermitFactsPage($user_subscription_start_date, $user_subscription_end_date);

        // Create the Spreadsheet.

        $spreadsheet = new Spreadsheet();

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

        // 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()->getColumnDimension("I")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("J")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("K")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("L")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("M")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("N")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("O")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("P")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("Q")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("R")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("S")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("T")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("U")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("V")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("W")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("X")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("Y")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("Z")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("AA")->setAutoSize(true);

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

        $spreadsheet->setActiveSheetIndex(0)->setCellValue("A1", "permitfact_legacy_id")
                ->setCellValue("B1", "Entry Date")
                ->setCellValue("C1", "Permit Number")
                ->setCellValue("D1", "Permit Date")
                ->setCellValue("E1", "Project Address")
                ->setCellValue("F1", "Project Name")
                ->setCellValue("G1", "Project Type")
                ->setCellValue("H1", "Project City")
                ->setCellValue("I1", "Project State")
                ->setCellValue("J1", "Project Zip")
                ->setCellValue("K1", "Company")
                ->setCellValue("L1", "Address")
                ->setCellValue("M1", "City")
                ->setCellValue("N1", "State")
                ->setCellValue("O1", "Zip")
                ->setCellValue("P1", "Phone Number")
                ->setCellValue("Q1", "Size")
                ->setCellValue("R1", "Value")
                ->setCellValue("S1", "Work Type")
                ->setCellValue("T1", "Building Type")
                ->setCellValue("U1", "County")
                ->setCellValue("V1", "Owner Name")
                ->setCellValue("W1", "Owner Address")
                ->setCellValue("X1", "Owner City")
                ->setCellValue("Y1", "Owner State")
                ->setCellValue("Z1", "Owner Zip")
                ->setCellValue("AA1", "Owner 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()->getStyle("I:I")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("J:J")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("K:K")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("L:L")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("M:M")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("N:N")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("O:O")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("P:P")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("Q:Q")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("R:R")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("S:S")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("T:T")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("U:U")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("V:V")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("W:W")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("X:X")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("Y:Y")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("Z:Z")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("AA:AA")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);

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

        // Create the Detail Rows.

        $row = 2;

        foreach ($permitfacts as $permitfact) {

            $spreadsheet->getActiveSheet()->setCellValue("A" . $row, (string) $permitfact->permitfact_legacy_id);
            $spreadsheet->getActiveSheet()->setCellValue("B" . $row, (string) $permitfact->permitfact_entry_date_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("C" . $row, (string) $permitfact->permitfact_permit_number);
            $spreadsheet->getActiveSheet()->setCellValue("D" . $row, (string) $permitfact->permitfact_permit_date_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("E" . $row, (string) $permitfact->permitfact_project_address);
            $spreadsheet->getActiveSheet()->setCellValue("F" . $row, (string) $permitfact->permitfact_project_name);
            $spreadsheet->getActiveSheet()->setCellValue("G" . $row, (string) $permitfact->permitfact_project_type_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("H" . $row, (string) $permitfact->project_city_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("U" . $row, (string) $permitfact->permitfact_project_state);
            $spreadsheet->getActiveSheet()->setCellValue("J" . $row, (string) $permitfact->permitfact_project_zip);
            $spreadsheet->getActiveSheet()->setCellValue("K" . $row, (string) $permitfact->permitfact_company);
            $spreadsheet->getActiveSheet()->setCellValue("L" . $row, (string) $permitfact->permitfact_address);
            $spreadsheet->getActiveSheet()->setCellValue("M" . $row, (string) $permitfact->permitfact_city_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("N" . $row, (string) $permitfact->permitfact_state);
            $spreadsheet->getActiveSheet()->setCellValue("O" . $row, (string) $permitfact->permitfact_zip);
            $spreadsheet->getActiveSheet()->setCellValue("P" . $row, $this->formatPhoneNumber((string) $permitfact->permitfact_phone));
            $spreadsheet->getActiveSheet()->setCellValue("Q" . $row, (string) $permitfact->permitfact_size);
            $spreadsheet->getActiveSheet()->setCellValue("R" . $row, (string) $permitfact->permitfact_value);
            $spreadsheet->getActiveSheet()->setCellValue("S" . $row, (string) $permitfact->permitfact_work_type_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("T" . $row, (string) $permitfact->permitfact_building_type_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("U" . $row, (string) $permitfact->permitfact_county_name_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("V" . $row, (string) $permitfact->permitfact_owner_name);
            $spreadsheet->getActiveSheet()->setCellValue("W" . $row, (string) $permitfact->permitfact_owner_address);
            $spreadsheet->getActiveSheet()->setCellValue("X" . $row, (string) $permitfact->permitfact_owner_city);
            $spreadsheet->getActiveSheet()->setCellValue("Y" . $row, (string) $permitfact->permitfact_owner_state);
            $spreadsheet->getActiveSheet()->setCellValue("Z" . $row, (string) $permitfact->permitfact_owner_zip);
            $spreadsheet->getActiveSheet()->setCellValue("AA" . $row, $this->formatPhoneNumber((string) $permitfact->permitfact_owner_phone));

            $row++;
        }

        $spreadsheet->setActiveSheetIndex(0);

        $filename = str_replace(' ', '_', $subscription->record->subscription_full_title) . "_" . date("Y-m-d_h:i:s");

        // Download the spreadsheet

        header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        header("Content-disposition: attachment; filename={$filename}.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();
    }
}

?>
