1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00

Merge pull request #727 from turbo124/develop

API work
This commit is contained in:
David Bomba 2016-02-17 14:47:36 +11:00
commit 35d905f4f4
11 changed files with 132 additions and 17 deletions

View File

@ -36,14 +36,14 @@ class AccountApiController extends BaseAPIController
{ {
if ( ! env(API_SECRET) || $request->api_secret !== env(API_SECRET)) { if ( ! env(API_SECRET) || $request->api_secret !== env(API_SECRET)) {
sleep(ERROR_DELAY); sleep(ERROR_DELAY);
return 'Invalid secret'; return $this->errorResponse(['message'=>'Invalid secret'],401);
} }
if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) { if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
return $this->processLogin($request); return $this->processLogin($request);
} else { } else {
sleep(ERROR_DELAY); sleep(ERROR_DELAY);
return 'Invalid credentials'; return $this->errorResponse(['message'=>'Invalid credentials'],401);
} }
} }

View File

@ -113,13 +113,13 @@ class BaseAPIController extends Controller
return Response::make($response, 200, $headers); return Response::make($response, 200, $headers);
} }
protected function errorResponse($response) protected function errorResponse($response, $httpErrorCode = 400)
{ {
$error['error'] = $response; $error['error'] = $response;
$error = json_encode($error, JSON_PRETTY_PRINT); $error = json_encode($error, JSON_PRETTY_PRINT);
$headers = Utils::getApiHeaders(); $headers = Utils::getApiHeaders();
return Response::make($error, 400, $headers); return Response::make($error, $httpErrorCode, $headers);
} }

View File

@ -136,11 +136,11 @@ class ClientApiController extends BaseAPIController
{ {
if ($request->action == ACTION_ARCHIVE) { if ($request->action == ACTION_ARCHIVE) {
try {
$client = Client::scope($publicId)->withTrashed()->firstOrFail(); $client = Client::scope($publicId)->withTrashed()->first();
} catch (ModelNotFoundException $e) {
if(!$client)
return $this->errorResponse(['message'=>'Record not found'], 400); return $this->errorResponse(['message'=>'Record not found'], 400);
}
$this->clientRepo->archive($client); $this->clientRepo->archive($client);
@ -154,7 +154,7 @@ class ClientApiController extends BaseAPIController
$client = Client::scope($publicId)->withTrashed()->first(); $client = Client::scope($publicId)->withTrashed()->first();
if(!$client) if(!$client)
return $this->errorResponse(['message'=>'Client not found.']); return $this->errorResponse(['message'=>'Client not found.'], 400);
$this->clientRepo->restore($client); $this->clientRepo->restore($client);
@ -173,7 +173,7 @@ class ClientApiController extends BaseAPIController
->first(); ->first();
if(!$client) if(!$client)
return $this->errorResponse(['message'=>'Client not found.']); return $this->errorResponse(['message'=>'Client not found.'],400);
$transformer = new ClientTransformer(Auth::user()->account, Input::get('serializer')); $transformer = new ClientTransformer(Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($client, $transformer, ENTITY_CLIENT); $data = $this->createItem($client, $transformer, ENTITY_CLIENT);

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

@ -1,6 +1,7 @@
<?php namespace App\Http\Controllers; <?php namespace App\Http\Controllers;
use Auth; use Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Request;
use Utils; use Utils;
use Response; use Response;
@ -282,12 +283,17 @@ class InvoiceApiController extends BaseAPIController
$data = Input::all(); $data = Input::all();
$error = null; $error = null;
$invoice = Invoice::scope($data['id'])->firstOrFail(); $invoice = Invoice::scope($data['id'])->withTrashed()->first();
if(!$invoice)
return $this->errorResponse(['message'=>'Invoice does not exist.'], 400);
$this->mailer->sendInvoice($invoice, false, false);
$this->mailer->sendInvoice($invoice);
if($error) { if($error) {
$response['error'] = "There was an error sending the invoice"; return $this->errorResponse(['message'=>'There was an error sending the invoice'], 400);
} }
else { else {
$response = json_encode(RESULT_SUCCESS, JSON_PRETTY_PRINT); $response = json_encode(RESULT_SUCCESS, JSON_PRETTY_PRINT);

View File

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

View File

@ -236,6 +236,7 @@ Route::group(['middleware' => 'api', 'prefix' => 'api/v1'], function()
Route::resource('products', 'ProductApiController'); Route::resource('products', 'ProductApiController');
Route::resource('tax_rates', 'TaxRateApiController'); Route::resource('tax_rates', 'TaxRateApiController');
Route::resource('users', 'UserApiController'); Route::resource('users', 'UserApiController');
Route::resource('expenses','ExpenseApiController');
// Vendor // Vendor
Route::resource('vendors', 'VendorApiController'); Route::resource('vendors', 'VendorApiController');

View File

@ -124,6 +124,11 @@ class Vendor extends EntityModel
return $this->belongsTo('App\Models\Industry'); return $this->belongsTo('App\Models\Industry');
} }
public function expenses()
{
return $this->hasMany('App\Models\Expense','vendor_id','id');
}
public function addVendorContact($data, $isPrimary = false) public function addVendorContact($data, $isPrimary = false)
{ {
$publicId = isset($data['public_id']) ? $data['public_id'] : false; $publicId = isset($data['public_id']) ? $data['public_id'] : false;

View File

@ -41,6 +41,8 @@ class ContactMailer extends Mailer
$client = $invoice->client; $client = $invoice->client;
$account = $invoice->account; $account = $invoice->account;
$response = null;
if ($client->trashed()) { if ($client->trashed()) {
return trans('texts.email_errors.inactive_client'); return trans('texts.email_errors.inactive_client');
} elseif ($invoice->trashed()) { } 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 = [ protected $availableIncludes = [
'contacts', 'vendorContacts',
'invoices', 'invoices',
'expenses',
]; ];
public function includeContacts(Vendor $vendor) public function includeVendorContacts(Vendor $vendor)
{ {
$transformer = new ContactTransformer($this->account, $this->serializer); $transformer = new VendorContactTransformer($this->account, $this->serializer);
return $this->includeCollection($vendor->contacts, $transformer, ENTITY_CONTACT); return $this->includeCollection($vendor->vendorContacts, $transformer, ENTITY_CONTACT);
} }
public function includeInvoices(Vendor $vendor) public function includeInvoices(Vendor $vendor)
@ -52,6 +53,12 @@ class VendorTransformer extends EntityTransformer
return $this->includeCollection($vendor->invoices, $transformer, ENTITY_INVOICE); 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) public function transform(Vendor $vendor)
{ {
return [ return [