| @@ -25,13 +25,16 @@ | ||
| 25 | 25 | 'coupon_code', |
| 26 | 26 | 'coupon_discount', |
| 27 | 27 | 'subtotal', |
| 28 | 28 | 'total', |
| 29 | + 'currency', | |
| 29 | 30 | 'payment_method', |
| 30 | 31 | 'payment_intent_id', |
| 31 | 32 | 'payment_transaction_id', |
| 32 | 33 | 'created_at', |
| 33 | - 'updated_at' | |
| 34 | + 'updated_at', | |
| 35 | + 'custom_fields', | |
| 36 | + 'directorist_id', | |
| 34 | 37 | ]; |
| 35 | 38 | |
| 36 | 39 | /** |
| 37 | 40 | * Create a new order record |
| @@ -59,6 +62,69 @@ | ||
| 59 | 62 | protected function generate_order_no() { |
| 60 | 63 | $randomNum = time(); |
| 61 | 64 | |
| 62 | 65 | return 'BKNG-' . $randomNum; |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * Calculate total revenue for fully paid orders within a date range | |
| 70 | + * Excludes canceled/cancelled orders | |
| 71 | + * | |
| 72 | + * @param string $start_date Start date (Y-m-d format) | |
| 73 | + * @param string $end_date End date (Y-m-d format) | |
| 74 | + * @param array $order_ids Optional array of order IDs to filter by | |
| 75 | + * | |
| 76 | + * @return float Total revenue | |
| 77 | + */ | |
| 78 | + public function get_revenue_by_date_range( string $start_date, string $end_date, array $order_ids = array() ) { | |
| 79 | + $query = $this->query() | |
| 80 | + ->where( 'payment_status', '=', 'fully_paid' ) | |
| 81 | + ->where( 'status', '!=', 'canceled' ) | |
| 82 | + ->where( 'status', '!=', 'cancelled' ) | |
| 83 | + ->where( 'created_at', '>=', $start_date . ' 00:00:00' ) | |
| 84 | + ->where( 'created_at', '<=', $end_date . ' 23:59:59' ); | |
| 85 | + | |
| 86 | + if ( ! empty( $order_ids ) ) { | |
| 87 | + $query->where_in( 'id', $order_ids ); | |
| 88 | + } | |
| 89 | + | |
| 90 | + $sum = $query->sum( 'total' ); | |
| 91 | + | |
| 92 | + // Additional safety check to ensure we always return a valid float | |
| 93 | + if ( !is_numeric( $sum ) || !is_finite( $sum ) ) { | |
| 94 | + return 0.0; | |
| 95 | + } | |
| 96 | + | |
| 97 | + return (float) $sum; | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * Calculate revenue for a specific date range with custom filters | |
| 102 | + * | |
| 103 | + * @param string $start_date Start date (Y-m-d format) | |
| 104 | + * @param string $end_date End date (Y-m-d format) | |
| 105 | + * @param callable|null $additional_conditions Callback to add additional conditions to the query | |
| 106 | + * | |
| 107 | + * @return float Total revenue | |
| 108 | + */ | |
| 109 | + public function get_revenue_with_conditions( string $start_date, string $end_date, ?callable $additional_conditions = null ) { | |
| 110 | + $query = $this->query() | |
| 111 | + ->where( 'payment_status', '=', 'fully_paid' ) | |
| 112 | + ->where( 'status', '!=', 'canceled' ) | |
| 113 | + ->where( 'status', '!=', 'cancelled' ) | |
| 114 | + ->where( 'created_at', '>=', $start_date . ' 00:00:00' ) | |
| 115 | + ->where( 'created_at', '<=', $end_date . ' 23:59:59' ); | |
| 116 | + | |
| 117 | + if ( $additional_conditions && is_callable( $additional_conditions ) ) { | |
| 118 | + $additional_conditions( $query ); | |
| 119 | + } | |
| 120 | + | |
| 121 | + $sum = $query->sum( 'total' ); | |
| 122 | + | |
| 123 | + // Additional safety check to ensure we always return a valid float | |
| 124 | + if ( !is_numeric( $sum ) || !is_finite( $sum ) ) { | |
| 125 | + return 0.0; | |
| 126 | + } | |
| 127 | + | |
| 128 | + return (float) $sum; | |
| 63 | 129 | } |
| 64 | 130 | } |