510 lines
12 KiB
TypeScript
510 lines
12 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 { formatWaktu } from '@/components/Form/FiltersType/reference'
|
|
import { setHeaderStyle } from '@/report/utils/xlsx'
|
|
import { formatNumber } from '@/utils/numbers'
|
|
|
|
const reportName = 'Rekapitulasi Cleansing Transaksi TM'
|
|
const fontSize = 5
|
|
const detailFontSize = 5
|
|
|
|
const formatData = (rawData: any) => {
|
|
const formattedData: any = []
|
|
const total: any = {
|
|
month1: 0,
|
|
month2: 1,
|
|
month3: 0,
|
|
month4: 0,
|
|
month5: 0,
|
|
month6: 0,
|
|
month7: 0,
|
|
month8: 0,
|
|
month9: 0,
|
|
month10: 0,
|
|
month11: 0,
|
|
month12: 0
|
|
}
|
|
|
|
formattedData.push([{ content: 'SELURUH UNIT', colSpan: 14, styles: { fontStyle: 'bold' } }])
|
|
|
|
formattedData.push(
|
|
...rawData.map((item: any, i: any) => {
|
|
total.month1 += item.month1
|
|
total.month2 += item.month2
|
|
total.month3 += item.month3
|
|
total.month4 += item.month4
|
|
total.month5 += item.month5
|
|
total.month6 += item.month6
|
|
total.month7 += item.month7
|
|
total.month8 += item.month8
|
|
total.month9 += item.month9
|
|
total.month10 += item.month10
|
|
total.month11 += item.month11
|
|
total.month12 += item.month12
|
|
|
|
return [
|
|
{ content: ++i, styles: { halign: 'right' } },
|
|
item.nama_ulp,
|
|
formatNumber(item.month1),
|
|
formatNumber(item.month2),
|
|
formatNumber(item.month3),
|
|
formatNumber(item.month4),
|
|
formatNumber(item.month5),
|
|
formatNumber(item.month6),
|
|
formatNumber(item.month7),
|
|
formatNumber(item.month8),
|
|
formatNumber(item.month9),
|
|
formatNumber(item.month10),
|
|
formatNumber(item.month11),
|
|
formatNumber(item.month12)
|
|
]
|
|
})
|
|
)
|
|
|
|
formattedData.push([
|
|
{ content: 'TOTAL', colSpan: 2, styles: { fontStyle: 'bold' } },
|
|
total.month1,
|
|
total.month2,
|
|
total.month3,
|
|
total.month4,
|
|
total.month5,
|
|
total.month6,
|
|
total.month7,
|
|
total.month8,
|
|
total.month9,
|
|
total.month10,
|
|
total.month11,
|
|
total.month12
|
|
])
|
|
|
|
return formattedData
|
|
}
|
|
|
|
const formatMetaData = (reportMeta: any) => {
|
|
const periode = reportMeta.periode ? reportMeta.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, rawData: any, preview: boolean = false) => {
|
|
const data = formatData(rawData)
|
|
const meta = formatMetaData(reportMeta)
|
|
const doc = new jsPDF({
|
|
orientation: 'landscape'
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [
|
|
['PT. PLN(Persero)', '', ''],
|
|
[
|
|
{ content: 'UNIT INDUK', styles: { cellWidth: 40 } },
|
|
{ content: ':', styles: { cellWidth: 1 } },
|
|
reportMeta.uid
|
|
? reportMeta.uid.name.toUpperCase()
|
|
: 'Semua Unit Induk Distribusi/Wilayah'.toUpperCase()
|
|
],
|
|
[
|
|
'UNIT PELAKSANA PELAYANAN PELANGGAN',
|
|
':',
|
|
reportMeta.up3
|
|
? reportMeta.up3.name.toUpperCase()
|
|
: 'Semua Unit Pelaksanaan Pelayanan Pelanggan'.toUpperCase()
|
|
],
|
|
[
|
|
'POSKO',
|
|
':',
|
|
reportMeta.posko ? reportMeta.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: 23
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [
|
|
[
|
|
{
|
|
content: 'No',
|
|
rowSpan: 2
|
|
},
|
|
{
|
|
content: 'Nama Unit',
|
|
rowSpan: 2
|
|
},
|
|
{
|
|
content: 'Bulan',
|
|
colSpan: 12
|
|
}
|
|
],
|
|
[
|
|
'Januari',
|
|
'Februari',
|
|
'Maret',
|
|
'April',
|
|
'Mei',
|
|
'Juni',
|
|
'Juli',
|
|
'Agustus',
|
|
'September',
|
|
'Oktober',
|
|
'November',
|
|
'Desember'
|
|
]
|
|
],
|
|
body: data,
|
|
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',
|
|
cellWidth: 'wrap',
|
|
halign: 'center',
|
|
valign: 'middle'
|
|
},
|
|
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()
|
|
})
|
|
}
|
|
|
|
if (data.cell.text[0] === 'TOTAL') {
|
|
for (const key in data.row.cells) {
|
|
data.row.cells[key].styles.fillColor = [192, 192, 192]
|
|
data.row.cells[key].styles.fontStyle = 'bold'
|
|
}
|
|
}
|
|
},
|
|
startY: 30
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [
|
|
[`${meta.dayTo}, ${meta.dateToFormat}`],
|
|
[
|
|
{
|
|
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 }
|
|
})
|
|
|
|
if (preview) {
|
|
doc.setProperties({
|
|
title: `${reportName}`
|
|
})
|
|
window.open(doc.output('bloburl'))
|
|
} else {
|
|
doc.save(`Laporan ${reportName}.pdf`, { returnPromise: true }).then(() => {
|
|
console.log('pdf berhasil disimpan')
|
|
})
|
|
}
|
|
}
|
|
|
|
const exportDetailToPDF = (reportMeta: any, rawData: any) => {
|
|
const meta = formatMetaData(reportMeta)
|
|
const doc = new jsPDF({
|
|
orientation: 'landscape'
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [['PT. PLN(Persero)']],
|
|
styles: {
|
|
fontSize: detailFontSize,
|
|
cellPadding: 0.1,
|
|
textColor: [0, 0, 0],
|
|
fontStyle: 'bold'
|
|
},
|
|
theme: 'plain',
|
|
startY: 10,
|
|
margin: 5
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [
|
|
[`Daftar Detail ${reportName}`.toUpperCase()],
|
|
[`PERIODE TANGGAL : ${meta.dateFromFormat} SD TGL ${meta.dateToFormat}`]
|
|
],
|
|
styles: {
|
|
fontSize: detailFontSize,
|
|
cellPadding: 0.1,
|
|
textColor: [0, 0, 0],
|
|
fontStyle: 'bold',
|
|
halign: 'center'
|
|
},
|
|
theme: 'plain',
|
|
startY: 18,
|
|
margin: 5
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [
|
|
[
|
|
'No',
|
|
'No Laporan',
|
|
'Tgl Catat',
|
|
'Tgl Pekerjaan',
|
|
'Tgl Nyala',
|
|
'Durasi Pencatatan',
|
|
'Durasi Recovery Time',
|
|
'Keterangan Cleansing',
|
|
'Posko',
|
|
'Tindakan',
|
|
'Penyebab'
|
|
]
|
|
],
|
|
body: rawData.map((item: any, i: any) => [
|
|
{ content: i + 1, styles: { halign: 'right' } },
|
|
item.no_laporan,
|
|
item.tgl_catat,
|
|
item.tgl_pekerjaan,
|
|
item.tgl_nyala,
|
|
item.durasi_pencatatan,
|
|
item.durasi_recovery_time,
|
|
item.keterangan_cleansing,
|
|
item.nama_posko,
|
|
item.tindakan,
|
|
item.penyebab
|
|
]),
|
|
styles: {
|
|
fontSize: detailFontSize,
|
|
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',
|
|
valign: 'middle'
|
|
},
|
|
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: 24,
|
|
margin: 5
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [
|
|
[`${meta.dayTo}, ${meta.dateToFormat}`],
|
|
[
|
|
{
|
|
content: '(.........................................)',
|
|
styles: { minCellHeight: 8, valign: 'bottom' }
|
|
}
|
|
]
|
|
],
|
|
styles: {
|
|
fontSize: detailFontSize,
|
|
cellPadding: 0.1,
|
|
textColor: [0, 0, 0],
|
|
fontStyle: 'bold',
|
|
halign: 'center'
|
|
},
|
|
theme: 'plain',
|
|
tableWidth: 50,
|
|
margin: { left: 230 }
|
|
})
|
|
|
|
doc.save(`Laporan Detail ${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,
|
|
`UNIT INDUK : ${
|
|
reportMeta.uid
|
|
? reportMeta.uid.name.toUpperCase()
|
|
: 'Semua Unit Induk Distribusi/Wilayah'.toUpperCase()
|
|
}`
|
|
)
|
|
setHeaderStyle(
|
|
worksheet,
|
|
3,
|
|
1,
|
|
`UNIT PELAKSANA PELAYANAN PELANGGAN : ${
|
|
reportMeta.up3
|
|
? reportMeta.up3.name.toUpperCase()
|
|
: 'Semua Unit Pelaksanaan Pelayanan Pelanggan'.toUpperCase()
|
|
}`
|
|
)
|
|
setHeaderStyle(
|
|
worksheet,
|
|
4,
|
|
1,
|
|
`POSKO : ${
|
|
reportMeta.posko ? reportMeta.posko.name.toUpperCase() : 'Semua Posko'.toUpperCase()
|
|
}`
|
|
)
|
|
|
|
setHeaderStyle(worksheet, 7, 1, `${reportName}`.toUpperCase(), true)
|
|
setHeaderStyle(
|
|
worksheet,
|
|
8,
|
|
1,
|
|
`PERIODE TANGGAL : ${meta.dateFromFormat} SD TGL ${meta.dateToFormat}`,
|
|
true
|
|
)
|
|
|
|
worksheet.mergeCells('A1:M1')
|
|
worksheet.mergeCells('A2:M2')
|
|
worksheet.mergeCells('A3:M3')
|
|
worksheet.mergeCells('A4:M4')
|
|
worksheet.mergeCells('A7:M7')
|
|
worksheet.mergeCells('A8:M8')
|
|
|
|
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 exportDetailToXLSX = (reportMeta: any, e: any) => {
|
|
const meta = formatMetaData(reportMeta)
|
|
const workbook = new Workbook()
|
|
const worksheet = workbook.addWorksheet(`Detail ${reportName}`)
|
|
|
|
setHeaderStyle(worksheet, 1, 1, 'PT. PLN(Persero)')
|
|
setHeaderStyle(worksheet, 3, 1, `Daftar Detail ${reportName}`.toUpperCase(), true)
|
|
setHeaderStyle(
|
|
worksheet,
|
|
4,
|
|
1,
|
|
`PERIODE TANGGAL : ${meta.dateFromFormat} SD TGL ${meta.dateToFormat}`,
|
|
true
|
|
)
|
|
|
|
worksheet.mergeCells('A3:K3')
|
|
worksheet.mergeCells('A4:K4')
|
|
|
|
exportToExcel({
|
|
component: e.component,
|
|
worksheet,
|
|
autoFilterEnabled: true,
|
|
topLeftCell: { row: 6, column: 1 },
|
|
loadPanel: {
|
|
enabled: false
|
|
}
|
|
}).then(() => {
|
|
workbook.xlsx.writeBuffer().then((buffer: any) => {
|
|
saveAs(
|
|
new Blob([buffer], { type: 'application/octet-stream' }),
|
|
`Laporan Detail ${reportName}.xlsx`
|
|
)
|
|
})
|
|
})
|
|
|
|
e.cancel = true
|
|
}
|
|
|
|
export { exportToPDF, exportToXLSX, exportDetailToPDF, exportDetailToXLSX }
|