89 lines
2.3 KiB
PHP
Raw Normal View History

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