Record used material on tasks and suggest workflow steps' default materials
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
+34
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Catalog\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Catalog Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $workflow_template_step_id
|
||||
* @property int $stock_item_id
|
||||
* @property int $quantity_milli
|
||||
* @property int $sort_order
|
||||
*/
|
||||
#[Table('workflow_template_step_materials')]
|
||||
#[Fillable(['workflow_template_step_id', 'stock_item_id', 'quantity_milli', 'sort_order'])]
|
||||
class WorkflowTemplateStepMaterialModel extends Model
|
||||
{
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'workflow_template_step_id' => 'integer',
|
||||
'stock_item_id' => 'integer',
|
||||
'quantity_milli' => 'integer',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Modules\Catalog\Infrastructure\Persistence\Eloquent;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Catalog Infrastructure layer.
|
||||
@@ -20,6 +21,14 @@ use Illuminate\Database\Eloquent\Model;
|
||||
#[Fillable(['workflow_template_id', 'work_type_id', 'title', 'description', 'sort_order'])]
|
||||
class WorkflowTemplateStepModel extends Model
|
||||
{
|
||||
/**
|
||||
* @return HasMany<WorkflowTemplateStepMaterialModel, $this>
|
||||
*/
|
||||
public function materials(): HasMany
|
||||
{
|
||||
return $this->hasMany(WorkflowTemplateStepMaterialModel::class, 'workflow_template_step_id')->orderBy('sort_order');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
|
||||
+7
@@ -3,10 +3,12 @@
|
||||
namespace App\Modules\Catalog\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\WorkflowTemplateQueries;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowStepMaterialSummary;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowStepSummary;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateDetails;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateSummary;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkflowTemplateModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkflowTemplateStepMaterialModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkflowTemplateStepModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
@@ -93,6 +95,7 @@ class EloquentWorkflowTemplateQueries implements WorkflowTemplateQueries
|
||||
}
|
||||
|
||||
$steps = WorkflowTemplateStepModel::query()
|
||||
->with('materials')
|
||||
->whereIn('workflow_template_id', $models->pluck('id'))
|
||||
->orderBy('sort_order')
|
||||
->get()
|
||||
@@ -119,6 +122,10 @@ class EloquentWorkflowTemplateQueries implements WorkflowTemplateQueries
|
||||
$step->work_type_id,
|
||||
$step->work_type_id ?? $model->work_type_id,
|
||||
$step->work_type_id === null ? $workTypeName : (string) $workTypeNames->get($step->work_type_id),
|
||||
$step->materials
|
||||
->map(fn (WorkflowTemplateStepMaterialModel $material): WorkflowStepMaterialSummary => new WorkflowStepMaterialSummary($material->stock_item_id, $material->quantity_milli))
|
||||
->values()
|
||||
->all(),
|
||||
))
|
||||
->values()
|
||||
->all(),
|
||||
|
||||
+18
-3
@@ -6,15 +6,17 @@ use App\Modules\Catalog\Domain\Contracts\WorkflowTemplateRepository;
|
||||
use App\Modules\Catalog\Domain\Entities\WorkflowTemplate;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\WorkflowStep;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\WorkflowStepMaterial;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\WorkflowStepTitle;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkflowTemplateModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkflowTemplateStepMaterialModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkflowTemplateStepModel;
|
||||
|
||||
class EloquentWorkflowTemplateRepository implements WorkflowTemplateRepository
|
||||
{
|
||||
public function findById(int $id): ?WorkflowTemplate
|
||||
{
|
||||
$model = WorkflowTemplateModel::query()->with('steps')->find($id);
|
||||
$model = WorkflowTemplateModel::query()->with('steps.materials')->find($id);
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
@@ -33,16 +35,25 @@ class EloquentWorkflowTemplateRepository implements WorkflowTemplateRepository
|
||||
WorkflowTemplateStepModel::query()->where('workflow_template_id', $model->id)->delete();
|
||||
|
||||
foreach ($template->steps() as $index => $step) {
|
||||
WorkflowTemplateStepModel::query()->create([
|
||||
$stepModel = WorkflowTemplateStepModel::query()->create([
|
||||
'workflow_template_id' => $model->id,
|
||||
'work_type_id' => $step->workTypeId,
|
||||
'title' => $step->title->value,
|
||||
'description' => $step->description,
|
||||
'sort_order' => $index + 1,
|
||||
]);
|
||||
|
||||
foreach ($step->materials as $materialIndex => $material) {
|
||||
WorkflowTemplateStepMaterialModel::query()->create([
|
||||
'workflow_template_step_id' => $stepModel->id,
|
||||
'stock_item_id' => $material->stockItemId,
|
||||
'quantity_milli' => $material->quantityMilli,
|
||||
'sort_order' => $materialIndex + 1,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->toEntity($model->load('steps'));
|
||||
return $this->toEntity($model->load('steps.materials'));
|
||||
}
|
||||
|
||||
public function isNameTaken(int $workTypeId, string $name, ?int $exceptId = null): bool
|
||||
@@ -66,6 +77,10 @@ class EloquentWorkflowTemplateRepository implements WorkflowTemplateRepository
|
||||
new WorkflowStepTitle($step->title),
|
||||
$step->description,
|
||||
$step->work_type_id,
|
||||
$step->materials
|
||||
->map(fn (WorkflowTemplateStepMaterialModel $material): WorkflowStepMaterial => new WorkflowStepMaterial($material->stock_item_id, $material->quantity_milli))
|
||||
->values()
|
||||
->all(),
|
||||
))
|
||||
->values()
|
||||
->all(),
|
||||
|
||||
Reference in New Issue
Block a user