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 ( {isLoading && (
)} { products && address && ( <>
Jika mengalami kesulitan dalam melakukan pembelian di website Indoteknik. Hubungi kami disini

Alamat Pengiriman

Ubah Alamat
{ selectedAddress && (

{ selectedAddress.name }

{ selectedAddress.mobile }

{ selectedAddress.street } { selectedAddress.street2 }

) }
{products.map((product, index) => (
{product.parent.name}

{product.parent.name}

{product.code || '-'} {product.attributes.length > 0 ? ` | ${product.attributes.join(', ')}` : ''}

{currencyFormat(product.price.price_discount)} × {product.quantity} Barang

{currencyFormat(product.quantity * product.price.price_discount)}

))}

Ringkasan Pesanan

{products.length} Barang


Subtotal

{currencyFormat(totalPriceBeforeTax)}

PPN 11%

{currencyFormat(totalTaxAmount)}

Total Diskon

- {currencyFormat(totalDiscountAmount)}


Grand Total

{currencyFormat(totalPriceBeforeTax + totalTaxAmount - totalDiscountAmount)}

*) Belum termasuk biaya pengiriman

Dengan melakukan pembelian melalui website Indoteknik, saya menyetujui Syarat & Ketentuan yang berlaku

Pilih Metode Pembayaran

{ payments.map((payment, index) => ( )) }
) }
) }