mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-09 20:52:56 +01:00
Expense and Vendor Transformers for API
This commit is contained in:
parent
e3ee79f369
commit
f959164f2a
64
app/Http/Controllers/ExpenseApiController.php
Normal file
64
app/Http/Controllers/ExpenseApiController.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php namespace App\Http\Controllers;
|
||||
// vendor
|
||||
use App\Models\Expense;
|
||||
use app\Ninja\Repositories\ExpenseRepository;
|
||||
use App\Ninja\Transformers\ExpenseTransformer;
|
||||
use App\Services\ExpenseService;
|
||||
use Utils;
|
||||
use Response;
|
||||
use Input;
|
||||
use Auth;
|
||||
|
||||
|
||||
class ExpenseApiController extends BaseAPIController
|
||||
{
|
||||
// Expenses
|
||||
protected $expenseRepo;
|
||||
protected $expenseService;
|
||||
|
||||
public function __construct(ExpenseRepository $expenseRepo, ExpenseService $expenseService)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->expenseRepo = $expenseRepo;
|
||||
$this->expenseService = $expenseService;
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
|
||||
$expenses = Expense::scope()
|
||||
->withTrashed()
|
||||
->orderBy('created_at','desc');
|
||||
|
||||
$expenses = $expenses->paginate();
|
||||
|
||||
$transformer = new ExpenseTransformer(Auth::user()->account, Input::get('serializer'));
|
||||
$paginator = Expense::scope()->withTrashed()->paginate();
|
||||
|
||||
$data = $this->createCollection($expenses, $transformer, ENTITY_EXPENSE, $paginator);
|
||||
|
||||
return $this->response($data);
|
||||
|
||||
}
|
||||
|
||||
public function update()
|
||||
{
|
||||
//stub
|
||||
|
||||
}
|
||||
|
||||
public function store()
|
||||
{
|
||||
//stub
|
||||
|
||||
}
|
||||
|
||||
public function destroy()
|
||||
{
|
||||
//stub
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -48,6 +48,7 @@ class VendorApiController extends BaseAPIController
|
||||
{
|
||||
$vendors = Vendor::scope()
|
||||
->with($this->getIncluded())
|
||||
->withTrashed()
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate();
|
||||
|
||||
|
@ -236,6 +236,7 @@ Route::group(['middleware' => 'api', 'prefix' => 'api/v1'], function()
|
||||
Route::resource('products', 'ProductApiController');
|
||||
Route::resource('tax_rates', 'TaxRateApiController');
|
||||
Route::resource('users', 'UserApiController');
|
||||
Route::resource('expenses','ExpenseApiController');
|
||||
|
||||
// Vendor
|
||||
Route::resource('vendors', 'VendorApiController');
|
||||
|
@ -124,6 +124,11 @@ class Vendor extends EntityModel
|
||||
return $this->belongsTo('App\Models\Industry');
|
||||
}
|
||||
|
||||
public function expenses()
|
||||
{
|
||||
return $this->hasMany('App\Models\Expense','vendor_id','id');
|
||||
}
|
||||
|
||||
public function addVendorContact($data, $isPrimary = false)
|
||||
{
|
||||
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
|
||||
|
@ -41,6 +41,8 @@ class ContactMailer extends Mailer
|
||||
$client = $invoice->client;
|
||||
$account = $invoice->account;
|
||||
|
||||
$response = null;
|
||||
|
||||
if ($client->trashed()) {
|
||||
return trans('texts.email_errors.inactive_client');
|
||||
} elseif ($invoice->trashed()) {
|
||||
|
29
app/Ninja/Transformers/ExpenseTransformer.php
Normal file
29
app/Ninja/Transformers/ExpenseTransformer.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php namespace App\Ninja\Transformers;
|
||||
|
||||
use App\Models\Account;
|
||||
use App\Models\Expense;
|
||||
use League\Fractal;
|
||||
|
||||
class ExpenseTransformer extends EntityTransformer
|
||||
{
|
||||
public function transform(Expense $expense)
|
||||
{
|
||||
return [
|
||||
'id' => (int) $expense->public_id,
|
||||
'private_notes' => $expense->private_notes,
|
||||
'public_notes' => $expense->public_notes,
|
||||
'should_be_invoiced' => (bool) $expense->should_be_invoiced,
|
||||
'updated_at' => $this->getTimestamp($expense->updated_at),
|
||||
'archived_at' => $this->getTimestamp($expense->deleted_at),
|
||||
'transaction_id' => $expense->transaction_id,
|
||||
'bank_id' => $expense->bank_id,
|
||||
'expense_currency_id' => (int) $expense->expense_currency_id,
|
||||
'account_key' => $this->account->account_key,
|
||||
'amount' => (float) $expense->amount,
|
||||
'expense_date' => $expense->expense_date,
|
||||
'exchange_rate' => (float) $expense->exchange_rate,
|
||||
'invoice_currency_id' => (int) $expense->invoice_currency_id,
|
||||
'is_deleted' => (bool) $expense->is_deleted,
|
||||
];
|
||||
}
|
||||
}
|
@ -36,14 +36,15 @@ class VendorTransformer extends EntityTransformer
|
||||
*/
|
||||
|
||||
protected $availableIncludes = [
|
||||
'contacts',
|
||||
'vendorContacts',
|
||||
'invoices',
|
||||
'expenses',
|
||||
];
|
||||
|
||||
public function includeContacts(Vendor $vendor)
|
||||
public function includeVendorContacts(Vendor $vendor)
|
||||
{
|
||||
$transformer = new ContactTransformer($this->account, $this->serializer);
|
||||
return $this->includeCollection($vendor->contacts, $transformer, ENTITY_CONTACT);
|
||||
$transformer = new VendorContactTransformer($this->account, $this->serializer);
|
||||
return $this->includeCollection($vendor->vendorContacts, $transformer, ENTITY_CONTACT);
|
||||
}
|
||||
|
||||
public function includeInvoices(Vendor $vendor)
|
||||
@ -52,6 +53,12 @@ class VendorTransformer extends EntityTransformer
|
||||
return $this->includeCollection($vendor->invoices, $transformer, ENTITY_INVOICE);
|
||||
}
|
||||
|
||||
public function includeExpenses(Vendor $vendor)
|
||||
{
|
||||
$transformer = new ExpenseTransformer($this->account, $this->serializer);
|
||||
return $this->includeCollection($vendor->expenses, $transformer, ENTITY_EXPENSE);
|
||||
}
|
||||
|
||||
public function transform(Vendor $vendor)
|
||||
{
|
||||
return [
|
||||
|
Loading…
Reference in New Issue
Block a user