<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>COMPROBANTE</title>
  <!-- Bootstrap Core CSS -->
  <script type="text/javascript">
    window.print();
  </script>
  <style type="text/css">
    html {
      margin: 0;
    }

    body {
      font-family: Helvetica, Arial, Verdana, sans-serif;
      /*Trebuchet MS,*/
      margin: 5mm 3mm 2mm 3mm;
      font-style: normal;
      font-size: 8pt !important;
    }

    table {
      width: 100%;
      border-collapse: collapse;
    }

    td {
      padding: 0;
    }

    .negrita {
      font-weight: bold;
    }

    .tabla-modena>tbody>tr>td {
      text-align: center;
    }
  </style>
</head>

<body>
  <table>
    <?php if ($empresa->tipo == 0) { ?>
      <tr>
        <td align="center" class="negrita"><?= $empresa->nombre ?></td>
      </tr>
      <tr>
        <td align="center"><?= $empresa->razonsocial ?></td>
      </tr>
    <?php } else { ?>
      <tr>
        <td align="center" class="negrita"><?= $empresa->razonsocial ?></td>
      </tr>
    <?php } ?>
    <tr>
      <td align="center" class="negrita"><?= substr($empresa->direccion, 0, 30) ?></td>
    </tr>
    <tr>
      <td align="center" class="negrita"><?= $empresa->distrito . ' - ' . $empresa->provincia . ' - ' . $empresa->departamento ?></td>
    </tr>
    <tr>
      <td align="center" class="negrita">RUC <?= $empresa->ruc ?></td>
    </tr>
    <tr>
      <td align="center">TELF. <?= $empresa->telefono ?></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
  </table>
  <hr>
  <table>
    <tr>
      <td align="center" class="negrita">CIERRRE CAJA DIARIO</td>
    </tr>
    <tr>
      <td align="center">ENCARGADO: <?= $usuario->nombre . ' ' . $usuario->apellido ?></td>
    </tr>
    <tr>
      <td align="center">FECHA/HORA DE APERTURA: <?= $caja->apertura ?></td>
    </tr>
    <tr>
      <td align="center">FECHA/HORA DE CIERRE: <?= $caja->cierre ?></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
  </table>
  <hr>
  <!-- <table border="1px" class="tabla-modena ">
    <thead>
      <tr>
        <th>TIPO DE MONEDA (SOLES)</th>
        <th>CANTIDAD DE MONEDA</th>
        <th>SUB TOTAL</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>0.10</td>
        <td><?= number_format($monedero->diezcentimos, 2) ?></td>
        <td><?= number_format($monedero->diezcentimos * 0.10, 2) ?></td>
      </tr>
      <tr>
        <td>0.20</td>
        <td><?= number_format($monedero->veintecentimos, 2) ?></td>
        <td><?= number_format($monedero->veintecentimos * 0.20, 2) ?></td>
      </tr>
      <tr>
        <td>0.50</td>
        <td><?= number_format($monedero->cincuentacentimos, 2) ?></td>
        <td><?= number_format($monedero->cincuentacentimos * 0.50, 2) ?></td>
      </tr>
      <tr>
        <td>1.00</td>
        <td><?= number_format($monedero->unsol, 2)  ?></td>
        <td><?= number_format($monedero->unsol * 1, 2) ?></td>
      </tr>
      <tr>
        <td>2.00</td>
        <td><?= number_format($monedero->dossoles, 2)  ?></td>
        <td><?= number_format($monedero->dossoles * 2, 2) ?></td>
      </tr>
      <tr>
        <td>5.00</td>
        <td><?= number_format($monedero->cincosoles, 2) ?></td>
        <td><?= number_format($monedero->cincosoles * 5, 2) ?></td>
      </tr>
      <tr>
        <td>10.00</td>
        <td><?= number_format($monedero->diezsoles, 2)  ?></td>
        <td><?= number_format($monedero->diezsoles * 10, 2) ?></td>
      </tr>
      <tr>
        <td>20.00</td>
        <td><?= number_format($monedero->veintesoles, 2) ?></td>
        <td><?= number_format($monedero->veintesoles * 20, 2) ?></td>
      </tr>
      <tr>
        <td>50.00</td>
        <td><?= number_format($monedero->cincuentasoles, 2) ?></td>
        <td><?= number_format($monedero->cincuentasoles * 50, 2) ?></td>
      </tr>
      <tr>
        <td>100.00</td>
        <td><?= number_format($monedero->ciensoles, 2)  ?></td>
        <td><?= number_format($monedero->ciensoles * 100, 2) ?></td>
      </tr>
      <tr>
        <td>200.00</td>
        <td><?= number_format($monedero->doscientossoles, 2)  ?></td>
        <td><?= number_format($monedero->doscientossoles * 200, 2) ?></td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <td colspan="2" style="text-align:center; font-weight:bold">CIERRE DE CAJA:</td>
        <td style="text-align:center; font-weight:bold" colspan="2"><?= number_format($montototal, 2) ?></td>
      </tr>
    </tfoot>
  </table> -->

  <br>
  <table border="1px" style="text-align:center">
    <thead>
      <tr>
        <th colspan="3">OPERACIONES A CONTADO</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>TIPO DE VENTA</td>
        <td>ACUMULADO</td>
      </tr>
      <tr>
        <td>EFECTIVO</td>
        <td>S/. <?= number_format($caja->efectivo, 2) ?></td>
      </tr>
      <tr>
        <td>TARJETA</td>
        <td>S/. <?= number_format($caja->tarjeta, 2) ?></td>
      </tr>
      <tr>
        <td>YAPE</td>
        <td>S/. <?= number_format($caja->yape, 2) ?></td>
      </tr>
      <tr>
        <td>PLIN</td>
        <td>S/. <?= number_format($caja->plin, 2) ?></td>
      </tr>
      <tr>
        <td>TRNASFERENCIA</td>
        <td>S/. <?= number_format($caja->transferencia, 2) ?></td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <td>TOTAL</td>
        <td>S/. <?= number_format($caja->efectivo +  $caja->tarjeta +  $caja->yape +  $caja->plin +  $caja->transferencia, 2) ?></td>
      </tr>
    </tfoot>
  </table>
  <hr>
  <table border="1px" style="text-align:center">
    <thead>
      <tr>
        <th>VENTAS A CREDITO</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>ACUMULADO</td>
      </tr>
      <tr>
        <td>S/. <?= number_format($caja->credito, 2) ?></td>
      </tr>
    </tbody>
  </table>
  <br>
  <table border="1px" style="text-align:center">
    <thead>
      <tr>
        <th colspan="3">VENTAS CON DESCUENTOS</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>N°</td>
        <td>RECIBO</td>
        <td>DESCUENTO</td>
      </tr>
      <? $descuentos = $this->db->where('caja', $caja->id)->where('descuento>', 0)->where('estado', '1')->get('venta')->result();
      $cdesc = 0; ?>
      <? foreach ($descuentos as $key => $value) { ?>
        <? $cdesc += $value->descuento; ?>
        <tr>
          <td><?= $key + 1 ?></td>
          <td><?= $value->serie . '-' . $value->numero ?></td>
          <td><?= $value->descuento ?></td>
        </tr>
      <? } ?>
    </tbody>
    <tfoot>
      <tr>
        <th colspan="2"> Total</th>
        <th><?= number_format($cdesc, 2); ?></th>
      </tr>
    </tfoot>
  </table>
  <br>

  <table border="1px" style="text-align:center">
    <thead>
      <tr>
        <th colspan="3">VENTAS ANULADAS</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>N°</td>
        <td>RECIBO</td>
        <td>MOTIVO</td>
      </tr>
      <? $anulados = $this->db->where('caja', $caja->id)->where('estado', '3')->get('venta')->result(); ?>
      <? foreach ($anulados as $key => $value) { ?>
        <tr>
          <td><?= $key + 1 ?></td>
          <td><?= $value->serie . '-' . $value->numero ?></td>
          <td style="text-align:left"><?= $value->anular_motivo ?></td>
        </tr>
      <? } ?>
    </tbody>
  </table>
  <br>

  <table border="1px" style="text-align:center">
    <thead>
      <tr>
        <th colspan="3">COBRO DE RECIBOS A CREDITO EN EFECTIVO</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>N°</td>
        <td>RECIBO</td>
        <td>PAGO</td>
      </tr>
      <? $this->db->join('venta as v', 'v.id=i.venta');
      $this->db->where('formapago', 'credito');
      $this->db->where('i.metodopago', 'efectivo');
      $pagcredito = $this->db->where('i.created', $caja->created)->get('ingreso i')->result();
      $pcre = 0; ?>
      <? foreach ($pagcredito as $key => $value) { ?>
        <? $pcre += $value->monto; ?>
        <tr>
          <td><?= $key + 1 ?></td>
          <td><?= $value->serie . '-' . $value->numero ?></td>
          <td><?= $value->monto ?></td>
        </tr>
      <? } ?>
    </tbody>
    <tfoot>
      <tr>
        <th colspan="2"> Total</th>
        <th><?= number_format($pcre, 2); ?></th>
      </tr>
    </tfoot>
  </table>
  <br>

  <table border="1px" style="text-align:center">
    <thead>
      <tr>
        <th colspan="3">COBRO DE RECIBOS A CREDITO NO EFECTIVO</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>N°</td>
        <td>RECIBO</td>
        <td>PAGO</td>
      </tr>
      <? $this->db->join('venta as v', 'v.id=i.venta');
      $this->db->where('formapago', 'credito');
      $this->db->where('i.metodopago!=', 'efectivo');
      $pagcredito = $this->db->where('i.created', $caja->created)->get('ingreso i')->result();
      $pcre = 0; ?>
      <? foreach ($pagcredito as $key => $value) { ?>
        <? $pcre += $value->monto; ?>
        <tr>
          <td><?= $key + 1 ?></td>
          <td><?= $value->serie . '-' . $value->numero ?></td>
          <td><?= $value->monto ?></td>
        </tr>
      <? } ?>
    </tbody>
    <tfoot>
      <tr>
        <th colspan="2"> Total</th>
        <th><?= number_format($pcre, 2); ?></th>
      </tr>
    </tfoot>
  </table>
  <br>

  <table border="1px" style="text-align:center">
    <thead>
      <tr>
        <th colspan="5">GASTOS EN CAJA</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>N°</td>
        <td>CONCEPTO</td>
        <td>DETALLE</td>
        <td>ACUMULADO</td>
      </tr>
      <?php
      $dataEgreso = $this->db->where("caja", $caja->id)->where("tipopago", "EFECTIVO")->get("egreso")->result();
      $totalAcumuladoEgreso = 0;
      foreach ($dataEgreso as $indiceEgreso => $valueEgreso) {
        $dataConceptoEgreso = $this->Controlador_model->get($valueEgreso->concepto, "concepto");
        $totalAcumuladoEgreso += $valueEgreso->montototal;
      ?>
        <tr>
          <td><?= $indiceEgreso + 1 ?></td>
          <td><?= $dataConceptoEgreso->concepto ?></td>
          <td><?= $valueEgreso->observacion ?></td>
          <td>S/. <?= number_format($valueEgreso->montototal, 2) ?></td>
        </tr>
      <?php } ?>
    </tbody>
    <tfoot>
      <tr>
        <td colspan="3">TOTAL</td>
        <td>S/. <?= number_format($totalAcumuladoEgreso, 2) ?></td>
      </tr>
    </tfoot>
  </table>
  <br>
  <table border="1" style="text-align:center">
    <thead>
      <tr>
        <th>+ SALDO INICIAL</th>
        <td>S/. + <?= $caja->saldoinicial ?></td>
      </tr>
      <tr>
        <th>+ EFECTIVO OPERACIONES</th>
        <td>S/. + <?= number_format($caja->efectivo, 2) ?></td>
      </tr>
      <tr>
        <th>- GASTOS EN CAJA</th>
        <td>S/. - <?= number_format($totalAcumuladoEgreso, 2) ?></td>
      </tr>
      <tr>
        <th>DINERO EN CAJA</th>
        <?php
        
          $totalCaja = ($caja->efectivo  + $caja->saldoinicial) - ($totalAcumuladoEgreso);
        ?>
        <th>S/. <?= number_format($totalCaja, 2) ?></th>
      </tr>
    </thead>
  </table>

  <br>
  <table border="1" style="text-align:center">
    <thead>
      <tr>
        <th>DINERO EN CAJA</th>
        <th>MONTO REGISTRADO AL CERRAR CAJA</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <?php
        $monedero = $this->db->where('caja', $caja->id)->get('monedero')->row();
        $montoCerrarCaja = floatval($monedero->montototal);
        $totalCaja = floatval($totalCaja);
        //var_dump($totalCaja);var_dump($montoCerrarCaja);//var_dump($resultCuadre);
        if ($totalCaja >= $montoCerrarCaja) {
          $resultCuadre = $totalCaja - $montoCerrarCaja;
          $estadocaja = $resultCuadre == 0 ? "SIN OBSERVACION EN LA CAJA" : "FALTO ENTREGAR";
          $emoji = $resultCuadre == 0 ? "😀" : "😢";
        } else {
          $resultCuadre = $montoCerrarCaja - $totalCaja;
          $estadocaja = "NO SE ENCONTRO REGISTRO DE";
          $emoji = "😢";
        }
        ?>
        <td>S/. <?= number_format($totalCaja, 2) ?></td>
        <td><?= number_format($montoCerrarCaja, 2) ?></td>
      </tr>
      <tr>
        <?php $diferencia =  $resultCuadre == 0 ? "" : ": S/. " . number_format($resultCuadre, 2); ?>
        <td colspan="2" style="font-size:12px"><b><?= $estadocaja . $diferencia ?></b></td>
      </tr>
      <tr>
        <td colspan="2" style="font-size:40px"><?= $emoji ?></td>
      </tr>
    </tbody>
  </table>
  <br>
  <table>
    <tr>
      <td colspan="4" align="center">PRODUCTOS VENDIDOS</td>
    </tr>
    <tr>
      <td colspan="4" align="center">
        <hr>
      </td>
    </tr>
    <tr>
      <td>PRODUCTO</td>
      <td align="right">PRECIO</td>
      <td align="right">CANTIDAD</td>
      <td align="right">SUBTOTAL</td>
    </tr>
    <tr>
      <td colspan="4" align="center">
        <hr>
      </td>
    </tr>
    <?php $totalventas = 0;
    foreach ($posales as $value) { ?>
      <?php $totalventas += $value->subtotal; ?>
      <tr>
        <td><?= ucwords(strtolower($value->producto)) ?></td>
        <td align="right"><?= $value->precio ?></td>
        <td align="right"><?= $value->cantidad . " UND" ?></td>
        <td align="right"><?= number_format($value->subtotal, 2) ?></td>
      </tr>
    <?php } ?>
    <tr>
      <td colspan="4" align="center">
        <hr>
      </td>
    </tr>
    <tr>
      <td>TOTAL</td>
      <td colspan="3" align="right"><?= number_format($totalventas, 2) ?></td>
    </tr>
  </table>
</body>

</html>