1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
import { ExclamationCircleIcon } from "@heroicons/react/24/solid";
import { useEffect, useState } from "react";
import { toast } from "react-hot-toast";
import Alert from "../../components/Alert";
import AppBar from "../../components/AppBar";
import Image from "../../components/Image";
import Layout from "../../components/Layout";
import LineDivider from "../../components/LineDivider";
import Link from "../../components/Link";
import ProgressBar from "../../components/ProgressBar";
import Spinner from "../../components/Spinner";
import apiOdoo from "../../helpers/apiOdoo";
import { useAuth } from "../../helpers/auth";
import { getCart } from "../../helpers/cart";
import currencyFormat from "../../helpers/currencyFormat";
import { createSlug } from "../../helpers/slug";
export default function Checkout() {
const [auth] = useAuth();
const [address, setAddress] = useState(null);
const [selectedAddress, setSelectedAddress] = useState(null);
const [selectedPayment, setSelectedPayment] = useState(null);
const [products, setProducts] = useState(null);
const [totalPriceBeforeTax, setTotalPriceBeforeTax] = useState(0);
const [totalTaxAmount, setTotalTaxAmount] = useState(0);
const [totalDiscountAmount, setTotalDiscountAmount] = useState(0);
const [isLoading, setIsLoading] = useState(true);
const payments = [
{ name: 'BCA', number: '8870-4000-81' },
{ name: 'MANDIRI', number: '155-0067-6869-75' },
];
useEffect(() => {
const getAddress = async () => {
if (auth) {
const dataAddress = await apiOdoo('GET', `/api/v1/user/${auth.id}/address`);
setAddress(dataAddress);
}
};
getAddress();
}, [auth]);
useEffect(() => {
const getProducts = async () => {
let cart = getCart();
let productIds = Object
.values(cart)
.filter((itemCart) => itemCart.to_process == true)
.map((itemCart) => itemCart.product_id);
if (productIds.length > 0) {
productIds = productIds.join(',');
let dataProducts = await apiOdoo('GET', `/api/v1/product_variant/${productIds}`);
dataProducts = dataProducts.map((product) => ({
...product,
quantity: cart[product.id].quantity,
to_process: cart[product.id].to_process,
}));
setProducts(dataProducts);
}
};
getProducts();
}, []);
useEffect(() => {
if (address) setSelectedAddress(address[0]);
}, [address]);
useEffect(() => {
if (products) {
const productsToProcess = products.filter((product) => product.to_process == true);
let calculateTotalPriceBeforeTax = 0;
let calculateTotalTaxAmount = 0;
let calculateTotalDiscountAmount = 0;
productsToProcess.forEach(product => {
let priceBeforeTax = product.price.price / 1.11;
calculateTotalPriceBeforeTax += priceBeforeTax * product.quantity;
calculateTotalTaxAmount += (product.price.price - priceBeforeTax) * product.quantity;
calculateTotalDiscountAmount += (product.price.price - product.price.price_discount) * product.quantity;
});
setTotalPriceBeforeTax(calculateTotalPriceBeforeTax);
setTotalTaxAmount(calculateTotalTaxAmount);
setTotalDiscountAmount(calculateTotalDiscountAmount);
}
}, [products]);
useEffect(() => {
if (address && products) setIsLoading(false);
}, [address, products]);
return (
<Layout>
<AppBar title="Checkout" />
{isLoading && (
<div className="flex justify-center items-center gap-x-3 mt-14">
<Spinner className="w-10 text-gray_r-8 fill-gray_r-12" />
</div>
)}
{ products && address && (
<>
<ProgressBar
current={2}
labels={['Keranjang', 'Pembayaran', 'Selesai']}
/>
<LineDivider/>
<Alert type="info" className="text-caption-2 flex gap-x-3 items-center my-2">
<div>
<ExclamationCircleIcon className="w-6 text-blue-700"/>
</div>
<span>Jika mengalami kesulitan dalam melakukan pembelian di website Indoteknik. Hubungi kami disini</span>
</Alert>
<LineDivider/>
<div className="p-4">
<div className="flex justify-between items-center">
<h2>Alamat Pengiriman</h2>
<Link className="text-caption-1" href="/">Ubah Alamat</Link>
</div>
{ selectedAddress && (
<div className="mt-4 text-caption-1">
<p className="font-medium">{ selectedAddress.name }</p>
<p className="mt-2 text-gray_r-11">{ selectedAddress.mobile }</p>
<p className="mt-1 text-gray_r-11">{ selectedAddress.street } { selectedAddress.street2 }</p>
</div>
) }
</div>
<LineDivider/>
<div className="p-4 flex flex-col gap-y-4">
{products.map((product, index) => (
<div className="flex gap-x-3" key={index}>
<div className="w-4/12 flex items-center gap-x-2">
<Image
src={product.parent.image}
alt={product.parent.name}
className="object-contain object-center border border-gray_r-6 h-32 w-full rounded-md"
/>
</div>
<div className="w-8/12 flex flex-col">
<p className="product-card__title wrap-line-ellipsis-2">
{product.parent.name}
</p>
<p className="text-caption-2 text-gray_r-11 mt-1">
{product.code || '-'}
{product.attributes.length > 0 ? ` | ${product.attributes.join(', ')}` : ''}
</p>
<p className="text-caption-2 text-gray_r-11 mt-1">
{currencyFormat(product.price.price_discount)} × {product.quantity} Barang
</p>
<p className="text-caption-2 text-gray_r-12 font-bold mt-2">
{currencyFormat(product.quantity * product.price.price_discount)}
</p>
</div>
</div>
))}
</div>
<LineDivider/>
<div className="p-4">
<div className="flex justify-between items-center">
<h2>Ringkasan Pesanan</h2>
<p className="text-gray_r-11 text-caption-1">{products.length} Barang</p>
</div>
<hr className="my-4 border-gray_r-6"/>
<div className="flex flex-col gap-y-4">
<div className="flex gap-x-2 justify-between">
<p>Subtotal</p>
<p className="font-medium">{currencyFormat(totalPriceBeforeTax)}</p>
</div>
<div className="flex gap-x-2 justify-between">
<p>PPN 11%</p>
<p className="font-medium">{currencyFormat(totalTaxAmount)}</p>
</div>
<div className="flex gap-x-2 justify-between">
<p>Total Diskon</p>
<p className="font-medium text-red_r-11">- {currencyFormat(totalDiscountAmount)}</p>
</div>
</div>
<hr className="my-4 border-gray_r-6"/>
<div className="flex gap-x-2 justify-between mb-4">
<p>Grand Total</p>
<p className="font-medium text-yellow_r-11">{currencyFormat(totalPriceBeforeTax + totalTaxAmount - totalDiscountAmount)}</p>
</div>
<p className="text-caption-2 text-gray_r-10 mb-2">*) Belum termasuk biaya pengiriman</p>
<p className="text-caption-2 text-gray_r-10 leading-5">
Dengan melakukan pembelian melalui website Indoteknik, saya menyetujui <Link href="/">Syarat & Ketentuan</Link> yang berlaku
</p>
</div>
<LineDivider/>
<div className="p-4">
<h2>Pilih Metode Pembayaran</h2>
<div className="grid gap-y-3 mt-4">
{ payments.map((payment, index) => (
<button
type="button"
className={"text-left border border-gray_r-6 rounded-md p-3 " + (selectedPayment == payment.name && 'border-yellow_r-10 bg-yellow_r-3')}
onClick={() => setSelectedPayment(payment.name)}
key={index}
>
<p>{payment.name} - {payment.number}</p>
<p className="mt-1 text-gray_r-11">PT. Indoteknik Dotcom Gemilang</p>
</button>
)) }
</div>
</div>
<LineDivider/>
<div className="flex gap-x-3 p-4">
<button
className="flex-1 btn-yellow"
onClick={() => router.push('/shop/checkout')}
>
Bayar
</button>
</div>
</>
) }
</Layout>
)
}
|