<?php
/*
  $Id: percent.php,v 1.33 2002/05/30 15:38:29 dgw_ Exp $

  The Exchange Project - Community Made Shopping!
  http://www.theexchangeproject.org

  Copyright (c) 2000,2001 The Exchange Project

  Released under the GNU General Public License
*/

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

// class constructor
    function percent() {
      $this->code = 'percent';
      $this->title = MODULE_SHIPPING_PERCENT_TEXT_TITLE;
      $this->description = MODULE_SHIPPING_PERCENT_TEXT_DESCRIPTION;
      $this->icon = '';
      $this->enabled = MODULE_SHIPPING_PERCENT_STATUS;
    }

// class methods
    function selection() {
      $selection_string = '<table border="0" cellspacing="0" cellpadding="0" width="100%">' . "\n" .
                          '  <tr>' . "\n" .
                          '    <td class="main">' . (($this->icon) ? tep_image($this->icon, $this->description) : '') . ' ' . MODULE_SHIPPING_PERCENT_TEXT_DESCRIPTION . '</td>' . "\n" .
                          '    <td align="right" class="main">' . tep_draw_checkbox_field('shipping_quote_percent', '1', true) . '</td>' . "\n" .
                          '  </tr>' . "\n" .
                          '</table>' . "\n";

      return $selection_string;
    }

    function quote($method = '') {
      global $cart;

      $shipping_total = $cart->show_total();
      $shipping_amount = $cart->show_weight();


	if (($shipping_total * (MODULE_SHIPPING_PERCENTAGE/100)) > MODULE_SHIPPING_MIN_RATE) {
		$shipping_percent_cost = $shipping_total * (MODULE_SHIPPING_PERCENTAGE/100);
	} else {
		$shipping_percent_cost = MODULE_SHIPPING_MIN_RATE;
	}

	$this->quotes = array('id' => $this->code,
				'module' => $this->title,
			        'methods' => array(array('id' => $this->code,
							 'title' =>MODULE_SHIPPING_PERCENT_TEXT_WAY,
							 'cost' => $shipping_percent_cost))); 
	return $this->quotes;
    }

    function cheapest() {
      global $shipping_count, $shipping_cheapest, $shipping_cheapest_cost, $shipping_percent_cost;

      if ( ($GLOBALS['shipping_quote_all'] == '1') || ($GLOBALS['shipping_quote_percent'] == '1') ) {
        if ($shipping_count == 0) {
          $shipping_cheapest = 'percent';
          $shipping_cheapest_cost = $shipping_percent_cost;
        } else {
          if ($shipping_percent_cost < $shipping_cheapest_cost) {
            $shipping_cheapest = 'percent';
            $shipping_cheapest_cost = $shipping_percent_cost;
          }
        }
        $shipping_count++;
      }
	return $shipping_cheapest_cost;
    }

    function display() {
      global $HTTP_GET_VARS, $currencies, $shipping_cheapest, $shipping_percent_method, $shipping_percent_cost, $shipping_selected;

// set a global for the radio field (auto select cheapest shipping method)
      if (!$shipping_selected) $shipping_selected = $shipping_cheapest;

      if ( ($GLOBALS['shipping_quote_all'] == '1') || ($GLOBALS['shipping_quote_percent'] == '1') ) {
        $display_string = '<table border="0" width="100%" cellspacing="0" cellpadding="0">' . "\n" .
                          '  <tr>' . "\n" .
                          '    <td class="main">' . (($this->icon) ? tep_image($this->icon, $this->description) : '') . ' ' . MODULE_SHIPPING_PERCENT_TEXT_DESCRIPTION . ' <small><i>(' . $shipping_percent_method . ')</i></small></td>' . "\n" .
                          '    <td align="right" class="main">' . $currencies->format($shipping_percent_cost);
        if (tep_count_shipping_modules() > 1) {
          $display_string .= tep_draw_radio_field('shipping_selected', 'percent') .
                             tep_draw_hidden_field('shipping_percent_cost', $shipping_percent_cost) .
                             tep_draw_hidden_field('shipping_percent_method', $shipping_percent_method) . '</td>' . "\n";
        } else {
          $display_string .= tep_draw_hidden_field('shipping_selected', 'percent') .
                             tep_draw_hidden_field('shipping_percent_cost', $shipping_percent_cost) .
                             tep_draw_hidden_field('shipping_percent_method', $shipping_percent_method) . '</td>' . "\n";
        }
        $display_string .= '  </tr>' . "\n" .
                           '</table>' . "\n";
      }

      return $display_string;
    }

    function confirm() {
      global $HTTP_POST_VARS, $shipping_cost, $shipping_method, $shipping_selected;

      if ($shipping_selected == 'percent') {
        $shipping_cost = $HTTP_POST_VARS['shipping_percent_cost'];
        $shipping_method = $HTTP_POST_VARS['shipping_percent_method'];
      }
    }

    function check() {
      if (!isset($this->check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_PERCENT_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 ('Envío por Porcentaje Activado ', 'MODULE_SHIPPING_PERCENT_STATUS', '1', '¿Desea ofrecer los gastos de envío en porcentaje?', '6', '5', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Monto mínimo','MODULE_SHIPPING_MIN_RATE','100000.00','¿Cuál es el monto mínimo para activar el tipo de envío?','6','6',now())");
     tep_db_query("insert into " . TABLE_CONFIGURATION . "(configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Porcentaje','MODULE_SHIPPING_PERCENTAGE','10.00','¿Cuál es el porcentaje?','6','7',now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_PERCENT_STATUS'");
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_MIN_RATE'");
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_PERCENTAGE'");
    }

    function keys() {
      $keys = array('MODULE_SHIPPING_PERCENT_STATUS', 'MODULE_SHIPPING_MIN_RATE','MODULE_SHIPPING_PERCENTAGE');

      return $keys;
    }
  }
?>
