jws-frontend/src/stores/report/index.ts
Thanaphon Frappet 47395b0847
Some checks failed
Spell Check / Spell Check with Typos (push) Failing after 8s
refactor: downlod add start date and end date
2025-03-12 15:36:02 +07:00

253 lines
5.7 KiB
TypeScript

import { ref } from 'vue';
import { defineStore } from 'pinia';
import { api } from 'src/boot/axios';
import {
Report,
ReportPayment,
ReportProduct,
ReportQuotation,
ReportSale,
ReportProfit,
CustomerDept,
} from './types';
import { baseUrl } from '../utils';
import { getToken } from 'src/services/keycloak';
const ENDPOINT = 'report';
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()) },
});
const text = await res.json();
const blob = new Blob([text], { type: 'text/csv' });
if (res.ok && blob) {
const a = document.createElement('a');
a.download = (filename || 'report') + '.csv';
a.href = window.URL.createObjectURL(blob);
a.click();
a.remove();
}
}
export async function downloadReportQuotation(params?: {
startDate: string | Date;
endDate: string | Date;
}) {
await _download(
baseUrl + '/' + ENDPOINT + '/quotation/download',
'quotation-report',
params,
);
}
export async function getReportQuotation(params?: {
startDate: string | Date;
endDate: string | Date;
}) {
const res = await api.get<ReportQuotation[]>(`/${ENDPOINT}/quotation`, {
params,
});
if (res.status < 400) {
res.data;
return res.data;
}
return null;
}
export async function downloadReportInvoice(params?: {
startDate: string | Date;
endDate: string | Date;
}) {
await _download(
baseUrl + '/' + ENDPOINT + '/invoice/download',
'invoice-report',
params,
);
}
export async function getReportInvoice(params?: {
startDate: string | Date;
endDate: string | Date;
}) {
const res = await api.get<Report[]>(`/${ENDPOINT}/invoice`, { params });
if (res.status < 400) {
return res.data;
}
return null;
}
export async function downloadReportReceipt(params?: {
startDate: string | Date;
endDate: string | Date;
}) {
await _download(
baseUrl + '/' + ENDPOINT + '/receipt/download',
'receipt-report',
params,
);
}
export async function getReportReceipt(params?: {
startDate: string | Date;
endDate: string | Date;
}) {
const res = await api.get<Report[]>(`/${ENDPOINT}/receipt`, { params });
if (res.status < 400) {
return res.data;
}
return null;
}
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,
);
}
export async function getReportSale(params?: {
startDate: string | Date;
endDate: string | Date;
}) {
const res = await api.get<ReportSale>(`/${ENDPOINT}/sale`, { params });
if (res.status < 400) {
return res.data;
}
return null;
}
export async function downloadReportProduct(params?: {
startDate: string | Date;
endDate: string | Date;
}) {
await _download(
baseUrl + '/' + ENDPOINT + '/receipt/download',
'product-report',
params,
);
}
export async function getReportProduct(params?: {
startDate: string | Date;
endDate: string | Date;
}) {
const res = await api.get<ReportProduct[]>(`/${ENDPOINT}/product`, {
params,
});
if (res.status < 400) {
return res.data;
}
return null;
}
export async function getReportPayment(params?: {
startDate: string | Date;
endDate: string | Date;
}) {
const res = await api.get<ReportPayment[]>(`/${ENDPOINT}/payment`, {
params,
});
if (res.status < 400) {
return res.data;
}
return null;
}
export async function getReportProfit(params?: {
years?: number;
startDate?: string | Date;
endDate?: string | Date;
}) {
let opts = params || {};
if (params?.years) {
const currentYear = new Date().getFullYear();
opts.startDate = new Date(currentYear - params.years, 0, 1);
opts.endDate = new Date(currentYear, 11, 31);
}
const res = await api.get<ReportProfit>(`/${ENDPOINT}/profit`, {
params: {
startDate: opts.startDate,
endDate: opts.endDate,
},
});
if (res.status < 400) {
return res.data;
}
return null;
}
export async function getReportDept() {
const res = await api.get<CustomerDept[]>(`/${ENDPOINT}/customer-dept`);
if (res.status < 400) {
return res.data;
}
return null;
}
export const useReportStore = defineStore('report-store', () => {
const dataReportQuotation = ref<ReportQuotation[]>([]);
const dataReportInvoice = ref<Report[]>([]);
const dataReportReceipt = ref<Report[]>([]);
const dataReportSale = ref<ReportSale>();
const dataReportProduct = ref<ReportProduct[]>([]);
const dataReportPayment = ref<ReportPayment[]>([]);
const dataReportProfit = ref<ReportProfit>();
const detaReportDept = ref<CustomerDept[]>([]);
return {
dataReportQuotation,
dataReportInvoice,
dataReportReceipt,
dataReportSale,
dataReportProduct,
dataReportPayment,
dataReportProfit,
detaReportDept,
downloadReportQuotation,
getReportQuotation,
downloadReportInvoice,
getReportInvoice,
downloadReportReceipt,
getReportReceipt,
downloadReportSale,
getReportSale,
downloadReportProduct,
getReportProduct,
getReportPayment,
getReportProfit,
getReportDept,
};
});