mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-24 23:35:45 +00:00
feat(FE-316): Add uniformity Excel template generator
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import { formatNumber, formatDate } from '@/lib/helper';
|
||||
import { toast } from 'react-hot-toast';
|
||||
import * as XLSX from 'xlsx';
|
||||
import { ProjectFlockKandangLookup } from '@/types/api/production/project-flock';
|
||||
|
||||
export const generateUniformityTemplate = (
|
||||
availableQuantity: number,
|
||||
projectFlockKandangLookup: ProjectFlockKandangLookup
|
||||
) => {
|
||||
try {
|
||||
// Calculate 2% sample from total population
|
||||
const sampleSize = Math.round(availableQuantity * 0.02);
|
||||
|
||||
const data = Array.from({ length: sampleSize }, (_, index) => ({
|
||||
NO: index + 1,
|
||||
BW: '',
|
||||
}));
|
||||
|
||||
const worksheet = XLSX.utils.json_to_sheet(data, {
|
||||
header: ['NO', 'BW'],
|
||||
});
|
||||
|
||||
worksheet['!cols'] = [{ wch: 10 }, { wch: 15 }];
|
||||
|
||||
const workbook = XLSX.utils.book_new();
|
||||
|
||||
const kandangName = projectFlockKandangLookup.kandang?.name || 'Kandang';
|
||||
const flockName = projectFlockKandangLookup.project_flock?.flock_name || '';
|
||||
const flockPeriod = projectFlockKandangLookup.project_flock?.period || 1;
|
||||
|
||||
const sheetName =
|
||||
kandangName.length > 31 ? kandangName.substring(0, 31) : kandangName;
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, sheetName);
|
||||
|
||||
const sanitizedFlockName = flockName
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '');
|
||||
|
||||
const sanitizedKandangName = kandangName
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '');
|
||||
|
||||
const filename = `${formatDate(
|
||||
new Date(),
|
||||
'YYYY-MM-DD'
|
||||
)}-${sanitizedFlockName}-${sanitizedKandangName}-periode-${flockPeriod}-${sampleSize}-data.xlsx`;
|
||||
|
||||
XLSX.writeFile(workbook, filename);
|
||||
|
||||
toast.success(
|
||||
`Template berhasil dibuat dengan ${formatNumber(sampleSize)} baris data (2% dari ${formatNumber(availableQuantity)} populasi).`
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error generating uniformity template:', error);
|
||||
toast.error('Gagal membuat template Excel. Silakan coba lagi.');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user