firefly-iii/app/Models/Budget.php

191 lines
6.7 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* Budget.php
2020-02-16 06:55:32 -06:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 01:40:00 -05:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Models;
2015-02-27 09:48:33 -06:00
use Eloquent;
2018-06-06 14:23:00 -05:00
use FireflyIII\User;
2021-03-28 04:46:23 -05:00
use Illuminate\Database\Eloquent\Collection;
2015-02-27 09:48:33 -06:00
use Illuminate\Database\Eloquent\Model;
2016-04-06 02:27:45 -05:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2018-08-04 10:30:06 -05:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
2015-02-27 09:48:33 -06:00
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Builder;
2021-03-28 04:46:23 -05:00
use Illuminate\Support\Carbon;
2016-01-09 09:09:26 -06:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2015-02-27 09:48:33 -06:00
2016-11-18 13:06:08 -06:00
/**
2020-12-03 23:20:44 -06:00
* FireflyIII\Models\Budget
2018-06-29 22:21:21 -05:00
*
2022-03-29 07:59:58 -05:00
* @property int $id
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Carbon|null $deleted_at
* @property int $user_id
* @property string $name
* @property bool $active
* @property bool $encrypted
* @property int $order
* @property-read Collection|Attachment[] $attachments
* @property-read int|null $attachments_count
* @property-read Collection|AutoBudget[] $autoBudgets
* @property-read int|null $auto_budgets_count
* @property-read Collection|BudgetLimit[] $budgetlimits
* @property-read int|null $budgetlimits_count
* @property-read Collection|TransactionJournal[] $transactionJournals
* @property-read int|null $transaction_journals_count
* @property-read Collection|Transaction[] $transactions
* @property-read int|null $transactions_count
* @property-read User $user
* @method static \Illuminate\Database\Eloquent\Builder|Budget newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Budget newQuery()
* @method static Builder|Budget onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|Budget query()
* @method static \Illuminate\Database\Eloquent\Builder|Budget whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|Budget whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Budget whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Budget whereEncrypted($value)
* @method static \Illuminate\Database\Eloquent\Builder|Budget whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Budget whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|Budget whereOrder($value)
* @method static \Illuminate\Database\Eloquent\Builder|Budget whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Budget whereUserId($value)
* @method static Builder|Budget withTrashed()
* @method static Builder|Budget withoutTrashed()
* @mixin Eloquent
2022-03-29 07:59:58 -05:00
* @property string $email
* @property int|null $user_group_id
2021-09-18 03:08:10 -05:00
* @method static \Illuminate\Database\Eloquent\Builder|Budget whereUserGroupId($value)
2022-03-29 07:59:58 -05:00
* @property-read Collection|Note[] $notes
* @property-read int|null $notes_count
2016-11-18 13:06:08 -06:00
*/
2015-02-27 09:48:33 -06:00
class Budget extends Model
{
2018-04-02 08:10:40 -05:00
use SoftDeletes;
2015-02-27 09:48:33 -06:00
2016-12-24 10:36:51 -06:00
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts
= [
2017-11-03 10:04:17 -05:00
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
2016-12-24 10:36:51 -06:00
'active' => 'boolean',
'encrypted' => 'boolean',
];
2018-08-04 10:30:06 -05:00
/** @var array Fields that can be filled */
protected $fillable = ['user_id', 'name', 'active', 'order'];
2018-08-04 10:30:06 -05:00
/** @var array Hidden from view */
2017-11-05 12:49:20 -06:00
protected $hidden = ['encrypted'];
2015-02-27 09:48:33 -06:00
/**
2018-08-04 10:30:06 -05:00
* Route binder. Converts the key in the URL to the specified object (or throw 404).
*
2017-12-29 02:05:35 -06:00
* @param string $value
*
* @return Budget
2021-03-28 04:46:23 -05:00
* @throws NotFoundHttpException
*/
2018-02-09 12:24:15 -06:00
public static function routeBinder(string $value): Budget
{
if (auth()->check()) {
2022-03-29 07:59:58 -05:00
$budgetId = (int) $value;
2018-07-22 09:35:46 -05:00
/** @var User $user */
$user = auth()->user();
/** @var Budget $budget */
2018-08-06 12:14:30 -05:00
$budget = $user->budgets()->find($budgetId);
2018-04-02 08:10:40 -05:00
if (null !== $budget) {
return $budget;
}
}
2022-10-30 08:24:28 -05:00
throw new NotFoundHttpException();
}
2015-02-27 09:48:33 -06:00
/**
* @codeCoverageIgnore
2021-03-28 04:46:23 -05:00
* @return MorphMany
2015-02-27 09:48:33 -06:00
*/
2021-03-28 04:46:23 -05:00
public function attachments(): MorphMany
2015-02-27 09:48:33 -06:00
{
2021-03-28 04:46:23 -05:00
return $this->morphMany(Attachment::class, 'attachable');
2015-02-27 09:48:33 -06:00
}
2020-03-13 15:35:22 -05:00
/**
* @codeCoverageIgnore
* @return HasMany
*/
2021-03-28 04:46:23 -05:00
public function autoBudgets(): HasMany
2020-03-13 15:35:22 -05:00
{
2021-03-28 04:46:23 -05:00
return $this->hasMany(AutoBudget::class);
2020-03-13 15:35:22 -05:00
}
2021-03-28 04:46:23 -05:00
/**
* @codeCoverageIgnore
2021-03-28 04:46:23 -05:00
* @return HasMany
*/
2021-03-28 04:46:23 -05:00
public function budgetlimits(): HasMany
{
2021-03-28 04:46:23 -05:00
return $this->hasMany(BudgetLimit::class);
}
/**
* @codeCoverageIgnore
* Get all of the notes.
*/
public function notes(): MorphMany
{
return $this->morphMany(Note::class, 'noteable');
}
2015-03-30 13:08:27 -05:00
/**
* @codeCoverageIgnore
2018-08-04 10:30:06 -05:00
* @return BelongsToMany
2015-03-30 13:08:27 -05:00
*/
2018-08-04 10:30:06 -05:00
public function transactionJournals(): BelongsToMany
2015-03-30 13:08:27 -05:00
{
2018-04-27 23:23:13 -05:00
return $this->belongsToMany(TransactionJournal::class, 'budget_transaction_journal', 'budget_id');
2015-04-03 15:54:21 -05:00
}
2015-03-30 13:08:27 -05:00
/**
* @codeCoverageIgnore
2018-08-04 10:30:06 -05:00
* @return BelongsToMany
*/
2018-08-04 10:30:06 -05:00
public function transactions(): BelongsToMany
{
2018-04-27 23:23:13 -05:00
return $this->belongsToMany(Transaction::class, 'budget_transaction', 'budget_id');
}
2015-04-03 15:54:21 -05:00
/**
* @codeCoverageIgnore
2016-04-06 02:27:45 -05:00
* @return BelongsTo
2015-04-03 15:54:21 -05:00
*/
2016-04-06 02:27:45 -05:00
public function user(): BelongsTo
2015-04-03 15:54:21 -05:00
{
2018-04-27 23:23:13 -05:00
return $this->belongsTo(User::class);
2015-03-30 13:08:27 -05:00
}
2015-02-27 09:48:33 -06:00
}