Update ShowServicios.php
This commit is contained in:
@@ -110,7 +110,7 @@ class ShowServicios extends Component
|
|||||||
->orderBy('ubicacion')
|
->orderBy('ubicacion')
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
//dd($consulta_servicios);
|
//dd($consulta_servicios);
|
||||||
|
|
||||||
|
|
||||||
return view('livewire.show-servicios', [
|
return view('livewire.show-servicios', [
|
||||||
@@ -174,24 +174,23 @@ class ShowServicios extends Component
|
|||||||
});
|
});
|
||||||
})
|
})
|
||||||
->count();
|
->count();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan;
|
$cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan;
|
||||||
$cantidad_restante = $servicio_tarifa->pantallas - $cantidad_solicitada;
|
$cantidad_restante = $servicio_tarifa->pantallas - $cantidad_solicitada;
|
||||||
|
|
||||||
//Traer todas las cuentas por pantalla 'activo' de los ultimos 15 dias. solo disponibles
|
//Traer todas las cuentas por pantalla 'activo' de los ultimos 15 dias. solo disponibles
|
||||||
$baseQuery = Cuentas::select('*')->whereDate('inicio', '>=', $this->fecha)
|
$baseQuery = Cuentas::select('*')->whereDate('inicio', '>=', $this->fecha)
|
||||||
->whereDate('vencimiento', '>=', $this->fecha_final_1)
|
->whereDate('vencimiento', '>=', $this->fecha_final_1)
|
||||||
->whereDate('vencimiento', '<=', $this->fecha_final_2)
|
->whereDate('vencimiento', '<=', $this->fecha_final_2)
|
||||||
->where('servicio_id', $servicio_tarifa->id)
|
->where('servicio_id', $servicio_tarifa->id)
|
||||||
->where('estado', 'activo')
|
->where('estado', 'activo')
|
||||||
->withCount('historiales')
|
->withCount('historiales')
|
||||||
->where('estado', 'activo');
|
->where('estado', 'activo');
|
||||||
|
|
||||||
$cuentas_pantallas = Cuentas::fromSub($baseQuery, 'alias')
|
$cuentas_pantallas = Cuentas::fromSub($baseQuery, 'alias')
|
||||||
->where('historiales_count', '<=', $cantidad_restante)
|
->where('historiales_count', '<=', $cantidad_restante)
|
||||||
->orderBy('inicio', 'ASC')
|
->orderBy('inicio', 'ASC')
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -210,7 +209,6 @@ class ShowServicios extends Component
|
|||||||
} else {
|
} else {
|
||||||
$this->disponibles = $cuentas_pantallas;
|
$this->disponibles = $cuentas_pantallas;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//dd($this->tarjetaPlan);
|
//dd($this->tarjetaPlan);
|
||||||
@@ -222,7 +220,6 @@ class ShowServicios extends Component
|
|||||||
$this->cantidad_tarjetaPlan = 1;
|
$this->cantidad_tarjetaPlan = 1;
|
||||||
$this->tarjetaPlan = false;
|
$this->tarjetaPlan = false;
|
||||||
$this->tarifa_id = '';
|
$this->tarifa_id = '';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function notificacionPocas($servicio, $restantes)
|
public function notificacionPocas($servicio, $restantes)
|
||||||
@@ -270,30 +267,30 @@ class ShowServicios extends Component
|
|||||||
$usuario_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null;
|
$usuario_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null;
|
||||||
$usuario_id = $usuario_existe;
|
$usuario_id = $usuario_existe;
|
||||||
|
|
||||||
if (is_null($usuario_existe)) {
|
if (is_null($usuario_existe)) {
|
||||||
$usuario = new User;
|
$usuario = new User;
|
||||||
$usuario->name = $this->nombre_tarjetaPlan;
|
$usuario->name = $this->nombre_tarjetaPlan;
|
||||||
$usuario->email = $this->email_tarjetaPlan;
|
$usuario->email = $this->email_tarjetaPlan;
|
||||||
$usuario->password = '123';
|
$usuario->password = '123';
|
||||||
$usuario->celular = '+57' . $this->celular_tarjetaPlan;
|
$usuario->celular = '+57' . $this->celular_tarjetaPlan;
|
||||||
$usuario->save();
|
$usuario->save();
|
||||||
|
|
||||||
$usuario_id = $usuario->id;
|
$usuario_id = $usuario->id;
|
||||||
}
|
}
|
||||||
//dd($this->fechaFinal);
|
//dd($this->fechaFinal);
|
||||||
$historial = new Historiale;
|
$historial = new Historiale;
|
||||||
$historial->fecha_inicio = Carbon::now();
|
$historial->fecha_inicio = Carbon::now();
|
||||||
$historial->fecha_final = $this->fechaFinal;
|
$historial->fecha_final = $this->fechaFinal;
|
||||||
$historial->valor = $total_valor;
|
$historial->valor = $total_valor;
|
||||||
$historial->tipo_pago = 'manual';
|
$historial->tipo_pago = 'manual';
|
||||||
$historial->estado = 'pendiente';
|
$historial->estado = 'pendiente';
|
||||||
$historial->vendedor_id = Auth()->user()->id;
|
$historial->vendedor_id = Auth()->user()->id;
|
||||||
$historial->tarifa_id = $this->tarifa_id;
|
$historial->tarifa_id = $this->tarifa_id;
|
||||||
$historial->cliente_id = $usuario_id;
|
$historial->cliente_id = $usuario_id;
|
||||||
$historial->cantidad = $this->cantidad_tarjetaPlan;
|
$historial->cantidad = $this->cantidad_tarjetaPlan;
|
||||||
$historial->nombre_cliente = $this->nombre_tarjetaPlan;
|
$historial->nombre_cliente = $this->nombre_tarjetaPlan;
|
||||||
$historial->utilidad = $utilidad_total;
|
$historial->utilidad = $utilidad_total;
|
||||||
$historial->save();
|
$historial->save();
|
||||||
|
|
||||||
$pantallas = Tarifas::where('id', $this->tarifa_id)->with('servicio')->first();
|
$pantallas = Tarifas::where('id', $this->tarifa_id)->with('servicio')->first();
|
||||||
|
|
||||||
@@ -344,7 +341,6 @@ class ShowServicios extends Component
|
|||||||
->orderBy('inicio', 'ASC')
|
->orderBy('inicio', 'ASC')
|
||||||
->get();
|
->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
} elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) {
|
} elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) {
|
||||||
|
|
||||||
//Cuentas completas
|
//Cuentas completas
|
||||||
@@ -357,8 +353,6 @@ class ShowServicios extends Component
|
|||||||
->where('estado', 'pendiente')
|
->where('estado', 'pendiente')
|
||||||
->doesntHave('perfil')
|
->doesntHave('perfil')
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
|
|
||||||
} elseif ((int) $pantalla_servicio < (int) $cantidad_solicitada) {
|
} elseif ((int) $pantalla_servicio < (int) $cantidad_solicitada) {
|
||||||
|
|
||||||
$suma2 = $pantalla_servicio - $pantallas;
|
$suma2 = $pantalla_servicio - $pantallas;
|
||||||
@@ -375,8 +369,6 @@ class ShowServicios extends Component
|
|||||||
->where('historiales_count', '<=', $suma2)
|
->where('historiales_count', '<=', $suma2)
|
||||||
->orderBy('inicio', 'ASC')
|
->orderBy('inicio', 'ASC')
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$cuenta = null;
|
$cuenta = null;
|
||||||
$cantidad_necesaria = null;
|
$cantidad_necesaria = null;
|
||||||
@@ -453,7 +445,6 @@ class ShowServicios extends Component
|
|||||||
'position' => 'top'
|
'position' => 'top'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
$eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();
|
$eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();
|
||||||
@@ -468,6 +459,46 @@ class ShowServicios extends Component
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Verificar si hay perfiles duplicados en historial_cuenta recién creados
|
||||||
|
$perfilesDuplicados = Historial_cuenta::select('cuenta_id', 'perfil')
|
||||||
|
->where('historial_id', $historial->id)
|
||||||
|
->groupBy('cuenta_id', 'perfil')
|
||||||
|
->havingRaw('COUNT(*) > 1')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
if ($perfilesDuplicados->isNotEmpty()) {
|
||||||
|
// Eliminar historial_cuenta
|
||||||
|
Historial_cuenta::where('historial_id', $historial->id)->delete();
|
||||||
|
|
||||||
|
// Cancelar historial
|
||||||
|
$historial->update(['estado' => 'cancelado']);
|
||||||
|
|
||||||
|
// Registrar en log_general
|
||||||
|
$detalleError = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email
|
||||||
|
. ' tuvo un error por perfiles duplicados en la compra del servicio '
|
||||||
|
. $servicioNombre . ' - ' . $cuentasMostar
|
||||||
|
. ', cantidad de ' . $this->cantidad_tarjetaPlan
|
||||||
|
. ', valor: $' . $total_valor
|
||||||
|
. '. Se canceló la operación y se eliminaron los perfiles duplicados.';
|
||||||
|
|
||||||
|
$logsGeneral = new Log_general();
|
||||||
|
$logsGeneral->user_id = Auth::user()->id;
|
||||||
|
$logsGeneral->detalle = $detalleError;
|
||||||
|
$logsGeneral->tipo = 'compra_error';
|
||||||
|
$logsGeneral->historial_id = $historial->id;
|
||||||
|
$logsGeneral->save();
|
||||||
|
|
||||||
|
$this->alert('warning', 'Error: Se detectaron perfiles duplicados. Se canceló la compra.', [
|
||||||
|
'position' => 'top'
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Salir antes de confirmar la transacción
|
||||||
|
DB::rollBack();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
DB::commit();
|
DB::commit();
|
||||||
|
|
||||||
$this->tarjetaPlan = false;
|
$this->tarjetaPlan = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user