feat: add exportToExcel method

This commit is contained in:
ValdiANS
2026-01-27 18:58:03 +07:00
parent 81e5a180ba
commit 8f6597e7df
@@ -1,3 +1,4 @@
import * as XLSX from 'xlsx';
import axios from 'axios';
import { BaseApiService } from '@/services/api/base';
import {
@@ -11,12 +12,14 @@ import {
TransferToLaying,
UpdateTransferToLayingPayload,
} from '@/types/api/production/transfer-to-laying';
import { httpClient } from '@/services/http/client';
import { httpClient, httpClientFetcher } from '@/services/http/client';
import {
ProjectFlockAvailableQuantity,
ProjectFlockMaxQuantity,
} from '@/types/api/production/project-flock';
import { isResponseSuccess } from '@/lib/api-helper';
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
import toast from 'react-hot-toast';
import { formatDate } from '@/lib/helper';
export class TransferToLayingService extends BaseApiService<
TransferToLaying,
@@ -221,6 +224,60 @@ export class TransferToLayingService extends BaseApiService<
}
}
async exportToExcel(initialQueryString: string) {
const params = new URLSearchParams(initialQueryString);
params.set('limit', '9999999');
const queryString = `?${params.toString()}`;
try {
const transferToLayings = await httpClientFetcher<
BaseApiResponse<TransferToLaying[]>
>(`${this.basePath}${queryString}`);
if (isResponseError(transferToLayings)) {
toast.error('Gagal melakukan export transfer to laying! Coba lagi.');
return;
}
const rows = transferToLayings.data;
const formattedRows = [];
for (let i = 0; i < rows.length; i++) {
formattedRows.push({
id: rows[i].id,
transfer_number: rows[i].transfer_number,
transfer_date: formatDate(rows[i].transfer_date, 'DD-MM-YYYY'),
project_flock_source: rows[i].from_project_flock.flock_name,
project_flock_target: rows[i].to_project_flock.flock_name,
pending_usage_qty: rows[i].pending_usage_qty,
usage_qty: rows[i].usage_qty,
project_flock_source_kandang: rows[i].sources
.map((item) => item.source_project_flock_kandang.kandang.name)
.join(', '),
project_flock_target_kandang: rows[i].targets
.map((item) => item.target_project_flock_kandang.kandang.name)
.join(', '),
created_user: rows[i].created_user.name,
created_at: formatDate(rows[i].created_at, 'DD-MM-YYYY'),
updated_at: formatDate(rows[i].updated_at, 'DD-MM-YYYY'),
notes: rows[i].notes,
});
}
const ws = XLSX.utils.json_to_sheet(formattedRows);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'transfer-ke-laying');
// triggers download in browser
XLSX.writeFile(wb, 'transfer-ke-laying.xlsx');
} catch (error) {
toast.error('Gagal melakukan export transfer to laying! Coba lagi.');
}
}
async getApprovalHistory(
transferToLayingId: number,
group: boolean = true,