%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/local/apache/htdocs/app/Http/Controllers/
Upload File :
Create Path :
Current File : //usr/local/apache/htdocs/app/Http/Controllers/HomeController.php

<?php

namespace App\Http\Controllers;

use App\Models\Commande;
use App\Models\Commercial;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index(Request $request)
    {
        $m = gmdate('m');
        $m1 = $m < 12 ? $m + 1 : 1;
        $y = gmdate('Y');
        $ye = $y + 1;
        $y1 = $m1 < $m ? $y + 1 : $y;
        $start_month = Carbon::createFromFormat('d-m-Y H:i:s', "01-{$m}-{$y} 00:00:00");
        $end_month = Carbon::createFromFormat('d-m-Y H:i:s', "01-{$m1}-{$y1} 00:00:00");
        $paid_month = $paid_months = Commande::whereBetween('created_at', [$start_month->format('Y-m-d')." 00:00:00", $end_month->format('Y-m-d')." 00:00:00"])->whereNotIn('status', ['cancelled', 'returned'])->get();

        if( $request->user()->hasRole('commercial') ) {
            $paid_month = $paid_months->filter(function($commande) use ($request) {
                return $commande->commercial->hasRole('commercial') && $request->user()->id == $commande->commercial_id;
            });
        }

        $paid_month = $paid_month->sum(function($commande) {
            return $commande->paid_reglements->sum('montant');
        });
        $paid_year = Commande::whereBetween('created_at', ["{$y}-01-01 00:00:00", "{$ye}-01-01 00:00:00"])->whereNotIn('status', ['cancelled', 'returned'])->get();
        if( $request->user()->hasRole('commercial') ) {
            $paid_year = $paid_year->filter(function($commande) use ($request) {
                return $commande->commercial->hasRole('commercial') && $request->user()->id == $commande->commercial_id;
            });
        }
        $paid_year = $paid_year->sum(function($commande) {
            return $commande->paid_reglements->sum('montant');
        });
        $unpaid = Commande::whereNotIn('status', ['cancelled', 'returned'])->get()->filter(function($commande) {
            return $commande->unpaid_reglements->count('montant') > 0;
        })->count();
        $cheques = Commande::whereNotIn('status', ['cancelled', 'returned'])->whereHas('unpaid_reglements', function($query) {
            return $query->where('method', 'cheque');
        })->get()->filter(function($commande) {
            return $commande->unpaid_reglements->count('montant') > 0;
        })->count();
        $each_months = Commande::whereBetween('created_at', ["{$y}-01-01 00:00:00", "{$ye}-01-01 00:00:00"])
        ->orderBy('created_at')
        ->get();

        if( $request->user()->hasRole('commercial') ) {
            $each_months = $each_months->filter(function($commande) use ($request) {
                return $commande->commercial->hasRole('commercial') && $request->user()->id == $commande->commercial_id;
            });
        }

        $each_months = $each_months->groupBy(function ($val) {
            return Carbon::parse($val->created_at)->format('m');
        })->map(function($commande) {
            return $commande->sum(function($commande) {
                return $commande->paid_reglements->sum('montant');
            });
        })->all();
        for($i=1; $i <= 12; $i++) {
            if( !isset( $each_months[$i] ) )
            $each_months[$i] = 0;
        }
        ksort($each_months);
        $each_months = array_values($each_months);
        //Each commercial
        $labels = [];
        $each_commercials = User::role('commercial')->get()->map(function ($com) use (&$labels) {
            $labels[] = $com->name;
            return $com;
        })->map(function($com) {
            return $com->month_commandes->sum(function($commande) {
                return $commande->paid_reglements->sum('montant');
            });
        })->all();
        $pt = $paid_months->sum(function($commande) {
            return $commande->paid_reglements->sum('montant');
        });
        foreach($each_commercials as $k => $e) {
            $each_commercials[$k] = $pt ? ($e * 100) / $pt : 0;
        }
        $each_commercials = array_values($each_commercials);
        return view('Admin.dashboard', compact('paid_month', 'paid_year', 'unpaid', 'cheques', 'each_months', 'each_commercials', 'labels'));
    }
}

Zerion Mini Shell 1.0