| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 22:11:51 +01:00
										 |  |  | namespace FireflyIII\Database\Account; | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2015-01-02 06:05:40 +01:00
										 |  |  | use FireflyIII\Database\CommonDatabaseCallsInterface; | 
					
						
							|  |  |  | use FireflyIII\Database\CUDInterface; | 
					
						
							| 
									
										
										
										
											2014-12-13 22:11:51 +01:00
										 |  |  | use FireflyIII\Database\SwitchUser; | 
					
						
							| 
									
										
										
										
											2015-01-16 07:07:57 +01:00
										 |  |  | use FireflyIII\Exception\NotImplementedException; | 
					
						
							| 
									
										
										
										
											2015-01-13 20:43:54 +01:00
										 |  |  | use Illuminate\Database\Eloquent\Builder as EloquentBuilder; | 
					
						
							| 
									
										
										
										
											2014-12-20 15:00:53 +01:00
										 |  |  | use Illuminate\Database\Eloquent\Model as Eloquent; | 
					
						
							| 
									
										
										
										
											2015-01-13 20:43:54 +01:00
										 |  |  | use Illuminate\Database\Query\Builder as QueryBuilder; | 
					
						
							| 
									
										
										
										
											2014-11-12 21:19:31 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2015-01-16 07:07:57 +01:00
										 |  |  | use Illuminate\Support\MessageBag; | 
					
						
							| 
									
										
										
										
											2015-01-13 20:43:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Class Account | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-12-20 07:33:59 +01:00
										 |  |  |  * @package    FireflyIII\Database | 
					
						
							| 
									
										
										
										
											2014-12-19 21:36:53 +01:00
										 |  |  |  * @implements FireflyIII\Database\Account\AccountInterface | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-01-02 06:05:40 +01:00
										 |  |  | class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInterface | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     use SwitchUser; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 10:30:52 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setUser(\Auth::user()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-02 14:58:12 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * @param array $types | 
					
						
							| 
									
										
										
										
											2014-11-02 14:58:12 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * @return int | 
					
						
							| 
									
										
										
										
											2014-11-02 14:58:12 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |     public function countAccountsByType(array $types) | 
					
						
							| 
									
										
										
										
											2014-11-02 14:58:12 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |         return $this->getUser()->accounts()->accountTypeIn($types)->count(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-11-02 14:58:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param array $types | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-11-22 23:30:45 +01:00
										 |  |  |     public function getAccountsByType(array $types) | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         /* | 
					
						
							|  |  |  |          * Basic query: | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2015-01-16 07:07:57 +01:00
										 |  |  |         $query = $this->getUser()->accounts()->accountTypeIn($types)->withMeta()->orderBy('name', 'ASC');; | 
					
						
							|  |  |  |         $set = $query->get(['accounts.*']); | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 15:37:53 +01:00
										 |  |  |         $set->each( | 
					
						
							|  |  |  |             function (\Account $account) { | 
					
						
							| 
									
										
										
										
											2015-01-16 07:07:57 +01:00
										 |  |  |                 /* | 
					
						
							|  |  |  |                  * Get last activity date. | 
					
						
							|  |  |  |                  */ | 
					
						
							| 
									
										
										
										
											2014-12-07 15:37:53 +01:00
										 |  |  |                 $account->lastActivityDate = $this->getLastActivity($account); | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 15:37:53 +01:00
										 |  |  |         return $set; | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * @param \Account $account | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * @return \TransactionJournal|null | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |     public function openingBalanceTransaction(\Account $account) | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |         return \TransactionJournal::withRelevantData()->accountIs($account)->leftJoin( | 
					
						
							| 
									
										
										
										
											2014-11-13 17:01:09 +01:00
										 |  |  |             'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id' | 
					
						
							|  |  |  |         )->where('transaction_types.type', 'Opening balance')->first(['transaction_journals.*']); | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * @param \Account $account | 
					
						
							|  |  |  |      * @param array    $data | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |     public function storeInitialBalance(\Account $account, array $data) | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |         $opposingData    = ['name' => $account->name . ' Initial Balance', 'active' => 0, 'what' => 'initial']; | 
					
						
							|  |  |  |         $opposingAccount = $this->store($opposingData); | 
					
						
							| 
									
										
										
										
											2015-01-17 08:58:49 +01:00
										 |  |  |         $balance         = floatval($data['openingbalance']); | 
					
						
							|  |  |  |         $date            = new Carbon($data['openingbalancedate']); | 
					
						
							|  |  |  |         /** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $journals */ | 
					
						
							|  |  |  |         $journals    = \App::make('FireflyIII\Database\TransactionJournal\TransactionJournal'); | 
					
						
							|  |  |  |         $fromAccount = $opposingAccount; | 
					
						
							|  |  |  |         $toAccount   = $account; | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |         if ($balance < 0) { | 
					
						
							| 
									
										
										
										
											2015-01-17 08:58:49 +01:00
										 |  |  |             $fromAccount = $account; | 
					
						
							|  |  |  |             $toAccount   = $opposingAccount; | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         // data for transaction journal:
 | 
					
						
							|  |  |  |         $balance = $balance < 0 ? $balance * -1 : $balance; | 
					
						
							| 
									
										
										
										
											2015-01-13 20:43:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var \FireflyIII\Database\TransactionType\TransactionType $typeRepository */ | 
					
						
							|  |  |  |         $typeRepository = \App::make('FireflyIII\Database\TransactionType\TransactionType'); | 
					
						
							|  |  |  |         $type           = $typeRepository->findByWhat('opening'); | 
					
						
							| 
									
										
										
										
											2015-01-17 08:58:49 +01:00
										 |  |  |         $currency       = \Amount::getDefaultCurrency(); | 
					
						
							| 
									
										
										
										
											2015-01-13 20:43:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-17 08:58:49 +01:00
										 |  |  |         $opening = ['transaction_type_id' => $type->id, 'transaction_currency_id' => $currency->id, 'amount' => $balance, 'from' => $fromAccount, | 
					
						
							|  |  |  |                     'completed'           => 0, 'currency' => 'EUR', 'what' => 'opening', 'to' => $toAccount, 'date' => $date, | 
					
						
							|  |  |  |                     'description'         => 'Opening balance for new account ' . $account->name,]; | 
					
						
							| 
									
										
										
										
											2014-11-12 21:19:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-17 08:58:49 +01:00
										 |  |  |         $validation = $journals->validate($opening); | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |         if ($validation['errors']->count() == 0) { | 
					
						
							| 
									
										
										
										
											2015-01-17 08:58:49 +01:00
										 |  |  |             $journals->store($opening); | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2015-01-13 20:43:54 +01:00
										 |  |  |             \Log::error('Initial balance created is not valid (Database/Account)'); | 
					
						
							| 
									
										
										
										
											2015-01-01 23:12:12 +01:00
										 |  |  |             \Log::error($validation['errors']->all()); | 
					
						
							|  |  |  |             \App::abort(500); | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-17 08:58:49 +01:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-17 07:25:44 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param \Account $account | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getLastActivity(\Account $account) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $lastActivityKey = 'account.' . $account->id . '.lastActivityDate'; | 
					
						
							|  |  |  |         if (\Cache::has($lastActivityKey)) { | 
					
						
							|  |  |  |             return \Cache::get($lastActivityKey); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $transaction = $account->transactions() | 
					
						
							|  |  |  |                                ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') | 
					
						
							|  |  |  |                                ->orderBy('transaction_journals.date', 'DESC')->first(); | 
					
						
							|  |  |  |         if ($transaction) { | 
					
						
							|  |  |  |             $date = $transaction->transactionJournal->date; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $date = 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         \Cache::forever($lastActivityKey, $date); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $date; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-01-18 21:07:40 +01:00
										 |  |  |      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) // cannot make it shorter because of query.
 | 
					
						
							| 
									
										
										
										
											2014-12-20 15:00:53 +01:00
										 |  |  |      * @param Eloquent $model | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-12-20 15:00:53 +01:00
										 |  |  |     public function destroy(Eloquent $model) | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-01-18 09:49:32 +01:00
										 |  |  |         $journals     = \TransactionJournal::whereIn( | 
					
						
							| 
									
										
										
										
											2015-01-02 09:06:44 +01:00
										 |  |  |             'id', function (QueryBuilder $query) use ($model) { | 
					
						
							| 
									
										
										
										
											2014-12-15 19:39:01 +01:00
										 |  |  |             $query->select('transaction_journal_id') | 
					
						
							| 
									
										
										
										
											2015-01-18 21:07:40 +01:00
										 |  |  |                   ->from('transactions') | 
					
						
							|  |  |  |                   ->whereIn( | 
					
						
							|  |  |  |                       'account_id', function (QueryBuilder $query) use ($model) { | 
					
						
							|  |  |  |                       $query | 
					
						
							| 
									
										
										
										
											2015-01-24 06:56:02 +01:00
										 |  |  |                           ->select('accounts.id') | 
					
						
							|  |  |  |                           ->from('accounts') | 
					
						
							|  |  |  |                           ->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') | 
					
						
							| 
									
										
										
										
											2015-01-18 21:07:40 +01:00
										 |  |  |                           ->where( | 
					
						
							|  |  |  |                               function (QueryBuilder $q) use ($model) { | 
					
						
							|  |  |  |                                   $q->where('id', $model->id); | 
					
						
							|  |  |  |                                   $q->orWhere( | 
					
						
							|  |  |  |                                       function (QueryBuilder $q) use ($model) { | 
					
						
							|  |  |  |                                           $q->where('accounts.name', 'LIKE', '%' . $model->name . '%'); | 
					
						
							| 
									
										
										
										
											2015-01-24 06:56:02 +01:00
										 |  |  |                                           $q->where('account_types.type', 'Initial balance account'); | 
					
						
							| 
									
										
										
										
											2015-01-18 21:07:40 +01:00
										 |  |  |                                           $q->where('accounts.active', 0); | 
					
						
							|  |  |  |                                       } | 
					
						
							|  |  |  |                                   ); | 
					
						
							|  |  |  |                               } | 
					
						
							|  |  |  |                           )->where('accounts.user_id', $this->getUser()->id); | 
					
						
							|  |  |  |                   } | 
					
						
							|  |  |  |                   )->get(); | 
					
						
							| 
									
										
										
										
											2014-12-15 19:39:01 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-12-18 19:51:08 +01:00
										 |  |  |         )->get(); | 
					
						
							|  |  |  |         $transactions = []; | 
					
						
							|  |  |  |         /** @var \TransactionJournal $journal */ | 
					
						
							|  |  |  |         foreach ($journals as $journal) { | 
					
						
							|  |  |  |             /** @var \Transaction $t */ | 
					
						
							|  |  |  |             foreach ($journal->transactions as $t) { | 
					
						
							|  |  |  |                 $transactions[] = intval($t->id); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $journal->delete(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-12-20 07:33:59 +01:00
										 |  |  |         if (count($transactions) > 0) { | 
					
						
							|  |  |  |             \Transaction::whereIn('id', $transactions)->delete(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-11-27 16:20:16 +01:00
										 |  |  |         \Event::fire('account.destroy', [$model]); | 
					
						
							| 
									
										
										
										
											2015-01-24 06:56:02 +01:00
										 |  |  |         \Account:: | 
					
						
							|  |  |  |             leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') | 
					
						
							|  |  |  |             ->where( | 
					
						
							| 
									
										
										
										
											2015-01-02 09:06:44 +01:00
										 |  |  |             function (EloquentBuilder $q) use ($model) { | 
					
						
							| 
									
										
										
										
											2014-12-06 12:12:55 +01:00
										 |  |  |                 $q->where('id', $model->id); | 
					
						
							|  |  |  |                 $q->orWhere( | 
					
						
							| 
									
										
										
										
											2015-01-02 09:06:44 +01:00
										 |  |  |                     function (EloquentBuilder $q) use ($model) { | 
					
						
							| 
									
										
										
										
											2014-12-06 12:12:55 +01:00
										 |  |  |                         $q->where('accounts.name', 'LIKE', '%' . $model->name . '%'); | 
					
						
							| 
									
										
										
										
											2015-01-24 06:56:02 +01:00
										 |  |  |                         $q->where('account_types.type', 'Initial balance account'); | 
					
						
							| 
									
										
										
										
											2014-12-06 12:12:55 +01:00
										 |  |  |                         $q->where('accounts.active', 0); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ); | 
					
						
							| 
									
										
										
										
											2014-12-07 15:37:53 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         )->delete(); | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * @param array $data | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-12-06 12:12:55 +01:00
										 |  |  |      * @return \Eloquent | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |     public function store(array $data) | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 22:11:51 +01:00
										 |  |  |         /** @var \FireflyIII\Database\AccountType\AccountType $acctType */ | 
					
						
							|  |  |  |         $acctType = \App::make('FireflyIII\Database\AccountType\AccountType'); | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $accountType = $acctType->findByWhat($data['what']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $data['user_id']         = $this->getUser()->id; | 
					
						
							|  |  |  |         $data['account_type_id'] = $accountType->id; | 
					
						
							|  |  |  |         $data['active']          = isset($data['active']) && $data['active'] === '1' ? 1 : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $data    = array_except($data, ['_token', 'what']); | 
					
						
							|  |  |  |         $account = new \Account($data); | 
					
						
							| 
									
										
										
										
											2014-12-06 12:12:55 +01:00
										 |  |  |         if (!$account->isValid()) { | 
					
						
							| 
									
										
										
										
											2015-01-13 20:43:54 +01:00
										 |  |  |             \Log::error('Account created is not valid (Database/Account)'); | 
					
						
							| 
									
										
										
										
											2015-01-01 23:12:12 +01:00
										 |  |  |             \Log::error($account->getErrors()->all()); | 
					
						
							|  |  |  |             \App::abort(500); | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         $account->save(); | 
					
						
							| 
									
										
										
										
											2015-01-16 07:07:57 +01:00
										 |  |  |         if (isset($data['openingbalance']) && floatval($data['openingbalance']) != 0) { | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |             $this->storeInitialBalance($account, $data); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-05 21:44:01 +01:00
										 |  |  |         switch ($account->accountType->type) { | 
					
						
							|  |  |  |             case 'Asset account': | 
					
						
							|  |  |  |             case 'Default account': | 
					
						
							|  |  |  |                 $account->updateMeta('accountRole', $data['account_role']); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /* Tell transaction journal to store a new one.*/ | 
					
						
							| 
									
										
										
										
											2014-11-27 16:20:16 +01:00
										 |  |  |         \Event::fire('account.store', [$account]); | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $account; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-12-20 15:00:53 +01:00
										 |  |  |      * @param Eloquent $model | 
					
						
							|  |  |  |      * @param array    $data | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-12-20 15:00:53 +01:00
										 |  |  |     public function update(Eloquent $model, array $data) | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |         $model->name   = $data['name']; | 
					
						
							|  |  |  |         $model->active = isset($data['active']) ? intval($data['active']) : 0; | 
					
						
							| 
									
										
										
										
											2014-12-05 21:39:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         switch ($model->accountType->type) { | 
					
						
							|  |  |  |             case 'Asset account': | 
					
						
							|  |  |  |             case 'Default account': | 
					
						
							|  |  |  |                 $model->updateMeta('accountRole', $data['account_role']); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |         $model->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-27 16:20:16 +01:00
										 |  |  |         if (isset($data['openingbalance']) && isset($data['openingbalancedate']) && strlen($data['openingbalancedate']) > 0) { | 
					
						
							| 
									
										
										
										
											2015-01-01 22:32:25 +01:00
										 |  |  |             /** @noinspection PhpParamsInspection */ | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |             $openingBalance = $this->openingBalanceTransaction($model); | 
					
						
							| 
									
										
										
										
											2014-12-05 21:39:26 +01:00
										 |  |  |             if (is_null($openingBalance)) { | 
					
						
							|  |  |  |                 $this->storeInitialBalance($model, $data); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $openingBalance->date = new Carbon($data['openingbalancedate']); | 
					
						
							|  |  |  |                 $openingBalance->save(); | 
					
						
							|  |  |  |                 $amount = floatval($data['openingbalance']); | 
					
						
							|  |  |  |                 /** @var \Transaction $transaction */ | 
					
						
							|  |  |  |                 foreach ($openingBalance->transactions as $transaction) { | 
					
						
							|  |  |  |                     if ($transaction->account_id == $model->id) { | 
					
						
							|  |  |  |                         $transaction->amount = $amount; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         $transaction->amount = $amount * -1; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     $transaction->save(); | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-11-27 16:20:16 +01:00
										 |  |  |         \Event::fire('account.update', [$model]); | 
					
						
							| 
									
										
										
										
											2014-12-05 21:39:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-01-16 07:07:57 +01:00
										 |  |  |      * Validates a model. Returns an array containing MessageBags | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |      * errors/warnings/successes. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array $model | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function validate(array $model) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-01-16 07:07:57 +01:00
										 |  |  |         $warnings  = new MessageBag; | 
					
						
							|  |  |  |         $successes = new MessageBag; | 
					
						
							|  |  |  |         $errors    = new MessageBag; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* | 
					
						
							|  |  |  |          * Name validation: | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         if (!isset($model['name'])) { | 
					
						
							|  |  |  |             $errors->add('name', 'Name is mandatory'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($model['name']) && strlen($model['name']) == 0) { | 
					
						
							|  |  |  |             $errors->add('name', 'Name is too short'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (isset($model['name']) && strlen($model['name']) > 100) { | 
					
						
							|  |  |  |             $errors->add('name', 'Name is too long'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $validator = \Validator::make([$model], \Account::$rules); | 
					
						
							|  |  |  |         if ($validator->invalid()) { | 
					
						
							|  |  |  |             $errors->merge($errors); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($model['account_role']) && !in_array($model['account_role'], array_keys(\Config::get('firefly.accountRoles')))) { | 
					
						
							|  |  |  |             $errors->add('account_role', 'Invalid account role'); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $successes->add('account_role', 'OK'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* | 
					
						
							|  |  |  |          * type validation. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         if (!isset($model['what'])) { | 
					
						
							|  |  |  |             $errors->add('name', 'Internal error: need to know type of account!'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* | 
					
						
							|  |  |  |          * Opening balance and opening balance date. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         if (isset($model['what']) && $model['what'] == 'asset') { | 
					
						
							|  |  |  |             if (isset($model['openingbalance']) && strlen($model['openingbalance']) > 0 && !is_numeric($model['openingbalance'])) { | 
					
						
							|  |  |  |                 $errors->add('openingbalance', 'This is not a number.'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (isset($model['openingbalancedate']) && strlen($model['openingbalancedate']) > 0) { | 
					
						
							|  |  |  |                 try { | 
					
						
							|  |  |  |                     new Carbon($model['openingbalancedate']); | 
					
						
							|  |  |  |                 } catch (\Exception $e) { | 
					
						
							|  |  |  |                     $errors->add('openingbalancedate', 'This date is invalid.'); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$errors->has('name')) { | 
					
						
							|  |  |  |             $successes->add('name', 'OK'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!$errors->has('openingbalance')) { | 
					
						
							|  |  |  |             $successes->add('openingbalance', 'OK'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!$errors->has('openingbalancedate')) { | 
					
						
							|  |  |  |             $successes->add('openingbalancedate', 'OK'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes]; | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 22:21:48 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * Returns an object with id $id. | 
					
						
							| 
									
										
										
										
											2014-11-12 22:21:48 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-12-19 21:18:42 +01:00
										 |  |  |      * @param int $objectId | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-12-06 12:12:55 +01:00
										 |  |  |      * @return \Eloquent | 
					
						
							| 
									
										
										
										
											2014-11-12 22:21:48 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-12-19 21:18:42 +01:00
										 |  |  |     public function find($objectId) | 
					
						
							| 
									
										
										
										
											2014-11-12 22:21:48 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-12-19 21:18:42 +01:00
										 |  |  |         return $this->getUser()->accounts()->find($objectId); | 
					
						
							| 
									
										
										
										
											2014-11-12 22:21:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-01-17 10:06:12 +01:00
										 |  |  |      * @SuppressWarnings(PHPMD.UnusedFormalParameter) | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * @param $what | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-01-16 07:07:57 +01:00
										 |  |  |      * @throws NotImplementedException | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * @return \AccountType|null | 
					
						
							| 
									
										
										
										
											2014-11-12 22:21:48 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |     public function findByWhat($what) | 
					
						
							| 
									
										
										
										
											2014-11-12 22:21:48 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-01-16 07:07:57 +01:00
										 |  |  |         throw new NotImplementedException; | 
					
						
							| 
									
										
										
										
											2014-11-12 22:21:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns all objects. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							| 
									
										
										
										
											2015-01-16 07:07:57 +01:00
										 |  |  |      * @throws NotImplementedException | 
					
						
							| 
									
										
										
										
											2014-11-12 22:21:48 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function get() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-01-16 07:07:57 +01:00
										 |  |  |         throw new NotImplementedException; | 
					
						
							| 
									
										
										
										
											2014-11-12 22:21:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * @param array $ids | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |      * @return Collection | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |     public function getByIds(array $ids) | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |         return $this->getUser()->accounts()->whereIn('id', $ids)->get(); | 
					
						
							| 
									
										
										
										
											2014-10-28 05:58:48 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-10-29 10:30:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 21:59:02 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $name | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return static | 
					
						
							|  |  |  |      * @throws \FireflyIII\Exception\FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |     public function firstExpenseAccountOrCreate($name) | 
					
						
							| 
									
										
										
										
											2014-10-29 10:30:52 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-01-17 10:06:12 +01:00
										 |  |  |         /** @var \FireflyIII\Database\AccountType\AccountType $typeRepository */ | 
					
						
							|  |  |  |         $typeRepository = \App::make('FireflyIII\Database\AccountType\AccountType'); | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-17 10:06:12 +01:00
										 |  |  |         $accountType = $typeRepository->findByWhat('expense'); | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 19:33:50 +01:00
										 |  |  |         // if name is "", find cash account:
 | 
					
						
							|  |  |  |         if (strlen($name) == 0) { | 
					
						
							| 
									
										
										
										
											2015-01-17 10:06:12 +01:00
										 |  |  |             $cashAccountType = $typeRepository->findByWhat('cash'); | 
					
						
							| 
									
										
										
										
											2014-11-14 19:33:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // find or create cash account:
 | 
					
						
							|  |  |  |             return \Account::firstOrCreate( | 
					
						
							| 
									
										
										
										
											2014-12-18 19:51:08 +01:00
										 |  |  |                 ['name' => 'Cash account', 'account_type_id' => $cashAccountType->id, 'active' => 0, 'user_id' => $this->getUser()->id,] | 
					
						
							| 
									
										
										
										
											2014-11-14 19:33:50 +01:00
										 |  |  |             ); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:09 +01:00
										 |  |  |         $data = ['user_id' => $this->getUser()->id, 'account_type_id' => $accountType->id, 'name' => $name, 'active' => 1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return \Account::firstOrCreate($data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 10:30:52 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-11-13 17:01:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 21:59:02 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $name | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return static | 
					
						
							|  |  |  |      * @throws \FireflyIII\Exception\FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-11-13 17:01:09 +01:00
										 |  |  |     public function firstRevenueAccountOrCreate($name) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-01-17 10:06:12 +01:00
										 |  |  |         /** @var \FireflyIII\Database\AccountType\AccountType $typeRepository */ | 
					
						
							|  |  |  |         $typeRepository = \App::make('FireflyIII\Database\AccountType\AccountType'); | 
					
						
							| 
									
										
										
										
											2014-11-13 17:01:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-17 10:06:12 +01:00
										 |  |  |         $accountType = $typeRepository->findByWhat('revenue'); | 
					
						
							| 
									
										
										
										
											2014-11-13 17:01:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-23 06:45:20 +01:00
										 |  |  |         // if name is "", find cash account:
 | 
					
						
							|  |  |  |         if (strlen($name) == 0) { | 
					
						
							|  |  |  |             $cashAccountType = $typeRepository->findByWhat('cash'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // find or create cash account:
 | 
					
						
							|  |  |  |             return \Account::firstOrCreate( | 
					
						
							|  |  |  |                 ['name' => 'Cash account', 'account_type_id' => $cashAccountType->id, 'active' => 0, 'user_id' => $this->getUser()->id,] | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 17:01:09 +01:00
										 |  |  |         $data = ['user_id' => $this->getUser()->id, 'account_type_id' => $accountType->id, 'name' => $name, 'active' => 1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return \Account::firstOrCreate($data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 21:59:02 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param \Account $account | 
					
						
							|  |  |  |      * @param int      $limit | 
					
						
							|  |  |  |      * @param string   $range | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return \Illuminate\Pagination\Paginator | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-12-07 15:37:53 +01:00
										 |  |  |     public function getTransactionJournals(\Account $account, $limit = 50, $range = 'session') | 
					
						
							| 
									
										
										
										
											2014-11-14 09:34:53 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $offset = intval(\Input::get('page')) > 0 ? intval(\Input::get('page')) * $limit : 0; | 
					
						
							|  |  |  |         $items  = []; | 
					
						
							| 
									
										
										
										
											2014-12-07 15:37:53 +01:00
										 |  |  |         $query  = $this->getUser() | 
					
						
							|  |  |  |                        ->transactionJournals() | 
					
						
							|  |  |  |                        ->withRelevantData() | 
					
						
							|  |  |  |                        ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') | 
					
						
							|  |  |  |                        ->where('transactions.account_id', $account->id) | 
					
						
							|  |  |  |                        ->orderBy('date', 'DESC'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($range == 'session') { | 
					
						
							| 
									
										
										
										
											2014-12-13 21:59:02 +01:00
										 |  |  |             $query->before(\Session::get('end', Carbon::now()->startOfMonth())); | 
					
						
							|  |  |  |             $query->after(\Session::get('start', Carbon::now()->startOfMonth())); | 
					
						
							| 
									
										
										
										
											2014-12-07 15:37:53 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         $count = $query->count(); | 
					
						
							|  |  |  |         $set   = $query->take($limit)->offset($offset)->get(['transaction_journals.*']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 09:34:53 +01:00
										 |  |  |         foreach ($set as $entry) { | 
					
						
							|  |  |  |             $items[] = $entry; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return \Paginator::make($items, $count, $limit); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-16 07:07:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-02 06:05:40 +01:00
										 |  |  | } |