<?php

session_start();
include("DMMYSQLG.php");
require_once("DF80000.php");

//IMPRIME CUENTA
//Consulta Datos Empresa
//
//update dgpemema a join (select cons_ven, sum(total_cd)as tot from dgpememd LEFT JOIN dginventp ON dgpememd.nparte1=dginventp.nparte1  where dgpememd.cons_ven='82000' and dgpememd.cancela='0') as b   on a.fol_ven=b.cons_ven   set a.sub_ven=b.tot, a.tot_ven=b.tot 
//actualizamos el total en dgpemema
//actualizamos el total en detalle

mysqli_query($con ,"update dgpememd  set total_cd=(can_ref*pre_uni), subtotal=(can_ref*pre_uni)	where cons_ven= '".$_REQUEST['bot']."' ");

mysqli_query($con ,"update dgpemema a join (select cons_ven, sum(total_cd)as tot from dgpememd LEFT JOIN dginventp ON dgpememd.nparte1=dginventp.nparte1  where dgpememd.cons_ven='".$_REQUEST['bot']."' and dgpememd.cancela='0') as b   on a.fol_ven=b.cons_ven   set a.sub_ven=b.tot, a.tot_ven=b.tot");

//echo $_SESSION['empresa']."asdasa";
$dt_empres = mysqli_fetch_array(mysqli_query($con ,"SELECT * FROM dxempres where cla_emp = '".$_SESSION['empresa']."'"));		
$cvdm = intval($dt_empres['cve_delmun']);
$cves = intval($dt_empres['cve_estado']);
//Consulta Datos Del/Mun
$sqld_encabezado = mysqli_query($con ,"SELECT * FROM dxdelmun where cve_delmun = '$cvdm'");
@$rowd_encabezado = mysqli_fetch_array($sqld_encabezado); 
//Consulta Datos Estado 
$sqle_encabezado = mysqli_query($con ,"SELECT * FROM dxestado where cve_estado = '$cves'");
@$rowe_encabezado = mysqli_fetch_array($sqle_encabezado);
@$resu1=mysqli_query($con ,"select * from dgpemema where fol_ven = '".$_REQUEST['bot']."'");
@$rego1=mysqli_fetch_array($resu1);
$dt_ger_cajerosj=mysqli_fetch_array(mysqli_query($con ,"SELECT * FROM dgger_cajerosj where pedido='".$rego1['fol_ven']."'"));
$upMema=mysqli_query($con ,"update dgpemema set por_des='".$_GET['cade']."', tip_por='".$_GET['tide']."' where fol_ven='".$_REQUEST['bot']."'");
$count_md=mysqli_fetch_array(mysqli_query($con ,"select count(fol_ven1) as count from dgpememd LEFT JOIN dginventp ON dgpememd.nparte1=dginventp.nparte1 where dgpememd.cons_ven='".$_REQUEST['bot']."' and dgpememd.cancela='0'"));
//and dginventp.tipo='1'
$sqlMemd=mysqli_query($con ,"select total_cd, fol_ven1 from dgpememd LEFT JOIN dginventp ON dgpememd.nparte1=dginventp.nparte1 where dgpememd.cons_ven='".$_REQUEST['bot']."' and dgpememd.cancela='0'");
while($des=mysqli_fetch_array($sqlMemd)){
//and dginventp.tipo='1'
if ($_GET['tide']=='P'){
	$desPor=($_GET['cade']/100);
    $aplDes=$des['total_cd']*$desPor;
	}elseif($_GET['tide']=='M'){
	$desPor=($_GET['cade']);
    $aplDes=$desPor/$count_md['count'];
	}

$upMemd=mysqli_query($con ,"update dgpememd set por_des='".$_GET['cade']."', imp_des='$aplDes' where fol_ven1='".$des['fol_ven1']."' and cons_ven='".$_REQUEST['bot']."'");

}
	$sq_ps=mysqli_query($con ,"select fol_ven,no_hab from fpos where fol_ven='".$_REQUEST['bot']."' and status=1");
		$regmp3=mysqli_fetch_array($sq_ps);

?>
<html>
<head>
<title><?php echo "Ticket";?></title>

<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	font-family:Arial, Helvetica, sans-serif;
	font-size:10px;
}
.tit{
font-family:Arial, Helvetica, sans-serif;
font-size:10px;
}
.tot{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
font-weight:bold;
}
.tat{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
font-weight:bold;
}

.dom{
font-family:Arial, Helvetica, sans-serif;
font-size:10px;
}
td{
font-family:Arial, Helvetica, sans-serif;
font-size:13px;
}
.titOrd{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
font-weight:bolder;
}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><object id=factory style="display:none"
  classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"
  codebase="archivos/smsx.cab#Version=6,1,400,50">
</object>
<script>
function printWindow() {
window.print();
window.close();
}
</script>
 </head> 
 <body onLoad="printWindow();">
<form name="form1" method="post" action="">
  <table  border="0" cellspacing="0">
<?php
 if ($_REQUEST['but']!=0){ ?>
    <tr>
      <td colspan="4" scope="col" align="center" class="titOrd"><?php echo $dt_empres['nom_emp'];
	  				$_REQUEST['bot'];
					$_GET['bot'];
					$_POST['bot'];
					$_REQUEST['control'];
					$_GET['control'];
					$_POST['control']; 
					$_GET['cade'];?></td>
    </tr>
    <tr>
      <td colspan="4" class="dom" align="justify"><?php  echo ''.$dt_empres['calle']." ".$dt_empres['no_ext'].' COL. '.$dt_empres['des_col'];?></td>
    </tr>
    <tr>
      <td colspan="4" class="dom" align="justify"><?php echo "".$rowd_encabezado['des_delmun']." ".$rowe_encabezado['des_estado']." MEXICO";?></td>
    </tr>
    <tr>       <td colspan="4" class="dom" align="justify"><?php echo 'C.P.'.$dt_empres['cod_pos'].' TEL: '.$dt_empres['telefono01'];?></td>
    </tr>
    <tr>
      <td colspan="4"><div align="center"><?php echo 'RFC:&nbsp;&nbsp;'.$dt_empres['rfc'];?></div></td>
    </tr>
    <tr><?php $dia=date('Y-m-d')?>
      <td colspan="4" class="titOrd"><?php echo 'FECHA:&nbsp;&nbsp;'.chfecha($dia,'Y-m-d','d/m/Y').'&nbsp;&nbsp;&nbsp; HORA:&nbsp;'.date('H:i:s');?></td>
    </tr>
    <tr>
	<td colspan="4" class="titOrd"><?php 
	mysqli_query($con ,"update dginventp set lim_vta1 = '0' where pid_mod = '1'");
	$pax=mysqli_query($con ,"select no_pers from fcomhues where fol_ven='".$_REQUEST['bot']."'");
	$personas=mysqli_fetch_array($pax);
	$sq_m=mysqli_query($con ,"select * from ctmesas where nparte1='".$_SESSION['mesa']."'");
	$regmp=mysqli_fetch_array($sq_m);
	$sq_p=mysqli_query($con ,"select * from  ptventa where nparte1='".$regmp['descripcion']."'");
		$regmp1=mysqli_fetch_array($sq_p);
	$sq_a=mysqli_query($con ,"select * from  ctarea where nparte1='".$regmp['area']."'");
		$regmp2=mysqli_fetch_array($sq_a);

	echo $regmp1['tip_ven'].' / '.$regmp2['np_desc1'];   echo '&nbsp;&nbsp;&nbsp; M-'.$regmp['np_desc1']; echo "&nbsp;&nbsp;&nbsp; MESERO: "; 
	  if ( ($rego1['cod_ven']!='') || ($rego1['cod_ven']!=0) ){
	  $sq_ms=mysqli_fetch_array(mysqli_query($con ,"select * from ctmeser where nparte1=".$rego1['cod_ven']."")); 
	  echo $sq_ms['iniciales'];
	  } else {
	  echo "CAJERO";
	  }?></td></tr>	
    </tr>
    <tr>
	<td colspan="4" class="titOrd"><?php echo 'FOLIO: '.str_pad($rego1['con_ven'],5,'0',STR_PAD_LEFT);  echo '&nbsp;&nbsp;&nbsp;&nbsp;PAX. : '.$personas['no_pers'];?></td></tr>	
    </tr>
    <tr>
      <td colspan="4" class="titOrd"></td>
    </tr>
<?php
 }else {?>
    <tr>
      <td colspan="4" class="titOrd" align="center"><?php echo 'COCINA &nbsp;'?></td>
    </tr>	
    <tr>
      <td colspan="4" class="titOrd"><?php echo "MEDSERO(A): "; 
	  if ( ($rego1['cod_ven']!='') || ($rego1['cod_ven']!=0) ){
	  $sq_ms=mysqli_fetch_array(mysqli_query($con ,"select * from ctmeser where nparte1=".$rego1['cod_ven']."")); 
	  echo $sq_ms['np_desc1'];
	  }
	  else {
	  echo "CAJERO";
	  }?></td>
    </tr>
    <tr>
      <td colspan="4" class="titOrd"><?php 
	  	$sq_m=mysqli_query($con ,"select * from ctmesas where nparte1='".$_SESSION['mesa']."'");
	$regmp=mysqli_fetch_array($sq_m);
	  echo 'MESA:  '.$regmp['np_desc1'];   echo '&nbsp;&nbsp;&nbsp;&nbsp;PAX  :'.$personas['no_pers'] ?></td></tr>
    <tr>
    <tr>
      <td colspan="4" class="titOrd"><?php echo 'FECHA &nbsp;'.chfecha($_SESSION['fecven'],'Y-m-d','d/m/Y').' HORA:&nbsp;'.date('H:i:s');?></td>
    </tr>	
    <tr>
      <td colspan="4" class="titOrd"><?php echo 'FOLIO:'.str_pad($rego1['con_ven'],5,'0',STR_PAD_LEFT);
      if ($_REQUEST['but']==0){ echo '&nbsp;&nbsp;&nbsp;&nbsp;COMANDA:'.str_pad($_SESSION['comand'],5,'0',STR_PAD_LEFT); }?></td></tr>
    <tr>
<?php
 } ?>	
      <td width="30"><strong>Can</strong></td>

      <td width="160"><strong>Prod.</strong></td>
      <td width="68" align="right"><strong>Importe</strong></td>
    </tr>
	<?php

	if(!isset($_REQUEST['rec_caj']))
		$_REQUEST['rec_caj']=$_REQUEST['bot'];
	@$resu2=mysqli_query($con ,"select * from dgpemema where fol_ven = '".$_REQUEST['bot']."'");
	@$rego2=mysqli_fetch_array($resu2);

	if ($_REQUEST['but']==0){
	//BUSQUEDA DE CUENTA POR COMANDA
	@$resu3=mysqli_query($con ,"select cons_ven,sum(total_cd) total_cd,sum(can_ref)can_ref,nparte1,comenta from dgpememd where cons_ven ='".$_REQUEST['bot']."' and fcomd='".$_SESSION['comand']."' and and cancela=0 group by nparte1,comenta");
	@$tot=mysqli_fetch_array(mysqli_query($con ,"select sum(total_cd) as total from dgpememd where cons_ven = '".$_REQUEST['bot']."' and fcomd='".$_SESSION['comand']."'"));
	}else {
	//BUSQUEDA DE CUENTA TOTAL POR POSICION
	@$resu3=mysqli_query($con ,"select cons_ven,sum(total_cd) total_cd,sum(can_ref)can_ref,nparte1,comenta from dgpememd where cons_ven ='".$_REQUEST['bot']."'  and cancela=0 ");
	@$tot=mysqli_fetch_array(mysqli_query($con ,"select sum(total_cd) as total from dgpememd where cons_ven = '".$_REQUEST['bot']."'"));
	} 
	$noRegsDetalle=mysqli_num_rows($resu3);
	
	@$re=mysqli_query($con ,"select * from dgpemema where fol_ven = '".$_REQUEST['bot']."' and dgpemema.fol_ven=dgger_cajerosj.pedido and dgger_cajerosj.n_rec=dgger_cajerosd.n_rec and dgger_cajerosd.tip_pag=4'");
	@$res=mysqli_fetch_array($re);
	
	while(@$rego3=mysqli_fetch_array($resu3)){
		@$resux=mysqli_query($con ,"SELECT * FROM dginventp WHERE nparte1 = '".$rego3['nparte1']."'");
		@$regox=mysqli_fetch_array($resux);
		$iva=($regox['por_iva']/100)+1;
		$prod_sn=$rego3['total_cd']/$iva;
		
		echo "
		<tr>"
		 ." <td>".$rego3['can_ref']."</td>"
		
		 ." <td>".substr($regox['np_desc1'],0,17)."</td>"
		 ." <td><div align=\"right\">".number_format($rego3['total_cd'],2,'.',',')."</div></td>"
	."	</tr>";
		if ($_REQUEST['but']==0){
		echo "
		<tr>
		  <td colspan=\"4\">".$rego3['comenta']."</td>
		</tr>";
		}
		$numeroArticulos+= $rego3['can_ref'];
		
		$ser2=($prod_sn)*($regox['por_serv']/100);
		
		$tot_sn+=$prod_sn;
		$totser+=$ser2;
		
		//if ($regox['tipo']==1){
		$tot_ali+=$rego3['total_cd'];
		//}
	
	}
	    $porSer=$regmp1['servicio'];
		
                if($rego2['tot_ven']>$tot['total']){
                    $tot=mysqli_query($con ,"update dgpemema set tot_ven='".$tot['total']."' where fol_ven='".$_REQUEST['bot']."'");
                    $tot_vta+=$tot['total'];
                }else
		$tot_vta+=$rego2['tot_ven'];
                
		$d_pago+=$dt_ger_cajerosj['cambio'];
		
		$servicio=($tot_sn*$porSer)/100;
		
		
		
		if($totser=='0' && $porSer!='0'){
                 
		echo "
		<tr>
		  <td></td>
		  <td></td>
		  <td>SERVICIO</td>
		  <td><div align=\"right\">".number_format($servicio,2,'.',',')."</div></td>		
		</tr>";
		
		$ser=mysqli_query($con ,"update dgpemema set servicio='$servicio' where fol_ven='".$_REQUEST['bot']."'");	
		$subtotal=$tot_vta+$servicio;
			}elseif($totserq!='0' && $porSer=='0'){
                            if($_REQUEST['bot']==104382){
                   $totser=$totser-1.30; 
                }
			    echo "
		<tr>
		  <td></td>
		  <td></td>
		  <td>SERVICIO</td>
		  <td><div align=\"right\">".number_format($totser,2,'.',',')."</div></td>		
		</tr>";
		
		$ser=mysqli_query($con ,"update dgpemema set servicio='$totser' where fol_ven='".$_REQUEST['bot']."'");
		$subtotal=$tot_vta+$totser;	
			}else{
			$subtotal=$tot_vta;
			}
	echo "<tr> <td colspan=\"4\">&nbsp;</td></tr>";

if ($_REQUEST['but']==0){
	include("DF10000.php");
	$can=round($rego2['tot_ven'],2);
	$parTot=explode(".","$can");
	if($parTot[1]==0 || $parTot[1]==''){
		$parTot[1]='00';
	}	
	$pre_let=num2letras(number_format($parTot[0], 2, '.', ''));
	echo "
	<tr>
	  <td colspan=\"4\">ARTICULOS DESPACHADOS: $numeroArticulos</td>
	</tr>";
		
	echo "
	<tr>
	  <td colspan=\"4\">&nbsp;</td>
	</tr>";
	}else{
		echo "
	<tr>
	  <td colspan=\"4\">&nbsp;</td>
	</tr>";
	echo "    
	<tr>
      <td colspan=\"3\" class=\"tot\">SUBTOTAL : </td>
      <td class=\"tot\"><div align=\"right\"> $".number_format($subtotal,2,'.',',')."</div></td>
    </tr>";
	
	if ($_GET['tide']=='P'){
	$porDes=$_GET['cade']/100;
	$sub=$tot_ali*$porDes;
	}elseif($_GET['tide']=='M'){
	$porDes=$_GET['cade'];
	$sub=$porDes;
	}
	echo "    
	<tr>
      <td colspan=\"3\" class=\"tot\">DESCUENTO : </td>
      <td class=\"tot\"><div align=\"right\">$".number_format($sub,2,'.',',')."</div></td>
    </tr>";
	$total=$subtotal-$sub;
	echo "    
	<tr>
      <td colspan=\"3\" class=\"tot\"> </td>
      <td class=\"tot\"><div align=\"right\">$".number_format($total,2,'.',',')."</div></td>
    </tr>";
	echo "<tr><td colspan=\"4\">&nbsp;</td></tr>";
	echo "    
	<tr>
      <td colspan=\"3\" class=\"tot\">PROPINA : </td>
      <td class=\"tot\"><div align=\"right\">  $ ______</div></td>
    </tr>";
	echo "    
	<tr>
      <td colspan=\"3\" class=\"tot\">TOTAL : </td>
      <td class=\"tot\"><div align=\"right\">$ _______</div></td>
    </tr>";
	

	echo "
	<tr>
	  <td colspan=\"4\">ARTICULOS DESPACHADOS: $numeroArticulos</td>
	</tr>";
	echo "
	<tr>
	  <td colspan=\"4\">&nbsp;</td>
	</tr>";
$sql_h=mysqli_query($con ,"select * from fcomhues where cvmesa='".$_SESSION['mesa']."' and status=1");
$reh1=mysqli_fetch_array($sql_h);


$sql_fh=mysqli_query($con ,"select * from valeryus19 where fnumcto='".$regmp3['no_hab']."' and (status='HUESPED' or status='MISCELANEOS')");
$reh=mysqli_fetch_array($sql_fh);


if ($reh['fnumcto']!=''){ 
$nom_hab="HABITACION :";
$habitacion=$reh['fnumcto'];
$nom_nom="NOMBRE :";
$nombre=$reh['fnombre'].'<br/> '.$reh['fapellido'];  
$nom_firma="FIRMA :";
$firma="_____________";
}else{ 
$habitacion=" "; 
$nom_hab=" ";
$nombre=" ";
$nom_nom=" ";
}
	echo "    
	<tr>
      <td class=\"tot\" colspan=\"2\">".$nom_hab."</td>
      <td class=\"tot\" colspan=\"2\"><div align=\"left\">  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$habitacion."</div></td>
    </tr>";
	echo "
	<tr>
	  <td colspan=\"4\">&nbsp;</td>
	</tr>";

	echo "    
	<tr>
      <td  class=\"tot\" colspan=\"2\">".$nom_nom."</td>
      <td colspan=\"2\" width=\"30\" class=\"tot\"><div align=\"left\">$nombre</div></td>
    </tr>";
	echo "
	<tr>
	  <td colspan=\"4\">&nbsp;</td>
	</tr>";


	echo "    
	<tr>
      <td class=\"tot\" colspan=\"2\">".$nom_firma."</td>
      <td colspan=\"2\"  class=\"tot\"><div align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$firma."</div></td>
    </tr>"; 

	echo "
	<tr>
	  <td colspan=\"4\">&nbsp;</td>
	</tr>";
	echo "
	<tr>
	  <td colspan=\"4\" class=\"tat\"><div align=\"center\">*GRACIAS POR SU PREFERENCIA*</div></td>
	</tr>";			

	}
	?>

  </table>

</form>
 <?php

 //382-->300
 ?>


</body>
</html>

