<?php

//ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
//error_reporting(E_ALL);


include_once("DSA3000.php");
include_once("DMMYSQLG.php");

include_once('DF80000.php'); 
 
	  require_once("valeryus00000_CL.php");
  $update=mysqli_query($con ,"UPDATE dgpemema as a JOIN fpropina as b ON a.fol_ven = b.fol_ven SET a.tip =b.tip");
function reporteMensual(){
  $valeryus09=new valeryus09($data);

//$_POST['fec']='30/01/2017';
//$_POST['fec2']='30/01/2017';
//$_POST['r1']='T';
   $datosfec= explode("/", $_POST['fec']);
     $month = $datosfec[1];
     $year = $datosfec[2];
     $fecmesin= date('Y-m-d', mktime(0,0,0, $month, 1, $year));
     $fecIni=$fecmesin;
     $fecFin=chfecha(isset($_POST['fec2'])?$_POST['fec2']:"",'d/m/Y','Y-m-d');
	//Pie de Pgina

$resul=array();

  if ($_POST['r1']=='N')
  $condicion='n_reg="'.$_POST['id_ope'].'"';
  else if ($_POST['r1']=='T')
  $condicion='n_reg > 0';
  else 
  $condicion='n_reg = 0';
  
 	
	
  $sql_cb=mysqli_query($con ,"select * from valeryus09 where $condicion and pmontomax='CARGO' and tip='0' order by descripcion ");
  while ($reg=mysqli_fetch_array($sql_cb)){
  
  $ptventa=mysqli_fetch_array(mysqli_query($con ,"select GROUP_CONCAT(nparte1)as nparte1 from ptventa where codigo='".$reg['n_reg']."'"));
           if ($reg['restringido']=='SI'){
			@$rest = mysqli_query($con ,"
		SELECT 
			dgpemema.fol_ven,
			dgpemema.con_ven,
			dgpemema.fec_ven,
			dgpemema.cod_ven,
			dgpemema.imp_pag,
			ctmeser.np_desc1 as nomVen,
			dgpemema.por_des as descuento,
			ctmesas.np_desc1 as dmesa,
			ptventa.np_desc1 as dptvta,
			fpropina.tip as propina,
			dgger_cajerosj.tip_mov as tipag,
			dgpemema.cancela as cancela,
			dgger_cajerosj.fol_unp as folp
		FROM 
			dgpemema
			LEFT JOIN fpos ON fpos.fol_ven=dgpemema.fol_ven
			LEFT JOIN ctmesas ON ctmesas.nparte1=fpos.mesa
			LEFT JOIN ctarea ON ctarea.nparte1=ctmesas.area
			LEFT JOIN ctmeser  ON ctmeser.nparte1=dgpemema.cod_ven
			LEFT JOIN ptventa ON ptventa.nparte1=ctmesas.descripcion
			LEFT JOIN dgger_cajerosj ON dgger_cajerosj.pedido=dgpemema.fol_ven		
			LEFT JOIN  fpropina ON fpropina.fol_ven=dgpemema.fol_ven	
			LEFT JOIN  dgtippag ON dgtippag.tip_pag=dgger_cajerosj.tip_mov								
			left JOIN dgcatven ON dgpemema.cod_ven=dgcatven.cod_ven
			left JOIN dgger_cajerosd ON dgger_cajerosj.n_rec=dgger_cajerosd.n_rec
			
		where dgpemema.fec_ven between '".$fecIni."' and '".$fecFin."' and ptventa.nparte1 in (".$ptventa['nparte1']." )
		group by dgpemema.fol_ven
		ORDER BY 
		
			dgpemema.con_ven");
		$comida=0;
		while($rest1 = mysqli_fetch_array($rest)){
@$sqal = mysqli_query($con ,"SELECT sum((can_ref*pre_bas)-imp_des) as totAli from dgpememd left outer join dginventp on dginventp.nparte1=dgpememd.nparte1 left join dgpemema on dgpememd.cons_ven=dgpemema.fol_ven where dgpememd.cons_ven='".$rest1['fol_ven']."' and dgpememd.cancela=0 and dgpemema.cancela='0' and dgpemema.cancelado='0' group by dgpememd.nparte1");
		
		while ($com=mysqli_fetch_array($sqal)){
		
		$comida += $com['totAli'];
		
		}
		}
		$comSnIva=$comida/1.16;
	    $comSlIva=$comSnIva*.16;
	    $ventas=$comSnIva+$comSlIva;
		
	/*	if ($reg['n_reg']=='37'){ //total de propinas
	
	$tip=mysqli_fetch_array(mysqli_query($con ,"select sum(tip) as propina from dgpemema where fec_ven between '".$fecIni."' and '".$fecFin."' and (cancelado='0' and cancela='0')"));
	
	    $pdf->Cell(19,3,' $'.number_format($tip['propina'],2,'.',','),0,0,'R',0);
		$pdf->Cell(19,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
		$pdf->Cell(18,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
	    $pdf->Cell(19,3,' $'.number_format($tip['propina'],2,'.',','),0,1,'R',0);
	
	}elseif ($reg['n_reg']=='109'){
	
	$ser=mysqli_fetch_array(mysqli_query($con ,"select sum(servicio) as servicio from dgpemema where fec_ven between '".$fecIni."' and '".$fecFin."' and (cancelado='0' and cancela='0')"));
	
	    $pdf->Cell(19,3,' $'.number_format($ser['servicio'],2,'.',','),0,0,'R',0);
		$pdf->Cell(19,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
		$pdf->Cell(18,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
	    $pdf->Cell(19,3,' $'.number_format($ser['servicio'],2,'.',','),0,1,'R',0);
	
	}else{*/
	           	$resul[$reg['n_reg']]=' $'.number_format($comSnIva,2,'.',',');

		
		
	        $totSinIva+= $comSnIva;
		    $totPvIva+= $comSlIva;
		
		//}
		}else{
		
		
	$sql="select valeryus030.*,valeryus19.n_reg as folh from valeryus19 left join valeryus030 on (valeryus030.nparte1=valeryus19.n_reg) where valeryus030.nparte1_det='".$reg['n_reg']."' and (valeryus030.acc_fec between '".$fecIni."' and '".$fecFin."')";					
	$sql_dt=mysqli_query($con ,$sql);
	$cta1=0;
	$cargo1=0;
	$abono1=0;
	$sin_iva=0;
	$desIva=0;
	$desIsh=0;
	while($red=mysqli_fetch_array($sql_dt)){
	$cta ++; 
		if(($red['cancelado'] == 0) || ($red['cancelado']==2)){  
		$cargo1 +=$red['costo'];
		$abono1 +=$red['total'];
		$detDet=mysqli_fetch_array(mysqli_query($con ,"select * from valeryus09 where n_reg='".$reg['n_reg']."'"));
		
		if ($detDet['gra']=='SI' and $detDet['sobre']=='SI'){
		   $iva=($detDet['gravable']/100);
		   $ish=($detDet['impuesto']/100);
		   $impuesto=($iva+$ish)+1;   
			  $sin_iva=$cargo1/$impuesto;
				 $desIva=$sin_iva*$iva;
				 $desIsh=$sin_iva*$ish;
		
		  }elseif($detDet['gra']=='SI' and $detDet['sobre']!='SI'){
		     $iva=($detDet['gravable']/100);
		     $impuesto=$iva+1;
		        $sin_iva=$cargo1/$impuesto;
		        $desIva=$cargo1-$sin_iva;
		
		    }elseif($detDet['no_gra']=='SI'){
		       $sin_iva=$cargo1;
			  
			  }elseif($detDet['otro']=='SI'){
		         $sin_iva=$cargo1;
			}
		}
	}

$resul[$reg['n_reg']]=' $'.number_format($sin_iva,2,'.',',');

		
		$numVen +=$cta;
		$totSnIva +=$sin_iva;
		$totIva +=$desIva;
		$totIsh +=$desIsh;
		$TotCargo2 +=$cargo1;
  }
  }
        $ivaTotal=$totSinIva+$totSnIva+$tip['propina']+$ser['servicio'];
		$totSoloIva=$totIva+$totPvIva;
		$totCargos=$ivaTotal+$totSoloIva+$totIsh;
  
   

$resul['SubtotalMes']=' $'.number_format($ivaTotal,2,'.',',');
			$resul['IvaMes']=' $'.number_format($totSoloIva,2,'.',',');
	$resul['IshMes']=' $'.number_format($totIsh,2,'.',',');
	
	
	
	
	////++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	$sqlasd=mysqli_query($con ,"select * from valeryus09 where $condicion and pmontomax='CARGO' and tip='1' order by descripcion ");
  while ($rog=mysqli_fetch_array($sqlasd)){
  
  
  
if ($rog['n_reg']=='37'){ //total de propinas
	
	$tip=mysqli_fetch_array(mysqli_query($con ,"select sum(tip) as propina from dgpemema where fec_ven between '".$fecIni."' and '".$fecFin."' and (cancelado='0' and cancela='0')"));
	
$resul['propinaMes']=' $'.number_format($tip['propina'],2,'.',',');
	    
	}elseif ($rog['n_reg']=='109'){
	
	$ser=mysqli_fetch_array(mysqli_query($con ,"select sum(servicio) as servicio from dgpemema where fec_ven between '".$fecIni."' and '".$fecFin."' and (cancelado='0' and cancela='0')"));
	
	  //  $pdf->Cell(19,3,' $'.number_format($ser['servicio'],2,'.',','),0,0,'R',0);
		$resul['servicioMes']=' $'.number_format($ser['servicio'],2,'.',',');
	
	}else{
	
	$sqlsd="select valeryus030.*,valeryus19.n_reg as folh from valeryus19 left join valeryus030 on (valeryus030.nparte1=valeryus19.n_reg) where valeryus030.nparte1_det='".$rog['n_reg']."' and (valeryus030.acc_fec between '".$fecIni."' and '".$fecFin."')";					
	$sqldft=mysqli_query($con ,$sqlsd);
	$cta1=0;
	$cargo2=0;
	$abono1=0;
	$sin_iva=0;
	$desIva=0;
	$desIsh=0;
	while($rod=mysqli_fetch_array($sqldft)){
	$cta ++; 
		if(($rod['cancelado'] == 0) || ($rod['cancelado']==2)){  
		$cargo2 +=$rod['costo'];
		$abono1 +=$rod['total'];
		//$detDet=mysqli_fetch_array(mysqli_query($con ,"select * from valeryus09 where n_reg='$reg['n_reg']'"));
		
		
		}
	}

$resul[$rog['n_reg']]=' $'.number_format($cargo2,2,'.',',');
		
			    //$totSinIva+= $comSnIva;
		    $totPrp+= $cargo2;
		
		}


  }	
		$ksjdf=$ivaTotal+$totPrp+$tip['propina']+$ser['servicio'];
		$gdfgd=$totCargos+$totPrp+$ser['servicio']+$tip['propina'];
		
		
$resul['tipMes']=' $'.number_format($ksjdf+$totIsh+$totSoloIva,2,'.',',');















		

//ABONOS		
   $totAbo=0;
  $sql_hotel09=$valeryus09->report_getGeneralabo();		
  if(!$sql_hotel09)die(mysqli_error($con));
  while ($rega=mysqli_fetch_array($sql_hotel09)){
 	
	$sql="select * from valeryus030 where nparte1_det='".$rega['nReg']."' and (acc_fec between '".$fecIni."' and '".$fecFin."') and depositos='0'";					
					
	$sql_dt=mysqli_query($con ,$sql);
	$cta=0;
	$cargo=0;
	$abono=0;
	while($red=mysqli_fetch_array($sql_dt)){
	$cta ++; 
		if(($red['cancelado'] == 0) || ($red['cancelado']==2)){  
		$cargo +=$red['costo'];
		$abono +=$red['total'];
		}
		
	}
	  
	  
	  
	  if($rega['codIgo']!='0'){
	  $taDet='dgger_cajerosd';
	  $table='dgger_cajerosj';
	
	$sql_pago=mysqli_fetch_array(mysqli_query($con ,"select sum($taDet.importe) as paGos from dgpemema left join $table on dgpemema.fol_ven=$table.pedido left join $taDet on $table.n_rec=$taDet.n_rec where dgpemema.fec_ven between '$fecIni' and '$fecFin' and (dgpemema.cancelado='0' and dgpemema.cancela='0') and $taDet.tip_pag='".$rega['tipPag']."'"));
	//$pdf->Cell(60,3,"select sum($taDet.importe) as paGos from dgpemema left join $table on dgpemema.fol_ven=$table.pedido left join $taDet on $table.n_rec=$taDet.n_rec where dgpemema.fec_ven between '$fecIni' and '$fecFin' and (dgpemema.cancelado='0' and dgpemema.cancela='0') and $taDet.tip_pag='".$rega['tipPag']."'",0,1,'R',0);
	
$resul[$rega['nReg']]=' $'.number_format($pagos=$abono+$sql_pago['paGos'],2,'.',',');
	$totAbo+=$pagos;		
	}
	  
	  
	  
		$numVen +=$cta;
		$TotAbono +=$abono;
		
  }
 $resul['TotabonoMes']=' $'.number_format($totAbo,2,'.',',');
	$saldo=$gdfgd-$totAbo;		
		 $resul['TotaSaldo']=' $'.number_format($saldo,2,'.',',');
		   $resul['Saldonum']=$saldo;
return $resul;
}
?>
