2015-02-09 00:23:39 -06:00
|
|
|
<?php
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2015-02-09 00:23:39 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Journal;
|
|
|
|
|
2015-02-27 07:27:04 -06:00
|
|
|
use Auth;
|
2015-05-04 16:46:14 -05:00
|
|
|
use Carbon\Carbon;
|
2015-04-28 03:36:13 -05:00
|
|
|
use DB;
|
2016-02-17 08:52:46 -06:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2015-02-24 15:53:38 -06:00
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use FireflyIII\Models\AccountType;
|
|
|
|
use FireflyIII\Models\Budget;
|
|
|
|
use FireflyIII\Models\Category;
|
2015-04-28 03:36:13 -05:00
|
|
|
use FireflyIII\Models\Tag;
|
2015-02-24 15:53:38 -06:00
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\Models\TransactionType;
|
2015-05-04 16:46:14 -05:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
2015-02-25 14:19:06 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2015-04-03 15:54:21 -05:00
|
|
|
use Log;
|
2015-02-24 15:53:38 -06:00
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
|
|
|
* Class JournalRepository
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Repositories\Journal
|
|
|
|
*/
|
|
|
|
class JournalRepository implements JournalRepositoryInterface
|
|
|
|
{
|
2015-02-09 00:23:39 -06:00
|
|
|
|
2015-05-04 16:46:14 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function delete(TransactionJournal $journal)
|
|
|
|
{
|
|
|
|
$journal->delete();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-03 15:54:21 -05:00
|
|
|
/**
|
|
|
|
* Get users first transaction journal
|
|
|
|
*
|
|
|
|
* @return TransactionJournal
|
|
|
|
*/
|
|
|
|
public function first()
|
|
|
|
{
|
2015-12-25 10:11:55 -06:00
|
|
|
$entry = Auth::user()->transactionjournals()->orderBy('date', 'ASC')->first(['transaction_journals.*']);
|
|
|
|
|
|
|
|
return $entry;
|
2015-04-03 15:54:21 -05:00
|
|
|
}
|
|
|
|
|
2015-05-04 16:46:14 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param Transaction $transaction
|
|
|
|
*
|
2015-05-26 13:57:31 -05:00
|
|
|
* @return integer
|
2015-05-04 16:46:14 -05:00
|
|
|
*/
|
|
|
|
public function getAmountBefore(TransactionJournal $journal, Transaction $transaction)
|
|
|
|
{
|
2015-05-23 08:42:19 -05:00
|
|
|
$set = $transaction->account->transactions()->leftJoin(
|
|
|
|
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
|
|
|
)
|
|
|
|
->where('transaction_journals.date', '<=', $journal->date->format('Y-m-d'))
|
|
|
|
->where('transaction_journals.order', '>=', $journal->order)
|
|
|
|
->where('transaction_journals.id', '!=', $journal->id)
|
2015-05-24 04:41:52 -05:00
|
|
|
->get(['transactions.*']);
|
2016-02-05 06:09:18 -06:00
|
|
|
bcscale(2);
|
|
|
|
$sum = '0';
|
2015-05-24 04:41:52 -05:00
|
|
|
foreach ($set as $entry) {
|
2016-02-05 06:09:18 -06:00
|
|
|
$sum = bcadd($entry->amount, $sum);
|
2015-05-23 08:42:19 -05:00
|
|
|
}
|
2015-05-24 04:41:52 -05:00
|
|
|
|
2015-05-23 08:42:19 -05:00
|
|
|
return $sum;
|
|
|
|
|
2015-05-04 16:46:14 -05:00
|
|
|
}
|
|
|
|
|
2016-02-17 13:31:18 -06:00
|
|
|
/**
|
|
|
|
* @param array $types
|
|
|
|
* @param int $offset
|
|
|
|
* @param int $count
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getCollectionOfTypes(array $types, int $offset, int $count)
|
|
|
|
{
|
|
|
|
$set = Auth::user()->transactionJournals()->transactionTypes($types)
|
|
|
|
->take($count)->offset($offset)
|
|
|
|
->orderBy('date', 'DESC')
|
|
|
|
->orderBy('order', 'ASC')
|
|
|
|
->orderBy('id', 'DESC')
|
|
|
|
->get(
|
|
|
|
['transaction_journals.*']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2015-04-11 12:59:41 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionType $dbType
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getJournalsOfType(TransactionType $dbType)
|
|
|
|
{
|
|
|
|
return Auth::user()->transactionjournals()->where('transaction_type_id', $dbType->id)->orderBy('id', 'DESC')->take(50)->get();
|
|
|
|
}
|
|
|
|
|
2015-05-04 16:46:14 -05:00
|
|
|
/**
|
|
|
|
* @param array $types
|
|
|
|
* @param int $offset
|
|
|
|
* @param int $page
|
|
|
|
*
|
2016-02-17 14:14:32 -06:00
|
|
|
* @param int $pagesize
|
|
|
|
*
|
2015-05-04 16:46:14 -05:00
|
|
|
* @return LengthAwarePaginator
|
|
|
|
*/
|
2016-02-17 03:17:16 -06:00
|
|
|
public function getJournalsOfTypes(array $types, int $offset, int $page, int $pagesize = 50)
|
2015-05-04 16:46:14 -05:00
|
|
|
{
|
2016-03-02 05:33:07 -06:00
|
|
|
$set = Auth::user()
|
|
|
|
->transactionJournals()
|
|
|
|
->expanded()
|
|
|
|
->transactionTypes($types)
|
|
|
|
->take($pagesize)
|
|
|
|
->offset($offset)
|
|
|
|
->orderBy('date', 'DESC')
|
|
|
|
->orderBy('order', 'ASC')
|
|
|
|
->orderBy('id', 'DESC')
|
|
|
|
->get(TransactionJournal::QUERYFIELDS);
|
|
|
|
|
2016-03-02 05:53:20 -06:00
|
|
|
$count = Auth::user()->transactionJournals()->transactionTypes($types)->count();
|
2016-02-17 03:17:16 -06:00
|
|
|
$journals = new LengthAwarePaginator($set, $count, $pagesize, $page);
|
2015-05-04 16:46:14 -05:00
|
|
|
|
|
|
|
return $journals;
|
|
|
|
}
|
|
|
|
|
2015-04-11 12:59:41 -05:00
|
|
|
/**
|
2016-02-05 02:25:15 -06:00
|
|
|
* @param string $type
|
2015-04-11 12:59:41 -05:00
|
|
|
*
|
|
|
|
* @return TransactionType
|
|
|
|
*/
|
2016-02-05 02:25:15 -06:00
|
|
|
public function getTransactionType(string $type)
|
2015-04-11 12:59:41 -05:00
|
|
|
{
|
|
|
|
return TransactionType::whereType($type)->first();
|
|
|
|
}
|
|
|
|
|
2015-05-04 16:46:14 -05:00
|
|
|
/**
|
2015-07-06 15:23:34 -05:00
|
|
|
* @param int $journalId
|
2015-05-04 16:46:14 -05:00
|
|
|
* @param Carbon $date
|
|
|
|
*
|
|
|
|
* @return TransactionJournal
|
|
|
|
*/
|
2016-02-05 02:25:15 -06:00
|
|
|
public function getWithDate(int $journalId, Carbon $date)
|
2015-05-04 16:46:14 -05:00
|
|
|
{
|
2015-07-06 15:23:34 -05:00
|
|
|
return Auth::user()->transactionjournals()->where('id', $journalId)->where('date', $date->format('Y-m-d 00:00:00'))->first();
|
2015-05-04 16:46:14 -05:00
|
|
|
}
|
|
|
|
|
2015-04-28 03:36:13 -05:00
|
|
|
/**
|
2015-04-28 06:47:13 -05:00
|
|
|
*
|
|
|
|
* * Remember: a balancingAct takes at most one expense and one transfer.
|
|
|
|
* an advancePayment takes at most one expense, infinite deposits and NO transfers.
|
|
|
|
*
|
2015-04-28 03:36:13 -05:00
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param array $array
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function saveTags(TransactionJournal $journal, array $array)
|
|
|
|
{
|
2015-04-28 10:10:52 -05:00
|
|
|
/** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
|
2015-07-07 12:09:45 -05:00
|
|
|
$tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
|
2015-04-28 10:10:52 -05:00
|
|
|
|
2015-04-28 03:36:13 -05:00
|
|
|
foreach ($array as $name) {
|
2015-04-28 14:01:54 -05:00
|
|
|
if (strlen(trim($name)) > 0) {
|
|
|
|
$tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
|
2015-07-06 11:48:17 -05:00
|
|
|
if (!is_null($tag)) {
|
|
|
|
$tagRepository->connect($journal, $tag);
|
|
|
|
}
|
2015-04-28 14:01:54 -05:00
|
|
|
}
|
2015-04-28 03:36:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 15:53:38 -06:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return TransactionJournal
|
|
|
|
*/
|
|
|
|
public function store(array $data)
|
|
|
|
{
|
|
|
|
// find transaction type.
|
|
|
|
$transactionType = TransactionType::where('type', ucfirst($data['what']))->first();
|
|
|
|
|
|
|
|
// store actual journal.
|
|
|
|
$journal = new TransactionJournal(
|
|
|
|
[
|
2015-12-28 09:52:21 -06:00
|
|
|
'user_id' => $data['user'],
|
|
|
|
'transaction_type_id' => $transactionType->id,
|
|
|
|
'transaction_currency_id' => $data['amount_currency_id_amount'],
|
|
|
|
'description' => $data['description'],
|
|
|
|
'completed' => 0,
|
|
|
|
'date' => $data['date'],
|
2015-02-24 15:53:38 -06:00
|
|
|
]
|
|
|
|
);
|
|
|
|
$journal->save();
|
|
|
|
|
|
|
|
|
|
|
|
// store or get category
|
|
|
|
if (strlen($data['category']) > 0) {
|
2015-04-09 10:46:47 -05:00
|
|
|
$category = Category::firstOrCreateEncrypted(['name' => $data['category'], 'user_id' => $data['user']]);
|
2015-02-24 15:53:38 -06:00
|
|
|
$journal->categories()->save($category);
|
|
|
|
}
|
|
|
|
|
|
|
|
// store or get budget
|
|
|
|
if (intval($data['budget_id']) > 0) {
|
2015-05-05 03:23:01 -05:00
|
|
|
/** @var \FireflyIII\Models\Budget $budget */
|
2015-02-24 15:53:38 -06:00
|
|
|
$budget = Budget::find($data['budget_id']);
|
|
|
|
$journal->budgets()->save($budget);
|
|
|
|
}
|
|
|
|
|
|
|
|
// store accounts (depends on type)
|
2015-07-06 11:48:17 -05:00
|
|
|
list($fromAccount, $toAccount) = $this->storeAccounts($transactionType, $data);
|
2015-02-24 15:53:38 -06:00
|
|
|
|
|
|
|
// store accompanying transactions.
|
|
|
|
Transaction::create( // first transaction.
|
|
|
|
[
|
2015-07-06 11:48:17 -05:00
|
|
|
'account_id' => $fromAccount->id,
|
2015-02-24 15:53:38 -06:00
|
|
|
'transaction_journal_id' => $journal->id,
|
2016-01-15 16:12:52 -06:00
|
|
|
'amount' => $data['amount'] * -1,
|
2015-02-24 15:53:38 -06:00
|
|
|
]
|
|
|
|
);
|
2015-04-03 15:54:21 -05:00
|
|
|
Transaction::create( // second transaction.
|
2015-02-24 15:53:38 -06:00
|
|
|
[
|
2015-07-06 11:48:17 -05:00
|
|
|
'account_id' => $toAccount->id,
|
2015-02-24 15:53:38 -06:00
|
|
|
'transaction_journal_id' => $journal->id,
|
2016-01-15 16:12:52 -06:00
|
|
|
'amount' => $data['amount'],
|
2015-02-24 15:53:38 -06:00
|
|
|
]
|
|
|
|
);
|
2015-02-27 07:27:04 -06:00
|
|
|
$journal->completed = 1;
|
|
|
|
$journal->save();
|
2015-02-24 15:53:38 -06:00
|
|
|
|
2015-04-28 07:08:58 -05:00
|
|
|
// store tags
|
|
|
|
if (isset($data['tags']) && is_array($data['tags'])) {
|
|
|
|
$this->saveTags($journal, $data['tags']);
|
|
|
|
}
|
|
|
|
|
2015-02-24 15:53:38 -06:00
|
|
|
return $journal;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2015-02-27 07:27:04 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param array $data
|
|
|
|
*
|
2015-05-26 13:57:31 -05:00
|
|
|
* @return TransactionJournal
|
2015-02-27 07:27:04 -06:00
|
|
|
*/
|
|
|
|
public function update(TransactionJournal $journal, array $data)
|
|
|
|
{
|
|
|
|
// update actual journal.
|
2015-12-28 00:12:12 -06:00
|
|
|
$journal->transaction_currency_id = $data['amount_currency_id_amount'];
|
2015-02-27 07:27:04 -06:00
|
|
|
$journal->description = $data['description'];
|
|
|
|
$journal->date = $data['date'];
|
|
|
|
|
|
|
|
|
|
|
|
// unlink all categories, recreate them:
|
|
|
|
$journal->categories()->detach();
|
|
|
|
if (strlen($data['category']) > 0) {
|
2015-04-09 10:46:47 -05:00
|
|
|
$category = Category::firstOrCreateEncrypted(['name' => $data['category'], 'user_id' => $data['user']]);
|
2015-02-27 07:27:04 -06:00
|
|
|
$journal->categories()->save($category);
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlink all budgets and recreate them:
|
|
|
|
$journal->budgets()->detach();
|
|
|
|
if (intval($data['budget_id']) > 0) {
|
2015-05-05 03:23:01 -05:00
|
|
|
/** @var \FireflyIII\Models\Budget $budget */
|
2015-02-27 07:27:04 -06:00
|
|
|
$budget = Budget::find($data['budget_id']);
|
|
|
|
$journal->budgets()->save($budget);
|
|
|
|
}
|
|
|
|
|
|
|
|
// store accounts (depends on type)
|
2015-07-06 11:57:15 -05:00
|
|
|
list($fromAccount, $toAccount) = $this->storeAccounts($journal->transactionType, $data);
|
2015-03-29 05:32:00 -05:00
|
|
|
|
|
|
|
// update the from and to transaction.
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($journal->transactions()->get() as $transaction) {
|
2015-07-17 10:35:04 -05:00
|
|
|
if ($transaction->amount < 0) {
|
2015-03-29 05:32:00 -05:00
|
|
|
// this is the from transaction, negative amount:
|
|
|
|
$transaction->amount = $data['amount'] * -1;
|
2015-07-06 11:57:15 -05:00
|
|
|
$transaction->account_id = $fromAccount->id;
|
2015-03-29 05:32:00 -05:00
|
|
|
$transaction->save();
|
|
|
|
}
|
2015-07-17 10:35:04 -05:00
|
|
|
if ($transaction->amount > 0) {
|
2015-03-29 05:32:00 -05:00
|
|
|
$transaction->amount = $data['amount'];
|
2015-07-06 11:57:15 -05:00
|
|
|
$transaction->account_id = $toAccount->id;
|
2015-03-29 05:32:00 -05:00
|
|
|
$transaction->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$journal->save();
|
|
|
|
|
2015-04-28 03:36:13 -05:00
|
|
|
// update tags:
|
|
|
|
if (isset($data['tags']) && is_array($data['tags'])) {
|
|
|
|
$this->updateTags($journal, $data['tags']);
|
|
|
|
}
|
|
|
|
|
2015-03-29 05:32:00 -05:00
|
|
|
return $journal;
|
|
|
|
}
|
|
|
|
|
2015-04-28 03:36:13 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param array $array
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function updateTags(TransactionJournal $journal, array $array)
|
|
|
|
{
|
2015-04-28 10:10:52 -05:00
|
|
|
// create tag repository
|
|
|
|
/** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
|
2015-07-07 12:09:45 -05:00
|
|
|
$tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
|
2015-04-28 10:10:52 -05:00
|
|
|
|
|
|
|
|
2015-04-28 03:36:13 -05:00
|
|
|
// find or create all tags:
|
|
|
|
$tags = [];
|
|
|
|
$ids = [];
|
|
|
|
foreach ($array as $name) {
|
2015-04-28 14:01:54 -05:00
|
|
|
if (strlen(trim($name)) > 0) {
|
|
|
|
$tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
|
|
|
|
$tags[] = $tag;
|
|
|
|
$ids[] = $tag->id;
|
|
|
|
}
|
2015-04-28 03:36:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete all tags connected to journal not in this array:
|
|
|
|
if (count($ids) > 0) {
|
|
|
|
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->whereNotIn('tag_id', $ids)->delete();
|
|
|
|
}
|
2015-05-02 15:30:59 -05:00
|
|
|
// if count is zero, delete them all:
|
2015-05-03 05:58:55 -05:00
|
|
|
if (count($ids) == 0) {
|
2015-05-02 15:30:59 -05:00
|
|
|
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->delete();
|
|
|
|
}
|
2015-04-28 03:36:13 -05:00
|
|
|
|
|
|
|
// connect each tag to journal (if not yet connected):
|
2015-04-28 06:47:13 -05:00
|
|
|
/** @var Tag $tag */
|
|
|
|
foreach ($tags as $tag) {
|
2015-04-28 10:10:52 -05:00
|
|
|
$tagRepository->connect($journal, $tag);
|
2015-04-28 03:36:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 05:32:00 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionType $type
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return array
|
2016-02-17 14:14:32 -06:00
|
|
|
* @throws FireflyException
|
2016-01-15 13:48:06 -06:00
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
2015-03-29 05:32:00 -05:00
|
|
|
*/
|
|
|
|
protected function storeAccounts(TransactionType $type, array $data)
|
|
|
|
{
|
2015-05-17 05:49:09 -05:00
|
|
|
$fromAccount = null;
|
|
|
|
$toAccount = null;
|
2015-03-29 05:32:00 -05:00
|
|
|
switch ($type->type) {
|
2015-12-09 18:39:50 -06:00
|
|
|
case TransactionType::WITHDRAWAL:
|
2015-05-17 05:49:09 -05:00
|
|
|
list($fromAccount, $toAccount) = $this->storeWithdrawalAccounts($data);
|
2015-02-27 07:27:04 -06:00
|
|
|
break;
|
|
|
|
|
2015-12-09 18:39:50 -06:00
|
|
|
case TransactionType::DEPOSIT:
|
2015-05-17 05:49:09 -05:00
|
|
|
list($fromAccount, $toAccount) = $this->storeDepositAccounts($data);
|
2015-02-27 07:27:04 -06:00
|
|
|
|
|
|
|
break;
|
2015-12-09 18:39:50 -06:00
|
|
|
case TransactionType::TRANSFER:
|
2015-05-17 05:49:09 -05:00
|
|
|
$fromAccount = Account::find($data['account_from_id']);
|
|
|
|
$toAccount = Account::find($data['account_to_id']);
|
2015-02-27 07:27:04 -06:00
|
|
|
break;
|
|
|
|
}
|
2015-05-17 05:49:09 -05:00
|
|
|
|
|
|
|
if (is_null($toAccount)) {
|
2015-04-03 15:54:21 -05:00
|
|
|
Log::error('"to"-account is null, so we cannot continue!');
|
2016-02-17 08:52:46 -06:00
|
|
|
throw new FireflyException('"to"-account is null, so we cannot continue!');
|
2015-05-09 11:30:58 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2015-04-03 15:54:21 -05:00
|
|
|
}
|
2015-05-09 11:30:58 -05:00
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
2015-05-17 05:49:09 -05:00
|
|
|
if (is_null($fromAccount)) {
|
2015-04-03 15:54:21 -05:00
|
|
|
Log::error('"from"-account is null, so we cannot continue!');
|
2016-02-17 08:52:46 -06:00
|
|
|
throw new FireflyException('"from"-account is null, so we cannot continue!');
|
2015-07-07 12:09:45 -05:00
|
|
|
|
2015-05-09 11:30:58 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2015-04-03 15:54:21 -05:00
|
|
|
}
|
2015-05-14 02:51:54 -05:00
|
|
|
|
2015-05-09 11:30:58 -05:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-02-27 07:27:04 -06:00
|
|
|
|
2015-05-17 05:49:09 -05:00
|
|
|
return [$fromAccount, $toAccount];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-01-20 08:23:36 -06:00
|
|
|
protected function storeDepositAccounts(array $data)
|
2015-05-17 05:49:09 -05:00
|
|
|
{
|
2016-01-20 08:23:36 -06:00
|
|
|
$toAccount = Account::find($data['account_id']);
|
2015-05-17 05:49:09 -05:00
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
if (strlen($data['revenue_account']) > 0) {
|
|
|
|
$fromType = AccountType::where('type', 'Revenue account')->first();
|
|
|
|
$fromAccount = Account::firstOrCreateEncrypted(
|
|
|
|
['user_id' => $data['user'], 'account_type_id' => $fromType->id, 'name' => $data['revenue_account'], 'active' => 1]
|
2015-05-17 05:49:09 -05:00
|
|
|
);
|
|
|
|
} else {
|
2016-01-20 08:23:36 -06:00
|
|
|
$toType = AccountType::where('type', 'Cash account')->first();
|
|
|
|
$fromAccount = Account::firstOrCreateEncrypted(
|
2015-05-17 05:49:09 -05:00
|
|
|
['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$fromAccount, $toAccount];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-01-20 08:23:36 -06:00
|
|
|
protected function storeWithdrawalAccounts(array $data)
|
2015-05-17 05:49:09 -05:00
|
|
|
{
|
2016-01-20 08:23:36 -06:00
|
|
|
$fromAccount = Account::find($data['account_id']);
|
2015-05-17 05:49:09 -05:00
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
if (strlen($data['expense_account']) > 0) {
|
|
|
|
$toType = AccountType::where('type', 'Expense account')->first();
|
|
|
|
$toAccount = Account::firstOrCreateEncrypted(
|
|
|
|
['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => $data['expense_account'], 'active' => 1]
|
2015-05-17 05:49:09 -05:00
|
|
|
);
|
|
|
|
} else {
|
2016-01-20 08:23:36 -06:00
|
|
|
$toType = AccountType::where('type', 'Cash account')->first();
|
|
|
|
$toAccount = Account::firstOrCreateEncrypted(
|
2015-05-17 05:49:09 -05:00
|
|
|
['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$fromAccount, $toAccount];
|
2015-02-27 07:27:04 -06:00
|
|
|
}
|
2015-04-03 02:30:44 -05:00
|
|
|
}
|