| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Admin\Components\Analytics\Coupon; |
| 4 |
|
| 5 |
use Sellkit\Admin\Components\Analytics; |
| 6 |
use Sellkit\Admin\Components\Analytics\Analytics_Base; |
| 7 |
use Sellkit\Database; |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || die(); |
| 10 |
|
| 11 |
/** |
| 12 |
* Components class. |
| 13 |
* |
| 14 |
* @since 1.1.0 |
| 15 |
*/ |
| 16 |
class Total_Discount extends Analytics_Base { |
| 17 |
|
| 18 |
/** |
| 19 |
* Class constructor. |
| 20 |
* |
| 21 |
* @since 1.1.0 |
| 22 |
* @param int $rule_id Discount id. |
| 23 |
*/ |
| 24 |
public function __construct( $rule_id ) { |
| 25 |
$this->target_id = $rule_id; |
| 26 |
|
| 27 |
$this->set_coupon_details(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Set discount details. |
| 32 |
* |
| 33 |
* @since 1.1.0 |
| 34 |
*/ |
| 35 |
public function set_coupon_details() { |
| 36 |
global $wpdb; |
| 37 |
|
| 38 |
$start_date = time() - ( 60 * 60 * 24 * Analytics::$date_range ); |
| 39 |
|
| 40 |
$sellkit_prefix = Database::DATABASE_PREFIX; |
| 41 |
$coupon_table = "{$wpdb->prefix}{$sellkit_prefix}applied_coupon"; |
| 42 |
|
| 43 |
// phpcs:disable |
| 44 |
$total_discounts = $wpdb->get_results( |
| 45 |
$wpdb->prepare( "SELECT FROM_UNIXTIME(applied_at, '%%b_%%d') as `day`, SUM( total_discount ) as total FROM {$coupon_table} |
| 46 |
where applied_at > {$start_date} and rule_id " . Analytics::target_id_condition( $this->target_id ) . " %d |
| 47 |
GROUP BY `day` ORDER BY `day` ASC", $this->target_id ), |
| 48 |
ARRAY_A ); |
| 49 |
// phpcs:enable |
| 50 |
|
| 51 |
$prepared_coupons = []; |
| 52 |
foreach ( $total_discounts as $total_discount ) { |
| 53 |
$prepared_coupons[ $total_discount['day'] ] = $total_discount['total']; |
| 54 |
} |
| 55 |
|
| 56 |
$this->output = $prepared_coupons; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Getting chart data. |
| 61 |
* |
| 62 |
* @since 1.1.0 |
| 63 |
*/ |
| 64 |
public function get_data() { |
| 65 |
$chart_data = []; |
| 66 |
|
| 67 |
for ( $i = Analytics::$date_range; $i >= 0; $i-- ) { |
| 68 |
$time = strtotime( "-{$i} days" ); |
| 69 |
$date = date( 'M_d', $time ); |
| 70 |
|
| 71 |
$chart_data[] = [ |
| 72 |
'date' => str_replace( '_', ' ', $date ), |
| 73 |
'value' => ! empty( $this->output[ $date ] ) ? floatval( $this->output[ $date ] ) : 0, |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
return $chart_data; |
| 78 |
} |
| 79 |
} |
| 80 |
|