99 lines
3.7 KiB
PHP
99 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\IndustrialEngineering\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\IndustrialEngineering\Http\Controllers\Concerns\AuthorizesIndustrialEngineering;
|
|
use Modules\IndustrialEngineering\Models\CustomerInstalledAsset;
|
|
use Modules\IndustrialEngineering\Models\SerializedUnit;
|
|
use Modules\IndustrialEngineering\Services\IntegrationBridgeService;
|
|
use Modules\IndustrialEngineering\Services\TraceabilityService;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class InstalledAssetController extends Controller
|
|
{
|
|
use AuthorizesIndustrialEngineering;
|
|
|
|
public function __construct(
|
|
protected TraceabilityService $traceabilityService,
|
|
protected IntegrationBridgeService $integrationBridge
|
|
) {
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorizeIe('ie.installed.view');
|
|
$businessId = $this->businessId();
|
|
|
|
if ($request->ajax()) {
|
|
return DataTables::of(CustomerInstalledAsset::forBusiness($businessId)->with('customer'))
|
|
->addColumn('customer_name', fn ($row) => $row->customer?->name ?? '—')
|
|
->addColumn('action', fn ($row) => '<a href="'.action([self::class, 'show'], $row->id).'" class="btn btn-xs btn-info"><i class="fa fa-building"></i></a>')
|
|
->rawColumns(['action'])
|
|
->make(true);
|
|
}
|
|
|
|
return view('industrialengineering::installed_assets.index');
|
|
}
|
|
|
|
public function lookup(Request $request)
|
|
{
|
|
$this->authorizeIe('ie.installed.view');
|
|
$query = $request->input('q', '');
|
|
$result = null;
|
|
if (! empty($query)) {
|
|
$result = $this->traceabilityService->lookup($this->businessId(), $query);
|
|
}
|
|
|
|
return view('industrialengineering::installed_assets.lookup', compact('result', 'query'));
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$this->authorizeIe('ie.installed.view');
|
|
$asset = CustomerInstalledAsset::forBusiness($this->businessId())
|
|
->with(['customer', 'serializedUnit.components', 'lineRevision', 'warranties', 'maintenanceEquipment', 'commissioningRecords'])
|
|
->findOrFail($id);
|
|
|
|
return view('industrialengineering::installed_assets.show', compact('asset'));
|
|
}
|
|
|
|
public function registerFromUnit(Request $request)
|
|
{
|
|
$this->authorizeIe('ie.installed.create');
|
|
$data = $request->validate([
|
|
'serialized_unit_id' => 'required|exists:ie_serialized_units,id',
|
|
'customer_id' => 'required|exists:contacts,id',
|
|
'asset_code' => 'required|string|max:100',
|
|
'name' => 'required|string|max:255',
|
|
'project_id' => 'nullable|exists:pjt_projects,id',
|
|
]);
|
|
|
|
$unit = SerializedUnit::forBusiness($this->businessId())->findOrFail($data['serialized_unit_id']);
|
|
$asset = $this->traceabilityService->registerInstalledAsset(
|
|
$unit,
|
|
(int) $data['customer_id'],
|
|
$data['asset_code'],
|
|
$data['name'],
|
|
null,
|
|
null,
|
|
$data['project_id'] ?? null
|
|
);
|
|
|
|
$this->integrationBridge->syncToMaintenanceEquipment($asset);
|
|
|
|
return redirect()->action([self::class, 'show'], $asset->id)
|
|
->with('status', ['success' => true, 'msg' => __('industrialengineering::lang.asset_registered')]);
|
|
}
|
|
|
|
public function syncMaintenance($id)
|
|
{
|
|
$this->authorizeIe('ie.installed.update');
|
|
$asset = CustomerInstalledAsset::forBusiness($this->businessId())->findOrFail($id);
|
|
$this->integrationBridge->syncToMaintenanceEquipment($asset);
|
|
|
|
return back()->with('status', ['success' => true, 'msg' => __('industrialengineering::lang.synced_to_maintenance')]);
|
|
}
|
|
}
|