Add reusable routes for department self-serve machine types, n8n workflows, and executions, with corresponding unit tests and service integration.
This commit is contained in:
+263
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
|
||||
app_require('classes/economic_v2_versioning_service.php');
|
||||
app_require('classes/economic_v2_distribution_service.php');
|
||||
|
||||
use classes\economic_v2_distribution_service;
|
||||
use classes\economic_v2_versioning_service;
|
||||
|
||||
if (!class_exists('FakeEconomicV2DistributionVersioningService')) {
|
||||
class FakeEconomicV2DistributionVersioningService extends economic_v2_versioning_service
|
||||
{
|
||||
public ?array $fixedVersion = null;
|
||||
public array $subscriptionVersions = [];
|
||||
public int $backfillCalls = 0;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function resolveFixedPricingVersionAt(int $customer_number, string $timestamp): ?array
|
||||
{
|
||||
return $this->fixedVersion;
|
||||
}
|
||||
|
||||
public function resolveVehicleSubscriptionVersionsAt(int $customer_number, string $timestamp): array
|
||||
{
|
||||
return $this->subscriptionVersions;
|
||||
}
|
||||
|
||||
public function resolveDiscountOverrideAt(int $customer_number, bool $is_category, string|int $object_id, string $timestamp): ?array
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function runBestEffortBackfill(): array
|
||||
{
|
||||
$this->backfillCalls++;
|
||||
return [
|
||||
'fixed_pricing' => ['inserted' => 0, 'updated' => 0, 'closed' => 0, 'noop' => 0],
|
||||
'vehicle_subscriptions' => ['inserted' => 0, 'updated' => 0, 'closed' => 0, 'noop' => 0],
|
||||
'discount_overrides' => ['inserted' => 0, 'updated' => 0, 'closed' => 0, 'noop' => 0],
|
||||
'inferred' => ['fixed_pricing' => 0, 'vehicle_subscriptions' => 0],
|
||||
'warnings' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!class_exists('TestableEconomicV2DistributionService')) {
|
||||
class TestableEconomicV2DistributionService extends economic_v2_distribution_service
|
||||
{
|
||||
public array $stubOrders = [];
|
||||
public array $stubOrderItems = [];
|
||||
public array $stubVersionRows = [];
|
||||
public array $subscriptionPrices = [];
|
||||
public array $versionTableState = [];
|
||||
|
||||
public function __construct(?economic_v2_versioning_service $versioning = null)
|
||||
{
|
||||
parent::__construct($versioning);
|
||||
}
|
||||
|
||||
protected function fetchOrdersInRange(string $from_ts, string $to_ts): array
|
||||
{
|
||||
return $this->stubOrders;
|
||||
}
|
||||
|
||||
protected function fetchOrderItemsByOrderIds(array $order_ids): array
|
||||
{
|
||||
return $this->stubOrderItems;
|
||||
}
|
||||
|
||||
protected function fetchVehicleSubscriptionVersionRows(string $from_ts, string $to_ts): array
|
||||
{
|
||||
return $this->stubVersionRows;
|
||||
}
|
||||
|
||||
protected function calculateOrderOriginalPrice(array $order_items, int $customer_number, int $department_id, string $timestamp): float
|
||||
{
|
||||
$total = 0.0;
|
||||
foreach ($order_items as $item) {
|
||||
$total += (float)($item['price'] ?? 0) * (float)($item['quantity'] ?? 0);
|
||||
}
|
||||
return $total;
|
||||
}
|
||||
|
||||
protected function getSubscriptionMonthlyPrice(int $vehicle_type): float
|
||||
{
|
||||
return (float)($this->subscriptionPrices[$vehicle_type] ?? 0.0);
|
||||
}
|
||||
|
||||
protected function buildCustomerEnvelope(int $customer_number, array $transaction_map): array
|
||||
{
|
||||
return [
|
||||
'id' => $customer_number,
|
||||
'customer_number' => $customer_number,
|
||||
'customer_name' => 'Customer ' . $customer_number,
|
||||
'transactions' => array_values($transaction_map),
|
||||
'requires_action' => false,
|
||||
'meta' => [],
|
||||
];
|
||||
}
|
||||
|
||||
protected function buildTransactionObject(int $order_id, string $created_at, int $department_id, ?float $amount = null): array
|
||||
{
|
||||
return [
|
||||
'id' => $order_id,
|
||||
'date' => $created_at,
|
||||
'amount' => round((float)($amount ?? 0.0), 5),
|
||||
'booked' => true,
|
||||
'department_id' => $department_id,
|
||||
'excluded' => !$this->isDepartmentEligible($department_id),
|
||||
];
|
||||
}
|
||||
|
||||
protected function isDepartmentEligible(int $department_id): bool
|
||||
{
|
||||
return $department_id > 0 && $department_id !== 10;
|
||||
}
|
||||
|
||||
protected function parseDepartmentMap(array $department_map): array
|
||||
{
|
||||
$parsed = [];
|
||||
foreach ($department_map as $department_id => $amount) {
|
||||
$parsed['Department ' . $department_id] = round((float)$amount, 5);
|
||||
}
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
protected function versionTableHasRows(string $table): bool
|
||||
{
|
||||
if (array_key_exists($table, $this->versionTableState)) {
|
||||
return (bool)$this->versionTableState[$table];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
it('runs best-effort backfill when fixed pricing version history is empty', function (): void {
|
||||
$versioning = new FakeEconomicV2DistributionVersioningService();
|
||||
$versioning->fixedVersion = [
|
||||
'id' => 91,
|
||||
'price' => 499.95,
|
||||
'description' => 'Fixed pricing system order',
|
||||
'source' => 'backfill.inferred_fixed_pricing_order',
|
||||
'confidence' => 0.55,
|
||||
'inferred' => true,
|
||||
'effective_from' => '2026-01-01 00:00:00',
|
||||
'effective_to' => null,
|
||||
];
|
||||
|
||||
$service = new TestableEconomicV2DistributionService($versioning);
|
||||
$service->versionTableState = [
|
||||
'customer_fixed_pricing_versions' => false,
|
||||
];
|
||||
$service->stubOrders = [[
|
||||
'id' => 501,
|
||||
'customer_id' => 12345,
|
||||
'department_id' => 10,
|
||||
'created_at' => '2026-01-15 10:00:00',
|
||||
'reg_1' => '',
|
||||
'reference' => 'Fast pris aftale',
|
||||
]];
|
||||
$service->stubOrderItems = [
|
||||
501 => [[
|
||||
'product_id' => 61,
|
||||
'price' => 499.95,
|
||||
'quantity' => 1,
|
||||
'reference' => '',
|
||||
]],
|
||||
];
|
||||
|
||||
$result = $service->getFixedPricingDistribution('2026-01-01', '2026-01-31');
|
||||
|
||||
expect($versioning->backfillCalls)->toBe(1);
|
||||
expect($result['customers'])->toHaveCount(1);
|
||||
});
|
||||
|
||||
it('falls back to system orders for fixed pricing when regular orders yield no customers', function (): void {
|
||||
$versioning = new FakeEconomicV2DistributionVersioningService();
|
||||
$versioning->fixedVersion = [
|
||||
'id' => 91,
|
||||
'price' => 499.95,
|
||||
'description' => 'Fixed pricing system order',
|
||||
'source' => 'backfill.inferred_fixed_pricing_order',
|
||||
'confidence' => 0.55,
|
||||
'inferred' => true,
|
||||
'effective_from' => '2026-01-01 00:00:00',
|
||||
'effective_to' => null,
|
||||
];
|
||||
|
||||
$service = new TestableEconomicV2DistributionService($versioning);
|
||||
$service->stubOrders = [[
|
||||
'id' => 501,
|
||||
'customer_id' => 12345,
|
||||
'department_id' => 10,
|
||||
'created_at' => '2026-01-15 10:00:00',
|
||||
'reg_1' => '',
|
||||
'reference' => 'Fast pris aftale',
|
||||
]];
|
||||
$service->stubOrderItems = [
|
||||
501 => [[
|
||||
'product_id' => 61,
|
||||
'price' => 499.95,
|
||||
'quantity' => 1,
|
||||
'reference' => '',
|
||||
]],
|
||||
];
|
||||
|
||||
$result = $service->getFixedPricingDistribution('2026-01-01', '2026-01-31');
|
||||
|
||||
expect($result['customers'])->toHaveCount(1);
|
||||
expect($result['customers'][0]['customer_number'])->toBe(12345);
|
||||
expect($result['customers'][0]['meta']['fixed_pricing']['price'])->toBe(499.95);
|
||||
expect($result['customers'][0]['meta']['fixed_pricing']['version_groups'][0]['order_ids'])->toBe([501]);
|
||||
expect($result['collective_results']['total_fixed_price'])->toBe(499.95);
|
||||
expect($result['warnings'])->toContain('System order fallback used for fixed pricing (department 10).');
|
||||
});
|
||||
|
||||
it('falls back to system orders for wash subscriptions when regular orders yield no customers', function (): void {
|
||||
$versioning = new FakeEconomicV2DistributionVersioningService();
|
||||
$versioning->subscriptionVersions = [[
|
||||
'id' => 42,
|
||||
'reg' => 'AB12345',
|
||||
'vehicle_type' => 77,
|
||||
'source' => 'backfill.inferred_subscription_order',
|
||||
'confidence' => 0.5,
|
||||
'inferred' => true,
|
||||
]];
|
||||
|
||||
$service = new TestableEconomicV2DistributionService($versioning);
|
||||
$service->subscriptionPrices = [
|
||||
77 => 299.0,
|
||||
];
|
||||
$service->stubOrders = [[
|
||||
'id' => 601,
|
||||
'customer_id' => 12345,
|
||||
'department_id' => 10,
|
||||
'created_at' => '2026-01-15 10:00:00',
|
||||
'reg_1' => '',
|
||||
'reference' => 'Vaskeabonnementer',
|
||||
]];
|
||||
$service->stubOrderItems = [
|
||||
601 => [[
|
||||
'product_id' => 77,
|
||||
'price' => 299.0,
|
||||
'quantity' => 1,
|
||||
'reference' => 'AB12345',
|
||||
]],
|
||||
];
|
||||
|
||||
$result = $service->getWashSubscriptionsDistribution('2026-01-01', '2026-01-31');
|
||||
|
||||
expect($result['customers'])->toHaveCount(1);
|
||||
expect($result['customers'][0]['customer_number'])->toBe(12345);
|
||||
expect($result['customers'][0]['meta']['subscription']['subscription_total'])->toBe(299.0);
|
||||
expect($result['customers'][0]['meta']['subscription']['version_groups'][0]['reg'])->toBe('AB12345');
|
||||
expect($result['customers'][0]['meta']['subscription']['version_groups'][0]['department_distribution']['10'])->toBe(299.0);
|
||||
expect($result['collective_results']['total_subscription_price'])->toBe(299.0);
|
||||
expect($result['warnings'])->toContain('System order fallback used for wash subscriptions (department 10).');
|
||||
});
|
||||
Reference in New Issue
Block a user