mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 02:26:58 +00:00
Clean up and simplify code.
This commit is contained in:
@@ -63,6 +63,28 @@ class ReminderHelper implements ReminderHelperInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create all reminders for a piggy bank for a given date.
|
||||||
|
*
|
||||||
|
* @param PiggyBank $piggyBank
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function createReminders(PiggyBank $piggyBank, Carbon $date)
|
||||||
|
{
|
||||||
|
$ranges = $this->getReminderRanges($piggyBank);
|
||||||
|
|
||||||
|
foreach ($ranges as $range) {
|
||||||
|
if ($date < $range['end'] && $date > $range['start']) {
|
||||||
|
// create a reminder here!
|
||||||
|
$this->createReminder($piggyBank, $range['start'], $range['end']);
|
||||||
|
// stop looping, we're done.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This routine will return an array consisting of two dates which indicate the start
|
* This routine will return an array consisting of two dates which indicate the start
|
||||||
* and end date for each reminder that this piggy bank will have, if the piggy bank has
|
* and end date for each reminder that this piggy bank will have, if the piggy bank has
|
||||||
|
@@ -4,6 +4,7 @@ namespace FireflyIII\Helpers\Reminders;
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Models\PiggyBank;
|
use FireflyIII\Models\PiggyBank;
|
||||||
|
use FireflyIII\Models\PiggyBankRepetition;
|
||||||
use FireflyIII\Models\Reminder;
|
use FireflyIII\Models\Reminder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,4 +50,13 @@ interface ReminderHelperInterface
|
|||||||
* @return Reminder
|
* @return Reminder
|
||||||
*/
|
*/
|
||||||
public function createReminder(PiggyBank $piggyBank, Carbon $start, Carbon $end);
|
public function createReminder(PiggyBank $piggyBank, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create all reminders for a piggy bank for a given date.
|
||||||
|
*
|
||||||
|
* @param PiggyBank $piggyBank
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function createReminders(PiggyBank $piggyBank, Carbon $date);
|
||||||
}
|
}
|
||||||
|
@@ -43,14 +43,9 @@ class JsonController extends Controller
|
|||||||
|
|
||||||
/** @var Bill $bill */
|
/** @var Bill $bill */
|
||||||
foreach ($bills as $bill) {
|
foreach ($bills as $bill) {
|
||||||
$ranges = $repository->getRanges($bill, $start, $end);
|
$amount += $repository->billPaymentsInRange($bill, $start, $end);
|
||||||
|
|
||||||
foreach ($ranges as $range) {
|
|
||||||
// paid a bill in this range?
|
|
||||||
$amount += $repository->getJournalsInRange($bill, $range['start'], $range['end'])->sum('amount');
|
|
||||||
}
|
}
|
||||||
}
|
unset($bill, $bills);
|
||||||
unset($ranges, $bill, $range, $bills);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find credit card accounts and possibly unpaid credit card bills.
|
* Find credit card accounts and possibly unpaid credit card bills.
|
||||||
|
@@ -52,17 +52,16 @@ class PiggyBanks
|
|||||||
->leftJoin('piggy_bank_repetitions', 'piggy_banks.id', '=', 'piggy_bank_repetitions.piggy_bank_id')
|
->leftJoin('piggy_bank_repetitions', 'piggy_banks.id', '=', 'piggy_bank_repetitions.piggy_bank_id')
|
||||||
->whereNull('piggy_bank_repetitions.id')
|
->whereNull('piggy_bank_repetitions.id')
|
||||||
->get(['piggy_banks.id', 'piggy_banks.startdate', 'piggy_banks.targetdate']);
|
->get(['piggy_banks.id', 'piggy_banks.startdate', 'piggy_banks.targetdate']);
|
||||||
if ($set->count() > 0) {
|
|
||||||
/** @var PiggyBank $partialPiggy */
|
/** @var PiggyBank $partialPiggy */
|
||||||
foreach ($set as $partialPiggy) {
|
foreach ($set as $partialPiggy) {
|
||||||
$repetition = new PiggyBankRepetition;
|
$repetition = new PiggyBankRepetition;
|
||||||
$repetition->piggyBank()->associate($partialPiggy);
|
$repetition->piggyBank()->associate($partialPiggy);
|
||||||
$repetition->startdate = is_null($partialPiggy->startdate) ? null : $partialPiggy->startdate;
|
$repetition->startdate = $partialPiggy->startdate;
|
||||||
$repetition->targetdate = is_null($partialPiggy->targetdate) ? null : $partialPiggy->targetdate;
|
$repetition->targetdate = $partialPiggy->targetdate;
|
||||||
$repetition->currentamount = 0;
|
$repetition->currentamount = 0;
|
||||||
$repetition->save();
|
$repetition->save();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
unset($partialPiggy, $set, $repetition);
|
unset($partialPiggy, $set, $repetition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -49,32 +49,18 @@ class Reminders
|
|||||||
if ($this->auth->check() && !$request->isXmlHttpRequest()) {
|
if ($this->auth->check() && !$request->isXmlHttpRequest()) {
|
||||||
// do reminders stuff.
|
// do reminders stuff.
|
||||||
$piggyBanks = $this->auth->user()->piggyBanks()->where('remind_me', 1)->get();
|
$piggyBanks = $this->auth->user()->piggyBanks()->where('remind_me', 1)->get();
|
||||||
$today = new Carbon;
|
|
||||||
/** @var \FireflyIII\Helpers\Reminders\ReminderHelperInterface $helper */
|
/** @var \FireflyIII\Helpers\Reminders\ReminderHelperInterface $helper */
|
||||||
$helper = App::make('FireflyIII\Helpers\Reminders\ReminderHelperInterface');
|
$helper = App::make('FireflyIII\Helpers\Reminders\ReminderHelperInterface');
|
||||||
|
|
||||||
/** @var PiggyBank $piggyBank */
|
/** @var PiggyBank $piggyBank */
|
||||||
foreach ($piggyBanks as $piggyBank) {
|
foreach ($piggyBanks as $piggyBank) {
|
||||||
$ranges = $helper->getReminderRanges($piggyBank);
|
$helper->createReminders($piggyBank, new Carbon);
|
||||||
|
|
||||||
foreach ($ranges as $range) {
|
|
||||||
if ($today < $range['end'] && $today > $range['start']) {
|
|
||||||
// create a reminder here!
|
|
||||||
$helper->createReminder($piggyBank, $range['start'], $range['end']);
|
|
||||||
// stop looping, we're done.
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// delete invalid reminders
|
// delete invalid reminders
|
||||||
$set = $this->auth->user()->reminders()->leftJoin('piggy_banks', 'piggy_banks.id', '=', 'remindersable_id')->whereNull('piggy_banks.id')->get(
|
Reminder::whereUserId($this->auth->user()->id)
|
||||||
['reminders.id']
|
->leftJoin('piggy_banks', 'piggy_banks.id', '=', 'remindersable_id')
|
||||||
);
|
->whereNull('piggy_banks.id')
|
||||||
foreach ($set as $reminder) {
|
->delete();
|
||||||
$reminder->delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// get and list active reminders:
|
// get and list active reminders:
|
||||||
$reminders = $this->auth->user()->reminders()->today()->get();
|
$reminders = $this->auth->user()->reminders()->today()->get();
|
||||||
|
@@ -22,6 +22,27 @@ use Navigation;
|
|||||||
*/
|
*/
|
||||||
class BillRepository implements BillRepositoryInterface
|
class BillRepository implements BillRepositoryInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Returns the sum of all payments connected to this bill between the dates.
|
||||||
|
*
|
||||||
|
* @param Bill $bill
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function billPaymentsInRange(Bill $bill, Carbon $start, Carbon $end)
|
||||||
|
{
|
||||||
|
$amount = 0;
|
||||||
|
$journals = $bill->transactionjournals()->before($end)->after($start)->get();
|
||||||
|
/** @var TransactionJournal $journal */
|
||||||
|
foreach ($journals as $journal) {
|
||||||
|
$amount += $journal->amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $amount;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a fake bill to help the chart controller.
|
* Create a fake bill to help the chart controller.
|
||||||
*
|
*
|
||||||
|
@@ -15,6 +15,17 @@ use Illuminate\Support\Collection;
|
|||||||
interface BillRepositoryInterface
|
interface BillRepositoryInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the sum of all payments connected to this bill between the dates.
|
||||||
|
*
|
||||||
|
* @param Bill $bill
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function billPaymentsInRange(Bill $bill, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a fake bill to help the chart controller.
|
* Create a fake bill to help the chart controller.
|
||||||
*
|
*
|
||||||
|
@@ -33,90 +33,23 @@ class TagRepository implements TagRepositoryInterface
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($tag->tagMode == 'nothing') {
|
switch ($tag->tagMode) {
|
||||||
// save it, no problem:
|
case 'nothing':
|
||||||
$journal->tags()->save($tag);
|
$journal->tags()->save($tag);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
break;
|
||||||
|
case 'balancingAct':
|
||||||
/*
|
return $this->connectBalancingAct($journal, $tag);
|
||||||
* get some withdrawal types:
|
break;
|
||||||
*/
|
case 'advancePayment':
|
||||||
/** @var TransactionType $withdrawal */
|
return $this->connectAdvancePayment($journal, $tag);
|
||||||
$withdrawal = TransactionType::whereType('Withdrawal')->first();
|
break;
|
||||||
/** @var TransactionType $deposit */
|
|
||||||
$deposit = TransactionType::whereType('Deposit')->first();
|
|
||||||
/** @var TransactionType $transfer */
|
|
||||||
$transfer = TransactionType::whereType('Transfer')->first();
|
|
||||||
|
|
||||||
$withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
|
|
||||||
$transfers = $tag->transactionjournals()->where('transaction_type_id', $transfer->id)->count();
|
|
||||||
$deposits = $tag->transactionjournals()->where('transaction_type_id', $deposit->id)->count();
|
|
||||||
|
|
||||||
if ($tag->tagMode == 'balancingAct') {
|
|
||||||
|
|
||||||
// only if this is the only withdrawal.
|
|
||||||
if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
|
|
||||||
$journal->tags()->save($tag);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// and only if this is the only transfer
|
|
||||||
if ($journal->transaction_type_id == $transfer->id && $transfers < 1) {
|
|
||||||
$journal->tags()->save($tag);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ignore expense
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if ($tag->tagMode == 'advancePayment') {
|
|
||||||
|
|
||||||
// advance payments cannot accept transfers:
|
|
||||||
if ($journal->transaction_type_id == $transfer->id) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// the first transaction to be attached to this
|
|
||||||
// tag is attached just like that:
|
|
||||||
if ($withdrawals < 1 && $deposits < 1) {
|
|
||||||
$journal->tags()->save($tag);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if withdrawal and already has a withdrawal, return false:
|
|
||||||
if ($journal->transaction_type_id == $withdrawal->id && $withdrawals == 1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if already has transaction journals, must match ALL asset account id's:
|
|
||||||
if ($deposits > 0 || $withdrawals == 1) {
|
|
||||||
$match = true;
|
|
||||||
/** @var TransactionJournal $check */
|
|
||||||
foreach ($tag->transactionjournals as $check) {
|
|
||||||
if ($check->assetAccount->id != $journal->assetAccount->id) {
|
|
||||||
$match = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($match) {
|
|
||||||
$journal->tags()->save($tag);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// @codeCoverageIgnoreStart
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// @codeCoverageIgnoreEnd
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Tag $tag
|
* @param Tag $tag
|
||||||
*
|
*
|
||||||
@@ -144,6 +77,7 @@ class TagRepository implements TagRepositoryInterface
|
|||||||
|
|
||||||
return $tags;
|
return $tags;
|
||||||
}
|
}
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
@@ -187,4 +121,106 @@ class TagRepository implements TagRepositoryInterface
|
|||||||
|
|
||||||
return $tag;
|
return $tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
* @param Tag $tag
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
protected function connectBalancingAct(TransactionJournal $journal, Tag $tag)
|
||||||
|
{
|
||||||
|
/** @var TransactionType $withdrawal */
|
||||||
|
$withdrawal = TransactionType::whereType('Withdrawal')->first();
|
||||||
|
$withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
|
||||||
|
/** @var TransactionType $transfer */
|
||||||
|
$transfer = TransactionType::whereType('Transfer')->first();
|
||||||
|
$transfers = $tag->transactionjournals()->where('transaction_type_id', $transfer->id)->count();
|
||||||
|
|
||||||
|
|
||||||
|
// only if this is the only withdrawal.
|
||||||
|
if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
|
||||||
|
$journal->tags()->save($tag);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// and only if this is the only transfer
|
||||||
|
if ($journal->transaction_type_id == $transfer->id && $transfers < 1) {
|
||||||
|
$journal->tags()->save($tag);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ignore expense
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
* @param Tag $tag
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
protected function connectAdvancePayment(TransactionJournal $journal, Tag $tag)
|
||||||
|
{
|
||||||
|
/** @var TransactionType $transfer */
|
||||||
|
$transfer = TransactionType::whereType('Transfer')->first();
|
||||||
|
/** @var TransactionType $withdrawal */
|
||||||
|
$withdrawal = TransactionType::whereType('Withdrawal')->first();
|
||||||
|
/** @var TransactionType $deposit */
|
||||||
|
$deposit = TransactionType::whereType('Deposit')->first();
|
||||||
|
|
||||||
|
$withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
|
||||||
|
$deposits = $tag->transactionjournals()->where('transaction_type_id', $deposit->id)->count();
|
||||||
|
|
||||||
|
// advance payments cannot accept transfers:
|
||||||
|
if ($journal->transaction_type_id == $transfer->id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the first transaction to be attached to this
|
||||||
|
// tag is attached just like that:
|
||||||
|
if ($withdrawals < 1 && $deposits < 1) {
|
||||||
|
$journal->tags()->save($tag);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if withdrawal and already has a withdrawal, return false:
|
||||||
|
if ($journal->transaction_type_id == $withdrawal->id && $withdrawals == 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if already has transaction journals, must match ALL asset account id's:
|
||||||
|
if ($deposits > 0 || $withdrawals == 1) {
|
||||||
|
return $this->matchAll($journal, $tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
* @param Tag $tag
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function matchAll(TransactionJournal $journal, Tag $tag)
|
||||||
|
{
|
||||||
|
$match = true;
|
||||||
|
/** @var TransactionJournal $check */
|
||||||
|
foreach ($tag->transactionjournals as $check) {
|
||||||
|
if ($check->assetAccount->id != $journal->assetAccount->id) {
|
||||||
|
$match = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($match) {
|
||||||
|
$journal->tags()->save($tag);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -3,10 +3,12 @@
|
|||||||
namespace FireflyIII\Support\Twig;
|
namespace FireflyIII\Support\Twig;
|
||||||
|
|
||||||
use App;
|
use App;
|
||||||
|
use Carbon\Carbon;
|
||||||
use Config;
|
use Config;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
use Route;
|
use Route;
|
||||||
|
use Session;
|
||||||
use Twig_Extension;
|
use Twig_Extension;
|
||||||
use Twig_SimpleFilter;
|
use Twig_SimpleFilter;
|
||||||
use Twig_SimpleFunction;
|
use Twig_SimpleFunction;
|
||||||
@@ -57,8 +59,9 @@ class General extends Twig_Extension
|
|||||||
if (is_null($account)) {
|
if (is_null($account)) {
|
||||||
return 'NULL';
|
return 'NULL';
|
||||||
}
|
}
|
||||||
|
$date = Session::get('end', Carbon::now()->endOfMonth());
|
||||||
|
|
||||||
return App::make('steam')->balance($account);
|
return App::make('steam')->balance($account, $date);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user