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:
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Inventory\Application\Contracts;
|
||||
|
||||
use App\Modules\Inventory\Application\DTOs\StockUsageEntry;
|
||||
use App\Modules\Inventory\Application\DTOs\StockValueEntry;
|
||||
|
||||
/**
|
||||
* Stock figures for management's reports (spec 19.8). Days are Y-m-d.
|
||||
*/
|
||||
interface StockReportQueries
|
||||
{
|
||||
/**
|
||||
* Use, write-offs and stock count corrections between the days.
|
||||
*
|
||||
* @return list<StockUsageEntry>
|
||||
*/
|
||||
public function usage(string $from, string $to): array;
|
||||
|
||||
/**
|
||||
* Every item with movements up to the day, or in use, in group order.
|
||||
*
|
||||
* @return list<StockValueEntry>
|
||||
*/
|
||||
public function valueOn(string $day): array;
|
||||
|
||||
/**
|
||||
* The value of the stock used and written off on jobs' tasks between
|
||||
* the days, by job id (spec 19.8: material cost in profitability).
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
public function materialCostsByJob(string $from, string $to): array;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Inventory\Application\DTOs;
|
||||
|
||||
/**
|
||||
* Stock taken out in a period, summed by item, kind, machine and job: the
|
||||
* quantity (in thousandths) and value (in cents) as positive amounts
|
||||
* taken; a stock count correction that added stock is negative. Reversals
|
||||
* count with the movement they reverse. $kind is consumption, write_off
|
||||
* or adjustment.
|
||||
*/
|
||||
final readonly class StockUsageEntry
|
||||
{
|
||||
public function __construct(
|
||||
public int $itemId,
|
||||
public string $itemName,
|
||||
public string $groupName,
|
||||
public string $unit,
|
||||
public string $kind,
|
||||
public ?int $machineId,
|
||||
public ?int $jobId,
|
||||
public int $quantityMilli,
|
||||
public int $valueCents,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Inventory\Application\DTOs;
|
||||
|
||||
/**
|
||||
* An item's balance (in thousandths) and book value (in cents) at the end
|
||||
* of a day: the sums of its movements up to the day, each at its own cost.
|
||||
*/
|
||||
final readonly class StockValueEntry
|
||||
{
|
||||
public function __construct(
|
||||
public int $itemId,
|
||||
public string $itemName,
|
||||
public string $groupName,
|
||||
public string $unit,
|
||||
public int $balanceMilli,
|
||||
public int $valueCents,
|
||||
public ?int $reorderLevelMilli,
|
||||
public bool $isActive,
|
||||
) {}
|
||||
|
||||
public function needsReorder(): bool
|
||||
{
|
||||
return $this->reorderLevelMilli !== null && $this->balanceMilli <= $this->reorderLevelMilli;
|
||||
}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Inventory\Application\Contracts\StockReportQueries;
|
||||
use App\Modules\Inventory\Application\DTOs\StockUsageEntry;
|
||||
use App\Modules\Inventory\Application\DTOs\StockValueEntry;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Stock report figures straight from the movements. A reversal counts as
|
||||
* the kind of the movement it reverses, so undone use nets to nothing.
|
||||
*/
|
||||
class EloquentStockReportQueries implements StockReportQueries
|
||||
{
|
||||
public function usage(string $from, string $to): array
|
||||
{
|
||||
$rows = DB::table('stock_movements as m')
|
||||
->leftJoin('stock_movements as o', 'o.id', '=', 'm.reverses_movement_id')
|
||||
->join('stock_items as i', 'i.id', '=', 'm.stock_item_id')
|
||||
->join('stock_item_groups as g', 'g.id', '=', 'i.stock_item_group_id')
|
||||
->whereDate('m.occurred_on', '>=', $from)
|
||||
->whereDate('m.occurred_on', '<=', $to)
|
||||
->where(fn (Builder $query) => $query
|
||||
->whereIn('m.kind', ['consumption', 'write_off', 'adjustment'])
|
||||
->orWhere(fn (Builder $query) => $query->where('m.kind', 'reversal')->whereIn('o.kind', ['consumption', 'write_off'])))
|
||||
->selectRaw("i.id as item_id, i.name as item_name, g.name as group_name, i.unit as unit, CASE WHEN m.kind = 'reversal' THEN o.kind ELSE m.kind END as kind, m.machine_id, m.customer_job_id, SUM(m.quantity_milli) as quantity, SUM(m.value_cents) as value")
|
||||
->groupBy('i.id', 'i.name', 'g.name', 'i.unit', DB::raw("CASE WHEN m.kind = 'reversal' THEN o.kind ELSE m.kind END"), 'm.machine_id', 'm.customer_job_id')
|
||||
->orderBy('g.name')
|
||||
->orderBy('i.name')
|
||||
->get();
|
||||
|
||||
return $rows
|
||||
->map(fn (object $row): StockUsageEntry => new StockUsageEntry(
|
||||
(int) $row->item_id,
|
||||
(string) $row->item_name,
|
||||
(string) $row->group_name,
|
||||
(string) $row->unit,
|
||||
(string) $row->kind,
|
||||
$row->machine_id === null ? null : (int) $row->machine_id,
|
||||
$row->customer_job_id === null ? null : (int) $row->customer_job_id,
|
||||
-(int) $row->quantity,
|
||||
-(int) $row->value,
|
||||
))
|
||||
->filter(fn (StockUsageEntry $entry): bool => $entry->quantityMilli !== 0 || $entry->valueCents !== 0)
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function valueOn(string $day): array
|
||||
{
|
||||
$sums = DB::table('stock_movements')
|
||||
->whereDate('occurred_on', '<=', $day)
|
||||
->groupBy('stock_item_id')
|
||||
->selectRaw('stock_item_id, SUM(quantity_milli) as quantity, SUM(value_cents) as value')
|
||||
->get()
|
||||
->keyBy('stock_item_id');
|
||||
|
||||
return DB::table('stock_items as i')
|
||||
->join('stock_item_groups as g', 'g.id', '=', 'i.stock_item_group_id')
|
||||
->orderBy('g.sort_order')
|
||||
->orderBy('i.name')
|
||||
->get(['i.id', 'i.name', 'i.unit', 'i.reorder_level_milli', 'i.is_active', 'g.name as group_name'])
|
||||
->filter(fn (object $item): bool => (bool) $item->is_active || $sums->has($item->id))
|
||||
->map(fn (object $item): StockValueEntry => new StockValueEntry(
|
||||
(int) $item->id,
|
||||
(string) $item->name,
|
||||
(string) $item->group_name,
|
||||
(string) $item->unit,
|
||||
(int) ($sums->get($item->id)->quantity ?? 0),
|
||||
(int) ($sums->get($item->id)->value ?? 0),
|
||||
$item->reorder_level_milli === null ? null : (int) $item->reorder_level_milli,
|
||||
(bool) $item->is_active,
|
||||
))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function materialCostsByJob(string $from, string $to): array
|
||||
{
|
||||
return DB::table('stock_movements')
|
||||
->whereNotNull('customer_job_id')
|
||||
->whereIn('kind', ['consumption', 'write_off', 'reversal'])
|
||||
->whereDate('occurred_on', '>=', $from)
|
||||
->whereDate('occurred_on', '<=', $to)
|
||||
->groupBy('customer_job_id')
|
||||
->selectRaw('customer_job_id, SUM(value_cents) as value')
|
||||
->pluck('value', 'customer_job_id')
|
||||
->map(fn ($value): int => -(int) $value)
|
||||
->filter(fn (int $value): bool => $value !== 0)
|
||||
->all();
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Inventory\Application\Contracts\InventoryCountQueries;
|
||||
use App\Modules\Inventory\Application\Contracts\StockItemImages;
|
||||
use App\Modules\Inventory\Application\Contracts\StockQueries;
|
||||
use App\Modules\Inventory\Application\Contracts\StockReportQueries;
|
||||
use App\Modules\Inventory\Application\Contracts\StockTasks;
|
||||
use App\Modules\Inventory\Application\UseCases\TaskMaterials;
|
||||
use App\Modules\Inventory\Domain\Contracts\InventoryCountRepository;
|
||||
@@ -23,6 +24,7 @@ use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentStockI
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentStockItemRepository;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentStockMovementRepository;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentStockQueries;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentStockReportQueries;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentTaskMaterialSuggestionRepository;
|
||||
use App\Modules\Jobs\Application\Contracts\TaskUsage;
|
||||
use App\Modules\Jobs\Domain\Events\TaskMaterialsSuggested;
|
||||
@@ -52,6 +54,7 @@ class InventoryServiceProvider extends ServiceProvider
|
||||
StockTasks::class => TasksForStock::class,
|
||||
InventoryCountRepository::class => EloquentInventoryCountRepository::class,
|
||||
InventoryCountQueries::class => EloquentInventoryCountQueries::class,
|
||||
StockReportQueries::class => EloquentStockReportQueries::class,
|
||||
];
|
||||
|
||||
public function register(): void
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\Contracts;
|
||||
|
||||
use App\Modules\Inventory\Application\DTOs\StockUsageEntry;
|
||||
use App\Modules\Inventory\Application\DTOs\StockValueEntry;
|
||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||
|
||||
/**
|
||||
* Stock figures from the Inventory module and machine names from the
|
||||
* Machines module (spec 19.8).
|
||||
*/
|
||||
interface ReportStock
|
||||
{
|
||||
/**
|
||||
* @return list<StockUsageEntry>
|
||||
*/
|
||||
public function usage(ReportPeriod $period): array;
|
||||
|
||||
/**
|
||||
* @return list<StockValueEntry>
|
||||
*/
|
||||
public function valueOn(string $day): array;
|
||||
|
||||
/**
|
||||
* The value of stock used on each job's tasks, by job id.
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
public function materialCostsByJob(ReportPeriod $period): array;
|
||||
|
||||
/**
|
||||
* @param list<int> $ids
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function machineNames(array $ids): array;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\DTOs;
|
||||
|
||||
/**
|
||||
* The stock usage report: by item, group, machine and job, and in total.
|
||||
*/
|
||||
final readonly class StockUsageReportData
|
||||
{
|
||||
/**
|
||||
* @param list<StockUsageRow> $items
|
||||
* @param list<StockUsageRow> $groups
|
||||
* @param list<StockUsageRow> $machines
|
||||
* @param list<StockUsageRow> $jobs
|
||||
*/
|
||||
public function __construct(
|
||||
public array $items,
|
||||
public array $groups,
|
||||
public array $machines,
|
||||
public array $jobs,
|
||||
public StockUsageRow $total,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\DTOs;
|
||||
|
||||
/**
|
||||
* Stock taken out in a period for one item, or a sum for a group, machine
|
||||
* or job ($unit null for sums of different items): quantities in
|
||||
* thousandths, values in cents, use, write-offs and stock count
|
||||
* corrections apart.
|
||||
*/
|
||||
final readonly class StockUsageRow
|
||||
{
|
||||
public function __construct(
|
||||
public string $label,
|
||||
public ?string $group,
|
||||
public ?string $unit,
|
||||
public int $usedMilli,
|
||||
public int $usedCents,
|
||||
public int $writtenOffMilli,
|
||||
public int $writtenOffCents,
|
||||
public int $adjustedMilli,
|
||||
public int $adjustedCents,
|
||||
) {}
|
||||
|
||||
public function totalCents(): int
|
||||
{
|
||||
return $this->usedCents + $this->writtenOffCents + $this->adjustedCents;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\DTOs;
|
||||
|
||||
use App\Modules\Inventory\Application\DTOs\StockValueEntry;
|
||||
|
||||
/**
|
||||
* The stock value report of a day: the items, their total value in cents
|
||||
* and those to order.
|
||||
*/
|
||||
final readonly class StockValueReportData
|
||||
{
|
||||
/**
|
||||
* @param list<StockValueEntry> $items
|
||||
* @param list<StockValueEntry> $toOrder
|
||||
*/
|
||||
public function __construct(
|
||||
public array $items,
|
||||
public int $totalCents,
|
||||
public array $toOrder,
|
||||
) {}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use App\Modules\Reports\Application\Contracts\ReportExpenses;
|
||||
use App\Modules\Reports\Application\Contracts\ReportHours;
|
||||
use App\Modules\Reports\Application\Contracts\ReportJobs;
|
||||
use App\Modules\Reports\Application\Contracts\ReportRevenue;
|
||||
use App\Modules\Reports\Application\Contracts\ReportStock;
|
||||
use App\Modules\Reports\Application\DTOs\JobProfit;
|
||||
use App\Modules\Reports\Application\DTOs\ProfitabilityReport;
|
||||
use App\Modules\Reports\Application\DTOs\ServiceUnitProfit;
|
||||
@@ -30,6 +31,7 @@ final readonly class ProfitabilityByJob
|
||||
private ReportRevenue $revenue,
|
||||
private ReportJobs $jobs,
|
||||
private ReportCatalog $catalog,
|
||||
private ReportStock $stock,
|
||||
) {}
|
||||
|
||||
public function execute(ReportPeriod $period, bool $includeInternal = false): ProfitabilityReport
|
||||
@@ -40,10 +42,11 @@ final readonly class ProfitabilityByJob
|
||||
}
|
||||
|
||||
$expenseCosts = $this->expenses->costsByJob($period);
|
||||
$materialCosts = $this->stock->materialCostsByJob($period);
|
||||
$revenue = $this->revenue->byJob($period);
|
||||
$hourlyCost = $this->catalog->internalHourlyCostCents();
|
||||
|
||||
$jobIds = array_values(array_unique([...array_keys($quarters), ...array_keys($expenseCosts), ...array_keys($revenue)]));
|
||||
$jobIds = array_values(array_unique([...array_keys($quarters), ...array_keys($expenseCosts), ...array_keys($materialCosts), ...array_keys($revenue)]));
|
||||
$jobs = $this->jobs->infoOf($jobIds);
|
||||
|
||||
$rows = [];
|
||||
@@ -60,6 +63,7 @@ final readonly class ProfitabilityByJob
|
||||
$billed->expenseCents ?? 0,
|
||||
$expenseCosts[$job->id] ?? 0,
|
||||
$hourlyCost,
|
||||
$materialCosts[$job->id] ?? 0,
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\UseCases;
|
||||
|
||||
use App\Modules\Inventory\Application\DTOs\StockUsageEntry;
|
||||
use App\Modules\Reports\Application\Contracts\ReportJobs;
|
||||
use App\Modules\Reports\Application\Contracts\ReportStock;
|
||||
use App\Modules\Reports\Application\DTOs\StockUsageReportData;
|
||||
use App\Modules\Reports\Application\DTOs\StockUsageRow;
|
||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||
|
||||
/**
|
||||
* Stock usage (Varaston käyttö, spec 19.8): stock used, written off and
|
||||
* corrected in stock counts in a period, at its cost, by item, item group,
|
||||
* machine and job. Stock used without a machine or job is summed under
|
||||
* "none".
|
||||
*/
|
||||
final readonly class StockUsageReport
|
||||
{
|
||||
public function __construct(
|
||||
private ReportStock $stock,
|
||||
private ReportJobs $jobs,
|
||||
) {}
|
||||
|
||||
public function execute(ReportPeriod $period, string $noneLabel = '–'): StockUsageReportData
|
||||
{
|
||||
$entries = $this->stock->usage($period);
|
||||
|
||||
$machineIds = array_values(array_unique(array_filter(array_map(fn (StockUsageEntry $entry): ?int => $entry->machineId, $entries))));
|
||||
$jobIds = array_values(array_unique(array_filter(array_map(fn (StockUsageEntry $entry): ?int => $entry->jobId, $entries))));
|
||||
$machineNames = $this->stock->machineNames($machineIds);
|
||||
$jobs = $this->jobs->infoOf($jobIds);
|
||||
|
||||
$items = $this->sum($entries, fn (StockUsageEntry $entry): string => $entry->groupName."\0".$entry->itemName."\0".$entry->unit, true);
|
||||
$groups = $this->sum($entries, fn (StockUsageEntry $entry): string => $entry->groupName);
|
||||
$machines = $this->sum($entries, fn (StockUsageEntry $entry): string => $entry->machineId === null ? $noneLabel : ($machineNames[$entry->machineId] ?? ''));
|
||||
$jobRows = $this->sum($entries, function (StockUsageEntry $entry) use ($jobs, $noneLabel): string {
|
||||
$job = $entry->jobId === null ? null : ($jobs[$entry->jobId] ?? null);
|
||||
|
||||
return $job === null ? $noneLabel : $job->number.' '.$job->name;
|
||||
});
|
||||
|
||||
$total = $this->sum($entries, fn (): string => '')[0] ?? new StockUsageRow('', null, null, 0, 0, 0, 0, 0, 0);
|
||||
|
||||
return new StockUsageReportData($items, $groups, $machines, $jobRows, $total);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<StockUsageEntry> $entries
|
||||
* @param callable(StockUsageEntry): string $key
|
||||
* @return list<StockUsageRow>
|
||||
*/
|
||||
private function sum(array $entries, callable $key, bool $byItem = false): array
|
||||
{
|
||||
/** @var array<string, array{used: array{int, int}, write_off: array{int, int}, adjustment: array{int, int}}> $sums */
|
||||
$sums = [];
|
||||
/** @var array<string, StockUsageEntry> $firsts */
|
||||
$firsts = [];
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
$k = $key($entry);
|
||||
$firsts[$k] ??= $entry;
|
||||
$sum = $sums[$k] ?? ['used' => [0, 0], 'write_off' => [0, 0], 'adjustment' => [0, 0]];
|
||||
$kind = match ($entry->kind) {
|
||||
'consumption' => 'used',
|
||||
'write_off' => 'write_off',
|
||||
default => 'adjustment',
|
||||
};
|
||||
$sum[$kind][0] += $entry->quantityMilli;
|
||||
$sum[$kind][1] += $entry->valueCents;
|
||||
$sums[$k] = $sum;
|
||||
}
|
||||
|
||||
ksort($sums, SORT_NATURAL | SORT_FLAG_CASE);
|
||||
$rows = [];
|
||||
|
||||
foreach ($sums as $k => $sum) {
|
||||
$first = $firsts[$k];
|
||||
$rows[] = new StockUsageRow(
|
||||
$byItem ? $first->itemName : (string) $k,
|
||||
$byItem ? $first->groupName : null,
|
||||
$byItem ? $first->unit : null,
|
||||
$sum['used'][0],
|
||||
$sum['used'][1],
|
||||
$sum['write_off'][0],
|
||||
$sum['write_off'][1],
|
||||
$sum['adjustment'][0],
|
||||
$sum['adjustment'][1],
|
||||
);
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\UseCases;
|
||||
|
||||
use App\Modules\Inventory\Application\DTOs\StockValueEntry;
|
||||
use App\Modules\Reports\Application\Contracts\ReportStock;
|
||||
use App\Modules\Reports\Application\DTOs\StockValueReportData;
|
||||
|
||||
/**
|
||||
* Stock value (Varaston arvo, spec 19.8): every item's balance and book
|
||||
* value at the end of a day, the total, and the items to order.
|
||||
*/
|
||||
final readonly class StockValueReport
|
||||
{
|
||||
public function __construct(private ReportStock $stock) {}
|
||||
|
||||
public function execute(string $day): StockValueReportData
|
||||
{
|
||||
$items = $this->stock->valueOn($day);
|
||||
|
||||
return new StockValueReportData(
|
||||
$items,
|
||||
array_sum(array_map(fn (StockValueEntry $item): int => $item->valueCents, $items)),
|
||||
array_values(array_filter($items, fn (StockValueEntry $item): bool => $item->isActive && $item->needsReorder())),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ namespace App\Modules\Reports\Domain\ValueObjects;
|
||||
* - billed for the work (hours, fixed price, monthly fees) and for expenses;
|
||||
* - the purchase cost of the expenses and the cost of the hours worked at
|
||||
* the company's one internal hourly cost (null when it is not set);
|
||||
* - the cost of the stock used on the job (material, spec 19.8), which is
|
||||
* not billed separately;
|
||||
* - the margin: everything billed less those costs (null without the hourly cost).
|
||||
*/
|
||||
final readonly class Profit
|
||||
@@ -22,12 +24,13 @@ final readonly class Profit
|
||||
public int $expenseRevenueCents = 0,
|
||||
public int $expenseCostCents = 0,
|
||||
public ?int $hourCostCents = 0,
|
||||
public int $materialCostCents = 0,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param int|null $hourlyCostCents The internal cost of an hour, null when not set.
|
||||
*/
|
||||
public static function of(int $quarters, int $billedQuarters, int $workCents, int $expenseRevenueCents, int $expenseCostCents, ?int $hourlyCostCents): self
|
||||
public static function of(int $quarters, int $billedQuarters, int $workCents, int $expenseRevenueCents, int $expenseCostCents, ?int $hourlyCostCents, int $materialCostCents = 0): self
|
||||
{
|
||||
return new self(
|
||||
$quarters,
|
||||
@@ -37,6 +40,7 @@ final readonly class Profit
|
||||
$expenseCostCents,
|
||||
// Rounded half up to a cent.
|
||||
$hourlyCostCents === null ? null : intdiv($quarters * $hourlyCostCents * 2 + 4, 8),
|
||||
$materialCostCents,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -47,7 +51,7 @@ final readonly class Profit
|
||||
|
||||
public function marginCents(): ?int
|
||||
{
|
||||
return $this->hourCostCents === null ? null : $this->revenueCents() - $this->expenseCostCents - $this->hourCostCents;
|
||||
return $this->hourCostCents === null ? null : $this->revenueCents() - $this->expenseCostCents - $this->materialCostCents - $this->hourCostCents;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,6 +81,7 @@ final readonly class Profit
|
||||
$this->expenseRevenueCents + $other->expenseRevenueCents,
|
||||
$this->expenseCostCents + $other->expenseCostCents,
|
||||
$this->hourCostCents === null || $other->hourCostCents === null ? null : $this->hourCostCents + $other->hourCostCents,
|
||||
$this->materialCostCents + $other->materialCostCents,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -100,6 +105,7 @@ final readonly class Profit
|
||||
self::share($this->expenseRevenueCents, $parts, $index),
|
||||
self::share($this->expenseCostCents, $parts, $index),
|
||||
$this->hourCostCents === null ? null : self::share($this->hourCostCents, $parts, $index),
|
||||
self::share($this->materialCostCents, $parts, $index),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Reports\Infrastructure\Adapters;
|
||||
|
||||
use App\Modules\Inventory\Application\Contracts\StockReportQueries;
|
||||
use App\Modules\Machines\Application\Contracts\MachineQueries;
|
||||
use App\Modules\Reports\Application\Contracts\ReportStock;
|
||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||
|
||||
/**
|
||||
* Stock for reports through the Inventory and Machines modules.
|
||||
*/
|
||||
class StockForReports implements ReportStock
|
||||
{
|
||||
public function __construct(
|
||||
private StockReportQueries $stock,
|
||||
private MachineQueries $machines,
|
||||
) {}
|
||||
|
||||
public function usage(ReportPeriod $period): array
|
||||
{
|
||||
return $this->stock->usage($period->from, $period->to);
|
||||
}
|
||||
|
||||
public function valueOn(string $day): array
|
||||
{
|
||||
return $this->stock->valueOn($day);
|
||||
}
|
||||
|
||||
public function materialCostsByJob(ReportPeriod $period): array
|
||||
{
|
||||
return $this->stock->materialCostsByJob($period->from, $period->to);
|
||||
}
|
||||
|
||||
public function machineNames(array $ids): array
|
||||
{
|
||||
$names = [];
|
||||
|
||||
foreach ($this->machines->machines(includeInactive: true) as $machine) {
|
||||
if (in_array($machine->id, $ids, true)) {
|
||||
$names[$machine->id] = $machine->name;
|
||||
}
|
||||
}
|
||||
|
||||
return $names;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Modules\Reports\Infrastructure\Providers;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Reports\Application\Contracts\ReportCatalog;
|
||||
use App\Modules\Reports\Application\Contracts\ReportExpenses;
|
||||
use App\Modules\Reports\Application\Contracts\ReportHours;
|
||||
@@ -10,6 +11,7 @@ use App\Modules\Reports\Application\Contracts\ReportJobs;
|
||||
use App\Modules\Reports\Application\Contracts\ReportPeople;
|
||||
use App\Modules\Reports\Application\Contracts\ReportRevenue;
|
||||
use App\Modules\Reports\Application\Contracts\ReportSales;
|
||||
use App\Modules\Reports\Application\Contracts\ReportStock;
|
||||
use App\Modules\Reports\Infrastructure\Adapters\CatalogForReports;
|
||||
use App\Modules\Reports\Infrastructure\Adapters\ExpensesForReports;
|
||||
use App\Modules\Reports\Infrastructure\Adapters\HoursForReports;
|
||||
@@ -17,6 +19,7 @@ use App\Modules\Reports\Infrastructure\Adapters\JobsForReports;
|
||||
use App\Modules\Reports\Infrastructure\Adapters\PeopleForReports;
|
||||
use App\Modules\Reports\Infrastructure\Adapters\RevenueForReports;
|
||||
use App\Modules\Reports\Infrastructure\Adapters\SalesForReports;
|
||||
use App\Modules\Reports\Infrastructure\Adapters\StockForReports;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Livewire\Livewire;
|
||||
@@ -37,6 +40,7 @@ class ReportsServiceProvider extends ServiceProvider
|
||||
ReportCatalog::class => CatalogForReports::class,
|
||||
ReportPeople::class => PeopleForReports::class,
|
||||
ReportSales::class => SalesForReports::class,
|
||||
ReportStock::class => StockForReports::class,
|
||||
ReportExpenses::class => ExpensesForReports::class,
|
||||
ReportRevenue::class => RevenueForReports::class,
|
||||
];
|
||||
@@ -52,6 +56,8 @@ class ReportsServiceProvider extends ServiceProvider
|
||||
}
|
||||
|
||||
// Reports are for admin and management (spec 2.4).
|
||||
Gate::define('reports.stock', fn (User $user): bool => $user->role->seesAllMoney() && app(CatalogQueries::class)->stockServiceUnitIds() !== []);
|
||||
|
||||
Gate::define('reports.view', fn (User $user): bool => $user->role->seesAllMoney());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ class ProfitabilityCsv implements ReportCsv
|
||||
$profit->workCents,
|
||||
$profit->expenseRevenueCents,
|
||||
$profit->expenseCostCents,
|
||||
$profit->materialCostCents,
|
||||
$profit->hourCostCents ?? '',
|
||||
$profit->marginCents() ?? '',
|
||||
];
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Reports\Presentation\Csv;
|
||||
|
||||
use App\Modules\Inventory\Domain\ValueObjects\StockQuantity;
|
||||
use App\Modules\Reports\Application\UseCases\StockUsageReport;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Stock usage as CSV: a row per item, quantities with a decimal comma and
|
||||
* values in cents (VAT 0).
|
||||
*/
|
||||
class StockUsageCsv implements ReportCsv
|
||||
{
|
||||
public function __construct(private StockUsageReport $report) {}
|
||||
|
||||
public function fileName(Request $request): string
|
||||
{
|
||||
$period = CsvFilters::period($request);
|
||||
|
||||
return __('reports.stock_usage.file').'-'.$period->from.'-'.$period->to;
|
||||
}
|
||||
|
||||
public function headers(): array
|
||||
{
|
||||
return array_values(__('reports.stock_usage.csv'));
|
||||
}
|
||||
|
||||
public function rows(Request $request): iterable
|
||||
{
|
||||
foreach ($this->report->execute(CsvFilters::period($request))->items as $row) {
|
||||
yield [
|
||||
$row->label,
|
||||
(string) $row->group,
|
||||
__('inventory.units.'.$row->unit),
|
||||
StockQuantity::inputValue($row->usedMilli),
|
||||
$row->usedCents,
|
||||
StockQuantity::inputValue($row->writtenOffMilli),
|
||||
$row->writtenOffCents,
|
||||
StockQuantity::inputValue($row->adjustedMilli),
|
||||
$row->adjustedCents,
|
||||
$row->totalCents(),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Reports\Presentation\Csv;
|
||||
|
||||
use App\Modules\Inventory\Domain\ValueObjects\StockQuantity;
|
||||
use App\Modules\Reports\Application\UseCases\StockValueReport;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Stock value of a day as CSV: a row per item, the balance with a decimal
|
||||
* comma and the value in cents (VAT 0).
|
||||
*/
|
||||
class StockValueCsv implements ReportCsv
|
||||
{
|
||||
public function __construct(private StockValueReport $report) {}
|
||||
|
||||
public function fileName(Request $request): string
|
||||
{
|
||||
return __('reports.stock_value.file').'-'.CsvFilters::day($request, 'paiva');
|
||||
}
|
||||
|
||||
public function headers(): array
|
||||
{
|
||||
return array_values(__('reports.stock_value.csv'));
|
||||
}
|
||||
|
||||
public function rows(Request $request): iterable
|
||||
{
|
||||
foreach ($this->report->execute(CsvFilters::day($request, 'paiva'))->items as $item) {
|
||||
yield [
|
||||
$item->itemName,
|
||||
$item->groupName,
|
||||
__('inventory.units.'.$item->unit),
|
||||
StockQuantity::inputValue($item->balanceMilli),
|
||||
$item->valueCents,
|
||||
$item->needsReorder() ? __('inventory.status.reorder') : '',
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,8 @@ use App\Modules\Reports\Presentation\Csv\ProfitabilityCsv;
|
||||
use App\Modules\Reports\Presentation\Csv\ReimbursementsCsv;
|
||||
use App\Modules\Reports\Presentation\Csv\ReportCsv;
|
||||
use App\Modules\Reports\Presentation\Csv\SalesCsv;
|
||||
use App\Modules\Reports\Presentation\Csv\StockUsageCsv;
|
||||
use App\Modules\Reports\Presentation\Csv\StockValueCsv;
|
||||
use App\Modules\Reports\Presentation\Csv\UnbilledCsv;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
@@ -22,7 +24,7 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
class DownloadReport
|
||||
{
|
||||
/** The reports that have a CSV, by their URL key. */
|
||||
public const array REPORTS = ['hours', 'unbilled', 'profitability', 'sales', 'reimbursements'];
|
||||
public const array REPORTS = ['hours', 'unbilled', 'profitability', 'sales', 'reimbursements', 'stock-usage', 'stock-value'];
|
||||
|
||||
public function __invoke(string $report, Request $request): StreamedResponse
|
||||
{
|
||||
@@ -79,6 +81,8 @@ class DownloadReport
|
||||
'profitability' => app(ProfitabilityCsv::class),
|
||||
'sales' => app(SalesCsv::class),
|
||||
'reimbursements' => app(ReimbursementsCsv::class),
|
||||
'stock-usage' => app(StockUsageCsv::class),
|
||||
'stock-value' => app(StockValueCsv::class),
|
||||
default => abort(404),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
@@ -12,5 +12,7 @@ Route::middleware(['web', 'auth', 'can:reports.view'])
|
||||
Route::livewire('/profitability', 'reports::profitability')->name('profitability');
|
||||
Route::livewire('/sales', 'reports::sales')->name('sales');
|
||||
Route::livewire('/reimbursements', 'reports::reimbursements')->name('reimbursements');
|
||||
Route::livewire('/stock-usage', 'reports::stock-usage')->middleware('can:reports.stock')->name('stock-usage');
|
||||
Route::livewire('/stock-value', 'reports::stock-value')->middleware('can:reports.stock')->name('stock-value');
|
||||
Route::get('/{report}/csv', DownloadReport::class)->whereIn('report', DownloadReport::REPORTS)->name('csv');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user