You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
4.4 KiB
156 lines
4.4 KiB
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Controllers\AdminController;
|
|
use App\Models\OrderItems;
|
|
use App\Models\Orders;
|
|
use App\Models\Products;
|
|
use App\Models\Recouvrement;
|
|
|
|
|
|
class PerformanceController extends AdminController
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
private $pageTitle = 'Performance';
|
|
|
|
public function index()
|
|
{
|
|
$this->verifyRole('viewPerformance');
|
|
|
|
$data = [
|
|
'user_permission' => $this->permission,
|
|
'page_title' => $this->pageTitle
|
|
];
|
|
|
|
$this->render_template('performance/index', $data);
|
|
}
|
|
|
|
|
|
|
|
public function fetchPerformanceData()
|
|
{
|
|
helper(['url', 'form']);
|
|
|
|
$Orders = new Orders();
|
|
$OrderItems = new OrderItems();
|
|
$Products = new Products();
|
|
|
|
$draw = intval($this->request->getVar('draw'));
|
|
$date = $this->request->getGet('date'); // format: YYYY-MM-DD / YYYY-W## / YYYY-MM
|
|
$type = $this->request->getGet('type'); // 'day', 'week', 'month'
|
|
|
|
if (!$type || !$date) {
|
|
$type = 'day';
|
|
$date = date('Y-m-d');
|
|
}
|
|
|
|
// Déterminer les données à récupérer
|
|
if ($type === 'day') {
|
|
$performance_data = $Orders->getUserPerformanceToday($date);
|
|
} elseif ($type === 'week') {
|
|
$performance_data = $Orders->getUserPerformanceByWeek($date);
|
|
} elseif ($type === 'month') {
|
|
$performance_data = $Orders->getUserPerformanceByMonth($date);
|
|
} else {
|
|
$performance_data = [];
|
|
}
|
|
|
|
$totalOrdersAllUsers = 0;
|
|
$totalSalesAllUsers = 0;
|
|
$totalProductPricesAllUsers = 0;
|
|
$data = [];
|
|
|
|
foreach ($performance_data as $value) {
|
|
$userId = $value['user_id'];
|
|
$userName = $value['full_name'];
|
|
$orders = (int) $value['total_user_order'];
|
|
$sales = (float) $value['total_prix_vente'];
|
|
$orderIds = $value['order_ids'] ?? [];
|
|
|
|
$productIds = $OrderItems->getProductIds($orderIds);
|
|
$totalProductPrice = $Products->getTotalProductPriceByIds($productIds);
|
|
|
|
$totalOrdersAllUsers += $orders;
|
|
$totalSalesAllUsers += $sales;
|
|
$totalProductPricesAllUsers += $totalProductPrice;
|
|
|
|
|
|
$data[] = [
|
|
$userName,
|
|
number_format($orders, 0, '.', ' '),
|
|
number_format($sales - $totalProductPrice, 0, '.', ' ') . ' Ar'
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
return $this->response->setJSON([
|
|
'data' => $data
|
|
]);
|
|
}
|
|
|
|
public function fetchTotalPerformanceData()
|
|
{
|
|
helper(['url', 'form']);
|
|
|
|
$Orders = new Orders();
|
|
$OrderItems = new OrderItems();
|
|
$Products = new Products();
|
|
|
|
$draw = intval($this->request->getVar('draw'));
|
|
$date = $this->request->getGet('date'); // format: YYYY-MM-DD / YYYY-W## / YYYY-MM
|
|
$type = $this->request->getGet('type'); // 'day', 'week', 'month'
|
|
|
|
if (!$type || !$date) {
|
|
$type = 'day';
|
|
$date = date('Y-m-d');
|
|
}
|
|
|
|
// Déterminer les données à récupérer
|
|
if ($type === 'day') {
|
|
$performance_data = $Orders->getUserPerformanceToday($date);
|
|
} elseif ($type === 'week') {
|
|
$performance_data = $Orders->getUserPerformanceByWeek($date);
|
|
} elseif ($type === 'month') {
|
|
$performance_data = $Orders->getUserPerformanceByMonth($date);
|
|
} else {
|
|
$performance_data = [];
|
|
}
|
|
|
|
$totalOrdersAllUsers = 0;
|
|
$totalSalesAllUsers = 0;
|
|
$totalProductPricesAllUsers = 0;
|
|
$data = [];
|
|
|
|
foreach ($performance_data as $value) {
|
|
$userId = $value['user_id'];
|
|
$userName = $value['full_name'];
|
|
$orders = (int) $value['total_user_order'];
|
|
$sales = (float) $value['total_prix_vente'];
|
|
$orderIds = $value['order_ids'] ?? [];
|
|
|
|
$productIds = $OrderItems->getProductIds($orderIds);
|
|
$totalProductPrice = (float) $Products->getTotalProductPriceByIds($productIds);
|
|
|
|
$totalOrdersAllUsers += $orders;
|
|
$totalSalesAllUsers += $sales;
|
|
$totalProductPricesAllUsers += $totalProductPrice;
|
|
|
|
$data = [
|
|
"total_commande"=> $orders,
|
|
"total_benefice"=> number_format($totalSalesAllUsers - $totalProductPricesAllUsers, 0, '.', ' '),
|
|
];
|
|
}
|
|
|
|
return $this->response->setJSON($data);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|