diff --git a/app/Http/Controllers/Bo/CreneauxController.php b/app/Http/Controllers/Bo/CreneauxController.php index a713959..2520a59 100644 --- a/app/Http/Controllers/Bo/CreneauxController.php +++ b/app/Http/Controllers/Bo/CreneauxController.php @@ -3,30 +3,50 @@ namespace App\Http\Controllers\Bo; use App\Http\Controllers\Controller; -use App\Models\CollecteException; +use App\Models\Magasin; use App\Models\TimeslotSetting; use App\Models\Setting; use Illuminate\Http\Request; class CreneauxController extends Controller { - public function settings(Request $request) + public function settings(Request $request,$id) { - $timeslotSettings = TimeslotSetting::all(); + $magasin = Magasin::findOrFail($id); + $timeslotSettings = $magasin->timeslotSettings; + if ($timeslotSettings->count() === 0) { + //Aucun settings pour ce magasin + $timeslotSettings = $this->makeCreneauxSettings($id); + } $plage_days = Setting::where('key', 'order_withdrawal_period')->firstOrFail()->value; - $exceptionsCollecte = CollecteException::orderBy('date')->get(); - return view('bo.creneaux.settings', compact('timeslotSettings', 'plage_days', 'exceptionsCollecte')); + + + return view('bo.creneaux.settings', compact('timeslotSettings', 'plage_days', 'magasin')); } - public function updateSettings(Request $request) + public function makeCreneauxSettings($id) { + for ($i=0;$i<7;$i++) { + $dayset = new TimeslotSetting(); + $dayset->magasin_id = $id; + $dayset->day_index = ''.$i; + $dayset->save(); + } + return Magasin::findOrFail($id)->timeslotSettings; + } + + public function updateSettings(Request $request,$id) { $plage = $request->plage; $plage_days = $request->plage_days; + $magasin_id = $id; foreach($plage as $index => $day) { - $setting = TimeslotSetting::where('day_index', ''.$index)->firstOrFail(); + $setting = TimeslotSetting::where('day_index', ''.$index) + ->where('magasin_id', $magasin_id) + ->firstOrFail(); + //$setting->magasin_id = $magasin_id; ne doit pas changer $setting->morning_start = $day['morning_start']; $setting->morning_end = $day['morning_end']; $setting->afternoon_start = $day['afternoon_start']; @@ -41,42 +61,6 @@ class CreneauxController extends Controller $order_withdrawal_period->value = $plage_days; $order_withdrawal_period->save(); - return redirect()->route('bo.creneaux.settings')->with('success', 'Modifications enregistrées !'); - } - - public function saveExceptions(Request $request) - { - $collecteException = $request->collecte_exception; - - for ($i = 0; $i < count($collecteException); $i++) { - $collecteExceptionToInsert = !empty($collecteException[$i]["date"]); - if (isset($collecteException[$i]["id"]) && $collecteException[$i]["id"] !== null && $collecteExceptionToInsert) { - $ce = CollecteException::findOrFail($collecteException[$i]["id"]); - $ce->date = $collecteException[$i]["date"]; - $ce->comment = $collecteException[$i]["comment"]; - $ce->save(); - } else { - if ($collecteExceptionToInsert) { - $ce = CollecteException::updateOrCreate(['date' => $collecteException[$i]["date"], 'comment' => $collecteException[$i]["comment"]]); - $collecteException[$i]["id"] = $ce->id; - } else { - unset($collecteException[$i]); - $i--; - } - } - } - - //remet dans l'ordre avant de renvoyer - $collecteException = CollecteException::orderBy('date')->get(); - return $collecteException; - - } - - public function destroyExceptions(Request $request) - { - $collecteException = CollecteException::findOrFail($request->id); - $collecteException->delete(); - - return "Suppression terminée !"; + return redirect()->route('bo.creneaux.settings', $magasin_id)->with('success', 'Modifications enregistrées !'); } } diff --git a/app/Http/Controllers/Bo/MagasinsController.php b/app/Http/Controllers/Bo/MagasinsController.php index 3b1f3e3..97b0625 100644 --- a/app/Http/Controllers/Bo/MagasinsController.php +++ b/app/Http/Controllers/Bo/MagasinsController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bo; use App\Http\Controllers\Controller; use App\Models\Magasin; +use App\Models\CollecteException; use Illuminate\Http\Request; class MagasinsController extends Controller @@ -68,7 +69,8 @@ class MagasinsController extends Controller public function edit($id) { $magasin = Magasin::findOrFail($id); - return view('bo.magasins.edit', compact('magasin')); + $exceptionsCollecte = $magasin->collecteExceptions()->orderBy('date')->get(); + return view('bo.magasins.edit', compact('magasin', 'exceptionsCollecte')); } /** @@ -112,4 +114,58 @@ class MagasinsController extends Controller $shop->delete(); return redirect()->route('bo.magasins.index'); } + + + public function saveExceptions(Request $request) + { + $collecteException = $request->collecte_exception; + + for ($i = 0; $i < count($collecteException); $i++) { + $collecteExceptionToInsert = !empty($collecteException[$i]["date"]); + if (isset($collecteException[$i]["id"]) && $collecteException[$i]["id"] !== null && $collecteExceptionToInsert) { + $ce = CollecteException::findOrFail($collecteException[$i]["id"]); + $ce->date = $collecteException[$i]["date"]; + $ce->comment = $collecteException[$i]["comment"]; + $ce->magasin_id = $collecteException[$i]["magasin_id"]; + $ce->save(); + } else { + if ($collecteExceptionToInsert) { + $ce = CollecteException::updateOrCreate([ + 'date' => $collecteException[$i]["date"], + 'comment' => $collecteException[$i]["comment"], + 'magasin_id' => $collecteException[$i]["magasin_id"] + ]); + $collecteException[$i]["id"] = $ce->id; + + } else { + unset($collecteException[$i]); + $i--; + } + } + } + + //remet dans l'ordre avant de renvoyer + //$collecteException = CollecteException::where('magasin_id',)->orderBy('date')->get(); + $this->arraySortByColumn($collecteException, "date"); + return $collecteException; + + } + + public function arraySortByColumn(&$array, $column, $direction = SORT_ASC) { + $reference_array = array(); + + foreach($array as $key => $row) { + $reference_array[$key] = $row[$column]; + } + + array_multisort($reference_array, $direction, $array); + } + + public function destroyExceptions(Request $request) + { + $collecteException = CollecteException::findOrFail($request->id); + $collecteException->delete(); + + return "Suppression terminée !"; + } } diff --git a/app/Http/Controllers/Bo/OrdersController.php b/app/Http/Controllers/Bo/OrdersController.php index 570b5dc..1447699 100644 --- a/app/Http/Controllers/Bo/OrdersController.php +++ b/app/Http/Controllers/Bo/OrdersController.php @@ -169,13 +169,31 @@ class OrdersController extends Controller { $activestep = 3; + $magasin = Magasin::first(); + + $availCreneaux = array(); + //plus besoin car chargé par le composant vue. + //$availCreneaux = $this->getAvailableCreneauxByShop($magasin->id); + + $order = Order::where('reference', '=', $reference)->firstOrFail(); + + return view('sales.pick-slot', compact('activestep', 'availCreneaux', 'order', 'magasin')); + } + + /** + * @return mixed + */ + public function getAvailableCreneauxByShop($id) + { //setting à stocker et réutiliser $plage_days = 14; - $timeslotSettings = TimeslotSetting::all(); + $magasin = Magasin::findOrFail($id); + $timeslotSettings = $magasin->timeslotSettings; + //$timeslotSettings = TimeslotSetting::all(); //fabriquons un tableau recap $plage = array(); - foreach($timeslotSettings as $day) { + foreach ($timeslotSettings as $day) { $plage[$day->day_index] = array( 'morning_start' => $day->morning_start, @@ -190,29 +208,39 @@ class OrdersController extends Controller //génération des plages //start today - $today = CarbonImmutable::now()->startOfDay(); + $today = CarbonImmutable::now()->startOfDay(); + $weekDay = $today->weekday(); //final boundary (permet de limiter la liste des jours d'exception) $finalday = $today->add($plage_days, 'day'); - $now = CarbonImmutable::now(); + $now = CarbonImmutable::now(); //jours exceptions - $noCollectDays = CollecteException::whereDate('date','>',$now)->whereDate('date','<=',$finalday)->orderBy('date')->get(); + // $noCollectDays = CollecteException::where('magasin_id',$magasin->id) + // ->whereDate('date', '>', $now) + // ->whereDate('date', '<=', $finalday) + // ->orderBy('date')->get(); + $noCollectDays = $magasin->collecteExceptions() + ->whereDate('date', '>', $now) + ->whereDate('date', '<=', $finalday) + ->orderBy('date')->get(); + + $delaiPrepa = CarbonInterval::createFromFormat('h:i', $plage[$weekDay]['min_order_preparation'] ?? '01:00'); $availCreneaux = array(); for ($i = 0; $i < $plage_days; $i++) { /** @var Carbon */ - $current = $today->add($i, 'day'); - $weekDay = $current->weekday(); - $interval = CarbonInterval::createFromFormat('h:i', $plage[$weekDay]['interval'] ?? '00:15'); - $delaiPrepa = CarbonInterval::createFromFormat('h:i', $plage[$weekDay]['min_order_preparation'] ?? '01:00'); + $current = $today->add($i, 'day'); + $weekDay = $current->weekday(); + $interval = CarbonInterval::createFromFormat('h:i', $plage[$weekDay]['interval'] ?? '00:15'); + //si c'est un jour exception skip $skip = false; foreach ($noCollectDays as $not_today) { - if($current->isSameDay($not_today->date)) { + if ($current->isSameDay($not_today->date)) { $skip = true; } } - if($skip) { + if ($skip) { continue; } //matinée @@ -221,14 +249,20 @@ class OrdersController extends Controller $endTime = Carbon::createFromTimeString($plage[$weekDay]['morning_end']); do { $creneau = $currentTime->format('H:i'); - $nbtakens = Timeslot::where('day',$current->format('d/m/Y'))->where('time',$creneau)->count(); - if($nbtakens >= $plage[$weekDay]['max_orders_per_interval']) { + $nbtakens = Timeslot::where('day', $current->format('d/m/Y')) + ->where('time', $creneau)->count(); + + if ($nbtakens >= $plage[$weekDay]['max_orders_per_interval']) { $isDispo = false; } else { $isDispo = true; } + + if($current->addHours($currentTime->format('H'))->addMinutes($currentTime->format('i')) < $now->add($delaiPrepa)) { + $isDispo = false; + } //attention si la date est passée (today mais avant maintenant) : pas de créneau - if ($current > $today || $currentTime >= $now->add($delaiPrepa)) { + if ($current > $today || $currentTime >= $now) { $availCreneaux[$current->format('d/m/Y')][] = array($creneau,$isDispo); } $currentTime->add($interval); @@ -246,19 +280,25 @@ class OrdersController extends Controller } else { $isDispo = true; } + + if($current->addHours($currentTime->format('H'))->addMinutes($currentTime->format('i')) < $now->add($delaiPrepa)) { + $isDispo = false; + } + //attention si la date est passée (today mais avant maintenant) : pas de créneau - if ($current > $today || $currentTime >= $now->add($delaiPrepa)) { + if ($current > $today || $currentTime >= $now) { $availCreneaux[$current->format('d/m/Y')][] = array($creneau,$isDispo); } $currentTime->add($interval); } while ($currentTime <= $endTime); } } + return $availCreneaux; + } - $order = Order::where('reference', '=', $reference)->firstOrFail(); - $magasin = Magasin::first(); - - return view('sales.pick-slot', compact('activestep', 'availCreneaux', 'order', 'magasin')); + //call ajax pour le composant vue de timeslots + public function getAvailableCreneaux(Request $request) { + return $this->getAvailableCreneauxByShop($request->id); } /** diff --git a/app/Models/CollecteException.php b/app/Models/CollecteException.php index b97a806..5512342 100644 --- a/app/Models/CollecteException.php +++ b/app/Models/CollecteException.php @@ -10,7 +10,7 @@ class CollecteException extends Model { use HasFactory; - protected $fillable = ['date', 'comment']; + protected $fillable = ['date', 'comment', 'magasin_id']; /** * The attributes that should be cast to native types. @@ -25,4 +25,9 @@ class CollecteException extends Model { return $date->format('Y-m-d'); } + + public function magasin() + { + return $this->belongsTo(Magasin::class); + } } diff --git a/app/Models/Magasin.php b/app/Models/Magasin.php index df5f70e..1d4e930 100644 --- a/app/Models/Magasin.php +++ b/app/Models/Magasin.php @@ -14,4 +14,12 @@ class Magasin extends Model 'latitude', 'longitude', 'gmaps_placeid', 'gmaps_embed_href', 'gmaps_link_href', ]; + + public function timeslotSettings() { + return $this->hasMany(TimeslotSetting::class); + } + + public function collecteExceptions() { + return $this->hasMany(CollecteException::class); + } } diff --git a/app/Models/TimeslotSetting.php b/app/Models/TimeslotSetting.php index fd605f0..8c2d24e 100644 --- a/app/Models/TimeslotSetting.php +++ b/app/Models/TimeslotSetting.php @@ -8,4 +8,9 @@ use Illuminate\Database\Eloquent\Model; class TimeslotSetting extends Model { use HasFactory; + + public function magasin() + { + return $this->belongsTo(Magasin::class); + } } diff --git a/database/migrations/2021_04_22_153147_make_child_of_magasins_time_settings_table.php b/database/migrations/2021_04_22_153147_make_child_of_magasins_time_settings_table.php new file mode 100644 index 0000000..5dcfe91 --- /dev/null +++ b/database/migrations/2021_04_22_153147_make_child_of_magasins_time_settings_table.php @@ -0,0 +1,54 @@ +dropUnique('timeslot_settings_day_index_unique'); + $table->foreignId('magasin_id')->after('id')->default(1)->constrained('magasins'); + $table->unique(array('magasin_id', 'day_index')); + }); + + Schema::table('timeslot_settings', function (Blueprint $table) { + $table->unsignedBigInteger('magasin_id')->default(null)->change(); + }); + + Schema::table('collecte_exceptions', function (Blueprint $table) { + $table->foreignId('magasin_id')->after('id')->default(1)->constrained('magasins'); + }); + + Schema::table('collecte_exceptions', function (Blueprint $table) { + $table->unsignedBigInteger('magasin_id')->default(null)->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('timeslot_settings', function (Blueprint $table) { + $table->dropForeign(['magasin_id']); + $table->dropUnique('timeslot_settings_magasin_id_day_index_unique'); + $table->dropColumn('magasin_id'); + $table->unique('day_index'); + }); + + Schema::table('collecte_exceptions', function (Blueprint $table) { + $table->dropForeign(['magasin_id']); + $table->dropColumn('magasin_id'); + }); + } +} diff --git a/public/b-o/js/app.js b/public/b-o/js/app.js index 4087838..7433e4a 100644 --- a/public/b-o/js/app.js +++ b/public/b-o/js/app.js @@ -12270,7 +12270,7 @@ __webpack_require__.r(__webpack_exports__); console.log('CreneauxExceptions Component mounted.'); this.datas = JSON.parse(this.dbdatas); }, - props: ['name', 'dbdatas'], + props: ['name', 'dbdatas', 'magasinId'], data: function data() { return { datas: [], @@ -12283,7 +12283,8 @@ __webpack_require__.r(__webpack_exports__); newItem: function newItem() { this.datas.push({ date: '', - comment: '' + comment: '', + magasin_id: this.magasinId }); }, removeItem: function removeItem(index) { diff --git a/resources/js/components/CreneauxExceptions.vue b/resources/js/components/CreneauxExceptions.vue index 7e2b854..6b9c547 100644 --- a/resources/js/components/CreneauxExceptions.vue +++ b/resources/js/components/CreneauxExceptions.vue @@ -43,6 +43,7 @@ props: [ 'name', 'dbdatas', + 'magasinId' ], data() { return { @@ -56,7 +57,7 @@ }, methods: { newItem() { - this.datas.push({date: '', comment: ''}); + this.datas.push({date: '', comment: '', magasin_id: this.magasinId}); }, removeItem(index) { if (this.datas[index]['id'] !== undefined) { diff --git a/resources/views/bo/creneaux/settings.blade.php b/resources/views/bo/creneaux/settings.blade.php index 3ef6724..dac6cec 100644 --- a/resources/views/bo/creneaux/settings.blade.php +++ b/resources/views/bo/creneaux/settings.blade.php @@ -1,10 +1,10 @@ @extends('layouts.bo-layout') @section('content') -