## Summary Adds an optional `invoice_email` (Danish: *faktura email*) field to customers, so e-conomic can deliver invoices to a dedicated accounting mailbox instead of the customer's primary email. Linear: **TRU-77** (DRIFT 16) ## Changes - **Migration** (additive, via existing schema_bootstrap pattern) - New `customer_invoice_email_schema_bootstrap` adds the `invoice_email VARCHAR(255) NULL` column to `users` after `wash_certificate_email`. Idempotent — skips when the column already exists. - **Domain object — `objects/users_o.php`** - New `invoice_email` object property. - `getInvoiceEmail()` returns the dedicated address or falls back to the primary `email`. - `getInvoiceEmailOverride()` returns only the explicit override (no fallback). - `setInvoiceEmail($email)` validates and writes the value; `null`/empty clears it. - `add($customer_number, $password, $role, ?$invoice_email = null)` now accepts the optional field and persists it. - The user payload output now exposes `invoice_email` and `invoice_email_fallback`. - **API — `routes/usersRoute.php`** - `POST /users` accepts an optional `invoice_email`, validated before insert. - `PUT /users` accepts `invoice_email` (including null/empty to clear) on existing users. - **Customer mass import — `classes/customer_mass_import_service.php`** - Payload now accepts `invoice_email`. - `normalizeInvoiceEmail()` rejects malformed addresses before any e-conomic call. - `resolveInvoiceEmail()` / `resolveCreateEmail()` route the e-conomic customer email to the dedicated address when set, otherwise the primary `email` (with the existing `jb@truckwash.dk` fallback when neither is provided). - `syncLocalCustomer()` persists `invoice_email` on the local user. - `import()` result now includes the resolved `invoice_email`. - **Tests — `tests/Unit/Customers/CustomerInvoiceEmailTest.php` (new)** - Schema bootstrap adds the column when missing. - Schema bootstrap is a no-op when the column already exists. - Schema bootstrap skips when the `users` table is not present. - e-conomic customer email is set to `invoice_email` when provided. - e-conomic customer email falls back to `email` when `invoice_email` is omitted. - Invalid `invoice_email` is rejected before any e-conomic call. ## Backwards compatibility - The column is nullable; existing rows are unaffected. - The `add()` signature is additive (new optional parameter with default `null`). - The route payloads ignore `invoice_email` unless supplied, so no client change is required. ## Linear - TRU-77 (DRIFT 16: "Add 'faktura email' field to customer creation form") --------- Co-authored-by: Jeppe B <jeppe@copenhagentruckwash.io> Co-authored-by: OpenClaw Bugfix Bot <openclaw-bot@truckwash.dk> Co-authored-by: bugfix sub-agent <bugfix@openclaw.local>
94 lines
3.2 KiB
PHP
94 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace classes;
|
|
|
|
/**
|
|
* Ensures additive schema for the customer `invoice_email` field
|
|
* (TRU-77 / DRIFT 16). The field is optional and stores an
|
|
* e-mail address that should receive the customer's invoices
|
|
* separately from the customer's primary `email`.
|
|
*/
|
|
class customer_invoice_email_schema_bootstrap
|
|
{
|
|
private static bool $initialized = false;
|
|
private const TABLE = 'users';
|
|
private const COLUMN = 'invoice_email';
|
|
|
|
public static function ensureSchema(): void
|
|
{
|
|
if (self::$initialized) {
|
|
return;
|
|
}
|
|
|
|
global $db;
|
|
if (!isset($db) || !is_object($db) || !method_exists($db, 'query')) {
|
|
return;
|
|
}
|
|
|
|
self::ensureUsersTable($db);
|
|
self::ensureInvoiceEmailColumn($db);
|
|
|
|
self::$initialized = true;
|
|
}
|
|
|
|
private static function ensureUsersTable(object $db): void
|
|
{
|
|
$db->query(
|
|
"CREATE TABLE IF NOT EXISTS users (
|
|
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
customer_number INT NOT NULL,
|
|
display_name VARCHAR(255) NULL,
|
|
email VARCHAR(255) NULL,
|
|
phone_country_code INT NULL,
|
|
phone BIGINT NULL,
|
|
password VARCHAR(255) NULL,
|
|
group_id INT NOT NULL DEFAULT 0,
|
|
xlvask_customer_id VARCHAR(255) NULL,
|
|
sms_notifications_enabled TINYINT(1) NOT NULL DEFAULT 0,
|
|
email_notifications_enabled TINYINT(1) NOT NULL DEFAULT 0,
|
|
wash_certificate_email VARCHAR(255) NULL,
|
|
invoice_email VARCHAR(255) NULL,
|
|
two_factor_enabled TINYINT(1) NOT NULL DEFAULT 0,
|
|
two_factor_secret VARCHAR(255) NULL,
|
|
created_at DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
deleted_at DATETIME NULL,
|
|
KEY idx_users_customer_number (customer_number),
|
|
KEY idx_users_group_id (group_id)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"
|
|
);
|
|
}
|
|
|
|
private static function ensureInvoiceEmailColumn(object $db): void
|
|
{
|
|
if (!self::tableExists($db, self::TABLE)) {
|
|
return;
|
|
}
|
|
if (self::columnExists($db, self::TABLE, self::COLUMN)) {
|
|
return;
|
|
}
|
|
|
|
$safeTable = str_replace('`', '', self::TABLE);
|
|
$db->query(
|
|
"ALTER TABLE `{$safeTable}`
|
|
ADD COLUMN " . self::COLUMN . " VARCHAR(255) NULL
|
|
AFTER wash_certificate_email"
|
|
);
|
|
}
|
|
|
|
private static function tableExists(object $db, string $table): bool
|
|
{
|
|
$safeTable = str_replace('`', '', $table);
|
|
$result = $db->query("SHOW TABLES LIKE '{$safeTable}'");
|
|
return $result && (int)$result->num_rows > 0;
|
|
}
|
|
|
|
private static function columnExists(object $db, string $table, string $column): bool
|
|
{
|
|
$safeTable = str_replace('`', '', $table);
|
|
$safeColumn = str_replace("'", '', $column);
|
|
$result = $db->query("SHOW COLUMNS FROM `{$safeTable}` LIKE '{$safeColumn}'");
|
|
return $result && (int)$result->num_rows > 0;
|
|
}
|
|
}
|