<?php

// ============================
// Export Business Facts to Excel
// ============================

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

class Export_BusinessFactsToExcel 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;

        $businessfacts = (new BusinessFacts())->getBusinessFactsPage($user_subscription_start_date, $user_subscription_end_date);

        // Create the Spreadsheet.

        $spreadsheet = new Spreadsheet();

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

        // Create the Base Sheet.

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

        // 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()->getStyle("A1:W1")->getFont()->setBold(true);
        $spreadsheet->getActiveSheet()->getStyle("A1:W1")->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB("D3D3D3");

        $spreadsheet->setActiveSheetIndex(0)->setCellValue("A1", "BusinessFactID")
                ->setCellValue("B1", "Company")
                ->setCellValue("C1", "Address1")
                ->setCellValue("D1", "Address2")
                ->setCellValue("E1", "City")
                ->setCellValue("F1", "State")
                ->setCellValue("G1", "Zip")
                ->setCellValue("H1", "County")
                ->setCellValue("I1", "Phone")
                ->setCellValue("J1", "Fax")
                ->setCellValue("K1", "EmailAddress")
                ->setCellValue("L1", "BC")
                ->setCellValue("M1", "Description")
                ->setCellValue("N1", "ES")
                ->setCellValue("O1", "BusinessInception")
                ->setCellValue("P1", "SIC")
                ->setCellValue("Q1", "SIC_Description")
                ->setCellValue("R1", "ContactName")
                ->setCellValue("S1", "ContactTitle")
                ->setCellValue("T1", "LicenseDate")
                ->setCellValue("U1", "HomeBasedBusiness")
                ->setCellValue("V1", "YearEstablished")
                ->setCellValue("W1", "EntryDate");

        $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()->freezePane("A2");

        // Create the Detail Rows.

        $row = 2;

        foreach ($businessfacts as $businessfact) {

            $spreadsheet->getActiveSheet()->setCellValue("A" . $row, (string) $businessfact->businessfact_legacy_id);
            $spreadsheet->getActiveSheet()->setCellValue("B" . $row, (string) $businessfact->businessfact_company);
            $spreadsheet->getActiveSheet()->setCellValue("C" . $row, (string) $businessfact->businessfact_address_one);
            $spreadsheet->getActiveSheet()->setCellValue("D" . $row, (string) $businessfact->businessfact_address_two);
            $spreadsheet->getActiveSheet()->setCellValue("E" . $row, (string) $businessfact->businessfact_city_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("F" . $row, (string) $businessfact->businessfact_state);
            $spreadsheet->getActiveSheet()->setCellValue("G" . $row, (string) $businessfact->businessfact_zip);
            $spreadsheet->getActiveSheet()->setCellValue("H" . $row, (string) $businessfact->businessfact_county_name_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("I" . $row, (string) $this->formatPhoneNumber($businessfact->businessfact_phone));
            $spreadsheet->getActiveSheet()->setCellValue("J" . $row, (string) $this->formatPhoneNumber($businessfact->businessfact_fax));
            $spreadsheet->getActiveSheet()->setCellValue("K" . $row, (string) $businessfact->businessfact_email_address);
            $spreadsheet->getActiveSheet()->setCellValue("L" . $row, (string) $businessfact->businessfact_business_class_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("M" . $row, (string) $businessfact->businessfact_action_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("N" . $row, (string) $businessfact->businessfact_employee_count);
            $spreadsheet->getActiveSheet()->setCellValue("O" . $row, (string) $businessfact->businessfact_inception);
            $spreadsheet->getActiveSheet()->setCellValue("P" . $row, (string) $businessfact->businessfact_sic_code);
            $spreadsheet->getActiveSheet()->setCellValue("Q" . $row, (string) $businessfact->businessfact_siccode_description_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("R" . $row, (string) $businessfact->businessfact_contact_name);
            $spreadsheet->getActiveSheet()->setCellValue("S" . $row, (string) $businessfact->businessfact_contact_title);
            $spreadsheet->getActiveSheet()->setCellValue("T" . $row, (string) $businessfact->businessfact_license_date_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("U" . $row, (string) $businessfact->businessfact_home_based_business);
            $spreadsheet->getActiveSheet()->setCellValue("V" . $row, (string) $businessfact->businessfact_year_established);
            $spreadsheet->getActiveSheet()->setCellValue("W" . $row, (string) $businessfact->businessfact_entry_date_verbose);

            $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();
    }
}

?>
