check()) { if ($value->account->user_id == auth()->user()->id) { return $value; } } throw new NotFoundHttpException; } /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function account() { return $this->belongsTo('FireflyIII\Models\Account'); } /** * Grabs the PiggyBankRepetition that's currently relevant / active * * @returns PiggyBankRepetition */ public function currentRelevantRep(): PiggyBankRepetition { if (!is_null($this->currentRep)) { return $this->currentRep; } // repeating piggy banks are no longer supported. $rep = $this->piggyBankRepetitions()->first(['piggy_bank_repetitions.*']); if (is_null($rep)) { return new PiggyBankRepetition(); } $this->currentRep = $rep; return $rep; } /** * * @param $value * * @return string */ public function getNameAttribute($value) { if (intval($this->encrypted) == 1) { return Crypt::decrypt($value); } return $value; } /** * * @param Carbon $date * * @return string */ public function leftOnAccount(Carbon $date): string { $balance = Steam::balanceIgnoreVirtual($this->account, $date); /** @var PiggyBank $p */ foreach ($this->account->piggyBanks as $piggyBank) { $currentAmount = $piggyBank->currentRelevantRep()->currentamount ?? '0'; $balance = bcsub($balance, $currentAmount); } return $balance; } /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function piggyBankEvents() { return $this->hasMany('FireflyIII\Models\PiggyBankEvent'); } /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function piggyBankRepetitions() { return $this->hasMany('FireflyIII\Models\PiggyBankRepetition'); } /** * * @param $value */ public function setNameAttribute($value) { $this->attributes['name'] = Crypt::encrypt($value); $this->attributes['encrypted'] = true; } /** * @param $value */ public function setTargetamountAttribute($value) { $this->attributes['targetamount'] = strval(round($value, 2)); } }