2026-11-15 18:10:08 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 14:21:17 +01:00
|
|
|
namespace Tests\Integration\WithApp\App\Services;
|
|
|
|
|
|
2026-11-15 18:10:08 +01:00
|
|
|
use App\Models\Customer;
|
|
|
|
|
use App\Models\Employee;
|
|
|
|
|
use App\Models\Order;
|
|
|
|
|
use App\Models\OrderLine;
|
2026-11-16 20:19:31 +01:00
|
|
|
use App\Models\OrderTunnelAttempt;
|
2026-11-15 18:10:08 +01:00
|
|
|
use App\Services\StatsService;
|
2026-11-16 15:49:48 +01:00
|
|
|
use Carbon\Carbon;
|
2026-11-15 18:10:08 +01:00
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
|
use Domain\Enums\OrderStatus;
|
|
|
|
|
use Domain\Enums\ProductBrand;
|
|
|
|
|
use Domain\Enums\StatsCustomerType;
|
|
|
|
|
use Domain\Enums\StatsShopType;
|
|
|
|
|
use Domain\Enums\TaxRate;
|
|
|
|
|
use Domain\Enums\Units\OrderUnit;
|
|
|
|
|
use Domain\Models\ValueObjects\Code;
|
|
|
|
|
use Domain\Models\ValueObjects\DeliveryAddress;
|
|
|
|
|
use Domain\Models\ValueObjects\Email;
|
|
|
|
|
use Domain\Models\ValueObjects\Entities\ProductId;
|
|
|
|
|
use Domain\Models\ValueObjects\IncoId;
|
|
|
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
|
|
|
use Domain\Models\ValueObjects\OrderEmployee;
|
|
|
|
|
use Domain\Models\ValueObjects\OrderLineProduct;
|
|
|
|
|
use Domain\Models\ValueObjects\OrderReference;
|
|
|
|
|
use Domain\Models\ValueObjects\ProductWeight;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperServiceCode;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperSiteCode;
|
2026-11-16 15:49:48 +01:00
|
|
|
use Illuminate\Support\Facades\Date;
|
2026-11-15 18:10:08 +01:00
|
|
|
use Money\Currency;
|
|
|
|
|
use Money\Money;
|
2026-11-17 12:15:39 +01:00
|
|
|
use Shared\Helpers\UuidHelper;
|
2026-11-15 18:10:08 +01:00
|
|
|
|
|
|
|
|
covers(StatsService::class);
|
|
|
|
|
|
2026-11-16 15:49:48 +01:00
|
|
|
beforeEach(function (): void {
|
|
|
|
|
$now = CarbonImmutable::parse('2026-06-15 10:00:00', 'Europe/Paris');
|
|
|
|
|
Date::setTestNow($now);
|
|
|
|
|
CarbonImmutable::setTestNow($now);
|
|
|
|
|
Carbon::setTestNow($now);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(function (): void {
|
|
|
|
|
Date::setTestNow();
|
|
|
|
|
CarbonImmutable::setTestNow();
|
|
|
|
|
Carbon::setTestNow();
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-15 18:10:08 +01:00
|
|
|
describe('StatsService', function (): void {
|
|
|
|
|
$createEmployee = function (int $id, string $matricule, ?Customer $customer = null, ?CarbonImmutable $createdAt = null): Employee {
|
|
|
|
|
$timestamp = $createdAt ?? CarbonImmutable::now();
|
|
|
|
|
|
|
|
|
|
$codeSuffix = str_replace('0', '9', (string) $id);
|
|
|
|
|
|
2026-11-16 15:54:41 +01:00
|
|
|
return Employee::query()->forceCreate([
|
2026-11-15 18:10:08 +01:00
|
|
|
'id' => $id,
|
|
|
|
|
'customer_id' => $customer?->id,
|
|
|
|
|
'matricule' => new Matricule($matricule),
|
|
|
|
|
'code' => new Code('ABCDE' . $codeSuffix),
|
|
|
|
|
'nom' => 'Doe',
|
|
|
|
|
'prenom' => 'John',
|
|
|
|
|
'etablissement' => 'ETAB',
|
|
|
|
|
'site' => 'SITE',
|
|
|
|
|
'service' => '101',
|
|
|
|
|
'created_at' => $timestamp,
|
|
|
|
|
'updated_at' => $timestamp,
|
|
|
|
|
]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$createOrder = function (
|
|
|
|
|
OrderStatus $status,
|
|
|
|
|
string $matricule,
|
|
|
|
|
CarbonImmutable $createdAt,
|
|
|
|
|
?CarbonImmutable $pickupDay = null,
|
|
|
|
|
int $totalCents = 1000,
|
|
|
|
|
): Order {
|
2026-11-16 15:54:41 +01:00
|
|
|
return Order::query()->forceCreate([
|
2026-11-17 12:15:39 +01:00
|
|
|
'id' => UuidHelper::v7($createdAt)->toString(),
|
|
|
|
|
'reference' => new OrderReference(UuidHelper::v7()),
|
2026-11-15 18:10:08 +01:00
|
|
|
'status' => $status->value,
|
|
|
|
|
'total' => new Money($totalCents, new Currency('EUR')),
|
|
|
|
|
'pickup_day' => $pickupDay,
|
|
|
|
|
'employee' => new OrderEmployee(
|
|
|
|
|
new Matricule($matricule),
|
|
|
|
|
'John',
|
|
|
|
|
'Doe',
|
|
|
|
|
new ShopperEstablishmentCode('FAC01'),
|
|
|
|
|
new ShopperSiteCode('SITE'),
|
|
|
|
|
new ShopperServiceCode('101'),
|
|
|
|
|
),
|
|
|
|
|
'delivery_address' => new DeliveryAddress('Name', 'Addr', '00000', 'City'),
|
|
|
|
|
]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
it('calculates membership percentage and returns 0 when no active employees exist', function () use ($createEmployee): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$service = new StatsService(new Currency('EUR'));
|
|
|
|
|
expect($service->getMembershipPercentage())->toBe(0.0);
|
|
|
|
|
|
2026-11-16 15:54:41 +01:00
|
|
|
$customer = Customer::query()->forceCreate([
|
2026-11-17 12:15:39 +01:00
|
|
|
'id' => UuidHelper::v7()->toString(),
|
|
|
|
|
'hashcode' => UuidHelper::v4()->toString(),
|
2026-11-15 18:10:08 +01:00
|
|
|
'email' => new Email('member@example.com'),
|
|
|
|
|
]);
|
|
|
|
|
$createEmployee(101, '1000101', $customer);
|
|
|
|
|
$createEmployee(102, '1000102', null);
|
|
|
|
|
$createEmployee(103, '1000103', null);
|
|
|
|
|
$createEmployee(104, '1000104', null);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$percentage = $service->getMembershipPercentage();
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($percentage)->toBe(25.0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('calculates membership rate within period and previous period', function () use ($createEmployee): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$service = new StatsService(new Currency('EUR'));
|
|
|
|
|
$now = CarbonImmutable::now();
|
|
|
|
|
$inRange = $now->subDays(3);
|
|
|
|
|
$outRange = $now->subDays(20);
|
2026-11-16 15:54:41 +01:00
|
|
|
$customer1 = Customer::query()->forceCreate([
|
2026-11-17 12:15:39 +01:00
|
|
|
'id' => UuidHelper::v7()->toString(),
|
|
|
|
|
'hashcode' => UuidHelper::v4()->toString(),
|
2026-11-15 18:10:08 +01:00
|
|
|
'email' => new Email('range1@example.com'),
|
|
|
|
|
]);
|
|
|
|
|
$createEmployee(201, '1000201', $customer1, $inRange);
|
|
|
|
|
$createEmployee(202, '1000202', null, $inRange);
|
|
|
|
|
$createEmployee(203, '1000203', null, $outRange);
|
|
|
|
|
|
|
|
|
|
$start = $now->subDays(10);
|
|
|
|
|
$end = $now;
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$rate = $service->getMembershipRate($start, $end);
|
|
|
|
|
$previousPeriod = $service->getPreviousPeriod($start, $end);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($rate)->toBe(50.0)
|
|
|
|
|
->and($previousPeriod->getStartDate()->toDateString())->toBe($start->copy()->subDays(11)->toDateString())
|
|
|
|
|
->and($previousPeriod->getIncludedEndDate()->toDateString())->toBe($start->copy()->subDays(1)->toDateString());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('computes customer and shop stats over current and previous periods', function () use ($createEmployee, $createOrder): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$service = new StatsService(new Currency('EUR'));
|
|
|
|
|
$now = CarbonImmutable::now();
|
2026-11-16 15:54:41 +01:00
|
|
|
$customer = Customer::query()->forceCreate([
|
2026-11-17 12:15:39 +01:00
|
|
|
'id' => UuidHelper::v7()->toString(),
|
|
|
|
|
'hashcode' => UuidHelper::v4()->toString(),
|
2026-11-15 18:10:08 +01:00
|
|
|
'email' => new Email('stats@example.com'),
|
|
|
|
|
]);
|
|
|
|
|
$createEmployee(301, '1000301', $customer, $now->subDays(3));
|
|
|
|
|
$createEmployee(302, '1000302', null, $now->subDays(15));
|
|
|
|
|
|
|
|
|
|
$validatedOrder = $createOrder(
|
|
|
|
|
OrderStatus::VALIDATED,
|
|
|
|
|
'1000301',
|
|
|
|
|
$now->subDays(2),
|
|
|
|
|
$now->addDays(2),
|
|
|
|
|
totalCents: 1200,
|
|
|
|
|
);
|
|
|
|
|
$createOrder(
|
|
|
|
|
OrderStatus::CANCELED,
|
|
|
|
|
'1000301',
|
|
|
|
|
$now->subDays(2),
|
|
|
|
|
$now->addDays(10),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$productVo = new OrderLineProduct(
|
|
|
|
|
incoId: new IncoId(1),
|
2026-11-17 12:15:39 +01:00
|
|
|
originalId: new ProductId(UuidHelper::v7()),
|
2026-11-15 18:10:08 +01:00
|
|
|
name: 'Poulet',
|
|
|
|
|
weight: new ProductWeight(1000, false),
|
|
|
|
|
brand: ProductBrand::loue,
|
|
|
|
|
orderUnit: OrderUnit::PCE,
|
|
|
|
|
price: new Money(1055, new Currency('EUR')),
|
|
|
|
|
discountedPrice: new Money(1055, new Currency('EUR')),
|
|
|
|
|
priceKgTtc: new Money(1055, new Currency('EUR')),
|
|
|
|
|
taxRate: TaxRate::TVA_5_5,
|
|
|
|
|
);
|
2026-11-16 15:54:41 +01:00
|
|
|
OrderLine::query()->forceCreate([
|
2026-11-15 18:10:08 +01:00
|
|
|
'order_id' => $validatedOrder->id,
|
|
|
|
|
'product' => $productVo,
|
|
|
|
|
'quantity' => 2,
|
|
|
|
|
'unit_price' => new Money(1000, new Currency('EUR')),
|
|
|
|
|
'unit_tax' => new Money(55, new Currency('EUR')),
|
|
|
|
|
'unit_discount' => new Money(0, new Currency('EUR')),
|
|
|
|
|
'tax_rate' => TaxRate::TVA_5_5,
|
|
|
|
|
'total_ht' => new Money(2000, new Currency('EUR')),
|
|
|
|
|
'total_ttc' => new Money(2110, new Currency('EUR')),
|
|
|
|
|
'total_tax' => new Money(110, new Currency('EUR')),
|
|
|
|
|
'total_discount' => new Money(0, new Currency('EUR')),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$customerStats = $service->getStats(StatsCustomerType::cases(), $now->subDays(5), $now);
|
|
|
|
|
$shopStats = $service->getStats(StatsShopType::cases(), $now->subDays(5), $now);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($customerStats[StatsCustomerType::nbSalarie->value]['current'])->toBe(1)
|
|
|
|
|
->and($customerStats[StatsCustomerType::nbSalarieWithAccount->value]['current'])->toBe(1)
|
|
|
|
|
->and($customerStats[StatsCustomerType::membershipRate->value]['current'])->toBe(100.0)
|
|
|
|
|
->and($shopStats[StatsShopType::nbValidateCmd->value]['current'])->toBe(1)
|
|
|
|
|
->and($shopStats[StatsShopType::nbCanceledCmd->value]['current'])->toBe(1)
|
|
|
|
|
->and($shopStats[StatsShopType::nbWaitingCmd->value]['current'])->toBe(0)
|
|
|
|
|
->and($shopStats[StatsShopType::canceledRate->value]['current'])->toBe(50.0)
|
|
|
|
|
->and($shopStats[StatsShopType::nbActiveClients->value]['current'])->toBe(1)
|
|
|
|
|
->and($shopStats[StatsShopType::nbCmdWith7DayDelay->value]['current'])->toBe(1)
|
|
|
|
|
->and($shopStats[StatsShopType::nbCmdWith14DayDelay->value]['current'])->toBe(0)
|
|
|
|
|
->and($shopStats[StatsShopType::caHt->value]['current'])->toBe(20.0)
|
|
|
|
|
->and($shopStats[StatsShopType::caTtc->value]['current'])->toBe(21.1)
|
|
|
|
|
->and($shopStats[StatsShopType::averageBasketHt->value]['current'])->toBe(20.0)
|
|
|
|
|
->and($shopStats[StatsShopType::averageProductCountPerOrder->value]['current'])->toBe(2.0);
|
|
|
|
|
});
|
2026-11-16 20:19:31 +01:00
|
|
|
|
|
|
|
|
it('combines legacy WAITING orders and unconverted tunnel attempts in waiting stats', function () use ($createOrder): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$service = new StatsService(new Currency('EUR'));
|
|
|
|
|
$now = CarbonImmutable::now();
|
|
|
|
|
$customer = Customer::query()->forceCreate([
|
2026-11-17 12:15:39 +01:00
|
|
|
'id' => UuidHelper::v7()->toString(),
|
|
|
|
|
'hashcode' => UuidHelper::v4()->toString(),
|
2026-11-16 20:19:31 +01:00
|
|
|
'email' => new Email('attempts-stats@example.com'),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// 1 legacy WAITING order
|
|
|
|
|
$createOrder(OrderStatus::WAITING, '1000401', $now->subDays(2));
|
|
|
|
|
// 1 VALIDATED + 1 CANCELED business orders
|
|
|
|
|
$validatedOrder = $createOrder(OrderStatus::VALIDATED, '1000401', $now->subDays(2));
|
|
|
|
|
$createOrder(OrderStatus::CANCELED, '1000401', $now->subDays(2));
|
|
|
|
|
// Technical PENDING and FAILED orders must never pollute nbWaitingCmd or canceledRate
|
|
|
|
|
$createOrder(OrderStatus::PENDING, '1000401', $now->subDays(2));
|
|
|
|
|
$createOrder(OrderStatus::FAILED, '1000401', $now->subDays(2));
|
|
|
|
|
|
|
|
|
|
// 2 unconverted attempts in period
|
|
|
|
|
OrderTunnelAttempt::query()->forceCreate([
|
|
|
|
|
'customer_id' => $customer->id,
|
|
|
|
|
'order_id' => null,
|
|
|
|
|
'converted_at' => null,
|
|
|
|
|
'created_at' => $now->subDay()->utc(),
|
|
|
|
|
'updated_at' => $now->subDay()->utc(),
|
|
|
|
|
]);
|
|
|
|
|
OrderTunnelAttempt::query()->forceCreate([
|
|
|
|
|
'customer_id' => $customer->id,
|
|
|
|
|
'order_id' => null,
|
|
|
|
|
'converted_at' => null,
|
|
|
|
|
'created_at' => $now->subHours(2)->utc(),
|
|
|
|
|
'updated_at' => $now->subHours(2)->utc(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// 1 converted attempt (must not be counted as waiting)
|
|
|
|
|
OrderTunnelAttempt::query()->forceCreate([
|
|
|
|
|
'customer_id' => $customer->id,
|
|
|
|
|
'order_id' => $validatedOrder->id,
|
|
|
|
|
'converted_at' => $now->subDays(2)->utc(),
|
|
|
|
|
'created_at' => $now->subDays(2)->utc(),
|
|
|
|
|
'updated_at' => $now->subDays(2)->utc(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// 1 unconverted attempt outside the queried period (must not be counted)
|
|
|
|
|
OrderTunnelAttempt::query()->forceCreate([
|
|
|
|
|
'customer_id' => $customer->id,
|
|
|
|
|
'order_id' => null,
|
|
|
|
|
'converted_at' => null,
|
|
|
|
|
'created_at' => $now->subDays(12)->utc(),
|
|
|
|
|
'updated_at' => $now->subDays(12)->utc(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$shopStats = $service->getStats(StatsShopType::cases(), $now->subDays(5), $now);
|
|
|
|
|
|
|
|
|
|
// THEN: waiting = 1 legacy + 2 unconverted = 3 ; canceled = 1 ; validated = 1 => canceledRate = (4 / 5) * 100 = 80.0
|
|
|
|
|
expect($shopStats[StatsShopType::nbWaitingCmd->value]['current'])->toBe(3)
|
|
|
|
|
->and($shopStats[StatsShopType::nbValidateCmd->value]['current'])->toBe(1)
|
|
|
|
|
->and($shopStats[StatsShopType::nbCanceledCmd->value]['current'])->toBe(1)
|
|
|
|
|
->and($shopStats[StatsShopType::canceledRate->value]['current'])->toBe(80.0);
|
|
|
|
|
});
|
2026-11-17 12:39:18 +01:00
|
|
|
|
|
|
|
|
it('counts VALIDATED, CANCELED and WAITING customers as active while excluding PENDING, FAILED and unconverted attempts', function () use (
|
|
|
|
|
$createEmployee,
|
|
|
|
|
$createOrder,
|
|
|
|
|
): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$service = new StatsService(new Currency('EUR'));
|
|
|
|
|
$now = CarbonImmutable::now();
|
|
|
|
|
|
|
|
|
|
$createActiveCustomer = function (int $id, string $matricule, string $email) use ($createEmployee): Customer {
|
|
|
|
|
$customer = Customer::query()->forceCreate([
|
|
|
|
|
'id' => UuidHelper::v7()->toString(),
|
|
|
|
|
'hashcode' => UuidHelper::v4()->toString(),
|
|
|
|
|
'email' => new Email($email),
|
|
|
|
|
]);
|
|
|
|
|
$createEmployee($id, $matricule, $customer);
|
|
|
|
|
|
|
|
|
|
return $customer;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$createActiveCustomer(11, '1000501', 'validated@example.com');
|
|
|
|
|
$createActiveCustomer(12, '1000502', 'canceled@example.com');
|
|
|
|
|
$createActiveCustomer(13, '1000503', 'waiting@example.com');
|
|
|
|
|
$createActiveCustomer(14, '1000504', 'pending@example.com');
|
|
|
|
|
$createActiveCustomer(15, '1000505', 'failed@example.com');
|
|
|
|
|
$attemptOnlyCustomer = $createActiveCustomer(16, '1000506', 'attempt@example.com');
|
|
|
|
|
|
|
|
|
|
// 2 VALIDATED + 1 CANCELED + 1 legacy WAITING => 3 active customers
|
|
|
|
|
$createOrder(OrderStatus::VALIDATED, '1000501', $now->subDays(2));
|
|
|
|
|
$createOrder(OrderStatus::VALIDATED, '1000501', $now->subDay());
|
|
|
|
|
$createOrder(OrderStatus::CANCELED, '1000502', $now->subDays(2));
|
|
|
|
|
$createOrder(OrderStatus::WAITING, '1000503', $now->subDays(2));
|
|
|
|
|
|
|
|
|
|
// Technical PENDING / FAILED orders & unconverted tunnel attempts must not inflate active clients
|
|
|
|
|
$createOrder(OrderStatus::PENDING, '1000504', $now->subDays(2));
|
|
|
|
|
$createOrder(OrderStatus::FAILED, '1000505', $now->subDays(2));
|
|
|
|
|
OrderTunnelAttempt::query()->forceCreate([
|
|
|
|
|
'customer_id' => $attemptOnlyCustomer->id,
|
|
|
|
|
'order_id' => null,
|
|
|
|
|
'converted_at' => null,
|
|
|
|
|
'created_at' => $now->subDay()->utc(),
|
|
|
|
|
'updated_at' => $now->subDay()->utc(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$shopStats = $service->getStats(
|
|
|
|
|
[StatsShopType::nbActiveClients, StatsShopType::purchaseFrequency],
|
|
|
|
|
$now->subDays(5),
|
|
|
|
|
$now,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// THEN: 3 active clients (1000501, 1000502, 1000503), purchaseFrequency = round(2 / 3, 2) = 0.67
|
|
|
|
|
expect($shopStats[StatsShopType::nbActiveClients->value]['current'])->toBe(3)
|
|
|
|
|
->and($shopStats[StatsShopType::purchaseFrequency->value]['current'])->toBe(0.67);
|
|
|
|
|
});
|
2026-11-15 18:10:08 +01:00
|
|
|
});
|