336 lines
8.2 KiB
TypeScript
336 lines
8.2 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 { formatNumber } from '@/utils/numbers'
|
|
import { exportToWord } from './doc/MonalisaLB_EnergyNotSales_DOC'
|
|
|
|
const reportName = '(Monalisa) Energy Not Sales (ENS)'
|
|
const fontSize = 5
|
|
|
|
const groupingData = (data: any) => {
|
|
const groupedData: any = {}
|
|
|
|
data.forEach((item: any) => {
|
|
const { nama_regional } = item
|
|
|
|
if (!groupedData[nama_regional]) {
|
|
groupedData[nama_regional] = { data: [] }
|
|
}
|
|
|
|
groupedData[nama_regional].data.push(item)
|
|
})
|
|
|
|
for (const regional in groupedData) {
|
|
const data = groupedData[regional].data
|
|
|
|
data.forEach((item: any) => {
|
|
if (!groupedData[regional].summary) {
|
|
groupedData[regional].summary = {
|
|
terencana: 0,
|
|
tidak_terencana: 0,
|
|
bencana_alam: 0
|
|
}
|
|
}
|
|
|
|
groupedData[regional].summary.terencana += item.terencana
|
|
groupedData[regional].summary.tidak_terencana += item.tidak_terencana
|
|
groupedData[regional].summary.bencana_alam += item.bencana_alam
|
|
})
|
|
}
|
|
|
|
return groupedData
|
|
}
|
|
|
|
const formatData = (rawData: any) => {
|
|
const data = groupingData(rawData)
|
|
const formattedData: any = []
|
|
|
|
const total: any = {
|
|
terencana: 0,
|
|
tidak_terencana: 0,
|
|
bencana_alam: 0
|
|
}
|
|
|
|
formattedData.push([{ content: 'NASIONAL', colSpan: 6, styles: { fontStyle: 'bold' } }])
|
|
|
|
for (const regional in data) {
|
|
const summary = data[regional].summary
|
|
|
|
formattedData.push([
|
|
regional,
|
|
formatNumber(summary.terencana),
|
|
formatNumber(summary.tidak_terencana),
|
|
formatNumber(summary.bencana_alam),
|
|
formatNumber(summary.terencana + summary.tidak_terencana + summary.bencana_alam),
|
|
formatNumber(summary.terencana + summary.tidak_terencana)
|
|
])
|
|
|
|
total.terencana += summary.terencana
|
|
total.tidak_terencana += summary.tidak_terencana
|
|
total.bencana_alam += summary.bencana_alam
|
|
}
|
|
|
|
formattedData.push([
|
|
{ content: 'TOTAL', styles: { fontStyle: 'bold' } },
|
|
formatNumber(total.terencana),
|
|
formatNumber(total.tidak_terencana),
|
|
formatNumber(total.bencana_alam),
|
|
formatNumber(total.terencana + total.tidak_terencana + total.bencana_alam),
|
|
formatNumber(total.terencana + total.tidak_terencana)
|
|
])
|
|
|
|
return formattedData
|
|
}
|
|
|
|
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)
|
|
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()
|
|
],
|
|
[
|
|
'REGIONAL',
|
|
':',
|
|
reportMeta.regional
|
|
? reportMeta.regional.name.toUpperCase()
|
|
: 'Semua Regional'.toUpperCase()
|
|
]
|
|
],
|
|
styles: {
|
|
fontSize,
|
|
cellPadding: 0.1,
|
|
textColor: [0, 0, 0],
|
|
fontStyle: 'bold'
|
|
},
|
|
theme: 'plain',
|
|
startY: 10
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [[`${reportName}`.toUpperCase()], [reportMeta.periode]],
|
|
styles: {
|
|
fontSize,
|
|
cellPadding: 0.1,
|
|
textColor: [0, 0, 0],
|
|
fontStyle: 'bold',
|
|
halign: 'center'
|
|
},
|
|
theme: 'plain',
|
|
startY: 23
|
|
})
|
|
|
|
autoTable(doc, {
|
|
head: [
|
|
[
|
|
{
|
|
content: 'Nama Unit',
|
|
rowSpan: 2
|
|
},
|
|
{
|
|
content: 'ENS (Distribusi)',
|
|
colSpan: 3
|
|
},
|
|
{
|
|
content: 'Total',
|
|
rowSpan: 2
|
|
},
|
|
{
|
|
content: 'KPI ENS',
|
|
rowSpan: 2
|
|
}
|
|
],
|
|
['Terencana', 'Tidak Terencana', 'Bencana Alam'],
|
|
['1', '2', '3', '4', '5 = 2+3+4', '6 = 2+3']
|
|
],
|
|
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 exportToXLSX = (reportMeta: any, e: any) => {
|
|
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,
|
|
5,
|
|
1,
|
|
`REGIONAL : ${
|
|
reportMeta.regional ? reportMeta.regional.name.toUpperCase() : 'Semua Regional'.toUpperCase()
|
|
}`
|
|
)
|
|
|
|
setHeaderStyle(worksheet, 7, 1, `${reportName}`.toUpperCase(), true)
|
|
setHeaderStyle(worksheet, 8, 1, reportMeta.periode, true)
|
|
|
|
worksheet.mergeCells('A1:F1')
|
|
worksheet.mergeCells('A2:F2')
|
|
worksheet.mergeCells('A3:F3')
|
|
worksheet.mergeCells('A4:F4')
|
|
worksheet.mergeCells('A5:F5')
|
|
worksheet.mergeCells('A7:F7')
|
|
worksheet.mergeCells('A8:F8')
|
|
|
|
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, formatData(rawData), `Laporan ${reportName}`, null)
|
|
}
|
|
|
|
export { exportToPDF, exportToXLSX, exportToDOCX}
|