2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
2019-12-31 15:49:09 +03:00
|
|
|
namespace App\Http\Controllers\Api\Purchases;
|
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\Purchase\Bill as Request;
|
|
|
|
use App\Jobs\Purchase\CreateBill;
|
|
|
|
use App\Jobs\Purchase\DeleteBill;
|
|
|
|
use App\Jobs\Purchase\UpdateBill;
|
|
|
|
use App\Models\Purchase\Bill;
|
|
|
|
use App\Transformers\Purchase\Bill as Transformer;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
class Bills extends ApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return \Dingo\Api\Http\Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2020-01-13 12:47:59 +03:00
|
|
|
$bills = Bill::with(['contact', 'items', 'transactions', 'histories'])->collect(['billed_at'=> 'desc']);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
return $this->response->paginator($bills, new Transformer());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param Bill $bill
|
|
|
|
* @return \Dingo\Api\Http\Response
|
|
|
|
*/
|
|
|
|
public function show(Bill $bill)
|
|
|
|
{
|
|
|
|
return $this->response->item($bill, 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
|
|
|
$bill = $this->dispatch(new CreateBill($request));
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return $this->response->created(url('api/bills/' . $bill->id));
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param $bill
|
|
|
|
* @param $request
|
|
|
|
* @return \Dingo\Api\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(Bill $bill, Request $request)
|
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
$bill = $this->dispatch(new UpdateBill($bill, $request));
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return $this->item($bill->fresh(), new Transformer());
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param Bill $bill
|
|
|
|
* @return \Dingo\Api\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy(Bill $bill)
|
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
try {
|
|
|
|
$this->dispatch(new DeleteBill($bill));
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return $this->response->noContent();
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
$this->response->errorUnauthorized($e->getMessage());
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
}
|