Skip to content

Commit 48b0306

Browse files
committed
make order
1 parent 7780181 commit 48b0306

File tree

6 files changed

+202
-102
lines changed

6 files changed

+202
-102
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@ VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
6161

6262
IMAGE_PROFILE=profile/cV8nuMT7VBfYYtANwUxegJ366XDbw0nXdxLEvehk.jpg
6363
IMAGE_PRODUCT=product/sPISped9AR4XyNKlkSpiUyo7OkcJj8mSBmPuF6Ky.png
64+
IMAGE_PROOF=proof/fmg7fWMmb7mNvnHHA70IlRXxRF4wsD9J6dQAUZkV.png
6465

6566
API_RAJAONGKIR=YOUR_API_KEY

app/Http/Controllers/OrderController.php

+57
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Models\Order;
56
use App\Models\Product;
67
use Illuminate\Http\Request;
78

@@ -14,4 +15,60 @@ public function makeOrderGet(Product $product)
1415

1516
return view("/order/make_order", $data);
1617
}
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+
}
1774
}

app/Models/Order.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Order extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $guarded = ['id'];
13+
}

public/js/order.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function hideMessage(payment_method) {
2+
if (
3+
$("#" + payment_method + "_alert").html() != null &&
4+
$("#" + payment_method + "_alert").css("display") != "none"
5+
) {
6+
$("#" + payment_method + "_alert").css("display", "none");
7+
} else if (payment_method == "cod") {
8+
$("#bank_alert").css("display", "block");
9+
} else {
10+
$("#cod_alert").css("display", "block");
11+
}
12+
}
13+
14+
function hideBankMessage() {
15+
$("#bank_id_alert").css("display") != "none";
16+
}

0 commit comments

Comments
 (0)