<?php $r="";
require_once($r."inc/funciones.php");
validNotSession($r);
$titlepage = 'DASHBOARD';
$mysqli = connectDB($r);
session_start();
$spe=$_SESSION['perfil'];
$act_consulta = viewroleaccess(1000);
$hoy = date("Y-m-d");
$mes = date("Y-m");
$ultimomes =  date("Y-m-d",strtotime($hoy."- 1 month"));

?>
  <?php include $r."inc/head.php"; ?>

<body id="page-top">
<?php include $r."inc/nav.php"; ?>
  <div class="content-wrapper">
    <div class="container-fluid ">
        <? if($act_consulta){ ?>
        
        <style>
            h4{
                background: #cccccc;
                text-align: center;    width: calc(100% + 30px);
                margin-left: -15px;
                border-bottom: 1px solid #000;
            }
        </style>
        <form class="form-inline p-4" method="GET">
          <div class="form-group mb-2">
            <label for="staticEmail2">de</label>
            <input type="date" class="form-control" name="de" required>
          </div>
          <div class="form-group mx-sm-3 mb-2">
            <label for="inputPassword2">a</label>
            <input type="date" class="form-control" name="a" required>
          </div>
          <button type="submit" class="btn btn-primary mb-2">Consultar</button>
        </form>
        <? if(isset($_GET['de'])){ $de = $_GET['de']; $a = $_GET['a'];?>
          <div class="row p-3">
                <h4>De <?= $de ?> a <?= $a ?></h4>
            <div class="col-xs-12 col-md-4 col-lg-4 border border-dark">
              <canvas id="oilChart4"></canvas>
            </div>
            <div class="col-xs-6 col-md-8">
            <table class="table table-striped table-bordered">
              <tbody>
                  
                <tr class="text-success">
                  <td scope="row"><span>Facturado</span></td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(`total`+`adicional`) AS 'total' FROM `z_pedidos`  WHERE (`estado` != 4 AND `estado` != 5) AND `fecha`  BETWEEN '$de 00:00:00.000000' AND '$a 23:59:59.999999'"); $row1=$sql1->fetch_assoc(); $ic = $row1['total']; echo moneda($ic); ?> </td>
                </tr>
                <!--<tr class="text-success">
                  <td scope="row"><span>Facturado a</span></td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(`total`+`adicional`) AS 'total' FROM `z_pedidos`  WHERE `pago` = 2 AND `fecha`  BETWEEN '$de 00:00:00.000000' AND '$a 23:59:59.999999'"); $row1=$sql1->fetch_assoc(); $ic2 = $row1['total']; echo moneda($ic2); ?> </td>
                </tr>-->
                <tr class="text-success">
                  <td scope="row"><span onclick="infopf('consu','<?= $de; ?>','<?= $a ?>')">Cobrado de Facturado</span></td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(z_abonos.abono) AS 'total'  FROM `z_pedidos` INNER JOIN `z_abonos` ON z_pedidos.codigopedido=z_abonos.codigopedido WHERE (z_pedidos.`estado` != 4 OR z_pedidos.`estado` != 5) AND z_pedidos.fecha BETWEEN '$de 00:00:00.000000' AND '$a 23:59:59.999999'"); $row1=$sql1->fetch_assoc(); $ipc = $row1['total']; 
                  $sql1=$mysqli->query("SELECT SUM(z_pagos.monto) AS 'total'  FROM `z_pedidos` INNER JOIN `z_pagos` ON z_pedidos.codigopedido=z_pagos.idpedido WHERE (z_pedidos.`estado` != 4 OR z_pedidos.`estado` != 5) AND z_pedidos.fecha BETWEEN '$de 00:00:00.000000' AND '$a 23:59:59.999999'"); $row1=$sql1->fetch_assoc(); $ipc = $row1['total']; echo moneda($ipc); ?> </td>
                </tr>
                <tr class="text-danger">
                  <td scope="row">Por cobrar facturado</td>
                  <td><?= moneda($ic-$ic2); ?> </td>
                </tr>
                <tr class="text-info">
                  <td scope="row"><span onclick="infop('consu','<?= $de; ?>','<?= $a ?>')">Cartera recaudada</span></td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(`abono`) AS 'total' FROM `z_abonos` WHERE `fecha`  BETWEEN '$de 00:00:00.000000' AND '$a 23:59:59.999999'"); $row1=$sql1->fetch_assoc(); $pac = $row1['total'];?> 
                  <? $sql1=$mysqli->query("SELECT SUM(`monto`) AS 'total' FROM `z_pagos` WHERE `fecha`  BETWEEN '$de 00:00:00.000000' AND '$a 23:59:59.999999'"); $row1=$sql1->fetch_assoc(); $pc = $row1['total']; echo moneda($pc+$pac); ?> </td>
                </tr>
                
                <tr class="text-warning">
                  <td scope="row">Gastos</td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(`cantidad`) AS 'total' FROM `z_gastos` WHERE `fecha`  BETWEEN '$de' AND '$a'"); $row1=$sql1->fetch_assoc(); $gc =  $row1['total']; echo moneda($gc); ?> </td>
                </tr>
                
                <tr class="text-primary h4">
                  <td scope="row">Total</td>
                  <td><?= moneda($ic+$gc); ?></td>
                </tr>
              </tbody>
            </table>
            </div>
        </div>
        <? } ?>
      <div class="row p-3">
            <h4>Hoy</h4>
        <div class="col-xs-12 col-md-4 col-lg-4 border border-dark">
          <canvas id="oilChart3"></canvas>
        </div>
        <div class="col-xs-6 col-md-8">
            <table class="table table-striped table-bordered">
              <tbody>
                <tr class="text-success">
                  <td scope="row"><span>Facturado</span></td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(`total`) AS total FROM `z_pedidos` WHERE `fecha`  LIKE '%$hoy%'"); $row1=$sql1->fetch_assoc(); $ihoy = $row1['total']; echo moneda($ihoy); ?> </td>
                </tr>
                <tr class="text-success">
                  <td scope="row"><span onclick="infopf('hoy','0','<?= $hoy ?>')">Cobrado de Facturado</span></td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(z_abonos.abono) AS 'total'  FROM `z_pedidos` INNER JOIN `z_abonos` ON z_pedidos.codigopedido=z_abonos.codigopedido WHERE z_pedidos.fecha LIKE '%$hoy%'"); $row1=$sql1->fetch_assoc(); $aiphoy = $row1['total'];
                  $sql1=$mysqli->query("SELECT SUM(z_pagos.monto) AS 'total'  FROM `z_pedidos` INNER JOIN `z_pagos` ON z_pedidos.codigopedido=z_pagos.idpedido WHERE z_pedidos.fecha LIKE '%$hoy%'"); $row1=$sql1->fetch_assoc(); $iphoy = $row1['total'];echo moneda($iphoy+$aiphoy); ?> </td>
                </tr>
                <tr class="text-danger">
                  <td scope="row">Por cobrar facturado</td>
                  <td><?= moneda($ihoy-$iphoy); ?> </td>
                </tr>
                <tr class="text-info">
                  <td scope="row"><span onclick="infop('hoy','0','<?= $hoy ?>')">Cartera recaudada</span></td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(`abono`) AS 'total' FROM `z_abonos` WHERE `fecha`  LIKE '%$hoy%'"); $row1=$sql1->fetch_assoc(); $pac = $row1['total'];?> 
                  <? $sql1=$mysqli->query("SELECT SUM(`monto`) AS total FROM `z_pagos` WHERE `fecha`  LIKE '%$hoy%'"); $row1=$sql1->fetch_assoc(); $phoy = $row1['total']; echo moneda($phoy+$pac); ?> </td>
                </tr>
                
                <tr class="text-warning">
                  <td scope="row">Gastos</td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(`cantidad`) AS 'total' FROM `z_gastos` WHERE `fecha`  LIKE '%$hoy%'"); $row1=$sql1->fetch_assoc(); $ghoy =  $row1['total']; echo moneda($ghoy); ?> </td>
                </tr>
                
                <tr class="text-primary h4">
                  <td scope="row">Total</td>
                  <td><?= moneda($ihoy+$choy-$ghoy); ?></td>
                </tr>
              </tbody>
            </table>
            
        </div>
    </div>
    
    
      <div class="row p-3">
            <h4>Mes en curso</h4>
        <div class="col-xs-12 col-md-4 col-lg-4 border border-dark">
          <canvas id="oilChart2"></canvas>
        </div>
        <div class="col-xs-6 col-md-8">
            <table class="table table-striped table-bordered">
              <tbody>
                <tr class="text-success">
                  <td scope="row"><span>Facturado</span></td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(`total`) AS 'total' FROM `z_pedidos` WHERE `fecha`  LIKE '%$mes%'"); $row1=$sql1->fetch_assoc(); $imes = $row1['total']; echo moneda($imes); ?> </td>
                </tr>
                <tr class="text-success">
                  <td scope="row"><span onclick="infopf('mes','0','<?= $mes ?>')">Cobrado de Facturado</span></td>
                  <td><? 
                  $sql1=$mysqli->query("SELECT SUM(z_abonos.abono) AS 'total'  FROM `z_pedidos` INNER JOIN `z_abonos` ON z_pedidos.codigopedido=z_abonos.codigopedido WHERE z_pedidos.fecha LIKE '%$mes%'"); $row1=$sql1->fetch_assoc(); $aipmes = $row1['total'];
                  $sql1=$mysqli->query("SELECT SUM(z_pagos.monto) AS 'total'  FROM `z_pedidos` INNER JOIN `z_pagos` ON z_pedidos.codigopedido=z_pagos.idpedido WHERE z_pedidos.fecha LIKE '%$mes%'"); $row1=$sql1->fetch_assoc(); $ipmes = $row1['total']; echo moneda($ipmes+$aipmes); ?> </td>
                </tr>
                <tr class="text-danger">
                  <td scope="row">Por cobrar facturado</td>
                  <td><?= moneda($imes-$ipmes); ?> </td>
                </tr>
                <tr class="text-info">
                  <td scope="row"><span onclick="infop('mes','0','<?= $mes ?>')">Cartera recaudada</span></td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(`abono`) AS 'total' FROM `z_abonos` WHERE `fecha`  LIKE '%$mes%'"); $row1=$sql1->fetch_assoc(); $pac = $row1['total'];?> 
                  <? $sql1=$mysqli->query("SELECT SUM(`monto`) AS 'total' FROM `z_pagos` WHERE `fecha`  LIKE '%$mes%'"); $row1=$sql1->fetch_assoc(); $pmes = $row1['total']; echo moneda($pmes+$pac); ?> </td>
                </tr>
                
                <tr class="text-warning">
                  <td scope="row">Gastos</td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(`cantidad`) AS 'total' FROM `z_gastos` WHERE `fecha`  LIKE '%$mes%'"); $row1=$sql1->fetch_assoc(); $gmes =  $row1['total']; echo moneda($gmes); ?> </td>
                </tr>
                
                <tr class="text-primary h4">
                  <td scope="row">Total</td>
                  <td><?= moneda($imes+$cmes-$gmes); ?></td>
                </tr>
              </tbody>
            </table>
            
        </div>
    </div>
    
    
      <div class="row p-3">
            <h4>Ultimos 30 Dias</h4>
        <div class="col-xs-12 col-md-4 col-lg-4 border border-dark">
          <canvas id="oilChart1"></canvas>
        </div>
        <div class="col-xs-6 col-md-8">
            <table class="table table-striped table-bordered">
              <tbody>
                <tr class="text-success">
                  <td scope="row"><span>Facturado</span></td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(`total`) AS 'total' FROM `z_pedidos` WHERE `fecha`  BETWEEN '$ultimomes' AND '$hoy'"); $row1=$sql1->fetch_assoc(); $iultimo = $row1['total']; echo moneda($iultimo); ?> </td>
                </tr>
                <tr class="text-success">
                  <td scope="row"><span onclick="infopf('umes','<?= $ultimomes; ?>','<?= $hoy ?>')">Cobrado de Facturado</span></td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(z_abonos.abono) AS 'total'  FROM `z_pedidos` INNER JOIN `z_abonos` ON z_pedidos.codigopedido=z_abonos.codigopedido WHERE z_pedidos.fecha BETWEEN '$ultimomes' AND '$hoy'"); $row1=$sql1->fetch_assoc(); $aipultimo = $row1['total'];
                  $sql1=$mysqli->query("SELECT SUM(z_pagos.monto) AS 'total'  FROM `z_pedidos` INNER JOIN `z_pagos` ON z_pedidos.codigopedido=z_pagos.idpedido WHERE z_pedidos.fecha BETWEEN '$ultimomes' AND '$hoy'"); $row1=$sql1->fetch_assoc(); $ipultimo = $row1['total'];echo moneda($ipultimo+$aipultimo); ?> </td>
                </tr>
                <tr class="text-danger">
                  <td scope="row">Por cobrar facturado</td>
                  <td><?= moneda($iultimo-$ipultimo); ?> </td>
                </tr>
                <tr class="text-info">
                  <td scope="row"><span onclick="infop('umes','<?= $ultimomes; ?>','<?= $hoy ?>')">Cartera recaudada</span></td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(`abono`) AS 'total' FROM `z_abonos` WHERE `fecha` BETWEEN '$ultimomes' AND '$hoy'"); $row1=$sql1->fetch_assoc(); $pac = $row1['total'];?> 
                  <? $sql1=$mysqli->query("SELECT SUM(`monto`) AS 'total' FROM `z_pagos` WHERE `fecha`  BETWEEN '$ultimomes' AND '$hoy'"); $row1=$sql1->fetch_assoc(); $pultimo = $row1['total']; echo moneda($pultimo+$pac); ?> </td>
                </tr>
                
                <tr class="text-warning">
                  <td scope="row">Gastos</td>
                  <td><? $sql1=$mysqli->query("SELECT SUM(`cantidad`) AS 'total' FROM `z_gastos` WHERE `fecha`  BETWEEN '$ultimomes' AND '$hoy'"); $row1=$sql1->fetch_assoc(); $gultimo =  $row1['total']; echo moneda($gultimo); ?> </td>
                </tr>
                
                <tr class="text-primary h4">
                  <td scope="row">Total</td>
                  <td><?= moneda($iultimo+$cultimo-$gultimo); ?></td>
                </tr>
              </tbody>
            </table>
            
        </div>
    </div>
      <? } ?>
      <?php include $r."inc/foot.php"; ?>
    </div>
  </div>
  
<? if($act_consulta){ ?>     
  <script>
        var oilCanvas1 = document.getElementById("oilChart1");
        var oilCanvas2 = document.getElementById("oilChart2");
        var oilCanvas3 = document.getElementById("oilChart3");
        var oilCanvas4 = document.getElementById("oilChart4");
        
        Chart.defaults.global.defaultFontFamily = "Lato";
        Chart.defaults.global.defaultFontSize = 18;
        
        var oilData1 = {
            labels: [
                "Gastos",
                "Ingresos",
            ],
            datasets: [
                {
                    data: [<?= $gultimo.','.$iultimo; ?>],
                    backgroundColor: ["#F5B7B1","#73C6B6"]
                }]
        };
        var oilData2 = {
            labels: [
                "Gastos",
                "Ingresos",
            ],
            datasets: [
                {
                    data: [<?= $gmes.','.$imes; ?>],
                    backgroundColor: ["#F5B7B1","#73C6B6"]
                }]
        };
        var oilData3 = {
            labels: [
                "Gastos",
                "Ingresos",
            ],
            datasets: [
                {
                    data: [<?= $ghoy.','.$ihoy; ?>],
                    backgroundColor: ["#F5B7B1","#73C6B6"]
                }]
        };
<? if(isset($_GET['de'])){ $de = $_GET['de']; $a = $_GET['a'];?>
var oilData4 = {
    labels: [
        "Gastos",
        "Ingresos",
        "Cobrado",
    ],
    datasets: [
        {
            data: [<?= $gc.','.$ic.','.$pc; ?>],
            backgroundColor: ["#F5B7B1","#73C6B6","#53C6B6"]
        }]
};
<? } ?>
var pieChart = new Chart(oilCanvas1, {  type: 'pie',  data: oilData1});
var pieChart = new Chart(oilCanvas2, {  type: 'pie',  data: oilData2});
var pieChart = new Chart(oilCanvas3, {  type: 'pie',  data: oilData3});
var pieChart = new Chart(oilCanvas4, {  type: 'pie',  data: oilData4});

function infop(con, de, a) {
    $.post("acciones/infopagosall.php", { con: con, de: de, a: a })
      .done(function(data) {
        swal({
          title: "Info",
          text: data,
          html: true, // Indica que el contenido es HTML
          confirmButtonText: "Cerrar"
        });
      });
}

function infopf(con, de, a) {
    $.post("acciones/infopagosf.php", { con: con, de: de, a: a })
      .done(function(data) {
        swal({
          title: "Info",
          text: data,
          html: true, // Indica que el contenido es HTML
          confirmButtonText: "Cerrar"
        });
      });
}
  </script>
  
  <? } ?>
</body>
</html>