Fix on invoices, add delivery reference, wip on dashboard concurrency requests designed on template
This commit is contained in:
@@ -13,9 +13,13 @@ class HomeController extends Controller
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
public function index()
|
||||
public function index(Request $request, $period = false)
|
||||
{
|
||||
$data = Dashboards::getStats();
|
||||
$data = $request->all();
|
||||
$start = $request->input('start');
|
||||
$end = $request->input('end');
|
||||
$data = Dashboards::getStats($start, $end);
|
||||
// dump($data);
|
||||
return view('Admin.Shop.Dashboard.index', $data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,33 +5,20 @@ namespace App\Http\Controllers\Admin\Shop;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
use App\Repositories\Core\Auth\Users;
|
||||
use App\Repositories\Shop\Dashboards;
|
||||
|
||||
class DashboardController extends Controller
|
||||
{
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the application dashboard.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public function index()
|
||||
public function index(Request $request)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if (Users::hasRole('admin')) {
|
||||
$dashboard = 'dashboard';
|
||||
$data = [];
|
||||
}
|
||||
$data = $request->all();
|
||||
$data = Dashboards::getStats($data);
|
||||
dump($data);
|
||||
|
||||
return view('Admin.Shop.Dashboard.index', $data);
|
||||
}
|
||||
|
||||
@@ -28,18 +28,19 @@ class InvoiceController extends Controller
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$data = Invoices::get($id);
|
||||
$data['invoice'] = Invoices::get($id)->toArray();
|
||||
return view('Admin.Shop.Invoices.view', $data);
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$data['customer'] = Invoices::get($id)->toArray();
|
||||
$data['invoice'] = Invoices::get($id, ['order.customer', 'order.address', 'order.detail'])->toArray();
|
||||
$data['statuses'] = Invoices::statuses();
|
||||
return view('Admin.Shop.Invoices.edit', $data);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
return Invoices::destroy($id);
|
||||
return Invoices::delete($id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,4 +31,14 @@ class Invoice extends Model
|
||||
{
|
||||
return $this->belongsToThrough(Customer::class, Order::class, null, '', [Customer::class => 'customer_id', Order::class => 'order_id']);
|
||||
}
|
||||
|
||||
public function scopeByUUID($query, $uuid)
|
||||
{
|
||||
return $query->where('uuid', $uuid);
|
||||
}
|
||||
|
||||
public function scopeByPeriod($query, $start, $end)
|
||||
{
|
||||
return $query->whereBetween('created_at', [$start, $end]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,11 @@ class Order extends Model
|
||||
return $this->belongsTo(SaleChannel::class);
|
||||
}
|
||||
|
||||
public function scopeByUUID($query, $uuid)
|
||||
{
|
||||
return $query->where('uuid', $uuid);
|
||||
}
|
||||
|
||||
public function scopeByCustomer($query, $customer_id)
|
||||
{
|
||||
return $query->where('customer_id', $customer_id);
|
||||
@@ -56,6 +61,21 @@ class Order extends Model
|
||||
return $query->where('delivery_id', $delivery_id);
|
||||
}
|
||||
|
||||
public function scopePreparation($query)
|
||||
{
|
||||
return $query->byStatus(1);
|
||||
}
|
||||
|
||||
public function scopeSended($query)
|
||||
{
|
||||
return $query->byStatus(2);
|
||||
}
|
||||
|
||||
public function scopeReceived($query)
|
||||
{
|
||||
return $query->byStatus(3);
|
||||
}
|
||||
|
||||
public function scopeByStatus($query, $status)
|
||||
{
|
||||
return $query->where('status', $status);
|
||||
@@ -65,4 +85,9 @@ class Order extends Model
|
||||
{
|
||||
return $query->where('payment_type', $payment_type);
|
||||
}
|
||||
|
||||
public function scopeByPeriod($query, $start, $end, $field = 'created_at')
|
||||
{
|
||||
return $query->whereBetween($field, [$start, $end]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,59 @@ use function League\Period\interval_after;
|
||||
|
||||
class DateRange
|
||||
{
|
||||
|
||||
public static function today()
|
||||
{
|
||||
return self::previousDay(0);
|
||||
}
|
||||
|
||||
public static function currentWeek()
|
||||
{
|
||||
return self::previousWeek(0);
|
||||
}
|
||||
|
||||
public static function currentMonth()
|
||||
{
|
||||
return self::previousMonth(0);
|
||||
}
|
||||
|
||||
public static function currentYear()
|
||||
{
|
||||
return self::previousYear(0);
|
||||
}
|
||||
|
||||
public static function previousDay($nb = 1)
|
||||
{
|
||||
return [
|
||||
'start' => Carbon::now()->subDay($nb)->startOfDay(),
|
||||
'end' => Carbon::now()->subDay($nb)->endOfDay(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function previousWeek($nb = 1)
|
||||
{
|
||||
return [
|
||||
'start' => Carbon::now()->subWeek($nb)->startOfWeek(),
|
||||
'end' => Carbon::now()->subWeek($nb)->endOfWeek(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function previousMonth($nb = 1)
|
||||
{
|
||||
return [
|
||||
'start' => Carbon::now()->subMonth($nb)->startOfMonth(),
|
||||
'end' => Carbon::now()->subMonth($nb)->endOfMonth(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function previousYear($nb = 1)
|
||||
{
|
||||
return [
|
||||
'start' => Carbon::now()->subYear($nb)->startOfYear(),
|
||||
'end' => Carbon::now()->subYear($nb)->endOfYear(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPeriodsLastMonthWithLabels($nb, $with_actual = true)
|
||||
{
|
||||
$periods = DateRange::PeriodsToCarbon(DateRange::getPeriodsLastMonth($nb, $with_actual));
|
||||
@@ -30,7 +83,7 @@ class DateRange
|
||||
public static function getPeriodsLastMonth($nb = 1, $with_actual = true)
|
||||
{
|
||||
$end = $with_actual ? 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()->subMonth($nb - 1);
|
||||
$t = self::getPeriodsbyMonth($begin, $end);
|
||||
return self::getPeriodsbyMonth($begin, $end);
|
||||
}
|
||||
@@ -58,14 +111,14 @@ class DateRange
|
||||
{
|
||||
$end = $with_actual ? Carbon::now()->endOfWeek() : self::lastWeek();
|
||||
$begin = $end->copy()->subWeek($nb);
|
||||
return static::getPeriodsbyWeek($begin, $end);
|
||||
return self::getPeriodsbyWeek($begin, $end);
|
||||
}
|
||||
|
||||
public static function getPeriodsLastDay($nb = 1, $with_actual = true)
|
||||
{
|
||||
$end = $with_actual ? Carbon::now()->endOfDay() : static::lastDay();
|
||||
$begin = $end->copy()->subDay($nb);
|
||||
return static::getPeriodsbyDay($begin, $end);
|
||||
return self::getPeriodsbyDay($begin, $end);
|
||||
}
|
||||
|
||||
public static function byDay()
|
||||
@@ -92,16 +145,16 @@ class DateRange
|
||||
{
|
||||
$quarter = Carbon::now()->quarter;
|
||||
switch ($quarter) {
|
||||
case 1:
|
||||
case 2:
|
||||
$date = Carbon::now()->startOfYear();
|
||||
break;
|
||||
case 3:
|
||||
$date = Carbon::now()->startOfQuarter();
|
||||
break;
|
||||
case 4:
|
||||
$date = Carbon::now()->subMonth(3)->startOfQuarter();
|
||||
break;
|
||||
case 1:
|
||||
case 2:
|
||||
$date = Carbon::now()->startOfYear();
|
||||
break;
|
||||
case 3:
|
||||
$date = Carbon::now()->startOfQuarter();
|
||||
break;
|
||||
case 4:
|
||||
$date = Carbon::now()->subMonth(3)->startOfQuarter();
|
||||
break;
|
||||
}
|
||||
return [$date, $date->addMonth(6)];
|
||||
}
|
||||
@@ -143,7 +196,7 @@ class DateRange
|
||||
|
||||
public static function getPeriods($begin, $end, $duration, $interval = 1)
|
||||
{
|
||||
$period = new Period($begin, $end);
|
||||
$period = new Period($begin, $end, $interval);
|
||||
foreach ($period->getDatePeriod($duration) as $day) {
|
||||
$daterange[] = interval_after($day, $duration);
|
||||
}
|
||||
|
||||
51
app/Repositories/Core/DateStats.php
Normal file
51
app/Repositories/Core/DateStats.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Core;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
Trait DateStats
|
||||
{
|
||||
public static function countByMonth($prev = 0)
|
||||
{
|
||||
$start = Carbon::now()->startOfMonth($prev);
|
||||
$end = Carbon::now()->endOfMonth($prev);
|
||||
return self::countByPeriod($start, $end);
|
||||
}
|
||||
|
||||
public static function countByPeriod($start, $end, $field = 'created_at')
|
||||
{
|
||||
return self::getModel()->whereBetween($field, [$start, $end])->count();
|
||||
}
|
||||
|
||||
public static function avgByMonth($prev = 0, $field = false)
|
||||
{
|
||||
$start = Carbon::now()->startOfMonth($prev);
|
||||
$end = Carbon::now()->endOfMonth($prev);
|
||||
return self::avgByPeriod($start, $end, $field);
|
||||
}
|
||||
|
||||
public static function avgByPeriod($start, $end, $field)
|
||||
{
|
||||
$c = self::countByPeriod($start, $end);
|
||||
return $c ? round(self::sumByPeriod($start, $end, $field) / $c, 2) : 0;
|
||||
}
|
||||
|
||||
public static function sumByMonth($prev = 0, $field = false)
|
||||
{
|
||||
$start = Carbon::now()->startOfMonth($prev);
|
||||
$end = Carbon::now()->endOfMonth($prev);
|
||||
return self::sumByPeriod($start, $end, $field);
|
||||
}
|
||||
|
||||
public static function sumByPeriod($start, $end, $field = 'created_at')
|
||||
{
|
||||
return self::getModel()->whereBetween($field, [$start, $end])->sum($field);
|
||||
}
|
||||
|
||||
public static function getModel()
|
||||
{
|
||||
return new Model;
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,11 @@ use App\Models\Shop\Customer;
|
||||
|
||||
class Customers
|
||||
{
|
||||
public static function count()
|
||||
{
|
||||
return Customer::count();
|
||||
}
|
||||
|
||||
public static function editProfile($id = false)
|
||||
{
|
||||
$id = $id ? $id : self::getId();
|
||||
|
||||
@@ -7,12 +7,17 @@ use App\Models\Shop\Homepage;
|
||||
class Dashboards
|
||||
{
|
||||
|
||||
public static function getStats()
|
||||
public static function getStats($start, $end)
|
||||
{
|
||||
return [
|
||||
'orders_count' => Orders::countByMonth(),
|
||||
'orders_sum' => Orders::sumByMonth(),
|
||||
'orders_avg' => Orders::avgByMonth(),
|
||||
'orders_count' => Orders::countByPeriod($start, $end),
|
||||
'orders_sum' => Orders::sumByPeriod($start, $end, 'total_shipped'),
|
||||
'orders_avg' => Orders::avgByPeriod($start, $end, 'total_shipped'),
|
||||
'offers_count' => Offers::count(),
|
||||
'clients_count' => Customers::count(),
|
||||
'preparationLess24H' => Orders::countInPreparationLess24H(),
|
||||
'preparationLess48H' => Orders::countInPreparationLess48H(),
|
||||
'preparationMore48H' => Orders::countInPreparationMore48H(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ class Invoices
|
||||
public static function getNewRef()
|
||||
{
|
||||
$ref = date('ym') . '00000';
|
||||
$last_ref = Invoice::orderBy('ref', 'desc')->where('ref', '>', $ref)->first();
|
||||
$last_ref = Invoice::orderBy('id', 'desc')->first();
|
||||
return $last_ref ? $last_ref->ref + 1 : $ref + 1;
|
||||
}
|
||||
|
||||
@@ -72,6 +72,6 @@ class Invoices
|
||||
|
||||
public static function statuses()
|
||||
{
|
||||
return ['En attente', 'Non soldée', 'Soldée'];
|
||||
return ['En attente', 'Paiement partiel', 'Soldée', 'Paiement rejeté'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,11 @@ use App\Repositories\Core\User\ShopCart;
|
||||
|
||||
class Offers
|
||||
{
|
||||
public static function count()
|
||||
{
|
||||
return Offer::count();
|
||||
}
|
||||
|
||||
public static function getFull($id, $sale_channel_id = false)
|
||||
{
|
||||
$sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID();
|
||||
|
||||
@@ -4,11 +4,15 @@ namespace App\Repositories\Shop;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use App\Models\Shop\Order;
|
||||
use App\Repositories\Core\DateStats;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Orders
|
||||
{
|
||||
|
||||
use DateStats;
|
||||
|
||||
public static function saveOrder($data)
|
||||
{
|
||||
$data += $data['basket'];
|
||||
@@ -31,21 +35,29 @@ class Orders
|
||||
return Order::count();
|
||||
}
|
||||
|
||||
public static function countByMonth()
|
||||
public static function countInPreparationLess24H()
|
||||
{
|
||||
$start = Carbon::now()->startOfMonth();
|
||||
return Order::where('created_at', '>', $start)->count();
|
||||
$start = Carbon::now()->subHours(24);
|
||||
$end = Carbon::now();
|
||||
return Order::preparation()->byPeriod($start, $end, 'updated_at')->count();
|
||||
}
|
||||
|
||||
public static function sumByMonth()
|
||||
public static function countInPreparationLess48H()
|
||||
{
|
||||
$start = Carbon::now()->startOfMonth();
|
||||
return Order::where('created_at', '>', $start)->sum('total_shipped');
|
||||
$start = Carbon::now()->subHours(48);
|
||||
$end = Carbon::now();
|
||||
return Order::preparation()->byPeriod($start, $end, 'updated_at')->count();
|
||||
}
|
||||
|
||||
public static function avgByMonth()
|
||||
public static function countInPreparationMore48H()
|
||||
{
|
||||
return self::countByMonth() ? round(self::sumByMonth() / self::countByMonth(), 2) : 0;
|
||||
$start = Carbon::now()->subHours(48);
|
||||
return Order::preparation()->where('updated_at', '>', $start)->count();
|
||||
}
|
||||
|
||||
public static function getTotalByPeriod($start, $end)
|
||||
{
|
||||
return self::sumByPeriod($start, $end, 'total_shipped');
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
@@ -53,7 +65,7 @@ class Orders
|
||||
return [
|
||||
'order' => self::get($id, ['customer', 'address', 'detail']),
|
||||
'statuses' => self::statuses(),
|
||||
'payment_types' => self::payment_types(),
|
||||
'payment_types' => self::paymentTypes(),
|
||||
'sale_channels' => SaleChannels::getOptions(),
|
||||
];
|
||||
}
|
||||
@@ -100,17 +112,23 @@ class Orders
|
||||
return self::statuses()[$id] ?? false;
|
||||
}
|
||||
|
||||
public static function getStatusByName($name)
|
||||
{
|
||||
$data = array_flip(self::statuses());
|
||||
return $data[$name] ?? '';
|
||||
}
|
||||
|
||||
public static function statuses()
|
||||
{
|
||||
return ['En attente', 'Expédié', 'Livré'];
|
||||
return ['En attente', 'Préparation', 'Expédié', 'Livré'];
|
||||
}
|
||||
|
||||
public static function getPaymentType($id)
|
||||
{
|
||||
return self::payment_types()[$id] ?? false;
|
||||
return self::paymentTypes()[$id] ?? false;
|
||||
}
|
||||
|
||||
public static function payment_types()
|
||||
public static function paymentTypes()
|
||||
{
|
||||
return ['', 'CARTE BANCAIRE', 'CHEQUE', 'VIREMENT BANCAIRE'];
|
||||
}
|
||||
@@ -118,8 +136,12 @@ class Orders
|
||||
public static function getNewRef()
|
||||
{
|
||||
$ref = date('ym') . '00000';
|
||||
$last_ref = Order::orderBy('ref', 'desc')->where('ref', '>', $ref)->first();
|
||||
$last_ref = Order::orderBy('id', 'desc')->first();
|
||||
return $last_ref ? $last_ref->ref + 1 : $ref + 1;
|
||||
}
|
||||
|
||||
public static function getModel()
|
||||
{
|
||||
return Order::query();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user