620 lines
16 KiB
TypeScript
620 lines
16 KiB
TypeScript
import { exportDataGrid as exportToExcel } from 'devextreme/excel_exporter'
|
|
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, formatPercentage } from '@/utils/numbers'
|
|
import { exportToWord, exportDetailToWord } from './doc/RKeluhan_ALL'
|
|
|
|
const reportName = 'Rekapitulasi Keluhan All'
|
|
const fontSize = 5
|
|
const detailFontSize = 5
|
|
|
|
const getGroupParent = (reportMeta: any) => {
|
|
if (reportMeta.ulp.id != 0) {
|
|
return {
|
|
parent: reportMeta.up3.name,
|
|
summaryName: reportMeta.ulp.name,
|
|
summaryKey: 'nama_ulp'
|
|
}
|
|
}
|
|
|
|
if (reportMeta.up3.id != 0) {
|
|
return {
|
|
parent: reportMeta.uid.name,
|
|
summaryName: reportMeta.up3.name,
|
|
summaryKey: 'nama_up3'
|
|
}
|
|
}
|
|
|
|
if (reportMeta.uid.id != 0) {
|
|
return {
|
|
parent: 'regional',
|
|
summaryName: reportMeta.uid.name,
|
|
summaryKey: 'nama_uid'
|
|
}
|
|
}
|
|
|
|
return ''
|
|
}
|
|
|
|
const groupingData = (data: any, reportMeta: any) => {
|
|
const groupedData: any = {}
|
|
const groupParent = getGroupParent(reportMeta)
|
|
|
|
if (groupParent === '') {
|
|
return {
|
|
data,
|
|
parent: '',
|
|
summaryName: ''
|
|
}
|
|
}
|
|
|
|
data.forEach((item: any) => {
|
|
const group = item[groupParent.summaryKey]
|
|
|
|
if (!groupedData[group]) {
|
|
groupedData[group] = []
|
|
}
|
|
|
|
groupedData[group].push(item)
|
|
})
|
|
|
|
return {
|
|
data: groupedData,
|
|
parent: groupParent.parent,
|
|
summaryName: groupParent.summaryName
|
|
}
|
|
}
|
|
|
|
const formatData = (rawData: any, reportMeta: any) => {
|
|
const tempData = groupingData(rawData, reportMeta)
|
|
const data = tempData.data[tempData.summaryName] || tempData.data
|
|
const formattedData: any = []
|
|
|
|
let parentName = ''
|
|
let summaryName = tempData.summaryName ? tempData.summaryName : 'Seluruh Unit'
|
|
|
|
if (tempData.parent === '') {
|
|
parentName = 'Seluruh Distribusi'
|
|
} else if (tempData.parent === 'regional') {
|
|
parentName = tempData.data[tempData.summaryName][0].nama_regional
|
|
} else {
|
|
parentName = tempData.parent
|
|
}
|
|
|
|
const total: any = {
|
|
total: 0,
|
|
total_selesai: 0,
|
|
total_inproses: 0,
|
|
avg_durasi_response: [],
|
|
max_durasi_response: [],
|
|
min_durasi_response: [],
|
|
total_diatas_sla_response: 0,
|
|
total_dibawah_sla_response: 0,
|
|
avg_durasi_recovery: [],
|
|
max_durasi_recovery: [],
|
|
min_durasi_recovery: [],
|
|
total_diatas_sla_recovery: 0,
|
|
total_dibawah_sla_recovery: 0
|
|
}
|
|
|
|
data.forEach((item: any) => {
|
|
total.total += item.total
|
|
total.total_selesai += item.total_selesai
|
|
total.total_inproses += item.total_inproses
|
|
total.avg_durasi_response.push(item.avg_durasi_response)
|
|
total.max_durasi_response.push(item.max_durasi_response)
|
|
total.min_durasi_response.push(item.min_durasi_response)
|
|
total.total_diatas_sla_response += item.total_diatas_sla_response
|
|
total.total_dibawah_sla_response += item.total_dibawah_sla_response
|
|
total.avg_durasi_recovery.push(item.avg_durasi_recovery)
|
|
total.max_durasi_recovery.push(item.max_durasi_recovery)
|
|
total.min_durasi_recovery.push(item.min_durasi_recovery)
|
|
total.total_diatas_sla_recovery += item.total_diatas_sla_recovery
|
|
total.total_dibawah_sla_recovery += item.total_dibawah_sla_recovery
|
|
})
|
|
|
|
const persenSelesai =
|
|
total.total_selesai != total.total ? (total.total_selesai / total.total) * 100 : 100
|
|
const persenInproses =
|
|
total.total_inproses != total.total ? (total.total_inproses / total.total) * 100 : 0
|
|
|
|
formattedData.push([{ content: parentName, colSpan: 17, styles: { fontStyle: 'bold' } }])
|
|
|
|
const result = [
|
|
formatNumber(total.total),
|
|
formatNumber(total.total_selesai),
|
|
formatPercentage(persenSelesai),
|
|
formatNumber(total.total_inproses),
|
|
formatPercentage(persenInproses),
|
|
formatNumber(
|
|
total.avg_durasi_response.length
|
|
? total.avg_durasi_response.reduce((a: any, b: any) => a + b) /
|
|
total.avg_durasi_response.length
|
|
: 0
|
|
),
|
|
formatWaktu(Math.max(...total.max_durasi_response)),
|
|
formatWaktu(Math.min(...total.min_durasi_response)),
|
|
formatNumber(total.total_diatas_sla_response),
|
|
formatNumber(total.total_dibawah_sla_response),
|
|
formatNumber(
|
|
total.avg_durasi_recovery.length
|
|
? total.avg_durasi_recovery.reduce((a: any, b: any) => a + b) /
|
|
total.avg_durasi_recovery.length
|
|
: 0
|
|
),
|
|
formatWaktu(Math.max(...total.max_durasi_recovery)),
|
|
formatWaktu(Math.min(...total.min_durasi_recovery)),
|
|
formatNumber(total.total_diatas_sla_recovery),
|
|
formatNumber(total.total_dibawah_sla_recovery)
|
|
]
|
|
|
|
formattedData.push([{ content: '1', styles: { halign: 'right' } }, summaryName, ...result])
|
|
formattedData.push([{ content: 'TOTAL', colSpan: 2, styles: { fontStyle: 'bold' } }, ...result])
|
|
|
|
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 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 data = formatData(rawData, reportMeta)
|
|
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()
|
|
],
|
|
[
|
|
'UNIT LAYANAN PELANGGAN',
|
|
':',
|
|
reportMeta.ulp
|
|
? reportMeta.ulp.name.toUpperCase()
|
|
: 'Semua Unit Layanan Pelanggan'.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: 'Response Time',
|
|
colSpan: 10
|
|
},
|
|
{
|
|
content: 'Penyelesaian',
|
|
colSpan: 5
|
|
}
|
|
],
|
|
[
|
|
'Total',
|
|
'Selesai',
|
|
'%',
|
|
'Inproses',
|
|
'%',
|
|
'Rata-Rata',
|
|
'Max',
|
|
'Min',
|
|
'>SLA',
|
|
'<SLA',
|
|
'Rata-Rata',
|
|
'Max',
|
|
'Min',
|
|
'>SLA',
|
|
'<SLA'
|
|
]
|
|
],
|
|
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: [
|
|
[`${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 }
|
|
})
|
|
|
|
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 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 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 Lapor',
|
|
'Dalam Proses Bidang',
|
|
'Selesai Bidang Unit',
|
|
'Durasi Response Time',
|
|
'Durasi Recovery Time',
|
|
'Status',
|
|
'IDPEL/NO METER',
|
|
'Nama Pelapor',
|
|
'Alamat Pelapor',
|
|
'No Telp Pelapor',
|
|
'Keterangan Pelapor',
|
|
'Rayon',
|
|
'Uraian',
|
|
'Response Pelanggan'
|
|
]
|
|
],
|
|
body: rawData.map((item: any, i: any) => [
|
|
{ content: i + 1, styles: { halign: 'right' } },
|
|
item.no_laporan,
|
|
item.waktu_lapor,
|
|
item.waktu_response,
|
|
item.waktu_recovery,
|
|
item.durasi_response_time ? formatWaktu(item.durasi_response_time) : '-',
|
|
item.durasi_recovery_time ? formatWaktu(item.durasi_recovery_time) : '-',
|
|
item.status_akhir,
|
|
item.idpel_nometer,
|
|
item.nama_pelapor,
|
|
item.alamat_pelapor,
|
|
item.no_telp_pelapor,
|
|
item.keterangan_pelapor,
|
|
item.nama_ulp,
|
|
item.uraian,
|
|
item.respon_pelanggan
|
|
]),
|
|
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',
|
|
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()
|
|
})
|
|
}
|
|
},
|
|
startY: 24,
|
|
margin: 5
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [
|
|
[`${day}, ${date}-${month}-${year}`],
|
|
[
|
|
{
|
|
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,
|
|
`UNIT LAYANAN PELANGGAN : ${
|
|
reportMeta.ulp
|
|
? reportMeta.ulp.name.toUpperCase()
|
|
: 'Semua Unit Layanan Pelanggan'.toUpperCase()
|
|
}`
|
|
)
|
|
|
|
setHeaderStyle(worksheet, 6, 1, `${reportName}`.toUpperCase(), true)
|
|
setHeaderStyle(
|
|
worksheet,
|
|
7,
|
|
1,
|
|
`PERIODE TANGGAL : ${meta.dateFromFormat} SD TGL ${meta.dateToFormat}`,
|
|
true
|
|
)
|
|
|
|
worksheet.mergeCells('A1:Q1')
|
|
worksheet.mergeCells('A2:Q2')
|
|
worksheet.mergeCells('A3:Q3')
|
|
worksheet.mergeCells('A4:Q4')
|
|
worksheet.mergeCells('A6:Q6')
|
|
worksheet.mergeCells('A7:Q7')
|
|
|
|
exportToExcel({
|
|
component: e.component,
|
|
worksheet,
|
|
autoFilterEnabled: true,
|
|
topLeftCell: { row: 9, 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('A1:P1')
|
|
worksheet.mergeCells('A3:P3')
|
|
worksheet.mergeCells('A4:P4')
|
|
|
|
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
|
|
}
|
|
|
|
const exportToDOCX = (reportMeta: any, rawData: any) => {
|
|
const meta = formatMetaData(reportMeta)
|
|
exportToWord(reportMeta, meta, formatData(rawData, reportMeta), reportName)
|
|
}
|
|
|
|
const exportDetailToDOCX = (reportMeta: any, rawData: any) => {
|
|
const meta = formatMetaData(reportMeta)
|
|
exportDetailToWord(meta, rawData, reportName)
|
|
}
|
|
|
|
export {
|
|
exportToPDF,
|
|
exportToXLSX,
|
|
exportDetailToPDF,
|
|
exportDetailToXLSX,
|
|
exportToDOCX,
|
|
exportDetailToDOCX
|
|
}
|