mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-06 22:21:42 +00:00
Make sure bills API is consistent.
This commit is contained in:
@@ -67,6 +67,8 @@ class AttachmentTransformer extends TransformerAbstract
|
||||
{
|
||||
return [
|
||||
'id' => (int)$attachment->id,
|
||||
'updated_at' => $attachment->updated_at->toAtomString(),
|
||||
'created_at' => $attachment->created_at->toAtomString(),
|
||||
'attachable_type' => $attachment->attachable_type,
|
||||
'md5' => $attachment->md5,
|
||||
'filename' => $attachment->filename,
|
||||
|
||||
@@ -25,11 +25,12 @@ namespace FireflyIII\Transformers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Models\Bill;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
use League\Fractal\Resource\Item;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
|
||||
/**
|
||||
* Class BillTransformer
|
||||
@@ -41,13 +42,13 @@ class BillTransformer extends TransformerAbstract
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = ['attachments', 'notes', 'transactionJournals', 'user'];
|
||||
protected $availableIncludes = ['attachments', 'journals', 'user'];
|
||||
/**
|
||||
* List of resources to automatically include
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultIncludes = ['notes',];
|
||||
protected $defaultIncludes = [];
|
||||
/** @var Carbon */
|
||||
private $end = null;
|
||||
/** @var Carbon */
|
||||
@@ -65,18 +66,6 @@ class BillTransformer extends TransformerAbstract
|
||||
$this->end = $end;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @return \League\Fractal\Resource\Item
|
||||
*/
|
||||
public function includeUser(Bill $bill): Item
|
||||
{
|
||||
$user = $bill->user()->first();
|
||||
|
||||
return $this->item($user, new UserTransformer, 'user');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Bill $bill
|
||||
*
|
||||
@@ -86,7 +75,7 @@ class BillTransformer extends TransformerAbstract
|
||||
{
|
||||
$attachments = $bill->attachments()->get();
|
||||
|
||||
return $this->collection($attachments, new AttachmentTransformer, 'attachment');
|
||||
return $this->collection($attachments, new AttachmentTransformer, 'attachments');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,23 +83,23 @@ class BillTransformer extends TransformerAbstract
|
||||
*
|
||||
* @return FractalCollection
|
||||
*/
|
||||
public function includeNotes(Bill $bill): FractalCollection
|
||||
{
|
||||
$notes = $bill->notes()->get();
|
||||
|
||||
return $this->collection($notes, new NoteTransformer, 'note');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @return FractalCollection
|
||||
*/
|
||||
public function includeTransactionJournals(Bill $bill): FractalCollection
|
||||
public function includeJournals(Bill $bill): FractalCollection
|
||||
{
|
||||
$journals = $bill->transactionJournals()->get();
|
||||
|
||||
return $this->collection($journals, new JournalTransformer, 'transaction_journal');
|
||||
return $this->collection($journals, new TransactionJournalTransformer, 'journals');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @return \League\Fractal\Resource\Item
|
||||
*/
|
||||
public function includeUser(Bill $bill): Item
|
||||
{
|
||||
$user = $bill->user()->first();
|
||||
|
||||
return $this->item($user, new UserTransformer, 'users');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,6 +113,8 @@ class BillTransformer extends TransformerAbstract
|
||||
$payDates = $this->payDates($bill);
|
||||
$data = [
|
||||
'id' => (int)$bill->id,
|
||||
'updated_at' => $bill->updated_at->toAtomString(),
|
||||
'created_at' => $bill->created_at->toAtomString(),
|
||||
'name' => $bill->name,
|
||||
'match' => explode(',', $bill->match),
|
||||
'amount_min' => round($bill->amount_min, 2),
|
||||
@@ -137,15 +128,19 @@ class BillTransformer extends TransformerAbstract
|
||||
'pay_dates' => $payDates,
|
||||
'paid_dates' => $paidData['paid_dates'],
|
||||
'next_expected_match' => $paidData['next_expected_match'],
|
||||
'notes' => null,
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/bill/' . $bill->id,
|
||||
'uri' => '/bills/' . $bill->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
// todo updated at, created at
|
||||
/** @var Note $note */
|
||||
$note = $bill->notes()->first();
|
||||
if (!is_null($note)) {
|
||||
$data['notes'] = $note->text;
|
||||
}
|
||||
|
||||
return $data;
|
||||
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* NoteTransformer.php
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Models\Note;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
/**
|
||||
* Class NoteTransformer
|
||||
*/
|
||||
class NoteTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* @param Note $note
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Note $note): array
|
||||
{
|
||||
$converter = new CommonMarkConverter;
|
||||
|
||||
return [
|
||||
'id' => (int)$note->id,
|
||||
'notable_type' => $note->noteable_type,
|
||||
'title' => $note->title,
|
||||
'text' => $note->text,
|
||||
'markdown' => $converter->convertToHtml($note->text),
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/note/' . $note->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,15 +24,15 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Journal\JournalTaskerInterface;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
/**
|
||||
* Class JournalTransformer
|
||||
* Class TransactionJournalTransformer
|
||||
*/
|
||||
class JournalTransformer extends TransformerAbstract
|
||||
class TransactionJournalTransformer extends TransformerAbstract
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -40,13 +40,13 @@ class JournalTransformer extends TransformerAbstract
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = ['attachments', 'notes', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'meta', 'piggy_bank_events'];
|
||||
protected $availableIncludes = ['attachments', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'meta', 'piggy_bank_events'];
|
||||
/**
|
||||
* List of resources to automatically include
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultIncludes = ['transactions',];
|
||||
protected $defaultIncludes = ['transactions'];
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
@@ -55,13 +55,12 @@ class JournalTransformer extends TransformerAbstract
|
||||
*/
|
||||
public function includeTransactions(TransactionJournal $journal): FractalCollection
|
||||
{
|
||||
$tasker = app(JournalTaskerInterface::class);
|
||||
$tasker->setUser($journal->user);
|
||||
$transactions = $tasker->getTransactionsOverview($journal);
|
||||
$set = $journal->transactions()->where('amount', '<', 0)->get(['transactions.*']);
|
||||
|
||||
return $this->collection($transactions, new TransactionTransformer, 'transaction');
|
||||
return $this->collection($set, new TransactionTransformer, 'transactions');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
@@ -69,20 +68,30 @@ class JournalTransformer extends TransformerAbstract
|
||||
*/
|
||||
public function transform(TransactionJournal $journal): array
|
||||
{
|
||||
return [
|
||||
$data = [
|
||||
'id' => (int)$journal->id,
|
||||
'updated_at' => $journal->updated_at->toAtomString(),
|
||||
'created_at' => $journal->created_at->toAtomString(),
|
||||
'type' => $journal->transactionType->type,
|
||||
'description' => $journal->description,
|
||||
'date' => $journal->date->format('Y-m-d'),
|
||||
'order' => $journal->order,
|
||||
'completed' => $journal->completed,
|
||||
'notes' => null,
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/transaction_journal/' . $journal->id,
|
||||
'uri' => '/journals/' . $journal->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
/** @var Note $note */
|
||||
$note = $journal->notes()->first();
|
||||
if (!is_null($note)) {
|
||||
$data['notes'] = $note->text;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Models\Transaction;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
/**
|
||||
@@ -32,37 +33,41 @@ use League\Fractal\TransformerAbstract;
|
||||
class TransactionTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* @param array $original
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(array $original): array
|
||||
public function transform(Transaction $transaction): array
|
||||
{
|
||||
$id = $original['source_id'];
|
||||
$foreignAmount = null;
|
||||
if (!is_null($original['foreign_source_amount'])) {
|
||||
$foreignAmount = round($original['foreign_source_amount'], $original['foreign_currency_dp']);
|
||||
}
|
||||
$return = [
|
||||
'id' => $id,
|
||||
'amount' => round($original['source_amount'], $original['transaction_currency_dp']),
|
||||
'currency_id' => $original['transaction_currency_id'],
|
||||
'currency_code' => $original['transaction_currency_code'],
|
||||
'foreign_amount' => $foreignAmount,
|
||||
'foreign_currency_id' => $original['foreign_currency_id'],
|
||||
'foreign_currency_code' => $original['foreign_currency_code'],
|
||||
'description' => $original['description'],
|
||||
'links' => [
|
||||
$opposing = Transaction
|
||||
::where('transaction_journal_id', $transaction->transaction_journal_id)
|
||||
->where('identifier', $transaction->identifier)
|
||||
->where('amount', $transaction->amount * -1)
|
||||
->whereNull('deleted_at')
|
||||
->first(['transactions.*']);
|
||||
|
||||
$data = [
|
||||
'id' => (int)$transaction->id,
|
||||
'updated_at' => $transaction->updated_at->toAtomString(),
|
||||
'created_at' => $transaction->created_at->toAtomString(),
|
||||
'source_id' => (int)$transaction->account_id,
|
||||
'destination_id' => $opposing->account_id,
|
||||
'description' => $transaction->description,
|
||||
'currency_id' => (int)$transaction->transaction_currency_id,
|
||||
'amount' => (float)$transaction->amount,
|
||||
'foreign_currency_id' => is_null($transaction->foreign_currency_id) ? null : (int)$transaction->foreign_currency_id,
|
||||
'foreign_amount' => is_null($transaction->foreign_amount) ? null : (float)$transaction->foreign_amount,
|
||||
'identifier' => (int)$transaction->identifier,
|
||||
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/transaction/' . $id,
|
||||
'uri' => '/transactions/' . $transaction->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
// todo source account, dest account, budget, category
|
||||
|
||||
return $return;
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -41,11 +41,16 @@ class UserTransformer extends TransformerAbstract
|
||||
{
|
||||
|
||||
return [
|
||||
'id' => (int)$user->id,
|
||||
'links' => [
|
||||
'id' => (int)$user->id,
|
||||
'updated_at' => $user->updated_at->toAtomString(),
|
||||
'created_at' => $user->created_at->toAtomString(),
|
||||
'email' => $user->email,
|
||||
'blocked' => intval($user->blocked) === 1,
|
||||
'blocked_code' => $user->blocked_code,
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/user/' . $user->id,
|
||||
'uri' => '/users/' . $user->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user