<?php
/* 
  $Id:ot_pobraniepp.php, v 1.0 2004/07/11 23:23:23
  ot_surcharge.php,v 1.0 2003/06/19 01:13:43 hpdl wib $
 
  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2002 osCommerce

  Released under the GNU General Public License
  Prepared for Polish Post Office Packages .:. 2oo4 ER Group .:. ergroup@o2.pl
  
*/

  class ot_pobraniepp {
    var $title, $output;

    function ot_pobraniepp() {
      $this->code = 'ot_pobraniepp';
      $this->title = MODULE_PAYMENT_POBRANIEPP_TITLE;
      $this->description = MODULE_PAYMENT_POBRANIEPP_DESCRIPTION;
      $this->enabled = (( defined('MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING') && (MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING == 'tak')) ? 'nie' : MODULE_PAYMENT_POBRANIEPP_STATUS); 
      $this->sort_order = MODULE_PAYMENT_POBRANIEPP_SORT_ORDER;
      $this->minimum = MODULE_PAYMENT_POBRANIEPP_MINIMUM;
      $this->include_tax = MODULE_PAYMENT_POBRANIEPP_INC_TAX;
      $this->howto = MODULE_PAYMENT_POBRANIEPP_HOWTO;
      $this->calculate_tax = MODULE_PAYMENT_POBRANIEPP_CALC_TAX;
//      $this->credit_class = true;
      $this->output = array();
    }

    function process() {
     global $order, $currencies;

      $od_amount = $this->calculate_fee($this->get_order_total());
      if ($od_amount>0) {
      $this->addition = $od_amount;
      $this->output[] = array('title' => $this->title . ':',
                              'text' => '<b>' . $currencies->format($od_amount) . '</b>',
                              'value' => $od_amount);
    $order->info['total'] = $order->info['total'] + $od_amount;
}
}


function calculate_fee($amount) {
    global $order, $customer_id, $payment, $method;
    $do = false;
    
    if ($amount > $this->minimum) {
    $table = split("[,]" , MODULE_PAYMENT_POBRANIEPP_TYPE);
    for ($i = 0; $i < count($table); $i++) {
          if ($payment == $table[$i]) $do = true;
    }
   
    $ship2ot_table = split("[,]", MODULE_PAYMENT_POBRANIEPP_S_TYPE);
    for ($i = 0; $i < count($ship2ot_table); $i++) {
	$ship2ot_table[$i] = trim($ship2ot_table[$i]);
	$ship2ot_table[$i] = $ship2ot_table[$i] . '_' .$ship2ot_table[$i];
    	if ($GLOBALS['shipping']['id'] == $ship2ot_table[$i]) $doo = true;
    } 
    
 
//      if (in_array($GLOBALS['shipping']['id'], $ship2ot_table)) $do = false;    

    
    if ($do && $doo) {
// Calculate tax reduction if necessary
    if($this->calculate_tax == 'tak') {
// Calculate main tax reduction
      $tod_amount = round($order->info['tax']*10)/10*$od_pc/100;
      $order->info['tax'] = $order->info['tax'] + $tod_amount;
// Calculate tax group deductions
      reset($order->info['tax_groups']);
      while (list($key, $value) = each($order->info['tax_groups'])) {
        $god_amount = round($value*10)/10*$od_pc/100;
        $order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] + $god_amount;
      }
	  }
	  
	$stawka = 0; //opłata stała
	$od_pc = 0.0; //%
	$amount = $amount + $order->info['shipping_cost']; //doliczenie paczki do ceny
	$od_amount = round($amount*10)/10*$od_pc/100 + $stawka; //procent + kwota bazowa
	if ($od_amount < 3.5) {$od_amount=3.5;}
	$od_amount = $od_amount + $tod_amount;
	}
	}
	return $od_amount;
}
	
	
function get_order_total() {
    global  $order, $cart;
    $order_total = $order->info['total'];
// Check if gift voucher is in cart and adjust total
    $products = $cart->get_products();
    for ($i=0; $i<sizeof($products); $i++) {
      $t_prid = tep_get_prid($products[$i]['id']);
      $gv_query = tep_db_query("select products_price, products_tax_class_id, products_model from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . "'");
      $gv_result = tep_db_fetch_array($gv_query);
      if (ereg('^GIFT', addslashes($gv_result['products_model']))) {
        $qty = $cart->get_quantity($t_prid);
        $products_tax = tep_get_tax_rate($gv_result['products_tax_class_id']);
        if ($this->include_tax =='nie') {
           $gv_amount = $gv_result['products_price'] * $qty;
        } else {
          $gv_amount = ($gv_result['products_price'] + tep_calculate_tax($gv_result['products_price'],$products_tax)) * $qty;
        }
        $order_total=$order_total - $gv_amount;
      }
    }
    if ($this->include_tax == 'nie') $order_total=$order_total-$order->info['tax'];
   
	$order_total=$order_total-$order->info['shipping_cost'];
    return $order_total;
  }


    function check() {
      if (!isset($this->check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_POBRANIEPP_STATUS'");
        $this->check = tep_db_num_rows($check_query);
      }

      return $this->check;
    }

    function keys() {
      return array('MODULE_PAYMENT_POBRANIEPP_STATUS', 'MODULE_PAYMENT_POBRANIEPP_SORT_ORDER','MODULE_PAYMENT_POBRANIEPP_HOWTO','MODULE_PAYMENT_POBRANIEPP_MINIMUM', 'MODULE_PAYMENT_POBRANIEPP_TYPE', 'MODULE_PAYMENT_POBRANIEPP_INC_TAX', 'MODULE_PAYMENT_POBRANIEPP_S_TYPE', 'MODULE_PAYMENT_POBRANIEPP_CALC_TAX');
    }

    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 ('Pokazuj', 'MODULE_PAYMENT_POBRANIEPP_STATUS', 'tak', 'Czy chcesz wlaczyc oplate za Pobranie?', '6', '1','tep_cfg_select_option(array(\'tak\', \'nie\'), ', 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_PAYMENT_POBRANIEPP_SORT_ORDER', '111', 'Porzadek sortowania.', '6', '2', now())");
// ze wzgledu na zmiane w cenniku Poczty Polskiej opcja zostrala wylaczona w wersji 1.1
      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 ('Rodzaj pobrania', 'MODULE_PAYMENT_POBRANIEPP_HOWTO', 'wpłata STANDARD', 'Sposób w jaki zostaną przekazane nam pieniądze', '6', '5', 'tep_cfg_select_option(array(\'wpłata STANDARD\'), ', 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 ('Wliczac Podatek', 'MODULE_PAYMENT_POBRANIEPP_INC_TAX', 'tak', 'Wlicz podatek do przeliczen.', '6', '6','tep_cfg_select_option(array(\'tak\', \'nie\'), ', 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 ('Przelicz podatek', 'MODULE_PAYMENT_POBRANIEPP_CALC_TAX', 'nie', 'Przelicz podatek dla zmienionej oplaty.', '6', '5','tep_cfg_select_option(array(\'tak\', \'nie\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Minimalna wartosc', 'MODULE_PAYMENT_POBRANIEPP_MINIMUM', '', 'Minimalna wartosc zamowienia dla naliczania oplaty', '6', '2', now())");
    tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Włącz pobranie dla płatności', 'MODULE_PAYMENT_POBRANIEPP_TYPE', 'cod', 'Rodzaj platnosci dla naliczania oplaty', '6', '7', now())");
    tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Wlacz pobranie dla wysylki', 'MODULE_PAYMENT_POBRANIEPP_S_TYPE', 'flat', 'Rodzaj wysyłki, dla której naliczana jest opłata', '6', '8', now())");
    }

    function remove() {
      $keys = '';
      $keys_array = $this->keys();
      for ($i=0; $i<sizeof($keys_array); $i++) {
        $keys .= "'" . $keys_array[$i] . "',";
      }
      $keys = substr($keys, 0, -1);

      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in (" . $keys . ")");
    }
  }
?>