Files
breadcrumb-the-shire/core/Repository/Org/DepartmentRepository.php

304 lines
12 KiB
PHP
Raw Normal View History

2026-02-04 23:31:53 +01:00
<?php
2026-02-11 19:28:12 +01:00
namespace MintyPHP\Repository\Org;
2026-02-04 23:31:53 +01:00
use MintyPHP\DB;
2026-02-11 19:28:12 +01:00
use MintyPHP\Repository\Support\RepoQuery;
use MintyPHP\Repository\Support\RepositoryArrayHelper;
2026-02-04 23:31:53 +01:00
/** Reads and writes department records with cost center, code tracking, and pagination. */
2026-03-05 08:26:51 +01:00
class DepartmentRepository implements DepartmentRepositoryInterface
2026-02-04 23:31:53 +01:00
{
2026-02-23 12:58:19 +01:00
public function list(): array
2026-02-04 23:31:53 +01:00
{
$rows = DB::select(
'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments order by id desc'
2026-02-04 23:31:53 +01:00
);
return RepositoryArrayHelper::unwrapList($rows, 'departments');
2026-02-04 23:31:53 +01:00
}
2026-02-23 12:58:19 +01:00
public function listIds(): array
2026-02-04 23:31:53 +01:00
{
$rows = DB::select('select id from departments');
return RepositoryArrayHelper::extractIds($rows, 'departments');
2026-02-04 23:31:53 +01:00
}
2026-02-23 12:58:19 +01:00
public function listActiveIdsByTenantIds(array $tenantIds): array
{
$tenantIds = RepositoryArrayHelper::sanitizePositiveIds($tenantIds);
if (!$tenantIds) {
return [];
}
$placeholders = implode(',', array_fill(0, count($tenantIds), '?'));
$rows = DB::select(
'select id from departments where active = 1 and tenant_id in (' . $placeholders . ')',
...array_map('strval', $tenantIds)
);
return RepositoryArrayHelper::extractIds($rows, 'departments');
}
2026-02-23 12:58:19 +01:00
public function listByTenantIds(array $tenantIds): array
2026-02-04 23:31:53 +01:00
{
$tenantIds = RepositoryArrayHelper::sanitizePositiveIds($tenantIds);
2026-02-04 23:31:53 +01:00
if (!$tenantIds) {
return [];
}
$placeholders = implode(',', array_fill(0, count($tenantIds), '?'));
$rows = DB::select(
'select departments.id, departments.uuid, departments.description, departments.tenant_id, departments.code, departments.cost_center, departments.active, departments.created_by, departments.modified_by, departments.created, departments.modified ' .
"from departments departments join tenants t on t.id = departments.tenant_id and t.status = 'active' " .
'where departments.active = 1 and departments.tenant_id in (' . $placeholders . ') ' .
2026-02-04 23:31:53 +01:00
'order by departments.description asc',
...array_map('strval', $tenantIds)
);
return RepositoryArrayHelper::unwrapList($rows, 'departments');
2026-02-04 23:31:53 +01:00
}
2026-02-23 12:58:19 +01:00
public function listByIds(array $departmentIds, bool $includeInactive = false): array
2026-02-04 23:31:53 +01:00
{
$departmentIds = RepositoryArrayHelper::sanitizePositiveIds($departmentIds);
2026-02-04 23:31:53 +01:00
if (!$departmentIds) {
return [];
}
$placeholders = implode(',', array_fill(0, count($departmentIds), '?'));
$activeSql = $includeInactive ? '' : 'active = 1 and ';
2026-02-04 23:31:53 +01:00
$rows = DB::select(
'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments where ' . $activeSql . 'id in (' . $placeholders . ') order by description asc',
2026-02-04 23:31:53 +01:00
...array_map('strval', $departmentIds)
);
return RepositoryArrayHelper::unwrapList($rows, 'departments');
2026-02-04 23:31:53 +01:00
}
2026-02-23 12:58:19 +01:00
public function listPaged(array $options): array
2026-02-04 23:31:53 +01:00
{
$search = trim((string) ($options['search'] ?? ''));
$tenant = trim((string) ($options['tenant'] ?? ''));
2026-02-11 19:28:12 +01:00
$allowedOrder = ['id', 'uuid', 'description', 'code', 'cost_center', 'active', 'created', 'modified'];
[$limit, $offset] = RepoQuery::sanitizeLimitOffset($options);
[$order, $dir] = RepoQuery::sanitizeOrder($options, $allowedOrder);
2026-02-04 23:31:53 +01:00
$where = [];
$params = [];
2026-02-11 19:28:12 +01:00
RepoQuery::addLikeFilter(
$where,
$params,
['description', 'uuid', 'code', 'cost_center'],
$search
);
$activeValue = array_key_exists('active', $options) ? $options['active'] : null;
RepoQuery::addEnumFilter($where, $params, $activeValue, [
['aliases' => ['1', 'true', 'active'], 'sql' => 'departments.active = 1'],
['aliases' => ['0', 'false', 'inactive'], 'sql' => 'departments.active = 0'],
]);
RepoQuery::addEqualsFilter(
$where,
$params,
$tenant,
"exists (select 1 from tenants t where t.id = departments.tenant_id and t.status = 'active' and t.uuid = ?)"
2026-02-11 19:28:12 +01:00
);
2026-02-04 23:31:53 +01:00
if (!empty($options['tenantUserId'])) {
$tenantUserId = (int) $options['tenantUserId'];
if ($tenantUserId > 0) {
$where[] = 'exists (select 1 from user_tenants ut ' .
"join tenants t on t.id = ut.tenant_id and t.status = 'active' " .
'where ut.user_id = ? and ut.tenant_id = departments.tenant_id)';
$params[] = (string) $tenantUserId;
2026-02-04 23:31:53 +01:00
}
} elseif (array_key_exists('tenantIds', $options)) {
$tenantIds = $options['tenantIds'];
$tenantIds = toIntIds($tenantIds);
2026-02-04 23:31:53 +01:00
if ($tenantIds) {
$where[] = 'departments.tenant_id in (???)';
$params[] = $tenantIds;
2026-02-04 23:31:53 +01:00
} else {
$where[] = '1=0';
2026-02-04 23:31:53 +01:00
}
}
$whereSql = $where ? (' where ' . implode(' and ', $where)) : '';
$count = DB::selectValue('select count(*) from departments' . $whereSql, ...$params);
$total = $count ? (int) $count : 0;
$query = 'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments' .
2026-02-04 23:31:53 +01:00
$whereSql .
sprintf(' order by `%s` %s limit ? offset ?', $order, $dir);
$queryParams = array_merge($params, [(string) $limit, (string) $offset]);
$rows = call_user_func_array(['MintyPHP\\DB', 'select'], array_merge([$query], $queryParams));
$list = RepositoryArrayHelper::unwrapList($rows, 'departments');
2026-02-04 23:31:53 +01:00
$ids = [];
foreach ($list as $department) {
if (isset($department['id'])) {
$ids[] = (int) $department['id'];
}
}
$tenantLabelsByDepartment = [];
if ($ids) {
$placeholders = implode(',', array_fill(0, count($ids), '?'));
$labelRows = DB::select(
'select d.id as department_id, t.description as description from departments d join tenants t on t.id = d.tenant_id ' .
'where d.id in (' . $placeholders . ') order by t.description asc',
2026-02-04 23:31:53 +01:00
...array_map('strval', $ids)
);
foreach ((array) $labelRows as $row) {
$departmentId = 0;
$label = '';
foreach ((array) $row as $table) {
if (!is_array($table)) {
continue;
2026-02-04 23:31:53 +01:00
}
if ($departmentId === 0 && isset($table['department_id'])) {
$departmentId = (int) $table['department_id'];
2026-02-04 23:31:53 +01:00
}
if ($label === '' && isset($table['description'])) {
$label = (string) $table['description'];
2026-02-04 23:31:53 +01:00
}
}
if ($departmentId > 0 && $label !== '') {
$tenantLabelsByDepartment[$departmentId] = [$label];
}
}
2026-02-04 23:31:53 +01:00
}
foreach ($list as &$department) {
$departmentId = (int) ($department['id'] ?? 0);
$department['tenant_labels'] = $tenantLabelsByDepartment[$departmentId] ?? [];
}
unset($department);
return [
'total' => $total,
'rows' => $list,
];
}
2026-02-23 12:58:19 +01:00
public function find(int $id): ?array
2026-02-04 23:31:53 +01:00
{
$row = DB::selectOne(
'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments where id = ? limit 1',
2026-02-04 23:31:53 +01:00
(string) $id
);
return RepositoryArrayHelper::unwrap($row, 'departments');
2026-02-04 23:31:53 +01:00
}
2026-02-23 12:58:19 +01:00
public function findByUuid(string $uuid): ?array
2026-02-04 23:31:53 +01:00
{
$row = DB::selectOne(
'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments where uuid = ? limit 1',
2026-02-04 23:31:53 +01:00
$uuid
);
return RepositoryArrayHelper::unwrap($row, 'departments');
2026-02-04 23:31:53 +01:00
}
2026-02-23 12:58:19 +01:00
public function existsByCode(string $code, int $excludeId = 0): bool
2026-02-11 19:28:12 +01:00
{
$code = trim($code);
if ($code === '') {
return false;
}
if ($excludeId > 0) {
$count = DB::selectValue('select count(*) from departments where code = ? and id != ?', $code, (string) $excludeId);
} else {
$count = DB::selectValue('select count(*) from departments where code = ?', $code);
}
return (int) $count > 0;
}
2026-02-23 12:58:19 +01:00
public function existsByTenantAndDescription(int $tenantId, string $description, int $excludeId = 0): bool
2026-02-04 23:31:53 +01:00
{
$tenantId = (int) $tenantId;
$description = trim($description);
if ($tenantId <= 0 || $description === '') {
return false;
}
if ($excludeId > 0) {
$count = DB::selectValue(
'select count(*) from departments where tenant_id = ? and lower(description) = lower(?) and id != ?',
(string) $tenantId,
$description,
(string) $excludeId
);
} else {
$count = DB::selectValue(
'select count(*) from departments where tenant_id = ? and lower(description) = lower(?)',
(string) $tenantId,
$description
);
}
return (int) $count > 0;
}
2026-02-23 12:58:19 +01:00
public function countByTenantId(int $tenantId): int
{
if ($tenantId <= 0) {
return 0;
}
$count = DB::selectValue('select count(*) from departments where tenant_id = ?', (string) $tenantId);
return $count ? (int) $count : 0;
2026-02-04 23:31:53 +01:00
}
public function create(array $data): int|false
2026-02-04 23:31:53 +01:00
{
return DB::insert(
'insert into departments (uuid, description, tenant_id, code, cost_center, active, created_by, created) values (?,?,?,?,?,?,?,NOW())',
$data['uuid'] ?? RepoQuery::uuidV4(),
2026-02-04 23:31:53 +01:00
$data['description'],
(string) ($data['tenant_id'] ?? 0),
2026-02-11 19:28:12 +01:00
$data['code'] ?? null,
$data['cost_center'] ?? null,
$data['active'] ?? 1,
2026-02-04 23:31:53 +01:00
$data['created_by'] ?? null
);
}
2026-02-23 12:58:19 +01:00
public function update(int $id, array $data): bool
2026-02-04 23:31:53 +01:00
{
$fields = [
'description' => $data['description'],
'tenant_id' => (string) ($data['tenant_id'] ?? 0),
2026-02-11 19:28:12 +01:00
'code' => $data['code'] ?? null,
'cost_center' => $data['cost_center'] ?? null,
'active' => $data['active'] ?? 1,
2026-02-04 23:31:53 +01:00
];
if (array_key_exists('modified_by', $data)) {
$fields['modified_by'] = $data['modified_by'];
}
$setParts = [];
$params = [];
foreach ($fields as $field => $value) {
$setParts[] = sprintf('`%s` = ?', $field);
$params[] = $value;
}
$params[] = (string) $id;
$query = 'update departments set ' . implode(', ', $setParts) . ' where id = ?';
$result = DB::update($query, ...$params);
return $result !== false;
}
2026-02-23 12:58:19 +01:00
public function setTenant(int $id, int $tenantId): bool
{
if ($id <= 0 || $tenantId <= 0) {
return false;
}
$result = DB::update(
'update departments set tenant_id = ? where id = ?',
(string) $tenantId,
(string) $id
);
return $result !== false;
}
2026-02-23 12:58:19 +01:00
public function delete(int $id): bool
2026-02-04 23:31:53 +01:00
{
$result = DB::delete('delete from departments where id = ?', (string) $id);
return $result !== false;
}
}