<?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":

                $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 )
                           
                  values ( :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 ) ";

                $connection = $this->connect();

                $connection->beginTransaction();
                $statement = $connection->prepare($sql_insert);

                $rdi_records = $this->getRdiTable();
                $rdi_record_count = $rdi_records->count();

                $processed_record_count = 0;

                foreach ($rdi_records as $record) {

                    $rdiconsolidation_permit_facts_id = (integer) $record->permit_facts_id ?: 0;
                    $rdiconsolidation_entry_date_str = (integer) $record->entry_date_str ?: "";
                    $rdiconsolidation_entry_date = (string) $record->entry_date ?: "0000-00-00";
                    $rdiconsolidation_permit_number = (string) $record->permit_number ?: "";
                    $rdiconsolidation_permit_date_str = (string) $record->permit_date_str ?: "";
                    $rdiconsolidation_permit_date = (string) $record->permit_date ?: "0000-00-00";
                    $rdiconsolidation_project_addr = (string) $record->project_addr ?: "";
                    $rdiconsolidation_sub_div_name = (string) $record->sub_div_name ?: "";
                    $rdiconsolidation_project_type = (string) $record->project_type ?: "";
                    $rdiconsolidation_company = (string) $record->company ?: "";
                    $rdiconsolidation_address = (string) $record->address ?: "";
                    $rdiconsolidation_city = (string) $record->city ?: "";
                    $rdiconsolidation_state = (string) $record->state ?: "";
                    $rdiconsolidation_zip = (string) $record->zip ?: "";
                    $rdiconsolidation_phone = (string) $record->phone ?: "";
                    $rdiconsolidation_project_city = (string) $record->project_city ?: "";
                    $rdiconsolidation_project_state = (string) $record->project_state ?: "";
                    $rdiconsolidation_size = (integer) $record->size ?: 0;
                    $rdiconsolidation_units = (integer) $record->units ?: 0;
                    $rdiconsolidation_building_type = (integer) $record->building_type ?: 0;
                    $rdiconsolidation_value = (integer) $record->value ?: 0;
                    $rdiconsolidation_work_type = (integer) $record->work_type ?: 0;
                    $rdiconsolidation_county = (string) $record->county ?: "";
                    $rdiconsolidation_lot = (string) $record->lot ?: "";
                    $rdiconsolidation_dist_ll = (string) $record->dist_ll ?: "";
                    $rdiconsolidation_ct1 = (integer) $record->ct1 ?: 0;
                    $rdiconsolidation_owner_name = (string) $record->owner_name ?: "";
                    $rdiconsolidation_owner_address = (string) $record->owner_address ?: "";
                    $rdiconsolidation_owner_city = (string) $record->owner_city ?: "";
                    $rdiconsolidation_owner_state = (string) $record->owner_state ?: "";
                    $rdiconsolidation_owner_zip = (string) $record->owner_zip ?: "";
                    $rdiconsolidation_owner_phone = (string) $record->owner_phone ?: "";
                    $rdiconsolidation_ytd_permits = (integer) $record->ytd_permits ?: 0;
                    $rdiconsolidation_ytd_value = (integer) $record->ytd_value ?: 0;
                    $rdiconsolidation_12mth_prev_permits = (integer) $record->{"12mth_prev_permits"} ?: 0;
                    $rdiconsolidation_date = date("Y-m-d");

                    $statement->bindValue(":rdiconsolidation_permit_facts_id", $rdiconsolidation_permit_facts_id);
                    $statement->bindValue(":rdiconsolidation_entry_date_str", $rdiconsolidation_entry_date_str);
                    $statement->bindValue(":rdiconsolidation_entry_date", $rdiconsolidation_entry_date);
                    $statement->bindValue(":rdiconsolidation_permit_number", $rdiconsolidation_permit_number);
                    $statement->bindValue(":rdiconsolidation_permit_date_str", $rdiconsolidation_permit_date_str);
                    $statement->bindValue(":rdiconsolidation_permit_date", $rdiconsolidation_permit_date);
                    $statement->bindValue(":rdiconsolidation_project_addr", $rdiconsolidation_project_addr);
                    $statement->bindValue(":rdiconsolidation_sub_div_name", $rdiconsolidation_sub_div_name);
                    $statement->bindValue(":rdiconsolidation_project_type", $rdiconsolidation_project_type);
                    $statement->bindValue(":rdiconsolidation_company", $rdiconsolidation_company);
                    $statement->bindValue(":rdiconsolidation_address", $rdiconsolidation_address);
                    $statement->bindValue(":rdiconsolidation_city", $rdiconsolidation_city);
                    $statement->bindValue(":rdiconsolidation_state", $rdiconsolidation_state);
                    $statement->bindValue(":rdiconsolidation_zip", $rdiconsolidation_zip);
                    $statement->bindValue(":rdiconsolidation_phone", $rdiconsolidation_phone);
                    $statement->bindValue(":rdiconsolidation_project_city", $rdiconsolidation_project_city);
                    $statement->bindValue(":rdiconsolidation_project_state", $rdiconsolidation_project_state);
                    $statement->bindValue(":rdiconsolidation_size", $rdiconsolidation_size);
                    $statement->bindValue(":rdiconsolidation_units", $rdiconsolidation_units);
                    $statement->bindValue(":rdiconsolidation_building_type", $rdiconsolidation_building_type);
                    $statement->bindValue(":rdiconsolidation_value", $rdiconsolidation_value);
                    $statement->bindValue(":rdiconsolidation_work_type", $rdiconsolidation_work_type);
                    $statement->bindValue(":rdiconsolidation_county", $rdiconsolidation_county);
                    $statement->bindValue(":rdiconsolidation_lot", $rdiconsolidation_lot);
                    $statement->bindValue(":rdiconsolidation_dist_ll", $rdiconsolidation_dist_ll);
                    $statement->bindValue(":rdiconsolidation_ct1", $rdiconsolidation_ct1);
                    $statement->bindValue(":rdiconsolidation_owner_name", $rdiconsolidation_owner_name);
                    $statement->bindValue(":rdiconsolidation_owner_address", $rdiconsolidation_owner_address);
                    $statement->bindValue(":rdiconsolidation_owner_city", $rdiconsolidation_owner_city);
                    $statement->bindValue(":rdiconsolidation_owner_state", $rdiconsolidation_owner_state);
                    $statement->bindValue(":rdiconsolidation_owner_zip", $rdiconsolidation_owner_zip);
                    $statement->bindValue(":rdiconsolidation_owner_phone", $rdiconsolidation_owner_phone);
                    $statement->bindValue(":rdiconsolidation_ytd_permits", $rdiconsolidation_ytd_permits);
                    $statement->bindValue(":rdiconsolidation_ytd_value", $rdiconsolidation_ytd_value);
                    $statement->bindValue(":rdiconsolidation_12mth_prev_permits", $rdiconsolidation_12mth_prev_permits);
                    $statement->bindValue(":rdiconsolidation_date", $rdiconsolidation_date);

                    if (!$statement->execute()) {

                        $connection->rollBack();
                        $this->debug($connection->errorInfo()[2]);
                    }

                    $processed_record_count++;
                }

                if ($processed_record_count !== $rdi_record_count) {
                    $connection->rollBack();
                    $XSLParms["CONSOLIDATION_ERROR"] = "Record Counts Did not match. Rolling Back Database. Contact Support.";
                } else {
                    $connection->commit();
                    $this->executeSQL("truncate rdis");
                }

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

                break;

            default:

                $this->displayHome();
        }
    }

    // ====================
    // Return an RDI Object
    // ====================

    public function getRdiTable() {

        $SQL = "SELECT * FROM permits";

        return $this->getTable("rdis", $SQL);
    }
}

?>
