323 lines
14 KiB
PHP
323 lines
14 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Directories\EloquentOrderDirectory;
|
|
use App\Models\Customer;
|
|
use App\Models\Employee;
|
|
use App\Models\Order;
|
|
use App\Models\OrderLine;
|
|
use Carbon\CarbonImmutable;
|
|
use Domain\Enums\OrderStatus;
|
|
use Domain\Enums\ProductBrand;
|
|
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;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Money\Currency;
|
|
use Money\Money;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Shared\Pagination\PageNumber;
|
|
use Shared\Pagination\PageSize;
|
|
use Shared\Pagination\Pagination;
|
|
|
|
describe('EloquentOrderDirectory', function () {
|
|
it('returns empty paginator if customer has no employee matricule', function () {
|
|
// GIVEN
|
|
$directory = new EloquentOrderDirectory();
|
|
$customer = Customer::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'hashcode' => Uuid::uuid4()->toString(),
|
|
'email' => new Email('noemp@example.com'),
|
|
]);
|
|
|
|
$pagination = Pagination::ofPage(new PageNumber(1), new PageSize(10));
|
|
|
|
// WHEN
|
|
$result = $directory->getPaginatedOrdersOf($customer, $pagination);
|
|
|
|
// THEN
|
|
expect($result->total())->toBe(0);
|
|
});
|
|
|
|
it('returns paginated orders for customer with employee', function () {
|
|
// GIVEN
|
|
$directory = new EloquentOrderDirectory();
|
|
$customer = Customer::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'hashcode' => Uuid::uuid4()->toString(),
|
|
'email' => new Email('hasemp@example.com'),
|
|
]);
|
|
Employee::forceCreate([
|
|
'id' => 201,
|
|
'customer_id' => $customer->id,
|
|
'matricule' => new Matricule('1000201'),
|
|
'nom' => 'Doe',
|
|
'prenom' => 'John',
|
|
'code' => new Code('ABCDEF19'),
|
|
'etablissement' => 'ETAB',
|
|
'site' => 'SITE',
|
|
'service' => '101',
|
|
'created_at' => CarbonImmutable::now(),
|
|
'updated_at' => CarbonImmutable::now(),
|
|
]);
|
|
|
|
Order::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'reference' => new OrderReference(Uuid::uuid7()),
|
|
'status' => OrderStatus::VALIDATED->value,
|
|
'total' => new Money(1000, new Currency('EUR')),
|
|
'employee' => new OrderEmployee(new Matricule('1000201'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
|
|
'delivery_address' => new DeliveryAddress('Name', 'Addr', '00000', 'City'),
|
|
]);
|
|
|
|
Order::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'reference' => new OrderReference(Uuid::uuid7()),
|
|
'status' => OrderStatus::VALIDATED->value,
|
|
'total' => new Money(1000, new Currency('EUR')),
|
|
'employee' => new OrderEmployee(new Matricule('1000999'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
|
|
'delivery_address' => new DeliveryAddress('Name', 'Addr', '00000', 'City'),
|
|
]);
|
|
|
|
$pagination = Pagination::ofPage(new PageNumber(1), new PageSize(10));
|
|
|
|
// WHEN
|
|
$result = $directory->getPaginatedOrdersOf($customer, $pagination);
|
|
|
|
// THEN
|
|
expect($result->total())->toBe(1);
|
|
});
|
|
|
|
it('finds an order by uuid', function () {
|
|
// GIVEN
|
|
$directory = new EloquentOrderDirectory();
|
|
$order = Order::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'reference' => new OrderReference(Uuid::uuid7()),
|
|
'total' => new Money(1000, new Currency('EUR')),
|
|
'status' => OrderStatus::VALIDATED->value,
|
|
'employee' => new OrderEmployee(new Matricule('1000999'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
|
|
'delivery_address' => new DeliveryAddress('Name', 'Addr', '00000', 'City'),
|
|
]);
|
|
|
|
// WHEN
|
|
$result = $directory->find(Uuid::fromString($order->id));
|
|
|
|
// THEN
|
|
expect($result)->not->toBeNull();
|
|
expect($result->id)->toBe($order->id);
|
|
});
|
|
|
|
it('returns null if order not found', function () {
|
|
// GIVEN
|
|
$directory = new EloquentOrderDirectory();
|
|
|
|
// WHEN
|
|
$result = $directory->find(Uuid::uuid7());
|
|
|
|
// THEN
|
|
expect($result)->toBeNull();
|
|
});
|
|
|
|
it('finds order of customer', function () {
|
|
// GIVEN
|
|
$directory = new EloquentOrderDirectory();
|
|
$customer = Customer::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'hashcode' => Uuid::uuid4()->toString(),
|
|
'email' => new Email('findof@example.com'),
|
|
]);
|
|
Employee::forceCreate([
|
|
'id' => 202,
|
|
'customer_id' => $customer->id,
|
|
'matricule' => new Matricule('1000202'),
|
|
'nom' => 'Doe',
|
|
'prenom' => 'John',
|
|
'code' => new Code('ABCDEF22'),
|
|
'etablissement' => 'ETAB',
|
|
'site' => 'SITE',
|
|
'service' => '101',
|
|
'created_at' => CarbonImmutable::now(),
|
|
'updated_at' => CarbonImmutable::now(),
|
|
]);
|
|
|
|
$orderId = Uuid::uuid7()->toString();
|
|
Order::forceCreate([
|
|
'id' => $orderId,
|
|
'reference' => new OrderReference(Uuid::uuid7()),
|
|
'total' => new Money(1000, new Currency('EUR')),
|
|
'status' => OrderStatus::VALIDATED->value,
|
|
'employee' => new OrderEmployee(new Matricule('1000202'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
|
|
'delivery_address' => new DeliveryAddress('Name', 'Addr', '00000', 'City'),
|
|
]);
|
|
|
|
// WHEN
|
|
$result = $directory->findOf($customer, Uuid::fromString($orderId));
|
|
|
|
// THEN
|
|
expect($result)->not->toBeNull();
|
|
expect($result->id)->toBe($orderId);
|
|
});
|
|
|
|
it('gets validated orders in date range', function () {
|
|
// GIVEN
|
|
$directory = new EloquentOrderDirectory();
|
|
|
|
$pastDate = CarbonImmutable::now()->subDays(5);
|
|
Order::forceCreate([
|
|
'id' => Uuid::uuid7($pastDate)->toString(),
|
|
'reference' => new OrderReference(Uuid::uuid7()),
|
|
'total' => new Money(1000, new Currency('EUR')),
|
|
'status' => OrderStatus::VALIDATED->value,
|
|
'employee' => new OrderEmployee(new Matricule('1000999'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
|
|
'delivery_address' => new DeliveryAddress('Name', 'Addr', '00000', 'City'),
|
|
]);
|
|
Order::forceCreate([
|
|
'id' => Uuid::uuid7($pastDate)->toString(),
|
|
'reference' => new OrderReference(Uuid::uuid7()),
|
|
'total' => new Money(1000, new Currency('EUR')),
|
|
'status' => OrderStatus::WAITING->value,
|
|
'employee' => new OrderEmployee(new Matricule('1000999'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
|
|
'delivery_address' => new DeliveryAddress('Name', 'Addr', '00000', 'City'),
|
|
]);
|
|
|
|
// WHEN
|
|
$start = CarbonImmutable::now()->subDays(10);
|
|
$end = CarbonImmutable::now();
|
|
$results = $directory->getValidatedOrders($start, $end);
|
|
|
|
// THEN
|
|
expect($results->count())->toBeGreaterThanOrEqual(1);
|
|
});
|
|
|
|
it('gets orders by status in date range', function () {
|
|
// GIVEN
|
|
$directory = new EloquentOrderDirectory();
|
|
|
|
$pastDate = CarbonImmutable::now()->subDays(5);
|
|
Order::forceCreate([
|
|
'id' => Uuid::uuid7($pastDate)->toString(),
|
|
'reference' => new OrderReference(Uuid::uuid7()),
|
|
'total' => new Money(1000, new Currency('EUR')),
|
|
'status' => OrderStatus::CANCELED->value,
|
|
'employee' => new OrderEmployee(new Matricule('1000999'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
|
|
'delivery_address' => new DeliveryAddress('Name', 'Addr', '00000', 'City'),
|
|
]);
|
|
|
|
// WHEN
|
|
$start = CarbonImmutable::now()->subDays(10);
|
|
$end = CarbonImmutable::now();
|
|
$results = $directory->getOrdersByStatus(OrderStatus::CANCELED, $start, $end);
|
|
|
|
// THEN
|
|
expect($results)->toBeInstanceOf(Collection::class);
|
|
});
|
|
|
|
it('gets number of active clients in date range', function () {
|
|
// GIVEN
|
|
$directory = new EloquentOrderDirectory();
|
|
|
|
$customer = Customer::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'hashcode' => Uuid::uuid4()->toString(),
|
|
'email' => new Email('active@example.com'),
|
|
]);
|
|
Employee::forceCreate([
|
|
'id' => 203,
|
|
'customer_id' => $customer->id,
|
|
'matricule' => new Matricule('1000203'),
|
|
'nom' => 'Doe',
|
|
'prenom' => 'John',
|
|
'code' => new Code('ABCDEF21'),
|
|
'etablissement' => 'ETAB',
|
|
'site' => 'SITE',
|
|
'service' => '101',
|
|
'created_at' => CarbonImmutable::now(),
|
|
'updated_at' => CarbonImmutable::now(),
|
|
]);
|
|
|
|
$pastDate = CarbonImmutable::now()->subDays(5);
|
|
Order::forceCreate([
|
|
'id' => Uuid::uuid7($pastDate)->toString(),
|
|
'reference' => new OrderReference(Uuid::uuid7()),
|
|
'total' => new Money(1000, new Currency('EUR')),
|
|
'status' => OrderStatus::VALIDATED->value,
|
|
'employee' => new OrderEmployee(new Matricule('1000203'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
|
|
'delivery_address' => new DeliveryAddress('Name', 'Addr', '00000', 'City'),
|
|
]);
|
|
|
|
// WHEN
|
|
$start = CarbonImmutable::now()->subDays(10);
|
|
$end = CarbonImmutable::now();
|
|
$count = $directory->getNbActiveClients($start, $end);
|
|
|
|
// THEN
|
|
expect($count)->toBeGreaterThanOrEqual(1);
|
|
});
|
|
|
|
it('gets CA HT', function () {
|
|
// GIVEN
|
|
$directory = new EloquentOrderDirectory();
|
|
|
|
$pastDate = CarbonImmutable::now()->subDays(5);
|
|
$order = Order::forceCreate([
|
|
'id' => Uuid::uuid7($pastDate)->toString(),
|
|
'reference' => new OrderReference(Uuid::uuid7()),
|
|
'total' => new Money(1000, new Currency('EUR')),
|
|
'status' => OrderStatus::VALIDATED->value,
|
|
'employee' => new OrderEmployee(new Matricule('1000999'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
|
|
'delivery_address' => new DeliveryAddress('Name', 'Addr', '00000', 'City'),
|
|
]);
|
|
|
|
OrderLine::forceCreate([
|
|
'id' => 1,
|
|
'order_id' => $order->id,
|
|
'quantity' => 2,
|
|
'unit_price' => new Money(500, new Currency('EUR')),
|
|
'unit_tax' => new Money(100, new Currency('EUR')),
|
|
'unit_discount' => new Money(0, new Currency('EUR')),
|
|
'tax_rate' => TaxRate::TVA_20,
|
|
'total_ht' => new Money(1000, new Currency('EUR')),
|
|
'total_tax' => new Money(200, new Currency('EUR')),
|
|
'total_discount' => new Money(0, new Currency('EUR')),
|
|
'total_ttc' => new Money(1200, new Currency('EUR')),
|
|
'product' => new OrderLineProduct(
|
|
incoId: new IncoId(12345),
|
|
originalId: new ProductId(Uuid::uuid7()),
|
|
name: 'test product',
|
|
weight: new ProductWeight(1000, false),
|
|
brand: ProductBrand::none,
|
|
orderUnit: OrderUnit::PCE,
|
|
price: new Money(500, new Currency('EUR')),
|
|
discountedPrice: new Money(500, new Currency('EUR')),
|
|
priceKgTtc: new Money(600, new Currency('EUR')),
|
|
taxRate: TaxRate::TVA_20
|
|
),
|
|
]);
|
|
|
|
// WHEN
|
|
$start = CarbonImmutable::now()->subDays(10);
|
|
$end = CarbonImmutable::now();
|
|
$caHt = $directory->getCaHt($start, $end);
|
|
$caTtc = $directory->getCaTtc($start, $end);
|
|
$qty = $directory->getTotalQuantity($start, $end);
|
|
|
|
// THEN
|
|
expect($caHt)->toBeGreaterThanOrEqual(10.0);
|
|
expect($caTtc)->toBeGreaterThanOrEqual(12.0);
|
|
expect($qty)->toBeGreaterThanOrEqual(2);
|
|
});
|
|
});
|