2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Helpers\Helper;
|
|
|
|
use App\Models\Accessory;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\User;
|
Partialize forms (#2884)
* Consolidate edit form elements into reusable partials.
This is a large code change that doesn't do much immediately. It
refactors all of the various edit.blade.php files to reference
standardized partials, so that they all reference the same base html
layout. This has the side effect of moving everything to the new fancy
"required" indicators, and making things look consistent.
In addition, I've gone ahead and renamed a few database fields. We had
Assetmodel::modelno and Consumable::model_no, I've renamed both to
model_number. We had items using ::note and ::notes, I've standardized
on ::notes. Component used total_qty where consumables and accessories
used qty, so I've moved everything to qty (And fixed a few bugs in the
helper file in the process.
TODO includes looking at how/where to place the modal javascripts to
allow for on the fly creation from all places, rather than just the
asset page.
Rename assetmodel::modelno to model_number for clarity and consistency
Rename consumable::model_no to model_number for clarity and consistency
Rename assetmodel::note to notes for clarity and consistency
Port asset and assetmodel to new partials layout. Adapt all code to the renamed model_number and notes database changes. Fix some stying.
* Share a settings variable with all views.
* Allow editing the per_page setting. We showed the value, but we never showed it on the edit page..
* use snipeSettings in all views instead of the long ugly path.
* War on partials. Centralize all bootstrap table javascript
* Use model_number instead of modelno in importer
* Codacy fix.
* More unification/deduplication. Create an edit form template layout that we use as the base for all edit forms. This gives the same interface for editing everything and makes the edit.blade.* files much easier to read.
* Use a ViewComposer instead of sharing the variable directly. Fixes artisan optimize trying to hit the db--which ruins new installs
* Fix DB seeder.
* Base sql dump and csv's to import data from for tests.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* A few things to make acceptance tests work. Add a name to the companies table, and make the locations table have the correct name
* Use a .env.tests file for testing functional and unit to allow a separate database.
* Add functional tests for compoents, groups, and licenses.
* Now that the config is in the functional.yml, this just confuses things.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* update db dump
* Update tests to new reality
* env for the test setup
* only load the database at beginning of tests, not between each Functional test.
* Fix a miss from renaming note to notes.
* Set Termination date when creating an asset. It was only set on edit.
* Rename serial_number to serial in components for consistency.
* Update validation rules to match limits in database. Currently we just accepted the values and they were truncated when adding to DB.
* Much more detailed functional testing of creating items. This checks to make sure all values on form have been successfully persisted to database.
2016-11-16 16:56:57 -08:00
|
|
|
use Auth;
|
2016-05-12 21:01:31 -07:00
|
|
|
use Carbon\Carbon;
|
2016-03-25 01:18:05 -07:00
|
|
|
use Config;
|
|
|
|
use DB;
|
Partialize forms (#2884)
* Consolidate edit form elements into reusable partials.
This is a large code change that doesn't do much immediately. It
refactors all of the various edit.blade.php files to reference
standardized partials, so that they all reference the same base html
layout. This has the side effect of moving everything to the new fancy
"required" indicators, and making things look consistent.
In addition, I've gone ahead and renamed a few database fields. We had
Assetmodel::modelno and Consumable::model_no, I've renamed both to
model_number. We had items using ::note and ::notes, I've standardized
on ::notes. Component used total_qty where consumables and accessories
used qty, so I've moved everything to qty (And fixed a few bugs in the
helper file in the process.
TODO includes looking at how/where to place the modal javascripts to
allow for on the fly creation from all places, rather than just the
asset page.
Rename assetmodel::modelno to model_number for clarity and consistency
Rename consumable::model_no to model_number for clarity and consistency
Rename assetmodel::note to notes for clarity and consistency
Port asset and assetmodel to new partials layout. Adapt all code to the renamed model_number and notes database changes. Fix some stying.
* Share a settings variable with all views.
* Allow editing the per_page setting. We showed the value, but we never showed it on the edit page..
* use snipeSettings in all views instead of the long ugly path.
* War on partials. Centralize all bootstrap table javascript
* Use model_number instead of modelno in importer
* Codacy fix.
* More unification/deduplication. Create an edit form template layout that we use as the base for all edit forms. This gives the same interface for editing everything and makes the edit.blade.* files much easier to read.
* Use a ViewComposer instead of sharing the variable directly. Fixes artisan optimize trying to hit the db--which ruins new installs
* Fix DB seeder.
* Base sql dump and csv's to import data from for tests.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* A few things to make acceptance tests work. Add a name to the companies table, and make the locations table have the correct name
* Use a .env.tests file for testing functional and unit to allow a separate database.
* Add functional tests for compoents, groups, and licenses.
* Now that the config is in the functional.yml, this just confuses things.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* update db dump
* Update tests to new reality
* env for the test setup
* only load the database at beginning of tests, not between each Functional test.
* Fix a miss from renaming note to notes.
* Set Termination date when creating an asset. It was only set on edit.
* Rename serial_number to serial in components for consistency.
* Update validation rules to match limits in database. Currently we just accepted the values and they were truncated when adding to DB.
* Much more detailed functional testing of creating items. This checks to make sure all values on form have been successfully persisted to database.
2016-11-16 16:56:57 -08:00
|
|
|
use Gate;
|
2016-03-25 01:18:05 -07:00
|
|
|
use Input;
|
|
|
|
use Lang;
|
|
|
|
use Mail;
|
|
|
|
use Redirect;
|
2016-12-15 15:47:08 -08:00
|
|
|
use Illuminate\Http\Request;
|
2016-03-25 01:18:05 -07:00
|
|
|
use Slack;
|
|
|
|
use Str;
|
|
|
|
use View;
|
2017-10-19 16:25:24 -07:00
|
|
|
use Image;
|
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-04-12 19:24:02 -07:00
|
|
|
/** This controller handles all actions related to Accessories for
|
2016-04-07 13:21:09 -07:00
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v1.0
|
2016-03-25 01:18:05 -07:00
|
|
|
*/
|
|
|
|
class AccessoriesController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the accessories listing, which is generated in getDatatable.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see AccessoriesController::getDatatable() method that generates the JSON response
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-12-15 04:09:40 -08:00
|
|
|
public function index(Request $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-12-19 11:04:28 -08:00
|
|
|
$this->authorize('index', Accessory::class);
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('accessories/index');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view with a form to create a new Accessory.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-12-15 04:09:40 -08:00
|
|
|
public function create(Request $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-12-19 11:04:28 -08:00
|
|
|
$this->authorize('create', Accessory::class);
|
2017-10-28 11:17:52 -07:00
|
|
|
$category_type = 'accessory';
|
|
|
|
return view('accessories/edit')->with('category_type', $category_type)
|
2017-10-28 09:21:39 -07:00
|
|
|
->with('item', new Accessory);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate and save new Accessory from form post
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2017-10-19 16:25:24 -07:00
|
|
|
public function store(ImageUploadRequest $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-12-19 11:04:28 -08:00
|
|
|
$this->authorize(Accessory::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// create a new model instance
|
|
|
|
$accessory = new Accessory();
|
|
|
|
|
|
|
|
// Update the accessory data
|
2016-12-19 22:00:50 -08:00
|
|
|
$accessory->name = request('name');
|
|
|
|
$accessory->category_id = request('category_id');
|
|
|
|
$accessory->location_id = request('location_id');
|
|
|
|
$accessory->min_amt = request('min_amt');
|
|
|
|
$accessory->company_id = Company::getIdForCurrentUser(request('company_id'));
|
|
|
|
$accessory->order_number = request('order_number');
|
|
|
|
$accessory->manufacturer_id = request('manufacturer_id');
|
|
|
|
$accessory->model_number = request('model_number');
|
2016-12-26 15:17:46 -08:00
|
|
|
$accessory->purchase_date = request('purchase_date');
|
|
|
|
$accessory->purchase_cost = Helper::ParseFloat(request('purchase_cost'));
|
2016-12-19 22:00:50 -08:00
|
|
|
$accessory->qty = request('qty');
|
2016-08-16 18:49:54 -07:00
|
|
|
$accessory->user_id = Auth::user()->id;
|
2017-10-19 16:25:24 -07:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
// Was the accessory created?
|
|
|
|
if ($accessory->save()) {
|
|
|
|
// Redirect to the new accessory page
|
2016-12-15 04:09:40 -08:00
|
|
|
return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.create.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($accessory->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return view for the Accessory update form, prepopulated with existing data
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $accessoryId
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-12-15 04:09:40 -08:00
|
|
|
public function edit(Request $request, $accessoryId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2017-10-28 11:17:52 -07:00
|
|
|
|
|
|
|
if ($item = Accessory::find($accessoryId)) {
|
|
|
|
$this->authorize($item);
|
|
|
|
$category_type = 'accessory';
|
|
|
|
return view('accessories/edit', compact('item'))->with('category_type', $category_type);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2017-10-28 11:17:52 -07:00
|
|
|
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist'));
|
2016-12-19 11:04:28 -08:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save edited Accessory from form post
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $accessoryId
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2017-10-19 16:25:24 -07:00
|
|
|
public function update(ImageUploadRequest $request, $accessoryId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
if (is_null($accessory = Accessory::find($accessoryId))) {
|
2016-12-15 04:09:40 -08:00
|
|
|
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-12-19 11:04:28 -08:00
|
|
|
$this->authorize($accessory);
|
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
// Update the accessory data
|
2016-12-26 15:17:46 -08:00
|
|
|
$accessory->name = request('name');
|
|
|
|
$accessory->location_id = request('location_id');
|
2016-12-19 22:00:50 -08:00
|
|
|
$accessory->min_amt = request('min_amt');
|
|
|
|
$accessory->category_id = request('category_id');
|
|
|
|
$accessory->company_id = Company::getIdForCurrentUser(request('company_id'));
|
|
|
|
$accessory->manufacturer_id = request('manufacturer_id');
|
|
|
|
$accessory->order_number = request('order_number');
|
|
|
|
$accessory->model_number = request('model_number');
|
2017-10-19 16:25:24 -07:00
|
|
|
$accessory->purchase_date = request('purchase_date');
|
|
|
|
$accessory->purchase_cost = request('purchase_cost');
|
2016-12-19 22:00:50 -08:00
|
|
|
$accessory->qty = request('qty');
|
2017-10-19 16:25:24 -07:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2017-10-19 16:25:24 -07:00
|
|
|
// Was the accessory updated?
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($accessory->save()) {
|
2016-12-15 04:09:40 -08:00
|
|
|
return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.update.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($accessory->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the given accessory.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $accessoryId
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-12-15 04:09:40 -08:00
|
|
|
public function destroy(Request $request, $accessoryId)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
if (is_null($accessory = Accessory::find($accessoryId))) {
|
2016-12-15 04:09:40 -08:00
|
|
|
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.not_found'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-12-19 11:04:28 -08:00
|
|
|
$this->authorize($accessory);
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
if ($accessory->hasUsers() > 0) {
|
2016-12-15 04:09:40 -08:00
|
|
|
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.assoc_users', array('count'=> $accessory->hasUsers())));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
$accessory->delete();
|
|
|
|
return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.delete.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view that invokes the ajax table which contains
|
|
|
|
* the content for the accessory detail view, which is generated in getDataView.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @param int $accessoryID
|
2016-03-25 01:18:05 -07:00
|
|
|
* @see AccessoriesController::getDataView() method that generates the JSON response
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-12-15 04:09:40 -08:00
|
|
|
public function show(Request $request, $accessoryID = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
$accessory = Accessory::find($accessoryID);
|
2016-12-19 11:04:28 -08:00
|
|
|
$this->authorize('view', $accessory);
|
2016-03-25 01:18:05 -07:00
|
|
|
if (isset($accessory->id)) {
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('accessories/view', compact('accessory'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2017-10-18 08:53:25 -07:00
|
|
|
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist', compact('id')));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the form to checkout an Accessory to a user.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $accessoryId
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-03-25 15:24:12 -07:00
|
|
|
public function getCheckout(Request $request, $accessoryId)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// Check if the accessory exists
|
|
|
|
if (is_null($accessory = Accessory::find($accessoryId))) {
|
|
|
|
// Redirect to the accessory management page with error
|
2016-12-19 11:04:28 -08:00
|
|
|
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.not_found'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-12-19 11:04:28 -08:00
|
|
|
$this->authorize('checkout', $accessory);
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
// Get the dropdown of users and then pass it to the checkout view
|
2017-10-26 03:43:28 -07:00
|
|
|
return view('accessories/checkout', compact('accessory'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the Accessory checkout information.
|
|
|
|
*
|
|
|
|
* If Slack is enabled and/or asset acceptance is enabled, it will also
|
|
|
|
* trigger a Slack message and send an email.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $accessoryId
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-03-25 15:24:12 -07:00
|
|
|
public function postCheckout(Request $request, $accessoryId)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// Check if the accessory exists
|
|
|
|
if (is_null($accessory = Accessory::find($accessoryId))) {
|
|
|
|
// Redirect to the accessory management page with error
|
2016-12-15 20:02:47 -08:00
|
|
|
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.user_not_found'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-12-19 11:04:28 -08:00
|
|
|
$this->authorize('checkout', $accessory);
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if (!$user = User::find(Input::get('assigned_to'))) {
|
2017-10-26 03:43:28 -07:00
|
|
|
return redirect()->route('checkout/accessory', $accessory->id)->with('error', trans('admin/accessories/message.checkout.user_does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the accessory data
|
2016-12-26 15:19:04 -08:00
|
|
|
$accessory->assigned_to = e(Input::get('assigned_to'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-12-26 15:19:04 -08:00
|
|
|
$accessory->users()->attach($accessory->id, [
|
|
|
|
'accessory_id' => $accessory->id,
|
|
|
|
'created_at' => Carbon::now(),
|
|
|
|
'user_id' => Auth::id(),
|
|
|
|
'assigned_to' => $request->get('assigned_to')
|
|
|
|
]);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2017-08-31 11:14:21 -07:00
|
|
|
$logaction = $accessory->logCheckout(e(Input::get('note')), $user);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
DB::table('accessories_users')->where('assigned_to', '=', $accessory->assigned_to)->where('accessory_id', '=', $accessory->id)->first();
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
$data['log_id'] = $logaction->id;
|
|
|
|
$data['eula'] = $accessory->getEula();
|
|
|
|
$data['first_name'] = $user->first_name;
|
|
|
|
$data['item_name'] = $accessory->name;
|
|
|
|
$data['checkout_date'] = $logaction->created_at;
|
|
|
|
$data['item_tag'] = '';
|
|
|
|
$data['expected_checkin'] = '';
|
|
|
|
$data['note'] = $logaction->note;
|
|
|
|
$data['require_acceptance'] = $accessory->requireAcceptance();
|
2016-12-26 15:19:04 -08:00
|
|
|
// TODO: Port this to new mail notifications
|
2017-10-26 03:43:28 -07:00
|
|
|
|
2017-09-28 20:57:33 -07:00
|
|
|
if ((($accessory->requireAcceptance()=='1') || ($accessory->getEula())) && ($user->email!='')) {
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
Mail::send('emails.accept-accessory', $data, function ($m) use ($user) {
|
|
|
|
$m->to($user->email, $user->first_name . ' ' . $user->last_name);
|
2016-09-20 07:20:10 -07:00
|
|
|
$m->replyTo(config('mail.reply_to.address'), config('mail.reply_to.name'));
|
2016-09-26 14:13:07 -07:00
|
|
|
$m->subject(trans('mail.Confirm_accessory_delivery'));
|
2016-03-25 01:18:05 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the new accessory page
|
2016-12-15 04:09:40 -08:00
|
|
|
return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.checkout.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
/**
|
|
|
|
* Check the accessory back into inventory
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param Request $request
|
|
|
|
* @param integer $accessoryUserId
|
|
|
|
* @param string $backto
|
|
|
|
* @return View
|
|
|
|
* @internal param int $accessoryId
|
|
|
|
*/
|
2016-03-25 15:24:12 -07:00
|
|
|
public function getCheckin(Request $request, $accessoryUserId = null, $backto = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// Check if the accessory exists
|
|
|
|
if (is_null($accessory_user = DB::table('accessories_users')->find($accessoryUserId))) {
|
|
|
|
// Redirect to the accessory management page with error
|
2016-12-15 04:09:40 -08:00
|
|
|
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.not_found'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$accessory = Accessory::find($accessory_user->accessory_id);
|
2016-12-19 11:04:28 -08:00
|
|
|
$this->authorize('checkin', $accessory);
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('accessories/checkin', compact('accessory'))->with('backto', $backto);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
/**
|
|
|
|
* Check in the item so that it can be checked out again to someone else
|
|
|
|
*
|
|
|
|
* @uses Accessory::checkin_email() to determine if an email can and should be sent
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param Request $request
|
|
|
|
* @param integer $accessoryUserId
|
|
|
|
* @param string $backto
|
|
|
|
* @return Redirect
|
|
|
|
* @internal param int $accessoryId
|
|
|
|
*/
|
2016-03-25 15:24:12 -07:00
|
|
|
public function postCheckin(Request $request, $accessoryUserId = null, $backto = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// Check if the accessory exists
|
|
|
|
if (is_null($accessory_user = DB::table('accessories_users')->find($accessoryUserId))) {
|
|
|
|
// Redirect to the accessory management page with error
|
2018-03-02 19:26:41 -08:00
|
|
|
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$accessory = Accessory::find($accessory_user->accessory_id);
|
|
|
|
|
2016-12-19 11:04:28 -08:00
|
|
|
$this->authorize('checkin', $accessory);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-03-25 15:24:12 -07:00
|
|
|
$return_to = e($accessory_user->assigned_to);
|
2016-11-17 15:54:29 -08:00
|
|
|
$logaction = $accessory->logCheckin(User::find($return_to), e(Input::get('note')));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
// Was the accessory updated?
|
2016-03-25 01:18:05 -07:00
|
|
|
if (DB::table('accessories_users')->where('id', '=', $accessory_user->id)->delete()) {
|
|
|
|
if (!is_null($accessory_user->assigned_to)) {
|
|
|
|
$user = User::find($accessory_user->assigned_to);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['log_id'] = $logaction->id;
|
2016-03-25 15:24:12 -07:00
|
|
|
$data['first_name'] = e($user->first_name);
|
2018-03-02 19:26:41 -08:00
|
|
|
$data['last_name'] = e($user->last_name);
|
2016-03-25 15:24:12 -07:00
|
|
|
$data['item_name'] = e($accessory->name);
|
|
|
|
$data['checkin_date'] = e($logaction->created_at);
|
2016-03-25 01:18:05 -07:00
|
|
|
$data['item_tag'] = '';
|
2016-03-25 15:24:12 -07:00
|
|
|
$data['note'] = e($logaction->note);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2017-09-28 20:57:33 -07:00
|
|
|
if ((($accessory->checkin_email()=='1')) && ($user->email!='')) {
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
Mail::send('emails.checkin-asset', $data, function ($m) use ($user) {
|
|
|
|
$m->to($user->email, $user->first_name . ' ' . $user->last_name);
|
2016-09-20 07:20:10 -07:00
|
|
|
$m->replyTo(config('mail.reply_to.address'), config('mail.reply_to.name'));
|
2016-09-26 14:13:07 -07:00
|
|
|
$m->subject(trans('mail.Confirm_Accessory_Checkin'));
|
2016-03-25 01:18:05 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($backto=='user') {
|
2016-12-19 11:04:28 -08:00
|
|
|
return redirect()->route("users.show", $return_to)->with('success', trans('admin/accessories/message.checkin.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
return redirect()->route("accessories.show", $accessory->id)->with('success', trans('admin/accessories/message.checkin.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
// Redirect to the accessory management page with error
|
2016-12-15 04:09:40 -08:00
|
|
|
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.checkin.error'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|