File: //proc/thread-self/cwd/download_invoice.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once __DIR__ . '/vendor/autoload.php';
use Dompdf\Dompdf;
use Dompdf\Options;
use Picqer\Barcode\BarcodeGeneratorPNG;
session_start();
if (!isset($_SESSION['user'])) {
die("未登入,請重新登入");
}
// 1. Dompdf Options
$options = new Options();
$options->set('defaultFont', 'NotoSansCJKTC');
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true);
$options->set('isFontSubsettingEnabled', true);
$options->set('chroot', __DIR__);
$dompdf = new Dompdf($options);
// 2. 從 Session 取資料
$user = $_SESSION['user'] ?? "未提供";
$name = $_SESSION['name'] ?? "未提供";
$member = $_SESSION['member'] ?? "未提供";
$endday = $_SESSION['endday'] ?? "未提供";
$speed = $_SESSION['speed'] ?? "未提供";
$discount = $_SESSION['discount'] ?? "未提供";
$code2 = $_SESSION['code2'] ?? "0000000000";
$season = $_SESSION['season'] ?? "";
$halfyear = $_SESSION['halfyear'] ?? "";
$yearPlan = $_SESSION['year'] ?? "";
$twoyear = $_SESSION['twoyear'] ?? "";
$today = new DateTime();
$today->modify('+2 days'); // 延後兩天
// 讓 newtoday 顯示「xxxx年xx月xx日」
$newtoday = $today->format('Y年m月d日');
// 3. 第一條碼 (JS 同邏輯)
function generateBarcodeNumberJSStyle($dateObj) {
$year = (int)$dateObj->format('Y') - 1911;
if ($year < 10) {
$year = '0' . $year;
} elseif ($year < 100) {
$year = (string)$year;
} else {
$year = str_pad($year - 100, 2, '0', STR_PAD_LEFT);
}
$month = $dateObj->format('m');
$day = $dateObj->format('d');
return "{$year}{$month}{$day}627";
}
$barcode1 = generateBarcodeNumberJSStyle($today);
// 4. 產生第一 / 第二條碼圖片
$generator = new BarcodeGeneratorPNG();
$barcode1Image = '<img src="data:image/png;base64,'
. base64_encode($generator->getBarcode($barcode1, $generator::TYPE_CODE_39, 1 ,30))
. '" width="200">';
$barcode2Image = '<img src="data:image/png;base64,'
. base64_encode($generator->getBarcode($code2, $generator::TYPE_CODE_39, 1 ,30))
. '" width="250">';
// 5. 第三條碼邏輯
function getDigit($str, $pos) {
return isset($str[$pos - 1]) ? (int)$str[$pos - 1] : 0;
}
function generateThirdBarcode($firstBarcode, $secondBarcode, $monthlyFee) {
$part1 = substr($firstBarcode, 0, 4);
$feeStr = str_pad($monthlyFee, 9, '0', STR_PAD_LEFT);
$sum1 = 0;
foreach ([1, 3, 5, 7, 9] as $pos) {
$sum1 += getDigit($firstBarcode, $pos);
}
foreach ([1, 3, 5, 7, 9, 11, 13, 15] as $pos) {
$sum1 += getDigit($secondBarcode, $pos);
}
foreach ([1, 3, 5, 7, 9] as $pos) {
$sum1 += getDigit($feeStr, $pos);
}
$sum1 += getDigit($firstBarcode, 1) + getDigit($firstBarcode, 3);
$mod1 = $sum1 % 11;
$checkChar1 = ($mod1 === 10) ? "B" : (($mod1 === 0) ? "A" : $mod1);
$sum2 = 0;
foreach ([2, 4, 6, 8] as $pos) {
$sum2 += getDigit($firstBarcode, $pos);
}
foreach ([2, 4, 6, 8, 10, 12, 14, 16] as $pos) {
$sum2 += getDigit($secondBarcode, $pos);
}
foreach ([2, 4, 6, 8] as $pos) {
$sum2 += getDigit($feeStr, $pos);
}
$sum2 += getDigit($firstBarcode, 2) + getDigit($firstBarcode, 4);
$mod2 = $sum2 % 11;
$checkChar2 = ($mod2 === 10) ? "Y" : (($mod2 === 0) ? "X" : $mod2);
return $part1 . $checkChar1 . $checkChar2 . $feeStr;
}
// 6. 取方案金額 => 生成第三條碼
$plans = [];
if (!empty($season)) $plans['season'] = (int)$season;
if (!empty($halfyear)) $plans['halfyear'] = (int)$halfyear;
if (!empty($yearPlan)) $plans['year'] = (int)$yearPlan;
if (!empty($twoyear)) $plans['twoyear'] = (int)$twoyear;
$planNames = [
'season' => '季繳',
'halfyear' => '半年繳',
'year' => '年繳',
'twoyear' => '兩年繳',
];
// 組第三條碼表格
$thirdBarcodesHtml = "";
if (count($plans) > 0) {
foreach ($plans as $key => $price) {
$label = $planNames[$key] ?? $key;
$thirdBarcode = generateThirdBarcode($barcode1, $code2, $price);
// 條碼圖片 + 文字都置中
$thirdBarcodeImg = '
<div style="text-align:center;">
<p style="margin:0;">' . $thirdBarcode . '</p>
<img src="data:image/png;base64,'
. base64_encode($generator->getBarcode($thirdBarcode, $generator::TYPE_CODE_39, 1 ,30))
. '" width="250">
</div>';
$thirdBarcodesHtml .= "
<tr>
<td style='width:90%; font-weight:bold;'>
<div style='text-align:center;'>{$label} <br>{$price}元</div>
</td>
<td>
{$thirdBarcodeImg}
</td>
</tr>
";
}
} else {
// 若沒有任何方案
$thirdBarcodesHtml .= "
<tr>
<td colspan='2' style='color:red; text-align:center;'>未設定任何續約方案</td>
</tr>
";
}
// 7. 組 HTML + CSS
$html = <<<HTML
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<style>
body {
font-family: 'NotoSansCJKTC', sans-serif;
margin: 20px;
font-size: 12px;
line-height: 1.6;
}
.info-table {
width: 100%;
margin-bottom: 5px;
}
.info-table td {
vertical-align: top;
padding: 2px;
}
.info-right {
text-align: right;
}
.layout-table {
width: 100%;
border-collapse: collapse;
}
.layout-table td {
vertical-align: top;
padding: 5px;
}
.notes {
font-size: 10px;
margin-top: 5px;
border: 1px solid #000; /* 加上1px黑色實線邊框 */
padding: 8px; /* 給內容留些內距 */
border-radius: 4px; /* 可選:若想要圓角 */
}
.company-info {
font-size: 14px;
margin-bottom: 10px;
}
.barcode-block {
text-align: center;
margin: 10px 0;
}
.third-table {
border-collapse: collapse;
width: 100%;
}
.third-table th, .third-table td {
border: 1px solid #000;
padding: 6px;
vertical-align: middle; /* 垂直置中 */
}
.title {
font-weight: bold;
}
</style>
</head>
<body>
<!-- 左側深色框 + 右側公司資訊 (同一行) -->
<table style="width: 100%; border-collapse: collapse;">
<tr style="white-space: nowrap;">
<td style="vertical-align: middle;">
<!-- 用 <div> 包起來做 inline-block -->
<div style="
display: inline-block;
background-color: #333;
color: #fff;
padding: 5px 10px;
border-radius: 4px;
font-size: 30px;
font-weight: bold;
white-space: nowrap;">
上網費繳款單
</div>
</td>
<td style="text-align: right;">
桃園市桃園區大興西路二段61號16樓<br>
https://www.ysnet.com.tw<br>
客服專線:03-358-5867 Fax:03-301-2115
</td>
</tr>
</table>
<hr style="border: none; border-top: 1px dotted #000;">
<!-- 公司 & 帳單資訊 -->
<div style="font-size:30px;text-align:center;">亞訊股份有限公司</div>
<!-- 帳單主要資訊 (左邊用戶資料 + 右邊繳費截止日) -->
<table class="info-table">
<tr>
<!-- 左側資訊 -->
<td style="font-size:14px;">
客戶編號:$user<br>
社區名稱:$member<br>
用戶名稱:$name<br>
寬頻方案:$speed<br>
租約到期日:$endday<br>
</td>
<!-- 右側資訊 -->
<td class="info-right" style="width: 220px;">
<table style="border: 1px solid #000; margin-left:auto;">
<tr><td>請您務必於繳費有效日<br>
<strong style =" background-color: #333;color: #fff;font-size:18px;">$newtoday</strong><br>
之前至附近便利商店繳費<br></td></tr>
</table>
</td>
</tr>
</table>
<span style="font-size:24px;">續期優惠:$discount</span>
<div class="notes">
<span style="font-size:12px">注意事項</span>
<p>
1. 用戶可彈性選擇以下一種費率繳納。起租日接續前期到期日計算,為保障上網權益,請準時繳納。<br>
2. 以下租期方案均不綁約,但請用戶依需要選擇適當方案,若中途解約,視為自願放棄預繳優惠。<br>
3. 本單據各項金額項目數字係由機器列印,若有塗改無經收人蓋章無效。<br>
4. 本公司收到款項後十日內,由綠界科技系統開立電子發票,開立完成後會以電子郵件或以簡訊通知發票已開立完成。</p>
</div>
<br>
<hr style="border: none; border-top: 3px dotted #000;">
<span style="display: block;text-align:center;font-size:24px;">亞訊股份有限公司 上網費繳款單 <strong style="font-size:12px">本聯由代收超商收執</strong></span>
<!-- 左右兩欄 -->
<table class="layout-table">
<tr>
<td style="width: 50%;">
<span>
客戶編號:$user<br>
用戶名稱:$name<br>
繳費期限:<strong style =" background-color: #333;color: #fff;font-size:18px;">$newtoday</strong><br>
</span>
<p class="title">便利商店專用條碼區</p>
<!-- 第一條碼 -->
<div class="barcode-block">
<p>第一段條碼</p>
<p>{$barcode1Image}</p>
<span style="font-size:10px;">{$barcode1}</span>
</div>
<!-- 第二條碼 -->
<div class="barcode-block">
<p>第二段條碼</p>
<p>{$barcode2Image}</p>
<span style="font-size:10px;">{$code2}</span>
</div>
</td>
<td style="width: 50%;">
<p class="title">第三條碼 (請選擇金額繳納)</p>
<table class="third-table">
<tr>
<th style="width: 30%;">方案</th>
<th>條碼</th>
</tr>
{$thirdBarcodesHtml}
</table>
</td>
</tr>
</table>
<br>
<hr style="border: none; border-top: 2px dotted #000;">
<p style="font-size:10px;">
桃園市桃園區大興西路二段61號16樓。 https://www.ysnet.com.tw。 客服專線:03-358-5867 Fax:03-301-2115
</p>
</body>
</html>
HTML;
// 8. 輸出 PDF
$dompdf->loadHtml($html);
// portrait 或 landscape 依需求
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream($user.".pdf", ["Attachment" => true]);
exit();