69 lines
2.5 KiB
PHP
Executable File
69 lines
2.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
use App\Models\Historiale;
|
|
use App\Models\Role;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Livewire\Component;
|
|
use Jantinnerezo\LivewireAlert\LivewireAlert;
|
|
use Livewire\WithPagination;
|
|
|
|
class ShowReportes extends Component
|
|
{
|
|
use LivewireAlert;
|
|
use WithPagination;
|
|
|
|
public $fecha;
|
|
public $mes;
|
|
public $usuario;
|
|
|
|
public function render()
|
|
{
|
|
if (!empty($this->usuario)) {
|
|
$id_user = $this->usuario;
|
|
|
|
$consulta_suma = Historiale::where('vendedor_id', $id_user)->sum('valor');
|
|
$consulta_cantidad = Historiale::where('vendedor_id', $id_user)->count();
|
|
|
|
if (!empty($this->mes)) {
|
|
$consulta_suma = Historiale::where('vendedor_id', $id_user)->whereMonth('created_at', $this->mes)->sum('valor');
|
|
$consulta_cantidad = Historiale::where('vendedor_id', $id_user)->whereMonth('created_at', $this->mes)->count();
|
|
}
|
|
} elseif (empty($this->usuario)) {
|
|
if (auth()->user()->rol->nombre == "super" || auth()->user()->rol->nombre == "administrador") {
|
|
$consulta_suma = Historiale::sum('valor');
|
|
$consulta_cantidad = Historiale::count();
|
|
} else {
|
|
$consulta_suma = Historiale::where('vendedor_id', auth()->user()->id)->sum('valor');
|
|
$consulta_cantidad = Historiale::where('vendedor_id', auth()->user()->id)->count();
|
|
}
|
|
|
|
if (!empty($this->mes)) {
|
|
$consulta_suma = Historiale::where('vendedor_id', auth()->user()->id)->whereMonth('created_at', $this->mes)->sum('valor');
|
|
$consulta_cantidad = Historiale::where('vendedor_id', auth()->user()->id)->whereMonth('created_at', $this->mes)->count();
|
|
}
|
|
}
|
|
|
|
$distribuidores = Role::where('nombre', 'distribuidor')->orWhere('nombre', 'mayorista')
|
|
->with(['usuarios' => function ($query) {
|
|
$query->orderBy('name', 'asc');
|
|
}])->first();
|
|
|
|
if (!empty($this->usuario)) {
|
|
$id_user = $this->usuario;
|
|
|
|
$historial = Historiale::where('cliente_id', $id_user)->orderBy('id', 'DESC')->paginate(10);
|
|
} else {
|
|
$historial = Historiale::where('cliente_id', auth()->user()->id)->orderBy('id', 'DESC')->paginate(10);
|
|
}
|
|
|
|
return view('livewire.show-reportes', [
|
|
'suma' => $consulta_suma,
|
|
'cantidad' => $consulta_cantidad,
|
|
'roles' => $distribuidores,
|
|
'historiales' => $historial,
|
|
]);
|
|
}
|
|
}
|