81 lines
3.0 KiB
PHP
81 lines
3.0 KiB
PHP
<?php
|
|
require __DIR__ . '/_bootstrap.php';
|
|
|
|
/**
|
|
* @OA\Post(
|
|
* path="/owner-save.php",
|
|
* tags={"Owners"},
|
|
* summary="Create or update owner",
|
|
* description="owner_id missing/0 → create; otherwise update own owner.",
|
|
* security={{"bearerAuth":{}}},
|
|
* @OA\RequestBody(required=true, @OA\JsonContent(
|
|
* required={"tax_code"},
|
|
* @OA\Property(property="owner_id", type="integer"),
|
|
* @OA\Property(property="owner_type", type="string", enum={"individual","company"}),
|
|
* @OA\Property(property="first_name", type="string"),
|
|
* @OA\Property(property="last_name", type="string"),
|
|
* @OA\Property(property="company_name", type="string"),
|
|
* @OA\Property(property="tax_code", type="string"),
|
|
* @OA\Property(property="email", type="string"),
|
|
* @OA\Property(property="phone", type="string"),
|
|
* @OA\Property(property="address", type="string"),
|
|
* @OA\Property(property="postal_code", type="string"),
|
|
* @OA\Property(property="city", type="string"),
|
|
* @OA\Property(property="province", type="string"),
|
|
* @OA\Property(property="country", type="integer"),
|
|
* @OA\Property(property="role", type="string")
|
|
* )),
|
|
* @OA\Response(response=200, description="OK", @OA\JsonContent(
|
|
* @OA\Property(property="data", ref="#/components/schemas/Owner"))),
|
|
* @OA\Response(response=403, ref="#/components/responses/Forbidden"),
|
|
* @OA\Response(response=422, ref="#/components/responses/ValidationError")
|
|
* )
|
|
*/
|
|
require_method('POST');
|
|
$user = require_auth($pdo);
|
|
$in = body();
|
|
|
|
$allowed = [
|
|
'owner_type', 'first_name', 'last_name', 'company_name', 'tax_code', 'email',
|
|
'phone', 'address', 'postal_code', 'city', 'province', 'country', 'role',
|
|
];
|
|
|
|
if (trim((string) ($in['tax_code'] ?? '')) === '') {
|
|
json_error(422, 'Validation failed', ['tax_code' => ['Required']]);
|
|
}
|
|
|
|
$data = [];
|
|
foreach ($allowed as $col) {
|
|
if (array_key_exists($col, $in)) {
|
|
$data[$col] = $in[$col] !== '' ? $in[$col] : null;
|
|
}
|
|
}
|
|
|
|
$ownerId = (int) ($in['owner_id'] ?? 0);
|
|
|
|
if ($ownerId > 0) {
|
|
if (!user_owns_owner($pdo, $user, $ownerId)) {
|
|
json_error(403, 'No access to this owner');
|
|
}
|
|
if ($data) {
|
|
$set = implode(', ', array_map(fn ($c) => "$c = ?", array_keys($data)));
|
|
$pdo->prepare("UPDATE property_owners SET $set WHERE owner_id = ? AND user_id = ?")
|
|
->execute([...array_values($data), $ownerId, $user['id']]);
|
|
}
|
|
} else {
|
|
$data['user_id'] = $user['id'];
|
|
// NOT NULL without a default in the legacy schema.
|
|
$data['owner_type'] ??= 'individual';
|
|
$data['email'] ??= '';
|
|
$cols = implode(', ', array_keys($data));
|
|
$ph = implode(', ', array_fill(0, count($data), '?'));
|
|
$pdo->prepare("INSERT INTO property_owners ($cols) VALUES ($ph)")
|
|
->execute(array_values($data));
|
|
$ownerId = (int) $pdo->lastInsertId();
|
|
}
|
|
|
|
$stmt = $pdo->prepare('SELECT * FROM property_owners WHERE owner_id = ? LIMIT 1');
|
|
$stmt->execute([$ownerId]);
|
|
|
|
json_data(present_owner($stmt->fetch()));
|