Contracts stage 3: prepaid hours - hours included each month, and blocks bought with a price, expiry, note and an optional draft invoice; a nightly draw of billable ticket time, oldest ticket first from the hours that run out first, with Draw now and Put back, and a ticket the hours run out on staying in Unbilled Time at the usual rate; Prepaid hours card, a low hours email to the owner, and hours left on the contracts list, client overview, ticket, Unbilled Time and the client portal; [Monthly Hours] placeholder; new functions/contract_hours.php; DB 3.4.4 adds monthly hours and the alert level to contracts and templates, plus contract_blocks and contract_block_uses

This commit is contained in:
johnnyq
2026-10-25 01:06:27 -04:00
parent d0620067a1
commit 0f9038b1ed
27 changed files with 1431 additions and 35 deletions

View File

@@ -0,0 +1,65 @@
<?php
/*
* ITFlow - Database update to version 3.4.4 (from 3.4.3)
* Included by admin/database_updates.php - do not access directly
*
* Client contracts, stage 3: prepaid hours. A contract's blocks of hours -
* bought, or included each month - are contract_blocks; the time drawn from
* them, ticket by ticket, is contract_block_uses (functions/contract_hours.php).
* A contract (and a template) can include hours each month, and say how
* few hours left is low enough to tell its owner.
*/
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
$itflow_column = function ($table, $column) use ($mysqli) {
$result = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$table' AND COLUMN_NAME = '$column' LIMIT 1");
return $result && mysqli_num_rows($result) > 0;
};
foreach (['contract_templates' => 'contract_template', 'contracts' => 'contract'] as $itflow_table => $itflow_prefix) {
if (!$itflow_column($itflow_table, "{$itflow_prefix}_monthly_hours")) {
mysqli_query($mysqli, "ALTER TABLE `$itflow_table` ADD `{$itflow_prefix}_monthly_hours` decimal(8,2) NOT NULL DEFAULT 0.00");
}
if (!$itflow_column($itflow_table, "{$itflow_prefix}_hours_alert")) {
mysqli_query($mysqli, "ALTER TABLE `$itflow_table` ADD `{$itflow_prefix}_hours_alert` decimal(8,2) NOT NULL DEFAULT 0.00");
}
}
if (!$itflow_column('contracts', 'contract_hours_alerted_at')) {
mysqli_query($mysqli, "ALTER TABLE `contracts` ADD `contract_hours_alerted_at` datetime DEFAULT NULL");
}
mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS `contract_blocks` (
`contract_block_id` int(11) NOT NULL AUTO_INCREMENT,
`contract_block_contract_id` int(11) NOT NULL,
`contract_block_hours` decimal(8,2) NOT NULL,
`contract_block_price` decimal(15,2) DEFAULT NULL,
`contract_block_bought` date NOT NULL,
`contract_block_expires` date DEFAULT NULL,
`contract_block_month` char(7) DEFAULT NULL,
`contract_block_invoice_id` int(11) NOT NULL DEFAULT 0,
`contract_block_note` varchar(255) DEFAULT NULL,
`contract_block_created_by` int(11) NOT NULL DEFAULT 0,
`contract_block_created_at` datetime NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`contract_block_id`),
UNIQUE KEY `contract_block_month` (`contract_block_contract_id`, `contract_block_month`),
CONSTRAINT `contract_blocks_ibfk_1` FOREIGN KEY (`contract_block_contract_id`) REFERENCES `contracts` (`contract_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS `contract_block_uses` (
`contract_block_use_id` int(11) NOT NULL AUTO_INCREMENT,
`contract_block_use_block_id` int(11) NOT NULL,
`contract_block_use_ticket_id` int(11) NOT NULL,
`contract_block_use_hours` decimal(8,2) NOT NULL,
`contract_block_use_full` tinyint(1) NOT NULL DEFAULT 0,
`contract_block_use_created_by` int(11) NOT NULL DEFAULT 0,
`contract_block_use_created_at` datetime NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`contract_block_use_id`),
KEY `contract_block_use_block_id` (`contract_block_use_block_id`),
KEY `contract_block_use_ticket_id` (`contract_block_use_ticket_id`),
CONSTRAINT `contract_block_uses_ibfk_1` FOREIGN KEY (`contract_block_use_block_id`) REFERENCES `contract_blocks` (`contract_block_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
unset($itflow_column, $itflow_table, $itflow_prefix);

View File

@@ -16,6 +16,9 @@ $t_value = function ($key, $default = '') use ($t) {
$t_money = function ($value) {
return $value === '' || $value === null ? '' : number_format((float) $value, 2, '.', '');
};
$t_hours = function ($value) {
return floatval($value) > 0 ? rtrim(rtrim(number_format(floatval($value), 2, '.', ''), '0'), '.') : '';
};
$t_term = intval($t_value('term_months', 12));
$t_slas = contractSlaChoices($t ? array_map(function ($part) use ($t) { return intval($t["contract_template_sla_{$part}_id"]); }, CONTRACT_SLA_PRIORITIES) : []);
$tab = 'templateTab' . bin2hex(random_bytes(3));
@@ -96,6 +99,27 @@ $tab = 'templateTab' . bin2hex(random_bytes(3));
<input type="text" class="form-control" name="support_hours" maxlength="100" placeholder="Mon-Fri 8am-6pm, emergencies 24/7" value="<?= escapeHtml($t_value('support_hours')) ?>">
</div>
</div>
<?php if (contractHoursReady()) { ?>
<div class="row g-3 mb-1">
<div class="col-md-6">
<label class="form-label">Hours included each month</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-hourglass-half"></i></span>
<input type="text" inputmode="decimal" class="form-control" name="monthly_hours" placeholder="None" value="<?= escapeHtml($t_hours($t_value('monthly_hours', 0))) ?>">
<span class="input-group-text">hours</span>
</div>
</div>
<div class="col-md-6">
<label class="form-label">Tell the owner under</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-bell"></i></span>
<input type="text" inputmode="decimal" class="form-control" name="hours_alert" placeholder="Don't tell" value="<?= escapeHtml($t_hours($t_value('hours_alert', 0))) ?>">
<span class="input-group-text">hours left</span>
</div>
</div>
</div>
<small class="text-secondary d-block mb-3">Tickets' time is drawn from the hours included each month - for that month only - and from any hours bought on the contract, before it is billed.</small>
<?php } ?>
<div class="row g-3 mb-3">
<div class="col-md-4">
<label class="form-label">Hourly rate</label>

View File

@@ -64,6 +64,9 @@ if (isset($_GET['copy_contract_template'])) {
$columns = ['description', 'type', 'rate_standard', 'rate_after_hours', 'net_terms', 'support_hours', 'details',
'term_months', 'auto_renew', 'notice_days', 'sla_low_id', 'sla_medium_id', 'sla_high_id', 'sla_urgent_id'];
if (contractHoursReady()) {
array_push($columns, 'monthly_hours', 'hours_alert');
}
$columns = implode(', ', array_map(function ($column) { return "contract_template_$column"; }, $columns));
mysqli_query($mysqli, "INSERT INTO contract_templates (contract_template_name, $columns)
SELECT '" . escapeSql($new_name) . "', $columns FROM contract_templates WHERE contract_template_id = $contract_template_id");