From d4d4227dfb2fefa56ded8ff5897469459f56b069 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 2 Feb 2023 17:13:12 +0700 Subject: no message --- src/pages/shop/checkout.js | 105 +++++++++++++++++++++------------------ src/pages/shop/product/[slug].js | 78 +++++++++++++++++++---------- 2 files changed, 107 insertions(+), 76 deletions(-) (limited to 'src/pages/shop') diff --git a/src/pages/shop/checkout.js b/src/pages/shop/checkout.js index f55b200f..875cf0f1 100644 --- a/src/pages/shop/checkout.js +++ b/src/pages/shop/checkout.js @@ -20,6 +20,7 @@ import VariantCard from "@/components/variants/VariantCard"; export default function Checkout() { const router = useRouter(); + const { product_id, qty } = router.query; const [ auth ] = useAuth(); const [ addresses, setAddresses ] = useState(null); const [ poNumber, setPoNumber ] = useState(''); @@ -30,8 +31,7 @@ export default function Checkout() { }); const [ selectedPayment, setSelectedPayment ] = useState(null); const [ products, setProducts ] = useState(null); - const [ totalPriceBeforeTax, setTotalPriceBeforeTax ] = useState(0); - const [ totalTaxAmount, setTotalTaxAmount ] = useState(0); + const [ totalAmount, setTotalAmount ] = useState(0); const [ totalDiscountAmount, setTotalDiscountAmount ] = useState(0); const [ finishCheckout, setFinishCheckout ] = useState(null); @@ -53,25 +53,34 @@ export default function Checkout() { useEffect(() => { const getProducts = async () => { let cart = getCart(); - let productIds = Object - .values(cart) - .filter((itemCart) => itemCart.selected == true) - .map((itemCart) => itemCart.product_id); + let productIds = []; + if (product_id) { + productIds = [parseInt(product_id)]; + } else { + productIds = Object + .values(cart) + .filter((itemCart) => itemCart.selected == 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, - selected: cart[product.id].selected, - })); + dataProducts = dataProducts.map((product) => { + if (product_id) { + product.quantity = 1; + if (qty) product.quantity = parseInt(qty); + } else { + product.quantity = cart[product.id].quantity; + } + return product; + }); setProducts(dataProducts); } else { if (auth) router.push('/shop/cart'); } }; getProducts(); - }, [router, auth]); + }, [router, auth, product_id, qty]); useEffect(() => { if (addresses) { @@ -92,18 +101,13 @@ export default function Checkout() { useEffect(() => { if (products) { - const productsSelected = products.filter((product) => product.selected == true); - let calculateTotalPriceBeforeTax = 0; - let calculateTotalTaxAmount = 0; + let calculateTotalAmount = 0; let calculateTotalDiscountAmount = 0; - productsSelected.forEach(product => { - let priceBeforeTax = product.price.price / 1.11; - calculateTotalPriceBeforeTax += priceBeforeTax * product.quantity; - calculateTotalTaxAmount += (product.price.price - priceBeforeTax) * product.quantity; + products.forEach(product => { + calculateTotalAmount += product.price.price * product.quantity; calculateTotalDiscountAmount += (product.price.price - product.price.price_discount) * product.quantity; }); - setTotalPriceBeforeTax(calculateTotalPriceBeforeTax); - setTotalTaxAmount(calculateTotalTaxAmount); + setTotalAmount(calculateTotalAmount); setTotalDiscountAmount(calculateTotalDiscountAmount); } }, [products]); @@ -115,17 +119,18 @@ export default function Checkout() { }); return; } + if (poFile && poFile.size > 5000000) { + toast.error('Maksimal ukuran file adalah 5MB', { + position: 'bottom-center' + }); + return; + } let productOrder = products.map((product) => ({ 'product_id': product.id, 'quantity': product.quantity })); let data = { 'partner_shipping_id': selectedAddress.shipping.id, 'partner_invoice_id': selectedAddress.invoicing.id, - 'order_line': JSON.stringify(productOrder) - }; - if (auth?.company && !poFile) { - toast.error('Mohon isi file PO', { - position: 'bottom-center' - }); - return; + 'order_line': JSON.stringify(productOrder), + 'type': 'sale_order' }; if (poNumber) data.po_number = poNumber; if (poFile) data.po_file = await getFileBase64(poFile); @@ -192,7 +197,7 @@ export default function Checkout() {

{ selectedAddress.shipping.name }

{ selectedAddress.shipping.mobile }

-

{ selectedAddress.shipping.street }, { selectedAddress.shipping.city.name }

+

{ selectedAddress.shipping.street }, { selectedAddress.shipping?.city?.name }

) } @@ -219,22 +224,26 @@ export default function Checkout() {
-

Subtotal

-

{currencyFormat(totalPriceBeforeTax)}

-
-
-

PPN 11%

-

{currencyFormat(totalTaxAmount)}

+

Total Belanja

+

{currencyFormat(totalAmount)}

Total Diskon

- {currencyFormat(totalDiscountAmount)}

+
+

Subtotal

+

{currencyFormat(totalAmount - totalDiscountAmount)}

+
+
+

PPN 11% (Incl.)

+

{currencyFormat((totalAmount - totalDiscountAmount) * 0.11)}

+

Grand Total

-

{currencyFormat(totalPriceBeforeTax + totalTaxAmount - totalDiscountAmount)}

+

{currencyFormat(totalAmount - totalDiscountAmount)}

*) Belum termasuk biaya pengiriman

@@ -284,21 +293,9 @@ export default function Checkout() {

Purchase Order

-
- - setPoNumber(e.target.value)} - /> -
setPoFile(e.target.files[0])} />
+
+ + setPoNumber(e.target.value)} + /> +
+

Ukuran dokumen PO Maksimal 5MB

diff --git a/src/pages/shop/product/[slug].js b/src/pages/shop/product/[slug].js index bcfb12ba..281f2bc2 100644 --- a/src/pages/shop/product/[slug].js +++ b/src/pages/shop/product/[slug].js @@ -138,6 +138,23 @@ export default function ProductDetail({ product }) { return true; } + const checkoutProduct = () => { + if (!auth) { + toast.error('Login terlebih dahulu untuk melanjutkan', { duration: 2000 }); + router.push('/login'); + return; + } + if (product.variant_total > 1 && !selectedVariant) { + toast.error('Pilih varian terlebih dahulu untuk melanjutkan pembelian', { duration: 2000 }); + return; + } + if (quantity < 0) { + toast.error('Jumlah barang yang ditambahkan minimal 1 pcs', { duration: 2000 }); + return; + } + router.push(`/shop/checkout?product_id=${activeVariant.id}&qty=${quantity}`); + } + return ( <>
@@ -187,36 +204,38 @@ export default function ProductDetail({ product }) {
-
-
- - -
-
- - -
+
+ +
+
- + + +
@@ -230,7 +249,7 @@ export default function ProductDetail({ product }) {

{product.variant_total} Varian

-

Nomor SKU

+

No. SKU

SKU-{activeVariant.id}

@@ -239,9 +258,14 @@ export default function ProductDetail({ product }) {

Stok

-

- {activeVariant.stock > 0 ? (activeVariant.stock > 5 ? 'Lebih dari 5' : 'Kurang dari 5') : '0'} -

+
+ {activeVariant.stock > 0 ? (activeVariant.stock > 5 && ( + <> +
Ready Stock
+
{activeVariant.stock > 5 ? '> 5' : '< 5'}
+ + )) : '0'} +

Berat Barang

@@ -259,7 +283,7 @@ export default function ProductDetail({ product }) {
-

Produk Lainnya

+

Kamu Mungkin Juga Suka

-- cgit v1.2.3