Files
booking/booking.manual_payment.inc

253 lines
9.2 KiB
PHP

<?php
// $Id: booking.manual_payment.inc,v 0.1
/**
* @file
* Helper functions relating to manual processing of payments
*/
function booking_manual_payment_admin($nid)
{
global $event;
$payment_type_options = array();
$payment_balance_options = array();
$form = array();
$options = array();
$early_price_applies = _booking_is_earlybird();
$payment_balance_options[0] = t('Total Payment (sets total amount paid to this value)');
$payment_balance_options[1] = t('Balance Payment (adds this value to the total amount paid)');
//collect the various payment options
$result = db_query("SELECT pid, booking_price_descrip, booking_price, booking_late_price FROM {booking_price} where booking_eventid = :eid " .
"and booking_price_active=1",
array(':eid' => $event->eid,));
//empty payment option
$payment_type_options[0] = '';
foreach($result as $row)
{
$price = $early_price_applies == true ? $row->booking_price : $row->booking_late_price;
$payment_type_options[$row->pid] = $row->booking_price_descrip . ' ($' . $price . ')';
}
//any html to put at the start of the form
$prefix = t("<h3>Create manual payment records</h3><p>Enter the amount below, select the people making the payment, then click the Update button at the bottom of the page</p>");
$form['booking_earlybird'] = array (
'#type' => 'hidden',
'#value' => $early_price_applies,
);
$form['payment-type'] = array(
'#type' => 'select',
'#title' => t('Payment Type'),
'#description' => t('Either select one of the built-in payment amounts, or enter a custom dollar amount in the next field.'),
'#required' => FALSE,
'#default_value' => '0',
'#options' => $payment_type_options,
);
$form['payment-custom-amount'] = array(
'#type' => 'textfield',
'#title' => t('Custom Amount Paid'),
'#maxlength' => 10,
'#field_prefix' => '$',
'#required' => FALSE,
'#default_value' => '0.00'
);
$form['payment-balance-type'] = array(
'#type' => 'radios',
'#title' => t('Balance or total payment?'),
'#description' => t('For balance payments, the amount entered above will be added to any payments the person has already made.<br />' .
'A total payment will replace the person\'s total payment with the value entered above.'),
'#options' => $payment_balance_options,
'#default_value' => 1,
'#required' => FALSE,
);
$header = array (
'booking_nid' => array('data' => t('Booking ID')),
'booking_name' => array('data' => t('Name')),
'booking_email' => array('data' => t('Email Address')),
'amount_paid' => array('data' => t('Amount Paid To Date')),
'amount_reqd' => array('data' => t('Gross Payment Required')),
'booking_status' => t('Status'),
'booking_fully_paid' => t('Fully Paid?'),
'welfare_required' => t('Welfare Required?'),
);
$result = db_query("SELECT * FROM {booking_person} WHERE booking_eventid = :eid",
array(':eid' => $event->eid));
foreach($result as $data)
{
$options[$data->nid] = array (
'booking_nid' => l(t('!id', array('!id' => $data->nid)), t('node/!id', array('!id' => $data->nid))),
'booking_name' => $data->booking_firstname . " " . $data->booking_lastname,
'booking_email' => $data->booking_email,
'amount_paid' => $data->booking_amount_paid,
'amount_reqd' => $data->booking_total_pay_reqd,
'booking_status' => _booking_status_generate($data->booking_status),
'booking_fully_paid' => $data->booking_amount_paid < $data->booking_total_pay_reqd ? 'No' : 'Yes',
'welfare_required' => $data->booking_welfare_required == 'Y' ? 'Yes' : 'No',
);
}
$form['table'] = array (
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
);
$form['submit'] = array (
'#type' => 'submit',
'#value' => t('Update'),
);
return array (
'first_para' => array (
'#type' => 'markup',
'#markup' => $prefix,
),
'form' => $form,
);
}
function booking_manual_payment_admin_submit($form, &$form_state) {
global $event;
$counter = 0;
$price = 0;
$fully_paid = 'N';
$payment_date = REQUEST_TIME;
$checkboxes = $form_state['values']['table']; //$values['booking_price_active'];
//watchdog('booking', 'Formstate when setting buttons: @info', array ('@info' => var_export($form_state['values'], TRUE)));
//watchdog('booking', 'Checkboxes when setting buttons: @info', array ('@info' => var_export($checkboxes, TRUE)));
//watchdog('booking', 'Manual payment form contents: @info', array('@info' => var_export($form_state['values'], TRUE)));
//check if there is a pre-defined payment type selected
if ((!empty($form_state['values']['payment-type'])) && $form_state['values']['payment-custom-amount'] == '0.00') {
//look up the price relating to the price id selected
$price_query = db_query("SELECT price.booking_price, price.booking_late_price, price.booking_price_descrip " .
"FROM {booking_price} price " .
"WHERE price.pid = :pid ",
array(':pid' => $form_state['values']['payment-type']))
->fetchObject();
$price = $form_state['values']['booking_earlybird'] == true ? $price_query->booking_price : $price_query->booking_late_price;
$description = $price_query->booking_price_descrip;
} elseif ($form_state['values']['payment-custom-amount'] != '0.00' && is_numeric($form_state['values']['payment-custom-amount'])) {
$price = $form_state['values']['payment-custom-amount'];
$description = "Custom Amount";
} else {
drupal_set_message("Error: Could not determine payment amount to update user(s).", 'error', FALSE);
return;
}
//loop through the list of attendees
foreach($checkboxes as $key => $value) {
//if an attendee's checkbox was ticked, then process their payment
if (is_numeric($key) && $value != 0) {
//check if they exist in the database first
/*
$person = db_query("SELECT person.nid, person.booking_firstname, person.booking_lastname, person.booking_status, " .
"person.booking_partner_id, person.booking_amount_paid " .
"FROM {booking_person} person " .
"WHERE nid = :nid",
array(':nid' => $key))
->fetchObject();
*/
$person = node_load($key);
if ($person) {
//check whether this is a balance or total payment
$is_balance = $form_state['values']['payment-balance-type'];
//balance payment
if ($is_balance == 1) {
//add this payment to their existing balance
$total_paid = $person->booking_amount_paid + $price;
//total payment
} elseif ($is_balance == 0) {
//set this as the persons new balance
$total_paid = $price;
//change $price so that we add the correct dollar amount in the payment record
//the actual amount newly paid is the price entered in the form, subtracting what they had previously paid
//use the function so it takes care of paypal transaction fees etc
$newprice = $price - _booking_amount_paid($key, $person);
watchdog('booking', 'Changing price for payment record from $!price to !newprice.', array('!price' => $price, '!newprice' => $newprice));
$price = $newprice;
}
//check if they have now fully paid
if ($total_paid >= _booking_total_due($person)) {
$fully_paid = 'Y';
}
//work out what their booking status is now
if ($person->booking_status == 1 || _booking_check_bookings_full() == FALSE) {
//either they were already booked in, or there is room on the booked-in list
$status = 1;
} else {
//there must be a waiting list, so put them on that
$status = 2;
}
/*
//determine what their booking status is now
if (_booking_check_bookings_full() == True || $person->booking_status == 2)
$status = 2;
else
$status = 1;
*/
watchdog('booking', 'New payment for regn id !nid of $!price and status is now !status',
array('!nid' => $key, '!price' => $price, '!status' => $status));
db_update('booking_person')
->fields(array(
'booking_amount_paid' => $total_paid,
'booking_status' => $status,
'booking_payment_complete' => $fully_paid,
))
->condition('nid', $key)
->execute();
//Create a payment record entry for this update
$result = db_insert('booking_payment')
->fields(array(
'booking_person_nid' => $key,
'booking_eventid' => $event->eid,
'booking_mc_gross' => $price,
'booking_mc_currency' => 'AUD',
'booking_mc_fee' => '0.00',
'booking_quantity' => 1,
'booking_invoice' => 'ManualPayment',
'booking_payer_id' => '',
'booking_payment_date' => $payment_date,
'booking_payment_status' => '',
'booking_first_name' => $person->booking_firstname,
'booking_last_name' => $person->booking_lastname,
'booking_buyer_email' => '',
'booking_payer_status' => '',
'booking_item_name' => $description,
'booking_ipn_track_id' => '',
))
->execute();
//send any emails if necessary
//at this point we don't know if it was a balance payment but send TRUE anyway
_booking_postpayment_trigger($key, $person, TRUE);
$counter++;
}
else
drupal_set_message("Error: Unable to find person with registration ID " . $key, 'error', FALSE);
}
}
drupal_set_message("Added manual payments for $counter people of " . '$' . "$price.", 'status', FALSE);
}