diff --git a/classes/Base.php b/classes/Base.php index c3d6c96..5145a87 100644 --- a/classes/Base.php +++ b/classes/Base.php @@ -128,7 +128,7 @@ class Base { $host = 'localhost'; } else { $host = '192.168.1.190'; - $host = '100.67.215.67'; +// $host = '100.67.215.67'; } try { diff --git a/classes/PermitsBySubdivision.php b/classes/PermitsBySubdivision.php index 740c0d0..b43b056 100644 --- a/classes/PermitsBySubdivision.php +++ b/classes/PermitsBySubdivision.php @@ -340,9 +340,6 @@ class PermitsBySubdivision extends Base { $statement->bindValue(":limit", $this->pagination_size, PDO::PARAM_INT); $statement->bindValue(":offset", $offset, PDO::PARAM_INT); -// echo $projecttype; -// die(); - $statement->execute(); $permits = $this->getTableXML("permits", $statement); @@ -368,44 +365,118 @@ class PermitsBySubdivision extends Base { // Fetch All Permits for Exports // ============================= - public function getAllPermitsBySubDivision($permit_area, $projecttype) { + public function getAllPermitsBySubdivision($permit_area, $projecttype, $searchcriteria = array()) { $connection = $this->connect(); + $searchcriteria = is_array($searchcriteria) ? $searchcriteria : []; + + $conditions = ["r.permit_project_type = :permit_project_type"]; + $parameters = [":permit_project_type" => [$projecttype, PDO::PARAM_STR]]; + + // County can be posted as [6, 7] or as a comma-separated string + $countyValues = $searchcriteria["permit_county"] ?? []; + if (!is_array($countyValues)) { + $countyValues = explode(",", (string) $countyValues); + } + + $countyIds = []; + foreach ($countyValues as $countyValue) { + $countyValue = trim((string) $countyValue); + if ($countyValue !== "" && ctype_digit($countyValue)) { + $countyIds[] = (int) $countyValue; + } + } + $countyIds = array_values(array_unique($countyIds)); + + if ($permit_area != "All") { + $conditions[] = "r.permit_county in ( + select county_serial + from view_counties + where county_area = :permit_area )"; + $parameters[":permit_area"] = [$permit_area, PDO::PARAM_STR]; + } + + if ($countyIds) { + $countyPlaceholders = []; + foreach ($countyIds as $index => $countyId) { + $placeholder = ":permit_county_{$index}"; + $countyPlaceholders[] = $placeholder; + $parameters[$placeholder] = [$countyId, PDO::PARAM_INT]; + } + $conditions[] = "r.permit_county in (" . implode(", ", $countyPlaceholders) . ")"; + } + + $textFilters = [ + "permit_company" => "permit_company", + "permit_sub_div_name" => "permit_sub_div_name", + "permit_project_city" => "permit_project_city", + "permit_project_addr" => "permit_project_addr" + ]; + + foreach ($textFilters as $criteriaKey => $column) { + $value = trim((string) ($searchcriteria[$criteriaKey] ?? "")); + if ($value !== "") { + $placeholder = ":{$criteriaKey}"; + $conditions[] = "r.{$column} like {$placeholder}"; + $parameters[$placeholder] = ["%{$value}%", PDO::PARAM_STR]; + } + } + + $normalizeDate = static function ($value) { + $value = trim((string) $value); + if ($value === "") { + return null; + } + $timestamp = strtotime($value); + return $timestamp === false ? null : date("Y-m-d", $timestamp); + }; + + $dateRanges = [ + ["permit_entry_date", "permit_entry_start_date", "permit_entry_end_date"], + ["permit_permit_date", "permit_permit_start_date", "permit_permit_end_date"] + ]; + + foreach ($dateRanges as [$column, $startKey, $endKey]) { + $startDate = $normalizeDate($searchcriteria[$startKey] ?? ""); + $endDate = $normalizeDate($searchcriteria[$endKey] ?? ""); + + if ($startDate !== null) { + $conditions[] = "r.{$column} >= :{$startKey}"; + $parameters[":{$startKey}"] = [$startDate, PDO::PARAM_STR]; + } + if ($endDate !== null) { + $conditions[] = "r.{$column} <= :{$endKey}"; + $parameters[":{$endKey}"] = [$endDate, PDO::PARAM_STR]; + } + } + + $where = implode(" and ", $conditions); + + $bindParameters = static function ($statement) use ($parameters) { + foreach ($parameters as $placeholder => [$value, $type]) { + $statement->bindValue($placeholder, $value, $type); + } + }; - // Count Query $SQL = "select count(*) as count - from view_permits r - join counties c on r.permit_county = c.county_serial - where r.permit_project_type = :permit_project_type - and r.permit_county in - (select county_serial - from view_counties - where county_area = :permit_area)"; + from view_permits r + where {$where}"; $statement = $connection->prepare($SQL); - - $statement->bindValue(":permit_project_type", $projecttype, PDO::PARAM_STR); - $statement->bindValue(":permit_area", $permit_area, PDO::PARAM_STR); - + $bindParameters($statement); $statement->execute(); + $recordset = $statement->fetch(PDO::FETCH_ASSOC); $count = $recordset ? (int) $recordset["count"] : 0; - // === DATA QUERY === - $SQL = "select r.*, c.county_name as permit_county_name_verbose - from view_permits r - join counties c on r.permit_county = c.county_serial - where r.permit_project_type = :permit_project_type - and r.permit_county in - (select county_serial - from view_counties - where county_area = :permit_area)"; + $SQL = "select r.*, c.county_name as county_name_verbose + from view_permits r + join counties c on r.permit_county = c.county_serial + where {$where} + order by r.permit_sub_div_name, r.permit_project_addr"; $statement = $connection->prepare($SQL); - - $statement->bindValue(":permit_project_type", $projecttype, PDO::PARAM_STR); - $statement->bindValue(":permit_area", $permit_area, PDO::PARAM_STR); - + $bindParameters($statement); $statement->execute(); $permits = $this->getTableXML("permits", $statement); diff --git a/js/searchPermitsBySubdivision.js b/js/searchPermitsBySubdivision.js index adaa5ed..882ac8c 100644 --- a/js/searchPermitsBySubdivision.js +++ b/js/searchPermitsBySubdivision.js @@ -204,8 +204,6 @@ $(document).ready(function () { }); - - // ==================== // Autocomplete Searches // ==================== @@ -529,6 +527,23 @@ $(document).ready(function () { }); +// // ------------------------ +// // Export All Permits - PDF +// // ------------------------ +// +// $(".btnExportAllPDF").on("click", function (e) { +// +// e.preventDefault(); +// +// window.open("", "PrintTab"); +// +// $("input[name = 'action']", "#PrintAllPermitsBySubdivisionPDFForm").val("print"); +// $("input[name = 'subscription_serial']", "#PrintAllPermitsBySubdivisionPDFForm").val($sessionStorage.subscription_serial); +// $("input[name = 'subscription_serial']", "#PrintAllPermitsBySubdivisionPDFForm").val($sessionStorage.subscription_serial); +// $("#PrintAllPermitsBySubdivisionPDFForm").submit(); +// +// }); + // ------------------------ // Export All Permits - PDF // ------------------------ @@ -537,14 +552,55 @@ $(document).ready(function () { e.preventDefault(); + const $printForm = $("#PrintAllPermitsBySubdivisionPDFForm"); + const $searchForm = $("#searchPermitsBySubdivision"); + + // Remove fields added by a previous PDF export. + $printForm.find(".print-search-field").remove(); + + // Copy populated search criteria into the PDF form. + $searchForm.find(":input[name]").not(":button, :submit, :reset").not("[name='action']").not("[name='subscription_serial']").each(function () { + + const $input = $(this); + const fieldName = $input.attr("name"); + + // Ignore unchecked checkboxes and radio buttons. + if (($input.is(":checkbox") || $input.is(":radio")) && !$input.is(":checked")) { + return; + } + + let values = $input.val(); + + if (values === null || values === undefined) { + return; + } + + // Supports multiple-select inputs. + if (!Array.isArray(values)) { + values = [values]; + } + + $.each(values, function (_, value) { + + value = String(value).trim(); + + if (value === "") { + return; + } + + $("", {type: "hidden", name: fieldName, value: value, class: "print-search-field"}).appendTo($printForm); + }); + }); + + $printForm.find("[name='action']").val("print"); + $printForm.find("[name='subscription_serial']").val($sessionStorage.subscription_serial); + window.open("", "PrintTab"); - - $("input[name = 'action']", "#PrintAllPermitsBySubdivisionPDFForm").val("print"); - $("input[name = 'subscription_serial']", "#PrintAllPermitsBySubdivisionPDFForm").val($sessionStorage.subscription_serial); - $("#PrintAllPermitsBySubdivisionPDFForm").submit(); - + $printForm.trigger("submit"); }); + + // -------------------------- // Export All Permits - Excel // -------------------------- diff --git a/reports/Export_AllPermitsBySubdivision_CSV.php b/reports/Export_AllPermitsBySubdivision_CSV.php index 6ef5a30..9e873d5 100644 --- a/reports/Export_AllPermitsBySubdivision_CSV.php +++ b/reports/Export_AllPermitsBySubdivision_CSV.php @@ -46,20 +46,20 @@ class Export_AllPermitsBySubdivision_CSV extends Base { $permit_value = 0.00; foreach ($permits as $permit) { + + $county_name = $permit->county_name_verbose; + $subdivision = $permit->permit_sub_div_name; + $address = "{$permit->permit_address} {$permit->permit_city} {$permit->permit_state} {$permit->permit_zip}"; + $city = $permit->permit_city; + $builder = $permit->permit_company; + $permit_date = $permit->permit_permit_date_verbose; + $square_footage = $permit->permit_size_verbose; + $value = $permit->permit_value_verbose; - $county_name = $permit->rdi_county_name_verbose; - $subdivision = $permit->rdi_sub_div_name; - $address = "{$permit->rdi_address} {$permit->rdi_city} {$permit->rdi_state} {$permit->rdi_zip}"; - $city = $permit->rdi_city; - $builder = $permit->rdi_company; - $permit_date = $permit->rdi_permit_date_verbose; - $square_footage = $permit->rdi_size_verbose; - $value = $permit->rdi_value_verbose; + $permit_ytd_value = "$" . number_format((int) $permit->permit_ytd_value, 2); + $permit_value = "$" . number_format((int) $permit->permit_value, 2); - $permit_ytd_value = "$" . number_format((int) $permit->rdi_ytd_value, 2); - $permit_value = "$" . number_format((int) $permit->rdi_value, 2); - - $permit_data = array($county_name, $subdivision, $address, $city, $builder, $permit_date, $square_footage, $value); + $permit_data = array($county_name, $subdivision, $address, $city, $builder, $permit_date, $square_footage, $value); fputcsv($output, $permit_data, ',', '"', '\\'); } diff --git a/reports/Export_AllPermitsBySubdivision_Excel.php b/reports/Export_AllPermitsBySubdivision_Excel.php index bf57d9b..2b62004 100644 --- a/reports/Export_AllPermitsBySubdivision_Excel.php +++ b/reports/Export_AllPermitsBySubdivision_Excel.php @@ -35,7 +35,7 @@ class Export_AllPermitsBySubdivision_Excel extends Base { $permit_area = $subscription->record->subscription_area; $projecttype = $subscription->record->subscription_projecttype; - $permits = (new PermitsBySubdivision())->getAllPermitsBySubdivision($permit_area, $projecttype); + $permits = (new PermitsBySubdivision())->getAllPermitsBySubdivision($permit_area, $projecttype, $_POST); // Create the Spreadsheet. @@ -89,16 +89,16 @@ class Export_AllPermitsBySubdivision_Excel extends Base { foreach ($permits as $permit) { - $address = "{$permit->rdi_address} {$permit->rdi_city}, {$permit->rdi_state} {$permit->rdi_zip}"; + $address = "{$permit->permit_address} {$permit->permit_city}, {$permit->permit_state} {$permit->permit_zip}"; - $spreadsheet->getActiveSheet()->setCellValue("A" . $row, (string) $permit->rdi_county_name_verbose); - $spreadsheet->getActiveSheet()->setCellValue("B" . $row, (string) $permit->rdi_sub_div_name); + $spreadsheet->getActiveSheet()->setCellValue("A" . $row, (string) $permit->county_name_verbose); + $spreadsheet->getActiveSheet()->setCellValue("B" . $row, (string) $permit->permit_sub_div_name); $spreadsheet->getActiveSheet()->setCellValue("C" . $row, (string) $address); - $spreadsheet->getActiveSheet()->setCellValue("D" . $row, (string) $permit->rdi_city); - $spreadsheet->getActiveSheet()->setCellValue("E" . $row, (string) $permit->rdi_company); - $spreadsheet->getActiveSheet()->setCellValue("F" . $row, (string) $permit->rdi_permit_date_verbose); - $spreadsheet->getActiveSheet()->setCellValue("G" . $row, (string) $permit->rdi_size_verbose); - $spreadsheet->getActiveSheet()->setCellValue("H" . $row, (string) $permit->rdi_value_verbose); + $spreadsheet->getActiveSheet()->setCellValue("D" . $row, (string) $permit->permit_city); + $spreadsheet->getActiveSheet()->setCellValue("E" . $row, (string) $permit->permit_company); + $spreadsheet->getActiveSheet()->setCellValue("F" . $row, (string) $permit->permit_permit_date_verbose); + $spreadsheet->getActiveSheet()->setCellValue("G" . $row, (string) $permit->permit_size_verbose); + $spreadsheet->getActiveSheet()->setCellValue("H" . $row, (string) $permit->permit_value_verbose); $row++; } diff --git a/reports/Print_All_PermitsBySubdivision.php b/reports/Print_All_PermitsBySubdivision.php index 0b0934d..91b822a 100644 --- a/reports/Print_All_PermitsBySubdivision.php +++ b/reports/Print_All_PermitsBySubdivision.php @@ -99,7 +99,7 @@ class AllPermitsBySubdivision_Print extends TCPDF { // Get the data for the report - $permits = (new PermitsBySubdivision())->getAllPermitsBySubdivision($permit_area, $projecttype); + $permits = (new PermitsBySubdivision())->getAllPermitsBySubdivision($permit_area, $projecttype, $_POST); // ------- // Permits @@ -141,16 +141,16 @@ class AllPermitsBySubdivision_Print extends TCPDF { $this->stripeLine($rowCount, 268); $rowCount++; - $address = "{$permit->rdi_address} {$permit->rdi_city}, {$permit->rdi_state} {$permit->rdi_zip}"; + $address = "{$permit->permit_address} {$permit->permit_city}, {$permit->permit_state} {$permit->permit_zip}"; - $permit_value = "$" . number_format((int) $permit->rdi_value, 2); + $permit_value = "$" . number_format((int) $permit->permit_value, 2); - $this->Cell(15, 0, $permit->rdi_county_name_verbose, 0, 0, "L", false, "", 3); - $this->Cell(40, 0, $permit->rdi_sub_div_name, 0, 0, "L", false, "", 3); + $this->Cell(15, 0, $permit->county_name_verbose, 0, 0, "L", false, "", 3); + $this->Cell(40, 0, $permit->permit_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(60, 0, $permit->permit_company, 0, 0, "L", false, "", 3); + $this->Cell(20, 0, $permit->permit_permit_date_verbose, 0, 0, "L", false, "", 3); + $this->Cell(15, 0, $permit->permit_size_verbose, 0, 0, "L", false, "", 3); $this->Cell(20, 0, $permit_value, 0, 0, "L", false, "", 3); $this->Ln(4); diff --git a/views/view_permits.php b/views/view_permits.php index edc649d..873eaa9 100644 --- a/views/view_permits.php +++ b/views/view_permits.php @@ -7,28 +7,32 @@ $SQL = "create view view_permits as date_format(permit_permit_date, '%c/%e/%Y') as permit_permit_date_verbose, date_format(permit_created, '%c/%e/%Y %l:%i %p') as permit_created_verbose, date_format(permit_changed, '%c/%e/%Y %l:%i %p') as permit_changed_verbose, - + IF( - permit_phone IS NULL OR permit_phone = '', - 'Not Provided', - CONCAT('(', SUBSTRING(permit_phone, 1, 3), ') ', - SUBSTRING(permit_phone, 4, 3), '-', + permit_phone IS NULL OR permit_phone = '', + 'Not Provided', + CONCAT('(', SUBSTRING(permit_phone, 1, 3), ') ', + SUBSTRING(permit_phone, 4, 3), '-', SUBSTRING(permit_phone, 7, 4) )) as permit_phone_verbose, concat('$', format(permit_value, 2)) as permit_value_verbose, format(permit_size, 0) as permit_size_verbose, - - trends.prev_ytd_permits as previous_ytd_permits, - trends.ytd_permits as ytd_permits, + + trends.prev_ytd_permits as previous_ytd_permits, + trends.ytd_permits as ytd_permits, trends.permits_trend, - + trends.prev_ytd_value as previous_ytd_value, - trends.ytd_value - - + trends.ytd_value, + + counties.county_name as county_name_verbose + from permits - + join view_company_permit_trends as trends - on permits.permit_company = trends.permit_company"; + on permits.permit_company = trends.permit_company + + join counties + on permits.permit_county = counties.county_serial"; ?> \ No newline at end of file diff --git a/xsl/searchPermitsBySubdivision.xsl b/xsl/searchPermitsBySubdivision.xsl index 2d40b7b..31b9357 100644 --- a/xsl/searchPermitsBySubdivision.xsl +++ b/xsl/searchPermitsBySubdivision.xsl @@ -47,7 +47,7 @@