diff --git a/app/Http/Controllers/ExpenseApiController.php b/app/Http/Controllers/ExpenseApiController.php index ea4425df86..5734d577fe 100644 --- a/app/Http/Controllers/ExpenseApiController.php +++ b/app/Http/Controllers/ExpenseApiController.php @@ -3,6 +3,9 @@ use App\Models\Expense; use App\Ninja\Repositories\ExpenseRepository; use App\Services\ExpenseService; +use App\Http\Requests\ExpenseRequest; +use App\Http\Requests\CreateExpenseRequest; +use App\Http\Requests\UpdateExpenseRequest; class ExpenseApiController extends BaseAPIController { @@ -20,6 +23,22 @@ class ExpenseApiController extends BaseAPIController $this->expenseService = $expenseService; } + /** + * @SWG\Get( + * path="/expenses", + * summary="List of expenses", + * tags={"expense"}, + * @SWG\Response( + * response=200, + * description="A list with expenses", + * @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Expense")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + */ public function index() { $expenses = Expense::scope() @@ -30,23 +49,101 @@ class ExpenseApiController extends BaseAPIController return $this->listResponse($expenses); } - public function update() + /** + * @SWG\Post( + * path="/expenses", + * tags={"expense"}, + * summary="Create a expense", + * @SWG\Parameter( + * in="body", + * name="body", + * @SWG\Schema(ref="#/definitions/Expense") + * ), + * @SWG\Response( + * response=200, + * description="New expense", + * @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Expense")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + */ + public function store(CreateExpenseRequest $request) { - //stub + $expense = $this->expenseRepo->save($request->input()); + $expense = Expense::scope($expense->public_id) + ->with('client', 'invoice', 'vendor') + ->first(); + + return $this->itemResponse($expense); } - public function store() + /** + * @SWG\Put( + * path="/expenses/{expense_id}", + * tags={"expense"}, + * summary="Update a expense", + * @SWG\Parameter( + * in="body", + * name="body", + * @SWG\Schema(ref="#/definitions/Expense") + * ), + * @SWG\Response( + * response=200, + * description="Update expense", + * @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Expense")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + */ + public function update(UpdateExpenseRequest $request, $publicId) { - //stub + if ($request->action) { + return $this->handleAction($request); + } + $data = $request->input(); + $data['public_id'] = $publicId; + $expense = $this->expenseRepo->save($data, $request->entity()); + + return $this->itemResponse($expense); } - public function destroy() + /** + * @SWG\Delete( + * path="/expenses/{expense_id}", + * tags={"expense"}, + * summary="Delete a expense", + * @SWG\Parameter( + * in="body", + * name="body", + * @SWG\Schema(ref="#/definitions/Expense") + * ), + * @SWG\Response( + * response=200, + * description="Delete expense", + * @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Expense")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + */ + public function destroy(ExpenseRequest $request) { - //stub + $expense = $request->entity(); + $this->expenseRepo->delete($expense); + + return $this->itemResponse($expense); } -} \ No newline at end of file +} diff --git a/app/Http/routes.php b/app/Http/routes.php index 9f4b6b9ed0..8abb80884e 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -308,11 +308,7 @@ Route::group(['middleware' => 'api', 'prefix' => 'api/v1'], function() Route::post('update_notifications', 'AccountApiController@updatePushNotifications'); Route::get('dashboard', 'DashboardApiController@index'); Route::resource('documents', 'DocumentAPIController'); - - // Vendor Route::resource('vendors', 'VendorApiController'); - - //Expense Route::resource('expenses', 'ExpenseApiController'); }); diff --git a/app/Ninja/Repositories/ExpenseRepository.php b/app/Ninja/Repositories/ExpenseRepository.php index 0174486490..7d72ed6b38 100644 --- a/app/Ninja/Repositories/ExpenseRepository.php +++ b/app/Ninja/Repositories/ExpenseRepository.php @@ -132,12 +132,10 @@ class ExpenseRepository extends BaseRepository // First auto fill $expense->fill($input); - $expense->expense_date = Utils::toSqlDate($input['expense_date']); - - if (isset($input['private_notes'])) { - $expense->private_notes = trim($input['private_notes']); + if (isset($input['expense_date'])) { + $expense->expense_date = Utils::toSqlDate($input['expense_date']); } - $expense->public_notes = trim($input['public_notes']); + $expense->should_be_invoiced = isset($input['should_be_invoiced']) && floatval($input['should_be_invoiced']) || $expense->client_id ? true : false; if ( ! $expense->expense_currency_id) { @@ -149,7 +147,9 @@ class ExpenseRepository extends BaseRepository $rate = isset($input['exchange_rate']) ? Utils::parseFloat($input['exchange_rate']) : 1; $expense->exchange_rate = round($rate, 4); - $expense->amount = round(Utils::parseFloat($input['amount']), 2); + if (isset($input['amount'])) { + $expense->amount = round(Utils::parseFloat($input['amount']), 2); + } $expense->save(); diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index 1f63746415..d020fd6a4d 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -492,31 +492,33 @@ class InvoiceRepository extends BaseRepository $invoice->invoice_items()->forceDelete(); } - $document_ids = !empty($data['document_ids'])?array_map('intval', $data['document_ids']):[];; - foreach ($document_ids as $document_id){ - $document = Document::scope($document_id)->first(); - if($document && Auth::user()->can('edit', $document)){ + if ( ! empty($data['document_ids'])) { + $document_ids = array_map('intval', $data['document_ids']); + foreach ($document_ids as $document_id){ + $document = Document::scope($document_id)->first(); + if($document && Auth::user()->can('edit', $document)){ - if($document->invoice_id && $document->invoice_id != $invoice->id){ - // From a clone - $document = $document->cloneDocument(); - $document_ids[] = $document->public_id;// Don't remove this document + if($document->invoice_id && $document->invoice_id != $invoice->id){ + // From a clone + $document = $document->cloneDocument(); + $document_ids[] = $document->public_id;// Don't remove this document + } + + $document->invoice_id = $invoice->id; + $document->expense_id = null; + $document->save(); } - - $document->invoice_id = $invoice->id; - $document->expense_id = null; - $document->save(); } - } - if ( ! $invoice->wasRecentlyCreated) { - foreach ($invoice->documents as $document){ - if(!in_array($document->public_id, $document_ids)){ - // Removed - // Not checking permissions; deleting a document is just editing the invoice - if($document->invoice_id == $invoice->id){ - // Make sure the document isn't on a clone - $document->delete(); + if ( ! $invoice->wasRecentlyCreated) { + foreach ($invoice->documents as $document){ + if(!in_array($document->public_id, $document_ids)){ + // Removed + // Not checking permissions; deleting a document is just editing the invoice + if($document->invoice_id == $invoice->id){ + // Make sure the document isn't on a clone + $document->delete(); + } } } }