<?php
/*
  sassi.php
  作成日：2004/12/2
　最終更新日：

　作成者：有限会社セカンドブレーン
　　　　　　青木隆平
　　　　　　ryuhei@secondbrain.co.jp

  Released under the GNU General Public License
*/

  class sassi {
    var $code, $title, $description, $icon, $enabled;

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

      $this->code = 'sassi';
      $this->title = MODULE_SHIPPING_SASSI_TEXT_TITLE;
      $this->description = MODULE_SHIPPING_SASSI_TEXT_DESCRIPTION;
      $this->sort_order = MODULE_SHIPPING_SASSI_SORT_ORDER;
      $this->icon = '';
      $this->tax_class = 0;
      $this->enabled = ((MODULE_SHIPPING_SASSI_STATUS == 'True') ? true : false);

      if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_SASSI_ZONE > 0) ) {
        $check_flag = false;
        $check_query = tep_db_query("select zone_id from " . SASSI_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_SHIPPING_SASSI_ZONE . "' and zone_country_id = '" . (int)$order->delivery['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->delivery['zone_id']) {
            $check_flag = true;
            break;
          }
        }

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

// class methods
    function quote($method = '') {
      global $order, $cart, $shipping_weight, $shipping_num_boxes;

      if (empty($shipping_weight)) {
        $this->quotes = array('id' => $this->code,
                              'module' => MODULE_SHIPPING_SASSI_TEXT_TITLE . MODULE_SHIPPING_SASSI_TEXT_GET_QUOTE_LATER,
                              'methods' => array(array('id' => $this->code,
                                                       'title' => MODULE_SHIPPING_SASSI_TEXT_WAY,
                                                       'cost' => 0)));
      } else {
        $sassi_cost = preg_split("/[:,]/" , MODULE_SHIPPING_SASSI_COST);
        $size = sizeof($sassi_cost);
        for ($i=0, $n=$size; $i<$n; $i+=2) {
          if ($shipping_weight <= $sassi_cost[$i]) {
            $shipping = $sassi_cost[$i+1];
            break;
          }
        }
        $shipping = $shipping * $shipping_num_boxes;
        $this->quotes = array('id' => $this->code,
                              'module' => MODULE_SHIPPING_SASSI_TEXT_TITLE,
                              'methods' => array(array('id' => $this->code,
                                                       'title' => MODULE_SHIPPING_SASSI_TEXT_WAY,
                                                       'cost' => $shipping + MODULE_SHIPPING_SASSI_HANDLING)));
      }

      if ($this->tax_class > 0) {
        $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
      }

      if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title);

      return $this->quotes;
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_SASSI_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_SHIPPING_SASSI_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, date_added) values ('配送サービス', 'MODULE_SHIPPING_SASSI_TEXT_WAY', '冊子小包', '配送サービスの名前', '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_SHIPPING_SASSI_STATUS', 'True', '冊子小包による配送を提供しますか?', '6', '0', '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_SHIPPING_SASSI_COST', '150:180,250:210,500:290,1000:340,2000:450,3000:590', '重量に基づいた配送料金の設定. 例: 150:180,250:210, ... 150g まで 180円, 250 まで 210円, ...', '6', '0', 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_SHIPPING_SASSI_HANDLING', '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_SHIPPING_SASSI_ZONE', '0', '配送地域を選択すると選択された地域のみで利用可能となります.', '6', '0', '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, date_added) values ('表示の整列順', 'MODULE_SHIPPING_SASSI_SORT_ORDER', '0', '表示の整列順を設定できます。数字が小さいほど上位に表示されます.', '6', '0', now())");
    }

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

    function keys() {
      return array('MODULE_SHIPPING_SASSI_TEXT_TITLE', 'MODULE_SHIPPING_SASSI_TEXT_WAY', 'MODULE_SHIPPING_SASSI_STATUS', 'MODULE_SHIPPING_SASSI_COST', 'MODULE_SHIPPING_SASSI_HANDLING', 'MODULE_SHIPPING_SASSI_ZONE', 'MODULE_SHIPPING_SASSI_SORT_ORDER');
    }
  }
?>
