ultimatepos/Modules/IndustrialEngineering/Http/Controllers/ResearchProjectController.php

85 lines
2.9 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\CompetitiveProduct;
use Modules\IndustrialEngineering\Models\ResearchProject;
use Yajra\DataTables\Facades\DataTables;
class ResearchProjectController extends Controller
{
use AuthorizesIndustrialEngineering;
public function index(Request $request)
{
$this->authorizeIe('ie.research.view');
$businessId = $this->businessId();
if ($request->ajax()) {
return DataTables::of(ResearchProject::forBusiness($businessId))
->addColumn('action', fn ($row) => '<a href="'.action([self::class, 'show'], $row->id).'" class="btn btn-xs btn-info"><i class="fa fa-flask"></i></a>')
->rawColumns(['action'])
->make(true);
}
return view('industrialengineering::research.index');
}
public function create()
{
$this->authorizeIe('ie.research.create');
return view('industrialengineering::research.create');
}
public function store(Request $request)
{
$this->authorizeIe('ie.research.create');
$data = $request->validate([
'project_code' => 'required|string|max:100',
'name' => 'required|string|max:255',
'research_type' => 'required|string',
'objective' => 'nullable|string',
]);
$data['business_id'] = $this->businessId();
$data['lead_researcher_id'] = auth()->id();
$project = ResearchProject::create($data);
return redirect()->action([self::class, 'show'], $project->id)
->with('status', ['success' => true, 'msg' => __('industrialengineering::lang.saved')]);
}
public function show($id)
{
$this->authorizeIe('ie.research.view');
$project = ResearchProject::forBusiness($this->businessId())
->with(['competitiveProducts.structures', 'benchmarks', 'findings'])
->findOrFail($id);
return view('industrialengineering::research.show', compact('project'));
}
public function addCompetitor(Request $request, $id)
{
$this->authorizeIe('ie.research.create');
$project = ResearchProject::forBusiness($this->businessId())->findOrFail($id);
$data = $request->validate([
'competitor_name' => 'required|string|max:255',
'product_name' => 'required|string|max:255',
'estimated_price' => 'nullable|numeric|min:0',
'notes' => 'nullable|string',
]);
$data['business_id'] = $this->businessId();
$data['research_project_id'] = $project->id;
CompetitiveProduct::create($data);
return back()->with('status', ['success' => true, 'msg' => __('industrialengineering::lang.competitor_added')]);
}
}