From 628dc1fb0ec5038ba00a006b68875c577b16736a Mon Sep 17 00:00:00 2001 From: ImJustChew Date: Tue, 19 Jul 2022 09:35:52 +0800 Subject: [PATCH] added finance report generation code --- src/pages/export/financereport.css | 0 src/pages/export/financereport.tsx | 2 +- src/pages/finance.tsx | 91 +++++++++++++++++++++++++++++- src/styles/globals.css | 2 +- 4 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 src/pages/export/financereport.css diff --git a/src/pages/export/financereport.css b/src/pages/export/financereport.css new file mode 100644 index 0000000..e69de29 diff --git a/src/pages/export/financereport.tsx b/src/pages/export/financereport.tsx index 160cd5b..4633981 100644 --- a/src/pages/export/financereport.tsx +++ b/src/pages/export/financereport.tsx @@ -53,7 +53,7 @@ const PrintFinance: NextPage = ({ stringified, error }) => { console.log(expensetotal) - return
+ return
diff --git a/src/pages/finance.tsx b/src/pages/finance.tsx index 1466860..779a792 100644 --- a/src/pages/finance.tsx +++ b/src/pages/finance.tsx @@ -3,18 +3,21 @@ import { useCollectionData } from "react-firebase-hooks/firestore"; import MemberLayout from "../components/MemberLayout"; import NewTransaction from "../components/NewTransaction"; import Page from "../components/Page"; -import { db, docConverter } from "../config/firebase"; +import {db, docConverter, functions} from '../config/firebase'; import { FinanceAccountType, Transaction } from "../types/Finance"; import FinanceAccount from "../components/FinanceAccount"; -import { format } from "date-fns"; +import { format, getMonth, getYear } from "date-fns"; import {Chip, Tooltip, IconButton, ListItemIcon, ListItemText, Menu, MenuItem, DialogContent, DialogActions, Button, DialogTitle} from '@mui/material'; import {NoteRounded, ReceiptRounded, Delete, MoreVertRounded} from '@mui/icons-material'; -import { useMemo } from "react"; +import { FC, useMemo, useState } from "react"; import AddAccount from "../components/AddAccount"; import {bindTrigger, bindMenu, usePopupState} from 'material-ui-popup-state/hooks'; import { useDialog } from "../hooks/useDialog"; import { fetchAPI } from "../utils/fetchAPI"; import { useAuth } from "../hooks/useAuth"; +import { useForm } from "react-hook-form"; +import FormTextField from "../components/form-components/FormTextField"; +import {httpsCallable} from 'firebase/functions'; const TransactionLog = ({ transaction, accounts }) => { const { user } = useAuth(); @@ -106,9 +109,84 @@ const TransactionLog = ({ transaction, accounts }) => { } +type PickReportForm = { year: number, month: number } + +const PickReportDateDialog: FC<{ onClose: () => void }> = ({ onClose }) => { + const { control, handleSubmit, formState: { isValid, isSubmitting } } = useForm({ + mode: 'onChange', + defaultValues: { + year: getYear(new Date()), + month: getMonth(new Date()) + } + }); + const { user } = useAuth(); + //use fetch to get the pdf and download it + const downloadFile = async (url: string, filename: string) => { + const response = await fetch(url, { + method: 'GET', + headers: { + 'Authorization': `Bearer ${await user.getIdToken()}` + } + }); + const blob = await response.blob(); + const urlCreator = window.URL || window.webkitURL; + const imageUrl = urlCreator.createObjectURL(blob); + const link = document.createElement('a'); + link.href = imageUrl; + link.download = filename; + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); + } + + const onSubmit = async (data: PickReportForm) => { + const generatePdf = httpsCallable<{url: string, pdfOptions: any}, string>(functions, 'generatePdf'); + const uploadedUrl = await generatePdf({ + url: `https://clphscec.ga/export/financereport?month=${data.year}-${data.month}`, + pdfOptions: { + format: 'A4', + printBackground: true, + margin: { + top: '0.5in', + right: '0.5in', + bottom: '0.5in', + left: '0.5in' + } + } + }) + await downloadFile(uploadedUrl.data, `${data.year}-${data.month.toString().padStart(2, '0')} Finance Report.pdf`) + onClose(); + } + + return <> + + + + + + + + + +} + + const Finance = () => { const [transactions = [], loading, error] = useCollectionData(query(collection(db, 'finance', 'CEC', 'transactions'), orderBy('date', 'desc')).withConverter(docConverter)); const [accounts = [], loadingAccounts, errorAccounts] = useCollectionData(collection(db, 'finance', 'CEC', 'accounts').withConverter(docConverter)); + const [openDialog, closeDialog] = useDialog(); console.log(transactions) const addNumbersFixed = (num1, num2) => { @@ -122,9 +200,16 @@ const Finance = () => { }, 0); }, [accounts]); + const handlePrintReport = () => { + openDialog({ + children: , + }) + } + return +

Accounts

diff --git a/src/styles/globals.css b/src/styles/globals.css index 4437a7f..9e154d6 100644 --- a/src/styles/globals.css +++ b/src/styles/globals.css @@ -33,7 +33,7 @@ body { font-family: 'KaiTi'; src: url('/public/fonts/KaiTi.ttf') format('ttf'), url('/public/fonts/KaiTi.ttf') format('ttf'); - font-weight: bold; + font-weight: normal; font-style: normal; font-display: block; }