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:
+40
-2
@@ -6,11 +6,14 @@ use App\Modules\Inventory\Application\Contracts\StockQueries;
|
||||
use App\Modules\Inventory\Application\DTOs\StockGroupSummary;
|
||||
use App\Modules\Inventory\Application\DTOs\StockItemSummary;
|
||||
use App\Modules\Inventory\Application\DTOs\StockMovementSummary;
|
||||
use App\Modules\Inventory\Application\DTOs\TaskMaterialSuggestion;
|
||||
use App\Modules\Inventory\Domain\Enums\StockUnit;
|
||||
use App\Modules\Inventory\Domain\Services\StockSearch;
|
||||
use App\Modules\Inventory\Domain\Services\StockValuation;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemGroupModel;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemModel;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockMovementModel;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\TaskMaterialSuggestionModel;
|
||||
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Carbon;
|
||||
@@ -97,6 +100,36 @@ class EloquentStockQueries implements StockQueries
|
||||
return $this->summaries(collect([$model]), $balance)[0];
|
||||
}
|
||||
|
||||
public function taskMovements(int $taskId): array
|
||||
{
|
||||
$models = StockMovementModel::query()
|
||||
->with('reversal:id,reverses_movement_id')
|
||||
->where('task_id', $taskId)
|
||||
->orderByDesc('id')
|
||||
->get();
|
||||
|
||||
return $this->summaries($models, 0, trackBalance: false);
|
||||
}
|
||||
|
||||
public function taskSuggestions(int $taskId): array
|
||||
{
|
||||
return TaskMaterialSuggestionModel::query()
|
||||
->join('stock_items', 'stock_items.id', '=', 'task_material_suggestions.stock_item_id')
|
||||
->where('task_material_suggestions.task_id', $taskId)
|
||||
->orderBy('task_material_suggestions.id')
|
||||
->get(['task_material_suggestions.*', 'stock_items.name as item_name', 'stock_items.unit as item_unit'])
|
||||
->map(fn (TaskMaterialSuggestionModel $model): TaskMaterialSuggestion => new TaskMaterialSuggestion(
|
||||
$model->id,
|
||||
$model->task_id,
|
||||
$model->stock_item_id,
|
||||
(string) $model->getAttribute('item_name'),
|
||||
StockUnit::from((string) $model->getAttribute('item_unit')),
|
||||
$model->quantity_milli,
|
||||
))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function imagePathOf(int $itemId, bool $thumbnail): ?string
|
||||
{
|
||||
$model = StockItemModel::query()->find($itemId, ['id', 'image_path', 'thumbnail_path']);
|
||||
@@ -147,9 +180,10 @@ class EloquentStockQueries implements StockQueries
|
||||
* @param int $balance The balance after the newest of them.
|
||||
* @return list<StockMovementSummary>
|
||||
*/
|
||||
private function summaries(Collection $models, int $balance): array
|
||||
private function summaries(Collection $models, int $balance, bool $trackBalance = true): array
|
||||
{
|
||||
$names = $this->users->namesByIds($models->pluck('user_id')->unique()->values()->all());
|
||||
$items = StockItemModel::query()->whereIn('id', $models->pluck('stock_item_id')->unique())->get(['id', 'name', 'unit'])->keyBy('id');
|
||||
$summaries = [];
|
||||
|
||||
foreach ($models as $model) {
|
||||
@@ -175,9 +209,13 @@ class EloquentStockQueries implements StockQueries
|
||||
documentNumber: $model->document_number,
|
||||
reversesMovementId: $model->reverses_movement_id,
|
||||
reversedByMovementId: $model->reversal?->id,
|
||||
itemName: (string) $items->get($model->stock_item_id)?->name,
|
||||
unit: $items->get($model->stock_item_id)?->unit,
|
||||
);
|
||||
|
||||
$balance -= $model->quantity_milli;
|
||||
if ($trackBalance) {
|
||||
$balance -= $model->quantity_milli;
|
||||
}
|
||||
}
|
||||
|
||||
return $summaries;
|
||||
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Inventory\Domain\Contracts\TaskMaterialSuggestionRepository;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\TaskMaterialSuggestionModel;
|
||||
|
||||
class EloquentTaskMaterialSuggestionRepository implements TaskMaterialSuggestionRepository
|
||||
{
|
||||
public function add(int $taskId, int $stockItemId, int $quantityMilli): void
|
||||
{
|
||||
TaskMaterialSuggestionModel::query()->create([
|
||||
'task_id' => $taskId,
|
||||
'stock_item_id' => $stockItemId,
|
||||
'quantity_milli' => $quantityMilli,
|
||||
]);
|
||||
}
|
||||
|
||||
public function find(int $id): ?array
|
||||
{
|
||||
$model = TaskMaterialSuggestionModel::query()->find($id);
|
||||
|
||||
return $model === null ? null : [
|
||||
'id' => $model->id,
|
||||
'taskId' => $model->task_id,
|
||||
'stockItemId' => $model->stock_item_id,
|
||||
'quantityMilli' => $model->quantity_milli,
|
||||
];
|
||||
}
|
||||
|
||||
public function remove(int $id): void
|
||||
{
|
||||
TaskMaterialSuggestionModel::query()->whereKey($id)->delete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user