| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Repositories\Account; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-21 12:16:41 +01:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2015-02-09 07:56:24 +01:00
										 |  |  | use Config; | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  | use DB; | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  | use FireflyIII\Models\AccountMeta; | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  | use FireflyIII\Models\PiggyBank; | 
					
						
							| 
									
										
										
										
											2015-03-13 08:44:07 +01:00
										 |  |  | use FireflyIII\Models\Preference; | 
					
						
							| 
									
										
										
										
											2015-02-09 07:56:24 +01:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							| 
									
										
										
										
											2016-03-20 11:47:10 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2015-04-04 21:23:37 +02:00
										 |  |  | use Illuminate\Database\Eloquent\Relations\HasMany; | 
					
						
							| 
									
										
										
										
											2015-02-21 12:16:41 +01:00
										 |  |  | use Illuminate\Pagination\LengthAwarePaginator; | 
					
						
							| 
									
										
										
										
											2015-03-13 08:44:07 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2015-02-21 12:16:41 +01:00
										 |  |  | use Session; | 
					
						
							| 
									
										
										
										
											2015-03-21 08:51:34 +01:00
										 |  |  | use Steam; | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-05 20:46:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-05-26 08:17:58 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |  * Class AccountRepository | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Repositories\Account | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AccountRepository implements AccountRepositoryInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 11:47:10 +01:00
										 |  |  |     /** @var User */ | 
					
						
							|  |  |  |     private $user; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-30 17:47:13 +02:00
										 |  |  |     /** @var array  */ | 
					
						
							|  |  |  |     private $validFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 11:47:10 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * AttachmentRepository constructor. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param User $user | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(User $user) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 08:44:07 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-04-04 21:23:37 +02:00
										 |  |  |      * @param array $types | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-03-13 08:44:07 +01:00
										 |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function countAccounts(array $types): int | 
					
						
							| 
									
										
										
										
											2015-03-13 08:44:07 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-03-20 16:47:53 +01:00
										 |  |  |         $count = $this->user->accounts()->accountTypeIn($types)->count(); | 
					
						
							| 
									
										
										
										
											2015-12-25 17:11:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $count; | 
					
						
							| 
									
										
										
										
											2015-03-13 08:44:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							| 
									
										
										
										
											2015-07-10 20:48:45 +02:00
										 |  |  |      * @param Account $moveTo | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return boolean | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function destroy(Account $account, Account $moveTo = null): bool | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-10 20:48:45 +02:00
										 |  |  |         if (!is_null($moveTo)) { | 
					
						
							|  |  |  |             // update all transactions:
 | 
					
						
							|  |  |  |             DB::table('transactions')->where('account_id', $account->id)->update(['account_id' => $moveTo->id]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |         $account->delete(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @deprecated | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param $accountId | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Account | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function find(int $accountId): Account | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-03-20 16:47:53 +01:00
										 |  |  |         return $this->user->accounts()->findOrNew($accountId); | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Gets all the accounts by ID, for a given set. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array $ids | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return \Illuminate\Support\Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function get(array $ids): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->user->accounts()->whereIn('id', $ids)->get(['accounts.*']); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-04 21:23:37 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param array $types | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function getAccounts(array $types): Collection | 
					
						
							| 
									
										
										
										
											2015-04-04 21:23:37 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-08 13:05:33 +02:00
										 |  |  |         /** @var Collection $result */ | 
					
						
							| 
									
										
										
										
											2016-03-20 16:47:53 +01:00
										 |  |  |         $result = $this->user->accounts()->with( | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |             ['accountmeta' => function (HasMany $query) { | 
					
						
							| 
									
										
										
										
											2015-04-04 21:23:37 +02:00
										 |  |  |                 $query->where('name', 'accountRole'); | 
					
						
							|  |  |  |             }] | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |         )->accountTypeIn($types)->get(['accounts.*']); | 
					
						
							| 
									
										
										
										
											2015-04-07 10:14:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 13:05:33 +02:00
										 |  |  |         $result = $result->sortBy( | 
					
						
							|  |  |  |             function (Account $account) { | 
					
						
							|  |  |  |                 return strtolower($account->name); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2016-01-15 13:13:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-07 10:14:10 +02:00
										 |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-12-27 07:59:00 +01:00
										 |  |  |      * This method returns the users credit cards, along with some basic information about the | 
					
						
							|  |  |  |      * balance they have on their CC. To be used in the JSON boxes on the front page that say | 
					
						
							|  |  |  |      * how many bills there are still left to pay. The balance will be saved in field "balance". | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * To get the balance, the field "date" is necessary. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Carbon $date | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-04-07 10:14:10 +02:00
										 |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function getCreditCards(Carbon $date): Collection | 
					
						
							| 
									
										
										
										
											2015-04-07 10:14:10 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-03-20 16:47:53 +01:00
										 |  |  |         $set = $this->user->accounts() | 
					
						
							|  |  |  |                           ->hasMetaValue('accountRole', 'ccAsset') | 
					
						
							|  |  |  |                           ->hasMetaValue('ccType', 'monthlyFull') | 
					
						
							|  |  |  |                           ->leftJoin('transactions', 'transactions.account_id', '=', 'accounts.id') | 
					
						
							|  |  |  |                           ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') | 
					
						
							|  |  |  |                           ->whereNull('transactions.deleted_at') | 
					
						
							|  |  |  |                           ->where('transaction_journals.date', '<=', $date->format('Y-m-d')) | 
					
						
							|  |  |  |                           ->groupBy('accounts.id') | 
					
						
							|  |  |  |                           ->get( | 
					
						
							|  |  |  |                               [ | 
					
						
							|  |  |  |                                   'accounts.*', | 
					
						
							|  |  |  |                                   'ccType.data as ccType', | 
					
						
							|  |  |  |                                   'accountRole.data as accountRole', | 
					
						
							|  |  |  |                                   DB::Raw('SUM(`transactions`.`amount`) AS `balance`'), | 
					
						
							|  |  |  |                               ] | 
					
						
							|  |  |  |                           ); | 
					
						
							| 
									
										
										
										
											2016-01-15 13:13:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-27 19:51:20 +01:00
										 |  |  |         return $set; | 
					
						
							| 
									
										
										
										
											2015-04-04 21:23:37 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-03 09:30:44 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param TransactionJournal $journal | 
					
						
							|  |  |  |      * @param Account            $account | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Transaction | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function getFirstTransaction(TransactionJournal $journal, Account $account): Transaction | 
					
						
							| 
									
										
										
										
											2015-04-03 09:30:44 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-26 08:44:34 +01:00
										 |  |  |         $transaction = $journal->transactions()->where('account_id', $account->id)->first(); | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |         if (is_null($transaction)) { | 
					
						
							|  |  |  |             $transaction = new Transaction; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-04-03 09:30:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-26 08:44:34 +01:00
										 |  |  |         return $transaction; | 
					
						
							| 
									
										
										
										
											2015-04-03 09:30:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 08:44:07 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Preference $preference | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function getFrontpageAccounts(Preference $preference): Collection | 
					
						
							| 
									
										
										
										
											2015-03-13 08:44:07 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-03-20 16:47:53 +01:00
										 |  |  |         $query = $this->user->accounts()->accountTypeIn(['Default account', 'Asset account']); | 
					
						
							| 
									
										
										
										
											2015-06-03 18:22:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-26 19:07:02 +02:00
										 |  |  |         if (count($preference->data) > 0) { | 
					
						
							| 
									
										
										
										
											2015-07-26 19:10:31 +02:00
										 |  |  |             $query->whereIn('accounts.id', $preference->data); | 
					
						
							| 
									
										
										
										
											2015-03-13 08:44:07 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-26 19:07:02 +02:00
										 |  |  |         $result = $query->get(['accounts.*']); | 
					
						
							| 
									
										
										
										
											2016-01-15 13:13:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-26 19:07:02 +02:00
										 |  |  |         return $result; | 
					
						
							| 
									
										
										
										
											2015-03-13 08:44:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-03-20 22:39:07 +01:00
										 |  |  |      * This method is used on the front page where (in turn) its viewed journals-tiny.php which (in turn) | 
					
						
							|  |  |  |      * is almost the only place where formatJournal is used. Aka, we can use some custom querying to get some specific. | 
					
						
							|  |  |  |      * fields using left joins. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-03-13 08:44:07 +01:00
										 |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param Carbon  $start | 
					
						
							|  |  |  |      * @param Carbon  $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function getFrontpageTransactions(Account $account, Carbon $start, Carbon $end): Collection | 
					
						
							| 
									
										
										
										
											2015-03-13 08:44:07 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-03-20 16:47:53 +01:00
										 |  |  |         $set = $this->user | 
					
						
							|  |  |  |             ->transactionjournals() | 
					
						
							|  |  |  |             ->with(['transactions']) | 
					
						
							|  |  |  |             ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') | 
					
						
							|  |  |  |             ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')->where('accounts.id', $account->id) | 
					
						
							|  |  |  |             ->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transaction_journals.transaction_currency_id') | 
					
						
							|  |  |  |             ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id') | 
					
						
							|  |  |  |             ->before($end) | 
					
						
							|  |  |  |             ->after($start) | 
					
						
							|  |  |  |             ->orderBy('transaction_journals.date', 'DESC') | 
					
						
							|  |  |  |             ->orderBy('transaction_journals.order', 'ASC') | 
					
						
							|  |  |  |             ->orderBy('transaction_journals.id', 'DESC') | 
					
						
							|  |  |  |             ->take(10) | 
					
						
							|  |  |  |             ->get(['transaction_journals.*', 'transaction_currencies.symbol', 'transaction_types.type']); | 
					
						
							| 
									
										
										
										
											2016-01-15 13:13:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 18:22:47 +02:00
										 |  |  |         return $set; | 
					
						
							| 
									
										
										
										
											2015-03-13 08:44:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-21 12:16:41 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param int     $page | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-04-07 18:48:34 +02:00
										 |  |  |      * @return LengthAwarePaginator | 
					
						
							| 
									
										
										
										
											2015-02-21 12:16:41 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function getJournals(Account $account, $page): LengthAwarePaginator | 
					
						
							| 
									
										
										
										
											2015-02-21 12:16:41 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-03-02 11:42:27 +01:00
										 |  |  |         $offset = ($page - 1) * 50; | 
					
						
							| 
									
										
										
										
											2016-03-20 16:47:53 +01:00
										 |  |  |         $query  = $this->user | 
					
						
							|  |  |  |             ->transactionJournals() | 
					
						
							| 
									
										
										
										
											2016-03-21 08:48:36 +01:00
										 |  |  |             ->expanded() | 
					
						
							| 
									
										
										
										
											2016-03-20 16:47:53 +01:00
										 |  |  |             ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') | 
					
						
							|  |  |  |             ->where('transactions.account_id', $account->id) | 
					
						
							|  |  |  |             ->orderBy('transaction_journals.date', 'DESC') | 
					
						
							|  |  |  |             ->orderBy('transaction_journals.order', 'ASC') | 
					
						
							|  |  |  |             ->orderBy('transaction_journals.id', 'DESC'); | 
					
						
							| 
									
										
										
										
											2015-02-21 12:16:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-23 21:55:52 +01:00
										 |  |  |         $count     = $query->count(); | 
					
						
							| 
									
										
										
										
											2016-03-21 08:48:36 +01:00
										 |  |  |         $set       = $query->take(50)->offset($offset)->get(TransactionJournal::QUERYFIELDS); | 
					
						
							| 
									
										
										
										
											2015-02-23 21:55:52 +01:00
										 |  |  |         $paginator = new LengthAwarePaginator($set, $count, 50, $page); | 
					
						
							| 
									
										
										
										
											2015-02-22 08:38:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-21 12:16:41 +01:00
										 |  |  |         return $paginator; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the accounts of a user that have piggy banks connected to them. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function getPiggyBankAccounts(): Collection | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-30 09:30:06 +01:00
										 |  |  |         $start      = clone Session::get('start', new Carbon); | 
					
						
							|  |  |  |         $end        = clone Session::get('end', new Carbon); | 
					
						
							|  |  |  |         $collection = new Collection(DB::table('piggy_banks')->distinct()->get(['piggy_banks.account_id'])); | 
					
						
							|  |  |  |         $ids        = $collection->pluck('account_id')->toArray(); | 
					
						
							| 
									
										
										
										
											2016-01-01 12:41:00 +01:00
										 |  |  |         $accounts   = new Collection; | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $ids = array_unique($ids); | 
					
						
							|  |  |  |         if (count($ids) > 0) { | 
					
						
							| 
									
										
										
										
											2016-03-20 16:47:53 +01:00
										 |  |  |             $accounts = $this->user->accounts()->whereIn('id', $ids)->where('accounts.active', 1)->get(); | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-26 19:42:28 +02:00
										 |  |  |         bcscale(2); | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $accounts->each( | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |             function (Account $account) use ($start, $end) { | 
					
						
							| 
									
										
										
										
											2015-04-13 21:13:05 +02:00
										 |  |  |                 $account->startBalance = Steam::balance($account, $start, true); | 
					
						
							|  |  |  |                 $account->endBalance   = Steam::balance($account, $end, true); | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  |                 $account->piggyBalance = 0; | 
					
						
							|  |  |  |                 /** @var PiggyBank $piggyBank */ | 
					
						
							|  |  |  |                 foreach ($account->piggyBanks as $piggyBank) { | 
					
						
							|  |  |  |                     $account->piggyBalance += $piggyBank->currentRelevantRep()->currentamount; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 // sum of piggy bank amounts on this account:
 | 
					
						
							|  |  |  |                 // diff between endBalance and piggyBalance.
 | 
					
						
							|  |  |  |                 // then, percentage.
 | 
					
						
							| 
									
										
										
										
											2015-07-26 19:42:28 +02:00
										 |  |  |                 $difference          = bcsub($account->endBalance, $account->piggyBalance); | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  |                 $account->difference = $difference; | 
					
						
							| 
									
										
										
										
											2015-05-06 18:09:45 +02:00
										 |  |  |                 $account->percentage = $difference != 0 && $account->endBalance != 0 ? round((($difference / $account->endBalance) * 100)) : 100; | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $accounts; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-21 08:51:34 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get savings accounts and the balance difference in the period. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function getSavingsAccounts(): Collection | 
					
						
							| 
									
										
										
										
											2015-03-21 08:51:34 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-03-20 16:47:53 +01:00
										 |  |  |         $accounts = $this->user->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC') | 
					
						
							|  |  |  |                                ->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id') | 
					
						
							|  |  |  |                                ->where('account_meta.name', 'accountRole') | 
					
						
							|  |  |  |                                ->where('accounts.active', 1) | 
					
						
							|  |  |  |                                ->where('account_meta.data', '"savingAsset"') | 
					
						
							|  |  |  |                                ->get(['accounts.*']); | 
					
						
							| 
									
										
										
										
											2015-03-31 19:21:49 +02:00
										 |  |  |         $start    = clone Session::get('start', new Carbon); | 
					
						
							|  |  |  |         $end      = clone Session::get('end', new Carbon); | 
					
						
							| 
									
										
										
										
											2015-03-21 08:51:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-26 19:42:28 +02:00
										 |  |  |         bcscale(2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-21 08:51:34 +01:00
										 |  |  |         $accounts->each( | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |             function (Account $account) use ($start, $end) { | 
					
						
							| 
									
										
										
										
											2015-03-21 08:51:34 +01:00
										 |  |  |                 $account->startBalance = Steam::balance($account, $start); | 
					
						
							|  |  |  |                 $account->endBalance   = Steam::balance($account, $end); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // diff (negative when lost, positive when gained)
 | 
					
						
							| 
									
										
										
										
											2015-07-26 19:42:28 +02:00
										 |  |  |                 $diff = bcsub($account->endBalance, $account->startBalance); | 
					
						
							| 
									
										
										
										
											2015-03-21 08:51:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-21 08:55:55 +01:00
										 |  |  |                 if ($diff < 0 && $account->startBalance > 0) { | 
					
						
							| 
									
										
										
										
											2015-03-21 08:51:34 +01:00
										 |  |  |                     // percentage lost compared to start.
 | 
					
						
							|  |  |  |                     $pct = (($diff * -1) / $account->startBalance) * 100; | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2015-03-21 08:55:55 +01:00
										 |  |  |                     if ($diff >= 0 && $account->startBalance > 0) { | 
					
						
							|  |  |  |                         $pct = ($diff / $account->startBalance) * 100; | 
					
						
							|  |  |  |                     } else { | 
					
						
							| 
									
										
										
										
											2015-03-21 08:56:24 +01:00
										 |  |  |                         $pct = 100; | 
					
						
							| 
									
										
										
										
											2015-03-21 08:55:55 +01:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2015-03-21 08:51:34 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2015-03-21 08:55:55 +01:00
										 |  |  |                 $pct                 = $pct > 100 ? 100 : $pct; | 
					
						
							| 
									
										
										
										
											2015-03-21 08:51:34 +01:00
										 |  |  |                 $account->difference = $diff; | 
					
						
							|  |  |  |                 $account->percentage = round($pct); | 
					
						
							| 
									
										
										
										
											2015-05-07 20:56:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-21 08:51:34 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $accounts; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-02 11:42:27 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							| 
									
										
										
										
											2015-05-20 19:55:53 +02:00
										 |  |  |      * @param Carbon  $date | 
					
						
							| 
									
										
										
										
											2015-03-02 11:42:27 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return float | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function leftOnAccount(Account $account, Carbon $date): string | 
					
						
							| 
									
										
										
										
											2015-03-02 11:42:27 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-17 09:35:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $balance = Steam::balance($account, $date, true); | 
					
						
							| 
									
										
										
										
											2015-03-02 11:42:27 +01:00
										 |  |  |         /** @var PiggyBank $p */ | 
					
						
							|  |  |  |         foreach ($account->piggybanks()->get() as $p) { | 
					
						
							|  |  |  |             $balance -= $p->currentRelevantRep()->currentamount; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $balance; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return TransactionJournal|null | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function openingBalanceTransaction(Account $account): TransactionJournal | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-26 08:44:34 +01:00
										 |  |  |         $journal = TransactionJournal | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |             ::orderBy('transaction_journals.date', 'ASC') | 
					
						
							| 
									
										
										
										
											2016-01-15 22:41:26 +01:00
										 |  |  |             ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') | 
					
						
							|  |  |  |             ->where('transactions.account_id', $account->id) | 
					
						
							| 
									
										
										
										
											2015-12-09 22:39:50 -02:00
										 |  |  |             ->transactionTypes([TransactionType::OPENING_BALANCE]) | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |             ->orderBy('created_at', 'ASC') | 
					
						
							|  |  |  |             ->first(['transaction_journals.*']); | 
					
						
							| 
									
										
										
										
											2016-03-19 17:34:37 +01:00
										 |  |  |         if (is_null($journal)) { | 
					
						
							| 
									
										
										
										
											2016-03-19 17:34:02 +01:00
										 |  |  |             return new TransactionJournal; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-12-26 08:44:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $journal; | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param array $data | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-05-27 08:36:26 +02:00
										 |  |  |      * @return Account | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function store(array $data): Account | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-03-29 08:24:56 +02:00
										 |  |  |         $newAccount = $this->storeAccount($data); | 
					
						
							| 
									
										
										
										
											2015-07-06 18:48:17 +02:00
										 |  |  |         if (!is_null($newAccount)) { | 
					
						
							|  |  |  |             $this->storeMetadata($newAccount, $data); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // continue with the opposing account:
 | 
					
						
							|  |  |  |         if ($data['openingBalance'] != 0) { | 
					
						
							| 
									
										
										
										
											2015-02-09 07:56:24 +01:00
										 |  |  |             $opposingData = [ | 
					
						
							| 
									
										
										
										
											2015-06-02 17:58:30 +02:00
										 |  |  |                 'user'           => $data['user'], | 
					
						
							| 
									
										
										
										
											2015-08-15 21:45:29 +02:00
										 |  |  |                 'accountType'    => 'initial', | 
					
						
							| 
									
										
										
										
											2015-06-01 18:13:54 +02:00
										 |  |  |                 'virtualBalance' => 0, | 
					
						
							| 
									
										
										
										
											2015-06-02 17:58:30 +02:00
										 |  |  |                 'name'           => $data['name'] . ' initial balance', | 
					
						
							|  |  |  |                 'active'         => false, | 
					
						
							| 
									
										
										
										
											2015-07-03 12:51:14 +02:00
										 |  |  |                 'iban'           => '', | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |             $opposing     = $this->storeAccount($opposingData); | 
					
						
							| 
									
										
										
										
											2015-07-06 18:57:15 +02:00
										 |  |  |             if (!is_null($opposing) && !is_null($newAccount)) { | 
					
						
							| 
									
										
										
										
											2015-07-06 18:13:57 +02:00
										 |  |  |                 $this->storeInitialBalance($newAccount, $opposing, $data); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-02-09 07:56:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $newAccount; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $account | 
					
						
							|  |  |  |      * @param $name | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return AccountMeta | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function storeMeta($account, $name, $value): AccountMeta | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return AccountMeta::create(['name' => $name, 'data' => $value, 'account_id' => $account->id,]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-07-26 19:07:02 +02:00
										 |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function sumOfEverything(): string | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-03-20 16:47:53 +01:00
										 |  |  |         return strval($this->user->transactions()->sum('amount')); | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							| 
									
										
										
										
											2015-05-05 10:23:01 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-15 19:37:09 +01:00
										 |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) // need the complexity.
 | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-05-05 10:23:01 +02:00
										 |  |  |      * @return Account | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     public function update(Account $account, array $data): Account | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         // update the account:
 | 
					
						
							| 
									
										
										
										
											2015-04-01 19:42:14 +02:00
										 |  |  |         $account->name            = $data['name']; | 
					
						
							|  |  |  |         $account->active          = $data['active'] == '1' ? true : false; | 
					
						
							|  |  |  |         $account->virtual_balance = $data['virtualBalance']; | 
					
						
							| 
									
										
										
										
											2015-07-03 12:51:14 +02:00
										 |  |  |         $account->iban            = $data['iban']; | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |         $account->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-29 08:24:56 +02:00
										 |  |  |         $this->updateMetadata($account, $data); | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |         $openingBalance = $this->openingBalanceTransaction($account); | 
					
						
							|  |  |  |         if ($data['openingBalance'] != 0) { | 
					
						
							| 
									
										
										
										
											2016-03-30 17:47:13 +02:00
										 |  |  |             if (!is_null($openingBalance->id)) { | 
					
						
							| 
									
										
										
										
											2015-03-29 08:24:56 +02:00
										 |  |  |                 $this->updateInitialBalance($account, $openingBalance, $data); | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 $type         = $data['openingBalance'] < 0 ? 'expense' : 'revenue'; | 
					
						
							|  |  |  |                 $opposingData = [ | 
					
						
							| 
									
										
										
										
											2015-06-02 17:58:30 +02:00
										 |  |  |                     'user'           => $data['user'], | 
					
						
							|  |  |  |                     'accountType'    => $type, | 
					
						
							|  |  |  |                     'name'           => $data['name'] . ' initial balance', | 
					
						
							|  |  |  |                     'active'         => false, | 
					
						
							| 
									
										
										
										
											2015-07-09 14:04:01 +02:00
										 |  |  |                     'iban'           => '', | 
					
						
							| 
									
										
										
										
											2015-06-02 17:58:30 +02:00
										 |  |  |                     'virtualBalance' => 0, | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |                 ]; | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |                 $opposing     = $this->storeAccount($opposingData); | 
					
						
							| 
									
										
										
										
											2015-07-06 18:48:17 +02:00
										 |  |  |                 if (!is_null($opposing)) { | 
					
						
							|  |  |  |                     $this->storeInitialBalance($account, $opposing, $data); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2015-05-26 08:17:58 +02:00
										 |  |  |             if ($openingBalance) { // opening balance is zero, should we delete it?
 | 
					
						
							| 
									
										
										
										
											2015-05-26 18:57:31 +00:00
										 |  |  |                 $openingBalance->delete(); // delete existing opening balance.
 | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $account; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param array $data | 
					
						
							| 
									
										
										
										
											2015-02-09 07:56:24 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return Account | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     protected function storeAccount(array $data): Account | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-02-09 07:56:24 +01:00
										 |  |  |         $type        = Config::get('firefly.accountTypeByIdentifier.' . $data['accountType']); | 
					
						
							|  |  |  |         $accountType = AccountType::whereType($type)->first(); | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |         $newAccount  = new Account( | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 'user_id'         => $data['user'], | 
					
						
							|  |  |  |                 'account_type_id' => $accountType->id, | 
					
						
							|  |  |  |                 'name'            => $data['name'], | 
					
						
							| 
									
										
										
										
											2015-06-01 18:13:54 +02:00
										 |  |  |                 'virtual_balance' => $data['virtualBalance'], | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |                 'active'          => $data['active'] === true ? true : false, | 
					
						
							| 
									
										
										
										
											2015-07-03 12:51:14 +02:00
										 |  |  |                 'iban'            => $data['iban'], | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |             ] | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2015-03-26 18:05:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |         if (!$newAccount->isValid()) { | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |             // does the account already exist?
 | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |             $searchData      = [ | 
					
						
							| 
									
										
										
										
											2015-05-08 07:27:29 +02:00
										 |  |  |                 'user_id'         => $data['user'], | 
					
						
							|  |  |  |                 'account_type_id' => $accountType->id, | 
					
						
							| 
									
										
										
										
											2015-06-01 18:13:54 +02:00
										 |  |  |                 'virtual_balance' => $data['virtualBalance'], | 
					
						
							| 
									
										
										
										
											2015-07-03 12:51:14 +02:00
										 |  |  |                 'name'            => $data['name'], | 
					
						
							|  |  |  |                 'iban'            => $data['iban'], | 
					
						
							| 
									
										
										
										
											2015-05-08 07:27:29 +02:00
										 |  |  |             ]; | 
					
						
							|  |  |  |             $existingAccount = Account::firstOrNullEncrypted($searchData); | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |             if (!$existingAccount) { | 
					
						
							|  |  |  |                 Log::error('Account create error: ' . $newAccount->getErrors()->toJson()); | 
					
						
							| 
									
										
										
										
											2015-07-07 19:09:45 +02:00
										 |  |  |                 abort(500); | 
					
						
							| 
									
										
										
										
											2015-05-08 07:39:05 +02:00
										 |  |  |                 // @codeCoverageIgnoreStart
 | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-05-08 07:39:05 +02:00
										 |  |  |             // @codeCoverageIgnoreEnd
 | 
					
						
							| 
									
										
										
										
											2015-02-14 14:25:29 +01:00
										 |  |  |             $newAccount = $existingAccount; | 
					
						
							| 
									
										
										
										
											2015-05-08 07:39:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         $newAccount->save(); | 
					
						
							| 
									
										
										
										
											2015-02-09 07:56:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $newAccount; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param Account $opposing | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return TransactionJournal | 
					
						
							| 
									
										
										
										
											2015-02-09 07:56:24 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     protected function storeInitialBalance(Account $account, Account $opposing, array $data): TransactionJournal | 
					
						
							| 
									
										
										
										
											2015-02-09 07:56:24 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-09 22:39:50 -02:00
										 |  |  |         $transactionType = TransactionType::whereType(TransactionType::OPENING_BALANCE)->first(); | 
					
						
							| 
									
										
										
										
											2015-07-26 17:03:05 +02:00
										 |  |  |         $journal         = TransactionJournal::create( | 
					
						
							| 
									
										
										
										
											2015-02-09 07:56:24 +01:00
										 |  |  |             [ | 
					
						
							|  |  |  |                 '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'], | 
					
						
							| 
									
										
										
										
											2016-01-15 19:37:09 +01:00
										 |  |  |                 'encrypted'               => true, | 
					
						
							| 
									
										
										
										
											2015-02-09 07:56:24 +01:00
										 |  |  |             ] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-26 17:03:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 22:23:34 +02:00
										 |  |  |         $one = new Transaction(['account_id' => $firstAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $firstAmount]); | 
					
						
							|  |  |  |         $one->save();// first transaction: from
 | 
					
						
							| 
									
										
										
										
											2015-02-09 07:56:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 22:23:34 +02:00
										 |  |  |         $two = new Transaction(['account_id' => $secondAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $secondAmount]); | 
					
						
							|  |  |  |         $two->save(); // second transaction: to
 | 
					
						
							| 
									
										
										
										
											2015-02-09 07:56:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $journal; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-09 07:23:39 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							| 
									
										
										
										
											2015-03-02 11:42:27 +01:00
										 |  |  |      * @param array   $data | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     protected function storeMetadata(Account $account, array $data) | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-03-30 17:47:13 +02:00
										 |  |  |         foreach ($this->validFields as $field) { | 
					
						
							| 
									
										
										
										
											2016-01-15 19:37:09 +01:00
										 |  |  |             if (isset($data[$field])) { | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |                 $metaData = new AccountMeta( | 
					
						
							|  |  |  |                     [ | 
					
						
							|  |  |  |                         'account_id' => $account->id, | 
					
						
							|  |  |  |                         'name'       => $field, | 
					
						
							|  |  |  |                         'data'       => $data[$field], | 
					
						
							|  |  |  |                     ] | 
					
						
							|  |  |  |                 ); | 
					
						
							|  |  |  |                 $metaData->save(); | 
					
						
							| 
									
										
										
										
											2015-03-02 11:42:27 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-02 20:05:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account            $account | 
					
						
							|  |  |  |      * @param TransactionJournal $journal | 
					
						
							|  |  |  |      * @param array              $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return TransactionJournal | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  |     protected function updateInitialBalance(Account $account, TransactionJournal $journal, array $data): TransactionJournal | 
					
						
							| 
									
										
										
										
											2015-03-02 20:05:28 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $journal->date = $data['openingBalanceDate']; | 
					
						
							| 
									
										
										
										
											2016-01-22 15:44:23 +01:00
										 |  |  |         $journal->save(); | 
					
						
							| 
									
										
										
										
											2016-03-19 17:28:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-02 20:05:28 +01:00
										 |  |  |         /** @var Transaction $transaction */ | 
					
						
							|  |  |  |         foreach ($journal->transactions()->get() as $transaction) { | 
					
						
							|  |  |  |             if ($account->id == $transaction->account_id) { | 
					
						
							|  |  |  |                 $transaction->amount = $data['openingBalance']; | 
					
						
							|  |  |  |                 $transaction->save(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if ($account->id != $transaction->account_id) { | 
					
						
							|  |  |  |                 $transaction->amount = $data['openingBalance'] * -1; | 
					
						
							|  |  |  |                 $transaction->save(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $journal; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-04 06:56:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							| 
									
										
										
										
											2015-12-04 06:56:45 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     protected function updateMetadata(Account $account, array $data) | 
					
						
							| 
									
										
										
										
											2015-12-04 06:56:45 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-03-30 17:47:13 +02:00
										 |  |  |         foreach ($this->validFields as $field) { | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |             $entry = $account->accountMeta()->where('name', $field)->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (isset($data[$field])) { | 
					
						
							|  |  |  |                 // update if new data is present:
 | 
					
						
							|  |  |  |                 if (!is_null($entry)) { | 
					
						
							|  |  |  |                     $entry->data = $data[$field]; | 
					
						
							|  |  |  |                     $entry->save(); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $metaData = new AccountMeta( | 
					
						
							|  |  |  |                         [ | 
					
						
							|  |  |  |                             'account_id' => $account->id, | 
					
						
							|  |  |  |                             'name'       => $field, | 
					
						
							|  |  |  |                             'data'       => $data[$field], | 
					
						
							|  |  |  |                         ] | 
					
						
							|  |  |  |                     ); | 
					
						
							|  |  |  |                     $metaData->save(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-04 06:56:45 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-29 08:14:32 +02:00
										 |  |  | } |