Files
Toiminnanohjaus/tests/Feature/Modules/Sales/QuickSaleOrdersTest.php
T
2026-11-26 17:11:54 +02:00

168 lines
7.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ProductModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockMovementModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineQueueEntryModel;
use App\Modules\Sales\Infrastructure\Persistence\Eloquent\QuickSaleModel;
use App\Modules\Sales\Infrastructure\Persistence\Eloquent\QuickSaleOrderLineModel;
use App\Modules\Sales\Infrastructure\Persistence\Eloquent\QuickSaleOrderModel;
use App\Modules\TimeTracking\Infrastructure\Persistence\Eloquent\TimeEntryModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->travelTo(now()->setTimezone(config('app.business_timezone'))->setTime(10, 0));
$this->manager = User::factory()->management()->create(['name' => 'Maija Johto']);
$this->employee = User::factory()->employee()->create(['name' => 'Eero Työntekijä']);
$print = ServiceUnitModel::query()->where('name', 'Painotuotanto')->sole();
$print->update(['uses_stock' => true]);
$this->printing = WorkTypeModel::query()->where('name', 'painotyö')->sole();
$this->printer = MachineModel::factory()->inUnits([$print->id])->create(['name' => 'Tarratulostin']);
$this->sticker = ProductModel::factory()->consumer()->create(['name' => 'Tarra', 'price_cents' => 1000, 'work_type_id' => $this->printing->id]);
$this->copy = ProductModel::factory()->consumer()->create(['name' => 'Tuloste', 'price_cents' => 100]);
$this->vinyl = StockItemModel::factory()->create(['name' => 'Tarramateriaali', 'unit' => 'square_metre']);
$this->transfer = StockItemModel::factory()->create(['name' => 'Siirtotarrapaperi', 'unit' => 'sheet']);
$this->actingAs($this->manager);
Livewire::test('machines::production-process', ['productId' => $this->sticker->id])
->call('addStep')
->set('steps.0.machine_id', (string) $this->printer->id)
->set('steps.0.duration', '0,25')
->call('addMaterial', 0)
->set('steps.0.materials.0.stock_item_id', (string) $this->vinyl->id)
->set('steps.0.materials.0.quantity', '0,5')
->call('addStep')
->set('steps.1.machine_id', '')
->set('steps.1.duration', '0')
->call('addMaterial', 1)
->set('steps.1.materials.0.stock_item_id', (string) $this->transfer->id)
->set('steps.1.materials.0.quantity', '1')
->call('save')
->assertHasNoErrors()
->assertSet('message', __('machines.process.saved'));
$this->actingAs($this->employee);
});
function sendOrder(object $test): mixed
{
return Livewire::test('sales::sell')
->call('addProduct', $test->sticker->id)
->call('addProduct', $test->sticker->id)
->call('addProduct', $test->copy->id)
->assertSee(__('sales.orders.send'))
->assertDontSee(__('sales.sell.payment_method'))
->set('orderCustomerName', 'Matti')
->call('save');
}
it('sends a sale with made products to production with the day\'s pickup number', function () {
sendOrder($this)
->assertHasNoErrors()
->assertSet('savedPickupNumber', 1)
->assertSet('savedMessage', fn (string $message) => str_contains($message, 'Noutonumero 1'));
$order = QuickSaleOrderModel::query()->sole();
$line = QuickSaleOrderLineModel::query()->where('product_name', 'Tarra')->sole();
expect($order->customer_name)->toBe('Matti')
->and($order->status)->toBe('in_production')
->and(QuickSaleModel::query()->count())->toBe(0)
->and(MachineQueueEntryModel::query()->sole()->only(['machine_id', 'quick_sale_order_line_id', 'production_step_position']))
->toBe(['machine_id' => $this->printer->id, 'quick_sale_order_line_id' => $line->id, 'production_step_position' => 0]);
sendOrder($this)->assertSet('savedPickupNumber', 2);
Livewire::test('sales::orders')->assertSee('Nro 1')->assertSee('Matti')->assertSee('Valmistuksessa');
});
it('makes an order step by step, records materials and hours, and takes the payment', function () {
sendOrder($this);
$order = QuickSaleOrderModel::query()->sole();
$entry = MachineQueueEntryModel::query()->sole();
Livewire::test('machines::console', ['machine' => $this->printer->id])
->assertSee('Nro 1')
->assertSee('2 × Tarra')
->call('startOrderComplete', $entry->id)
->assertSet('orderUsed.'.$this->vinyl->id, '1')
->call('changeOrderUsed', $this->vinyl->id, 1)
->call('completeOrder')
->assertHasNoErrors()
->assertDontSee('2 × Tarra');
$movements = StockMovementModel::query()->where('stock_item_id', $this->vinyl->id)->orderBy('id')->get();
expect($movements->map->only(['kind', 'quantity_milli', 'machine_id'])->map(fn ($row) => [$row['kind']->value, $row['quantity_milli'], $row['machine_id']])->all())
->toBe([['consumption', -1_000, $this->printer->id], ['write_off', -1_000, $this->printer->id]])
->and($movements->last()->reason)->toBe('uusintatuloste')
->and((float) TimeEntryModel::query()->sole()->hours)->toBe(0.5)
->and(MachineQueueEntryModel::query()->whereNull('completed_at')->count())->toBe(0);
$line = QuickSaleOrderLineModel::query()->where('product_name', 'Tarra')->sole();
Livewire::test('sales::orders')
->assertSee('vaihe 2/2')
->call('startComplete', $line->id)
->assertSet('used.'.$this->transfer->id, '2')
->call('complete')
->assertSet('message', 'Tilauksen 1 tuote Tarra on valmis.')
->assertSee('Valmis, maksettavana')
->call('startPay', $order->id)
->set('paymentMethod', 'card')
->call('pay')
->assertHasNoErrors()
->assertSet('message', fn (string $message) => str_starts_with($message, 'Tilaus 1 maksettu: 21,00'))
->assertDontSee('Nro 1');
$sale = QuickSaleModel::query()->sole();
expect($sale->quick_sale_order_id)->toBe($order->id)
->and($sale->total_cents)->toBe(2_100)
->and($order->fresh()->status)->toBe('completed')
->and(TimeEntryModel::query()->count())->toBe(1);
});
it('lets an order be paid in advance and cancelled only while unpaid', function () {
sendOrder($this);
sendOrder($this);
[$first, $second] = QuickSaleOrderModel::query()->orderBy('id')->get()->all();
Livewire::test('sales::orders')
->call('startPay', $first->id)
->call('pay')
->assertSee('maksettu')
->call('confirmCancel', $second->id)
->call('cancel')
->assertSet('message', 'Tilaus 2 peruttu.');
expect($first->fresh()->status)->toBe('in_production')
->and($first->fresh()->paid_at)->not->toBeNull()
->and($second->fresh()->status)->toBe('cancelled')
->and(MachineQueueEntryModel::query()->whereNull('completed_at')->count())->toBe(1);
Livewire::test('sales::index')->call('confirmClose')->assertDontSee('tilaus on maksamatta');
sendOrder($this);
Livewire::test('sales::index')->assertSee('1 tilaus on maksamatta');
});
it('sells products without a process at once as before', function () {
Livewire::test('sales::sell')
->call('addProduct', $this->copy->id)
->assertSee(__('sales.sell.save'))
->call('save')
->assertHasNoErrors();
expect(QuickSaleModel::query()->count())->toBe(1)
->and(QuickSaleOrderModel::query()->count())->toBe(0);
});