import * as XLSX from 'xlsx'; import toast from 'react-hot-toast'; import { formatDate } from '@/lib/helper'; import { isResponseSuccess } from '@/lib/api-helper'; import { BaseApiService } from '@/services/api/base'; import { httpClient, httpClientFetcher } from '@/services/http/client'; import { BaseApiResponse } from '@/types/api/api-general'; import { ProductionResult } from '@/types/api/report/production-result'; import { BaseProjectFlockKandang } from '@/types/api/production/project-flock-kandang'; export class ProductionResultReportApiService extends BaseApiService< ProductionResult, unknown, unknown > { constructor(basePath: string = '/reports/production-result') { super(basePath); } async getAllProductionResultFetcher( endpoint: string ): Promise> { return await httpClientFetcher>( endpoint ); } async exportProductionResultToExcel( projectFlockKandangs: BaseProjectFlockKandang[] | null ) { try { const mappedProductionResults: { projectFlockKandang: BaseProjectFlockKandang; productionResult: ProductionResult[] | null; }[] = []; projectFlockKandangs?.forEach(async (projectFlockKandang) => { const getProductionResultPath = `${this.basePath}/${projectFlockKandang.id}?page=1&limit=99999999`; const getProductionResultRes = await httpClient< BaseApiResponse >(getProductionResultPath); mappedProductionResults.push({ projectFlockKandang, productionResult: isResponseSuccess(getProductionResultRes) ? getProductionResultRes.data : null, }); }); const rows = mappedProductionResults; if (!rows || rows.length === 0) { toast.error('Tidak ada data untuk diexport.'); return; } // Group by Project Flock Kandang Name const groupedData: Record< string, Record[] > = {}; rows.forEach((row) => { const kandangName = row.projectFlockKandang.kandang.name || 'Unknown'; if (!groupedData[kandangName]) { groupedData[kandangName] = []; } row.productionResult?.forEach((productionResult) => { groupedData[kandangName].push({ woa: productionResult.woa, bw: productionResult.bw, std_bw: productionResult.std_bw, uniformity: productionResult.uniformity, std_uniformity: productionResult.std_uniformity, dep_kum: productionResult.dep_kum, dep_std: productionResult.dep_std, butiran_utuh: productionResult.butiran_utuh, butiran_putih: productionResult.butiran_putih, butiran_retak: productionResult.butiran_retak, butiran_pecah: productionResult.butiran_pecah, butiran_jumlah: productionResult.butiran_jumlah, total_butir: productionResult.total_butir, kg_utuh: productionResult.kg_utuh, kg_putih: productionResult.kg_putih, kg_retak: productionResult.kg_retak, kg_pecah: productionResult.kg_pecah, kg_jumlah: productionResult.kg_jumlah, total_kg: productionResult.total_kg, persen_utuh: productionResult.persen_utuh, persen_putih: productionResult.persen_putih, persen_retak: productionResult.persen_retak, persen_pecah: productionResult.persen_pecah, hd: productionResult.hd, hd_std: productionResult.hd_std, fi: productionResult.fi, fi_std: productionResult.fi_std, em: productionResult.em, em_std: productionResult.em_std, ew: productionResult.ew, ew_std: productionResult.ew_std, fcr: productionResult.fcr, fcr_std: productionResult.fcr_std, hh: productionResult.hh, hh_std: productionResult.hh_std, project_flock_name: productionResult.project_flock.name, project_flock_category: productionResult.project_flock.category, kandang_name: productionResult.project_flock.kandang.name, created_at: formatDate(productionResult.created_at, 'YYYY-MM-DD'), updated_at: formatDate(productionResult.updated_at, 'YYYY-MM-DD'), }); }); }); const wb = XLSX.utils.book_new(); Object.keys(groupedData).forEach((sheetName) => { const ws = XLSX.utils.json_to_sheet(groupedData[sheetName]); // Sheet names cannot exceed 31 chars const safeSheetName = sheetName.substring(0, 31); XLSX.utils.book_append_sheet(wb, ws, safeSheetName); }); const productionResultExcelFileName = `laporan-hasil-produksi-${formatDate(Date.now(), 'YYYY-MM-DD')}-${rows[0].projectFlockKandang.project_flock.flock_name}.xlsx`; XLSX.writeFile(wb, productionResultExcelFileName); } catch (error) { console.error(error); toast.error('Gagal melakukan export laporan hasil produksi! Coba lagi.'); } } } export const ProductionResultReportApi = new ProductionResultReportApiService( '/reports/production-result' );