From 47395b0847ead908cad4bda311731cf27c7e80ac Mon Sep 17 00:00:00 2001 From: Thanaphon Frappet Date: Wed, 12 Mar 2025 15:36:02 +0700 Subject: [PATCH] refactor: downlod add start date and end date --- src/pages/14_report/MainPage.vue | 52 +++++++++++++++++++++++++++----- src/stores/report/index.ts | 52 +++++++++++++++++++++++++++++--- 2 files changed, 91 insertions(+), 13 deletions(-) diff --git a/src/pages/14_report/MainPage.vue b/src/pages/14_report/MainPage.vue index e53f408e..7242d11f 100644 --- a/src/pages/14_report/MainPage.vue +++ b/src/pages/14_report/MainPage.vue @@ -305,7 +305,12 @@ watch([() => pastYears.value], async () => { style="margin-left: auto" :icon="'material-symbols:download'" :label="$t('general.download')" - @click.stop="reportStore.downloadReportQuotation()" + @click.stop=" + reportStore.downloadReportQuotation({ + startDate: state.date[0], + endDate: state.date[1], + }) + " /> @@ -346,7 +351,12 @@ watch([() => pastYears.value], async () => { style="margin-left: auto" :icon="'material-symbols:download'" :label="$t('general.download')" - @click.stop="reportStore.downloadReportInvoice()" + @click.stop=" + reportStore.downloadReportInvoice({ + startDate: state.date[0], + endDate: state.date[1], + }) + " /> @@ -386,7 +396,12 @@ watch([() => pastYears.value], async () => { style="margin-left: auto" :icon="'material-symbols:download'" :label="$t('general.download')" - @click.stop="reportStore.downloadReportReceipt()" + @click.stop=" + reportStore.downloadReportReceipt({ + startDate: state.date[0], + endDate: state.date[1], + }) + " /> @@ -428,7 +443,12 @@ watch([() => pastYears.value], async () => { style="margin-left: auto" :icon="'material-symbols:download'" :label="$t('general.download')" - @click.stop="reportStore.downloadReportInvoice()" + @click.stop=" + reportStore.downloadReportInvoice({ + startDate: state.date[0], + endDate: state.date[1], + }) + " /> @@ -469,7 +489,12 @@ watch([() => pastYears.value], async () => { style="margin-left: auto" :icon="'material-symbols:download'" :label="$t('general.download')" - @click.stop="reportStore.downloadReportInvoice()" + @click.stop=" + reportStore.downloadReportInvoice({ + startDate: state.date[0], + endDate: state.date[1], + }) + " /> @@ -492,7 +517,10 @@ watch([() => pastYears.value], async () => { :icon="'material-symbols:download'" :label="$t('general.download')" @click.stop=" - reportStore.downloadReportSale('by-customer') + reportStore.downloadReportSale('by-customer', { + startDate: state.date[0], + endDate: state.date[1], + }) " /> @@ -533,7 +561,10 @@ watch([() => pastYears.value], async () => { :icon="'material-symbols:download'" :label="$t('general.download')" @click.stop=" - reportStore.downloadReportSale('by-product-group') + reportStore.downloadReportSale('by-product-group', { + startDate: state.date[0], + endDate: state.date[1], + }) " /> @@ -565,7 +596,12 @@ watch([() => pastYears.value], async () => { style="margin-left: auto" :icon="'material-symbols:download'" :label="$t('general.download')" - @click.stop="reportStore.downloadReportSale('by-sale')" + @click.stop=" + reportStore.downloadReportSale('by-sale', { + startDate: state.date[0], + endDate: state.date[1], + }) + " /> diff --git a/src/stores/report/index.ts b/src/stores/report/index.ts index 0fa216cd..8e259dbe 100644 --- a/src/stores/report/index.ts +++ b/src/stores/report/index.ts @@ -15,7 +15,28 @@ import { getToken } from 'src/services/keycloak'; const ENDPOINT = 'report'; -async function _download(url: string, filename?: string) { +async function _download( + url: string, + filename?: string, + params?: { + startDate: string | Date; + endDate: string | Date; + }, +) { + if (params) { + const queryParams = new URLSearchParams({ + startDate: + params.startDate instanceof Date + ? params.startDate.toISOString() + : params.startDate, + endDate: + params.endDate instanceof Date + ? params.endDate.toISOString() + : params.endDate, + }).toString(); + url += '?' + queryParams; + } + const res = await fetch(url, { headers: { ['Authorization']: 'Bearer ' + (await getToken()) }, }); @@ -30,10 +51,14 @@ async function _download(url: string, filename?: string) { } } -export async function downloadReportQuotation() { +export async function downloadReportQuotation(params?: { + startDate: string | Date; + endDate: string | Date; +}) { await _download( baseUrl + '/' + ENDPOINT + '/quotation/download', 'quotation-report', + params, ); } @@ -51,10 +76,14 @@ export async function getReportQuotation(params?: { return null; } -export async function downloadReportInvoice() { +export async function downloadReportInvoice(params?: { + startDate: string | Date; + endDate: string | Date; +}) { await _download( baseUrl + '/' + ENDPOINT + '/invoice/download', 'invoice-report', + params, ); } @@ -69,10 +98,14 @@ export async function getReportInvoice(params?: { return null; } -export async function downloadReportReceipt() { +export async function downloadReportReceipt(params?: { + startDate: string | Date; + endDate: string | Date; +}) { await _download( baseUrl + '/' + ENDPOINT + '/receipt/download', 'receipt-report', + params, ); } @@ -89,10 +122,15 @@ export async function getReportReceipt(params?: { export async function downloadReportSale( category: 'by-product-group' | 'by-sale' | 'by-customer', + params?: { + startDate: string | Date; + endDate: string | Date; + }, ) { await _download( baseUrl + '/' + ENDPOINT + '/sale/' + category + '/download', 'sale-' + category + '-report', + params, ); } @@ -108,10 +146,14 @@ export async function getReportSale(params?: { return null; } -export async function downloadReportProduct() { +export async function downloadReportProduct(params?: { + startDate: string | Date; + endDate: string | Date; +}) { await _download( baseUrl + '/' + ENDPOINT + '/receipt/download', 'product-report', + params, ); }