| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Models; |
| 4 |
|
| 5 |
use Booktics\Abstracts\Booktics_Database; |
| 6 |
use Booktics\Abstracts\Model; |
| 7 |
|
| 8 |
class Guest_Model extends Model { |
| 9 |
|
| 10 |
protected $table = 'booktics_guests'; |
| 11 |
protected $fillable = array( |
| 12 |
'id', |
| 13 |
'wp_user_id', |
| 14 |
'name', |
| 15 |
'user_login', |
| 16 |
'email', |
| 17 |
'phone', |
| 18 |
'description', |
| 19 |
'status', |
| 20 |
'image', |
| 21 |
'created_at', |
| 22 |
'updated_at', |
| 23 |
); |
| 24 |
|
| 25 |
/** |
| 26 |
* Get virtual attributes for the guest model |
| 27 |
* |
| 28 |
* @param object $row |
| 29 |
* |
| 30 |
* @return array |
| 31 |
*/ |
| 32 |
protected function get_virtual_attributes( object $row ): array { |
| 33 |
if ( ! $row ) { |
| 34 |
return array(); |
| 35 |
} |
| 36 |
|
| 37 |
$db = new Booktics_Database(); |
| 38 |
|
| 39 |
$total_booking = ( new Order_Model( $db ) )->count( array( 'customer_id' => $row->id ) ); |
| 40 |
$total_amount = ( new Order_Model( $db ) )->sum( 'total', array( 'customer_id' => $row->id ) ); |
| 41 |
|
| 42 |
$orders = ( new Order_Model( $db ) )->get( array( 'customer_id' => $row->id ) ); |
| 43 |
|
| 44 |
// Remove canceled appointment costs from total amount for each customer |
| 45 |
if ( isset( $orders['items'] ) && is_array( $orders['items'] ) ) { |
| 46 |
foreach ( $orders['items'] as $order ) { |
| 47 |
$appointments = (new Appointment_Model())->where('order_id', $order->id); |
| 48 |
|
| 49 |
if ( ! isset( $appointments ) || ! is_array( $appointments )) { |
| 50 |
continue; |
| 51 |
} |
| 52 |
|
| 53 |
foreach ( $appointments as $appointment) { |
| 54 |
if ( $appointment->status === 'canceled' ) { |
| 55 |
$total_amount -= $order->total; |
| 56 |
$total_booking -= 1; |
| 57 |
break; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return array( |
| 64 |
'status_label' => ( $row->status == 1 ) ? 'Active' : 'Deactive', |
| 65 |
'total_booking' => $total_booking, |
| 66 |
'total_amount' => $total_amount, |
| 67 |
'user_registered' => gmdate( 'd M Y h:iA', strtotime( $row->created_at ) ), |
| 68 |
); |
| 69 |
} |
| 70 |
} |
| 71 |
|