summaryrefslogtreecommitdiff
path: root/src/pages/shop/checkout.js
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-02-02 17:13:12 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-02-02 17:13:12 +0700
commitd4d4227dfb2fefa56ded8ff5897469459f56b069 (patch)
tree46b1572614684e7472b60b696d148749cdc71f77 /src/pages/shop/checkout.js
parentbe2bc90edc387966cb1b23c60a80e4b5fcf4bec9 (diff)
no message
Diffstat (limited to 'src/pages/shop/checkout.js')
-rw-r--r--src/pages/shop/checkout.js105
1 files changed, 56 insertions, 49 deletions
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() {
<div className="mt-4 text-caption-1">
<p className="font-medium">{ selectedAddress.shipping.name }</p>
<p className="mt-2 text-gray_r-11">{ selectedAddress.shipping.mobile }</p>
- <p className="mt-1 text-gray_r-11">{ selectedAddress.shipping.street }, { selectedAddress.shipping.city.name }</p>
+ <p className="mt-1 text-gray_r-11">{ selectedAddress.shipping.street }, { selectedAddress.shipping?.city?.name }</p>
</div>
) }
</div>
@@ -219,22 +224,26 @@ export default function Checkout() {
<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>
+ <p>Total Belanja</p>
+ <p className="font-medium">{currencyFormat(totalAmount)}</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 className="flex gap-x-2 justify-between">
+ <p>Subtotal</p>
+ <p className="font-medium">{currencyFormat(totalAmount - totalDiscountAmount)}</p>
+ </div>
+ <div className="flex gap-x-2 justify-between">
+ <p>PPN 11% (Incl.)</p>
+ <p className="font-medium">{currencyFormat((totalAmount - totalDiscountAmount) * 0.11)}</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>
+ <p className="font-medium text-yellow_r-11">{currencyFormat(totalAmount - 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">
@@ -285,20 +294,8 @@ export default function Checkout() {
<div className="mt-4 flex gap-x-3">
<div className="w-6/12">
- <label className="form-label font-normal">Nomor PO</label>
- <input
- type="text"
- className="form-input mt-2 h-12"
- value={poNumber}
- onChange={(e) => setPoNumber(e.target.value)}
- />
- </div>
- <div className="w-6/12">
<label className="form-label font-normal">
- File PO
- {auth?.company && (
- <span className="font-normal text-gray_r-11 ml-1">(Wajib diisi)</span>
- )}
+ Dokumen PO
</label>
<input
type="file"
@@ -307,7 +304,17 @@ export default function Checkout() {
onChange={(e) => setPoFile(e.target.files[0])}
/>
</div>
+ <div className="w-6/12">
+ <label className="form-label font-normal">Nomor PO</label>
+ <input
+ type="text"
+ className="form-input mt-2 h-12"
+ value={poNumber}
+ onChange={(e) => setPoNumber(e.target.value)}
+ />
+ </div>
</div>
+ <p className="text-caption-2 text-gray_r-11 mt-2">Ukuran dokumen PO Maksimal 5MB</p>
</div>
<LineDivider/>