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

262 lines
9.5 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 Auth;
use Carbon\Carbon;
2015-03-06 01:20:27 -06:00
use DB;
use FireflyIII\Models\Account;
2015-03-06 01:20:27 -06:00
use FireflyIII\Models\Bill;
use FireflyIII\Models\Transaction;
2015-03-29 00:51:56 -05:00
use FireflyIII\Models\TransactionType;
2015-03-06 01:20:27 -06:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Input;
2015-03-10 11:26:31 -05:00
use Preferences;
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
{
/**
*
*/
public function box(BillRepositoryInterface $repository)
{
$amount = 0;
$start = Session::get('start');
$end = Session::get('end');
$box = 'empty';
switch (Input::get('box')) {
case 'in':
$box = Input::get('box');
$in = Auth::user()->transactionjournals()
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->before($end)
->after($start)
->transactionTypes(['Deposit'])
->where('transactions.amount', '>', 0)
->first([DB::Raw('SUM(transactions.amount) as `amount`')]);
if (!is_null($in)) {
$amount = floatval($in->amount);
}
break;
case 'out':
$box = Input::get('box');
$in = Auth::user()->transactionjournals()
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->before($end)
->after($start)
->transactionTypes(['Withdrawal'])
->where('transactions.amount', '>', 0)
->first([DB::Raw('SUM(transactions.amount) as `amount`')]);
if (!is_null($in)) {
$amount = floatval($in->amount);
}
2015-02-24 15:53:38 -06:00
2015-03-06 01:20:27 -06:00
break;
case 'bills-unpaid':
$box = 'bills-unpaid';
$bills = Auth::user()->bills()->where('active', 1)->get();
/** @var Bill $bill */
foreach ($bills as $bill) {
$ranges = $repository->getRanges($bill, $start, $end);
foreach ($ranges as $range) {
// paid a bill in this range?
$count = $bill->transactionjournals()->before($range['end'])->after($range['start'])->count();
if ($count == 0) {
2015-03-07 05:34:03 -06:00
$amount += floatval($bill->amount_max + $bill->amount_min / 2);
2015-03-06 01:20:27 -06:00
}
}
}
/**
* Find credit card accounts and possibly unpaid credit card bills.
*/
$creditCards = Auth::user()->accounts()
->hasMetaValue('accountRole', 'ccAsset')
->hasMetaValue('ccType', 'monthlyFull')
->get(
[
'accounts.*',
'ccType.data as ccType',
'accountRole.data as accountRole'
]
);
// if the balance is not zero, the monthly payment is still underway.
/** @var Account $creditCard */
foreach ($creditCards as $creditCard) {
$balance = Steam::balance($creditCard, null, true);
if ($balance < 0) {
// unpaid!
$amount += $balance * -1;
}
}
2015-03-10 11:26:31 -05:00
break;
2015-03-06 01:20:27 -06:00
case 'bills-paid':
$box = 'bills-paid';
// these two functions are the same as the chart TODO
$bills = Auth::user()->bills()->where('active', 1)->get();
/** @var Bill $bill */
foreach ($bills as $bill) {
$ranges = $repository->getRanges($bill, $start, $end);
foreach ($ranges as $range) {
// paid a bill in this range?
$count = $bill->transactionjournals()->before($range['end'])->after($range['start'])->count();
if ($count != 0) {
2015-03-29 14:27:51 -05:00
$journal = $bill->transactionjournals()->with('transactions')->before($range['end'])->after($range['start'])->first();
$currentAmount = 0;
2015-03-06 01:20:27 -06:00
foreach ($journal->transactions as $t) {
if (floatval($t->amount) > 0) {
$currentAmount = floatval($t->amount);
}
}
$amount += $currentAmount;
}
}
}
/**
* Find credit card accounts and possibly unpaid credit card bills.
*/
$creditCards = Auth::user()->accounts()
->hasMetaValue('accountRole', 'ccAsset')
->hasMetaValue('ccType', 'monthlyFull')
->get(
[
'accounts.*',
'ccType.data as ccType',
'accountRole.data as accountRole'
]
);
// if the balance is not zero, the monthly payment is still underway.
/** @var Account $creditCard */
foreach ($creditCards as $creditCard) {
$balance = Steam::balance($creditCard, null, 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.
$transactions = $creditCard->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->before($end)->after($start)->get();
if ($transactions->count() > 0) {
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$journal = $transaction->transactionJournal;
if ($journal->transactionType->type == 'Transfer') {
$amount += floatval($transaction->amount);
}
}
}
}
}
2015-03-06 01:20:27 -06:00
}
2015-03-10 11:26:31 -05:00
return Response::json(['box' => $box, '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.
*
* @return \Illuminate\Http\JsonResponse
*/
public function categories()
{
2015-03-06 01:20:27 -06:00
$list = Auth::user()->categories()->orderBy('name', 'ASC')->get();
$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.
*
* @return \Illuminate\Http\JsonResponse
*/
public function expenseAccounts()
{
2015-03-10 11:26:31 -05:00
$list = Auth::user()->accounts()->orderBy('accounts.name', 'ASC')->accountTypeIn(['Expense account', 'Beneficiary account'])->get();
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);
}
/**
* @return \Illuminate\Http\JsonResponse
*/
public function revenueAccounts()
{
2015-03-10 11:26:31 -05:00
$list = Auth::user()->accounts()->accountTypeIn(['Revenue account'])->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
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-03-10 11:26:31 -05:00
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-03-27 07:16:14 -05:00
public function setSharedReports()
2015-03-10 11:26:31 -05:00
{
$pref = Preferences::get('showSharedReports', false);
2015-03-27 07:16:14 -05:00
$new = !$pref->data;
Preferences::set('showSharedReports', $new);
2015-03-10 11:26:31 -05:00
2015-03-27 07:16:14 -05:00
return Response::json(['value' => $new]);
2015-03-10 11:26:31 -05:00
}
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-03-27 07:16:14 -05:00
public function showSharedReports()
2015-03-10 11:26:31 -05:00
{
$pref = Preferences::get('showSharedReports', false);
2015-03-27 07:16:14 -05:00
return Response::json(['value' => $pref->data]);
}
public function transactionJournals($what)
{
$descriptions = [];
2015-03-29 00:51:56 -05:00
$dbType = TransactionType::whereType($what)->first();
$journals = Auth::user()->transactionjournals()->where('transaction_type_id', $dbType->id)
->orderBy('id', 'DESC')->take(50)
->get();
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
}