Add the quick sales report
Quick sales of a period by cash day, product, seller and VAT rate, with credits, open cash days flagged, a chart by day or month and CSV. VAT is taken out of each day's total of a rate, as on the cash day balance. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ProductModel;
|
||||
use App\Modules\Sales\Domain\Enums\PaymentMethod;
|
||||
use App\Modules\Sales\Infrastructure\Persistence\Eloquent\QuickSaleModel;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Livewire\Livewire;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->actingAs(User::factory()->management()->create());
|
||||
$this->seller = User::factory()->employee()->create(['name' => 'Myyjä Maija']);
|
||||
$this->photos = ProductModel::factory()->create(['name' => 'Passikuvat', 'price_cents' => 1990]);
|
||||
$this->prints = ProductModel::factory()->create(['name' => 'Tuloste', 'price_cents' => 100]);
|
||||
});
|
||||
|
||||
function reportSale(string $day, ProductModel $product, int $quantity, PaymentMethod $method = PaymentMethod::Cash, int $vat = 2550): QuickSaleModel
|
||||
{
|
||||
return QuickSaleModel::factory()->withLine($product, $quantity, $vat)->create([
|
||||
'user_id' => test()->seller->id,
|
||||
'payment_method' => $method,
|
||||
'sold_on' => $day,
|
||||
'sold_at' => $day.' 10:00:00',
|
||||
]);
|
||||
}
|
||||
|
||||
it('sums quick sales by day, product, seller and VAT rate with credits', function () {
|
||||
$first = reportSale('2026-11-02', $this->photos, 2);
|
||||
reportSale('2026-11-02', $this->prints, 5, PaymentMethod::Card, 1350);
|
||||
reportSale('2026-11-03', $this->photos, 1, PaymentMethod::Card);
|
||||
QuickSaleModel::factory()->withLine($this->photos, -2)->create([
|
||||
'user_id' => $this->seller->id, 'sold_on' => '2026-11-03', 'sold_at' => '2026-11-03 12:00:00', 'credits_quick_sale_id' => $first->id,
|
||||
]);
|
||||
reportSale('2026-12-01', $this->photos, 9);
|
||||
DB::table('cash_days')->insert(['day' => '2026-11-02', 'closed_at' => now(), 'closed_by' => $this->seller->id, 'created_at' => now(), 'updated_at' => now()]);
|
||||
|
||||
$report = Livewire::withQueryParams(['alku' => '2026-11-01', 'loppu' => '2026-11-30'])
|
||||
->test('reports::sales')
|
||||
->assertOk()
|
||||
->assertSee('Passikuvat')
|
||||
->assertSee('Myyjä Maija')
|
||||
->instance()
|
||||
->report();
|
||||
|
||||
expect($report->count)->toBe(4)
|
||||
->and($report->cashCents)->toBe(0)
|
||||
->and($report->cardCents)->toBe(2490)
|
||||
->and($report->openDays)->toBe(1)
|
||||
->and($report->days[0])->toBe(['day' => '2026-11-02', 'count' => 2, 'cashCents' => 3980, 'cardCents' => 500, 'isClosed' => true, 'totalCents' => 4480])
|
||||
->and($report->products[0])->toBe(['name' => 'Passikuvat', 'quantity' => 1, 'grossCents' => 1990])
|
||||
->and($report->sellers)->toBe([['name' => 'Myyjä Maija', 'count' => 4, 'grossCents' => 2490]])
|
||||
->and($report->vatTotals)->toBe([
|
||||
['vatBasisPoints' => 2550, 'grossCents' => 1990, 'netCents' => 1585, 'vatCents' => 405],
|
||||
['vatBasisPoints' => 1350, 'grossCents' => 500, 'netCents' => 441, 'vatCents' => 59],
|
||||
])
|
||||
->and($report->series)->toBe(['2026-11-02' => 4480, '2026-11-03' => -1990]);
|
||||
});
|
||||
|
||||
it('charts a long period by month and downloads the days as CSV', function () {
|
||||
reportSale('2026-03-02', $this->photos, 1);
|
||||
reportSale('2026-03-20', $this->photos, 1);
|
||||
|
||||
$report = Livewire::withQueryParams(['alku' => '2026-01-01', 'loppu' => '2026-12-31'])->test('reports::sales')->instance()->report();
|
||||
expect($report->seriesByMonth)->toBeTrue()
|
||||
->and($report->series)->toBe(['2026-03' => 3980]);
|
||||
|
||||
$response = $this->get(route('reports.csv', ['report' => 'sales', 'alku' => '2026-01-01', 'loppu' => '2026-12-31']))
|
||||
->assertOk()
|
||||
->assertDownload('pikamyynnit-2026-01-01-2026-12-31.csv');
|
||||
|
||||
expect($response->streamedContent())->toContain('2026-03-02;1;1990;0;1990;ei');
|
||||
});
|
||||
Reference in New Issue
Block a user