| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This file contains the callbacks for the default line items for orders and
|
| 7 |
* the various functions that make line items work.
|
| 8 |
*
|
| 9 |
* Line items are defined using hook_line_item() and use a callback to handle
|
| 10 |
* the different processes involved in line item viewing/editing/calculating.
|
| 11 |
* The default line items are defined in uc_order_line_item() in uc_order.module.
|
| 12 |
*/
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Handle the subtotal line item.
|
| 16 |
*/
|
| 17 |
function uc_line_item_subtotal($op, $arg1) {
|
| 18 |
switch ($op) {
|
| 19 |
case 'load':
|
| 20 |
$lines[] = array(
|
| 21 |
'id' => 'subtotal',
|
| 22 |
'title' => t('Subtotal'),
|
| 23 |
'amount' => uc_order_get_total($arg1, TRUE),
|
| 24 |
);
|
| 25 |
return $lines;
|
| 26 |
case 'cart-preview':
|
| 27 |
$subtotal = 0;
|
| 28 |
foreach ($arg1 as $item) {
|
| 29 |
$total = ($item->qty) ? $item->qty * $item->price : $item->price;
|
| 30 |
$subtotal += $total;
|
| 31 |
}
|
| 32 |
if (module_exists('uc_payment') && variable_get('uc_pane_payment_enabled', TRUE)) {
|
| 33 |
uc_add_js("\$(document).ready( function() { set_line_item('subtotal', '". t('Subtotal') ."', ". $subtotal .", -10); } );", 'inline');
|
| 34 |
}
|
| 35 |
break;
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
| 40 |
* Handle the total line item.
|
| 41 |
*/
|
| 42 |
function uc_line_item_total($op, $arg1) {
|
| 43 |
switch ($op) {
|
| 44 |
case 'display':
|
| 45 |
$lines[] = array(
|
| 46 |
'id' => 'total',
|
| 47 |
'title' => t('Total'),
|
| 48 |
'amount' => uc_order_get_total($arg1),
|
| 49 |
);
|
| 50 |
return $lines;
|
| 51 |
}
|
| 52 |
}
|
| 53 |
|
| 54 |
/**
|
| 55 |
* Calculate the total value of line items of types that should be calculated.
|
| 56 |
*/
|
| 57 |
function uc_line_items_calculate($order) {
|
| 58 |
$total = 0;
|
| 59 |
|
| 60 |
if (is_array($order->line_items)) {
|
| 61 |
foreach ($order->line_items as $item) {
|
| 62 |
if (_line_item_data($item['type'], 'calculated') == TRUE) {
|
| 63 |
$total += $item['amount'];
|
| 64 |
}
|
| 65 |
}
|
| 66 |
}
|
| 67 |
|
| 68 |
return $total;
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Update a line item.
|
| 73 |
*/
|
| 74 |
function uc_order_update_line_item($id, $title, $amount) {
|
| 75 |
db_query("UPDATE {uc_order_line_items} SET title = '%s', amount = %f "
|
| 76 |
."WHERE line_item_id = %d", $title, $amount, $id);
|
| 77 |
return TRUE;
|
| 78 |
}
|
| 79 |
|
| 80 |
/**
|
| 81 |
* Delete a line item, or pass $order as TRUE and $id as an order_id to delete
|
| 82 |
* every line item attached to an order.
|
| 83 |
*/
|
| 84 |
function uc_order_delete_line_item($id, $order = FALSE) {
|
| 85 |
if ($order === FALSE) {
|
| 86 |
db_query("DELETE FROM {uc_order_line_items} WHERE line_item_id = %d", $id);
|
| 87 |
}
|
| 88 |
else {
|
| 89 |
db_query("DELETE FROM {uc_order_line_items} WHERE order_id = %d", $id);
|
| 90 |
}
|
| 91 |
return TRUE;
|
| 92 |
}
|
| 93 |
|
| 94 |
function uc_order_line_item_add($order_id, $type, $title, $amount, $weight = NULL) {
|
| 95 |
if (is_null($weight)) {
|
| 96 |
$weight = _line_item_data($type, 'weight');
|
| 97 |
}
|
| 98 |
|
| 99 |
db_query("INSERT INTO {uc_order_line_items} (order_id, type, title, amount, weight) VALUES (%d, '%s', '%s', %f, %d)", $order_id, $type, $title, $amount, $weight);
|
| 100 |
|
| 101 |
return TRUE;
|
| 102 |
}
|
| 103 |
|
| 104 |
/**
|
| 105 |
* Build a list of line items defined in the enabled modules.
|
| 106 |
*/
|
| 107 |
function _line_item_list($action = NULL) {
|
| 108 |
static $items;
|
| 109 |
|
| 110 |
if (count($items) > 0 && $action !== 'rebuild') {
|
| 111 |
return $items;
|
| 112 |
}
|
| 113 |
|
| 114 |
$items = module_invoke_all('line_item', NULL);
|
| 115 |
foreach ($items as $i => $value) {
|
| 116 |
$items[$i]['enabled'] = variable_get('uc_li_'. $items[$i]['id'] .'_enabled', (!isset($items[$i]['enabled']) ? TRUE : $items[$i]['enabled']));
|
| 117 |
$items[$i]['weight'] = variable_get('uc_li_'. $items[$i]['id'] .'_weight', (!isset($items[$i]['weight']) ? 1 : $items[$i]['weight']));
|
| 118 |
}
|
| 119 |
usort($items, 'uc_weight_sort');
|
| 120 |
|
| 121 |
return $items;
|
| 122 |
}
|
| 123 |
|
| 124 |
/**
|
| 125 |
* Return data from a line item by ID and the array key.
|
| 126 |
*/
|
| 127 |
function _line_item_data($item_id, $key) {
|
| 128 |
$items = _line_item_list();
|
| 129 |
foreach ($items as $item) {
|
| 130 |
if ($item['id'] == $item_id) {
|
| 131 |
return $item[$key];
|
| 132 |
}
|
| 133 |
}
|
| 134 |
}
|
| 135 |
|