<?php
/*
  $Id: yuupack.php,v 1.5 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/classes/_yuupack.php
// catalog/includes/languages/japanese/modules/shipping/yuupack.php
// catalog/includes/modules/shipping/yuupack.php
// catalog/images/icons/shipping_yuupack.gif
//
// [参照]
// http://www.post.yusei.go.jp/service/parcel/you_pack/
//
// [履歴]
// 2004/09/30 料金改定に対応
//
  class yuupack {
    var $code, $title, $description, $sort_order, $icon, $tax_class, $enabled;
    var $yuupack_countries, $yuupack_countries_nbr;
    var $quote;

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

      $this->code        = 'yuupack';
      $this->title       = MODULE_SHIPPING_YUUPACK_TEXT_TITLE;
      $this->description = MODULE_SHIPPING_YUUPACK_TEXT_DESCRIPTION;
      $this->sort_order  = MODULE_SHIPPING_YUUPACK_SORT_ORDER;
      $this->icon        = DIR_WS_ICONS . 'shipping_yuupack.gif';
      $this->tax_class   = in_array('MODULE_SHIPPING_YUUPACK_TAX_CLASS',$this->keys())
                           ? MODULE_SHIPPING_YUUPACK_TAX_CLASS : 0;
      $this->enabled = ((MODULE_SHIPPING_YUUPACK_STATUS == 'True') ? true : false);

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

      if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_YUUPACK_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_YUUPACK_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 $total_weight;
//    global $shipping_weight, $shipping_num_boxes;
      global $order;
      global $a_yuupack_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->yuupack_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_YUUPACK_FREE_SHIPPING != 'True')
               || ((int)$cart->show_total() < (int)MODULE_SHIPPING_YUUPACK_OVER) ) {

              include(DIR_WS_CLASSES . '_yuupack.php');
              $rate = new _YuuPack($this->code, MODULE_SHIPPING_YUUPACK_TEXT_WAY_NORMAL,
                                    STORE_ORIGIN_ZONE, STORE_ORIGIN_COUNTRY);

              // $size_table[] = array(サイズ => 最大重量,...) を作成する
              $a_table = preg_split("/[:,]/" , MODULE_SHIPPING_YUUPACK_TABLE);
              $num = count($a_table);
              $size_table = array();
              for ($i = 0; $i + 1 < $num; $i += 2) {
                $size_table[(int)$a_table[$i + 1]] = $a_table[$i];
              }

              $rate->SetDest($s_zone_code, $this->yuupack_countries[$country_id]);
              $rate->SetWeight($total_weight);   // 重量または体積の合計
              $rate->SetMaxWeight((MODULE_SHIPPING_YUUPACK_MODE == 'weight')
                ? (30 - SHIPPING_BOX_WEIGHT) : 0);// 最大重量
              $rate->SetSizeTable($size_table);  // サイズごとの重量/体積の制限
              $rate->SetItems($cart->count_contents()); // 商品点数

              $tmpQuote = $rate->GetQuote(); // id, title, cost | error

              if (isset($tmpQuote['error'])) {
                  $this->quotes['error'] = $tmpQuote['error'];
              } else {
                // 重量が０のときは、送料未定
                if (!$shipping_weight) {
                  $this->quotes['module'] = $this->title . MODULE_SHIPPING_YUUPACK_TEXT_GET_QUOTE_LATER;
                  $tmpQuote['cost'] = 0;
                } else {
                  $boxes = max($tmpQuote['boxes'], 1); // 個数(個数計算は内部で行なう)
                  $unit = (MODULE_SHIPPING_YUUPACK_MODE == 'weight') ? ' kg' : ' L';

                  $this->quotes['module'] = $this->title
                      . ' (' . $boxes . ' x ' . $tmpQuote['wv'] . $unit . ')';

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

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

        return $this->quotes;
    }

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

        $selected = $a_yuupack_time[0]['id'];
        if ( isset($_POST['yuupack_timespec']) ) {
            $selected = $_POST['yuupack_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_YUUPACK_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_YUUPACK_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_YUUPACK_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_YUUPACK_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_YUUPACK_TABLE', '3:60,7:80,10:100,15:120,18:140,22:160,30:170', '重量または体積に基づいたサイズの設定.<br />例(重量:サイズ): 3:60,7:80,10:100,15:120,18:140,22:160,30:170<br />例(体積:サイズ): 7:60,18:80,30:100,60:120,90:140,140:160,160:170', '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_YUUPACK_MODE', 'weight', '重量(weight)または体積(volume)のどちらを計算表の基準とするかを選んでください. weightを選んだら重量(kg)をvolumeを選んだら体積(リットル)を商品の重量欄に入力してください.', '6', '2', 'tep_cfg_select_option(array(\'weight\', \'volume\'), ', 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_YUUPACK_ROOM', '1.1', '体積(volume)による計算で２個以上の商品を１パッケージに詰め合わせる場合の余裕率(1.0以上)を指定します.', '6', '3', 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_YUUPACK_HANDLING', '0', '送料に適用する取扱手数料を設定できます.', '6', '4', 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_YUUPACK_FREE_SHIPPING', 'False', '送料無料設定を有効にしますか? [合計モジュール]-[送料]-[送料無料設定]を優先する場合は False を選んでください.', '6', '5', '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_YUUPACK_OVER', '5000', '設定金額以上をご購入の場合は送料を無料にします.', '6', '6', 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_YUUPACK_DISCOUNT', '0', '送料の値引率を指定します.(％)', '6', '7', 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_YUUPACK_TAX_CLASS', '0', '配送料金に適用される税種別を選んでください.', '6', '8', '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_YUUPACK_ZONE', '0', '配送地域を選択すると選択された地域のみで利用可能となります.', '6', '9', '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_YUUPACK_SORT_ORDER', '0', '表示の整列順を設定できます. 数字が小さいほど上位に表示されます.', '6', '10', now())");
    }

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

    function keys() {
      return array(
        'MODULE_SHIPPING_YUUPACK_TEXT_TITLE',
        'MODULE_SHIPPING_YUUPACK_TEXT_WAY_NORMAL',
        'MODULE_SHIPPING_YUUPACK_STATUS',

        'MODULE_SHIPPING_YUUPACK_TABLE',
        'MODULE_SHIPPING_YUUPACK_MODE',
        'MODULE_SHIPPING_YUUPACK_ROOM',

        'MODULE_SHIPPING_YUUPACK_HANDLING',
        'MODULE_SHIPPING_YUUPACK_FREE_SHIPPING',
        'MODULE_SHIPPING_YUUPACK_OVER',
        'MODULE_SHIPPING_YUUPACK_DISCOUNT',
//      'MODULE_SHIPPING_YUUPACK_TAX_CLASS',
        'MODULE_SHIPPING_YUUPACK_ZONE',
        'MODULE_SHIPPING_YUUPACK_SORT_ORDER',
      );
    }
  }
?>
