1
0
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:
David Bomba 2016-02-17 14:32:20 +11:00
parent e3ee79f369
commit f959164f2a
7 changed files with 113 additions and 4 deletions

View 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
}
}

View File

@ -48,6 +48,7 @@ class VendorApiController extends BaseAPIController
{
$vendors = Vendor::scope()
->with($this->getIncluded())
->withTrashed()
->orderBy('created_at', 'desc')
->paginate();

View File

@ -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');

View File

@ -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;

View File

@ -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()) {

View 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,
];
}
}

View File

@ -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 [