Files
stradaautopartes/app/Http/Livewire/DashboardController.php
2023-08-16 16:13:34 -07:00

85 lines
3.0 KiB
PHP

<?php
namespace App\Http\Livewire;
use App\Models\CajaMovimiento;
use App\Models\Movimiento;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
class DashboardController extends Component
{
const ESTADO_VENTA =1, ESTADO_ABONO = 2, ESTADO_CANCELACION = 3;
public $cajaModal = false, $cajaInicial;
public function render()
{
// dd(Movimiento::where([
// ['estado_movimiento_id','=', DashboardController::ESTADO_VENTA],
// [DB::raw('DATE(created_at)'),'=',Carbon::now()->format('Y-m-d')]
// ])->count());
$movimiento = $this->totalDiaTipoMovimiento(DashboardController::ESTADO_VENTA);
$abono = $this->totalDiaTipoMovimiento(DashboardController::ESTADO_ABONO);
$cancelacion = $this->totalDiaTipoMovimiento(DashboardController::ESTADO_CANCELACION);
$this->validarCaja();
return view('dashboard',[
'movimiento' => $movimiento,
'abono' => $abono,
'cancelacion' => $cancelacion,
'num_movimiento' => Movimiento::where([
['estado_movimiento_id','=', DashboardController::ESTADO_VENTA],
[DB::raw('DATE(created_at)'),'=',Carbon::now()->format('Y-m-d')]
])->count(),
'num_abono' => Movimiento::where([
['estado_movimiento_id','=', DashboardController::ESTADO_ABONO],
[DB::raw('DATE(created_at)'),'=',Carbon::now()->format('Y-m-d')]
])->count(),
'num_cancelacion' => Movimiento::where([
['estado_movimiento_id','=', DashboardController::ESTADO_CANCELACION],
[DB::raw('DATE(created_at)'),'=',Carbon::now()->format('Y-m-d')]
])->count(),
'caja' => CajaMovimiento::cajaInicial(),
]);
}
public function totalDiaTipoMovimiento($tipo_movimiento)
{
return
Movimiento::where([
['estado_movimiento_id','=', $tipo_movimiento],
[DB::raw('DATE(created_at)'),'=',Carbon::now()->format('Y-m-d')],
])->sum('pago_efectivo') +
Movimiento::where([
['estado_movimiento_id','=', $tipo_movimiento],
[DB::raw('DATE(created_at)'),'=',Carbon::now()->format('Y-m-d')],
])->sum('pago_tarjeta') +
Movimiento::where([
['estado_movimiento_id','=', $tipo_movimiento],
[DB::raw('DATE(created_at)'),'=',Carbon::now()->format('Y-m-d')],
])->sum('pago_vales');
}
public function validarCaja()
{
if(!CajaMovimiento::cajaInicial()->count())
{
$this->cajaModal = true;
}
}
public function save()
{
$this->validate([
'cajaInicial' => 'required|numeric',
]);
CajaMovimiento::create([
'cantidad' => $this->cajaInicial,
'estado_caja_movimiento_id' => CajaMovimiento::CODIGO_CAJA,
'user_id' => auth()->user()->id,
]);
$this->cajaModal = false;
}
}