Files
Toiminnanohjaus/app/Modules/Reports/Presentation/Livewire/⚡stock-value.blade.php

144 lines
6.0 KiB
PHP

<?php
use App\Modules\Inventory\Domain\ValueObjects\StockQuantity;
use App\Modules\Reports\Application\DTOs\StockValueReportData;
use App\Modules\Reports\Application\UseCases\StockValueReport;
use App\Modules\Reports\Presentation\Livewire\Concerns\FormatsReports;
use App\Modules\Users\Application\Contracts\Clock;
use Carbon\CarbonImmutable;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Url;
use Livewire\Component;
/**
* Stock value (Varaston arvo, spec 19.8): every item's balance and book
* value at the end of the chosen day, the total, and the items to order.
* Management only.
*/
new class extends Component
{
use FormatsReports;
#[Url(as: 'paiva', except: '')]
public string $day = '';
public function mount(Clock $clock): void
{
$this->authorize('reports.view');
if ($this->validDay() === null) {
$this->day = $clock->today()->format('Y-m-d');
}
}
#[Computed]
public function report(): ?StockValueReportData
{
$day = $this->validDay();
return $day === null ? null : app(StockValueReport::class)->execute($day);
}
public function updatedDay(): void
{
$this->authorize('reports.view');
unset($this->report);
}
public function quantity(int $milli, string $unit): string
{
return StockQuantity::format($milli)."\u{00A0}".__('inventory.units.'.$unit);
}
public function signedMoney(int $cents): string
{
return ($cents < 0 ? '-' : '').$this->money(abs($cents));
}
public function render(): mixed
{
return $this->view()->title(__('reports.stock_value.title'));
}
private function validDay(): ?string
{
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $this->day) !== 1) {
return null;
}
$date = CarbonImmutable::createFromFormat('!Y-m-d', $this->day);
return $date !== null && $date->format('Y-m-d') === $this->day ? $this->day : null;
}
};
?>
<div class="mx-auto max-w-6xl space-y-6 p-4 sm:p-6">
<div class="space-y-2">
<h1 class="text-2xl font-semibold">{{ __('reports.stock_value.title') }}</h1>
<p class="max-w-3xl text-sm">{{ __('reports.stock_value.intro') }}</p>
</div>
<div class="max-w-xs">
<x-input id="stock-value-day" wire:model.live="day" type="date" :label="__('reports.stock_value.day')" />
</div>
@php($report = $this->report)
@if ($report === null)
<p role="alert" class="text-sm text-red-700 dark:text-red-400">{{ __('inventory.errors.day') }}</p>
@else
<div class="flex flex-wrap items-center justify-between gap-3">
<h2 class="text-xl font-semibold"><span role="status" aria-live="polite">{{ __('reports.stock_value.total', ['date' => $this->dateLabel($day), 'amount' => $this->signedMoney($report->totalCents)]) }}</span></h2>
<x-button :href="route('reports.csv', ['report' => 'stock-value', 'paiva' => $day])" subtle color="black" icon="arrow-down-tray" :text="__('reports.download_csv')" />
</div>
@if ($report->toOrder !== [])
<section aria-labelledby="to-order" class="space-y-2 rounded-lg border border-amber-700 p-3 dark:border-amber-400">
<h3 id="to-order" class="flex items-center gap-2 font-semibold">
<x-icon name="shopping-cart" class="size-5" aria-hidden="true" />
{{ __('reports.stock_value.to_order') }}
</h3>
<ul class="list-disc pl-5 text-sm">
@foreach ($report->toOrder as $item)
<li wire:key="order-{{ $item->itemId }}">{{ $item->itemName }}: {{ $this->quantity($item->balanceMilli, $item->unit) }}</li>
@endforeach
</ul>
</section>
@endif
@if ($report->items === [])
<p>{{ __('reports.stock_value.empty') }}</p>
@else
<div class="overflow-x-auto rounded focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-600" tabindex="0" role="region" aria-label="{{ __('reports.stock_value.table', ['date' => $this->dateLabel($day)]) }}">
<table class="w-full text-sm">
<caption class="sr-only">{{ __('reports.stock_value.table', ['date' => $this->dateLabel($day)]) }}</caption>
<thead>
<tr class="border-b border-zinc-300 text-left dark:border-zinc-600">
<th scope="col" class="py-2 pr-3">{{ __('reports.stock_usage.item') }}</th>
<th scope="col" class="py-2 pr-3">{{ __('reports.stock_usage.group') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('inventory.columns.balance') }}</th>
<th scope="col" class="py-2 text-right">{{ __('inventory.columns.value') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
@foreach ($report->items as $item)
<tr wire:key="value-{{ $item->itemId }}">
<th scope="row" class="py-2 pr-3 text-left font-normal">
{{ $item->itemName }}
@if ($item->needsReorder())
<span class="block text-xs font-medium">{{ __('inventory.status.reorder') }}</span>
@endif
</th>
<td class="py-2 pr-3">{{ $item->groupName }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->quantity($item->balanceMilli, $item->unit) }}</td>
<td class="py-2 text-right whitespace-nowrap">{{ $this->signedMoney($item->valueCents) }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endif
@endif
</div>