2014-10-27 23:58:48 -05:00
|
|
|
<?php
|
|
|
|
|
2014-12-13 15:11:51 -06:00
|
|
|
namespace FireflyIII\Database\Transaction;
|
2014-10-27 23:58:48 -05:00
|
|
|
|
2014-12-13 15:11:51 -06:00
|
|
|
use FireflyIII\Database\CommonDatabaseCalls;
|
|
|
|
use FireflyIII\Database\CUD;
|
|
|
|
use FireflyIII\Database\SwitchUser;
|
2014-12-06 05:12:55 -06:00
|
|
|
use FireflyIII\Exception\FireflyException;
|
2014-10-27 23:58:48 -05:00
|
|
|
use FireflyIII\Exception\NotImplementedException;
|
2014-12-20 08:00:53 -06:00
|
|
|
use Illuminate\Database\Eloquent\Model as Eloquent;
|
2014-10-27 23:58:48 -05:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\MessageBag;
|
2014-12-06 05:12:55 -06:00
|
|
|
|
2014-10-27 23:58:48 -05:00
|
|
|
/**
|
|
|
|
* Class Transaction
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Database
|
|
|
|
*/
|
2014-11-22 16:30:45 -06:00
|
|
|
class Transaction implements CUD, CommonDatabaseCalls
|
2014-10-27 23:58:48 -05:00
|
|
|
{
|
|
|
|
use SwitchUser;
|
|
|
|
|
2014-11-12 15:37:09 -06:00
|
|
|
/**
|
2014-12-20 08:00:53 -06:00
|
|
|
* @param Eloquent $model
|
2014-11-12 15:37:09 -06:00
|
|
|
*
|
|
|
|
* @return bool
|
2014-12-13 14:59:02 -06:00
|
|
|
* @throws NotImplementedException
|
2014-11-12 15:37:09 -06:00
|
|
|
*/
|
2014-12-20 08:00:53 -06:00
|
|
|
public function destroy(Eloquent $model)
|
2014-11-12 15:37:09 -06:00
|
|
|
{
|
|
|
|
// TODO: Implement destroy() method.
|
|
|
|
throw new NotImplementedException;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
2014-12-06 05:12:55 -06:00
|
|
|
* @return \Eloquent
|
2014-12-13 14:59:02 -06:00
|
|
|
* @throws FireflyException
|
2014-11-12 15:37:09 -06:00
|
|
|
*/
|
|
|
|
public function store(array $data)
|
|
|
|
{
|
|
|
|
$transaction = new \Transaction;
|
|
|
|
$transaction->account()->associate($data['account']);
|
|
|
|
$transaction->transactionJournal()->associate($data['transaction_journal']);
|
|
|
|
$transaction->amount = floatval($data['amount']);
|
|
|
|
if (isset($data['piggybank'])) {
|
|
|
|
$transaction->piggybank()->associate($data['piggybank']);
|
|
|
|
}
|
|
|
|
if (isset($data['description'])) {
|
|
|
|
$transaction->description = $data['description'];
|
|
|
|
}
|
2014-12-06 05:12:55 -06:00
|
|
|
if ($transaction->isValid()) {
|
2014-11-12 15:37:09 -06:00
|
|
|
$transaction->save();
|
|
|
|
} else {
|
2014-12-06 05:12:55 -06:00
|
|
|
throw new FireflyException($transaction->getErrors()->first());
|
2014-11-12 15:37:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return $transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-20 08:00:53 -06:00
|
|
|
* @param Eloquent $model
|
|
|
|
* @param array $data
|
2014-11-12 15:37:09 -06:00
|
|
|
*
|
|
|
|
* @return bool
|
2014-12-13 14:59:02 -06:00
|
|
|
* @throws NotImplementedException
|
2014-11-12 15:37:09 -06:00
|
|
|
*/
|
2014-12-20 08:00:53 -06:00
|
|
|
public function update(Eloquent $model, array $data)
|
2014-11-12 15:37:09 -06:00
|
|
|
{
|
|
|
|
// TODO: Implement update() method.
|
|
|
|
throw new NotImplementedException;
|
|
|
|
}
|
|
|
|
|
2014-10-27 23:58:48 -05:00
|
|
|
/**
|
|
|
|
* Validates an array. Returns an array containing MessageBags
|
|
|
|
* errors/warnings/successes.
|
|
|
|
*
|
|
|
|
* @param array $model
|
|
|
|
*
|
2014-12-14 13:40:02 -06:00
|
|
|
* @return MessageBag
|
2014-10-27 23:58:48 -05:00
|
|
|
*/
|
|
|
|
public function validate(array $model)
|
|
|
|
{
|
2014-12-14 13:40:02 -06:00
|
|
|
$errors = new MessageBag;
|
|
|
|
if (is_null($model['account'])) {
|
2014-10-27 23:58:48 -05:00
|
|
|
$errors->add('account', 'No account present');
|
|
|
|
}
|
2014-12-14 13:40:02 -06:00
|
|
|
if (is_null($model['transaction_journal'])) {
|
|
|
|
$errors->add('transaction_journal', 'No valid transaction journal present');
|
2014-10-27 23:58:48 -05:00
|
|
|
}
|
|
|
|
|
2014-12-14 13:40:02 -06:00
|
|
|
return $errors;
|
2014-11-12 15:21:48 -06:00
|
|
|
}
|
|
|
|
|
2014-10-27 23:58:48 -05:00
|
|
|
/**
|
2014-11-12 15:37:09 -06:00
|
|
|
* Returns an object with id $id.
|
2014-10-27 23:58:48 -05:00
|
|
|
*
|
2014-12-19 14:18:42 -06:00
|
|
|
* @param int $objectId
|
2014-11-12 15:37:09 -06:00
|
|
|
*
|
2014-12-06 05:12:55 -06:00
|
|
|
* @return \Eloquent
|
2014-12-13 14:59:02 -06:00
|
|
|
* @throws NotImplementedException
|
2014-10-27 23:58:48 -05:00
|
|
|
*/
|
2014-12-19 14:18:42 -06:00
|
|
|
public function find($objectId)
|
2014-10-27 23:58:48 -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-27 23:58:48 -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-27 23:58:48 -05:00
|
|
|
*
|
2014-11-12 15:37:09 -06:00
|
|
|
* @param $what
|
2014-10-27 23:58:48 -05:00
|
|
|
*
|
2014-11-12 15:37:09 -06:00
|
|
|
* @return \AccountType|null
|
2014-12-13 14:59:02 -06:00
|
|
|
* @throws NotImplementedException
|
2014-10-27 23:58:48 -05:00
|
|
|
*/
|
2014-11-12 15:37:09 -06:00
|
|
|
public function findByWhat($what)
|
2014-10-27 23:58:48 -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-27 23:58:48 -05:00
|
|
|
}
|
2014-11-02 07:58:12 -06:00
|
|
|
|
|
|
|
/**
|
2014-11-12 15:21:48 -06:00
|
|
|
* Returns all objects.
|
2014-11-02 07:58:12 -06:00
|
|
|
*
|
2014-11-12 15:21:48 -06:00
|
|
|
* @return Collection
|
2014-12-13 14:59:02 -06:00
|
|
|
* @throws NotImplementedException
|
2014-11-02 07:58:12 -06:00
|
|
|
*/
|
2014-11-12 15:21:48 -06:00
|
|
|
public function get()
|
2014-11-02 07:58:12 -06:00
|
|
|
{
|
2014-11-12 15:21:48 -06:00
|
|
|
// TODO: Implement get() method.
|
|
|
|
throw new NotImplementedException;
|
2014-11-02 07:58:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $ids
|
|
|
|
*
|
|
|
|
* @return Collection
|
2014-12-13 14:59:02 -06:00
|
|
|
* @throws NotImplementedException
|
2014-11-02 07:58:12 -06:00
|
|
|
*/
|
|
|
|
public function getByIds(array $ids)
|
|
|
|
{
|
|
|
|
// TODO: Implement getByIds() method.
|
2014-11-12 15:21:48 -06:00
|
|
|
throw new NotImplementedException;
|
|
|
|
}
|
2014-10-27 23:58:48 -05:00
|
|
|
}
|