payer-view.php
2 years ago
payment-by-record.php
2 years ago
payment-count-view.php
2 years ago
payment-for-export-view.php
2 years ago
payment-view.php
2 years ago
payment-with-record-view.php
2 years ago
payment-view.php
133 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Gateways\Query_Views; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Tools; |
| 7 | use Jet_Form_Builder\Db_Queries\Query_Builder; |
| 8 | use Jet_Form_Builder\Db_Queries\Query_Conditions_Builder; |
| 9 | use Jet_Form_Builder\Db_Queries\Views\View_Base; |
| 10 | use JFB_Modules\Gateways\Db_Models\Payer_Model; |
| 11 | use JFB_Modules\Gateways\Db_Models\Payer_Shipping_Model; |
| 12 | use JFB_Modules\Gateways\Db_Models\Payment_Model; |
| 13 | use JFB_Modules\Gateways\Db_Models\Payment_To_Payer_Shipping_Model; |
| 14 | use JFB_Modules\Gateways\Db_Models\Payment_To_Record; |
| 15 | |
| 16 | // If this file is called directly, abort. |
| 17 | if ( ! defined( 'WPINC' ) ) { |
| 18 | die; |
| 19 | } |
| 20 | |
| 21 | class Payment_View extends View_Base { |
| 22 | |
| 23 | /** |
| 24 | * @since 3.1.0 |
| 25 | */ |
| 26 | const AVAILABLE_STATUSES = array( |
| 27 | 'COMPLETED', |
| 28 | 'VOIDED', |
| 29 | ); |
| 30 | |
| 31 | protected $with_record = false; |
| 32 | |
| 33 | public function __construct() { |
| 34 | $this->set_select( |
| 35 | array_merge( |
| 36 | Payment_Model::schema_columns(), |
| 37 | Payer_Shipping_Model::schema_columns( 'ship' ), |
| 38 | Payer_Model::schema_columns( 'payer' ) |
| 39 | ) |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | protected $order_by = array( |
| 44 | array( |
| 45 | 'column' => 'id', |
| 46 | 'sort' => self::FROM_HIGH_TO_LOW, |
| 47 | ), |
| 48 | ); |
| 49 | |
| 50 | public function set_filters( array $filters ) { |
| 51 | parent::set_filters( $filters ); |
| 52 | |
| 53 | $this->set_status_filter(); |
| 54 | |
| 55 | return $this; |
| 56 | } |
| 57 | |
| 58 | protected function set_status_filter() { |
| 59 | $status = $this->filters['status'] ?? ''; |
| 60 | |
| 61 | if ( ! in_array( $status, self::AVAILABLE_STATUSES, true ) ) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | $is_completed = 'COMPLETED' === $status; |
| 66 | |
| 67 | $this->add_conditions( |
| 68 | array( |
| 69 | array( |
| 70 | 'type' => $is_completed |
| 71 | ? Query_Conditions_Builder::TYPE_EQUAL |
| 72 | : Query_Conditions_Builder::TYPE_NOT_EQUAL, |
| 73 | 'values' => array( 'status', 'COMPLETED' ), |
| 74 | ), |
| 75 | ) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param Query_Builder $builder |
| 81 | */ |
| 82 | public function get_prepared_join( Query_Builder $builder ) { |
| 83 | $payments_to_p_ships = ( new Payment_To_Payer_Shipping_Model() )->create()::table(); |
| 84 | $payers = ( new Payer_Model() )->create()::table(); |
| 85 | $payments = Payment_Model::table(); |
| 86 | $payers_ship = Payer_Shipping_Model::table(); |
| 87 | |
| 88 | $builder->join = " |
| 89 | LEFT JOIN `{$payments_to_p_ships}` ON 1=1 |
| 90 | AND `{$payments}`.`id` = `{$payments_to_p_ships}`.`payment_id` |
| 91 | |
| 92 | LEFT JOIN `{$payers_ship}` ON 1=1 |
| 93 | AND {$payers_ship}.`id` = `{$payments_to_p_ships}`.`payer_shipping_id` |
| 94 | |
| 95 | LEFT JOIN `{$payers}` ON 1=1 |
| 96 | AND `{$payers}`.`id` = `{$payers_ship}`.`payer_id` |
| 97 | "; |
| 98 | |
| 99 | if ( ! $this->with_record ) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | $payment_to_record = ( new Payment_To_Record() )->create()::table(); |
| 104 | |
| 105 | $builder->join .= " |
| 106 | LEFT JOIN `{$payment_to_record}` ON 1=1 |
| 107 | AND `$payment_to_record`.`payment_id` = `{$payments}`.`id` |
| 108 | "; |
| 109 | } |
| 110 | |
| 111 | public function table(): string { |
| 112 | return Payment_Model::table(); |
| 113 | } |
| 114 | |
| 115 | public function set_payment_id( $payment_id ) { |
| 116 | $this->set_conditions( |
| 117 | static::prepare_columns( |
| 118 | array( |
| 119 | 'id' => $payment_id, |
| 120 | ) |
| 121 | ) |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @param bool $with_record |
| 127 | */ |
| 128 | public function set_with_record( bool $with_record ) { |
| 129 | $this->with_record = $with_record; |
| 130 | } |
| 131 | |
| 132 | } |
| 133 |