InvoiceScriptsController.php
3 months ago
InvoicesListTable.php
2 months ago
InvoicesViewController.php
1 year ago
InvoicesListTable.php
349 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\Invoices; |
| 4 | |
| 5 | use SureCart\Controllers\Admin\Tables\HasModeFilter; |
| 6 | use SureCart\Controllers\Admin\Tables\ListTable; |
| 7 | use SureCart\Models\Invoice; |
| 8 | |
| 9 | /** |
| 10 | * Create a new table class that will extend the WP_List_Table |
| 11 | */ |
| 12 | class InvoicesListTable extends ListTable { |
| 13 | use HasModeFilter; |
| 14 | |
| 15 | /** |
| 16 | * Prepare the items for the table to process. |
| 17 | * |
| 18 | * @return void |
| 19 | */ |
| 20 | public function prepare_items() { |
| 21 | $columns = $this->get_columns(); |
| 22 | $hidden = $this->get_hidden_columns(); |
| 23 | $sortable = $this->get_sortable_columns(); |
| 24 | |
| 25 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 26 | |
| 27 | $query = $this->table_data(); |
| 28 | if ( is_wp_error( $query ) ) { |
| 29 | $this->items = []; |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | $this->set_pagination_args( |
| 34 | [ |
| 35 | 'total_items' => $query->pagination->count, |
| 36 | 'per_page' => $this->get_items_per_page( 'invoices' ), |
| 37 | ] |
| 38 | ); |
| 39 | |
| 40 | $this->items = $query->data; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Override the parent columns method. Defines the columns to use in your listing table. |
| 45 | * |
| 46 | * @return array |
| 47 | */ |
| 48 | public function get_columns() { |
| 49 | return array_merge( |
| 50 | [ |
| 51 | 'invoice' => __( 'Invoice', 'surecart' ), |
| 52 | 'issue_date' => __( 'Issued', 'surecart' ), |
| 53 | 'due_date' => __( 'Due', 'surecart' ), |
| 54 | 'status' => __( 'Status', 'surecart' ), |
| 55 | 'customer' => __( 'Customer', 'surecart' ), |
| 56 | 'method' => __( 'Method', 'surecart' ), |
| 57 | 'total' => __( 'Total', 'surecart' ), |
| 58 | 'created' => __( 'Created', 'surecart' ), |
| 59 | 'mode' => '', |
| 60 | ], |
| 61 | parent::get_columns() |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Show the payment method for the invoice. |
| 67 | * |
| 68 | * @param Invoice $invoice Invoice model. |
| 69 | * |
| 70 | * @return string |
| 71 | */ |
| 72 | public function column_method( $invoice ) { |
| 73 | if ( isset( $invoice->checkout->manual_payment_method->name ) ) { |
| 74 | return '<sc-tag>' . esc_html( $invoice->checkout->manual_payment_method->name ) . '</sc-tag>'; |
| 75 | } |
| 76 | ob_start(); |
| 77 | ?> |
| 78 | <sc-payment-method id="sc-method-<?php echo esc_attr( $invoice->id ); ?>"></sc-payment-method> |
| 79 | <script> |
| 80 | document.getElementById('sc-method-<?php echo esc_attr( $invoice->id ); ?>').paymentMethod = <?php echo wp_json_encode( $invoice->checkout->payment_method ); ?>; |
| 81 | </script> |
| 82 | <?php |
| 83 | return ob_get_clean(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Define which columns are hidden. |
| 88 | * |
| 89 | * @return array |
| 90 | */ |
| 91 | public function get_hidden_columns() { |
| 92 | return array(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the table data. |
| 97 | * |
| 98 | * @return object |
| 99 | */ |
| 100 | protected function table_data() { |
| 101 | $status = $this->getStatus(); |
| 102 | $mode = sanitize_text_field( wp_unslash( $_GET['mode'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 103 | $where = array( |
| 104 | 'query' => $this->get_search_query(), |
| 105 | ); |
| 106 | |
| 107 | if ( ! empty( $mode ) ) { |
| 108 | $where['live_mode'] = 'live' === $mode; |
| 109 | } |
| 110 | |
| 111 | if ( $status ) { |
| 112 | $where['status'] = [ $status ]; |
| 113 | } |
| 114 | |
| 115 | // Check if there is any sc_collection in the query, then filter it. |
| 116 | if ( ! empty( $_GET['sc_customer'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 117 | $where['customer_ids'] = array( sanitize_text_field( wp_unslash( $_GET['sc_customer'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 118 | } |
| 119 | |
| 120 | return Invoice::where( $where ) |
| 121 | ->with( [ 'charge', 'checkout', 'checkout.order', 'checkout.charge', 'checkout.payment_method', 'checkout.manual_payment_method', 'checkout.customer', 'payment_method.card', 'payment_method.payment_instrument', 'payment_method.paypal_account', 'payment_method.bank_account' ] ) |
| 122 | ->paginate( |
| 123 | [ |
| 124 | 'per_page' => $this->get_items_per_page( 'invoices' ), |
| 125 | 'page' => $this->get_pagenum(), |
| 126 | ] |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Generates views links. |
| 132 | * |
| 133 | * @return array |
| 134 | */ |
| 135 | protected function get_views() { |
| 136 | $statuses = [ |
| 137 | 'all' => __( 'All', 'surecart' ), |
| 138 | 'paid' => __( 'Paid', 'surecart' ), |
| 139 | 'draft' => __( 'Draft', 'surecart' ), |
| 140 | 'open' => __( 'Open', 'surecart' ), |
| 141 | ]; |
| 142 | |
| 143 | $link = \SureCart::getUrl()->index( 'invoices' ); |
| 144 | $status_links = []; |
| 145 | |
| 146 | foreach ( $statuses as $status => $label ) { |
| 147 | $current_link_attributes = ''; |
| 148 | |
| 149 | if ( ! empty( $_GET['status'] ) ) { |
| 150 | if ( $status === $_GET['status'] ) { |
| 151 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 152 | } |
| 153 | } elseif ( 'all' === $status ) { |
| 154 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 155 | } |
| 156 | |
| 157 | $link = add_query_arg( 'status', $status, $link ); |
| 158 | |
| 159 | if ( isset( $_GET['live_mode'] ) ) { |
| 160 | $link = add_query_arg( 'live_mode', sanitize_text_field( wp_unslash( $_GET['live_mode'] ) ), $link ); |
| 161 | } |
| 162 | |
| 163 | $status_links[ $status ] = "<a href='$link'$current_link_attributes>" . $label . '</a>'; |
| 164 | } |
| 165 | |
| 166 | // filter links. |
| 167 | return apply_filters( 'sc_invoice_status_links', $status_links ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get the query status. |
| 172 | * |
| 173 | * @return array |
| 174 | */ |
| 175 | public function getStatus() { |
| 176 | $status = sanitize_text_field( wp_unslash( $_GET['status'] ?? 'all' ) ); |
| 177 | if ( 'all' === $status ) { |
| 178 | return []; |
| 179 | } |
| 180 | return $status ? [ esc_html( $status ) ] : []; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Handle the total column. |
| 185 | * |
| 186 | * @param Invoice $invoice Invoice Model. |
| 187 | * |
| 188 | * @return string |
| 189 | */ |
| 190 | public function column_total( $invoice ) { |
| 191 | return $invoice->checkout->total_display_amount; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Handle invoice due date. |
| 196 | * |
| 197 | * @param Invoice $invoice Invoice model. |
| 198 | * |
| 199 | * @return string |
| 200 | */ |
| 201 | public function column_due_date( $invoice ) { |
| 202 | return $invoice->due_date_date ?? '-'; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Handle invoice issue date. |
| 207 | * |
| 208 | * @param Invoice $invoice Invoice model. |
| 209 | * |
| 210 | * @return string |
| 211 | */ |
| 212 | public function column_issue_date( $invoice ) { |
| 213 | return $invoice->issue_date_date ?? '-'; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Show customer as tag. |
| 218 | * |
| 219 | * @param Invoice $invoice The invoice model. |
| 220 | */ |
| 221 | public function column_customer( $invoice ) { |
| 222 | $customer = $invoice->checkout->customer ?? null; |
| 223 | |
| 224 | if ( empty( $customer ) ) { |
| 225 | return '-'; |
| 226 | } |
| 227 | |
| 228 | $url = \SureCart::getUrl()->edit( 'customer', $customer->id ); |
| 229 | return '<a href="' . esc_url( $url ) . '">' . esc_html( ! empty( $customer->name ) ? $customer->name : $customer->email ) . '</a>'; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Handle the status. |
| 234 | * |
| 235 | * @param Invoice $invoice Invoice Model. |
| 236 | * |
| 237 | * @return string |
| 238 | */ |
| 239 | public function column_status( $invoice ) { |
| 240 | if ( ! empty( $invoice->checkout->charge->fully_refunded ) ) { |
| 241 | return '<sc-tag type="danger">' . __( 'Refunded', 'surecart' ) . '</sc-tag>'; |
| 242 | } |
| 243 | return '<sc-invoice-status-badge status="' . esc_attr( $invoice->status ) . '"></sc-invoice-status-badge>'; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Handle Invoice column. |
| 248 | * |
| 249 | * @param Invoice $invoice Invoice model. |
| 250 | * |
| 251 | * @return string |
| 252 | */ |
| 253 | public function column_invoice( $invoice ) { |
| 254 | ob_start(); |
| 255 | ?> |
| 256 | <a class="row-title" aria-label="<?php echo esc_attr__( 'Edit Invoice', 'surecart' ); ?>" href="<?php echo esc_url( \SureCart::getUrl()->edit( 'invoice', $invoice->id ) ); ?>"> |
| 257 | <?php echo ! empty( $invoice->checkout->order->number ) ? '#' . esc_html( $invoice->checkout->order->number ) : esc_html_e( '(draft)', 'surecart' ); ?> |
| 258 | </a> |
| 259 | |
| 260 | <?php |
| 261 | echo wp_kses_post( |
| 262 | $this->row_actions( |
| 263 | array_filter( |
| 264 | [ |
| 265 | 'edit' => '<a href="' . esc_url( \SureCart::getUrl()->edit( 'invoice', $invoice->id ) ) . '" aria-label="' . esc_attr__( 'Edit Invoice', 'surecart' ) . '">' . __( 'Edit', 'surecart' ) . '</a>', |
| 266 | 'view' => 'open' === $invoice->status && ! empty( $invoice->checkout_url ) ? '<a href="' . esc_url( $invoice->checkout_url ) . '" aria-label="' . esc_attr__( 'View Payment Page', 'surecart' ) . '">' . __( 'View Payment Page', 'surecart' ) . '</a>' : null, |
| 267 | ] |
| 268 | ), |
| 269 | ) |
| 270 | ); |
| 271 | |
| 272 | return ob_get_clean(); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Handle the mode column. |
| 277 | * |
| 278 | * @param Invoice $invoice Invoice model. |
| 279 | * |
| 280 | * @return string |
| 281 | */ |
| 282 | public function column_mode( $invoice ) { |
| 283 | return empty( $invoice->checkout->live_mode ) ? '<sc-tag type="warning">' . __( 'Test', 'surecart' ) . '</sc-tag>' : ''; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Displays extra table navigation. |
| 288 | * |
| 289 | * @param string $which Top or bottom placement. |
| 290 | */ |
| 291 | protected function extra_tablenav( $which ) { |
| 292 | ?> |
| 293 | <input type="hidden" name="page" value="sc-invoices" /> |
| 294 | |
| 295 | <?php if ( ! empty( $_GET['status'] ) ) : ?> |
| 296 | <input type="hidden" name="status" value="<?php echo esc_attr( $_GET['status'] ); ?>" /> |
| 297 | <?php endif; ?> |
| 298 | |
| 299 | <?php if ( ! empty( $_GET['live_mode'] ) ) : ?> |
| 300 | <input type="hidden" name="live_mode" value="<?php echo esc_attr( $_GET['live_mode'] ); ?>" /> |
| 301 | <?php endif; ?> |
| 302 | |
| 303 | <div class="alignleft actions"> |
| 304 | <?php |
| 305 | if ( 'top' === $which ) { |
| 306 | ob_start(); |
| 307 | $this->mode_dropdown(); |
| 308 | |
| 309 | /** |
| 310 | * Fires before the Filter button on the Posts and Pages list tables. |
| 311 | * |
| 312 | * The Filter button allows sorting by date and/or category on the |
| 313 | * Posts list table, and sorting by date on the Pages list table. |
| 314 | * |
| 315 | * @since 2.1.0 |
| 316 | * @since 4.4.0 The `$post_type` parameter was added. |
| 317 | * @since 4.6.0 The `$which` parameter was added. |
| 318 | * |
| 319 | * @param string $post_type The post type slug. |
| 320 | * @param string $which The location of the extra table nav markup: |
| 321 | * 'top' or 'bottom' for WP_Posts_List_Table, |
| 322 | * 'bar' for WP_Media_List_Table. |
| 323 | */ |
| 324 | do_action( 'restrict_manage_invoices', $this->screen->post_type, $which ); |
| 325 | |
| 326 | $output = ob_get_clean(); |
| 327 | |
| 328 | if ( ! empty( $output ) ) { |
| 329 | echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 330 | submit_button( __( 'Filter', 'surecart' ), '', 'filter_action', false, array( 'id' => 'filter-by-fulfillment-submit' ) ); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | ?> |
| 335 | </div> |
| 336 | |
| 337 | <?php |
| 338 | /** |
| 339 | * Fires immediately following the closing "actions" div in the tablenav for the posts |
| 340 | * list table. |
| 341 | * |
| 342 | * @since 4.4.0 |
| 343 | * |
| 344 | * @param string $which The location of the extra table nav markup: 'top' or 'bottom'. |
| 345 | */ |
| 346 | do_action( 'manage_invoices_extra_tablenav', $which ); |
| 347 | } |
| 348 | } |
| 349 |