diff --git a/app/Http/Controllers/CustomerController.php b/app/Http/Controllers/CustomerController.php index 7ae92c5..863033b 100644 --- a/app/Http/Controllers/CustomerController.php +++ b/app/Http/Controllers/CustomerController.php @@ -96,10 +96,7 @@ class CustomerController extends Controller /** @var \Illuminate\Pagination\AbstractPaginator */ $customers = $query->paginate(config('crud.paginate', 50)); - // use a crud controller (any) to gather crudModels - $modelsClasses = (new BrandController())->getCrudModels(); return view('customer.index', compact( - 'modelsClasses', 'modelClass', 'modelTable', 'customers', @@ -114,10 +111,7 @@ class CustomerController extends Controller $customerLogin = CustomerLogin::where('customer_id', $customer->id) ->where('brand_id', $brand->id)->first(); - // use a crud controller (any) to gather crudModels - $modelsClasses = (new BrandController())->getCrudModels(); return view('customer.show', compact( - 'modelsClasses', 'customerInfos', 'customerLogin' )); @@ -140,12 +134,9 @@ class CustomerController extends Controller public function export() { - // use a crud controller (any) to gather crudModels - $modelsClasses = (new BrandController())->getCrudModels(); - $brands = Brand::all(); + $brands = Brand::all(); return view('customer.export', compact( - 'brands', - 'modelsClasses' + 'brands' )); } diff --git a/app/Http/Controllers/StatsController.php b/app/Http/Controllers/StatsController.php new file mode 100644 index 0000000..b2ad706 --- /dev/null +++ b/app/Http/Controllers/StatsController.php @@ -0,0 +1,20 @@ +post()) { + $stats = BrandStat::whereDate('created_at', '>=', $request->start) + ->whereDate('created_at', '<=', $request->end) + ->whereBrandId($request->brand)->get(); + } + return view('stats.view-stats-by-brands', \compact('stats')); + } +} diff --git a/app/Http/Requests/StatsByBrandRequest.php b/app/Http/Requests/StatsByBrandRequest.php new file mode 100644 index 0000000..c5dd953 --- /dev/null +++ b/app/Http/Requests/StatsByBrandRequest.php @@ -0,0 +1,52 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + if ($this->post()) { + try { + if ((Carbon::parse($this->end)->diffInDays(Carbon::parse($this->start)) + 1) > 365) { + throw ValidationException::withMessages([ + 'Vous devez sélectionner une période de maximum 1 ans' + ]); + } + } catch (Exception $e) { + if (\get_class($e) !== ValidationException::class) { + throw ValidationException::withMessages([ + 'Les dates séléectionnées ne sembles pas être valides' + ]); + } + throw $e; + } + return [ + 'brand' => 'required|exists:brands,id', + 'start' => 'required|date|date_format:Y-m-d', + 'end' => 'required|date|date_format:Y-m-d|after_or_equal:start', + ]; + } + return []; + } +} diff --git a/app/Models/BrandStat.php b/app/Models/BrandStat.php index 8b12948..b6f94da 100644 --- a/app/Models/BrandStat.php +++ b/app/Models/BrandStat.php @@ -2,13 +2,11 @@ namespace App\Models; -use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Carbon; class BrandStat extends Model { - use HasFactory; public $timestamps = false; @@ -25,7 +23,9 @@ class BrandStat extends Model protected static function booted() { static::creating(function (BrandStat $stat) { - $stat->created_at = Carbon::now(); + if (!$stat->created_at) { + $stat->created_at = Carbon::now(); + } }); } diff --git a/config/app.php b/config/app.php index ea1878b..d8778aa 100644 --- a/config/app.php +++ b/config/app.php @@ -232,6 +232,7 @@ return [ 'Brand' => App\Models\Brand::class, 'Customer' => App\Models\Customer::class, 'Answer' => App\Models\Answer::class, + 'BrandController' => App\Http\Controllers\BrandController::class, ], ]; diff --git a/database/seeders/BrandStatSeeder.php b/database/seeders/BrandStatSeeder.php new file mode 100644 index 0000000..e32c31a --- /dev/null +++ b/database/seeders/BrandStatSeeder.php @@ -0,0 +1,50 @@ + 0; $i--) { + $total += floor($i / 2); + // pourcentage active + $pActive = $active * ((731 - $i) * 100 / 730); + // pourcentage nouveau + $pNew = $new * ($i * 100 / 730); + + $brandStat = new BrandStat([ + 'active' => \floor($total * $pActive), + 'inactive' => \ceil($total * $pActive), + 'old_accounts' => \ceil($total * $pNew), + 'new_accounts' => \floor($total * $pNew), + 'total_accounts' => $total, + 'total_opt_in' => ceil($total * 0.15) // 15% optin + ]); + $brandStat->brand()->associate($brand); + $brandStat->created_at = $startDay; + $brandStat->save(); + $startDay = $startDay->subDay(); + } + } + } +} diff --git a/resources/views/layouts/backend/navbar.blade.php b/resources/views/layouts/backend/navbar.blade.php index 29ef539..fce175b 100644 --- a/resources/views/layouts/backend/navbar.blade.php +++ b/resources/views/layouts/backend/navbar.blade.php @@ -9,17 +9,25 @@
-