// Check if accountant is logged in
if (!isset($_SESSION['accountant_id'])) {
header("Location: login.php");
exit();
}
// Retrieve the list of services for the dropdown
try {
stmtservices=pdo->prepare("SELECT service_id, service_name, service_price FROM services");
services = e) {
echo "Error fetching services: " . htmlspecialchars($e->getMessage());
exit();
}
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /;
/!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /;
/!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /;
/!40101 SET NAMES utf8mb4 */;
--
-- Database: u406552082_info
--
-- Table structure for table admins
CREATE TABLE admins (
id int(11) NOT NULL,
username varchar(50) NOT NULL,
password varchar(255) NOT NULL,
email varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table budgets
CREATE TABLE budgets (
id int(11) NOT NULL,
month varchar(7) DEFAULT NULL,
budgeted_revenue decimal(10,2) DEFAULT NULL,
budgeted_expenses decimal(10,2) DEFAULT NULL,
notes text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table comments
CREATE TABLE comments (
id int(11) NOT NULL,
patient_id int(11) NOT NULL,
sender enum('patient','doctor','admin') NOT NULL,
message text NOT NULL,
timestamp datetime DEFAULT current_timestamp(),
parent_id int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table doctors
CREATE TABLE doctors (
id int(11) NOT NULL,
employee_id int(11) NOT NULL,
name varchar(255) NOT NULL,
username varchar(100) NOT NULL,
password varchar(255) NOT NULL,
email varchar(255) NOT NULL,
specialization varchar(255) DEFAULT NULL,
phone varchar(20) DEFAULT NULL,
registration_date date DEFAULT curdate(),
profile_picture varchar(255) DEFAULT NULL,
department varchar(100) DEFAULT NULL,
date_of_joining date DEFAULT NULL,
status enum('active','inactive') DEFAULT 'active'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table doctor_patient
CREATE TABLE doctor_patient (
id int(11) NOT NULL,
doctor_id int(11) NOT NULL,
patient_id int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table employees
CREATE TABLE employees (
id int(11) NOT NULL,
name varchar(255) NOT NULL,
role enum('admin','doctor','staff','accountant') NOT NULL,
email varchar(255) NOT NULL,
phone varchar(20) DEFAULT NULL,
department varchar(100) DEFAULT NULL,
date_of_joining date DEFAULT NULL,
status enum('active','inactive') DEFAULT 'active',
username varchar(100) NOT NULL,
password varchar(255) NOT NULL,
created_at timestamp NULL DEFAULT current_timestamp(),
updated_at timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
profile_picture varchar(255) DEFAULT NULL,
specialization varchar(255) DEFAULT NULL,
is_online tinyint(1) DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table health_records
CREATE TABLE health_records (
id int(11) NOT NULL,
patient_id int(11) NOT NULL,
blood_pressure varchar(20) DEFAULT NULL,
weight decimal(5,2) DEFAULT NULL,
bmi decimal(4,2) DEFAULT NULL,
cholesterol decimal(5,2) DEFAULT NULL,
prescription text DEFAULT NULL,
treatment_progress text DEFAULT NULL,
record_date date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table insurance_claims
CREATE TABLE insurance_claims (
id int(11) NOT NULL,
patient_id int(11) DEFAULT NULL,
invoice_id int(11) DEFAULT NULL,
claim_amount decimal(10,2) DEFAULT NULL,
status enum('Pending','Approved','Rejected') DEFAULT NULL,
submission_date date DEFAULT NULL,
processing_date date DEFAULT NULL,
notes text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table invoices
CREATE TABLE invoices (
id int(11) NOT NULL,
patient_id int(11) NOT NULL,
service_name varchar(255) NOT NULL,
service_description text DEFAULT NULL,
cost decimal(10,2) NOT NULL,
tax_rate decimal(5,2) DEFAULT 0.00,
discount decimal(5,2) DEFAULT 0.00,
total_amount decimal(10,2) NOT NULL,
invoice_date date DEFAULT curdate(),
due_date date DEFAULT NULL,
notes text DEFAULT NULL,
tax decimal(5,2) DEFAULT 0.00
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table invoice_services
CREATE TABLE invoice_services (
id int(11) NOT NULL,
invoice_id int(11) NOT NULL,
service_id int(11) NOT NULL,
service_name varchar(255) NOT NULL,
cost decimal(10,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table patients
CREATE TABLE patients (
id int(11) NOT NULL,
name varchar(100) NOT NULL,
age int(11) NOT NULL,
gender enum('Male','Female','Other') NOT NULL,
phone varchar(20) DEFAULT NULL,
email varchar(100) DEFAULT NULL,
medical_history text DEFAULT NULL,
created_at timestamp NULL DEFAULT current_timestamp(),
updated_at timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
date_of_birth date DEFAULT NULL,
registration_datetime datetime DEFAULT current_timestamp(),
registration_date date DEFAULT NULL,
status enum('Initial Consultation','Diagnostic Phase','Treatment in Progress','Improving','Monitoring','Maintenance Phase','Awaiting Test Results','Follow-up Needed','Paused','Completed Program','Relapsed','Discontinued','Inactive') DEFAULT 'Initial Consultation',
doctor_id int(11) DEFAULT NULL,
address varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table patient_logins
CREATE TABLE patient_logins (
id int(11) NOT NULL,
patient_id int(11) NOT NULL,
username varchar(50) NOT NULL,
password varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table payments
CREATE TABLE payments (
id int(11) NOT NULL,
patient_id int(11) NOT NULL,
invoice_id int(11) NOT NULL,
amount_paid decimal(10,2) NOT NULL,
payment_date date NOT NULL,
payment_method varchar(50) DEFAULT NULL,
notes text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table prescriptions
CREATE TABLE prescriptions (
id int(11) NOT NULL,
patient_id int(11) NOT NULL,
doctor_id int(11) DEFAULT NULL,
medications text NOT NULL,
instructions text DEFAULT NULL,
date_prescribed datetime DEFAULT current_timestamp(),
status enum('active','inactive') DEFAULT 'active'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table salaries
CREATE TABLE salaries (
id int(11) NOT NULL,
employee_id int(11) NOT NULL,
basic_salary decimal(10,2) NOT NULL,
bonuses decimal(10,2) DEFAULT 0.00,
deductions decimal(10,2) DEFAULT 0.00,
overtime_hours int(11) DEFAULT 0,
overtime_rate decimal(10,2) DEFAULT 0.00,
tax decimal(10,2) DEFAULT 0.00,
payment_date date NOT NULL,
net_salary decimal(10,2) NOT NULL,
status enum('Pending','Paid') DEFAULT 'Pending',
notes text DEFAULT NULL,
payslip_path varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table services
CREATE TABLE services (
service_id int(11) NOT NULL,
service_name varchar(255) NOT NULL,
service_price decimal(10,2) NOT NULL,
service_description text DEFAULT NULL,
created_at timestamp NULL DEFAULT current_timestamp(),
updated_at timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE invoice_services
ADD CONSTRAINT invoice_services_ibfk_1 FOREIGN KEY (invoice_id) REFERENCES invoices (id) ON DELETE CASCADE,
ADD CONSTRAINT invoice_services_ibfk_2 FOREIGN KEY (service_id) REFERENCES services (service_id) ON DELETE CASCADE;
--
-- Constraints for table patients
ALTER TABLE patients
ADD CONSTRAINT patients_ibfk_1 FOREIGN KEY (doctor_id) REFERENCES doctors (id) ON DELETE SET NULL ON UPDATE CASCADE;
--
-- Constraints for table patient_logins
ALTER TABLE patient_logins
ADD CONSTRAINT patient_logins_ibfk_1 FOREIGN KEY (patient_id) REFERENCES patients (id) ON DELETE CASCADE;
--
-- Constraints for table payments
ALTER TABLE payments
ADD CONSTRAINT payments_ibfk_1 FOREIGN KEY (patient_id) REFERENCES patients (id) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT payments_ibfk_2 FOREIGN KEY (invoice_id) REFERENCES invoices (id) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table prescriptions
ALTER TABLE prescriptions
ADD CONSTRAINT prescriptions_ibfk_1 FOREIGN KEY (patient_id) REFERENCES patients (id) ON DELETE CASCADE,
ADD CONSTRAINT prescriptions_ibfk_2 FOREIGN KEY (doctor_id) REFERENCES doctors (id) ON DELETE SET NULL;
--
-- Constraints for table salaries
ALTER TABLE salaries
ADD CONSTRAINT salaries_ibfk_1 FOREIGN KEY (employee_id) REFERENCES employees (id) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /;
/!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /;
/!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;) so i want this codes like this admin/create_invoce.php (<?php
//invoice/create_invoice.php
include('../db_connect.php');
// Retrieve the list of services for the dropdown
services=[];sql_services = "SELECT service_id, service_name, service_price FROM services";
stmtservices=pdo->prepare(sqlservices);stmt_services->execute();
services=stmt_services->fetchAll(PDO::FETCH_ASSOC);
// Fetch patient details via AJAX
if (isset(_GET['search_patient'])) {
search_query = '%' . GET[′searchpatient′].′search_id = null;
if (is_numeric(_GET['search_patient'])) {
search_id = $_GET['search_patient'];
}
$sql_patient = "SELECT id, name, phone, email, date_of_birth, registration_date
FROM patients
WHERE name LIKE :search_query";
if ($search_id !== null) {
$sql_patient .= " OR id = :search_id";
}
$stmt_patient = $pdo->prepare($sql_patient);
$stmt_patient->bindParam(':search_query', $search_query, PDO::PARAM_STR);
if ($search_id !== null) {
$stmt_patient->bindParam(':search_id', $search_id, PDO::PARAM_INT);
}
$stmt_patient->execute();
$patients = $stmt_patient->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($patients);
exit();
}
// Handle invoice submission
if (isset(_POST['submit_invoice'])) {
if (!isset(_POST['patient_id']) || _POST['patient_id'] == '') {
error = "Please select a patient.";
} elseif (!isset(POST[′services′])∣∣empty(_POST['services'])) {
error = "Please select at least one service.";
} else {
try {
pdo->beginTransaction();
$patient_id = $_POST['patient_id'];
$services_selected = $_POST['services'];
$notes = $_POST['notes'];
// Calculate subtotal
$subtotal = 0;
foreach ($services_selected as $service_id) {
$sql_service = "SELECT service_name, service_price FROM services WHERE service_id = :service_id";
$stmt_service = $pdo->prepare($sql_service);
$stmt_service->bindParam(':service_id', $service_id, PDO::PARAM_INT);
$stmt_service->execute();
$service = $stmt_service->fetch(PDO::FETCH_ASSOC);
if ($service) {
$subtotal += floatval($service['service_price']);
}
}
// Get discount and tax from POST data
$discount = isset($_POST['discount']) ? floatval($_POST['discount']) : 0; // Discount as amount
$tax = isset($_POST['tax']) ? floatval($_POST['tax']) : 0; // Tax as percentage
// Apply discount first
$taxable_amount = $subtotal - $discount;
if ($taxable_amount < 0) {
$taxable_amount = 0; // Ensure it does not go negative
}
// Calculate tax amount
$tax_amount = $taxable_amount * $tax / 100;
// Calculate total amount
$total = $taxable_amount + $tax_amount;
// Insert invoice into invoices table
$sql_invoice = "INSERT INTO invoices (patient_id, total_amount, invoice_date, notes, discount, tax)
VALUES (:patient_id, :total_amount, CURRENT_DATE, :notes, :discount, :tax)";
$stmt_invoice = $pdo->prepare($sql_invoice);
$stmt_invoice->bindParam(':patient_id', $patient_id, PDO::PARAM_INT);
$stmt_invoice->bindParam(':total_amount', $total);
$stmt_invoice->bindParam(':notes', $notes);
$stmt_invoice->bindParam(':discount', $discount);
$stmt_invoice->bindParam(':tax', $tax);
$stmt_invoice->execute();
$invoice_id = $pdo->lastInsertId();
// Insert services into invoice_services table
foreach ($services_selected as $service_id) {
$sql_service = "SELECT service_name, service_price FROM services WHERE service_id = :service_id";
$stmt_service = $pdo->prepare($sql_service);
$stmt_service->bindParam(':service_id', $service_id, PDO::PARAM_INT);
$stmt_service->execute();
$service = $stmt_service->fetch(PDO::FETCH_ASSOC);
if ($service) {
$sql_invoice_service = "INSERT INTO invoice_services (invoice_id, service_id, service_name, cost)
VALUES (:invoice_id, :service_id, :service_name, :cost)";
$stmt_invoice_service = $pdo->prepare($sql_invoice_service);
$stmt_invoice_service->bindParam(':invoice_id', $invoice_id, PDO::PARAM_INT);
$stmt_invoice_service->bindParam(':service_id', $service_id, PDO::PARAM_INT);
$stmt_invoice_service->bindParam(':service_name', $service['service_name']);
$stmt_invoice_service->bindParam(':cost', $service['service_price']);
$stmt_invoice_service->execute();
}
}
$pdo->commit();
header('Location: view_invoice.php?invoice_id=' . $invoice_id);
exit();
} catch (Exception $e) {
$pdo->rollBack();
$error = "Error creating invoice: " . $e->getMessage();
}
}
Here is the updated accountant/create_invoice.php code, adjusted to function similarly to your admin/create_invoice.php and incorporating your database structure:
We start the session and include the database connection.
We check if an accountant is logged in.
We fetch the list of services and handle any errors.
We handle the AJAX request for patient search.
We process the form submission, performing input validation and using prepared statements to prevent SQL injection.
We calculate the subtotal, discount, tax, and total amount.
We insert the invoice and related services into the database within a transaction.
HTML Section:
We structure the form to include patient search, service selection, invoice summary, and notes.
We display any errors using Bootstrap's alert classes.
JavaScript Section:
We use jQuery to handle patient search via AJAX and dynamically update the service rows.
We calculate the subtotal and total amount on the fly as services are added or removed.
Note: Make sure to adjust any file paths or session variable names ($_SESSION['accountant_id']) according to your actual setup.
Security Considerations:
All database interactions use prepared statements with bound parameters to prevent SQL injection.
User inputs are validated and sanitized appropriately.
Error messages are sanitized using htmlspecialchars.
Database Adjustments:
Ensure that your invoices table has columns matching the ones used in the INSERT statement (e.g., total_amount, notes, discount, tax_rate, cost, service_name).
This should provide you with a functioning create_invoice.php file for your accountant's section, closely matching your admin/create_invoice.php and compatible with your provided database structure.