Files

350 lines
13 KiB
PHP

<?php
require('fpdf/fpdf.php');
class BA_Template extends FPDF
{
protected $content;
protected $isFirstPage = true;
protected $B = 0;
protected $I = 0;
protected $U = 0;
public function __construct($content)
{
parent::__construct();
$this->content = $content;
$this->SetMargins(20, 25, 20);
$this->AddPage();
}
// ===========================================================
// ✅ WriteHTML() — mendukung <b><i><u><br><p> + justify tanpa pecah kalimat
// ===========================================================
function WriteHTML($html)
{
$html = str_replace("\n", ' ', $html);
$html = preg_replace('/\s+/', ' ', $html);
// Pisah berdasarkan tag HTML
$a = preg_split('/<(.*)>/U', $html, -1, PREG_SPLIT_DELIM_CAPTURE);
$buffer = ''; // simpan teks inline sementara
foreach ($a as $i => $e) {
if ($i % 2 == 0) {
// Konten teks biasa
$buffer .= html_entity_decode($e, ENT_QUOTES, 'UTF-8');
} else {
// Tag HTML
if ($e[0] == '/') {
// Tag tutup
$tag = strtoupper(substr($e, 1));
if (in_array($tag, ['B', 'I', 'U'])) {
$this->SetStyle($tag, false);
} elseif ($tag == 'P') {
if (trim($buffer) != '') {
$this->MultiCell(0, 6, trim($buffer), 0, 'J');
$buffer = '';
}
$this->Ln(6);
}
} else {
// Tag buka
$a2 = explode(' ', $e);
$tag = strtoupper(array_shift($a2));
$attr = [];
foreach ($a2 as $v) {
if (preg_match('/([^=]*)=["\']?([^"\']*)/', $v, $a3))
$attr[strtoupper($a3[1])] = $a3[2];
}
if (in_array($tag, ['B', 'I', 'U'])) {
$this->SetStyle($tag, true);
} elseif ($tag == 'BR') {
// Flush teks sebelum ganti baris
if (trim($buffer) != '') {
$this->MultiCell(0, 6, trim($buffer), 0, 'J');
$buffer = '';
}
$this->Ln(6);
} elseif ($tag == 'P') {
// Flush sebelum paragraf baru
if (trim($buffer) != '') {
$this->MultiCell(0, 6, trim($buffer), 0, 'J');
$buffer = '';
}
$this->Ln(6);
}
}
}
}
// flush sisa buffer terakhir
if (trim($buffer) != '') {
$this->MultiCell(0, 6, trim($buffer), 0, 'J');
}
}
// ===========================================================
// SetStyle untuk <b><i><u>
// ===========================================================
function SetStyle($tag, $enable)
{
$this->$tag += ($enable ? 1 : -1);
$style = '';
foreach (['B', 'I', 'U'] as $s) {
if ($this->$s > 0) $style .= $s;
}
$this->SetFont('', $style);
}
// ===========================================================
// HEADER
// ===========================================================
function Header()
{
$header = $this->content['header'];
if (file_exists('controller/surat_pdf/bg-pdf.png')) {
$this->Image('controller/surat_pdf/bg-pdf.png', 0, 0, 210, 297);
}
if ($this->isFirstPage) {
if (!empty($header['imglogo']) && file_exists($header['imglogo'])) {
$this->Image($header['imglogo'], 25, 5, 45);
}
$this->SetY(12);
$this->SetFont('Arial', 'B', 12);
$this->SetTextColor(255, 102, 0);
$this->Cell(0, 7, strtoupper($header['nama_perusahaan']), 0, 1, 'R');
$this->SetFont('Arial', '', 10);
$this->SetTextColor(80, 80, 80);
$this->SetX(130);
$this->Cell(0, 5, $header['kantor'], 0, 1, 'R');
$this->Ln(1);
$iconSize = 4;
$lineHeight = 5.5;
$lineKosong = ' ';
$startX = 160;
$this->SetFont('Arial', '', 9);
$this->SetTextColor(90, 90, 90);
// Telp
$this->SetX(120);
$this->Cell(0, $lineHeight, $header['telp'] . $lineKosong, 0, 0, 'R');
if (file_exists('controller/surat_pdf/icon-phone.png')) {
$this->Image('controller/surat_pdf/icon-phone.png', $startX + 25, $this->GetY() + 0.5, $iconSize);
}
$this->Ln($lineHeight);
// Email
$this->SetX(120);
$this->Cell(0, $lineHeight, $header['email'] . $lineKosong, 0, 0, 'R');
if (file_exists('controller/surat_pdf/icon-mail.png')) {
$this->Image('controller/surat_pdf/icon-mail.png', $startX + 25, $this->GetY() + 0.5, $iconSize);
}
$this->Ln($lineHeight);
// Website
$this->SetX(120);
$this->Cell(0, $lineHeight, $header['website'] . $lineKosong, 0, 0, 'R');
if (file_exists('controller/surat_pdf/icon-web.png')) {
$this->Image('controller/surat_pdf/icon-web.png', $startX + 25, $this->GetY() + 0.5, $iconSize);
}
$this->Ln($lineHeight);
$this->SetDrawColor(128, 128, 128);
$this->SetLineWidth(0.7);
$this->Line(21, 45, 130, 45);
$this->SetDrawColor(255, 102, 0);
$this->Line(132, 45, 189, 45);
$this->Ln(12);
$this->isFirstPage = false;
}
}
// ===========================================================
// FOOTER
// ===========================================================
function Footer()
{
$footer = $this->content['footer'];
$this->SetY(-15);
$this->SetFont('Arial', 'I', 8);
$this->SetTextColor(100, 100, 100);
$this->Cell(0, 10, ($footer['text'] ?? 'Surat') . ' | Halaman ' . $this->PageNo(), 0, 0, 'R');
}
// ===========================================================
// Section Title
// ===========================================================
function SectionTitle($title)
{
$this->Ln(5);
$this->SetFont('Arial', '', 11);
$this->SetTextColor(255, 102, 0);
$this->Cell(0, 8, strtoupper($title), 0, 1, 'L');
$this->SetTextColor(0, 0, 0);
$this->Ln(2);
}
// ===========================================================
// Body Content
// ===========================================================
function BodyContent()
{
$body = $this->content['body'];
if (!empty($body['bodyHead']['text'])) {
$this->SetFont('Arial', '', 11);
$this->WriteHTML($body['bodyHead']['text']);
$this->Ln(5);
}
foreach ($body['bodyData'] as $key => $value) {
$this->SetX(30);
$this->SetFont('Arial', 'B', 11);
$this->Cell(50, 8, $key, 0, 0);
$this->SetFont('Arial', '', 11);
$this->MultiCell(0, 8, ': ' . $value, 0, 'L');
}
foreach ($body['bodyNote'] as $note) {
$this->SectionTitle($note['judul']);
$this->SetFont('Arial', '', 11);
$this->WriteHTML($note['text']);
}
$ttd = $body['bodyTTD'];
$this->Ln(15);
$this->Cell(0, 6, 'Hormat Kami,', 0, 1, 'L');
$this->Ln(20);
if (!empty($ttd['imgttd']) && file_exists($ttd['imgttd'])) {
$this->Image($ttd['imgttd'], 20, $this->GetY() - 20, 35);
}
$this->SetFont('Arial', 'B', 11);
$this->Cell(0, 6, $ttd['nama'], 0, 1, 'L');
$this->SetFont('Arial', '', 10);
$this->Cell(0, 6, $ttd['jabatan'], 0, 1, 'L');
}
}
// ==========================================================
// 🔸 TEMPLATE + FUNCTION
// ==========================================================
function getTemplate($tempalateName)
{
if ($tempalateName == 'ba_pemasangan') {
$template = [
'header' => [
],
'body' => [
'bodyHead' => [
'text' => 'Terimakasih, Anda Telah Berlangganan Layanan Internet di {{1}}<br>Berikut Konfirmasi Detail Data sbb :'
],
'bodyData' => [
],
'bodyNote' => [
[
'judul' => 'CATATAN PENTING',
'text' => 'Untuk biaya pemasangan {{1}},- dibayar setelah pemasangan kepada teknisi. Tidak ada biaya tambahan, jika ada mohon laporkan kepada kami. Pembayaran bulanan dimulai bulan {{2}}. Internet dari tanggal {{3}} adalah gratis.<br>Kami menggunakan sistem pembayaran PRABAYAR, di mana pembayaran bulan {{4}} digunakan untuk layanan bulan tersebut. Wajib berlangganan minimal 6 bulan, jika berhenti sebelum 6 bulan akan dikenakan denda maksimal Rp. 500.000 (disesuaikan). Semua telah dijelaskan oleh teknisi dan diverifikasi oleh tim admin.'
],
[
'judul' => 'CARA PEMBAYARAN',
'text' => 'Pembayaran dapat dilakukan dengan mengklik tombol "Bayar Sekarang" atau melalui tautan yang dikirimkan pada pesan tagihan WhatsApp Official kami mulai bulan {{1}}.'
],
[
'judul' => 'LAYANAN PELANGGAN',
'text' => 'Jika ada kendala, silahkan hubungi Customer Service kami melalui WhatsApp : 081211610233 Dengan menyebutkan Nomor Layanan : {{1}} (tertera di stiker modem).'
],
[
'judul' => 'TENTANG KAMI',
'text' => 'PT. LINTAS JARINGAN NUSANTARA Kantor Layanan Cikembar adalah Penyedia Layanan Internet berbasis Fiber Optic dan Wireless. Kami melayani Corporate, Rumahan, serta Agency Hotspot Unlimited Kuota dengan berbagai pilihan paket menarik dan harga terjangkau. Selain itu kami juga menyediakan jasa setting jaringan LAN, Fiber Optic, Wireless, Web Designer, CCTV, dan lain sebagainya. Kami berkomitmen memberikan pelayanan terbaik, cepat, dan berkualitas kepada seluruh pelanggan kami.'
]
],
'bodyTTD' => [
]
],
'footer' => [
'text' => 'PT. LINTAS JARINGAN NUSANTARA'
]
];
}
elseif ($tempalateName == 'ba_perbaikan') {
$template = [
'header' => [
'imglogo' => 'assets/logo.png',
'nama_perusahaan' => 'PT. LINTAS JARINGAN NUSANTARA',
'kantor' => 'Jl. Raya Sukamaju No. 77, Cimahi',
'telp' => '0813-5678-9876',
'email' => 'support@lnj.co.id',
'website' => 'www.lnj.co.id',
],
'footer' => [
'text' => 'Dokumen Berita Acara Perbaikan',
]
];
}
else {
$template = null;
}
return $template;
}
function mergeTemplateWithData($data)
{
$template = getTemplate($data['template_name']);
$result = $data;
// Header override
$result['header'] = array_merge($template['header'], $data['header']);
// BodyHead: replace {{1}}, {{2}} dst dengan parameter
if (!empty($template['body']['bodyHead']['text'])) {
$text = $template['body']['bodyHead']['text'];
$params = $data['body']['bodyHead']['parameter'] ?? [];
foreach ($params as $i => $val) {
$text = str_replace('{{' . ($i + 1) . '}}', $val, $text);
}
$result['body']['bodyHead']['text'] = $text;
}
// BodyData ambil dari data
$result['body']['bodyData'] = $data['body']['bodyData'];
// BodyNote
foreach ($template['body']['bodyNote'] as $i => $note) {
$text = $note['text'];
$params = $data['body']['bodyNote'][$i]['parameter'] ?? [];
foreach ($params as $p => $v) {
$text = str_replace('{{' . ($p + 1) . '}}', $v, $text);
}
$result['body']['bodyNote'][$i]['text'] = $text;
}
// BodyTTD override data (jika ada gambar)
$result['body']['bodyTTD'] = array_merge($template['body']['bodyTTD'], $data['body']['bodyTTD']);
// Footer
$result['footer'] = array_merge($template['footer'], $data['footer']);
return $result;
}
?>