2016-03-25 15:50:08 -07:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
namespace App\Http\Controllers;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
2016-03-25 01:18:05 -07:00
|
|
|
use App\Models\Company;
|
2016-12-15 15:48:30 -08:00
|
|
|
use Illuminate\Http\Request;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-07-04 14:37:58 -07:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use \Illuminate\Contracts\View\View;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
|
|
|
* This controller handles all actions related to Companies for
|
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v1.0
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
final class CompaniesController extends Controller
|
|
|
|
{
|
2016-03-28 22:51:49 -07:00
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns view to display listing of companies.
|
|
|
|
*
|
|
|
|
* @author [Abdullah Alansari] [<ahimta@gmail.com>]
|
|
|
|
* @since [v1.8]
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function index() : View
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-07-12 18:28:02 -07:00
|
|
|
$this->authorize('view', Company::class);
|
|
|
|
|
2018-12-12 18:23:39 -08:00
|
|
|
return view('companies/index');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-03-28 22:51:49 -07:00
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns view to create a new company.
|
|
|
|
*
|
|
|
|
* @author [Abdullah Alansari] [<ahimta@gmail.com>]
|
|
|
|
* @since [v1.8]
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function create() : View
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-07-12 18:28:02 -07:00
|
|
|
$this->authorize('create', Company::class);
|
|
|
|
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('companies/edit')->with('item', new Company);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-03-28 22:51:49 -07:00
|
|
|
/**
|
2016-12-19 22:00:50 -08:00
|
|
|
* Save data from new company form.
|
|
|
|
*
|
|
|
|
* @author [Abdullah Alansari] [<ahimta@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @param Request $request
|
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function store(ImageUploadRequest $request) : RedirectResponse
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-07-12 18:28:02 -07:00
|
|
|
$this->authorize('create', Company::class);
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
$company = new Company;
|
2016-12-19 22:00:50 -08:00
|
|
|
$company->name = $request->input('name');
|
2023-07-10 11:44:21 -07:00
|
|
|
$company->phone = $request->input('phone');
|
|
|
|
$company->fax = $request->input('fax');
|
2023-08-13 08:18:25 -07:00
|
|
|
$company->email = $request->input('email');
|
2025-02-10 16:32:07 -08:00
|
|
|
$company->notes = $request->input('notes');
|
2024-09-19 12:34:54 -07:00
|
|
|
$company->created_by = auth()->id();
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2020-08-24 15:43:11 -07:00
|
|
|
$company = $request->handleImages($company);
|
2017-10-25 22:35:58 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($company->save()) {
|
2016-12-15 15:48:30 -08:00
|
|
|
return redirect()->route('companies.index')
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('success', trans('admin/companies/message.create.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
return redirect()->back()->withInput()->withErrors($company->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-03-28 22:51:49 -07:00
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Return form to edit existing company.
|
|
|
|
*
|
|
|
|
* @author [Abdullah Alansari] [<ahimta@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @param int $companyId
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2025-02-18 18:49:05 -08:00
|
|
|
public function edit(Company $company) : View | RedirectResponse
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2025-02-18 18:49:05 -08:00
|
|
|
$this->authorize('update', $company);
|
|
|
|
return view('companies/edit');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-03-28 22:51:49 -07:00
|
|
|
/**
|
2016-12-19 22:00:50 -08:00
|
|
|
* Save data from edit company form.
|
|
|
|
*
|
|
|
|
* @author [Abdullah Alansari] [<ahimta@gmail.com>]
|
|
|
|
* @since [v1.8]
|
2018-07-24 19:35:26 -07:00
|
|
|
* @param ImageUploadRequest $request
|
2016-12-19 22:00:50 -08:00
|
|
|
* @param int $companyId
|
|
|
|
*/
|
2025-02-18 18:49:05 -08:00
|
|
|
public function update(ImageUploadRequest $request, Company $company) : RedirectResponse
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
|
2018-07-12 18:28:02 -07:00
|
|
|
$this->authorize('update', $company);
|
2016-12-19 22:00:50 -08:00
|
|
|
$company->name = $request->input('name');
|
2023-07-10 11:44:21 -07:00
|
|
|
$company->phone = $request->input('phone');
|
|
|
|
$company->fax = $request->input('fax');
|
2023-08-13 08:18:25 -07:00
|
|
|
$company->email = $request->input('email');
|
2025-02-10 16:32:07 -08:00
|
|
|
$company->notes = $request->input('notes');
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2020-08-24 15:43:11 -07:00
|
|
|
$company = $request->handleImages($company);
|
2017-10-25 22:35:58 -07:00
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
if ($company->save()) {
|
|
|
|
return redirect()->route('companies.index')
|
|
|
|
->with('success', trans('admin/companies/message.update.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2023-08-13 08:18:25 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($company->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-03-28 22:51:49 -07:00
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Delete company
|
|
|
|
*
|
|
|
|
* @author [Abdullah Alansari] [<ahimta@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @param int $companyId
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function destroy($companyId) : RedirectResponse
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
if (is_null($company = Company::find($companyId))) {
|
2016-12-15 15:48:30 -08:00
|
|
|
return redirect()->route('companies.index')
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('error', trans('admin/companies/message.not_found'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2018-09-29 21:33:52 -07:00
|
|
|
|
2020-05-23 10:36:02 -07:00
|
|
|
$this->authorize('delete', $company);
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $company->isDeletable()) {
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route('companies.index')
|
|
|
|
->with('error', trans('admin/companies/message.assoc_users'));
|
2020-05-23 10:36:02 -07:00
|
|
|
}
|
2018-07-24 19:35:26 -07:00
|
|
|
|
2020-05-23 10:36:02 -07:00
|
|
|
if ($company->image) {
|
2021-06-10 13:15:52 -07:00
|
|
|
try {
|
2020-05-23 10:36:02 -07:00
|
|
|
Storage::disk('public')->delete('companies'.'/'.$company->image);
|
|
|
|
} catch (\Exception $e) {
|
2024-05-29 04:38:15 -07:00
|
|
|
Log::debug($e);
|
2020-05-23 10:36:02 -07:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2020-05-23 10:36:02 -07:00
|
|
|
|
|
|
|
$company->delete();
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2020-05-23 10:36:02 -07:00
|
|
|
return redirect()->route('companies.index')
|
|
|
|
->with('success', trans('admin/companies/message.delete.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2017-02-08 08:48:41 -08:00
|
|
|
|
2025-02-18 18:49:05 -08:00
|
|
|
public function show(Company $company) : View | RedirectResponse
|
2021-06-10 13:15:52 -07:00
|
|
|
{
|
2017-02-08 08:48:41 -08:00
|
|
|
$this->authorize('view', Company::class);
|
2021-06-10 13:15:52 -07:00
|
|
|
return view('companies/view')->with('company', $company);
|
2017-02-08 08:48:41 -08:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|