2
2
3
3
namespace App \Http \Controllers ;
4
4
5
+ use App \Models \Order ;
5
6
use App \Models \Product ;
6
7
use Illuminate \Http \Request ;
7
8
@@ -14,4 +15,60 @@ public function makeOrderGet(Product $product)
14
15
15
16
return view ("/order/make_order " , $ data );
16
17
}
18
+
19
+
20
+ public function makeOrderPost (Request $ request , Product $ product )
21
+ {
22
+
23
+ $ rules = [
24
+ 'address ' => 'required|max:255 ' ,
25
+ 'payment_method ' => 'required|numeric ' ,
26
+ 'quantity ' => 'required|numeric|gt:0|lt: ' . $ product ->stock ,
27
+ 'total_price ' => 'required|numeric|gte:0 ' ,
28
+ 'coupon_used ' => 'required|numeric|gte:0 ' ,
29
+ ];
30
+
31
+ $ message = [
32
+ 'payment_method.required ' => 'Please select the payment method ' ,
33
+ 'quantity.lt ' . $ product ->stock => '
34
+ sorry the current available stock is ' . $ product ->stock ,
35
+ ];
36
+
37
+ if ($ request ->payment_method == 1 ) {
38
+ $ rules ['bank_id ' ] = 'required|numeric ' ;
39
+ $ message ['bank_id.required ' ] = 'Please select the bank ' ;
40
+ }
41
+
42
+ $ validatedData = $ request ->validate ($ rules , $ message );
43
+
44
+ try {
45
+ $ data = [
46
+ "product_id " => $ product ->id ,
47
+ "user_id " => auth ()->user ()->id ,
48
+ "quantity " => $ validatedData ["quantity " ],
49
+ "address " => $ validatedData ["address " ],
50
+ "total_price " => $ validatedData ["total_price " ],
51
+ "payment_id " => $ validatedData ["payment_method " ],
52
+ "note_id " => ($ validatedData ["payment_method " ] == 1 ) ? 2 : 1 ,
53
+ "status_id " => 2 ,
54
+ "transaction_doc " => ($ validatedData ["payment_method " ] == 1 ) ? env ("IMAGE_PROOF " ) : null ,
55
+ "is_done " => 0 ,
56
+ "coupon_used " => $ validatedData ["coupon_used " ]
57
+ ];
58
+
59
+ if ($ validatedData ["payment_method " ] == 1 ) {
60
+ $ data ['bank_id ' ] = $ validatedData ["bank_id " ];
61
+ }
62
+
63
+ Order::create ($ data );
64
+
65
+ $ message = "Orders has been created! " ;
66
+
67
+ myFlasherBuilder (message: $ message , success: true );
68
+
69
+ return redirect ("/home " );
70
+ } catch (\Illuminate \Database \QueryException $ exception ) {
71
+ return abort (500 );
72
+ }
73
+ }
17
74
}
0 commit comments