| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * PiggyBankRepository.php | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Copyright (c) 2017 thegrumpydictator@gmail.com | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * This file is part of Firefly III. | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Firefly III is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							| 
									
										
										
										
											2017-12-17 14:44:05 +01:00
										 |  |  |  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Repositories\PiggyBank; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2016-10-22 10:13:49 +02:00
										 |  |  | use FireflyIII\Models\Note; | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | use FireflyIII\Models\PiggyBank; | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  | use FireflyIII\Models\PiggyBankEvent; | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  | use FireflyIII\Models\PiggyBankRepetition; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2016-03-03 08:58:15 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2015-02-27 11:02:08 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class PiggyBankRepository. | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class PiggyBankRepository implements PiggyBankRepositoryInterface | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-03 08:58:15 +01:00
										 |  |  |     /** @var User */ | 
					
						
							|  |  |  |     private $user; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-22 17:02:15 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param PiggyBank $piggyBank | 
					
						
							|  |  |  |      * @param string    $amount | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function addAmount(PiggyBank $piggyBank, string $amount): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $repetition                = $piggyBank->currentRelevantRep(); | 
					
						
							|  |  |  |         $currentAmount             = $repetition->currentamount ?? '0'; | 
					
						
							|  |  |  |         $repetition->currentamount = bcadd($currentAmount, $amount); | 
					
						
							|  |  |  |         $repetition->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // create event
 | 
					
						
							|  |  |  |         $this->createEvent($piggyBank, $amount); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param PiggyBankRepetition $repetition | 
					
						
							|  |  |  |      * @param string              $amount | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $newAmount                 = bcadd($repetition->currentamount, $amount); | 
					
						
							|  |  |  |         $repetition->currentamount = $newAmount; | 
					
						
							|  |  |  |         $repetition->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $newAmount; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-22 17:02:15 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param PiggyBank $piggyBank | 
					
						
							|  |  |  |      * @param string    $amount | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function canAddAmount(PiggyBank $piggyBank, string $amount): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $leftOnAccount = $piggyBank->leftOnAccount(new Carbon); | 
					
						
							|  |  |  |         $savedSoFar    = strval($piggyBank->currentRelevantRep()->currentamount); | 
					
						
							|  |  |  |         $leftToSave    = bcsub($piggyBank->targetamount, $savedSoFar); | 
					
						
							|  |  |  |         $maxAmount     = strval(min(round($leftOnAccount, 12), round($leftToSave, 12))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return bccomp($amount, $maxAmount) <= 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param PiggyBank $piggyBank | 
					
						
							|  |  |  |      * @param string    $amount | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function canRemoveAmount(PiggyBank $piggyBank, string $amount): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $savedSoFar = $piggyBank->currentRelevantRep()->currentamount; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return bccomp($amount, $savedSoFar) <= 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param PiggyBank $piggyBank | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |      * @param string    $amount | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |      * @return PiggyBankEvent | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |     public function createEvent(PiggyBank $piggyBank, string $amount): PiggyBankEvent | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-02-25 05:57:01 +01:00
										 |  |  |         /** @var PiggyBankEvent $event */ | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |         $event = PiggyBankEvent::create(['date' => Carbon::now(), 'amount' => $amount, 'piggy_bank_id' => $piggyBank->id]); | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |         return $event; | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param PiggyBank          $piggyBank | 
					
						
							|  |  |  |      * @param string             $amount | 
					
						
							|  |  |  |      * @param TransactionJournal $journal | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return PiggyBankEvent | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function createEventWithJournal(PiggyBank $piggyBank, string $amount, TransactionJournal $journal): PiggyBankEvent | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var PiggyBankEvent $event */ | 
					
						
							|  |  |  |         $event = PiggyBankEvent::create( | 
					
						
							|  |  |  |             ['piggy_bank_id' => $piggyBank->id, 'transaction_journal_id' => $journal->id, 'date' => $journal->date, 'amount' => $amount] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $event; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param PiggyBank $piggyBank | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-05-01 07:09:58 +02:00
										 |  |  |      * @throws \Exception | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |     public function destroy(PiggyBank $piggyBank): bool | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |         $piggyBank->delete(); | 
					
						
							| 
									
										
										
										
											2016-04-06 19:44:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-17 15:19:07 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param int $piggyBankid | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return PiggyBank | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function find(int $piggyBankid): PiggyBank | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $piggyBank = $this->user->piggyBanks()->where('piggy_banks.id', $piggyBankid)->first(['piggy_banks.*']); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (null !== $piggyBank) { | 
					
						
							| 
									
										
										
										
											2016-05-17 15:19:07 +02:00
										 |  |  |             return $piggyBank; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return new PiggyBank(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param PiggyBank $piggyBank | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |     public function getEvents(PiggyBank $piggyBank): Collection | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $piggyBank->piggyBankEvents()->orderBy('date', 'DESC')->orderBy('id', 'DESC')->get(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Used for connecting to a piggy bank. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param PiggyBank           $piggyBank | 
					
						
							|  |  |  |      * @param PiggyBankRepetition $repetition | 
					
						
							|  |  |  |      * @param TransactionJournal  $journal | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getExactAmount(PiggyBank $piggyBank, PiggyBankRepetition $repetition, TransactionJournal $journal): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $amount  = $journal->amountPositive(); | 
					
						
							|  |  |  |         $sources = $journal->sourceAccountList()->pluck('id')->toArray(); | 
					
						
							|  |  |  |         $room    = bcsub(strval($piggyBank->targetamount), strval($repetition->currentamount)); | 
					
						
							|  |  |  |         $compare = bcmul($repetition->currentamount, '-1'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Log::debug(sprintf('Will add/remove %f to piggy bank #%d ("%s")', $amount, $piggyBank->id, $piggyBank->name)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // if piggy account matches source account, the amount is positive
 | 
					
						
							|  |  |  |         if (in_array($piggyBank->account_id, $sources)) { | 
					
						
							|  |  |  |             $amount = bcmul($amount, '-1'); | 
					
						
							|  |  |  |             Log::debug(sprintf('Account #%d is the source, so will remove amount from piggy bank.', $piggyBank->account_id)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // if the amount is positive, make sure it fits in piggy bank:
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (1 === bccomp($amount, '0') && bccomp($room, $amount) === -1) { | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |             // amount is positive and $room is smaller than $amount
 | 
					
						
							|  |  |  |             Log::debug(sprintf('Room in piggy bank for extra money is %f', $room)); | 
					
						
							|  |  |  |             Log::debug(sprintf('There is NO room to add %f to piggy bank #%d ("%s")', $amount, $piggyBank->id, $piggyBank->name)); | 
					
						
							|  |  |  |             Log::debug(sprintf('New amount is %f', $room)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $room; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // amount is negative and $currentamount is smaller than $amount
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (bccomp($amount, '0') === -1 && 1 === bccomp($compare, $amount)) { | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |             Log::debug(sprintf('Max amount to remove is %f', $repetition->currentamount)); | 
					
						
							|  |  |  |             Log::debug(sprintf('Cannot remove %f from piggy bank #%d ("%s")', $amount, $piggyBank->id, $piggyBank->name)); | 
					
						
							|  |  |  |             Log::debug(sprintf('New amount is %f', $compare)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $compare; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $amount; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-24 20:38:58 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |     public function getMaxOrder(): int | 
					
						
							| 
									
										
										
										
											2016-01-24 20:38:58 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-03-03 08:58:15 +01:00
										 |  |  |         return intval($this->user->piggyBanks()->max('order')); | 
					
						
							| 
									
										
										
										
											2016-01-24 20:38:58 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |     public function getPiggyBanks(): Collection | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @var Collection $set */ | 
					
						
							| 
									
										
										
										
											2016-03-03 08:58:15 +01:00
										 |  |  |         $set = $this->user->piggyBanks()->orderBy('order', 'ASC')->get(); | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $set; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-15 12:08:41 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Also add amount in name. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-12-04 18:02:19 +01:00
										 |  |  |     public function getPiggyBanksWithAmount(): Collection | 
					
						
							| 
									
										
										
										
											2016-05-15 12:08:41 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-10-05 11:49:06 +02:00
										 |  |  |         $currency = app('amount')->getDefaultCurrency(); | 
					
						
							| 
									
										
										
										
											2017-06-05 11:12:50 +02:00
										 |  |  |         $set      = $this->getPiggyBanks(); | 
					
						
							| 
									
										
										
										
											2016-05-15 12:08:41 +02:00
										 |  |  |         foreach ($set as $piggy) { | 
					
						
							| 
									
										
										
										
											2016-08-24 20:06:45 +02:00
										 |  |  |             $currentAmount = $piggy->currentRelevantRep()->currentamount ?? '0'; | 
					
						
							| 
									
										
										
										
											2017-06-04 23:39:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-05 11:49:06 +02:00
										 |  |  |             $piggy->name = $piggy->name . ' (' . app('amount')->formatAnything($currency, $currentAmount, false) . ')'; | 
					
						
							| 
									
										
										
										
											2016-05-15 12:08:41 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $set; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param PiggyBank $piggyBank | 
					
						
							|  |  |  |      * @param Carbon    $date | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return PiggyBankRepetition | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getRepetition(PiggyBank $piggyBank, Carbon $date): PiggyBankRepetition | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($date)->first(); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (null === $repetition) { | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |             return new PiggyBankRepetition; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $repetition; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-22 17:02:15 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param PiggyBank $piggyBank | 
					
						
							|  |  |  |      * @param string    $amount | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function removeAmount(PiggyBank $piggyBank, string $amount): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $repetition                = $piggyBank->currentRelevantRep(); | 
					
						
							|  |  |  |         $repetition->currentamount = bcsub($repetition->currentamount, $amount); | 
					
						
							|  |  |  |         $repetition->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // create event
 | 
					
						
							|  |  |  |         $this->createEvent($piggyBank, bcmul($amount, '-1')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-15 18:00:33 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Set all piggy banks to order 0. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2015-03-15 18:00:33 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |     public function reset(): bool | 
					
						
							| 
									
										
										
										
											2015-03-15 18:00:33 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-09 22:30:16 +02:00
										 |  |  |         // split query to make it work in sqlite:
 | 
					
						
							| 
									
										
										
										
											2016-12-15 21:35:33 +01:00
										 |  |  |         $set = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.id') | 
					
						
							| 
									
										
										
										
											2016-03-03 08:58:15 +01:00
										 |  |  |                         ->where('accounts.user_id', $this->user->id)->get(['piggy_banks.*']); | 
					
						
							| 
									
										
										
										
											2015-05-09 22:30:16 +02:00
										 |  |  |         foreach ($set as $e) { | 
					
						
							|  |  |  |             $e->order = 0; | 
					
						
							|  |  |  |             $e->save(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2015-03-15 18:00:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * set id of piggy bank. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-07-09 06:13:39 +02:00
										 |  |  |      * @param int $piggyBankId | 
					
						
							| 
									
										
										
										
											2015-03-15 18:00:33 +01:00
										 |  |  |      * @param int $order | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2015-03-15 18:00:33 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |     public function setOrder(int $piggyBankId, int $order): bool | 
					
						
							| 
									
										
										
										
											2015-03-15 18:00:33 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-03-03 08:58:15 +01:00
										 |  |  |         $piggyBank = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', $this->user->id) | 
					
						
							| 
									
										
										
										
											2015-07-09 06:13:39 +02:00
										 |  |  |                               ->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']); | 
					
						
							| 
									
										
										
										
											2015-03-15 18:00:33 +01:00
										 |  |  |         if ($piggyBank) { | 
					
						
							|  |  |  |             $piggyBank->order = $order; | 
					
						
							|  |  |  |             $piggyBank->save(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-04-06 19:44:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2015-03-15 18:00:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-30 16:46:30 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param User $user | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setUser(User $user) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param array $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return PiggyBank | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |     public function store(array $data): PiggyBank | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-23 12:10:22 +02:00
										 |  |  |         $data['order'] = $this->getMaxOrder() + 1; | 
					
						
							| 
									
										
										
										
											2017-02-25 05:57:01 +01:00
										 |  |  |         /** @var PiggyBank $piggyBank */ | 
					
						
							|  |  |  |         $piggyBank = PiggyBank::create($data); | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-22 10:13:49 +02:00
										 |  |  |         $this->updateNote($piggyBank, $data['note']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  |         return $piggyBank; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-05-05 10:23:01 +02:00
										 |  |  |      * @param PiggyBank $piggyBank | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  |      * @param array     $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return PiggyBank | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |     public function update(PiggyBank $piggyBank, array $data): PiggyBank | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $piggyBank->name         = $data['name']; | 
					
						
							|  |  |  |         $piggyBank->account_id   = intval($data['account_id']); | 
					
						
							| 
									
										
										
										
											2016-03-16 17:48:07 +01:00
										 |  |  |         $piggyBank->targetamount = round($data['targetamount'], 2); | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  |         $piggyBank->targetdate   = $data['targetdate']; | 
					
						
							|  |  |  |         $piggyBank->startdate    = $data['startdate']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $piggyBank->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-22 10:13:49 +02:00
										 |  |  |         $this->updateNote($piggyBank, $data['note']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-21 11:03:04 +02:00
										 |  |  |         // if the piggy bank is now smaller than the current relevant rep,
 | 
					
						
							|  |  |  |         // remove money from the rep.
 | 
					
						
							|  |  |  |         $repetition = $piggyBank->currentRelevantRep(); | 
					
						
							|  |  |  |         if ($repetition->currentamount > $piggyBank->targetamount) { | 
					
						
							| 
									
										
										
										
											2016-10-07 09:40:50 +02:00
										 |  |  |             $diff = bcsub($piggyBank->targetamount, $repetition->currentamount); | 
					
						
							|  |  |  |             $this->createEvent($piggyBank, $diff); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-21 11:03:04 +02:00
										 |  |  |             $repetition->currentamount = $piggyBank->targetamount; | 
					
						
							|  |  |  |             $repetition->save(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  |         return $piggyBank; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-22 10:13:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param PiggyBank $piggyBank | 
					
						
							|  |  |  |      * @param string    $note | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function updateNote(PiggyBank $piggyBank, string $note): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (0 === strlen($note)) { | 
					
						
							| 
									
										
										
										
											2016-10-22 10:13:49 +02:00
										 |  |  |             $dbNote = $piggyBank->notes()->first(); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |             if (null !== $dbNote) { | 
					
						
							| 
									
										
										
										
											2016-10-22 10:13:49 +02:00
										 |  |  |                 $dbNote->delete(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-10-23 12:10:22 +02:00
										 |  |  |         $dbNote = $piggyBank->notes()->first(); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (null === $dbNote) { | 
					
						
							| 
									
										
										
										
											2016-10-23 12:10:22 +02:00
										 |  |  |             $dbNote = new Note(); | 
					
						
							| 
									
										
										
										
											2016-10-22 10:13:49 +02:00
										 |  |  |             $dbNote->noteable()->associate($piggyBank); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $dbNote->text = trim($note); | 
					
						
							|  |  |  |         $dbNote->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-29 08:14:32 +02:00
										 |  |  | } |