Function Reference: geodir_payment_get_invoices

Summary

Get invoices using given arguments.

Global Values

$wpdb
(object) (required) WordPress Database object.

Default: None

Parameters

$args
(array) (required) Query args.

Default: None

Return Values

(array)
  • invoice array.

Change Log

Since: 1.2.6

1.3.6 Filter box added.

Source Code

function geodir_payment_get_invoices( $args ) {
	global $wpdb;

	$where = '';
	
	if ( !empty( $args['invoice_id'] ) && $args['invoice_id'] > 0 ) {
		$where .= "AND id = '" . $args['invoice_id'] . "' ";
	}
	
	if ( !empty( $args['search'] ) && $args['search'] != '' ) {
		$threshold = absint(get_option('geodir_payment_invoice_threshold'));
		$threshold = $threshold > 20 ? 20 : $threshold;
		
		$prefix = get_option('geodir_payment_invoice_prefix');
	
		$where .= "AND ( id LIKE '" . wp_slash( $args['search'] ) . "' OR '" . wp_slash( $args['search'] ) . "' LIKE CONCAT( SUBSTR( '" . wp_slash( $prefix ) . "', 1, 10 ), LPAD( id, '" . $threshold . "', '0' ) ) OR post_id LIKE '" . wp_slash( $args['search'] ) . "' ) ";
	}

	if ( !empty( $args['status'] ) && $args['status'] != '' ) {
		if ($args['status'] == 'confirmed') {
			$where .= "AND ( status = '" . wp_slash( $args['status'] ) . "' OR status = 'paid' OR status = 'active' OR status = 'subscription-payment' OR status = 'free' ) ";
		} else if ($args['status'] == 'pending') {
			$where .= "AND ( status = '" . wp_slash( $args['status'] ) . "' OR status = 'unpaid' ) AND paymentmethod IS NOT NULL ";
		} else if ($args['status'] == 'incomplete') {
			$where .= "AND ( status = 'pending' AND ( paymentmethod IS NULL OR paymentmethod = '' ) ) ";
		} else {
			$where .= "AND status = '" . wp_slash( $args['status'] ) . "'";
		}
	}
	
	$sql = "SELECT COUNT(*) FROM " . INVOICE_TABLE . " WHERE 1=1 " . $where;
	$total_items = $wpdb->get_var( $sql );
	
	if ( !empty( $args['count'] ) ) {
		return $total_items;
	}
	
	$total_pages = ( $total_items > 0 && isset( $args['per_page'] ) && $args['per_page'] > 0 ) ? ceil( $total_items / $args['per_page'] ) : 0;
	$args['total_pages'] = $total_pages;
	
	$pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
	
	if ( isset( $args['total_pages'] ) && $pagenum > $args['total_pages'] ) {
		$pagenum = $args['total_pages'];
	}
	
	$pagenum = max( 1, $pagenum );
	$args['total_items'] = $total_items;
	$args['pagenum'] = $pagenum;
	
	$limits = '';
	if ( isset( $args['per_page'] ) && $args['per_page'] > 0 ) {
		$offset = ( $pagenum - 1 ) * $args['per_page'];
		if ( $offset > 0 ) {
			$limits = 'LIMIT ' . $offset . ',' . $args['per_page'];
		} else {
			$limits = 'LIMIT ' . $args['per_page'];
		}
	}
	
	$orderby = '';
	if (!empty($args['orderby']) && !empty($args['order'])) {
		$orderby .= ' ORDER BY ' . $args['orderby'] . ' ' . $args['order'];
	}
	
	$sql = "SELECT * FROM " . INVOICE_TABLE . " WHERE 1=1 " . $where . " " . $orderby . " " . $limits;

	$items = $wpdb->get_results( $sql );
	$result = array();
	$result['items'] = $items;
	$result['total_items'] = $total_items;
	$result['total_pages'] = $total_pages;
	$result['pagenum'] = $pagenum;	
	$result['pagination'] = geodir_payment_admin_pagination( $args );
	$result['pagination_top'] = geodir_payment_admin_pagination( $args, 'top' );
	$result['filter_box'] = geodir_payment_invoice_admin_search_box( __( 'Filter', 'geodir_payments' ), 'invoice' );

	return $result;
}