<?php

require_once('config.php');

$_hall = $_POST['hall'];
$_table = $_POST['table'];
$_product = $_POST['product'];
$_quantity = floatval($_POST['quantity']);
$_waiter = $_POST['waiter'];
$_extra_price = floatval($_POST['extra_price']);
$_extra_text = trim($_POST['extra_text']);
$_reserve = trim($_POST['reserve']);
$_portion = trim($_POST['portion']);

$time = date("H:i:s");
$day = date("Y-d-m 00:00:00:000");

$admin = odbc_fetch_array(odbc_exec($db,"SELECT * FROM Q_Admin WHERE hall_name='$_hall'"));
switch($_portion){
  case "az":
    $oran = $admin['azyuzde']>0 ? $admin['azyuzde'] : 60;
    $sub_name = "(AZ) ";
  break;

  case "bucuk":
    $oran = 150;
    $sub_name = "(1.5) ";
  break;

  case "duble":
    $oran = $admin['dubleyuzde']>0 ? $admin['dubleyuzde'] : 200;
    $sub_name = "(D) ";
  break;

  default:
    $oran = 100;
    $sub_name = "";
}
$carpan = $oran/100;

$response = array("successful" => false);

odbc_exec($db, "UPDATE Q_SETUP SET SILME_NO = SILME_NO+1");
$silme_no = odbc_fetch_array(odbc_exec($db,"SELECT SILME_NO FROM Q_SETUP"));
$silme_no = intval($silme_no['SILME_NO']);

$product = odbc_fetch_array(odbc_exec($db,"SELECT * FROM Q_ITEMS WHERE KODU='$_product'"));
$group = odbc_fetch_array(odbc_exec($db,"SELECT * FROM Q_Grups WHERE GRUP='$product[GRUP]'"));
$admin_second = odbc_fetch_array(odbc_exec($db,"SELECT * FROM Q_Admin_Second WHERE hall_name='$_hall' AND GRUP='$product[GRUP]'"));
$waiter = odbc_fetch_array(odbc_exec($db,"SELECT * FROM Q_Userkontrol WHERE pwd='$_waiter'"));

odbc_exec($db, "UPDATE Q_master_table SET waiter='$waiter[username]', waipwd='$waiter[pwd]', gelissaat='$time', today='$day' WHERE hall_name='$_hall' AND Masa='$_table' AND ((waiter='' AND waipwd='') OR (waiter IS NULL AND waipwd IS NULL)) ");

$product_name = $sub_name.$product['ADI'];

$add_to_delete = odbc_exec($db, "INSERT INTO Q_master_delete (hall_name,masa,ozellik,ReceteKodu,ReceteAdi,adet,category,department,gndr,iade,pda,sil,articel_id) VALUES
          ('$_hall','$_table','$_extra_text','$product[KODU]','$product_name','$_quantity','$group[CATEGORY]','$admin_second[DEPARTMENT]', 'gonder', '0', '0', '$silme_no', '$admin_second[articel_id]')");

if($add_to_delete){
  $total = ($product['SFIYAT'] + $_extra_price) * $carpan * $_quantity;
  $add_to_detay = odbc_exec($db, "INSERT INTO Q_master_detay (hall_name,masa,ozellik,ReceteKodu,ReceteAdi,categori,department,mal,bfiyat,adet,total,yaz,payfor,sip_time,sil,PC_NAME,PC_USER,fatura,waiter) VALUES
            ('$_hall','$_table','$_extra_text','$product[KODU]','$product_name','$group[CATEGORY]','$admin_second[DEPARTMENT]', '$product[MALIYET]', '$product[SFIYAT]', '$_quantity', '$total', '0', '0', '$time', '$silme_no', 'MOBIL',
              '$waiter[username]','$group[fatura]', '$waiter[username]')");

  if($add_to_detay){

	odbc_exec($db, "INSERT INTO Q_bugs (GUN,ZAMAN,garson,username,hall_name,masa,total,sebep,URUN_ADI,ADET,deta,PCNAME) VALUES
	        ('$day','$time', '$waiter[username]', '$waiter[username]','$_hall','$_table', '$total','Ekle','$product_name', '$_quantity','Ekle','MOBIL')");

    odbc_exec($db, "UPDATE Q_master_table SET total = total+$total, subtotal = subtotal+$total WHERE hall_name='$_hall' AND Masa='$_table'");

    $response = array("successful" => true);
  }
}

// $products = array();
// while($group = odbc_fetch_array($groups_query) ) {
//
//   $products_query = odbc_exec($db,"SELECT KODU, ADI, GRUP, SFIYAT FROM Q_ITEMS WHERE ADI!='' AND GRUP='$group[GRUP]'");
//   $_products = array();
//   while($product = odbc_fetch_array($products_query) ) {
//     $_products[] = array(
//       "code" => $product['KODU'],
//       "name" => utf8_encode($product['ADI']),
//       "group" => $product['GRUP']
//     );
//   }
//
//   $products[] = array(
//     "name" => $group['GRUP'],
//     "items" => $_products
//   );
//
// }

// $response = array("successful" => true, "categories" => $products);
utf8_print($response);

?>
