akaunting/modules/BC21/Http/Controllers/Api/Sales/Invoices.php

92 lines
2.3 KiB
PHP
Raw Normal View History

2017-09-14 19:21:00 +00:00
<?php
2019-12-31 12:49:09 +00:00
namespace App\Http\Controllers\Api\Sales;
2017-09-14 19:21:00 +00:00
2019-11-16 07:21:14 +00:00
use App\Abstracts\Http\ApiController;
2020-12-23 22:28:38 +00:00
use App\Http\Requests\Document\Document as Request;
use App\Jobs\Document\CreateDocument;
use App\Jobs\Document\DeleteDocument;
use App\Jobs\Document\UpdateDocument;
use App\Models\Document\Document;
2019-12-31 12:49:09 +00:00
use App\Transformers\Sale\Invoice as Transformer;
2017-09-14 19:21:00 +00:00
class Invoices extends ApiController
{
/**
* Display a listing of the resource.
*
* @return \Dingo\Api\Http\Response
*/
public function index()
{
2020-12-23 22:28:38 +00:00
$invoices = Document::invoice()->with('contact', 'histories', 'items', 'transactions')->collect(['issued_at'=> 'desc']);
2017-09-14 19:21:00 +00:00
return $this->response->paginator($invoices, new Transformer());
}
/**
* Display the specified resource.
*
2018-03-28 12:30:00 +00:00
* @param $id
2017-09-14 19:21:00 +00:00
* @return \Dingo\Api\Http\Response
*/
2018-03-28 12:30:00 +00:00
public function show($id)
2017-09-14 19:21:00 +00:00
{
2018-03-28 12:30:00 +00:00
// Check if we're querying by id or number
if (is_numeric($id)) {
2020-12-23 22:28:38 +00:00
$invoice = Document::find($id);
2018-03-28 12:30:00 +00:00
} else {
2020-12-23 22:28:38 +00:00
$invoice = Document::where('document_number', $id)->first();
2018-03-28 12:30:00 +00:00
}
2017-09-14 19:21:00 +00:00
return $this->response->item($invoice, new Transformer());
}
/**
* Store a newly created resource in storage.
*
* @param $request
2020-12-23 22:28:38 +00:00
*
2017-09-14 19:21:00 +00:00
* @return \Dingo\Api\Http\Response
*/
public function store(Request $request)
{
2020-12-23 22:28:38 +00:00
$invoice = $this->dispatch(new CreateDocument($request));
2017-09-14 19:21:00 +00:00
2020-08-11 13:52:10 +00:00
return $this->response->created(route('api.invoices.show', $invoice->id));
2017-09-14 19:21:00 +00:00
}
/**
* Update the specified resource in storage.
*
* @param $invoice
2017-09-14 19:21:00 +00:00
* @param $request
2020-12-23 22:28:38 +00:00
*
2017-09-14 19:21:00 +00:00
* @return \Dingo\Api\Http\Response
*/
public function update(Document $invoice, Request $request)
2017-09-14 19:21:00 +00:00
{
$invoice = $this->dispatch(new UpdateDocument($invoice, $request));
2017-09-14 19:21:00 +00:00
return $this->response->item($invoice->fresh(), new Transformer());
2017-09-14 19:21:00 +00:00
}
/**
* Remove the specified resource from storage.
*
* @param Document $invoice
2020-12-23 22:28:38 +00:00
*
2017-09-14 19:21:00 +00:00
* @return \Dingo\Api\Http\Response
*/
public function destroy(Document $invoice)
2017-09-14 19:21:00 +00:00
{
2019-11-16 07:21:14 +00:00
try {
$this->dispatch(new DeleteDocument($invoice));
2017-10-12 15:06:40 +00:00
2019-11-16 07:21:14 +00:00
return $this->response->noContent();
} catch(\Exception $e) {
$this->response->errorUnauthorized($e->getMessage());
2017-10-12 15:06:40 +00:00
}
}
2017-09-14 19:21:00 +00:00
}