This commit is contained in:
Juan Felipe Duarte
2025-07-31 15:19:22 -07:00
parent ca5fbd454a
commit c7586cf6cd
13 changed files with 215 additions and 34 deletions
+49 -4
View File
@@ -256,6 +256,8 @@ class ShowPromociones extends Component
$historial->utilidad = $this->utilidad * $this->cantidad_comprarPromo;
$historial->save();
$utilidad_total = (int) $this->utilidad * (int) $this->cantidad_comprarPromo;
$this->historial = $historial;
for ($p = 0; $p < (int) $this->cantidad_comprarPromo; $p++) {
@@ -290,8 +292,6 @@ class ShowPromociones extends Component
->where('historiales_count', '=', 0)
->orderBy('inicio', 'ASC')
->first();
} else {
$baseQuery = Cuentas::select('*')->where('servicio_id', $servicio_id)
->whereDate('inicio', '>=', $this->fecha)
@@ -304,10 +304,9 @@ class ShowPromociones extends Component
->where('historiales_count', '<=', (int) $faltante)
->orderBy('inicio', 'ASC')
->first();
}
if (!is_null($cuenta)) {
$perfil = $cuenta->historiales_count;
@@ -379,6 +378,52 @@ class ShowPromociones extends Component
$logsGeneral->historial_id = $historial->id;
$logsGeneral->save();
// Si el campo subvendedor tiene id entonces la utilidad va a su subvendedor padre
if (Auth::user()->subvendedor_id) {
if ($utilidad_total > 0) {
$subvendedorPadre = User::with('saldo')->find(Auth::user()->subvendedor_id);
if ($subvendedorPadre && $subvendedorPadre->saldo) {
// Sumar la utilidad al saldo actual
$nuevoSaldo = $subvendedorPadre->saldo->valor + $utilidad_total;
// Actualizar el saldo
$subvendedorPadre->saldo->update([
'valor' => $nuevoSaldo
]);
// Registrar en el log la utilidad generada
$logsGeneral = new Log_general();
$logsGeneral->user_id = Auth::user()->id;
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email .
' ha comprado la promocion ' . $this->consulta->nombre . ' - ' . $this->consulta->descripcion .
' cantidad de ' . $this->cantidad_tarjetaPlan .
' por un valor de $' . $this->historial->valor .
', Saldo anterior: $' . $saldo_anterior .
', Nuevo saldo disponible: $' . $nuevo_saldo .
'. Esta compra generó una utilidad de $' . $utilidad_total .
' al usuario padre ' . $subvendedorPadre->name . ' - ' . $subvendedorPadre->email . '.';
$logsGeneral->historial_id = $historial->id;
$logsGeneral->tipo = 'compra';
$logsGeneral->save();
}
}
} else {
// Log sin utilidad al padre
$logsGeneral = new Log_general();
$logsGeneral->user_id = Auth::user()->id;
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email .
' ha comprado la promocion ' . $this->consulta->nombre . ' - ' . $this->consulta->descripcion .
' cantidad de ' . $this->historial->cantidad .
' por un valor de $' . $this->historial->valor .
', Saldo anterior: $' . $saldo_anterior .
', Nuevo saldo disponible: $' . $nuevo_saldo;
$logsGeneral->historial_id = $historial->id;
$logsGeneral->tipo = 'compra';
$logsGeneral->save();
}
$this->modalComprarPromo = false;
$this->credenciales($historial);
} else {