Invoice API Issue Fixed
This commit is contained in:
parent
4f988d107b
commit
14f5931f6b
|
|
@ -0,0 +1,104 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Webkul\API\Http\Controllers\Shop;
|
||||||
|
|
||||||
|
class InvoiceController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Contains current guard.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $guard;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains route related configuration.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $_config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repository object.
|
||||||
|
*
|
||||||
|
* @var \Webkul\Core\Eloquent\Repository
|
||||||
|
*/
|
||||||
|
protected $repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new controller instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->guard = request()->has('token') ? 'api' : 'customer';
|
||||||
|
|
||||||
|
$this->_config = request('_config');
|
||||||
|
|
||||||
|
if (isset($this->_config['authorization_required']) && $this->_config['authorization_required']) {
|
||||||
|
|
||||||
|
auth()->setDefaultDriver($this->guard);
|
||||||
|
|
||||||
|
$this->middleware('auth:' . $this->guard);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->repository = app($this->_config['repository']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a listing of the invoices.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$query = $this->repository->scopeQuery(function($query) {
|
||||||
|
$query = $query->leftJoin('orders', 'invoices.order_id', '=', 'orders.id')->select('invoices.*', 'orders.customer_id');
|
||||||
|
|
||||||
|
if (isset($this->_config['authorization_required']) && $this->_config['authorization_required']) {
|
||||||
|
$query = $query->where('customer_id', auth()->user()->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (request()->except(['page', 'limit', 'pagination', 'sort', 'order', 'token']) as $input => $value) {
|
||||||
|
$query = $query->whereIn($input, array_map('trim', explode(',', $value)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($sort = request()->input('sort')) {
|
||||||
|
$query = $query->orderBy($sort, request()->input('order') ?? 'desc');
|
||||||
|
} else {
|
||||||
|
$query = $query->orderBy('id', 'desc');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (is_null(request()->input('pagination')) || request()->input('pagination')) {
|
||||||
|
$results = $query->paginate(request()->input('limit') ?? 10);
|
||||||
|
} else {
|
||||||
|
$results = $query->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->_config['resource']::collection($results);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an individual invoice.
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function get($id)
|
||||||
|
{
|
||||||
|
if (isset($this->_config['authorization_required']) && $this->_config['authorization_required']) {
|
||||||
|
$query = $this->repository->leftJoin('orders', 'invoices.order_id', '=', 'orders.id')
|
||||||
|
->select('invoices.*', 'orders.customer_id')
|
||||||
|
->where('customer_id', auth()->user()->id)
|
||||||
|
->findOrFail($id);
|
||||||
|
} else {
|
||||||
|
$query = $this->repository->findOrFail($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new $this->_config['resource']($query);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -202,13 +202,13 @@ Route::group(['prefix' => 'api'], function ($router) {
|
||||||
|
|
||||||
|
|
||||||
//Invoice routes
|
//Invoice routes
|
||||||
Route::get('invoices', 'ResourceController@index')->defaults('_config', [
|
Route::get('invoices', 'InvoiceController@index')->defaults('_config', [
|
||||||
'repository' => 'Webkul\Sales\Repositories\InvoiceRepository',
|
'repository' => 'Webkul\Sales\Repositories\InvoiceRepository',
|
||||||
'resource' => 'Webkul\API\Http\Resources\Sales\Invoice',
|
'resource' => 'Webkul\API\Http\Resources\Sales\Invoice',
|
||||||
'authorization_required' => true
|
'authorization_required' => true
|
||||||
]);
|
]);
|
||||||
|
|
||||||
Route::get('invoices/{id}', 'ResourceController@get')->defaults('_config', [
|
Route::get('invoices/{id}', 'InvoiceController@get')->defaults('_config', [
|
||||||
'repository' => 'Webkul\Sales\Repositories\InvoiceRepository',
|
'repository' => 'Webkul\Sales\Repositories\InvoiceRepository',
|
||||||
'resource' => 'Webkul\API\Http\Resources\Sales\Invoice',
|
'resource' => 'Webkul\API\Http\Resources\Sales\Invoice',
|
||||||
'authorization_required' => true
|
'authorization_required' => true
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue