Files

199 lines
7.2 KiB
PHP
Executable File

<?php
namespace App\Console;
use App\Models\Historiale;
use App\Models\Servicio;
use App\Models\User;
use App\Models\Cuentas;
use App\Models\log;
use App\Models\Configuracione;
use App\Models\Historial_cuenta;
use App\Models\ChatContact;
use App\Models\ChatConfig;
use App\Notifications\Vencimiento;
use Carbon\Carbon;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Notification;
use Twilio\Rest\Client;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('userips:clean')->hourly();
// $schedule->exec('nohup php /home/u111286300/domains/sirpremium.com.co/public_html/app/artisan queue:work --sleep=3 --tries=3 > /dev/null 2>&1 &')->everyMinute();
// $schedule->exec('nohup php /var/www/sirpremium/artisan queue:work --sleep=3 --tries=3 > /dev/null 2>&1 &')->everyMinute();
$schedule->command('telescope:prune --hours=24')->daily();
// Eliminar comprobantes de pago con más de 7 días
$schedule->call(function () {
$dir = storage_path('app/public/comprobantes');
if (! is_dir($dir)) return;
foreach (glob($dir . '/*') as $file) {
if (is_file($file) && filemtime($file) < strtotime('-7 days')) {
@unlink($file);
}
}
})->daily();
$schedule->command('tarea:validarCompra')->everyFiveMinutes();
$schedule->call(function () {
$fechaHoy = Carbon::now();
$fechaManana = $fechaHoy->copy()->addDay(); // Obtenemos la fecha de mañana sumando un día a la fecha actual
$consulta_historial = Historiale::whereDate('fecha_final', $fechaManana)->with('cliente')->get();
foreach ($consulta_historial as $item) {
$user = User::find($item->cliente->id);
if (!empty($item->promocion_id)) {
$nombre = $item->promocion->nombre;
}
if (!empty($item->tarifa_id)) {
$nombre = $item->tarifa->servicio->nombre . '-' . $item->tarifa->pantallas . 'P';
}
$notificacion = [
//'valor' => $value['precio'],
'fecha_final' => $item->fecha_final,
'cliente' => $item->cliente->name,
'servicio' => $nombre,
'tipo' => 'vencimiento',
'vendedor' => $item->vendedor->name,
];
Notification::sendNow($user, new Vencimiento($notificacion));
// Notificación por el canal de chat que tenga el cliente
$fechaFmt = Carbon::parse($item->fecha_final)->format('d/m/Y');
$msgCanal = "⚠️ Aviso de vencimiento\n\n"
. "Hola {$item->cliente->name}, tu servicio de {$nombre} vence el {$fechaFmt}.\n\n"
. "¿Quieres renovarlo? Escríbeme aquí y te ayudo 👇";
$contactos = ChatContact::where('user_id', $user->id)->get();
foreach ($contactos as $contacto) {
if ($contacto->canal === 'telegram' && $contacto->canal_id) {
$tokenTg = ChatConfig::get('telegram_token');
if ($tokenTg) {
Http::get("https://api.telegram.org/bot{$tokenTg}/sendMessage", [
'chat_id' => $contacto->canal_id,
'text' => "⚠️ *Aviso de vencimiento*\n\n"
. "Hola {$item->cliente->name}, tu servicio de *{$nombre}* vence el *{$fechaFmt}*.\n\n"
. "¿Quieres renovarlo? Escríbeme aquí y te ayudo 👇",
'parse_mode' => 'Markdown',
]);
}
} elseif ($contacto->canal === 'web') {
$conv = $contacto->activeConversation();
if ($conv) {
\App\Models\ChatMessage::create([
'conversation_id' => $conv->id,
'tipo' => 'bot',
'tipo_ui' => 'text',
'contenido' => $msgCanal,
'leido' => false,
]);
}
}
}
//$fecha=Carbon::parse($item->fecha_final)->format('d/m/Y');
//$message = 'Su '.$nombre.', Vence Hoy, '.$fecha.'. Renueva ahora!!' ;
//$recipients= '+57'.$item->cliente->celular;
//$auth_basic = base64_encode('admin@jaguarws.ga:9u3xxXhHt8maeiOexg07pFKI0rpYazI0');
//$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.'", "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);
//return $response;
// if ($err) {
//
//} else {
//
//}
}
$fechaHoy1 = Carbon::now();
$fechaHoy1 = Carbon::parse($fechaHoy1)->subDay(1);
$consulta_historial1 = Historiale::whereDate('fecha_final', '<', $fechaHoy1)->with('cliente', 'cuentas')->get();
foreach ($consulta_historial1 as $item1) {
foreach ($item1->cuentas->unique() as $item2) {
$logs = new log;
$logs->historial_id = $item1->id;
$logs->cuenta_id = $item2->id;
$logs->save();
Historial_cuenta::where('historial_id', $item1->id)->where('cuenta_id', $item2->id)->delete();
}
}
})->dailyAt(Configuracione::orderby('id', 'desc')->first()->hora);
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__ . '/Commands');
require base_path('routes/console.php');
}
}