firefly-iii/app/Http/Controllers/JsonController.php

242 lines
7.8 KiB
PHP
Raw Normal View History

2015-02-24 15:53:38 -06:00
<?php namespace FireflyIII\Http\Controllers;
2015-03-06 01:20:27 -06:00
use Amount;
use Carbon\Carbon;
2015-04-09 14:27:35 -05:00
use FireflyIII\Helpers\Report\ReportQueryInterface;
use FireflyIII\Models\Account;
2015-03-06 01:20:27 -06:00
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2015-03-06 01:20:27 -06:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2015-04-11 12:59:41 -05:00
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2015-04-28 03:36:13 -05:00
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Support\Collection;
2015-02-24 15:53:38 -06:00
use Response;
2015-03-06 01:20:27 -06:00
use Session;
use Steam;
2015-03-29 00:51:56 -05:00
2015-02-24 15:53:38 -06:00
/**
* Class JsonController
*
* @package FireflyIII\Http\Controllers
*/
2015-03-06 01:20:27 -06:00
class JsonController extends Controller
{
2015-03-06 01:20:27 -06:00
/**
2015-05-03 05:58:55 -05:00
* @param BillRepositoryInterface $repository
*
* @param AccountRepositoryInterface $accountRepository
2015-03-06 01:20:27 -06:00
*
* @return \Symfony\Component\HttpFoundation\Response
2015-03-06 01:20:27 -06:00
*/
public function boxBillsPaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
2015-03-06 01:20:27 -06:00
{
2015-04-11 12:59:41 -05:00
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
$amount = 0;
2015-04-09 14:29:58 -05:00
2015-05-17 02:18:44 -05:00
// these two functions are the same as the chart
2015-04-11 00:32:11 -05:00
$bills = $repository->getActiveBills();
/** @var Bill $bill */
foreach ($bills as $bill) {
2015-05-17 03:01:47 -05:00
$amount += $repository->billPaymentsInRange($bill, $start, $end);
}
2015-05-17 03:01:47 -05:00
unset($bill, $bills);
/**
* Find credit card accounts and possibly unpaid credit card bills.
*/
$creditCards = $accountRepository->getCreditCards();
// if the balance is not zero, the monthly payment is still underway.
/** @var Account $creditCard */
foreach ($creditCards as $creditCard) {
2015-05-17 02:35:49 -05:00
$balance = Steam::balance($creditCard, $end, true);
if ($balance == 0) {
// find a transfer TO the credit card which should account for
// anything paid. If not, the CC is not yet used.
2015-04-11 05:27:56 -05:00
$amount += $accountRepository->getTransfersInRange($creditCard, $start, $end)->sum('amount');
}
}
2015-04-10 00:19:45 -05:00
return Response::json(['box' => 'bills-paid', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]);
}
/**
* @param BillRepositoryInterface $repository
* @param AccountRepositoryInterface $accountRepository
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function boxBillsUnpaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
{
$amount = 0;
2015-04-11 12:59:41 -05:00
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
$bills = $repository->getActiveBills();
$unpaid = new Collection; // bills
/** @var Bill $bill */
foreach ($bills as $bill) {
2015-04-11 12:59:41 -05:00
$ranges = $repository->getRanges($bill, $start, $end);
2015-04-10 00:19:45 -05:00
foreach ($ranges as $range) {
$journals = $repository->getJournalsInRange($bill, $range['start'], $range['end']);
if ($journals->count() == 0) {
$unpaid->push([$bill, $range['start']]);
}
}
}
2015-04-11 05:27:56 -05:00
unset($bill, $bills, $range, $ranges);
$creditCards = $accountRepository->getCreditCards();
foreach ($creditCards as $creditCard) {
2015-05-17 02:35:49 -05:00
$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;
$fakeAmount = $balance * -1;
$fakeBill = $repository->createFakeBill($description, $date, $fakeAmount);
$unpaid->push([$fakeBill, $date]);
}
2015-03-06 01:20:27 -06:00
}
/** @var Bill $entry */
foreach ($unpaid as $entry) {
$current = ($entry[0]->amount_max + $entry[0]->amount_min) / 2;
$amount += $current;
}
return Response::json(['box' => 'bills-unpaid', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]);
}
/**
* @param ReportQueryInterface $reportQuery
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function boxIn(ReportQueryInterface $reportQuery)
{
2015-04-11 12:59:41 -05:00
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
2015-05-19 23:49:51 -05:00
$amount = $reportQuery->incomeInPeriodCorrected($start, $end, true)->sum('amount');
return Response::json(['box' => 'in', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]);
}
/**
* @param ReportQueryInterface $reportQuery
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function boxOut(ReportQueryInterface $reportQuery)
{
2015-04-11 12:59:41 -05:00
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
2015-05-19 23:49:51 -05:00
$amount = $reportQuery->expenseInPeriodCorrected($start, $end, true)->sum('amount');
2015-04-10 00:19:45 -05:00
return Response::json(['box' => 'out', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]);
2015-03-06 01:20:27 -06:00
}
2015-02-24 15:53:38 -06:00
/**
* Returns a list of categories.
*
2015-05-03 05:58:55 -05:00
* @param CategoryRepositoryInterface $repository
*
2015-02-24 15:53:38 -06:00
* @return \Illuminate\Http\JsonResponse
*/
2015-04-11 12:59:41 -05:00
public function categories(CategoryRepositoryInterface $repository)
2015-02-24 15:53:38 -06:00
{
2015-04-11 12:59:41 -05:00
$list = $repository->getCategories();
2015-03-06 01:20:27 -06:00
$return = [];
2015-02-24 15:53:38 -06:00
foreach ($list as $entry) {
$return[] = $entry->name;
}
2015-04-11 12:59:41 -05:00
sort($return);
2015-02-24 15:53:38 -06:00
return Response::json($return);
}
/**
* Returns a JSON list of all beneficiaries.
*
2015-05-03 05:58:55 -05:00
* @param AccountRepositoryInterface $accountRepository
*
2015-02-24 15:53:38 -06:00
* @return \Illuminate\Http\JsonResponse
*/
2015-04-11 12:59:41 -05:00
public function expenseAccounts(AccountRepositoryInterface $accountRepository)
2015-02-24 15:53:38 -06:00
{
2015-04-11 12:59:41 -05:00
$list = $accountRepository->getAccounts(['Expense account', 'Beneficiary account']);
2015-03-06 01:20:27 -06:00
$return = [];
2015-02-24 15:53:38 -06:00
foreach ($list as $entry) {
$return[] = $entry->name;
}
return Response::json($return);
}
/**
2015-05-03 05:58:55 -05:00
* @param AccountRepositoryInterface $accountRepository
*
2015-02-24 15:53:38 -06:00
* @return \Illuminate\Http\JsonResponse
*/
2015-04-11 12:59:41 -05:00
public function revenueAccounts(AccountRepositoryInterface $accountRepository)
2015-02-24 15:53:38 -06:00
{
2015-04-11 12:59:41 -05:00
$list = $accountRepository->getAccounts(['Revenue account']);
2015-03-06 01:20:27 -06:00
$return = [];
2015-02-24 15:53:38 -06:00
foreach ($list as $entry) {
$return[] = $entry->name;
}
return Response::json($return);
}
/**
* Returns a JSON list of all beneficiaries.
*
* @param TagRepositoryInterface $tagRepository
*
* @return \Illuminate\Http\JsonResponse
*/
public function tags(TagRepositoryInterface $tagRepository)
{
$list = $tagRepository->get();
$return = [];
foreach ($list as $entry) {
$return[] = $entry->tag;
}
return Response::json($return);
}
2015-04-07 11:25:21 -05:00
/**
2015-05-03 05:54:39 -05:00
* @param JournalRepositoryInterface $repository
* @param $what
2015-04-07 11:25:21 -05:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-05-03 05:54:39 -05:00
public function transactionJournals(JournalRepositoryInterface $repository, $what)
2015-03-27 07:16:14 -05:00
{
$descriptions = [];
2015-04-11 12:59:41 -05:00
$dbType = $repository->getTransactionType($what);
$journals = $repository->getJournalsOfType($dbType);
2015-03-29 00:51:56 -05:00
foreach ($journals as $j) {
$descriptions[] = $j->description;
}
2015-03-27 07:16:14 -05:00
$descriptions = array_unique($descriptions);
2015-03-27 07:25:46 -05:00
sort($descriptions);
2015-03-29 00:51:56 -05:00
2015-03-27 07:16:14 -05:00
return Response::json($descriptions);
2015-03-10 11:26:31 -05:00
}
2015-02-24 15:53:38 -06:00
}