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