All files / routes turnos.service.js

80.27% Statements 118/147
64.28% Branches 18/28
100% Functions 5/5
80.27% Lines 118/147

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 1471x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x               2x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x                   3x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 5x 3x 3x 3x 3x       3x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x               2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 2x       2x 1x 1x 1x 1x
//AISLANDO LAS FUNCIONALIDADES DE TURNOS
async function getTurnoAbierto(pool, id_empresa) {
  try {
    const [rows] = await pool.query(
      'SELECT * FROM turnos WHERE id_empresa=? AND estado="abierto" ORDER BY fecha_apertura DESC LIMIT 1',
      [id_empresa]
    );
 
    if (rows.length === 0) {
      return {
        success: false,
        message: 'No hay turno abierto para esta empresa',
        data: null,
      };
    }
 
    return {
      success: true,
      message: 'Turno abierto encontrado',
      data: rows[0],
    };
  } catch (error) {
    console.error('Error al obtener turno abierto:', error);
    return {
      success: false,
      message: 'Error al obtener turno abierto',
      data: null,
    };
  }
}
 
 
 
async function getTotalesSistema(pool, id_empresa, fecha_desde, fecha_hasta) {
  try {
    const [rows] = await pool.query(
      `SELECT 
          SUM(CASE WHEN metodo_pago='efectivo' THEN monto ELSE 0 END) AS efectivo,
          SUM(CASE WHEN metodo_pago='tarjeta' THEN monto ELSE 0 END) AS tarjeta,
          SUM(CASE WHEN metodo_pago='QR' THEN monto ELSE 0 END) AS qr,
          SUM(monto) AS total
       FROM pagos
       WHERE id_empresa=? AND fecha_pago BETWEEN ? AND COALESCE(?, NOW())`,
      [id_empresa, fecha_desde, fecha_hasta || null]
    );
 
    const r = rows[0] || {};
    return {
      efectivo: Number(r.efectivo || 0),
      tarjeta: Number(r.tarjeta || 0),
      qr: Number(r.qr || 0),
      total: Number(r.total || 0),
    };
  } catch (error) {
    console.error('Error al obtener totales del sistema:', error);
    return {
      efectivo: 0,
      tarjeta: 0,
      qr: 0,
      total: 0,
      error: 'Error al obtener totales del sistema',
    };
  }
}
 
 
 
async function getConteoTickets(pool, id_empresa, fecha_desde, fecha_hasta) {
  try {
    const [rows] = await pool.query(
      `SELECT v.tipo, COUNT(*) as cnt
       FROM movimientos m
       JOIN vehiculos v ON v.id_vehiculo = m.id_vehiculo
       WHERE m.id_empresa=? AND m.estado='finalizado'
         AND m.fecha_salida BETWEEN ? AND COALESCE(?, NOW())
       GROUP BY v.tipo`,
      [id_empresa, fecha_desde, fecha_hasta || null]
    );
 
    const map = { carro: 0, moto: 0, bici: 0 };
    rows.forEach((r) => {
      if (map[r.tipo] != null) map[r.tipo] = Number(r.cnt || 0);
    });
 
    return { success: true, total: map.carro + map.moto + map.bici, porTipo: map };
  } catch (error) {
    console.error('Error al obtener conteo de tickets:', error);
    return { success: false, message: 'Error al obtener conteo de tickets', total: 0, porTipo: { carro: 0, moto: 0, bici: 0 } };
  }
}
 
 
 
async function obtenerTurnoActual(pool, id_empresa) {
  try {
    const [rows] = await pool.query(
      'SELECT * FROM turnos WHERE id_empresa=? AND estado="abierto" ORDER BY fecha_apertura DESC LIMIT 1',
      [id_empresa]
    );
 
    const turno = rows[0] || null;
 
    return {
      success: true,
      message: turno ? 'Turno activo encontrado' : 'No hay turno activo',
      data: turno,
    };
  } catch (error) {
    console.error('Error al obtener turno actual:', error);
    return {
      success: false,
      message: 'Error al obtener turno actual',
      data: null,
    };
  }
}
 
 
 
async function abrirTurno(pool, id_empresa, id_usuario, base_inicial, observacion_apertura) {
  try {
    // Validar que no haya turno abierto
    const [abiertos] = await pool.query(
      'SELECT id_turno FROM turnos WHERE id_empresa=? AND estado="abierto"',
      [id_empresa]
    );
 
    if (abiertos.length) {
      return { success: false, message: 'Ya existe un turno abierto', data: null };
    }
 
    // Crear nuevo turno
    const [result] = await pool.query(
      'INSERT INTO turnos (id_empresa,id_usuario,base_inicial,observacion_apertura) VALUES (?,?,?,?)',
      [id_empresa, id_usuario, Number(base_inicial || 0), observacion_apertura || null]
    );
 
    return { success: true, data: { id_turno: result.insertId } };
  } catch (error) {
    console.error('Error abriendo turno:', error);
    return { success: false, message: 'Error abriendo turno' };
  }
}
 
 
 
module.exports = {getTurnoAbierto, getTotalesSistema, getConteoTickets, obtenerTurnoActual, abrirTurno};