<?php

require_once ("tcpdf/tcpdf.php");

// ==========================
// Print Selected Work Orders
// ==========================

class Print_Selected_PermitsBySubdivision extends Base {

    // Variables.

    private $output_type = "normal";

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

    public function __construct($output_type = "normal") {

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

        date_default_timezone_set(self::TIMEZONE);

        $this->output_type = $output_type;
    }

    // ================
    // Print the Report
    // ================

    public function print() {

        $report = new SelectedPermitsBySubdivision_Print();

        $report->selected_permits = $_SESSION[self::REPORTS_POST]["selected_permits"];
        $report->subscription_serial = $_SESSION[self::REPORTS_POST]["subscription_serial"];
        $report->output_type = $this->output_type;

        ob_clean();

        if ($this->output_type == "string") {
            return $report->render();
        } else {
            $report->render();
        }
    }
}

// ==========================
// Class to Generate the .pdf
// ==========================

class SelectedPermitsBySubdivision_Print extends TCPDF {

    // Variables

    public $selected_permits = array();
    public $permit_serial = 0;
    public $subscription_serial = 0;
    public $first_page = true;
    public $output_type = "normal";

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

    public function debug($object = "") {

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

    // =================
    // Render the Report
    // =================

    public function render() {

        set_time_limit(0);

//        $subscription = (new Subscriptions())->getSubscription($this->subscription_serial);
        // Set report defaults

        $this->title = "All Permit By Subdivision Records";

        $this->SetFont("Calibri");
        $this->SetMargins(5, 26, 10);
        $this->SetHeaderMargin(10);
        $this->SetFooterMargin(10);
        $this->setCellPaddings(0, 0, 0, 0);
        $this->setCellMargins(2, 0, 0, 0);

        $this->addPage("L", "LETTER");

        $rowCount = 0;

        // Get the data for the report

        $permits = (new PermitsBySubdivision())->getSelectedPermits($this->selected_permits);

        foreach ($permits as $permit) {

            $county_serial = $permit->rdi_county;

            $county_name = (new Counties())->getCounty($county_serial);
            $permit->county_name_verbose = $county_name->record->county_name;
        }

        // -------
        // Permits
        // -------

        $permit_ytd_value = 0.00;
        $permit_value = 0.00;

        if ($permits->count() > 0) {

            $this->Ln(7);

            $this->SetFont("Calibri", "B", 10);

            $this->Cell(15, 0, "", "", 0, "L", false, "", 3);
            $this->Cell(40, 0, "", "", 0, "L", false, "", 3);
            $this->Cell(85, 0, "", "", 0, "L", false, "", 3);
            $this->Cell(60, 0, "", "", 0, "L", false, "", 3);
            $this->Cell(20, 0, "", "", 0, "L", false, "", 3);
            $this->Cell(15, 0, "Square ", "", 0, "L", false, "", 3);
            $this->Cell(20, 0, "", "", 0, "L", false, "", 3);

            $this->Ln();

            $this->Cell(15, 0, "County", "B", 0, "L", false, "", 3);
            $this->Cell(40, 0, "Subdivision", "B", 0, "L", false, "", 3);
            $this->Cell(85, 0, "Address", "B", 0, "L", false, "", 3);
            $this->Cell(60, 0, "Builder/Company", "B", 0, "L", false, "", 3);
            $this->Cell(20, 0, "Permit Date", "B", 0, "L", false, "", 3);
            $this->Cell(15, 0, "Footage", "B", 0, "L", false, "", 3);
            $this->Cell(20, 0, "Value", "B", 0, "L", false, "", 3);

            $this->SetFont("Calibri", "", 10);

            $this->Ln(6);

            foreach ($permits as $permit) {

                $this->stripeLine($rowCount, 268);
                $rowCount++;

                $address = "{$permit->rdi_address} {$permit->rdi_city}, {$permit->rdi_state} {$permit->rdi_zip}";

                $permit_value = "$" . number_format((int) $permit->rdi_value, 2);

                $this->Cell(15, 0, $permit->county_name_verbose, 0, 0, "L", false, "", 3);
                $this->Cell(40, 0, $permit->rdi_sub_div_name, 0, 0, "L", false, "", 3);
                $this->Cell(85, 0, $address, 0, 0, "L", false, "", 3);
                $this->Cell(60, 0, $permit->rdi_company, 0, 0, "L", false, "", 3);
                $this->Cell(20, 0, $permit->rdi_permit_date_verbose, 0, 0, "L", false, "", 3);
                $this->Cell(15, 0, $permit->rdi_size, 0, 0, "L", false, "", 3);
                $this->Cell(20, 0, $permit_value, 0, 0, "L", false, "", 3);

                $this->Ln(4);

                if ($rowCount == 38) {

                    $this->AddPage("L", "LETTER");

                    $this->SetFont("Calibri", "B", 10);

                    $this->Cell(15, 0, "", "", 0, "L", false, "", 3);
                    $this->Cell(40, 0, "", "", 0, "L", false, "", 3);
                    $this->Cell(75, 0, "", "", 0, "L", false, "", 3);
                    $this->Cell(60, 0, "", "", 0, "L", false, "", 3);
                    $this->Cell(20, 0, "", "", 0, "L", false, "", 3);
                    $this->Cell(15, 0, "Square ", "", 0, "L", false, "", 3);
                    $this->Cell(20, 0, "", "", 0, "L", false, "", 3);

                    $this->Ln();

                    $this->Cell(15, 0, "County", "B", 0, "L", false, "", 3);
                    $this->Cell(40, 0, "Subdivision", "B", 0, "L", false, "", 3);
                    $this->Cell(75, 0, "Address", "B", 0, "L", false, "", 3);
                    $this->Cell(60, 0, "Builder/Company", "B", 0, "L", false, "", 3);
                    $this->Cell(20, 0, "Permit Date", "B", 0, "L", false, "", 3);
                    $this->Cell(15, 0, "Footage", "B", 0, "L", false, "", 3);
                    $this->Cell(20, 0, "Value", "B", 0, "L", false, "", 3);

                    $this->SetFont("Calibri", "", 10);

                    $this->Ln(6);
                    $rowCount = 0;
                }
            }
        }

        // --------------
        // Return the pdf
        // --------------

        if ($this->output_type == "string") {
            return $this->Output("Selected_Permits_By_Subdivision_Records.pdf", "S");
        } else {
            $this->Output("Selected_Permits_By_Subdivision_Records.pdf");
        }
    }

    // ======
    // Header
    // ======

    public function Header() {

        $this->setCellPaddings(0, 0, 0, 0);
        $this->setCellMargins(2, 0, 0, 0);

        $this->Image("images/dec-international-logo.png", 10, 10, 25, 12);

        $this->SetFont("Calibri", "B", 16);

        $this->Cell(0, 6, "DEC International", 0, 0, "C");
        $this->Ln(6);
        $this->Cell(0, 6, "All Permit By Subdivision Records", 0, 1, "C");
    }

    // ======
    // Footer
    // ======

    public function Footer() {

        $this->SetFont("Calibri", "I", 8);

        $X = $this->GetX();

        $this->Cell(0, 10, "DEC International", 0, false, "C");

        $this->SetX($X);

        $this->Cell(40, 10, "Page " . $this->getAliasNumPage() . "/" . $this->getAliasNbPages(), 0, false, "L");
        $this->Cell(0, 10, date("n/j/Y g:i A"), 0, false, "R");
    }

    // =======================
    // Stripe the printed line
    // =======================

    private function stripeLine($pRowCount = 0, $pRowWidth = 0) {

        $this->SetFillColor(($pRowCount % 2) ? 220 : 255);
        $X = $this->GetX();
        $this->Cell($pRowWidth, 0, "", 0, 0, "L", true);
        $this->SetX($X);
    }
}

?>
