[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/includes/modules/order_total/ -> ot_loyalty_discount.php (source)

   1  <?php
   2  /*

   3  ot_loyalty_discount.php

   4  $Id: ot_loyalty_discount.php 14 2006-07-28 17:42:07Z user $

   5  

   6  

   7    osCMax Power E-Commerce

   8    http://oscdox.com

   9  

  10    Copyright 2006 osCMax2005 osCMax, 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        if ($od_amount>0) {
  37        $this->deduction = $od_amount;
  38        $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 . '%:',
  39                                'text' => '<b>' . $currencies->format($od_amount) . '</b>',
  40                                'value' => $od_amount);
  41      $order->info['total'] = $order->info['total'] - $od_amount;
  42      if ($this->sort_order < $ot_subtotal->sort_order) {
  43        $order->info['subtotal'] = $order->info['subtotal'] - $od_amount;
  44      }
  45  }
  46      }
  47        
  48     
  49    function calculate_credit($amount_order, $amount_cum_order) {
  50      global $order;
  51      $od_amount=0;
  52      $table_cost = split("[:,]" , MODULE_LOYALTY_DISCOUNT_TABLE);
  53      for ($i = 0; $i < count($table_cost); $i+=2) {
  54            if ($amount_cum_order >= $table_cost[$i]) {
  55              $od_pc = $table_cost[$i+1];
  56              $this->od_pc = $od_pc;
  57            }
  58          }
  59  // Calculate tax reduction if necessary

  60      if($this->calculate_tax == 'true') {
  61  // Calculate main tax reduction

  62        $tod_amount = round($order->info['tax']*10)/10*$od_pc/100;
  63        $order->info['tax'] = $order->info['tax'] - $tod_amount;
  64  // Calculate tax group deductions

  65        reset($order->info['tax_groups']);
  66        while (list($key, $value) = each($order->info['tax_groups'])) {
  67          $god_amount = round($value*10)/10*$od_pc/100;
  68          $order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;
  69        }  
  70      }
  71      $od_amount = round($amount_order*10)/10*$od_pc/100;
  72      $od_amount = $od_amount + $tod_amount;
  73      return $od_amount;
  74    }
  75  
  76  
  77    function get_order_total() {
  78      global  $order, $cart;
  79      $order_total = $order->info['total'];
  80  // Check if gift voucher is in cart and adjust total

  81      $products = $cart->get_products();
  82      for ($i=0; $i<sizeof($products); $i++) {
  83        $t_prid = tep_get_prid($products[$i]['id']);
  84        $gv_query = tep_db_query("select products_price, products_tax_class_id, products_model from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . "'");
  85        $gv_result = tep_db_fetch_array($gv_query);
  86        if (ereg('^GIFT', addslashes($gv_result['products_model']))) { 
  87          $qty = $cart->get_quantity($t_prid);
  88          $products_tax = tep_get_tax_rate($gv_result['products_tax_class_id']);
  89          if ($this->include_tax =='false') {
  90             $gv_amount = $gv_result['products_price'] * $qty;
  91          } else {
  92            $gv_amount = ($gv_result['products_price'] + tep_calculate_tax($gv_result['products_price'],$products_tax)) * $qty;
  93          }
  94          $order_total=$order_total - $gv_amount;
  95        }
  96      }
  97      if ($this->include_tax == 'false') $order_total=$order_total-$order->info['tax'];
  98      if ($this->include_shipping == 'false') $order_total=$order_total-$order->info['shipping_cost'];
  99      return $order_total;
 100    }   
 101      
 102  	function get_cum_order_total() {
 103        global $order, $customer_id;
 104      $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";  
 105          $history_query = tep_db_query($history_query_raw);
 106        if (tep_db_num_rows($history_query)) {
 107        $cum_order_total = 0;
 108        $cutoff_date = $this->get_cutoff_date();
 109        while ($history = tep_db_fetch_array($history_query)) {
 110          if ($this->get_date_in_period($cutoff_date, $history['date_purchased']) == true){
 111          $cum_order_total = $cum_order_total + $history['order_total'];
 112          }
 113        }
 114        $this->cum_order_total = $cum_order_total;
 115        return $cum_order_total;
 116        
 117        }    
 118        else {
 119        $cum_order_total = 0;
 120        $this->cum_order_total = $cum_order_total;
 121        return $cum_order_total;
 122        }    
 123      }
 124      
 125  	function get_cutoff_date() {
 126        $rightnow = time();
 127        switch ($this->cum_order_period) {
 128        case alltime:
 129        $this->period_string = MODULE_LOYALTY_DISCOUNT_WITHUS;
 130        $cutoff_date = 0;
 131        return $cutoff_date;
 132        break;
 133        case year:
 134        $this->period_string = MODULE_LOYALTY_DISCOUNT_LAST . MODULE_LOYALTY_DISCOUNT_YEAR;
 135        $cutoff_date = $rightnow - (60*60*24*365);
 136        return $cutoff_date;
 137        break;
 138        case quarter:
 139        $this->period_string = MODULE_LOYALTY_DISCOUNT_LAST . MODULE_LOYALTY_DISCOUNT_QUARTER;
 140        $cutoff_date = $rightnow - (60*60*24*92);
 141        return $cutoff_date;
 142        break;
 143        case month:
 144        $this->period_string = MODULE_LOYALTY_DISCOUNT_LAST . MODULE_LOYALTY_DISCOUNT_MONTH;
 145        $cutoff_date = $rightnow - (60*60*24*31);
 146        return $cutoff_date;
 147        break;
 148        default:
 149        $cutoff_date = $rightnow;
 150        return $cutoff_date;
 151        }
 152      }
 153      
 154        function get_date_in_period($cutoff_date, $raw_date) {
 155      if ( ($raw_date == '0000-00-00 00:00:00') || ($raw_date == '') ) return false;
 156      
 157      $year = (int)substr($raw_date, 0, 4);
 158      $month = (int)substr($raw_date, 5, 2);
 159      $day = (int)substr($raw_date, 8, 2);
 160      $hour = (int)substr($raw_date, 11, 2);
 161      $minute = (int)substr($raw_date, 14, 2);
 162      $second = (int)substr($raw_date, 17, 2);
 163  
 164      $order_date_purchased = mktime($hour,$minute,$second,$month,$day,$year);
 165      if ($order_date_purchased >= $cutoff_date) {return true;}
 166      else {return false;}
 167    }
 168    
 169    
 170      function check() {
 171        if (!isset($this->check)) {
 172          $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_LOYALTY_DISCOUNT_STATUS'");
 173          $this->check = tep_db_num_rows($check_query);
 174        }
 175  
 176        return $this->check;
 177      }
 178  
 179      function keys() {
 180        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');
 181      }
 182  
 183      function install() {
 184        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())");
 185        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())");
 186        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())");
 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 ('Include Tax', 'MODULE_LOYALTY_DISCOUNT_INC_TAX', 'true', 'Include Tax in calculation.', '6', '4','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, 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())");
 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 ('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())");
 190        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())");
 191        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())");
 192      }
 193  
 194      function remove() {
 195        $keys = '';
 196        $keys_array = $this->keys();
 197        for ($i=0; $i<sizeof($keys_array); $i++) {
 198          $keys .= "'" . $keys_array[$i] . "',";
 199        }
 200        $keys = substr($keys, 0, -1);
 201  
 202        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in (" . $keys . ")");
 203      }
 204    }
 205  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7