<commit_before><?php

namespace App;

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

use Auth;
use DB;
use URL;
use Theme;

use App\GASModel;
use App\SluggableID;
use App\BookedProduct;
use App\ExportableTrait;

class Order extends Model
{
    use AttachableTrait, ExportableTrait, GASModel, SluggableID, PayableTrait;

    public $incrementing = false;

    public function supplier()
    {
        return $this->belongsTo('App\Supplier');
    }

    public function aggregate()
    {
        return $this->belongsTo('App\Aggregate');
    }

    public function products()
    {
        return $this->belongsToMany('App\Product')->with('measure')->with('category')->with('variants')->withPivot('discount_enabled');
    }

    public function bookings()
    {
        return $this->hasMany('App\Booking')->with('user');
    }

    public function payment()
    {
        return $this->belongsTo('App\Movement');
    }

    public function getSlugID()
    {
        return sprintf('%s::%s', $this->supplier->id, str_slug(strftime('%d %B %G', strtotime($this->start))));
    }

    public function printableName()
    {
        $ret = $this->supplier->name;

        if (!empty($this->comment))
            $ret .= ' - ' . $this->comment;

        $ret .= ' - ' . $this->internal_number;

        return $ret;
    }

    public function printableHeader()
    {
        $ret = $this->printableName();
        $icons = $this->icons();

        if (!empty($icons)) {
            $ret .= '<div class="pull-right">';

            foreach ($icons as $i) {
                $ret .= '<span class="glyphicon glyphicon-'.$i.'" aria-hidden="true"></span>&nbsp;';
            }

            $ret .= '</div>';
        }

        $ret .= sprintf('<br/><small>%s</small>', $this->printableDates());

        return $ret;
    }

    public function printableDates()
    {
        $start = strtotime($this->start);
        $end = strtotime($this->end);
        $string = sprintf('da %s a %s', strftime('%A %d %B %G', $start), strftime('%A %d %B %G', $end));
        if ($this->shipping != null && $this->shipping != '0000-00-00') {
            $shipping = strtotime($this->shipping);
            $string .= sprintf(', in consegna %s', strftime('%A %d %B %G', $shipping));
        }

        return $string;
    }

    public function getBookingURL()
    {
        return URL::action('BookingController@index').'#' . $this->aggregate->id;
    }

    public function userBooking($userid = null)
    {
        if ($userid == null) {
            $userid = Auth::user()->id;
        }

        $ret = $this->hasMany('App\Booking')->whereHas('user', function ($query) use ($userid) {
            $query->where('id', '=', $userid);
        })->first();

        if ($ret == null) {
            $b = new Booking();
            $b->user_id = $userid;
            $b->order_id = $this->id;
            $b->status = 'pending';

            return $b;
        } else {
            return $ret;
        }
    }

    public function getInternalNumberAttribute()
    {
        $year = date('Y', strtotime($this->start));
        return (Order::where('supplier_id', $this->supplier_id)->where(DB::raw('YEAR(start)'), $year)->where('start', '<', $this->start)->count() + 1) . '/' . $year;<commit_msg>numero ordini globale per l'anno<commit_after><?php

namespace App;

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

use Auth;
use DB;
use URL;
use Theme;

use App\GASModel;
use App\SluggableID;
use App\BookedProduct;
use App\ExportableTrait;

class Order extends Model
{
    use AttachableTrait, ExportableTrait, GASModel, SluggableID, PayableTrait;

    public $incrementing = false;

    public function supplier()
    {
        return $this->belongsTo('App\Supplier');
    }

    public function aggregate()
    {
        return $this->belongsTo('App\Aggregate');
    }

    public function products()
    {
        return $this->belongsToMany('App\Product')->with('measure')->with('category')->with('variants')->withPivot('discount_enabled');
    }

    public function bookings()
    {
        return $this->hasMany('App\Booking')->with('user');
    }

    public function payment()
    {
        return $this->belongsTo('App\Movement');
    }

    public function getSlugID()
    {
        return sprintf('%s::%s', $this->supplier->id, str_slug(strftime('%d %B %G', strtotime($this->start))));
    }

    public function printableName()
    {
        $ret = $this->supplier->name;

        if (!empty($this->comment))
            $ret .= ' - ' . $this->comment;

        $ret .= ' - ' . $this->internal_number;

        return $ret;
    }

    public function printableHeader()
    {
        $ret = $this->printableName();
        $icons = $this->icons();

        if (!empty($icons)) {
            $ret .= '<div class="pull-right">';

            foreach ($icons as $i) {
                $ret .= '<span class="glyphicon glyphicon-'.$i.'" aria-hidden="true"></span>&nbsp;';
            }

            $ret .= '</div>';
        }

        $ret .= sprintf('<br/><small>%s</small>', $this->printableDates());

        return $ret;
    }

    public function printableDates()
    {
        $start = strtotime($this->start);
        $end = strtotime($this->end);
        $string = sprintf('da %s a %s', strftime('%A %d %B %G', $start), strftime('%A %d %B %G', $end));
        if ($this->shipping != null && $this->shipping != '0000-00-00') {
            $shipping = strtotime($this->shipping);
            $string .= sprintf(', in consegna %s', strftime('%A %d %B %G', $shipping));
        }

        return $string;
    }

    public function getBookingURL()
    {
        return URL::action('BookingController@index').'#' . $this->aggregate->id;
    }

    public function userBooking($userid = null)
    {
        if ($userid == null) {
            $userid = Auth::user()->id;
        }

        $ret = $this->hasMany('App\Booking')->whereHas('user', function ($query) use ($userid) {
            $query->where('id', '=', $userid);
        })->first();

        if ($ret == null) {
            $b = new Booking();
            $b->user_id = $userid;
            $b->order_id = $this->id;
            $b->status = 'pending';

            return $b;
        } else {
            return $ret;
        }
    }

    public function getInternalNumberAttribute()
    {
        if(!isset($this->internal_number_cache)) {
            $o = $this;
            $year = date('Y', strtotime($o->start));

            $this->internal_number_cache = (Order::where(DB::raw('YEAR(start)'), $year)->where(function($query) use ($o) {
                $query->where('start', '<', $this->start)->orWhere(function($query) use ($o) {
                    $query->where('start', $this->start)->where('id', '<', $this->id);
                });
            })->count() + 1) . '/' . $year;
        }

        return $this->internal_number_cache;
{'fraction_non_alphanumeric': 0.18522776260957033, 'fraction_numerical': 0.0025865785313981893, 'mean_word_length': 2.241732650209595, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 110, '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': '1310213', 'n_tokens_mistral': 2258, 'n_tokens_neox': 2197, 'n_words': 430}