PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/classes/order_total.php

http://daocart.googlecode.com/
PHP | 233 lines | 162 code | 12 blank | 59 comment | 44 complexity | 1bbe4ae3c176aa7bc2d4043742d7e1d1 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * File contains the order-totals-processing class ("order-total")
  4. *
  5. * @package classes
  6. * @copyright Copyright 2003-2007 Zen Cart Development Team
  7. * @copyright Portions Copyright 2003 osCommerce
  8. * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
  9. * @version $Id: order_total.php 7543 2007-11-29 01:46:56Z drbyte $
  10. */
  11. /**
  12. * order-total class
  13. *
  14. * Handles all order-total processing functions
  15. *
  16. * @package classes
  17. */
  18. if (!defined('IS_ADMIN_FLAG')) {
  19. die('Illegal Access');
  20. }
  21. class order_total extends base {
  22. var $modules = array();
  23. // class constructor
  24. function order_total() {
  25. global $messageStack;
  26. if (defined('MODULE_ORDER_TOTAL_INSTALLED') && zen_not_null(MODULE_ORDER_TOTAL_INSTALLED)) {
  27. $module_list = explode(';', MODULE_ORDER_TOTAL_INSTALLED);
  28. reset($module_list);
  29. while (list(, $value) = each($module_list)) {
  30. //include(DIR_WS_LANGUAGES . $_SESSION['language'] . '/modules/order_total/' . $value);
  31. $lang_file = zen_get_file_directory(DIR_WS_LANGUAGES . $_SESSION['language'] . '/modules/order_total/', $value, 'false');
  32. if (@file_exists($lang_file)) {
  33. include_once($lang_file);
  34. } else {
  35. if (IS_ADMIN_FLAG === false) {
  36. $messageStack->add(WARNING_COULD_NOT_LOCATE_LANG_FILE . $lang_file, 'caution');
  37. } else {
  38. $messageStack->add_session(WARNING_COULD_NOT_LOCATE_LANG_FILE . $lang_file, 'caution');
  39. }
  40. }
  41. $module_file = DIR_WS_MODULES . 'order_total/' . $value;
  42. if (@file_exists($module_file)) {
  43. include_once($module_file);
  44. $class = substr($value, 0, strrpos($value, '.'));
  45. $GLOBALS[$class] = new $class;
  46. $this->modules[] = $value;
  47. }
  48. }
  49. }
  50. }
  51. function process() {
  52. global $order;
  53. $order_total_array = array();
  54. if (is_array($this->modules)) {
  55. reset($this->modules);
  56. while (list(, $value) = each($this->modules)) {
  57. $class = substr($value, 0, strrpos($value, '.'));
  58. if (!isset($GLOBALS[$class])) continue;
  59. $GLOBALS[$class]->process();
  60. for ($i=0, $n=sizeof($GLOBALS[$class]->output); $i<$n; $i++) {
  61. if (zen_not_null($GLOBALS[$class]->output[$i]['title']) && zen_not_null($GLOBALS[$class]->output[$i]['text'])) {
  62. $order_total_array[] = array('code' => $GLOBALS[$class]->code,
  63. 'title' => $GLOBALS[$class]->output[$i]['title'],
  64. 'text' => $GLOBALS[$class]->output[$i]['text'],
  65. 'value' => $GLOBALS[$class]->output[$i]['value'],
  66. 'sort_order' => $GLOBALS[$class]->sort_order);
  67. }
  68. }
  69. }
  70. }
  71. return $order_total_array;
  72. }
  73. function output($return_html=false) {
  74. global $template, $current_page_base;
  75. $output_string = '';
  76. if (is_array($this->modules)) {
  77. reset($this->modules);
  78. while (list(, $value) = each($this->modules)) {
  79. $class = substr($value, 0, strrpos($value, '.'));
  80. $size = sizeof($GLOBALS[$class]->output);
  81. // ideally, the first part of this IF statement should be dropped, and the ELSE portion is all that should be kept
  82. if ($return_html == true) {
  83. for ($i=0; $i<$size; $i++) {
  84. $output_string .= ' <tr>' . "\n" .
  85. ' <td align="right" class="' . str_replace('_', '-', $GLOBALS[$class]->code) . '-Text">' . $GLOBALS[$class]->output[$i]['title'] . '</td>' . "\n" .
  86. ' <td align="right" class="' . str_replace('_', '-', $GLOBALS[$class]->code) . '-Amount">' . $GLOBALS[$class]->output[$i]['text'] . '</td>' . "\n" .
  87. ' </tr>';
  88. }
  89. } else {
  90. // use a template file for output instead of hard-coded HTML
  91. require($template->get_template_dir('tpl_modules_order_totals.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_order_totals.php');
  92. }
  93. }
  94. }
  95. return $output_string;
  96. }
  97. //
  98. // This function is called in checkout payment after display of payment methods. It actually calls
  99. // two credit class functions.
  100. //
  101. // use_credit_amount() is normally a checkbox used to decide whether the credit amount should be applied to reduce
  102. // the order total. Whether this is a Gift Voucher, or discount coupon or reward points etc.
  103. //
  104. // The second function called is credit_selection(). This in the credit classes already made is usually a redeem box.
  105. // for entering a Gift Voucher number. Note credit classes can decide whether this part is displayed depending on
  106. // E.g. a setting in the admin section.
  107. //
  108. function credit_selection() {
  109. $selection_array = array();
  110. if (is_array($this->modules)) {
  111. reset($this->modules);
  112. while (list(, $value) = each($this->modules)) {
  113. $class = substr($value, 0, strrpos($value, '.'));
  114. if ($GLOBALS[$class]->credit_class ) {
  115. $selection = $GLOBALS[$class]->credit_selection();
  116. if (is_array($selection)) $selection_array[] = $selection;
  117. }
  118. }
  119. }
  120. return $selection_array;
  121. }
  122. // update_credit_account is called in checkout process on a per product basis. It's purpose
  123. // is to decide whether each product in the cart should add something to a credit account.
  124. // e.g. for the Gift Voucher it checks whether the product is a Gift voucher and then adds the amount
  125. // to the Gift Voucher account.
  126. // Another use would be to check if the product would give reward points and add these to the points/reward account.
  127. //
  128. function update_credit_account($i) {
  129. if (MODULE_ORDER_TOTAL_INSTALLED) {
  130. reset($this->modules);
  131. while (list(, $value) = each($this->modules)) {
  132. $class = substr($value, 0, strrpos($value, '.'));
  133. if ( $GLOBALS[$class]->credit_class ) {
  134. $GLOBALS[$class]->update_credit_account($i);
  135. }
  136. }
  137. }
  138. }
  139. // This function is called in checkout confirmation.
  140. // It's main use is for credit classes that use the credit_selection() method. This is usually for
  141. // entering redeem codes(Gift Vouchers/Discount Coupons). This function is used to validate these codes.
  142. // If they are valid then the necessary actions are taken, if not valid we are returned to checkout payment
  143. // with an error
  144. function collect_posts() {
  145. if (MODULE_ORDER_TOTAL_INSTALLED) {
  146. reset($this->modules);
  147. while (list(, $value) = each($this->modules)) {
  148. $class = substr($value, 0, strrpos($value, '.'));
  149. if ( $GLOBALS[$class]->credit_class ) {
  150. $post_var = 'c' . $GLOBALS[$class]->code;
  151. if ($_POST[$post_var]) $_SESSION[$post_var] = $_POST[$post_var];
  152. $GLOBALS[$class]->collect_posts();
  153. }
  154. }
  155. }
  156. }
  157. // pre_confirmation_check is called on checkout confirmation. It's function is to decide whether the
  158. // credits available are greater than the order total. If they are then a variable (credit_covers) is set to
  159. // true. This is used to bypass the payment method. In other words if the Gift Voucher is more than the order
  160. // total, we don't want to go to paypal etc.
  161. //
  162. function pre_confirmation_check() {
  163. global $order, $credit_covers;
  164. if (MODULE_ORDER_TOTAL_INSTALLED) {
  165. $total_deductions = 0;
  166. reset($this->modules);
  167. while (list(, $value) = each($this->modules)) {
  168. $class = substr($value, 0, strrpos($value, '.'));
  169. if ( $GLOBALS[$class]->credit_class ) {
  170. $order_total = $GLOBALS[$class]->get_order_total();
  171. if (is_array($order_total)) $order_total = $order_total['total'];
  172. $total_deductions = $total_deductions + $GLOBALS[$class]->pre_confirmation_check($order_total);
  173. }
  174. }
  175. $difference = $order->info['total'] - $total_deductions;
  176. if ( $difference <= 0.009 ) {
  177. $credit_covers = true;
  178. }
  179. }
  180. }
  181. // this function is called in checkout process. it tests whether a decision was made at checkout payment to use
  182. // the credit amount be applied aginst the order. If so some action is taken. E.g. for a Gift voucher the account
  183. // is reduced the order total amount.
  184. //
  185. function apply_credit() {
  186. if (MODULE_ORDER_TOTAL_INSTALLED) {
  187. reset($this->modules);
  188. while (list(, $value) = each($this->modules)) {
  189. $class = substr($value, 0, strrpos($value, '.'));
  190. if ( $GLOBALS[$class]->credit_class) {
  191. $GLOBALS[$class]->apply_credit();
  192. }
  193. }
  194. }
  195. }
  196. // Called in checkout process to clear session variables created by each credit class module.
  197. //
  198. function clear_posts() {
  199. global $_POST;
  200. if (MODULE_ORDER_TOTAL_INSTALLED) {
  201. reset($this->modules);
  202. while (list(, $value) = each($this->modules)) {
  203. $class = substr($value, 0, strrpos($value, '.'));
  204. if ( $GLOBALS[$class]->credit_class && method_exists($GLOBALS[$class], 'clear_posts')) {
  205. $GLOBALS[$class]->clear_posts();
  206. }
  207. }
  208. }
  209. }
  210. // Called at various times. This function calulates the total value of the order that the
  211. // credit will be applied against. This varies depending on whether the credit class applies
  212. // to shipping & tax
  213. //
  214. function get_order_total_main($class, $order_total) {
  215. global $credit, $order;
  216. // if ($GLOBALS[$class]->include_tax == 'false') $order_total=$order_total-$order->info['tax'];
  217. // if ($GLOBALS[$class]->include_shipping == 'false') $order_total=$order_total-$order->info['shipping_cost'];
  218. return $order_total;
  219. }
  220. }
  221. ?>