Guest User

Untitled

a guest
Mar 15th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.76 KB | None | 0 0
  1. <?php
  2. /*
  3. ot_loyalty_discount.php
  4. $Id: ot_loyalty_discount.php,v 1.0 2003/06/09 22:49:11 sp Exp $
  5.  
  6.  
  7. osCommerce, Open Source E-Commerce Solutions
  8. http://www.oscommerce.com
  9.  
  10. Copyright (c) 2002 osCommerce
  11.  
  12. Released under the GNU General Plic License
  13. */
  14.  
  15. class ot_loyalty_discount {
  16. var $title, $output;
  17.  
  18. function ot_loyalty_discount() {
  19. $this->code = ot_loyalty_discount;
  20. $this->title = MODULE_LOYALTY_DISCOUNT_TITLE;
  21. $this->description = MODULE_LOYALTY_DISCOUNT_DESCRIPTION;
  22. $this->enabled = MODULE_LOYALTY_DISCOUNT_STATUS;
  23. $this->sort_order = MODULE_LOYALTY_DISCOUNT_SORT_ORDER;
  24. $this->include_shipping = MODULE_LOYALTY_DISCOUNT_INC_SHIPPING;
  25. $this->include_tax = MODULE_LOYALTY_DISCOUNT_INC_TAX;
  26. $this->calculate_tax = MODULE_LOYALTY_DISCOUNT_CALC_TAX;
  27. $this->table = MODULE_LOYALTY_DISCOUNT_TABLE;
  28. $this->loyalty_order_status = MODULE_LOYALTY_DISCOUNT_ORDER_STATUS;
  29. $this->cum_order_period = MODULE_LOYALTY_DISCOUNT_CUMORDER_PERIOD;
  30. $this->output = array();
  31. }
  32.  
  33. function process() {
  34. global $order, $ot_subtotal, $currencies;
  35. $od_amount = $this->calculate_credit($this->get_order_total(), $this->get_cum_order_total());
  36.  
  37. // round discount to nearest cent. Discount of less than .5 cent will not be deducted from amount payable.
  38. $od_amount = round($od_amount, 2);
  39. if ($od_amount>0) { // deduct discount from amount payable
  40. $this->deduction = $od_amount;
  41. $this->output[] = array('title' => $this->title . ':<br>' . MODULE_LOYALTY_DISCOUNT_SPENT . $currencies->format($this->cum_order_total) . $this->period_string . MODULE_LOYALTY_DISCOUNT_QUALIFY . $this->od_pc . '%:',
  42. 'text' => '<b>' . $currencies->format($od_amount) .'<b>' ,
  43. 'value' => $od_amount);
  44. $order->info['total'] = $order->info['total'] - $od_amount;
  45. if ($this->sort_order < $ot_subtotal->sort_order) {
  46. $order->info['subtotal'] = $order->info['subtotal'] - $od_amount;
  47. }
  48. }
  49. } // end of function process()
  50.  
  51.  
  52. function calculate_credit($amount_order, $amount_cum_order) {
  53. global $order;
  54. $od_amount=0;
  55. $table_cost = split("[:,]" , MODULE_LOYALTY_DISCOUNT_TABLE);
  56. for ($i = 0; $i < count($table_cost); $i+=2) {
  57. if ($amount_cum_order >= $table_cost[$i]) {
  58. $od_pc = $table_cost[$i+1];
  59. $this->od_pc = $od_pc;
  60. }
  61. }
  62. // Calculate tax reduction if necessary
  63. if($this->calculate_tax == 'true') {
  64. // Calculate main tax reduction
  65. $tod_amount = $order->info['tax']*$od_pc/100;
  66. $order->info['tax'] = $order->info['tax'] - $tod_amount;
  67. // Calculate tax group deductions
  68. reset($order->info['tax_groups']);
  69. while (list($key, $value) = each($order->info['tax_groups'])) {
  70. $god_amount = $value*$od_pc/100;
  71. $order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;
  72. }
  73. }
  74. $od_amount = $amount_order*$od_pc/100;
  75. $od_amount = $od_amount + $tod_amount;
  76. return $od_amount;
  77. }
  78.  
  79.  
  80. function get_order_total() {
  81. global $order, $cart;
  82. $order_total = $order->info['total'];
  83. // Check if gift voucher is in cart and adjust total
  84. $products = $cart->get_products();
  85. for ($i=0; $i<sizeof($products); $i++) {
  86. $t_prid = tep_get_prid($products[$i]['id']);
  87. $gv_query = tep_db_query("select products_price, products_tax_class_id, products_model from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . "'");
  88. $gv_result = tep_db_fetch_array($gv_query);
  89. if (ereg('^GIFT', addslashes($gv_result['products_model']))) {
  90. $qty = $cart->get_quantity($t_prid);
  91. $products_tax = tep_get_tax_rate($gv_result['products_tax_class_id']);
  92. if ($this->include_tax =='false') {
  93. $gv_amount = $gv_result['products_price'] * $qty;
  94. } else {
  95. $gv_amount = ($gv_result['products_price'] + tep_calculate_tax($gv_result['products_price'],$products_tax)) * $qty;
  96. }
  97. $order_total=$order_total - $gv_amount;
  98. }
  99. }
  100. if ($this->include_tax == 'false') $order_total=$order_total-$order->info['tax'];
  101. if ($this->include_shipping == 'false') $order_total=$order_total-$order->info['shipping_cost'];
  102. return $order_total;
  103. }
  104.  
  105. function get_cum_order_total() {
  106. global $order, $customer_id;
  107. $history_query_raw = "select o.date_purchased, ot.value as order_total from " . TABLE_ORDERS . " o left join " . TABLE_ORDERS_TOTAL . " ot on (o.orders_id = ot.orders_id) where o.customers_id = '" . $customer_id . "' and ot.class = 'ot_subtotal' and o.orders_status = '" . $this->loyalty_order_status . "' order by date_purchased DESC";
  108. $history_query = tep_db_query($history_query_raw);
  109. if (tep_db_num_rows($history_query)) {
  110. $cum_order_total = 0;
  111. $cutoff_date = $this->get_cutoff_date();
  112. while ($history = tep_db_fetch_array($history_query)) {
  113. if ($this->get_date_in_period($cutoff_date, $history['date_purchased']) == true){
  114. $cum_order_total = $cum_order_total + $history['order_total'];
  115. }
  116. }
  117. $this->cum_order_total = $cum_order_total;
  118. return $cum_order_total;
  119.  
  120. }
  121. else {
  122. $cum_order_total = 0;
  123. $this->cum_order_total = $cum_order_total;
  124. return $cum_order_total;
  125. }
  126. }
  127.  
  128. function get_cutoff_date() {
  129. $rightnow = time();
  130. switch ($this->cum_order_period) {
  131. case alltime:
  132. $this->period_string = MODULE_LOYALTY_DISCOUNT_WITHUS;
  133. $cutoff_date = 0;
  134. return $cutoff_date;
  135. break;
  136. case year:
  137. $this->period_string = MODULE_LOYALTY_DISCOUNT_LAST . MODULE_LOYALTY_DISCOUNT_YEAR;
  138. $cutoff_date = $rightnow - (60*60*24*365);
  139. return $cutoff_date;
  140. break;
  141. case quarter:
  142. $this->period_string = MODULE_LOYALTY_DISCOUNT_LAST . MODULE_LOYALTY_DISCOUNT_QUARTER;
  143. $cutoff_date = $rightnow - (60*60*24*92);
  144. return $cutoff_date;
  145. break;
  146. case month:
  147. $this->period_string = MODULE_LOYALTY_DISCOUNT_LAST . MODULE_LOYALTY_DISCOUNT_MONTH;
  148. $cutoff_date = $rightnow - (60*60*24*31);
  149. return $cutoff_date;
  150. break;
  151. default:
  152. $cutoff_date = $rightnow;
  153. return $cutoff_date;
  154. }
  155. }
  156.  
  157. function get_date_in_period($cutoff_date, $raw_date) {
  158. if ( ($raw_date == '0000-00-00 00:00:00') || ($raw_date == '') ) return false;
  159.  
  160. $year = (int)substr($raw_date, 0, 4);
  161. $month = (int)substr($raw_date, 5, 2);
  162. $day = (int)substr($raw_date, 8, 2);
  163. $hour = (int)substr($raw_date, 11, 2);
  164. $minute = (int)substr($raw_date, 14, 2);
  165. $second = (int)substr($raw_date, 17, 2);
  166.  
  167. $order_date_purchased = mktime($hour,$minute,$second,$month,$day,$year);
  168. if ($order_date_purchased >= $cutoff_date) {return true;}
  169. else {return false;}
  170. }
  171.  
  172.  
  173. function check() {
  174. if (!isset($this->check)) {
  175. $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_LOYALTY_DISCOUNT_STATUS'");
  176. $this->check = tep_db_num_rows($check_query);
  177. }
  178.  
  179. return $this->check;
  180. }
  181.  
  182. function keys() {
  183. return array('MODULE_LOYALTY_DISCOUNT_STATUS', 'MODULE_LOYALTY_DISCOUNT_SORT_ORDER', 'MODULE_LOYALTY_DISCOUNT_CUMORDER_PERIOD', 'MODULE_LOYALTY_DISCOUNT_TABLE', 'MODULE_LOYALTY_DISCOUNT_INC_SHIPPING', 'MODULE_LOYALTY_DISCOUNT_INC_TAX', 'MODULE_LOYALTY_DISCOUNT_CALC_TAX', 'MODULE_LOYALTY_DISCOUNT_ORDER_STATUS');
  184. }
  185.  
  186. function install() {
  187. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Display Total', 'MODULE_LOYALTY_DISCOUNT_STATUS', 'true', 'Do you want to enable the Order Discount?', '6', '1','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
  188. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_LOYALTY_DISCOUNT_SORT_ORDER', '999', 'Sort order of display.', '6', '2', now())");
  189. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Include Shipping', 'MODULE_LOYALTY_DISCOUNT_INC_SHIPPING', 'true', 'Include Shipping in calculation', '6', '3', 'tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
  190. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Include Tax', 'MODULE_LOYALTY_DISCOUNT_INC_TAX', 'true', 'Include Tax in calculation.', '6', '4','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
  191. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Calculate Tax', 'MODULE_LOYALTY_DISCOUNT_CALC_TAX', 'false', 'Re-calculate Tax on discounted amount.', '6', '5','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
  192. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Cumulative order total period', 'MODULE_LOYALTY_DISCOUNT_CUMORDER_PERIOD', 'year', 'Set the period over which to calculate cumulative order total.', '6', '6','tep_cfg_select_option(array(\'alltime\', \'year\', \'quarter\', \'month\'), ', now())");
  193. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Discount Percentage', 'MODULE_LOYALTY_DISCOUNT_TABLE', '1000:5,1500:7.5,2000:10,3000:12.5,5000:15', 'Set the cumulative order total breaks per period set above, and discount percentages', '6', '7', now())");
  194. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Order Status', 'MODULE_LOYALTY_DISCOUNT_ORDER_STATUS', '3', 'Set the minimum order status for an order to add it to the total amount ordered', '6', '8', now())");
  195. }
  196.  
  197. function remove() {
  198. $keys = '';
  199. $keys_array = $this->keys();
  200. for ($i=0; $i<sizeof($keys_array); $i++) {
  201. $keys .= "'" . $keys_array[$i] . "',";
  202. }
  203. $keys = substr($keys, 0, -1);
  204.  
  205. tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in (" . $keys . ")");
  206. }
  207. }
  208. ?>
Add Comment
Please, Sign In to add comment