Mini Shell
<?php
// ปิด error เพื่อป้องกันไม่ให้ข้อความ error รบกวนการสร้างไฟล์ PDF
error_reporting(0);
include 'mpdf/mpdf.php'; // ตรวจสอบชื่อโฟลเดอร์ mpdf ของคุณให้ตรงด้วยนะครับ
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";
}
date_default_timezone_set("Asia/Bangkok");
$eng_date = time();
// รับค่าเงื่อนไขที่ส่งมาจากฟอร์ม
$sahakornID = isset($_POST['sahakornID']) ? mysqli_real_escape_string($link, $_POST['sahakornID']) : '';
$year = isset($_POST['year']) ? mysqli_real_escape_string($link, $_POST['year']) : '';
// ดึงข้อมูลชื่อสหกรณ์และจังหวัด
$sk_name = "ไม่พบข้อมูล";
$sk_province = "-";
if($sahakornID != ''){
$sql_sk = "SELECT name, province FROM sahakorn WHERE sahakornID = '$sahakornID'";
$rs_sk = mysqli_query($link, $sql_sk);
if($row_sk = mysqli_fetch_assoc($rs_sk)){
$sk_name = $row_sk['name'];
$sk_province = $row_sk['province'];
}
}
// ดึงข้อมูลตารางที่ผ่านการคำนวณแล้ว
$sql_data = "SELECT * FROM calculate_fertilizer WHERE sahakornID = '$sahakornID' AND year = '$year'";
$rs_data = mysqli_query($link, $sql_data);
ob_start();
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<style>
/* CSS สำหรับจัดรูปแบบให้ PDF */
body {
font-family: 'Garuda', 'Sarabun', sans-serif;
font-size: 11pt; /* ขนาดฟอนต์พื้นฐานของหน้ากระดาษ */
}
.text-center { text-align: center; }
.text-right { text-align: right; }
.text-left { text-align: left; }
/* ปรับแต่งรูปแบบตารางข้อมูล */
table.report-table {
width: 100%;
border-collapse: collapse;
margin-top: 15px;
font-size: 10pt; /* ปรับลดขนาดฟอนต์ในตารางให้เล็กลงและสมส่วนขึ้น */
}
table.report-table th, table.report-table td {
border: 1px solid #000;
padding: 5px 4px; /* ปรับลดช่องว่างในเซลล์ตารางลงเล็กน้อยให้ดูกระชับ */
}
table.report-table th {
background-color: #eeeeee;
font-weight: bold;
}
/* ปรับแต่งส่วนหัวรายงานตรงกลาง */
.header-report {
text-align: center;
margin-bottom: 20px;
}
.header-report h3 { margin: 0 0 5px 0; padding: 0; font-size: 15pt; font-weight: bold; }
.header-report h4 { margin: 0 0 5px 0; padding: 0; font-size: 13pt; }
.header-report p { margin: 0 0 5px 0; padding: 0; font-size: 11pt; }
</style>
</head>
<body>
<div class="header-report">
<h3>รายงานคำนวณแม่ปุ๋ยคงเหลือ</h3>
<h4>ชื่อสหกรณ์: <?php echo htmlspecialchars($sk_name); ?></h4>
<p>ปีบัญชี: <?php echo htmlspecialchars($year); ?> จังหวัด: <?php echo htmlspecialchars($sk_province); ?></p>
<p>วันที่ออกรายงาน: <?php echo DateThai(date("Y-m-d")); ?></p>
</div>
<table class="report-table">
<thead>
<tr>
<th width="4%" class="text-center">ลำดับ</th>
<th width="15%" class="text-center">แม่ปุ๋ย/สารเติม</th>
<th width="9%" class="text-center">วัตถุดิบคงเหลือ<br>ต้นปี</th>
<th width="9%" class="text-center">ซื้อวัตถุดิบ<br>ระหว่างปี</th>
<th width="10%" class="text-center">รวม</th>
<th width="9%" class="text-center">ขาย<br>วัตถุดิบ</th>
<th width="9%" class="text-center">เบิกวัตถุดิบไป<br>ผลิต</th>
<th width="10%" class="text-center">วัตถุดิบคงเหลือ<br>สิ้นปี</th>
<th width="9%" class="text-center">ปริมาณนับ<br>ได้(กก.)</th>
<th width="8%" class="text-center">ขาด/เกิน</th>
<th width="8%" class="text-center">คิดเป็น<br>กระสอบ</th>
</tr>
</thead>
<tbody>
<?php
if(mysqli_num_rows($rs_data) > 0) {
$i = 1;
// ตัวแปรสำหรับคำนวณผลรวมแถวสุดท้าย
$sum_remaining = 0; $sum_buy = 0; $sum_total_col = 0;
$sum_sell = 0; $sum_pick_up = 0; $sum_year_balance = 0;
$sum_countable = 0; $sum_tooshort = 0; $sum_sack = 0;
while($row = mysqli_fetch_assoc($rs_data)){
$total = $row['remaining'] + $row['buy'];
// บวกรวมค่าแต่ละคอลัมน์
$sum_remaining += $row['remaining'];
$sum_buy += $row['buy'];
$sum_total_col += $total;
$sum_sell += $row['sell'];
$sum_pick_up += $row['pick_up'];
$sum_year_balance += $row['year_balance'];
$sum_countable += $row['countable_F'];
$sum_tooshort += $row['tooshort_F'];
$sum_sack += $row['sack_C'];
?>
<tr>
<td class="text-center"><?php echo $i; ?></td>
<td class="text-left"><?php echo htmlspecialchars($row['name_pui_C']); ?></td>
<td class="text-right"><?php echo number_format($row['remaining'], 2); ?></td>
<td class="text-right"><?php echo number_format($row['buy'], 2); ?></td>
<td class="text-right"><b><?php echo number_format($total, 2); ?></b></td>
<td class="text-right"><?php echo number_format($row['sell'], 2); ?></td>
<td class="text-right"><?php echo number_format($row['pick_up'], 2); ?></td>
<td class="text-right"><b><?php echo number_format($row['year_balance'], 2); ?></b></td>
<td class="text-right"><?php echo number_format($row['countable_F'], 2); ?></td>
<td class="text-right"><?php echo number_format($row['tooshort_F'], 2); ?></td>
<td class="text-right"><?php echo number_format($row['sack_C'], 2); ?></td>
</tr>
<?php
$i++;
}
?>
<tr style="background-color: #f9f9f9; font-weight: bold;">
<td colspan="2" class="text-center">รวมทั้งสิ้น</td>
<td class="text-right"><?php echo number_format($sum_remaining, 2); ?></td>
<td class="text-right"><?php echo number_format($sum_buy, 2); ?></td>
<td class="text-right"><?php echo number_format($sum_total_col, 2); ?></td>
<td class="text-right"><?php echo number_format($sum_sell, 2); ?></td>
<td class="text-right"><?php echo number_format($sum_pick_up, 2); ?></td>
<td class="text-right"><?php echo number_format($sum_year_balance, 2); ?></td>
<td class="text-right"><?php echo number_format($sum_countable, 2); ?></td>
<td class="text-right"><?php echo number_format($sum_tooshort, 2); ?></td>
<td class="text-right"><?php echo number_format($sum_sack, 2); ?></td>
</tr>
<?php
} else {
?>
<tr>
<td colspan="11" class="text-center" style="padding: 15px;">ไม่พบข้อมูลสำหรับการออกรายงาน</td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
<?php
$html = ob_get_contents();
ob_end_clean();
// สร้าง PDF แนวนอน (A4-L)
$mpdf = new mPDF('utf-8','A4-L');
// ตั้งค่า Header (แถบด้านบนสุด)
$mpdf->margin_header = 9;
$mpdf->defaultheaderline = 1; // เปิดใช้งานเส้นขีด 1 เส้นใต้ Header (ค่าปกติ)
$mpdf->SetHeader(' | รายงานการคำนวณวัตถุดิบแม่ปุ๋ยคงเหลือ | ออกรายงานเมื่อ: '.DateThai(date("Y-m-d")));
// ตั้งค่า Footer (แถบด้านล่างสุด)
$mpdf->margin_footer = 9;
$mpdf->defaultfooterline = 0; // ปิดเส้นขอบบน Footer
$mpdf->SetFooter('หน้าที่ {PAGENO}');
// ดึงไฟล์ CSS (ถ้ามี)
if(file_exists('assets/css/printpdf.css')) {
$stylesheet = file_get_contents('assets/css/printpdf.css');
$mpdf->WriteHTML($stylesheet, 1);
}
// วาดเนื้อหา HTML ลง PDF
$mpdf->WriteHTML($html);
// แสดงผลไฟล์ PDF
$mpdf->Output('Report_Fertilizer_'.time().'.pdf','I');
exit;
?>
Zerion Mini Shell 1.0