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.
 
 
 
 
 
 

72 lines
1.7 KiB

<?php
namespace App\Models;
use CodeIgniter\Model;
class Reports extends Model
{
/**
* table name
* @var string
*/
protected $table = 'orders';
/**
* total month
* @return array<int|string>
*/
private function months()
{
return ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
}
/**
* Getting the year of the orders (paid_status = 1)
* @return array
*/
public function getOrderYear()
{
$result = $this->where('paid_status', 1)->findAll();
$return_data = array_map(function($v) {
return date('Y', strtotime($v['date_time']));
}, $result);
return array_values(array_unique($return_data));
}
/**
* Getting the order reports based on the year and months
* @param mixed $year
* @return array<array>
*/
public function getOrderData($year)
{
if ($year) {
$months = $this->months();
// Fetch orders with paid_status = 1
$result = $this->where('paid_status', 1)->findAll();
$final_data = [];
foreach ($months as $month) {
$get_mon_year = $year . '-' . $month;
$final_data[$get_mon_year] = [];
foreach ($result as $v) {
$month_year = date('Y-m', strtotime($v['date_time']));
if ($get_mon_year == $month_year) {
$final_data[$get_mon_year][] = $v;
}
}
}
// die(var_dump($final_data));
return $final_data;
}
return [];
}
}