Commit 5c48a8a6 authored by Michael Iseard's avatar Michael Iseard
Browse files

Rewrite get_campaign_total method to get more stats

parent 32873d0d
......@@ -96,16 +96,16 @@ class CampaignsTable extends WP_List_Table {
foreach ( $campaigns as $key => $campaign ) {
$id = $campaign['id'];
$transactions = $mapper->get_all_by( [ 'campaign_id' => $id ] );
$campaigns[ $key ]['date'] = date( "r", hexdec( substr( $id, 3, 8 ) ) );
$campaigns[ $key ]['transactions'] = 0;
$campaigns[ $key ]['total'] = $this->campaigns::get_campaign_total( $id );
if ( $transactions ) {
$campaigns[ $key ]['last_donation'] = end( $transactions )->created;
$campaigns[ $key ]['transactions'] = count( $transactions );
$campaigns[ $key ]['currency'] = $transactions[0]->currency;
}
$campaign_total = $this->campaigns::get_campaign_stats( $id );
$campaigns[ $key ]['date'] = date( "r", hexdec( substr( $id, 3, 8 ) ) );
$campaigns[ $key ]['transactions'] = 0;
$campaigns[ $key ]['goal'] = $campaign['campaign_goal'];
$campaigns[ $key ]['total'] = 0;
$campaigns[ $key ]['currency'] = 'EUR';
$campaigns[ $key ]['total'] = $campaign_total['total'];
$campaigns[ $key ]['last_donation'] = $campaign_total['last_donation'];
$campaigns[ $key ]['transactions'] = $campaign_total['count'];
}
return $campaigns;
......
......@@ -301,7 +301,7 @@ class Front {
$campaigns = new Campaigns();
if ( ! empty( $atts['campaign_id'] ) ) {
$campaign = $campaigns->get_campaign( $atts['campaign_id'] );
$campaign['total'] = $campaigns::get_campaign_total( $atts['campaign_id'] );
$campaign['total'] = $campaigns::get_campaign_stats( $atts['campaign_id'] )['total'];
}
// Bail if no campaign found
......
......@@ -135,31 +135,47 @@ class Campaigns {
}
/**
* Gets total value paid for campaign
* Gets transaction stats for campaign
*
* @param string $campaign_id
*
* @return float|int
* @return array
*/
public static function get_campaign_total( string $campaign_id ) {
public static function get_campaign_stats( string $campaign_id ): ?array {
$mapper = new MapperService( TransactionEntity::class );
$transactions = $mapper->get_all_by( [
'campaign_id' => $campaign_id,
] );
return array_sum( array_map( function ( $transaction ) {
if ( 'paid' === $transaction->status ) {
$refunds = $transaction->get_refund();
if ( $refunds ) {
return $refunds->remaining;
} else {
return $transaction->value;
if ( $transactions ) {
$values = array_map( function ( $transaction ) {
if ( 'paid' === $transaction->status ) {
$refunds = $transaction->get_refund();
if ( $refunds ) {
return $refunds->remaining;
} else {
return $transaction->value;
}
}
}
return 0;
},(array) $transactions ) );
return 0;
},
$transactions );
return [
'count' => count( $values ),
'total' => array_sum( $values ),
'last_donation' => end( $transactions )->created,
];
}
return [
'count' => 0,
'total' => 0,
'last_donation' => '',
];
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment