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
PaymentMethods.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\Membership\DashboardPage; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\Order\OrderStatus; |
| 6 | |
| 7 | class PaymentMethods extends AbstractReport |
| 8 | { |
| 9 | public function get_where_clause($groupBy = false, $start_date = '', $end_date = '') |
| 10 | { |
| 11 | $start_date = $this->start_date_carbon; |
| 12 | $end_date = $this->end_date_carbon; |
| 13 | |
| 14 | $sql = "date_created >= %s AND date_created <= %s"; |
| 15 | |
| 16 | $variables = [ |
| 17 | $start_date->utc()->toDateTimeString(), |
| 18 | $end_date->utc()->toDateTimeString() |
| 19 | ]; |
| 20 | |
| 21 | if ( ! empty($this->plan_id)) { |
| 22 | $sql .= " AND plan_id = %d"; |
| 23 | $variables[] = $this->plan_id; |
| 24 | } |
| 25 | |
| 26 | $sql .= " GROUP BY payment_method ORDER BY sales DESC LIMIT 10"; |
| 27 | |
| 28 | return $this->wpdb()->prepare($sql, $variables); |
| 29 | } |
| 30 | |
| 31 | protected function get_query_data() |
| 32 | { |
| 33 | static $cache = null; |
| 34 | |
| 35 | if (is_null($cache)) { |
| 36 | $cache = $this->wpdb()->get_results( |
| 37 | $this->wpdb()->prepare( |
| 38 | "SELECT payment_method, SUM(total) as earnings, COUNT(id) as sales FROM {$this->db_table} WHERE status = %s AND {$this->get_where_clause()}", |
| 39 | OrderStatus::COMPLETED |
| 40 | ), |
| 41 | 'ARRAY_A' |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | return $cache; |
| 46 | } |
| 47 | |
| 48 | public function get_interval_data($bucket, $payment_method) |
| 49 | { |
| 50 | return wp_list_filter($bucket, ['payment_method' => $payment_method]); |
| 51 | } |
| 52 | |
| 53 | public function get_total($start_date = '', $end_date = '') |
| 54 | { |
| 55 | return null; |
| 56 | } |
| 57 | |
| 58 | public function get_trend() |
| 59 | { |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | public function get_labels() |
| 64 | { |
| 65 | static $labels = null; |
| 66 | |
| 67 | if (is_null($labels)) { |
| 68 | |
| 69 | $labels = []; |
| 70 | |
| 71 | $query_data = $this->get_query_data(); |
| 72 | |
| 73 | if (is_array($query_data) && ! empty($query_data)) { |
| 74 | foreach ($query_data as $datum) { |
| 75 | $payment_method = $datum['payment_method']; |
| 76 | $payment_method_obj = ppress_get_payment_method($payment_method); |
| 77 | $labels[$payment_method] = $payment_method_obj ? $payment_method_obj->get_method_title() : $payment_method; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return $labels; |
| 83 | } |
| 84 | |
| 85 | public function get_data() |
| 86 | { |
| 87 | $query_data = $this->get_query_data(); |
| 88 | |
| 89 | $sales_dataset = []; |
| 90 | $earnings_dataset = []; |
| 91 | |
| 92 | if (is_array($query_data) && ! empty($query_data)) { |
| 93 | |
| 94 | foreach ($this->get_labels() as $payment_method => $label) { |
| 95 | |
| 96 | $data = $this->get_interval_data($query_data, $payment_method); |
| 97 | |
| 98 | $data = reset($data); |
| 99 | |
| 100 | $sales_dataset[] = $data['sales']; |
| 101 | $earnings_dataset[] = ppress_sanitize_amount($data['earnings']); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return [ |
| 106 | [ |
| 107 | 'label' => esc_html__('Sales', 'wp-user-avatar'), |
| 108 | 'data' => $sales_dataset, |
| 109 | 'borderColor' => 'rgb(54, 162, 235)', |
| 110 | 'backgroundColor' => 'rgba(54, 162, 235, 0.5)', |
| 111 | 'stack' => 'combined', |
| 112 | 'type' => 'bar', |
| 113 | 'yAxisID' => 'y' |
| 114 | ], |
| 115 | [ |
| 116 | 'label' => esc_html__('Earnings', 'wp-user-avatar'), |
| 117 | 'data' => $earnings_dataset, |
| 118 | 'borderColor' => 'rgb(255, 99, 132)', |
| 119 | 'backgroundColor' => 'rgba(255, 99, 132, 0.5)', |
| 120 | 'stack' => 'combined', |
| 121 | 'yAxisID' => 'y1' |
| 122 | ] |
| 123 | ]; |
| 124 | } |
| 125 | } |