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,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())),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user