<commit_before><?php

namespace App;

use DB;

trait CreditableTrait
{
    public function balances()
    {
        return $this->morphMany('App\Balance', 'target')->orderBy('date', 'desc');
    }

    private function fixFirstBalance()
    {
        $balance = new Balance();
        $balance->target_id = $this->id;
        $balance->target_type = get_class($this);
        $balance->bank = 0;
        $balance->cash = 0;
        $balance->gas = 0;
        $balance->suppliers = 0;
        $balance->deposits = 0;
        $balance->paypal = 0;
        $balance->current = true;
        $balance->date = date('Y-m-d');
        $balance->save();
        return $balance;
    }

    public function resetCurrentBalance()
    {
        $this->current_balance->delete();

        if ($this->balances()->count() == 0) {
            return $this->fixFirstBalance();
        }
        else {
            $latest = $this->balances()->where('current', false)->first();
            $new = $latest->replicate();

            $new->date = date('Y-m-d G:i:s');
            $new->current = true;
            $new->save();
            return $new;
        }
    }

    public static function acceptedClasses()
    {
        $ret = [];

        $models = modelsUsingTrait('App\CreditableTrait');
        foreach($models as $m)
            $ret[$m] = $m::commonClassName();

        return $ret;
    }

    public static function resetAllCurrentBalances()
    {
        $current_status = [];

        $classes = DB::table('balances')->select('target_type')->distinct()->get();
        foreach($classes as $c) {
            $class = $c->target_type;

            $current_status[$class] = [];
            $fields = $class::balanceFields();

            if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($class)))
                $objects = $class::withTrashed()->get();
            else
                $objects = $class::all();

            foreach($objects as $obj) {
                $now = [];
                $cb = $obj->current_balance;

                if (is_null($cb)) {
                    foreach($fields as $field => $name)
                        $now[$field] = 0;
                }
                else {
                    foreach($fields as $field => $name)
                        $now[$field] = $cb->$field;
                }

                $current_status[$class][$obj->id] = $now;

                $obj->resetCurrentBalance();
            }
        }

        return $current_status;
    }

    public static function duplicateAllCurrentBalances($latest_date)
    {
        $classes = DB::table('balances')->select('target_type')->distinct()->get();
        foreach($classes as $c) {
            $class = $c->target_type;
            $objects = $class::all();
            foreach($objects as $obj) {
                $latest = $obj->current_balance;
                $new = $latest->replicate();
                $latest->date = $latest_date;
                $latest->current = false;
                $latest->save();
                $new->save();
            }
        }
    }

    /*
        Si aspetta come parametro un array formattato come quello restituito da
        resetAllCurrentBalances()

        [
            'Classe' => [
                'ID Oggetto' => [
                    'cash' => XXX,
                    'bank' => XXX,
                ],
                'ID Oggetto' => [
                    'cash' => XXX,
                    'bank' => XXX,
                ],
            ]
        ]
    */
    public static function compareBalances($old_balances)
    {
        $diff = [];

        foreach($old_balances as $class => $ids) {
            $fields = $class::balanceFields();

            foreach($ids as $id => $old) {
                $obj = $class::find($id);
                if (is_null($obj))
                    continue;

                $cb = $obj->current_balance;
                foreach($fields as $field => $name) {
                    if ($old[$field] != $cb->$field) {
                        $diff[$obj->printableName()] = [
                            $old[$field],
                            $cb->$field
                        ];

                        break;
                    }
                }
            }
        }

        return $diff;
    }

    public function getCurrentBalanceAttribute()
    {
        $proxy = $this->getBalanceProxy();

        if(is_null($proxy)) {
            $balance = $this->balances()->where('current', true)->first();
            if (is_null($balance)) {
                $balance = $this->balances()->where('current', false)->first();
                if (is_null($balance)) {
                    $balance = $this->fixFirstBalance();
                }
                else {
                    $balance->current = true;<commit_msg>fix su calcolo saldi in presenza di proxy<commit_after><?php

namespace App;

use DB;

trait CreditableTrait
{
    public function balances()
    {
        $proxy = $this->getBalanceProxy();
        if (is_null($proxy))
            return $this->morphMany('App\Balance', 'target')->orderBy('date', 'desc');
        else
            return $proxy->balances();
    }

    private function fixFirstBalance()
    {
        $balance = new Balance();
        $balance->target_id = $this->id;
        $balance->target_type = get_class($this);
        $balance->bank = 0;
        $balance->cash = 0;
        $balance->gas = 0;
        $balance->suppliers = 0;
        $balance->deposits = 0;
        $balance->paypal = 0;
        $balance->current = true;
        $balance->date = date('Y-m-d');
        $balance->save();
        return $balance;
    }

    public function resetCurrentBalance()
    {
        $this->current_balance->delete();

        if ($this->balances()->count() == 0) {
            return $this->fixFirstBalance();
        }
        else {
            $latest = $this->balances()->where('current', false)->first();
            $new = $latest->replicate();

            $new->date = date('Y-m-d G:i:s');
            $new->current = true;
            $new->save();
            return $new;
        }
    }

    public static function acceptedClasses()
    {
        $ret = [];

        $models = modelsUsingTrait('App\CreditableTrait');
        foreach($models as $m)
            $ret[$m] = $m::commonClassName();

        return $ret;
    }

    public static function resetAllCurrentBalances()
    {
        $current_status = [];

        $classes = DB::table('balances')->select('target_type')->distinct()->get();
        foreach($classes as $c) {
            $class = $c->target_type;
            $current_status[$class] = [];

            if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($class)))
                $objects = $class::withTrashed()->get();
            else
                $objects = $class::all();

            foreach($objects as $obj) {
                $proxy = $obj->getBalanceProxy();
                if ($proxy != null)
                    $obj = $proxy;

                $class = get_class($obj);
                $fields = $class::balanceFields();

                if (!isset($current_status[$class]))
                    $current_status[$class] = [];

                $now = [];
                $cb = $obj->current_balance;

                if (is_null($cb)) {
                    foreach($fields as $field => $name)
                        $now[$field] = 0;
                }
                else {
                    foreach($fields as $field => $name)
                        $now[$field] = $cb->$field;
                }

                $current_status[$class][$obj->id] = $now;

                $obj->resetCurrentBalance();
            }
        }

        return $current_status;
    }

    public static function duplicateAllCurrentBalances($latest_date)
    {
        $classes = DB::table('balances')->select('target_type')->distinct()->get();
        foreach($classes as $c) {
            $class = $c->target_type;
            $objects = $class::all();
            foreach($objects as $obj) {
                $latest = $obj->current_balance;
                $new = $latest->replicate();
                $latest->date = $latest_date;
                $latest->current = false;
                $latest->save();
                $new->save();
            }
        }
    }

    /*
        Si aspetta come parametro un array formattato come quello restituito da
        resetAllCurrentBalances()

        [
            'Classe' => [
                'ID Oggetto' => [
                    'cash' => XXX,
                    'bank' => XXX,
                ],
                'ID Oggetto' => [
                    'cash' => XXX,
                    'bank' => XXX,
                ],
            ]
        ]
    */
    public static function compareBalances($old_balances)
    {
        $diff = [];

        foreach($old_balances as $class => $ids) {
            foreach($ids as $id => $old) {
                $obj = $class::find($id);
                if (is_null($obj))
                    continue;

                $proxy = $obj->getBalanceProxy();
                if ($proxy != null) {
                    $obj = $proxy;
                    $proxy_class = get_class($obj);
                    $fields = $proxy_class::balanceFields();
                }
                else {
                    $fields = $class::balanceFields();
                }

                $cb = $obj->current_balance;
                foreach($fields as $field => $name) {
                    if ($old[$field] != $cb->$field) {
                        $diff[$obj->printableName()] = [
                            $old[$field],
                            $cb->$field
                        ];

                        break;
                    }
                }
            }
        }

        return $diff;
    }

    public function getCurrentBalanceAttribute()
    {
        $proxy = $this->getBalanceProxy();

        if(is_null($proxy)) {
            $balance = $this->balances()->where('current', true)->first();
            if (is_null($balance)) {
                $balance = $this->balances()->where('current', false)->first();
                if (is_null($balance)) {
                    $balance = $this->fixFirstBalance();
                }
                else {
                    $balance->current = true;
{'fraction_non_alphanumeric': 0.1491101491101491, 'fraction_numerical': 0.0015392015392015391, 'mean_word_length': 1.3262474826583128, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 147, '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': '788177', 'n_tokens_mistral': 2891, 'n_tokens_neox': 2728, 'n_words': 509}