mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Should allow to store new accounts.
This commit is contained in:
@@ -52,8 +52,8 @@ class AccountController extends Controller
|
|||||||
public function store(AccountFormRequest $request, AccountRepositoryInterface $repository)
|
public function store(AccountFormRequest $request, AccountRepositoryInterface $repository)
|
||||||
{
|
{
|
||||||
$accountData = [
|
$accountData = [
|
||||||
'name' => $request->input('name'),
|
'name' => $request->input('name') . rand(1,1000),
|
||||||
'accountType' => Config::get('firefly.accountTypeByIdentifier.' . $request->input('what')),
|
'accountType' => $request->input('what'),
|
||||||
'active' => true,
|
'active' => true,
|
||||||
'user' => Auth::user()->id,
|
'user' => Auth::user()->id,
|
||||||
'accountRole' => $request->input('accountRole'),
|
'accountRole' => $request->input('accountRole'),
|
||||||
|
@@ -2,10 +2,19 @@
|
|||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
use Watson\Validating\ValidatingTrait;
|
||||||
|
|
||||||
class Transaction extends Model
|
class Transaction extends Model
|
||||||
{
|
{
|
||||||
use SoftDeletes;
|
protected $fillable = ['account_id', 'transaction_journal_id', 'description', 'amount'];
|
||||||
|
protected $rules
|
||||||
|
= [
|
||||||
|
'account_id' => 'required|exists:accounts,id',
|
||||||
|
'transaction_journal_id' => 'required|exists:transaction_journals,id',
|
||||||
|
'description' => 'between:1,255',
|
||||||
|
'amount' => 'required|numeric'
|
||||||
|
];
|
||||||
|
use SoftDeletes, ValidatingTrait;
|
||||||
|
|
||||||
public function account()
|
public function account()
|
||||||
{
|
{
|
||||||
|
@@ -5,10 +5,30 @@ use Crypt;
|
|||||||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
use Watson\Validating\ValidatingTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class TransactionJournal
|
||||||
|
*
|
||||||
|
* @package FireflyIII\Models
|
||||||
|
*/
|
||||||
class TransactionJournal extends Model
|
class TransactionJournal extends Model
|
||||||
{
|
{
|
||||||
use SoftDeletes;
|
use SoftDeletes, ValidatingTrait;
|
||||||
|
|
||||||
|
protected $fillable = ['user_id', 'transaction_type_id', 'bill_id', 'transaction_currency_id', 'description', 'completed', 'date', 'encrypted'];
|
||||||
|
|
||||||
|
protected $rules
|
||||||
|
= [
|
||||||
|
'user_id' => 'required|exists:users,id',
|
||||||
|
'transaction_type_id' => 'required|exists:transaction_types,id',
|
||||||
|
'bill_id' => 'exists:bills,id',
|
||||||
|
'transaction_currency_id' => 'required|exists:transaction_currencies,id',
|
||||||
|
'description' => 'required|between:1,1024',
|
||||||
|
'completed' => 'required|boolean',
|
||||||
|
'date' => 'required|date',
|
||||||
|
'encrypted' => 'required|boolean'
|
||||||
|
];
|
||||||
|
|
||||||
public function bill()
|
public function bill()
|
||||||
{
|
{
|
||||||
|
@@ -3,8 +3,12 @@
|
|||||||
namespace FireflyIII\Repositories\Account;
|
namespace FireflyIII\Repositories\Account;
|
||||||
|
|
||||||
use App;
|
use App;
|
||||||
|
use Config;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
|
use FireflyIII\Models\Transaction;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
|
use FireflyIII\Models\TransactionType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class AccountRepository
|
* Class AccountRepository
|
||||||
@@ -26,14 +30,16 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
|
|
||||||
// continue with the opposing account:
|
// continue with the opposing account:
|
||||||
if ($data['openingBalance'] != 0) {
|
if ($data['openingBalance'] != 0) {
|
||||||
$type = $data['openingBalance'] < 0 ? 'expense' : 'revenue';
|
$type = $data['openingBalance'] < 0 ? 'expense' : 'revenue';
|
||||||
$opposing = [
|
$opposingData = [
|
||||||
'user' => $data['user'],
|
'user' => $data['user'],
|
||||||
'accountType' => $type,
|
'accountType' => $type,
|
||||||
'name' => $data['name'] . ' initial balance',
|
'name' => $data['name'] . ' initial balance',
|
||||||
'active' => false,
|
'active' => false,
|
||||||
];
|
];
|
||||||
$this->_store($opposing);
|
$opposing = $this->_store($opposingData);
|
||||||
|
$this->_storeInitialBalance($newAccount, $opposing, $data);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $newAccount;
|
return $newAccount;
|
||||||
@@ -42,10 +48,13 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return Account
|
||||||
*/
|
*/
|
||||||
protected function _store(array $data)
|
protected function _store(array $data)
|
||||||
{
|
{
|
||||||
$accountType = AccountType::whereType($data['accountType'])->first();
|
$type = Config::get('firefly.accountTypeByIdentifier.' . $data['accountType']);
|
||||||
|
$accountType = AccountType::whereType($type)->first();
|
||||||
$newAccount = new Account(
|
$newAccount = new Account(
|
||||||
[
|
[
|
||||||
'user_id' => $data['user'],
|
'user_id' => $data['user'],
|
||||||
@@ -58,6 +67,78 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
App::abort(500);
|
App::abort(500);
|
||||||
}
|
}
|
||||||
$newAccount->save();
|
$newAccount->save();
|
||||||
|
|
||||||
|
return $newAccount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Account $account
|
||||||
|
* @param Account $opposing
|
||||||
|
* @param array $data
|
||||||
|
*/
|
||||||
|
protected function _storeInitialBalance(Account $account, Account $opposing, array $data)
|
||||||
|
{
|
||||||
|
$type = $data['openingBalance'] < 0 ? 'Withdrawal' : 'Deposit';
|
||||||
|
$transactionType = TransactionType::whereType($type)->first();
|
||||||
|
|
||||||
|
$journal = new TransactionJournal(
|
||||||
|
[
|
||||||
|
'user_id' => $data['user'],
|
||||||
|
'transaction_type_id' => $transactionType->id,
|
||||||
|
'bill_id' => null,
|
||||||
|
'transaction_currency_id' => $data['openingBalanceCurrency'],
|
||||||
|
'description' => 'Initial balance for "' . $account->name . '"',
|
||||||
|
'completed' => true,
|
||||||
|
'date' => $data['openingBalanceDate'],
|
||||||
|
'encrypted' => true
|
||||||
|
]
|
||||||
|
);
|
||||||
|
if (!$journal->isValid()) {
|
||||||
|
App::abort(500);
|
||||||
|
}
|
||||||
|
$journal->save();
|
||||||
|
|
||||||
|
|
||||||
|
if ($data['openingBalance'] < 0) {
|
||||||
|
$firstAccount = $opposing;
|
||||||
|
$secondAccount = $account;
|
||||||
|
$firstAmount = $data['openingBalance'] * -1;
|
||||||
|
$secondAmount = $data['openingBalance'];
|
||||||
|
} else {
|
||||||
|
$firstAccount = $account;
|
||||||
|
$secondAccount = $opposing;
|
||||||
|
$firstAmount = $data['openingBalance'];
|
||||||
|
$secondAmount = $data['openingBalance'] * -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// first transaction: from
|
||||||
|
$one = new Transaction(
|
||||||
|
[
|
||||||
|
'account_id' => $firstAccount->id,
|
||||||
|
'transaction_journal_id' => $journal->id,
|
||||||
|
'amount' => $firstAmount
|
||||||
|
]
|
||||||
|
);
|
||||||
|
if (!$one->isValid()) {
|
||||||
|
App::abort(500);
|
||||||
|
}
|
||||||
|
$one->save();
|
||||||
|
|
||||||
|
// second transaction: to
|
||||||
|
$two = new Transaction(
|
||||||
|
[
|
||||||
|
'account_id' => $secondAccount->id,
|
||||||
|
'transaction_journal_id' => $journal->id,
|
||||||
|
'amount' => $secondAmount
|
||||||
|
]
|
||||||
|
);
|
||||||
|
if (!$two->isValid()) {
|
||||||
|
App::abort(500);
|
||||||
|
}
|
||||||
|
$two->save();
|
||||||
|
|
||||||
|
return $journal;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user