[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

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

   1  <?php
   2  /*

   3  $Id: ot_gv.php 3 2006-05-27 04:59:07Z user $

   4  

   5    osCMax Power E-Commerce

   6    http://oscdox.com

   7  

   8    Copyright 2006 osCMax2005 osCMax, 2002 osCommerce

   9  

  10    Released under the GNU General Public License

  11  */
  12  
  13    class ot_gv {
  14      var $title, $output;
  15  
  16      function ot_gv() {
  17        $this->code = 'ot_gv';
  18        $this->title = MODULE_ORDER_TOTAL_GV_TITLE;
  19        $this->header = MODULE_ORDER_TOTAL_GV_HEADER;
  20        $this->description = MODULE_ORDER_TOTAL_GV_DESCRIPTION;
  21        $this->user_prompt = MODULE_ORDER_TOTAL_GV_USER_PROMPT;
  22        $this->enabled = MODULE_ORDER_TOTAL_GV_STATUS;
  23        $this->sort_order = MODULE_ORDER_TOTAL_GV_SORT_ORDER;
  24        $this->include_shipping = MODULE_ORDER_TOTAL_GV_INC_SHIPPING;
  25        $this->include_tax = MODULE_ORDER_TOTAL_GV_INC_TAX;
  26        $this->calculate_tax = MODULE_ORDER_TOTAL_GV_CALC_TAX;
  27        $this->credit_tax = MODULE_ORDER_TOTAL_GV_CREDIT_TAX;
  28        $this->tax_class  = MODULE_ORDER_TOTAL_GV_TAX_CLASS;
  29        $this->show_redeem_box = MODULE_ORDER_TOTAL_GV_REDEEM_BOX;
  30        $this->credit_class = true;
  31        $this->checkbox = $this->user_prompt . '<input type="checkbox" onClick="submitFunction()" name="' . 'c' . $this->code . '">';
  32        $this->output = array();
  33      }
  34  
  35      function process() {
  36        global $order, $currencies, $cot_gv;
  37  //      if ($_SESSION['cot_gv']) {  // old code Strider

  38         if (tep_session_is_registered('cot_gv')) {
  39          $order_total = $this->get_order_total();
  40          $od_amount = $this->calculate_credit($order_total);
  41          if ($this->calculate_tax != "None") {
  42            $tod_amount = $this->calculate_tax_deduction($order_total, $od_amount, $this->calculate_tax);
  43            $od_amount = $this->calculate_credit($order_total);
  44          }
  45          $this->deduction = $od_amount;
  46  //        if (($this->calculate_tax == "Credit Note") && (DISPLAY_PRICE_WITH_TAX != 'true')) {

  47  //          $od_amount -= $tod_amount;

  48  //          $order->info['total'] -= $tod_amount;

  49  //        }

  50          $order->info['total'] = $order->info['total'] - $od_amount;
  51          if ($od_amount > 0) {
  52            $this->output[] = array('title' => $this->title . ':',
  53                             'text' => '<b>' . $currencies->format($od_amount) . '</b>',
  54                             'value' => $od_amount);
  55          }
  56        } 
  57      }
  58  
  59      function selection_test() {
  60        global $customer_id;
  61        if ($this->user_has_gv_account($customer_id)) {
  62          return true;
  63        } else {
  64          return false;
  65        }
  66      }
  67  
  68      function pre_confirmation_check($order_total) {
  69          global $cot_gv, $order;
  70  //        if ($_SESSION['cot_gv']) {  // old code Strider

  71              $od_amount = 0; // set the default amount we will send back

  72            if (tep_session_is_registered('cot_gv')) {
  73  // pre confirmation check doesn't do a true order process. It just attempts to see if

  74  // there is enough to handle the order. But depending on settings it will not be shown

  75  // all of the order so this is why we do this runaround jane. What do we know so far.

  76  // nothing. Since we need to know if we process the full amount we need to call get order total

  77  // if there has been something before us then

  78  
  79            if ($this->include_tax == 'false') {
  80                      $order_total = $order_total - $order->info['tax'];
  81                  }
  82            if ($this->include_shipping == 'false') {
  83                      $order_total = $order_total - $order->info['shipping_cost'];
  84                  }
  85                  $od_amount = $this->calculate_credit($order_total);
  86  
  87  
  88                  if ($this->calculate_tax != "None") {
  89                      $tod_amount = $this->calculate_tax_deduction($order_total, $od_amount, $this->calculate_tax);
  90                      $od_amount = $this->calculate_credit($order_total)+$tod_amount;
  91                  }
  92              }
  93          return $od_amount;
  94      }
  95      // original code

  96      /*function pre_confirmation_check($order_total) {

  97        if ($SESSION['cot_gv']) {

  98          $gv_payment_amount = $this->calculate_credit($order_total);

  99        }

 100        return $gv_payment_amount;

 101      } */
 102  
 103      function use_credit_amount() {
 104          global $cot_gv;
 105  //      $_SESSION['cot_gv'] = false;     // old code - Strider

 106        $cot_gv = false;
 107        if ($this->selection_test()) {
 108          $output_string .=  '    <td align="right" class="main">';
 109          $output_string .= '<b>' . $this->checkbox . '</b>' . '</td>' . "\n";
 110        }
 111        return $output_string;
 112      }
 113  
 114      function update_credit_account($i) {
 115        global $order, $customer_id, $insert_id, $REMOTE_ADDR;
 116        if (ereg('^GIFT', addslashes($order->products[$i]['model']))) {
 117          $gv_order_amount = ($order->products[$i]['final_price'] * $order->products[$i]['qty']);
 118          if ($this->credit_tax=='true') $gv_order_amount = $gv_order_amount * (100 + $order->products[$i]['tax']) / 100;
 119  //        $gv_order_amount += 0.001;

 120          $gv_order_amount = $gv_order_amount * 100 / 100;
 121          if (MODULE_ORDER_TOTAL_GV_QUEUE == 'false') {
 122            // GV_QUEUE is true so release amount to account immediately

 123            $gv_query=tep_db_query("select amount from " . TABLE_COUPON_GV_CUSTOMER . " where customer_id = '" . $customer_id . "'");
 124            $customer_gv = false;
 125            $total_gv_amount = 0;
 126            if ($gv_result = tep_db_fetch_array($gv_query)) {
 127              $total_gv_amount = $gv_result['amount'];
 128              $customer_gv = true;
 129            }     
 130            $total_gv_amount = $total_gv_amount + $gv_order_amount;
 131            if ($customer_gv) {
 132              $gv_update=tep_db_query("update " . TABLE_COUPON_GV_CUSTOMER . " set amount = '" . $total_gv_amount . "' where customer_id = '" . $customer_id . "'");
 133            } else {
 134              $gv_insert=tep_db_query("insert into " . TABLE_COUPON_GV_CUSTOMER . " (customer_id, amount) values ('" . $customer_id . "', '" . $total_gv_amount . "')");
 135            }
 136          } else {
 137           // GV_QUEUE is true - so queue the gv for release by store owner

 138            $gv_insert=tep_db_query("insert into " . TABLE_COUPON_GV_QUEUE . " (customer_id, order_id, amount, date_created, ipaddr) values ('" . $customer_id . "', '" . $insert_id . "', '" . $gv_order_amount . "', NOW(), '" . $REMOTE_ADDR . "')");
 139          }
 140        }
 141      }
 142  
 143      function credit_selection() {
 144        global $customer_id, $currencies, $language;
 145        $selection_string = '';
 146        $gv_query = tep_db_query("select coupon_id from " . TABLE_COUPONS . " where coupon_type = 'G' and coupon_active='Y'");
 147        if (tep_db_num_rows($gv_query)) {
 148          $selection_string .= '<tr>' . "\n";
 149          $selection_string .= '  <td width="10">' .  tep_draw_separator('pixel_trans.gif', '10', '1') .'</td>';
 150          $selection_string .= '  <td class="main">' . "\n";
 151          $image_submit = '<input type="image" name="submit_redeem" onClick="submitFunction()" src="' . DIR_WS_LANGUAGES . $language . '/images/buttons/button_redeem.gif" border="0" alt="' . IMAGE_REDEEM_VOUCHER . '" title = "' . IMAGE_REDEEM_VOUCHER . '">';
 152          $selection_string .= TEXT_ENTER_GV_CODE . tep_draw_input_field('gv_redeem_code') . '</td>';
 153          $selection_string .= ' <td align="right">' . $image_submit . '</td>';
 154          $selection_string .= '  <td width="10">' . tep_draw_separator('pixel_trans.gif', '10', '1') . '</td>';
 155          $selection_string .= '</tr>' . "\n";
 156        }
 157      return $selection_string;
 158      }
 159  
 160      function apply_credit() {
 161        global $order, $customer_id, $coupon_no, $cot_gv;
 162        if (tep_session_is_registered('cot_gv')) {
 163          $gv_query = tep_db_query("select amount from " . TABLE_COUPON_GV_CUSTOMER . " where customer_id = '" . $customer_id . "'");
 164          $gv_result = tep_db_fetch_array($gv_query);
 165          $gv_payment_amount = $this->deduction;
 166          $gv_amount = $gv_result['amount'] - $gv_payment_amount;
 167          $gv_update = tep_db_query("update " . TABLE_COUPON_GV_CUSTOMER . " set amount = '" . $gv_amount . "' where customer_id = '" . $customer_id . "'");
 168        }
 169        return $gv_payment_amount;
 170      }
 171  
 172      function collect_posts() {
 173  // Security Update + $cot_gv added in Global in v5.13 by Rigadin + updated URL parameters of all tep_redirect function called inside this function

 174        global $currencies, $HTTP_POST_VARS, $customer_id, $coupon_no, $REMOTE_ADDR, $cot_gv;
 175        if ($HTTP_POST_VARS['gv_redeem_code']) {
 176          // Security update by Rigadin in v5.13: add slashes in front of user input

 177          $gv_query = tep_db_query("select coupon_id, coupon_type, coupon_amount from " . TABLE_COUPONS . " where coupon_code = '" . tep_db_input($HTTP_POST_VARS['gv_redeem_code']) . "'");
 178          $gv_result = tep_db_fetch_array($gv_query);
 179          if (tep_db_num_rows($gv_query) != 0) {
 180            $redeem_query = tep_db_query("select * from " . TABLE_COUPON_REDEEM_TRACK . " where coupon_id = '" . $gv_result['coupon_id'] . "'");
 181            if ( (tep_db_num_rows($redeem_query) != 0) && ($gv_result['coupon_type'] == 'G') ) {   
 182            // error, this coupon has been redeemed already.

 183              tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error='.$this->code.'&error=' . urlencode(ERROR_NO_INVALID_REDEEM_GV), 'SSL'));
 184            }
 185          }
 186          // Next line added by Rigadin in v5.13 to give an error when unknown code entered

 187          elseif ($HTTP_POST_VARS['submit_redeem_x']) tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error='.$this->code.'&error=' . urlencode(ERROR_NO_INVALID_REDEEM_GV), 'SSL'));
 188          if ($gv_result['coupon_type'] == 'G') {
 189            $gv_amount = $gv_result['coupon_amount'];
 190            // Things to set

 191            // ip address of claimant

 192            // customer id of claimant

 193            // date 

 194            // redemption flag

 195            // now update customer account with gv_amount

 196            $gv_amount_query=tep_db_query("select amount from " . TABLE_COUPON_GV_CUSTOMER . " where customer_id = '" . $customer_id . "'");
 197            $customer_gv = false;
 198            $total_gv_amount = $gv_amount;;
 199            if ($gv_amount_result = tep_db_fetch_array($gv_amount_query)) {
 200              $total_gv_amount = $gv_amount_result['amount'] + $gv_amount;
 201              $customer_gv = true;
 202            }
 203            $gv_update = tep_db_query("update " . TABLE_COUPONS . " set coupon_active = 'N' where coupon_id = '" . $gv_result['coupon_id'] . "'");
 204            $gv_redeem = tep_db_query("insert into  " . TABLE_COUPON_REDEEM_TRACK . " (coupon_id, customer_id, redeem_date, redeem_ip) values ('" . $gv_result['coupon_id'] . "', '" . $customer_id . "', now(),'" . $REMOTE_ADDR . "')");
 205            if ($customer_gv) {
 206              // already has gv_amount so update

 207              $gv_update = tep_db_query("update " . TABLE_COUPON_GV_CUSTOMER . " set amount = '" . $total_gv_amount . "' where customer_id = '" . $customer_id . "'");
 208            } else {
 209              // no gv_amount so insert

 210              $gv_insert = tep_db_query("insert into " . TABLE_COUPON_GV_CUSTOMER . " (customer_id, amount) values ('" . $customer_id . "', '" . $total_gv_amount . "')");
 211            }
 212            tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error='.$this->code.'&error=' . urlencode(ERROR_REDEEMED_AMOUNT. $currencies->format($gv_amount)), 'SSL'));
 213         }  
 214       }
 215       if ($HTTP_POST_VARS['submit_redeem_x'] && $gv_result['coupon_type'] == 'G') tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error='.$this->code.'&error=' . urlencode(ERROR_NO_REDEEM_CODE), 'SSL'));
 216     }  
 217  
 218      function calculate_credit($amount) {
 219        global $customer_id, $order;
 220        $gv_query=tep_db_query("select amount from " . TABLE_COUPON_GV_CUSTOMER . " where customer_id = '" . $customer_id . "'");
 221        $gv_result=tep_db_fetch_array($gv_query);
 222        $gv_payment_amount = $gv_result['amount'];
 223        $gv_amount = $gv_payment_amount;
 224        $save_total_cost = $amount;
 225        $full_cost = $save_total_cost - $gv_payment_amount;
 226        if ($full_cost <= 0) {
 227          $full_cost = 0;
 228          $gv_payment_amount = $save_total_cost;
 229        }
 230        return tep_round($gv_payment_amount,2);
 231      }
 232  
 233      function calculate_tax_deduction($amount, $od_amount, $method) {
 234        global $order;
 235        switch ($method) {
 236          case 'Standard':
 237          // Amended line, was giving an error when a zero value was arriving here. v5.13 by Rigadin

 238          //$ratio1 = tep_round($od_amount / $amount,2);

 239          $ratio1= ($amount==0? 0 : tep_round($od_amount / $amount,2) );
 240          $tod_amount = 0;
 241          reset($order->info['tax_groups']);
 242          while (list($key, $value) = each($order->info['tax_groups'])) {
 243            $tax_rate = tep_get_tax_rate_from_desc($key);
 244            $total_net += $tax_rate * $order->info['tax_groups'][$key];
 245          }
 246          if ($od_amount > $total_net) $od_amount = $total_net;
 247          reset($order->info['tax_groups']);
 248          while (list($key, $value) = each($order->info['tax_groups'])) {
 249            $tax_rate = tep_get_tax_rate_from_desc($key);
 250            $net = $tax_rate * $order->info['tax_groups'][$key];
 251            if ($net > 0) {
 252              $god_amount = $order->info['tax_groups'][$key] * $ratio1;
 253              $tod_amount += $god_amount;
 254              $order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;
 255            }
 256          }
 257          $order->info['tax'] -= $tod_amount;
 258          $order->info['total'] -= $tod_amount;
 259          break;
 260          case 'Credit Note':
 261            $tax_rate = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
 262            $tax_desc = tep_get_tax_description($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
 263            $tod_amount = $this->deduction / (100 + $tax_rate)* $tax_rate;
 264            $order->info['tax_groups'][$tax_desc] -= $tod_amount;
 265  //          $order->info['total'] -= $tod_amount;

 266          break;
 267          default:
 268        }
 269        return $tod_amount;
 270      }
 271  
 272      function user_has_gv_account($c_id) {
 273        $gv_query = tep_db_query("select amount from " . TABLE_COUPON_GV_CUSTOMER . " where customer_id = '" . $c_id . "'");
 274        if ($gv_result = tep_db_fetch_array($gv_query)) {
 275          if ($gv_result['amount']>0) {
 276            return true;
 277          }
 278        }
 279        return false;
 280      }
 281  
 282      function get_order_total() {
 283        global $order;
 284        $order_total = $order->info['total'];
 285        if ($this->include_tax == 'false') $order_total = $order_total - $order->info['tax'];
 286        if ($this->include_shipping == 'false') $order_total = $order_total - $order->info['shipping_cost'];
 287  
 288        return $order_total;
 289      }    
 290  // START added by Rigadin in v5.13, needed to show module errors on checkout_payment page

 291      function get_error() {
 292        global $HTTP_GET_VARS;
 293  
 294        $error = array('title' => MODULE_ORDER_TOTAL_GV_TEXT_ERROR,
 295                       'error' => stripslashes(urldecode($HTTP_GET_VARS['error'])));
 296  
 297        return $error;
 298      }
 299  // END added by Rigadin        

 300  
 301      function check() {
 302        if (!isset($this->check)) {
 303          $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_ORDER_TOTAL_GV_STATUS'");
 304          $this->check = tep_db_num_rows($check_query);
 305        }
 306  
 307        return $this->check;
 308      }
 309  
 310      function keys() {
 311        return array('MODULE_ORDER_TOTAL_GV_STATUS', 'MODULE_ORDER_TOTAL_GV_SORT_ORDER', 'MODULE_ORDER_TOTAL_GV_QUEUE', 'MODULE_ORDER_TOTAL_GV_INC_SHIPPING', 'MODULE_ORDER_TOTAL_GV_INC_TAX', 'MODULE_ORDER_TOTAL_GV_CALC_TAX', 'MODULE_ORDER_TOTAL_GV_TAX_CLASS', 'MODULE_ORDER_TOTAL_GV_CREDIT_TAX');
 312      }
 313  
 314      function install() {
 315        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_ORDER_TOTAL_GV_STATUS', 'true', 'Do you want to display the Gift Voucher value?', '6', '1','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
 316        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_ORDER_TOTAL_GV_SORT_ORDER', '740', 'Sort order of display.', '6', '2', now())");
 317        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 ('Queue Purchases', 'MODULE_ORDER_TOTAL_GV_QUEUE', 'true', 'Do you want to queue purchases of the Gift Voucher?', '6', '3','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
 318        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_ORDER_TOTAL_GV_INC_SHIPPING', 'true', 'Include Shipping in calculation', '6', '5', 'tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
 319        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_ORDER_TOTAL_GV_INC_TAX', 'true', 'Include Tax in calculation.', '6', '6','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
 320        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 ('Re-calculate Tax', 'MODULE_ORDER_TOTAL_GV_CALC_TAX', 'None', 'Re-Calculate Tax', '6', '7','tep_cfg_select_option(array(\'None\', \'Standard\', \'Credit Note\'), ', now())");
 321        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_ORDER_TOTAL_GV_TAX_CLASS', '0', 'Use the following tax class when treating Gift Voucher as Credit Note.', '6', '0', 'tep_get_tax_class_title', 'tep_cfg_pull_down_tax_classes(', now())");
 322        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 ('Credit including Tax', 'MODULE_ORDER_TOTAL_GV_CREDIT_TAX', 'false', 'Add tax to purchased Gift Voucher when crediting to Account', '6', '8','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
 323      }
 324  
 325      function remove() {
 326        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
 327      }
 328    }
 329  ?>


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