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(`/${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(`/${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(`/${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(`/${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(`/${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(`/${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(`/${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(`/${ENDPOINT}/customer-dept`); if (res.status < 400) { return res.data; } return null; } export const useReportStore = defineStore('report-store', () => { const dataReportQuotation = ref([]); const dataReportInvoice = ref([]); const dataReportReceipt = ref([]); const dataReportSale = ref(); const dataReportProduct = ref([]); const dataReportPayment = ref([]); const dataReportProfit = ref(); const detaReportDept = ref([]); return { dataReportQuotation, dataReportInvoice, dataReportReceipt, dataReportSale, dataReportProduct, dataReportPayment, dataReportProfit, detaReportDept, downloadReportQuotation, getReportQuotation, downloadReportInvoice, getReportInvoice, downloadReportReceipt, getReportReceipt, downloadReportSale, getReportSale, downloadReportProduct, getReportProduct, getReportPayment, getReportProfit, getReportDept, }; });