Loads of changes

This commit is contained in:
2014-02-24 23:53:12 +11:00
parent 66d4c89679
commit b710954bdd
10 changed files with 357 additions and 251 deletions

View File

@@ -175,6 +175,11 @@ function _datetime_array_to_ts($date)
//watchdog('booking', 'Date-time Conversion: @info', array('@info' => var_export($date, TRUE)));
date_default_timezone_set(TIMEZONE);
$tz = new DateTimeZone(TIMEZONE);
if (empty($date) || ($date['month'] == '' && $date['day'] == '' && $date['year'] == ''))
return 0;
else
watchdog('booking', "Date array to timestamp: @info", array('@info' => var_export($date, TRUE)));
$gmt_ts = mktime($date['hour'], $date['minute'], 0, $date['month'], $date['day'], $date['year']);
$ts = new DateTime("@$gmt_ts");
@@ -294,6 +299,17 @@ function _booking_avg_age($sum, $count, $reference_ts)
return $difference->y . " years, " . $difference->m . " months, " . $difference->d . " days";
}
/**
* Helper function to calculate persons age in years at start of event
*/
function _booking_get_age_years($dob_ts)
{
global $event;
$reference_ts = _booking_convert_ts($event->booking_event_start);
$dob = _booking_convert_ts($dob_ts);
return $dob->diff($reference_ts)->y;
}
function _date_range_to_string($date_start, $date_end) {
$start = _booking_split_date($date_start);
$end = _booking_split_date($date_end);
@@ -338,6 +354,44 @@ function _date_range_to_string($date_start, $date_end) {
return $final_string;
}
/**
* Function for calculating statistics of attendees
*/
function _booking_generate_statistics($booking_list)
{
global $event;
$statistics = array(
'males' => 0,
'females' => 0,
'under20' => 0,
'20to25' => 0,
'over25' => 0,
);
foreach ($booking_list as $person)
{
//ignore people that havent paid or are no longer coming
if ($person->booking_status == 0 || $person->booking_status == 3)
continue;
if ($person->booking_gender == 'M')
$statistics['males']++;
else
$statistics['females']++;
$age = _booking_get_age_years($person->booking_dob);
if ($age < 20)
$statistics['under20']++;
elseif($age >= 20 && $age < 25)
$statistics['20to25']++;
else
$statistics['over25']++;
}
return $statistics;
}
//figure out if we're in the right time period for discounted registration rates
function _booking_is_earlybird()
@@ -426,7 +480,9 @@ function _booking_amount_paid($nid, $person = NULL)
if ($person->booking_partner_id > 0)
{
//watchdog('booking', "Checking total paid for married person");
//look for payments in the payment transaction table
//if we're combining the payment for married couples
if (variable_get('booking_enable_combined_pricing', 0) == 1)
{
@@ -448,7 +504,7 @@ function _booking_amount_paid($nid, $person = NULL)
//watchdog('booking', "Total amount paid according to paypal payments table is " . $amount_paid);
//if there were no results, $amount_paid will still be 0
//there were no payment transactions, so rely on what the amount is set to in the individual registration records
if ($amount_paid == 0)
{
//if we're combining the payment for married couples
@@ -480,6 +536,8 @@ function _booking_amount_paid($nid, $person = NULL)
foreach ($query as $payment)
$amount_paid += ($payment->booking_mc_gross - $payment->booking_mc_fee);
//watchdog('booking', "Total amount paid according to paypal payments table is " . $amount_paid);
//if there were no results, $amount_paid will still be 0
if ($amount_paid == 0)
$amount_paid = $person->booking_amount_paid;
@@ -501,16 +559,24 @@ function _booking_amount_owing($nid, $amount_paid = 0)
//fetch details about the person
$person = db_query("SELECT price.booking_price, price.booking_late_price, person.booking_payment_id, " .
"person.booking_total_pay_reqd, person.booking_amount_paid, person.booking_partner_id, person.booking_country " .
"person.booking_total_pay_reqd, person.booking_amount_paid, person.booking_partner_id, person.booking_country, person.booking_welfare_required " .
"FROM {booking_person} person, {booking_price} price " .
"WHERE person.nid = :nid " .
"AND person.booking_payment_id = price.pid",
array(':nid' => $nid))
->fetchObject();
//check for early bird rate or full rate
if (_booking_is_earlybird() == TRUE)
//determine what rate this person needs to pay
if ($person->booking_welfare_required == 'Y')
{
//cater for any manual adjustment
//watchdog('booking', "This registration is eligible for welfare rates");
$total_due = $person->booking_total_pay_reqd;
}
//the early bird rate will be defined by default for the registration
elseif (_booking_is_earlybird() == TRUE)
$total_due = $person->booking_total_pay_reqd;
//finally we must be in the late-fee period
else
$total_due = $person->booking_late_price;
@@ -519,80 +585,9 @@ function _booking_amount_owing($nid, $amount_paid = 0)
//if we didn't get the amount paid as a command line parameter, check it now
if ($amount_paid == 0)
{
//watchdog('booking', "Checking total amount already paid by " . $nid);
$amount_paid = _booking_amount_paid($nid, $person);
}
/*
//check for a spouse
if ($person->booking_partner_id > 0)
{
watchdog('booking', "Checking outstanding balance for married person");
//if we're combining the payment for married couples
if (variable_get('booking_enable_combined_pricing', 0) == 1)
{
//check for payments in the paypal transaction table from either spouse
$query = db_query("SELECT booking_mc_gross FROM booking_payment " .
"WHERE booking_person_nid = :nid OR booking_person_nid = :spousenid",
array(':nid' => $nid, 'spousenid' => $person->booking_partner_id));
}
else {
//check for payments in the paypal transaction table from just this person
$query = db_query("SELECT booking_mc_gross FROM booking_payment " .
"WHERE booking_person_nid = :nid",
array(':nid' => $nid));
}
//add up all the payments
foreach ($query as $payment)
$amount_paid += $payment->booking_mc_gross;
watchdog('booking', "Total amount paid according to paypal payments table is " . $amount_paid);
//if there were no results, $amount_paid will still be 0
if ($amount_paid == 0)
{
//if we're combining the payment for married couples
if (variable_get('booking_enable_combined_pricing', 0) == 1)
{
$spouse = db_query("SELECT person.booking_amount_paid " .
"FROM {booking_person} person " .
"WHERE person.nid = :nid ",
array(':nid' => $person->booking_partner_id))
->fetchObject();
$amount_paid = $person->booking_amount_paid + $spouse->booking_amount_paid;
watchdog('booking', "Total combined payments for couple based on totals in booking_person table is " . $amount_paid);
}
else
{
$amount_paid = $person->booking_amount_paid;
watchdog('booking', "Total amount paid for individual based on totals in booking_person table is " . $amount_paid);
}
}
}
else
{
//Check if there are payment records in the paypal transaction table for this unmarried person
//if so, base their payments on the gross amount in there rather than what the person booking_person database says
$query = db_query("SELECT booking_mc_gross FROM booking_payment " .
"WHERE booking_person_nid = :nid",
array(':nid' => $nid));
foreach ($query as $payment)
$amount_paid += $payment->booking_mc_gross;
//if there were no results, $amount_paid will still be 0
if ($amount_paid == 0)
$amount_paid = $person->booking_amount_paid;
//in case there has been some manual adjustment
elseif ($amount_paid < $person->booking_amount_paid)
$amount_paid = $person->booking_amount_paid;
}
watchdog('booking', "Total amount already paid for this registration " . $nid . " is " . $amount_paid);
*/
}
//check if there is anything outstanding
if ($amount_paid >= $total_due)
{
//watchdog('booking', "This person doesn't owe any money: @info", array('@info' => var_export($person, TRUE)));
@@ -609,13 +604,55 @@ function _booking_amount_owing($nid, $amount_paid = 0)
$amount_owing = $amount_owing / (1 - 0.024);
//paypal charges 3.4 percent if they're doing a currency conversion
else
{
$amount_owing = $amount_owing / (1 - 0.034);
watchdog('booking', "This is an international registration.");
}
}
else
$amount_owing = $total_due - $amount_paid;
return number_format($amount_owing, 2, '.', '');
}
/**
* Helper function to cycle through a list without getting in an endless loop
* Used for calculating study groups and maybe readings groups
*
* Each iteration through the list the maximum permitted size for a given entry will be increased by one
* until we find an available entry or we run out of cycle counts
*/
function _booking_loop_carefully(&$list, $field, &$i, $maximum, $list_size, $max_cycle_count)
{
$cycle = 0;
while ($list[$i][$field] >= $maximum)
{
drupal_set_message(t("Field !field is !value for entry !index, which larger than maximum allowed size, !max. Moving to next entry in list.",
array('!field' => $field, '!value' => $list[$i][$field], '!max' => $maximum, '!index' => $i)));
$i++;
//reset the counter if we go past the end
if ($i > $list_size)
{
$i = 1;
//make sure we don't get stuck in an infinite loop - only go past the end once
if ($cycle >= $max_cycle_count)
{
drupal_set_message(t("Reached the end of !field list. Unable to find satisfactory entry after !num cycles.",
array('!field' => $field, '!num' => $cycle)));
return;
}
elseif ($cycle > 0)
{
//temporarily increase the maximum size of our groups by one
$maximum++;
}
//always increment the cycle counter if we've reached the end
$cycle++;;
}
}
}
/**
* Helper function to generate paypal form for payments
*/
@@ -628,6 +665,8 @@ function _booking_paypal_form($person, $invoiceid, $amount_owing, $button_text)
*/
function _booking_paypal_form_builder($node, &$form_state, $person, $invoiceid, $amount_owing, $button_text) {
global $event;
//get our current path so we can send the user back here if they cancel
$path = isset($_GET['q']) ? $_GET['q'] : '<front>';
//paypal specific settings
@@ -653,7 +692,7 @@ function _booking_paypal_form_builder($node, &$form_state, $person, $invoiceid,
$form['#action'] = url(variable_get('booking_paypal_sandbox', 0) ? BOOKING_PAYPAL_SUBMIT_URL_SANDBOX : BOOKING_PAYPAL_SUBMIT_URL, array('absolute' => TRUE));
//turn the array into a form
foreach($vars as $name => $value) {
$form[$name] = array(
'#type' => 'hidden',