93 lines
4.7 KiB
PHP
93 lines
4.7 KiB
PHP
<?php
|
||
|
||
namespace App\Modules\Inventory\Infrastructure\Providers;
|
||
|
||
use App\Models\User;
|
||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||
use App\Modules\Inventory\Application\Contracts\InventoryCountQueries;
|
||
use App\Modules\Inventory\Application\Contracts\StockItemImages;
|
||
use App\Modules\Inventory\Application\Contracts\StockQueries;
|
||
use App\Modules\Inventory\Application\Contracts\StockReportQueries;
|
||
use App\Modules\Inventory\Application\Contracts\StockTasks;
|
||
use App\Modules\Inventory\Application\UseCases\TaskMaterials;
|
||
use App\Modules\Inventory\Domain\Contracts\InventoryCountRepository;
|
||
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\Domain\Contracts\TaskMaterialSuggestionRepository;
|
||
use App\Modules\Inventory\Infrastructure\Adapters\TasksForStock;
|
||
use App\Modules\Inventory\Infrastructure\Adapters\TaskUsageWithStock;
|
||
use App\Modules\Inventory\Infrastructure\Files\StockItemImageFiles;
|
||
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentInventoryCountQueries;
|
||
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentInventoryCountRepository;
|
||
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\Inventory\Infrastructure\Persistence\Repositories\EloquentStockReportQueries;
|
||
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentTaskMaterialSuggestionRepository;
|
||
use App\Modules\Jobs\Application\Contracts\TaskUsage;
|
||
use App\Modules\Jobs\Domain\Events\TaskMaterialsSuggested;
|
||
use App\Modules\Users\Domain\Enums\Role;
|
||
use Illuminate\Support\Facades\Event;
|
||
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,
|
||
TaskMaterialSuggestionRepository::class => EloquentTaskMaterialSuggestionRepository::class,
|
||
StockTasks::class => TasksForStock::class,
|
||
InventoryCountRepository::class => EloquentInventoryCountRepository::class,
|
||
InventoryCountQueries::class => EloquentInventoryCountQueries::class,
|
||
StockReportQueries::class => EloquentStockReportQueries::class,
|
||
];
|
||
|
||
public function register(): void
|
||
{
|
||
// Registered after TimeTracking: a task with material is kept like one with hours.
|
||
$this->app->extend(TaskUsage::class, fn (TaskUsage $usage): TaskUsage => new TaskUsageWithStock($usage));
|
||
}
|
||
|
||
public function boot(): void
|
||
{
|
||
Event::listen(TaskMaterialsSuggested::class, fn (TaskMaterialsSuggested $event) => app(TaskMaterials::class)->suggest($event->taskId, $event->materials));
|
||
|
||
$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() !== []);
|
||
}
|
||
}
|