Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ForGravity/2c1fe9747ec875250c65799d6008af6c to your computer and use it in GitHub Desktop.
Save ForGravity/2c1fe9747ec875250c65799d6008af6c to your computer and use it in GitHub Desktop.
<?php
add_filter( 'fg_entryautomation_export_email_message', 'add_payment_summary', 10, 4 );
function add_payment_summary( $message, $settings, $form, $file_name ) {
// Get search criteria.
$search_criteria = fg_entryautomation()->get_search_criteria( $settings, $form );
// Get entries for search criteria.
$entries = GFAPI::get_entries( $form['id'], $search_criteria );
// Initialize our counts.
$entries_paid = 0;
$entries_unpaid = 0;
$paid_total = 0;
$unpaid_total = 0;
// Loop through entries.
foreach ( $entries as $entry ) {
// If entry was paid, add to paid totals.
if ( 'Paid' === $entry['payment_status'] ) {
// Increase entries paid count.
$entries_paid++;
// Increase payment total.
$paid_total += $entry['payment_amount'];
} else {
// Increase entries unpaid count.
$entries_unpaid++;
// Increase payment total.
$unpaid_total += $entry['payment_amount'];
}
}
// Start payment summary with date range.
$summary = sprintf( 'Payment summary for %s to %s', $search_criteria['start_date'], $search_criteria['end_date'] ) . PHP_EOL . PHP_EOL;
// Add entries count.
$summary .= sprintf( 'Total Entries: %d', count( $entries ) ) . PHP_EOL . PHP_EOL;
// Add paid data.
$summary .= sprintf( 'Paid Entries: %d (%d%%)', $entries_paid, round( ( $entries_paid / count( $entries ) ) * 100 ) ) . PHP_EOL;
$summary .= sprintf( 'Paid Entries Total: %s', GFCommon::to_money( $paid_total ) ) . PHP_EOL . PHP_EOL;
// Add unpaid data.
$summary .= sprintf( 'Unpaid Entries: %d (%d%%)', $entries_unpaid, round( ( $entries_unpaid / count( $entries ) ) * 100 ) ) . PHP_EOL;
$summary .= sprintf( 'Unpaid Entries Total: %s', GFCommon::to_money( $unpaid_total ) );
// Return payment summary as message.
return $summary;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment