summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/checkout/api/checkoutApi.js8
-rw-r--r--src/lib/checkout/components/Checkout.jsx67
2 files changed, 64 insertions, 11 deletions
diff --git a/src/lib/checkout/api/checkoutApi.js b/src/lib/checkout/api/checkoutApi.js
index fd982fff..39d25068 100644
--- a/src/lib/checkout/api/checkoutApi.js
+++ b/src/lib/checkout/api/checkoutApi.js
@@ -18,3 +18,11 @@ export const getProductsCheckout = async (query) => {
const result = await odooApi('GET', url);
return result;
};
+
+async function checkVoucherApplicability(voucherId, productCategoryId) {
+ const response = await fetch(
+ `/api/voucher/${voucherId}/check_applicability?category_id=${productCategoryId}`
+ );
+ const data = await response.json();
+ return data.is_applicable; // true jika voucher berlaku untuk kategori, false jika tidak
+}
diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx
index c9bff3c1..9ccebe72 100644
--- a/src/lib/checkout/components/Checkout.jsx
+++ b/src/lib/checkout/components/Checkout.jsx
@@ -39,18 +39,20 @@ const { getProductsCheckout } = require('../api/checkoutApi');
function convertToInternational(number) {
if (typeof number !== 'string') {
- throw new Error("Input harus berupa string");
+ throw new Error('Input harus berupa string');
}
if (number.startsWith('08')) {
- return '+62' + number.slice(2);
+ return '+62' + number.slice(2);
}
return number;
}
const Checkout = () => {
- const PPN = process.env.NEXT_PUBLIC_PPN ? parseFloat(process.env.NEXT_PUBLIC_PPN) : 0;
+ const PPN = process.env.NEXT_PUBLIC_PPN
+ ? parseFloat(process.env.NEXT_PUBLIC_PPN)
+ : 0;
const router = useRouter();
const query = router.query.source ?? null;
const qVoucher = router.query.voucher ?? null;
@@ -145,16 +147,37 @@ const Checkout = () => {
if (!listVouchers) {
try {
setLoadingVoucher(true);
+ const productCategories = products
+ ?.reduce((categories, product) => {
+ console.log('Processing product:', product.name);
+ console.log('Product categories:', product.categories);
+
+ if (product.categories && Array.isArray(product.categories)) {
+ product.categories.forEach((category) => {
+ if (category.id && !categories.includes(category.id)) {
+ categories.push(category.id);
+ }
+ });
+ }
+ return categories;
+ }, [])
+ .join(',');
+
+ console.log('Final categories string:', productCategories);
+
let dataVoucher = await getVoucher(auth?.id, {
source: query,
type: 'all,brand',
- partner_id : auth?.partnerId,
+ partner_id: auth?.partnerId,
+ voucher_category: productCategories, // Add the product categories
});
+ console.log('All vouchers received:', dataVoucher);
SetListVoucher(dataVoucher);
let dataVoucherShipping = await getVoucher(auth?.id, {
source: query,
type: 'shipping',
+ voucher_category: productCategories, // Add the product categories
});
SetListVoucherShipping(dataVoucherShipping);
} finally {
@@ -164,10 +187,25 @@ const Checkout = () => {
};
const VoucherCode = async (code) => {
- // let dataVoucher = await findVoucher(code, auth.id, query);
+ const productCategories = products
+ ?.reduce((categories, product) => {
+ if (product.categories && Array.isArray(product.categories)) {
+ product.categories.forEach((category) => {
+ if (category.id && !categories.includes(category.id)) {
+ categories.push(category.id);
+ }
+ });
+ }
+ return categories;
+ }, [])
+ .join(',');
+
+ console.log('Voucher code search with categories:', productCategories);
+
let dataVoucher = await getVoucher(auth?.id, {
source: query,
code: code,
+ voucher_category: productCategories, // Add the product categories
});
if (dataVoucher.length <= 0) {
SetFindVoucher(1);
@@ -517,10 +555,12 @@ const Checkout = () => {
gtag('set', 'user_data', {
email: auth.email,
- phone_number: convertToInternational(auth.mobile) ?? convertToInternational(auth.phone),
+ phone_number:
+ convertToInternational(auth.mobile) ??
+ convertToInternational(auth.phone),
});
- gtag('config', 'AW-954540379', { ' allow_enhanced_conversions':true } ) ;
+ gtag('config', 'AW-954540379', { ' allow_enhanced_conversions': true });
for (const product of products) deleteItemCart({ productId: product.id });
if (grandTotal > 0) {
@@ -1223,7 +1263,9 @@ const Checkout = () => {
<div>{currencyFormat(cartCheckout?.subtotal)}</div>
</div>
<div className='flex gap-x-2 justify-between'>
- <div className='text-gray_r-11'>PPN {((PPN - 1) * 100).toFixed(0)}%</div>
+ <div className='text-gray_r-11'>
+ PPN {((PPN - 1) * 100).toFixed(0)}%
+ </div>
<div>{currencyFormat(cartCheckout?.tax)}</div>
</div>
<div className='flex gap-x-2 justify-between'>
@@ -1527,7 +1569,9 @@ const Checkout = () => {
<div>{currencyFormat(cartCheckout?.subtotal)}</div>
</div>
<div className='flex gap-x-2 justify-between'>
- <div className='text-gray_r-11'>PPN {((PPN - 1) * 100).toFixed(0)}%</div>
+ <div className='text-gray_r-11'>
+ PPN {((PPN - 1) * 100).toFixed(0)}%
+ </div>
<div>{currencyFormat(cartCheckout?.tax)}</div>
</div>
<div className='flex gap-x-2 justify-between'>
@@ -1710,7 +1754,8 @@ const SectionAddress = ({ address, label, url }) => (
);
const SectionValidation = ({ address }) =>
- address?.stateId == 0 || address?.rajaongkirCityId == 0 && (
+ address?.stateId == 0 ||
+ (address?.rajaongkirCityId == 0 && (
<BottomPopup active={true} title='Update Alamat'>
<div className='leading-7 text-gray_r-12/80'>
Mohon untuk memperbarui alamat Anda dengan mengklik tombol di bawah ini.{' '}
@@ -1724,7 +1769,7 @@ const SectionValidation = ({ address }) =>
</Link>
</div>
</BottomPopup>
- );
+ ));
const SectionExpedisi = ({
address,