diff --git a/app/Helpers/Report/BudgetReportHelper.php b/app/Helpers/Report/BudgetReportHelper.php index ccfc7099c1..8a9de7eaec 100644 --- a/app/Helpers/Report/BudgetReportHelper.php +++ b/app/Helpers/Report/BudgetReportHelper.php @@ -124,8 +124,8 @@ class BudgetReportHelper implements BudgetReportHelperInterface $set = new Collection; /** @var Budget $budget */ foreach ($budgets as $budget) { - $expenses = $repository->getExpensesPerDay($budget, $start, $end, $accounts); - $total = strval($expenses->sum('dailyAmount')); + $expenses = $repository->spentPerDay($budget, $start, $end, $accounts); + $total = strval(array_sum($expenses)); if (bccomp($total, '0') === -1) { $set->push($budget); } diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php index 7b8164c693..0fb3acae7c 100644 --- a/app/Http/Controllers/BudgetController.php +++ b/app/Http/Controllers/BudgetController.php @@ -264,7 +264,7 @@ class BudgetController extends Controller $set = $budget->limitrepetitions()->orderBy('startdate', 'DESC')->get(); $subTitle = e($budget->name); $journals->setPath('/budgets/show/' . $budget->id); - $spentArray = $repository->spentPerDay($budget, $start, $end); + $spentArray = $repository->spentPerDay($budget, $start, $end, new Collection); $limits = new Collection(); /** @var LimitRepetition $entry */ @@ -298,7 +298,7 @@ class BudgetController extends Controller $set = new Collection([$repetition]); $subTitle = trans('firefly.budget_in_month', ['name' => $budget->name, 'month' => $repetition->startdate->formatLocalized($this->monthFormat)]); $journals->setPath('/budgets/show/' . $budget->id . '/' . $repetition->id); - $spentArray = $repository->spentPerDay($budget, $start, $end); + $spentArray = $repository->spentPerDay($budget, $start, $end, new Collection); $limits = new Collection(); /** @var LimitRepetition $entry */ diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php index 8a30017f99..27e78f2142 100644 --- a/app/Http/Controllers/Chart/AccountController.php +++ b/app/Http/Controllers/Chart/AccountController.php @@ -13,7 +13,7 @@ use Illuminate\Support\Collection; use Preferences; use Response; -/** +/** checked * Class AccountController * * @package FireflyIII\Http\Controllers\Chart diff --git a/app/Http/Controllers/Chart/BillController.php b/app/Http/Controllers/Chart/BillController.php index 00316ed43e..f0b654f7f4 100644 --- a/app/Http/Controllers/Chart/BillController.php +++ b/app/Http/Controllers/Chart/BillController.php @@ -24,7 +24,7 @@ class BillController extends Controller protected $generator; /** - * + * checked */ public function __construct() { diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 537b037a3c..66814507ed 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -39,6 +39,8 @@ class BudgetController extends Controller } /** + * checked + * * @param BudgetRepositoryInterface $repository * @param Budget $budget * @@ -59,7 +61,7 @@ class BudgetController extends Controller $cache->addProperty('budget'); if ($cache->has()) { - return Response::json($cache->get()); + //return Response::json($cache->get()); } $final = clone $last; @@ -67,7 +69,7 @@ class BudgetController extends Controller $last = Navigation::endOfX($last, $range, $final); $entries = new Collection; // get all expenses: - $spentArray = $repository->spentPerDay($budget, $first, $last); + $spentArray = $repository->spentPerDay($budget, $first, $last, new Collection); while ($first < $last) { @@ -113,19 +115,14 @@ class BudgetController extends Controller return Response::json($cache->get()); } - $set = $repository->getExpensesPerDay($budget, $start, $end); + $set = $repository->spentPerDay($budget, $start, $end, new Collection); $entries = new Collection; $amount = $repetition->amount; // get sum (har har)! while ($start <= $end) { $formatted = $start->format('Y-m-d'); - $filtered = $set->filter( - function (Budget $obj) use ($formatted) { - return $obj->date == $formatted; - } - ); - $sum = is_null($filtered->first()) ? '0' : $filtered->first()->dailyAmount; + $sum = $set[$formatted] ?? '0'; /* * Sum of expenses on this day: @@ -135,7 +132,7 @@ class BudgetController extends Controller $start->addDay(); } - $data = $this->generator->budgetLimit($entries); + $data = $this->generator->budgetLimit($entries, 'monthAndDay'); $cache->store($data); return Response::json($data); diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 4759d19f54..ce34559df8 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -100,7 +100,7 @@ class ReportController extends Controller // lower threshold if ($start < session('first')) { - Log::debug('Start is ' . $start . ' but sessionfirst is ' . session('first')); + Log::debug('Start is ' . $start . ' but session first is ' . session('first')); $start = session('first'); } diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 8417d5850a..a118f4ed15 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -1,7 +1,7 @@ 'between:1,255', 'amount' => 'required|numeric', ]; - protected $dates = ['created_at', 'updated_at', 'deleted_at']; use SoftDeletes, ValidatingTrait; + /** + * @param Builder $query + * @param string $table + * + * @return bool + */ + public static function isJoined(Builder $query, string $table):bool + { + $joins = $query->getQuery()->joins; + if (is_null($joins)) { + return false; + } + foreach ($joins as $join) { + if ($join->table === $table) { + return true; + } + } + + return false; + } + /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ @@ -59,54 +80,6 @@ class Transaction extends Model return $this->belongsTo('FireflyIII\Models\Account'); } - /** - * @param $value - * - * @return float|int - */ - public function getAmountAttribute($value) - { - return $value; - } - - /** - * @param EloquentBuilder $query - * @param Carbon $date - * - * @return mixed - */ - public function scopeAfter(EloquentBuilder $query, Carbon $date) - { - return $query->where('transaction_journals.date', '>=', $date->format('Y-m-d 00:00:00')); - } - - /** - * @param EloquentBuilder $query - * @param Carbon $date - * - * @return mixed - */ - public function scopeBefore(EloquentBuilder $query, Carbon $date) - { - return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00')); - } - - /** - * @param $value - */ - public function setAmountAttribute($value) - { - $this->attributes['amount'] = strval(round($value, 2)); - } - - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ - public function transactionJournal() - { - return $this->belongsTo('FireflyIII\Models\TransactionJournal'); - } - /** * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ @@ -122,4 +95,74 @@ class Transaction extends Model { return $this->belongsToMany('FireflyIII\Models\Category'); } + + /** + * @param $value + * + * @return float|int + */ + public function getAmountAttribute($value) + { + return $value; + } + + /** + * + * @param Builder $query + * @param Carbon $date + */ + public function scopeAfter(Builder $query, Carbon $date) + { + if (!self::isJoined($query, 'transaction_journals')) { + $query->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'); + } + $query->where('transaction_journals.date', '>=', $date->format('Y-m-d 00:00:00')); + } + + /** + * + * @param Builder $query + * @param Carbon $date + * + */ + public function scopeBefore(Builder $query, Carbon $date) + { + if (!self::isJoined($query, 'transaction_journals')) { + $query->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'); + } + $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00')); + } + + /** + * + * @param Builder $query + * @param array $types + */ + public function scopeTransactionTypes(Builder $query, array $types) + { + if (!self::isJoined($query, 'transaction_journals')) { + $query->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'); + } + + if (!self::isJoined($query, 'transaction_types')) { + $query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'); + } + $query->whereIn('transaction_types.type', $types); + } + + /** + * @param $value + */ + public function setAmountAttribute($value) + { + $this->attributes['amount'] = strval(round($value, 2)); + } + + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function transactionJournal() + { + return $this->belongsTo('FireflyIII\Models\TransactionJournal'); + } } diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index c897a171f2..c097f4b230 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -21,6 +21,7 @@ use Illuminate\Database\Query\JoinClause; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Input; +use Log; /** * Class BudgetRepository @@ -310,16 +311,19 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn * @param Carbon $start * @param Carbon $end * + * @deprecated + * * @return array */ public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array { + // get budgets, $ids = $accounts->pluck('id')->toArray(); $budgetIds = $budgets->pluck('id')->toArray(); /** @var Collection $set */ $set = $this->user->budgets() - ->leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id') + ->join('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id') ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id') ->leftJoin( 'transactions', function (JoinClause $join) { @@ -340,6 +344,27 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn ] ); + // run it again, for transactions this time. + /** @var Collection $secondSet */ + $secondSet = $this->user->budgets() + ->join('budget_transaction', 'budgets.id', '=', 'budget_transaction.budget_id') + ->leftJoin('transactions', 'transactions.id', '=', 'budget_transaction.transaction_id') + ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + ->where('transactions.amount', '<', 0) + ->groupBy('budgets.id') + ->groupBy('dateFormatted') + ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) + ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) + ->whereIn('transactions.account_id', $ids) + ->whereIn('budgets.id', $budgetIds) + ->get( + [ + 'budgets.*', + DB::raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y") AS `dateFormatted`'), + DB::raw('SUM(`transactions`.`amount`) AS `sumAmount`'), + ] + ); + $set = $set->sortBy( function (Budget $budget) { return strtolower($budget->name); @@ -348,16 +373,36 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn $return = []; foreach ($set as $budget) { + Log::debug('First set, budget #' . $budget->id . ' (' . $budget->name . ')'); $id = $budget->id; if (!isset($return[$id])) { + Log::debug('$return[$id] is not set, now created.'); $return[$id] = [ 'budget' => $budget, 'entries' => [], ]; } + Log::debug('Add new entry to entries, for ' . $budget->dateFormatted . ' and amount ' . $budget->sumAmount); // store each entry: $return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount; } + unset($budget); + + // run the second set: + foreach ($secondSet as $entry) { + $id = $entry->id; + // create it if it still does not exist (not really likely) + if (!isset($return[$id])) { + $return[$id] = [ + 'budget' => $entry, + 'entries' => [], + ]; + } + // this one might be filled too: + $startAmount = $return[$id]['entries'][$entry->dateFormatted] ?? '0'; + // store each entry: + $return[$id]['entries'][$entry->dateFormatted] = bcadd($startAmount, $entry->sumAmount); + } return $return; } @@ -453,40 +498,6 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn return $set; } - /** - * Returns the expenses for this budget grouped per day, with the date - * in "date" (a string, not a Carbon) and the amount in "dailyAmount". - * - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts - * - * @return Collection - */ - public function getExpensesPerDay(Budget $budget, Carbon $start, Carbon $end, Collection $accounts = null): Collection - { - $query = $this->user->budgets() - ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.budget_id', '=', 'budgets.id') - ->leftJoin('transaction_journals', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - ->leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') - ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) - ->whereNull('transaction_journals.deleted_at') - ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) - ->where('budgets.id', $budget->id) - ->where('transactions.amount', '<', 0) - ->groupBy('transaction_journals.date') - ->orderBy('transaction_journals.date'); - if (!is_null($accounts) && $accounts->count() > 0) { - $ids = $accounts->pluck('id')->toArray(); - $query->whereIn('transactions.account_id', $ids); - } - $set - = $query->get(['transaction_journals.date', DB::raw('SUM(`transactions`.`amount`) as `dailyAmount`')]); - - return $set; - } - /** * @param Budget $budget * @@ -785,13 +796,14 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn * Where yyyy-mm-dd is the date and is the money spent using DEPOSITS in the $budget * from all the users accounts. * - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end + * @param Budget $budget + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts * * @return array */ - public function spentPerDay(Budget $budget, Carbon $start, Carbon $end): array + public function spentPerDay(Budget $budget, Carbon $start, Carbon $end, Collection $accounts): array { /** @var Collection $query */ $query = $budget->transactionjournals() @@ -807,6 +819,24 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn $return[$entry['dateFormatted']] = $entry['sum']; } + // also search transactions: + $query = $budget->transactions() + ->transactionTypes([TransactionType::WITHDRAWAL]) + ->where('transactions.amount', '<', 0) + ->before($end) + ->after($start) + ->groupBy('dateFormatted')->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`transactions`.`amount`) AS `sum`')]); + foreach ($query as $newEntry) { + // add to return array. + $date = $newEntry['dateFormatted']; + if (isset($return[$date])) { + $return[$date] = bcadd($newEntry['sum'], $return[$date]); + continue; + } + + $return[$date] = $newEntry['sum']; + } + return $return; } diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index b62bc3f208..577b5e18aa 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -131,6 +131,8 @@ interface BudgetRepositoryInterface * @param Carbon $start * @param Carbon $end * + * @deprecated + * * @return array */ public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array; @@ -180,19 +182,6 @@ interface BudgetRepositoryInterface */ public function getExpenses(Budget $budget, Collection $accounts, Carbon $start, Carbon $end):Collection; - /** - * Returns the expenses for this budget grouped per day, with the date - * in "date" (a string, not a Carbon) and the amount in "dailyAmount". - * - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts - * - * @return Collection - */ - public function getExpensesPerDay(Budget $budget, Carbon $start, Carbon $end, Collection $accounts = null) : Collection; - /** * @param Budget $budget * @@ -287,10 +276,11 @@ interface BudgetRepositoryInterface * @param Budget $budget * @param Carbon $start * @param Carbon $end + * @param Collection $accounts * * @return array */ - public function spentPerDay(Budget $budget, Carbon $start, Carbon $end): array; + public function spentPerDay(Budget $budget, Carbon $start, Carbon $end, Collection $accounts): array; /** * @param array $data diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index 617c87e9f8..d9ddb99c54 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -81,9 +81,13 @@ class JournalRepository implements JournalRepositoryInterface public function first(): TransactionJournal { $entry = $this->user->transactionjournals()->orderBy('date', 'ASC')->first(['transaction_journals.*']); + if (is_null($entry)) { + Log::debug('Could not find first transaction journal.'); + return new TransactionJournal; } + Log::debug('Found first journal: ', ['date' => $entry->date->format('Y-m-d')]); return $entry; } diff --git a/database/seeds/SplitDataSeeder.php b/database/seeds/SplitDataSeeder.php index 1e4c864b8e..a7c01b30db 100644 --- a/database/seeds/SplitDataSeeder.php +++ b/database/seeds/SplitDataSeeder.php @@ -20,6 +20,7 @@ use Carbon\Carbon; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Support\Migration\TestData; +use FireflyIII\User; use Illuminate\Database\Seeder; /** @@ -49,101 +50,206 @@ class SplitDataSeeder extends Seeder $user = TestData::createUsers(); // create all kinds of static data: - $assets = [ - [ - 'name' => 'Checking Account', - 'iban' => 'NL11XOLA6707795988', - 'meta' => [ - 'accountRole' => 'defaultAsset', - ], - ], - [ - 'name' => 'Alternate Checking Account', - 'iban' => 'NL40UKBK3619908726', - 'meta' => [ - 'accountRole' => 'defaultAsset', - ], - ], - [ - 'name' => 'Savings Account', - 'iban' => 'NL96DZCO4665940223', - 'meta' => [ - 'accountRole' => 'savingAsset', - ], - ], - [ - 'name' => 'Shared Checking Account', - 'iban' => 'NL81RCQZ7160379858', - 'meta' => [ - 'accountRole' => 'sharedAsset', - ], - ], - ]; + $assets = [['name' => 'Checking Account', 'iban' => 'NL11XOLA6707795988', 'meta' => ['accountRole' => 'defaultAsset']], + ['name' => 'Alternate Checking Account', 'iban' => 'NL40UKBK3619908726', 'meta' => ['accountRole' => 'defaultAsset']], + ['name' => 'Savings Account', 'iban' => 'NL96DZCO4665940223', 'meta' => ['accountRole' => 'savingAsset']], + ['name' => 'Shared Checking Account', 'iban' => 'NL81RCQZ7160379858', 'meta' => ['accountRole' => 'sharedAsset']]]; + + // some asset accounts TestData::createAssetAccounts($user, $assets); + + // budgets, categories and others: TestData::createBudgets($user); TestData::createCategories($user); TestData::createExpenseAccounts($user); TestData::createRevenueAccounts($user); TestData::createPiggybanks($user, 'Savings Account'); + TestData::createBills($user); + // some bills /* * Create splitted expense of 66,- */ - $today = new Carbon; - $today->subDays(2); + $today = new Carbon('2012-03-14'); if (!$skipWithdrawal) { - $journal = TransactionJournal::create( + $this->generateWithdrawals($user); + } + + // create splitted income of 99,- + $today->addDay(); + + if (!$skipDeposit) { + $this->generateDeposits(); + } + // create a splitted transfer of 57,- (19) + // $today->addDay(); + + if (!$skipTransfer) { + $this->generateTransfers(); + } + } + + private function generateDeposits() + { + $journal = TransactionJournal::create( + [ + 'user_id' => $user->id, + 'transaction_type_id' => 2, // expense + 'transaction_currency_id' => 1, + 'description' => 'Split Income (journal)', + 'completed' => 1, + 'date' => $today->format('Y-m-d'), + ] + ); + + // split in 6 transactions (multiple destinations). 22,- each + // source is TestData Checking Account. + // also attach some budgets and stuff. + $destinations = ['Checking Account', 'Savings Account', 'Shared Checking Account']; + $source = TestData::findAccount($user, 'Belastingdienst'); + $budgets = ['Groceries', 'Groceries', 'Car']; + $categories = ['Bills', 'Bills', 'Car']; + foreach ($destinations as $index => $dest) { + $bud = $budgets[$index]; + $cat = $categories[$index]; + $destination = TestData::findAccount($user, $dest); + + $one = Transaction::create( [ - 'user_id' => $user->id, - 'transaction_type_id' => 1, // withdrawal - 'transaction_currency_id' => 1, - 'description' => 'Split Even Expense (journal (50/50))', - 'completed' => 1, - 'date' => $today->format('Y-m-d'), + 'account_id' => $source->id, + 'transaction_journal_id' => $journal->id, + 'amount' => '-33', + ] ); - // split in 6 transactions (multiple destinations). 22,- each - // source is TestData Checking Account. - // also attach some budgets and stuff. - $destinations = ['SixtyFive', 'EightyFour']; - $budgets = ['Groceries', 'Car']; - $categories = ['Bills', 'Bills']; - $amounts = [50, 50]; - $source = TestData::findAccount($user, 'Alternate Checking Account'); - foreach ($destinations as $index => $dest) { - $bud = $budgets[$index]; - $cat = $categories[$index]; - $destination = TestData::findAccount($user, $dest); + $two = Transaction::create( + [ + 'account_id' => $destination->id, + 'transaction_journal_id' => $journal->id, + 'amount' => '33', - $one = Transaction::create( - [ - 'account_id' => $source->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amounts[$index] * -1, + ] + ); - ] - ); + $one->budgets()->save(TestData::findBudget($user, $bud)); + $two->budgets()->save(TestData::findBudget($user, $bud)); - $two = Transaction::create( - [ - 'account_id' => $destination->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amounts[$index], - - ] - ); - - $one->budgets()->save(TestData::findBudget($user, $bud)); - $two->budgets()->save(TestData::findBudget($user, $bud)); - - $one->categories()->save(TestData::findCategory($user, $cat)); - $two->categories()->save(TestData::findCategory($user, $cat)); - } + $one->categories()->save(TestData::findCategory($user, $cat)); + $two->categories()->save(TestData::findCategory($user, $cat)); } - // AND ANOTHER ONE - $today->addDay(); + } + + private function generateTransfers() + { + $journal = TransactionJournal::create( + [ + 'user_id' => $user->id, + 'transaction_type_id' => 3, // transfer + 'transaction_currency_id' => 1, + 'description' => 'Split Transfer (journal)', + 'completed' => 1, + 'date' => $today->format('Y-m-d'), + ] + ); + + + $source = TestData::findAccount($user, 'Alternate Checking Account'); + $destinations = ['Checking Account', 'Savings Account', 'Shared Checking Account']; + $budgets = ['Groceries', 'Groceries', 'Car']; + $categories = ['Bills', 'Bills', 'Car']; + foreach ($destinations as $index => $dest) { + $bud = $budgets[$index]; + $cat = $categories[$index]; + $destination = TestData::findAccount($user, $dest); + + $one = Transaction::create( + [ + 'account_id' => $source->id, + 'transaction_journal_id' => $journal->id, + 'amount' => '-19', + + ] + ); + + $two = Transaction::create( + [ + 'account_id' => $destination->id, + 'transaction_journal_id' => $journal->id, + 'amount' => '19', + + ] + ); + + $one->budgets()->save(TestData::findBudget($user, $bud)); + $two->budgets()->save(TestData::findBudget($user, $bud)); + + $one->categories()->save(TestData::findCategory($user, $cat)); + $two->categories()->save(TestData::findCategory($user, $cat)); + } + } + + private function generateWithdrawals(User $user) + { + /* + * TRANSACTION ONE + */ + $date = new Carbon('2012-03-15'); + $journal = TransactionJournal::create( + [ + 'user_id' => $user->id, + 'transaction_type_id' => 1, // withdrawal + 'transaction_currency_id' => 1, + 'description' => 'Split Even Expense (journal (50/50))', + 'completed' => 1, + 'date' => $date->format('Y-m-d'), + ] + ); + + // split in 6 transactions (multiple destinations). 22,- each + // source is TestData Checking Account. + // also attach some budgets and stuff. + $destinations = ['SixtyFive', 'EightyFour']; + $budgets = ['Groceries', 'Car']; + $categories = ['Bills', 'Bills']; + $amounts = [50, 50]; + $source = TestData::findAccount($user, 'Alternate Checking Account'); + foreach ($destinations as $index => $dest) { + $bud = $budgets[$index]; + $cat = $categories[$index]; + $destination = TestData::findAccount($user, $dest); + + $one = Transaction::create( + [ + 'account_id' => $source->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amounts[$index] * -1, + + ] + ); + + $two = Transaction::create( + [ + 'account_id' => $destination->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amounts[$index], + + ] + ); + + $one->budgets()->save(TestData::findBudget($user, $bud)); + $two->budgets()->save(TestData::findBudget($user, $bud)); + + $one->categories()->save(TestData::findCategory($user, $cat)); + $two->categories()->save(TestData::findCategory($user, $cat)); + } + + /* + * GENERATE TRANSACTION TWO. + */ + + $date = new Carbon; $journal = TransactionJournal::create( [ 'user_id' => $user->id, @@ -151,7 +257,7 @@ class SplitDataSeeder extends Seeder 'transaction_currency_id' => 1, 'description' => 'Split Uneven Expense (journal (15/34/51=100))', 'completed' => 1, - 'date' => $today->format('Y-m-d'), + 'date' => $date->format('Y-m-d'), ] ); @@ -192,108 +298,5 @@ class SplitDataSeeder extends Seeder $one->categories()->save(TestData::findCategory($user, $cat)); $two->categories()->save(TestData::findCategory($user, $cat)); } - - // create splitted income of 99,- - $today->addDay(); - - if (!$skipDeposit) { - $journal = TransactionJournal::create( - [ - 'user_id' => $user->id, - 'transaction_type_id' => 2, // expense - 'transaction_currency_id' => 1, - 'description' => 'Split Income (journal)', - 'completed' => 1, - 'date' => $today->format('Y-m-d'), - ] - ); - - // split in 6 transactions (multiple destinations). 22,- each - // source is TestData Checking Account. - // also attach some budgets and stuff. - $destinations = ['Checking Account', 'Savings Account', 'Shared Checking Account']; - $source = TestData::findAccount($user, 'Belastingdienst'); - $budgets = ['Groceries', 'Groceries', 'Car']; - $categories = ['Bills', 'Bills', 'Car']; - foreach ($destinations as $index => $dest) { - $bud = $budgets[$index]; - $cat = $categories[$index]; - $destination = TestData::findAccount($user, $dest); - - $one = Transaction::create( - [ - 'account_id' => $source->id, - 'transaction_journal_id' => $journal->id, - 'amount' => '-33', - - ] - ); - - $two = Transaction::create( - [ - 'account_id' => $destination->id, - 'transaction_journal_id' => $journal->id, - 'amount' => '33', - - ] - ); - - $one->budgets()->save(TestData::findBudget($user, $bud)); - $two->budgets()->save(TestData::findBudget($user, $bud)); - - $one->categories()->save(TestData::findCategory($user, $cat)); - $two->categories()->save(TestData::findCategory($user, $cat)); - } - } - // create a splitted transfer of 57,- (19) - $today->addDay(); - - if (!$skipTransfer) { - $journal = TransactionJournal::create( - [ - 'user_id' => $user->id, - 'transaction_type_id' => 3, // transfer - 'transaction_currency_id' => 1, - 'description' => 'Split Transfer (journal)', - 'completed' => 1, - 'date' => $today->format('Y-m-d'), - ] - ); - - - $source = TestData::findAccount($user, 'Alternate Checking Account'); - $destinations = ['Checking Account', 'Savings Account', 'Shared Checking Account']; - $budgets = ['Groceries', 'Groceries', 'Car']; - $categories = ['Bills', 'Bills', 'Car']; - foreach ($destinations as $index => $dest) { - $bud = $budgets[$index]; - $cat = $categories[$index]; - $destination = TestData::findAccount($user, $dest); - - $one = Transaction::create( - [ - 'account_id' => $source->id, - 'transaction_journal_id' => $journal->id, - 'amount' => '-19', - - ] - ); - - $two = Transaction::create( - [ - 'account_id' => $destination->id, - 'transaction_journal_id' => $journal->id, - 'amount' => '19', - - ] - ); - - $one->budgets()->save(TestData::findBudget($user, $bud)); - $two->budgets()->save(TestData::findBudget($user, $bud)); - - $one->categories()->save(TestData::findCategory($user, $cat)); - $two->categories()->save(TestData::findCategory($user, $cat)); - } - } } }