<commit_before><?php

namespace App\Services;

use App\Exceptions\AuthException;
use App\Exceptions\IllegalArgumentException;

use Auth;
use Log;
use DB;

use App\Date;

class DatesService extends BaseService
{
    public function list($target = null, $editable = false)
    {
        $user = $this->ensureAuth(['supplier.orders' => null]);
        $query = Date::where('type', '!=', 'internal')->orderBy('date', 'asc');

        if ($target != null) {
            $query->where('target_type', get_class($target))->where('target_id', $target->id);
        }

        if ($editable == true) {
            $suppliers = $user->targetsByAction('supplier.orders');
            $query->where('target_type', 'App\Supplier')->whereIn('target_id', array_keys($suppliers));
        }

        return $query->get();
    }

    public function show($id)
    {
        return Date::findOrFail($id);
    }

    /*
        Questa funzione gestisce sia l'aggiornamento collettivo delle date
        relative ai fornitori che l'aggiornamento di una singola data "interna"
        editata dal pannello delle notifiche
    */
    public function update($id, array $request)
    {
        if ($id == 0) {
            $user = $this->ensureAuth(['supplier.orders' => null]);
            $suppliers = array_keys($user->targetsByAction('supplier.orders'));

            $ids = $request['id'];
            $targets = $request['target_id'];
            $dates = $request['date'];
            $recurrings = $request['recurring'];
            $descriptions = $request['description'];
            $types = $request['type'];

            $saved_ids = [];

            foreach($ids as $index => $id) {
                if (in_array($targets[$index], $suppliers) == false)
                    continue;

                if (empty($id))
                    $date = new Date();
                else
                    $date = Date::find($id);

                $date->target_type = 'App\Supplier';
                $date->target_id = $targets[$index];
                $date->date = null;
                $date->recurring = '';

                if (!empty($dates[$index]))
                    $date->date = decodeDate($dates[$index]);
                else
                    $date->recurring = json_encode(decodePeriodic($recurrings[$index]));

                $date->description = $descriptions[$index];
                $date->type = $types[$index];
                $date->save();

                $saved_ids[] = $date->id;
            }

            Date::where('type', '!=', 'internal')->whereIn('target_id', $suppliers)->whereNotIn('id', $saved_ids)->delete();
            return null;
        }
        else {
            $this->ensureAuth(['notifications.admin' => 'gas']);
            $date = Date::findOrFail($id);
            $date->date = decodeDate($request['date']);
            $date->description = $request['description'];
            $date->save();
            return $date;
        }<commit_msg>fix in salvataggio date<commit_after><?php

namespace App\Services;

use App\Exceptions\AuthException;
use App\Exceptions\IllegalArgumentException;

use Auth;
use Log;
use DB;

use App\Date;

class DatesService extends BaseService
{
    public function list($target = null, $editable = false)
    {
        $user = $this->ensureAuth(['supplier.orders' => null]);
        $query = Date::where('type', '!=', 'internal')->orderBy('date', 'asc');

        if ($target != null) {
            $query->where('target_type', get_class($target))->where('target_id', $target->id);
        }

        if ($editable == true) {
            $suppliers = $user->targetsByAction('supplier.orders');
            $query->where('target_type', 'App\Supplier')->whereIn('target_id', array_keys($suppliers));
        }

        return $query->get();
    }

    public function show($id)
    {
        return Date::findOrFail($id);
    }

    /*
        Questa funzione gestisce sia l'aggiornamento collettivo delle date
        relative ai fornitori che l'aggiornamento di una singola data "interna"
        editata dal pannello delle notifiche
    */
    public function update($id, array $request)
    {
        if ($id == 0) {
            $user = $this->ensureAuth(['supplier.orders' => null]);
            $suppliers = array_keys($user->targetsByAction('supplier.orders'));

            $ids = $request['id'];
            $targets = $request['target_id'];
            $dates = $request['date'];
            $recurrings = $request['recurring'];
            $descriptions = $request['description'];
            $types = $request['type'];

            $saved_ids = [];

            foreach($ids as $index => $id) {
                if (in_array($targets[$index], $suppliers) == false)
                    continue;

                if (empty($id))
                    $date = new Date();
                else
                    $date = Date::find($id);

                $date->target_type = 'App\Supplier';
                $date->target_id = $targets[$index];
                $date->date = null;
                $date->recurring = '';

                if (!empty($dates[$index]))
                    $date->date = decodeDate($dates[$index]);
                else if (!empty($recurrings[$index]))
                    $date->recurring = json_encode(decodePeriodic($recurrings[$index]));

                if (empty($date->date) && empty($date->recurring))
                    continue;

                $date->description = $descriptions[$index];
                $date->type = $types[$index];
                $date->save();

                $saved_ids[] = $date->id;
            }

            Date::where('type', '!=', 'internal')->whereIn('target_id', $suppliers)->whereNotIn('id', $saved_ids)->delete();
            return null;
        }
        else {
            $this->ensureAuth(['notifications.admin' => 'gas']);
            $date = Date::findOrFail($id);
            $date->date = decodeDate($request['date']);
            $date->description = $request['description'];
            $date->save();
            return $date;
        }
{'fraction_non_alphanumeric': 0.16220653423083237, 'fraction_numerical': 0.0003283533081595797, 'mean_word_length': 1.9458413926499032, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, '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': '2250722', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1653, 'n_words': 325}