Files
booking/booking.reports.inc

547 lines
22 KiB
PHP

<?php
// $Id: booking.confirm.inc,v 0.1 2011/07/12
/**
* List people and how much they have paid
*/
function booking_report_summary() {
global $event;
//$text = "";
$output = "";
$non_australia_count = 0;
$bookedin_counter = 0;
$notpaid_counter = 0;
$waiting_counter = 0;
$male_count = 0;
$female_count = 0;
$baptised_count = 0;
$married_count = 0;
$total_paid = 0;
$dob_total = 0;
$male_dob_total = 0;
$female_dob_total = 0;
$person_count = 0;
$welfare_count = 0;
$fullypaid_count = 0;
$stats_attributes = array('style' => 'max-width:30%');
//TODO: add count of welfare people, and number of people fully paid
//define sorting information with the header
//as per http://www.drup-all.com/blog/table-sort-pagination-drupal-7
$header = array(
array('data' => t('Id'), 'field' => 'nid', 'sort' => 'asc'),
array('data' => t('Edit Link')),
array('data' => t('Name'), 'field' => 'booking_lastname', 'sort' => 'asc'),
array('data' => t('Email'), 'field' => 'booking_email', 'sort' => 'asc'),
array('data' => t('Payment To Date'), 'field' => 'booking_amount_paid', 'sort' => 'asc'),
array('data' => t('Total Payment Required'), 'field' => 'booking_total_pay_reqd', 'sort' => 'asc'),
array('data' => t('Welfare Required?'), 'field' => 'booking_welfare_required', 'sort' => 'asc'),
);
$rows = array();
$state_header = array('State', 'Count');
$state_rows = array();
$ecclesia_header = array('State','Ecclesia', 'Count');
$ecclesia_rows = array();
//do some analysis about the people booked in
//first the summary of states
$query = db_select('booking_person', 'p')
->fields('p', array('booking_state', 'booking_country'))
->condition('p.booking_event_id', $event->eid, '=');
$query->groupBy('p.booking_state');
$query->addExpression('COUNT(p.booking_state)', 'state_count');
$query->orderBy('state_count', 'DESC');
$state_stats = $query->execute();
foreach ($state_stats as $state) {
if (strcmp($state->booking_country,'Australia') == 0 )
$state_rows[] = array($state->booking_state, $state->state_count);
else
$non_australia_count += $state->state_count;
}
//non australian states
$state_rows[] = array('International', $non_australia_count);
//general stats
$query = db_select('booking_person', 'p')
->fields('p', array('booking_baptised', 'booking_married', 'booking_gender', 'booking_dob'))
->condition('p.booking_event_id', $event->eid, '=');
$general_stats = $query->execute();
foreach ($general_stats as $person)
{
$dob_total += $person->booking_dob;
$person_count++;
if ($person->booking_gender == 'M')
{
$male_count++;
$male_dob_total += $person->booking_dob;
}
else
{
$female_count++;
$female_dob_total += $person->booking_dob;
}
if ($person->booking_baptised == 'Y')
$baptised_count++;
if ($person->booking_married == 'Y')
$married_count++;
}
//bookings by ecclesia
$query = db_select('booking_person', 'p')
->fields('p', array('booking_ecclesia','booking_state'))
->condition('p.booking_event_id', $event->eid, '=');
$query->groupBy('p.booking_ecclesia');
$query->addExpression('COUNT(p.booking_ecclesia)', 'ecclesia_count');
$query->orderBy('booking_state')
->orderBy('ecclesia_count','DESC');
$stats = $query->execute();
foreach ($stats as $ecclesia) {
$ecclesia_rows[] = array($ecclesia->booking_state,$ecclesia->booking_ecclesia, $ecclesia->ecclesia_count);
}
//more detailed summary
//allow user-selectable sorting of columns as per http://www.drup-all.com/blog/table-sort-pagination-drupal-7
$query = db_select('booking_person', 'p')
->fields('p')
->condition('p.booking_event_id', $event->eid, '=');
$table_sort = $query->extend('TableSort')->orderbyHeader($header);
$result = $table_sort->execute();
foreach ($result as $person) {
$rows[] = array(
l(t('!id', array('!id' => $person->nid)), t('node/!id', array('!id' => $person->nid))),
l(t('!id', array('!id' => $person->nid)), t('node/!id/edit', array('!id' => $person->nid))),
t('!first !last', array('!first' => ucwords($person->booking_firstname), '!last' => ucwords($person->booking_lastname))),
t('!email', array('!email' => $person->booking_email)),
t('!payment', array('!payment' => $person->booking_amount_paid)),
t('!payment', array('!payment' => $person->booking_total_pay_reqd)),
t($person->booking_welfare_required == 'Y' ? 'Yes' : 'No'),
);
$total_paid += $person->booking_amount_paid;
//booking status
if ($person->booking_status == 0)
$notpaid_counter++;
elseif ($person->booking_status == 1)
$bookedin_counter++;
elseif ($person->booking_status == 2)
$waiting_counter++;
//welfare
if ($person->booking_welfare_required == 'Y')
$welfare_count++;
//fully paid?
if ($person->booking_amount_paid >= $person->booking_total_pay_reqd || $person->booking_total_pay_reqd == "0.00")
{
//watchdog('booking', "Fully paid person, ID !id (!first !last)", array('!id' => $person->nid,'!first' => ucwords($person->booking_firstname), '!last' => ucwords($person->booking_lastname)));
$fullypaid_count++;
}
elseif ($person->booking_partner_id > 0 && _booking_amount_owing($person->nid) == 0)
{
//watchdog('booking', "Fully paid married person, ID !id (!first !last)", array('!id' => $person->nid,'!first' => ucwords($person->booking_firstname), '!last' => ucwords($person->booking_lastname)));
$fullypaid_count++;
}
}
//output everything
$output .= t("<h3>General Statistics</h3>");
$output .= t("<p>There are !boys males and !girls females registered. Of these, !baptised are baptised and !married are married.<br />",
array('!boys' => $male_count, '!girls' => $female_count, '!baptised' => $baptised_count, '!married' => $married_count
));
$output .= t("The overall average age at the start of the week will be !average. The male average age will be !maleaverage. The female average age will be !femaleaverage.<br />",
array('!average' => _booking_avg_age($dob_total, $person_count, $event->booking_event_start), '!maleaverage' => _booking_avg_age($male_dob_total, $male_count, $event->booking_event_start),
'!femaleaverage' => _booking_avg_age($female_dob_total, $female_count, $event->booking_event_start)
));
$output .= t("There are !bookedin registrations currently booked in, !waiting on waiting list, and !notpaid haven't paid, which comes to a total of !total people who have filled in the registration form.</p>",
array('!bookedin' => $bookedin_counter, '!waiting' => $waiting_counter, '!notpaid' => $notpaid_counter, '!total' => $person_count));
$output .= t("<p>There are !welfare people with special financial consideration approved. !fullypaid people have completed their payments.<br />",
array('!welfare' => $welfare_count, '!fullypaid' => $fullypaid_count
));
$output .= t("Total amount paid: $!paid</p>", array('!paid' => $total_paid));
$output .= t("<h3>Bookings by state</h3>");
$output .= theme('table', array('header' => $state_header, 'rows' => $state_rows, 'attributes' => $stats_attributes));
$output .= t("<h3>Bookings by ecclesia</h3>");
$output .= theme('table', array('header' => $ecclesia_header, 'rows' => $ecclesia_rows, 'attributes' => $stats_attributes));
$output .= t("<h3>Summary of attendees for !event.</h3>", array('!event' => $event->booking_eventname));
$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'sort-table')));
return $output;
}
/**
* List the contents of the payments table
*/
function booking_report_paypal_payments() {
global $event;
$text = "";
$output = "";
$bookedin_counter = 0;
$total_paid = 0;
$total_fees = 0;
$header = array('Node Id', 'Name', 'Email', 'Payment Date', 'Gross Payment', 'Currency',
'Paypal Fee', 'Invoice', 'Payment Status', 'Payer ID', 'Item Name', 'IPN track ID');
$rows = array();
//fetch the payment table
$query = db_select('booking_payment', 'pa')
->condition('pa.booking_eventid', $event->eid,'=')
->fields('pa');
$result = $query->execute();
//$result = db_query("SELECT * FROM {booking_person} p WHERE p.booking_event_id = :eid",
// array(':eid' => $event->eid));
foreach ($result as $person) {
$rows[] = array(
l(t('!id', array('!id' => $person->booking_person_nid)), t('node/!id', array('!id' => $person->booking_person_nid))),
t('!first !last', array('!first' => ucwords($person->booking_first_name), '!last' => ucwords($person->booking_last_name))),
t('!email', array('!email' => $person->booking_buyer_email)),
t(_booking_convert_ts($person->booking_payment_date)->format('j F, Y H:i')),
t('!payment', array('!payment' => $person->booking_mc_gross)),
t('!currency', array('!currency' => $person->booking_mc_currency)),
t('!fee', array('!fee' => $person->booking_mc_fee)),
t('!invoice', array('!invoice' => $person->booking_invoice)),
t($person->booking_payment_status),
t($person->booking_payer_id),
t($person->booking_item_name),
t($person->booking_ipn_track_id)
);
$total_paid += $person->booking_mc_gross;
$total_fees += $person->booking_mc_fee;
}
$output .= t("<p>The following table presents payment entries from paypal and manual payments that have been made for !event.</p>", array('!event' => $event->booking_eventname));
$output .= theme('table', array('header' => $header, 'rows' => $rows));
$output .= t("<p>Gross amount paid: $!paid. Total amount of fees paid: $!fees</p>", array('!paid' => $total_paid, '!fees' => $total_fees));
return $output;
}
function booking_coming_page() {
global $event;
$output = "";
$table = "";
//$header = array('Name', 'Occupation', 'State');
$header = array('Name', 'State');
$booking_limit = variable_get('booking_regn_limit','350');
$rows = array();
if (variable_get('booking_auto_show_on_lists', 1) == 1)
{
$or = db_or()->condition('p.booking_status', 0)->condition('p.booking_status', 1);
$result = db_select('booking_person', 'p')
->fields('p', array('booking_firstname', 'booking_lastname', 'booking_state', 'booking_readinggroup', 'booking_country'))
->condition($or)
->condition('p.booking_event_id', $event->eid, '=')
->orderBy('booking_country')
->orderBy('booking_state')
->orderBy('booking_lastname')
->orderBy('booking_firstname')
->execute();
}
else
{
$result = db_select('booking_person', 'p')
->fields('p', array('booking_firstname', 'booking_lastname', 'booking_state', 'booking_readinggroup', 'booking_country'))
->condition('p.booking_status', 1)
->condition('p.booking_event_id', $event->eid, '=')
->orderBy('booking_country')
->orderBy('booking_state')
->orderBy('booking_lastname')
->orderBy('booking_firstname')
->execute();
/*
$result = db_query('SELECT booking_firstname, booking_lastname, booking_state, booking_readinggroup, booking_country
FROM (
SELECT booking_firstname, booking_lastname, booking_state, booking_readinggroup, booking_country
FROM {booking_person}
WHERE booking_status = 1 and booking_event_id = :eid
) AS booking
ORDER BY booking_state, booking_lastname, booking_firstname',
array(':eid' => $event->eid));
*/
}
//while ($booking = db_fetch_object($result)) {
//watchdog('booking', "Who's coming query: @info", array('@info' => var_export($result, TRUE)));
foreach ($result as $person) {
$rows[] = array(
t('!first !last', array('!first' => ucwords($person->booking_firstname),
'!last' => ucwords($person->booking_lastname))),
//only for when we have readings groups
//t('!group',array('!group' => $person->booking_readinggroup)),
t('!state', array('!state' => ($person->booking_country === variable_get('booking_default_country')) ? $person->booking_state : $person->booking_country)),
);
}
//watchdog('booking', "Who's coming formatted: @info", array('@info' => var_export($rows, TRUE)));
//output the results
//check there were some bookings
if (count($rows) > 0)
{
if (count($rows) >= $booking_limit)
{
//there is a waiting list
$output .= token_replace(variable_get('booking_whoscoming_pre_waitlist_text'), booking_define_tokens());
}
else
{
//there's no waiting list
$output .= token_replace(variable_get('booking_whoscoming_pre_text'), booking_define_tokens());
}
//theme the table of registrations
$output .= theme('table', array('header' => $header, 'rows' => $rows));
}
else
{
//no bookings
$output .= token_replace(variable_get('booking_whoscoming_pre_noregistrations_text'), booking_define_tokens());
}
//include any post-text
$output .= token_replace(variable_get('booking_whoscoming_post_text'), booking_define_tokens());
return $output;
}
function booking_waitinglist_page() {
global $event;
$output = "";
$table = "";
$count = 1;
//$header = array('Name', 'Occupation', 'State');
$header = array('Name', 'State', 'Position');
$booking_limit = variable_get('booking_regn_limit','350');
$rows = array();
$result = db_query('SELECT DISTINCT nid, booking_firstname, booking_lastname, booking_state, booking_readinggroup, booking_country
FROM (
SELECT p.nid, p.booking_firstname, p.booking_lastname, p.booking_state, p.booking_country, p.booking_readinggroup, pay.booking_payment_date
FROM {booking_person} p, {booking_payment} pay
WHERE booking_status = 2 and booking_event_id = :eid and p.nid = pay.booking_person_nid
) AS booking
ORDER BY booking_payment_date',
array(':eid' => $event->eid));
//watchdog('booking', "Who's coming query: @info", array('@info' => var_export($result, TRUE)));
foreach ($result as $person) {
$rows[] = array(
t('!first !last', array('!first' => ucwords($person->booking_firstname),
'!last' => ucwords($person->booking_lastname))),
//only for when we have readings groups
//t('!group',array('!group' => $person->booking_readinggroup)),
t('!state', array('!state' => $person->booking_country == 'Australia' ? $person->booking_state : $person->booking_country)),
t($count++),
);
}
//watchdog('booking', "Who's coming formatted: @info", array('@info' => var_export($rows, TRUE)));
//output the results
//check there were people on the waiting list
if (count($rows) > 0)
{
//include the pre-text
$output .= token_replace(variable_get('booking_waitingpage_pre_text'), booking_define_tokens());
//theme the table of waiting list people
$output .= theme('table', array('header' => $header, 'rows' => $rows));
}
else
{
//no one on the waiting list
$output .= token_replace(variable_get('booking_waitingpage_pre_nowaitlist_text'), booking_define_tokens());
}
//include any post-text
$output .= token_replace(variable_get('booking_waitingpage_post_text'), booking_define_tokens());
return $output;
}
/**
* Generate a CSV file as a report of all current registrations
*/
function booking_csv_report() {
global $event;
$name = 'bookings-' . format_date(time(), 'custom', 'Y-m-d-His');
$filename = file_directory_temp() . '/' . $name;
$delimiter = ',';
$enclosure = '"';
$encloseAll = true;
$nullToMysqlNull = true;
$delimiter_esc = preg_quote($delimiter, '/');
$enclosure_esc = preg_quote($enclosure, '/');
$fields_to_skip = array('booking_payment_id', 'booking_event_id', 'booking_tempid');
//query the db
/*
$query = db_select('booking_person', 'p');
$query->join('booking_price', 'pr', 'p.booking_payment_id = pr.pid');
$query->condition('p.booking_event_id', $event->eid)
->fields('p')
->fields('pr', array('booking_price', 'booking_price_descrip'));
$result = $query->execute()->fetchAll();
*/
//pivot table based on http://anothermysqldba.blogspot.de/2013/06/pivot-tables-example-in-mysql.html
$query = db_query("select distinct p.*, s1.booking_session_id as session1, s1.booking_is_leader as session1_leader, s1.booking_is_helper as session1_helper, s2.booking_session_id as session2, s2.booking_is_leader as session2_leader, s2.booking_is_helper as session2_helper, s3.booking_session_id as session3, s3.booking_is_leader as session3_leader, s3.booking_is_helper as session3_helper, s4.booking_session_id as session4, s4.booking_is_leader as session4_leader, s4.booking_is_helper as session4_helper, s5.booking_session_id as session5, s5.booking_is_leader as session5_leader, s5.booking_is_helper as session5_helper, s6.booking_session_id as session6, s6.booking_is_leader as session6_leader, s6.booking_is_helper as session6_helper from {booking_person} p
left outer join {booking_studygroup_mapping} s1 on p.nid = s1.booking_node_id and s1.booking_studygroup_id = 1
left outer join {booking_studygroup_mapping} s2 on p.nid = s2.booking_node_id and s2.booking_studygroup_id = 2
left outer join {booking_studygroup_mapping} s3 on p.nid = s3.booking_node_id and s3.booking_studygroup_id = 3
left outer join {booking_studygroup_mapping} s4 on p.nid = s4.booking_node_id and s4.booking_studygroup_id = 4
left outer join {booking_studygroup_mapping} s5 on p.nid = s5.booking_node_id and s5.booking_studygroup_id = 5
left outer join {booking_studygroup_mapping} s6 on p.nid = s6.booking_node_id and s6.booking_studygroup_id = 6
where p.booking_event_id = :eid ORDER BY p.nid", array('eid' => $event->eid));
$result = $query->fetchAllAssoc('nid');
//watchdog('booking', "CSV raw data: @info", array('@info' => var_export($result, TRUE)));
//open the filehandle
$handle = @fopen($filename, 'w');
//write the header based on the first result
$header_array = array();
foreach (reset($result) as $key => $value)
{
if (in_array($key, $fields_to_skip))
continue;
$header_array[] = $key;
//add in a special column for a processed version of the date of birth
if (strcmp($key,"booking_dob") == 0)
$header_array[] = "booking_dob_processed";
}
$header = implode( $delimiter, $header_array );
//watchdog('booking', "CSV header: @info", array('@info' => var_export($header_array, TRUE)));
@fwrite($handle, $header . "\n");
//each record
foreach ($result as $record) {
$output = array();
//each keypair in the record
foreach ($record as $key => $value)
{
//fields to skip
if (in_array($key, $fields_to_skip))
continue;
//check for null
if ($value === NULL && $nullToMysqlNull) {
$output[] = 'NULL';
continue;
}
//handle dates
if ($key == 'booking_dob') {
$output[] = format_date($value, 'custom', 'd/m/Y');
$output[] = '"' . _booking_avg_age($value, 1, $event->booking_event_start) . '"';
continue;
}
if ($key == 'booking_passport_expiry_date') {
$output[] = format_date($value, 'custom', 'd/m/Y');
continue;
}
//handle more exact dates
if ($key == 'booking_timestamp') {
$output[] = format_date($value, 'custom', 'd/m/Y H:i');
continue;
}
//booking status
if ($key == 'booking_status') {
$output[] = _booking_status_generate($value);
continue;
}
// Enclose fields containing $delimiter, $enclosure or whitespace
if ( $encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $value ) ) {
$output[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $value) . $enclosure;
}
else {
$output[] = $field;
}
}
$row = implode($delimiter, $output) . "\n";
//watchdog('booking', "Report filename: " . $filename . ".");
/*
//$header = "Barcode,Timestamp,First Name,Last Name,Gender,Date of Birth,Street,Suburb,Postcode,State,Country,Home Phone Number,Mobile Phone Number,Email,Second Email,Ecclesia,Baptised,Married,Partner Name,Room Mate 1,Room Mate 2,Help Areas,Attending Post Conference,Payment Method,Deposit Payment Deadline,Amount Paid,Discount,Status,T-shirt size";
$header = "First Name,Last Name,Gender,Date of Birth,Married,Partner Name,Email Address,Mobile Phone,Home Phone,Baptised,";
if (variable_get('booking_enable_passport', 1) == 1)
$header .= "Passport Number,Passport Expiry,Passport Issue Name,Passport Issue Location,";
if (variable_get('booking_enable_tshirts', 0) == 1)
$header .= "T-Shirt size,";
$header .= "Ecclesia,Street,Suburb,Post Code,State,Country,Emergency Contact Name,Emergency Contact Type,Emergency Contact Phone," .
"Emergency Contact Phone Alt,Medicare Number,Help Reading,Help Chairing,Help Music,Dietary Requirements,First Aid,Nurse," .
"Booking Status,Date Paid,Amount Due,Amount Paid";
if (variable_get('booking_enable_skills', 1) == 1)
$header .= ",Building Skills,Cooking Skills,Child Minding Skills,Other Languages,Mission Experience";
$query = db_query("SELECT * FROM {booking_person} p WHERE p.booking_event_id = :eid",
array(':eid' => $event->eid));
$result = $query->fetchAll();
$music_help = empty($r->booking_help_music) ? '' : $r->booking_help_music;
$row = ucwords($r->booking_firstname) . ',' . ucwords($r->booking_lastname) . ',' . $r->booking_gender . ',' . format_date($r->booking_dob, 'custom', 'd/m/Y') . ',' .
$r->booking_married . ',' . $r->booking_partner_name . ',' . $r->booking_email . ',"' . $r->booking_mobile . '","' . $r->booking_phone . '",' .
$r->booking_baptised . ',';
if (variable_get('booking_enable_passport', 1) == 1)
$row .= $r->booking_passport_num . ',' .
$row .= '"' . $r->booking_ecclesia . '","' . $r->booking_street . '","' . $r->booking_suburb . '",' . $r->booking_postcode . ',' .
$r->booking_state . ',' . $r->booking_country . ',"' .
$r->booking_guardian_name . '",' . $r->booking_guardian_type . ',"' . $r->booking_guardian_phone . '","' . $r->booking_guardian_phone_alt . '",' .
$r->booking_medicare . ',' .
$r->booking_help_reading . ',' . $r->booking_help_chairing . ',"' . $music_help . '","' .
$r->booking_dietary . '",' . $r->booking_firstaid . ',' . $r->booking_nurse . ',' .
_booking_status_generate($r->booking_status) . ',' . format_date(_booking_datepaid_ts($r->nid), 'custom', 'd/m/Y H:i') . ',' .
$r->booking_total_pay_reqd . ',' . $r->booking_amount_paid . "\n";
*/
@fwrite($handle, $row);
//$index++;
}
@fclose($handle);
drupal_add_http_header("Content-type", "application/octet-stream; charset=utf-8");
drupal_add_http_header("Content-Disposition", "attachment; filename=" . $name . ".csv");
@readfile($filename);
@unlink($filename);
exit(0);
}