<?php

require_once ("tcpdf/tcpdf.php");

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

class Print_All_PermitsByBuilder 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 AllPermitsByBuilder_Print();

        $report->subscription_serial = $_SESSION[self::REPORTS_POST]["subscription_serial"] ?? 0;
        $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 AllPermitsByBuilder_Print extends TCPDF {

    // Variables

    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);

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

        // Set report defaults

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

        $this->SetFont("Calibri");
        $this->SetMargins(10, 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 PermitsByBuilder())->getAllPermits($permit_area, $projecttype);

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

        $permit_ytd_value = 0.00;
        $permit_value = 0.00;

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

            $this->Ln(7);

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

            $this->Cell(52, 0, "", "", 0, "L", false, "", 3);
            $this->Cell(10, 0, "", "", 0, "L", false, "", 3);
            $this->Cell(20, 0, "", "", 0, "L", false, "", 3);
            $this->Cell(15, 0, "Current", "", 0, "L", false, "", 3);
            $this->Cell(20, 0, "Current Year", "", 0, "L", false, "", 3);
            $this->Cell(15, 0, "Last", "", 0, "L", false, "", 3);
            $this->Cell(85, 0, "", "", 0, "L", false, "", 3);
            $this->Cell(25, 0, "", "", 0, "L", false, "", 3);

            $this->Ln();

            $this->Cell(52, 0, "Company/Builder", "B", 0, "L", false, "", 3);
            $this->Cell(10, 0, "Permits", "B", 0, "L", false, "", 3);
            $this->Cell(20, 0, "Value", "B", 0, "L", false, "", 3);
            $this->Cell(15, 0, "Year YTD", "B", 0, "L", false, "", 3);
            $this->Cell(20, 0, "YTD Value", "B", 0, "L", false, "", 3);
            $this->Cell(15, 0, "12 Months", "B", 0, "L", false, "", 3);
            $this->Cell(85, 0, "Address", "B", 0, "L", false, "", 3);
            $this->Cell(25, 0, "Phone", "B", 0, "L", false, "", 3);

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

            $this->Ln(6);

            foreach ($permits as $permit) {

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

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

                $this->Cell(52, 0, $permit->permit_company, 0, 0, "L", false, "", 3);
                $this->Cell(10, 0, $permit->permit_permit_count, 0, 0, "C", false, "", 3);
                $this->Cell(20, 0, $permit_value, 0, 0, "L", false, "", 3);
                $this->Cell(15, 0, $permit->ytd_permits, 0, 0, "C", false, "", 3);
                $this->Cell(20, 0, $permit_ytd_value, 0, 0, "L", false, "", 3);
                $this->Cell(15, 0, $permit->previous_ytd_permits, 0, 0, "C", false, "", 3);
                $this->Cell(85, 0, $address, 0, 0, "L", false, "", 3);
                $this->Cell(25, 0, $permit->permit_phone_verbose, 0, 0, "L", false, "", 3);

                $this->Ln(4);

                if ($rowCount == 38) {

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

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

                    $this->Cell(52, 0, "", "", 0, "L", false, "", 3);
                    $this->Cell(10, 0, "", "", 0, "L", false, "", 3);
                    $this->Cell(20, 0, "", "", 0, "L", false, "", 3);
                    $this->Cell(15, 0, "Current", "", 0, "L", false, "", 3);
                    $this->Cell(20, 0, "Current Year", "", 0, "L", false, "", 3);
                    $this->Cell(15, 0, "Last", "", 0, "L", false, "", 3);
                    $this->Cell(85, 0, "", "", 0, "L", false, "", 3);
                    $this->Cell(25, 0, "", "", 0, "L", false, "", 3);

                    $this->Ln();

                    $this->Cell(52, 0, "Company/Builder", "B", 0, "L", false, "", 3);
                    $this->Cell(10, 0, "Permits", "B", 0, "L", false, "", 3);
                    $this->Cell(20, 0, "Value", "B", 0, "L", false, "", 3);
                    $this->Cell(15, 0, "Year YTD", "B", 0, "L", false, "", 3);
                    $this->Cell(20, 0, "YTD Value", "B", 0, "L", false, "", 3);
                    $this->Cell(15, 0, "12 Months", "B", 0, "L", false, "", 3);
                    $this->Cell(85, 0, "Address", "B", 0, "L", false, "", 3);
                    $this->Cell(25, 0, "Phone", "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("AllPermitByBuilderRecords.pdf", "S");
        } else {
            $this->Output("AllPermitByBuilderRecords.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 Builder 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);
    }
}

?>
