views
3 years ago
AbstractExport.php
1 year ago
CustomersExport.php
6 months ago
OrdersExport.php
3 years ago
PlansExport.php
3 years ago
ProductSalesExport.php
3 years ago
SalesEarningsExport.php
3 years ago
SettingsPage.php
1 year ago
SubscriptionsExport.php
3 years ago
index.php
3 years ago
SalesEarningsExport.php
82 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\Membership\ExportPage; |
| 4 | |
| 5 | use ProfilePress\Core\Base; |
| 6 | use ProfilePress\Core\Membership\Models\Order\OrderStatus; |
| 7 | use ProfilePressVendor\Carbon\CarbonImmutable; |
| 8 | |
| 9 | class SalesEarningsExport extends AbstractExport |
| 10 | { |
| 11 | protected function headers() |
| 12 | { |
| 13 | return [ |
| 14 | __('Date', 'wp-user-avatar'), |
| 15 | __('Sales', 'wp-user-avatar'), |
| 16 | __('Earnings', 'wp-user-avatar') |
| 17 | ]; |
| 18 | } |
| 19 | |
| 20 | public function get_data($page = 1, $limit = 9999) |
| 21 | { |
| 22 | global $wpdb; |
| 23 | |
| 24 | $start_date = $this->form['order-export-start'] ?? ''; |
| 25 | $end_date = $this->form['order-export-end'] ?? ''; |
| 26 | |
| 27 | $payment_method = $this->form['payment_method'] ?? ''; |
| 28 | |
| 29 | $plan_id = (int)$this->form['plan_id']; |
| 30 | $customer_id = (int)$this->form['customer_id']; |
| 31 | |
| 32 | $table = Base::orders_db_table(); |
| 33 | |
| 34 | $wp_timezone = wp_timezone(); |
| 35 | |
| 36 | $replacements = [OrderStatus::COMPLETED]; |
| 37 | $sql = "SELECT DATE(date_created) as date, COUNT(id) as sales, SUM(total) as total from $table WHERE status = %s"; |
| 38 | |
| 39 | if ( ! empty($plan_id)) { |
| 40 | $replacements[] = $plan_id; |
| 41 | $sql .= " AND plan_id = %d"; |
| 42 | } |
| 43 | |
| 44 | if ( ! empty($customer_id)) { |
| 45 | $replacements[] = $customer_id; |
| 46 | $sql .= " AND customer_id = %d"; |
| 47 | } |
| 48 | |
| 49 | if ( ! empty($payment_method)) { |
| 50 | $replacements[] = sanitize_text_field($payment_method); |
| 51 | $sql .= " AND payment_method = %s"; |
| 52 | } |
| 53 | |
| 54 | if ( ! empty($start_date)) { |
| 55 | $replacements[] = CarbonImmutable::parse($start_date, $wp_timezone)->startOfDay()->utc()->toDateTimeString(); |
| 56 | $sql .= " AND date_created >= %s"; |
| 57 | } |
| 58 | |
| 59 | if ( ! empty($end_date)) { |
| 60 | $replacements[] = CarbonImmutable::parse($end_date, $wp_timezone)->endOfDay()->utc()->toDateTimeString(); |
| 61 | $sql .= " AND date_created <= %s"; |
| 62 | } |
| 63 | |
| 64 | $sql .= " GROUP BY date"; |
| 65 | |
| 66 | $page = max(1, intval($page)); |
| 67 | |
| 68 | $offset = ($page - 1) * intval($limit); |
| 69 | |
| 70 | if ($limit > 0) { |
| 71 | $sql .= " LIMIT %d"; |
| 72 | $replacements[] = $limit; |
| 73 | } |
| 74 | |
| 75 | if ($offset > 0) { |
| 76 | $sql .= " OFFSET %d"; |
| 77 | $replacements[] = $offset; |
| 78 | } |
| 79 | |
| 80 | return $wpdb->get_results($wpdb->prepare($sql, $replacements), ARRAY_A); |
| 81 | } |
| 82 | } |