<?php
/*
  $Id: sagawaex.php,v 1.3 2007/04/16 05:40:59 ryu Exp $

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

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/
//
// 配送モジュール 佐川急便
// TAMURA Toshihiko (tamura@bitscope.co.jp)
//
// [関連ファイル]
// catalog/includes/modules/shipping/sagawaex.php
// catalog/includes/classes/_sagawaex.php
// catalog/includes/languages/japanese/modules/shipping/sagawaex.php
// catalog/images/icons/shipping_sagawaex.gif
//
// [参照]
// http://www.sagawa-exp.co.jp/business/budjetserch-j.html
//
// [修正] by RyuheiAoki 2004/12/02
// 重量0のときに送料受注後回答の注意書きを追加
//
  class sagawaex {
    var $code, $title, $description, $sort_order, $icon, $tax_class, $enabled;
    var $sagawaex_countries, $sagawaex_countries_nbr;
    var $quote;

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

      $this->code        = 'sagawaex';
      $this->title       = MODULE_SHIPPING_SAGAWAEX_TEXT_TITLE;
      $this->description = MODULE_SHIPPING_SAGAWAEX_TEXT_DESCRIPTION;
      $this->sort_order  = MODULE_SHIPPING_SAGAWAEX_SORT_ORDER;
      $this->icon        = DIR_WS_ICONS . 'shipping_sagawaex.gif';
      $this->tax_class   = in_array('MODULE_SHIPPING_SAGAWAEX_TAX_CLASS',$this->keys()) ? MODULE_SHIPPING_SAGAWAEX_TAX_CLASS : 0;
      $this->enabled = ((MODULE_SHIPPING_SAGAWAEX_STATUS == 'True') ? true : false);

      // only these three are needed since SagawaEx only ships to them
      // convert TEP country id to ISO 3166 id
      $this->sagawaex_countries = array(107 => 'JP');
      $this->sagawaex_countries_nbr = array(107);

      if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_SAGAWAEX_ZONE > 0) ) {
        $check_flag = false;
        $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_SHIPPING_SAGAWAEX_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() {
      global $shipping_weight, $shipping_num_boxes;
      global $order;
      global $a_sagawaex_time;
      global $cart;

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

      $country_id = $order->delivery['country']['id'];
      $zone_id    = $order->delivery['zone_id'];

      if (in_array($country_id, $this->sagawaex_countries_nbr)) {
          $zoneinfo = tep_db_query("SELECT zone_code FROM zones WHERE zone_id = '". (int)$zone_id ."'");
          $a_zonevalues = tep_db_fetch_array($zoneinfo);
          $s_zone_code = $a_zonevalues['zone_code'];

          if ( (MODULE_SHIPPING_SAGAWAEX_FREE_SHIPPING != 'True') || ((int)$cart->show_total() < (int)MODULE_SHIPPING_SAGAWAEX_OVER) ) {
              include(DIR_WS_CLASSES . '_sagawaex.php');
              $rate = new _SagawaEx($this->code, MODULE_SHIPPING_SAGAWAEX_TEXT_WAY_NORMAL,
                                STORE_ORIGIN_ZONE, STORE_ORIGIN_COUNTRY);
              $rate->SetDest($s_zone_code, $this->sagawaex_countries[$country_id]);
              $rate->SetWeight($shipping_weight);
              $tmpQuote = $rate->GetQuote(); // id, title, cost | error

              if (isset($tmpQuote['error'])) {
                  $this->quotes['error'] = $tmpQuote['error'];
              } else {
// BOF koshoten.net-2.0.0
// 送料未定対応
/*
                  $this->quotes['module'] = $this->title
                      . ' (' . $shipping_num_boxes . ' x ' . $shipping_weight . 'kg)';

                  $tmpQuote['cost'] *= $shipping_num_boxes;
                  // 送料ディスカウント
                  if (defined('MODULE_SHIPPING_SAGAWAEX_DISCOUNT') && 0 < (int)MODULE_SHIPPING_SAGAWAEX_DISCOUNT) {
                      $tmpQuote['cost'] -= (int)($tmpQuote['cost'] * MODULE_SHIPPING_SAGAWAEX_DISCOUNT / 100);
                  }
                  // 手数料
                  $tmpQuote['cost'] += MODULE_SHIPPING_SAGAWAEX_HANDLING;
*/
                  // 重量が０のときは、送料未定
                  if (!$shipping_weight) {
              	    $this->quotes['module'] = $this->title . MODULE_SHIPPING_SAGAWAEX_TEXT_GET_QUOTE_LATER;
              	    $tmpQuote['cost'] = 0;
                  } else {
                      $this->quotes['module'] = $this->title
                          . ' (' . $shipping_num_boxes . ' x ' . $shipping_weight . 'kg)';

                      $tmpQuote['cost'] *= $shipping_num_boxes;
                      // 送料ディスカウント
                      if (defined('MODULE_SHIPPING_SAGAWAEX_DISCOUNT') && 0 < (int)MODULE_SHIPPING_SAGAWAEX_DISCOUNT) {
                          $tmpQuote['cost'] -= (int)($tmpQuote['cost'] * MODULE_SHIPPING_SAGAWAEX_DISCOUNT / 100);
                      }
                      // 手数料
                      $tmpQuote['cost'] += MODULE_SHIPPING_SAGAWAEX_HANDLING;
                  }
// EOF koshoten.net-2.0.0
              }
          } else {
              $tmpQuote = array('id' => $this->code, 'title' => MODULE_SHIPPING_SAGAWAEX_TEXT_WAY_NORMAL, 'cost' => 0);
          }

          if (!isset($tmpQuote['error'])) {
              // 配送時刻指定
              $timespec = $this->get_timespec();
              $tmpQuote['option'] = TEXT_TIME_SPECIFY
                  . tep_draw_pull_down_menu('sagawaex_timespec', $a_sagawaex_time, $timespec);
              $tmpQuote['timespec'] = $timespec;
          }

          $this->quotes['methods'][] = $tmpQuote;

          if ($this->tax_class > 0) {
              $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $country_id, $zone_id);
          }
        } else {
            $this->quotes['error'] = MODULE_SHIPPING_SAGAWAEX_TEXT_NOTAVAILABLE;
        }

        return $this->quotes;
    }

    // 時刻を指定するプルダウンメニューの'value'を返す
    function get_timespec() {
        global $a_sagawaex_time;
        global $shipping;
        

        $selected = $a_sagawaex_time[0]['id'];
        if ( isset($_POST['sagawaex_timespec']) ) {
            $selected = $_POST['sagawaex_timespec'];
        } elseif ( is_array($shipping) ) { // see checkout_shipping.php
            list($module, $method) = explode('_', $shipping['id']);
            if ($module == $this->code) {
                $selected = $shipping['timespec'];
            }
        }
        return $selected;
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from "
          . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_SAGAWAEX_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_SAGAWAEX_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_SAGAWAEX_TEXT_WAY_NORMAL', '通常便', '配送サービスの名前.', '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_SAGAWAEX_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_SAGAWAEX_HANDLING', '0', '送料に適用する取扱手数料を設定できます.', '6', '1', 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_SAGAWAEX_FREE_SHIPPING', 'False', '送料無料設定を有効にしますか? [合計モジュール]-[送料]-[送料無料設定]を優先する場合は False を選んでください.', '6', '2', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");

      tep_db_query("INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('送料を無料にする購入金額設定', 'MODULE_SHIPPING_SAGAWAEX_OVER', '5000', '設定金額以上をご購入の場合は送料を無料にします.', '6', '3', now())");

      tep_db_query("INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('送料の値引率', 'MODULE_SHIPPING_SAGAWAEX_DISCOUNT', '0', '送料の値引率を指定します.(％)', '6', '4', 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_SAGAWAEX_TAX_CLASS', '0', '配送料金に適用される税種別を選んでください.', '6', '3', 'tep_get_tax_class_title', 'tep_cfg_pull_down_tax_classes(', 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_SAGAWAEX_ZONE', '0', '配送地域を選択すると選択された地域のみで利用可能となります.', '6', '5', '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_SAGAWAEX_SORT_ORDER', '0', '表示の整列順を設定できます. 数字が小さいほど上位に表示されます.', '6', '6', now())");
    }

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

    function keys() {
      return array(
        'MODULE_SHIPPING_SAGAWAEX_TEXT_TITLE',
        'MODULE_SHIPPING_SAGAWAEX_TEXT_WAY_NORMAL',
        'MODULE_SHIPPING_SAGAWAEX_STATUS',
        'MODULE_SHIPPING_SAGAWAEX_HANDLING',
        'MODULE_SHIPPING_SAGAWAEX_FREE_SHIPPING',
        'MODULE_SHIPPING_SAGAWAEX_OVER',
        'MODULE_SHIPPING_SAGAWAEX_DISCOUNT',
//      'MODULE_SHIPPING_SAGAWAEX_TAX_CLASS',
        'MODULE_SHIPPING_SAGAWAEX_ZONE',
        'MODULE_SHIPPING_SAGAWAEX_SORT_ORDER',
      );
    }
  }
?>