<?php
/*
  www.oscGold.com (C) Kamelia-Net Jacek Krysiak
  modul oscGold wer 4.0
*/

 class kurierilosc {
   var $code, $title, $description, $enabled, $num_kurier;

// class constructor
   function kurierilosc() {
     global $order;
     $this->code = 'kurierilosc';
     $this->title = MODULE_SHIPPING_KURIERILOSC_TEXT_TITLE;
     $this->description = MODULE_SHIPPING_KURIERILOSC_TEXT_DESCRIPTION;
     $this->sort_order = MODULE_SHIPPING_KURIERILOSC_SORT_ORDER;
     $this->icon = '';
     $this->tax_class = MODULE_SHIPPING_KURIERILOSC_TAX_CLASS;
     $this->enabled = ((MODULE_SHIPPING_KURIERILOSC_STATUS == 'True') ? true : false);

     // CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED
     $this->num_kurier =1;

      if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_KURIERILOSC_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_KURIERILOSC_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($method = '') {
     global $total, $order, $shipping_weight, $shipping_num_boxes;
	 $dest_zone = 1;
     $shipping = -1;
	 // okreslanie ilosci towarow w koszyku
	 $ilosc_produktow = 0;
	 for ($s=0, $z=sizeof($order->products); $s<$z; $s++) {
		  $ilosc_produktow = $ilosc_produktow + $order->products[$s]['qty'];
	 }	   
	 // koniec
     $kurierilosc_cost = constant('MODULE_SHIPPING_KURIERILOSC_COST_' . $dest_zone);
     if ($kurierilosc_cost > 0) {
           $shipping = $kurierilosc_cost*$ilosc_produktow;
           $shipping_method = MODULE_SHIPPING_KURIERILOSC_TEXT_WAY . ' ' . $order->delivery['country']['title'] . ': ';
           //if ($shipping_num_boxes > 1) {
           //  $shipping_method .= $shipping_num_boxes . 'x ';
           //}
           $shipping_method .= $ilosc_produktow . ' ' . MODULE_SHIPPING_KURIERILOSC_TEXT_UNITS;
     }

     if ($shipping == -1) {
         $shipping_cost = 0;
         $shipping_method = MODULE_SHIPPING_KURIERILOSC_UNDEFINED_RATE;
       } else {
         $shipping_cost = ($shipping * $shipping_num_boxes) + constant('MODULE_SHIPPING_KURIERILOSC_HANDLING_' . $dest_zone);
     }

     $this->quotes = array('id' => $this->code,
                           'module' => MODULE_SHIPPING_KURIERILOSC_TEXT_TITLE,
                           'methods' => array(array('id' => $this->code,
                                                    'title' => $shipping_method,
                                                    'cost' => $shipping_cost)));
     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_KURIERILOSC_STATUS'");
       $this->_check = tep_db_num_rows($check_query);
     }
     return $this->_check;
   }

   // elari - Added to select default country if not in listing
   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 ('Włączenie modułu', 'MODULE_SHIPPING_KURIERILOSC_STATUS', 'True', 'Czy włączyć moduł Kuriera ?', '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, use_function, set_function, date_added) values ('Grupa VAT', 'MODULE_SHIPPING_KURIERILOSC_TAX_CLASS', '0', 'Użyj następującej grupy VAT odnośnie tego kosztu wysyłki.', '6', '0', '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 ('Strefa dostawy', 'MODULE_SHIPPING_KURIERILOSC_ZONE', '0', 'Wybierz strefę, dla której ma być stosowany ten sposob dostawy', '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 ('Sortowanie', 'MODULE_SHIPPING_KURIERILOSC_SORT_ORDER', '0', 'Kolejność wyświetlania wśród innych modułów wysyłki.', '6', '0', now())");
     for ($i = 1; $i <= $this->num_kurier; $i++) {
        $default_countries = '';
       if ($i == 1) {
         $strefa = 'Polska';
         $default_countries = 'PL';
         $shipping_table = '5.00';
       }
       tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Jednostkowy koszt', 'MODULE_SHIPPING_KURIERILOSC_COST_" . $i ."', '" . $shipping_table . "', 'Koszty przesyłki - jednostkowy koszt dla pojedynczej sztuki', '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 ('Dodatkowy stały koszt obsługi wysyłki', 'MODULE_SHIPPING_KURIERILOSC_HANDLING_" . $i ."', '0', 'Dodatkowy stały koszt obsługi wysyłki', '6', '0', now())");
     }
   }

   // elari - Added to select default country if not in listing
   function remove() {
     tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
   }

   function keys() {
     $keys = array('MODULE_SHIPPING_KURIERILOSC_STATUS', 'MODULE_SHIPPING_KURIERILOSC_TAX_CLASS', 'MODULE_SHIPPING_KURIERILOSC_SORT_ORDER', 'MODULE_SHIPPING_KURIERILOSC_ZONE');
     for ($i=1; $i<=$this->num_kurier; $i++) {
       $keys[] = 'MODULE_SHIPPING_KURIERILOSC_COST_' . $i;
       $keys[] = 'MODULE_SHIPPING_KURIERILOSC_HANDLING_' . $i;
     }
     return $keys;
   }
 }
?>