mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 18:41:08 +00:00
Cleaned up some bill related code.
This commit is contained in:
@@ -21,7 +21,11 @@ use Steam;
|
|||||||
class ReportQuery implements ReportQueryInterface
|
class ReportQuery implements ReportQueryInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* See ReportQueryInterface::incomeInPeriodCorrected
|
* See ReportQueryInterface::incomeInPeriodCorrected.
|
||||||
|
*
|
||||||
|
* This method's length is caused mainly by the query build stuff. Therefor:
|
||||||
|
*
|
||||||
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||||
*
|
*
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
* @param Carbon $end
|
* @param Carbon $end
|
||||||
|
@@ -9,10 +9,8 @@ use FireflyIII\Models\TransactionJournal;
|
|||||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||||
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||||
use FireflyIII\Support\CacheProperties;
|
use FireflyIII\Support\CacheProperties;
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
use Response;
|
use Response;
|
||||||
use Session;
|
use Session;
|
||||||
use Steam;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BillController
|
* Class BillController
|
||||||
@@ -39,19 +37,14 @@ class BillController extends Controller
|
|||||||
* Shows all bills and whether or not theyve been paid this month (pie chart).
|
* Shows all bills and whether or not theyve been paid this month (pie chart).
|
||||||
*
|
*
|
||||||
* @param BillRepositoryInterface $repository
|
* @param BillRepositoryInterface $repository
|
||||||
* @param AccountRepositoryInterface $accounts
|
|
||||||
*
|
*
|
||||||
* @return \Symfony\Component\HttpFoundation\Response
|
* @return \Symfony\Component\HttpFoundation\Response
|
||||||
*/
|
*/
|
||||||
public function frontpage(BillRepositoryInterface $repository, AccountRepositoryInterface $accounts)
|
public function frontpage(BillRepositoryInterface $repository)
|
||||||
{
|
{
|
||||||
|
|
||||||
$start = Session::get('start', Carbon::now()->startOfMonth());
|
$start = Session::get('start', Carbon::now()->startOfMonth());
|
||||||
$end = Session::get('end', Carbon::now()->endOfMonth());
|
$end = Session::get('end', Carbon::now()->endOfMonth());
|
||||||
|
$cache = new CacheProperties(); // chart properties for cache:
|
||||||
|
|
||||||
// chart properties for cache:
|
|
||||||
$cache = new CacheProperties();
|
|
||||||
$cache->addProperty($start);
|
$cache->addProperty($start);
|
||||||
$cache->addProperty($end);
|
$cache->addProperty($end);
|
||||||
$cache->addProperty('bills');
|
$cache->addProperty('bills');
|
||||||
@@ -60,46 +53,13 @@ class BillController extends Controller
|
|||||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
$bills = $repository->getActiveBills();
|
$set = $repository->getBillsForChart($start, $end);
|
||||||
$paid = new Collection; // journals.
|
|
||||||
$unpaid = new Collection; // bills
|
|
||||||
|
|
||||||
|
// optionally expand this set with credit card data
|
||||||
|
$set = $repository->getCreditCardInfoForChart($set, $start, $end);
|
||||||
|
$paid = $set->get('paid');
|
||||||
|
$unpaid = $set->get('unpaid');
|
||||||
|
|
||||||
/** @var Bill $bill */
|
|
||||||
foreach ($bills as $bill) {
|
|
||||||
$ranges = $repository->getRanges($bill, $start, $end);
|
|
||||||
|
|
||||||
foreach ($ranges as $range) {
|
|
||||||
// paid a bill in this range?
|
|
||||||
$journals = $repository->getJournalsInRange($bill, $range['start'], $range['end']);
|
|
||||||
if ($journals->count() == 0) {
|
|
||||||
$unpaid->push([$bill, $range['start']]);
|
|
||||||
} else {
|
|
||||||
$paid = $paid->merge($journals);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$creditCards = $accounts->getCreditCards();
|
|
||||||
foreach ($creditCards as $creditCard) {
|
|
||||||
$balance = Steam::balance($creditCard, $end, true);
|
|
||||||
$date = new Carbon($creditCard->getMeta('ccMonthlyPaymentDate'));
|
|
||||||
if ($balance < 0) {
|
|
||||||
// unpaid! create a fake bill that matches the amount.
|
|
||||||
$description = $creditCard->name;
|
|
||||||
$amount = $balance * -1;
|
|
||||||
$fakeBill = $repository->createFakeBill($description, $date, $amount);
|
|
||||||
unset($description, $amount);
|
|
||||||
$unpaid->push([$fakeBill, $date]);
|
|
||||||
}
|
|
||||||
if ($balance == 0) {
|
|
||||||
// find transfer(s) TO the credit card which should account for
|
|
||||||
// anything paid. If not, the CC is not yet used.
|
|
||||||
$journals = $accounts->getTransfersInRange($creditCard, $start, $end);
|
|
||||||
$paid = $paid->merge($journals);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// build chart:
|
// build chart:
|
||||||
$data = $this->generator->frontpage($paid, $unpaid);
|
$data = $this->generator->frontpage($paid, $unpaid);
|
||||||
|
@@ -10,6 +10,7 @@ use FireflyIII\Models\Transaction;
|
|||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Navigation;
|
use Navigation;
|
||||||
|
use Steam;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BillRepository
|
* Class BillRepository
|
||||||
@@ -39,6 +40,7 @@ class BillRepository implements BillRepositoryInterface
|
|||||||
return $amount;
|
return $amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a fake bill to help the chart controller.
|
* Create a fake bill to help the chart controller.
|
||||||
*
|
*
|
||||||
@@ -383,4 +385,84 @@ class BillRepository implements BillRepositoryInterface
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a collection of paid bills and a collection of unpaid bills to be used
|
||||||
|
* in the pie chart on the front page.
|
||||||
|
*
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getBillsForChart(Carbon $start, Carbon $end)
|
||||||
|
{
|
||||||
|
$paid = new Collection;
|
||||||
|
$unpaid = new Collection;
|
||||||
|
|
||||||
|
$bills = $this->getActiveBills();
|
||||||
|
/** @var Bill $bill */
|
||||||
|
foreach ($bills as $bill) {
|
||||||
|
$ranges = $this->getRanges($bill, $start, $end);
|
||||||
|
|
||||||
|
foreach ($ranges as $range) {
|
||||||
|
// paid a bill in this range?
|
||||||
|
$journals = $this->getJournalsInRange($bill, $range['start'], $range['end']);
|
||||||
|
if ($journals->count() == 0) {
|
||||||
|
$unpaid->push([$bill, $range['start']]);
|
||||||
|
} else {
|
||||||
|
$paid = $paid->merge($journals);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$set = new Collection;
|
||||||
|
$set->put('paid', $paid);
|
||||||
|
$set->put('unpaid', $unpaid);
|
||||||
|
|
||||||
|
return $set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes the paid/unpaid bills collection set up before and expands it using
|
||||||
|
* credit cards the user might have.
|
||||||
|
*
|
||||||
|
* @param Collection $set
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getCreditCardInfoForChart(Collection $set, Carbon $start, Carbon $end)
|
||||||
|
{
|
||||||
|
|
||||||
|
$accounts = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
|
||||||
|
$creditCards = $accounts->getCreditCards();
|
||||||
|
$paid = $set->get('paid');
|
||||||
|
$unpaid = $set->get('unpaid');
|
||||||
|
|
||||||
|
foreach ($creditCards as $creditCard) {
|
||||||
|
$balance = Steam::balance($creditCard, $end, true);
|
||||||
|
$date = new Carbon($creditCard->getMeta('ccMonthlyPaymentDate'));
|
||||||
|
if ($balance < 0) {
|
||||||
|
// unpaid! create a fake bill that matches the amount.
|
||||||
|
$description = $creditCard->name;
|
||||||
|
$amount = $balance * -1;
|
||||||
|
$fakeBill = $this->createFakeBill($description, $date, $amount);
|
||||||
|
unset($description, $amount);
|
||||||
|
$unpaid->push([$fakeBill, $date]);
|
||||||
|
}
|
||||||
|
if ($balance == 0) {
|
||||||
|
// find transfer(s) TO the credit card which should account for
|
||||||
|
// anything paid. If not, the CC is not yet used.
|
||||||
|
$journals = $accounts->getTransfersInRange($creditCard, $start, $end);
|
||||||
|
$paid = $paid->merge($journals);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$set = new Collection;
|
||||||
|
$set->put('paid', $paid);
|
||||||
|
$set->put('unpaid', $unpaid);
|
||||||
|
|
||||||
|
return $set;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -15,6 +15,29 @@ use Illuminate\Support\Collection;
|
|||||||
interface BillRepositoryInterface
|
interface BillRepositoryInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes the paid/unpaid bills collection set up before and expands it using
|
||||||
|
* credit cards the user might have.
|
||||||
|
*
|
||||||
|
* @param Collection $set
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getCreditCardInfoForChart(Collection $set, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a collection of paid bills and a collection of unpaid bills to be used
|
||||||
|
* in the pie chart on the front page.
|
||||||
|
*
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getBillsForChart(Carbon $start, Carbon $end);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the sum of all payments connected to this bill between the dates.
|
* Returns the sum of all payments connected to this bill between the dates.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user