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
|
||||
|
||||
Reference in New Issue
Block a user