mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:56:28 +00:00
494 lines
22 KiB
TypeScript
494 lines
22 KiB
TypeScript
import dayjs from 'dayjs';
|
|
import { Workbook } from "exceljs";
|
|
import { type IHistory } from '@/app/(dashboard)/history/components/type';
|
|
import { type IUser } from '@/app/admin/administration/user/component/type';
|
|
import { type IExcelJSColumnType } from '@/types/types';
|
|
|
|
export const toDataURL = (url: any) => fetch(url)
|
|
.then(response => response.blob())
|
|
.then(blob => new Promise((resolve, reject) => {
|
|
const reader = new FileReader()
|
|
reader.onloadend = () => resolve(reader.result)
|
|
reader.onerror = reject
|
|
reader.readAsDataURL(blob)
|
|
}
|
|
))
|
|
|
|
export const exportTable = async (user: IUser, srcDataTable: any[], columns: IExcelJSColumnType[], title: string) => {
|
|
try {
|
|
const workbook = new Workbook();
|
|
const worksheet = workbook.addWorksheet("Sheet1", { pageSetup: { orientation: 'portrait' } });
|
|
|
|
const base64Img = await toDataURL('/assets/projects/nutech.png')
|
|
const logo = workbook.addImage({
|
|
base64: base64Img as string,
|
|
extension: 'png',
|
|
});
|
|
worksheet.addImage(logo, {
|
|
tl: { col: 1, row: 1 },
|
|
ext: { width: 250, height: 75 }
|
|
})
|
|
|
|
worksheet.mergeCells('B6:K6')
|
|
const titleCell = worksheet.getCell('B6')
|
|
titleCell.font = {
|
|
size: 15,
|
|
underline: true,
|
|
bold: true
|
|
}
|
|
titleCell.alignment = {
|
|
horizontal: 'center'
|
|
}
|
|
titleCell.value = title || 'Export Data'
|
|
|
|
|
|
// Table Headers
|
|
worksheet.getRow(8).values = columns.map(item => item.header);
|
|
worksheet.getRow(8).font = { bold: true }
|
|
worksheet.getRow(8).eachCell({ includeEmpty: false }, (cell: any) => {
|
|
cell.border = border
|
|
})
|
|
worksheet.columns = columns.map(item => {
|
|
return {
|
|
key: item.key,
|
|
width: item.width || 20,
|
|
style: item.style || { numFmt: 'General' }
|
|
}
|
|
})
|
|
|
|
// Set table value
|
|
srcDataTable.forEach((item, index: number) => {
|
|
|
|
const obj = columns.map(column => {
|
|
if (column.key === 'number') {
|
|
return {
|
|
[column.key]: index + 1
|
|
}
|
|
} else if (column.value) {
|
|
let columnValue;
|
|
|
|
if (column.value) {
|
|
const nestedValue = column.value?.split('.');
|
|
if (nestedValue.length === 1) {
|
|
columnValue = item[column.value];
|
|
} else {
|
|
let currentItem = item;
|
|
for (const part of nestedValue) {
|
|
currentItem = currentItem[part];
|
|
if (!currentItem) break;
|
|
}
|
|
columnValue = currentItem;
|
|
}
|
|
}
|
|
|
|
if (column.style?.numFmt === 'dd/mm/yyyy') {
|
|
return {
|
|
[column.key]: columnValue ? new Date(columnValue).toLocaleDateString("id-ID", { timeZone: "Asia/Jakarta" }) : ' '
|
|
};
|
|
} else if (column.style?.numFmt === '[$-13809]hh:mm:ss;@') {
|
|
return {
|
|
[column.key]: columnValue ? dayjs(columnValue).format('HH:mm A') : ' '
|
|
};
|
|
} else if (column.hyperlink) {
|
|
return {
|
|
[column.key]: columnValue ? { text: 'View Photo', hyperlink: process.env.BASE_URL_IMG + columnValue, font: { color: { argb: '0000FF' } } } : ' '
|
|
};
|
|
} else {
|
|
return {
|
|
[column.key]: columnValue || ' '
|
|
};
|
|
}
|
|
} else {
|
|
const value = item[column.key]
|
|
if (column.style?.numFmt === 'dd/mm/yyyy') {
|
|
return {
|
|
[column.key]: value ? new Date(value).toLocaleDateString("id-ID", { timeZone: "Asia/Jakarta" }) : ' '
|
|
};
|
|
} else if (column.style?.numFmt === '[$-13809]hh:mm:ss;@') {
|
|
return {
|
|
[column.key]: value ? dayjs(value).format('HH:mm A') : ' '
|
|
};
|
|
} else if (column.hyperlink) {
|
|
return {
|
|
[column.key]: value ? { text: 'View Photo', hyperlink: process.env.BASE_URL_IMG + value, font: { color: { argb: '0000FF' } } } : ' '
|
|
};
|
|
} else {
|
|
return {
|
|
[column.key]: value || ' '
|
|
};
|
|
}
|
|
}
|
|
})
|
|
|
|
const convertedJson: any = {};
|
|
|
|
obj.forEach((item: any) => {
|
|
const key = Object.keys(item)[0]
|
|
const value = item[key]
|
|
convertedJson[key] = value
|
|
});
|
|
|
|
const result = [convertedJson]
|
|
|
|
const row = worksheet.addRow(result[0])
|
|
// console.log(row)
|
|
row.eachCell((cell: any) => {
|
|
cell.border = border
|
|
})
|
|
|
|
row.eachCell(function (cell: any, colNumber: any) {
|
|
if (cell.value && cell.value.hyperlink)
|
|
row.getCell(colNumber).font = { underline: true, color: { argb: "004e47cc" } };
|
|
});
|
|
})
|
|
|
|
// Signature
|
|
let lastRowTable = 0
|
|
await worksheet.eachRow({ includeEmpty: false }, async function (row: any, rowNumber: number) {
|
|
lastRowTable = await rowNumber
|
|
});
|
|
|
|
const generateAtCellTitle = worksheet.getCell(`${columnToLetter(columns.length - 1)}${lastRowTable + 2}`)
|
|
generateAtCellTitle.value = 'Generate at'
|
|
generateAtCellTitle.border = border
|
|
generateAtCellTitle.font = { bold: true }
|
|
const generateAtCellValue = worksheet.getCell(`${columnToLetter(columns.length)}${lastRowTable + 2}`)
|
|
generateAtCellValue.value = dayjs().format('MMM DD, YYYY HH:mm:ss')
|
|
generateAtCellValue.border = border
|
|
const generateByCellTitle = worksheet.getCell(`${columnToLetter(columns.length - 1)}${lastRowTable + 3}`)
|
|
generateByCellTitle.value = 'Generate by'
|
|
generateByCellTitle.border = border
|
|
generateByCellTitle.font = { bold: true }
|
|
const generateByCellValue = worksheet.getCell(`${columnToLetter(columns.length)}${lastRowTable + 3}`)
|
|
generateByCellValue.value = user?.fullname
|
|
generateByCellValue.border = border
|
|
|
|
|
|
workbook.creator = user?.fullname;
|
|
workbook.lastModifiedBy = user?.fullname;
|
|
workbook.created = new Date(1985, 8, 30);
|
|
workbook.modified = new Date();
|
|
workbook.lastPrinted = new Date(2016, 9, 27);
|
|
|
|
|
|
|
|
const fileBuffer = await workbook.xlsx.writeBuffer()
|
|
const blob = new Blob([fileBuffer], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }) // application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/vnd.ms-excel
|
|
return blob
|
|
} catch (error: any) {
|
|
console.log("ERROR GENERATE EXCEL :", error)
|
|
return null
|
|
}
|
|
}
|
|
|
|
export const exportEmployeeHistory = async (user: IUser, srcDataTable: IHistory[]) => {
|
|
try {
|
|
const columns = [
|
|
{ header: 'No', key: 'number', width: 5, style: { numFmt: '0;[Red]0' } },
|
|
{ header: 'Presence code', key: 'presence_checkin_code' },
|
|
{ header: 'Date', key: 'presence_date', style: { numFmt: 'dd/mm/yyyy' } },
|
|
{ header: 'Working Status', key: 'working_status' },
|
|
{ header: 'Check-in Time', key: 'working_hours_start', style: { numFmt: '[$-13809]hh:mm:ss;@' } },
|
|
{ header: 'Check-in Location', key: 'location_name' },
|
|
{ header: 'Check-in Device', key: 'device' },
|
|
{ header: 'Check-in Note', key: 'note' },
|
|
{ header: 'Check-in Photo', key: 'face_image' },
|
|
{ header: 'Check-out Time', key: 'working_hours_end', style: { numFmt: '[$-13809]hh:mm:ss;@' } },
|
|
{ header: 'Check-out Location', key: 'co_location_name' },
|
|
{ header: 'Check-out Device', key: 'co_device' },
|
|
{ header: 'Check-out Note', key: 'co_note' },
|
|
{ header: 'Check-out Photo', key: 'co_face_image' },
|
|
{ header: 'Auto Checkout', key: 'autocheckout' },
|
|
{ header: 'Approval', key: 'approval' },
|
|
];
|
|
|
|
const workbook = new Workbook();
|
|
const worksheet = workbook.addWorksheet("Sheet1", { pageSetup: { orientation: 'portrait' } });
|
|
|
|
// @ts-ignore
|
|
// const base64Img = new Buffer.from(imageBuffer).toString('base64')
|
|
const base64Img = await toDataURL('/assets/projects/nutech.png')
|
|
const logo = workbook.addImage({
|
|
base64: base64Img as string,
|
|
extension: 'png',
|
|
});
|
|
worksheet.addImage(logo, {
|
|
tl: { col: 1, row: 1 },
|
|
ext: { width: 250, height: 75 }
|
|
})
|
|
|
|
worksheet.mergeCells('B6:K6')
|
|
const titleCell = worksheet.getCell('B6')
|
|
titleCell.font = {
|
|
size: 15,
|
|
underline: true,
|
|
bold: true
|
|
}
|
|
titleCell.alignment = {
|
|
horizontal: 'center'
|
|
}
|
|
titleCell.value = "Presence History Report"
|
|
|
|
|
|
// Table Headers
|
|
worksheet.getRow(8).values = columns.map(item => item.header);
|
|
worksheet.getRow(8).font = { bold: true }
|
|
worksheet.getRow(8).eachCell({ includeEmpty: false }, (cell: any) => {
|
|
cell.border = border
|
|
})
|
|
worksheet.columns = columns.map(item => {
|
|
return {
|
|
key: item.key,
|
|
width: item.width || 20,
|
|
style: item.style || { numFmt: 'General' }
|
|
}
|
|
})
|
|
|
|
// Set table value
|
|
srcDataTable.forEach((item, index: number) => {
|
|
const row = worksheet.addRow({
|
|
number: index + 1,
|
|
presence_checkin_code: item.presence_checkin_code,
|
|
working_status: item.working_status,
|
|
presence_date: new Date(item.presence_date as string).toLocaleDateString("id-ID", { timeZone: "Asia/Jakarta" }),
|
|
device: item.device?.name,
|
|
working_hours_start: dayjs(item.working_hours_start).format('HH:mm A'),
|
|
location_name: item.location_name,
|
|
note: item.note,
|
|
face_image: item.face_image ? { text: 'View Photo', hyperlink: process.env.BASE_URL_IMG + item.face_image, font: { color: { argb: '0000FF' } } } : ' ',
|
|
working_hours_end: item.presence_checkout ? dayjs(item.presence_checkout?.working_hours_end).format('HH:mm A') : 'Not check-out yet',
|
|
co_location_name: item.presence_checkout?.location_name || ' ',
|
|
co_device: item.presence_checkout?.device?.name || ' ',
|
|
co_note: item.presence_checkout?.note || ' ',
|
|
co_face_image: item.presence_checkout?.face_image ? { text: 'View Photo', hyperlink: process.env.BASE_URL_IMG + item.presence_checkout?.face_image, font: { color: { argb: '0000FF' } } } : ' ',
|
|
autocheckout: item.presence_checkout?.auto_checkout ? 'Yes' : ' ',
|
|
approval: item.status?.code === 'AP' || item.status?.code === 'NA' || item.status?.code === 'R' ? item.status?.name : ' '
|
|
})
|
|
// console.log(row)
|
|
row.eachCell((cell: any) => {
|
|
cell.border = border
|
|
})
|
|
|
|
row.eachCell(function (cell: any, colNumber: any) {
|
|
if (cell.value && cell.value.hyperlink)
|
|
row.getCell(colNumber).font = { underline: true, color: { argb: "004e47cc" } };
|
|
});
|
|
})
|
|
|
|
// Signature
|
|
let lastRowTable = 0
|
|
await worksheet.eachRow({ includeEmpty: false }, async function (row: any, rowNumber: number) {
|
|
lastRowTable = await rowNumber
|
|
});
|
|
|
|
// const lastCellTable = `${columnToLetter(columns.length - 1)}${lastRowTable + 2}`
|
|
// const signCell = `${columnToLetter(columns.length)}${lastRowTable + 4}`
|
|
// worksheet.mergeCells(signCell, lastCellTable)
|
|
|
|
// const signCellValue = worksheet.getCell(signCell)
|
|
// signCellValue.font = signFont
|
|
// signCellValue.alignment = {
|
|
// horizontal: 'center',
|
|
// wrapText: true
|
|
// }
|
|
// signCellValue.border = border
|
|
// signCellValue.value = `Export by:\n${user?.fullname}`
|
|
|
|
const generateAtCellTitle = worksheet.getCell(`${columnToLetter(columns.length - 1)}${lastRowTable + 2}`)
|
|
generateAtCellTitle.value = 'Generate at'
|
|
generateAtCellTitle.border = border
|
|
generateAtCellTitle.font = { bold: true }
|
|
const generateAtCellValue = worksheet.getCell(`${columnToLetter(columns.length)}${lastRowTable + 2}`)
|
|
generateAtCellValue.value = dayjs().format('MMM DD, YYYY HH:mm:ss')
|
|
generateAtCellValue.border = border
|
|
const generateByCellTitle = worksheet.getCell(`${columnToLetter(columns.length - 1)}${lastRowTable + 3}`)
|
|
generateByCellTitle.value = 'Generate by'
|
|
generateByCellTitle.border = border
|
|
generateByCellTitle.font = { bold: true }
|
|
const generateByCellValue = worksheet.getCell(`${columnToLetter(columns.length)}${lastRowTable + 3}`)
|
|
generateByCellValue.value = user?.fullname
|
|
generateByCellValue.border = border
|
|
|
|
|
|
workbook.creator = user?.fullname;
|
|
workbook.lastModifiedBy = user?.fullname;
|
|
workbook.created = new Date(1985, 8, 30);
|
|
workbook.modified = new Date();
|
|
workbook.lastPrinted = new Date(2016, 9, 27);
|
|
|
|
|
|
// const fileName = `Presence History - ${user.nik} - ${dayjs().format('YYMMDDHHMMSS')}.xlsx`;
|
|
|
|
const fileBuffer = await workbook.xlsx.writeBuffer()
|
|
const blob = new Blob([fileBuffer], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }) // application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/vnd.ms-excel
|
|
return blob
|
|
// const res = await blobToBase64(blob) // => return as base64
|
|
// return res
|
|
} catch (error: any) {
|
|
console.log("ERROR GENERATE EXCEL :", error)
|
|
return null
|
|
}
|
|
}
|
|
|
|
export const exportAllEmployeeHistory = async (user: IUser, srcDataTable: IHistory[]) => {
|
|
try {
|
|
const columns = [
|
|
{ header: 'No', key: 'number', width: 5, style: { numFmt: '0;[Red]0' } },
|
|
{ header: 'Presence code', key: 'presence_checkin_code' },
|
|
{ header: 'Date', key: 'presence_date', style: { numFmt: 'dd/mm/yyyy' } },
|
|
{ header: 'Employee', key: 'employee' },
|
|
{ header: 'Working Status', key: 'working_status' },
|
|
{ header: 'Check-in Time', key: 'working_hours_start', style: { numFmt: '[$-13809]hh:mm:ss;@' } },
|
|
{ header: 'Check-in Location', key: 'location_name' },
|
|
{ header: 'Check-in Device', key: 'device' },
|
|
{ header: 'Check-in Note', key: 'note' },
|
|
{ header: 'Check-in Photo', key: 'face_image' },
|
|
{ header: 'Check-out Time', key: 'working_hours_end', style: { numFmt: '[$-13809]hh:mm:ss;@' } },
|
|
{ header: 'Check-out Location', key: 'co_location_name' },
|
|
{ header: 'Check-out Device', key: 'co_device' },
|
|
{ header: 'Check-out Note', key: 'co_note' },
|
|
{ header: 'Check-out Photo', key: 'co_face_image' },
|
|
{ header: 'Auto Checkout', key: 'autocheckout' },
|
|
{ header: 'Approval', key: 'approval' },
|
|
];
|
|
|
|
const workbook = new Workbook();
|
|
const worksheet = workbook.addWorksheet("Sheet1", { pageSetup: { orientation: 'portrait' } });
|
|
|
|
// @ts-ignore
|
|
// const base64Img = new Buffer.from(imageBuffer).toString('base64')
|
|
const base64Img = await toDataURL('/assets/projects/nutech.png')
|
|
const logo = workbook.addImage({
|
|
base64: base64Img as string,
|
|
extension: 'png',
|
|
});
|
|
worksheet.addImage(logo, {
|
|
tl: { col: 1, row: 1 },
|
|
ext: { width: 250, height: 75 }
|
|
})
|
|
|
|
worksheet.mergeCells('B6:K6')
|
|
const titleCell = worksheet.getCell('B6')
|
|
titleCell.font = {
|
|
size: 15,
|
|
underline: true,
|
|
bold: true
|
|
}
|
|
titleCell.alignment = {
|
|
horizontal: 'center'
|
|
}
|
|
titleCell.value = "Attendance History Report"
|
|
|
|
|
|
// Table Headers
|
|
worksheet.getRow(8).values = columns.map(item => item.header);
|
|
worksheet.getRow(8).font = { bold: true }
|
|
worksheet.getRow(8).eachCell({ includeEmpty: false }, (cell: any) => {
|
|
cell.border = border
|
|
})
|
|
worksheet.columns = columns.map(item => {
|
|
return {
|
|
key: item.key,
|
|
width: item.width || 20,
|
|
style: item.style || { numFmt: 'General' }
|
|
}
|
|
})
|
|
|
|
// Set table value
|
|
srcDataTable.forEach((item, index: number) => {
|
|
const row = worksheet.addRow({
|
|
number: index + 1,
|
|
presence_checkin_code: item.presence_checkin_code,
|
|
presence_date: new Date(item.presence_date as string).toLocaleDateString("id-ID", { timeZone: "Asia/Jakarta" }),
|
|
employee: item.user?.fullname,
|
|
working_status: item.working_status,
|
|
device: item.device?.name,
|
|
working_hours_start: dayjs(item.working_hours_start).format('HH:mm'),
|
|
location_name: item.location_name,
|
|
note: item.note,
|
|
face_image: item.face_image ? { text: 'View Photo', hyperlink: process.env.BASE_URL_IMG + item.face_image, font: { color: { argb: '0000FF' } } } : ' ',
|
|
working_hours_end: item.presence_checkout ? dayjs(item.presence_checkout?.working_hours_end).format('HH:mm') : 'Not check-out yet',
|
|
co_location_name: item.presence_checkout?.location_name || ' ',
|
|
co_device: item.presence_checkout?.device?.name || ' ',
|
|
co_note: item.presence_checkout?.note || ' ',
|
|
co_face_image: item.presence_checkout?.face_image ? { text: 'View Photo', hyperlink: process.env.BASE_URL_IMG + item.presence_checkout?.face_image, font: { color: { argb: '0000FF' } } } : ' ',
|
|
autocheckout: item.presence_checkout?.auto_checkout ? 'Yes' : ' ',
|
|
approval: item.status?.code === 'AP' || item.status?.code === 'NA' || item.status?.code === 'R' ? item.status?.name : ' '
|
|
})
|
|
// console.log(row)
|
|
row.eachCell((cell: any) => {
|
|
cell.border = border
|
|
})
|
|
|
|
row.eachCell(function (cell: any, colNumber: any) {
|
|
if (cell.value && cell.value.hyperlink)
|
|
row.getCell(colNumber).font = { underline: true, color: { argb: "004e47cc" } };
|
|
});
|
|
})
|
|
|
|
// Signature
|
|
let lastRowTable = 0
|
|
await worksheet.eachRow({ includeEmpty: false }, async function (row: any, rowNumber: number) {
|
|
lastRowTable = await rowNumber
|
|
});
|
|
|
|
const generateAtCellTitle = worksheet.getCell(`${columnToLetter(columns.length - 1)}${lastRowTable + 2}`)
|
|
generateAtCellTitle.value = 'Generate at'
|
|
generateAtCellTitle.border = border
|
|
generateAtCellTitle.font = { bold: true }
|
|
const generateAtCellValue = worksheet.getCell(`${columnToLetter(columns.length)}${lastRowTable + 2}`)
|
|
generateAtCellValue.value = dayjs().format('MMM DD, YYYY HH:mm:ss')
|
|
generateAtCellValue.border = border
|
|
const generateByCellTitle = worksheet.getCell(`${columnToLetter(columns.length - 1)}${lastRowTable + 3}`)
|
|
generateByCellTitle.value = 'Generate by'
|
|
generateByCellTitle.border = border
|
|
generateByCellTitle.font = { bold: true }
|
|
const generateByCellValue = worksheet.getCell(`${columnToLetter(columns.length)}${lastRowTable + 3}`)
|
|
generateByCellValue.value = user?.fullname
|
|
generateByCellValue.border = border
|
|
|
|
|
|
workbook.creator = user?.fullname;
|
|
workbook.lastModifiedBy = user?.fullname;
|
|
workbook.created = new Date(1985, 8, 30);
|
|
workbook.modified = new Date();
|
|
workbook.lastPrinted = new Date(2016, 9, 27);
|
|
|
|
|
|
// const fileName = `Presence History - ${user.nik} - ${dayjs().format('YYMMDDHHMMSS')}.xlsx`;
|
|
|
|
const fileBuffer = await workbook.xlsx.writeBuffer()
|
|
const blob = new Blob([fileBuffer], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }) // application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/vnd.ms-excel
|
|
return blob
|
|
// const res = await blobToBase64(blob) // => return as base64
|
|
// return res
|
|
} catch (error: any) {
|
|
console.log("ERROR GENERATE EXCEL :", error)
|
|
return null
|
|
}
|
|
}
|
|
|
|
const border: any = {
|
|
top: { style: 'thin' },
|
|
left: { style: 'thin' },
|
|
bottom: { style: 'thin' },
|
|
right: { style: 'thin' },
|
|
}
|
|
|
|
const signFont = {
|
|
name: "Comic Sans MS",
|
|
family: 4,
|
|
size: 11,
|
|
underline: true,
|
|
bold: true
|
|
}
|
|
|
|
export function columnToLetter(column: number) {
|
|
var temp, letter = '';
|
|
while (column > 0) {
|
|
temp = (column - 1) % 26;
|
|
letter = String.fromCharCode(temp + 65) + letter;
|
|
column = (column - temp - 1) / 26;
|
|
}
|
|
return letter;
|
|
} |