Mini Shell
<?php
// เริ่มสร้างไฟล์สำหรับ PDF
include 'mpdf/mpdf.php';
include 'db/database.php';
// ฟังก์ชันสำหรับแปลงวันที่เป็นภาษาไทย
function DateThai($strDate) {
$strYear = date("Y",strtotime($strDate))+543;
$strMonth= date("n",strtotime($strDate));
$strDay= date("j",strtotime($strDate));
$strMonthCut = Array("","มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
$strMonthThai=$strMonthCut[$strMonth];
return "$strDay $strMonthThai $strYear";
}
// รับค่าจากหน้าจอหลัก
$search_sahakornID = isset($_POST['sahakornID']) ? $_POST['sahakornID'] : '';
$search_year = isset($_POST['year']) ? $_POST['year'] : '';
// ป้องกันการเข้าถึงไฟล์โดยตรงแบบไม่ได้ส่งค่ามา
if(empty($search_sahakornID) || empty($search_year)){
echo "กรุณาเลือกข้อมูลสหกรณ์และปีบัญชีก่อนพิมพ์รายงาน";
exit;
}
// 1. ดึงข้อมูลจากฐานข้อมูล
$sql_report = "SELECT c.*, f.name_formula, s.name as sahakorn_name, s.province
FROM calculate_remaining c
LEFT JOIN sahakorn s ON c.sahakornID = s.sahakornID
LEFT JOIN formula f ON c.formulaID = f.formulaID
WHERE c.sahakornID = '$search_sahakornID' AND c.year = '$search_year'
ORDER BY f.name_formula ASC";
$res_report = mysqli_query($link, $sql_report);
$report_data = [];
$header_sahakorn_name = "ไม่ทราบชื่อสหกรณ์";
$header_province = "-";
if($res_report && mysqli_num_rows($res_report) > 0) {
while($row = mysqli_fetch_assoc($res_report)){
$report_data[] = $row;
}
$header_sahakorn_name = $report_data[0]['sahakorn_name'];
$header_province = $report_data[0]['province'];
} else {
$res_sk_name = mysqli_query($link, "SELECT name FROM sahakorn WHERE sahakornID = '$search_sahakornID'");
if($res_sk_name && mysqli_num_rows($res_sk_name) > 0) {
$header_sahakorn_name = mysqli_fetch_assoc($res_sk_name)['name'];
}
}
// เริ่มเก็บ HTML ใส่ตัวแปร
ob_start();
?>
<style>
body {
font-family: "Garuda", "Kinnari", sans-serif;
color: #000000;
}
/* ปรับแต่งตาราง */
table.report-table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
table.report-table th, table.report-table td {
border: 1px solid #000000;
padding: 5px 3px; /* ลด Padding ให้ตารางดูไม่บวม */
color: #000000;
}
table.report-table th {
text-align: center;
background-color: #f2f2f2; /* สีพื้นหลังหัวตาราง */
font-weight: bold;
font-size: 10pt; /* ลดขนาดหัวตาราง */
}
table.report-table td {
text-align: right;
font-size: 9pt; /* ลดขนาดเนื้อหาในตาราง */
}
.text-center { text-align: center !important; }
.text-left { text-align: left !important; }
.text-right { text-align: right !important; }
.total-row td {
font-weight: bold;
background-color: #e8ede1;
}
</style>
<div style="text-align: center; margin-bottom: 15px;">
<div style="font-size: 14pt; font-weight: bold; margin-bottom: 6px;">รายงานปุ๋ยผสมคงเหลือ</div>
<div style="font-size: 11pt; font-weight: bold; margin-bottom: 4px;">ชื่อสหกรณ์ : <?php echo $header_sahakorn_name; ?></div>
<div style="font-size: 11pt;">ปีบัญชี : <b><?php echo $search_year; ?></b> จังหวัด : <b><?php echo $header_province; ?></b></div>
</div>
<table class="report-table">
<thead>
<tr>
<th width="4%" rowspan="2">ลำดับ</th>
<th width="18%" rowspan="2">สูตรปุ๋ยที่ผสม</th>
<th rowspan="2">ปุ๋ยผสมคงเหลือ<br>ยกมาต้นปี</th>
<th rowspan="2">รับจากการ<br>ผลิต</th>
<th rowspan="2">รวม</th>
<th rowspan="2">ขายปุ๋ยผสม</th>
<th rowspan="2">เบิกใช้</th>
<th colspan="2">ปุ๋ยผสมคงเหลือ<br>สิ้นปี</th>
<th colspan="2">ปริมาณนับได้</th>
<th colspan="2">ขาด/เกิน</th>
</tr>
<tr>
<th style="font-size: 9pt;">กิโล</th>
<th style="font-size: 9pt;">กระสอบ</th>
<th style="font-size: 9pt;">กิโล</th>
<th style="font-size: 9pt;">กระสอบ</th>
<th style="font-size: 9pt;">กิโล</th>
<th style="font-size: 9pt;">กระสอบ</th>
</tr>
</thead>
<tbody>
<?php
if(count($report_data) > 0) {
$no = 1;
$sum_remaining = 0; $sum_production = 0; $sum_total = 0;
$sum_sell = 0; $sum_use = 0; $sum_balance = 0;
$sum_sack = 0; $sum_countable = 0; $sum_tooshort = 0;
foreach($report_data as $row) {
$row_total = $row['remaining_pui'] + $row['from_production'];
$sum_remaining += $row['remaining_pui'];
$sum_production += $row['from_production'];
$sum_total += $row_total;
$sum_sell += $row['sell_pui'];
$sum_use += $row['use'];
$sum_balance += $row['pui_year_balance'];
$sum_sack += $row['sack_R'];
$sum_countable += $row['countable_R'];
$sum_tooshort += $row['tooshort_R'];
?>
<tr>
<td class="text-center"><?php echo $no++; ?></td>
<td class="text-left"><?php echo $row['name_formula']; ?></td>
<td><?php echo number_format($row['remaining_pui'], 2); ?></td>
<td><?php echo number_format($row['from_production'], 2); ?></td>
<td style="font-weight: bold;"><?php echo number_format($row_total, 2); ?></td>
<td><?php echo number_format($row['sell_pui'], 2); ?></td>
<td><?php echo number_format($row['use'], 2); ?></td>
<td style="font-weight: bold;"><?php echo number_format($row['pui_year_balance'], 2); ?></td>
<td><?php $balance_sack = $row['pui_year_balance'] / 50; echo number_format($balance_sack, 2); ?></td>
<td><?php echo number_format($row['countable_R'], 2); ?></td>
<td><?php $countable_sack = $row['countable_R'] / 50; echo number_format($countable_sack, 2); ?></td>
<td style="font-weight: bold;">
<?php echo number_format($row['tooshort_R'], 2); ?>
</td>
<td style="font-weight: bold;">
<?php $tooshort_sack = $row['tooshort_R'] / 50; echo number_format($tooshort_sack, 2); ?>
</td>
</tr>
<?php
}
?>
<tr class="total-row">
<td colspan="2" class="text-right" style="padding-right: 15px;">ผลรวมทั้งหมด</td>
<td><?php echo number_format($sum_remaining, 2); ?></td>
<td><?php echo number_format($sum_production, 2); ?></td>
<td><?php echo number_format($sum_total, 2); ?></td>
<td><?php echo number_format($sum_sell, 2); ?></td>
<td><?php echo number_format($sum_use, 2); ?></td>
<td><?php echo number_format($sum_balance, 2); ?></td>
<td><?php $sum_balance_sack = $sum_balance / 50; echo number_format($sum_balance_sack, 2); ?></td>
<td><?php echo number_format($sum_countable, 2); ?></td>
<td><?php $sum_countable_sack = $sum_countable / 50; echo number_format($sum_countable_sack, 2); ?></td>
<td><?php echo number_format($sum_tooshort, 2); ?></td>
<td><?php $sum_tooshort_sack = $sum_tooshort / 50; echo number_format($sum_tooshort_sack, 2); ?></td>
</tr>
<?php
} else {
echo '<tr><td colspan="13" class="text-center">ไม่มีข้อมูล</td></tr>';
}
?>
</tbody>
</table>
<?php
$html = ob_get_contents();
ob_end_clean();
// =========================================================
// 2. สร้างเอกสาร mPDF
// =========================================================
$mpdf = new mPDF('utf-8', 'A4-L');
$mpdf->margin_header = 9;
// ส่วน Header ตามที่กำหนด
$eng_date = date("Y-m-d");
$mpdf->SetHeader(' | รายงานปุ๋ยผสมคงเหลือ | ออกรายงานเมื่อ: ' . DateThai($eng_date));
$mpdf->margin_footer = 9;
$mpdf->SetFooter('หน้าที่ {PAGENO}');
// โหลด HTML (ให้ mPDF จัดการแท็ก <style> อัตโนมัติ)
$mpdf->WriteHTML($html);
// สั่งแสดงผลไฟล์ PDF ออกสู่เบราว์เซอร์
$mpdf->Output('Report_Pui_'.time().'.pdf', 'I');
exit;
?>
Zerion Mini Shell 1.0