Merge branch 'develop' of https://github.com/turbo124/invoiceninja into develop

This commit is contained in:
David Bomba 2016-05-05 07:05:00 +10:00
commit aafe14eced
12 changed files with 52 additions and 22 deletions

View File

@ -3,6 +3,7 @@
use Session; use Session;
use Utils; use Utils;
use Auth; use Auth;
use Log;
use Input; use Input;
use Response; use Response;
use Request; use Request;
@ -66,6 +67,10 @@ class BaseAPIController extends Controller
} else { } else {
$this->manager->setSerializer(new ArraySerializer()); $this->manager->setSerializer(new ArraySerializer());
} }
if (Utils::isNinjaDev()) {
\DB::enableQueryLog();
}
} }
protected function handleAction($request) protected function handleAction($request)
@ -82,7 +87,13 @@ class BaseAPIController extends Controller
protected function listResponse($query) protected function listResponse($query)
{ {
//\DB::enableQueryLog(); $transformerClass = EntityModel::getTransformerName($this->entityType);
$transformer = new $transformerClass(Auth::user()->account, Input::get('serializer'));
$include = $transformer->getDefaultIncludes();
$include = $this->getRequestIncludes($include);
$query->with($include);
if ($clientPublicId = Input::get('client_id')) { if ($clientPublicId = Input::get('client_id')) {
$filter = function($query) use ($clientPublicId) { $filter = function($query) use ($clientPublicId) {
$query->where('public_id', '=', $clientPublicId); $query->where('public_id', '=', $clientPublicId);
@ -98,12 +109,8 @@ class BaseAPIController extends Controller
} }
} }
$transformerClass = EntityModel::getTransformerName($this->entityType);
$transformer = new $transformerClass(Auth::user()->account, Input::get('serializer'));
$data = $this->createCollection($query, $transformer, $this->entityType); $data = $this->createCollection($query, $transformer, $this->entityType);
//return \DB::getQueryLog();
return $this->response($data); return $this->response($data);
} }
@ -113,7 +120,7 @@ class BaseAPIController extends Controller
$transformer = new $transformerClass(Auth::user()->account, Input::get('serializer')); $transformer = new $transformerClass(Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($item, $transformer, $this->entityType); $data = $this->createItem($item, $transformer, $this->entityType);
return $this->response($data); return $this->response($data);
} }
@ -146,6 +153,12 @@ class BaseAPIController extends Controller
protected function response($response) protected function response($response)
{ {
if (Utils::isNinjaDev()) {
$count = count(\DB::getQueryLog());
Log::info(Request::method() . ' - ' . Request::url() . ": $count queries");
//Log::info(print_r(\DB::getQueryLog(), true));
}
$index = Request::get('index') ?: 'data'; $index = Request::get('index') ?: 'data';
if ($index == 'none') { if ($index == 'none') {
@ -178,9 +191,9 @@ class BaseAPIController extends Controller
} }
protected function getIncluded() protected function getRequestIncludes($data)
{ {
$data = ['user']; $data[] = 'user';
$included = Request::get('include'); $included = Request::get('include');
$included = explode(',', $included); $included = explode(',', $included);

View File

@ -44,7 +44,6 @@ class ClientApiController extends BaseAPIController
public function index() public function index()
{ {
$clients = Client::scope() $clients = Client::scope()
->with($this->getIncluded())
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->withTrashed(); ->withTrashed();

View File

@ -61,7 +61,7 @@ class InvoiceApiController extends BaseAPIController
{ {
$invoices = Invoice::scope() $invoices = Invoice::scope()
->withTrashed() ->withTrashed()
->with(array_merge(['invoice_items'], $this->getIncluded())) ->with('invoice_items')
->orderBy('created_at', 'desc'); ->orderBy('created_at', 'desc');
return $this->listResponse($invoices); return $this->listResponse($invoices);

View File

@ -49,7 +49,7 @@ class PaymentApiController extends BaseAPIController
{ {
$payments = Payment::scope() $payments = Payment::scope()
->withTrashed() ->withTrashed()
->with(array_merge(['client.contacts', 'invitation', 'user', 'invoice'], $this->getIncluded())) ->with(['client.contacts', 'invitation', 'user', 'invoice'])
->orderBy('created_at', 'desc'); ->orderBy('created_at', 'desc');
return $this->listResponse($payments); return $this->listResponse($payments);

View File

@ -42,7 +42,6 @@ class TaskApiController extends BaseAPIController
{ {
$payments = Task::scope() $payments = Task::scope()
->withTrashed() ->withTrashed()
->with($this->getIncluded())
->orderBy('created_at', 'desc'); ->orderBy('created_at', 'desc');
return $this->listResponse($payments); return $this->listResponse($payments);

View File

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

View File

@ -58,7 +58,7 @@ class ClientTransformer extends EntityTransformer
public function includeInvoices(Client $client) public function includeInvoices(Client $client)
{ {
$transformer = new InvoiceTransformer($this->account, $this->serializer); $transformer = new InvoiceTransformer($this->account, $this->serializer, $client);
return $this->includeCollection($client->invoices, $transformer, ENTITY_INVOICE); return $this->includeCollection($client->invoices, $transformer, ENTITY_INVOICE);
} }

View File

@ -37,4 +37,9 @@ class EntityTransformer extends TransformerAbstract
{ {
return $date ? $date->getTimestamp() : null; return $date ? $date->getTimestamp() : null;
} }
public function getDefaultIncludes()
{
return $this->defaultIncludes;
}
} }

View File

@ -6,9 +6,15 @@ use League\Fractal;
class ExpenseTransformer extends EntityTransformer class ExpenseTransformer extends EntityTransformer
{ {
public function __construct($account = null, $serializer = null, $client = null)
{
parent::__construct($account, $serializer);
$this->client = $client;
}
public function transform(Expense $expense) public function transform(Expense $expense)
{ {
return [ return [
'id' => (int) $expense->public_id, 'id' => (int) $expense->public_id,
'private_notes' => $expense->private_notes, 'private_notes' => $expense->private_notes,
@ -25,7 +31,7 @@ class ExpenseTransformer extends EntityTransformer
'exchange_rate' => (float) $expense->exchange_rate, 'exchange_rate' => (float) $expense->exchange_rate,
'invoice_currency_id' => (int) $expense->invoice_currency_id, 'invoice_currency_id' => (int) $expense->invoice_currency_id,
'is_deleted' => (bool) $expense->is_deleted, 'is_deleted' => (bool) $expense->is_deleted,
'client_id' => isset($expense->client->public_id) ? (int) $expense->client->public_id : null, 'client_id' => $this->client ? $this->client->public_id : (isset($expense->client->public_id) ? (int) $expense->client->public_id : null),
'invoice_id' => isset($expense->invoice->public_id) ? (int) $expense->invoice->public_id : null, 'invoice_id' => isset($expense->invoice->public_id) ? (int) $expense->invoice->public_id : null,
'vendor_id' => isset($expense->vendor->public_id) ? (int) $expense->vendor->public_id : null, 'vendor_id' => isset($expense->vendor->public_id) ? (int) $expense->vendor->public_id : null,
]; ];

View File

@ -31,6 +31,13 @@ class InvoiceTransformer extends EntityTransformer
'expenses', 'expenses',
]; ];
public function __construct($account = null, $serializer = null, $client = null)
{
parent::__construct($account, $serializer);
$this->client = $client;
}
public function includeInvoiceItems(Invoice $invoice) public function includeInvoiceItems(Invoice $invoice)
{ {
$transformer = new InvoiceItemTransformer($this->account, $this->serializer); $transformer = new InvoiceItemTransformer($this->account, $this->serializer);
@ -45,7 +52,7 @@ class InvoiceTransformer extends EntityTransformer
public function includePayments(Invoice $invoice) public function includePayments(Invoice $invoice)
{ {
$transformer = new PaymentTransformer($this->account, $this->serializer); $transformer = new PaymentTransformer($this->account, $this->serializer, $invoice);
return $this->includeCollection($invoice->payments, $transformer, ENTITY_PAYMENT); return $this->includeCollection($invoice->payments, $transformer, ENTITY_PAYMENT);
} }
@ -68,7 +75,7 @@ class InvoiceTransformer extends EntityTransformer
'id' => (int) $invoice->public_id, 'id' => (int) $invoice->public_id,
'amount' => (float) $invoice->amount, 'amount' => (float) $invoice->amount,
'balance' => (float) $invoice->balance, 'balance' => (float) $invoice->balance,
'client_id' => (int) $invoice->client->public_id, 'client_id' => (int) ($this->client ? $this->client->public_id : $invoice->client->public_id),
'invoice_status_id' => (int) $invoice->invoice_status_id, 'invoice_status_id' => (int) $invoice->invoice_status_id,
'updated_at' => $this->getTimestamp($invoice->updated_at), 'updated_at' => $this->getTimestamp($invoice->updated_at),
'archived_at' => $this->getTimestamp($invoice->deleted_at), 'archived_at' => $this->getTimestamp($invoice->deleted_at),

View File

@ -26,10 +26,11 @@ class PaymentTransformer extends EntityTransformer
]; ];
public function __construct(Account $account) public function __construct($account = null, $serializer = null, $invoice = null)
{ {
parent::__construct($account); parent::__construct($account, $serializer);
$this->invoice = $invoice;
} }
public function includeInvoice(Payment $payment) public function includeInvoice(Payment $payment)
@ -57,7 +58,7 @@ class PaymentTransformer extends EntityTransformer
'archived_at' => $this->getTimestamp($payment->deleted_at), 'archived_at' => $this->getTimestamp($payment->deleted_at),
'is_deleted' => (bool) $payment->is_deleted, 'is_deleted' => (bool) $payment->is_deleted,
'payment_type_id' => (int) $payment->payment_type_id, 'payment_type_id' => (int) $payment->payment_type_id,
'invoice_id' => (int) $payment->invoice->public_id, 'invoice_id' => (int) ($this->invoice ? $this->invoice->public_id : $payment->invoice->public_id),
]; ];
} }
} }

View File

@ -48,6 +48,7 @@ class ExpenseCest
// invoice expense // invoice expense
$I->executeJS('submitAction(\'invoice\')'); $I->executeJS('submitAction(\'invoice\')');
$I->wait(3);
$I->click('Save'); $I->click('Save');
$I->wait(3); $I->wait(3);
$I->see($clientEmail); $I->see($clientEmail);