462 lines
14 KiB
PHP
462 lines
14 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
class Budgets extends Base {
|
||
|
|
|
||
|
|
// Variables.
|
||
|
|
|
||
|
|
private $current_user_serial = 0;
|
||
|
|
private $current_user_id = "";
|
||
|
|
private $current_user_name = "";
|
||
|
|
private $current_user_email = "";
|
||
|
|
private $pagination_size = 20;
|
||
|
|
private $pagination_page = 1;
|
||
|
|
|
||
|
|
// =================
|
||
|
|
// 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] ?? "";
|
||
|
|
|
||
|
|
$this->pagination_page = $_SESSION[self::PAGINATION_PAGE] ?? 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
// ============
|
||
|
|
// View Budgets
|
||
|
|
// ============
|
||
|
|
|
||
|
|
public function viewBudgets() {
|
||
|
|
|
||
|
|
$budgets = $this->getBudgetsPage();
|
||
|
|
$popovers = (new Popovers())->getPopovers();
|
||
|
|
|
||
|
|
$XML = $this->mergeXML($budgets, "<XML/>");
|
||
|
|
$XML = $this->mergeXML($popovers, $XML);
|
||
|
|
|
||
|
|
$content = $this->applyXSL($XML, $this->getXSL("viewBudgets"));
|
||
|
|
|
||
|
|
$this->displayContent($content);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ==================
|
||
|
|
// View Budget Months
|
||
|
|
// ==================
|
||
|
|
|
||
|
|
public function viewBudgetMonths() {
|
||
|
|
|
||
|
|
$budget_serial = filter_input(INPUT_POST, "budget_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
||
|
|
|
||
|
|
$budgetmonths = $this->getBudgetMonths($budget_serial);
|
||
|
|
|
||
|
|
$content = $this->applyXSL($budgetmonths, $this->getXSL("viewBudgetMonths"));
|
||
|
|
|
||
|
|
$this->displayContent($content);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ==============
|
||
|
|
// Budget Details
|
||
|
|
// ==============
|
||
|
|
|
||
|
|
public function budgetDetails() {
|
||
|
|
|
||
|
|
$budgetmonth_serial = filter_input(INPUT_POST, "budgetmonth_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
||
|
|
|
||
|
|
$budgetdetails = $this->getBudgetDetails($budgetmonth_serial);
|
||
|
|
|
||
|
|
$content = $this->applyXSL($budgetdetails, $this->getXSL("budgetDetails"));
|
||
|
|
|
||
|
|
$this->displayContent($content);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ==========
|
||
|
|
// Add Budget
|
||
|
|
// ==========
|
||
|
|
|
||
|
|
public function addBudget() {
|
||
|
|
|
||
|
|
$step = filter_input(INPUT_POST, "step") ?: "prompt";
|
||
|
|
|
||
|
|
switch ($step) {
|
||
|
|
|
||
|
|
case "prompt":
|
||
|
|
|
||
|
|
$content = $this->applyXSL("", $this->getXSL("addBudget"));
|
||
|
|
|
||
|
|
$this->displayContent($content);
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
case "add":
|
||
|
|
|
||
|
|
$budget_year = filter_input(INPUT_POST, "budget_year", FILTER_SANITIZE_NUMBER_INT);
|
||
|
|
$budget_year = ($budget_year !== null && $budget_year !== false && $budget_year !== '') ? (int) $budget_year : null;
|
||
|
|
|
||
|
|
$connection = $this->connect();
|
||
|
|
|
||
|
|
try {
|
||
|
|
$connection->beginTransaction();
|
||
|
|
|
||
|
|
// -------------------
|
||
|
|
// Insert into budgets
|
||
|
|
// -------------------
|
||
|
|
|
||
|
|
$sql = "INSERT INTO budgets
|
||
|
|
( budget_year,
|
||
|
|
budget_creator,
|
||
|
|
budget_created )
|
||
|
|
VALUES
|
||
|
|
( :budget_year,
|
||
|
|
:budget_creator,
|
||
|
|
:budget_created )";
|
||
|
|
|
||
|
|
$statement = $connection->prepare($sql);
|
||
|
|
|
||
|
|
$statement->bindValue(':budget_year', $budget_year, PDO::PARAM_INT);
|
||
|
|
$statement->bindValue(':budget_creator', $this->current_user_name);
|
||
|
|
$statement->bindValue(':budget_created', $this->getTimestamp());
|
||
|
|
$statement->execute();
|
||
|
|
|
||
|
|
$budget_id = (int) $connection->lastInsertId();
|
||
|
|
|
||
|
|
// -------------------------
|
||
|
|
// Insert 12 months records
|
||
|
|
// -------------------------
|
||
|
|
|
||
|
|
$monthlist = (new Dropdowns())->getDropdownsByDropdownType(1);
|
||
|
|
|
||
|
|
$sql = "INSERT INTO budgetmonths
|
||
|
|
( budgetmonth_budget,
|
||
|
|
budgetmonth_month,
|
||
|
|
budgetmonth_creator,
|
||
|
|
budgetmonth_created )
|
||
|
|
VALUES
|
||
|
|
( :budgetmonth_budget,
|
||
|
|
:budgetmonth_month,
|
||
|
|
:budgetmonth_creator,
|
||
|
|
:budgetmonth_created )";
|
||
|
|
|
||
|
|
$statement = $connection->prepare($sql);
|
||
|
|
|
||
|
|
foreach ($monthlist as $month) {
|
||
|
|
$statement->bindValue(':budgetmonth_budget', $budget_id, PDO::PARAM_INT);
|
||
|
|
$statement->bindValue(':budgetmonth_month', (int) $month->dropdown_serial, PDO::PARAM_INT);
|
||
|
|
$statement->bindValue(':budgetmonth_creator', $this->current_user_name);
|
||
|
|
$statement->bindValue(':budgetmonth_created', $this->getTimestamp());
|
||
|
|
$statement->execute();
|
||
|
|
}
|
||
|
|
|
||
|
|
$connection->commit();
|
||
|
|
} catch (Throwable $e) {
|
||
|
|
if ($connection->inTransaction()) {
|
||
|
|
$connection->rollBack();
|
||
|
|
}
|
||
|
|
throw $e; // or handle/log and return a friendly error
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
default:
|
||
|
|
|
||
|
|
$this->displayHome();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// =============
|
||
|
|
// Delete Budget
|
||
|
|
// =============
|
||
|
|
|
||
|
|
public function deleteBudget() {
|
||
|
|
|
||
|
|
$budget_serial = filter_input(INPUT_POST, "budget_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
||
|
|
|
||
|
|
$budget = $this->getBudget($budget_serial);
|
||
|
|
|
||
|
|
if ($budget->count() == 0) {
|
||
|
|
$this->logError("Invalid Budget ({$budget_serial}). " . __METHOD__);
|
||
|
|
}
|
||
|
|
|
||
|
|
$SQL = "delete from budgets
|
||
|
|
where budget_serial = {$budget_serial}";
|
||
|
|
|
||
|
|
$this->executeSQL($SQL);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ============================
|
||
|
|
// Determine if a Budget exists
|
||
|
|
// ============================
|
||
|
|
|
||
|
|
public function budgetExists() {
|
||
|
|
|
||
|
|
$budget_serial = filter_input(INPUT_POST, "budget_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
||
|
|
$budget_year = filter_input(INPUT_POST, "budget_year") ?: "";
|
||
|
|
|
||
|
|
$SQL = "select budget_serial
|
||
|
|
from view_budgets
|
||
|
|
where budget_serial != :budget_serial
|
||
|
|
and lower(budget_year) = :budget_year";
|
||
|
|
|
||
|
|
$statement = $this->connect()->prepare($SQL);
|
||
|
|
|
||
|
|
$statement->bindValue(":budget_serial", $budget_serial);
|
||
|
|
$statement->bindValue(":budget_year", strtolower($budget_year));
|
||
|
|
|
||
|
|
$statement->execute();
|
||
|
|
|
||
|
|
$recordset = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||
|
|
|
||
|
|
$exists = (count($recordset) > 0) ? true : false;
|
||
|
|
|
||
|
|
echo json_encode($exists);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ======================================
|
||
|
|
// Determine if a Budget is Active (AJAX)
|
||
|
|
// ======================================
|
||
|
|
|
||
|
|
public function budgetActive() {
|
||
|
|
|
||
|
|
$budget_serial = filter_input(INPUT_POST, "budget_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
||
|
|
|
||
|
|
$active = false;
|
||
|
|
|
||
|
|
$SQL = "select workorder_budget
|
||
|
|
from workorders
|
||
|
|
where workorder_budget = {$budget_serial}
|
||
|
|
limit 1";
|
||
|
|
|
||
|
|
$workorders = $this->getTable("workorders", $SQL);
|
||
|
|
|
||
|
|
$active = ($workorders->count() > 0) ? true : false;
|
||
|
|
|
||
|
|
echo json_encode($active);
|
||
|
|
}
|
||
|
|
|
||
|
|
// =============
|
||
|
|
// Edit a Budget
|
||
|
|
// =============
|
||
|
|
|
||
|
|
public function editBudget() {
|
||
|
|
|
||
|
|
$step = filter_input(INPUT_POST, "step") ?: "prompt";
|
||
|
|
|
||
|
|
switch ($step) {
|
||
|
|
|
||
|
|
case "prompt":
|
||
|
|
|
||
|
|
$budget_serial = filter_input(INPUT_POST, "budget_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
||
|
|
|
||
|
|
$budget = $this->getBudget($budget_serial);
|
||
|
|
|
||
|
|
if ($budget->count() == 0) {
|
||
|
|
$this->logError("Invalid Budget ({$budget_serial}). " . __METHOD__);
|
||
|
|
}
|
||
|
|
|
||
|
|
$XML = $this->mergeXML($budget, "<XML/>");
|
||
|
|
|
||
|
|
$content = $this->applyXSL($XML, $this->getXSL("editBudget"));
|
||
|
|
|
||
|
|
$this->displayContent($content);
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
case "update":
|
||
|
|
|
||
|
|
$budget_serial = filter_input(INPUT_POST, "budget_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
||
|
|
$budget_name = filter_input(INPUT_POST, "budget_name") ?: "";
|
||
|
|
$budget_classes = filter_input(INPUT_POST, "budget_classes") ?: "";
|
||
|
|
$budget_active = filter_input(INPUT_POST, "budget_active", FILTER_VALIDATE_BOOLEAN) ?: false;
|
||
|
|
$budget_default = filter_input(INPUT_POST, "budget_default", FILTER_VALIDATE_BOOLEAN) ?: false;
|
||
|
|
|
||
|
|
// Verify the Budget
|
||
|
|
|
||
|
|
$budget = $this->getBudget($budget_serial);
|
||
|
|
|
||
|
|
if ($budget->count() == 0) {
|
||
|
|
$this->logError("Invalid Budget ({$budget_serial}). " . __METHOD__);
|
||
|
|
}
|
||
|
|
|
||
|
|
$SQL = "update budgets
|
||
|
|
set budget_name = :budget_name,
|
||
|
|
budget_classes = :budget_classes,
|
||
|
|
budget_active = :budget_active,
|
||
|
|
budget_default = :budget_default,
|
||
|
|
budget_changer = :budget_changer,
|
||
|
|
budget_changed = :budget_changed
|
||
|
|
where budget_serial = :budget_serial";
|
||
|
|
|
||
|
|
$statement = $this->connect()->prepare($SQL);
|
||
|
|
|
||
|
|
$statement->bindValue(":budget_serial", $budget_serial);
|
||
|
|
$statement->bindValue(":budget_name", $budget_name);
|
||
|
|
$statement->bindValue(":budget_classes", $budget_classes);
|
||
|
|
$statement->bindValue(":budget_active", $budget_active, PDO::PARAM_BOOL);
|
||
|
|
$statement->bindValue(":budget_default", $budget_default, PDO::PARAM_BOOL);
|
||
|
|
$statement->bindValue(":budget_changer", $this->current_user_name);
|
||
|
|
$statement->bindValue(":budget_changed", $this->getTimestamp());
|
||
|
|
|
||
|
|
$statement->execute();
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
default:
|
||
|
|
|
||
|
|
$this->displayHome();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ================================
|
||
|
|
// Return an Active Budgets Object
|
||
|
|
// ================================
|
||
|
|
|
||
|
|
public function getActiveBudgets() {
|
||
|
|
|
||
|
|
$SQL = "select view_budgets.*
|
||
|
|
from view_budgets
|
||
|
|
where budget_active is true";
|
||
|
|
|
||
|
|
return $this->getTable("budgets", $SQL);
|
||
|
|
}
|
||
|
|
|
||
|
|
// -------------------------
|
||
|
|
// Return the Default Budget
|
||
|
|
// -------------------------
|
||
|
|
|
||
|
|
public function getDefaultBudget() {
|
||
|
|
|
||
|
|
$SQL = "select budget_serial
|
||
|
|
from view_budgets
|
||
|
|
where budget_default is true";
|
||
|
|
|
||
|
|
return $this->getTable("budgets", $SQL);
|
||
|
|
}
|
||
|
|
|
||
|
|
// =======================
|
||
|
|
// Return an Budget Object
|
||
|
|
// =======================
|
||
|
|
|
||
|
|
public function getBudget($budget_serial = 0) {
|
||
|
|
|
||
|
|
$SQL = "select view_budgets.*
|
||
|
|
from view_budgets
|
||
|
|
where budget_serial = {$budget_serial}";
|
||
|
|
|
||
|
|
return $this->getTable("budgets", $SQL);
|
||
|
|
}
|
||
|
|
|
||
|
|
// =======================
|
||
|
|
// Return an Budget Object
|
||
|
|
// =======================
|
||
|
|
|
||
|
|
public function getBudgetMonths($budget_serial = 0) {
|
||
|
|
|
||
|
|
$SQL = "select view_budgetmonths.*
|
||
|
|
from view_budgetmonths
|
||
|
|
where view_budgetmonths.budgetmonth_budget = {$budget_serial}";
|
||
|
|
|
||
|
|
return $this->getTable("budgetmonths", $SQL);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ===========================
|
||
|
|
// Return a Budgets Object
|
||
|
|
// ===========================
|
||
|
|
|
||
|
|
public function getBudgets() {
|
||
|
|
|
||
|
|
$SQL = "select view_budgets.*
|
||
|
|
from view_budgets";
|
||
|
|
|
||
|
|
return $this->getTable("budgets", $SQL);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ============================
|
||
|
|
// Return a Budgets Page Object
|
||
|
|
// ============================
|
||
|
|
|
||
|
|
public function getBudgetsPage() {
|
||
|
|
|
||
|
|
// Find Criteria
|
||
|
|
|
||
|
|
$find_budget = filter_input(INPUT_POST, "find_budget") ?: "";
|
||
|
|
$find_budget = $this->sanitizeString($find_budget);
|
||
|
|
|
||
|
|
$find_words = implode("|", explode(" ", $find_budget));
|
||
|
|
|
||
|
|
// Record Count
|
||
|
|
|
||
|
|
$connection = $this->connect();
|
||
|
|
|
||
|
|
$SQL = "select count(*) as count
|
||
|
|
from view_budgets
|
||
|
|
where budget_year regexp :budget_year";
|
||
|
|
|
||
|
|
$statement = $connection->prepare($SQL);
|
||
|
|
|
||
|
|
$statement->bindValue(":budget_year", $find_words);
|
||
|
|
|
||
|
|
$statement->execute();
|
||
|
|
|
||
|
|
$recordset = $statement->fetch(PDO::FETCH_ASSOC);
|
||
|
|
|
||
|
|
$count = (count($recordset) > 0) ? (integer) $recordset["count"] : (integer) 0;
|
||
|
|
|
||
|
|
// Pagination
|
||
|
|
|
||
|
|
$pagination = filter_input(INPUT_POST, "pagination") ?: "";
|
||
|
|
|
||
|
|
switch ($pagination) {
|
||
|
|
|
||
|
|
case "first" : $this->pagination_page = 1;
|
||
|
|
break;
|
||
|
|
case "previous" : $this->pagination_page = max(($this->pagination_page - 1), 1);
|
||
|
|
break;
|
||
|
|
case "next" : $this->pagination_page = ($this->pagination_page + 1);
|
||
|
|
break;
|
||
|
|
case "last" : $this->pagination_page = ($count / $this->pagination_size);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
$offset = (($this->pagination_page * $this->pagination_size) - $this->pagination_size);
|
||
|
|
$start = ($offset + 1);
|
||
|
|
$end = min($count, (($start + $this->pagination_size) - 1));
|
||
|
|
|
||
|
|
// Data
|
||
|
|
|
||
|
|
$SQL = "select view_budgets.*
|
||
|
|
from view_budgets
|
||
|
|
where budget_year regexp :budget_year
|
||
|
|
order by budget_year
|
||
|
|
limit {$this->pagination_size}
|
||
|
|
offset {$offset}";
|
||
|
|
|
||
|
|
$statement = $connection->prepare($SQL);
|
||
|
|
|
||
|
|
$statement->bindValue(":budget_year", $find_words);
|
||
|
|
|
||
|
|
$statement->execute();
|
||
|
|
|
||
|
|
$budgets = $this->getTableXML("budgets", $statement);
|
||
|
|
|
||
|
|
// Insert Pagination Attributes
|
||
|
|
|
||
|
|
$budgets->addAttribute("count", $count);
|
||
|
|
$budgets->addAttribute("start", $start);
|
||
|
|
$budgets->addAttribute("end", $end);
|
||
|
|
$budgets->addAttribute("page", $this->pagination_page);
|
||
|
|
|
||
|
|
$_SESSION[self::PAGINATION_PAGE] = $this->pagination_page;
|
||
|
|
|
||
|
|
return $budgets;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
?>
|