Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
Signed-off-by: snipe <[email protected]>

# Conflicts:
#	.all-contributorsrc
#	CONTRIBUTORS.md
  • Loading branch information
snipe committed Jul 11, 2024
2 parents c1937b6 + aebe969 commit 1a541ce
Show file tree
Hide file tree
Showing 23 changed files with 527 additions and 145 deletions.
10 changes: 10 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -3137,6 +3137,15 @@
"doc"
]
},
{
"login": "FlorentDotMe",
"name": "Florent Bervas",
"avatar_url": "https://avatars.githubusercontent.com/u/292081?v=4",
"profile": "http://spoontux.net",
"contributions": [
"code"
]
},
{
"login": "Galaxy102",
"name": "Konstantin Köhring",
Expand All @@ -3146,5 +3155,6 @@
"code"
]
}

]
}
100 changes: 50 additions & 50 deletions CONTRIBUTORS.md

Large diffs are not rendered by default.

60 changes: 28 additions & 32 deletions app/Http/Controllers/Api/ConsumablesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Events\CheckoutableCheckedOut;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreConsumableRequest;
use App\Http\Transformers\ConsumablesTransformer;
use App\Http\Transformers\SelectlistTransformer;
use App\Models\Company;
Expand All @@ -27,27 +28,8 @@ public function index(Request $request) : array
{
$this->authorize('index', Consumable::class);

// This array is what determines which fields should be allowed to be sorted on ON the table itself, no relations
// Relations will be handled in query scopes a little further down.
$allowed_columns =
[
'id',
'name',
'order_number',
'min_amt',
'purchase_date',
'purchase_cost',
'company',
'category',
'model_number',
'item_no',
'qty',
'image',
'notes',
];

$consumables = Consumable::select('consumables.*')
->with('company', '___location', 'category', 'users', 'manufacturer');
$consumables = Consumable::with('company', '___location', 'category', 'supplier', 'manufacturer')
->withCount('users as consumables_users_count');

if ($request->filled('search')) {
$consumables = $consumables->TextSearch(e($request->input('search')));
Expand Down Expand Up @@ -89,15 +71,9 @@ public function index(Request $request) : array
// Make sure the offset and limit are actually integers and do not exceed system limits
$offset = ($request->input('offset') > $consumables->count()) ? $consumables->count() : app('api_offset_value');
$limit = app('api_limit_value');

$allowed_columns = ['id', 'name', 'order_number', 'min_amt', 'purchase_date', 'purchase_cost', 'company', 'category', 'model_number', 'item_no', 'manufacturer', '___location', 'qty', 'image'];
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';

$sort_override = $request->input('sort');
$column_sort = in_array($sort_override, $allowed_columns) ? $sort_override : 'created_at';


switch ($sort_override) {
switch ($request->input('sort')) {
case 'category':
$consumables = $consumables->OrderCategory($order);
break;
Expand All @@ -111,10 +87,30 @@ public function index(Request $request) : array
$consumables = $consumables->OrderCompany($order);
break;
case 'supplier':
$components = $consumables->OrderSupplier($order);
$consumables = $consumables->OrderSupplier($order);
break;
default:
$consumables = $consumables->orderBy($column_sort, $order);
// This array is what determines which fields should be allowed to be sorted on ON the table itself.
// These must match a column on the consumables table directly.
$allowed_columns = [
'id',
'name',
'order_number',
'min_amt',
'purchase_date',
'purchase_cost',
'company',
'category',
'model_number',
'item_no',
'manufacturer',
'___location',
'qty',
'image'
];

$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
$consumables = $consumables->orderBy($sort, $order);
break;
}

Expand All @@ -131,7 +127,7 @@ public function index(Request $request) : array
* @since [v4.0]
* @param \App\Http\Requests\ImageUploadRequest $request
*/
public function store(ImageUploadRequest $request) : JsonResponse
public function store(StoreConsumableRequest $request) : JsonResponse
{
$this->authorize('create', Consumable::class);
$consumable = new Consumable;
Expand Down Expand Up @@ -167,7 +163,7 @@ public function show($id) : array
* @param \App\Http\Requests\ImageUploadRequest $request
* @param int $id
*/
public function update(ImageUploadRequest $request, $id) : JsonResponse
public function update(StoreConsumableRequest $request, $id) : JsonResponse
{
$this->authorize('update', Consumable::class);
$consumable = Consumable::findOrFail($id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use App\Events\CheckoutableCheckedOut;
use App\Http\Controllers\Controller;
use App\Models\Accessory;
use App\Models\Consumable;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use \Illuminate\Contracts\View\View;
use \Illuminate\Http\RedirectResponse;

class ConsumableCheckoutController extends Controller
{
Expand All @@ -20,13 +19,11 @@ class ConsumableCheckoutController extends Controller
* @see ConsumableCheckoutController::store() method that stores the data.
* @since [v1.0]
* @param int $id
* @return \Illuminate\Contracts\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function create($id)
public function create($id) : View | RedirectResponse
{

if ($consumable = Consumable::with('users')->find($id)) {
if ($consumable = Consumable::find($id)) {

$this->authorize('checkout', $consumable);

Expand Down
13 changes: 7 additions & 6 deletions app/Http/Controllers/Consumables/ConsumablesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use App\Models\Company;
use App\Models\Consumable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\RedirectResponse;
use \Illuminate\Contracts\View\View;
use App\Http\Requests\StoreConsumableRequest;

/**
* This controller handles all actions related to Consumables for
Expand Down Expand Up @@ -62,7 +64,7 @@ public function create()
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function store(ImageUploadRequest $request)
public function store(StoreConsumableRequest $request)
{
$this->authorize('create', Consumable::class);
$consumable = new Consumable();
Expand Down Expand Up @@ -99,10 +101,8 @@ public function store(ImageUploadRequest $request)
* @param int $consumableId
* @see ConsumablesController::postEdit() method that stores the form data.
* @since [v1.0]
* @return \Illuminate\Contracts\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function edit($consumableId = null)
public function edit($consumableId = null) : View | RedirectResponse
{
if ($item = Consumable::find($consumableId)) {
$this->authorize($item);
Expand All @@ -124,7 +124,7 @@ public function edit($consumableId = null)
* @see ConsumablesController::getEdit() method that stores the form data.
* @since [v1.0]
*/
public function update(ImageUploadRequest $request, $consumableId = null)
public function update(StoreConsumableRequest $request, $consumableId = null)
{
if (is_null($consumable = Consumable::find($consumableId))) {
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.does_not_exist'));
Expand Down Expand Up @@ -182,6 +182,7 @@ public function destroy($consumableId)
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found'));
}
$this->authorize($consumable);

$consumable->delete();
// Redirect to the locations management page
return redirect()->route('consumables.index')->with('success', trans('admin/consumables/message.delete.success'));
Expand Down
56 changes: 56 additions & 0 deletions app/Http/Requests/StoreConsumableRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Http\Requests;

use App\Models\Consumable;
use App\Models\Category;
use Illuminate\Support\Facades\Gate;

class StoreConsumableRequest extends ImageUploadRequest
{

/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return Gate::allows('create', new Consumable);
}

public function prepareForValidation(): void
{

if ($this->category_id) {
if ($category = Category::find($this->category_id)) {
$this->merge([
'category_type' => $category->category_type ?? null,
]);
}
}

}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return array_merge(
['category_type' => 'in:consumable'],
parent::rules(),
);
}

public function messages(): array
{
$messages = ['category_type.in' => trans('admin/consumables/message.invalid_category_type')];
return $messages;
}

public function response(array $errors)
{
return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag);
}
}
Loading

0 comments on commit 1a541ce

Please sign in to comment.