| @@ -1,64 +1,130 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace Booktics\Models; | |
| 4 | - | |
| 5 | -use Booktics\Abstracts\Model; | |
| 6 | - | |
| 7 | -/** | |
| 8 | - * Class Order_Model | |
| 9 | - * | |
| 10 | - * Handles database operations for orders in the Booktics system. | |
| 11 | - * | |
| 12 | - * @package Booktics\Models | |
| 13 | - */ | |
| 14 | -class Order_Model extends Model { | |
| 15 | - protected $table = 'booktics_orders'; | |
| 16 | - protected $fillable = [ | |
| 17 | - 'id', | |
| 18 | - 'order_no', | |
| 19 | - 'customer_id', | |
| 20 | - 'customer_note', | |
| 21 | - 'status', | |
| 22 | - 'payment_status', | |
| 23 | - 'price_breakdown', | |
| 24 | - 'tax_total', | |
| 25 | - 'coupon_code', | |
| 26 | - 'coupon_discount', | |
| 27 | - 'subtotal', | |
| 28 | - 'total', | |
| 29 | - 'payment_method', | |
| 30 | - 'payment_intent_id', | |
| 31 | - 'payment_transaction_id', | |
| 32 | - 'created_at', | |
| 33 | - 'updated_at' | |
| 34 | - ]; | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * Create a new order record | |
| 38 | - * | |
| 39 | - * @param array $data Order data to be created | |
| 40 | - * | |
| 41 | - * @return mixed The created order record | |
| 42 | - */ | |
| 43 | - public function create( array $data ) { | |
| 44 | - if ( empty( $data['order_no'] ) ) { | |
| 45 | - $data['order_no'] = $this->generate_order_no(); | |
| 46 | - } | |
| 47 | - | |
| 48 | - $data['price_breakdown'] = wp_json_encode( $data['price_breakdown'] ); | |
| 49 | - | |
| 50 | - return parent::create( $data ); | |
| 51 | - } | |
| 52 | - | |
| 53 | - | |
| 54 | - /** | |
| 55 | - * Generate a unique order number | |
| 56 | - * | |
| 57 | - * @return string | |
| 58 | - */ | |
| 59 | - protected function generate_order_no() { | |
| 60 | - $randomNum = time(); | |
| 61 | - | |
| 62 | - return 'BKNG-' . $randomNum; | |
| 63 | - } | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace Booktics\Models; | |
| 4 | + | |
| 5 | +use Booktics\Abstracts\Model; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Class Order_Model | |
| 9 | + * | |
| 10 | + * Handles database operations for orders in the Booktics system. | |
| 11 | + * | |
| 12 | + * @package Booktics\Models | |
| 13 | + */ | |
| 14 | +class Order_Model extends Model { | |
| 15 | + protected $table = 'booktics_orders'; | |
| 16 | + protected $fillable = [ | |
| 17 | + 'id', | |
| 18 | + 'order_no', | |
| 19 | + 'customer_id', | |
| 20 | + 'customer_note', | |
| 21 | + 'status', | |
| 22 | + 'payment_status', | |
| 23 | + 'price_breakdown', | |
| 24 | + 'tax_total', | |
| 25 | + 'coupon_code', | |
| 26 | + 'coupon_discount', | |
| 27 | + 'subtotal', | |
| 28 | + 'total', | |
| 29 | + 'currency', | |
| 30 | + 'payment_method', | |
| 31 | + 'payment_intent_id', | |
| 32 | + 'payment_transaction_id', | |
| 33 | + 'created_at', | |
| 34 | + 'updated_at', | |
| 35 | + 'custom_fields', | |
| 36 | + 'directorist_id', | |
| 37 | + ]; | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * Create a new order record | |
| 41 | + * | |
| 42 | + * @param array $data Order data to be created | |
| 43 | + * | |
| 44 | + * @return mixed The created order record | |
| 45 | + */ | |
| 46 | + public function create( array $data ) { | |
| 47 | + if ( empty( $data['order_no'] ) ) { | |
| 48 | + $data['order_no'] = $this->generate_order_no(); | |
| 49 | + } | |
| 50 | + | |
| 51 | + $data['price_breakdown'] = wp_json_encode( $data['price_breakdown'] ); | |
| 52 | + | |
| 53 | + return parent::create( $data ); | |
| 54 | + } | |
| 55 | + | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Generate a unique order number | |
| 59 | + * | |
| 60 | + * @return string | |
| 61 | + */ | |
| 62 | + protected function generate_order_no() { | |
| 63 | + $randomNum = time(); | |
| 64 | + | |
| 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; | |
| 129 | + } | |
| 64 | 130 | } |