<?php
include_once 'init-lite.php';
?>
<?php

if (!isset($_REQUEST['oid']))
    die('xxx');

$HaveOrderID = $_REQUEST['oid'];

if ($debug) console_log("+菜 :  HaveOrderID :  " . $HaveOrderID);
$result="";
//$sql_Update="Update Orders set ShipCity='' where Orders.OrderID=".$HaveOrderID;
//$orders->exec($sql_Update); 
//********如果有菜单号，调入资料  Start
//$orders->beginTransaction();
    $SQL_UserCode    = $_REQUEST['UserCode'];
if (isset($_REQUEST['UserCode'])) {
    $SQL_OrderID     = $HaveOrderID;
    $SQL_ProductName = $_REQUEST['ProductName'];
    $SQL_UnitPrice   = $_REQUEST['UnitPrice'];
    $SQL_Discount    = '0';
    $SQL_Quantity    = $_REQUEST['Quantity'];
    $SQL_QuantityOrg = $_REQUEST['QuantityOrg'];
    
    $is_existProduct        = 0;
    $query_RecordsetProduct = "SELECT  sum(Quantity) as QuantityTotal  FROM [Order Details] where OrderID=" . $HaveOrderID . " and  UserCode = '" . $SQL_UserCode . "' ";
	//if ($debug) console_log("+菜 SELECT  :    " .  $query_RecordsetProduct);
 	$values                 = $orders->query($query_RecordsetProduct);
    if (is_iterable($values)) {
        foreach ($values as $row_RecordsetProduct) {
			if (!empty($row_RecordsetProduct['QuantityTotal'])) $is_existProduct = 1;
        }
    }
		 if ($is_existProduct) {
            //if ($debug) console_log("+菜 update :  UserCode :  " . $SQL_UserCode . " x " . GetSQLValueString($SQL_Quantity, "double"));
			
            //'存在，如果数量=0，删除
            //'否则读取 菜单的默认action=厨单/酒吧单，更新到销售表，原本的action数量叠加。
            //'action数量在打印后自动清空

			if ($SQL_Quantity=='0'){
				$updateSQL       = sprintf("delete from [Order Details] where OrderID=%s and UserCode=%s",
				 GetSQLValueString($SQL_OrderID, "long"), 
				 GetSQLValueStringGBK($SQL_UserCode, "text")
				 );
			}else{
				$actionstr="Select TOP(1) case when Categories.PrintCookTicket=1 then 'cook' when Categories.PrintCookTicketII=1 then 'cookII' when Categories.PrintCookTicket2=1 then 'bar' else '' end as [Action]  
 FROM     Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID  where UserCode=".GetSQLValueStringGBK($SQL_UserCode, "text");
				$action=getSQLresult($actionstr);
				$updateSQL       = sprintf("UPDATE [Order Details] SET Quantity=%s,[PrnQty] = [PrnQty] + %s,[Action]=%s WHERE UserCode=%s and OrderID=%s",
				GetSQLValueString($SQL_Quantity, "double"), 
				GetSQLValueString(($SQL_Quantity-$SQL_QuantityOrg), "double"),
				GetSQLValueStringGBK($action, "text"), 
				GetSQLValueStringGBK($SQL_UserCode, "text"), 
				GetSQLValueString($SQL_OrderID, "long"
				));
    	    }
		    if ($debug) console_log("+菜 update  :    " .   $updateSQL);
            $orders->exec($updateSQL);    
    
		 }else {
         if ($debug) console_log("+菜 INSERT :  UserCode :  " . $SQL_UserCode . " x " . $SQL_Quantity);
/*        $insertSQL = sprintf("INSERT INTO [Order Details] 
			([OrderID], [ProductID], [UserCode], [ProductName],[UnitPrice], [Discount],[Quantity])
			 VALUES  (%s, %s, %s, %s, %s,%s, %s)", 
			 GetSQLValueString($SQL_OrderID, "long"), 
			 GetSQLValueStringGBK($SQL_UserCode, "text"), 
			 GetSQLValueStringGBK($SQL_UserCode, "text"), 
			 GetSQLValueStringGBK($SQL_ProductName, "text"), 
			 GetSQLValueString($SQL_UnitPrice, "double"), 
			 GetSQLValueString($SQL_Discount, "int"), 
			 GetSQLValueString($SQL_Quantity, "double")
			 );*/
 
		$insertSQL = sprintf("INSERT INTO [Order Details]
           ([OrderID]
           ,[ProductID]
           ,[UserCode]
           ,[ProductName]
           ,[UnitPrice]
           ,[Quantity]
           ,[Discount]
           ,[Tax]
           ,[PrnQty]
           ,[Action]
			) 
			Select TOP(1)    
			%s as OrderID
			,Products.ProductID
			,Products.UserCode
			,Products.ProductName
			,Products.UnitPrice
			,%s as Quantity
			,case when Products.Discount is null then 0 else Products.Discount end
			,Products.Tax
			,%s as PrnQty
			 ,case when Categories.PrintCookTicket=1 then 'cook' when Categories.PrintCookTicketII=1 then 'cookII' when Categories.PrintCookTicket2=1 then 'bar' else '' end as [Action]  
			FROM      Products INNER JOIN
                Categories ON Products.CategoryID = Categories.CategoryID  where UserCode=%s",
				GetSQLValueString($SQL_OrderID, "long"), 
				GetSQLValueString($SQL_Quantity, "double"),
				GetSQLValueString(($SQL_Quantity-$SQL_QuantityOrg), "double"),
				GetSQLValueStringGBK($SQL_UserCode, "text")
				);
		//die($insertSQL);
        if ($debug) console_log("+菜 INSERT  :    " .   $insertSQL );
        $orders->exec($insertSQL);
    }
	
	//返回Action供行调用ajax刷新
	$SQlStr = "SELECT top 1 [Action] as a  FROM [Order Details]  where  OrderID=" . $HaveOrderID . " and  UserCode = '" . $SQL_UserCode . "' ";
	$values = $orders->query($SQlStr);
	if (is_iterable($values)) {
		foreach ($values as $pSQlStr) {
			$pSQlStr['a']=loadFromMssql($pSQlStr['a']);
			$result= $pSQlStr['a'];
			//if ($debug) console_log("action :  " . $result);
		}
	}
   
}
//********如果有菜单号，调入资料  END   

$SQlStr = "SELECT top 1 sum(UnitPrice*Quantity)  FROM [Order Details]  where OrderID='$HaveOrderID'  group by   OrderID ";
$values = $orders->query($SQlStr);
if (is_iterable($values)) {
    foreach ($values as $pSQlStr) {
        $theTotalValue = $pSQlStr[0];
        $result =$result.';'. number_format($theTotalValue, 2);
        //if ($debug) console_log("theTotalValue :  " . $theTotalValue);
    }
}

echo $result;
//$orders->commit();
/* 提交更改 */
?>