Report stock usage and value, add material cost to profitability, document phase 5

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 17:24:07 +02:00
co-authored by Claude Sonnet 5.5
parent 38b8075a9c
commit 3207d0064a
34 changed files with 1145 additions and 24 deletions
@@ -133,7 +133,7 @@ new class extends Component
<td class="py-2 pr-3 text-right">{{ $unit->jobCount }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($unit->profit->quarters) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($unit->profit->revenueCents()) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($unit->profit->expenseCostCents + ($unit->profit->hourCostCents ?? 0)) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($unit->profit->expenseCostCents + $unit->profit->materialCostCents + ($unit->profit->hourCostCents ?? 0)) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->margin($unit->profit) }}</td>
<td class="py-2 text-right whitespace-nowrap">{{ $this->marginPercent($unit->profit) }}</td>
</tr>
@@ -167,6 +167,7 @@ new class extends Component
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.work_revenue') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.expense_revenue') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.expense_cost') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.material_cost') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.hour_cost') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.margin') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.margin_percent') }}</th>
@@ -188,6 +189,7 @@ new class extends Component
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($row->profit->workCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($row->profit->expenseRevenueCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($row->profit->expenseCostCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($row->profit->materialCostCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hourCost($row->profit) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->margin($row->profit) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->marginPercent($row->profit) }}</td>
@@ -203,6 +205,7 @@ new class extends Component
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($report->total->workCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($report->total->expenseRevenueCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($report->total->expenseCostCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($report->total->materialCostCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hourCost($report->total) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->margin($report->total) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->marginPercent($report->total) }}</td>
@@ -0,0 +1,153 @@
<?php
use App\Modules\Inventory\Domain\ValueObjects\StockQuantity;
use App\Modules\Reports\Application\DTOs\StockUsageReportData;
use App\Modules\Reports\Application\DTOs\StockUsageRow;
use App\Modules\Reports\Application\UseCases\StockUsageReport;
use App\Modules\Reports\Presentation\Livewire\Concerns\ReportPage;
use App\Modules\Users\Application\Contracts\Clock;
use Livewire\Attributes\Computed;
use Livewire\Component;
/**
* Stock usage (Varaston käyttö, spec 19.8): stock used, written off and
* corrected in stock counts in a period, quantities and values at cost,
* by item, group, machine and job. Management only.
*/
new class extends Component
{
use ReportPage;
public function mount(Clock $clock): void
{
$this->startPeriod($clock, 'this_month');
}
#[Computed]
public function report(): ?StockUsageReportData
{
$period = $this->period();
return $period === null ? null : app(StockUsageReport::class)->execute($period, __('reports.stock_usage.none_target'));
}
public function updated(): void
{
$this->authorize('reports.view');
unset($this->report);
}
public function quantity(int $milli, ?string $unit): string
{
return StockQuantity::format($milli).($unit === null ? '' : "\u{00A0}".__('inventory.units.'.$unit));
}
public function signedMoney(int $cents): string
{
return ($cents < 0 ? '-' : '').$this->money(abs($cents));
}
/**
* @return list<array{key: string, rows: list<StockUsageRow>}>
*/
public function summaries(StockUsageReportData $report): array
{
return [
['key' => 'groups', 'rows' => $report->groups],
['key' => 'machines', 'rows' => $report->machines],
['key' => 'jobs', 'rows' => $report->jobs],
];
}
public function render(): mixed
{
return $this->view()->title(__('reports.stock_usage.title'));
}
};
?>
<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_usage.title') }}</h1>
<p class="max-w-3xl text-sm">{{ __('reports.stock_usage.intro') }}</p>
</div>
<x-reports.period-filter :presets="$this->periodPresets()" :valid="$this->period !== null" />
@php($report = $this->report)
@if ($report !== null)
<div class="flex flex-wrap items-center justify-between gap-3">
<h2 class="text-xl font-semibold"><span role="status" aria-live="polite">{{ $this->periodLabel() }}</span></h2>
<x-button :href="$this->csvUrl('stock-usage')" subtle color="black" icon="arrow-down-tray" :text="__('reports.download_csv')" />
</div>
@if ($report->items === [])
<p>{{ __('reports.stock_usage.empty') }}</p>
@else
<p class="font-semibold">{{ __('reports.stock_usage.total', ['amount' => $this->signedMoney($report->total->totalCents())]) }}</p>
<section aria-labelledby="usage-items" class="space-y-2">
<h3 id="usage-items" class="text-lg font-semibold">{{ __('reports.stock_usage.by_item') }}</h3>
<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_usage.by_item') }}, {{ $this->periodLabel() }}">
<table class="w-full text-sm">
<caption class="sr-only">{{ __('reports.stock_usage.by_item') }}, {{ $this->periodLabel() }}</caption>
<thead>
<tr class="border-b border-zinc-300 text-left align-bottom 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">{{ __('reports.stock_usage.used') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.stock_usage.written_off') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.stock_usage.adjusted') }}</th>
<th scope="col" class="py-2 text-right">{{ __('reports.stock_usage.value') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
@foreach ($report->items as $index => $row)
<tr wire:key="item-{{ $index }}">
<th scope="row" class="py-2 pr-3 text-left font-normal">{{ $row->label }}</th>
<td class="py-2 pr-3">{{ $row->group }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->quantity($row->usedMilli, $row->unit) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->quantity($row->writtenOffMilli, $row->unit) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->quantity($row->adjustedMilli, $row->unit) }}</td>
<td class="py-2 text-right whitespace-nowrap">{{ $this->signedMoney($row->totalCents()) }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</section>
@foreach ($this->summaries($report) as $summary)
<section wire:key="summary-{{ $summary['key'] }}" aria-labelledby="usage-{{ $summary['key'] }}" class="space-y-2">
<h3 id="usage-{{ $summary['key'] }}" class="text-lg font-semibold">{{ __('reports.stock_usage.by_'.$summary['key']) }}</h3>
<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_usage.by_'.$summary['key']) }}, {{ $this->periodLabel() }}">
<table class="w-full text-sm">
<caption class="sr-only">{{ __('reports.stock_usage.by_'.$summary['key']) }}, {{ $this->periodLabel() }}</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.'.$summary['key'].'_column') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.stock_usage.used_value') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.stock_usage.written_off_value') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.stock_usage.adjusted_value') }}</th>
<th scope="col" class="py-2 text-right">{{ __('reports.stock_usage.value') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
@foreach ($summary['rows'] as $index => $row)
<tr wire:key="{{ $summary['key'] }}-{{ $index }}">
<th scope="row" class="py-2 pr-3 text-left font-normal">{{ $row->label }}</th>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->signedMoney($row->usedCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->signedMoney($row->writtenOffCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->signedMoney($row->adjustedCents) }}</td>
<td class="py-2 text-right whitespace-nowrap">{{ $this->signedMoney($row->totalCents()) }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</section>
@endforeach
@endif
@endif
</div>
@@ -0,0 +1,143 @@
<?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>