282 lines
6.6 KiB
TypeScript
282 lines
6.6 KiB
TypeScript
import { exportDataGrid as exportToExcel } from 'devextreme/excel_exporter'
|
|
import {
|
|
Document,
|
|
AlignmentType,
|
|
Packer,
|
|
Paragraph,
|
|
Table,
|
|
TableCell,
|
|
TableRow,
|
|
VerticalAlign,
|
|
TextRun,
|
|
WidthType,
|
|
PageOrientation
|
|
} from 'docx'
|
|
import { saveAs } from 'file-saver'
|
|
import { jsPDF } from 'jspdf'
|
|
import autoTable from 'jspdf-autotable'
|
|
import { Workbook } from 'exceljs'
|
|
import { setHeaderStyle } from '@/report/utils/xlsx'
|
|
import { exportToWord } from './doc/Material_RPM_DOC'
|
|
|
|
const reportName = 'Rekapitulasi Pemakaian Material'
|
|
const fontSize = 5
|
|
|
|
const formatMetaData = (reportMeta: any) => {
|
|
const periode = reportMeta.value.periode ? reportMeta.value.periode.split(' s/d ') : ''
|
|
|
|
let dateFromFormat = ''
|
|
let dateToFormat = ''
|
|
let dayTo = ''
|
|
|
|
if (periode != '') {
|
|
const dateFrom = new Date(periode[0].split('-').reverse().join('-'))
|
|
const dateTo = new Date(periode[1].split('-').reverse().join('-'))
|
|
|
|
dateFromFormat = `${dateFrom.getDate()}-${dateFrom.toLocaleString('id-ID', {
|
|
month: 'long'
|
|
})}-${dateFrom.getFullYear()}`
|
|
|
|
dateToFormat = `${dateTo.getDate()}-${dateTo.toLocaleString('id-ID', {
|
|
month: 'long'
|
|
})}-${dateTo.getFullYear()}`
|
|
|
|
dayTo = dateTo.toLocaleString('id-ID', { weekday: 'long' })
|
|
}
|
|
|
|
return { dateFromFormat, dateToFormat, dayTo }
|
|
}
|
|
|
|
const exportToPDF = (reportMeta: any, data: any) => {
|
|
const meta = formatMetaData(reportMeta)
|
|
const day = new Date().toLocaleString('id-ID', { weekday: 'long' })
|
|
const date = new Date().getDate()
|
|
const month = new Date().toLocaleString('id-ID', { month: 'long' })
|
|
const year = new Date().getFullYear()
|
|
const doc = new jsPDF({
|
|
orientation: 'landscape'
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [
|
|
['PT. PLN(Persero)', '', ''],
|
|
[
|
|
{ content: 'DISTRIBUSI/WILAYAH', styles: { cellWidth: 35 } },
|
|
{ content: ':', styles: { cellWidth: 1 } },
|
|
reportMeta.value.uid
|
|
? reportMeta.value.uid.name.toUpperCase()
|
|
: 'Semua Distribusi/Wilayah'.toUpperCase()
|
|
],
|
|
[
|
|
'AREA',
|
|
':',
|
|
reportMeta.value.up3 ? reportMeta.value.up3.name.toUpperCase() : 'Semua Area'.toUpperCase()
|
|
],
|
|
[
|
|
'POSKO',
|
|
':',
|
|
reportMeta.value.posko
|
|
? reportMeta.value.posko.name.toUpperCase()
|
|
: 'Semua Posko'.toUpperCase()
|
|
]
|
|
],
|
|
styles: {
|
|
fontSize,
|
|
cellPadding: 0.1,
|
|
textColor: [0, 0, 0],
|
|
fontStyle: 'bold'
|
|
},
|
|
theme: 'plain',
|
|
startY: 10
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [
|
|
[`${reportName}`.toUpperCase()],
|
|
[`PERIODE TANGGAL : ${meta.dateFromFormat} SD TGL ${meta.dateToFormat}`]
|
|
],
|
|
styles: {
|
|
fontSize,
|
|
cellPadding: 0.1,
|
|
textColor: [0, 0, 0],
|
|
fontStyle: 'bold',
|
|
halign: 'center'
|
|
},
|
|
theme: 'plain',
|
|
startY: 25
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [
|
|
[
|
|
{
|
|
content: 'No',
|
|
rowSpan: 2
|
|
},
|
|
{
|
|
content: 'Kode Material',
|
|
rowSpan: 2
|
|
},
|
|
{
|
|
content: 'Nama Material',
|
|
rowSpan: 2
|
|
},
|
|
{
|
|
content: 'Satuan',
|
|
rowSpan: 2
|
|
},
|
|
{
|
|
content: 'Sumber Material',
|
|
rowSpan: 2
|
|
},
|
|
{
|
|
content: 'Tanggal',
|
|
colSpan: 31
|
|
},
|
|
{
|
|
content: 'Jumlah',
|
|
rowSpan: 2
|
|
}
|
|
],
|
|
[
|
|
...Array.from({ length: 31 }, (_, i) => i + 1).map((item) => ({
|
|
content: item.toString()
|
|
}))
|
|
]
|
|
],
|
|
body: data.value.map((item: any, i: any) => [
|
|
{ content: ++i, styles: { halign: 'right' } },
|
|
item.kode,
|
|
item.nama_material,
|
|
item.satuan,
|
|
item.sumber_material,
|
|
...Array.from({ length: 31 }, (_, i) => item[`tgl${i + 1}`]),
|
|
item.total
|
|
]),
|
|
styles: {
|
|
fontSize,
|
|
cellPadding: 1,
|
|
lineColor: [0, 0, 0],
|
|
lineWidth: 0.1,
|
|
cellWidth: 'auto'
|
|
},
|
|
rowPageBreak: 'auto',
|
|
headStyles: {
|
|
fillColor: [192, 192, 192],
|
|
textColor: [0, 0, 0],
|
|
fontStyle: 'bold',
|
|
halign: 'center'
|
|
},
|
|
bodyStyles: {
|
|
textColor: [0, 0, 0]
|
|
},
|
|
didParseCell: function (data) {
|
|
if (data.row.section === 'head') {
|
|
data.cell.text = data.cell.text.map(function (word: any) {
|
|
return word.toUpperCase()
|
|
})
|
|
}
|
|
},
|
|
startY: 35
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [
|
|
[`${day}, ${date}-${month}-${year}`],
|
|
[
|
|
{
|
|
content: '(.........................................)',
|
|
styles: { minCellHeight: 8, valign: 'bottom' }
|
|
}
|
|
]
|
|
],
|
|
styles: {
|
|
fontSize,
|
|
cellPadding: 0.1,
|
|
textColor: [0, 0, 0],
|
|
fontStyle: 'bold',
|
|
halign: 'center'
|
|
},
|
|
theme: 'plain',
|
|
tableWidth: 50,
|
|
margin: { left: 230 }
|
|
})
|
|
|
|
doc.save(`Laporan ${reportName}.pdf`, { returnPromise: true }).then(() => {
|
|
console.log('pdf berhasil disimpan')
|
|
})
|
|
}
|
|
|
|
const exportToXLSX = (reportMeta: any, e: any) => {
|
|
const meta = formatMetaData(reportMeta)
|
|
const workbook = new Workbook()
|
|
const worksheet = workbook.addWorksheet(`${reportName}`)
|
|
|
|
setHeaderStyle(worksheet, 1, 1, 'PT. PLN(Persero)')
|
|
setHeaderStyle(
|
|
worksheet,
|
|
2,
|
|
1,
|
|
`DISTRIBUSI/WILAYAH : ${
|
|
reportMeta.value.uid
|
|
? reportMeta.value.uid.name.toUpperCase()
|
|
: 'Semua Distribusi/Wilayah'.toUpperCase()
|
|
}`
|
|
)
|
|
setHeaderStyle(
|
|
worksheet,
|
|
3,
|
|
1,
|
|
`AREA : ${
|
|
reportMeta.value.up3 ? reportMeta.value.up3.name.toUpperCase() : 'Semua Area'.toUpperCase()
|
|
}`
|
|
)
|
|
setHeaderStyle(
|
|
worksheet,
|
|
4,
|
|
1,
|
|
`POSKO : ${
|
|
reportMeta.value.posko
|
|
? reportMeta.value.posko.name.toUpperCase()
|
|
: 'Semua Posko'.toUpperCase()
|
|
}`
|
|
)
|
|
|
|
setHeaderStyle(worksheet, 6, 1, `${reportName}`.toUpperCase(), true)
|
|
setHeaderStyle(
|
|
worksheet,
|
|
7,
|
|
1,
|
|
`PERIODE TANGGAL : ${meta.dateFromFormat} SD TGL ${meta.dateToFormat}`,
|
|
true
|
|
)
|
|
|
|
worksheet.mergeCells('A1:AK1')
|
|
worksheet.mergeCells('A2:AK2')
|
|
worksheet.mergeCells('A3:AK3')
|
|
worksheet.mergeCells('A4:AK4')
|
|
worksheet.mergeCells('A6:AK6')
|
|
worksheet.mergeCells('A7:AK7')
|
|
|
|
exportToExcel({
|
|
component: e.component,
|
|
worksheet,
|
|
autoFilterEnabled: true,
|
|
topLeftCell: { row: 10, column: 1 },
|
|
loadPanel: {
|
|
enabled: false
|
|
}
|
|
}).then(() => {
|
|
workbook.xlsx.writeBuffer().then((buffer: any) => {
|
|
saveAs(new Blob([buffer], { type: 'application/octet-stream' }), `Laporan ${reportName}.xlsx`)
|
|
})
|
|
})
|
|
|
|
e.cancel = true
|
|
}
|
|
|
|
const exportToDOCX = (reportMeta: any, rawData: any) => {
|
|
exportToWord(reportMeta, rawData, `Laporan ${reportName}`, formatMetaData(reportMeta))
|
|
}
|
|
export { exportToPDF, exportToXLSX, exportToDOCX }
|