Files
firefly-iii/app/lib/FireflyIII/Database/Budget/Budget.php

359 lines
10 KiB
PHP
Raw Normal View History

2014-10-29 10:30:52 +01:00
<?php
2014-12-13 22:11:51 +01:00
namespace FireflyIII\Database\Budget;
2014-10-29 10:30:52 +01:00
use Carbon\Carbon;
2014-12-13 22:11:51 +01:00
use FireflyIII\Database\CommonDatabaseCalls;
use FireflyIII\Database\CUD;
use FireflyIII\Database\SwitchUser;
2014-12-14 20:40:02 +01:00
use FireflyIII\Exception\FireflyException;
use FireflyIII\Exception\NotImplementedException;
2014-12-20 15:00:53 +01:00
use Illuminate\Database\Eloquent\Model as Eloquent;
2014-11-12 22:21:48 +01:00
use Illuminate\Support\Collection;
2014-10-29 10:30:52 +01:00
use Illuminate\Support\MessageBag;
2014-10-29 10:30:52 +01:00
/**
* Class Budget
*
* @package FireflyIII\Database
*/
class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
{
use SwitchUser;
/**
*
*/
public function __construct()
{
$this->setUser(\Auth::user());
}
/**
2014-12-20 15:00:53 +01:00
* @param Eloquent $model
2014-10-29 10:30:52 +01:00
*
2014-11-12 22:37:09 +01:00
* @return bool
2014-10-29 10:30:52 +01:00
*/
2014-12-20 15:00:53 +01:00
public function destroy(Eloquent $model)
2014-10-29 10:30:52 +01:00
{
2014-11-12 22:37:09 +01:00
$model->delete();
return true;
}
/**
* @param array $data
*
2014-12-06 12:12:55 +01:00
* @return \Eloquent
2014-12-14 20:40:02 +01:00
* @throws FireflyException
2014-11-12 22:37:09 +01:00
*/
public function store(array $data)
{
$budget = new \Budget($data);
2014-12-06 12:12:55 +01:00
if (!$budget->isValid()) {
2014-12-14 20:40:02 +01:00
\Log::error('Could not store budget: ' . $budget->getErrors()->toJson());
throw new FireflyException($budget->getErrors()->first());
2014-11-12 22:37:09 +01:00
}
$budget->save();
return $budget;
2014-10-29 10:30:52 +01:00
}
/**
2014-12-20 15:00:53 +01:00
* @param Eloquent $model
* @param array $data
2014-10-29 10:30:52 +01:00
*
* @return bool
*/
2014-12-20 15:00:53 +01:00
public function update(Eloquent $model, array $data)
2014-10-29 10:30:52 +01:00
{
2014-11-12 22:37:09 +01:00
$model->name = $data['name'];
$model->save();
2014-11-06 07:38:15 +01:00
return true;
2014-10-29 10:30:52 +01:00
}
/**
* Validates an array. Returns an array containing MessageBags
* errors/warnings/successes.
*
* @param array $model
*
* @return array
*/
public function validate(array $model)
{
2014-11-12 22:37:09 +01:00
$warnings = new MessageBag;
2014-11-06 07:38:15 +01:00
$successes = new MessageBag;
2014-12-20 16:06:25 +01:00
$budget = new \Budget($model);
$budget->isValid();
$errors = $budget->getErrors();
2014-11-06 20:33:37 +01:00
if (!$errors->has('name')) {
$successes->add('name', 'OK');
2014-11-06 07:38:15 +01:00
}
2014-11-12 22:37:09 +01:00
return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes];
2014-10-29 10:30:52 +01:00
}
2014-11-04 20:37:00 +01:00
/**
2014-11-12 22:37:09 +01:00
* Returns an object with id $id.
*
2014-12-19 21:18:42 +01:00
* @param int $objectId
2014-11-12 22:37:09 +01:00
*
2014-12-06 12:12:55 +01:00
* @return \Eloquent
2014-11-04 20:37:00 +01:00
*/
2014-12-19 21:18:42 +01:00
public function find($objectId)
{
2014-12-19 21:18:42 +01:00
return $this->getUser()->budgets()->find($objectId);
2014-10-29 10:30:52 +01:00
}
/**
2014-11-12 22:37:09 +01:00
* Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
2014-10-29 10:30:52 +01:00
*
2014-11-12 22:37:09 +01:00
* @param $what
*
* @return \AccountType|null
2014-12-13 21:59:02 +01:00
* @throws NotImplementedException
2014-10-29 10:30:52 +01:00
*/
2014-11-12 22:37:09 +01:00
public function findByWhat($what)
2014-10-29 10:30:52 +01:00
{
2014-11-12 22:37:09 +01:00
// TODO: Implement findByWhat() method.
throw new NotImplementedException;
2014-10-29 10:30:52 +01:00
}
/**
2014-11-12 22:37:09 +01:00
* Returns all objects.
*
2014-11-12 22:37:09 +01:00
* @return Collection
*/
2014-11-12 22:37:09 +01:00
public function get()
{
2014-11-12 22:37:09 +01:00
$budgets = $this->getUser()->budgets()->get();
2014-11-06 07:38:15 +01:00
2014-11-12 22:37:09 +01:00
return $budgets;
}
2014-11-12 22:21:48 +01:00
/**
2014-11-12 22:37:09 +01:00
* @param array $ids
2014-11-12 22:21:48 +01:00
*
2014-11-12 22:37:09 +01:00
* @return Collection
2014-12-13 21:59:02 +01:00
* @throws NotImplementedException
2014-11-12 22:21:48 +01:00
*/
2014-11-12 22:37:09 +01:00
public function getByIds(array $ids)
2014-11-12 22:21:48 +01:00
{
2014-11-12 22:37:09 +01:00
// TODO: Implement getByIds() method.
2014-11-12 22:21:48 +01:00
throw new NotImplementedException;
}
2014-12-12 07:13:40 +01:00
/**
* Returns all the transaction journals for a limit, possibly limited by a limit repetition.
*
* @param \Budget $budget
* @param \LimitRepetition $repetition
* @param int $take
*
* @return \Illuminate\Pagination\Paginator
*/
public function getJournals(\Budget $budget, \LimitRepetition $repetition = null, $take = 50)
{
$offset = intval(\Input::get('page')) > 0 ? intval(\Input::get('page')) * $take : 0;
$setQuery = $budget->transactionJournals()->withRelevantData()->take($take)->offset($offset)->orderBy('date', 'DESC');
$countQuery = $budget->transactionJournals();
if (!is_null($repetition)) {
2014-12-12 07:42:38 +01:00
$setQuery->after($repetition->startdate)->before($repetition->enddate);
$countQuery->after($repetition->startdate)->before($repetition->enddate);
2014-12-12 07:13:40 +01:00
}
$set = $setQuery->get(['transaction_journals.*']);
$count = $countQuery->count();
$items = [];
foreach ($set as $entry) {
$items[] = $entry;
}
return \Paginator::make($items, $count, $take);
}
2014-12-13 21:59:02 +01:00
/**
* @param \Budget $budget
* @param int $limit
*
* @return \Illuminate\Pagination\Paginator
*/
public function getTransactionJournals(\Budget $budget, $limit = 50)
{
$offset = intval(\Input::get('page')) > 0 ? intval(\Input::get('page')) * $limit : 0;
$set = $budget->transactionJournals()->withRelevantData()->take($limit)->offset($offset)->orderBy('date', 'DESC')->get(['transaction_journals.*']);
$count = $budget->transactionJournals()->count();
$items = [];
foreach ($set as $entry) {
$items[] = $entry;
}
return \Paginator::make($items, $count, $limit);
}
2014-12-13 21:59:02 +01:00
/**
* @param \Budget $budget
* @param \LimitRepetition $repetition
* @param int $limit
*
* @return \Illuminate\Pagination\Paginator
*/
public function getTransactionJournalsInRepetition(\Budget $budget, \LimitRepetition $repetition, $limit = 50)
{
$start = $repetition->startdate;
2014-11-17 07:33:18 +01:00
$end = $repetition->enddate;
$offset = intval(\Input::get('page')) > 0 ? intval(\Input::get('page')) * $limit : 0;
2014-11-17 07:33:18 +01:00
$set = $budget->transactionJournals()->withRelevantData()->before($end)->after($start)->take($limit)->offset($offset)->orderBy('date', 'DESC')->get(
['transaction_journals.*']
);
$count = $budget->transactionJournals()->before($end)->after($start)->count();
$items = [];
foreach ($set as $entry) {
$items[] = $entry;
}
return \Paginator::make($items, $count, $limit);
}
2014-11-12 22:21:48 +01:00
/**
2014-11-12 22:37:09 +01:00
* @param \Budget $budget
* @param Carbon $date
2014-11-12 22:21:48 +01:00
*
2014-11-12 22:37:09 +01:00
* @return \LimitRepetition|null
2014-11-12 22:21:48 +01:00
*/
2014-11-12 22:37:09 +01:00
public function repetitionOnStartingOnDate(\Budget $budget, Carbon $date)
2014-11-12 22:21:48 +01:00
{
2014-11-12 22:37:09 +01:00
return \LimitRepetition::
leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
2014-12-20 15:00:53 +01:00
->where('limit_repetitions.startdate', $date->format('Y-m-d'))
->where('budget_limits.budget_id', $budget->id)
->first(['limit_repetitions.*']);
2014-11-12 22:21:48 +01:00
}
/**
2014-11-12 22:37:09 +01:00
* @param Carbon $start
* @param Carbon $end
2014-11-12 22:21:48 +01:00
*
* @return Collection
*/
2014-11-12 22:37:09 +01:00
public function transactionsWithoutBudgetInDateRange(Carbon $start, Carbon $end)
2014-11-12 22:21:48 +01:00
{
2014-11-12 22:37:09 +01:00
// Add expenses that have no budget:
return $this->getUser()
2014-12-20 15:00:53 +01:00
->transactionjournals()
->whereNotIn(
'transaction_journals.id', function ($query) use ($start, $end) {
$query
->select('transaction_journals.id')
->from('transaction_journals')
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'));
}
)
->before($end)
->after($start)
->lessThan(0)
->transactionTypes(['Withdrawal'])
->get();
2014-11-12 22:21:48 +01:00
}
/**
2014-11-12 22:37:09 +01:00
* @param \Budget $budget
* @param Carbon $date
2014-11-12 22:21:48 +01:00
*
2014-11-12 22:37:09 +01:00
* @return float
2014-11-12 22:21:48 +01:00
*/
2014-11-12 22:37:09 +01:00
public function spentInMonth(\Budget $budget, Carbon $date)
2014-11-12 22:21:48 +01:00
{
2014-11-12 22:37:09 +01:00
$end = clone $date;
$date->startOfMonth();
$end->endOfMonth();
$sum = floatval($budget->transactionjournals()->before($end)->after($date)->lessThan(0)->sum('amount')) * -1;
return $sum;
2014-11-12 22:21:48 +01:00
}
2014-11-26 17:20:18 +01:00
/**
* @param \Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return float
*/
public function spentInPeriod(\Budget $budget, Carbon $start, Carbon $end)
{
$sum = floatval($budget->transactionjournals()->before($end)->after($start)->lessThan(0)->sum('amount')) * -1;
return $sum;
}
2014-12-07 20:45:52 +01:00
/**
2014-12-12 07:13:40 +01:00
* This method updates the amount (envelope) for the given date and budget. This results in a (new) limit (aka an envelope)
* for that budget. Returned to the user is the new limit repetition.
*
2014-12-07 20:45:52 +01:00
* @param \Budget $budget
* @param Carbon $date
* @param $amount
*
2014-12-12 07:13:40 +01:00
* @return \LimitRepetition
2014-12-07 20:45:52 +01:00
* @throws \Exception
*/
public function updateLimitAmount(\Budget $budget, Carbon $date, $amount)
{
/** @var \Limit $limit */
$limit = $this->limitOnStartingOnDate($budget, $date);
if (!$limit) {
// create one!
2014-12-13 22:54:52 +01:00
$limit = new \BudgetLimit;
2014-12-07 20:45:52 +01:00
$limit->budget()->associate($budget);
2014-12-12 07:13:40 +01:00
$limit->startdate = $date;
$limit->amount = $amount;
2014-12-07 20:45:52 +01:00
$limit->repeat_freq = 'monthly';
2014-12-12 07:13:40 +01:00
$limit->repeats = 0;
2014-12-20 15:00:53 +01:00
$result = $limit->save();
\Log::info('Created new limit? ' . boolval($result));
\Log::info('ID: ' . $limit->id);
2014-12-07 20:45:52 +01:00
/*
* A newly stored limit also created a limit repetition.
*/
\Event::fire('limits.store', [$limit]);
} else {
if ($amount > 0) {
$limit->amount = $amount;
$limit->save();
/*
* An updated limit also updates the associated limit repetitions.
*/
\Event::fire('limits.update', [$limit]);
} else {
$limit->delete();
}
}
2014-12-12 07:13:40 +01:00
return $limit->limitrepetitions()->first();
2014-12-07 20:45:52 +01:00
}
/**
* @param \Budget $budget
* @param Carbon $date
*
* @return \Limit
*/
public function limitOnStartingOnDate(\Budget $budget, Carbon $date)
{
2014-12-13 22:54:52 +01:00
return $budget->budgetLimits()->where('startdate', $date->format('Y-m-d'))->first();
2014-12-07 20:45:52 +01:00
}
2014-10-29 10:30:52 +01:00
}