Files
sirpremiumv2/app/Http/Livewire/ShowClientes.php
T
2025-04-04 20:11:51 -05:00

313 lines
10 KiB
PHP
Executable File

<?php
namespace App\Http\Livewire;
use App\Models\Historial_cuenta;
use App\Models\Historiale;
use App\Models\User;
use App\Notifications\Vencimiento;
use Carbon\Carbon;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use App\Models\Configuracione;
use Livewire\WithPagination;
use Jantinnerezo\LivewireAlert\LivewireAlert;
class ShowClientes extends Component
{
use LivewireAlert;
use WithPagination;
public $date;
public $buscar;
public $fechaActual;
public $verCliente = false;
public $ver_nombre;
public $ver_email;
public $ver_fecha_inicio;
public $ver_fecha_vencimiento;
public $ver_paquete;
public $precio_paquete;
public $dias_paquete;
public $consulta_historial;
public $venceHoy;
public $vencidosHoy = false;
public $visto = false;
public $vencidHoy;
public $configuracion;
public $msj;
public $entradas = 10;
public function render()
{
$fechaHoy = Carbon::now()->addDay(1);
$this->fechaActual = date('d/m/Y');
$fechaVencimiento = Carbon::now()->subDay(30);
$consulta_clientes = Historiale::
where('vendedor_id', Auth()->user()->id)->
whereDate('fecha_final', '>', $fechaVencimiento);
$this->configuracion = User::where('id', Auth::user()->id)->whereDate('updated_at', '!=', Carbon::now())->first();
if ($this->configuracion) {
$this->vencidosHoy = true;
$this->vencidHoy = $consulta_clientes->whereDate('fecha_final', $fechaHoy)->get();
$this->configuracion = User::where('id', Auth::user()->id)->update(['visto' => true]);
}
$this->msj = Configuracione::orderby('id', 'desc')->select('msj_compra', 'msj_wp', 'clave_1', 'clave_2', 'clave_3', 'clave_4', 'clave_5', 'clave_6', 'iptv')->first();
if (!empty($this->buscar)) {
$consulta_clientes = $consulta_clientes->where(function ($query) {
$query->whereHas('cuentas', function ($subQuery) {
$subQuery->where('correo', 'ILIKE', '%' . $this->buscar . '%');
})->orWhereHas('cliente', function ($subQuery) {
$subQuery->where('email', 'ILIKE', '%' . $this->buscar . '%');
});
});
}
if ($this->venceHoy) {
$consulta_clientes = $consulta_clientes->whereDate('fecha_final', $fechaHoy);
}
$consulta_clientes = $consulta_clientes->with('tarifa', 'promocion', 'cliente')
->orderBy('id', 'DESC')
->paginate($this->entradas);
return view('livewire.show-clientes', [
'clientes' => $consulta_clientes
]);
}
public function verCliente($id)
{
$this->consulta_historial = Historiale::with('promocion', 'tarifa', 'cliente', 'cuentas')->where('id', $id)->first();
$this->verCliente = true;
}
public function consultaPassword($id)
{
$historiale = Historiale::with('promocion', 'tarifa', 'cliente', 'cuentas')->where('id', $id)->first();
if ($historiale && $historiale->cuentas->isNotEmpty()) {
// Assuming 'password' is a property on the Cuentas model
return $historiale->cuentas->first()->password ?? '';
} else {
return '';
}
}
public function enviarNotificacion($id)
{
$consulta_historial = Historiale::where('id', $id)->with('tarifa', 'cliente')->first();
$user = User::find($consulta_historial->cliente);
$notificacion = [
//'valor' => $value['precio'],
'fecha_final' => $consulta_historial->fecha_final,
'cliente' => $consulta_historial->cliente->name,
'servicio' => $consulta_historial->tarifa->servicio->nombre . '-' . $consulta_historial->tarifa->pantallas . 'pantallas',
'tipo' => 'vencimiento',
'vendedor' => auth()->user()->username,
];
Notification::send($user, new Vencimiento($notificacion));
$this->alert('success', 'Notificación enviada', [
'position' => 'top'
]);
}
public function enviarNotificacionPromo($id)
{
$consulta_historial = Historiale::where('id', $id)->with('promocion', 'cliente')->first();
$user = User::find($consulta_historial->cliente);
$nombre = $consulta_historial->promocion->nombre;
$descripcion = $consulta_historial->promocion->descripcion;
$notificacion = [
//'valor' => $value['precio'],
'fecha_final' => $consulta_historial->fecha_final,
'cliente' => $consulta_historial->cliente->name,
'servicio' => 'PROMO ' . $nombre . '-' . $descripcion,
'tipo' => 'vencimiento',
'vendedor' => auth()->user()->username,
];
Notification::send($user, new Vencimiento($notificacion));
$this->alert('success', 'Notificación enviada', [
'position' => 'top'
]);
}
public function notSMSpromo($id)
{
$consulta_historial = Historiale::where('id', $id)->with('promocion', 'cliente')->first();
$user = User::find($consulta_historial->cliente);
$nombre = $consulta_historial->promocion->nombre;
$fecha = Carbon::parse($consulta_historial->fecha_final)->format('Y/m/d');
$fecha_humana = Carbon::parse($consulta_historial->fecha_final)->diffForHumans();
//dd($fecha_humana);
//$descripcion = $consulta_historial->promocion->descripcion;
$message = 'Su ' . $nombre . ', Vence ' . $fecha_humana . ', ' . $fecha . '. Renueva ahora!!';
$recipients = '+57' . $consulta_historial->cliente->celular;
$this->sendMessage($message, $recipients);
}
public function notSMS($id)
{
$consulta_historial = Historiale::where('id', $id)->with('tarifa', 'cliente')->first();
$user = User::find($consulta_historial->cliente);
$nombre = $consulta_historial->tarifa->servicio->nombre . '-' . $consulta_historial->tarifa->pantallas . 'P';
$fecha = Carbon::parse($consulta_historial->fecha_final)->format('Y/m/d');
$fecha_humana = Carbon::parse($consulta_historial->fecha_final)->diffForHumans();
$message = 'Su ' . $nombre . ', Vence ' . $fecha_humana . ', ' . $fecha . '. Renueva ahora!!';
$recipients = '+57' . $consulta_historial->cliente->celular;
$this->sendMessage($message, $recipients);
}
public function sendMessage($message, $recipients)
{
// try {
// $account_sid = getenv("TWILIO_SID");
// $auth_token = getenv("TWILIO_TOKEN");
// $twilio_number = getenv("TWILIO_FROM");
// $client = new Client($account_sid, $auth_token);
// $client->messages->create($recipients,
// ['from' => $twilio_number, 'body' => $message] );
// $this->alert('success', 'Notificación enviada por SMS! ', [
// 'position' => 'top'
// ]);
// }catch(Exception $e){
// $this->alert('warning', 'Error enviando SMS!'.$e->getMessage(), [
// 'position' => 'top'
// ]);
// }
$auth_basic = base64_encode('admin@jaguarws.ga:9u3xxXhHt8maeiOexg07pFKI0rpYazI0');
//dd(getenv('USER_LABSMOBILE'));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.labsmobile.com/json/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{"message":"' . $message . '","nofilter":"1, "tpoa":"Sender","recipient":[{"msisdn":"' . $recipients . '"}]}',
CURLOPT_HTTPHEADER => array(
"Authorization: Basic " . $auth_basic,
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$this->alert('warning', 'Error enviando SMS!' . $err, [
'position' => 'top'
]);
} else {
//dd($response);
if (!empty(json_decode($response)->subid)) {
$this->alert('success', 'Notificación enviada por SMS! ' . json_decode($response)->subid, [
'position' => 'top'
]);
} else {
$this->alert('warning', 'Servicio de SMS en mantenimiento!' . json_decode($response)->code, [
'position' => 'top'
]);
}
}
}
public function cerrar()
{
$this->configuracion = User::where('id', Auth::user()->id)->update(['visto' => true]);
//dd($this->configuracion);
$this->visto = true;
$this->vencidosHoy = false;
}
public function renovar($id)
{
$historiale = Historiale::with('tarifa', 'cuentas')->find($id);
if (!$historiale) {
return;
}
$tarifa = $historiale->tarifa;
$diasTarifa = $tarifa->dias;
$valorTarifa = $tarifa->valor;
$cuenta = $historiale->cuentas->first();
$vencimientoCuenta = Carbon::parse($cuenta->vencimiento);
$nuevaFechaVencimiento = $vencimientoCuenta->addDays($diasTarifa);
$newFechaInicio = Carbon::parse($historiale->fecha_inicio)->addDays($diasTarifa);
$newFechaFinal = Carbon::parse($historiale->fecha_final)->addDays($diasTarifa);
$nuevoHistorial = $historiale->replicate();
$nuevoHistorial->fecha_inicio = $newFechaInicio;
$nuevoHistorial->fecha_final = $newFechaFinal;
$nuevoHistorial->save();
$pantallas = $cuenta->servicio->completa;
//dd($pantallas);
for ($i = 1; $i <= $pantallas; $i++) {
$historial_cuenta = new Historial_cuenta;
$historial_cuenta->historial_id = $nuevoHistorial->id;
$historial_cuenta->cuenta_id = $cuenta->id;
$historial_cuenta->perfil = $i;
$historial_cuenta->save();
}
$cuenta->vencimiento = $nuevaFechaVencimiento;
$cuenta->save();
$user = Auth::user();
$user->saldo->valor -= $valorTarifa;
$user->saldo->save();
$this->alert('success', 'Registro renovado con éxito. Nuevas fechas aplicadas.', [
'position' => 'top'
]);
}
}