187 lines
9.2 KiB
PHP
187 lines
9.2 KiB
PHP
<?php
|
||
|
||
use App\Modules\Sales\Application\DTOs\SaleData;
|
||
use App\Modules\Sales\Application\UseCases\CloseCashDay;
|
||
use App\Modules\Sales\Application\UseCases\CreditQuickSale;
|
||
use App\Modules\Sales\Application\UseCases\ReopenCashDay;
|
||
use App\Modules\Sales\Application\UseCases\SellQuickSale;
|
||
use App\Modules\Sales\Domain\Entities\CashDay;
|
||
use App\Modules\Sales\Domain\Entities\QuickSale;
|
||
use App\Modules\Sales\Domain\Enums\PaymentMethod;
|
||
use App\Modules\Sales\Domain\Exceptions\CashDayClosed;
|
||
use App\Modules\Sales\Domain\Exceptions\CashDayInFuture;
|
||
use App\Modules\Sales\Domain\Exceptions\CashDayNotClosed;
|
||
use App\Modules\Sales\Domain\Exceptions\CreditCannotBeCredited;
|
||
use App\Modules\Sales\Domain\Exceptions\InvalidQuantity;
|
||
use App\Modules\Sales\Domain\Exceptions\ProductNotSellable;
|
||
use App\Modules\Sales\Domain\Exceptions\QuickSaleAlreadyCredited;
|
||
use App\Modules\Sales\Domain\Exceptions\QuickSaleCustomerNotFound;
|
||
use App\Modules\Sales\Domain\Exceptions\QuickSaleEmpty;
|
||
use App\Modules\Sales\Domain\ValueObjects\CashDayBalance;
|
||
use App\Modules\Sales\Domain\ValueObjects\QuickSaleLine;
|
||
use App\Modules\Sales\Domain\ValueObjects\VatTotal;
|
||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
|
||
use Tests\Fakes\FakeClock;
|
||
use Tests\Fakes\FakeCustomerQueries;
|
||
use Tests\Fakes\FakeQuickSaleHours;
|
||
use Tests\Fakes\FakeQuickSaleProducts;
|
||
use Tests\Fakes\FakeReceiptMailer;
|
||
use Tests\Fakes\FakeTransactionManager;
|
||
use Tests\Fakes\InMemoryCashDayRepository;
|
||
use Tests\Fakes\InMemoryQuickSaleRepository;
|
||
|
||
beforeEach(function () {
|
||
$this->sales = new InMemoryQuickSaleRepository;
|
||
$this->cashDays = new InMemoryCashDayRepository;
|
||
$this->products = new FakeQuickSaleProducts;
|
||
$this->hours = new FakeQuickSaleHours;
|
||
$this->customers = new FakeCustomerQueries;
|
||
$this->mailer = new FakeReceiptMailer;
|
||
$this->clock = new FakeClock(new DateTimeImmutable('2026-11-24'));
|
||
$transactions = new FakeTransactionManager;
|
||
|
||
$this->sell = new SellQuickSale($this->sales, $this->cashDays, $this->products, $this->hours, $this->mailer, $this->customers, $transactions, $this->clock);
|
||
$this->credit = new CreditQuickSale($this->sales, $this->cashDays, $this->mailer, $transactions, $this->clock);
|
||
$this->close = new CloseCashDay($this->cashDays, $this->sales, $transactions, $this->clock);
|
||
$this->reopen = new ReopenCashDay($this->cashDays, $transactions, $this->clock);
|
||
|
||
$this->products->add(1, 'Passikuvat', 1500, durationQuarters: 1, workTypeId: 7);
|
||
$this->products->add(2, 'Digitointi', 2000, durationQuarters: 2, workTypeId: 7);
|
||
$this->products->add(3, 'Kirja', 1000, vatBasisPoints: 1000);
|
||
});
|
||
|
||
it('totals a sale and credits it with the lines negated and the same payment method', function () {
|
||
$sale = QuickSale::reconstitute(5, 1, null, PaymentMethod::Card, new DateTimeImmutable, new DateTimeImmutable('2026-11-20'), [
|
||
QuickSaleLine::sold(1, 'Passikuvat', 2, 1500, 2550),
|
||
QuickSaleLine::sold(3, 'Kirja', 1, 1000, 1000),
|
||
], null);
|
||
$credit = QuickSale::creditFor($sale, 9, new DateTimeImmutable, new DateTimeImmutable('2026-11-24'));
|
||
|
||
expect($sale->totalCents())->toBe(4000)
|
||
->and($credit->totalCents())->toBe(-4000)
|
||
->and($credit->paymentMethod())->toBe(PaymentMethod::Card)
|
||
->and($credit->creditsSaleId())->toBe(5)
|
||
->and($credit->sellerId())->toBe(9)
|
||
->and(array_map(fn (QuickSaleLine $line): int => $line->quantity, $credit->lines()))->toBe([-2, -1]);
|
||
|
||
QuickSale::creditFor(QuickSale::reconstitute(6, 1, null, PaymentMethod::Card, new DateTimeImmutable, new DateTimeImmutable, $credit->lines(), 5), 9, new DateTimeImmutable, new DateTimeImmutable);
|
||
})->throws(CreditCannotBeCredited::class);
|
||
|
||
it('rejects empty sales and quantities outside 1–999', function (callable $make) {
|
||
$make();
|
||
})->with([
|
||
'no lines' => [fn () => QuickSale::sell(1, null, PaymentMethod::Cash, [], new DateTimeImmutable, new DateTimeImmutable)],
|
||
'zero' => [fn () => QuickSaleLine::sold(1, 'A', 0, 100, 0)],
|
||
'too many' => [fn () => QuickSaleLine::sold(1, 'A', 1000, 100, 0)],
|
||
])->throws(DomainException::class);
|
||
|
||
it('takes the VAT out of each rate\'s total, credits included', function () {
|
||
$day = new DateTimeImmutable('2026-11-24');
|
||
$balance = CashDayBalance::of([
|
||
QuickSale::sell(1, null, PaymentMethod::Cash, [QuickSaleLine::sold(1, 'A', 3, 1500, 2550)], $day, $day),
|
||
QuickSale::sell(1, null, PaymentMethod::Card, [QuickSaleLine::sold(3, 'B', 1, 1000, 1000), QuickSaleLine::sold(1, 'A', 1, 1500, 2550)], $day, $day),
|
||
QuickSale::reconstitute(9, 1, null, PaymentMethod::Cash, $day, $day, [new QuickSaleLine(1, 'A', -1, 1500, 2550)], 1),
|
||
]);
|
||
|
||
expect($balance->saleCount)->toBe(3)
|
||
->and($balance->cashCents)->toBe(3000)
|
||
->and($balance->cardCents)->toBe(2500)
|
||
->and($balance->totalCents())->toBe(5500)
|
||
->and($balance->vatTotals)->toEqual([
|
||
new VatTotal(2550, 4500, 3586, 914),
|
||
new VatTotal(1000, 1000, 909, 91),
|
||
])
|
||
->and(VatTotal::ofGross(2550, -1500))->toEqual(new VatTotal(2550, -1500, -1195, -305));
|
||
});
|
||
|
||
it('sells at current prices on today and books the hours by work type', function () {
|
||
$result = $this->sell->execute(4, new SaleData([1 => 2, 2 => 1, 3 => 1], 'card'));
|
||
|
||
$sale = $this->sales->findById($result->saleId);
|
||
|
||
expect($result->totalCents)->toBe(6000)
|
||
->and($result->bookedQuarters)->toBe(4)
|
||
->and($result->unbookedQuarters)->toBe(0)
|
||
->and($sale->soldOn()->format('Y-m-d'))->toBe('2026-11-24')
|
||
->and($sale->paymentMethod())->toBe(PaymentMethod::Card)
|
||
->and($this->hours->booked)->toBe([[4, '2026-11-24', 7, 4, $result->saleId]]);
|
||
});
|
||
|
||
it('keeps the sale when the hours cannot be booked', function () {
|
||
$this->hours->fails = true;
|
||
|
||
$result = $this->sell->execute(4, new SaleData([1 => 1], 'cash'));
|
||
|
||
expect($result->bookedQuarters)->toBe(0)
|
||
->and($result->unbookedQuarters)->toBe(1)
|
||
->and($this->sales->sales)->toHaveCount(1);
|
||
});
|
||
|
||
it('rejects what cannot be sold', function (array $quantities, ?int $customerId, string $exception) {
|
||
expect(fn () => $this->sell->execute(4, new SaleData($quantities, 'cash', $customerId)))->toThrow($exception);
|
||
})->with([
|
||
'unknown product' => [[99 => 1], null, ProductNotSellable::class],
|
||
'no products' => [[], null, QuickSaleEmpty::class],
|
||
'negative quantity' => [[1 => -1], null, InvalidQuantity::class],
|
||
'unknown customer' => [[1 => 1], 5, QuickSaleCustomerNotFound::class],
|
||
]);
|
||
|
||
it('credits a sale once, on today\'s cash day', function () {
|
||
$saleId = $this->sell->execute(4, new SaleData([1 => 1], 'cash'))->saleId;
|
||
$this->clock->today = new DateTimeImmutable('2026-11-25');
|
||
|
||
$creditId = $this->credit->execute($saleId, 8);
|
||
|
||
expect($this->sales->findById($creditId)->soldOn()->format('Y-m-d'))->toBe('2026-11-25')
|
||
->and(fn () => $this->credit->execute($saleId, 8))->toThrow(QuickSaleAlreadyCredited::class);
|
||
});
|
||
|
||
it('closes a day with its balance, refuses sales on it and lets it be reopened', function () {
|
||
$this->sell->execute(4, new SaleData([1 => 2], 'cash'));
|
||
$this->close->execute(new DateTimeImmutable('2026-11-24'), 4);
|
||
|
||
$day = $this->cashDays->forDay(new DateTimeImmutable('2026-11-24'));
|
||
|
||
expect($day->isClosed())->toBeTrue()
|
||
->and($day->closedBy())->toBe(4)
|
||
->and($day->balance()->totalCents())->toBe(3000)
|
||
->and(fn () => $this->sell->execute(4, new SaleData([1 => 1], 'cash')))->toThrow(CashDayClosed::class)
|
||
->and(fn () => $this->close->execute(new DateTimeImmutable('2026-11-24'), 4))->toThrow(CashDayClosed::class)
|
||
->and(fn () => $this->close->execute(new DateTimeImmutable('2026-11-25'), 4))->toThrow(CashDayInFuture::class);
|
||
|
||
$this->reopen->execute(new DateTimeImmutable('2026-11-24'), 1);
|
||
$day = $this->cashDays->forDay(new DateTimeImmutable('2026-11-24'));
|
||
|
||
expect($day->isClosed())->toBeFalse()
|
||
->and($day->balance())->toBeNull()
|
||
->and($day->reopenedBy())->toBe(1)
|
||
->and(fn () => $this->reopen->execute(new DateTimeImmutable('2026-11-24'), 1))->toThrow(CashDayNotClosed::class);
|
||
});
|
||
|
||
it('closes a day without sales with a zero balance', function () {
|
||
$day = CashDay::open(new DateTimeImmutable('2026-11-24'));
|
||
$day->close([], 1, new DateTimeImmutable);
|
||
|
||
expect($day->balance())->toEqual(CashDayBalance::empty());
|
||
});
|
||
|
||
it('emails the receipt when an address is given, and the credit receipt to the same address', function () {
|
||
$plain = $this->sell->execute(1, new SaleData([1 => 1], 'cash'));
|
||
$mailed = $this->sell->execute(1, new SaleData([1 => 1], 'card', receiptEmail: ' [email protected] '));
|
||
|
||
expect($plain->receiptEmail)->toBeNull()
|
||
->and($mailed->receiptEmail)->toBe('[email protected]')
|
||
->and($this->mailer->sent)->toBe([$mailed->saleId]);
|
||
|
||
$this->credit->execute($plain->saleId, 1);
|
||
$creditId = $this->credit->execute($mailed->saleId, 1);
|
||
|
||
expect($this->mailer->sent)->toBe([$mailed->saleId, $creditId])
|
||
->and($this->sales->findById($creditId)->receiptEmail()->value)->toBe('[email protected]');
|
||
});
|
||
|
||
it('rejects an invalid receipt address without recording the sale', function () {
|
||
$this->sell->execute(1, new SaleData([1 => 1], 'cash', receiptEmail: 'ei-osoite'));
|
||
})->throws(InvalidEmailAddress::class);
|