Add quick sales with cash days, credits and PDF receipts

Quick sales (pikamyynti) of active consumer products at the counter, in a
new Sales module:

- Product buttons add one to the sale's list per press; quantities can be
  lowered, raised or removed. Payment by cash or card, optional customer.
- Product durations are booked as the seller's hours on the yearly
  "Pikamyynnit" job (internal customer, not billed) through Jobs'
  ProvideQuickSaleTask and TimeTracking's AddTaskHours. The sale stands
  even when the hours cannot be booked, and the seller is told.
- Sales are never deleted: a credit records the lines negated on today's
  cash day, once per sale, by the seller or management.
- Closing a cash day stores its balance by payment method and VAT rate and
  stops sales on it; only management reopens a day.
- A printable A5 PDF receipt (barryvdh/laravel-dompdf) with the company's
  details, business ID, VAT number, lines and VAT breakdown.

Company details are kept in the settings and edited by the admin only on
the new Yritystiedot page. BusinessId moves from Customers to Shared so
both use the same check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 12:33:46 +02:00
co-authored by Claude Opus 5.5
parent c149c56388
commit 6de5d9bef1
101 changed files with 4768 additions and 29 deletions
@@ -6,9 +6,9 @@ use App\Modules\Customers\Domain\Entities\Customer;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Events\CustomerRegistered;
use App\Modules\Customers\Domain\Exceptions\BusinessIdAlreadyInUse;
use App\Modules\Customers\Domain\Exceptions\InvalidBusinessId;
use App\Modules\Customers\Domain\ValueObjects\BusinessId;
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
use App\Modules\Shared\Domain\Exceptions\InvalidBusinessId;
use App\Modules\Shared\Domain\ValueObjects\BusinessId;
use Tests\Fakes\FakeEventDispatcher;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryCustomerRepository;
@@ -6,8 +6,8 @@ use App\Modules\Customers\Domain\Entities\Customer;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\BusinessIdAlreadyInUse;
use App\Modules\Customers\Domain\Exceptions\CustomerNotFound;
use App\Modules\Customers\Domain\ValueObjects\BusinessId;
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
use App\Modules\Shared\Domain\ValueObjects\BusinessId;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryCustomerRepository;
@@ -3,8 +3,8 @@
use App\Modules\Customers\Domain\Entities\Customer;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\ConsumerCannotHaveBusinessId;
use App\Modules\Customers\Domain\ValueObjects\BusinessId;
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
use App\Modules\Shared\Domain\ValueObjects\BusinessId;
it('registers a company with a business id and no id yet', function () {
$customer = Customer::register(new CustomerName('Mainos Oy'), CustomerType::Company, new BusinessId('0737546-2'));
+164
View File
@@ -0,0 +1,164 @@
<?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 Tests\Fakes\FakeClock;
use Tests\Fakes\FakeCustomerQueries;
use Tests\Fakes\FakeQuickSaleHours;
use Tests\Fakes\FakeQuickSaleProducts;
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->clock = new FakeClock(new DateTimeImmutable('2026-11-24'));
$transactions = new FakeTransactionManager;
$this->sell = new SellQuickSale($this->sales, $this->cashDays, $this->products, $this->hours, $this->customers, $transactions, $this->clock);
$this->credit = new CreditQuickSale($this->sales, $this->cashDays, $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());
});
@@ -1,7 +1,7 @@
<?php
use App\Modules\Customers\Domain\Exceptions\InvalidBusinessId;
use App\Modules\Customers\Domain\ValueObjects\BusinessId;
use App\Modules\Shared\Domain\Exceptions\InvalidBusinessId;
use App\Modules\Shared\Domain\ValueObjects\BusinessId;
it('accepts a business id with a correct check digit', function (string $input, string $expected) {
expect((new BusinessId($input))->value)->toBe($expected);