More support for #142

This commit is contained in:
James Cole 2016-05-15 09:00:49 +02:00
parent 446ab62d38
commit 626404407e
9 changed files with 697 additions and 443 deletions

View File

@ -38,6 +38,7 @@ class ReportController extends Controller
/**
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
* @throws FireflyException
*/
public function info(Request $request)

View File

@ -436,7 +436,6 @@ class TransactionController extends Controller
->orderBy('amount', 'ASC')->first(
['transactions.*', DB::raw('SUM(`transactions`.`amount`) as `sum`')]
);
$final->description = '';
$transactions->push($final);
break;
case TransactionType::WITHDRAWAL:
@ -453,7 +452,6 @@ class TransactionController extends Controller
->orderBy('amount', 'ASC')->first(
['transactions.*', DB::raw('SUM(`transactions`.`amount`) as `sum`')]
);
$final->description = '';
$transactions->push($final);
break;
default:

View File

@ -61,7 +61,6 @@ class TestData
*/
private function createAccounts()
{
if (isset($this->data['accounts']) && is_array($this->data['accounts'])) {
$insert = [];
foreach ($this->data['accounts'] as $account) {
$insert[] = [
@ -77,8 +76,6 @@ class TestData
];
}
DB::table('accounts')->insert($insert);
}
if (isset($this->data['account-meta']) && is_array($this->data['account-meta'])) {
$insert = [];
foreach ($this->data['account-meta'] as $meta) {
$insert[] = [
@ -91,14 +88,12 @@ class TestData
}
DB::table('account_meta')->insert($insert);
}
}
/**
*
*/
private function createAttachments()
{
if (isset($this->data['attachments']) && is_array($this->data['attachments'])) {
$insert = [];
$disk = Storage::disk('upload');
foreach ($this->data['attachments'] as $attachment) {
@ -124,14 +119,12 @@ class TestData
$disk->put('at-' . $attachmentId . '.data', $data);
}
}
}
/**
*
*/
private function createBills()
{
if (isset($this->data['bills']) && is_array($this->data['bills'])) {
$insert = [];
foreach ($this->data['bills'] as $bill) {
$insert[] = [
@ -153,14 +146,12 @@ class TestData
}
DB::table('bills')->insert($insert);
}
}
/**
*
*/
private function createBudgets()
{
if (isset($this->data['budgets']) && is_array($this->data['budgets'])) {
$insert = [];
foreach ($this->data['budgets'] as $budget) {
$insert[] = [
@ -172,9 +163,7 @@ class TestData
];
}
DB::table('budgets')->insert($insert);
}
if (isset($this->data['budget-limits']) && is_array($this->data['budget-limits'])) {
foreach ($this->data['budget-limits'] as $limit) {
$amount = rand($limit['amount_min'], $limit['amount_max']);
$limitId = DB::table('budget_limits')->insertGetId(
@ -200,8 +189,6 @@ class TestData
]
);
}
}
if (isset($this->data['monthly-limits']) && is_array($this->data['monthly-limits'])) {
$current = clone $this->start;
while ($current <= $this->end) {
foreach ($this->data['monthly-limits'] as $limit) {
@ -234,14 +221,12 @@ class TestData
}
}
}
/**
*
*/
private function createCategories()
{
if (isset($this->data['categories']) && is_array($this->data['categories'])) {
$insert = [];
foreach ($this->data['categories'] as $category) {
$insert[] = [
@ -254,7 +239,6 @@ class TestData
}
DB::table('categories')->insert($insert);
}
}
/**
*
@ -268,7 +252,6 @@ class TestData
$month = $current->format('F');
// run all monthly withdrawals:
if (isset($this->data['monthly-withdrawals']) && is_array($this->data['monthly-withdrawals'])) {
foreach ($this->data['monthly-withdrawals'] as $withdrawal) {
$description = str_replace(':month', $month, $withdrawal['description']);
$journalId = DB::table('transaction_journals')->insertGetId(
@ -327,10 +310,8 @@ class TestData
);
}
}
}
// run all monthly deposits:
if (isset($this->data['monthly-deposits']) && is_array($this->data['monthly-deposits'])) {
foreach ($this->data['monthly-deposits'] as $deposit) {
$description = str_replace(':month', $month, $deposit['description']);
$journalId = DB::table('transaction_journals')->insertGetId(
@ -379,10 +360,7 @@ class TestData
);
}
}
}
// run all monthly transfers:
if (isset($this->data['monthly-transfers']) && is_array($this->data['monthly-transfers'])) {
foreach ($this->data['monthly-transfers'] as $transfer) {
$description = str_replace(':month', $month, $transfer['description']);
$journalId = DB::table('transaction_journals')->insertGetId(
@ -430,7 +408,6 @@ class TestData
);
}
}
}
$current->addMonth();
}
@ -438,12 +415,92 @@ class TestData
DB::table('transactions')->insert($transactions);
}
/**
*
*/
private function createMultiWithdrawals()
{
foreach ($this->data['multi-withdrawals'] as $withdrawal) {
$journalId = DB::table('transaction_journals')->insertGetId(
[
'created_at' => DB::raw('NOW()'),
'updated_at' => DB::raw('NOW()'),
'user_id' => $withdrawal['user_id'],
'transaction_type_id' => 1,
'transaction_currency_id' => 1,
'description' => Crypt::encrypt($withdrawal['description']),
'completed' => 1,
'date' => $withdrawal['date'],
'interest_date' => $withdrawal['interest_date'] ?? null,
'book_date' => $withdrawal['book_date'] ?? null,
'process_date' => $withdrawal['process_date'] ?? null,
'encrypted' => 1,
'order' => 0,
'tag_count' => 0,
]
);
foreach ($withdrawal['destination_ids'] as $index => $destination) {
$description = $withdrawal['description'] . ' (#' . ($index + 1) . ')';
$amount = $withdrawal['amounts'][$index];
$first = DB::table('transactions')->insertGetId(
[
'created_at' => DB::raw('NOW()'),
'updated_at' => DB::raw('NOW()'),
'account_id' => $withdrawal['source_id'],
'transaction_journal_id' => $journalId,
'description' => $description,
'amount' => $amount * -1,
]
);
$second = DB::table('transactions')->insertGetId(
[
'created_at' => DB::raw('NOW()'),
'updated_at' => DB::raw('NOW()'),
'account_id' => $destination,
'transaction_journal_id' => $journalId,
'description' => $description,
'amount' => $amount,
]
);
// link first and second to budget and category, if present.
if (isset($withdrawal['budget_ids'][$index])) {
DB::table('budget_transaction')->insert(
[
'budget_id' => $withdrawal['budget_ids'][$index],
'transaction_id' => $first,
]
);
DB::table('budget_transaction')->insert(
[
'budget_id' => $withdrawal['budget_ids'][$index],
'transaction_id' => $second,
]
);
}
if (isset($withdrawal['category_ids'][$index])) {
DB::table('category_transaction')->insert(
[
'category_id' => $withdrawal['category_ids'][$index],
'transaction_id' => $first,
]
);
DB::table('category_transaction')->insert(
[
'category_id' => $withdrawal['category_ids'][$index],
'transaction_id' => $second,
]
);
}
}
}
}
/**
*
*/
private function createPiggyBanks()
{
if (isset($this->data['piggy-banks']) && is_array($this->data['piggy-banks'])) {
foreach ($this->data['piggy-banks'] as $piggyBank) {
$piggyId = DB::table('piggy_banks')->insertGetId(
[
@ -472,14 +529,12 @@ class TestData
}
}
}
}
/**
*
*/
private function createRules()
{
if (isset($this->data['rule-groups']) && is_array($this->data['rule-groups'])) {
$insert = [];
foreach ($this->data['rule-groups'] as $group) {
$insert[] = [
@ -493,8 +548,6 @@ class TestData
];
}
DB::table('rule_groups')->insert($insert);
}
if (isset($this->data['rules']) && is_array($this->data['rules'])) {
$insert = [];
foreach ($this->data['rules'] as $rule) {
$insert[] = [
@ -510,9 +563,7 @@ class TestData
];
}
DB::table('rules')->insert($insert);
}
if (isset($this->data['rule-triggers']) && is_array($this->data['rule-triggers'])) {
$insert = [];
foreach ($this->data['rule-triggers'] as $trigger) {
$insert[] = [
@ -527,8 +578,7 @@ class TestData
];
}
DB::table('rule_triggers')->insert($insert);
}
if (isset($this->data['rule-actions']) && is_array($this->data['rule-actions'])) {
$insert = [];
foreach ($this->data['rule-actions'] as $action) {
$insert[] = [
@ -544,14 +594,12 @@ class TestData
}
DB::table('rule_actions')->insert($insert);
}
}
/**
*
*/
private function createTags()
{
if (isset($this->data['tags']) && is_array($this->data['tags'])) {
$insert = [];
foreach ($this->data['tags'] as $tag) {
$insert[]
@ -567,14 +615,12 @@ class TestData
}
DB::table('tags')->insert($insert);
}
}
/**
*
*/
private function createUsers()
{
if (isset($this->data['users']) && is_array($this->data['users'])) {
$insert = [];
foreach ($this->data['users'] as $user) {
$insert[]
@ -587,8 +633,6 @@ class TestData
}
DB::table('users')->insert($insert);
}
if (isset($this->data['roles']) && is_array($this->data['roles'])) {
$insert = [];
foreach ($this->data['roles'] as $role) {
$insert[]
@ -599,7 +643,6 @@ class TestData
}
DB::table('role_user')->insert($insert);
}
}
/**
*
@ -616,6 +659,7 @@ class TestData
$this->createTags();
$this->createJournals();
$this->createAttachments();
$this->createMultiWithdrawals();
}
}

View File

@ -6,6 +6,7 @@ namespace FireflyIII\Support\Twig;
use Amount;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\CacheProperties;
use Twig_Extension;
@ -19,6 +20,7 @@ use Twig_SimpleFunction;
*/
class Journal extends Twig_Extension
{
/**
* @return Twig_SimpleFunction
*/
@ -95,6 +97,10 @@ class Journal extends Twig_Extension
$this->getSourceAccount(),
$this->getDestinationAccount(),
$this->formatPerspective(),
$this->journalBudgets(),
$this->journalCategories(),
$this->transactionBudgets(),
$this->transactionCategories(),
];
return $functions;
@ -147,6 +153,143 @@ class Journal extends Twig_Extension
);
}
/**
* @return Twig_SimpleFunction
*/
public function journalBudgets(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'journalBudgets', function (TransactionJournal $journal): string {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('budget-string');
if ($cache->has()) {
//return $cache->get();
}
$budgets = [];
// get all budgets:
foreach ($journal->budgets as $budget) {
$budgets[] = '<a href="' . route('budgets.show', [$budget->id]) . '" title="' . e($budget->name) . '">' . e($budget->name) . '</a>';
}
// and more!
foreach ($journal->transactions as $transaction) {
foreach ($transaction->budgets as $budget) {
$budgets[] = '<a href="' . route('budgets.show', [$budget->id]) . '" title="' . e($budget->name) . '">' . e($budget->name) . '</a>';
}
}
$string = join(', ', array_unique($budgets));
$cache->store($string);
return $string;
}
);
}
/**
* @return Twig_SimpleFunction
*/
public function journalCategories(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'journalCategories', function (TransactionJournal $journal): string {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('category-string');
if ($cache->has()) {
return $cache->get();
}
$categories = [];
// get all budgets:
foreach ($journal->categories as $category) {
$categories[] = '<a href="' . route('categories.show', [$category->id]) . '" title="' . e($category->name) . '">' . e($category->name) . '</a>';
}
// and more!
foreach ($journal->transactions as $transaction) {
foreach ($transaction->categories as $category) {
$categories[] = '<a href="' . route('categories.show', [$category->id]) . '" title="' . e($category->name) . '">' . e($category->name)
. '</a>';
}
}
$string = join(', ', array_unique($categories));
$cache->store($string);
return $string;
}
);
}
/**
* @return Twig_SimpleFunction
*/
public function transactionBudgets(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'transactionBudgets', function (Transaction $transaction): string {
$cache = new CacheProperties;
$cache->addProperty($transaction->id);
$cache->addProperty('transaction');
$cache->addProperty('budget-string');
if ($cache->has()) {
// return $cache->get();
}
$budgets = [];
// get all budgets:
foreach ($transaction->budgets as $budget) {
$budgets[] = '<a href="' . route('budgets.show', [$budget->id]) . '" title="' . e($budget->name) . '">' . e($budget->name) . '</a>';
}
$string = join(', ', array_unique($budgets));
$cache->store($string);
return $string;
}
);
}
/**
* @return Twig_SimpleFunction
*/
public function transactionCategories(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'transactionCategories', function (Transaction $transaction): string {
$cache = new CacheProperties;
$cache->addProperty($transaction->id);
$cache->addProperty('transaction');
$cache->addProperty('category-string');
if ($cache->has()) {
// return $cache->get();
}
$categories = [];
// get all budgets:
foreach ($transaction->categories as $category) {
$categories[] = '<a href="' . route('categories.show', [$category->id]) . '" title="' . e($category->name) . '">' . e($category->name) . '</a>';
}
$string = join(', ', array_unique($categories));
$cache->store($string);
return $string;
}
);
}
/**
* @return Twig_SimpleFilter
*/

View File

@ -76,18 +76,14 @@
<!-- Do NOT hide the budget? -->
{% if not hideBudgets %}
<td class="hidden-xs">
{% if journal.budgets[0] %}
<a href="{{ route('budgets.show',journal.budgets[0].id) }}">{{ journal.budgets[0].name }}</a>
{% endif %}
{{ journalBudgets(journal)|raw }}
</td>
{% endif %}
<!-- Do NOT hide the category? -->
{% if not hideCategories %}
<td class="hidden-xs">
{% if journal.categories[0] %}
<a href="{{ route('categories.show',journal.categories[0].id) }}">{{ journal.categories[0].name }}</a>
{% endif %}
{{ journalCategories(journal)|raw }}
</td>
{% endif %}

View File

@ -62,15 +62,14 @@
<!-- Do NOT hide the budget? -->
{% if not hideBudget %}
<td class="hidden-xs">
{% if journal.budgets[0] %}
<a href="{{ route('budgets.show',journal.budgets[0].id) }}">{{ journal.budgets[0].name }}</a>
{% endif %}
{{ journalBudgets(journal)|raw }}
</td>
{% endif %}
<!-- Do NOT hide the category? -->
{% if not hideCategory %}
<td class="hidden-xs">
{{ journalCategories(journal)|raw }}
{% if journal.categories[0] %}
<a href="{{ route('categories.show',journal.categories[0].id) }}">{{ journal.categories[0].name }}</a>
{% endif %}

View File

@ -62,17 +62,12 @@
{% endif %}
</td>
{% if journal.budgets[0] %}
<td class="hide-budget"><a href="{{ route('budgets.show',journal.budgets[0].id) }}">{{ journal.budgets[0].name }}</a></td>
{% else %}
<td class="hide-budget"><em>{{ 'no_budget'|_ }}</em></td>
{% endif %}
{% if journal.categories[0] %}
<td class="hide-category"><a href="{{ route('categories.show',journal.categories[0].id) }}">{{ journal.categories[0].name }}</a></td>
{% else %}
<td class="hide-category"><em>{{ 'no_category'|_ }}</em></td>
{% endif %}
<td class="hide-budget">
{{ journalBudgets(journal)|raw }}
</td>
<td class="hide-category">
{{ journalCategories(journal)|raw }}
</td>
{% if journal.bill_id %}
<td class="hide-bill"><i class="fa fa-fw fa-rotate-right" title="{{ trans('list.bill') }}"></i>&nbsp;<a
href="{{ route('bills.show',journal.bill_id) }}">{{ journal.bill.name }}</a></td>

View File

@ -172,7 +172,7 @@
</tr>
<tr>
<td>{{ 'amount'|_ }}</td>
<td>{{ t.before|formatAmount}}</td>
<td>{{ t.before|formatAmount }}</td>
</tr>
<tr>
<td>{{ 'newBalance'|_ }}</td>
@ -184,22 +184,18 @@
<td>{{ t.description }}</td>
</tr>
{% endif %}
{% if t.categories[0] %}
<tr>
<td>{{ 'category'|_ }}</td>
<td>
<a href="{{ route('categories.show',t.categories[0].id) }}">{{ t.categories[0].name }}</a>
{{ transactionCategories(t)|raw }}
</td>
</tr>
{% endif %}
{% if t.budgets[0] %}
<tr>
<td>{{ 'budget'|_ }}</td>
<td>
<a href="{{ route('budgets.show',t.budgets[0].id) }}">{{ t.budgets[0].name }}</a>
{{ transactionBudgets(t)|raw }}
</td>
</tr>
{% endif %}
</table>
</div>
{% endfor %}
@ -249,7 +245,7 @@
</div>
</div>
<!-- more than two start-->
<!-- more than two transactions-->
{% if transactions.count > 2 %}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
@ -270,20 +266,24 @@
</tr>
</thead>
<tbody>
{% for t in transactions %}
{% for index, t in transactions %}
<tr>
<td>{{ t.description }}</td>
<td>
{% if (index+1) != transactions|length %}
{{ t.description }}
{% endif %}
</td>
<td><a href="{{ route('accounts.show',t.account.id) }}">{{ t.account.name }}</a> ({{ t.account.accounttype.type|_ }})</td>
<td>{{ t.sum|formatAmount }}</td>
<td>{{ t.before|formatAmount }} &rarr; {{ (t.sum+t.before)|formatAmount }}</td>
<td>
{% if t.budgets[0] %}
<a href="{{ route('budgets.show',t.budgets[0].id) }}">{{ t.budgets[0].name }}</a>
{% if (index+1) != transactions|length %}
{{ transactionBudgets(t)|raw }}
{% endif %}
</td>
<td>
{% if t.categories[0] %}
<a href="{{ route('categories.show',t.categories[0].id) }}">{{ t.categories[0].name }}</a>
{% if (index+1) != transactions|length %}
{{ transactionCategories(t)|raw }}
{% endif %}
</td>

View File

@ -293,6 +293,18 @@
{
"name": "Going out",
"user_id": 1
},
{
"name": "Multi budget A",
"user_id": 1
},
{
"name": "Multi budget B",
"user_id": 1
},
{
"name": "Multi budget C",
"user_id": 1
}
],
"budget-limits": [
@ -385,6 +397,18 @@
{
"name": "Going out",
"user_id": 1
},
{
"name": "Multi category A",
"user_id": 1
},
{
"name": "Multi category B",
"user_id": 1
},
{
"name": "Multi category C",
"user_id": 1
}
],
"piggy-banks": [
@ -840,5 +864,59 @@
"mime": "text\/plain",
"uploaded": 1
}
],
"multi-withdrawals": [
{
"user_id": 1,
"date": "2016-03-12",
"description": "Even multi-withdrawal (50, 50)",
"destination_ids": [
18,
19
],
"source_id": 1,
"amounts": [
50,
50
],
"category_ids": [
7,
8,
9
],
"budget_ids": [
7,
8,
9
]
},
{
"user_id": 1,
"date": "2016-05-12",
"description": "Uneven multi-withdrawal (15,34,51)",
"destination_ids": [
18,
19,
20
],
"source_id": 1,
"amounts": [
14,
35,
51
],
"category_ids": [
7,
8,
9
],
"budget_ids": [
7,
8,
9
]
}
],
"multi-deposits": [],
"multi-transfers": []
}