Skip to content

Commit 3ee76be

Browse files
committed
Added manufacturer and model_number to components
Signed-off-by: snipe <[email protected]>
1 parent d58f878 commit 3ee76be

File tree

12 files changed

+158
-8
lines changed

12 files changed

+158
-8
lines changed

app/Http/Controllers/Api/ComponentsController.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function index(Request $request) : JsonResponse | array
3838
'name',
3939
'min_amt',
4040
'order_number',
41+
'model_number',
4142
'serial',
4243
'purchase_date',
4344
'purchase_cost',
@@ -47,7 +48,7 @@ public function index(Request $request) : JsonResponse | array
4748
];
4849

4950
$components = Component::select('components.*')
50-
->with('company', 'location', 'category', 'assets', 'supplier', 'adminuser');
51+
->with('company', 'location', 'category', 'assets', 'supplier', 'adminuser', 'manufacturer');
5152

5253
if ($request->filled('search')) {
5354
$components = $components->TextSearch($request->input('search'));
@@ -69,6 +70,14 @@ public function index(Request $request) : JsonResponse | array
6970
$components->where('supplier_id', '=', $request->input('supplier_id'));
7071
}
7172

73+
if ($request->filled('manufacturer_id')) {
74+
$components->where('manufacturer_id', '=', $request->input('manufacturer_id'));
75+
}
76+
77+
if ($request->filled('model_number')) {
78+
$components->where('model_number', '=', $request->input('model_number'));
79+
}
80+
7281
if ($request->filled('location_id')) {
7382
$components->where('location_id', '=', $request->input('location_id'));
7483
}
@@ -98,6 +107,9 @@ public function index(Request $request) : JsonResponse | array
98107
case 'supplier':
99108
$components = $components->OrderSupplier($order);
100109
break;
110+
case 'manufacturer':
111+
$components = $components->OrderManufacturer($order);
112+
break;
101113
case 'created_by':
102114
$components = $components->OrderByCreatedBy($order);
103115
break;

app/Http/Controllers/Api/ManufacturersController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public function index(Request $request) : JsonResponse | array
6060
->withCount('assets as assets_count')
6161
->withCount('licenses as licenses_count')
6262
->withCount('consumables as consumables_count')
63-
->withCount('accessories as accessories_count');
63+
->withCount('accessories as accessories_count')
64+
->withCount('components as components_count');
6465

6566
if ($request->input('deleted') == 'true') {
6667
$manufacturers->onlyTrashed();

app/Http/Controllers/Components/ComponentsController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public function store(ImageUploadRequest $request)
7373
$component->name = $request->input('name');
7474
$component->category_id = $request->input('category_id');
7575
$component->supplier_id = $request->input('supplier_id');
76+
$component->manufacturer_id = $request->input('manufacturer_id');
77+
$component->model_number = $request->input('model_number');
7678
$component->location_id = $request->input('location_id');
7779
$component->company_id = Company::getIdForCurrentUser($request->input('company_id'));
7880
$component->order_number = $request->input('order_number', null);
@@ -150,6 +152,8 @@ public function update(ImageUploadRequest $request, $componentId = null)
150152
$component->name = $request->input('name');
151153
$component->category_id = $request->input('category_id');
152154
$component->supplier_id = $request->input('supplier_id');
155+
$component->manufacturer_id = $request->input('manufacturer_id');
156+
$component->model_number = $request->input('model_number');
153157
$component->location_id = $request->input('location_id');
154158
$component->company_id = Company::getIdForCurrentUser($request->input('company_id'));
155159
$component->order_number = $request->input('order_number');

app/Http/Transformers/ComponentsTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public function transformComponent(Component $component)
3838
'name' => e($component->category->name),
3939
] : null,
4040
'supplier' => ($component->supplier) ? ['id' => $component->supplier->id, 'name'=> e($component->supplier->name)] : null,
41+
'manufacturer' => ($component->manufacturer) ? ['id' => $component->manufacturer->id, 'name'=> e($component->manufacturer->name)] : null,
42+
'model_number' => ($component->model_number) ? e($component->model_number) : null,
4143
'order_number' => e($component->order_number),
4244
'purchase_date' => Helper::getFormattedDateObject($component->purchase_date, 'date'),
4345
'purchase_cost' => Helper::formatCurrencyOutput($component->purchase_cost),

app/Http/Transformers/ManufacturersTransformer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function transformManufacturer(Manufacturer $manufacturer = null)
3636
'licenses_count' => (int) $manufacturer->licenses_count,
3737
'consumables_count' => (int) $manufacturer->consumables_count,
3838
'accessories_count' => (int) $manufacturer->accessories_count,
39+
'components_count' => (int) $manufacturer->components_count,
3940
'created_by' => ($manufacturer->adminuser) ? [
4041
'id' => (int) $manufacturer->adminuser->id,
4142
'name'=> e($manufacturer->adminuser->present()->fullName()),

app/Models/Component.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Component extends SnipeModel
3838
'min_amt' => 'integer|min:0|nullable',
3939
'purchase_date' => 'date_format:Y-m-d|nullable',
4040
'purchase_cost' => 'numeric|nullable|gte:0|max:9999999999999',
41+
'manufacturer_id' => 'integer|exists:manufacturers,id|nullable',
4142
];
4243

4344
/**
@@ -60,6 +61,8 @@ class Component extends SnipeModel
6061
'company_id',
6162
'supplier_id',
6263
'location_id',
64+
'manufacturer_id',
65+
'model_number',
6366
'name',
6467
'purchase_cost',
6568
'purchase_date',
@@ -77,7 +80,15 @@ class Component extends SnipeModel
7780
*
7881
* @var array
7982
*/
80-
protected $searchableAttributes = ['name', 'order_number', 'serial', 'purchase_cost', 'purchase_date', 'notes'];
83+
protected $searchableAttributes = [
84+
'name',
85+
'order_number',
86+
'serial',
87+
'purchase_cost',
88+
'purchase_date',
89+
'notes',
90+
'model_number',
91+
];
8192

8293
/**
8394
* The relations and their attributes that should be included when searching the model.
@@ -89,6 +100,7 @@ class Component extends SnipeModel
89100
'company' => ['name'],
90101
'location' => ['name'],
91102
'supplier' => ['name'],
103+
'manufacturer' => ['name'],
92104
];
93105

94106

@@ -183,6 +195,19 @@ public function supplier()
183195
return $this->belongsTo(\App\Models\Supplier::class, 'supplier_id');
184196
}
185197

198+
199+
/**
200+
* Establishes the item -> manufacturer relationship
201+
*
202+
* @author [A. Gianotto] [<[email protected]>]
203+
* @since [v3.0]
204+
* @return \Illuminate\Database\Eloquent\Relations\Relation
205+
*/
206+
public function manufacturer()
207+
{
208+
return $this->belongsTo(\App\Models\Manufacturer::class, 'manufacturer_id');
209+
}
210+
186211
/**
187212
* Establishes the component -> action logs relationship
188213
*
@@ -311,6 +336,19 @@ public function scopeOrderSupplier($query, $order)
311336
return $query->leftJoin('suppliers', 'components.supplier_id', '=', 'suppliers.id')->orderBy('suppliers.name', $order);
312337
}
313338

339+
/**
340+
* Query builder scope to order on manufacturer
341+
*
342+
* @param \Illuminate\Database\Query\Builder $query Query builder instance
343+
* @param text $order Order
344+
*
345+
* @return \Illuminate\Database\Query\Builder Modified query builder
346+
*/
347+
public function scopeOrderManufacturer($query, $order)
348+
{
349+
return $query->leftJoin('manufacturers', 'components.manufacturer_id', '=', 'manufacturers.id')->orderBy('manufacturers.name', $order);
350+
}
351+
314352
public function scopeOrderByCreatedBy($query, $order)
315353
{
316354
return $query->leftJoin('users as admin_sort', 'components.created_by', '=', 'admin_sort.id')->select('components.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);

app/Models/Manufacturer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function isDeletable()
7878
&& (($this->licenses_count ?? $this->licenses()->count()) === 0)
7979
&& (($this->consumables_count ?? $this->consumables()->count()) === 0)
8080
&& (($this->accessories_count ?? $this->accessories()->count()) === 0)
81+
&& (($this->components_count ?? $this->components()->count()) === 0)
8182
&& ($this->deleted_at == '');
8283
}
8384

@@ -106,6 +107,10 @@ public function consumables()
106107
return $this->hasMany(\App\Models\Consumable::class, 'manufacturer_id');
107108
}
108109

110+
public function components()
111+
{
112+
return $this->hasMany(\App\Models\Component::class, 'manufacturer_id');
113+
}
109114

110115
public function adminuser()
111116
{

app/Presenters/ComponentPresenter.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,20 @@ public static function dataTableLayout()
6666
'title' => trans('general.supplier'),
6767
'visible' => false,
6868
'formatter' => 'suppliersLinkObjFormatter',
69-
],
70-
[
69+
], [
70+
'field' => 'model_number',
71+
'searchable' => true,
72+
'sortable' => true,
73+
'title' => trans('admin/models/table.modelnumber'),
74+
], [
75+
'field' => 'manufacturer',
76+
'searchable' => true,
77+
'sortable' => true,
78+
'switchable' => true,
79+
'title' => trans('general.manufacturer'),
80+
'visible' => false,
81+
'formatter' => 'manufacturersLinkObjFormatter',
82+
], [
7183
'field' => 'qty',
7284
'searchable' => false,
7385
'sortable' => true,

app/Presenters/ManufacturerPresenter.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,15 @@ public static function dataTableLayout()
124124
'title' => trans('general.accessories'),
125125
'visible' => true,
126126
'class' => 'css-accessory',
127-
],
128-
[
127+
], [
128+
'field' => 'components_count',
129+
'searchable' => false,
130+
'sortable' => true,
131+
'switchable' => true,
132+
'title' => trans('general.components'),
133+
'visible' => true,
134+
'class' => 'css-component',
135+
], [
129136
'field' => 'created_by',
130137
'searchable' => false,
131138
'sortable' => true,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('components', function (Blueprint $table) {
15+
$table->integer('manufacturer_id')->after('purchase_cost')->nullable()->default(null);
16+
$table->string('model_number')->after('purchase_cost')->nullable()->default(null);
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*/
23+
public function down(): void
24+
{
25+
Schema::table('components', function (Blueprint $table) {
26+
$table->dropColumn('manufacturer_id');
27+
$table->dropColumn('model_number');
28+
});
29+
}
30+
};

0 commit comments

Comments
 (0)