Skip to content

Commit

Permalink
add reimbursement request total and expense buildup columns to list view
Browse files Browse the repository at this point in the history
  • Loading branch information
timiwahalahti committed Sep 21, 2023
1 parent e4f6df9 commit ea2f5e8
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
add_filter( 'map_meta_cap', __NAMESPACE__ . '\modify_capabilities', 10, 4 );
add_filter( 'display_post_states', __NAMESPACE__ . '\display_post_states', 10, 2 );

// Columns.
add_filter( 'manage_'. POST_TYPE .'_posts_columns', __NAMESPACE__ . '\get_columns' );
add_action( 'manage_'. POST_TYPE .'_posts_custom_column', __NAMESPACE__ . '\render_columns', 10, 2 );

/**
* Register the custom post type
*
Expand Down Expand Up @@ -411,6 +415,64 @@ function display_post_states( $states, $post ) {
return $states;
}

/**
* Define columns for the Vendor Payments screen.
*
* @param array $_columns
* @return array
*/
function get_columns( $_columns ) {
$columns = array(
'cb' => $_columns['cb'],
'author' => esc_html__( 'Author' ),
'title' => $_columns['title'],
'date' => $_columns['date'],
'payment_amount' => esc_html__( 'Amount', 'wordcamporg' ),
'expenses' => esc_html__( 'Expenses', 'wordcamporg' ),
);

return $columns;
}

/**
* Render custom columns on the Vendor Payments screen.
*
* @param string $column
* @param int $post_id
*/
function render_columns( $column, $post_id ) {
switch ( $column ) {
case 'payment_amount':
$currency = get_post_meta( $post_id, '_wcbrr_currency', true );
if ( $currency && false === strpos( $currency, 'null' ) ) {
echo esc_html( $currency ) . ' ';
}

$total = 0;
$expenses = get_post_meta( $post_id, '_wcbrr_expenses', true );
if ( is_array( $expenses ) ) {
foreach ( $expenses as $expense ) {
$total += $expense['_wcbrr_amount'];
}
}

echo esc_html( $total );
break;

case 'expenses':
$currency = get_post_meta( $post_id, '_wcbrr_currency', true );

$expenses = get_post_meta( $post_id, '_wcbrr_expenses', true );
if ( is_array( $expenses ) ) {
foreach ( $expenses as $expense ) {
echo esc_html( "{$expense['_wcbrr_description']} ({$expense['_wcbrr_amount']} {$currency})<br/>" );
}
}

break;
}
}

/**
* Set the status when reimbursements are submitted.
*
Expand Down

0 comments on commit ea2f5e8

Please sign in to comment.