2018-11-13 09:33:32 -08:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-11-13 09:33:32 -08:00
|
|
|
namespace App\Http\Controllers\Kits;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Http\Controllers\CheckInOutRequest;
|
|
|
|
use App\Http\Controllers\Controller;
|
2018-11-13 09:33:32 -08:00
|
|
|
use App\Models\PredefinedKit;
|
|
|
|
use App\Models\PredefinedLicence;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Models\PredefinedModel;
|
2018-11-13 09:33:32 -08:00
|
|
|
use App\Models\User;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Services\PredefinedKitCheckoutService;
|
|
|
|
use Illuminate\Http\Request;
|
2019-05-23 19:39:30 -07:00
|
|
|
use Illuminate\Support\Arr;
|
2018-11-13 09:33:32 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This controller handles all access kits management:
|
|
|
|
* list, add/remove/change
|
|
|
|
*
|
2019-02-23 11:44:03 -08:00
|
|
|
* @author [D. Minaev.] [<dmitriy.minaev.v@gmail.com>]
|
2018-11-13 09:33:32 -08:00
|
|
|
*/
|
|
|
|
class CheckoutKitController extends Controller
|
|
|
|
{
|
|
|
|
public $kitService;
|
|
|
|
use CheckInOutRequest;
|
|
|
|
|
2019-02-23 11:44:03 -08:00
|
|
|
public function __construct(PredefinedKitCheckoutService $kitService)
|
2018-11-13 09:33:32 -08:00
|
|
|
{
|
|
|
|
$this->kitService = $kitService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-23 11:44:03 -08:00
|
|
|
* Show Bulk Checkout Page
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2019-02-23 11:44:03 -08:00
|
|
|
* @author [D. Minaev.] [<dmitriy.minaev.v@gmail.com>]
|
|
|
|
* @return View View to checkout
|
|
|
|
*/
|
2018-11-13 09:33:32 -08:00
|
|
|
public function showCheckout($kit_id)
|
|
|
|
{
|
|
|
|
$this->authorize('checkout', Asset::class);
|
|
|
|
|
|
|
|
$kit = PredefinedKit::findOrFail($kit_id);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-11-13 09:33:32 -08:00
|
|
|
return view('kits/checkout')->with('kit', $kit);
|
|
|
|
}
|
2019-02-23 11:44:03 -08:00
|
|
|
|
2018-11-13 09:33:32 -08:00
|
|
|
/**
|
2019-02-23 11:44:03 -08:00
|
|
|
* Validate and process the new Predefined Kit data.
|
|
|
|
*
|
|
|
|
* @author [D. Minaev.] [<dmitriy.minaev.v@gmail.com>]
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2018-11-13 09:33:32 -08:00
|
|
|
public function store(Request $request, $kit_id)
|
|
|
|
{
|
|
|
|
$user_id = e($request->get('user_id'));
|
2019-02-23 11:44:03 -08:00
|
|
|
if (is_null($user = User::find($user_id))) {
|
2018-11-13 09:33:32 -08:00
|
|
|
return redirect()->back()->with('error', trans('admin/users/message.user_not_found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$kit = new PredefinedKit();
|
|
|
|
$kit->id = $kit_id;
|
|
|
|
|
2019-05-23 19:39:30 -07:00
|
|
|
$checkout_result = $this->kitService->checkout($request, $kit, $user);
|
2021-06-10 13:15:52 -07:00
|
|
|
if (Arr::has($checkout_result, 'errors') && count($checkout_result['errors']) > 0) {
|
2019-05-23 19:39:30 -07:00
|
|
|
return redirect()->back()->with('error', 'Checkout error')->with('error_messages', $checkout_result['errors']); // TODO: trans
|
2018-11-13 09:33:32 -08:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2019-05-23 19:39:30 -07:00
|
|
|
return redirect()->back()->with('success', 'Checkout was successful')
|
|
|
|
->with('assets', Arr::get($checkout_result, 'assets', null))
|
|
|
|
->with('accessories', Arr::get($checkout_result, 'accessories', null))
|
|
|
|
->with('consumables', Arr::get($checkout_result, 'consumables', null)); // TODO: trans
|
2018-11-13 09:33:32 -08:00
|
|
|
}
|
|
|
|
}
|