Skip to content

Commit

Permalink
Fixes snipe#1044 - adds suppliers and image to accessories (snipe#4266)
Browse files Browse the repository at this point in the history
* Ignore accesories uploads

* API: Allow searching accessories by supplier id

* Adds suppliers and image upload to accessories

* Allow sorting by counts for suppliers

* Validate supplier image uploads

* Remove purchase_date from protected accessory array, it was converting it to datetime in datepicker
  • Loading branch information
snipe authored Oct 19, 2017
1 parent 1f247ff commit 4215a32
Show file tree
Hide file tree
Showing 17 changed files with 209 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public/uploads/logo.png
public/uploads/logo.svg
public/uploads/models/*
public/uploads/suppliers/*
public/uploads/accessories/*
public/uploads/users/*
storage/app/private_uploads/users/*
storage/debugbar/
Expand Down
63 changes: 58 additions & 5 deletions app/Http/Controllers/AccessoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Slack;
use Str;
use View;
use Image;
use App\Http\Requests\ImageUploadRequest;

/** This controller handles all actions related to Accessories for
* the Snipe-IT Asset Management application.
Expand Down Expand Up @@ -57,6 +59,7 @@ public function create(Request $request)
->with('item', new Accessory)
->with('category_list', Helper::categoryList('accessory'))
->with('company_list', Helper::companyList())
->with('supplier_list', Helper::suppliersList())
->with('location_list', Helper::locationsList())
->with('manufacturer_list', Helper::manufacturerList());
}
Expand All @@ -68,7 +71,7 @@ public function create(Request $request)
* @author [A. Gianotto] [<snipe@snipe.net>]
* @return Redirect
*/
public function store(Request $request)
public function store(ImageUploadRequest $request)
{
$this->authorize(Accessory::class);
// create a new model instance
Expand All @@ -87,6 +90,28 @@ public function store(Request $request)
$accessory->purchase_cost = Helper::ParseFloat(request('purchase_cost'));
$accessory->qty = request('qty');
$accessory->user_id = Auth::user()->id;
$accessory->supplier_id = request('supplier_id');

if ($request->hasFile('image')) {

if (!config('app.lock_passwords')) {
$image = $request->file('image');
$ext = $image->getClientOriginalExtension();
$file_name = "accessory-".str_random(18).'.'.$ext;
$path = public_path('/uploads/accessories');
if ($image->getClientOriginalExtension()!='svg') {
Image::make($image->getRealPath())->resize(null, 250, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})->save($path.'/'.$file_name);
} else {
$image->move($path, $file_name);
}
$accessory->image = $file_name;
}
}



// Was the accessory created?
if ($accessory->save()) {
Expand Down Expand Up @@ -116,6 +141,7 @@ public function edit(Request $request, $accessoryId = null)
->with('category_list', Helper::categoryList('accessory'))
->with('company_list', Helper::companyList())
->with('location_list', Helper::locationsList())
->with('supplier_list', Helper::suppliersList())
->with('manufacturer_list', Helper::manufacturerList());
}

Expand All @@ -127,7 +153,7 @@ public function edit(Request $request, $accessoryId = null)
* @param int $accessoryId
* @return Redirect
*/
public function update(Request $request, $accessoryId = null)
public function update(ImageUploadRequest $request, $accessoryId = null)
{
if (is_null($accessory = Accessory::find($accessoryId))) {
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist'));
Expand All @@ -144,11 +170,38 @@ public function update(Request $request, $accessoryId = null)
$accessory->manufacturer_id = request('manufacturer_id');
$accessory->order_number = request('order_number');
$accessory->model_number = request('model_number');
$accessory->purchase_date = request('purchase_date');
$accessory->purchase_cost = request('purchase_cost');
$accessory->purchase_date = request('purchase_date');
$accessory->purchase_cost = request('purchase_cost');
$accessory->qty = request('qty');
$accessory->supplier_id = request('supplier_id');

if ($request->hasFile('image')) {

if (!config('app.lock_passwords')) {


$image = $request->file('image');
$ext = $image->getClientOriginalExtension();
$file_name = "accessory-".str_random(18).'.'.$ext;
$path = public_path('/uploads/accessories');
if ($image->getClientOriginalExtension()!='svg') {
Image::make($image->getRealPath())->resize(null, 250, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})->save($path.'/'.$file_name);
} else {
$image->move($path, $file_name);
}
if (($accessory->image) && (file_exists($path.'/'.$accessory->image))) {
unlink($path.'/'.$accessory->image);
}

$accessory->image = $file_name;
}
}

// Was the accessory updated?

// Was the accessory updated?
if ($accessory->save()) {
return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.update.success'));
}
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/Api/AccessoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public function index(Request $request)
$accessories->where('manufacturer_id','=',$request->input('manufacturer_id'));
}

if ($request->has('supplier_id')) {
$accessories->where('supplier_id','=',$request->input('supplier_id'));
}

$offset = $request->input('offset', 0);
$limit = $request->input('limit', 50);
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Api/SuppliersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class SuppliersController extends Controller
public function index(Request $request)
{
$this->authorize('view', Supplier::class);
$allowed_columns = ['id','name','address','phone','contact','fax','email'];
$allowed_columns = ['id','name','address','phone','contact','fax','email','image','assets_count','licenses_count', 'accessories_count'];

$suppliers = Supplier::select(
array('id','name','address','address2','city','state','country','fax', 'phone','email','contact','created_at','updated_at','deleted_at')
)->withCount('assets')->withCount('licenses')->whereNull('deleted_at');
)->withCount('assets')->withCount('licenses')->withCount('accessories')->whereNull('deleted_at');


if ($request->has('search')) {
Expand Down
12 changes: 6 additions & 6 deletions app/Http/Controllers/SuppliersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use View;
use Auth;
use Illuminate\Http\Request;
use App\Http\Requests\ImageUploadRequest;

use Symfony\Component\HttpFoundation\JsonResponse;

Expand Down Expand Up @@ -56,7 +57,7 @@ public function create()
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request)
public function store(ImageUploadRequest $request)
{
// Create a new supplier
$supplier = new Supplier;
Expand Down Expand Up @@ -135,7 +136,7 @@ public function edit($supplierId = null)
* @param int $supplierId
* @return \Illuminate\Http\RedirectResponse
*/
public function update($supplierId = null, Request $request)
public function update($supplierId = null, ImageUploadRequest $request)
{
// Check if the supplier exists
if (is_null($supplier = Supplier::find($supplierId))) {
Expand All @@ -158,18 +159,17 @@ public function update($supplierId = null, Request $request)
$supplier->url = $supplier->addhttp(request('url'));
$supplier->notes = request('notes');


if (Input::file('image')) {
$image = $request->file('image');
$file_name = str_random(25).".".$image->getClientOriginalExtension();
$file_name = 'suppliers-'.str_random(25).".".$image->getClientOriginalExtension();
$path = public_path('uploads/suppliers/'.$file_name);
Image::make($image->getRealPath())->resize(300, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})->save($path);
$supplier->image = $file_name;
}

if (request('image_delete') == 1 && $request->file('image') == "") {
} elseif (request('image_delete') == 1) {
$supplier->image = null;
}

Expand Down
2 changes: 2 additions & 0 deletions app/Http/Transformers/AccessoriesTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function transformAccessory (Accessory $accessory)
'name' => e($accessory->name),
'company' => ($accessory->company) ? ['id' => $accessory->company->id,'name'=> e($accessory->company->name)] : null,
'manufacturer' => ($accessory->manufacturer) ? ['id' => $accessory->manufacturer->id,'name'=> e($accessory->manufacturer->name)] : null,
'supplier' => ($accessory->supplier) ? ['id' => $accessory->supplier->id,'name'=> e($accessory->supplier->name)] : null,
'model_number' => ($accessory->model_number) ? e($accessory->model_number) : null,
'category' => ($accessory->category) ? ['id' => $accessory->category->id,'name'=> e($accessory->category->name)] : null,
'location' => ($accessory->location) ? ['id' => $accessory->location->id,'name'=> e($accessory->location->name)] : null,
Expand All @@ -35,6 +36,7 @@ public function transformAccessory (Accessory $accessory)
'order_number' => ($accessory->order_number) ? e($accessory->order_number) : null,
'min_qty' => ($accessory->min_amt) ? (int) $accessory->min_amt : null,
'remaining_qty' => $accessory->numRemaining(),
'image' => ($accessory->image) ? url('/').'/uploads/accessories/'.e($accessory->image) : null,
'created_at' => Helper::getFormattedDateObject($accessory->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($accessory->updated_at, 'datetime'),

Expand Down
5 changes: 3 additions & 2 deletions app/Http/Transformers/SuppliersTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public function transformSupplier (Supplier $supplier = null)
'email' => ($supplier->email) ? e($supplier->email) : null,
'contact' => ($supplier->contact) ? e($supplier->contact) : null,
'assets_count' => (int) $supplier->assets_count,
'accessories_count' => (int) $supplier->accessories_count,
'licenses_count' => (int) $supplier->licenses_count,
'image' => ($supplier->image) ? e($supplier->image) : null,
'image' => ($supplier->image) ? url('/').'/uploads/suppliers/'.e($supplier->image) : null,
'notes' => ($supplier->notes) ? e($supplier->notes) : null,
'created_at' => Helper::getFormattedDateObject($supplier->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($supplier->updated_at, 'datetime'),
Expand All @@ -46,7 +47,7 @@ public function transformSupplier (Supplier $supplier = null)

$permissions_array['available_actions'] = [
'update' => Gate::allows('update', Supplier::class) ? true : false,
'delete' => Gate::allows('delete', Supplier::class) ? true : false,
'delete' => (Gate::allows('delete', Supplier::class) && ($supplier->assets_count == 0) && ($supplier->licenses_count == 0) && ($supplier->accessories_count == 0)) ? true : false,
];

$array += $permissions_array;
Expand Down
11 changes: 10 additions & 1 deletion app/Models/Accessory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Accessory extends SnipeModel
use Loggable, Presentable;
use SoftDeletes;

protected $dates = ['deleted_at', 'purchase_date'];
protected $dates = ['deleted_at'];
protected $table = 'accessories';
protected $casts = [
'requestable' => 'boolean'
Expand Down Expand Up @@ -61,10 +61,19 @@ class Accessory extends SnipeModel
'purchase_date',
'model_number',
'manufacturer_id',
'supplier_id',
'image',
'qty',
'requestable'
];


public function supplier()
{
return $this->belongsTo('\App\Models\Supplier', 'supplier_id');
}


public function setRequestableAttribute($value)
{
if ($value == '') {
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Supplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public function assets()
return $this->hasMany('\App\Models\Asset', 'supplier_id');
}

public function accessories()
{
return $this->hasMany('\App\Models\Accessory', 'supplier_id');
}

public function asset_maintenances()
{
return $this->hasMany('\App\Models\AssetMaintenance', 'supplier_id');
Expand Down
16 changes: 16 additions & 0 deletions app/Presenters/AccessoryPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public static function dataTableLayout()
"switchable" => true,
"title" => trans('general.id'),
"visible" => false
],[
"field" => "image",
"searchable" => false,
"sortable" => true,
"switchable" => true,
"title" => trans('admin/hardware/table.image'),
"visible" => true,
"formatter" => "imageFormatter"
], [
"field" => "company",
"searchable" => true,
Expand Down Expand Up @@ -63,6 +71,14 @@ public static function dataTableLayout()
"sortable" => true,
"title" => trans('general.manufacturer'),
"formatter" => "manufacturersLinkObjFormatter",
], [
"field" => "supplier",
"searchable" => true,
"sortable" => true,
"switchable" => true,
"title" => trans('general.supplier'),
"visible" => false,
"formatter" => "suppliersLinkObjFormatter"
], [
"field" => "location",
"searchable" => true,
Expand Down
11 changes: 7 additions & 4 deletions database/factories/AccessoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
'category_id' => 8,
'manufacturer_id' => 1,
'qty' => 10,
'min_amt' => 2
'min_amt' => 2,
'supplier_id' => rand(1,5)
];

});
Expand All @@ -36,7 +37,8 @@
'category_id' => 8,
'manufacturer_id' => 1,
'qty' => 15,
'min_amt' => 2
'min_amt' => 2,
'supplier_id' => rand(1,5)
];

});
Expand All @@ -48,15 +50,16 @@
'category_id' => 9,
'manufacturer_id' => 1,
'qty' => 13,
'min_amt' => 2
'min_amt' => 2,
'supplier_id' => rand(1,5)
];

});

$factory->state(App\Models\Accessory::class, 'microsoft-mouse', function ($faker) {

return [
'name' => 'Sculpt Comfort Mouse\'',
'name' => 'Sculpt Comfort Mouse',
'category_id' => 9,
'manufacturer_id' => 2,
'qty' => 13,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddImageAndSupplierToAccessories extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('accessories', function (Blueprint $table) {
$table->string('image')->nullable()->default(null);
$table->integer('supplier_id')->nullable()->default(null);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('accessories', function (Blueprint $table) {
$table->dropColumn('image');
$table->dropColumn('supplier_id');
});
}
}
1 change: 1 addition & 0 deletions public/uploads/accessories/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!.gitignore
Empty file.
Loading

0 comments on commit 4215a32

Please sign in to comment.