'max-width:30%'); //define sorting information with the header //as per http://www.drup-all.com/blog/table-sort-pagination-drupal-7 $header = array(); $header[] = array('data' => t('Id'), 'field' => 'nid', 'sort' => 'asc'); $header[] = array('data' => t('Name'), 'field' => 'booking_lastname'); $header[] = array('data' => t('Booking Status'), 'field' => 'booking_status'); // Add some optional columns if (variable_get('booking_enable_studygroups', 0) == 1) { $header[] = array('data' => t('Studygroups')); } if (variable_get('booking_enable_roomallocations', 0) == 1) { $header[] = array('data' => t('Room')); } $header[] = array('data' => t('Travel')); $header[] = array('data' => t('Email'), 'field' => 'booking_email'); $header[] = array('data' => t('Payment To Date'), 'field' => 'booking_amount_paid'); $header[] = array('data' => t('Total Payment Required')); $header[] = array('data' => t('Fully paid?'), 'field' => 'booking_payment_complete'); $header[] = array('data' => t('Refund Processed?'), 'field' => 'booking_refund_processed'); $header[] = array('data' => t('Refund Due'), 'field' => 'booking_refund_due'); $header[] = array('data' => t('Welfare Required?'), 'field' => 'booking_welfare_required'); $header[] = array('data' => t('Committee?'), 'field' => 'booking_committee_member'); $rows = array(); //the state summary table $state_header = array('State', 'Count', 'Males', 'Females', 'Baptised', 'Male Average Age', 'Female Average Age', 'Overall Average Age'); $state_rows = array(); $state_statistics = array(); //$state_statistics = new stdClass(); //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')); //include people either booked in or on the waiting list, that belong to the current event id $db_or = db_or(); $db_or->condition('p.booking_status', 1, '='); $db_or->condition('p.booking_status', 2, '='); $db_and = db_and()->condition($db_or)->condition('p.booking_eventid', $event->eid, '='); $query->condition($db_and); $query->groupBy('p.booking_state'); $query->addExpression('COUNT(p.booking_state)', 'state_count'); $query->orderBy('state_count', 'DESC'); $state_stats = $query->execute(); //add some age info to an array so we can calculate age info and generate the actual rows after the more detailed summary loop //include count of guys, girls, minimum, maximum and average ages foreach ($state_stats as $state) { if (strcmp($state->booking_country,'Australia') == 0 ) { //$state_rows[] = array($state->booking_state, $state->state_count); //store the total count for this state if (!isset($state_statistics[$state->booking_state])) { $state_statistics[$state->booking_state] = new stdClass(); } $state_statistics[$state->booking_state]->total_count = $state->state_count; $state_statistics[$state->booking_state]->male_count = 0; $state_statistics[$state->booking_state]->male_avg = 0; $state_statistics[$state->booking_state]->female_count = 0; $state_statistics[$state->booking_state]->female_avg = 0; $state_statistics[$state->booking_state]->total_avg = 0; $state_statistics[$state->booking_state]->baptised_count = 0; } else { $non_australia_count += $state->state_count; } } //non australian states //$state_rows[] = array('International', $non_australia_count); $state_statistics['International'] = new stdClass(); $state_statistics['International']->total_count = $non_australia_count; $state_statistics['International']->male_count = 0; $state_statistics['International']->male_avg = 0; $state_statistics['International']->female_count = 0; $state_statistics['International']->female_avg = 0; $state_statistics['International']->total_avg = 0; $state_statistics['International']->baptised_count = 0; //the ecclesia summary table $ecclesia_header = array('State','Ecclesia', 'Count'); $ecclesia_rows = array(); //bookings by ecclesia $query = db_select('booking_person', 'p') ->fields('p', array('booking_ecclesia','booking_state')) ->condition('p.booking_eventid', $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'); $query->join('booking_price', 'pr', 'pr.pid = p.booking_payment_id'); $query->leftJoin('booking_travel', 't', 'p.nid = t.booking_person_nid'); $query->condition('p.booking_eventid', $event->eid, '=') ->fields('p') ->fields('t') ->fields('pr', array('booking_price', 'booking_late_price')); $table_sort = $query->extend('TableSort')->orderbyHeader($header); $result = $table_sort->execute(); foreach ($result as $person) { $this_row = array(); //$amount_owing = _booking_amount_owing($person->nid, 0, FALSE); $amount_owing = _booking_amount_owing($person, 0, FALSE); //calculate the travel link if ($person->tid > 0) { $travel_link = l(t('Travel'), t('node/!id/edit', array('!id' => $person->tid))); } else { $travel_link = t('N/A'); } //define the row for this person $this_row[] = l(t('Edit !id', array('!id' => $person->nid)), t('node/!id/edit', array('!id' => $person->nid))); $this_row[] = l(t('!first !last', array('!first' => ucwords($person->booking_firstname), '!last' => ucwords($person->booking_lastname))), t('node/!id', array('!id' => $person->nid)) ); $this_row[] = _booking_status_generate($person->booking_status); if (variable_get('booking_enable_studygroups', 0) == 1) { $this_row[] = l(t('Groups'), t('admin/booking/!id/edit-studygroup-membership', array('!id' => $person->nid))); } if (variable_get('booking_enable_roomallocations', 0) == 1) { $this_row[] =l(t('Room'), t('admin/booking/!id/edit-room', array('!id' => $person->nid))); } $this_row[] = $travel_link; $this_row[] = t('!email', array('!email' => $person->booking_email)); $this_row[] = t('!payment', array('!payment' => $person->booking_amount_paid)); $this_row[] = t('!payment', array('!payment' => $amount_owing == 0 ? $person->booking_total_pay_reqd : _booking_total_due($person))); //$this_row[] = t('!fullypaid', array('!fullypaid' => $amount_owing == 0 ? 'Yes' : 'No')); $this_row[] = t('!fullypaid', array('!fullypaid' => $person->booking_payment_complete == 'Y' ? 'Yes' : 'No')); $this_row[] = t('!reqd', array('!reqd' => $person->booking_refund_processed == 'Y' ? 'Yes' : 'No')); $this_row[] = t('!payment', array('!payment' => $person->booking_refund_due)); $this_row[] = t($person->booking_welfare_required == 'Y' ? 'Yes' : 'No'); $this_row[] = t($person->booking_committee_member == 'Y' ? 'Yes' : 'No'); $rows[] = $this_row; //add up the total paid $total_paid += $person->booking_amount_paid; //subtract any refund if ($person->booking_refund_processed == 'Y' && $person->booking_refund_due > 0) { $total_refunds += $person->booking_refund_due; } //travel form completed? if (! empty($person->tid)) { $travelform_count++; } //booking status if ($person->booking_status == 0) { $notpaid_counter++; } elseif ($person->booking_status == 1) { $bookedin_counter++; } elseif ($person->booking_status == 2) { $waiting_counter++; } elseif ($person->booking_status == 5) { $hosts_counter++; } else { $notcoming_counter++; } //welfare if ($person->booking_welfare_required == 'Y') { $welfare_count++; } //committee? if ($person->booking_committee_member == 'Y') { $committee_count++; } //fully paid? if ($amount_owing == 0) { $fullypaid_count++; } $person_count++; $state = strcmp($person->booking_country,'Australia') == 0 ? $person->booking_state : 'International'; //general stats for booked in people if ($person->booking_status == 1) { //for overall average age $dob_total += $person->booking_dob; //store data for average ages per gender and state if ($person->booking_gender == 'M') { $male_count++; $male_dob_total += $person->booking_dob; $state_statistics[$state]->male_avg += $person->booking_dob; $state_statistics[$state]->male_count++; } else { $female_count++; $female_dob_total += $person->booking_dob; $state_statistics[$state]->female_avg += $person->booking_dob; $state_statistics[$state]->female_count++; } if ($person->booking_baptised == 'Y') { $baptised_count++; $state_statistics[$state]->baptised_count++; } if ($person->booking_married == 'Y') $married_count++; } } //generate the table for state statistics foreach ($state_statistics as $key => $value) { //$state_rows[] = array($state, $state->total_count, $state->male_count, $state->female_count, 0); $male_average_age = _booking_avg_age($value->male_avg, $value->male_count, $event->booking_event_start); $female_average_age = _booking_avg_age($value->female_avg, $value->female_count, $event->booking_event_start); $state_average_age = _booking_avg_age($value->female_avg + $value->male_avg, $value->female_count + $value->male_count, $event->booking_event_start); $data = array($key, $value->total_count, $value->male_count, $value->female_count, $value->baptised_count, $male_average_age, $female_average_age, $state_average_age); //watchdog('booking', "
State statistics:\n@info", array('@info' => print_r( $data, true))); $state_rows[] = $data; } //output everything $output .= t("
There are !bookedin registrations currently booked in, !waiting on waiting list, !notpaid haven't paid, !hosts are hosts, " . "and !notcoming are no longer coming, which comes to a total of !total people who have filled in the registration form. !travel people have filled in their travel form.
", array('!bookedin' => $bookedin_counter, '!waiting' => $waiting_counter, '!notpaid' => $notpaid_counter, '!total' => $person_count, '!travel' => $travelform_count, '!notcoming' => $notcoming_counter, '!hosts' => $hosts_counter)); $output .= t("There are !boys males and !girls females currently booked in. Of these, !baptised are baptised and !married are married.
", array('!boys' => $male_count, '!girls' => $female_count, '!baptised' => $baptised_count, '!married' => $married_count )); $output .= t("The combined 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.
There are !welfare people with special financial consideration approved, and !committee people on the committee. " . "!fullypaid people have completed their payments.
", array('!welfare' => $welfare_count, '!fullypaid' => $fullypaid_count, '!committee' => $committee_count, )); $output .= t("Total amount paid: $!paid, minus $!refunds in refunds.", array('!paid' => $total_paid, '!refunds' => $total_refunds)); $output .= t("The following table presents payment entries from paypal and manual payments that have been made for !event.
", array('!event' => $event->booking_eventname)); $output .= theme('table', array('header' => $header, 'rows' => $rows)); $output .= t("Gross amount paid: $!paid. Total amount of fees paid: $!fees
", array('!paid' => $total_paid, '!fees' => $total_fees)); return $output; } /** * List everyone's flight info */ function booking_report_flight_details() { global $event; $form = array(); $prefix = t("View flight information for all attendees with status currently set to booked in.
"); $header = array( 'booking_name' => array('data' => t('Name'), 'field' => 'booking_lastname'), 'booking_destination_country' => array('data' => t('Country'), 'field' => 'booking_destination_country'), 'booking_outflight_bookingnum' => array('data' => t('Reference'), 'field' => 'booking_outflight_bookingnum'), 'booking_outflight_flightnum' => array('data' => t('Flight #'), 'field' => 'booking_outflight_flightnum'), 'booking_outflight_origin' => array('data' => t('Description'), 'field' => 'booking_outflight_origin'), 'booking_outflight_origin_ts' => array('data' => t('Departure Time'), 'field' => 'booking_outflight_origin_ts'), 'booking_outflight_connecting_flightnum' => array('data' => t('Connecting Flight'), 'field' => 'booking_outflight_connecting_flightnum'), 'booking_outflight_destination' => array('data' => t('Description'), 'field' => 'booking_outflight_destination'), 'booking_outflight_destination_ts' => array('data' => t('Time'), 'field' => 'booking_outflight_destination_ts'), 'booking_rtrnflight_bookingnum' => array('data' => t('Return Reference'), 'field' => 'booking_rtrnflight_bookingnum'), 'booking_rtrnflight_flightnum' => array('data' => t('Flight #'), 'field' => 'booking_rtrnflight_flightnum'), 'booking_rtrnflight_origin' => array('data' => t('Description'), 'field' => 'booking_rtrnflight_origin'), 'booking_rtrnflight_origin_ts' => array('data' => t('Time'), 'field' => 'booking_rtrnflight_origin_ts'), ); $query = db_select('booking_person', 'p'); $query->fields('p'); $db_and = db_and(); $db_and->condition('p.booking_eventid', $event->eid, '='); $db_and->condition('p.booking_status', 1, '='); $query->condition($db_and); $table_sort = $query->extend('TableSort')->orderbyHeader($header); $result = $table_sort->execute(); foreach($result as $data) { $name_link = l(t('!first !last', array('!first' => ucwords($data->booking_firstname), '!last' => ucwords($data->booking_lastname))), t('node/!id', array('!id' => $data->nid)) ); $rows[] = array ( 'data' => array( $name_link, $data->booking_destination_country, $data->booking_outflight_bookingnum, $data->booking_outflight_flightnum, $data->booking_outflight_origin, $data->booking_outflight_origin_ts == 0 ? '' : format_date($data->booking_outflight_origin_ts, 'custom', 'd/m/Y H:i'), $data->booking_outflight_connecting_flightnum, $data->booking_outflight_destination, $data->booking_outflight_destination_ts == 0 ? '' : format_date($data->booking_outflight_destination_ts, 'custom', 'd/m/Y H:i'), $data->booking_rtrnflight_bookingnum, $data->booking_rtrnflight_flightnum, $data->booking_rtrnflight_origin, $data->booking_rtrnflight_origin_ts == 0 ? '' : format_date($data->booking_rtrnflight_origin_ts, 'custom', 'd/m/Y H:i'), ), ); } $result = array ( 'first_para' => array ( '#type' => 'markup', '#markup' => $prefix, ), 'table' => array ( '#theme' => 'table', '#header' => $header, '#rows' => $rows, '#attributes' => array('id' => 'sort-table'), //'#sticky' => FALSE, ) ); return $result; } /** * List everyone's travel info */ function booking_report_travel() { global $event; //$form = array(); $prefix = t("CSV report headers\n@info", array('@info' => print_r($header_array, true))); //$header = implode( $delimiter, $header_array ); //$csv .= $header . "\n"; //watchdog('booking', "CSV header: @info", array('@info' => var_export($header_array, TRUE))); //each record foreach ($result as $record) { //watchdog('booking', "CSV raw data entry: @info", array('@info' => var_export($record, TRUE))); $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; } //capitalise street name and suburb name if ($key == 'booking_street' || $key == 'booking_suburb') { $value = _booking_ucname($value); } //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 (in_array($key, $datetime_fields)) { $output[] = $value == 0 ? '0' : format_date($value, 'custom', 'd/m/Y H:i'); continue; } //handle numerical fields if (in_array($key, $number_only_fields)) { $output[] = $value . "\t"; continue; } //booking status if ($key == 'booking_status') { $output[] = _booking_status_generate($value); continue; } //studygroup group session roles if (preg_match("/session\d+_role/", $key)) { $output[] = _booking_studygroup_role_lookup($value); continue; } /* //this is not required since node_load already has the room location description for each attendee //room location if ($key == 'booking_room_location_id') { $output[] = $room_locations[$value]->booking_roomlocation_descrip; continue; } */ //room bed type if ($key == 'booking_room_bedtype') { $output[] = _booking_room_bedtype_lookup($value); continue; } //reading group team colour if ($key == $readinggroup_id) { $output[] = _booking_readinggroup_colour_lookup($value); continue; } //add in the amount owing using the nid as the key if ($key == 'nid') { $output[] = $value; //add the amount owing firstly including paypal fees then excluding them //bit of a hack since this can never be excluded from the report $output[] = _booking_amount_owing($record, 0, TRUE); $output[] = _booking_amount_owing($record, 0, FALSE); 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; //} } // End of record processing $rows[] = $output; //$row = implode($delimiter, $output) . "\n"; //$csv .= $row; } watchdog('booking_debug', "
CSV report spreadsheet rows\n@info", array('@info' => print_r( $rows, true))); // Create headers for Excel spreadsheet header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"'); header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Pragma: public'); $sheetname = $event->booking_eventname; $writer = new XLSXWriter(); $writer->setAuthor($event->booking_eventname); //Add the header row $writer->writeSheetHeader($sheetname, $header_array, $header_style); //Add the data foreach($rows as $row) { $writer->writeSheetRow($sheetname, $row, $row_style); } $writer->writeToStdOut(); exit(0); //see http://stackoverflow.com/questions/4348802/how-can-i-output-a-utf-8-csv-in-php-that-excel-will-read-properly // but none of these options seem to work //drupal_add_http_header("Content-type", "application/octet-stream; charset=utf-8"); //drupal_add_http_header("Content-Disposition", "attachment; filename=" . $name . ".csv"); //print $csv; //exit(0); }