2018-07-24 19:35:26 -07:00
< ? php
namespace App\Http\Controllers\Accessories ;
2018-09-10 07:40:26 -07:00
use App\Events\CheckoutableCheckedOut ;
2018-07-24 19:35:26 -07:00
use App\Http\Controllers\Controller ;
use App\Models\Accessory ;
use App\Models\User ;
use Carbon\Carbon ;
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\Auth ;
use Illuminate\Support\Facades\DB ;
use Illuminate\Support\Facades\Input ;
class AccessoryCheckoutController extends Controller
{
/**
* Return the form to checkout an Accessory to a user .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $accessoryId
* @ return View
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
public function create ( $accessoryId )
{
// Check if the accessory exists
if ( is_null ( $accessory = Accessory :: find ( $accessoryId ))) {
// Redirect to the accessory management page with error
return redirect () -> route ( 'accessories.index' ) -> with ( 'error' , trans ( 'admin/accessories/message.not_found' ));
}
if ( $accessory -> category ) {
$this -> authorize ( 'checkout' , $accessory );
// Get the dropdown of users and then pass it to the checkout view
return view ( 'accessories/checkout' , compact ( 'accessory' ));
}
return redirect () -> back () -> with ( 'error' , 'The category type for this accessory is not valid. Edit the accessory and select a valid accessory category.' );
}
/**
* 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 Request $request
* @ param int $accessoryId
* @ return Redirect
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
public function store ( Request $request , $accessoryId )
{
2021-06-10 13:15:52 -07:00
// Check if the accessory exists
2018-07-24 19:35:26 -07:00
if ( is_null ( $accessory = Accessory :: find ( $accessoryId ))) {
// Redirect to the accessory management page with error
return redirect () -> route ( 'accessories.index' ) -> with ( 'error' , trans ( 'admin/accessories/message.user_not_found' ));
}
$this -> authorize ( 'checkout' , $accessory );
2021-06-10 13:15:52 -07:00
if ( ! $user = User :: find ( $request -> input ( 'assigned_to' ))) {
2018-07-24 19:35:26 -07:00
return redirect () -> route ( 'checkout/accessory' , $accessory -> id ) -> with ( 'error' , trans ( 'admin/accessories/message.checkout.user_does_not_exist' ));
}
2021-06-10 13:15:52 -07:00
// Update the accessory data
2019-12-11 11:09:54 -08:00
$accessory -> assigned_to = e ( $request -> input ( 'assigned_to' ));
2018-07-24 19:35:26 -07:00
$accessory -> users () -> attach ( $accessory -> id , [
'accessory_id' => $accessory -> id ,
'created_at' => Carbon :: now (),
'user_id' => Auth :: id (),
2020-10-23 00:44:26 -07:00
'assigned_to' => $request -> get ( 'assigned_to' ),
2021-06-10 13:15:52 -07:00
'note' => $request -> input ( 'note' ),
2018-07-24 19:35:26 -07:00
]);
DB :: table ( 'accessories_users' ) -> where ( 'assigned_to' , '=' , $accessory -> assigned_to ) -> where ( 'accessory_id' , '=' , $accessory -> id ) -> first ();
2018-09-10 07:40:26 -07:00
event ( new CheckoutableCheckedOut ( $accessory , $user , Auth :: user (), $request -> input ( 'note' )));
2018-07-25 01:02:06 -07:00
2021-06-10 13:15:52 -07:00
// Redirect to the new accessory page
2018-07-24 19:35:26 -07:00
return redirect () -> route ( 'accessories.index' ) -> with ( 'success' , trans ( 'admin/accessories/message.checkout.success' ));
}
}