PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.12
Booktics – Appointment Booking Calendar for Service Businesses v1.0.12
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / base / models / order-model.php

order-model.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.12, at base/models/order-model.php

129 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'custom_fields',
35 'directorist_id',
36 ];
37
38 /**
39 * Create a new order record
40 *
41 * @param array $data Order data to be created
42 *
43 * @return mixed The created order record
44 */
45 public function create( array $data ) {
46 if ( empty( $data['order_no'] ) ) {
47 $data['order_no'] = $this->generate_order_no();
48 }
49
50 $data['price_breakdown'] = wp_json_encode( $data['price_breakdown'] );
51
52 return parent::create( $data );
53 }
54
55
56 /**
57 * Generate a unique order number
58 *
59 * @return string
60 */
61 protected function generate_order_no() {
62 $randomNum = time();
63
64 return 'BKNG-' . $randomNum;
65 }
66
67 /**
68 * Calculate total revenue for fully paid orders within a date range
69 * Excludes canceled/cancelled orders
70 *
71 * @param string $start_date Start date (Y-m-d format)
72 * @param string $end_date End date (Y-m-d format)
73 * @param array $order_ids Optional array of order IDs to filter by
74 *
75 * @return float Total revenue
76 */
77 public function get_revenue_by_date_range( string $start_date, string $end_date, array $order_ids = array() ) {
78 $query = $this->query()
79 ->where( 'payment_status', '=', 'fully_paid' )
80 ->where( 'status', '!=', 'canceled' )
81 ->where( 'status', '!=', 'cancelled' )
82 ->where( 'created_at', '>=', $start_date . ' 00:00:00' )
83 ->where( 'created_at', '<=', $end_date . ' 23:59:59' );
84
85 if ( ! empty( $order_ids ) ) {
86 $query->where_in( 'id', $order_ids );
87 }
88
89 $sum = $query->sum( 'total' );
90
91 // Additional safety check to ensure we always return a valid float
92 if ( !is_numeric( $sum ) || !is_finite( $sum ) ) {
93 return 0.0;
94 }
95
96 return (float) $sum;
97 }
98
99 /**
100 * Calculate revenue for a specific date range with custom filters
101 *
102 * @param string $start_date Start date (Y-m-d format)
103 * @param string $end_date End date (Y-m-d format)
104 * @param callable|null $additional_conditions Callback to add additional conditions to the query
105 *
106 * @return float Total revenue
107 */
108 public function get_revenue_with_conditions( string $start_date, string $end_date, ?callable $additional_conditions = null ) {
109 $query = $this->query()
110 ->where( 'payment_status', '=', 'fully_paid' )
111 ->where( 'status', '!=', 'canceled' )
112 ->where( 'status', '!=', 'cancelled' )
113 ->where( 'created_at', '>=', $start_date . ' 00:00:00' )
114 ->where( 'created_at', '<=', $end_date . ' 23:59:59' );
115
116 if ( $additional_conditions && is_callable( $additional_conditions ) ) {
117 $additional_conditions( $query );
118 }
119
120 $sum = $query->sum( 'total' );
121
122 // Additional safety check to ensure we always return a valid float
123 if ( !is_numeric( $sum ) || !is_finite( $sum ) ) {
124 return 0.0;
125 }
126
127 return (float) $sum;
128 }
129 }