Working on the API

This commit is contained in:
Hillel Coren 2015-12-27 13:08:58 +02:00
parent ddd5915ca1
commit c493f6e49a
15 changed files with 65 additions and 81 deletions

View File

@ -60,8 +60,36 @@ class AccountApiController extends BaseAPIController
public function show(Request $request) public function show(Request $request)
{ {
$account = Auth::user()->account; $account = Auth::user()->account;
$account->loadAllData(); $updatedAt = $request->updated_at ? date('Y-m-d H:i:s', $request->updated_at) : false;
if ($updatedAt) {
$account->load(['users' => function($query) use ($updatedAt) {
$query->where('updated_at', '>=', $updatedAt);
}])
->load(['clients' => function($query) use ($updatedAt) {
$query->where('updated_at', '>=', $updatedAt)->with('contacts');
}])
->load(['invoices' => function($query) use ($updatedAt) {
$query->where('updated_at', '>=', $updatedAt)->with('invoice_items', 'user', 'client');
}])
->load(['products' => function($query) use ($updatedAt) {
$query->where('updated_at', '>=', $updatedAt);
}])
->load(['tax_rates' => function($query) use ($updatedAt) {
$query->where('updated_at', '>=', $updatedAt);
}]);
} else {
$account->load(
'users',
'clients.contacts',
'invoices.invoice_items',
'invoices.user',
'invoices.client',
'products',
'tax_rates'
);
}
$transformer = new AccountTransformer(null, $request->serializer); $transformer = new AccountTransformer(null, $request->serializer);
$account = $this->createItem($account, $transformer, 'account'); $account = $this->createItem($account, $transformer, 'account');

View File

@ -50,7 +50,8 @@ class InvoiceApiController extends BaseAPIController
$paginator = Invoice::scope(); $paginator = Invoice::scope();
$invoices = Invoice::scope() $invoices = Invoice::scope()
->with(array_merge(['invoice_items'], $this->getIncluded())) ->with(array_merge(['invoice_items'], $this->getIncluded()))
->where('invoices.is_quote', '=', false); ->where('invoices.is_quote', '=', false)
->where('invoices.is_recurring', '=', false);
if ($clientPublicId = Input::get('client_id')) { if ($clientPublicId = Input::get('client_id')) {
$filter = function($query) use ($clientPublicId) { $filter = function($query) use ($clientPublicId) {

View File

@ -17,14 +17,8 @@ use App\Models\Invoice;
use App\Models\Client; use App\Models\Client;
use App\Models\Account; use App\Models\Account;
use App\Models\Product; use App\Models\Product;
use App\Models\Country;
use App\Models\TaxRate; use App\Models\TaxRate;
use App\Models\Currency;
use App\Models\Size;
use App\Models\Industry;
use App\Models\PaymentTerm;
use App\Models\InvoiceDesign; use App\Models\InvoiceDesign;
use App\Models\AccountGateway;
use App\Models\Activity; use App\Models\Activity;
use App\Models\Gateway; use App\Models\Gateway;
use App\Ninja\Mailers\ContactMailer as Mailer; use App\Ninja\Mailers\ContactMailer as Mailer;

View File

@ -92,7 +92,7 @@ class PublicClientController extends BaseController
public function invoiceDatatable() public function invoiceDatatable()
{ {
if (!$invitation = $this->getInvitation()) { if (!$invitation = $this->getInvitation()) {
return false; return '';
} }
return $this->invoiceRepo->getClientDatatable($invitation->contact_id, ENTITY_INVOICE, Input::get('sSearch')); return $this->invoiceRepo->getClientDatatable($invitation->contact_id, ENTITY_INVOICE, Input::get('sSearch'));

View File

@ -515,11 +515,6 @@ class Account extends Eloquent
$this->save(); $this->save();
} }
public function loadAllData()
{
$this->load('clients.getInvoices.invoice_items', 'clients.getQuotes.invoice_items', 'users', 'clients.contacts');
}
public function loadLocalizationSettings($client = false) public function loadLocalizationSettings($client = false)
{ {
$this->load('timezone', 'date_format', 'datetime_format', 'language'); $this->load('timezone', 'date_format', 'datetime_format', 'language');

View File

@ -101,27 +101,6 @@ class Client extends EntityModel
return $this->hasMany('App\Models\Invoice'); return $this->hasMany('App\Models\Invoice');
} }
public function getInvoices()
{
return $this->hasMany('App\Models\Invoice')
->where('is_quote', '=', false)
->where('is_recurring', '=', false);
}
public function getRecurringInvoices()
{
return $this->hasMany('App\Models\Invoice')
->where('is_quote', '=', false)
->where('is_recurring', '=', true);
}
public function getQuotes()
{
return $this->hasMany('App\Models\Invoice')
->where('is_quote', '=', true)
->where('is_recurring', '=', false);
}
public function payments() public function payments()
{ {
return $this->hasMany('App\Models\Payment'); return $this->hasMany('App\Models\Payment');

View File

@ -388,21 +388,17 @@ class InvoiceRepository extends BaseRepository
} }
if ($item['product_key']) { if ($item['product_key']) {
if (!\Auth::user()->account->update_products) {
continue;
}
$productKey = trim($item['product_key']); $productKey = trim($item['product_key']);
if (strtotime($productKey)) { if (\Auth::user()->account->update_products && ! strtotime($productKey)) {
continue; $product = Product::findProductByKey($productKey);
if (!$product) {
$product = Product::createNew();
$product->product_key = trim($item['product_key']);
}
$product->notes = $invoice->has_tasks ? '' : $item['notes'];
$product->cost = $item['cost'];
$product->save();
} }
$product = Product::findProductByKey($productKey);
if (!$product) {
$product = Product::createNew();
$product->product_key = trim($item['product_key']);
}
$product->notes = $invoice->has_tasks ? '' : $item['notes'];
$product->cost = $item['cost'];
$product->save();
} }
$invoiceItem = InvoiceItem::createNew(); $invoiceItem = InvoiceItem::createNew();

View File

@ -14,7 +14,6 @@ class AccountTransformer extends EntityTransformer
'users', 'users',
// 'clients', // 'clients',
'invoices', 'invoices',
'contacts',
'products', 'products',
'taxRates' 'taxRates'
]; ];
@ -37,12 +36,6 @@ class AccountTransformer extends EntityTransformer
return $this->includeCollection($account->invoices, $transformer, 'invoices'); return $this->includeCollection($account->invoices, $transformer, 'invoices');
} }
public function includeContacts(Account $account)
{
$transformer = new ContactTransformer($account, $this->serializer);
return $this->includeCollection($account->contacts, $transformer, 'contacts');
}
public function includeProducts(Account $account) public function includeProducts(Account $account)
{ {
$transformer = new ProductTransformer($account, $this->serializer); $transformer = new ProductTransformer($account, $this->serializer);
@ -55,7 +48,6 @@ class AccountTransformer extends EntityTransformer
return $this->includeCollection($account->tax_rates, $transformer, 'taxRates'); return $this->includeCollection($account->tax_rates, $transformer, 'taxRates');
} }
public function transform(Account $account) public function transform(Account $account)
{ {
return [ return [
@ -65,8 +57,8 @@ class AccountTransformer extends EntityTransformer
'timezone_id' => (int) $account->timezone_id, 'timezone_id' => (int) $account->timezone_id,
'date_format_id' => (int) $account->date_format_id, 'date_format_id' => (int) $account->date_format_id,
'datetime_format_id' => (int) $account->datetime_format_id, 'datetime_format_id' => (int) $account->datetime_format_id,
'updated_at' => $account->updated_at, 'updated_at' => $this->getTimestamp($account->updated_at),
'deleted_at' => $account->deleted_at, 'archived_at' => $this->getTimestamp($account->deleted_at),
'address1' => $account->address1, 'address1' => $account->address1,
'address2' => $account->address2, 'address2' => $account->address2,
'city' => $account->city, 'city' => $account->city,

View File

@ -17,8 +17,8 @@ class ClientTransformer extends EntityTransformer
* @SWG\Property(property="paid_to_date", type="float", example=10, readOnly=true) * @SWG\Property(property="paid_to_date", type="float", example=10, readOnly=true)
* @SWG\Property(property="user_id", type="integer", example=1) * @SWG\Property(property="user_id", type="integer", example=1)
* @SWG\Property(property="account_key", type="string", example="123456") * @SWG\Property(property="account_key", type="string", example="123456")
* @SWG\Property(property="updated_at", type="date-time", example="2016-01-01 12:10:00") * @SWG\Property(property="updated_at", type="timestamp", example="")
* @SWG\Property(property="deleted_at", type="date-time", example="2016-01-01 12:10:00") * @SWG\Property(property="archived_at", type="timestamp", example="1451160233")
* @SWG\Property(property="address1", type="string", example="10 Main St.") * @SWG\Property(property="address1", type="string", example="10 Main St.")
* @SWG\Property(property="address2", type="string", example="1st Floor") * @SWG\Property(property="address2", type="string", example="1st Floor")
* @SWG\Property(property="city", type="string", example="New York") * @SWG\Property(property="city", type="string", example="New York")
@ -43,7 +43,6 @@ class ClientTransformer extends EntityTransformer
protected $availableIncludes = [ protected $availableIncludes = [
'contacts', 'contacts',
'invoices', 'invoices',
'quotes',
]; ];
public function includeContacts(Client $client) public function includeContacts(Client $client)
@ -55,13 +54,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);
return $this->includeCollection($client->getInvoices, $transformer, ENTITY_INVOICE); return $this->includeCollection($client->invoices, $transformer, ENTITY_INVOICE);
}
public function includeQuotes(Client $client)
{
$transformer = new QuoteTransformer($this->account, $this->serializer);
return $this->includeCollection($client->getQuotes, $transformer, ENTITY_QUOTE);
} }
public function transform(Client $client) public function transform(Client $client)
@ -73,8 +66,8 @@ class ClientTransformer extends EntityTransformer
'paid_to_date' => (float) $client->paid_to_date, 'paid_to_date' => (float) $client->paid_to_date,
'user_id' => (int) $client->user->public_id + 1, 'user_id' => (int) $client->user->public_id + 1,
'account_key' => $this->account->account_key, 'account_key' => $this->account->account_key,
'updated_at' => $client->updated_at, 'updated_at' => $this->getTimestamp($client->updated_at),
'deleted_at' => $client->deleted_at, 'archived_at' => $this->getTimestamp($client->deleted_at),
'address1' => $client->address1, 'address1' => $client->address1,
'address2' => $client->address2, 'address2' => $client->address2,
'city' => $client->city, 'city' => $client->city,

View File

@ -13,8 +13,8 @@ class ContactTransformer extends EntityTransformer
'first_name' => $contact->first_name, 'first_name' => $contact->first_name,
'last_name' => $contact->last_name, 'last_name' => $contact->last_name,
'email' => $contact->email, 'email' => $contact->email,
'updated_at' => $contact->updated_at, 'updated_at' => $this->getTimestamp($contact->updated_at),
'deleted_at' => $contact->deleted_at, 'archived_at' => $this->getTimestamp($contact->deleted_at),
'is_primary' => (bool) $contact->is_primary, 'is_primary' => (bool) $contact->is_primary,
'phone' => $contact->phone, 'phone' => $contact->phone,
'last_login' => $contact->last_login, 'last_login' => $contact->last_login,

View File

@ -32,4 +32,9 @@ class EntityTransformer extends TransformerAbstract
return $this->item($data, $transformer, $entityType); return $this->item($data, $transformer, $entityType);
} }
protected function getTimestamp($date)
{
return $date ? $date->getTimestamp() : null;
}
} }

View File

@ -13,8 +13,8 @@ class InvoiceItemTransformer extends EntityTransformer
'product_key' => $item->product_key, 'product_key' => $item->product_key,
'account_key' => $this->account->account_key, 'account_key' => $this->account->account_key,
'user_id' => (int) $item->user_id, 'user_id' => (int) $item->user_id,
'updated_at' => $item->updated_at, 'updated_at' => $this->getTimestamp($item->updated_at),
'deleted_at' => $item->deleted_at, 'archived_at' => $this->getTimestamp($item->deleted_at),
'product_key' => $item->product_key, 'product_key' => $item->product_key,
'notes' => $item->notes, 'notes' => $item->notes,
'cost' => (float) $item->cost, 'cost' => (float) $item->cost,

View File

@ -38,8 +38,8 @@ class InvoiceTransformer extends EntityTransformer
'balance' => (float) $invoice->balance, 'balance' => (float) $invoice->balance,
'client_id' => (int) $invoice->client->public_id, 'client_id' => (int) $invoice->client->public_id,
'invoice_status_id' => (int) $invoice->invoice_status_id, 'invoice_status_id' => (int) $invoice->invoice_status_id,
'updated_at' => $invoice->updated_at, 'updated_at' => $this->getTimestamp($invoice->updated_at),
'deleted_at' => $invoice->deleted_at, 'archived_at' => $this->getTimestamp($invoice->deleted_at),
'invoice_number' => $invoice->invoice_number, 'invoice_number' => $invoice->invoice_number,
'discount' => (double) $invoice->discount, 'discount' => (double) $invoice->discount,
'po_number' => $invoice->po_number, 'po_number' => $invoice->po_number,
@ -48,6 +48,7 @@ class InvoiceTransformer extends EntityTransformer
'terms' => $invoice->terms, 'terms' => $invoice->terms,
'public_notes' => $invoice->public_notes, 'public_notes' => $invoice->public_notes,
'is_deleted' => (bool) $invoice->is_deleted, 'is_deleted' => (bool) $invoice->is_deleted,
'is_quote' => (bool) $invoice->is_quote,
'is_recurring' => (bool) $invoice->is_recurring, 'is_recurring' => (bool) $invoice->is_recurring,
'frequency_id' => (int) $invoice->frequency_id, 'frequency_id' => (int) $invoice->frequency_id,
'start_date' => $invoice->start_date, 'start_date' => $invoice->start_date,

View File

@ -25,8 +25,8 @@ class TaxRateTransformer extends EntityTransformer
'id' => (int) $taxRate->public_id, 'id' => (int) $taxRate->public_id,
'name' => $taxRate->name, 'name' => $taxRate->name,
'rate' => (float) $taxRate->rate, 'rate' => (float) $taxRate->rate,
'updated_at' => $taxRate->updated_at, 'updated_at' => $this->getTimestamp($taxRate->updated_at),
'archived_at' => $taxRate->deleted_at, 'archived_at' => $this->getTimestamp($taxRate->deleted_at),
'account_key' => $this->account->account_key, 'account_key' => $this->account->account_key,
]; ];
} }

View File

@ -14,8 +14,8 @@ class UserTransformer extends EntityTransformer
'last_name' => $user->last_name, 'last_name' => $user->last_name,
'email' => $user->email, 'email' => $user->email,
'account_key' => $user->account->account_key, 'account_key' => $user->account->account_key,
'updated_at' => $user->updated_at, 'updated_at' => $this->getTimestamp($user->updated_at),
'deleted_at' => $user->deleted_at, 'deleted_at' => $this->getTimestamp($user->deleted_at),
'phone' => $user->phone, 'phone' => $user->phone,
'username' => $user->username, 'username' => $user->username,
'registered' => (bool) $user->registered, 'registered' => (bool) $user->registered,