add graphs for stats

This commit is contained in:
ludo
2024-01-29 22:39:57 +01:00
parent 75107285e7
commit 9fcc81f4d9
7 changed files with 202 additions and 73 deletions

23
app/Charts/Shop/Order.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
namespace App\Charts\Shop;
use Akaunting\Apexcharts\Chart;
use App\Repositories\Shop\OrderStatistics;
class Order
{
public static function getMonthly()
{
$data = OrderStatistics::getTotalMonthly();
return (new Chart)->setType('bar')
->setWidth('100%')
->setHeight(300)
->setLabels(array_keys($data))
->setDataset('CA', 'bar', array_values($data))
->setColor('#334F17')
->setStrokeColors(['#527C39'])
;
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Repositories\Shop\Dashboards; use App\Repositories\Shop\Dashboards;
use App\Charts\Shop\Order;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class HomeController extends Controller class HomeController extends Controller
@@ -19,7 +20,9 @@ class HomeController extends Controller
$start = $request->input('start'); $start = $request->input('start');
$end = $request->input('end'); $end = $request->input('end');
$data = Dashboards::getStats($start, $end); $data = Dashboards::getStats($start, $end);
$data['chart'] = Order::getMonthly();
//dump($data);
//exit;
// dump($data); // dump($data);
return view('Admin.Shop.Dashboard.index', $data); return view('Admin.Shop.Dashboard.index', $data);
} }

View File

@@ -30,24 +30,24 @@ class DateRange
public static function previousDay($nb = 1) public static function previousDay($nb = 1)
{ {
return [ return [
'start' => Carbon::now()->subDay($nb)->startOfDay(), 'start' => Carbon::now()->subDays($nb)->startOfDay(),
'end' => Carbon::now()->subDay($nb)->endOfDay(), 'end' => Carbon::now()->subDays($nb)->endOfDay(),
]; ];
} }
public static function previousWeek($nb = 1) public static function previousWeek($nb = 1)
{ {
return [ return [
'start' => Carbon::now()->subWeek($nb)->startOfWeek(), 'start' => Carbon::now()->subWeeks($nb)->startOfWeek(),
'end' => Carbon::now()->subWeek($nb)->endOfWeek(), 'end' => Carbon::now()->subWeeks($nb)->endOfWeek(),
]; ];
} }
public static function previousMonth($nb = 1) public static function previousMonth($nb = 1)
{ {
return [ return [
'start' => Carbon::now()->subMonth($nb)->startOfMonth(), 'start' => Carbon::now()->subMonths($nb)->startOfMonth(),
'end' => Carbon::now()->subMonth($nb)->endOfMonth(), 'end' => Carbon::now()->subMonths($nb)->endOfMonth(),
]; ];
} }
@@ -79,7 +79,7 @@ class DateRange
public static function getPeriodsLastMonth($nb = 1, $withActual = true) public static function getPeriodsLastMonth($nb = 1, $withActual = true)
{ {
$end = $withActual ? Carbon::now()->endOfMonth() : self::lastMonth(); $end = $withActual ? Carbon::now()->endOfMonth() : self::lastMonth();
$begin = $nb === 1 ? $end->copy()->startOfMonth() : $end->copy()->startOfMonth()->subMonth($nb - 1); $begin = $nb === 1 ? $end->copy()->startOfMonth() : $end->copy()->startOfMonth()->subMonths($nb - 1);
return self::getPeriodsByMonth($begin, $end); return self::getPeriodsByMonth($begin, $end);
} }
@@ -108,7 +108,7 @@ class DateRange
public static function getPeriodsLastWeek($nb = 1, $withActual = true) public static function getPeriodsLastWeek($nb = 1, $withActual = true)
{ {
$end = $withActual ? Carbon::now()->endOfWeek() : self::lastWeek(); $end = $withActual ? Carbon::now()->endOfWeek() : self::lastWeek();
$begin = $end->copy()->subWeek($nb); $begin = $end->copy()->subWeeks($nb);
return static::getPeriodsByWeek($begin, $end); return static::getPeriodsByWeek($begin, $end);
} }
@@ -116,7 +116,7 @@ class DateRange
public static function getPeriodsLastDay($nb = 1, $withActual = true) public static function getPeriodsLastDay($nb = 1, $withActual = true)
{ {
$end = $withActual ? Carbon::now()->endOfDay() : static::lastDay(); $end = $withActual ? Carbon::now()->endOfDay() : static::lastDay();
$begin = $end->copy()->subDay($nb); $begin = $end->copy()->subDays($nb);
return static::getPeriodsByDay($begin, $end); return static::getPeriodsByDay($begin, $end);
} }
@@ -153,7 +153,7 @@ class DateRange
$date = Carbon::now()->startOfQuarter(); $date = Carbon::now()->startOfQuarter();
break; break;
case 4: case 4:
$date = Carbon::now()->subMonth(3)->startOfQuarter(); $date = Carbon::now()->subMonths(3)->startOfQuarter();
break; break;
default: default:
return false; return false;
@@ -169,17 +169,17 @@ class DateRange
public static function lastMonth() public static function lastMonth()
{ {
return Carbon::now()->subMonth()->startOfMonth(); return Carbon::now()->subMonths()->startOfMonth();
} }
public static function lastWeek() public static function lastWeek()
{ {
return Carbon::now()->subWeek()->startOfWeek(); return Carbon::now()->subWeeks()->startOfWeek();
} }
public static function lastDay() public static function lastDay()
{ {
return Carbon::now()->subDay()->startOfDay(); return Carbon::now()->subDays()->startOfDay();
} }
public static function getPeriodsByMonth($begin, $end, $interval = 1) public static function getPeriodsByMonth($begin, $end, $interval = 1)

View File

@@ -7,12 +7,83 @@ use Illuminate\Database\Eloquent\Model;
trait DateStats trait DateStats
{ {
public static function countByMonth($prev = 0) public static function sumDaily($field, $nbDays = 15, $fieldDate = 'created_at')
{ {
$start = Carbon::now()->startOfMonth($prev); $periods = DateRange::getPeriodsLastDay($nbDays);
$end = Carbon::now()->endOfMonth($prev); $values = self::sumPeriodly($periods, $field, $fieldDate);
return self::countByPeriod($start, $end); return self::buildLabels($periods, $values, 'dd Do MMM');
}
public static function countDaily($nbDays = 15, $fieldDate = 'created_at')
{
$periods = DateRange::getPeriodsLastDay($nbDays);
$values = self::countPeriodly($periods, $fieldDate);
return self::buildLabels($periods, $values, 'dd Do MMM');
}
public static function sumMonthly($field, $fieldDate = 'created_at')
{
$periods = DateRange::getPeriodsLastMonth(12);
$values = self::sumPeriodly($periods, $field, $fieldDate);
return self::buildLabels($periods, $values, 'MMM YY');
}
public static function countMonthly($fieldDate = 'created_at')
{
$periods = DateRange::getPeriodsLastMonth(12);
$values = self::countPeriodly($periods, $fieldDate);
return self::buildLabels($periods, $values, 'MMM YY');
}
public static function buildLabels($periods, $values, $format = 'MMM YY')
{
$data = [];
foreach ($periods as $key => $period) {
$index = Carbon::parse($period->startDate)->isoFormat($format);
$data[$index] = $values[$key];
}
return $data;
}
public static function sumPeriodly($periods, $field, $fieldDate = 'created_at')
{
$data = [];
foreach ($periods as $period) {
$data[] = self::sumByPeriod($period->startDate, $period->endDate, $field, $fieldDate);
}
return $data;
}
public static function countPeriodly($periods, $fieldDate = 'created_at')
{
$data = [];
foreach ($periods as $period) {
$data[] = self::countByPeriod($period->startDate, $period->endDate, $fieldDate);
}
return $data;
}
public static function countByMonth($prev = 0, $field = 'created_at')
{
$start = Carbon::now()->subMonths($prev)->startOfMonth();
$end = Carbon::now()->subMonths($prev)->endOfMonth();
return self::countByPeriod($start, $end, $field);
}
public static function countByDay($prev = 0, $field = 'created_at')
{
$start = Carbon::now()->subDays($prev)->startOfMonth();
$end = Carbon::now()->subDays($prev)->endOfMonth();
return self::countByPeriod($start, $end, $field);
} }
public static function countByPeriod($start, $end, $field = 'created_at') public static function countByPeriod($start, $end, $field = 'created_at')
@@ -20,32 +91,48 @@ trait DateStats
return self::getModel()->whereBetween($field, [$start, $end])->count(); return self::getModel()->whereBetween($field, [$start, $end])->count();
} }
public static function avgByMonth($prev = 0, $field = false) public static function avgByMonth($field, $prev = 0, $fieldDate = 'created_at')
{ {
$start = Carbon::now()->startOfMonth($prev); $start = Carbon::now()->subMonths($prev)->startOfMonth();
$end = Carbon::now()->endOfMonth($prev); $end = Carbon::now()->subMonths($prev)->endOfMonth();
return self::avgByPeriod($start, $end, $field); return self::avgByPeriod($start, $end, $field, $fieldDate);
} }
public static function avgByPeriod($start, $end, $field) public static function avgByDay($field, $prev = 0, $fieldDate = 'created_at')
{
$start = Carbon::now()->subDays($prev)->startOfMonth();
$end = Carbon::now()->subDays($prev)->endOfMonth();
return self::avgByPeriod($start, $end, $field, $fieldDate);
}
public static function avgByPeriod($start, $end, $field, $fieldDate = 'created_at')
{ {
$c = self::countByPeriod($start, $end); $c = self::countByPeriod($start, $end);
return $c ? round(self::sumByPeriod($start, $end, $field) / $c, 2) : 0; return $c ? round(self::sumByPeriod($start, $end, $field, $fieldDate) / $c, 2) : 0;
} }
public static function sumByMonth($prev = 0, $field = false) public static function sumByMonth($field, $prev = 0, $fieldDate = 'created_at')
{ {
$start = Carbon::now()->startOfMonth($prev); $start = Carbon::now()->subMonths($prev)->startOfMonth();
$end = Carbon::now()->endOfMonth($prev); $end = Carbon::now()->subMonths($prev)->endOfMonth();
return self::sumByPeriod($start, $end, $field); return self::sumByPeriod($start, $end, $field, $fieldDate);
} }
public static function sumByPeriod($start, $end, $field = 'created_at') public static function sumByDay($field, $prev = 0, $fieldDate = 'created_at')
{ {
return self::getModel()->whereBetween($field, [$start, $end])->sum($field); $start = Carbon::now()->subDays($prev)->startOfDay();
$end = Carbon::now()->subDays($prev)->endOfDay();
return self::sumByPeriod($start, $end, $field, $fieldDate);
}
public static function sumByPeriod($start, $end, $field, $fieldDate = 'created_at')
{
return self::getModel()->whereBetween($fieldDate, [$start, $end])->sum($field);
} }
public static function getModel() public static function getModel()

View File

@@ -4,10 +4,21 @@ namespace App\Repositories\Shop;
use App\Models\Shop\Order; use App\Models\Shop\Order;
use App\Repositories\Core\DateStats; use App\Repositories\Core\DateStats;
use App\Traits\Model\Basic;
class OrderStatistics class OrderStatistics
{ {
use DateStats; use Basic, DateStats;
public static function getTotalDaily()
{
return self::sumDaily('total_taxed');
}
public static function getTotalMonthly()
{
return self::sumMonthly('total_taxed');
}
public static function countPreparationLess2Days() public static function countPreparationLess2Days()
{ {
@@ -49,4 +60,9 @@ class OrderStatistics
{ {
return Order::ofLastMonth()->sum('total_taxed'); return Order::ofLastMonth()->sum('total_taxed');
} }
public static function getModel()
{
return Order::query();
}
} }

View File

@@ -1,40 +1,40 @@
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
<div class="card"> <div class="card">
<div class="card-header"> <div class="card-header">
<h3 class="card-title">Evolution du CA</h3> <h3 class="card-title">Evolution du CA</h3>
<div class="card-tools"> <div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse"> <button type="button" class="btn btn-tool" data-card-widget="collapse">
<i class="fas fa-minus"></i> <i class="fas fa-minus"></i>
</button> </button>
<div class="btn-group"> <div class="btn-group">
<button type="button" class="btn btn-tool dropdown-toggle" data-toggle="dropdown"> <button type="button" class="btn btn-tool dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-wrench"></i> <i class="fa fa-wrench"></i>
</button> </button>
<div class="dropdown-menu dropdown-menu-right" role="menu"> <div class="dropdown-menu dropdown-menu-right" role="menu">
<a href="#" class="dropdown-item">Par semaine</a> <a href="#" class="dropdown-item">Par semaine</a>
<a href="#" class="dropdown-item">Par mois</a> <a href="#" class="dropdown-item">Par mois</a>
<a class="dropdown-divider"></a> <a class="dropdown-divider"></a>
<a href="#" class="dropdown-item">Autre chose</a> <a href="#" class="dropdown-item">Autre chose</a>
</div> </div>
</div> </div>
<button type="button" class="btn btn-tool" data-widget="remove"> <button type="button" class="btn btn-tool" data-widget="remove">
<i class="fa fa-times"></i> <i class="fa fa-times"></i>
</button> </button>
</div> </div>
</div> </div>
<!-- /.box-header --> <!-- /.box-header -->
<div class="card-body"> <div class="card-body">
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
@include('Admin.Shop.Dashboard.components.chart') {!! $chart->container() !!}
</div> </div>
</div> </div>
</div> </div>
<div class="card-footer"> <div class="card-footer">
@include('Admin.Shop.Dashboard._partials.evolutions') @include('Admin.Shop.Dashboard._partials.evolutions')
</div> </div>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -5,7 +5,6 @@
@include('boilerplate::logs.style') @include('boilerplate::logs.style')
@section('content') @section('content')
<div class="btn-group mb-3" role="group" aria-label="" class="d-none"> <div class="btn-group mb-3" role="group" aria-label="" class="d-none">
<button type="button" class="btn btn-sm btn-secondary text-nowrap" data-id="day">jour</button> <button type="button" class="btn btn-sm btn-secondary text-nowrap" data-id="day">jour</button>
<button type="button" class="btn btn-sm btn-secondary text-nowrap" data-id="month">mois</button> <button type="button" class="btn btn-sm btn-secondary text-nowrap" data-id="month">mois</button>
@@ -23,15 +22,16 @@
@include('Admin.Shop.Dashboard._partials.counter') @include('Admin.Shop.Dashboard._partials.counter')
</div> </div>
<div class="col-lg-9"> <div class="col-lg-9">
@include('Admin.Shop.Dashboard._partials.report') @include('Admin.Shop.Dashboard._partials.report')
</div> </div>
</div> </div>
@endsection @endsection
@include('load.form.daterangepicker') @include('load.form.daterangepicker')
@push('js') @push('js')
@apexchartsScripts
{!! $chart->script() !!}
<script> <script>
initDaterangepicker(); initDaterangepicker();
</script> </script>