Webgui improve series handling and plan date precision

This commit is contained in:
thooge committed 2026-10-22 22:03:04 +02:00
1 parent c2df7e8013
commit b3825cb1b3
6 files changed
+373 -272

No files matched your search

+67 -18
View File
@@ -16,6 +16,23 @@ $id = gpc_get_int($_REQUEST, 'id', 0);
$opt_equipment = db_get_opt_equip($user->vid);
$opt_state = db_load_enum('maintenance', 'maintstate', true, true);
$opt_planprec = db_load_enum('maintenance', 'plan_precision', true, true);
// series definition is derived from first maintenance record by plan_date
$opt_series = array();
$sql = "SELECT series, activities "
. "FROM ("
. "SELECT series, activities, "
. "ROW_NUMBER() OVER (PARTITION BY series ORDER BY plan_date ASC) AS n "
. "FROM maintenance "
. "WHERE series>0"
. ") AS ranked "
. "WHERE n=1";
$sth = $pdo->query($sql);
$res = $sth->fetchAll();
foreach ($res as $row) {
$opt_series[$row['series']] = $row['activities'];
}
// ========== ACTIONS START ===================================================
@@ -31,6 +48,7 @@ switch ($submit = form_get_action()) {
case 'filter':
$flt = array();
$flt['equip'] = gpc_get_int($_POST, 'flt_equip');
$flt['series'] = gpc_get_int($_POST, 'flt_series');
$flt['txt'] = gpc_get_string($_POST, 'flt_txt');
$allowed = db_load_enum('maintenance', 'maintstate');
$flt['status'] = gpc_get_set($_POST, 'flt_status', $allowed);
@@ -55,11 +73,33 @@ switch ($submit = form_get_action()) {
$p[':maintid'] = $id;
$p[':eid'] = gpc_get_int($_POST, 'eid');
$p[':activities'] = gpc_get_string($_POST, 'activities', 80);
$p[':remarks'] = gpc_get_string($_POST, 'remarks', 150);
$p[':series'] = gpc_get_int($_POST, 'series');
$series = gpc_get_int($_POST, 'series');
if ($series == -3) {
// new series number by automatic
// TODO generic db function to create a new int val in column
$sql = "SELECT MIN(n.series) AS next_series "
. "FROM ("
. "SELECT 1 AS series "
. "UNION ALL "
. "SELECT DISTINCT series + 1 "
. "FROM maintenance "
. "WHERE series > 0"
. ") AS n "
. "LEFT JOIN maintenance m ON m.series=n.series "
. "WHERE m.series IS NULL";
$p[':series'] = $pdo->query($sql)->fetchColumn();
} else if ($series <= 0) {
$p[':series'] = NULL;
} else {
$p[':series'] = $series;
}
$p[':plan_date'] = gpc_get_date($_POST, 'plan_date');
$p[':plan_precision'] = gpc_get_enum($_POST, 'plan_precision', array_keys($opt_planprec));
$p[':interval_hours'] = gpc_get_int($_POST, 'interval_hours');
$p[':last_hours'] = gpc_get_int($_POST, 'last_hours');
$p[':completion_date'] = gpc_get_date($_POST, 'completion_date');
$p[':maintstate'] = gpc_get_string($_POST, 'maintstate');
$p[':remarks'] = gpc_get_string($_POST, 'remarks', 150);
db_exec_update('maintenance', $p, 'maintid');
$action = ACT_VIEW;
break;
@@ -84,13 +124,14 @@ if ($action == ACT_DEFAULT):
page_caption_search($pagetitle);
// load filter from db
$flt = db_get_filter($user->id, 204, ['equip','txt']);
$flt = db_get_filter($user->id, 204, ['equip','series','txt']);
// Filter
$opt_special = array(
-2 => _('&horbar; all &horbar;'),
-1 => _('&horbar; none &horbar;')
);
?>
<form method="post" action="<?=$g_scriptname?>">
<div id="filter" class="card">
@@ -110,6 +151,9 @@ foreach ($opt_special + $opt_equipment as $k => $v) {
?>
</select>
</div>
<?php
filter_create_select('flt_series', _('Series'), $g_opt_all + $g_opt_none + $opt_series, $flt->series);
?>
<div class="d-flex flex-nowrap gap-2">
<label for="flt_txt">Text</label>
<input type="text" class="form-control" name="flt_txt" maxlength="30" value="<?=$flt->txt ?? ''?>">
@@ -140,13 +184,18 @@ if (isset($flt->equip) and ($flt->equip == -1 or $flt->equip == 0)) {
$w[] = 'm.eid=:eid';
$p[':eid'] = $flt->equip;
}
if (isset($flt->series) and ($flt->series == -1)) {
$w[] = 'm.series IS NULL';
} elseif($flt->series > 0) {
$w[] = 'm.series=:series';
$p[':series'] = $flt->series;
}
if (strlen($flt->txt) > 1) {
$w[] = '(m.activities LIKE :txt OR m.remarks LIKE :txt)';
$p[':txt'] = '%'.$flt->txt.'%';
}
// condition for multiple flags is OR
// $parts = ["maintstate=''", 'maintstate IS NULL'];
$parts = [];
$parts = ['maintstate IS NULL']; // to get empty result
foreach ($flt->status as $s) {
$parts[] = "maintstate='$s'";
}
@@ -182,7 +231,6 @@ $sql = "SELECT m.maintid, m.activities, m.plan_date, m.interval_hours, m.maintst
$sql .= ' WHERE ' . $where;
$sql .= $order;
// if pagination:
$sql .= ' LIMIT ' . ($page - 1) * $rows_pp . ',' . $rows_pp;
@@ -217,8 +265,8 @@ foreach ($res as $row) {
echo "<td>", h($row['activities']) ,"</td>\n";
echo "<td>", $row['ename'] ,"</td>\n";
// next plan date or hours
echo "<td>", $row['plan_date'], "</td>\n";
echo "<td>", $row['maintstate'], "</td>\n";
echo "<td>", format_date($row['plan_date']), "</td>\n";
echo "<td>", get_enum($row['maintstate']), "</td>\n";
echo "<td>", h($row['remarks']) ,"</td>\n";
echo "<td>";
// Edit current record button
@@ -275,7 +323,8 @@ echo '<h2>', _('View Maintenance'), "</h2>\n";
// load maintenance record
$sql = "SELECT m.activities, m.eid, m.remarks, m.series, m.maintstate, e.ename, "
. "m.plan_date, m.completion_date, m.interval_hours, m.last_hours "
. "m.plan_date, m.plan_precision, m.completion_date, m.interval_hours, "
. "m.last_hours "
. "FROM maintenance AS m LEFT OUTER JOIN equipment AS e USING (eid) "
. "WHERE m.maintid=?";
$sth = $pdo->prepare($sql);
@@ -313,6 +362,7 @@ echo '<tr><th>', _('Series'),"</th><td>", $maint->series, "</td></tr>\n";
echo '<tr><th>', _('Interval'),"</th><td>", $maint->interval_hours, " h</td></tr>\n";
echo '<tr><th>', _('Last'),"</th><td>", $maint->last_hours, "</td></tr>\n";
echo '<tr><th>', _('Plan date'),"</th><td>", format_date($maint->plan_date), "</td></tr>\n";
echo '<tr><th>', _('Plan date precision'),"</th><td>", get_enum($maint->plan_precision), "</td></tr>\n";
echo '<tr><th>', _('Completion&nbsp;date'),"</th><td>", format_date($maint->completion_date), "</td></tr>\n";
echo '<tr><th scope="row">', _('State'),"</th><td>", get_enum($maint->maintstate), "</td></tr>\n";
@@ -397,15 +447,10 @@ echo '<h2>', _('Edit Maintenance'), "</h2>\n";
<label for="activities" class="form-label"><?=_('Activities')?></label>
<input type="text" class="form-control" id="activities" name="activities" value="<?=h($maint->activities)?>">
</div>
<?php form_create_select('eid', _('Equipment'), $opt_equipment, $maint->eid); ?>
<div class="mb-3">
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
<textarea class="form-control" id="remarks" name="remarks" rows="3"><?=h($maint->remarks)?></textarea>
</div>
<div class="mb-3">
<label for="series" class="form-label"><?=_('Series')?></label>
<input type="text" class="form-control" id="series" name="series" maxlength="4" value="<?=$maint->series ?>">
</div>
<?php
form_create_select('eid', _('Equipment'), $opt_equipment, $maint->eid);
form_create_select('series', _('Series'), $g_opt_none + $g_opt_new + $opt_series, $maint->series);
?>
<div class="mb-3">
<label for="interval_hours" class="form-label"><?=_('Interval hours')?></label>
<input type="number" class="form-control" id="interval_hours" name="interval_hours" inputmode="numeric" maxlength="6" value="<?=$maint->interval_hours ?>">
@@ -418,12 +463,16 @@ echo '<h2>', _('Edit Maintenance'), "</h2>\n";
<label for="plan_date"><?=_('Plan date')?></label>
<input type="date" class="form-control" id="plan_date" name="plan_date" value="<?=$maint->plan_date ?>">
</div>
<?php
form_create_select('plan_precision', _('Date plan precision'), $opt_planprec, $maint->plan_precision);
?>
<div class="mb-3">
<label for="completion_date"><?=_('Completion date')?></label>
<input type="date" class="form-control" id="completion_date" name="completion_date" value="<?=$maint->completion_date ?>">
</div>
<?php
form_create_select('maintstate', _('Status'), $opt_state, $maint->maintstate);
form_create_textarea('remarks', _('Remarks'), 3, 150, $maint->remarks);
form_edit_buttons($g_scriptname, $id);
echo "</form>\n";