2021-06-10 13:15:52 -07:00
|
|
|
<?php
|
|
|
|
|
2018-07-27 16:15:32 -07:00
|
|
|
namespace App\Http\Controllers\Account;
|
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
use App\Events\CheckoutAccepted;
|
|
|
|
use App\Events\CheckoutDeclined;
|
2018-07-27 16:15:32 -07:00
|
|
|
use App\Events\ItemAccepted;
|
|
|
|
use App\Events\ItemDeclined;
|
|
|
|
use App\Http\Controllers\Controller;
|
2022-03-10 12:15:50 -08:00
|
|
|
use App\Models\Actionlog;
|
|
|
|
use App\Models\Asset;
|
2018-07-28 03:45:33 -07:00
|
|
|
use App\Models\CheckoutAcceptance;
|
2018-07-27 16:15:32 -07:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Contracts\Acceptable;
|
2022-03-10 12:15:50 -08:00
|
|
|
use App\Models\User;
|
2018-07-27 16:15:32 -07:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Illuminate\Support\Str;
|
2022-03-08 16:42:27 -08:00
|
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
2018-07-27 16:15:32 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
class AcceptanceController extends Controller
|
|
|
|
{
|
2018-07-28 04:32:29 -07:00
|
|
|
/**
|
|
|
|
* Show a listing of pending checkout acceptances for the current user
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2018-07-28 04:32:29 -07:00
|
|
|
* @return View
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
public function index()
|
|
|
|
{
|
2018-07-28 03:45:33 -07:00
|
|
|
$acceptances = CheckoutAcceptance::forUser(Auth::user())->pending()->get();
|
|
|
|
|
|
|
|
return view('account/accept.index', compact('acceptances'));
|
2018-07-27 16:15:32 -07:00
|
|
|
}
|
2018-07-28 04:32:29 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a form to either accept or decline the checkout acceptance
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2018-07-28 04:32:29 -07:00
|
|
|
* @param int $id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
public function create($id)
|
|
|
|
{
|
2018-07-28 03:45:33 -07:00
|
|
|
$acceptance = CheckoutAcceptance::find($id);
|
2018-07-27 16:15:32 -07:00
|
|
|
|
2022-03-08 16:42:27 -08:00
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
if (is_null($acceptance)) {
|
2018-09-29 21:33:52 -07:00
|
|
|
return redirect()->route('account.accept')->with('error', trans('admin/hardware/message.does_not_exist'));
|
2018-07-27 16:15:32 -07:00
|
|
|
}
|
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
if (! $acceptance->isPending()) {
|
2018-07-27 16:15:32 -07:00
|
|
|
return redirect()->route('account.accept')->with('error', trans('admin/users/message.error.asset_already_accepted'));
|
2021-06-10 13:15:52 -07:00
|
|
|
}
|
2018-07-27 16:15:32 -07:00
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
if (! $acceptance->isCheckedOutTo(Auth::user())) {
|
2018-07-27 16:15:32 -07:00
|
|
|
return redirect()->route('account.accept')->with('error', trans('admin/users/message.error.incorrect_user_accepted'));
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! Company::isCurrentUserHasAccess($acceptance->checkoutable)) {
|
2018-07-27 16:15:32 -07:00
|
|
|
return redirect()->route('account.accept')->with('error', trans('general.insufficient_permissions'));
|
2021-06-10 13:15:52 -07:00
|
|
|
}
|
2018-07-27 16:15:32 -07:00
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
return view('account/accept.create', compact('acceptance'));
|
2021-06-10 13:15:52 -07:00
|
|
|
}
|
2018-07-27 16:15:32 -07:00
|
|
|
|
2018-07-28 04:32:29 -07:00
|
|
|
/**
|
|
|
|
* Stores the accept/decline of the checkout acceptance
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2018-07-28 04:32:29 -07:00
|
|
|
* @param Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
public function store(Request $request, $id)
|
|
|
|
{
|
2018-07-28 03:45:33 -07:00
|
|
|
$acceptance = CheckoutAcceptance::find($id);
|
2018-07-27 16:15:32 -07:00
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
if (is_null($acceptance)) {
|
2018-09-29 21:33:52 -07:00
|
|
|
return redirect()->route('account.accept')->with('error', trans('admin/hardware/message.does_not_exist'));
|
2018-07-27 16:15:32 -07:00
|
|
|
}
|
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
if (! $acceptance->isPending()) {
|
2018-07-27 16:15:32 -07:00
|
|
|
return redirect()->route('account.accept')->with('error', trans('admin/users/message.error.asset_already_accepted'));
|
2021-06-10 13:15:52 -07:00
|
|
|
}
|
2018-07-27 16:15:32 -07:00
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
if (! $acceptance->isCheckedOutTo(Auth::user())) {
|
2018-07-27 16:15:32 -07:00
|
|
|
return redirect()->route('account.accept')->with('error', trans('admin/users/message.error.incorrect_user_accepted'));
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! Company::isCurrentUserHasAccess($acceptance->checkoutable)) {
|
2018-07-27 16:15:32 -07:00
|
|
|
return redirect()->route('account.accept')->with('error', trans('general.insufficient_permissions'));
|
2021-06-10 13:15:52 -07:00
|
|
|
}
|
2018-07-27 16:15:32 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $request->filled('asset_acceptance')) {
|
2018-07-27 16:15:32 -07:00
|
|
|
return redirect()->back()->with('error', trans('admin/users/message.error.accept_or_decline'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the signature and save it
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! Storage::exists('private_uploads/signatures')) {
|
|
|
|
Storage::makeDirectory('private_uploads/signatures', 775);
|
|
|
|
}
|
2022-03-08 16:42:27 -08:00
|
|
|
|
2020-10-23 05:09:03 -07:00
|
|
|
$sig_filename = '';
|
2018-07-27 16:15:32 -07:00
|
|
|
if ($request->filled('signature_output')) {
|
2021-06-10 13:15:52 -07:00
|
|
|
$sig_filename = 'siglog-'.Str::uuid().'-'.date('Y-m-d-his').'.png';
|
2018-07-27 16:15:32 -07:00
|
|
|
$data_uri = e($request->input('signature_output'));
|
2021-06-10 13:15:52 -07:00
|
|
|
$encoded_image = explode(',', $data_uri);
|
2018-07-27 16:15:32 -07:00
|
|
|
$decoded_image = base64_decode($encoded_image[1]);
|
2022-03-14 19:38:36 -07:00
|
|
|
$acceptance->stored_eula_file = 'private_uploads/eula-pdfs/accepted-eula-'.date('Y-m-d-h-i-s').'.pdf';
|
2022-03-08 16:42:27 -08:00
|
|
|
$path = Storage::put('private_uploads/signatures/'.$sig_filename, (string) $decoded_image);
|
2018-07-27 16:15:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->input('asset_acceptance') == 'accepted') {
|
2018-07-28 04:09:21 -07:00
|
|
|
$acceptance->accept($sig_filename);
|
2022-03-08 16:42:27 -08:00
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
event(new CheckoutAccepted($acceptance));
|
2018-07-27 16:15:32 -07:00
|
|
|
|
|
|
|
$return_msg = trans('admin/users/message.accepted');
|
2022-03-08 16:42:27 -08:00
|
|
|
|
|
|
|
|
2018-07-27 16:15:32 -07:00
|
|
|
} else {
|
2021-06-10 13:15:52 -07:00
|
|
|
$acceptance->decline($sig_filename);
|
2018-07-27 16:15:32 -07:00
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
event(new CheckoutDeclined($acceptance));
|
2018-07-27 16:15:32 -07:00
|
|
|
|
|
|
|
$return_msg = trans('admin/users/message.declined');
|
|
|
|
}
|
|
|
|
|
2022-03-08 16:42:27 -08:00
|
|
|
$item = $acceptance->checkoutable_type::find($acceptance->checkoutable_id);
|
2022-02-24 14:50:16 -08:00
|
|
|
|
|
|
|
$data = [
|
2022-03-08 16:42:27 -08:00
|
|
|
'item' => $item,
|
|
|
|
'eula' => $item->getEula(),
|
|
|
|
'signature' => storage_path().'/private_uploads/signatures/'.$sig_filename,
|
|
|
|
'logo' => public_path().'/uploads/snipe-logo.png',
|
2022-02-24 14:50:16 -08:00
|
|
|
];
|
2022-03-08 16:42:27 -08:00
|
|
|
|
|
|
|
\Log::error(storage_path().'/eula-pdfs/'.$sig_filename);
|
|
|
|
|
|
|
|
$pdf = Pdf::loadView('account.accept.accept-eula', $data);
|
2022-03-14 19:38:36 -07:00
|
|
|
Storage::put($acceptance->stored_eula_file, $pdf->output());
|
|
|
|
$a=new Actionlog();
|
|
|
|
$a->stored_eula = $item->getEula();
|
|
|
|
$a->stored_eula_file = $acceptance->stored_eula_file;
|
|
|
|
$a->save();
|
|
|
|
\log::info($a);
|
|
|
|
|
2022-03-08 16:42:27 -08:00
|
|
|
|
|
|
|
return redirect()->to('account/accept')->with('success', $return_msg);
|
2022-02-24 14:50:16 -08:00
|
|
|
}
|
2022-03-08 16:42:27 -08:00
|
|
|
}
|