102 lines
4.0 KiB
PHP
102 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace Modules\Project\Http\Controllers;
|
|
|
|
use App\Utils\ModuleUtil;
|
|
use App\Utils\Util;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\Project\Entities\Project;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class ProjectEquipmentController extends Controller
|
|
{
|
|
public function __construct(
|
|
protected Util $commonUtil,
|
|
protected ModuleUtil $moduleUtil
|
|
) {}
|
|
|
|
public function equipment(Request $request, $project_id)
|
|
{
|
|
$business_id = (int) request()->session()->get('user.business_id');
|
|
$this->authorizeProjectAccess($business_id, (int) $project_id);
|
|
|
|
if (! $request->ajax()
|
|
|| ! class_exists(\Modules\Maintenance\Models\Equipment::class)
|
|
|| ! $this->moduleUtil->isModuleInstalled('Maintenance')) {
|
|
abort(404);
|
|
}
|
|
|
|
$query = \Modules\Maintenance\Models\Equipment::where('business_id', $business_id)
|
|
->where('project_id', $project_id)
|
|
->with(['category:id,name', 'productionLine:id,name'])
|
|
->orderByDesc('created_at');
|
|
|
|
$util = app(\Modules\Maintenance\Utils\MaintenanceUtil::class);
|
|
|
|
return DataTables::of($query)
|
|
->addColumn('category_name', fn ($row) => $row->category?->name ?? '—')
|
|
->addColumn('line_name', fn ($row) => $row->productionLine?->name ?? '—')
|
|
->addColumn('status_label', fn ($row) => $util->statusLabel($row->status))
|
|
->addColumn('action', function ($row) {
|
|
if (! auth()->user()->can('maintenance.equipment.view')) {
|
|
return '—';
|
|
}
|
|
|
|
return '<a href="'.action(
|
|
[\Modules\Maintenance\Http\Controllers\EquipmentController::class, 'show'],
|
|
[$row->id]
|
|
).'" class="btn btn-xs btn-info"><i class="fa fa-eye"></i></a>';
|
|
})
|
|
->rawColumns(['action'])
|
|
->make(true);
|
|
}
|
|
|
|
public function assets(Request $request, $project_id)
|
|
{
|
|
$business_id = (int) request()->session()->get('user.business_id');
|
|
$this->authorizeProjectAccess($business_id, (int) $project_id);
|
|
|
|
if (! $request->ajax()
|
|
|| ! class_exists(\Modules\AssetManagement\Entities\Asset::class)
|
|
|| ! $this->moduleUtil->isModuleInstalled('AssetManagement')) {
|
|
abort(404);
|
|
}
|
|
|
|
$query = \Modules\AssetManagement\Entities\Asset::where('business_id', $business_id)
|
|
->where('project_id', $project_id)
|
|
->orderByDesc('created_at');
|
|
|
|
return DataTables::of($query)
|
|
->editColumn('unit_price', function ($row) {
|
|
return '<span class="display_currency" data-currency_symbol="true" data-orig-value="'.$row->unit_price.'">'.$row->unit_price.'</span>';
|
|
})
|
|
->editColumn('quantity', fn ($row) => $this->commonUtil->num_f($row->quantity, false, null, true))
|
|
->addColumn('action', function ($row) {
|
|
if (! auth()->user()->can('asset.view') && ! auth()->user()->can('asset.view_all_maintenance')) {
|
|
return '—';
|
|
}
|
|
|
|
return '<a href="'.url('/asset/assets').'?project_id='.$row->project_id.'" class="btn btn-xs btn-info"><i class="fa fa-external-link-alt"></i></a>';
|
|
})
|
|
->rawColumns(['action', 'unit_price'])
|
|
->make(true);
|
|
}
|
|
|
|
protected function authorizeProjectAccess(int $business_id, int $project_id): void
|
|
{
|
|
if (! (auth()->user()->can('superadmin') || $this->moduleUtil->hasThePermissionInSubscription($business_id, 'project_module'))) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
$query = Project::where('business_id', $business_id)->where('id', $project_id);
|
|
|
|
if (! $this->commonUtil->is_admin(auth()->user(), $business_id)) {
|
|
$user_id = auth()->user()->id;
|
|
$query->whereHas('members', fn ($q) => $q->where('user_id', $user_id));
|
|
}
|
|
|
|
$query->firstOrFail();
|
|
}
|
|
}
|