<?
$fi=$_GET["fechaini"];
$ff=$_GET["fechafin"];
require("../conectar.php");
require("funciones.php");
?>	
<table class="ocupacion" border="1" align="center">
 <tr><td colspan="4"><h2>Ingreso x concepto</h2></td></tr>
  <tr>
    <td>Concepto </td>
    <td>No. de Clientes</td>
    <td>Cantidad en dolares</td>
    <td>Cantidad en pesos</td>
  </tr>
  <?
    $sql="SELECT concepto, count( DISTINCT id_concepto) as con from pagos  where(fecha between '$fi' and '$ff') and valadmon='s' group by concepto ";  
	$query=mysql_query($sql);
	$mayor=0;
		while($pago=mysql_fetch_array($query))
		{
		 $x++;
		 $concepto=$pago["concepto"];
		 $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
		 $color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
 ?><tr>
 <input name="ultimon" type="hidden" id="ultimon" >
    <td>

    <input name="concepto<?echo$x?>" type="hidden" id="concepto<?echo$x?>" value="<?echo$concepto;?>" ><?echo$concepto;?></td>
	<input name="color<?echo$x?>" type="hidden" id="color<?echo$x?>" value="<?echo$color;?>" >
    <td><? $totalclientes=$totalclientes+$pago["con"]; if($mayor<$pago["con"])$mayor=$pago["con"];?>
    <input name="cliente<?echo$x?>"  type="hidden" id="cliente<?echo$x?>" value="<?echo$pago["con"];?>"><font color="blue"><a onclick="clientelist('<?echo"p.concepto=/$concepto/"?>','<?echo$fi?>','<?echo$ff?>','<?echo$x?>');" ><?echo$pago["con"]?></a></font>
	<div id="clientelist<?echo$x;?>" > </div></td>
    <td>
	
	<?$sql="SELECT divisa,sum(monto) as monto from pagos  where (fecha between '$fi' and '$ff') and tipo_moneda='USD' and concepto='$concepto' and valadmon='s' group by concepto ";  
	$query1=mysql_query($sql); 
	$montous=mysql_fetch_array($query1);
	echo moneda($montous["monto"]);
	$totaldolarp=$totaldolarp+($montous["monto"]*$montous["divisa"]);
	$totaldolar=$totaldolar+$montous["monto"];?></td>
    <td><?$sql="SELECT sum(monto) as monto from pagos  where (fecha between '$fi' and '$ff') and tipo_moneda='MXN' and concepto='$concepto' and valadmon='s' group by concepto ";  

	$query1=mysql_query($sql); 

	$montopes=mysql_fetch_array($query1);

	echo moneda($montopes["monto"]);
	$totalpesos=$totalpesos+$montopes["monto"];
	?></td>

  </tr>

  <?
    if($montous['monto']==""){$usd="0";}else{$usd=$montous['monto'];}
    $tipos=array('CLIENTES','USD','MXN');
	$conc=$conc.",^$concepto^";
	$totgg3=$totgg3.",^$concepto^";
    $val=$val.",".$pago["con"];
	
	$vali=$vali.",".$pago['con'];
	$valiusd=$valiusd.",".$usd;
	$valimxn=$valimxn.",".round($montopes['monto'],2);
		
	$lc=strlen($val);
	$lc=$lc-1;
	$val=substr($val,-$lc);
		
	$totg=",{name:^$concepto^,data:[$val]}";
	$totgg=$totgg.$totg;
	
	$val="";
  }
    $ltg=strlen($totgg);
	$ltg=$ltg-1;
    $datacl=substr($totgg,-$ltg);
	
    $ltg3=strlen($conc);
	$ltg3=$ltg3-1;
    $conc3=substr($conc,-$ltg3);	
	
	$lc2=strlen($vali);
	$lc2=$lc2-1;
	$vali=substr($vali,-$lc2);
    
	$lc3=strlen($valiusd);
	$lc3=$lc3-1;
	$valiusd=substr($valiusd,-$lc3);

	$lc4=strlen($valimxn);
	$lc4=$lc4-1;
	$valimxn=substr($valimxn,-$lc4);
	
	$graficausdpesos="{name:^Clientes^,data:[$vali]},{name:^USD^,data:[$valiusd]},{name:^MXN^,data:[$valimxn]}";
	?>

  <tr>

    <td>Total</td>

    <td><?$mayor=$mayor+5;?>

    <!--<input name="total" type="text" id="total" value="<?echo $mayor;?>" readonly></td>-->
	<input name="total" type="text" id="total" value="<?echo $totalclientes;?>" readonly></td>
    <td><?echo moneda($totaldolar);?></td>

    <td><?echo moneda($totalpesos);?></td>

    <input name="ancho" type="hidden" id="ancho" value="<?echo$x;?>" >

  </tr>
  <tr>
    <th></th>
    <th></th>
    <th>Gran total</th>
    <th><?echo moneda($totalpesos+$totaldolarp);?></th>

    <input name="ancho" type="hidden" id="ancho" value="<?echo$x;?>" >

  </tr>
  <? 
  $va=$datacl;
  $val=str_replace("^", "'", $va);
  ?>
</table>
<script>
		<!-- 2. Add the JavaScript to initialize the chart on document ready -->		
			var chart;
			$(document).ready(function() {
				chart = new Highcharts.Chart({
					chart: {
						renderTo: 'container',
						defaultSeriesType: 'column',
						marginRight: 200,
						marginBottom: 50
					},
					title: {
						text: 'Ingresos x concepto',
						x: -20 //center
					},
					subtitle: {
						text: 'Mexico en Renta',
						x: -20
					},
					xAxis: {
						categories: ['Totales']
					},
					yAxis: {
						title: {
							text: 'Numero de clientes'
						},
						plotLines: [{
							value: 0,
							width: 1,
							color: '#808080'
						}]
					},
					tooltip: {
						formatter: function() {
				                return '<b>'+ this.series.name +'</b><br/>'+
								this.x +': '+ this.y +'';
						}
					},
					 plotOptions: {
						column: {
							dataLabels: {
								enabled: true
							}
						}
					},
					legend: {
						layout: 'vertical',
						align: 'right',
						verticalAlign: 'top',
						x: -10,
						y: 100,
						borderWidth: 1
					},
					series: [<?echo $val;?>]
				});
				
				
			});
</script>
<br>

<br>
<center><div id="container" style="width: 700px; height: 300px;"></div></center>
<br>

<br>
<?$val=$graficausdpesos;
$nom=$conc3;
 $conl=str_replace("^", "'", $nom);
 $val=str_replace("^", "'", $val);
?>
		<script type="text/javascript">
		
			var chart;
			$(document).ready(function() {
				chart = new Highcharts.Chart({
					chart: {
						renderTo: 'container2',
						defaultSeriesType: 'column',
					},
					title: {
						text: 'INGRESOS X CONCEPTO'
					},
					subtitle: {
						text: 'Mexico en Renta'
					},
					xAxis: {
						categories: [<?echo $conl;?>],
						labels: {
						rotation: -45,
						align: 'right',
						style: {
							fontSize: '12px',
							fontFamily: 'Verdana, sans-serif'
						}
						}
					},
					yAxis: {
						 labels: {
								formatter: function() {
									return Math.round(this.value);
								}},
						title: {
							text: 'Cantidad'
						}
					},
					tooltip: {
					 headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                     pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                    '<td style="padding:0"><b>{point.y:.1f}</b></td></tr>',
                     footerFormat: '</table>',
                     shared: true,
                     useHTML: true
					},
					plotOptions: {
						column: {
							pointPadding: 0.2,
							borderWidth: 0,
							dataLabels: {
							enabled: true
							}
						}
					},
				        series: [<?echo $val;?>]
				});
				
				
			});
				
		</script>
<center><div id="container2" style="width: 700px; height: 300px;"></div></center>