From 58160ad80ee33ce0b362da1ab0efa9e1c8c220da Mon Sep 17 00:00:00 2001 From: mhd-zaid Date: Sun, 3 Dec 2023 04:49:34 +0100 Subject: [PATCH] invoice client --- api/src/routes/ordersRoutes.js | 4 +- client/package.json | 1 + client/src/components/profile/OrderPdf.vue | 189 ++++++++++++++++++ .../src/sections/profile/HistorySection.vue | 123 +++++++----- 4 files changed, 266 insertions(+), 51 deletions(-) create mode 100644 client/src/components/profile/OrderPdf.vue diff --git a/api/src/routes/ordersRoutes.js b/api/src/routes/ordersRoutes.js index 051cec8..08dc318 100644 --- a/api/src/routes/ordersRoutes.js +++ b/api/src/routes/ordersRoutes.js @@ -104,8 +104,8 @@ export default ( } const orders = await Order.findAll({ - where: { user: user }, - include: "products", + where: { userId: user.id }, + include: ["products","user","payment"], }); if (orders.length === 0) { diff --git a/client/package.json b/client/package.json index 6e72c96..f9fdc7b 100644 --- a/client/package.json +++ b/client/package.json @@ -17,6 +17,7 @@ "@tailwindcss/aspect-ratio": "^0.4.2", "@tailwindcss/forms": "^0.5.7", "axios": "^1.5.0", + "html2pdf.js": "^0.9.0", "pinia": "^2.1.6", "vue": "^3.3.4", "vue-router": "^4.2.4", diff --git a/client/src/components/profile/OrderPdf.vue b/client/src/components/profile/OrderPdf.vue new file mode 100644 index 0000000..859b282 --- /dev/null +++ b/client/src/components/profile/OrderPdf.vue @@ -0,0 +1,189 @@ + + + diff --git a/client/src/sections/profile/HistorySection.vue b/client/src/sections/profile/HistorySection.vue index a46b5fb..445868a 100644 --- a/client/src/sections/profile/HistorySection.vue +++ b/client/src/sections/profile/HistorySection.vue @@ -7,32 +7,62 @@ import { } from '@headlessui/vue' import {EllipsisVerticalIcon} from '@heroicons/vue/24/outline' import {CheckCircleIcon} from '@heroicons/vue/20/solid' +import axiosInstance from '@/utils/axiosInstance' +import {getProductImage} from "@/types/ProductImageType"; +import { onMounted, ref } from 'vue'; +import OrderPdf from "@/components/profile/OrderPdf.vue"; +import html2pdf from 'html2pdf.js' -const orders = [ - { - number: 'WU88191111', - href: '#', - invoiceHref: '#', - createdDate: 'Jul 6, 2021', - createdDatetime: '2021-07-06', - deliveredDate: 'July 12, 2021', - deliveredDatetime: '2021-07-12', - total: '$160.00', - products: [ - { - id: 1, - name: 'Micro Backpack', - description: - 'Are you a minimalist looking for a compact carry option? The Micro Backpack is the perfect size for your essential everyday carry items. Wear it like a backpack or carry it like a satchel for all-day use.', - href: '#', - price: '$70.00', - imageSrc: 'https://tailwindui.com/img/ecommerce-images/order-history-page-03-product-01.jpg', - imageAlt: - 'Moss green canvas compact backpack with double top zipper, zipper front pouch, and matching carry handle and backpack straps.', - }, - ], - }, -] +const token = localStorage.getItem('token') +const isAuthenticated = !!token +let userId = '' +if (isAuthenticated) { + const payload = JSON.parse(atob(token.split('.')[1])) + userId = payload.userId +} + +const getUserOrders = async () => { + const {data} = await axiosInstance.get(`/orders/user/${userId}`) + data.forEach((order: any) => { + order.products.forEach((product: any) => { + product.imageSrc = getProductImage(product,2) + product.imageAlt = product.name + product.price = ((product.Orders_Products.price / 100) * product.Orders_Products.quantity).toFixed(2) + product.href = `/products/${product.id}` + }) + order.total = () => { + let total = 0.00 + order.products.forEach((product: any) => { + total += parseFloat(product.price); + }) + + return total.toFixed(2) + } + }) + + return data.filter((order: any) => order.status !== 'payment pending' && order.status !== 'payment failed') +} +const orders = ref() +onMounted(async () => { + orders.value = await getUserOrders() +}) + +const downloadInvoice = async (orderId:string) => { + const template = document.getElementById('invoice-template') + if(template){ + template.style.display = 'block' + await html2pdf(template, { + margin: [1,1], + filename: `facture-${orderId}.pdf`, + image: { type: 'jpeg', quality: 0.98 }, + html2canvas: { scale: 2,letterRendering: true }, + jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }, + pagebreak: { mode: ['avoid-all','css','legacy'] } + }) + template.style.display = 'none' + } + +}