Make quick sale products on machines as orders paid when ready
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?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\Machines\Application\DTOs\ProductionStepData;
|
||||
use App\Modules\Machines\Application\UseCases\SaveProductionProcess;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
|
||||
use App\Modules\Sales\Application\DTOs\OrderData;
|
||||
use App\Modules\Sales\Application\UseCases\QuickSaleOrders;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->user = User::factory()->management()->create(['name' => 'Johtaja']);
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$print = ServiceUnitModel::query()->where('name', 'Painotuotanto')->sole();
|
||||
$print->update(['uses_stock' => true]);
|
||||
$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' => $printing->id]);
|
||||
$vinyl = StockItemModel::factory()->create(['name' => 'Tarramateriaali', 'unit' => 'square_metre']);
|
||||
|
||||
app(SaveProductionProcess::class)->execute($this->sticker->id, [
|
||||
new ProductionStepData($this->printer->id, '0,25', $printing->id, [['stockItemId' => $vinyl->id, 'quantity' => '0,5']]),
|
||||
new ProductionStepData(null, '0', $printing->id),
|
||||
]);
|
||||
app(QuickSaleOrders::class)->send($this->user->id, new OrderData([$this->sticker->id => 2], customerName: 'Matti'));
|
||||
});
|
||||
|
||||
it('has no accessibility issues on the pages of quick sale orders', function (string $page, string $device, string $colorScheme) {
|
||||
$url = match ($page) {
|
||||
'sell' => route('sales.sell'),
|
||||
'product' => route('catalog.products.edit', $this->sticker->id),
|
||||
'console' => route('machines.console', $this->printer->id),
|
||||
};
|
||||
|
||||
$visit = visit($url);
|
||||
$visit = $device === 'mobile' ? $visit->on()->mobile() : $visit->on()->desktop();
|
||||
$visit = $colorScheme === 'dark' ? $visit->inDarkMode() : $visit->inLightMode();
|
||||
|
||||
$visit->assertSee($page === 'product' ? __('machines.process.title') : 'Nro 1')
|
||||
->assertNoJavaScriptErrors()
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
})->with(['sell', 'product', 'console'])->with([
|
||||
'desktop, light' => ['desktop', 'light'],
|
||||
'desktop, dark' => ['desktop', 'dark'],
|
||||
'mobile, light' => ['mobile', 'light'],
|
||||
'mobile, dark' => ['mobile', 'dark'],
|
||||
]);
|
||||
|
||||
it('completes an order on the console and pays it on the quick sale page with the keyboard', function () {
|
||||
visit(route('machines.console', $this->printer->id))
|
||||
->click('[aria-label="'.__('machines.console.order_finish_named', ['number' => 1]).'"]')
|
||||
->assertSee(__('machines.console.order_materials'))
|
||||
->assertNoAccessibilityIssues(level: 2)
|
||||
->click(__('machines.console.confirm_order'))
|
||||
->assertSee('Tilauksen 1 tuote Tarra kuitattu.');
|
||||
|
||||
visit(route('sales.sell'))
|
||||
->assertSee('vaihe 2/2')
|
||||
->click('[aria-label="'.__('sales.orders.complete_step_named', ['number' => 1, 'product' => 'Tarra']).'"]')
|
||||
->click(__('sales.orders.complete'))
|
||||
->assertSee('Tilauksen 1 tuote Tarra on valmis.')
|
||||
->click('[aria-label="'.__('sales.orders.pay_named', ['number' => 1]).'"]')
|
||||
->assertNoAccessibilityIssues(level: 2)
|
||||
->click(__('sales.orders.take_payment'))
|
||||
->assertSee('Tilaus 1 maksettu')
|
||||
->assertSee('Avaa kuitti');
|
||||
});
|
||||
@@ -12,6 +12,11 @@ class FakeQuickSaleHours implements QuickSaleHours
|
||||
|
||||
public bool $fails = false;
|
||||
|
||||
public function bookForOrder(int $userId, DateTimeImmutable $day, int $workTypeId, int $quarters, int $pickupNumber): bool
|
||||
{
|
||||
return $this->book($userId, $day, $workTypeId, $quarters, 0);
|
||||
}
|
||||
|
||||
public function book(int $sellerId, DateTimeImmutable $day, int $workTypeId, int $quarters, int $saleId): bool
|
||||
{
|
||||
if ($this->fails) {
|
||||
|
||||
@@ -36,6 +36,7 @@ class InMemoryQuickSaleRepository implements QuickSaleRepository
|
||||
$sale->lines(),
|
||||
$sale->creditsSaleId(),
|
||||
$sale->receiptEmail(),
|
||||
$sale->orderId(),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
<?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);
|
||||
});
|
||||
Reference in New Issue
Block a user