<?php
/**
 * @file
 * An example module for how to create a module tutorial.
 */
 
 function thtt_transfer_block_info() {
  $blocks['thtt_transfer'] = array('info' => t('Thai Happy Tour & Travel - Transfer'));
  return $blocks;
}

/**
 * Implements hook_permission().
 */
function thtt_transfer_permission() {
  return array(
    'access thtt_transfer' => array(
      'title' => t('Access content for the Transfer module'),
    )
  );
}

/**
 * Implements hook_menu().
 */
function thtt_transfer_menu() {
  $items = array();

  $items['admin/config/content/thtt_transfer'] = array(
    'title'             => 'Transfer Management',
    'description'       => 'Thai Happy Tour and Travel : Transfer management',
    'page callback'     => 'thtt_transfer_admin_view',
    'page arguments'    => '',
    'access arguments'  => array('access thtt_transfer'),
    'file'              => 'thtt_transfer.admin.inc',
    'type'              => MENU_NORMAL_ITEM,
  );
  
  $items['admin/config/content/thtt_transfer/type'] = array(
    'title'             => 'Transfer type management',
    'description'       => 'Add/Del/Edit type of transfer ie. Standare care, Family car etc.',
    'page callback'     => 'thtt_transfer_admin_type_view',
    'page arguments'    => '',
    'access arguments'  => array('access thtt_transfer'),
    'file'              => 'thtt_transfer.admin.inc',
    'type'              => MENU_NORMAL_ITEM,
  );

  $items['admin/config/content/thtt_transfer/type/add'] = array(
    'title' => 'Add a new transfer type',
    'description' => 'Add a new transfer type',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_thtt_transfer_admin_type_add_form', 'add'),
    'file' => 'thtt_transfer.admin.inc',
    'access arguments' => array('access thtt_transfer'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/thtt_transfer/type/edit/%'] = array(
    'title' => 'Edit transfer type',
    'description' => 'Edit transfer type',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_thtt_transfer_admin_type_edit_form', 6),
    'file' => 'thtt_transfer.admin.inc',
    'access arguments' => array('access thtt_transfer'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/thtt_transfer/type/delete/%/%'] = array(
    'title' => 'Delete transfer type',
    'description' => 'Delete transfer type',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_thtt_transfer_admin_type_delete_form', 6, 7),
    'file' => 'thtt_transfer.admin.inc',
    'access arguments' => array('access thtt_transfer'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/thtt_transfer/destination'] = array(
    'title'             => 'Transfer destination management',
    'description'       => 'Add/Del/Edit destination of transfer',
    'page callback'     => 'thtt_transfer_admin_destination_view',
    'page arguments'    => '',
    'access arguments'  => array('access thtt_transfer'),
    'file'              => 'thtt_transfer.admin.inc',
    'type'              => MENU_NORMAL_ITEM,
  );

  $items['admin/config/content/thtt_transfer/destination/add'] = array(
    'title' => 'Add a new transfer destination',
    'description' => 'Add a new transfer destination',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_thtt_transfer_admin_destination_add_form', 'add'),
    'file' => 'thtt_transfer.admin.inc',
    'access arguments' => array('access thtt_transfer'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/thtt_transfer/destination/edit/%'] = array(
    'title' => 'Edit transfer destination',
    'description' => 'Edit transfer destination',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_thtt_transfer_admin_destination_edit_form', 6),
    'file' => 'thtt_transfer.admin.inc',
    'access arguments' => array('access thtt_transfer'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/thtt_transfer/destination/delete/%/%'] = array(
    'title' => 'Delete transfer destination',
    'description' => 'Delete transfer destination',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_thtt_transfer_admin_destination_delete_form', 6, 7),
    'file' => 'thtt_transfer.admin.inc',
    'access arguments' => array('access thtt_transfer'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/thtt_transfer/price'] = array(
    'title'             => 'Transfer fare price Management',
    'description'       => 'Transfer fare price Management',
    'page callback'     => 'thtt_transfer_admin_price_view',
    'page arguments'    => '',
    'access arguments'  => array('access thtt_transfer'),
    'file'              => 'thtt_transfer.admin.inc',
    'type'              => MENU_NORMAL_ITEM,
  );

  $items['admin/config/content/thtt_transfer/price/add'] = array(
    'title' => 'Add a new price',
    'description' => 'Add a new price',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_thtt_transfer_admin_price_add_form', 'add'),
    'file' => 'thtt_transfer.admin.inc',
    'access arguments' => array('access thtt_transfer'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/thtt_transfer/price/edit/%'] = array(
    'title' => 'Edit transfer fare price',
    'description' => 'Edit transfer fare price',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_thtt_transfer_admin_price_edit_form', 6),
    'file' => 'thtt_transfer.admin.inc',
    'access arguments' => array('access thtt_transfer'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/thtt_transfer/price/delete/%/%'] = array(
    'title' => 'Delete fare price',
    'description' => 'Delete fare price',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_thtt_transfer_admin_price_delete_form', 6, 7),
    'file' => 'thtt_transfer.admin.inc',
    'access arguments' => array('access thtt_transfer'),
    'type' => MENU_CALLBACK,
  );
  
  $items['admin/config/content/thtt_transfer/transaction'] = array(
    'title' => 'Transfer Transaction Order',
    'description' => 'View transafer transaction order by customer',
    'page callback'     => '_thtt_transfer_admin_transaction',
    'page arguments'    => '',
    'access arguments'  => array('access thtt_transfer'),
    'type'              => MENU_NORMAL_ITEM,
  );
  
  return $items;
}

function thtt_transfer_admin_view() {
	$output = "";
	
  $result = db_select('thtt_m_tour','t')
    ->fields('t')
    ->execute();
  
  $header = array(t('Mangement Menu'), t('Operations'));
   $rows[] = array(
      array('data' => t('Transfer Transaction Order'), 'valign' => 'top'),
      array(
        'data' =>
        l(t('View order'), 'admin/config/content/thtt_transfer/transaction'),
        'valign' => 'top'
      )
    );
    
   $rows[] = array(
      array('data' => t('Transfer Type management'), 'valign' => 'top'),
      array(
        'data' =>
        l(t('List'), 'admin/config/content/thtt_transfer/type') . ' | '.
        l(t('Add new tour'), 'admin/config/content/thtt_transfer/type/add') ,
        'valign' => 'top'
      )
    );
    
    $rows[] = array(
      array('data' => t('Transfer destination management'), 'valign' => 'top'),
      array(
        'data' =>
        l(t('List'), 'admin/config/content/thtt_transfer/destination') . ' | '.
        l(t('Add new destination'), 'admin/config/content/thtt_transfer/destination/add') ,
        'valign' => 'top'
      )
    );
    
    $rows[] = array(
      array('data' => t('Transfer fare price management'), 'valign' => 'top'),
      array(
        'data' =>
        l(t('List'), 'admin/config/content/thtt_transfer/price') . ' | '.
        l(t('Add new fare price'), 'admin/config/content/thtt_transfer/price/add') ,
        'valign' => 'top'
      )
    );
  
  
  $output .= theme('table', array("header" => $header, "rows" => $rows));
  return $output;
}
  
function thtt_transfer_admin_type_view($ghi = 0, $jkl = '') {
  $output = "";
  $result = db_select('thtt_m_transfer_type','t')
		  	->fields('t')
		  	->execute();
  
  $output .= l(t('Add new type'), 'admin/config/content/thtt_transfer/type/add');
  
  $header = array(t('Type ID'), t('Name'), t('Description'), t('Pax'), t('Max Pax'), t('Max Vehicle'), t('Display order'), t('Operations'));
  foreach($result as $item) { 
  
   $rows[] = array(
      array('data' => $item->type_id, 'valign' => 'top'),
      array('data' => $item->type_name, 'valign' => 'top'),
      array('data' => $item->type_desc, 'valign' => 'top'),
      array('data' => $item->type_pax, 'valign' => 'top'),
      array('data' => $item->type_max_pax, 'valign' => 'top'),
      array('data' => $item->type_max_vehicle, 'valign' => 'top'),
      array('data' => $item->display_order, 'valign' => 'top'),
      array(
        'data' =>
        l(t('edit'), 'admin/config/content/thtt_transfer/type/edit/' . urlencode($item->type_id)) . ' ' .
        l(t('delete'), 'admin/config/content/thtt_transfer/type/delete/' . urlencode($item->type_id).'/'.urlencode($item->type_name)),
        'valign' => 'top'
      )
    );
  }
  
  $output .= theme('table', array("header" => $header, "rows" => $rows));
  return $output;
}
  
function thtt_transfer_admin_destination_view($ghi = 0, $jkl = '') {
  $output = "";
  $result = db_select('thtt_m_transfer_destination','t')
		  	->fields('t')
		  	->execute();
  
  $output .= l(t('Add new transfer destination'), 'admin/config/content/thtt_transfer/destination/add');
  
  $header = array(t('Destination ID'), t('Name'), t('Display order'), t('Operations'));
  foreach($result as $item) { 
  
   $rows[] = array(
      array('data' => $item->dest_id, 'valign' => 'top'),
      array('data' => $item->dest_name, 'valign' => 'top'),
      array('data' => $item->display_order, 'valign' => 'top'),
      array(
        'data' =>
        l(t('edit'), 'admin/config/content/thtt_transfer/destination/edit/' . urlencode($item->dest_id)) . ' ' .
        l(t('delete'), 'admin/config/content/thtt_transfer/destination/delete/' . urlencode($item->dest_id).'/'.urlencode($item->dest_name)),
        'valign' => 'top'
      )
    );
  }
  
  $output .= theme('table', array("header" => $header, "rows" => $rows));
  return $output;
}
  
  
function thtt_transfer_admin_price_view($ghi = 0, $jkl = '') {
	$output = "";
	
  $query = db_select('thtt_m_transfer_fare','t');
  $query->join('thtt_m_transfer_destination', 'd1', 't.fare_from = d1.dest_id');
  $query->join('thtt_m_transfer_destination', 'd2', 't.fare_to = d2.dest_id');
  $query->join('thtt_m_transfer_type', 'ty', 't.fare_type = ty.type_id');
  $query->fields('t')
  			->fields('ty', array('type_name'));
  $query->addField('d1', 'dest_name', 'dest_from_name');
  $query->addField('d2', 'dest_name', 'dest_to_name');
  $query->orderBy('d1.dest_name', 'ASC');
  $query->orderBy('d2.dest_name', 'ASC');
  $result = $query->execute();
  
  $output .= l(t('Add new fare price'), 'admin/config/content/thtt_transfer/price/add');
  
  $header = array(t('Fare price ID'), t('From'), t('To'), t('Type name'), t('Price'),  t('Operations'));
  foreach($result as $item) { 
	
   $rows[] = array(
      array('data' => $item->fare_id, 'valign' => 'top'),
      array('data' => $item->dest_from_name, 'valign' => 'top'),
      array('data' => $item->dest_to_name, 'valign' => 'top'),
      array('data' => $item->type_name, 'valign' => 'top'),
      array('data' => $item->fare_price, 'valign' => 'top'),
      array(
        'data' =>
        l(t('edit'), 'admin/config/content/thtt_transfer/price/edit/' . urlencode($item->fare_id)) . ' ' .
        l(t('delete'), 'admin/config/content/thtt_transfer/price/delete/' . urlencode($item->fare_id)),
        'valign' => 'top'
      )
    );
  }
  
  $output .= theme('table', array("header" => $header, "rows" => $rows));
  return $output;
}



function _thtt_transfer_admin_transaction($ghi = 0, $jkl = '') {
  $output = "";
  $ref_code = "";
  
  if(isset($_GET['ref_code'])){
  	$ref_code = $_GET['ref_code'];
  }
	
  $query = db_select('thtt_t_tf_reservation','t');
  $query->join('thtt_m_transfer_destination', 'd1', 't.fare_from = d1.dest_id');
  $query->join('thtt_m_transfer_destination', 'd2', 't.fare_to = d2.dest_id');
  $query->join('thtt_m_transfer_type', 'ty', 't.vehicle_type = ty.type_id');
  $query->join('thtt_m_member', 'm', 'm.member_id = t.member_id');
  $query->fields('t')
  			->fields('ty', array('type_name'));
  $query->addField('d1', 'dest_name', 'dest_from_name');
  $query->addField('d2', 'dest_name', 'dest_to_name');
  
  if($ref_code != ""){
	$query->condition('t.rsvn_ref', '%' . db_like($ref_code) . '%', 'LIKE');
  }
  
  
  $query->orderBy('t.rsvn_ref', 'DESC');
  $result = $query->execute();
  
  $output .= '<b>'.t('Search').'</b>';
  $output .= '<form>';
  $output .= t('Ref code').' : '.' <input type="text" name="ref_code" value="'.$ref_code.'" style="border: solid 1px #000000; line-height: 20px;"/> <input type="submit" name="search" vale="'.t('Search').'" style="width:80px; height:28px; border: solid 1px #000000; "/>';
  $output .= '</form>';
  $output .= '<BR/><BR/>';
  
  
  $header = array(t('Ref code'), t('IP Address'), t('Customer name'), t('Nationality'), t('Tel'), t('Email'), t('From'), t('To'), t('Journey'), t('vehicle_type'), t('Number vehicle'), t('Number passenger'), 
  					t('Hotel name'), t('Arrival flight'), t('Arrival date'), t('Depart flight'), t('Depart date'), t('Total price'), t('Special request'), t('Status'), t('Create date'),  t('Operations'));
  					
  foreach($result as $item) { 
	  
	  $status = "";
	  if($item->rsvn_status == 1){
	  	$status = "Unpaid";
	  }else if($item->rsvn_status == 2){
	  	$status = "Paid";  
	  }
	  
	   $journey = "N/A";
	   if($item->journey == 1){
	   	$journey = "One-way";
	   }else if($item->journey == 2){ 
	   	$journey = "Round-trip";
	   }	
	   
	   $vehicle_type = $item->type_name;
	   
	   $rows[] = array(
	      array('data' => $item->rsvn_ref, 'valign' => 'top'),
	      array('data' => $item->rsvn_ip, 'valign' => 'top'),
	      array('data' => $item->rsvn_guest_title.$item->rsvn_guest_fname.' '.$item->rsvn_guest_lname, 'valign' => 'top'),
	      array('data' => $item->rsvn_nationality, 'valign' => 'top'),
	      array('data' => $item->rsvn_tel, 'valign' => 'top'),
	      array('data' => $item->rsvn_email, 'valign' => 'top'),
	      array('data' => $item->dest_from_name, 'valign' => 'top'),
	      array('data' => $item->dest_to_name, 'valign' => 'top'),
	      array('data' => $journey, 'valign' => 'top'),
	      array('data' => $vehicle_type, 'valign' => 'top'),
	      array('data' => $item->number_vehicle, 'valign' => 'top'),
	      array('data' => $item->number_passenger, 'valign' => 'top'),
	      array('data' => $item->hotel_name, 'valign' => 'top'),
	      array('data' => $item->arrival_flight, 'valign' => 'top'),
	      array('data' => $item->arrival_date, 'valign' => 'top'),
	      array('data' => $item->depart_flight, 'valign' => 'top'),
	      array('data' => $item->depart_date, 'valign' => 'top'),
	      array('data' => $item->cost, 'valign' => 'top'),
	      array('data' => $item->remark, 'valign' => 'top'),
	      array('data' => t($status), 'valign' => 'top'),
	      array('data' => $item->create_date, 'valign' => 'top'),
	      array(
	        'data' =>
	        l(t('Set fnish order'), 'admin/config/content/thtt_transfer/transaction/finish/' . urlencode($item->rsvn_id)),
	        'valign' => 'top'
	      )
	    );
  }
  
  $output .= theme('table', array("header" => $header, "rows" => $rows));
  return $output;
}