<?php
/*
	osCommerce, Open Source E-Commerce Solutions 
	http://www.oscommerce.com 
	
	Copyright (c) 2003 osCommerce 
	
	osCommRes, Services Oline 
	http://www.oscommres.com 
	Copyright (c) 2005 osCommRes 
	
	Freeway eCommerce 
	http://www.zac-ware.com/freeway 
	Copyright (c) 2007 ZacWare
	
	Released under the GNU General Public License
*/

// Set flag that this is a parent file
  define( '_FEXEC', 1 );
	
  require('includes/application_top.php');
  require('includes/classes/http_client.php');
// BOF: WebMakers.com Added: Downloads Controller - Free Shipping
// Reset $shipping if free shipping is on and weight is not 0

if (tep_get_configuration_key_value('MODULE_SHIPPING_FREESHIPPER_STATUS') && $cart->show_weight()!=0) {
  tep_session_unregister('shipping');
}

// EOF: WebMakers.com Added: Downloads Controller - Free Shipping
// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
    $navigation->set_snapshot();
    tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
	if(tep_check_is_blocked_customer()){
		tep_redirect(tep_href_link(FILENAME_ACCOUNT,'isblocked=yes'));
	}
	if(tep_check_is_suspended_customer()){
		tep_redirect(tep_href_link(FILENAME_ACCOUNT,'suspended=yes'));
	} 
// if there is nothing in the customers cart, redirect them to the shopping cart page
  if ($cart->count_contents() < 1) {
    tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
  }
// BOF: WebMakers.com Added: Attributes Sorter and Copier and Quantity Controller
// Validate Cart for checkout
  $valid_to_checkout= true;
  
  $cart->get_products(true);
  if ($valid_to_checkout) { // Added lines 50-58 by sobi
  	$cart_subs_result=$cart->check_stored_subscription_purchased();
    if($cart_subs_result>2){
		if($cart_subs_result==4) {
			$subs_query=tep_db_query("select sc.subscription_categories_id as cat_id,s.subscription_id as id from " .TABLE_SUBSCRIPTIONS ." s," . TABLE_SUBSCRIPTION_TO_SUBSCRIPTION_CATEGORIES . " sc where s.subscription_id=sc.subscription_id and  s.subscription_type='M' and s.subscription_status=1");
			if(tep_db_num_rows($subs_query)>0)
			{
				$row=tep_db_fetch_array($subs_query);
				tep_redirect(tep_href_link(FILENAME_SUBSCRIPTION_INFO, 'sucPath=' . $row['cat_id']. '&subscription_id=' .$row['id']));
			}
//		   $messageStack->add_session('shopping_cart', 'Please purchase Stored Subscription Item','error');
		} else if($cart_subs_result==3){
		   //$messageStack->add_session('shopping_cart', 'You had Purchased the Stored Subscription Item already.Remove it','error');
		  // echo 'hh';
		  // break;
		   	$get_member_subs_query=tep_db_query("select subscription_id from " . TABLE_SUBSCRIPTIONS . " where subscription_type='M' and subscription_status=1  ");
					if(tep_db_num_rows($get_member_subs_query)>0){
					   $member_subscription=tep_db_fetch_array($get_member_subs_query);
					   $subscription_id = $member_subscription['subscription_id'];
					    if($cart->in_cart('S'.$subscription_id)) $cart->remove("S".$subscription_id);
						} 
						 tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));	
		   }
        
   }
  
    if($cart->check_forced_items_purchased()){
	   $messageStack->add_session('shopping_cart', 'Please purchase all forced order items ...','error');
       tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
   }
  }
// EOF: WebMakers.com Added: Attributes Sorter and Copier and Quantity Controller

// if no shipping destination address was selected, use the customers own address as default
  if (!tep_session_is_registered('sendto')) {
    tep_session_register('sendto');
    $sendto = $customer_default_address_id;
  } else {
// verify the selected shipping address
    $check_address_query = tep_db_query("select count(*) as total from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . (int)$customer_id . "' and address_book_id = '" . (int)$sendto . "'");
    $check_address = tep_db_fetch_array($check_address_query);

    if ($check_address['total'] != '1') {
      $sendto = $customer_default_address_id;
      if (tep_session_is_registered('shipping')) tep_session_unregister('shipping');
    }
  }

  if (!tep_session_is_registered('cartID')) tep_session_register('cartID');
  $cartID = $cart->cartID;
  
	if (NO_CHECKOUT_ZERO_PRICE=="1" && $cart->is_free_checkout()){
		tep_redirect(tep_href_link(FILENAME_CHECKOUT_PROCESS_FREE, '', 'SSL'));
	}

  require(DIR_WS_CLASSES . 'order.php');

  $order = new order;

// register a random ID in the session to check throughout the checkout procedure
// against alterations in the shopping cart contents

// if the order contains only virtual products, forward the customer to the billing page as
// a shipping address is not needed
// ICW CREDIT CLASS GV AMENDE LINE BELOW
//  if ($order->content_type == 'virtual') {
  if (($order->content_type == 'virtual') || ($order->content_type == 'virtual_weight') ) {
    if (!tep_session_is_registered('shipping')) tep_session_register('shipping');
    $shipping = false;
    $sendto = false;

    tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));	//by push
  }

  tep_check_country_differ();
  
  $total_weight = $cart->show_weight();
  $total_count = $cart->count_contents();

// load all enabled shipping modules
  require(DIR_WS_CLASSES . 'shipping.php');
  $shipping_modules = new shipping;
  $quotes_available = $shipping_modules->quote();
  if(sizeof($quotes_available)<=0){
  	tep_redirect(tep_href_link(FILENAME_SHOPPING_CART, '', 'SSL'));
  }

  if ( defined('MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING') && (MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING == 'true') ) {
    $pass = false;

    switch (MODULE_ORDER_TOTAL_SHIPPING_DESTINATION) {
      case 'national':
        if ($order->delivery['country_id'] == STORE_COUNTRY) {
          $pass = true;
        }
        break;
      case 'international':
        if ($order->delivery['country_id'] != STORE_COUNTRY) {
          $pass = true;
        }
        break;
      case 'both':
        $pass = true;
        break;
    }

    $free_shipping = false;
    if ( ($pass == true) && ($order->info['total'] >= MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER) ) {
      $free_shipping = true;

      include(DIR_WS_LANGUAGES . $language . '/modules/order_total/ot_shipping.php');
    }
  } else {
    $free_shipping = false;
  }

// process the selected shipping method
  if ( isset($HTTP_POST_VARS['action']) && ($HTTP_POST_VARS['action'] == 'process') ) {
    if (!tep_session_is_registered('comments')) tep_session_register('comments');
    if (tep_not_null($HTTP_POST_VARS['comments'])) {
      $comments = tep_db_prepare_input($HTTP_POST_VARS['comments']);
    }else {
    	tep_session_unregister('comments');
    }

    if (!tep_session_is_registered('shipping')) tep_session_register('shipping');

    if ( (tep_count_shipping_modules() > 0) || ($free_shipping == true) ) { 
      if ( (isset($HTTP_POST_VARS['shipping'])) && (strpos($HTTP_POST_VARS['shipping'], '_')) ) {
        $shipping = tep_db_prepare_input($HTTP_POST_VARS['shipping']);

        list($module, $method) = explode('_', $shipping);
        if ( is_object($$module) || ($shipping == 'free_free') ) {
          if ($shipping == 'free_free') {
            $quote[0]['methods'][0]['title'] = FREE_SHIPPING_TITLE;
            $quote[0]['methods'][0]['cost'] = '0';
          } else {
            $quote = $shipping_modules->quote($method, $module);
          }
          if (isset($quote['error'])) {
            tep_session_unregister('shipping');
          } else {
            if ( (isset($quote[0]['methods'][0]['title'])) && (isset($quote[0]['methods'][0]['cost'])) ) {
              $shipping = array('id' => $shipping,
                                'title' => (($free_shipping == true) ?  $quote[0]['methods'][0]['title'] : $quote[0]['module'] . ' (' . $quote[0]['methods'][0]['title'] . ')'),
                                'cost' => $quote[0]['methods'][0]['cost']);

              tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
            }
          }
        } else {
          tep_session_unregister('shipping');
        }
      } else {
	  	$messageStack->add('checkout_shipping','No shipping method selected for your order');
	  }
    } else {
      $shipping = false;
           
      tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
    }    
  }

// get all available shipping quotes
  $quotes = $shipping_modules->quote();
// if no shipping method has been selected, automatically select the cheapest method.
// if the modules status was changed when none were available, to save on implementing
// a javascript force-selection method, also automatically select the cheapest shipping
// method if more than one module is now enabled
  if ( !tep_session_is_registered('shipping') || ( tep_session_is_registered('shipping') && ($shipping == false) && (tep_count_shipping_modules() > 1) ) ) $shipping = $shipping_modules->cheapest();

  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CHECKOUT_SHIPPING);

  $breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
  $breadcrumb->add(NAVBAR_TITLE_2, tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));

  $content = CONTENT_CHECKOUT_SHIPPING;
  $javascript = $content . '.js';

  require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);

  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
