<?php

namespace App\Http\Livewire\Backend;

use App\Models\OrderManagement\Order;
use App\Models\OrderManagement\Scopes\OrderGlobalScope;
use App\Models\SettingManagement\Status;
use Livewire\Component;

class DashboardStatisticsComponent extends Component{
    public $currentMonthEarning = 0;
    public $currentAnnualEarning = 0;
    public $currentMonthOrderNew = 0;
    public $currentMonthOrderFinished = 0;


    public function mount(){

        $year = date('Y');
        $month = date('m');
        $builder = Order::withoutGlobalScope(OrderGlobalScope::class);
        $this->currentMonthEarning = $builder->whereIn('status_code',Order::getStatusesCollected())->whereMonth('updated_at', $month)->sum('total');
        $this->currentAnnualEarning = $builder->whereIn('status_code',Order::getStatusesCollected())->whereYear('updated_at', $year)->sum('total');
        $this->currentMonthOrderNew = $builder->whereMonth('created_at', $month)->count();
        $this->currentMonthOrderFinished = $builder->whereIn('status_code',Order::getStatusesCollected())->whereMonth('updated_at', $month)->count();
    }

    public function render(){
        return view('livewire.backend.dashboard-statistics-component');
    }
}
