From 53ad10eefabadf867cfbe7ad382cc01df745ffbe Mon Sep 17 00:00:00 2001 From: ludo Date: Mon, 29 Jan 2024 23:44:49 +0100 Subject: [PATCH] add new metrics, graph metrics, prepare basket to storage --- app/Charts/Shop/Order.php | 4 +- app/Http/Controllers/Admin/HomeController.php | 10 +- app/Models/Shop/Basket.php | 16 ++- app/Repositories/Core/DateRange.php | 2 +- app/Repositories/Shop/BasketStores.php | 43 ++++++ app/Repositories/Shop/Dashboards.php | 7 +- app/Repositories/Shop/OfferStocks.php | 10 ++ app/Repositories/Shop/OrderMetrics.php | 67 ++++++--- app/Repositories/Shop/OrderStatistics.php | 68 --------- .../Dashboard/_partials/counter.blade.php | 130 ++++++++---------- .../Dashboard/_partials/evolutions.blade.php | 64 ++++----- .../Admin/Shop/Dashboard/index.blade.php | 12 -- 12 files changed, 219 insertions(+), 214 deletions(-) create mode 100644 app/Repositories/Shop/BasketStores.php delete mode 100644 app/Repositories/Shop/OrderStatistics.php diff --git a/app/Charts/Shop/Order.php b/app/Charts/Shop/Order.php index 58bce705..d1fe17c9 100644 --- a/app/Charts/Shop/Order.php +++ b/app/Charts/Shop/Order.php @@ -3,13 +3,13 @@ namespace App\Charts\Shop; use Akaunting\Apexcharts\Chart; -use App\Repositories\Shop\OrderStatistics; +use App\Repositories\Shop\OrderMetrics; class Order { public static function getMonthly() { - $data = OrderStatistics::getTotalMonthly(); + $data = OrderMetrics::getTotalMonthly(); return (new Chart)->setType('bar') ->setWidth('100%') diff --git a/app/Http/Controllers/Admin/HomeController.php b/app/Http/Controllers/Admin/HomeController.php index cb2418ff..803e37e9 100644 --- a/app/Http/Controllers/Admin/HomeController.php +++ b/app/Http/Controllers/Admin/HomeController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Repositories\Shop\Dashboards; use App\Charts\Shop\Order; +use Carbon\Carbon; use Illuminate\Http\Request; class HomeController extends Controller @@ -16,14 +17,13 @@ class HomeController extends Controller public function index(Request $request, $period = false) { - $data = $request->all(); - $start = $request->input('start'); - $end = $request->input('end'); + $start = $request->input('start') ?? Carbon::now()->subMonths(12)->startOfMonth(); + $end = $request->input('end') ?? Carbon::now(); + $data = Dashboards::getStats($start, $end); $data['chart'] = Order::getMonthly(); - //dump($data); - //exit; // dump($data); + // exit; return view('Admin.Shop.Dashboard.index', $data); } } diff --git a/app/Models/Shop/Basket.php b/app/Models/Shop/Basket.php index 004bfa10..8ade01fd 100644 --- a/app/Models/Shop/Basket.php +++ b/app/Models/Shop/Basket.php @@ -7,6 +7,10 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; class Basket extends Model { + protected $fillable = [ + 'id', 'cart_data', + ]; + protected $guarded = ['id']; protected $table = 'shop_baskets'; @@ -15,4 +19,14 @@ class Basket extends Model { return $this->belongsTo(Offer::class); } -} + + public function setCartDataAttribute($value) + { + $this->attributes['cart_data'] = serialize($value); + } + + public function getCartDataAttribute($value) + { + return unserialize($value); + } +} \ No newline at end of file diff --git a/app/Repositories/Core/DateRange.php b/app/Repositories/Core/DateRange.php index b87ea28a..6bc78cd3 100644 --- a/app/Repositories/Core/DateRange.php +++ b/app/Repositories/Core/DateRange.php @@ -79,7 +79,7 @@ class DateRange public static function getPeriodsLastMonth($nb = 1, $withActual = true) { $end = $withActual ? Carbon::now()->endOfMonth() : self::lastMonth(); - $begin = $nb === 1 ? $end->copy()->startOfMonth() : $end->copy()->startOfMonth()->subMonths($nb - 1); + $begin = $nb === 1 ? $end->copy()->startOfMonth() : $end->copy()->subMonths($nb)->startOfMonth(); return self::getPeriodsByMonth($begin, $end); } diff --git a/app/Repositories/Shop/BasketStores.php b/app/Repositories/Shop/BasketStores.php new file mode 100644 index 00000000..54c43c6e --- /dev/null +++ b/app/Repositories/Shop/BasketStores.php @@ -0,0 +1,43 @@ +has($key)) + { + return new CartCollection(Basket::find($key)->cart_data); + } + else + { + return []; + } + } + + public function put($key, $value) + { + if($row = Basket::find($key)) + { + // update + $row->cart_data = $value; + $row->save(); + } + else + { + Basket::create([ + 'id' => $key, + 'cart_data' => $value + ]); + } + } +} diff --git a/app/Repositories/Shop/Dashboards.php b/app/Repositories/Shop/Dashboards.php index fb044bc0..e883d57d 100644 --- a/app/Repositories/Shop/Dashboards.php +++ b/app/Repositories/Shop/Dashboards.php @@ -12,9 +12,10 @@ class Dashboards 'orders_avg' => OrderMetrics::avgByPeriod($start, $end, 'total_shipped'), 'offers_count' => Offers::count(), 'clients_count' => Customers::count(), - 'preparationLess24H' => OrderMetrics::countInPreparationLess24H(), - 'preparationLess48H' => OrderMetrics::countInPreparationLess48H(), - 'preparationMore48H' => OrderMetrics::countInPreparationMore48H(), + 'preparationLess24H' => OrderMetrics::countPreparationLess24h(), + 'preparationLess48H' => OrderMetrics::countPreparationLess48h(), + 'preparationMore48H' => OrderMetrics::countPreparationMore48h(), + 'stocksWarning' => OfferStocks::countAlerts(), ]; } } diff --git a/app/Repositories/Shop/OfferStocks.php b/app/Repositories/Shop/OfferStocks.php index d03992f1..360d9388 100644 --- a/app/Repositories/Shop/OfferStocks.php +++ b/app/Repositories/Shop/OfferStocks.php @@ -22,4 +22,14 @@ class OfferStocks { return Offers::getField($id, 'stock_current'); } + + public static function getAlerts() + { + return Offer::active()->where('stock_current', '<', 15)->get(); + } + + public static function countAlerts() + { + return Offer::active()->where('stock_current', '<', 15)->count(); + } } diff --git a/app/Repositories/Shop/OrderMetrics.php b/app/Repositories/Shop/OrderMetrics.php index df2f8647..d2babe7b 100644 --- a/app/Repositories/Shop/OrderMetrics.php +++ b/app/Repositories/Shop/OrderMetrics.php @@ -4,38 +4,71 @@ namespace App\Repositories\Shop; use App\Models\Shop\Order; use App\Repositories\Core\DateStats; -use Carbon\Carbon; +use App\Traits\Model\Basic; class OrderMetrics { - use DateStats; + use Basic, DateStats; - public static function countInPreparationLess24H() + public static function getTotalDaily() { - $start = Carbon::now()->subHours(24); - $end = Carbon::now(); - - return Order::preparation()->byPeriod($start, $end, 'updated_at')->count(); + return self::sumDaily('total_taxed'); } - public static function countInPreparationLess48H() + public static function getTotalMonthly() { - $start = Carbon::now()->subHours(48); - $end = Carbon::now(); - - return Order::preparation()->byPeriod($start, $end, 'updated_at')->count(); + return self::sumMonthly('total_taxed'); } - public static function countInPreparationMore48H() + public static function countPreparationLess24h() { - $start = Carbon::now()->subHours(48); + $date = now()->subHours(24); - return Order::preparation()->where('updated_at', '>', $start)->count(); + return Order::Preparation()->where('updated_at', '>', $date)->count(); } - public static function getTotalByPeriod($start, $end) + public static function countPreparationLess48h() { - return self::sumByPeriod($start, $end, 'total_shipped'); + $date = now()->subHours(48); + + return Order::Preparation()->where('updated_at', '>', $date)->count(); + } + + public static function countPreparationMore48h() + { + $date = now()->subHours(48); + + return Order::Preparation()->where('updated_at', '<', $date)->count(); + } + + public static function countOfToday() + { + return Order::ofToday()->count(); + } + + public static function countOfLastWeek() + { + return Order::ofLastWeek()->count(); + } + + public static function countOfLastMonth() + { + return Order::ofLastMonth()->count(); + } + + public static function getTotalOfToday() + { + return Order::ofToday()->sum('total_taxed'); + } + + public static function getTotalOfLastWeek() + { + return Order::ofLastWeek()->sum('total_taxed'); + } + + public static function getTotalOfLastMonth() + { + return Order::ofLastMonth()->sum('total_taxed'); } public static function getModel() diff --git a/app/Repositories/Shop/OrderStatistics.php b/app/Repositories/Shop/OrderStatistics.php deleted file mode 100644 index 2939ea14..00000000 --- a/app/Repositories/Shop/OrderStatistics.php +++ /dev/null @@ -1,68 +0,0 @@ -Preparation()->count(); - } - - public static function countPreparationMore2Days() - { - $date = now()->subHours(48); - return Order::Preparation()->where('updated_at', '<', $date)->count(); - } - - public static function countOfToday() - { - return Order::ofToday()->count(); - } - - public static function countOfLastWeek() - { - return Order::ofLastWeek()->count(); - } - - public static function countOfLastMonth() - { - return Order::ofLastMonth()->count(); - } - - public static function getTotalOfToday() - { - return Order::ofToday()->sum('total_taxed'); - } - - public static function getTotalOfLastWeek() - { - return Order::ofLastWeek()->sum('total_taxed'); - } - - public static function getTotalOfLastMonth() - { - return Order::ofLastMonth()->sum('total_taxed'); - } - - public static function getModel() - { - return Order::query(); - } -} diff --git a/resources/views/Admin/Shop/Dashboard/_partials/counter.blade.php b/resources/views/Admin/Shop/Dashboard/_partials/counter.blade.php index 76729d5b..2a4c1a0a 100644 --- a/resources/views/Admin/Shop/Dashboard/_partials/counter.blade.php +++ b/resources/views/Admin/Shop/Dashboard/_partials/counter.blade.php @@ -1,74 +1,66 @@ -
- Paniers Actifs -
40
-
-
- dans les 30 dernières minutes -
+
+ Paniers Actifs +
40
+
+
+ dans les 30 dernières minutes +
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Commandes{{ $orders_count }}
En préparation depuis moins de 24h{{ $preparationLess24H ?? 0 }}
En préparation depuis moins de 48h{{ $preparationLess48H ?? 0 }}
En préparation depuis plus de 48h{{ $preparationMore48H ?? 0 }}
Echec de paiement{{ $paymentsFailed ?? 0 }}
- A rembourser
- Payés sans stock disponible -
{{ $refunds ?? 0 }}
- En attente de confirmation
- Pour les ventes pro -
{{ $ordersNotConfirmed ?? 0 }}
-
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Commandes{{ $orders_count }}
En préparation depuis moins de 24h{{ $preparationLess24H ?? 0 }}
En préparation depuis moins de 48h{{ $preparationLess48H ?? 0 }}
En préparation depuis plus de 48h{{ $preparationMore48H ?? 0 }}
Echec de paiement{{ $paymentsFailed ?? 0 }}
+ A rembourser
+ Payés sans stock disponible +
{{ $refunds ?? 0 }}
+ En attente de confirmation
+ Pour les ventes pro +
{{ $ordersNotConfirmed ?? 0 }}
+
- - - - - - - - - - -
Stock critique{{ $stocksWarning ?? 0 }}
Paramétrage du niveau critique
-
+ + + + + + +
Stock critique{{ $stocksWarning ?? 0 }}
+
- - - - - - - - - - -
Nouveaux clients{{ $newClients ?? 0 }}
Exporter le fichier clients
-
+ + + + + + +
Nouveaux clients{{ $newClients ?? 0 }}
+
diff --git a/resources/views/Admin/Shop/Dashboard/_partials/evolutions.blade.php b/resources/views/Admin/Shop/Dashboard/_partials/evolutions.blade.php index 6116c3d2..e62f76ba 100644 --- a/resources/views/Admin/Shop/Dashboard/_partials/evolutions.blade.php +++ b/resources/views/Admin/Shop/Dashboard/_partials/evolutions.blade.php @@ -1,38 +1,30 @@
-
-
- 17% -
{{ $orders_sum ?? 0 }} €
- TOTAL VENTE -
- -
- -
-
- 0% -
{{ $orders_avg ?? 0 }} €
- PANIER MOYEN -
- -
- -
-
- 20% -
{{ $clients_count ?? 0 }}
- NB CLIENTS -
- -
- -
-
- 18% -
{{ $offers_count ?? 0 }}
- NB OFFRES -
- -
+
+
+ 17% +
{{ $orders_sum ?? 0 }} €
+ TOTAL VENTE +
+
+
+
+ 0% +
{{ $orders_avg ?? 0 }} €
+ PANIER MOYEN +
+
+
+
+ 20% +
{{ $clients_count ?? 0 }}
+ NB CLIENTS +
+
+
+
+ 18% +
{{ $offers_count ?? 0 }}
+ NB OFFRES +
+
- diff --git a/resources/views/Admin/Shop/Dashboard/index.blade.php b/resources/views/Admin/Shop/Dashboard/index.blade.php index 7ed7c238..19e9713b 100644 --- a/resources/views/Admin/Shop/Dashboard/index.blade.php +++ b/resources/views/Admin/Shop/Dashboard/index.blade.php @@ -5,18 +5,6 @@ @include('boilerplate::logs.style') @section('content') -
- - - - - - - @include('components.form.daterangepicker', [ - 'name' => 'period', - ]) -
-
@include('Admin.Shop.Dashboard._partials.counter')