93 lines
3 KiB
PHP
93 lines
3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\ProductIncos\StoreProductIncoRequest;
|
|
use App\Http\Requests\Bo\ProductIncos\UpdateProductIncoRequest;
|
|
use App\Models\Product;
|
|
use App\Models\ProductInco;
|
|
use Domain\Enums\Nutriscore;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ProductIncosController extends Controller
|
|
{
|
|
public function create(Product $product): View
|
|
{
|
|
$this->authorize('createInco', $product);
|
|
|
|
$nutriscores = Nutriscore::cases();
|
|
|
|
return view('bo.product-incos.create', compact(
|
|
'product',
|
|
'nutriscores',
|
|
));
|
|
}
|
|
|
|
public function store(StoreProductIncoRequest $request, Product $product): RedirectResponse
|
|
{
|
|
$this->authorize('createInco', $product);
|
|
|
|
return DB::transaction(function () use ($request, $product) {
|
|
$inco = new ProductInco();
|
|
$inco->no_art = 1;
|
|
$inco->fill($request->only(
|
|
$inco->getFillable()
|
|
));
|
|
$inco->product()->associate($product);
|
|
|
|
if ($inco->saveOrFail()) {
|
|
return to_route('bo.products.incos.edit', compact('product', 'inco'))
|
|
->with('success', trans('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.product_incos', 1)),
|
|
]));
|
|
}
|
|
|
|
return to_route('bo.products.incos.create')
|
|
->with('error', trans('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.product_incos', 1)),
|
|
]));
|
|
});
|
|
}
|
|
|
|
public function edit(Product $product, ProductInco $inco): View
|
|
{
|
|
$this->authorize('updateInco', $product);
|
|
|
|
$nutriscores = Nutriscore::cases();
|
|
|
|
return view('bo.product-incos.edit', compact(
|
|
'product',
|
|
'inco',
|
|
'nutriscores',
|
|
));
|
|
}
|
|
|
|
public function update(UpdateProductIncoRequest $request, Product $product, ProductInco $inco): RedirectResponse
|
|
{
|
|
$this->authorize('update', $product);
|
|
|
|
return DB::transaction(function () use ($request, $product, $inco) {
|
|
$inco->fill($request->only(
|
|
$inco->getFillable()
|
|
));
|
|
|
|
if ($inco->saveOrFail()) {
|
|
return to_route('bo.products.incos.edit', compact('product', 'inco'))
|
|
->with('success', trans('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.product_incos', 1)),
|
|
]));
|
|
}
|
|
|
|
return to_route('bo.products.incos.edit', compact('product', 'inco'))
|
|
->with('error', trans('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.product_incos', 1)),
|
|
]));
|
|
});
|
|
}
|
|
}
|