firefly-iii/app/Repositories/PiggyBank/PiggyBankRepository.php

312 lines
7.8 KiB
PHP
Raw Normal View History

2015-02-25 12:32:33 -06:00
<?php
/**
* PiggyBankRepository.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-02-25 12:32:33 -06:00
namespace FireflyIII\Repositories\PiggyBank;
2016-05-15 05:08:41 -05:00
use Amount;
2015-04-20 14:57:20 -05:00
use Carbon\Carbon;
2016-10-22 03:13:49 -05:00
use FireflyIII\Models\Note;
2015-02-25 12:32:33 -06:00
use FireflyIII\Models\PiggyBank;
2015-04-20 14:57:20 -05:00
use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\User;
2015-02-27 04:02:08 -06:00
use Illuminate\Support\Collection;
2015-02-25 12:32:33 -06:00
/**
* Class PiggyBankRepository
*
* @package FireflyIII\Repositories\PiggyBank
*/
class PiggyBankRepository implements PiggyBankRepositoryInterface
{
/** @var User */
private $user;
/**
* @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;
}
/**
* @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 14:57:20 -05:00
/**
* @param PiggyBank $piggyBank
2016-02-05 02:25:15 -06:00
* @param string $amount
2015-04-20 14:57:20 -05:00
*
2016-04-06 02:27:45 -05:00
* @return PiggyBankEvent
2015-04-20 14:57:20 -05:00
*/
2016-04-06 02:27:45 -05:00
public function createEvent(PiggyBank $piggyBank, string $amount): PiggyBankEvent
2015-04-20 14:57:20 -05:00
{
2017-02-24 22:57:01 -06:00
/** @var PiggyBankEvent $event */
2016-04-06 02:27:45 -05:00
$event = PiggyBankEvent::create(['date' => Carbon::now(), 'amount' => $amount, 'piggy_bank_id' => $piggyBank->id]);
2015-04-20 14:57:20 -05:00
2016-04-06 02:27:45 -05:00
return $event;
2015-04-20 14:57:20 -05:00
}
/**
* @param PiggyBank $piggyBank
*
2016-04-06 02:27:45 -05:00
* @return bool
2016-05-01 00:09:58 -05:00
* @throws \Exception
2015-04-20 14:57:20 -05:00
*/
2016-04-06 02:27:45 -05:00
public function destroy(PiggyBank $piggyBank): bool
2015-04-20 14:57:20 -05:00
{
2016-04-06 02:27:45 -05:00
$piggyBank->delete();
2016-04-06 12:44:42 -05:00
2016-04-06 02:27:45 -05:00
return true;
2015-04-20 14:57:20 -05:00
}
/**
* @param int $piggyBankid
*
* @return PiggyBank
*/
public function find(int $piggyBankid): PiggyBank
{
$piggyBank = $this->user->piggyBanks()->where('piggy_banks.id', $piggyBankid)->first(['piggy_banks.*']);
if (!is_null($piggyBank)) {
return $piggyBank;
}
return new PiggyBank();
}
2015-04-20 14:57:20 -05:00
/**
* @param PiggyBank $piggyBank
*
* @return Collection
*/
2016-04-06 02:27:45 -05:00
public function getEvents(PiggyBank $piggyBank): Collection
2015-04-20 14:57:20 -05:00
{
return $piggyBank->piggyBankEvents()->orderBy('date', 'DESC')->orderBy('id', 'DESC')->get();
}
2016-01-24 13:38:58 -06:00
/**
* @return int
*/
2016-04-06 02:27:45 -05:00
public function getMaxOrder(): int
2016-01-24 13:38:58 -06:00
{
return intval($this->user->piggyBanks()->max('order'));
2016-01-24 13:38:58 -06:00
}
2015-04-20 14:57:20 -05:00
/**
* @return Collection
*/
2016-04-06 02:27:45 -05:00
public function getPiggyBanks(): Collection
2015-04-20 14:57:20 -05:00
{
/** @var Collection $set */
$set = $this->user->piggyBanks()->orderBy('order', 'ASC')->get();
2015-04-20 14:57:20 -05:00
return $set;
}
2016-05-15 05:08:41 -05:00
/**
* Also add amount in name.
*
* @return Collection
*/
public function getPiggyBanksWithAmount(): Collection
2016-05-15 05:08:41 -05:00
{
$set = $this->getPiggyBanks();
foreach ($set as $piggy) {
$currentAmount = $piggy->currentRelevantRep()->currentamount ?? '0';
$piggy->name = $piggy->name . ' (' . Amount::format($currentAmount, false) . ')';
2016-05-15 05:08:41 -05:00
}
return $set;
}
/**
* @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 12:00:33 -05:00
/**
* Set all piggy banks to order 0.
*
2016-04-06 02:27:45 -05:00
* @return bool
2015-03-15 12:00:33 -05:00
*/
2016-04-06 02:27:45 -05:00
public function reset(): bool
2015-03-15 12:00:33 -05:00
{
2015-05-09 15:30:16 -05:00
// split query to make it work in sqlite:
2016-12-15 14:35:33 -06:00
$set = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.id')
->where('accounts.user_id', $this->user->id)->get(['piggy_banks.*']);
2015-05-09 15:30:16 -05:00
foreach ($set as $e) {
$e->order = 0;
$e->save();
}
return true;
2015-03-15 12:00:33 -05:00
}
/**
*
* set id of piggy bank.
*
2015-07-08 23:13:39 -05:00
* @param int $piggyBankId
2015-03-15 12:00:33 -05:00
* @param int $order
*
2016-04-06 02:27:45 -05:00
* @return bool
2015-03-15 12:00:33 -05:00
*/
2016-04-06 02:27:45 -05:00
public function setOrder(int $piggyBankId, int $order): bool
2015-03-15 12:00:33 -05:00
{
$piggyBank = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', $this->user->id)
2015-07-08 23:13:39 -05:00
->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']);
2015-03-15 12:00:33 -05:00
if ($piggyBank) {
$piggyBank->order = $order;
$piggyBank->save();
}
2016-04-06 12:44:42 -05:00
2016-04-06 02:27:45 -05:00
return true;
2015-03-15 12:00:33 -05:00
}
2017-01-30 09:46:30 -06:00
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
}
/**
* @param array $data
*
* @return PiggyBank
*/
2016-04-06 02:27:45 -05:00
public function store(array $data): PiggyBank
{
2016-10-23 05:10:22 -05:00
$data['order'] = $this->getMaxOrder() + 1;
2017-02-24 22:57:01 -06:00
/** @var PiggyBank $piggyBank */
$piggyBank = PiggyBank::create($data);
2016-10-22 03:13:49 -05:00
$this->updateNote($piggyBank, $data['note']);
return $piggyBank;
}
/**
2015-05-05 03:23:01 -05:00
* @param PiggyBank $piggyBank
* @param array $data
*
* @return PiggyBank
*/
2016-04-06 02:27:45 -05:00
public function update(PiggyBank $piggyBank, array $data): PiggyBank
{
$piggyBank->name = $data['name'];
$piggyBank->account_id = intval($data['account_id']);
2016-03-16 11:48:07 -05:00
$piggyBank->targetamount = round($data['targetamount'], 2);
$piggyBank->targetdate = $data['targetdate'];
$piggyBank->startdate = $data['startdate'];
$piggyBank->save();
2016-10-22 03:13:49 -05:00
$this->updateNote($piggyBank, $data['note']);
2016-04-21 04:03:04 -05: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 02:40:50 -05:00
$diff = bcsub($piggyBank->targetamount, $repetition->currentamount);
$this->createEvent($piggyBank, $diff);
2016-04-21 04:03:04 -05:00
$repetition->currentamount = $piggyBank->targetamount;
$repetition->save();
}
return $piggyBank;
}
2016-10-22 03:13:49 -05:00
/**
* @param PiggyBank $piggyBank
* @param string $note
*
* @return bool
*/
private function updateNote(PiggyBank $piggyBank, string $note): bool
{
if (strlen($note) === 0) {
$dbNote = $piggyBank->notes()->first();
if (!is_null($dbNote)) {
$dbNote->delete();
}
return true;
}
2016-10-23 05:10:22 -05:00
$dbNote = $piggyBank->notes()->first();
2016-10-22 03:13:49 -05:00
if (is_null($dbNote)) {
2016-10-23 05:10:22 -05:00
$dbNote = new Note();
2016-10-22 03:13:49 -05:00
$dbNote->noteable()->associate($piggyBank);
}
$dbNote->text = trim($note);
$dbNote->save();
return true;
}
2015-03-29 01:14:32 -05:00
}