Mini Shell
<?php
include 'mpdf/mpdf.php';
include 'db/database.php';
// ดึงไฟล์ฟังก์ชันวันที่ภาษาไทยมาใช้งาน
include 'function_datethai.php';
date_default_timezone_set("Asia/Bangkok");
$eng_date = time();
// --- สร้างรูปแบบวันที่ภาษาไทย ---
$thai_day_arr = array("อาทิตย์","จันทร์","อังคาร","พุธ","พฤหัสบดี","ศุกร์","เสาร์");
$thai_month_arr = array("","มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
// วันที่สำหรับ Header ด้านบนขวา (เช่น วันศุกร์ที่ 6 เดือนมีนาคม พ.ศ.2569 11:28 น.)
$header_date = "วัน" . $thai_day_arr[date("w", $eng_date)] . "ที่ " . date("j", $eng_date) . " เดือน" . $thai_month_arr[date("n", $eng_date)] . " พ.ศ." . (date("Y", $eng_date) + 543) . " " . date("H:i", $eng_date) . " น.";
// วันที่สำหรับตรงกลางรายงาน (เช่น 6 มีนาคม 2569)
$body_date = date("j", $eng_date) . " " . $thai_month_arr[date("n", $eng_date)] . " " . (date("Y", $eng_date) + 543);
// รับค่ามาจาก URL
$sahakornID = isset($_GET['sahakornID']) ? $_GET['sahakornID'] : 0;
$year = isset($_GET['year']) ? $_GET['year'] : 0;
// ดึงชื่อสหกรณ์
$stmt_sh_name = mysqli_prepare($link, "SELECT name FROM sahakorn WHERE sahakornID = ?");
mysqli_stmt_bind_param($stmt_sh_name, "i", $sahakornID);
mysqli_stmt_execute($stmt_sh_name);
$res_sh = mysqli_stmt_get_result($stmt_sh_name);
$row_sh = mysqli_fetch_assoc($res_sh);
$sh_name = isset($row_sh['name']) ? $row_sh['name'] : '-';
mysqli_stmt_close($stmt_sh_name);
// ดึงชื่อวัตถุดิบทั้งหมดเพื่อทำหัวคอลัมน์
$sql_all_ingredients = "SELECT DISTINCT S_mother_pui
FROM sahakorn_process
WHERE sahakornID = ? AND year = ?
ORDER BY S_mother_pui ASC";
$stmt_all_ing = mysqli_prepare($link, $sql_all_ingredients);
mysqli_stmt_bind_param($stmt_all_ing, "ii", $sahakornID, $year);
mysqli_stmt_execute($stmt_all_ing);
$res_all_ing = mysqli_stmt_get_result($stmt_all_ing);
$all_ingredients = array();
$sum_ingredients_cols = array();
while($row_ing = mysqli_fetch_assoc($res_all_ing)){
$all_ingredients[] = $row_ing['S_mother_pui'];
$sum_ingredients_cols[$row_ing['S_mother_pui']] = 0;
}
mysqli_stmt_close($stmt_all_ing);
// ดึงข้อมูลตารางประวัติ
$sql_data = "SELECT sp.formulaID, f.name_formula, sp.sack, sp.S_mother_pui, sp.sum
FROM sahakorn_process sp
LEFT JOIN formula f ON sp.formulaID = f.formulaID
WHERE sp.sahakornID = ? AND sp.year = ?
ORDER BY sp.formulaID ASC";
$stmt_data = mysqli_prepare($link, $sql_data);
mysqli_stmt_bind_param($stmt_data, "ii", $sahakornID, $year);
mysqli_stmt_execute($stmt_data);
$res_data = mysqli_stmt_get_result($stmt_data);
$report_data = array();
while($row_d = mysqli_fetch_assoc($res_data)){
$fname = $row_d['name_formula'];
if(!isset($report_data[$fname])) {
$report_data[$fname] = array(
'sack' => $row_d['sack'],
'total_sum' => 0,
'ingredients' => array()
);
}
$report_data[$fname]['ingredients'][$row_d['S_mother_pui']] = $row_d['sum'];
$report_data[$fname]['total_sum'] += $row_d['sum'];
}
mysqli_stmt_close($stmt_data);
ob_start(); // เริ่มเก็บ HTML Content
?>
<div style="text-align: center; margin-bottom: 20px;">
<h2 style="font-weight: bold; margin-top: 0;">รายงานคำนวนแปรรูปปุ๋ยผสม</h2>
<h3 style="margin-top: 5px; margin-bottom: 5px;">ชื่อสหกรณ์: <?php echo htmlspecialchars($sh_name); ?></h3>
<h3 style="margin-top: 5px; margin-bottom: 5px;">ปีบัญชี: <?php echo $year; ?></h3>
<p style="font-size: 14pt;">วันที่ออกรายงาน: <?php echo $body_date; ?></p>
</div>
<table class="report-table">
<thead>
<tr>
<th class="text-center" width="6%">ลำดับ</th>
<th class="text-center" width="22%">สูตรปุ๋ยที่ผสม</th>
<th class="text-center" width="14%">จำนวนที่ผสม<br>(กระสอบ)</th>
<?php foreach($all_ingredients as $ing) { ?>
<th class="text-center"><?php echo htmlspecialchars($ing); ?></th>
<?php } ?>
<th class="text-center" width="15%">ยอดรวม<br>(กิโลกรัม)</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$grand_total_sack = 0;
$grand_total_sum = 0;
if (count($report_data) > 0) {
foreach($report_data as $fname => $data) {
$grand_total_sack += $data['sack'];
$grand_total_sum += $data['total_sum'];
?>
<tr>
<td class="text-center"><?php echo $i++; ?></td>
<td class="text-left"><?php echo htmlspecialchars($fname); ?></td>
<td class="text-right"><?php echo number_format($data['sack'], 2); ?></td>
<?php
foreach($all_ingredients as $ing) {
$val = isset($data['ingredients'][$ing]) ? $data['ingredients'][$ing] : 0;
$sum_ingredients_cols[$ing] += $val;
?>
<td class="text-right"><?php echo ($val > 0) ? number_format($val, 2) : '-'; ?></td>
<?php } ?>
<td class="text-right"><b><?php echo number_format($data['total_sum'], 2); ?></b></td>
</tr>
<?php
}
?>
<tr>
<td colspan="2" class="text-center"><b>รวมทั้งหมด</b></td>
<td class="text-right"><b><?php echo number_format($grand_total_sack, 2); ?></b></td>
<?php foreach($all_ingredients as $ing) { ?>
<td class="text-right"><b><?php echo number_format($sum_ingredients_cols[$ing], 2); ?></b></td>
<?php } ?>
<td class="text-right"><b><?php echo number_format($grand_total_sum, 2); ?></b></td>
</tr>
<?php } else {
$colspan = 4 + count($all_ingredients);
?>
<tr><td colspan="<?php echo $colspan; ?>" class="text-center">ไม่พบข้อมูลการแปรรูปปุ๋ยในปีบัญชีนี้</td></tr>
<?php } ?>
</tbody>
</table>
<?php
$html = ob_get_contents(); // โหลดเนื้อหา HTML ทั้งหมดมาไว้ในตัวแปร
ob_end_clean();
$mpdf = new mPDF('utf-8');
// -----------------------------------------------------
// ตั้งค่า Header/Footer ตามรูปที่แนบมา
// -----------------------------------------------------
$mpdf->SetHeader(' | รายงานการคำนวนแปรรูปปุ๋ยผสม | ออกรายงานเมื่อ: ' . $header_date);
// เพิ่มระยะขอบบน เพื่อไม่ให้ตารางชิดเส้น Header มากเกินไป
$mpdf->margin_header = 9;
$mpdf->margin_top = 22;
$mpdf->margin_footer = 9;
$mpdf->SetFooter('หน้า {PAGENO}');
// -----------------------------------------------------
// 1. แยก CSS ออกมาเขียนแยก และระบุ Class .report-table
// เพื่อป้องกันไม่ให้ mPDF นำสไตล์ขอบไปใส่ Header/Footer
// -----------------------------------------------------
$css = "
table.report-table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
table.report-table th, table.report-table td {
border: 1px solid #000000;
padding: 8px 5px;
font-size: 14pt;
}
table.report-table th {
background-color: #f2f2f2;
font-weight: bold;
vertical-align: middle;
}
table.report-table td {
vertical-align: middle;
}
.text-center { text-align: center; }
.text-right { text-align: right; }
.text-left { text-align: left; }
";
// นำเข้า CSS ก่อน (พารามิเตอร์ 1)
$mpdf->WriteHTML($css, 1);
// ถ้ามีไฟล์ CSS เดิมในระบบก็โหลดเข้ามาเสริมได้
$stylesheet = @file_get_contents('assets/css/printpdf.css');
if($stylesheet) {
$mpdf->WriteHTML($stylesheet, 1);
}
// -----------------------------------------------------
// 2. นำเข้า HTML เนื้อหา (พารามิเตอร์ 2)
// -----------------------------------------------------
$mpdf->WriteHTML($html, 2);
// สร้าง PDF
$mpdf->Output('report_fertilizer_'.time().'.pdf','I');
exit;
?>
Zerion Mini Shell 1.0