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