ot_gv.php

来自「Zen Cart是真正的电子商务艺术」· PHP 代码 · 共 381 行 · 第 1/2 页

PHP
381
字号
<?php
/**
 * ot_gv order-total module
 *
 * @package orderTotal
 * @copyright Copyright 2003-2007 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: ot_gv.php 7203 2007-10-07 12:29:53Z drbyte $
 */
/**
 * Enter description here...
 *
 */
class ot_gv {
  /**
   * Enter description here...
   *
   * @var unknown_type
   */
  var $title;
  /**
   * Enter description here...
   *
   * @var unknown_type
   */
  var $output;
  /**
   * Enter description here...
   *
   * @return ot_gv
   */
  function ot_gv() {
    global $currencies;
    $this->code = 'ot_gv';
    $this->title = MODULE_ORDER_TOTAL_GV_TITLE;
    $this->header = MODULE_ORDER_TOTAL_GV_HEADER;
    $this->description = MODULE_ORDER_TOTAL_GV_DESCRIPTION;
    $this->user_prompt = MODULE_ORDER_TOTAL_GV_USER_PROMPT;
    $this->sort_order = MODULE_ORDER_TOTAL_GV_SORT_ORDER;
    $this->include_shipping = MODULE_ORDER_TOTAL_GV_INC_SHIPPING;
    $this->include_tax = MODULE_ORDER_TOTAL_GV_INC_TAX;
    $this->calculate_tax = MODULE_ORDER_TOTAL_GV_CALC_TAX;
    $this->credit_tax = MODULE_ORDER_TOTAL_GV_CREDIT_TAX;
    $this->tax_class  = MODULE_ORDER_TOTAL_GV_TAX_CLASS;
    $this->show_redeem_box = MODULE_ORDER_TOTAL_GV_REDEEM_BOX;
    $this->credit_class = true;
    if (!zen_not_null(ltrim($_SESSION['cot_gv'], ' 0')) || $_SESSION['cot_gv'] == '0') $_SESSION['cot_gv'] = '0.00';
    $this->checkbox = $this->user_prompt . '<input type="text" size="6" onkeyup="submitFunction()" name="cot_gv" value="' . number_format($_SESSION['cot_gv'], 2) . '" onfocus="if (this.value == \'' . number_format($_SESSION['cot_gv'], 2) . '\') this.value = \'\';" />' . ($this->user_has_gv_account($_SESSION['customer_id']) > 0 ? '<br />' . MODULE_ORDER_TOTAL_GV_USER_BALANCE . $currencies->format($this->user_has_gv_account($_SESSION['customer_id'])) : '');
    $this->output = array();
    if (IS_ADMIN_FLAG === true) {
      if ($this->include_tax == 'true' && $this->calculate_tax != "None") {
        $this->title .= '<span class="alert">' . MODULE_ORDER_TOTAL_GV_INCLUDE_ERROR . '</span>';
      }
    }
  }
  /**
   * Enter description here...
   *
   */
  function process() {
    global $order, $currencies;
    if ($_SESSION['cot_gv']) {
      $od_amount = $this->calculate_deductions($this->get_order_total());
      $this->deduction = $od_amount['total'];
      if ($od_amount['total'] > 0) {
        reset($order->info['tax_groups']);
        $tax = 0;
        while (list($key, $value) = each($order->info['tax_groups'])) {
          if ($od_amount['tax_groups'][$key]) {
            $order->info['tax_groups'][$key] -= $od_amount['tax_groups'][$key];
            $tax += $od_amount['tax_groups'][$key];
          }
        }
        $order->info['total'] = $order->info['total'] - $od_amount['total'];
        if ($this->calculate_tax = "Standard") $order->info['total'] -= $tax;
        if ($order->info['total'] < 0) $order->info['total'] = 0;
        $order->info['tax'] = $order->info['tax'] - $od_amount['tax'];
        // prepare order-total output for display and storing to invoice
        $this->output[] = array('title' => $this->title . ':',
        'text' => '-' . $currencies->format($od_amount['total']),
        'value' => $od_amount['total']);
      }
    }
  }
  /**
   * This is called to reset any GV values, effectively cancelling all GV's applied during current login session
   */
  function clear_posts() {
    unset($_SESSION['cot_gv']);
  }
  /**
   * This just checks to see whether the currently-logged-in customer has any GV credits on their account
   */
  function selection_test() {
    if ($this->user_has_gv_account($_SESSION['customer_id'])) {
      return true;
    } else {
      return false;
    }
  }
  /**
   * Check for validity of redemption amounts and recalculate order totals to include proposed GV redemption deductions
   */
  function pre_confirmation_check($order_total) {
    global $order, $currencies, $messageStack;
    // clean out negative values and strip common currency symbols
    $_SESSION['cot_gv'] = preg_replace('/[^0-9.%]/', '', $_SESSION['cot_gv']);
    $_SESSION['cot_gv'] = abs($_SESSION['cot_gv']);

    if ($_SESSION['cot_gv'] > 0) {
      // if cot_gv value contains any nonvalid characters, throw error
      if (ereg('[^0-9/.]', trim($_SESSION['cot_gv']))) {
        $messageStack->add_session('checkout_payment', TEXT_INVALID_REDEEM_AMOUNT, error);
        zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
      }
      // if requested redemption amount is greater than value of credits on account, throw error
      if ($_SESSION['cot_gv'] > $currencies->value($this->user_has_gv_account($_SESSION['customer_id']))) {
        $messageStack->add_session('checkout_payment', TEXT_INVALID_REDEEM_AMOUNT, error);
        zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
      }
      $od_amount = $this->calculate_deductions($order_total);
      return $od_amount['total'] + $od_amount['tax'];
    }
    return 0;
  }
  /**
   * if customer has a GV balance, then we display the input field to allow entry of desired GV redemption amount
   */
  function use_credit_amount() {
    if ($this->selection_test()) {
      $output_string = $this->checkbox;
    }
    return $output_string;
  }
  /**
   * queue or release newly-purchased GV's
   */
  function update_credit_account($i) {
    global $db, $order, $insert_id;
    // only act on newly-purchased gift certificates
    if (ereg('^GIFT', addslashes($order->products[$i]['model']))) {
      // determine how much GV was purchased
      $gv_order_amount = ($order->products[$i]['final_price'] * $order->products[$i]['qty']);
      // if tax is to be calculated on purchased GVs, calculate it
      if ($this->credit_tax=='true') $gv_order_amount = $gv_order_amount * (100 + $order->products[$i]['tax']) / 100;
      $gv_order_amount = $gv_order_amount * 100 / 100;

      if (MODULE_ORDER_TOTAL_GV_QUEUE == 'false') {
        // GV_QUEUE is false so release amount to account immediately
        $gv_result = $this->user_has_gv_account($_SESSION['customer_id']);
        $customer_gv = false;
        $total_gv_amount = 0;
        if ($gv_result) {
          $total_gv_amount = $gv_result;
          $customer_gv = true;
        }
        $total_gv_amount = $total_gv_amount + $gv_order_amount;
        if ($customer_gv) {
          $db->Execute("update " . TABLE_COUPON_GV_CUSTOMER . " set amount = '" . $total_gv_amount . "' where customer_id = '" . (int)$_SESSION['customer_id'] . "'");
        } else {
          $db->Execute("insert into " . TABLE_COUPON_GV_CUSTOMER . " (customer_id, amount) values ('" . (int)$_SESSION['customer_id'] . "', '" . $total_gv_amount . "')");
        }
      } else {
        // GV_QUEUE is true - so queue the gv for release by store owner
        $db->Execute("insert into " . TABLE_COUPON_GV_QUEUE . " (customer_id, order_id, amount, date_created, ipaddr) values ('" . (int)$_SESSION['customer_id'] . "', '" . (int)$insert_id . "', '" . $gv_order_amount . "', NOW(), '" . $_SERVER['REMOTE_ADDR'] . "')");
      }
    }
  }
  /**
   * check system to see if GVs should be made available or not. If true, then supply GV-selection fields on checkout pages
   */
  function credit_selection() {
    global $db, $currencies;
    $gv_query = $db->Execute("select coupon_id from " . TABLE_COUPONS . " where coupon_type = 'G' and coupon_active='Y'");
    // checks to see if any GVs are in the system and active or if the current customer has any GV balance
    if ($gv_query->RecordCount() > 0 || $this->use_credit_amount()) {
      $selection = array('id' => $this->code,
                         'module' => $this->title,
                         'redeem_instructions' => MODULE_ORDER_TOTAL_GV_REDEEM_INSTRUCTIONS,
                         'checkbox' => $this->use_credit_amount(),
                         'fields' => array(array('title' => MODULE_ORDER_TOTAL_GV_TEXT_ENTER_CODE,
                         'field' => zen_draw_input_field('gv_redeem_code', '', 'id="disc-'.$this->code.'" onkeyup="submitFunction(0,0)"'),
                         'tag' => 'disc-'.$this->code
                         )));

    }
    return $selection;
  }
  /**
   * Verify that the customer has entered a valid redemption amount, and return the amount that can be applied to this order

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?