<commit_before><?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

use Auth;
use URL;
use Log;

use App\Scopes\RestrictedGAS;
use App\Events\AttachableToGas;

class Aggregate extends Model
{
    use GASModel;

    protected $dispatchesEvents = [
        'created' => AttachableToGas::class
    ];

    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope(new RestrictedGAS());
    }

    public function gas()
    {
        return $this->belongsToMany('App\Gas');
    }

    public function orders()
    {
        return $this->hasMany('App\Order')->with(['supplier', 'products'])->orderBy('aggregate_sorting', 'asc');
    }

    public function scopeSupplier($query, $supplier_id)
    {
        $query->whereHas('orders', function ($query) use ($supplier_id) {
            $query->where('supplier_id', '=', $supplier_id);
        });
    }

    public static function easyFilter($supplier, $startdate, $enddate, $statuses = null)
    {
        if (is_object($supplier))
            $supplier_id = $supplier->id;
        else
            $supplier_id = $supplier;

        if ($statuses == null)
            $statuses = ['open', 'closed', 'shipped', 'suspended', 'archived'];

        /*
            Questa funzione dovrebbe prendere in considerazione anche i permessi
            dell'utente corrente, e tornare solo gli aggregati che contengono
            ordini tipo:
            $user->can('supplier.orders', $order->supplier) || $user->can('supplier.shippings', $order->supplier)
        */

        $orders = self::with('orders')->whereHas('orders', function ($query) use ($supplier_id, $startdate, $enddate, $statuses) {
            if (!empty($supplier_id)) {
                if (is_array($supplier_id))
                    $query->whereIn('supplier_id', $supplier_id);
                else
                    $query->where('supplier_id', $supplier_id);
            }

            if (!empty($startdate))
                $query->where('start', '>=', $startdate);

            if (!empty($enddate))
                $query->where('end', '<=', $enddate);

            $query->whereIn('status', $statuses);
        })->get();

        $orders->sort(function($a, $b) {
            return strcmp($a->shipping, $b->shipping);
        });

        return $orders;
    }

    public function getStatusAttribute()
    {
        $priority = ['suspended', 'open', 'closed', 'shipped', 'archived'];
        $index = 10;

        foreach ($this->orders as $order) {
            $a = array_search($order->status, $priority);
            if ($a < $index) {
                $index = $a;
            }
        }

        if ($index == 10) {
            $index = 2;
        }

        return $priority[$index];
    }

    public function getDeliveriesAttribute()
    {
        foreach ($this->orders as $order) {
            return $order->deliveries;
        }

        return new Collection();
    }

    public static function getByStatus($user, $status)
    {
        switch($status) {
            /*
                Se cerco gli ordini aperti ed è stata abilitata la funzione per
                gestire gli ordini incompleti, devo considerare anche quelli chiusi
                ma con confezioni da completare
            */
            case 'open':
                $ret = new Collection();

                $aggregates = self::whereHas('orders', function ($query) use ($status, $user) {
                    $query->whereIn('status', ['open', 'closed'])->accessibleBooking();
                })->with(['orders'])->get();

                foreach($aggregates as $a) {
                    if ($a->status == 'open' || $a->hasPendingPackages()) {
                        $ret->push($a);
                    }
                }

                return $ret;

            case 'closed':
                return self::whereHas('orders', function ($query) use ($user) {
                    $query->where('status', 'closed')->where(function($query) use ($user) {
                        $query->whereHas('bookings', function($query) use ($user) {
                            $query->where('status', '!=', 'shipped')->where(function($query) use ($user) {
                                $query->where('user_id', $user->id)->orWhereIn('user_id', $user->friends()->pluck('id'));
                            });
                        })->orWhere(function($query) {
                            $query->accessibleBooking();
                        });
                    });
                })->with(['orders'])->get();
        }<commit_msg>altro fix accesso ordini<commit_after><?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

use Auth;
use URL;
use Log;

use App\Scopes\RestrictedGAS;
use App\Events\AttachableToGas;

class Aggregate extends Model
{
    use GASModel;

    protected $dispatchesEvents = [
        'created' => AttachableToGas::class
    ];

    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope(new RestrictedGAS());
    }

    public function gas()
    {
        return $this->belongsToMany('App\Gas');
    }

    public function orders()
    {
        return $this->hasMany('App\Order')->with(['supplier', 'products'])->orderBy('aggregate_sorting', 'asc');
    }

    public function scopeSupplier($query, $supplier_id)
    {
        $query->whereHas('orders', function ($query) use ($supplier_id) {
            $query->where('supplier_id', '=', $supplier_id);
        });
    }

    public static function easyFilter($supplier, $startdate, $enddate, $statuses = null)
    {
        if (is_object($supplier))
            $supplier_id = $supplier->id;
        else
            $supplier_id = $supplier;

        if ($statuses == null)
            $statuses = ['open', 'closed', 'shipped', 'suspended', 'archived'];

        /*
            Questa funzione dovrebbe prendere in considerazione anche i permessi
            dell'utente corrente, e tornare solo gli aggregati che contengono
            ordini tipo:
            $user->can('supplier.orders', $order->supplier) || $user->can('supplier.shippings', $order->supplier)
        */

        $orders = self::with('orders')->whereHas('orders', function ($query) use ($supplier_id, $startdate, $enddate, $statuses) {
            if (!empty($supplier_id)) {
                if (is_array($supplier_id))
                    $query->whereIn('supplier_id', $supplier_id);
                else
                    $query->where('supplier_id', $supplier_id);
            }

            if (!empty($startdate))
                $query->where('start', '>=', $startdate);

            if (!empty($enddate))
                $query->where('end', '<=', $enddate);

            $query->whereIn('status', $statuses);
        })->get();

        $orders->sort(function($a, $b) {
            return strcmp($a->shipping, $b->shipping);
        });

        return $orders;
    }

    public function getStatusAttribute()
    {
        $priority = ['suspended', 'open', 'closed', 'shipped', 'archived'];
        $index = 10;

        foreach ($this->orders as $order) {
            $a = array_search($order->status, $priority);
            if ($a < $index) {
                $index = $a;
            }
        }

        if ($index == 10) {
            $index = 2;
        }

        return $priority[$index];
    }

    public function getDeliveriesAttribute()
    {
        foreach ($this->orders as $order) {
            return $order->deliveries;
        }

        return new Collection();
    }

    public static function getByStatus($user, $status)
    {
        switch($status) {
            /*
                Se cerco gli ordini aperti ed è stata abilitata la funzione per
                gestire gli ordini incompleti, devo considerare anche quelli chiusi
                ma con confezioni da completare
            */
            case 'open':
                $ret = new Collection();

                $aggregates = self::whereHas('orders', function ($query) use ($status, $user) {
                    $query->whereIn('status', ['open', 'closed'])->accessibleBooking();
                })->with(['orders'])->get();

                foreach($aggregates as $a) {
                    if ($a->status == 'open' || $a->hasPendingPackages()) {
                        $ret->push($a);
                    }
                }

                return $ret;

            case 'closed':
                return self::whereHas('orders', function ($query) use ($user) {
                    $query->where('status', 'closed')->where(function($query) use ($user) {
                        $query->whereHas('bookings', function($query) use ($user) {
                            $query->where('status', '!=', 'shipped')->where(function($query) use ($user) {
                                $query->where('user_id', $user->id)->orWhereIn('user_id', $user->friends()->pluck('id'));
                            });
                        })->orWhere(function($query) {
                            $query->accessibleBooking();
                        })->orWhere(function($query) use ($user) {
                            $supplier_shippings = array_keys($user->targetsByAction('supplier.shippings'));
                            $query->whereIn('supplier_id', $supplier_shippings);
                        });
                    });
                })->with(['orders'])->get();
        }
{'fraction_non_alphanumeric': 0.15157828940514637, 'fraction_numerical': 0.0010417751849150954, 'mean_word_length': 1.8078385492834161, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6922030', 'n_tokens_mistral': 2765, 'n_tokens_neox': 2641, 'n_words': 548}