456 lines
16 KiB
PHP
456 lines
16 KiB
PHP
<?php
|
|
/******************************************************************************
|
|
* YMS - Yacht Management Software
|
|
* Copyright (C) 2024-2026 Thomas Hooge
|
|
*
|
|
* SPDX-License-Identifier: WTFPL
|
|
******************************************************************************/
|
|
|
|
require 'globals.inc';
|
|
require 'lib/gpc.inc';
|
|
|
|
$pagetitle=_('Company');
|
|
|
|
$id = gpc_get_int($_REQUEST, 'id', 0);
|
|
$opt_comptype = db_get_options(2);
|
|
|
|
// ========== ACTIONS START ===================================================
|
|
|
|
switch ($submit = form_get_action()) {
|
|
|
|
case NULL: break;
|
|
|
|
case 'add': $action = ACT_ADD; break;
|
|
case 'view': $action = ACT_VIEW; break;
|
|
case 'edit': $action = ACT_EDIT; break;
|
|
case 'del': $action = ACT_DELETE; break;
|
|
|
|
case 'filter':
|
|
$flt = array();
|
|
$flt['type'] = gpc_get_int($_POST, 'flt_type');
|
|
$flt['txt'] = gpc_get_string($_POST, 'flt_txt');
|
|
db_save_filter($flt, 208);
|
|
$action = ACT_DEFAULT;
|
|
break;
|
|
|
|
case 'freset':
|
|
db_clear_filter(208);
|
|
$action = ACT_DEFAULT;
|
|
break;
|
|
|
|
case 'insert':
|
|
$p[':compname'] = gpc_get_string($_POST, 'compname');
|
|
$p[':comptype'] = gpc_get_int($_POST, 'comptype');
|
|
$id = db_exec_insert('company', $p);
|
|
$action = ACT_VIEW;
|
|
break;
|
|
|
|
case 'update':
|
|
$p[':compid'] = $id;
|
|
$p[':compname'] = gpc_get_string($_POST, 'compname');
|
|
$p[':shortname'] = gpc_get_string($_POST, 'shortname', 20);
|
|
$p[':comptype'] = gpc_get_int($_POST, 'comptype');
|
|
$p[':comptype2'] = gpc_get_int($_POST, 'comptype2');
|
|
if ($p[':comptype2'] <= 0) $p[':comptype2'] = NULL;
|
|
$p[':street'] = gpc_get_string($_POST, 'street', 40);
|
|
$p[':zip'] = gpc_get_string($_POST, 'zip', 10);
|
|
$p[':city'] = gpc_get_string($_POST, 'city', 30);
|
|
$p[':country'] = gpc_get_string($_POST, 'country', 30);
|
|
$p[':contact'] = gpc_get_string($_POST, 'contact', 30);
|
|
$p[':phone'] = gpc_get_string($_POST, 'phone', 30);
|
|
$p[':email'] = gpc_get_string($_POST, 'email', 50);
|
|
$p[':web'] = gpc_get_string($_POST, 'web', 40);
|
|
$p[':customerno'] = gpc_get_string($_POST, 'customerno', 20);
|
|
$p[':contractno'] = gpc_get_string($_POST, 'contractno', 20);
|
|
$p[':remarks'] = gpc_get_string($_POST, 'remarks', 150);
|
|
db_exec_update('company', $p, 'compid');
|
|
$action = ACT_VIEW;
|
|
break;
|
|
|
|
case 'delete':
|
|
// Security token needed!
|
|
if (gpc_get_string($_POST, 'token', 16) != $_SESSION['token']) {
|
|
$g_error->Add(_('Delete prohibited, invalid security token!'));
|
|
$action = ACT_VIEW;
|
|
break;
|
|
}
|
|
unset($_SESSION['token']);
|
|
$sth = $pdo->prepare("DELETE FROM company WHERE compid=?");
|
|
$sth->execute([$id]);
|
|
$g_message->Add(sprintf(_('Deleted company no. %d'), $id));
|
|
$action = ACT_DEFAULT;
|
|
break;
|
|
|
|
default:
|
|
$g_error->Add(sprintf(_('Unknown function!'), $submit));
|
|
$valid = FALSE;
|
|
|
|
}
|
|
|
|
// ========== ACTIONS END =====================================================
|
|
|
|
require 'header.php';
|
|
|
|
// ========== PAGE CONTENT ====================================================
|
|
|
|
if ($action == ACT_DEFAULT):
|
|
// ========== VARIANT: default behavior =======================================
|
|
|
|
page_caption_search($pagetitle);
|
|
|
|
// load filter from db
|
|
$flt = db_get_filter($user->id, 208, ['type','txt']);
|
|
|
|
$w = array();
|
|
$p = array();
|
|
if ($flt->type > 0) {
|
|
$w[] = 'comptype=:comptype';
|
|
$p[':comptype'] = $flt->type;
|
|
}
|
|
if (strlen($flt->txt) > 1) {
|
|
$w[] = '(compname LIKE :txt OR remarks LIKE :txt)';
|
|
$p[':txt'] = '%'.$flt->txt.'%';
|
|
}
|
|
$where = join(' AND ', $w);
|
|
$order = ' ORDER BY comptype, compname';
|
|
|
|
// grand total of records for current vessel
|
|
$grandtotal = $pdo->query("SELECT COUNT(*) FROM company")->fetchColumn();
|
|
|
|
// get total record count for pagination and limit
|
|
$sql = "SELECT COUNT(*) FROM company";
|
|
if ($where) {
|
|
$sql .= " WHERE $where";
|
|
}
|
|
$sth = $pdo->prepare($sql);
|
|
try {
|
|
$sth->execute($p);
|
|
} catch(PDOException $e) {
|
|
$g_error->Add($e->getMessage());
|
|
$g_error->Add($sql);
|
|
$g_error->Add(print_r($p, true));
|
|
$g_error->PrintOut();
|
|
}
|
|
$numrows = $sth->fetchColumn();
|
|
$page = gpc_get_int($_REQUEST, 'p', 1);
|
|
$rows_pp = gpc_get_int($_REQUEST, 'n', $user->rows_pp);
|
|
$lastpage = ceil($numrows/$rows_pp);
|
|
|
|
$sql = "SELECT compid, compname, comptype, remarks "
|
|
. "FROM company";
|
|
if ($where) $sql .= ' WHERE ' . $where;
|
|
$sql .= $order;
|
|
|
|
// if pagination:
|
|
$sql .= ' LIMIT ' . ($page - 1) * $rows_pp . ',' . $rows_pp;
|
|
|
|
$sth = $pdo->prepare($sql);
|
|
try {
|
|
$sth->execute($p);
|
|
} catch(PDOException $e) {
|
|
$g_error->Add($e->getMessage());
|
|
$g_error->Add($sql);
|
|
$g_error->Add(print_r($p, true));
|
|
}
|
|
$res = $sth->fetchAll();
|
|
|
|
// Filter
|
|
$opt_special = array(
|
|
-2 => _('― all ―'),
|
|
);
|
|
?>
|
|
<form method="post" action="<?=$g_scriptname?>">
|
|
<div id="filter" class="card">
|
|
<div class="card-header"><i class="bi bi-funnel me-2"></i>Filter</div>
|
|
<div class="card-body d-flex flex-wrap gap-3">
|
|
<div class="d-flex flex-nowrap gap-2">
|
|
<label for="flt_type">Type</label>
|
|
<select id="flt_type" class="form-select" name="flt_type">
|
|
<?php
|
|
foreach ($opt_special + $opt_comptype as $k => $v) {
|
|
echo '<option value="', $k,'"';
|
|
if ($k == $flt->type) {
|
|
echo ' selected';
|
|
}
|
|
echo '>', $v, '</option>';
|
|
}
|
|
?>
|
|
</select>
|
|
</div>
|
|
<div class="d-flex flex-nowrap gap-2">
|
|
<label for="flt_txt">Text</label>
|
|
<input type="text" class="form-control" name="flt_txt" size="15" maxlength="30" value="<?=$flt->txt;?>">
|
|
</div>
|
|
</div>
|
|
<div class="card-footer">
|
|
<button name="submit[filter]" class="btn btn-sm btn-primary" title="<?=_('Apply')?>"><?=_('Go')?></button>
|
|
<button name="submit[freset]" class="btn btn-sm btn-secondary float-end" title="<?=_('Reset filter to defaults')?>"><?=_('Clear')?></button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
|
|
<?php
|
|
|
|
// Pagination on top
|
|
$pagination = get_pagination($g_scriptname, '', $page, $lastpage);
|
|
echo $pagination;
|
|
|
|
echo '<table class="table table-striped">', "\n";
|
|
echo "<thead>\n";
|
|
echo "<tr>";
|
|
echo '<th>#</th>';
|
|
echo "<th>" . _('Name') . "</th>";
|
|
echo "<th>" . _('Type') . "</th>";
|
|
echo "<th>" . _('Remarks') . "</th>";
|
|
echo "<th> </th>";
|
|
echo "</tr>\n";
|
|
echo "</thead>\n";
|
|
$i = ($page - 1) * $rows_pp;
|
|
$i0 = $i + 1;
|
|
foreach ($res as $row) {
|
|
$i++; // record number
|
|
echo "<tr>\n";
|
|
echo '<td>', $i;
|
|
echo '<a title="', _('View'), '" href="', $g_scriptname, '?f=view&id=', $row['compid'], '"><i class="bi bi-eye ms-2"></i></a>', "\n";
|
|
echo "</td>\n";
|
|
echo "<td>", $row['compname'], "</td>\n";
|
|
echo "<td>", $opt_comptype[$row['comptype']], "</td>\n";
|
|
echo "<td>", $row['remarks'], "</td>\n";
|
|
// Edit current record button
|
|
echo "<td>";
|
|
echo '<a title="', _('Edit'), '" href="', $g_scriptname, '?f=edit&id=', $row['compid'], '"><i class="bi-pencil"></i></a>';
|
|
echo "</td>\n";
|
|
echo "</tr>\n";
|
|
}
|
|
// Table summary
|
|
table_rec_summary($i0, $i, $numrows, 6);
|
|
echo "</table>\n";
|
|
|
|
echo $pagination;
|
|
|
|
form_add_button($g_scriptname);
|
|
|
|
|
|
elseif ($action == ACT_ADD):
|
|
// ========== VARIANT: add record =============================================
|
|
|
|
echo '<h2>', _('Add Company'), "</h2>\n";
|
|
?>
|
|
<form method="post" action="<?=$g_scriptname?>">
|
|
<div class="mb-3">
|
|
<label for="compname" class="form-label"><?=_('Name')?></label>
|
|
<input type="text" class="form-control" id="compname" name="compname">
|
|
</div>
|
|
<?php form_create_select('comptype', _('Company type'), $opt_comptype); ?>
|
|
<button type="submit" name="submit[insert]" class="btn btn-primary"><?=_('Save')?></button>
|
|
<a href="<?=$g_scriptname?>" class="btn btn-secondary"><?=_('Back')?></a>
|
|
</form>
|
|
<?php
|
|
|
|
elseif ($action == ACT_VIEW):
|
|
// ========== VARIANT: view single record =====================================
|
|
|
|
$sql = "SELECT compname, comptype, comptype2, shortname, street, zip, city,"
|
|
. " country, contact, phone, email, web, customerno, contractno, remarks "
|
|
. "FROM company "
|
|
. "WHERE compid=?";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([$id]);
|
|
$company = $sth->fetch(PDO::FETCH_OBJ);
|
|
|
|
echo "<h2>", $company->compname, "</h2>\n";
|
|
|
|
echo '<table class="table">', "\n";
|
|
echo '<tr><th scope="row" style="width:20%">', _('Name'),"</th><td>", $company->compname, "</td></tr>\n";
|
|
echo '<tr><th scope="row">', _('Short name'),"</th><td>", $company->shortname, "</td></tr>\n";
|
|
echo '<tr><th scope="row">', _('Type'),"</th><td>", $opt_comptype[$company->comptype], "</td></tr>\n";
|
|
echo '<tr><th scope="row">', _('Secondary type'),"</th><td>", $company->comptype2 ? $opt_comptype[$company->comptype2] : '-', "</td></tr>\n";
|
|
echo '<tr><th scope="row">', _('Street'),"</th><td>", $company->street, "</td></tr>\n";
|
|
echo '<tr><th scope="row">', _('Zip, City'),"</th><td>", $company->zip, ' ', $company->city, "</td></tr>\n";
|
|
echo '<tr><th scope="row">', _('Country'),"</th><td>", $company->country, "</td></tr>\n";
|
|
echo '<tr><th scope="row">', _('Contact'),"</th><td>", $company->contact, "</td></tr>\n";
|
|
|
|
echo '<tr><th scope="row">', _('Phone'),"</th><td>";
|
|
echo make_phonelink($company->phone),' ', $company->phone, "</td></tr>\n";
|
|
|
|
echo '<tr><th scope="row">', _('Email'),"</th><td>";
|
|
echo make_maillink($company->email),' ', $company->email, "</td></tr>\n";
|
|
|
|
echo '<tr><th scope="row">', _('Web'),"</th><td>";
|
|
// echo '<a title="', _('Go to website'), '" href="',
|
|
echo make_weblink($company->web), ' ', $company->web, "</td></tr>\n";
|
|
|
|
echo '<tr><th scope="row">', _('Customer no.'),"</th><td>", $company->customerno, "</td></tr>\n";
|
|
echo '<tr><th scope="row">', _('Contract no.'),"</th><td>", $company->contractno, "</td></tr>\n";
|
|
echo '<tr><th scope="row">', _('Remarks'),"</th><td>", $company->remarks, "</td></tr>\n";
|
|
echo "</table>\n";
|
|
|
|
form_view_buttons($g_scriptname, $id);
|
|
|
|
// show referenced equipment
|
|
$sql = "SELECT eid, ename FROM equipment WHERE supplier=:id OR manufacturer=:id";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([':id' => $id]);
|
|
if ($sth->rowCount() > 0) {
|
|
echo "<p>Referenced in equipment:</p>\n";
|
|
echo "<ul>\n";
|
|
foreach ($sth->fetchAll() as $row) {
|
|
echo "<li>", $row['ename'], ' ';
|
|
echo '<a title="', _('View'), '" href="equipment.php?f=view&id=', $row['eid'], '"><i class="bi-eye"></i></a>';
|
|
echo "</li>\n";
|
|
}
|
|
echo "</ul>\n";
|
|
} else {
|
|
echo '<p>', _('Not referenced in any equipment'), "<p>\n";
|
|
}
|
|
|
|
// show referenced invoices
|
|
$sql = "SELECT docid, refid, filename, title "
|
|
. "FROM docref AS r JOIN document AS d USING (docid) "
|
|
. "WHERE r.reftype='company' AND r.refid=? AND d.doctype='invoice'";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([$id]);
|
|
if ($sth->rowCount() > 0) {
|
|
echo "<p>", _('Invoices'), ":</p>\n";
|
|
echo "<ul>\n";
|
|
foreach ($sth->fetchAll() as $row) {
|
|
echo "<li>";
|
|
echo '<a href="dl.php?id=', $row['docid'], '">', $row['filename'], "</a>\n";
|
|
echo ' ', $row['title'];
|
|
echo "</li>\n";
|
|
}
|
|
echo "</ul>\n";
|
|
} else {
|
|
echo '<p>', _('No invoices'), "<p>\n";
|
|
}
|
|
|
|
elseif ($action == ACT_EDIT):
|
|
// ========== VARIANT: edit single record =====================================
|
|
|
|
echo '<h2>', _('Edit Company'), "</h2>\n";
|
|
|
|
$sql = "SELECT compname, comptype, comptype2, street, zip, city, country,"
|
|
. " contact, phone, email, web, customerno, contractno, remarks "
|
|
. "FROM company "
|
|
. "WHERE compid=?";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([$id]);
|
|
$company = $sth->fetch(PDO::FETCH_OBJ);
|
|
|
|
?>
|
|
<form method="post" action="<?=$g_scriptname?>">
|
|
<input type="hidden" name="id" value="<?=$id?>">
|
|
<div class="mb-3">
|
|
<label for="compname" class="form-label"><?=_('Name')?></label>
|
|
<input type="text" class="form-control" id="compname" name="compname" value="<?=$company->compname ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="shortname" class="form-label"><?=_('Short name')?></label>
|
|
<input type="text" class="form-control" id="shortname" name="shortname" value="<?=$company->shortname ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="comptype" class="form-label"><?=_('Company type')?></label>
|
|
<select name="comptype" class="form-control">
|
|
<?php
|
|
foreach ($opt_comptype as $k => $v) {
|
|
echo '<option value="', $k ,'"';
|
|
if ($company->comptype == $k) {
|
|
echo ' selected';
|
|
}
|
|
echo '>', $v, "</option>\n";
|
|
}
|
|
?>
|
|
</select>
|
|
</div>
|
|
<?php
|
|
form_create_select('comptype2', _('Secondary company type'), $g_opt_none + $opt_comptype, $company->comptype2);
|
|
?>
|
|
<div class="mb-3">
|
|
<label for="street" class="form-label"><?=_('Street')?></label>
|
|
<input type="text" class="form-control" id="street" name="street" value="<?=$company->street ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="zip" class="form-label"><?=_('Zip')?></label>
|
|
<input type="text" class="form-control" id="zip" name="zip" value="<?=$company->zip ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="city" class="form-label"><?=_('City')?></label>
|
|
<input type="text" class="form-control" id="city" name="city" value="<?=$company->city ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="country" class="form-label"><?=_('Country')?></label>
|
|
<input type="text" class="form-control" id="country" name="country" value="<?=$company->country ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="contact" class="form-label"><?=_('Contact')?></label>
|
|
<input type="text" class="form-control" id="contact" name="contact" value="<?=$company->contact ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="phone" class="form-label"><?=_('Phone')?></label>
|
|
<input type="text" class="form-control" id="phone" name="phone" value="<?=$company->phone ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label"><?=_('Email')?></label>
|
|
<input type="text" class="form-control" id="email" name="email" value="<?=$company->email ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="web" class="form-label"><?=_('Web')?></label>
|
|
<input type="text" class="form-control" id="web" name="web" value="<?=$company->web ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="customerno" class="form-label"><?=_('Customer no.')?></label>
|
|
<input type="text" class="form-control" id="customerno" name="customerno" value="<?=$company->customerno ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="contractno" class="form-label"><?=_('Contract no.')?></label>
|
|
<input type="text" class="form-control" id="contractno" name="contractno" value="<?=$company->contractno ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
|
|
<textarea class="form-control" id="remarks" name="remarks" rows="3"><?=$company->remarks ?></textarea>
|
|
</div>
|
|
<button type="submit" name="submit[update]" class="btn btn-primary"><?=_('Save')?></button>
|
|
<a href="<?=$g_scriptname?>?f=view&id=<?=$id?>" class="btn btn-secondary"><?=_('Back')?></a>
|
|
</form>
|
|
<?php
|
|
|
|
elseif ($action == ACT_DELETE):
|
|
// ========== VARIANT: delete record ==========================================
|
|
|
|
$sql = "SELECT compname, remarks FROM company WHERE compid=?";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([$id]);
|
|
$company = $sth->fetch(PDO::FETCH_OBJ);
|
|
|
|
echo '<h2>', _('Delete Company'), "</h2>\n";
|
|
echo '<p>', sprintf(_('Record no. %d'), $id), "</p>\n";
|
|
echo '<p>Name: ', $company->compname, "</p>";
|
|
echo '<p>Remarks: ', $company->remarks, "</p>";
|
|
|
|
// Still used as a supplier or manufacturer for equipment?
|
|
$sql = "SELECT COUNT(*) FROM equipment WHERE supplier=:id OR manufacturer=:id";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->bindValue(':id', $id, PDO::PARAM_INT);
|
|
$sth->execute();
|
|
$usecount = $sth->fetchColumn();
|
|
|
|
if ($usecount > 0) {
|
|
echo '<p class="alert alert-warning">', _('Company can not be deleted as there are equipment-references!'), "<p>\n";
|
|
echo '<a href="', $g_scriptname, '?f=view&id=', $id, '" class="btn btn-secondary">', _('Back'), "</a>\n";
|
|
|
|
} else {
|
|
echo '<p>', _('Deleting a company is final. There is no way back. Only delete if you are absolute sure.'), "</p>\n";
|
|
$_SESSION['token'] = bin2hex(random_bytes(8));
|
|
form_delete_buttons($g_scriptname, $id, $_SESSION['token']);
|
|
}
|
|
|
|
else:
|
|
// ========== ERROR UNKNOWN VARIANT ===========================================
|
|
|
|
echo '<p>', _('Unknown function call: Please report to system development!'), "</p>\n";
|
|
|
|
endif; // $action == ...
|
|
// ========== END OF VARIANTS =================================================
|
|
|
|
include 'footer.php';
|