1010use Illuminate \Http \Request ;
1111use \Illuminate \Contracts \View \View ;
1212use \Illuminate \Http \RedirectResponse ;
13+ use Illuminate \Support \Facades \DB ;
1314
1415class ConsumableCheckoutController extends Controller
1516{
@@ -63,8 +64,9 @@ public function create($id) : View | RedirectResponse
6364 */
6465 public function store (Request $ request , $ consumableId )
6566 {
66- if (is_null ($ consumable = Consumable::with ('users ' )->find ($ consumableId ))) {
67- return redirect ()->route ('consumables.index ' )->with ('error ' , trans ('admin/consumables/message.not_found ' ));
67+ if (is_null ($ consumable = Consumable::find ($ consumableId ))) {
68+ return redirect ()->route ('consumables.index ' )
69+ ->with ('error ' , trans ('admin/consumables/message.not_found ' ));
6870 }
6971
7072 $ this ->authorize ('checkout ' , $ consumable );
@@ -74,10 +76,16 @@ public function store(Request $request, $consumableId)
7476 if (!isset ($ quantity ) || !ctype_digit ((string )$ quantity ) || $ quantity <= 0 ) {
7577 $ quantity = 1 ;
7678 }
79+ // attaching large amounts of checkouts can exhaust memory.
80+ if ($ quantity > 10000 ){
81+ return redirect ()->back ()
82+ ->with ('error ' , trans ('admin/consumables/message.checkout.large_quantity_error ' , ['requested ' => $ quantity , 'remaining ' => $ consumable ->numRemaining () ]));
83+ }
7784
7885 // Make sure there is at least one available to checkout
79- if ($ consumable ->numRemaining () <= 0 || $ quantity > $ consumable ->numRemaining ()) {
80- return redirect ()->route ('consumables.index ' )->with ('error ' , trans ('admin/consumables/message.checkout.unavailable ' , ['requested ' => $ quantity , 'remaining ' => $ consumable ->numRemaining () ]));
86+ if ($ consumable ->numRemaining () <= 0 || $ quantity > $ consumable ->numRemaining () ){
87+ return redirect ()->route ('consumables.index ' )
88+ ->with ('error ' , trans ('admin/consumables/message.checkout.unavailable ' , ['requested ' => $ quantity , 'remaining ' => $ consumable ->numRemaining () ]));
8189 }
8290
8391 $ admin_user = auth ()->user ();
@@ -86,26 +94,35 @@ public function store(Request $request, $consumableId)
8694 // Check if the user exists
8795 if (is_null ($ user = User::find ($ assigned_to ))) {
8896 // Redirect to the consumable management page with error
89- return redirect ()->route ('consumables.checkout.show ' , $ consumable )->with ('error ' , trans ('admin/consumables/message.checkout.user_does_not_exist ' ))->withInput ();
97+ return redirect ()->route ('consumables.checkout.show ' , $ consumable )
98+ ->with ('error ' , trans ('admin/consumables/message.checkout.user_does_not_exist ' ))
99+ ->withInput ();
90100 }
91-
92- // Update the consumable data
93- $ consumable ->assigned_to = e ($ request ->input ('assigned_to ' ));
94-
95- for ($ i = 0 ; $ i < $ quantity ; $ i ++){
96- $ consumable ->users ()->attach ($ consumable ->id , [
97- 'consumable_id ' => $ consumable ->id ,
98- 'created_by ' => $ admin_user ->id ,
99- 'assigned_to ' => e ($ request ->input ('assigned_to ' )),
100- 'note ' => $ request ->input ('note ' ),
101- ]);
101+ $ now = now ();
102+
103+ try {
104+ $ data = [
105+ 'consumable_id ' => $ consumable ->id ,
106+ 'created_by ' => $ admin_user ->id ,
107+ 'assigned_to ' => $ assigned_to ,
108+ 'note ' => $ request ->input ('note ' ) ?: null ,
109+ 'created_at ' => $ now ,
110+ 'updated_at ' => $ now ,
111+ ];
112+
113+ // Update the consumable data
114+ $ attachData = array_fill (0 ,$ quantity , $ data );
115+
116+ DB ::transaction (function () use ($ consumable , $ attachData , $ user , $ request ) {
117+ $ consumable ->users ()->attach ($ attachData );
118+ event (new CheckoutableCheckedOut ($ consumable , $ user , auth ()->user (), $ request ->input ('note ' )));
119+ });
120+ }catch (\Exception $ e ){
121+ report ($ e );
122+ return redirect ()->back ()->with ('error ' , trans ('admin/consumables/message.checkout.checkout_error ' ));
102123 }
103124
104- $ consumable ->checkout_qty = $ quantity ;
105- event (new CheckoutableCheckedOut ($ consumable , $ user , auth ()->user (), $ request ->input ('note ' )));
106-
107- $ request ->request ->add (['checkout_to_type ' => 'user ' ]);
108- $ request ->request ->add (['assigned_user ' => $ user ->id ]);
125+ $ request ->request ->add (['checkout_to_type ' => 'user ' , 'assigned_user ' => $ user ->id ]);
109126
110127 session ()->put (['redirect_option ' => $ request ->get ('redirect_option ' ), 'checkout_to_type ' => $ request ->get ('checkout_to_type ' )]);
111128
0 commit comments