107 lines
3.2 KiB
PHP
107 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
use App\Models\Cuentas;
|
|
use App\Models\Historial_cuenta;
|
|
use App\Models\Historiale;
|
|
use App\Models\Promociones;
|
|
use App\Models\Role;
|
|
use App\Models\Saldo;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Livewire\Component;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class ShowIndexPromo extends Component
|
|
{
|
|
public $nombre_comprarPromo;
|
|
public $celular_comprarPromo;
|
|
public $email_comprarPromo;
|
|
public $cantidad_comprarPromo = 1;
|
|
public $fechaHoy;
|
|
public $fechaFinal;
|
|
public $promo_id;
|
|
|
|
protected $rules = [
|
|
'nombre_comprarPromo' => 'required',
|
|
'celular_comprarPromo' => 'required',
|
|
'email_comprarPromo' => 'required',
|
|
'cantidad_comprarPromo' => 'required',
|
|
];
|
|
public function render(Request $request)
|
|
{
|
|
if (empty($this->promo_id)) {
|
|
$this->promo_id = $request->promo;
|
|
}
|
|
|
|
$consulta_promo = Promociones::find($this->promo_id);
|
|
$this->consulta= $consulta_promo;
|
|
|
|
$promociones= Promociones::where('visible','true')->get();
|
|
$fechaActual = Carbon::now();
|
|
$this->fechaHoy = $fechaActual;
|
|
//dd($consulta_promo);
|
|
$this->fechaFinal = $fechaActual->addDay($consulta_promo->dias);
|
|
|
|
|
|
|
|
return view('livewire.show-index-promo',[
|
|
'promo' => $consulta_promo,
|
|
'promociones'=>$promociones
|
|
]);
|
|
}
|
|
|
|
public function comprar()
|
|
{
|
|
|
|
$this->validate();
|
|
|
|
|
|
$usuario_existe = User::firstWhere('email', $this->email_comprarPromo)->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;
|
|
}
|
|
if (empty(Auth::user())) {
|
|
$historial = new Historiale();
|
|
$historial->fecha_inicio = $this->fechaHoy;
|
|
$historial->fecha_final = $this->fechaFinal;
|
|
$historial->valor = $this->consulta->precio *$this->cantidad_comprarPromo;
|
|
$historial->tipo_pago = 'manual';
|
|
$historial->estado = 'pendiente';
|
|
$historial->vendedor_id = Role::where('nombre','super')->first()->usuarios->first()->id;
|
|
$historial->promocion_id = $this->consulta->id;
|
|
$historial->cliente_id = $usuario_id;
|
|
$historial->cantidad = $this->cantidad_comprarPromo;
|
|
$historial->save();
|
|
}else{
|
|
$historial = new Historiale;
|
|
$historial->fecha_inicio = $this->fechaHoy;
|
|
$historial->fecha_final = $this->fechaFinal;
|
|
$historial->valor = $this->consulta->precio *$this->cantidad_comprarPromo ;
|
|
$historial->tipo_pago = 'manual';
|
|
$historial->estado = 'pendiente';
|
|
$historial->vendedor_id = Auth::user()->id;
|
|
$historial->promocion_id = $this->consulta->id;
|
|
$historial->cliente_id = $usuario_id;
|
|
$historial->cantidad = $this->cantidad_comprarPromo;
|
|
$historial->save();
|
|
}
|
|
|
|
return redirect('/comprar?monto='.$historial->valor.'&id='.$historial->id);
|
|
|
|
}
|
|
}
|