Add stock items, groups and movements at a moving average cost
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Infrastructure\Files;
|
||||
|
||||
use App\Modules\Inventory\Application\Contracts\StockItemImages;
|
||||
use App\Modules\Inventory\Domain\Exceptions\InvalidStockImage;
|
||||
use App\Modules\Shared\Application\Contracts\ImageResizer;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Stock item images on the private local disk under stock-items/: an
|
||||
* upright JPEG without metadata and a thumbnail for touch-screen buttons.
|
||||
*/
|
||||
class StockItemImageFiles implements StockItemImages
|
||||
{
|
||||
public const string DISK = 'local';
|
||||
|
||||
public const int MAX_SIZE = 1280;
|
||||
|
||||
public const int THUMBNAIL_SIZE = 240;
|
||||
|
||||
public function __construct(private ImageResizer $resizer) {}
|
||||
|
||||
public function store(string $uploadedFilePath, int $itemId): array
|
||||
{
|
||||
$versions = $this->resizer->jpegVersions($uploadedFilePath, [self::MAX_SIZE, self::THUMBNAIL_SIZE]) ?? throw new InvalidStockImage;
|
||||
$name = 'stock-items/'.$itemId.'/'.Str::uuid()->toString();
|
||||
|
||||
Storage::disk(self::DISK)->put($name.'.jpg', $versions[0]['bytes']);
|
||||
Storage::disk(self::DISK)->put($name.'-thumb.jpg', $versions[1]['bytes']);
|
||||
|
||||
return ['path' => $name.'.jpg', 'thumbnailPath' => $name.'-thumb.jpg'];
|
||||
}
|
||||
|
||||
public function delete(array $paths): void
|
||||
{
|
||||
if ($paths !== []) {
|
||||
Storage::disk(self::DISK)->delete($paths);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use Database\Factories\StockItemGroupFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Attributes\UseFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Inventory Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property int $sort_order
|
||||
* @property bool $is_active
|
||||
*/
|
||||
#[Table('stock_item_groups')]
|
||||
#[Fillable(['name', 'sort_order', 'is_active'])]
|
||||
#[UseFactory(StockItemGroupFactory::class)]
|
||||
class StockItemGroupModel extends Model
|
||||
{
|
||||
/** @use HasFactory<StockItemGroupFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return HasMany<StockItemModel, $this>
|
||||
*/
|
||||
public function items(): HasMany
|
||||
{
|
||||
return $this->hasMany(StockItemModel::class, 'stock_item_group_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'sort_order' => 'integer',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use App\Modules\Inventory\Domain\Enums\StockUnit;
|
||||
use Database\Factories\StockItemFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Attributes\UseFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Inventory Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $stock_item_group_id
|
||||
* @property string $name
|
||||
* @property StockUnit $unit
|
||||
* @property string|null $code
|
||||
* @property string|null $supplier
|
||||
* @property int|null $reorder_level_milli
|
||||
* @property int $average_cost_hundredths
|
||||
* @property string|null $image_path
|
||||
* @property string|null $thumbnail_path
|
||||
* @property bool $is_active
|
||||
* @property int|null $balance_milli Selected sum of the movements.
|
||||
*/
|
||||
#[Table('stock_items')]
|
||||
#[Fillable(['stock_item_group_id', 'name', 'unit', 'code', 'supplier', 'reorder_level_milli', 'average_cost_hundredths', 'image_path', 'thumbnail_path', 'is_active'])]
|
||||
#[UseFactory(StockItemFactory::class)]
|
||||
class StockItemModel extends Model
|
||||
{
|
||||
/** @use HasFactory<StockItemFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return BelongsTo<StockItemGroupModel, $this>
|
||||
*/
|
||||
public function group(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(StockItemGroupModel::class, 'stock_item_group_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'stock_item_group_id' => 'integer',
|
||||
'unit' => StockUnit::class,
|
||||
'reorder_level_milli' => 'integer',
|
||||
'average_cost_hundredths' => 'integer',
|
||||
'is_active' => 'boolean',
|
||||
'balance_milli' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use App\Modules\Inventory\Domain\Enums\StockMovementKind;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Inventory Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $stock_item_id
|
||||
* @property StockMovementKind $kind
|
||||
* @property int $quantity_milli
|
||||
* @property int $unit_cost_hundredths
|
||||
* @property int $value_cents
|
||||
* @property Carbon $occurred_on
|
||||
* @property Carbon $recorded_at
|
||||
* @property int $user_id
|
||||
* @property int|null $task_id
|
||||
* @property int|null $customer_job_id
|
||||
* @property int|null $machine_id
|
||||
* @property int|null $quick_sale_order_line_id
|
||||
* @property int|null $maintenance_record_id
|
||||
* @property int|null $inventory_count_id
|
||||
* @property int|null $reverses_movement_id
|
||||
* @property string|null $reason
|
||||
* @property string|null $supplier
|
||||
* @property string|null $document_number
|
||||
*/
|
||||
#[Table('stock_movements')]
|
||||
#[Fillable([
|
||||
'stock_item_id', 'kind', 'quantity_milli', 'unit_cost_hundredths', 'value_cents', 'occurred_on', 'recorded_at', 'user_id',
|
||||
'task_id', 'customer_job_id', 'machine_id', 'quick_sale_order_line_id', 'maintenance_record_id', 'inventory_count_id',
|
||||
'reverses_movement_id', 'reason', 'supplier', 'document_number',
|
||||
])]
|
||||
class StockMovementModel extends Model
|
||||
{
|
||||
/**
|
||||
* @return HasOne<StockMovementModel, $this>
|
||||
*/
|
||||
public function reversal(): HasOne
|
||||
{
|
||||
return $this->hasOne(StockMovementModel::class, 'reverses_movement_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'stock_item_id' => 'integer',
|
||||
'kind' => StockMovementKind::class,
|
||||
'quantity_milli' => 'integer',
|
||||
'unit_cost_hundredths' => 'integer',
|
||||
'value_cents' => 'integer',
|
||||
'occurred_on' => 'immutable_date',
|
||||
'recorded_at' => 'immutable_datetime',
|
||||
'user_id' => 'integer',
|
||||
'task_id' => 'integer',
|
||||
'customer_job_id' => 'integer',
|
||||
'machine_id' => 'integer',
|
||||
'quick_sale_order_line_id' => 'integer',
|
||||
'maintenance_record_id' => 'integer',
|
||||
'inventory_count_id' => 'integer',
|
||||
'reverses_movement_id' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Inventory\Domain\Contracts\StockItemGroupRepository;
|
||||
use App\Modules\Inventory\Domain\Entities\StockItemGroup;
|
||||
use App\Modules\Inventory\Domain\ValueObjects\StockName;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemGroupModel;
|
||||
|
||||
class EloquentStockItemGroupRepository implements StockItemGroupRepository
|
||||
{
|
||||
public function findById(int $id): ?StockItemGroup
|
||||
{
|
||||
$model = StockItemGroupModel::query()->find($id);
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function save(StockItemGroup $group): StockItemGroup
|
||||
{
|
||||
$model = $group->id() === null ? new StockItemGroupModel : StockItemGroupModel::query()->findOrFail($group->id());
|
||||
|
||||
$model->fill([
|
||||
'name' => $group->name()->value,
|
||||
'sort_order' => $group->sortOrder(),
|
||||
'is_active' => $group->isActive(),
|
||||
])->save();
|
||||
|
||||
return $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function isNameTaken(string $name, ?int $exceptId = null): bool
|
||||
{
|
||||
return StockItemGroupModel::query()
|
||||
->whereRaw('LOWER(name) = ?', [mb_strtolower($name)])
|
||||
->when($exceptId !== null, fn ($query) => $query->whereKeyNot($exceptId))
|
||||
->exists();
|
||||
}
|
||||
|
||||
public function nextSortOrder(): int
|
||||
{
|
||||
return (int) StockItemGroupModel::query()->max('sort_order') + 1;
|
||||
}
|
||||
|
||||
public function neighbour(StockItemGroup $group, bool $up): ?StockItemGroup
|
||||
{
|
||||
$model = StockItemGroupModel::query()
|
||||
->where('sort_order', $up ? '<' : '>', $group->sortOrder())
|
||||
->orderBy('sort_order', $up ? 'desc' : 'asc')
|
||||
->first();
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
|
||||
private function toEntity(StockItemGroupModel $model): StockItemGroup
|
||||
{
|
||||
return StockItemGroup::reconstitute($model->id, new StockName($model->name), $model->sort_order, $model->is_active);
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Inventory\Domain\Contracts\StockItemRepository;
|
||||
use App\Modules\Inventory\Domain\Entities\StockItem;
|
||||
use App\Modules\Inventory\Domain\ValueObjects\StockName;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemModel;
|
||||
|
||||
class EloquentStockItemRepository implements StockItemRepository
|
||||
{
|
||||
public function findById(int $id): ?StockItem
|
||||
{
|
||||
$model = StockItemModel::query()->find($id);
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function lockById(int $id): ?StockItem
|
||||
{
|
||||
$model = StockItemModel::query()->lockForUpdate()->find($id);
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function save(StockItem $item): StockItem
|
||||
{
|
||||
$model = $item->id() === null ? new StockItemModel : StockItemModel::query()->findOrFail($item->id());
|
||||
|
||||
$model->fill([
|
||||
'stock_item_group_id' => $item->groupId(),
|
||||
'name' => $item->name()->value,
|
||||
'unit' => $item->unit(),
|
||||
'code' => $item->code(),
|
||||
'supplier' => $item->supplier(),
|
||||
'reorder_level_milli' => $item->reorderLevelMilli(),
|
||||
'average_cost_hundredths' => $item->averageCostHundredths(),
|
||||
'image_path' => $item->imagePath(),
|
||||
'thumbnail_path' => $item->thumbnailPath(),
|
||||
'is_active' => $item->isActive(),
|
||||
])->save();
|
||||
|
||||
return $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function isNameTaken(string $name, ?int $exceptId = null): bool
|
||||
{
|
||||
return StockItemModel::query()
|
||||
->whereRaw('LOWER(name) = ?', [mb_strtolower($name)])
|
||||
->when($exceptId !== null, fn ($query) => $query->whereKeyNot($exceptId))
|
||||
->exists();
|
||||
}
|
||||
|
||||
private function toEntity(StockItemModel $model): StockItem
|
||||
{
|
||||
return StockItem::reconstitute(
|
||||
$model->id,
|
||||
$model->stock_item_group_id,
|
||||
new StockName($model->name),
|
||||
$model->unit,
|
||||
$model->code,
|
||||
$model->supplier,
|
||||
$model->reorder_level_milli,
|
||||
$model->average_cost_hundredths,
|
||||
$model->image_path,
|
||||
$model->thumbnail_path,
|
||||
$model->is_active,
|
||||
);
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Inventory\Domain\Contracts\StockMovementRepository;
|
||||
use App\Modules\Inventory\Domain\Entities\StockMovement;
|
||||
use App\Modules\Inventory\Domain\ValueObjects\StockUseTarget;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockMovementModel;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class EloquentStockMovementRepository implements StockMovementRepository
|
||||
{
|
||||
public function add(StockMovement $movement): StockMovement
|
||||
{
|
||||
$target = $movement->target();
|
||||
|
||||
$model = StockMovementModel::query()->create([
|
||||
'stock_item_id' => $movement->itemId(),
|
||||
'kind' => $movement->kind(),
|
||||
'quantity_milli' => $movement->quantityMilli(),
|
||||
'unit_cost_hundredths' => $movement->unitCostHundredths(),
|
||||
'value_cents' => $movement->valueCents(),
|
||||
'occurred_on' => $movement->occurredOn()->format('Y-m-d'),
|
||||
'recorded_at' => $movement->recordedAt()->format('Y-m-d H:i:s'),
|
||||
'user_id' => $movement->userId(),
|
||||
'task_id' => $target?->taskId,
|
||||
'customer_job_id' => $target?->jobId,
|
||||
'machine_id' => $target?->machineId,
|
||||
'quick_sale_order_line_id' => $target?->orderLineId,
|
||||
'maintenance_record_id' => $target?->maintenanceRecordId,
|
||||
'inventory_count_id' => $movement->inventoryCountId(),
|
||||
'reverses_movement_id' => $movement->reversesMovementId(),
|
||||
'reason' => $movement->reason(),
|
||||
'supplier' => $movement->supplier(),
|
||||
'document_number' => $movement->documentNumber(),
|
||||
]);
|
||||
|
||||
return $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function findById(int $id): ?StockMovement
|
||||
{
|
||||
$model = StockMovementModel::query()->find($id);
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function isReversed(int $movementId): bool
|
||||
{
|
||||
return StockMovementModel::query()->where('reverses_movement_id', $movementId)->exists();
|
||||
}
|
||||
|
||||
public function balanceOf(int $itemId): int
|
||||
{
|
||||
return (int) StockMovementModel::query()->where('stock_item_id', $itemId)->sum('quantity_milli');
|
||||
}
|
||||
|
||||
private function toEntity(StockMovementModel $model): StockMovement
|
||||
{
|
||||
$hasTarget = $model->task_id !== null || $model->machine_id !== null || $model->quick_sale_order_line_id !== null
|
||||
|| $model->maintenance_record_id !== null || ($model->reason !== null && $model->inventory_count_id === null);
|
||||
|
||||
return StockMovement::reconstitute(
|
||||
$model->id,
|
||||
$model->stock_item_id,
|
||||
$model->kind,
|
||||
$model->quantity_milli,
|
||||
$model->unit_cost_hundredths,
|
||||
$model->value_cents,
|
||||
DateTimeImmutable::createFromInterface($model->occurred_on),
|
||||
DateTimeImmutable::createFromInterface($model->recorded_at),
|
||||
$model->user_id,
|
||||
$hasTarget ? new StockUseTarget($model->task_id, $model->customer_job_id, $model->machine_id, $model->quick_sale_order_line_id, $model->maintenance_record_id, $model->reason) : null,
|
||||
$model->reason,
|
||||
$model->supplier,
|
||||
$model->document_number,
|
||||
$model->inventory_count_id,
|
||||
$model->reverses_movement_id,
|
||||
);
|
||||
}
|
||||
}
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Infrastructure\Persistence\Repositories;
|
||||
|
||||
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\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\Users\Application\Contracts\UserAccountQueries;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class EloquentStockQueries implements StockQueries
|
||||
{
|
||||
public function __construct(private UserAccountQueries $users) {}
|
||||
|
||||
public function groups(bool $includeInactive = true): array
|
||||
{
|
||||
return StockItemGroupModel::query()
|
||||
->when(! $includeInactive, fn (Builder $query) => $query->where('is_active', true))
|
||||
->withCount('items')
|
||||
->orderBy('sort_order')
|
||||
->get()
|
||||
->map(fn (StockItemGroupModel $model): StockGroupSummary => new StockGroupSummary($model->id, $model->name, $model->is_active, (int) $model->getAttribute('items_count')))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function items(?string $search = null, ?int $groupId = null, bool $onlyReorder = false, bool $includeInactive = false): array
|
||||
{
|
||||
$search = trim((string) $search);
|
||||
|
||||
return $this->itemQuery()
|
||||
->when($groupId !== null, fn (Builder $query) => $query->where('stock_items.stock_item_group_id', $groupId))
|
||||
->when(! $includeInactive, fn (Builder $query) => $query->where('stock_items.is_active', true))
|
||||
->get()
|
||||
->filter(fn (StockItemModel $model): bool => $search === '' || StockSearch::matches($search, $model->name, $model->code))
|
||||
->map(fn (StockItemModel $model): StockItemSummary => $this->toSummary($model))
|
||||
->filter(fn (StockItemSummary $item): bool => ! $onlyReorder || $item->needsReorder())
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function findItem(int $id): ?StockItemSummary
|
||||
{
|
||||
$model = $this->itemQuery()->where('stock_items.id', $id)->first();
|
||||
|
||||
return $model === null ? null : $this->toSummary($model);
|
||||
}
|
||||
|
||||
public function itemsByIds(array $ids): array
|
||||
{
|
||||
if ($ids === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->itemQuery()
|
||||
->whereIn('stock_items.id', $ids)
|
||||
->get()
|
||||
->mapWithKeys(fn (StockItemModel $model): array => [$model->id => $this->toSummary($model)])
|
||||
->all();
|
||||
}
|
||||
|
||||
public function movementsOf(int $itemId, int $limit = 200): array
|
||||
{
|
||||
$models = StockMovementModel::query()
|
||||
->with('reversal:id,reverses_movement_id')
|
||||
->where('stock_item_id', $itemId)
|
||||
->orderByDesc('id')
|
||||
->limit($limit)
|
||||
->get();
|
||||
|
||||
$balance = (int) StockMovementModel::query()->where('stock_item_id', $itemId)->sum('quantity_milli');
|
||||
|
||||
return $this->summaries($models, $balance);
|
||||
}
|
||||
|
||||
public function findMovement(int $id): ?StockMovementSummary
|
||||
{
|
||||
$model = StockMovementModel::query()->with('reversal:id,reverses_movement_id')->find($id);
|
||||
|
||||
if ($model === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$balance = (int) StockMovementModel::query()
|
||||
->where('stock_item_id', $model->stock_item_id)
|
||||
->where('id', '<=', $model->id)
|
||||
->sum('quantity_milli');
|
||||
|
||||
return $this->summaries(collect([$model]), $balance)[0];
|
||||
}
|
||||
|
||||
public function imagePathOf(int $itemId, bool $thumbnail): ?string
|
||||
{
|
||||
$model = StockItemModel::query()->find($itemId, ['id', 'image_path', 'thumbnail_path']);
|
||||
|
||||
return $thumbnail ? $model?->thumbnail_path : $model?->image_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Builder<StockItemModel>
|
||||
*/
|
||||
private function itemQuery(): Builder
|
||||
{
|
||||
return StockItemModel::query()
|
||||
->join('stock_item_groups', 'stock_item_groups.id', '=', 'stock_items.stock_item_group_id')
|
||||
->select('stock_items.*')
|
||||
->addSelect('stock_item_groups.name as group_name')
|
||||
->selectSub(
|
||||
StockMovementModel::query()->selectRaw('COALESCE(SUM(quantity_milli), 0)')->whereColumn('stock_movements.stock_item_id', 'stock_items.id'),
|
||||
'balance_milli',
|
||||
)
|
||||
->orderBy('stock_item_groups.sort_order')
|
||||
->orderBy('stock_items.name');
|
||||
}
|
||||
|
||||
private function toSummary(StockItemModel $model): StockItemSummary
|
||||
{
|
||||
$balance = (int) $model->getAttribute('balance_milli');
|
||||
|
||||
return new StockItemSummary(
|
||||
id: $model->id,
|
||||
groupId: $model->stock_item_group_id,
|
||||
groupName: (string) $model->getAttribute('group_name'),
|
||||
name: $model->name,
|
||||
unit: $model->unit,
|
||||
code: $model->code,
|
||||
supplier: $model->supplier,
|
||||
reorderLevelMilli: $model->reorder_level_milli,
|
||||
balanceMilli: $balance,
|
||||
averageCostHundredths: $model->average_cost_hundredths,
|
||||
valueCents: StockValuation::valueCents($balance, $model->average_cost_hundredths),
|
||||
hasImage: $model->thumbnail_path !== null,
|
||||
isActive: $model->is_active,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, StockMovementModel> $models Newest first.
|
||||
* @param int $balance The balance after the newest of them.
|
||||
* @return list<StockMovementSummary>
|
||||
*/
|
||||
private function summaries(Collection $models, int $balance): array
|
||||
{
|
||||
$names = $this->users->namesByIds($models->pluck('user_id')->unique()->values()->all());
|
||||
$summaries = [];
|
||||
|
||||
foreach ($models as $model) {
|
||||
$summaries[] = new StockMovementSummary(
|
||||
id: $model->id,
|
||||
itemId: $model->stock_item_id,
|
||||
kind: $model->kind,
|
||||
quantityMilli: $model->quantity_milli,
|
||||
balanceAfterMilli: $balance,
|
||||
valueCents: $model->value_cents,
|
||||
occurredOn: $model->occurred_on->format('Y-m-d'),
|
||||
recordedAt: Carbon::instance($model->recorded_at)->setTimezone((string) config('app.business_timezone'))->format('Y-m-d H:i'),
|
||||
userId: $model->user_id,
|
||||
userName: $names[$model->user_id] ?? '',
|
||||
taskId: $model->task_id,
|
||||
jobId: $model->customer_job_id,
|
||||
machineId: $model->machine_id,
|
||||
orderLineId: $model->quick_sale_order_line_id,
|
||||
maintenanceRecordId: $model->maintenance_record_id,
|
||||
inventoryCountId: $model->inventory_count_id,
|
||||
reason: $model->reason,
|
||||
supplier: $model->supplier,
|
||||
documentNumber: $model->document_number,
|
||||
reversesMovementId: $model->reverses_movement_id,
|
||||
reversedByMovementId: $model->reversal?->id,
|
||||
);
|
||||
|
||||
$balance -= $model->quantity_milli;
|
||||
}
|
||||
|
||||
return $summaries;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Infrastructure\Providers;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Inventory\Application\Contracts\StockItemImages;
|
||||
use App\Modules\Inventory\Application\Contracts\StockQueries;
|
||||
use App\Modules\Inventory\Domain\Contracts\StockItemGroupRepository;
|
||||
use App\Modules\Inventory\Domain\Contracts\StockItemRepository;
|
||||
use App\Modules\Inventory\Domain\Contracts\StockMovementRepository;
|
||||
use App\Modules\Inventory\Infrastructure\Files\StockItemImageFiles;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentStockItemGroupRepository;
|
||||
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\Users\Domain\Enums\Role;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Livewire\Livewire;
|
||||
|
||||
/**
|
||||
* Wires the Inventory module (stock items, movements and stock counts,
|
||||
* spec 19.1–19.3) into Laravel: bindings, routes, the `inventory::`
|
||||
* Livewire namespace and the `inventory.*` abilities (spec 19.9).
|
||||
*/
|
||||
class InventoryServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* @var array<class-string, class-string>
|
||||
*/
|
||||
public array $bindings = [
|
||||
StockItemGroupRepository::class => EloquentStockItemGroupRepository::class,
|
||||
StockItemRepository::class => EloquentStockItemRepository::class,
|
||||
StockMovementRepository::class => EloquentStockMovementRepository::class,
|
||||
StockQueries::class => EloquentStockQueries::class,
|
||||
StockItemImages::class => StockItemImageFiles::class,
|
||||
];
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$presentationPath = dirname(__DIR__, 2).'/Presentation';
|
||||
|
||||
Livewire::addNamespace('inventory', viewPath: $presentationPath.'/Livewire');
|
||||
|
||||
if (! $this->app->routesAreCached()) {
|
||||
$this->loadRoutesFrom($presentationPath.'/Routes/web.php');
|
||||
}
|
||||
|
||||
$staff = fn (User $user): bool => $user->role !== Role::Subcontractor;
|
||||
$managers = fn (User $user): bool => in_array($user->role, [Role::Admin, Role::Management], true);
|
||||
|
||||
Gate::define('inventory.view', $staff);
|
||||
Gate::define('inventory.record-use', $staff);
|
||||
Gate::define('inventory.count', $staff);
|
||||
Gate::define('inventory.manage', $managers);
|
||||
Gate::define('inventory.manage-counts', $managers);
|
||||
Gate::define('inventory.reverse-any', $managers);
|
||||
Gate::define('inventory.see-value', $managers);
|
||||
|
||||
// The menu shows stock pages only when a service unit uses stock (spec 19).
|
||||
Gate::define('inventory.menu', fn (User $user): bool => $staff($user) && app(CatalogQueries::class)->stockServiceUnitIds() !== []);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user