Make quick sale products on machines as orders paid when ready
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
+97
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Machines\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Inventory\Application\Contracts\StockQueries;
|
||||
use App\Modules\Machines\Application\Contracts\ProductionProcessQueries;
|
||||
use App\Modules\Machines\Application\DTOs\ProductionStepSummary;
|
||||
use App\Modules\Machines\Domain\Contracts\ProductionProcessRepository;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Production steps and their materials, stored in production_steps and
|
||||
* production_step_materials.
|
||||
*/
|
||||
class EloquentProductionProcesses implements ProductionProcessQueries, ProductionProcessRepository
|
||||
{
|
||||
public function __construct(
|
||||
private CatalogQueries $catalog,
|
||||
private StockQueries $stock,
|
||||
) {}
|
||||
|
||||
public function replace(int $productId, array $steps): void
|
||||
{
|
||||
DB::table('production_steps')->where('product_id', $productId)->delete();
|
||||
$now = now();
|
||||
|
||||
foreach ($steps as $index => $step) {
|
||||
$stepId = DB::table('production_steps')->insertGetId([
|
||||
'product_id' => $productId,
|
||||
'sort_order' => $index + 1,
|
||||
'machine_id' => $step->machineId,
|
||||
'duration_quarters' => $step->durationQuarters,
|
||||
'work_type_id' => $step->workTypeId,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
|
||||
foreach ($step->materials as $material) {
|
||||
DB::table('production_step_materials')->insert([
|
||||
'production_step_id' => $stepId,
|
||||
'stock_item_id' => $material['stockItemId'],
|
||||
'quantity_milli' => $material['quantityMilli'],
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function processOf(int $productId): array
|
||||
{
|
||||
return $this->processesOf([$productId])[$productId] ?? [];
|
||||
}
|
||||
|
||||
public function processesOf(array $productIds): array
|
||||
{
|
||||
if ($productIds === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$steps = DB::table('production_steps')->whereIn('product_id', $productIds)->orderBy('product_id')->orderBy('sort_order')->get();
|
||||
|
||||
if ($steps->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$materials = DB::table('production_step_materials')->whereIn('production_step_id', $steps->pluck('id'))->orderBy('id')->get()->groupBy('production_step_id');
|
||||
$items = $this->stock->itemsByIds($materials->flatten(1)->pluck('stock_item_id')->map(fn ($id): int => (int) $id)->unique()->values()->all());
|
||||
$machineNames = MachineModel::query()->whereIn('id', $steps->pluck('machine_id')->filter())->pluck('name', 'id');
|
||||
$processes = [];
|
||||
|
||||
foreach ($steps->groupBy('product_id') as $productId => $productSteps) {
|
||||
foreach ($productSteps->values() as $position => $step) {
|
||||
$machineId = $step->machine_id === null ? null : (int) $step->machine_id;
|
||||
|
||||
$processes[(int) $productId][] = new ProductionStepSummary(
|
||||
position: $position,
|
||||
machineId: $machineId,
|
||||
machineName: $machineId === null ? null : (string) $machineNames->get($machineId),
|
||||
durationQuarters: (int) $step->duration_quarters,
|
||||
workTypeId: (int) $step->work_type_id,
|
||||
workTypeName: (string) $this->catalog->findWorkType((int) $step->work_type_id)?->name,
|
||||
materials: ($materials->get($step->id) ?? collect())->map(fn ($material): array => [
|
||||
'stockItemId' => (int) $material->stock_item_id,
|
||||
'itemName' => $items[(int) $material->stock_item_id]->name ?? '',
|
||||
'unit' => $items[(int) $material->stock_item_id]->unit->value ?? 'piece',
|
||||
'quantityMilli' => (int) $material->quantity_milli,
|
||||
])->values()->all(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $processes;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user