<?php
session_start();
date_default_timezone_set('America/Lima');
$script_tz = date_default_timezone_get();
@$xs_empresa=$_SESSION['s_empresa'];	
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Graficos</title>
</head>
<body>
<h1 align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:18px; color:#666666;">Ventas diarias (30 últimos días)</h1>
<canvas id="myChart1"></canvas>
<br>
<?php
$xsabado=65;
$xdomingo=12;

include("../conexion.php");

$xfecha = date("Y-m-d");
$arr_mont = array();
$arr_dia = array();
$n = 0;
$r = mysqli_query($conexion,"SELECT o.ordped_fecha, pt.pagtip_nombre, dp.docpag_nombre, e.emp_nom,
							SUM(((ordpeddet_precio - ordpeddet_precio_descuento) * (ordpeddet_cantidad - ordpeddet_cantidad_devolver)) 
							+ (ordpeddet_icbper_monto_unitario * (ordpeddet_cantidad - ordpeddet_cantidad_devolver))) AS monto,
							SUM(ordpeddet_cantidad - ordpeddet_cantidad_devolver) AS cantidad_total
							FROM orden_pedido o INNER JOIN pago_tipo pt ON o.pagtip_codi_fk=pt.pagtip_codi
							INNER JOIN orden_pedido_detalle opd ON opd.ordped_codi_fk=o.ordped_codi 
							INNER JOIN documento_pago dp ON dp.docpag_codi=o.docpag_codi_fk
							INNER JOIN empresas e ON o.emp_codi_fk=e.emp_codi
							WHERE o.ordped_fecha <= '$xfecha' 
							AND o.ordped_estado = 1 
							AND o.emp_codi_fk = $xs_empresa
							GROUP BY o.ordped_fecha
							ORDER BY o.ordped_fecha DESC
							LIMIT 30;");
while($row = mysqli_fetch_array($r)){
	$n = $n + 1;
	$zfecha = $row['ordped_fecha'];
	$arr_dia[$n] = saber_dia($zfecha).": ".date("d/m",strtotime($row['ordped_fecha']));
	//$arr_dia[$n] = date("D",strtotime($row['ordped_fecha'])).": ".date("d/m",strtotime($row['ordped_fecha']));
	$arr_mont[$n] = number_format($row['monto'],2,'.','');
}



function saber_dia($nombredia) {
	$dias = array('','Lunes','Martes','Miércoles','Jueves','Viernes','Sábado','Domingo');
	$fecha = $dias[date('N', strtotime($nombredia))];
	//echo $fecha;
	return $fecha;
}

?>
<script src="chart.js"></script>
<script>

var ctx1 = document.getElementById('myChart1').getContext('2d');
var chart = new Chart(ctx1, {
    type: 'line',
    data: {
        labels: ['<?php echo $arr_dia[30];?>', '<?php echo $arr_dia[29];?>', '<?php echo $arr_dia[28];?>', '<?php echo $arr_dia[27];?>', '<?php echo $arr_dia[26];?>', 
				 '<?php echo $arr_dia[25];?>', '<?php echo $arr_dia[24];?>', '<?php echo $arr_dia[23];?>', '<?php echo $arr_dia[22];?>', '<?php echo $arr_dia[21];?>', 
				 '<?php echo $arr_dia[20];?>', '<?php echo $arr_dia[19];?>', '<?php echo $arr_dia[18];?>', '<?php echo $arr_dia[17];?>', '<?php echo $arr_dia[16];?>', 
				 '<?php echo $arr_dia[15];?>', '<?php echo $arr_dia[14];?>', '<?php echo $arr_dia[13];?>', '<?php echo $arr_dia[12];?>', '<?php echo $arr_dia[11];?>', 
                 '<?php echo $arr_dia[10];?>', '<?php echo $arr_dia[9];?>', '<?php echo $arr_dia[8];?>', '<?php echo $arr_dia[7];?>', '<?php echo $arr_dia[6];?>', 
				 '<?php echo $arr_dia[5];?>', '<?php echo $arr_dia[4];?>', '<?php echo $arr_dia[3];?>', '<?php echo $arr_dia[2];?>', '<?php echo $arr_dia[1];?>'				 
				],
        datasets: [{
            label: 'Monto de Venta',
            backgroundColor: '#FFCC00',
            borderColor: '#CC3300',
            data: ['<?php echo $arr_mont[30];?>', '<?php echo $arr_mont[29];?>', '<?php echo $arr_mont[28];?>', '<?php echo $arr_mont[27];?>', '<?php echo $arr_mont[26];?>', 
				 '<?php echo $arr_mont[25];?>', '<?php echo $arr_mont[24];?>', '<?php echo $arr_mont[23];?>', '<?php echo $arr_mont[22];?>', '<?php echo $arr_mont[21];?>', 
				 '<?php echo $arr_mont[20];?>', '<?php echo $arr_mont[19];?>', '<?php echo $arr_mont[18];?>', '<?php echo $arr_mont[17];?>', '<?php echo $arr_mont[16];?>', 
				 '<?php echo $arr_mont[15];?>', '<?php echo $arr_mont[14];?>', '<?php echo $arr_mont[13];?>', '<?php echo $arr_mont[12];?>', '<?php echo $arr_mont[11];?>', 
                 '<?php echo $arr_mont[10];?>', '<?php echo $arr_mont[9];?>', '<?php echo $arr_mont[8];?>', '<?php echo $arr_mont[7];?>', '<?php echo $arr_mont[6];?>', 
				 '<?php echo $arr_mont[5];?>', '<?php echo $arr_mont[4];?>', '<?php echo $arr_mont[3];?>', '<?php echo $arr_mont[2];?>', '<?php echo $arr_mont[1];?>']
        }		
		]},
    options: {}
});



</script>
</body>
</html>    