<?php 
ob_start();
session_start();
include ("../_init.php");

if (!is_loggedin()) {
  redirect(root_url() . '/index.php?redirect_to=' . url());
}

if (user_group_id() != 1 && !has_permission('access', 'read_quotation')) {
  redirect(root_url() . '/'.ADMINDIRNAME.'/dashboard.php');
}

$document->setTitle(trans('LISTA DE VENTAS'));

$document->addScript('../assets/itsolution24/angular/modals/QuotationViewModal.js');
$document->addScript('../assets/itsolution24/angular/controllers/QuotationController.js');

include("header.php"); 
include ("left_sidebar.php") ;
?>

<div class="content-wrapper" ng-controller="QuotationController">
  <section class="content-header">
    <?php include ("../_inc/template/partials/apply_filter.php"); ?>
    <h1>
      <?php echo trans('VENTAS'); ?>
      <small><?php echo store('name'); ?></small>
    </h1>
    <ol class="breadcrumb">
      <li><a href="dashboard.php"><i class="fa fa-dashboard"></i> <?php echo trans('text_dashboard'); ?></a></li>
      <li>
        <?php if (isset($request->get['box_state']) && $request->get['box_state']=='open'): ?>
          <a href="quotation.php"><?php echo trans('VENTAS Y ESTADOS'); ?></a>
        <?php else: ?>
          <?php echo trans('Ventas'); ?>
        <?php endif; ?>
      </li>
      <?php if (isset($request->get['box_state']) && $request->get['box_state']=='open'): ?>
        <li class="active"><?php echo trans('text_add'); ?></li>
      <?php endif; ?>
    </ol>
  </section>

  <section class="content">
    <?php if(DEMO) : ?>
    <div class="box">
      <div class="box-body">
        <div class="alert alert-info">
          <p><span class="fa fa-info-circle"></span> <?php echo $demo_text; ?></p>
        </div>
      </div>
    </div>
    <?php endif; ?>

    <div class="box box-primary">
      <div class="box-header with-border">
        <h3 class="box-title"><?php echo trans('Detalle de ventas de productos'); ?></h3>
      </div>
      <div class="box-body">
        <form method="GET" class="form-inline">
          <input type="hidden" name="id_producto" value="<?php echo $request->get['id_producto']; ?>">
          <div class="form-group">
            <label for="fecha_desde">Desde:</label>
            <input type="date" name="fecha_desde" class="form-control" value="<?php echo isset($_GET['fecha_desde']) ? $_GET['fecha_desde'] : ''; ?>">
          </div>
          <div class="form-group">
            <label for="fecha_hasta">Hasta:</label>
            <input type="date" name="fecha_hasta" class="form-control" value="<?php echo isset($_GET['fecha_hasta']) ? $_GET['fecha_hasta'] : ''; ?>">
          </div>
          <button type="submit" class="btn btn-primary">Filtrar</button>
        </form>

        <div class="table-responsive">
          <?php 
          include "conexion_a.php";
          $id_producto = $request->get['id_producto'];

          $total = 0;
          $total_return = 0;
          $salida = 0;

          $fecha_desde = isset($_GET['fecha_desde']) ? $_GET['fecha_desde'] : '';
          $fecha_hasta = isset($_GET['fecha_hasta']) ? $_GET['fecha_hasta'] : '';
          $condiciones_fecha = "";
          if ($fecha_desde && $fecha_hasta) {
              $condiciones_fecha = " AND DATE(sIn.created_at) BETWEEN '$fecha_desde' AND '$fecha_hasta'";
          } elseif ($fecha_desde) {
              $condiciones_fecha = " AND DATE(sIn.created_at) >= '$fecha_desde'";
          } elseif ($fecha_hasta) {
              $condiciones_fecha = " AND DATE(sIn.created_at) <= '$fecha_hasta'";
          }

          $query = mysqli_query($conexion, "
              SELECT * FROM selling_item sIt 
              INNER JOIN selling_info sIn ON sIt.invoice_id = sIn.invoice_id 
              WHERE sIt.item_id = $id_producto 
              AND sIn.status != 0 
              $condiciones_fecha 
              ORDER BY sIn.created_at DESC
          ");
          $result = mysqli_num_rows($query);
          ?>

          <table class="table table-bordered table-striped">
            <thead>
              <tr class="bg-primary">
                <th>Fecha de venta</th>
                <th>Codigo producto</th>
                <th>Nombre de producto</th>
                <th>Cliente</th>
                <th>Vendido</th>
                <th>Devuelto</th>
                <th>Estado de entrega</th>
                <th>Numero Comprobante</th>
              </tr>
            </thead>
            <tbody>
              <?php
              if ($result > 0) {
                while ($data = mysqli_fetch_assoc($query)) {
                  $customer = get_the_customer($data['customer_id']);
                  $total += $data['item_quantity'];
                  $total_return += $data['return_quantity'];
                  $salida = $total - $total_return;
              ?>
              <tr<?php if ($data['entrega_estado'] != 2) echo ' class="warning"'; ?>>
                <td><?php echo $data['created_at']; ?></td>
                <td><?php echo $data['item_id']; ?></td>
                <td><?php echo $data['item_name']; ?></td>
                <td><?php echo $customer['customer_name']; ?></td>
                <td><?php echo $data['item_quantity']; ?></td>
                <td><?php echo $data['return_quantity']; ?></td>
                <td>
                  <?php 
                  switch ($data['entrega_estado']) {
                    case 1: echo 'DESPACHADO'; break;
                    case 2: echo 'ENTREGADO'; break;
                    case 0:
                    case 3: echo 'PENDIENTE'; break;
                    default: echo 'DESCONOCIDO';
                  }
                  ?>
                </td>
                <td><a href="view_invoice.php?invoice_id=<?php echo $data['invoice_id']; ?>" class="btn btn-link"><?php echo $data['invoice_id']; ?></a></td>
              </tr>
              <?php }
              } ?>
            </tbody>
            <tfoot>
              <tr class="bg-success">
                <td colspan="4">Total:</td>
                <td><?php echo $total; ?></td>
                <td><?php echo $total_return; ?></td>
                <td><?php echo $salida; ?></td>
                <td></td>
              </tr>
            </tfoot>
          </table>
        </div>
      </div>
    </div>
  </section>
</div>

<?php include ("footer.php"); ?>
