154 lines
5.2 KiB
PHP
154 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
use App\Models\Cuentas;
|
|
use App\Models\Historial_cuenta;
|
|
use App\Models\Historiale;
|
|
use App\Models\Role;
|
|
use App\Models\Servicio;
|
|
use App\Models\Tarifas;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Livewire\Component;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class ShowIndexServicio extends Component
|
|
{
|
|
|
|
public $tarifa_id;
|
|
public $fechaFinal;
|
|
public $disponibles;
|
|
public $fechaActual;
|
|
public $valor;
|
|
public $servicio_id;
|
|
|
|
public $celular_tarjetaPlan;
|
|
public $nombre_tarjetaPlan;
|
|
public $email_tarjetaPlan;
|
|
public $cantidad_tarjetaPlan = 1;
|
|
public $total;
|
|
public $rol_id;
|
|
|
|
protected $rules = [
|
|
'nombre_tarjetaPlan' => 'required',
|
|
'cantidad_tarjetaPlan' => 'required',
|
|
'email_tarjetaPlan' => 'required',
|
|
'celular_tarjetaPlan' => 'required',
|
|
];
|
|
|
|
public function render(Request $request)
|
|
{
|
|
if (empty($this->servicio_id)) {
|
|
$this->servicio_id = $request->servicio;
|
|
}
|
|
$this->total = (int) $this->valor * (int)$this->cantidad_tarjetaPlan;
|
|
$this->fechaHoy = Carbon::now();
|
|
$this->fechaHoy = $this->fechaHoy->format('d/m/Y');
|
|
|
|
$fechaActual = Carbon::now();
|
|
$this->fechaActual = Carbon::parse($this->fechaActual);
|
|
|
|
$date = $fechaActual ->subDay(8);
|
|
$this->fecha= $date->format('Y-m-d');
|
|
|
|
if (!empty($this->tarifa_id)) {
|
|
|
|
|
|
$tarifas = Tarifas::with('servicio')->where('id', $this->tarifa_id)->first();
|
|
$this->fecha_final_1 = $today = Carbon::now()->addDays($tarifas->dias-15);
|
|
$this->fecha_final_2 = $today = Carbon::now()->addDays($tarifas->dias+15);
|
|
|
|
$cuentas_creadas = Cuentas::whereDate('inicio','>=',$this->fecha)->where('servicio_id',$this->servicio_id)->where('estado','activo')->get();
|
|
$cuentas_tomadas = Historial_cuenta::whereIn('cuenta_id', $cuentas_creadas->pluck('id'))->count();
|
|
|
|
|
|
$cuentas_completas = Cuentas::whereDate('inicio','>=',$this->fecha)->whereDate('vencimiento','>=',$this->fecha_final_1)->whereDate('vencimiento','<=',$this->fecha_final_2)->where('servicio_id',$tarifas->servicio->id)->where('estado','pendiente')->get();
|
|
$cuentas_completas_tomadas = Historial_cuenta::whereIn('cuenta_id', $cuentas_completas->pluck('id'))
|
|
->count();
|
|
|
|
$this->fechaFinal = $this->fechaActual->addDay($tarifas->dias);
|
|
$this->fechaFinal = $this->fechaFinal->format('d/m/Y');
|
|
if ($tarifas->pantallas == $tarifas->servicio->completa) {
|
|
$this->disponibles = (($cuentas_completas->count()*$tarifas->servicio->completa) - $cuentas_completas_tomadas)/ $tarifas->pantallas;
|
|
}else{
|
|
$this->disponibles = (($cuentas_creadas->count()*$tarifas->servicio->pantallas) - $cuentas_tomadas)/ $tarifas->pantallas;
|
|
}
|
|
|
|
$this->valor= $tarifas->valor;
|
|
}
|
|
|
|
|
|
$this->rol_id = Role::where('nombre','cliente')->value('id');
|
|
|
|
|
|
$planes = Servicio::where('estado','activo')->withMax(['tarifas' => function ($query) {
|
|
$query->where('rol_id', $this->rol_id);
|
|
}],'valor')->withMin(['tarifas' => function ($query) {
|
|
$query->where('rol_id', $this->rol_id);
|
|
}],'valor')->with(['tarifas' => function ($query) {
|
|
$query->where('rol_id', $this->rol_id);
|
|
}])->get();
|
|
|
|
foreach ($planes as $plan ) {
|
|
if($plan->id == $this->servicio_id){
|
|
$servicio = $plan;
|
|
}
|
|
|
|
}
|
|
return view('livewire.show-index-servicio',[
|
|
'servicio' =>$servicio,
|
|
'planes' =>$planes
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
/* ---------------- Btn Comprar ---------------- */
|
|
public function comprar()
|
|
{
|
|
$this->validate();
|
|
|
|
$total_valor = (int) $this->valor * (int)$this->cantidad_tarjetaPlan;
|
|
|
|
$usuario_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null;
|
|
|
|
if (is_null($usuario_existe)) {
|
|
$usuario = new User;
|
|
$usuario->name = $this->nombre_tarjetaPlan;
|
|
$usuario->email = $this->email_tarjetaPlan;
|
|
$usuario->password = '123';
|
|
$usuario->celular = $this->celular_tarjetaPlan;
|
|
$usuario->save();
|
|
|
|
$usuario_id = $usuario->id;
|
|
}else{
|
|
|
|
$usuario_id = $usuario_existe;
|
|
|
|
}
|
|
|
|
$historial = new Historiale;
|
|
$historial->fecha_inicio = $this->fechaActual;
|
|
$historial->fecha_final = $this->fechaFinal;
|
|
$historial->valor = $total_valor ;
|
|
$historial->tipo_pago = 'manual';
|
|
$historial->estado = 'pendiente';
|
|
if (empty(Auth::user())) { // SI esta logueado o no
|
|
$historial->vendedor_id = Role::where('nombre','super')->first()->usuarios->first()->id;
|
|
}else{
|
|
$historial->vendedor_id = Auth::user()->id;
|
|
}
|
|
$historial->tarifa_id = $this->tarifa_id;
|
|
$historial->cliente_id = $usuario_id;
|
|
$historial->cantidad = $this->cantidad_tarjetaPlan;
|
|
$historial->save();
|
|
|
|
|
|
return redirect('/comprar?monto='.$historial->valor.'&id='.$historial->id);
|
|
}
|
|
|
|
|
|
}
|