<?php
/*

 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
*/

// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();


  class reservation {
    var $info, $totals, $sessions, $customer, $delivery, $content_type,$billing;

    function reservation($reservation_id = '') {
      $this->info = array();
      $this->totals = array();
      $this->products = array();
      $this->customer = array();
      $this->delivery = array();

      if (tep_not_null($reservation_id)) {
        $this->query($reservation_id);
      } else {
        $this->event_cart();
      }
    }

    function query($reservation_id) {
      global $languages_id;

      $reservation_id = tep_db_prepare_input($reservation_id);

      $reservation_query = tep_db_query("select customers_id, customers_name, customers_company, customers_street_address, customers_suburb, customers_city, customers_postcode, customers_state, customers_country, customers_telephone, customers_email_address, customers_address_format_id, billing_name, billing_company, billing_street_address, billing_suburb, billing_city, billing_postcode, billing_state, billing_country, billing_address_format_id, payment_method, cc_type, cc_owner, cc_number, cc_expires, currency, currency_value, date_registered, reservation_status, last_modified from " . TABLE_RESERVATIONS . " where resevation_id = '" . (int)$reservation_id . "'");
      $reservation = tep_db_fetch_array($reservation_query);

      $reservation_status_query = tep_db_query("select reservation_status_name from " . TABLE_RESERVATIONS_STATUS . " where reservations_status_id = '" . $reservation['reservation_status'] . "' and language_id = '" . (int)$languages_id . "'");
      $reservation_status = tep_db_fetch_array($reservation_status_query);

      $this->info = array('currency' => $reservation['currency'],
                          'currency_value' => $reservation['currency_value'],
                          'payment_method' => $reservation['payment_method'],
                          'cc_type' => $reservation['cc_type'],
                          'cc_owner' => $reservation['cc_owner'],
                          'cc_number' => $reservation['cc_number'],
                          'cc_expires' => $reservation['cc_expires'],
                          'date_registered' => $reservation['date_registered'],
                          'reservation_status' => $reservation_status['reservation_status_name'],
                          'last_modified' => $order['last_modified']);

      $this->customer = array('id' => $reservation['customers_id'],
                              'name' => $reservation['customers_name'],
                              'company' => $reservation['customers_company'],
                              'street_address' => $reservation['customers_street_address'],
                              'suburb' => $reservation['customers_suburb'],
                              'city' => $reservation['customers_city'],
                              'postcode' => $reservation['customers_postcode'],
                              'state' => $reservation['customers_state'],
                              'country' => $reservation['customers_country'],
                              'format_id' => $reservation['customers_address_format_id'],
                              'telephone' => $reservation['customers_telephone'],
                              'email_address' => $reservation['customers_email_address']);

      $this->delivery = array('name' => $reservation['delivery_name'],
                              'company' => $reservation['delivery_company'],
                              'street_address' => $reservation['delivery_street_address'],
                              'suburb' => $reservation['delivery_suburb'],
                              'city' => $reservation['delivery_city'],
                              'postcode' => $reservation['delivery_postcode'],
                              'state' => $reservation['delivery_state'],
                              'country' => $reservation['delivery_country'],
                              'format_id' => $reservation['delivery_address_format_id']);

      if (empty($this->delivery['name']) && empty($this->delivery['street_address'])) {
        $this->delivery = false;
      }

      $this->billing = array('name' => $reservation['billing_name'],
                             'company' => $reservation['billing_company'],
                             'street_address' => $reservation['billing_street_address'],
                             'suburb' => $reservation['billing_suburb'],
                             'city' => $reservation['billing_city'],
                             'postcode' => $reservation['billing_postcode'],
                             'state' => $reservation['billing_state'],
                             'country' => $reservation['billing_country'],
                             'format_id' => $reservation['billing_address_format_id']);

      $index = 0;
      $reservation_sessions_query = tep_db_query("select r.reservation_sessions_id, r.sessions_id, e.events_name, r.sessions_price, r.sessions_tax,  r.final_price from " . TABLE_RESERVATION_SESSIONS . " where reservation_id = '" . (int)$reservation_id . "'");
      while ($reservation_sessions = tep_db_fetch_array($reservation_sessions_query)) {
        $this->sessions[$index] = array('id' => $reservation_sessions['sessions_id'],
                                        'name' => $reservation_sessions['events_name'],
                                        'tax' => $reservation_sessions['sessions_tax'],
                                        'price' => $reservation_sessions['sessions_price'],
                                        'final_price' => $reservation_sessions['final_price']);

        $this->info['tax_groups']["{$this->sessions[$index]['tax']}"] = '1';

        $index++;
      }
    }

    function event_cart() {
      global $customer_id, $event_sendto, $event_billto, $event_cart, $languages_id, $currency, $currencies, $event_payment;


      $customer_address_query = tep_db_query("select c.customers_firstname, c.customers_lastname, c.customers_telephone, c.customers_email_address, ab.entry_company, ab.entry_street_address, ab.entry_suburb, ab.entry_postcode, ab.entry_city, ab.entry_zone_id, z.zone_name, co.countries_id, co.countries_name, co.countries_iso_code_2, co.countries_iso_code_3, co.address_format_id, ab.entry_state from " . TABLE_CUSTOMERS . " c, " . TABLE_ADDRESS_BOOK . " ab left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id) left join " . TABLE_COUNTRIES . " co on (ab.entry_country_id = co.countries_id) where c.customers_id = '" . (int)$customer_id . "' and ab.customers_id = '" . (int)$customer_id . "' and c.customers_default_address_id = ab.address_book_id");
      $customer_address = tep_db_fetch_array($customer_address_query);

      $billing_address_query = tep_db_query("select ab.entry_firstname, ab.entry_lastname, ab.entry_company, ab.entry_street_address, ab.entry_suburb, ab.entry_postcode, ab.entry_city, ab.entry_zone_id, z.zone_name, ab.entry_country_id, c.countries_id, c.countries_name, c.countries_iso_code_2, c.countries_iso_code_3, c.address_format_id, ab.entry_state from " . TABLE_ADDRESS_BOOK . " ab left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id) left join " . TABLE_COUNTRIES . " c on (ab.entry_country_id = c.countries_id) where ab.customers_id = '" . (int)$customer_id . "' and ab.address_book_id = '"
                                                . (int)$event_billto . "'");

      $billing_address = tep_db_fetch_array($billing_address_query);
      $tax_address_query = tep_db_query("select ab.entry_country_id, ab.entry_zone_id from " . TABLE_ADDRESS_BOOK . " ab left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id) where ab.customers_id = '" . (int)$customer_id . "' and ab.address_book_id = '" . (int)($this->content_type == 'virtual' ? $billto : $sendto) . "'");
      $tax_address = tep_db_fetch_array($tax_address_query);

      $this->info = array('reservation_status' => $GLOBALS['administrator_login'] ? $GLOBALS['administrator_login'] : DEFAULT_ORDERS_STATUS_ID,
                          'currency' => $currency,
                          'currency_value' => $currencies->currencies[$currency]['value'],
                          'event_payment_method' => $event_payment,
                          'cc_type' => (isset($GLOBALS['cc_type']) ? $GLOBALS['cc_type'] : ''),
                          'cc_owner' => (isset($GLOBALS['cc_owner']) ? $GLOBALS['cc_owner'] : ''),
                          'cc_number' => (isset($GLOBALS['cc_number']) ? $GLOBALS['cc_number'] : ''),
                          'cc_expires' => (isset($GLOBALS['cc_expires']) ? $GLOBALS['cc_expires'] : ''),
                          'tax' => 0,
                          'tax_groups' => array(),
                          'event_comments' => (isset($GLOBALS['event_comments']) ? $GLOBALS['event_comments'] : ''));

      if (isset($GLOBALS[$event_payment]) && is_object($GLOBALS[$event_payment])) {
        $this->info['event_payment_method'] = $GLOBALS[$event_payment]->title;

        if ( isset($GLOBALS[$event_payment]->reservation_status) && is_numeric($GLOBALS[$event_payment]->reservation_status) && ($GLOBALS[$event_payment]->reservation_status > 0) ) {
          $this->info['reservation_status'] = $GLOBALS[$event_payment]->reservation_status;
        }
      }

      $this->customer = array('firstname' => $customer_address['customers_firstname'],
                              'lastname' => $customer_address['customers_lastname'],
                              'company' => $customer_address['entry_company'],
                              'street_address' => $customer_address['entry_street_address'],
                              'suburb' => $customer_address['entry_suburb'],
                              'city' => $customer_address['entry_city'],
                              'postcode' => $customer_address['entry_postcode'],
                              'state' => ((tep_not_null($customer_address['entry_state'])) ? $customer_address['entry_state'] : $customer_address['zone_name']),
                              'zone_id' => $customer_address['entry_zone_id'],
                              'country' => array('id' => $customer_address['countries_id'], 'title' => $customer_address['countries_name'], 'iso_code_2' => $customer_address['countries_iso_code_2'], 'iso_code_3' => $customer_address['countries_iso_code_3']),
                              'format_id' => $customer_address['address_format_id'],
                              'telephone' => $customer_address['customers_telephone'],
                              'email_address' => $customer_address['customers_email_address']);

      $this->billing = array('firstname' => $billing_address['entry_firstname'],
                             'lastname' => $billing_address['entry_lastname'],
                             'company' => $billing_address['entry_company'],
                             'street_address' => $billing_address['entry_street_address'],
                             'suburb' => $billing_address['entry_suburb'],
                             'city' => $billing_address['entry_city'],
                             'postcode' => $billing_address['entry_postcode'],
                             'state' => ((tep_not_null($billing_address['entry_state'])) ? $billing_address['entry_state'] : $billing_address['zone_name']),
                             'zone_id' => $billing_address['entry_zone_id'],
                             'country' => array('id' => $billing_address['countries_id'], 'title' => $billing_address['countries_name'], 'iso_code_2' => $billing_address['countries_iso_code_2'], 'iso_code_3' => $billing_address['countries_iso_code_3']),
                             'country_id' => $billing_address['entry_country_id'],
                             'format_id' => $billing_address['address_format_id']);
   
      $index = 0;
      $sessions = $event_cart->get_sessions();
      for ($i=0, $n=sizeof($sessions); $i<$n; $i++) {
        $this->sessions[$index] = array('name' => $sessions[$i]['name'],
                                        'tax' => tep_get_tax_rate($sessions[$i]['tax_class_id'], $tax_address['entry_country_id'], $tax_address['entry_zone_id']),
                                        'tax_description' => tep_get_tax_description($sessions[$i]['tax_class_id'], $tax_address['entry_country_id'], $tax_address['entry_zone_id']),
                                        'fees' => $sessions[$i]['fees'],
                                        'final_fees' => $sessions[$i]['fees'],
				        'start_date' => $sessions[$i]['start_date'],
				        'start_time' => $sessions[$i]['start_time'],
				        'end_date' => $sessions[$i]['end_date'],
				        'end_time' => $sessions[$i]['end_time'],
                                        'id' => $sessions[$i]['id']);

        $sessions_tax = $this->sessions[$index]['tax'];
        $sessions_tax_description = $this->sessions[$index]['tax_description'];
        $index++;
      }

      if (DISPLAY_PRICE_WITH_TAX == 'true') {
        $this->info['total'] = $this->info['subtotal'] + $this->info['shipping_cost'];
      } else {
        $this->info['total'] = $this->info['subtotal'] + $this->info['tax'] + $this->info['shipping_cost'];
      }
    }
  }
?>
