2018-07-27 16:15:32 -07:00
|
|
|
<?php
|
|
|
|
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;
|
|
|
|
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\Consumable;
|
|
|
|
use App\Models\Contracts\Acceptable;
|
|
|
|
use App\Models\License;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
class AcceptanceController extends Controller {
|
|
|
|
|
|
|
|
public function index(Request $request) {
|
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 03:45:33 -07:00
|
|
|
public function create(Request $request, $id) {
|
2018-07-27 16:15:32 -07:00
|
|
|
|
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-07-27 16:15:32 -07:00
|
|
|
return redirect()->reoute('account.accept')->with('error', trans('admin/hardware/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
|
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'));
|
|
|
|
}
|
|
|
|
|
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'));
|
|
|
|
}
|
|
|
|
|
2018-07-28 03:45:33 -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'));
|
|
|
|
}
|
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
return view('account/accept.create', compact('acceptance'));
|
2018-07-27 16:15:32 -07:00
|
|
|
}
|
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
public function store(Request $request, $id) {
|
|
|
|
|
|
|
|
$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-07-27 16:15:32 -07:00
|
|
|
return redirect()->reoute('account.accept')->with('error', trans('admin/hardware/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
|
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'));
|
2018-07-28 03:45:33 -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'));
|
|
|
|
}
|
|
|
|
|
2018-07-28 03:45:33 -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'));
|
2018-07-28 03:45:33 -07:00
|
|
|
}
|
2018-07-27 16:15:32 -07:00
|
|
|
|
|
|
|
if (!$request->filled('asset_acceptance')) {
|
|
|
|
return redirect()->back()->with('error', trans('admin/users/message.error.accept_or_decline'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the signature and save it
|
|
|
|
*/
|
|
|
|
if ($request->filled('signature_output')) {
|
|
|
|
$path = config('app.private_uploads').'/signatures';
|
|
|
|
$sig_filename = "siglog-" .Str::uuid() . '-'.date('Y-m-d-his').".png";
|
|
|
|
$data_uri = e($request->input('signature_output'));
|
|
|
|
$encoded_image = explode(",", $data_uri);
|
|
|
|
$decoded_image = base64_decode($encoded_image[1]);
|
|
|
|
file_put_contents($path."/".$sig_filename, $decoded_image);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($request->input('asset_acceptance') == 'accepted') {
|
|
|
|
|
2018-07-28 04:09:21 -07:00
|
|
|
$acceptance->accept($sig_filename);
|
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');
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2018-07-28 04:09:21 -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');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->to('account/accept')->with('success', $return_msg);
|
|
|
|
}
|
|
|
|
}
|