From cff198277e14450f8d20d9e18548325e6f277682 Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Mon, 8 May 2023 16:40:15 +0700 Subject: raja ongkir --- src/lib/checkout/components/Checkout.jsx | 211 +++++++++++++++++++++++++++++-- 1 file changed, 203 insertions(+), 8 deletions(-) (limited to 'src/lib/checkout/components') diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index eee679fd..111117c9 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -19,6 +19,7 @@ import axios from 'axios' import Image from '@/core/components/elements/Image/Image' import MobileView from '@/core/components/views/MobileView' import DesktopView from '@/core/components/views/DesktopView' +import ExpedisiList from '../api/ExpedisiList' const Checkout = () => { const router = useRouter() @@ -61,7 +62,22 @@ const Checkout = () => { const [products, setProducts] = useState(null) const [totalAmount, setTotalAmount] = useState(0) const [totalDiscountAmount, setTotalDiscountAmount] = useState(0) + const [totalWeight, setTotalWeight] = useState(0) const [priceCheck, setPriceCheck] = useState(false) + const [listExpedisi, setExpedisi] = useState([]) + const [listserviceExpedisi, setListServiceExpedisi] = useState([]) + const [selectedExpedisi, setSelectedExpedisi] = useState(0) + const [biayaKirim, setBiayaKirim] = useState(null) + const [checkWeigth, setCheckWeight] = useState(false) + + useEffect(() => { + const loadExpedisi = async () => { + let dataExpedisi = await ExpedisiList() + dataExpedisi = dataExpedisi.map((city) => ({ value: city.id, label: city.name })) + setExpedisi(dataExpedisi) + } + loadExpedisi() + }, []) useEffect(() => { const loadProducts = async () => { @@ -95,16 +111,44 @@ const Checkout = () => { if (products) { let calculateTotalAmount = 0 let calculateTotalDiscountAmount = 0 + let calcuateTotalWeight = 0 products.forEach((product) => { calculateTotalAmount += product.price.price * product.quantity calculateTotalDiscountAmount += (product.price.price - product.price.priceDiscount) * product.quantity + calcuateTotalWeight += product.weight * product.quantity + if (product.weight == 0) { + setCheckWeight(true) + } }) setTotalAmount(calculateTotalAmount) setTotalDiscountAmount(calculateTotalDiscountAmount) + setTotalWeight(calcuateTotalWeight * 1000) } }, [products]) + useEffect(() => { + const loadServiceRajaOngkir = async () => { + const body = { + origin: 155, + destination: selectedAddress.shipping.rajaongkirCityId, + weight: totalWeight, + courier: selectedExpedisi, + originType: 'city', + destinationType: 'city' + } + setBiayaKirim(0) + const dataService = await axios('/api/rajaongkir-service?body=' + JSON.stringify(body)) + setListServiceExpedisi(dataService.data[0].costs) + } + if (selectedExpedisi != 0 && totalWeight > 0) { + loadServiceRajaOngkir() + } else { + setListServiceExpedisi() + setBiayaKirim(0) + } + }, [selectedExpedisi, selectedAddress, totalWeight]) + const poNumber = useRef(null) const poFile = useRef(null) @@ -125,6 +169,7 @@ const Checkout = () => { partner_shipping_id: selectedAddress.shipping.id, partner_invoice_id: selectedAddress.invoicing.id, order_line: JSON.stringify(productOrder), + delivery_amount : biayaKirim, type: 'sale_order' } if (poNumber.current.value) data.po_number = poNumber.current.value @@ -136,10 +181,11 @@ const Checkout = () => { return } - for (const product of products) deleteItemCart({ productId: product.id }) + // for (const product of products) deleteItemCart({ productId: product.id }) const payment = await axios.post( `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/midtrans-payment?transactionId=${isCheckouted.id}` ) + console.log(payment) setIsLoading(false) window.location.href = payment.data.redirectUrl } @@ -162,9 +208,50 @@ const Checkout = () => { - - - + {/* */} + + + + + + + + + {listserviceExpedisi && ( +
+
+
Service Type Expedisi :
+
+ +
+
+
+ )} +
{products && } @@ -195,12 +282,16 @@ const Checkout = () => {
PPN 11%
{currencyFormat(taxTotal)}
+
+
Biaya Kirim
+
{currencyFormat(biayaKirim)}
+

Grand Total
- {currencyFormat(totalAmount - totalDiscountAmount + taxTotal)} + {currencyFormat(totalAmount - totalDiscountAmount + taxTotal + biayaKirim)}
{/*

*) Belum termasuk biaya pengiriman

*/} @@ -262,8 +353,49 @@ const Checkout = () => {
- - + {/* */} + + + + + + + + + {listserviceExpedisi && ( +
+
+
Service Type Expedisi :
+
+ +
+
+
+ )}
@@ -298,6 +430,9 @@ const Checkout = () => { ? `| ${product?.attributes.join(', ')}` : ''}
+
+ Berat item : {product?.weight} Kg +
@@ -369,6 +504,10 @@ const Checkout = () => {
PPN 11%
{currencyFormat(taxTotal)}
+
+
Biaya Kirim
+
{currencyFormat(biayaKirim)}
+

@@ -376,7 +515,7 @@ const Checkout = () => {
Grand Total
- {currencyFormat(totalAmount - totalDiscountAmount + taxTotal)} + {currencyFormat(totalAmount - totalDiscountAmount + taxTotal + parseInt(biayaKirim))}

@@ -460,6 +599,62 @@ const SectionAddress = ({ address, label, url }) => ( )} ) + +const SectionValidation = ({ address, checkWeigth }) => ( + address?.rajaongkirCityId == 0 && ( +

+
+
+

+ SIlahkan Update alamat kirim melalui{' '} + + Link ini ! + +

+
+
+
+ ), + checkWeigth == true && ( +
+
+
+

+ Ada item barang yang belum di setting berat nya, silahkan hub admin di{' '} + + + Link InI ! + + +

+
+
+
+ ) +) + +const SectionExpedisi = ({ address, listExpedisi, setSelectedExpedisi, checkWeigth }) => + (address?.rajaongkirCityId > 0 && checkWeigth != true) && ( +
+
+
Pilih Expedisi :
+
+ +
+
+
+ ) const PickupAddress = ({ label }) => (
-- cgit v1.2.3