151 lines
3.9 KiB
TypeScript
151 lines
3.9 KiB
TypeScript
import { notification } from 'antd';
|
|
import {
|
|
MakeReportParams,
|
|
MakeReportResponse,
|
|
OverViewDataResponse,
|
|
ReportDetailList,
|
|
ReportDetailListParams,
|
|
ReportListParams,
|
|
ReportListType,
|
|
} from 'models';
|
|
import { API } from './api';
|
|
|
|
export async function getReportDetailList(params: ReportDetailListParams) {
|
|
try {
|
|
const response = await API.get<ReportDetailList>(
|
|
'/ctel/report_detail_list/',
|
|
{
|
|
params: {
|
|
report_id: params.report_id,
|
|
page: params.page,
|
|
},
|
|
},
|
|
);
|
|
return response.data;
|
|
} catch (error) {
|
|
notification.error({
|
|
message: `${error?.message}`,
|
|
});
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
export async function makeReport(params: MakeReportParams) {
|
|
const { end_date, start_date, subsidiary } = params;
|
|
try {
|
|
const response = await API.get<MakeReportResponse>(`/ctel/make_report/`, {
|
|
params: {
|
|
start_date: start_date,
|
|
end_date: end_date,
|
|
subsidiary: subsidiary,
|
|
},
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
notification.error({
|
|
message: `${error?.message}`,
|
|
});
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
export async function getReportList(params?: ReportListParams) {
|
|
try {
|
|
const response = await API.get<ReportListType>('/ctel/report_list/', {
|
|
params: {
|
|
page: params?.page,
|
|
page_size: params?.page_size,
|
|
start_date: params?.start_date,
|
|
end_date: params?.end_date,
|
|
},
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
notification.error({
|
|
message: `${error?.message}`,
|
|
});
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
export async function getOverViewReport(params?: ReportListParams) {
|
|
try {
|
|
const response = await API.get<OverViewDataResponse>('/ctel/overview/', {
|
|
params: {
|
|
page: params?.page,
|
|
page_size: params?.page_size,
|
|
start_date: params?.start_date,
|
|
end_date: params?.end_date,
|
|
subsidiary: params?.subsidiary,
|
|
},
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
notification.error({
|
|
message: `${error?.message}`,
|
|
});
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
export async function downloadReport(report_id: string) {
|
|
try {
|
|
const response = await API.get(`/ctel/get_report_file/${report_id}/`, {
|
|
responseType: 'blob', // Important
|
|
});
|
|
let filename = "report.xlsx";
|
|
try {
|
|
let basename = response.headers['content-disposition'].split('filename=')[1].split('.')[0];
|
|
let extension = response.headers['content-disposition'].split('.')[1].split(';')[0];
|
|
filename = `${basename}.${extension}`
|
|
} catch(err) {
|
|
console.log(err);
|
|
}
|
|
const file = new Blob([response.data], {
|
|
type: 'application/vnd.ms-excel',
|
|
});
|
|
// const fileURL = URL.createObjectURL(file);
|
|
// window.open(fileURL);
|
|
return {
|
|
file: file,
|
|
filename: filename,
|
|
}
|
|
} catch (error) {
|
|
notification.error({
|
|
message: `${error?.message}`,
|
|
});
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
|
|
export async function downloadDashboardReport(duration='30d', subsidiary='ALL') {
|
|
try {
|
|
const response = await API.get(`/api/ctel/overview_download_file/?duration=${duration}&subsidiary=${subsidiary}`, {
|
|
responseType: 'blob', // Important
|
|
});
|
|
let filename = "report.xlsx";
|
|
try {
|
|
let basename = response.headers['content-disposition'].split('filename=')[1].split('.')[0];
|
|
let extension = response.headers['content-disposition'].split('.')[1].split(';')[0];
|
|
filename = `${basename}.${extension}`
|
|
} catch(err) {
|
|
console.log(err);
|
|
}
|
|
const file = new Blob([response.data], {
|
|
type: 'application/vnd.ms-excel',
|
|
});
|
|
// const fileURL = URL.createObjectURL(file);
|
|
// window.open(fileURL);
|
|
return {
|
|
file: file,
|
|
filename: filename,
|
|
}
|
|
} catch (error) {
|
|
notification.error({
|
|
message: `${error?.message}`,
|
|
});
|
|
console.log(error);
|
|
}
|
|
}
|