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

243 lines
5.9 KiB
PHP
Raw Normal View History

2014-10-29 04:30:52 -05:00
<?php
namespace FireflyIII\Database;
use Carbon\Carbon;
2014-11-12 15:21:48 -06:00
use FireflyIII\Database\Ifaces\BudgetInterface;
use FireflyIII\Database\Ifaces\CommonDatabaseCalls;
use FireflyIII\Database\Ifaces\CUD;
use FireflyIII\Exception\NotImplementedException;
2014-11-12 15:21:48 -06:00
use Illuminate\Support\Collection;
2014-10-29 04:30:52 -05:00
use Illuminate\Support\MessageBag;
use LaravelBook\Ardent\Ardent;
2014-10-29 04:30:52 -05:00
/**
* Class Budget
*
* @package FireflyIII\Database
*/
class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
{
use SwitchUser;
/**
*
*/
public function __construct()
{
$this->setUser(\Auth::user());
}
/**
* @param \Budget $budget
2014-11-12 15:21:48 -06:00
* @param Carbon $date
2014-10-29 04:30:52 -05:00
*
* @return \LimitRepetition|null
*/
public function repetitionOnStartingOnDate(\Budget $budget, Carbon $date)
{
return \LimitRepetition::
leftJoin('limits', 'limit_repetitions.limit_id', '=', 'limits.id')->leftJoin(
'components', 'limits.component_id', '=', 'components.id'
)->where('limit_repetitions.startdate', $date->format('Y-m-d'))->where(
'components.id', $budget->id
)->first(['limit_repetitions.*']);
}
/**
* @param Ardent $model
*
* @return bool
*/
public function destroy(Ardent $model)
{
2014-11-06 00:38:15 -06:00
$model->delete();
return true;
2014-10-29 04:30:52 -05: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 15:21:48 -06:00
$warnings = new MessageBag;
2014-11-06 00:38:15 -06:00
$successes = new MessageBag;
2014-11-12 15:21:48 -06:00
$errors = new MessageBag;
2014-11-06 00:38:15 -06:00
if (isset($model['name'])) {
if (strlen($model['name']) < 1) {
2014-11-06 00:38:15 -06:00
$errors->add('name', 'Name is too short');
}
if (strlen($model['name']) > 200) {
2014-11-06 00:38:15 -06:00
$errors->add('name', 'Name is too long');
}
} else {
$errors->add('name', 'Name is mandatory');
}
2014-11-06 13:33:37 -06:00
$validator = \Validator::make($model, \Component::$rules);
2014-11-06 00:38:15 -06:00
if ($validator->invalid()) {
2014-11-06 13:33:37 -06:00
$errors->merge($validator->errors());
2014-11-06 00:38:15 -06:00
}
2014-11-06 13:33:37 -06:00
if (!$errors->has('name')) {
$successes->add('name', 'OK');
2014-11-06 00:38:15 -06:00
}
return [
2014-11-12 15:21:48 -06:00
'errors' => $errors,
'warnings' => $warnings,
2014-11-06 00:38:15 -06:00
'successes' => $successes
];
2014-10-29 04:30:52 -05:00
}
/**
* @param array $data
*
* @return Ardent
*/
public function store(array $data)
{
2014-11-06 00:38:15 -06:00
$data['user_id'] = $this->getUser()->id;
2014-11-12 15:21:48 -06:00
$budget = new \Budget($data);
2014-11-06 00:38:15 -06:00
$budget->class = 'Budget';
if (!$budget->validate()) {
var_dump($budget->errors()->all());
exit;
}
$budget->save();
return $budget;
2014-10-29 04:30:52 -05:00
}
/**
* Returns all objects.
*
* @return Collection
*/
public function get()
{
2014-11-04 13:37:00 -06:00
$budgets = $this->getUser()->budgets()->get();
return $budgets;
}
/**
* @param \Budget $budget
2014-11-12 15:21:48 -06:00
* @param Carbon $date
*
2014-11-04 13:37:00 -06:00
* @return float
*/
public function spentInMonth(\Budget $budget, Carbon $date)
{
2014-11-04 13:37:00 -06:00
$end = clone $date;
$date->startOfMonth();
$end->endOfMonth();
$sum = floatval($budget->transactionjournals()->before($end)->after($date)->lessThan(0)->sum('amount')) * -1;
return $sum;
2014-10-29 04:30:52 -05:00
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function transactionsWithoutBudgetInDateRange(Carbon $start, Carbon $end)
{
// Add expenses that have no budget:
return \Auth::user()->transactionjournals()->whereNotIn(
'transaction_journals.id', function ($query) use ($start, $end) {
$query->select('transaction_journals.id')->from('transaction_journals')
2014-11-12 15:21:48 -06:00
->leftJoin(
'component_transaction_journal', 'component_transaction_journal.transaction_journal_id', '=',
'transaction_journals.id'
)
->leftJoin('components', 'components.id', '=', 'component_transaction_journal.component_id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
->where('components.class', 'Budget');
2014-10-29 04:30:52 -05:00
}
)->before($end)->after($start)->lessThan(0)->transactionTypes(['Withdrawal'])->get();
}
/**
* @param Ardent $model
2014-11-12 15:21:48 -06:00
* @param array $data
*
* @return bool
*/
public function update(Ardent $model, array $data)
{
$model->name = $data['name'];
2014-11-06 00:38:15 -06:00
if (!$model->validate()) {
var_dump($model->errors()->all());
exit;
}
$model->save();
return true;
}
2014-11-12 15:21:48 -06:00
/**
* Validates a model. Returns an array containing MessageBags
* errors/warnings/successes.
*
* @param Ardent $model
*
* @return array
*/
public function validateObject(Ardent $model)
{
// TODO: Implement validateObject() method.
throw new NotImplementedException;
}
/**
* Returns an object with id $id.
*
* @param int $id
*
* @return Ardent
*/
public function find($id)
{
// TODO: Implement find() method.
throw new NotImplementedException;
}
/**
* @param array $ids
*
* @return Collection
*/
public function getByIds(array $ids)
{
// TODO: Implement getByIds() method.
throw new NotImplementedException;
}
/**
* Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
*
* @param $what
*
* @return \AccountType|null
*/
public function findByWhat($what)
{
// TODO: Implement findByWhat() method.
throw new NotImplementedException;
}
2014-10-29 04:30:52 -05:00
}