| 1 |
<?php
|
| 2 |
// $Id: uc_po.module,v 1.1 2008/05/07 19:35:39 rszrama Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Defines a purchase order payment method and the appropriate checkout and
|
| 7 |
* order panes.
|
| 8 |
*
|
| 9 |
* Maintained by Ryan (rszrama) of Commerce Guys - http://www.commerceguys.com
|
| 10 |
*/
|
| 11 |
|
| 12 |
|
| 13 |
/*******************************************************************************
|
| 14 |
* Hook Functions
|
| 15 |
******************************************************************************/
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Implementation of hook_menu().
|
| 19 |
*/
|
| 20 |
function uc_po_menu() {
|
| 21 |
$items = array();
|
| 22 |
|
| 23 |
$items['admin/store/orders/po'] = array(
|
| 24 |
'title' => 'Search by PO',
|
| 25 |
'page callback' => 'drupal_get_form',
|
| 26 |
'page arguments' => array('uc_po_search_form'),
|
| 27 |
'access arguments' => array('administer store'),
|
| 28 |
'description' => 'Search orders by PO number.',
|
| 29 |
'weight' => 5,
|
| 30 |
'type' => MENU_NORMAL_ITEM,
|
| 31 |
);
|
| 32 |
|
| 33 |
return $items;
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Implementation of hook_perm().
|
| 38 |
*/
|
| 39 |
function uc_po_perm() {
|
| 40 |
return array('pay by purchase order');
|
| 41 |
}
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Implementation of hook_token_values().
|
| 45 |
*/
|
| 46 |
function uc_po_token_values($type, $object = NULL) {
|
| 47 |
// Declare to the token module the value for the [po-number] token.
|
| 48 |
switch ($type) {
|
| 49 |
case 'order':
|
| 50 |
$values['po-number'] = $object->payment_details['po_number'];
|
| 51 |
break;
|
| 52 |
}
|
| 53 |
|
| 54 |
return $values;
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
* Implementation of hook_token_list().
|
| 59 |
*/
|
| 60 |
function uc_po_token_list($type = 'all') {
|
| 61 |
// When listing the available values for order tokens, include [po-number].
|
| 62 |
if ($type == 'uc_order') {
|
| 63 |
$tokens['uc_order']['po-number'] = t('The PO number for the order if one exists.');
|
| 64 |
}
|
| 65 |
|
| 66 |
return $tokens;
|
| 67 |
}
|
| 68 |
|
| 69 |
/*******************************************************************************
|
| 70 |
* Hook Functions (Ubercart)
|
| 71 |
******************************************************************************/
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Implementation of hook_uc_message().
|
| 75 |
*/
|
| 76 |
function uc_po_uc_message() {
|
| 77 |
$messages['po_instructions'] = t('Enter your purchase order number in the field below.');
|
| 78 |
|
| 79 |
return $messages;
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Implementation of hook_order().
|
| 84 |
*/
|
| 85 |
function uc_po_order($op, &$arg1, $arg2) {
|
| 86 |
switch ($op) {
|
| 87 |
// Save the PO number to the database.
|
| 88 |
case 'save':
|
| 89 |
if ($arg1->payment_method == 'po') {
|
| 90 |
// First attempt to update an existing PO number.
|
| 91 |
db_query("UPDATE {uc_payment_po} SET po_number = '%s' WHERE order_id = %d", $arg1->payment_details['po_number'], $arg1->order_id);
|
| 92 |
|
| 93 |
// Otherwise insert a new row.
|
| 94 |
if (!db_affected_rows()) {
|
| 95 |
db_query("INSERT INTO {uc_payment_po} (order_id, po_number) VALUES (%d, '%s')", $arg1->order_id, $arg1->payment_details['po_number']);
|
| 96 |
}
|
| 97 |
}
|
| 98 |
break;
|
| 99 |
|
| 100 |
// Load the PO number from the database.
|
| 101 |
case 'load':
|
| 102 |
$arg1->payment_details['po_number'] = db_result(db_query("SELECT po_number FROM {uc_payment_po} WHERE order_id = %d", $arg1->order_id));
|
| 103 |
break;
|
| 104 |
|
| 105 |
// Delete the PO number from the database.
|
| 106 |
case 'delete':
|
| 107 |
db_query("DELETE FROM {uc_payment_po} WHERE order_id = %d", $arg1->order_id);
|
| 108 |
break;
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
/**
|
| 113 |
* Implementation of hook_payment_method().
|
| 114 |
*/
|
| 115 |
function uc_po_payment_method() {
|
| 116 |
// The if statement checks to see if the user has permission to pay by
|
| 117 |
// purchase order or if the user is browsing admin pages before telling
|
| 118 |
// Ubercart that the purchase order payment method exists.
|
| 119 |
if (user_access('pay by purchase order') || arg(0) == 'admin') {
|
| 120 |
$methods[] = array(
|
| 121 |
'id' => 'po',
|
| 122 |
'name' => t('Purchase Order'),
|
| 123 |
'title' => t('Purchase Order'),
|
| 124 |
'desc' => t('Pay by purchase order.'),
|
| 125 |
'callback' => 'uc_payment_method_po',
|
| 126 |
'weight' => 4,
|
| 127 |
'checkout' => TRUE,
|
| 128 |
'backend' => TRUE,
|
| 129 |
);
|
| 130 |
}
|
| 131 |
|
| 132 |
return $methods;
|
| 133 |
}
|
| 134 |
|
| 135 |
/**
|
| 136 |
* Implementatio of hook_theme().
|
| 137 |
*/
|
| 138 |
function uc_po_theme($existing, $type, $theme, $path) {
|
| 139 |
return array(
|
| 140 |
'uc_payment_method_po_form' => array(
|
| 141 |
'arguments' => array('form' => NULL),
|
| 142 |
),
|
| 143 |
);
|
| 144 |
}
|
| 145 |
|
| 146 |
|
| 147 |
/*******************************************************************************
|
| 148 |
* Callback Functions, Forms, and Tables
|
| 149 |
******************************************************************************/
|
| 150 |
|
| 151 |
function uc_payment_method_po($op, &$arg1) {
|
| 152 |
switch ($op) {
|
| 153 |
// Displayed during checkout for the customer to enter a PO number.
|
| 154 |
case 'cart-details':
|
| 155 |
$details = drupal_get_form('uc_payment_method_po_form', $arg1);
|
| 156 |
return uc_strip_form($details);
|
| 157 |
|
| 158 |
// Handles receiving the data entered by the customer.
|
| 159 |
case 'cart-process':
|
| 160 |
if (empty($_POST['po_number'])) {
|
| 161 |
drupal_set_message(t('You must specify a PO number to pay by purchase order.'), 'error');
|
| 162 |
return FALSE;
|
| 163 |
}
|
| 164 |
$arg1->payment_details['po_number'] = $_POST['po_number'];
|
| 165 |
return TRUE;
|
| 166 |
|
| 167 |
// Display the PO number on the order review page prior to submission.
|
| 168 |
case 'cart-review':
|
| 169 |
return array(array('title' => t('PO number'), 'data' => check_plain($arg1->payment_details['po_number'])));
|
| 170 |
|
| 171 |
// Display the PO number in the payment order pane.
|
| 172 |
case 'customer-view':
|
| 173 |
case 'order-view':
|
| 174 |
return t('PO number: @po_number', array('@po_number' => $arg1->payment_details['po_number']));
|
| 175 |
|
| 176 |
// Displayed on the order edit screen so admin can update the PO number.
|
| 177 |
case 'order-details':
|
| 178 |
return uc_strip_form(drupal_get_form('uc_payment_method_po_form', $arg1));
|
| 179 |
|
| 180 |
// Update the PO number when the order form is submitted.
|
| 181 |
case 'edit-process':
|
| 182 |
$changes = array();
|
| 183 |
$changes['payment_details']['po_number'] = check_plain($_POST['po_number']);
|
| 184 |
return $changes;
|
| 185 |
|
| 186 |
// Add a few PO related fields to the payment methods settings form.
|
| 187 |
case 'settings':
|
| 188 |
$form['uc_po_permission'] = array(
|
| 189 |
'#value' => '<div>'. t('To checkout using the purchase order payment method, users must have the permission setting to pay by purchase order.')
|
| 190 |
.'<br />'. t('You can use account roles and permissions to give customers access to this payment method.') .'</div>',
|
| 191 |
);
|
| 192 |
$form['uc_po_instructions'] = array(
|
| 193 |
'#type' => 'textarea',
|
| 194 |
'#title' => t('Purchase order instructions'),
|
| 195 |
'#description' => t('Enter instructions to appear in the payment details at checkout.'),
|
| 196 |
'#default_value' => variable_get('uc_po_instructions', uc_get_message('po_instructions')),
|
| 197 |
);
|
| 198 |
return $form;
|
| 199 |
}
|
| 200 |
}
|
| 201 |
|
| 202 |
// Returns the form for users to enter or adjust a PO number.
|
| 203 |
function uc_payment_method_po_form($form_state, $order) {
|
| 204 |
$form['po_instructions'] = array(
|
| 205 |
'#value' => '<div class="payment-instructions">'
|
| 206 |
. variable_get('uc_po_instructions', uc_get_message('po_instructions'))
|
| 207 |
.'</div>',
|
| 208 |
);
|
| 209 |
$form['po_number'] = array(
|
| 210 |
'#type' => 'textfield',
|
| 211 |
'#title' => t('PO number'),
|
| 212 |
'#default_value' => $order->payment_details['po_number'],
|
| 213 |
'#size' => 32,
|
| 214 |
);
|
| 215 |
|
| 216 |
return $form;
|
| 217 |
}
|
| 218 |
|
| 219 |
// Themes the PO entry form to be in a table with the appropriate CSS rules for
|
| 220 |
// checkout and for the order edit screen.
|
| 221 |
function theme_uc_payment_method_po_form($form) {
|
| 222 |
$form['po_number']['#title'] = '';
|
| 223 |
$table = 'order-edit-table';
|
| 224 |
$label = 'oet-label';
|
| 225 |
|
| 226 |
if (arg(0) !== 'admin') {
|
| 227 |
$output = drupal_render($form['po_instructions']);
|
| 228 |
$table = 'inline-pane-table';
|
| 229 |
$label = 'field-label';
|
| 230 |
}
|
| 231 |
$output .= '<table class="'. $table .'"><tr><td class="'. $label .'">'
|
| 232 |
. t('PO number') .':</td><td>'. drupal_render($form['po_number'])
|
| 233 |
.'</td></tr></table>';
|
| 234 |
|
| 235 |
return $output;
|
| 236 |
}
|
| 237 |
|
| 238 |
// Allow administrators to search orders by PO number.
|
| 239 |
function uc_po_search_form($form_state, $po_number = '') {
|
| 240 |
// If a PO number was specified...
|
| 241 |
if (!empty($po_number)) {
|
| 242 |
$header = array(t('Order ID'), t('PO number'));
|
| 243 |
$rows = array();
|
| 244 |
|
| 245 |
// Query the database for any orders with a near-match of the PO number.
|
| 246 |
$result = db_query("SELECT order_id, po_number FROM {uc_payment_po} WHERE po_number LIKE '%%%s%%'", $po_number);
|
| 247 |
|
| 248 |
// Add a link to each order as a table row.
|
| 249 |
while ($order = db_fetch_object($result)) {
|
| 250 |
$rows[] = array(l($order->order_id, 'admin/store/orders/'. $order->order_id), $order->po_number);
|
| 251 |
}
|
| 252 |
|
| 253 |
// Alert the user if no orders were found.
|
| 254 |
if (empty($rows)) {
|
| 255 |
$rows[] = array(array('data' => t('No orders matched that PO number.'), 'colspan' => 2));
|
| 256 |
}
|
| 257 |
|
| 258 |
$form['results'] = array(
|
| 259 |
'#type' => 'fieldset',
|
| 260 |
'#title' => t('Search returned the following results'),
|
| 261 |
'#collapsible' => TRUE,
|
| 262 |
);
|
| 263 |
$form['results']['table'] = array(
|
| 264 |
'#value' => theme('table', $header, $rows),
|
| 265 |
);
|
| 266 |
}
|
| 267 |
|
| 268 |
$form['po_number'] = array(
|
| 269 |
'#type' => 'textfield',
|
| 270 |
'#title' => t('PO number'),
|
| 271 |
'#default_value' => $po_number,
|
| 272 |
'#size' => 32,
|
| 273 |
);
|
| 274 |
$form['submit'] = array(
|
| 275 |
'#type' => 'submit',
|
| 276 |
'#value' => t('Submit'),
|
| 277 |
);
|
| 278 |
|
| 279 |
return $form;
|
| 280 |
}
|
| 281 |
|
| 282 |
function uc_po_search_form_submit($form, &$form_state) {
|
| 283 |
$form_state['redirect'] = 'admin/store/orders/po/'. drupal_urlencode($form_state['values']['po_number']);
|
| 284 |
}
|
| 285 |
|