AbstractReport.php
3 years ago
Orders.php
3 years ago
PaymentMethods.php
3 years ago
Refunds.php
4 years ago
ReportFilterData.php
4 years ago
ReportInterval.php
4 years ago
Revenue.php
4 years ago
SettingsPage.php
1 year ago
Taxes.php
4 years ago
TopPlans.php
4 years ago
index.php
4 years ago
Orders.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\Membership\DashboardPage; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\Order\OrderStatus; |
| 6 | use ProfilePress\Core\Membership\Models\Order\OrderType; |
| 7 | |
| 8 | class Orders extends AbstractReport |
| 9 | { |
| 10 | public function get_total($start_date = '', $end_date = '') |
| 11 | { |
| 12 | static $bucket = []; |
| 13 | |
| 14 | $cache_key = md5('orders_get_total' . $start_date . $end_date); |
| 15 | |
| 16 | if ( ! isset($bucket[$cache_key])) { |
| 17 | $start_date = ! empty($start_date) ? $start_date : ''; |
| 18 | $end_date = ! empty($end_date) ? $end_date : ''; |
| 19 | |
| 20 | $where_clause = $this->get_where_clause(false, $start_date, $end_date); |
| 21 | |
| 22 | $bucket[$cache_key] = $this->wpdb()->get_var( |
| 23 | $this->wpdb()->prepare( |
| 24 | "SELECT COUNT(id) FROM {$this->db_table} WHERE status = %s AND $where_clause", OrderStatus::COMPLETED |
| 25 | ) |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | return ! $bucket[$cache_key] ? 0 : $bucket[$cache_key]; |
| 30 | } |
| 31 | |
| 32 | public function get_data() |
| 33 | { |
| 34 | $new_orders = $this->wpdb()->get_results( |
| 35 | $this->wpdb()->prepare( |
| 36 | "SELECT COUNT(id) AS order_total, {$this->get_date_column()} FROM {$this->db_table} WHERE status = %s AND order_type = %s AND {$this->get_where_clause($groupBy=true)}", |
| 37 | OrderStatus::COMPLETED, OrderType::NEW_ORDER |
| 38 | ), |
| 39 | 'ARRAY_A' |
| 40 | ); |
| 41 | |
| 42 | $renewal_orders = $this->wpdb()->get_results( |
| 43 | $this->wpdb()->prepare( |
| 44 | "SELECT COUNT(id) AS order_total, {$this->get_date_column()} FROM {$this->db_table} WHERE status = %s AND order_type = %s AND {$this->get_where_clause($groupBy=true)}", |
| 45 | OrderStatus::COMPLETED, OrderType::RENEWAL_ORDER |
| 46 | ), |
| 47 | 'ARRAY_A' |
| 48 | ); |
| 49 | |
| 50 | $upgrade_orders = $this->wpdb()->get_results( |
| 51 | $this->wpdb()->prepare( |
| 52 | "SELECT COUNT(id) AS order_total, {$this->get_date_column()} FROM {$this->db_table} WHERE status = %s AND order_type = %s AND {$this->get_where_clause($groupBy=true)}", |
| 53 | OrderStatus::COMPLETED, OrderType::UPGRADE |
| 54 | ), |
| 55 | 'ARRAY_A' |
| 56 | ); |
| 57 | |
| 58 | $downgrade_orders = $this->wpdb()->get_results( |
| 59 | $this->wpdb()->prepare( |
| 60 | "SELECT COUNT(id) AS order_total, {$this->get_date_column()} FROM {$this->db_table} WHERE status = %s AND order_type = %s AND {$this->get_where_clause($groupBy=true)}", |
| 61 | OrderStatus::COMPLETED, OrderType::DOWNGRADE |
| 62 | ), |
| 63 | 'ARRAY_A' |
| 64 | ); |
| 65 | |
| 66 | $no_dataset = []; |
| 67 | $ro_dataset = []; |
| 68 | $uo_dataset = []; |
| 69 | $do_dataset = []; |
| 70 | |
| 71 | foreach ($this->get_labels() as $datetime => $label) { |
| 72 | |
| 73 | $no_data = $this->get_interval_data($new_orders, $datetime); |
| 74 | $ro_data = $this->get_interval_data($renewal_orders, $datetime); |
| 75 | $uo_data = $this->get_interval_data($upgrade_orders, $datetime); |
| 76 | $do_data = $this->get_interval_data($downgrade_orders, $datetime); |
| 77 | |
| 78 | $no_dataset[] = ! empty($no_data) ? reset($no_data)['order_total'] : 0; |
| 79 | $ro_dataset[] = ! empty($ro_data) ? reset($ro_data)['order_total'] : 0; |
| 80 | $uo_dataset[] = ! empty($uo_data) ? reset($uo_data)['order_total'] : 0; |
| 81 | $do_dataset[] = ! empty($do_data) ? reset($do_data)['order_total'] : 0; |
| 82 | } |
| 83 | |
| 84 | return [ |
| 85 | [ |
| 86 | 'label' => esc_html__('New', 'wp-user-avatar'), |
| 87 | 'data' => $no_dataset, |
| 88 | 'borderColor' => 'rgb(54, 162, 235)', |
| 89 | 'backgroundColor' => 'rgba(54, 162, 235, 0.5)' |
| 90 | ], |
| 91 | [ |
| 92 | 'label' => esc_html__('Renewals', 'wp-user-avatar'), |
| 93 | 'data' => $ro_dataset, |
| 94 | 'borderColor' => 'rgb(255, 99, 132)', |
| 95 | 'backgroundColor' => 'rgba(255, 99, 132, 0.5)' |
| 96 | ], |
| 97 | [ |
| 98 | 'label' => esc_html__('Upgrades', 'wp-user-avatar'), |
| 99 | 'data' => $uo_dataset, |
| 100 | 'borderColor' => 'rgb(75,192,192)', |
| 101 | 'backgroundColor' => 'rgba(75,192,192, 0.5)' |
| 102 | ], |
| 103 | [ |
| 104 | 'label' => esc_html__('Downgrades', 'wp-user-avatar'), |
| 105 | 'data' => $do_dataset, |
| 106 | 'borderColor' => 'rgb(51,53,56)', |
| 107 | 'backgroundColor' => 'rgba(51,53,56, 0.5)' |
| 108 | ] |
| 109 | ]; |
| 110 | } |
| 111 | } |