<?php
/*
  $Id: cod_table_yamato.php,v 1.5 2007/04/16 05:40:47 ryu Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License

変更履歴
2005-02-09 代引き手数料をセッション変数で連携させるよう修正
           （ブラウザにより、hidden の値を引き継がないケースがあるためこれに対応する）
*/
// 代金引換払い(yamato用)
  class cod_table_yamato {
    var $code, $title, $description, $enabled;
    var $n_fee, $s_error;
    var $email_footer;

// class constructor
    function cod_table_yamato() {
      global $order;

      $this->code = 'cod_table_yamato';
      $this->title       = MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_TITLE;
      $this->description = MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_DESCRIPTION;
      $this->sort_order  = MODULE_PAYMENT_COD_TABLE_YAMATO_SORT_ORDER;
      $this->enabled = ((MODULE_PAYMENT_COD_TABLE_YAMATO_STATUS == 'True') && ($GLOBALS['shipping']['id'] == 'yamato_yamato') ? true : false);

      if ((int)MODULE_PAYMENT_COD_TABLE_YAMATO_ORDER_STATUS_ID > 0) {
        $this->order_status = MODULE_PAYMENT_COD_TABLE_YAMATO_ORDER_STATUS_ID;
      }

      if (is_object($order)) $this->update_status();

      $this->email_footer = MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_MAILFOOTER;
    }

// class methods
    function update_status() {
      global $order;

      if ( ($this->enabled == true) && ((int)MODULE_PAYMENT_COD_TABLE_YAMATO_ZONE > 0) ) {
        $check_flag = false;
        $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_PAYMENT_COD_TABLE_YAMATO_ZONE . "' and zone_country_id = '" . (int)$order->billing['country']['id'] . "' order by zone_id");
        while ($check = tep_db_fetch_array($check_query)) {
          if ($check['zone_id'] < 1) {
            $check_flag = true;
            break;
          } elseif ($check['zone_id'] == $order->billing['zone_id']) {
            $check_flag = true;
            break;
          }
        }

        if ($check_flag == false) {
          $this->enabled = false;
        }
      }
    }

// 代引手数料を計算する
    function calc_fee($total_cost) {
      $table_fee = preg_split("/[:,]/" , MODULE_PAYMENT_COD_TABLE_YAMATO_COST);
      $f_find = false;
      $this->n_fee = 0;
      for ($i = 0; $i < count($table_fee); $i+=2) {
        if ($total_cost <= $table_fee[$i]) {
          $this->n_fee = $table_fee[$i+1];
          $f_find = true;
          break;
        }
      }
      if ( !$f_find ) {
        $this->s_error = MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_OVERFLOW_ERROR;
      }

      return $f_find;
    }

// class methods
    function javascript_validation() {
      return false;
    }

    function selection() {
      global $currencies;
      global $order;

      $total_cost = $order->info['total'];      // 税金も含めた代金の総額
      $f_result = $this->calc_fee($total_cost); // 手数料

// 2005-02-09 add -s
      if (!tep_session_is_registered('codt_fee')) tep_session_register('codt_fee');
      $GLOBALS['codt_fee'] = $this->n_fee;
// 2005-02-09 add -e

// 2005-02-09 modi -s
/*
      $added_hidden = $f_result
          ? tep_draw_hidden_field('codt_fee', $this->n_fee).tep_draw_hidden_field('cod_total_cost', $total_cost)
          : tep_draw_hidden_field('codt_fee_error', $this->s_error);
*/
      $added_hidden = $f_result
          ? ''
          : tep_draw_hidden_field('codt_fee_error', $this->s_error);
// 2005-02-09 modi -e

      $s_message = $f_result
        ? (MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_FEE . '&nbsp;' . $currencies->format($this->n_fee))
        : ('<span class="caution">' . $this->s_error . '</span>');

      $selection = array(
          'id' => $this->code,
          'module' => $this->title,
          'fields' => array(array('title' => MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_PROCESS,
                                  'field' => ''),
                            array('title' => $s_message,
                                  'field' => $added_hidden))
      );

      return $selection;
    }

    function pre_confirmation_check() {
      return false;
    }

    function confirmation() {
      global $currencies;
      

      $s_result = !$_POST['codt_fee_error'];
// 2005-02-09 modi -s
/*
      $s_message = $s_result
        ? (MODULE_PAYMENT_COD_TABLE_TEXT_FEE . '&nbsp;' . $currencies->format($_POST['codt_fee']))
        : ('<font color="#FF0000">' . $_POST['codt_fee_error'] . '</font>');
*/
      $this->get_codt_fee_value();  // $GLOBALS['codt_fee'] を取得
      $s_message = $s_result
        ? (MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_FEE . '&nbsp;' . $currencies->format($GLOBALS['codt_fee']))
        : ('<span class="caution">' . $_POST['codt_fee_error'] . '</span>');
// 2005-02-09 modi -e

      $confirmation = array(
          'title' => $this->title,
          'fields' => array(array('title' => MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_PROCESS,
                                  'field' => ''),
                            array('title' => $s_message,
                                  'field' => ''),
                           )
      );
      return $confirmation;
    }

    function process_button() {
      global $currencies;
      
      global $order;

      // email_footer に使用する文字列
// 2005-02-09 modi -s
/*
      $s_message = $_POST['codt_fee_error']
        ? $_POST['codt_fee_error']
        : sprintf(MODULE_PAYMENT_COD_TABLE_TEXT_MAILFOOTER,
            $currencies->format($order->info['total']),
            $currencies->format($_POST['codt_fee']));

      return tep_draw_hidden_field('codt_message', htmlspecialchars($s_message))
           . tep_draw_hidden_field('codt_fee',$_POST['codt_fee']); // for ot_codt
*/
      $this->get_codt_fee_value();  // $GLOBALS['codt_fee'] を取得
      $s_message = $_POST['codt_fee_error']
        ? $_POST['codt_fee_error']
        : MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_MAILFOOTER;

      return tep_draw_hidden_field('codt_message', htmlspecialchars($s_message));
// 2005-02-09 modi -e
    }

    function before_process() {
      

      $this->email_footer = str_replace("\r\n","\n",$_POST['codt_message']);
    }

    function after_process() {
// 2005-02-09 add -s
      if (tep_session_is_registered('codt_fee')) tep_session_unregister('codt_fee');// 2005-02-09 add -e
      return false;
    }

    function get_error() {
      return false;
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_COD_TABLE_YAMATO_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }
      return $this->_check;
    }

    function install() {
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('支払い方法', 'MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_TITLE', 'ヤマトコレクトサービス', '支払い方法の名前', '6', '11' , now())");
      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 ('佐川急便の代金引換払いを有効にする', 'MODULE_PAYMENT_COD_TABLE_YAMATO_STATUS', 'True', '佐川急便の代金引換による支払いを受け付けますか?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ',now());");

      tep_db_query("insert into " . TABLE_CONFIGURATION .
      " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('代引手数料の計算表', 'MODULE_PAYMENT_COD_TABLE_YAMATO_COST', '9999:315,29999:420,99999:630,299999:1050,499999:2100,999999:3150,9999999:4200', '支払い金額に連動する代引手数料. 例: 9999:315,29999:420,99999:630,299999:1050 ... 9999円まで315円, 29999円まで420円, ...', '6', '2', now())");

      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('表示の整列順', 'MODULE_PAYMENT_COD_TABLE_YAMATO_SORT_ORDER', '0', '表示の整列順を設定できます。数字が小さいほど上位に表示されます.', '6', '0' , now())");
      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 ('適用地域', 'MODULE_PAYMENT_COD_TABLE_YAMATO_ZONE', '0', '適用地域を選択すると、選択した地域のみで利用可能となります.', '6', '3', 'tep_get_zone_class_title', 'tep_cfg_pull_down_zone_classes(', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('初期注文ステータス', 'MODULE_PAYMENT_COD_TABLE_YAMATO_ORDER_STATUS_ID', '0', '設定したステータスが受注時に適用されます.', '6', '4', 'tep_cfg_pull_down_order_statuses(', 'tep_get_order_status_name', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('支払い方法選択時の注釈', 'MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_PROCESS', '宅配業者が商品をお届けしたときに、商品と引き換えに代金（配送料・手数料等をふくむ）をお支払いください。', 'レジの支払い方法選択画面で表示する注釈です。', '6', '12' , now())");
      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 ('メールへの注釈', 'MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_MAILFOOTER', '商品お受け取り時に、代金（配送料・代引手数料含む）を\n配達員にお支払いください。', '注文確認メールに追記する文面です。', '6', '13', 'tep_cfg_textarea(', now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
      return array(
          'MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_TITLE',
          'MODULE_PAYMENT_COD_TABLE_YAMATO_STATUS',
          'MODULE_PAYMENT_COD_TABLE_YAMATO_COST',
          'MODULE_PAYMENT_COD_TABLE_YAMATO_SORT_ORDER',
          'MODULE_PAYMENT_COD_TABLE_YAMATO_ZONE',
          'MODULE_PAYMENT_COD_TABLE_YAMATO_ORDER_STATUS_ID',
          'MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_PROCESS',
          'MODULE_PAYMENT_COD_TABLE_YAMATO_TEXT_MAILFOOTER'
      );
    }
// 2005-02-09 add -s
  // -------------------------
  // 代引き手数料を取得する
  // -------------------------
    function get_codt_fee() {
      global $order;
      global $total_cost;

      $total_cost = $order->info['total'];      // 税金も含めた代金の総額
      $f_result = $this->calc_fee($total_cost); // 手数料の計算

      return $f_result;
    }

  // -------------------------
  // 代引き手数料の'value'を返す
  // -------------------------
    function get_codt_fee_value() {
      global $codt_fee;

      if (!tep_session_is_registered('codt_fee')) tep_session_register('codt_fee');

      if (isset($_SESSION['codt_fee'])) {
          $codt_fee = $_SESSION['codt_fee'];
      } elseif (!$codt_fee) {
        $f_result = get_codt_fee();
        $codt_fee = $f_result;
      }
    }
// 2005-02-09 add -e

  }
?>
