<?php
/*
  $Id: nipponex.php,v 1.1 2006/01/29 14:49:24 ryu Exp $

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

  Copyright (c) 2005 osCommerce

  Released under the GNU General Public License
  Based on sagawaex written by TAMURA Toshihiko (tamura@bitscope.co.jp) 2004/03/22 for MS2.1
*/
//
// [関連ファイル]
// catalog/includes/modules/shipping/nipponex.php
// catalog/includes/classes/_nipponex.php
// catalog/includes/languages/english/modules/shipping/nipponex.php
// catalog/includes/languages/japanese/modules/shipping/nipponex.php
// catalog/images/icons/shipping_nipponex.gif
//
// [参照]
// http://www.nittsu.co.jp/pelican/flash.htm
//
  class nipponex {
    var $code, $title, $description, $sort_order, $icon, $tax_class, $enabled;
    var $nipponex_countries, $nipponex_countries_nbr;
    var $quote;

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

      $this->code        = 'nipponex';
      $this->title       = MODULE_SHIPPING_NIPPONEX_TEXT_TITLE;
      $this->description = MODULE_SHIPPING_NIPPONEX_TEXT_DESCRIPTION;
      $this->sort_order  = MODULE_SHIPPING_NIPPONEX_SORT_ORDER;
      $this->icon        = DIR_WS_ICONS . 'shipping_nipponex.gif';
      $this->tax_class   = in_array('MODULE_SHIPPING_NIPPONEX_TAX_CLASS',$this->keys()) ? MODULE_SHIPPING_NIPPONEX_TAX_CLASS : 0;
      $this->enabled = ((MODULE_SHIPPING_NIPPONEX_STATUS == 'True') ? true : false);

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

      if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_NIPPONEX_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_NIPPONEX_ZONE . "' and zone_country_id = '" . $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_nipponex_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->nipponex_countries_nbr)) {
          $zoneinfo = tep_db_query("SELECT zone_code FROM zones WHERE zone_id = '".$zone_id."'");
          $a_zonevalues = tep_db_fetch_array($zoneinfo);
          $s_zone_code = $a_zonevalues['zone_code'];

          if ( (MODULE_SHIPPING_NIPPONEX_FREE_SHIPPING != 'True') || ((int)$cart->show_total() < (int)MODULE_SHIPPING_NIPPONEX_OVER) ) {
              include(DIR_WS_CLASSES . '_nipponex.php');
              $rate = new _NipponEx($this->code, MODULE_SHIPPING_NIPPONEX_TEXT_WAY_NORMAL,
                                STORE_ORIGIN_ZONE, STORE_ORIGIN_COUNTRY);
              $rate->SetDest($s_zone_code, $this->nipponex_countries[$country_id]);
              $rate->SetWeight($shipping_weight);
              $tmpQuote = $rate->GetQuote(); // id, title, cost | error

              if (isset($tmpQuote['error'])) {
                  $this->quotes['error'] = $tmpQuote['error'];
              } else {
                  $this->quotes['module'] = $this->title
                      . ' (' . $shipping_num_boxes . ' x ' . $shipping_weight . 'kg)';

                  $tmpQuote['cost'] *= $shipping_num_boxes;
                  // 送料ディスカウント
                  if (defined('MODULE_SHIPPING_NIPPONEX_DISCOUNT') && 0 < (int)MODULE_SHIPPING_NIPPONEX_DISCOUNT) {
                      $tmpQuote['cost'] -= (int)($tmpQuote['cost'] * MODULE_SHIPPING_NIPPONEX_DISCOUNT / 100);
                  }
                  // 手数料
                  $tmpQuote['cost'] += MODULE_SHIPPING_NIPPONEX_HANDLING;
              }
          } else {
              $tmpQuote = array('id' => $this->code, 'title' => MODULE_SHIPPING_NIPPONEX_TEXT_WAY_NORMAL, 'cost' => 0);
          }

          if (!isset($tmpQuote['error'])) {
              // 配送時刻指定
              $timespec = $this->get_timespec();
              $tmpQuote['option'] = TEXT_TIME_SPECIFY
                  . tep_draw_pull_down_menu('nipponex_timespec', $a_nipponex_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_NIPPONEX_TEXT_NOTAVAILABLE;
        }

        return $this->quotes;
    }

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

        $selected = $a_nipponex_time[0]['id'];
        if ( isset($_POST['nipponex_timespec']) ) {
            $selected = $_POST['nipponex_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_NIPPONEX_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, set_function, date_added) values ('ペリカン便の配送を有効にする', 'MODULE_SHIPPING_NIPPONEX_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_NIPPONEX_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_NIPPONEX_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_NIPPONEX_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_NIPPONEX_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_NIPPONEX_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_NIPPONEX_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_NIPPONEX_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_NIPPONEX_STATUS',
        'MODULE_SHIPPING_NIPPONEX_HANDLING',
        'MODULE_SHIPPING_NIPPONEX_FREE_SHIPPING',
        'MODULE_SHIPPING_NIPPONEX_OVER',
        'MODULE_SHIPPING_NIPPONEX_DISCOUNT',
//      'MODULE_SHIPPING_NIPPONEX_TAX_CLASS',
        'MODULE_SHIPPING_NIPPONEX_ZONE',
        'MODULE_SHIPPING_NIPPONEX_SORT_ORDER',
      );
    }
  }
?>