ldc-legaulois/app/Http/Controllers/Bo/ReductionsController.php
2022-02-10 16:14:58 +01:00

59 lines
1.8 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Enums\NotificationType;
use App\Events\NewNotification;
use App\Http\Controllers\Controller;
use App\Http\Requests\UpdateReductionRequest;
use App\Models\Range;
use App\Models\Reduction;
use App\Traits\UpdateModelStatus;
class ReductionsController extends Controller
{
use UpdateModelStatus;
public function index()
{
$r = Reduction::paginate();
$ranges = Range::with('products')->get();
return view('bo.reductions.index', compact('r', 'ranges'));
}
public function saveDatas(UpdateReductionRequest $request)
{
$reductionId = $request->reductionId;
$products = $request->products;
$ranges = $request->ranges;
$notification = $request->notification === null ? 0 : 1;
$reduction = Reduction::find($reductionId);
$reduction->ranges()->sync($ranges);
foreach ($ranges as $rangeId) {
$range = $reduction->ranges()->where('range_id', $rangeId)->first();
foreach ($products as $product) {
if (in_array($product, $range->products()->pluck('id')->toArray())) {
array_splice($products, array_search($product, $products), 1);
}
}
}
$reduction->products()->sync($products);
$reduction->notification = $notification;
$reduction->save();
if ($notification === 1) {
event(new NewNotification(NotificationType::reduction(), route('club.reductions'), $reduction));
}
return "Traitement terminé";
}
/**
* Permet la mise à jour des bons de réductions depuis MesBR manuellement dans le BO
*/
public function updateBr()
{
Reduction::updateBr();
return redirect()->route('bo.reductions.index');
}
}