From fdfb47c3a825258b871ac5921605642e5e05fdd8 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Tue, 21 Feb 2023 12:04:20 +0700 Subject: fix --- src/lib/transaction/api/transactionApi.js | 10 ++ src/lib/transaction/api/uploadPoApi.js | 10 ++ src/lib/transaction/components/Transaction.jsx | 149 ++++++++++++++++++++++++ src/lib/transaction/components/Transactions.jsx | 32 ++--- src/lib/transaction/hooks/useTransaction.js | 13 +++ src/lib/transaction/utils/transactions.js | 12 +- src/lib/variant/components/VariantCard.jsx | 97 +++++++++++++++ src/lib/variant/components/VariantGroupCard.jsx | 33 ++++++ 8 files changed, 339 insertions(+), 17 deletions(-) create mode 100644 src/lib/transaction/api/transactionApi.js create mode 100644 src/lib/transaction/api/uploadPoApi.js create mode 100644 src/lib/transaction/components/Transaction.jsx create mode 100644 src/lib/transaction/hooks/useTransaction.js create mode 100644 src/lib/variant/components/VariantCard.jsx create mode 100644 src/lib/variant/components/VariantGroupCard.jsx (limited to 'src/lib') diff --git a/src/lib/transaction/api/transactionApi.js b/src/lib/transaction/api/transactionApi.js new file mode 100644 index 00000000..7186f847 --- /dev/null +++ b/src/lib/transaction/api/transactionApi.js @@ -0,0 +1,10 @@ +import odooApi from "@/core/api/odooApi" +import { getAuth } from "@/core/utils/auth" + +const transactionApi = async ({ id }) => { + const auth = getAuth() + const dataTransaction = await odooApi('GET', `/api/v1/partner/${auth.partnerId}/sale_order/${id}`) + return dataTransaction +} + +export default transactionApi \ No newline at end of file diff --git a/src/lib/transaction/api/uploadPoApi.js b/src/lib/transaction/api/uploadPoApi.js new file mode 100644 index 00000000..00ad1d8d --- /dev/null +++ b/src/lib/transaction/api/uploadPoApi.js @@ -0,0 +1,10 @@ +import odooApi from "@/core/api/odooApi" +import { getAuth } from "@/core/utils/auth" + +const uploadPoApi = async ({ id, data }) => { + const auth = getAuth() + const dataUploadPo = await odooApi('POST', `/api/v1/partner/${auth.partnerId}/sale_order/${id}/upload_po`, data) + return dataUploadPo +} + +export default uploadPoApi \ No newline at end of file diff --git a/src/lib/transaction/components/Transaction.jsx b/src/lib/transaction/components/Transaction.jsx new file mode 100644 index 00000000..c9bdf715 --- /dev/null +++ b/src/lib/transaction/components/Transaction.jsx @@ -0,0 +1,149 @@ +import Spinner from "@/core/components/elements/Spinner/Spinner" +import useTransaction from "../hooks/useTransaction" +import TransactionStatusBadge from "./TransactionStatusBadge" +import Divider from "@/core/components/elements/Divider/Divider" +import { useRef, useState } from "react" +import { downloadPurchaseOrder } from "../utils/transactions" +import BottomPopup from "@/core/components/elements/Popup/BottomPopup" +import uploadPoApi from "../api/uploadPoApi" +import { toast } from "react-hot-toast" +import getFileBase64 from "@/core/utils/getFileBase64" +import currencyFormat from "@/core/utils/currencyFormat" +import VariantGroupCard from "@/lib/variant/components/VariantGroupCard" + +const Transaction = ({ id }) => { + const { transaction } = useTransaction({ id }) + + const poNumber = useRef('') + const poFile = useRef('') + const [ uploadPo, setUploadPo ] = useState(false) + const openUploadPo = () => setUploadPo(true) + const closeUploadPo = () => setUploadPo(false) + const submitUploadPo = async () => { + const file = poFile.current.files[0] + const name = poNumber.current.value + if (typeof file === 'undefined' || !name) { + toast.error('Nomor dan Dokumen PO harus diisi', { position: 'bottom-center' }) + return + } + if (file.size > 5000000) { + toast.error('Maksimal ukuran file adalah 5MB', { position: 'bottom-center' }) + return + } + const data = { name, file: await getFileBase64(file) } + const isUploaded = await uploadPoApi({ id, data }) + if (isUploaded) { + toast.success('Berhasil upload PO') + transaction.refetch() + closeUploadPo() + return + } + toast.error('Terjadi kesalahan internal, coba lagi nanti atau hubungi kami') + } + + return ( + <> + { transaction.isLoading && ( +
+ +
+ ) } + + { transaction.data?.name && ( + <> +
+ +
+ +
+
+ + { transaction.data?.name } + + + { transaction.data?.paymentTerm } + + + { transaction.data?.sales } + + + { transaction.data?.dateOrder } + +
+ + + +
+ + { transaction.data?.purchaseOrderName || '-' } + +
+

Dokumen PO

+ +
+
+ + + +
Detail Produk
+ +
+ +
+

Total Belanja

+

{ currencyFormat(transaction.data?.amountTotal) }

+
+
+ + + + +
+ + +
+
+ + +
+
+ + +
+
+ + )} + + + ) +} + +const DescriptionRow = ({ children, label }) => ( +
+ { label } + { children } +
+) + +export default Transaction \ No newline at end of file diff --git a/src/lib/transaction/components/Transactions.jsx b/src/lib/transaction/components/Transactions.jsx index 5eb1d947..246a4a2c 100644 --- a/src/lib/transaction/components/Transactions.jsx +++ b/src/lib/transaction/components/Transactions.jsx @@ -14,6 +14,7 @@ import BottomPopup from "@/core/components/elements/Popup/BottomPopup" import Pagination from "@/core/components/elements/Pagination/Pagination" import { toQuery } from "lodash-contrib" import _ from "lodash" +import Alert from "@/core/components/elements/Alert/Alert" const Transactions = () => { const router = useRouter() @@ -29,24 +30,22 @@ const Transactions = () => { offset: (page - 1) * limit, limit } - - const [ inputQuery, setInputQuery ] = useState(q) - const { transactions } = useTransactions({ query }) + const [ inputQuery, setInputQuery ] = useState(q) const [ toOthers, setToOthers ] = useState(null) - const [ toDelete, setToDelete ] = useState(null) + const [ toCancel, setToCancel ] = useState(null) const submitCancelTransaction = async () => { const isCancelled = await cancelTransactionApi({ partnerId: auth.partnerId, - transaction: toDelete + transaction: toCancel }) if (isCancelled) { toast.success('Berhasil batalkan transaksi') transactions.refetch() } - setToDelete(null) + setToCancel(null) } const pageCount = Math.ceil(transactions?.data?.saleOrderTotal / limit) @@ -82,6 +81,13 @@ const Transactions = () => { ) } + + { !transactions.isLoading && transactions.data?.saleOrders?.length === 0 && ( + + Tidak ada data transaksi + + ) } + { transactions.data?.saleOrders?.map((saleOrder, index) => (
@@ -132,21 +138,21 @@ const Transactions = () => { @@ -154,12 +160,12 @@ const Transactions = () => { setToDelete(null)} + active={toCancel} + close={() => setToCancel(null)} title="Batalkan Transaksi" >
- Apakah anda yakin membatalkan transaksi {toDelete?.name}? + Apakah anda yakin membatalkan transaksi {toCancel?.name}?
diff --git a/src/lib/transaction/hooks/useTransaction.js b/src/lib/transaction/hooks/useTransaction.js new file mode 100644 index 00000000..f2b493ee --- /dev/null +++ b/src/lib/transaction/hooks/useTransaction.js @@ -0,0 +1,13 @@ +import { useQuery } from "react-query" +import transactionApi from "../api/transactionApi" + +const useTransaction = ({ id }) => { + const fetchTransaction = async () => await transactionApi({ id }) + const { data, isLoading, refetch } = useQuery(`transaction-${id}`, fetchTransaction) + + return { + transaction: { data, isLoading, refetch } + } +} + +export default useTransaction \ No newline at end of file diff --git a/src/lib/transaction/utils/transactions.js b/src/lib/transaction/utils/transactions.js index 166e8a7e..03d4dbd4 100644 --- a/src/lib/transaction/utils/transactions.js +++ b/src/lib/transaction/utils/transactions.js @@ -1,10 +1,14 @@ -const downloadPurchaseOrder = (partnerId, transaction) => { - const url = `${process.env.ODOO_HOST}/api/v1/partner/${partnerId}/sale_order/${transaction.id}/download_po/${transaction.token}` +import { getAuth } from "@/core/utils/auth" + +const downloadPurchaseOrder = (transaction) => { + const auth = getAuth() + const url = `${process.env.ODOO_HOST}/api/v1/partner/${auth.partnerId}/sale_order/${transaction.id}/download_po/${transaction.token}` window.open(url, 'download') } -const downloadQuotation = (partnerId, transaction) => { - const url = `${process.env.ODOO_HOST}/api/v1/partner/${partnerId}/sale_order/${transaction.id}/download/${transaction.token}` +const downloadQuotation = (transaction) => { + const auth = getAuth() + const url = `${process.env.ODOO_HOST}/api/v1/partner/${auth.partnerId}/sale_order/${transaction.id}/download/${transaction.token}` window.open(url, 'download') } diff --git a/src/lib/variant/components/VariantCard.jsx b/src/lib/variant/components/VariantCard.jsx new file mode 100644 index 00000000..6c7ab22f --- /dev/null +++ b/src/lib/variant/components/VariantCard.jsx @@ -0,0 +1,97 @@ +import { useRouter } from "next/router" +import { toast } from "react-hot-toast" + +import Image from "@/core/components/elements/Image/Image" +import Link from "@/core/components/elements/Link/Link" +import { createSlug } from "@/core/utils/slug" +import currencyFormat from "@/core/utils/currencyFormat" +import { updateItemCart } from "@/core/utils/cart" + +const VariantCard = ({ + product, + openOnClick = true, + buyMore = false +}) => { + const router = useRouter() + + const addItemToCart = () => { + toast.success('Berhasil menambahkan ke keranjang', { duration: 1500 }) + updateItemCart({ + productId: product.id, + quantity: 1 + }) + return + } + + const checkoutItem = () => { + router.push(`/shop/checkout?product_id=${product.id}&qty=${product.quantity}`) + } + + const Card = () => ( +
+
+ {product.parent.name} +
+
+

+ {product.parent.name} +

+

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

+
+ {product.price.discountPercentage > 0 && ( + <> +

{currencyFormat(product.price.price)}

+ {product.price.discountPercentage}% + + )} +

{currencyFormat(product.price.priceDiscount)}

+
+

+ {currencyFormat(product.price.priceDiscount)} × {product.quantity} Barang +

+

+ {currencyFormat(product.quantity * product.price.priceDiscount)} +

+
+
+ ) + + if (openOnClick) { + return ( + <> + + + + { buyMore && ( +
+ + +
+ ) } + + ) + } + + return +} + +export default VariantCard \ No newline at end of file diff --git a/src/lib/variant/components/VariantGroupCard.jsx b/src/lib/variant/components/VariantGroupCard.jsx new file mode 100644 index 00000000..fd4f9b4d --- /dev/null +++ b/src/lib/variant/components/VariantGroupCard.jsx @@ -0,0 +1,33 @@ +import { useState } from "react" +import VariantCard from "./VariantCard" + +const VariantGroupCard = ({ + variants, + ...props +}) => { + const [ showAll, setShowAll ] = useState(false) + const variantsToShow = showAll ? variants : variants.slice(0, 2) + + return ( + <> + { variantsToShow?.map((variant, index) => ( + + )) } + { variants.length > 2 && ( + + ) } + + ) +} + +export default VariantGroupCard \ No newline at end of file -- cgit v1.2.3