Skip to content

Commit

Permalink
[add]CSV出力
Browse files Browse the repository at this point in the history
  • Loading branch information
KazumaSun committed Aug 8, 2023
1 parent 81f28df commit cadad9b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
27 changes: 26 additions & 1 deletion view/next-project/src/pages/purchasereports/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import clsx from 'clsx';
import Head from 'next/head';
import { useCallback, useEffect, useState, useMemo } from 'react';
import { useRecoilValue } from 'recoil';
import PrimaryButton from '@/components/common/OutlinePrimaryButton/OutlinePrimaryButton';

import { authAtom } from '@/store/atoms';
import { put } from '@/utils/api/purchaseReport';
import { createPurchaseReportCsv } from '@/utils/createPurchaseReportCsv';
import { downloadFile } from '@/utils/downloadFile';
import { get } from '@api/api_methods';
import { getCurrentUser } from '@api/currentUser';
import { Card, Checkbox, Title, BureauLabel } from '@components/common';
Expand Down Expand Up @@ -47,6 +50,13 @@ export async function getServerSideProps() {
};
}

const formatYYYYMMDD = (date: Date) => {
const yyyy = String(date.getFullYear());
const mm = String(date.getMonth() + 1).padStart(2, '0');
const dd = String(date.getDate()).padStart(2, '0');
return `${yyyy}${mm}${dd}`;
};

export default function PurchaseReports(props: Props) {
const auth = useRecoilValue(authAtom);
const [currentUser, setCurrentUser] = useState<User>();
Expand Down Expand Up @@ -165,7 +175,7 @@ export default function PurchaseReports(props: Props) {
</Head>
<Card>
<div className='mx-5 mt-10'>
<div className='flex'>
<div className='flex gap4'>
<Title title={'購入報告一覧'} />
<select
className='w-100 '
Expand All @@ -176,6 +186,21 @@ export default function PurchaseReports(props: Props) {
<option value='2022'>2022</option>
<option value='2023'>2023</option>
</select>
<PrimaryButton
className='hidden md:block'
onClick={async () => {
downloadFile({
downloadContent: await createPurchaseReportCsv(
filteredPurchaseReportViews,
props.expenses,
),
fileName: `購入申請一覧(${selectedYear})_${formatYYYYMMDD(new Date())}.csv`,
isBomAdded: true,
});
}}
>
CSVダウンロード
</PrimaryButton>
</div>
<div className='hidden justify-end md:flex'>
<OpenAddModalButton>報告登録</OpenAddModalButton>
Expand Down
52 changes: 52 additions & 0 deletions view/next-project/src/utils/createPurchaseReportCsv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { PurchaseReportView, Expense } from '../type/common';
import { createCsv, createCsvData } from './createCsv';

export const createPurchaseReportCsv = async (
purchaseReportsViews: PurchaseReportView[],
expenses: Expense[],
) =>
createCsv(
createCsvData(purchaseReportsViews, [
{
getCustomValue: (row) => row.purchaseReport.id || '',
label: 'ID',
},
{
getCustomValue: (row) =>
expenses.find((expense) => expense.id === row.purchaseOrder.expenseID)?.name || '',
label: '報告した局',
},
{
getCustomValue: (row) => String(row.purchaseReport.createdAt).split('T')[0] || '',
label: '報告日',
},
{
getCustomValue: (row) => row.purchaseOrder.deadline || '',
label: '購入日',
},
{
getCustomValue: (row) =>
(row.purchaseItems.reduce((sum, item) => item.financeCheck ? (sum + item.price * item.quantity) : sum, 0) + row.purchaseReport.addition - row.purchaseReport.discount)|| '0',
label: '合計金額',
},
{
getCustomValue: (row) => (row.purchaseReport.financeCheck ? '○' : '' || ''),
label: '財務局長チェック',
},
{
getCustomValue: (row) =>
row.purchaseItems
.map(
(item) =>
`${item.item}/${item.price}円/${item.quantity}個/` +
(item.financeCheck ? '○' : '×'),
)
.join(' , ') || '',
label: '購入物品 (品名/値段/数量/財務局長チェック)',
},
{
getCustomValue: (row) => row.purchaseReport.remark || '',
label: '備考',
},
]),
);

0 comments on commit cadad9b

Please sign in to comment.