175 lines
5.7 KiB
PHP
175 lines
5.7 KiB
PHP
<?php
|
|
class Crunchbutton_Chart_Order extends Crunchbutton_Chart {
|
|
|
|
public $unit = 'orders';
|
|
public $description = 'Orders';
|
|
|
|
public function __construct() {
|
|
parent::__construct();
|
|
}
|
|
|
|
public function byWeekdayByCommunity( $render = false ){
|
|
$query = "SELECT DATE_FORMAT(CONVERT_TZ(`date`, '-8:00', '-5:00'), '%W') AS `Day`,
|
|
COUNT(*) AS `Orders`,
|
|
c.name AS `Community`
|
|
FROM `order` o
|
|
LEFT JOIN community c ON o.id_community = c.id_community
|
|
WHERE env = 'live'
|
|
{$this->queryExcludeCommunties}
|
|
{$this->queryExcludeUsers}
|
|
AND c.id_community IN (1, 4)
|
|
GROUP BY DATE_FORMAT(CONVERT_TZ(`date`, '-8:00', '-5:00'), '%W'),
|
|
c.id_community
|
|
ORDER BY DATE_FORMAT(CONVERT_TZ(`date`, '-8:00', '-5:00'), '%Y%m%d'),
|
|
c.id_community";
|
|
|
|
$data = c::db()->get( $query );
|
|
if( $render ){
|
|
return array( 'data' => $data, 'unit' => $this->unity );
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public function byMonth( $render = false ){
|
|
$query = "SELECT DATE_FORMAT( o.date ,'%Y-%m') AS Month,
|
|
COUNT(*) AS Total
|
|
FROM `order` o
|
|
INNER JOIN user u ON u.id_user = o.id_user
|
|
LEFT JOIN community c ON o.id_community = c.id_community
|
|
WHERE
|
|
o.date >= '{$this->monthFrom}-01' AND o.date <= LAST_DAY( STR_TO_DATE( '{$this->monthTo}', '%Y-%m' ) )
|
|
{$this->queryExcludeCommunties}
|
|
{$this->queryExcludeUsers}
|
|
GROUP BY Month
|
|
ORDER BY Month ASC";
|
|
|
|
$parsedData = $this->parseDataMonthSimple( $query, $this->description );
|
|
if( $render ){
|
|
return array( 'data' => $parsedData, 'unit' => $this->unity, 'interval' => 'month' );
|
|
}
|
|
return $parsedData;
|
|
}
|
|
|
|
public function byWeek( $render = false ){
|
|
|
|
$query = "SELECT YEARWEEK(date) AS Week,
|
|
COUNT(*) AS Total
|
|
FROM `order` o
|
|
INNER JOIN user u ON u.id_user = o.id_user
|
|
LEFT JOIN community c ON o.id_community = c.id_community
|
|
WHERE
|
|
YEARWEEK(o.date) >= {$this->weekFrom} AND YEARWEEK(o.date) <= {$this->weekTo}
|
|
{$this->queryExcludeCommunties}
|
|
{$this->queryExcludeUsers}
|
|
GROUP BY YEARWEEK(date)
|
|
ORDER BY YEARWEEK(date) ASC";
|
|
|
|
$parsedData = $this->parseDataWeeksSimple( $query, $this->description );
|
|
if( $render ){
|
|
return array( 'data' => $parsedData, 'unit' => $this->unity );
|
|
}
|
|
return $parsedData;
|
|
}
|
|
|
|
public function byWeekPerCommunity( $render = false ){
|
|
|
|
$query = "SELECT YEARWEEK(date) AS Week,
|
|
COUNT(*) AS Total,
|
|
c.name AS 'Group'
|
|
FROM `order` o
|
|
INNER JOIN user u ON u.id_user = o.id_user
|
|
LEFT JOIN community c ON o.id_community = c.id_community
|
|
WHERE YEARWEEK(o.date) >= {$this->weekFrom} AND YEARWEEK(o.date) <= {$this->weekTo}
|
|
{$this->queryExcludeCommunties}
|
|
{$this->queryOnlyCommunties}
|
|
{$this->queryExcludeUsers}
|
|
GROUP BY YEARWEEK(date), c.name
|
|
ORDER BY YEARWEEK(date) DESC";
|
|
|
|
$parsedData = $this->parseDataWeeksGroup( $query, $this->description );
|
|
if( $render ){
|
|
return array( 'data' => $parsedData, 'unit' => $this->unity );
|
|
}
|
|
return $parsedData;
|
|
}
|
|
|
|
public function byUsersPerMonth( $render = false ){
|
|
|
|
$query = "SELECT DATE_FORMAT( o.date ,'%Y-%m') AS Month,
|
|
CAST(COUNT(*) / COUNT(DISTINCT((u.phone))) AS DECIMAL(14, 2)) Total
|
|
FROM `order` o
|
|
INNER JOIN user u ON u.id_user = o.id_user
|
|
LEFT JOIN community c ON o.id_community = c.id_community
|
|
WHERE o.date >= '{$this->monthFrom}-01' AND o.date <= LAST_DAY( STR_TO_DATE( '{$this->monthTo}', '%Y-%m' ) )
|
|
{$this->queryExcludeCommunties}
|
|
{$this->queryExcludeUsers}
|
|
GROUP BY Month
|
|
ORDER BY Month ASC";
|
|
|
|
$parsedData = $this->parseDataMonthSimple( $query, $this->description );
|
|
if( $render ){
|
|
return array( 'data' => $parsedData, 'unit' => $this->unity, 'interval' => 'month' );
|
|
}
|
|
return $parsedData;
|
|
}
|
|
|
|
public function byUsersPerWeek( $render = false ){
|
|
|
|
$query = "SELECT YEARWEEK(date) AS Week,
|
|
CAST(COUNT(*) / COUNT(DISTINCT((u.phone))) AS DECIMAL(14, 2)) Total
|
|
FROM `order` o
|
|
INNER JOIN user u ON u.id_user = o.id_user
|
|
LEFT JOIN community c ON o.id_community = c.id_community
|
|
WHERE YEARWEEK(o.date) >= {$this->weekFrom} AND YEARWEEK(o.date) <= {$this->weekTo}
|
|
{$this->queryExcludeCommunties}
|
|
{$this->queryExcludeUsers}
|
|
GROUP BY YEARWEEK(date)
|
|
ORDER BY YEARWEEK(date) DESC";
|
|
|
|
$parsedData = $this->parseDataWeeksSimple( $query, $this->description );
|
|
if( $render ){
|
|
return array( 'data' => $parsedData, 'unit' => $this->unity );
|
|
}
|
|
return $parsedData;
|
|
}
|
|
|
|
public function repeatByActiveuserByWeek( $render = false ){
|
|
|
|
$user = new Crunchbutton_Chart_User();
|
|
|
|
$activeUsers = $user->activeByWeek();
|
|
$newUsers = $user->newByWeek();
|
|
$orders = $this->byWeek();
|
|
|
|
$data = [];
|
|
for( $i = 0; $i < sizeof( $activeUsers ); $i++ ){
|
|
$active = $activeUsers[ $i ]->Total;
|
|
$order = $orders[ $i ]->Total;
|
|
$new = $newUsers[ $i ]->Total;
|
|
if( $i - 1 >= 0 ){
|
|
$activePrev = $activeUsers[ $i - 1 ]->Total;
|
|
} else {
|
|
$activePrev = 0;
|
|
}
|
|
|
|
// Formula (Orders minus New Users) / (Active Users) | Active Users = ( average of the current week and previous week's Active Users )
|
|
$activeUsersAvg = ( $active + $activePrev ) / 2;
|
|
|
|
$ordersMinusNewUsers = $order - $new;
|
|
|
|
if( $ordersMinusNewUsers != 0 && $activeUsersAvg != 0 ){
|
|
$result = ( $order - $new ) / ( $activeUsersAvg );
|
|
} else {
|
|
$result = 0;
|
|
}
|
|
|
|
$data[] = ( object ) array( 'Label' => $activeUsers[ $i ]->Label, 'Total' => number_format( $result, 4 ), 'Type' => 'Total' );
|
|
}
|
|
|
|
if( $render ){
|
|
return array( 'data' => $data, 'unit' => $this->unity );
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
} |