?
| Path : /home/u949514618/domains/drpktiwari.com/public_html/admin/ |
| Current File : /home/u949514618/domains/drpktiwari.com/public_html/admin/generate_image.php |
<?php
include 'db.php';
if (isset($_GET['id'])) {
$patientId = $_GET['id'];
// Fetch Patient Details
$query = "SELECT p.id, p.patient_name, p.contact_number, p.centre_id, p.doctor_id, p.referby,p.date,
GROUP_CONCAT(t.test_name SEPARATOR ', ') as tests,
SUM(pt.payable_amount) as payable_amount, pt.discount, pt.discount_type
FROM patient_registration p
JOIN patient_test_entry pt ON p.id = pt.patient_id
JOIN testentry t ON pt.test_id = t.id
WHERE p.id = '$patientId'
GROUP BY p.id";
$result = mysqli_query($conn, $query);
$data = mysqli_fetch_assoc($result);
// Fetch Centre details
$centre_query = "SELECT centre_name, address, email, phone FROM centre WHERE id = '{$data['centre_id']}'";
$centre_result = mysqli_query($conn, $centre_query);
$centre = mysqli_fetch_assoc($centre_result);
// Determine Refer By Field
if ($data['referby'] === 'Self') {
$referBy = "Self";
} else {
$doctor_query = "SELECT doctor_name FROM doctorentry WHERE id = '{$data['doctor_id']}'";
$doctor_result = mysqli_query($conn, $doctor_query);
$doctor = mysqli_fetch_assoc($doctor_result);
$referBy = $doctor ? $doctor['doctor_name'] : "Unknown Doctor";
}
if ($data) {
$imageWidth = 1414;
$imageHeight = 2000;
$image = imagecreate($imageWidth, $imageHeight);
// Colors
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
// Font settings
$regularFont = __DIR__ . '/arial.ttf';
$boldFont = __DIR__ . '/arialbd.ttf';
$fontSize = 24;
$fontSize14 = 14;
// Function to draw text
function drawText($image, $text, $x, $y, $fontSize, $font, $color, $align = 'left', $imageWidth = 1414) {
$textBox = imagettfbbox($fontSize, 0, $font, $text);
$textWidth = abs($textBox[2] - $textBox[0]);
if ($align === 'center') {
$x = ($imageWidth - $textWidth) / 2; // Center alignment
} elseif ($align === 'right') {
$x = $imageWidth - $textWidth - 50; // Right alignment with padding
}
imagettftext($image, $fontSize, 0, $x, $y, $color, $font, $text);
}
// Function to convert number to words
function numberToWords($number) {
$ones = array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"
);
$tens = array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety");
if ($number < 20) {
return $ones[$number];
} elseif ($number < 100) {
return $tens[intval($number / 10)] . " " . $ones[$number % 10];
} elseif ($number < 1000) {
return $ones[intval($number / 100)] . " Hundred " . numberToWords($number % 100);
} elseif ($number < 100000) {
return numberToWords(intval($number / 1000)) . " Thousand " . numberToWords($number % 1000);
} elseif ($number < 10000000) {
return numberToWords(intval($number / 100000)) . " Lakh " . numberToWords($number % 100000);
} else {
return numberToWords(intval($number / 10000000)) . " Crore " . numberToWords($number % 10000000);
}
}
// Total Calculation
$total_test_amount = $data['payable_amount'];
$discount = $data['discount'];
$discount_type = $data['discount_type'];
$net_payable = max(0, $total_test_amount - $discount);
// Convert Net Payable to Words
$netPayableWords = numberToWords($net_payable) . " Rupees Only";
$formattedDate = date("d-m-Y", strtotime($data['date']));
// Draw text with custom positions
drawText($image, $centre['centre_name'], 50, 50, $fontSize, $boldFont, $black, 'center');
drawText($image, $centre['address'], 50, 95, $fontSize14, $regularFont, $black, 'center');
drawText($image, "Website: www.drpktiwari.com", 300, 130, $fontSize14, $regularFont, $black, 'left');
drawText($image, "Email ID: " . $centre['email'], 350, 130, $fontSize14, $regularFont, $black, 'center');
drawText($image, "Mob: " . $centre['phone'], 950, 130, $fontSize14, $regularFont, $black, 'right');
drawText($image, "__________________________________________________________________________________________________________________________________", 50, 150, $fontSize14, $regularFont, $black, 'left');
drawText($image, "Reg No: " . $data['id'], 50, 200, $fontSize14, $boldFont, $black, 'left');
drawText($image, "Mob No.: " . $data['contact_number'], 50, 240, $fontSize14, $regularFont, $black, 'right');
drawText($image, "Patient Name: " . $data['patient_name'], 50, 240, $fontSize14, $boldFont, $black, 'left');
drawText($image, "Date: " . $formattedDate, 50, 200, $fontSize14, $regularFont, $black, 'right');
// UPDATED: Refer By Field (Self or Doctor's Name)
drawText($image, "Referred By: " . $referBy, 50, 280, $fontSize14, $regularFont, $black, 'left');
drawText($image, "__________________________________________________________________________________________________________________________________", 50, 300, $fontSize14, $regularFont, $black, 'left');
drawText($image, "Tests: " . $data['tests'], 50, 330, $fontSize14, $boldFont, $black, 'left');
drawText($image, "__________________________________________________________________________________________________________________________________", 50, 350, $fontSize14, $regularFont, $black, 'left');
drawText($image, "Total Amount: Rs. ", 50, 400, $fontSize14, $regularFont, $black, 'left');
drawText($image, number_format($total_test_amount, 2), 50, 400, $fontSize14, $boldFont, $black, 'right');
drawText($image, "Discount: Rs. ", 50, 430, $fontSize14, $regularFont, $black, 'left');
drawText($image, number_format($discount, 2), 50, 430, $fontSize14, $boldFont, $black, 'right');
drawText($image, "Payable Amount: Rs. ", 50, 460, $fontSize14, $regularFont, $black, 'left');
drawText($image, number_format($net_payable, 2), 50, 460, $fontSize14, $boldFont, $black, 'right');
drawText($image, "Paid Amount: Rs. ", 50, 490, $fontSize14, $regularFont, $black, 'left');
drawText($image, number_format($net_payable, 2), 50, 490, $fontSize14, $boldFont, $black, 'right');
drawText($image, "Due Amount: Rs. ", 50, 520, $fontSize14, $regularFont, $black, 'left');
drawText($image, "00.00", 50, 520, $fontSize14, $boldFont, $black, 'right');
drawText($image, "__________________________________________________________________________________________________________________________________", 50, 540, $fontSize14, $regularFont, $black, 'left');
drawText($image, "Net Payable in Words: " . $netPayableWords, 50, 570, $fontSize14, $boldFont, $black, 'left');
drawText($image, "Total: " . number_format($net_payable, 2), 50, 570, $fontSize14, $boldFont, $black, 'right');
drawText($image, "__________________________________________________________________________________________________________________________________", 50, 590, $fontSize14, $regularFont, $black, 'left');
drawText($image, "Signature ", 50, 680, $fontSize14, $regularFont, $black, 'right');
drawText($image, "<<<<<<<< For Preventive Health Checkup Visit: www.pktiwari.com >>>>>>>> ", 50, 740, $fontSize, $boldFont, $black, 'center');
drawText($image, "__________________________________________________________________________________________________________________________________", 50, 760, $fontSize14, $regularFont, $black, 'left');
drawText($image, $centre['centre_name'], 50, 860, $fontSize, $boldFont, $black, 'center');
drawText($image, $centre['address'], 50, 915, $fontSize14, $regularFont, $black, 'center');
drawText($image, "Website: www.drpktiwari.com", 300, 940, $fontSize14, $regularFont, $black, 'left');
drawText($image, "Email ID: " . $centre['email'], 350, 940, $fontSize14, $regularFont, $black, 'center');
drawText($image, "Mob: " . $centre['phone'], 950, 940, $fontSize14, $regularFont, $black, 'right');
drawText($image, "__________________________________________________________________________________________________________________________________", 50, 960, $fontSize14, $regularFont, $black, 'left');
drawText($image, "Reg No: " . $data['id'], 50, 1010, $fontSize14, $boldFont, $black, 'left');
drawText($image, "Mob No.: " . $data['contact_number'], 50, 1050, $fontSize14, $regularFont, $black, 'right');
drawText($image, "Patient Name: " . $data['patient_name'], 50, 1050, $fontSize14, $boldFont, $black, 'left');
drawText($image, "Date: " . $formattedDate, 50, 1010, $fontSize14, $regularFont, $black, 'right');
// UPDATED: Refer By Field (Self or Doctor's Name)
drawText($image, "Referred By: " . $referBy, 50, 1090, $fontSize14, $regularFont, $black, 'left');
drawText($image, "__________________________________________________________________________________________________________________________________", 50, 1110, $fontSize14, $regularFont, $black, 'left');
drawText($image, "Tests: " . $data['tests'], 50, 1140, $fontSize14, $boldFont, $black, 'left');
drawText($image, "__________________________________________________________________________________________________________________________________", 50, 1160, $fontSize14, $regularFont, $black, 'left');
drawText($image, "Total Amount: Rs. ", 50, 1210, $fontSize14, $regularFont, $black, 'left');
drawText($image, number_format($total_test_amount, 2), 50, 1210, $fontSize14, $boldFont, $black, 'right');
drawText($image, "Discount: Rs. ", 50, 1240, $fontSize14, $regularFont, $black, 'left');
drawText($image, number_format($discount, 2), 50, 1240, $fontSize14, $boldFont, $black, 'right');
drawText($image, "Payable Amount: Rs. ", 50, 1270, $fontSize14, $regularFont, $black, 'left');
drawText($image, number_format($net_payable, 2), 50, 1270, $fontSize14, $boldFont, $black, 'right');
drawText($image, "Paid Amount: Rs. ", 50, 1300, $fontSize14, $regularFont, $black, 'left');
drawText($image, number_format($net_payable, 2), 50, 1300, $fontSize14, $boldFont, $black, 'right');
drawText($image, "Due Amount: Rs. ", 50, 1330, $fontSize14, $regularFont, $black, 'left');
drawText($image, "00.00", 50, 1330, $fontSize14, $boldFont, $black, 'right');
drawText($image, "__________________________________________________________________________________________________________________________________", 50, 1350, $fontSize14, $regularFont, $black, 'left');
drawText($image, "Net Payable in Words: " . $netPayableWords, 50, 1380, $fontSize14, $boldFont, $black, 'left');
drawText($image, "Total: " . number_format($net_payable, 2), 50, 1380, $fontSize14, $boldFont, $black, 'right');
drawText($image, "__________________________________________________________________________________________________________________________________", 50, 1400, $fontSize14, $regularFont, $black, 'left');
drawText($image, "Signature ", 50, 1490, $fontSize14, $regularFont, $black, 'right');
drawText($image, "<<<<<<<< For Preventive Health Checkup Visit: www.pktiwari.com >>>>>>>> ", 50, 1550, $fontSize, $boldFont, $black, 'center');
drawText($image, "Ab Bimar Hone Se Pahle Health Checkup", 50, 1650, $fontSize, $boldFont, $black, 'center');
drawText($image, "Full Body Checkup @ Rs. 2500 only", 50, 1700, $fontSize, $boldFont, $black, 'center');
drawText($image, "Thanks For Your Visit", 50, 1780, $fontSize14, $regularFont, $black, 'center');
// Save and output image
$filePath = "bills/bill_" . $patientId . ".png";
imagepng($image, $filePath);
imagedestroy($image);
echo json_encode([
"status" => "success",
"image_url" => $filePath,
"contact_number" => $data['contact_number']
]);
} else {
echo json_encode(["status" => "error"]);
}
}
?>