<?php

require_once('config.php');

$_hall = $_POST['hall'];
$_table = $_POST['table'];
$_hall_to = $_POST['hall_to'];
$_table_to = $_POST['table_to'];

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


$table = odbc_exec($db,"SELECT * FROM Q_master_table WHERE hall_name='$_hall' AND Masa='$_table'");
$table = odbc_fetch_array($table);

$table_to = odbc_exec($db,"SELECT * FROM Q_master_table WHERE hall_name='$_hall_to' AND Masa='$_table_to'");
$table_to = odbc_fetch_array($table_to);

if($_hall == $_hall_to && $_table == $_table_to){
  $response = array("successful" => false, "error_title" => "Masa transfer hatası!", "error_message" => "Masa kendisine aktarılamaz.");
  echo json_encode($response);
  exit;
}

if(!$table || !$table_to){
  $response = array("successful" => false, "error_title" => "Masa transfer hatası!", "error_message" => "Masa bulunamadı.");
  echo json_encode($response);
  exit;
}

if($table['Adsno'] > 0 && $table_to['Adsno'] > 0){
  $response = array("successful" => false, "error_title" => "Masa transfer hatası!", "error_message" => "İki masada da adisyon var birleştirilemez.");
  echo json_encode($response);
  exit;
}

if($table['HESADI'] != '' && $table_to['HESADI'] != ''){
  $response = array("successful" => false, "error_title" => "Masa transfer hatası!", "error_message" => "İki masada da cari var birleştirilemez.");
  echo json_encode($response);
  exit;
}

$Adsno = ($table['Adsno'] > 0 ? $table['Adsno'] : $table_to['Adsno']);
$HESADI = ($table['HESADI'] != '' ? $table['HESADI'] : $table_to['HESADI']);

$adisyaz = ($table_to['waiter'] == '' ? $table['adisyaz'] : ($Adsno > 0 ? 1:0));

$total = $table['total'] + $table_to['total'];
$subtotal = $table['subtotal'] + $table_to['subtotal'];

$transfer = odbc_exec($db, "UPDATE Q_master_table SET
            HESADI='$HESADI',
            waiter='$table[waiter]',
            waipwd='$table[waipwd]',
            gelissaat='$table[gelissaat]',
            total='$total',
            subtotal='$subtotal',
            pax='$table[pax]',
            adisyaz='$adisyaz',
            Adsno='$Adsno',
            servpercent='$table[servpercent]',
            servtotal='$table[servtotal]',
            indpercent='$table[indpercent]',
            indtotal='$table[indtotal]',
            manser='$table[manser]',
            manindyuz='$table[manindyuz]'
          WHERE hall_name='$_hall_to' AND Masa='$_table_to';

          UPDATE Q_master_table SET
              HESADI='',
              waiter='',
              waipwd='',
              gelissaat='',
              total='0',
              subtotal='0',
              pax='0',
              adisyaz='0',
              Adsno='0',
              servpercent='0',
              servtotal='0',
              indpercent='0',
              indtotal='0',
              manser='0',
              manindyuz='0'
            WHERE hall_name='$_hall' AND Masa='$_table';

            UPDATE Q_master_detay SET hall_name='$_hall_to', Masa='$_table_to' WHERE hall_name='$_hall' AND Masa='$_table';

            UPDATE Q_master_delete SET hall_name='$_hall_to', masa='$_table_to' WHERE hall_name='$_hall' AND masa='$_table';

            UPDATE Q_Mars SET hall_name='$_hall_to', Masa='$_table_to' WHERE hall_name='$_hall' AND Masa='$_table';");


if($transfer)
  $response = array("successful" => true);
else
  $response = array("successful" => false);

utf8_print($response);

?>
