<?php
include "token.php";
//----打印 ------

//index.php 按提交按钮
//：将finorder.php 的表单提交 <form id="formfinorder">
//提交  prnOrderID  到init.php处理

if (!empty($_POST['prnOrderID'])) {
    $SQlStr = "SELECT top 1 sum(UnitPrice*Quantity)  FROM [Order Details]  where OrderID='".$_POST['prnOrderID']."'  group by   OrderID ";
    $values = $orders->query($SQlStr);
    if (is_iterable($values)) {
        foreach ($values as $pSQlStr) {
            $theTotalValue = $pSQlStr[0];
            //echo $theTotalValue;
        }
        //return $theValue;
    }
    
    if ($theTotalValue < 0 && date('Hi') > 1200 && date('Hi') < 1400)
        echo '<script type="text/javascript">alert("在12：00-14：00期间，最低消费为5欧元。");</script>在12：00-14：00期间，最低消费为5欧元。';
    else {
        
		$somethins=$_POST;
		$somethins['CompanyNameG']=$CompanyName;
		$somethins['CFG_Printer']=$CFG_Printer;
		$somethins['CFG_PrinterCook']=$CFG_PrinterCook;
		$somethins['CFG_PrinterCookII']=$CFG_PrinterCookII;
		$somethins['CFG_PrinterBar']=$CFG_PrinterBar;
		$somethins['GUID']=$GUID;
		$strSQLCfg  = "SELECT ID,Descr,REPLACE(keyname,'T-','' ) as keyname,kevalue FROM configWeb"; 
		$valuesCfg = $orders->query($strSQLCfg);
		if (is_iterable($valuesCfg)) {
		foreach ($valuesCfg as $p) {
			$p['keyname']    = loadFromMssql($p['keyname']);
			$p['kevalue']    = loadFromMssql($p['kevalue']);
			//echo $p['keyname'] .'  ' . $p['kevalue'].'<br/>';
			$fruit1 = array($p['keyname'] => $p['kevalue'] );  
			$somethins = array_merge_recursive($somethins,$fruit1);
			}
		}
 				 
		//print_r($somethins);  
		//die();
			    
        switch ($somethins['prntype']) {
			case "auto":
				$strSQLTitles  = "SELECT top 1 CompanyName,Address,City,PostalCode,Phone,wechat,ShipType,PaymentType FROM Customers where CustomerID=(SELECT CustomerID FROM Orders where OrderID=".$somethins['prnOrderID'].")"; 
				$valuesTitles = $orders->query($strSQLTitles);
				if (is_iterable($valuesTitles)) {
				foreach ($valuesTitles as $p) {
					$p['CompanyName']    = loadFromMssql($p['CompanyName']);
					$p['Address']    = loadFromMssql($p['kevalue']);
					$p['City']    = loadFromMssql($p['City']);
					$p['PostalCode']    = loadFromMssql($p['PostalCode']);
					$p['Phone']    = loadFromMssql($p['Phone']);
					$p['wechat']    = loadFromMssql($p['wechat']);
					$p['ShipType']    = loadFromMssql($p['ShipType']);
					$p['PaymentType']    = loadFromMssql($p['PaymentType']);
				 
					if (strlen($p['CompanyName'])>0) $somethins = array_merge_recursive($somethins,array('C_CompanyName' => $p['CompanyName'] ));
					if (strlen($p['Address'])>0) $somethins = array_merge_recursive($somethins,array('C_Address' => $p['Address'] ));
					if (strlen($p['City'])>0) $somethins = array_merge_recursive($somethins,array('C_City' => $p['City'] ));
					if (strlen($p['PostalCode'])>0) $somethins = array_merge_recursive($somethins,array('C_PostalCode' => $p['PostalCode'] ));
					if (strlen($p['Phone'])>0) $somethins = array_merge_recursive($somethins,array('C_Phone' => $p['Phone'] ));
					if (strlen($p['wechat'])>0) $somethins = array_merge_recursive($somethins,array('C_wechat' => $p['wechat'] ));
					if (strlen($p['ShipType'])>0) $somethins = array_merge_recursive($somethins,array('C_ShipType' => $p['ShipType'] ));
					if (strlen($p['PaymentType'])>0) $somethins = array_merge_recursive($somethins,array('C_PaymentType' => $p['PaymentType'] ));
 				}
				}
				//自动判断，循环打印三种单据
				$sql_auto = "SELECT   Action FROM [Order Details] WHERE   (OrderID = ".$somethins['prnOrderID'].") GROUP BY Action HAVING (Action <> N'') AND (COUNT(*) > 0) Order by Action desc";
				$valuesAction = $orders->query($sql_auto);
				$is_print=0;
				if (is_iterable($valuesAction)) {
					foreach ($valuesAction as $p) {
						$p['Action']    = loadFromMssql($p['Action']);
						$Action=$p['Action'];
						$ticket_txt .= printoneBig($Action,$somethins);
						/*echo '<script>alert("'.$p['Action'].'")</script>';*/
						//暂停 2 秒
						//sleep(2);
						$is_print=1;
					}
					if (!$is_print) $ticket_txt   = printone("",$somethins);
				}else{
				}

				break;
			default:
                $ticket_txt   = printone($somethins['prntype'],$somethins); //prnBarTicket	,	prnCookTicket	,	prnDishTicket
		}
    }
    $showMenu = 'printed';
}
function getNamePart($TProductName , $firstPart) {
		$strarr = explode("*",$TProductName);
		if (count($strarr) > 1){
			$TProductName = $strarr[$firstPart];
		}
		//die(count($strarr).$TProductName);
        return $TProductName;
}
function printoneBig($prntype,$somethins){
 		/*echo '<script>alert("'.$prntype.'")</script>';*/
        //global $orders;
		$CFG_Printer=$somethins['CFG_Printer'];
		$CFG_PrinterCook=$somethins['CFG_PrinterCook'];
		$CFG_PrinterCookII=$somethins['CFG_PrinterCookII'];
		$CFG_PrinterBar=$somethins['CFG_PrinterBar'];
		$CompanyName=$somethins['CompanyNameG'];
		$GUID=$somethins['GUID'];

		global $dbconnection;
		global $dbuser;
		global $dbpassword;
		global $CFG_PrintEnable; 
		$orders = new PDO($dbconnection,$dbuser,$dbpassword);
		//$orders->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  //反射错误码和错误信息。 
        $PrintType = '';
        $Printer    = $CFG_Printer;
        switch ($prntype) {
            case "dish":
                $Printer = $CFG_PrinterCook;
				$PrintType = "催菜";
                break;
            case "cook":
                $Printer = $CFG_PrinterCook;
				$PrintType = "厨单";
                break;
            case "cookII":
                $Printer = $CFG_PrinterCookII;
				$PrintType = "厨单II";
                break;
            case "bar":
                $Printer = $CFG_PrinterBar;
				$PrintType = "酒水单";
                break;
            case "help":
                $Printer = $CFG_PrinterBar;
				$PrintType = "请求服务员帮助";
                break;
            case "god":
                $Printer = $CFG_PrinterBar;
				$PrintType = "我要上天";
                break;
            default:
                $Printer = $CFG_Printer;
				$PrintType = "整单";

		}
		$ArrPrinter		= explode(":", $Printer);
		$PrinterReal	=(count($ArrPrinter)>0 && !empty($ArrPrinter[0])) ? $ArrPrinter[0] : $Printer;
		$PrinterPort	=(count($ArrPrinter)>1 && !empty($ArrPrinter[1])) ? $ArrPrinter[1] : '9100';
		$PrinterLineWords	=	(count($ArrPrinter)>2 && !empty($ArrPrinter[2])) ? $ArrPrinter[2] : 42 ; //打印机每行字数加权  40 / 42 /48 减去 
		
		$ticket_txt='';

        $input      = '='; //來源字串
        $pad_length =  $PrinterLineWords; //要補到幾個字
        $pad_type   = STR_PAD_LEFT; //從哪裡開始補 1.左右都補:STR_PAD_BOTH ,2.由左邊開始補:STR_PAD_LEFT , 3.由右邊開始補:STR_PAD_RIGHT
        $input      = str_pad($input, $pad_length, $input, STR_PAD_LEFT) . '<br>';
        
        $input2 = '-'; //來源字串
        $input2 = str_pad($input2, $pad_length, $input2, STR_PAD_LEFT) . '<br>';
        
        $prnOrderID = $somethins['prnOrderID'];
       
        $head = $somethins["PrintStrReceipt"] . $prnOrderID;
        
        $head2 = '';
        $ead3  = '';
        $Guid  = '';
        $CompanyName = '';
		
         if (!empty($somethins['head'])) $head = $somethins['head'];
        $head = str_pad($head, $pad_length, ' ', STR_PAD_BOTH) . '<br>';
		console_log("   C_CompanyName :  " . $somethins['C_CompanyName'] );
	
	   if (!empty($somethins['C_CompanyName'])) {
            $head2 = $somethins["PrintStrDesk"] . $somethins['C_CompanyName'] . '<br>';
            if (!empty($somethins['C_Address']))
                $head2 .= $somethins['C_Address'];
            if (!empty($somethins['C_City']))
                $head2 .= ',' . $somethins['C_City'];
            if (!empty($somethins['C_PostalCode']))
                $head2 .= ',' . $somethins['C_PostalCode'];
            if (!empty($somethins['C_Phone']) && $somethins['C_Phone'] != $somethins['C_CompanyName'])
                 $head2 .= '<br>' .$somethins["PrintStrTel"]. $somethins['C_Phone'];
            if (!empty($somethins['C_wechat']))
                $head2 .= '  微信: ' . $somethins['C_wechat'];
			
        } elseif (!empty($somethins['head2']))
            $head2 = $somethins['head2'];
        elseif (!empty($GUID) && empty($CompanyName))
            $head2 = $somethins["PrintStrDesk"] . $GUID. '<br>';
        elseif (!empty($CompanyName))
            $head2 = $somethins["PrintStrDesk"] . $CompanyName. '<br>';
  
        //$head2     = str_pad($head2, $pad_length, ' ', STR_PAD_BOTH) . '<br>';
        $datetime1 = date('H:i:s');
        $head3     =  $somethins["PrintStrDate"] . $datetime1;
        $head3     = str_pad($head3, $pad_length, ' ', STR_PAD_RIGHT) . '<br>';
        
        $titleBig .= $head;
        $title = $input;
        $title .= $head2 . $head3 . '<br>';

        $printtxt = '';
        
        $inputSP = ' '; //來源字串
		//$DescripcionLength = $PrinterLineWords - 8 - 8 ;
		//$printtxt .= str_pad('Cod ', 8, $inputSP, STR_PAD_RIGHT);
		//$printtxt .= str_pad('Can ', 8, $inputSP, STR_PAD_BOTH);
		//$printtxt .= str_pad('Descripcion', $DescripcionLength , $inputSP, STR_PAD_RIGHT);
		//$printtxt .= '<br>';
        $printtxt .= $input2;
        
        $PrinterCookSplit   = false;
        $prnTitle  = $printtxt;
        $printtxt = "";
        $printtxtCookSplit  = "";

        if (!empty($somethins['PrinterCookSplit'])){
            $PrinterCookSplit = $somethins['PrinterCookSplit'];
		}
        //echo "prnOrderID:".$prnOrderID;
        $TotalRealTime = 0;
        
	    if (!empty($prntype)) {
            $SQlStrQuantity = " [PrnQty] as ";
	    }
	    $SQlStrLIST = sprintf("select  UserCode,ProductName 
        + case when Remark is null or Remark='' then '' else + '#' + Remark end 
        + case when [action] is null or [action]='cook' or [action]='bar' or [action]='' then '' else + '' + [action] end
         as ProductName, %sQuantity,
		 UnitPrice  from [Order Details] where OrderID= %s" ,$SQlStrQuantity , $somethins['prnOrderID']);
        if (!empty($prntype)) {
            $SQlStrLIST .= sprintf(" and [Action]='%s'" , $prntype );
        }
        $SQlStrLIST .= "  Order by DATALENGTH(UserCode),UserCode";
 
         $SQlStrLIST = sprintf("SELECT   [Order Details].UserCode, [Order Details].ProductName + CASE WHEN [Order Details].Remark IS NULL OR
                [Order Details].Remark = '' THEN '' ELSE + '#' + [Order Details].Remark END + CASE WHEN [action] IS NULL OR
                [action] = 'cook' OR
                [action] = 'bar' OR
                [action] = '' THEN '' ELSE + '' + [action] END AS ProductName, [Order Details].%sQuantity, 
                [Order Details].UnitPrice, Categories.PrintGroup
FROM      Categories RIGHT OUTER JOIN
                Products ON Categories.CategoryID = Products.CategoryID RIGHT OUTER JOIN
                [Order Details] ON Products.ProductID = [Order Details].ProductID
WHERE   [Order Details].OrderID = %s ", $SQlStrQuantity, $somethins['prnOrderID']);
        if (!empty($prntype)) {
            $SQlStrLIST .= sprintf(" and [Action]='%s'" , $prntype );
        }
        $SQlStrLIST .= " ORDER BY  Categories.PrintGroup,DATALENGTH([Order Details].UserCode), [Order Details].UserCode";

        $GroupLine  = "";

        //$printtxt .= $SQlStrLIST.'<br/>';
		//echo('<hr/>');
		try{
        $values = $orders->query($SQlStrLIST);
        if (is_iterable($values)) {
            foreach ($values as $p) {
                 //打印分组
                if (strlen($GroupLine) > 0 && $p['PrintGroup'] != $GroupLine ){
                    $printtxt .= "  " . '<br>';
				}
                $GroupLine = $p['PrintGroup'];
				
				
                $p['UserCode']    = loadFromMssql($p['UserCode']);
                $p['ProductName'] = loadFromMssql($p['ProductName']);
				$Line1++;
                $printtxt .= str_pad( $p['UserCode'] . " x ", 6, $inputSP, STR_PAD_RIGHT);
                if (intval($p['Quantity']) == $p['Quantity'])
                    $printtxt .= str_pad(number_format($p['Quantity'], 0), 4, $inputSP, STR_PAD_BOTH) ;
                else
                    $printtxt .= str_pad(number_format($p['Quantity'], 1), 4, $inputSP, STR_PAD_BOTH) ;
                $TProductName = $p['ProductName'];
               if (isset($somethins['PrintCookTicketOnlyLastPart']) &&  $somethins['PrintCookTicketOnlyLastPart'] == true) {
                    //厨单只出现 * 号后的名字
                    $TProductName = getNamePart($TProductName, 1);
                }elseif (isset($somethins['PrintCookTicketOnlyFirstPart']) &&  $somethins['PrintCookTicketOnlyFirstPart'] == true) {
                    //厨单只打印*号前的文字
                    $TProductName = getNamePart($TProductName, 0);
				}
				
		 
                $printtxt .= $TProductName;
                $printtxt .= '<br>';

                if (!empty($prntype)) { 
					//打印后取消对应标记
                    $updateSQL = sprintf("update [Order Details] set Action='',PrnQty=0 where OrderID=%s and [Action]='%s'" , $somethins['prnOrderID'],$prntype);
                    //echo $updateSQL."<hr>";
                    $orders->exec($updateSQL);
                }
				
				//PrinterCookSplit
                if ($PrinterCookSplit) {

                    $printtxt = $prnTitle . $printtxt . $input2;

                    $totalCookSplit  = "";
                    $footCookSplit  = "";

                    $footCookSplit = "打印类型：" . $PrintType  . $somethins["Remark"]  . '<br>';

                    $ticket_txt = "$titleBig $title $printtxt $totalCookSplit $footCookSplit";
     

                    try{
                        if ($CFG_PrintEnable == 1){
							prn_txt($titleBig.$title,'' , '', '', $printtxt.$footCookSplit, $PrinterReal,$PrinterPort);
						}else{
                            $printtxtCookSplit .= $ticket_txt;
						}
                   }catch(Exception $e) {
  	  			   }


                    $printtxt = "";

			   } //PrinterCookSplit
			   
			   
           }
        }
		}catch (PDOException $e){
  		  die('错误的原因:'.$e->getMessage());
 
		}
		
		
		 if ($PrinterCookSplit == false) {		
				$printtxt  = $prnTitle . $printtxt . $input2;
		  
				$foot = "打印类型： $PrintType " . $somethins['Remark'];
		 
				//$orders->query($sql_new);
				$ticket_txt = "$titleBig $title $printtxt $total $foot";
				//die($titleBig.$title);
		
				$arr = array(
					'titleBig' => $titleBig,
					'title' => $title,
					'printtxt' => $printtxt,
					'total' => $total,
					'foot' => $foot,
					'CFG_Printer' => $PrinterReal
				);
				/*echo "<script>ajaxprint('" . json_encode($arr) . "');</script>";*/
				//调用打印接口
				if ($CFG_PrintEnable==1) prn_txt('','' , '', '',$titleBig.$title.$printtxt.$foot, $PrinterReal,$PrinterPort,false,false,$somethins["PrintCookFontSizeBig"]);
				if (!empty($somethins['PrinterCopyToCookII']) && $somethins['PrinterCopyToCookII']==1 && $Printer == $CFG_PrinterCook) {
					//厨单复制打印到厨单打印机II
					$ArrPrinter		= explode(":", $CFG_PrinterCookII);
					$PrinterReal	=(count($ArrPrinter)>0 && !empty($ArrPrinter[0])) ? $ArrPrinter[0] : $Printer;
					$PrinterPort	=(count($ArrPrinter)>1 && !empty($ArrPrinter[1])) ? $ArrPrinter[1] : '9100';
					if ($CFG_PrintEnable==1) prn_txt('','' , '', '', $titleBig.$title.$printtxt.$foot, $PrinterReal,$PrinterPort,false,false,$somethins["PrintCookFontSizeBig"]);
					$ticket_txt .= "-厨单复制打印到厨单打印机II- <br> $titleBig $title $printtxt $total $foot";

				}
		 }else{
			 
		 }
		return $ticket_txt.'<hr/>';
}

function printone($prntype,$somethins){
 		/*echo '<script>alert("'.$prntype.'")</script>';*/
        //global $orders;
		$CFG_Printer=$somethins['CFG_Printer'];
		$CFG_PrinterCook=$somethins['CFG_PrinterCook'];
		$CFG_PrinterCookII=$somethins['CFG_PrinterCookII'];
		$CFG_PrinterBar=$somethins['CFG_PrinterBar'];
		$CompanyName=$somethins['CompanyNameG'];
		$GUID=$somethins['GUID'];

		global $dbconnection;
		global $dbuser;
		global $dbpassword;
		global $CFG_PrintEnable; 
		$orders = new PDO($dbconnection,$dbuser,$dbpassword);
		//$orders->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  //反射错误码和错误信息。 
 
        $PrintType = '';
        $Printer    = $CFG_Printer;
        switch ($prntype) {
            case "dish":
                $Printer = $CFG_PrinterCook;
				$PrintType = "催菜";
                break;
            case "cook":
                $Printer = $CFG_PrinterCook;
				$PrintType = "厨单";
                break;
            case "cookII":
                $Printer = $CFG_PrinterCookII;
				$PrintType = "厨单II";
                break;
			case "bar":
                $Printer = $CFG_PrinterBar;
				$PrintType = "酒水单";
                break;
           default:
                $Printer = $CFG_Printer;
				$PrintType = "整单";

		}
		$ArrPrinter		= explode(":", $Printer);
		$Printer		=(count($ArrPrinter)>0 && !empty($ArrPrinter[0])) ? $ArrPrinter[0] : $Printer;
		$PrinterPort	=(count($ArrPrinter)>1 && !empty($ArrPrinter[1])) ? $ArrPrinter[1] : '9100';
		$PrinterLineWords	=	(count($ArrPrinter)>2 && !empty($ArrPrinter[2])) ?  (int) $ArrPrinter[2] : 42 ; //打印机每行字数加权  40 / 42 /48 减去 
 
        $ticket_txt='';

        $input      = '='; //來源字串
        $pad_length = $PrinterLineWords; //要補到幾個字
        $pad_type   = STR_PAD_LEFT; //從哪裡開始補 1.左右都補:STR_PAD_BOTH ,2.由左邊開始補:STR_PAD_LEFT , 3.由右邊開始補:STR_PAD_RIGHT
        $input      = str_pad($input, $pad_length, $input, STR_PAD_LEFT) . '<br>';
        
        $input2 = '-'; //來源字串
        $input2 = str_pad($input2, $pad_length, $input2, STR_PAD_LEFT) . '<br>';
          
        $head = 'TICKET TAKE AWAY' . "  id:" . $somethins['prnOrderID'];
        if (!empty($somethins['head']))
            $head = $somethins['head'];
        $head = str_pad($head, $pad_length, ' ', STR_PAD_BOTH) . '<br>';
        
        $head2 = 'TEL: 662 211 845  WeChat: momo61644';
        if (!empty($somethins['CompanyName'])) {
            $head2 = $somethins['CompanyName'] . '<br>';
            if (!empty($somethins['Address']))
                $head2 .= $somethins['Address'];
            if (!empty($somethins['City']))
                $head2 .= ',' . $somethins['City'];
            if (!empty($somethins['PostalCode']))
                $head2 .= ',' . $somethins['PostalCode'];
            if (!empty($somethins['Phone']))
                $head2 .= '<br>Tel: ' . $somethins['Phone'];
            if (!empty($somethins['wechat']))
                $head2 .= '  微信: ' . $somethins['wechat'];
        } elseif (!empty($somethins['head2']))
            $head2 = $somethins['head2'];
        elseif (!empty($GUID) && empty($CompanyName))
            $head2 = "WeChat: " . $GUID;
        elseif (!empty($CompanyName))
            $head2 = "WeChat: " . $CompanyName;
        $head2     = str_pad($head2, $pad_length, ' ', STR_PAD_BOTH) . '<br>';
        $datetime1 = date('Y-m-j H:i:s');
        $head3     = '下单时间 : ' . $datetime1;
        $head3     = str_pad($head3, $pad_length, ' ', STR_PAD_BOTH) . '<br>';
        
        $titleBig .= $head;
        $title .= $input;
        $title .= $head2;
        $title .= $head3 . '<br>';
        $printtxt = '';
        
        $inputSP = ' '; //來源字串
		$DescripcionLength = $PrinterLineWords   - 4 - 5 - 6 ;
        $printtxt .= str_pad($somethins["PrintStrQty"], 4, $inputSP, STR_PAD_BOTH);
        $printtxt .= str_pad($somethins["PrintStrDescription"], $DescripcionLength, $inputSP, STR_PAD_BOTH);
        $printtxt .= str_pad($somethins["PrintStrPrice"], 5, $inputSP, STR_PAD_BOTH);
        $printtxt .= str_pad($somethins["PrintStrSubtotal"], 6, $inputSP, STR_PAD_BOTH) ;
        $printtxt .= '<br>';
        $printtxt .= $input2;
        
         $TotalRealTime = 0;
        
	    if (!empty($prntype)) {
            $SQlStrQuantity = " [PrnQty] as ";
	    }
	    $SQlStrLIST = "select  UserCode,ProductName 
        + case when Remark is null or Remark='' then '' else + '#' + Remark end        
         as ProductName,Discount,
         " .$SQlStrQuantity ." Quantity,
		 UnitPrice  from [Order Details] where OrderID= " . $somethins[prnOrderID];
        if (!empty($prntype)) {
            $SQlStrLIST .= " and [Action]='" . $prntype . "'";
        }
        $SQlStrLIST .= " Order by UserCode";
 
        //$printtxt .= $SQlStrLIST.'<br/>';
		//echo('<hr/>');
		try{
        $values = $orders->query($SQlStrLIST);
        if (is_iterable($values)) {
            foreach ($values as $p) {
                $p['UserCode']    = loadFromMssql($p['UserCode']);
                $p['ProductName'] = loadFromMssql($p['ProductName']);
				$Line1++;
                $SubTotal         = $p['UnitPrice'] * $p['Quantity'] * (100 - $p['Discount']) / 100;
                $TotalRealTime = $TotalRealTime + $SubTotal;
                 
                if (intval($p['Quantity']) == $p['Quantity'])
                    $printtxt .= str_pad(number_format($p['Quantity'], 0), 4, $inputSP, STR_PAD_BOTH);
                else
                    $printtxt .= str_pad(number_format($p['Quantity'], 1), 4, $inputSP, STR_PAD_BOTH);
                $TProductName = $p['ProductName'];
								
               if (isset($somethins['PrintTicketOnlyFirstPart']) &&  $somethins['PrintTicketOnlyFirstPart'] == true) {
                    //帐单只打印*号前的文字
                    $TProductName = getNamePart($TProductName, 0);
                }elseif (isset($somethins['PrintTicketOnlyLastPart']) &&  $somethins['PrintTicketOnlyLastPart'] == true) {
                    //帐单只打印*号后的文字
                    $TProductName = getNamePart($TProductName, 1);
				}	
				
                if (strlen($TProductName) > $DescripcionLength) $printtxt .= mb_substr($TProductName, 0, $DescripcionLength, "utf-8");
                else $printtxt .= str_pad($TProductName, $DescripcionLength, $inputSP, STR_PAD_BOTH);
                $printtxt .= str_pad(number_format($p['UnitPrice'], 2), 5, $inputSP, STR_PAD_BOTH);
                $printtxt .= str_pad(number_format($SubTotal, 2), 6, $inputSP, STR_PAD_BOTH);
                $printtxt .= '<br>';
				if ($prntype=='') {
					//结账操作菜品数量
					$updateSQL = "update Products set limitQty =( SELECT top 1  Products.limitQty-[Order Details].Quantity FROM Products,  [Order Details]  WHERE Products.UserCode= [Order Details].UserCode and Products.limitQty<>-1 and  [Order Details].OrderID=" . $somethins['prnOrderID'] . " and [Order Details].UserCode='" . $p['UserCode'] . "') where UserCode='" . $p['UserCode'] . "'";
					$orders->exec($updateSQL);
				}
                if (!empty($prntype)) { 
					//打印后取消对应标记
                    $updateSQL = "update [Order Details] set Action='',PrnQty=0 where OrderID=" . $somethins['prnOrderID'] . " and [Action]='" . $prntype . "'";
                    //echo $updateSQL."<hr>";
                    $orders->exec($updateSQL);
                }
           }
        }
		}catch (PDOException $e){
  		  die('错误的原因:'.$e->getMessage());
 
		}
		$printtxt .= $input2;

        //$total=$input2.str_pad('TOTAL : ',40-strlen($TotalRealTime),$inputSP,STR_PAD_RIGHT) .$TotalRealTime.'<br><br>';
        $total    = 'TOTAL ' . number_format($TotalRealTime, 2) . '<br><br>';

	if ($prntype=='') {
        $ShipType = '';
        switch ($somethins['ShipType']) {
            case "takeaway":
                $ShipType = '外卖';
                break;
            case "inside":
                $ShipType = '店内';
                break;
            case "takemyself":
                $ShipType = '自提';
                break;
            default:
                $ShipType = '外卖';
        }
        $PaymentType = '';
        switch ($somethins['PaymentType']) {
            case "cash":
                $PaymentType = '现金';
                break;
            case "vor":
                $PaymentType = '饭票';
                break;
            case "other":
                $PaymentType = '其他';
                break;
            default:
                $PaymentType = '现金';
        }
        //订单类型： 外卖 店内 自提 
        //支付方式： 现金 饭票 其他
		

        $updateSQL = sprintf("UPDATE Orders SET Status=%s,Remark=%s,ShipType=%s,PaymentType=%s,OrderDate=%s,RequiredDate=%s,SubTotal=%s WHERE OrderID=%s", 
		GetSQLValueStringGBK('结账中', "text"), 
		GetSQLValueStringGBK($somethins['Remark'], "text"), 
		GetSQLValueStringGBK($somethins['ShipType'], "text"), 
		GetSQLValueStringGBK($somethins['PaymentType'], "text"), 
		GetSQLValueStringGBK($datetime1, "date"), 
		GetSQLValueStringGBK(date('Y-m-j'), "date"), 
		number_format($TotalRealTime, 2), 
		GetSQLValueStringGBK($somethins['prnOrderID'], "long"));
		$orders->exec($updateSQL);
        
 
        $foot = "订单类型： $ShipType<br>支付方式： $PaymentType<br>打印类型： $PrintType" . $somethins['Remark'];
}else{
		$foot = "打印类型： $PrintType " . $somethins['Remark'];
}
        //$orders->query($sql_new);
        $ticket_txt = "$titleBig $title $printtxt $total $foot";
        //die();

        $arr = array(
            'titleBig' => $titleBig,
            'title' => $title,
            'printtxt' => $printtxt,
            'total' => $total,
            'foot' => $foot,
            'CFG_Printer' => $Printer
        );
        /*echo "<script>ajaxprint('" . json_encode($arr) . "');</script>";*/
        //调用打印接口
		
	    if ($CFG_PrintEnable==1) prn_txt($titleBig, $title, $printtxt, $total, $foot, $Printer,$PrinterPort);
		return $ticket_txt.'<hr/>';		
	;
}
//----打印 end------
?>