<?php

require_once('config.php');

$_hall = $_POST['hall'];
$_table = $_POST['table'];

// echo $_hall.'-'.$_table;


$query = odbc_exec($db,"SELECT * FROM Q_master_table WHERE hall_name='$_hall' AND Masa='$_table'");
$table = odbc_fetch_array($query);
// print_r($table);
// $table = $table[0];

$master_delete = odbc_exec($db,"SELECT * FROM Q_master_delete WHERE hall_name='$_hall' AND Masa='$_table'");
$master_delete = odbc_num_rows($master_delete);

$rezerve = odbc_num_rows(odbc_exec($db,"SELECT * FROM Q_Mars WHERE hall_name='$_hall' AND Masa='$_table'"));

if(!$table['waiter']){
  $status = 0;
} elseif($master_delete > 0){
  $status = 1;
} elseif($table['adisyaz'] == 0){
  $status = 2;
} elseif($table['adisyaz'] == 1){
  $status = 3;
} else {
  $status = 99;
}


$table = array(
  "hall" => $table['hall_name'],
  "name" => $table['Masa'],
  "customer" => $table['HESADI'],
  "subtotal" => $table['subtotal'],
  "status" => $status,
  "addition_no" => intval($table['Adsno'])>0 ? intval($table['Adsno']) : '',
  "rezerve" => $rezerve,
  "waiter" => $table['waiter'],
  "time" => $table['gelissaat']
);


$query = odbc_exec($db,"SELECT * FROM Q_master_detay WHERE hall_name='$_hall' AND masa='$_table' ORDER BY sip_time DESC");

$items = array();
while($detail = odbc_fetch_array($query) ) {

  $items[] = array(
    "name" => utf8_encode($detail['ReceteAdi']),
    "quantity" => $detail['adet'],
    "total" => $detail['total'],
    "extra" => $detail['ozellik'],
    "total" => $detail['total'],
    "time" => $detail['sip_time'],
    "waiter" => utf8_encode($detail['waiter']),
  );
}

$response = array("successful" => true, "table" => $table, "items" => $items);

utf8_print($response);

?>
