126 lines
4.6 KiB
PHP
126 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Tms\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\Tms\Http\Controllers\Concerns\AuthorizesTms;
|
|
use Modules\Tms\Models\CustomsBroker;
|
|
use Modules\Tms\Utils\TmsUtil;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class CustomsBrokerController extends Controller
|
|
{
|
|
use AuthorizesTms;
|
|
|
|
public function __construct(protected TmsUtil $tmsUtil) {}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorizeTmsAccess('tms.brokers.view');
|
|
$business_id = $this->tmsUtil->getBusinessId();
|
|
|
|
if ($request->ajax()) {
|
|
$query = CustomsBroker::where('business_id', $business_id)
|
|
->with('contact:id,name')
|
|
->orderBy('name');
|
|
|
|
if ($request->boolean('active_only')) {
|
|
$query->where('is_active', true);
|
|
}
|
|
|
|
return DataTables::of($query)
|
|
->addColumn('contact_name', fn ($row) => $row->contact?->name ?? '—')
|
|
->editColumn('is_active', fn ($row) => $row->is_active ? __('tms::lang.active') : __('tms::lang.inactive'))
|
|
->addColumn('action', function ($row) {
|
|
$html = '<div class="btn-group">';
|
|
if (auth()->user()->can('tms.brokers.update')) {
|
|
$html .= '<button type="button" data-href="'.action([self::class, 'edit'], [$row->id]).'" class="btn btn-xs btn-primary btn-modal" data-container=".tms_broker_modal"><i class="glyphicon glyphicon-edit"></i></button>';
|
|
}
|
|
if (auth()->user()->can('tms.brokers.delete')) {
|
|
$html .= ' <button type="button" data-href="'.action([self::class, 'destroy'], [$row->id]).'" class="btn btn-xs btn-danger delete_tms_broker"><i class="glyphicon glyphicon-trash"></i></button>';
|
|
}
|
|
$html .= '</div>';
|
|
|
|
return $html;
|
|
})
|
|
->rawColumns(['action'])
|
|
->make(true);
|
|
}
|
|
|
|
return view('tms::brokers.index');
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->authorizeTmsAccess('tms.brokers.create');
|
|
$business_id = $this->tmsUtil->getBusinessId();
|
|
|
|
return view('tms::brokers.create', [
|
|
'suppliers' => $this->tmsUtil->getSuppliersDropdown($business_id),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$this->authorizeTmsAccess('tms.brokers.create');
|
|
$business_id = $this->tmsUtil->getBusinessId();
|
|
|
|
$data = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'contact_id' => 'nullable|exists:contacts,id',
|
|
'phone' => 'nullable|string|max:50',
|
|
'license_number' => 'nullable|string|max:100',
|
|
'notes' => 'nullable|string',
|
|
'is_active' => 'sometimes|boolean',
|
|
]);
|
|
|
|
CustomsBroker::create(array_merge($data, [
|
|
'business_id' => $business_id,
|
|
'is_active' => $request->boolean('is_active', true),
|
|
]));
|
|
|
|
return $this->tmsUtil->jsonOrRedirect($request, true, __('tms::lang.broker_created'));
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$this->authorizeTmsAccess('tms.brokers.update');
|
|
$business_id = $this->tmsUtil->getBusinessId();
|
|
$broker = CustomsBroker::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
return view('tms::brokers.edit', [
|
|
'broker' => $broker,
|
|
'suppliers' => $this->tmsUtil->getSuppliersDropdown($business_id),
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$this->authorizeTmsAccess('tms.brokers.update');
|
|
$broker = CustomsBroker::where('business_id', $this->tmsUtil->getBusinessId())->findOrFail($id);
|
|
|
|
$data = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'contact_id' => 'nullable|exists:contacts,id',
|
|
'phone' => 'nullable|string|max:50',
|
|
'license_number' => 'nullable|string|max:100',
|
|
'notes' => 'nullable|string',
|
|
'is_active' => 'sometimes|boolean',
|
|
]);
|
|
|
|
$broker->update(array_merge($data, ['is_active' => $request->boolean('is_active')]));
|
|
|
|
return $this->tmsUtil->jsonOrRedirect($request, true, __('tms::lang.broker_updated'));
|
|
}
|
|
|
|
public function destroy(Request $request, $id)
|
|
{
|
|
$this->authorizeTmsAccess('tms.brokers.delete');
|
|
$broker = CustomsBroker::where('business_id', $this->tmsUtil->getBusinessId())->findOrFail($id);
|
|
$broker->delete();
|
|
|
|
return $this->tmsUtil->jsonOrRedirect($request, true, __('tms::lang.broker_deleted'));
|
|
}
|
|
}
|