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

202 lines
5.3 KiB
PHP
Raw Normal View History

2014-10-29 04:30:52 -05:00
<?php
namespace FireflyIII\Database;
use Carbon\Carbon;
2014-10-30 13:26:28 -05:00
use FireflyIII\Database\Ifaces\CommonDatabaseCalls;
use FireflyIII\Database\Ifaces\CUD;
use FireflyIII\Database\Ifaces\RecurringInterface;
2014-11-12 15:37:09 -06:00
use FireflyIII\Exception\NotImplementedException;
use Illuminate\Support\Collection;
2014-11-13 04:17:39 -06:00
use Illuminate\Support\MessageBag;
2014-11-12 15:37:09 -06:00
use LaravelBook\Ardent\Ardent;
2014-10-29 04:30:52 -05:00
/**
* Class Recurring
*
* @package FireflyIII\Database
*/
class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface
{
use SwitchUser;
/**
*
*/
public function __construct()
{
$this->setUser(\Auth::user());
}
/**
2014-11-12 15:37:09 -06:00
* @param Ardent $model
2014-10-29 04:30:52 -05:00
*
2014-11-12 15:37:09 -06:00
* @return bool
2014-10-29 04:30:52 -05:00
*/
2014-11-12 15:37:09 -06:00
public function destroy(Ardent $model)
2014-10-29 04:30:52 -05:00
{
2014-11-12 15:37:09 -06:00
// TODO: Implement destroy() method.
throw new NotImplementedException;
2014-10-29 04:30:52 -05:00
}
2014-11-12 15:21:48 -06:00
/**
2014-11-12 15:37:09 -06:00
* @param array $data
2014-11-12 15:21:48 -06:00
*
2014-11-12 15:37:09 -06:00
* @return Ardent
2014-11-12 15:21:48 -06:00
*/
2014-11-12 15:37:09 -06:00
public function store(array $data)
2014-11-12 15:21:48 -06:00
{
2014-11-12 15:37:09 -06:00
// TODO: Implement store() method.
throw new NotImplementedException;
2014-11-12 15:21:48 -06:00
}
2014-10-29 04:30:52 -05:00
/**
* @param Ardent $model
2014-11-12 15:37:09 -06:00
* @param array $data
2014-10-29 04:30:52 -05:00
*
* @return bool
*/
2014-11-12 15:37:09 -06:00
public function update(Ardent $model, array $data)
2014-10-29 04:30:52 -05:00
{
2014-11-12 15:37:09 -06:00
// TODO: Implement update() method.
2014-11-12 15:21:48 -06:00
throw new NotImplementedException;
2014-10-29 04:30:52 -05:00
}
/**
2014-11-12 15:37:09 -06:00
* Validates an array. Returns an array containing MessageBags
2014-10-29 04:30:52 -05:00
* errors/warnings/successes.
*
2014-11-12 15:37:09 -06:00
* @param array $model
2014-10-29 04:30:52 -05:00
*
* @return array
*/
2014-11-12 15:37:09 -06:00
public function validate(array $model)
2014-10-29 04:30:52 -05:00
{
2014-11-13 04:17:39 -06:00
$warnings = new MessageBag;
$successes = new MessageBag;
$errors = new MessageBag;
if (isset($model['name']) && strlen($model['name']) == 0) {
$errors->add('name', 'Name must be longer.');
}
if (isset($model['name']) && strlen($model['name']) > 200) {
$errors->add('name', 'Name must be shorter.');
}
if (isset($model['match']) && strlen(trim($model['match'])) <= 2) {
$errors->add('match', 'Needs more matches.');
}
if (isset($model['amount_min']) && floatval($model['amount_min']) < 0.01) {
$errors->add('amount_min', 'Minimum amount must be higher.');
}
if (isset($model['amount_max']) && floatval($model['amount_max']) < 0.02) {
$errors->add('amount_max', 'Maximum amount must be higher.');
}
if(isset($model['amount_min']) && isset($model['amount_max']) && floatval($model['amount_min']) > floatval($model['amount_max'])) {
$errors->add('amount_max', 'Maximum amount can not be less than minimum amount.');
$errors->add('amount_min', 'Minimum amount can not be more than maximum amount.');
}
if ($model['date'] != '') {
try {
new Carbon($model['date']);
} catch (\Exception $e) {
$errors->add('date', 'Invalid date.');
}
}
$reminders = \Config::get('firefly.budget_periods');
if (!isset($model['repeat_freq']) || (isset($model['repeat_freq']) && !in_array($model['repeat_freq'], $reminders))) {
$errors->add('repeat_freq', 'Invalid reminder period');
}
if (isset($model['skip']) && intval($model['skip']) < 0) {
$errors->add('skip', 'Invalid skip.');
}
$set = ['name','match','amount_min','amount_max','date','repeat_freq','skip','automatch','active'];
foreach($set as $entry) {
if(!$errors->has($entry)) {
$successes->add($entry,'OK');
}
}
return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes];
2014-10-29 04:30:52 -05:00
}
/**
2014-11-12 15:37:09 -06:00
* Validates a model. Returns an array containing MessageBags
2014-10-29 04:30:52 -05:00
* errors/warnings/successes.
*
2014-11-12 15:37:09 -06:00
* @param Ardent $model
2014-10-29 04:30:52 -05:00
*
* @return array
*/
2014-11-12 15:37:09 -06:00
public function validateObject(Ardent $model)
2014-10-29 04:30:52 -05:00
{
2014-11-12 15:37:09 -06:00
// TODO: Implement validateObject() method.
2014-11-12 15:21:48 -06:00
throw new NotImplementedException;
2014-10-29 04:30:52 -05:00
}
/**
2014-11-12 15:37:09 -06:00
* Returns an object with id $id.
*
* @param int $id
2014-10-29 04:30:52 -05:00
*
* @return Ardent
*/
2014-11-12 15:37:09 -06:00
public function find($id)
2014-10-29 04:30:52 -05:00
{
2014-11-12 15:37:09 -06:00
// TODO: Implement find() method.
2014-11-12 15:21:48 -06:00
throw new NotImplementedException;
2014-10-29 04:30:52 -05:00
}
/**
2014-11-12 15:37:09 -06:00
* Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
2014-10-29 04:30:52 -05:00
*
2014-11-12 15:37:09 -06:00
* @param $what
*
* @return \AccountType|null
2014-10-29 04:30:52 -05:00
*/
2014-11-12 15:37:09 -06:00
public function findByWhat($what)
2014-10-29 04:30:52 -05:00
{
2014-11-12 15:37:09 -06:00
// TODO: Implement findByWhat() method.
2014-11-12 15:21:48 -06:00
throw new NotImplementedException;
2014-10-29 04:30:52 -05:00
}
/**
2014-11-12 15:37:09 -06:00
* Returns all objects.
2014-11-12 15:21:48 -06:00
*
2014-11-12 15:37:09 -06:00
* @return Collection
2014-10-29 04:30:52 -05:00
*/
2014-11-12 15:37:09 -06:00
public function get()
2014-10-29 04:30:52 -05:00
{
2014-11-12 15:37:09 -06:00
return $this->getUser()->recurringtransactions()->get();
2014-10-29 04:30:52 -05:00
}
/**
* @param array $ids
*
* @return Collection
*/
public function getByIds(array $ids)
{
// TODO: Implement getByIds() method.
2014-11-12 15:21:48 -06:00
throw new NotImplementedException;
2014-10-29 04:30:52 -05:00
}
/**
2014-11-12 15:37:09 -06:00
* @param \RecurringTransaction $recurring
* @param Carbon $start
* @param Carbon $end
2014-10-29 04:30:52 -05:00
*
2014-11-12 15:37:09 -06:00
* @return \TransactionJournal|null
2014-10-29 04:30:52 -05:00
*/
2014-11-12 15:37:09 -06:00
public function getJournalForRecurringInRange(\RecurringTransaction $recurring, Carbon $start, Carbon $end)
2014-10-29 04:30:52 -05:00
{
2014-11-12 15:37:09 -06:00
return $this->getUser()->transactionjournals()->where('recurring_transaction_id', $recurring->id)->after($start)->before($end)->first();
}
2014-10-29 04:30:52 -05:00
}