Add manufacturer lot traceability
This commit is contained in:
1 parent
2aadce17bd
commit
19ce29c3d0
10 files changed
+418
-14
No files matched your search
@@ -103,3 +103,81 @@ function upsert_addresses(PDO $pdo, int $partyId, array $data): void
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
function get_manufacturer_options(PDO $pdo): array
|
||||
{
|
||||
$stmt = $pdo->query(
|
||||
"SELECT p.id, p.name, p.email, p.phone
|
||||
FROM party p
|
||||
INNER JOIN party_role pr ON pr.party_id = p.id AND pr.role_code = 'manufacturer'
|
||||
WHERE p.status = 'active'
|
||||
ORDER BY p.name ASC, p.id ASC"
|
||||
);
|
||||
|
||||
return array_map(
|
||||
static fn (array $row): array => [
|
||||
'id' => (int) $row['id'],
|
||||
'name' => (string) $row['name'],
|
||||
'email' => (string) ($row['email'] ?? ''),
|
||||
'phone' => (string) ($row['phone'] ?? ''),
|
||||
],
|
||||
$stmt->fetchAll()
|
||||
);
|
||||
}
|
||||
|
||||
function create_manufacturer(PDO $pdo, string $name, ?string $email = null, ?string $phone = null): array
|
||||
{
|
||||
$name = trim($name);
|
||||
$email = $email !== null ? trim($email) : null;
|
||||
$phone = $phone !== null ? trim($phone) : null;
|
||||
|
||||
if ($name === '') {
|
||||
throw new InvalidArgumentException('Herstellername fehlt');
|
||||
}
|
||||
|
||||
$existingStmt = $pdo->prepare(
|
||||
"SELECT p.id, p.name, p.email, p.phone
|
||||
FROM party p
|
||||
INNER JOIN party_role pr ON pr.party_id = p.id AND pr.role_code = 'manufacturer'
|
||||
WHERE LOWER(TRIM(p.name)) = LOWER(:name)
|
||||
LIMIT 1
|
||||
FOR UPDATE"
|
||||
);
|
||||
$existingStmt->execute([':name' => $name]);
|
||||
$existing = $existingStmt->fetch();
|
||||
if (is_array($existing)) {
|
||||
return [
|
||||
'id' => (int) $existing['id'],
|
||||
'name' => (string) $existing['name'],
|
||||
'email' => (string) ($existing['email'] ?? ''),
|
||||
'phone' => (string) ($existing['phone'] ?? ''),
|
||||
];
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare(
|
||||
"INSERT INTO party (type, name, email, phone, status, created_at, updated_at)
|
||||
VALUES ('supplier', :name, :email, :phone, 'active', NOW(), NOW())
|
||||
RETURNING id, name, email, phone"
|
||||
);
|
||||
$stmt->execute([
|
||||
':name' => $name,
|
||||
':email' => $email !== '' ? $email : null,
|
||||
':phone' => $phone !== '' ? $phone : null,
|
||||
]);
|
||||
$party = $stmt->fetch();
|
||||
if (!is_array($party)) {
|
||||
throw new RuntimeException('Hersteller konnte nicht angelegt werden');
|
||||
}
|
||||
|
||||
$roleStmt = $pdo->prepare(
|
||||
"INSERT INTO party_role (party_id, role_code) VALUES (:party_id, 'manufacturer')"
|
||||
);
|
||||
$roleStmt->execute([':party_id' => (int) $party['id']]);
|
||||
|
||||
return [
|
||||
'id' => (int) $party['id'],
|
||||
'name' => (string) $party['name'],
|
||||
'email' => (string) ($party['email'] ?? ''),
|
||||
'phone' => (string) ($party['phone'] ?? ''),
|
||||
];
|
||||
}
|
||||
Reference in new issue
Block a user