'. t("Search for customers based on any of the following fields. Use * as a wildcard to match any character. For example, searching by last name for 's*' will return all customers whose last name starts with an s. (Leave a field empty to ignore it in the search.)") .'
';
print $output;
exit();
}
function uc_order_add_product_form($order_id, $nid) {
$form['nid'] = array('#type' => 'hidden', '#value' => $nid);
$form['add-qty'] = array('#type' => 'textfield', '#title' => 'Qty', '#default_value' => '1', '#size' => 2, '#maxlength' => 5);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add to order'),
'#attributes' => array('onclick' => 'return add_product_to_order('. $order_id .', '. $nid .');')
);
$form['cancel'] = array(
'#type' => 'submit',
'#value' => t('Cancel'),
'#attributes' => array('onclick' => "\$('#add-product-button').click(); return false;"),
);
return $form;
}
/**
* Display an invoice in the browser, convert it to PDF, or e-mail it as HTML.
*/
function uc_order_invoice($order_id, $op = 'view') {
$order = uc_order_load($order_id);
if ($order === FALSE) {
drupal_set_message(t('Order @order_id does not exist.', array('@order_id' => $order_id)));
drupal_goto('admin/store/orders');
}
$output = uc_order_load_invoice($order, $op, variable_get('uc_cust_order_invoice_template', 'customer'));
if ($op == 'print') {
print $output;
exit();
}
return $output;
}
function uc_order_mail_invoice_form($order_id) {
$order = uc_order_load($order_id);
if ($order === FALSE) {
drupal_set_message(t('Order @order_id does not exist.', array('@order_id' => $order_id)));
drupal_goto('admin/store/orders');
}
$form['order_id'] = array(
'#type' => 'hidden',
'#value' => $order->order_id,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('Recipient e-mail address'),
'#default_value' => $order->primary_email,
);
$form['submit' ] = array(
'#type' => 'submit',
'#value' => t('Mail invoice'),
);
return $form;
}
function uc_order_mail_invoice_form_validate($form_id, $form_values) {
$recipient = check_plain($form_values['email']);
if (empty($recipient) || !valid_email_address($recipient)) {
form_set_error('email', t('Invalid e-mail address.'));
}
}
function uc_order_mail_invoice_form_submit($form_id, $form_values) {
$order = uc_order_load($form_values['order_id']);
if ($order === FALSE) {
drupal_set_message(t('Order @order_id does not exist.', array('@order_id' => $order_id)));
drupal_goto('admin/store/orders');
}
$output = uc_order_load_invoice($order, 'admin-mail', variable_get('uc_cust_order_invoice_template', 'customer'));
$recipient = check_plain($form_values['email']);
$sent = drupal_mail('invoice', $recipient, t('Your Order Invoice'), $output, uc_store_email_from(), array('Content-Type' => 'text/html; charset=UTF-8; format=flowed'));
if (!$sent) {
drupal_set_message(t('E-mail failed.'));
}
else {
$message = t('Invoice e-mailed to @email.', array('@email' => $recipient));
drupal_set_message($message);
uc_order_log_changes($order->order_id, array($message));
}
}
/**
* Display a log of changes made to an order.
*/
function uc_order_log($order_id) {
$order = uc_order_load($order_id);
if ($order === FALSE) {
drupal_set_message(t('Order @order_id does not exist.', array('@order_id' => $order_id)));
drupal_goto('admin/store/orders');
}
$result = db_query("SELECT * FROM {uc_order_log} WHERE order_id = %d", $order_id);
if (db_num_rows($result) == 0) {
$output = 'No changes have been logged for this order.';
}
else {
$header = array(t('Time'), t('User'), t('Changes'));
while ($change = db_fetch_object($result)) {
$user = uc_get_initials($change->uid);
$rows[] = array('data' => array(format_date($change->created, 'short'), $user == '-' ? $user : l($user, 'user/'. $change->uid), $change->changes), 'valign' => 'top');
}
$output = theme('table', $header, $rows);
}
return $output;
}
// Confirmation form to delete an order.
function uc_order_delete_confirm_form($order_id) {
$order = uc_order_load($order_id);
if ($order === FALSE) {
drupal_set_message(t('Order @order_id does not exist.', array('@order_id' => $order_id)));
drupal_goto('admin/store/orders');
}
if (!uc_order_can_delete($order)) {
drupal_set_message(t('It is not possible to delete order @id.', array('@id' => $order->order_id)));
drupal_goto('admin/store/orders');
}
$form['order_id'] = array(
'#type' => 'value',
'#value' => $order_id
);
return confirm_form($form, t('Are you sure you want to delete order @order_id?', array('@order_id' => $order_id)), 'admin/store/orders', NULL, t('Delete'));
}
function uc_order_delete_confirm_form_submit($form_id, $form_values) {
// Delete the specified order.
uc_order_delete($form_values['order_id']);
// Display a message to the user and return to the order admin page.
drupal_set_message(t('Order @order_id completely removed from the database.', array('@order_id' => $form_values['order_id'])));
return 'admin/store/orders';
}
function uc_order_address_book() {
$uid = intval($_POST['uid']);
$type = $_POST['type'];
$func = $_POST['func'];
print drupal_get_form('uc_order_address_book_form', $uid, $type, $func);
exit();
}
function uc_order_address_book_form($uid = 0, $type = 'billing', $func = '') {
$select = uc_select_address($uid, $type, $func);
if ($uid == 0) {
$form['desc'] = array('#value' => ' '. t('You must select a customer before address information is available. ') .' ');
}
elseif (is_null($select)) {
$form['desc'] = array('#value' => ' '. t('No addresses found for customer.') .' ');
}
else {
$form['addresses'] = uc_select_address($uid, $type, $func, t('Select an address'));
$form['addresses']['#prefix'] = '
';
$form['addresses']['#suffix'] = '
';
}
$form['close'] = array(
'#type' => 'button',
'#value' => t('Close'),
'#attributes' => array('onclick' => "return close_address_select('#". $type ."_address_select');"),
);
return $form;
}
function uc_order_select_customer($email = NULL) {
$options = NULL;
// Return the search results and let them pick one!
if (arg(4) == 'search') {
$first_name = strtolower(str_replace('*', '%', check_plain(arg(5))));
$last_name = strtolower(str_replace('*', '%', check_plain(arg(6))));
$email = strtolower(str_replace('*', '%', check_plain(arg(7))));
if ($first_name !== '0' && $first_name !== '%') {
$where .= " AND LOWER(o.billing_first_name) LIKE '". $first_name ."'";
}
if ($last_name !== '0' && $last_name !== '%') {
$where .= " AND LOWER(o.billing_last_name) LIKE '". $last_name ."'";
}
if ($email !== '0' && $email !== '%') {
$where .= " AND (LOWER(o.primary_email) LIKE '". $email ."' OR LOWER(u.mail) LIKE '"
. $email ."')";
}
$query = "SELECT DISTINCT u.uid, u.mail, o.billing_first_name, "
."o.billing_last_name FROM {users} AS u LEFT JOIN {uc_orders} "
."AS o ON u.uid = o.uid WHERE u.uid > 0 AND (o.order_status "
."IS NULL OR o.order_status IN ". uc_order_status_list('general', TRUE) .")". $where
." ORDER BY o.billing_last_name ASC";
$result = db_query($query);
$options = array();
while ($user = db_fetch_object($result)) {
if (empty($user->billing_first_name) && empty($user->billing_last_name)) {
$name = '';
}
else {
$name = $user->billing_last_name .', '. $user->billing_first_name .' ';
}
$options[$user->uid .':'. $user->mail] = $name .'('. $user->mail .')';
}
if (count($options) == 0) {
$output .= '
'. t('Search returned no results.') .'
';
$options = NULL;
}
else {
$output .= '
'. t('Search returned the following:') .'
';
}
}
// Check to see if the e-mail address for a new user is unique.
if (arg(5) == 'check') {
$email = check_plain(arg(6));
if (!valid_email_address($email)) {
$output .= t('Invalid e-mail address.') .' ';
}
$result = db_query("SELECT uid, mail FROM {users} WHERE mail = '%s'", $email);
if ($user = db_fetch_object($result)) {
$output .= t('An account already exists for that e-mail.') .'
'. t('Search for a customer based on these fields.')
.' '. t('Use * as a wildcard to match any character.') .' '
.'('. t('Leave a field empty to ignore it in the search.')
.')
'. t('Enter an e-mail address for the new customer.') .'
',
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#size' => 24,
'#maxlength' => 96,
);
}
if (is_null(arg(4))) {
$form['search'] = array(
'#type' => 'submit',
'#value' => t('Search'),
'#attributes' => array('onclick' => 'return load_customer_search_results();'),
);
}
elseif (arg(4) == 'search') {
if (!is_null($options)) {
$form['select'] = array(
'#type' => 'submit',
'#value' => t('Select'),
'#attributes' => array('onclick' => 'return select_customer_search();'),
);
}
$form['back'] = array(
'#type' => 'submit',
'#value' => t('Back'),
'#attributes' => array('onclick' => 'return load_customer_search();'),
);
}
elseif (arg(4) == 'new') {
$form['sendmail'] = array(
'#type' => 'checkbox',
'#title' => t('E-mail customer account details.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#attributes' => array('onclick' => 'return check_new_customer_address();'),
);
}
$form['close'] = array(
'#type' => 'submit',
'#value' => t('Close'),
'#attributes' => array('onclick' => 'return close_customer_select();'),
);
return $form;
}
/**
* Form to add a line item to an order.
*/
function uc_order_add_line_item_form($order_id, $line_item_id) {
$func = _line_item_data($line_item_id, 'callback');
if (!function_exists($func) || ($form = $func('form', $order_id)) == NULL) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Line Item Title'),
'#description' => t('Display title of the line item.'),
'#size' => 32,
'#maxlength' => 128,
'#default_value' => _line_item_data($line_item_id, 'title'),
);
$form['amount'] = array(
'#type' => 'textfield',
'#title' => t('Line Item Amount'),
'#description' => t('Amount of the line item without a currency sign.'),
'#size' => 6,
'#maxlength' => 13,
);
}
$form['order_id'] = array(
'#type' => 'hidden',
'#value' => $order_id,
);
$form['line_item_id'] = array(
'#type' => 'hidden',
'#value' => $line_item_id,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add line item'),
'#suffix' => l(t('Cancel'), 'admin/store/orders/'. $order_id .'/edit'),
);
return $form;
}
function uc_order_add_line_item_form_validate($form_id, $form_values) {
$func = _line_item_data($form_values['line_item_id'], 'callback');
if (function_exists($func) && ($form = $func('form', $form_values['order_id'])) != NULL) {
$func('validate', $form_values['order_id']);
}
else {
if (!is_numeric($form_values['amount'])) {
form_set_error('amount', t('Amount must be numeric.'));
}
}
}
function uc_order_add_line_item_form_submit($form_id, $form_values) {
$func = _line_item_data($form_values['line_item_id'], 'callback');
if (function_exists($func) && ($form = $func('form', $form_values['order_id'])) != NULL) {
$func('submit', $form_values['order_id']);
}
else {
uc_order_line_item_add($form_values['order_id'], $form_values['line_item_id'],
$form_values['title'], $form_values['amount']);
drupal_set_message(t('Line item added to order.'));
}
return 'admin/store/orders/'. $form_values['order_id'] .'/edit';
}
/*******************************************************************************
* Module and Helper Functions
******************************************************************************/
/**
* Generate a new order for user $uid.
*/
function uc_order_new($uid = 0, $state = 'in_checkout') {
$order = new stdClass();
if ($uid > 0) {
$user = user_load(array('uid' => $uid));
$email = $user->mail;
}
$order->order_id = db_next_id('{uc_orders}_order_id');
$order->uid = $uid;
$order->order_status = uc_order_state_default($state);
$order->primary_email = $email;
db_query("INSERT INTO {uc_orders} (order_id, uid, order_status, order_total, "
."primary_email, delivery_first_name, delivery_last_name, delivery_phone, "
."delivery_company, delivery_street1, delivery_street2, delivery_city, "
."delivery_zone, delivery_postal_code, delivery_country, billing_first_name, "
."billing_last_name, billing_phone, billing_company, billing_street1, "
."billing_street2, billing_city, billing_zone, billing_postal_code, "
."billing_country, payment_method, data, created, modified) VALUES "
."(%d, %d, '%s', 0, '%s', '', '', '', '', '', '', '', 0, '', 0, '', "
."'', '', '', '', '', '', 0, 0, 0, '', '', %d, %d)", $order->order_id,
$uid, $order->order_status, $email, time(), time());
module_invoke_all('order', 'new', $order, NULL);
return $order;
}
/**
* Save an order to the database.
*/
function uc_order_save($order) {
if (is_null($order->order_id) || intval($order->order_id) == 0) {
return FALSE;
}
db_query("UPDATE {uc_orders} SET host = '%s', uid = %d, order_status = '%s', order_total = %f, primary_email = '%s', "
."delivery_first_name = '%s', delivery_last_name = '%s', delivery_phone = '%s', "
."delivery_company = '%s', delivery_street1 = '%s', delivery_street2 = '%s', "
."delivery_city = '%s', delivery_zone = %d, delivery_postal_code = '%s', delivery_country = %d, "
."billing_first_name = '%s', billing_last_name = '%s', billing_phone = '%s', "
."billing_company = '%s', billing_street1 = '%s', billing_street2 = '%s', "
."billing_city = '%s', billing_zone = %d, billing_postal_code = '%s', billing_country = %d, "
."payment_method = '%s', data = '%s', modified = %d WHERE order_id = %d",
$_SERVER['REMOTE_ADDR'], $order->uid, $order->order_status, uc_order_get_total($order),
$order->primary_email, $order->delivery_first_name, $order->delivery_last_name, $order->delivery_phone,
$order->delivery_company, $order->delivery_street1, $order->delivery_street2,
$order->delivery_city, $order->delivery_zone, $order->delivery_postal_code,
((is_null($order->delivery_country) || $order->delivery_country == 0) ? variable_get('uc_store_country', 840) : $order->delivery_country),
$order->billing_first_name, $order->billing_last_name, $order->billing_phone,
$order->billing_company, $order->billing_street1, $order->billing_street2,
$order->billing_city, $order->billing_zone, $order->billing_postal_code,
((is_null($order->billing_country) || $order->billing_country == 0) ? variable_get('uc_store_country', 840) : $order->billing_country),
$order->payment_method, serialize($order->data), time(), $order->order_id);
// Review this query for removal in the future. -RS
db_query("DELETE FROM {uc_order_products} WHERE order_id = %d", $order->order_id);
if (is_array($order->products)) {
foreach ($order->products as $product) {
if (module_exists('uc_attribute') && empty($order->modified)) {
$attributes = array();
$options = _uc_cart_product_get_options($product);
foreach ($options as $aid => $option) {
$attributes[$option['attribute']] = $option['name'];
}
$product->data['attributes'] = $attributes;
}
uc_order_product_save($order->order_id, $product);
}
}
// Invoke hook_order() in enabled modules.
foreach (module_implements('order') as $module) {
$func = $module .'_order';
$null = NULL;
$func('save', $order, $null);
}
}
/**
* Function to save a product to an order.
*/
function uc_order_product_save($order_id, $product) {
if (!$product->order_product_id) {
$product->order_product_id = db_next_id('{uc_order_products}_order_product_id');
}
db_query("UPDATE {uc_order_products} SET order_id = %d, nid = %d, qty = %d, cost = %f, price = %f, title = '%s', manufacturer = '%s', model = '%s', weight = %f, data = '%s' WHERE order_product_id = %d",
$order_id, $product->nid, $product->qty, $product->cost, $product->price, $product->title, $product->manufacturer, $product->model, $product->weight, serialize($product->data), $product->order_product_id);
if (!db_affected_rows()) {
db_query("INSERT INTO {uc_order_products} (order_product_id, order_id, nid, qty, cost, price, title, manufacturer, model, weight, data) "
."VALUES (%d, %d, %d, %d, %f, %f, '%s', '%s', '%s', %f, '%s')", $product->order_product_id, $order_id, $product->nid, $product->qty,
$product->cost, $product->price, $product->title, $product->manufacturer, $product->model, $product->weight, serialize($product->data));
}
}
/**
* Load an order from the database.
*/
function uc_order_load($order_id) {
if (is_null($order_id) || $order_id < 1) {
return FALSE;
}
$result = db_query("SELECT * FROM {uc_orders} WHERE order_id = %d", $order_id);
if (db_num_rows($result) == 0) {
return FALSE;
}
$order = db_fetch_object($result);
$order->data = unserialize($order->data);
$result = db_query("SELECT * FROM {uc_order_products} WHERE order_id = %d ORDER BY order_product_id", $order_id);
$order->products = array();
while ($product = db_fetch_object($result)) {
$product->data = unserialize($product->data);
$order->products[] = $product;
}
// Invoke hook_order() in enabled modules.
foreach (module_implements('order') as $module) {
$func = $module .'_order';
$null = NULL;
$func('load', $order, $null);
}
// Load line items... has to be last after everything has been loaded.
$order->line_items = uc_order_load_line_items($order->order_id, TRUE);
usort($order->line_items, 'uc_weight_sort');
// Merge it with the defaultish line items.
$order->line_items = array_merge($order->line_items, uc_order_load_line_items($order, FALSE));
usort($order->line_items, 'uc_weight_sort');
// Make sure the total still matches up...
if (($total = uc_order_get_total($order)) !== $order->order_total) {
db_query("UPDATE {uc_orders} SET order_total = %f WHERE order_id = %d", $total, $order->order_id);
$order->order_total = $total;
}
return $order;
}
/**
* Deletes an order and tells other modules to do the same.
*
* @param $order_id
* The ID of the order you wish to delete.
*/
function uc_order_delete($order_id) {
global $user;
$order = uc_order_load($order_id);
// Perform the operations if we're deleting a valid order.
if ($order !== FALSE) {
// Invoke hook_order() in enabled modules.
foreach (module_implements('order') as $module) {
$func = $module .'_order';
$null = NULL;
$func('delete', $order, $null, NULL);
}
// Delete data from the appropriate Ubercart order tables.
db_query("DELETE FROM {uc_orders} WHERE order_id = %d", $order_id);
db_query("DELETE FROM {uc_order_products} WHERE order_id = %d", $order_id);
db_query("DELETE FROM {uc_order_comments} WHERE order_id = %d", $order_id);
db_query("DELETE FROM {uc_order_admin_comments} WHERE order_id = %d", $order_id);
db_query("DELETE FROM {uc_order_log} WHERE order_id = %d", $order_id);
// Delete line items for the order.
uc_order_delete_line_item($order_id, TRUE);
// Log the action in the database.
watchdog('uc_order', t('Order @order_id deleted by user @uid.', array('@order_id' => $order_id, '@uid' => $user->uid)));
}
}
/**
* Return an array of comments or admin comments for an order.
*/
function uc_order_comments_load($order_id, $admin = FALSE) {
if (!$admin) {
$join = " LEFT JOIN {uc_order_statuses} AS os ON oc.order_status = os.order_status_id";
}
$result = db_query("SELECT * FROM {". (($admin) ? 'uc_order_admin_comments' : 'uc_order_comments') ."} AS oc". $join ." WHERE oc.order_id = %d ORDER BY oc.created", $order_id);
while ($comment = db_fetch_object($result)) {
$comments[] = $comment;
}
return $comments;
}
/**
* Insert a comment, $type being either 'order' or 'admin'
*/
function uc_order_comment_save($order_id, $uid, $message, $type = 'admin', $status = 'pending', $notify = FALSE) {
if ($type == 'admin') {
db_query("INSERT INTO {uc_order_admin_comments} (order_id, uid, message, created) VALUES (%d, %d, '%s', %d)", $order_id, $uid, $message, time());
}
elseif ($type == 'order') {
db_query("INSERT INTO {uc_order_comments} (order_id, uid, message, order_status, notified, created) VALUES (%d, %d, '%s', '%s', %d, %d)", $order_id, $uid, $message, $status, $notify ? 1 : 0, time());
}
}
/**
* Return an array containing an order's line items ordered by weight.
* if ($stored) { $order should be an order ID. }
*/
function uc_order_load_line_items($order, $stored) {
$items = array();
if ($stored) {
if (is_object($order)) {
$order = $order->order_id;
}
$result = db_query("SELECT * FROM {uc_order_line_items} WHERE order_id = %d", $order);
while ($row = db_fetch_object($result)) {
$items[] = array(
'line_item_id' => $row->line_item_id,
'type' => $row->type,
'title' => $row->title,
'amount' => $row->amount,
'weight' => $row->weight,
'data' => unserialize($row->data),
);
}
}
elseif (!$stored && is_object($order)) {
$item_types = _line_item_list();
foreach ($item_types as $type) {
if ($type['stored'] == FALSE
&& (isset($type['callback']) && function_exists($type['callback']))
&& (!isset($type['display_only']) || $type['display_only'] == FALSE)) {
$result = $type['callback']('load', $order);
if ($result !== FALSE && is_array($result)) {
foreach ($result as $line) {
$items[] = array(
'line_item_id' => $line['id'],
'type' => $type['id'],
'title' => $line['title'],
'amount' => $line['amount'],
'weight' => isset($line['weight']) ? $line['weight'] : $type['weight'],
'data' => $line['data'],
);
}
}
}
}
}
usort($items, 'uc_weight_sort');
return $items;
}
/**
* Update an order's status as long as no one objects.
*
* @param $order_id
* The ID of the order to be updated.
* @param $status
* The new status ID we want to move the order to.
* @return
* TRUE or FALSE depending on the success of the update.
*/
function uc_order_update_status($order_id, $status) {
// Return FALSE if an invalid $status is specified.
if (uc_order_status_data($status, 'id') == NULL) {
return FALSE;
}
$order = uc_order_load($order_id);
// Attempt the update if the order exists.
if ($order !== FALSE) {
// Return false if any module says the update is not good to go.
$return = module_invoke_all('order', 'can_update', $order, $status);
for ($i = 0; $i < count($return); $i++) {
if ($return[$i] === FALSE) {
return FALSE;
}
}
// Otherwise perform the update and log the changes.
db_query("UPDATE {uc_orders} SET order_status = '%s', modified = %d WHERE order_id = %d", $status, time(), $order_id);
module_invoke_all('order', 'update', $order, $status);
$change = array(t('Order status') => array('old' => uc_order_status_data($order->order_status, 'title'), 'new' => uc_order_status_data($status, 'title')));
uc_order_log_changes($order->order_id, $change);
$updated = uc_order_load($order_id);
workflow_ng_invoke_event('order_status_update', $order, $updated);
return TRUE;
}
// Return FALSE if the order didn't exist.
return FALSE;
}
/**
* Log changes made to an order.
*
* @param $order_id
* The ID of the order that was changed.
* @param $changes
* An array of changes with the keys being the name of the field changed and
* the values being associative arrays with the keys 'old' and 'new' to
* represent the old and new values of the field.
* @return
* TRUE or FALSE depending on whether or not changes were logged.
*/
function uc_order_log_changes($order_id, $changes) {
global $user;
if (count($changes) == 0) {
return FALSE;
}
foreach ($changes as $key => $value) {
if (is_array($value)) {
$items[] = t('@key changed from %old to %new.', array('@key' => $key, '%old' => $value['old'], '%new' => $value['new']));
}
elseif (is_string($value)) {
$items[] = $value;
}
}
db_query("INSERT INTO {uc_order_log} (order_id, uid, changes, created) VALUES "
."(%d, %d, '%s', %d)", $order_id, $user->uid, theme('item_list', $items), time());
return TRUE;
}
/**
* Return an address from an order object.
*
* $type = delivery | billing
*/
function uc_order_address($order, $type) {
$name = $order->{$type .'_first_name'} .' '. $order->{$type .'_last_name'};
$address = uc_address_format(
$order->{$type .'_first_name'},
$order->{$type .'_last_name'},
$order->{$type .'_company'},
$order->{$type .'_street1'},
$order->{$type .'_street2'},
$order->{$type .'_city'},
$order->{$type .'_zone'},
$order->{$type .'_postal_code'},
$order->{$type .'_country'}
);
if (variable_get('uc_order_capitalize_addresses', TRUE)) {
$address = drupal_strtoupper($address);
}
return $address;
}
/**
* Return TRUE if an order exists.
*/
function uc_order_exists($order_id) {
if (intval($order_id) <= 0) {
return FALSE;
}
$result = db_query("SELECT order_id FROM {uc_orders} WHERE order_id = %d", $order_id);
if (db_num_rows($result) == 0) {
return FALSE;
}
return TRUE;
}
/**
* Calculate up an order's total!
*/
function uc_order_get_total($order, $products_only = FALSE) {
$total = 0;
if (is_array($order->products)) {
foreach ($order->products as $product) {
$total += $product->price * $product->qty;
}
}
if ($products_only) {
return $total;
}
$total += uc_line_items_calculate($order);
$result = module_invoke_all('order', 'total', $order, NULL);
foreach ($result as $key => $value) {
$total += $value;
}
return $total;
}
function uc_order_is_shippable($order) {
if (!is_array($order->products) || empty($order->products)) {
return FALSE;
}
foreach ($order->products as $product) {
// Return FALSE if the product form specifies this as not shippable.
if ($product->data['shippable'] == FALSE) {
$results[] = FALSE;
continue;
}
// See if any other modules have a say in the matter...
$result = module_invoke_all('cart_item', 'can_ship', $product);
// Return TRUE by default.
if (empty($result) || in_array(TRUE, $result)) {
$results[] = TRUE;
continue;
}
$results[] = FALSE;
}
return in_array(TRUE, $results);
}
function _get_order_screen_titles() {
$titles = array(
'view' => t('View'),
'edit' => t('Edit'),
'invoice' => t('Invoice'),
'customer' => t('Customer'),
);
return $titles;
}
function uc_order_load_invoice($order, $op = 'view', $template = 'customer') {
static $invoice;
if (isset($invoice[$order->order_id][$template])) {
return $invoice[$order->order_id][$template];
}
$file = drupal_get_path('module', 'uc_order') .'/templates/'. $template .'.itpl.php';
if (file_exists($file)) {
switch ($op) {
case 'checkout-mail':
$thank_you_message = TRUE;
case 'admin-mail':
$help_text = TRUE;
$email_text = TRUE;
$store_footer = TRUE;
case 'view':
case 'print':
$business_header = TRUE;
$thank_you_message = $thank_you_message === TRUE ? TRUE : FALSE;
$shipping_method = TRUE;
break;
}
$products = $order->products;
if (!is_array($products)) {
$products = array();
}
$line_items = $order->line_items;
$items = _line_item_list();
foreach ($items as $item) {
if (isset($item['display_only']) && $item['display_only'] == TRUE) {
$result = $item['callback']('display', $order);
if (is_array($result)) {
foreach ($result as $line) {
$line_items[] = array(
'line_item_id' => $line['id'],
'title' => $line['title'],
'amount' => $line['amount'],
'weight' => $item['weight'],
'data' => $line['data'],
);
}
}
}
}
if (!is_array($line_items)) {
$line_items = array();
}
usort($line_items, 'uc_weight_sort');
ob_start();
require($file);
$output = ob_get_contents();
ob_end_clean();
}
$output = token_replace($output, 'global');
$output = token_replace($output, 'order', $order);
$invoice[$order->order_id][$template] = $output;
return $output;
}
/**
* Return array of invoice template files found in ubercart/uc_order/templates.
*/
function uc_invoice_template_list() {
static $templates = array();
// If the template list hasn't already been loaded...
if (empty($templates)) {
$dir = drupal_get_path('module', 'uc_order') .'/templates/';
// Loop through all the files found in the directory.
foreach (file_scan_directory($dir, '.*\.itpl\.php', array('.', '..', 'CVS'), 0, FALSE) as $file) {
// Add them by name to the templates array, trimming off the extension.
$templates[] = substr($file->name, 0, strlen($file->name) - 5);
}
sort($templates);
}
return $templates;
}
// Returns a list of options for a template select box.
function uc_order_template_options($custom = FALSE) {
$templates = uc_invoice_template_list();
$templates = drupal_map_assoc(uc_invoice_template_list());
if ($custom) {
$templates[0] = t('Custom template');
}
return $templates;
}
/**
* Return a sorted list of the order states defined in the various modules.
*
* @param $scope
* Specify the scope for the order states you want listed - all, general, or
* specific. States with a general scope are used on general lists and pages.
* @param $sql
* Pass this parameter as TRUE to alter the return value for a SQL query.
* @return
* Either an array of state arrays or a string containing an array of state
* ids for use in a SQL query.
*/
function uc_order_state_list($scope = 'all', $sql = FALSE) {
$states = module_invoke_all('order_state');
foreach ($states as $i => $value) {
if ($scope != 'all' && $states[$i]['scope'] != $scope) {
unset($states[$i]);
}
}
usort($states, 'uc_weight_sort');
if ($sql) {
foreach ($states as $state) {
$ids[] = $state['id'];
}
return "('". implode("', '", $ids) ."')";
}
return $states;
}
/**
* Return a bit of data from a state array based on the state ID and array key.
*
* @param $state_id
* The ID of the order state you want to get data from.
* @param $key
* The key in the state array whose value you want: id, title, weight, scope.
* @return
* The value of the key you specify.
*/
function uc_order_state_data($state_id, $key) {
static $states;
if (empty($states)) {
$data = uc_order_state_list();
foreach ($data as $state) {
$states[$state['id']] = $state;
}
}
return $states[$state_id][$key];
}
/**
* Return the default order status for a particular order state.
*
* @param $state_id
* The ID of the order state whose default status you want to find.
* @return
* A string containing the default order status ID for the specified state.
*/
function uc_order_state_default($state_id) {
static $default;
// Return the default value if it exists.
if (isset($default[$state_id])) {
return $default[$state_id];
}
// Attempt to get the default state from the form.
$default[$state_id] = variable_get('uc_state_'. $state_id .'_default', NULL);
// If it is not found, pick the lightest status for this state.
if (empty($default[$state_id])) {
$statuses = uc_order_status_list($state_id);
$default[$state_id] = $statuses[0]['id'];
}
return $default[$state_id];
}
/**
* Return a sorted list of order statuses, sortable by order state/scope.
*
* @param $scope
* Specify the scope for the order statuses you want listed - all, general,
* specific, or any order state id. Defaults to all.
* @param $sql
* Pass this parameter as TRUE to alter the return value for a SQL query.
* @param @action
* Empty by default. Set to rebuild to load the order statuses from scratch,
* disregarding the current cached value for the specified $scope.
* @return
* Either an array of state arrays or a string containing an array of state
* ids for use in a SQL query.
*/
function uc_order_status_list($scope = 'all', $sql = FALSE, $action = '') {
static $statuses;
if (!isset($statuses[$scope]) || $action == 'rebuild') {
switch ($scope) {
case 'all':
$result = db_query("SELECT * FROM {uc_order_statuses}");
break;
case 'general':
case 'specific':
$result = db_query("SELECT * FROM {uc_order_statuses} WHERE state IN "
. uc_order_state_list($scope, TRUE));
break;
default:
$result = db_query("SELECT * FROM {uc_order_statuses} WHERE state = '%s'", $scope);
break;
}
$statuses[$scope] = array();
while ($status = db_fetch_array($result)) {
$status['id'] = $status['order_status_id'];
unset($status['order_status_id']);
$statuses[$scope][] = $status;
}
usort($statuses[$scope], 'uc_weight_sort');
}
if ($sql) {
foreach ($statuses[$scope] as $status) {
$ids[] = $status['id'];
}
return "('". implode("', '", $ids) ."')";
}
return $statuses[$scope];
}
/**
* Return a bit of data from a status array based on status ID and array key.
*
* @param $status_id
* The ID of the order status you want to get data from.
* @param $key
* The key in the status array whose value you want: id, title, state, weight.
* @return
* The value of the key you specify.
*/
function uc_order_status_data($status_id, $key) {
static $statuses;
if (empty($statuses)) {
$data = uc_order_status_list();
foreach ($data as $status) {
$statuses[$status['id']] = $status;
}
}
return $statuses[$status_id][$key];
}
/**
* Return the actions a user may perform on an order.
*
* @param $icon_html
* Specify whether or not to return the result as an HTML string with the
* order action icon links.
* @return
* Valid actions for an order; returned according to the $icon_html parameter.
*/
function uc_order_actions($order, $icon_html = FALSE) {
$state = uc_order_status_data($order->order_status, 'state');
$order_id = array('@order_id' => $order->order_id);
if (user_access('view all orders')) {
$alt = t('View order @order_id.', $order_id);
$actions[] = array(
'name' => t('View'),
'url' => 'admin/store/orders/'. $order->order_id,
'icon' => '',
'title' => $alt,
);
}
if (user_access('edit orders')) {
$alt = t('Edit order @order_id.', $order_id);
$actions[] = array(
'name' => t('Edit'),
'url' => 'admin/store/orders/'. $order->order_id .'/edit',
'icon' => '',
'title' => $alt,
);
}
if (user_access('delete orders')) {
$can_delete = TRUE;
$return = module_invoke_all('order', 'can_delete', $order, NULL);
if (!empty($return)) {
foreach ($return as $response) {
if ($response === FALSE) {
$can_delete = FALSE;
}
}
}
if (user_access('delete any order') || ($state != 'completed' && $can_delete)) {
$alt = t('Delete order @order_id.', $order_id);
$actions[] = array(
'name' => t('Delete'),
'url' => 'admin/store/orders/'. $order->order_id .'/delete',
'icon' => '',
'title' => $alt,
);
}
}
$extra = module_invoke_all('order_actions', $order);
if (count($extra)) {
$actions = array_merge($actions, $extra);
}
if ($icon_html) {
foreach ($actions as $action) {
$output .= l($action['icon'], $action['url'], array('title' => $action['title']), NULL, NULL, NULL, TRUE);
}
return $output;
}
else {
return $actions;
}
}
/**
* Return TRUE if an order can be deleted by the current user.
*/
function uc_order_can_delete($order) {
$can_delete = FALSE;
foreach (uc_order_actions($order) as $action) {
if ($action['name'] == t('Delete')) {
$can_delete = TRUE;
}
}
return $can_delete;
}