<?php

class Consolidation extends Base {

    // Variables

    private $current_user_serial = 0;
    private $current_user_id     = "";
    private $current_user_name   = "";
    private $current_user_email  = "";

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

    public function __construct() {

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

        date_default_timezone_set(self::TIMEZONE);

        $this->current_user_serial = $_SESSION[self::USER_SERIAL] ?? 0;
        $this->current_user_id     = $_SESSION[self::USER_ID]     ?? "";
        $this->current_user_name   = $_SESSION[self::USER_NAME]   ?? "";
        $this->current_user_email  = $_SESSION[self::USER_EMAIL]  ?? "";
    }

    // ======================
    // Consolidation Sequence
    // ======================

    public function consolidationSequence() {

        set_time_limit(0);

        $step = filter_input(INPUT_POST, "step") ?: "prompt";

        switch ($step) {

            case "prompt":

                $content = $this->applyXSL("", $this->getXSL("consolidationAlert"));

                $this->displayContent($content);

                break;

            case "run":

                $XSLParms               = array();
                $permit_record_count    = 0;
                $processed_record_count = 0;
                $connection             = null;

                $sql_insert = "insert into rdiconsolidations
               
                        (  rdiconsolidation_permit_facts_id,
                           rdiconsolidation_entry_date_str,
                           rdiconsolidation_entry_date,
                           rdiconsolidation_permit_number,
                           rdiconsolidation_permit_date_str,
                           rdiconsolidation_permit_date,
                           rdiconsolidation_project_addr,
                           rdiconsolidation_sub_div_name,
                           rdiconsolidation_project_type,
                           rdiconsolidation_company,
                           rdiconsolidation_address,
                           rdiconsolidation_city,
                           rdiconsolidation_state,
                           rdiconsolidation_zip,
                           rdiconsolidation_phone,
                           rdiconsolidation_project_city,
                           rdiconsolidation_project_state,
                           rdiconsolidation_size,
                           rdiconsolidation_units,
                           rdiconsolidation_building_type,
                           rdiconsolidation_value,
                           rdiconsolidation_work_type,
                           rdiconsolidation_county,
                           rdiconsolidation_lot,
                           rdiconsolidation_dist_ll,
                           rdiconsolidation_ct1,
                           rdiconsolidation_owner_name,
                           rdiconsolidation_owner_address,
                           rdiconsolidation_owner_city,
                           rdiconsolidation_owner_state,
                           rdiconsolidation_owner_zip,
                           rdiconsolidation_owner_phone,
                           rdiconsolidation_ytd_permits,
                           rdiconsolidation_ytd_value,
                           rdiconsolidation_12mth_prev_permits,
                           rdiconsolidation_date )

                  select   coalesce(permit_permit_facts_id, 0),
                           coalesce(permit_entry_date_str, ''),
                           coalesce(permit_entry_date, '0000-00-00'),
                           coalesce(permit_permit_number, ''),
                           coalesce(permit_permit_date_str, ''),
                           coalesce(permit_permit_date, '0000-00-00'),
                           coalesce(permit_project_addr, ''),
                           coalesce(permit_sub_div_name, ''),
                           coalesce(permit_project_type, ''),
                           coalesce(permit_company, ''),
                           coalesce(permit_address, ''),
                           coalesce(permit_city, ''),
                           coalesce(permit_state, ''),
                           coalesce(permit_zip, ''),
                           coalesce(permit_phone, ''),
                           coalesce(permit_project_city, ''),
                           coalesce(permit_project_state, ''),
                           coalesce(permit_size, 0),
                           coalesce(permit_units, 0),
                           coalesce(permit_building_type, 0),
                           coalesce(permit_value, 0),
                           coalesce(permit_work_type, 0),
                           coalesce(permit_county, ''),
                           coalesce(permit_lot, ''),
                           coalesce(permit_dist_ll, ''),
                           coalesce(permit_ct1, 0),
                           coalesce(permit_owner_name, ''),
                           coalesce(permit_owner_address, ''),
                           coalesce(permit_owner_city, ''),
                           coalesce(permit_owner_state, ''),
                           coalesce(permit_owner_zip, ''),
                           coalesce(permit_owner_phone, ''),
                           coalesce(permit_ytd_permits, 0),
                           coalesce(permit_ytd_value, 0),
                           coalesce(permit_12mth_prev_permits, 0),
                           current_date()
                    from   permits";

                try {

                    $connection = $this->connect();

                    $connection->beginTransaction();

                    $permit_record_count     = (integer) $connection->query("select count(*) from permits")->fetchColumn();
                    $processed_record_count  = (integer) $connection->exec($sql_insert);

                    if ($processed_record_count !== $permit_record_count) {
                        throw new RuntimeException("Record counts did not match. Rolling back database. Contact Support.");
                    }

                    $connection->commit();
                    $connection->exec("truncate permits");
                } catch (Exception $e) {

                    if (($connection instanceof PDO) && $connection->inTransaction()) {
                        $connection->rollBack();
                    }

                    $XSLParms["CONSOLIDATION-ERROR"] = $e->getMessage();
                }

                $XSLParms["RDI-RECORD-COUNT"]       = $permit_record_count;
                $XSLParms["PROCESSED-RECORD-COUNT"] = $processed_record_count;
                $content                            = $this->applyXSL("", $this->getXSL("consolidationResults"), $XSLParms);

                $this->displayContent($content);

                break;

            default:

                $this->displayHome();
        }
    }

}

?>
