250 lines
9.3 KiB
PHP
250 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Back;
|
|
|
|
use App\Http\Requests\Back\InvoiceCompanies\StoreInvoiceCompanyRequest;
|
|
use App\Http\Requests\Back\InvoiceCompanies\UpdateInvoiceCompanyRequest;
|
|
use App\Models\InvoiceCompany;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Uwil\LaravelBackend\Controllers\BackendController;
|
|
use Uwil\LaravelBackend\Resources\CommonResource;
|
|
|
|
/**
|
|
* Handles CRUD operations for InvoiceCompany in the backend.
|
|
*/
|
|
class InvoiceCompanyController extends BackendController
|
|
{
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->authorizeResource(InvoiceCompany::class);
|
|
$this->modelName = InvoiceCompany::modelName();
|
|
$this->modelPage = InvoiceCompany::modelPage();
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function index(Request $request): \Inertia\Response
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder<\App\Models\InvoiceCompany> $query */
|
|
$query = InvoiceCompany::query();
|
|
|
|
if ($request->routeIs("{$this->routeName}{$this->modelName}.trash")) {
|
|
$query = $query->onlyTrashed();
|
|
}
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$query,
|
|
$search,
|
|
null,
|
|
'name',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.attributes.name'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($query);
|
|
|
|
/** Custom pagination */
|
|
$models = $this->paginate($query);
|
|
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Index", [
|
|
'models' => CommonResource::collection($models),
|
|
'search' => $search,
|
|
'searchFields' => $searchFields,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\InvoiceCompany $invoice_company
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function create(InvoiceCompany $invoice_company): \Inertia\Response
|
|
{
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Create", [
|
|
'data' => CommonResource::make($invoice_company),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show a specific resource.
|
|
*
|
|
* @param \App\Models\InvoiceCompany $invoice_company
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function show(InvoiceCompany $invoice_company): \Inertia\Response
|
|
{
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Show", [
|
|
'data' => CommonResource::make($invoice_company),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource.
|
|
*
|
|
* @param \App\Http\Requests\Back\InvoiceCompanies\StoreInvoiceCompanyRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreInvoiceCompanyRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$invoice_company = new InvoiceCompany();
|
|
$invoice_company->fill($request->validated());
|
|
|
|
if ($invoice_company->saveOrFail()) {
|
|
return to_route("{$this->routeName}{$this->modelName}.edit", $invoice_company)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
}
|
|
return back()
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\InvoiceCompany $invoice_company
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function edit(InvoiceCompany $invoice_company): \Inertia\Response
|
|
{
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Edit", [
|
|
'data' => CommonResource::make($invoice_company),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource.
|
|
*
|
|
* @param \App\Http\Requests\Back\InvoiceCompanies\UpdateInvoiceCompanyRequest $request
|
|
* @param \App\Models\InvoiceCompany $invoice_company
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(
|
|
UpdateInvoiceCompanyRequest $request,
|
|
InvoiceCompany $invoice_company
|
|
): \Illuminate\Http\RedirectResponse {
|
|
return DB::transaction(function () use ($request, $invoice_company) {
|
|
$invoice_company->fill($request->validated());
|
|
|
|
if ($invoice_company->saveOrFail()) {
|
|
return to_route("{$this->routeName}{$this->modelName}.edit", $invoice_company)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
}
|
|
return to_route("{$this->routeName}{$this->modelName}.edit", $invoice_company)
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\InvoiceCompany $invoice_company
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function duplicate(InvoiceCompany $invoice_company): \Inertia\Response
|
|
{
|
|
$invoice_company->loadMissing(InvoiceCompany::getCacheRelations());
|
|
$replica = $invoice_company->replicate();
|
|
collect($invoice_company->getRelations())
|
|
->each(fn($value, $relation) => $replica->setRelation($relation, $value));
|
|
return $this->create($replica);
|
|
}
|
|
|
|
/**
|
|
* Restore the specified resource.
|
|
*
|
|
* @param integer $invoice_company
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function restore(int $invoice_company): \Illuminate\Http\RedirectResponse
|
|
{
|
|
$invoice_company = InvoiceCompany::withTrashed()->findOrFail($invoice_company);
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str((string) \parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($invoice_company->restore()) {
|
|
return to_route("{$this->routeName}{$this->modelName}.trash", $previousQueries)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_restored', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
}
|
|
return back()
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_restored', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Destroy the specified resource.
|
|
*
|
|
* @param \App\Models\InvoiceCompany $invoice_company
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(InvoiceCompany $invoice_company): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str((string) \parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($invoice_company->delete()) {
|
|
return to_route("{$this->routeName}{$this->modelName}.index", $previousQueries)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
}
|
|
return back()
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Destroy the specified resource.
|
|
*
|
|
* @param \App\Models\InvoiceCompany|integer $invoice_company
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function forceDestroy(InvoiceCompany|int $invoice_company): \Illuminate\Http\RedirectResponse
|
|
{
|
|
if (is_int($invoice_company)) {
|
|
$invoice_company = InvoiceCompany::withTrashed()->findOrFail($invoice_company);
|
|
}
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str((string) \parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($invoice_company->forceDelete()) {
|
|
return to_route("{$this->routeName}{$this->modelName}.trash", $previousQueries)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
}
|
|
return back()
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
}
|
|
}
|