<?php

namespace App\Modules\Stats;

use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use App\Modules\Bookings\Repair as Repair;
use App\Http\Controllers\BaseController;

class StatsController extends BaseController
{
    /**
     *	The layout that should be used for responses.
     */
    protected $layout = 'layouts.master';

    public function getIndex()
    {
        if ((! Auth::user()->isMemberOf(4)) && (! Auth::user()->isMemberOf(14))) {
            return Redirect::to('/');
        }

        $data = [
            'repairs_today' => Repair::repairs()->completed()->today()->count(),
            'income'        => [
                'today'       => Repair::completed()->today()->sum('labour'),
                'week'        => Repair::completed()->thisWeek()->sum('labour'),
                'month'       => Repair::completed()->thisMonth()->sum('labour'),
                'booking_fee' => Repair::completed()->thisMonth()->whereNotNull('booking_fee_id')->count() * Config::get('settings.booking_fee'),
            ],
        ];

        $this->layout->content = View::make('stats::index', $data);
    }
}
