diff --git a/apply_migrations.php b/apply_migrations.php index 3e8640e..007ed32 100644 --- a/apply_migrations.php +++ b/apply_migrations.php @@ -1,18 +1,40 @@ setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); $files = glob('db/migrations/*.sql'); sort($files); foreach ($files as $file) { echo "Processing $file...\n"; - $sql = file_get_contents($file); - try { - $db->exec($sql); - echo "Done.\n"; - } catch (PDOException $e) { - echo "Error: " . $e->getMessage() . "\n"; + $sql_content = file_get_contents($file); + $sql_content = preg_replace('/--.*$/m', '', $sql_content); + $statements = explode(';', $sql_content); + + foreach ($statements as $sql) { + $sql = trim($sql); + if (empty($sql)) continue; + + try { + // Use query() instead of exec() to handle potential result sets (like SELECT 1) + // and close the cursor explicitly. + $stmt = $db->query($sql); + if ($stmt) { + $stmt->closeCursor(); + } + echo "Executed: " . substr(str_replace("\n", " ", $sql), 0, 60) . "...\n"; + } catch (PDOException $e) { + $msg = $e->getMessage(); + if (strpos($msg, "Duplicate column") !== false || + strpos($msg, "already exists") !== false || + strpos($msg, "Duplicate key") !== false) { + echo "Skipped (Exists): " . substr(str_replace("\n", " ", $sql), 0, 60) . "...\n"; + } else { + echo "Error: " . $msg . "\n"; + } + } } } - +echo "All migrations applied.\n"; diff --git a/db/migrations/20260305_create_suppliers_module.sql b/db/migrations/20260305_create_suppliers_module.sql new file mode 100644 index 0000000..c8e3b0e --- /dev/null +++ b/db/migrations/20260305_create_suppliers_module.sql @@ -0,0 +1,14 @@ +CREATE TABLE IF NOT EXISTS suppliers ( + id INT AUTO_INCREMENT PRIMARY KEY, + name_en VARCHAR(255) NOT NULL, + name_ar VARCHAR(255) NOT NULL, + contact_person VARCHAR(255), + phone VARCHAR(50), + email VARCHAR(100), + address TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +ALTER TABLE drugs ADD COLUMN expiry_date DATE DEFAULT NULL; +ALTER TABLE drugs ADD COLUMN supplier_id INT DEFAULT NULL; +ALTER TABLE drugs ADD CONSTRAINT fk_drugs_supplier FOREIGN KEY (supplier_id) REFERENCES suppliers(id) ON DELETE SET NULL; diff --git a/drugs.php b/drugs.php new file mode 100644 index 0000000..810b8fa --- /dev/null +++ b/drugs.php @@ -0,0 +1,12 @@ +prepare("INSERT INTO drugs (name_en, name_ar, group_id, description_en, description_ar, default_dosage, default_instructions, price) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); - $stmt->execute([$name_en, $name_ar, $group_id, $desc_en, $desc_ar, $dosage, $instructions, $price]); + $stmt = $db->prepare("INSERT INTO drugs (name_en, name_ar, group_id, description_en, description_ar, default_dosage, default_instructions, price, expiry_date, supplier_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + $stmt->execute([$name_en, $name_ar, $group_id, $desc_en, $desc_ar, $dosage, $instructions, $price, $expiry_date, $supplier_id]); $_SESSION['flash_message'] = __('add_drug') . ' ' . __('successfully'); $redirect = true; } @@ -680,10 +681,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $dosage = $_POST['default_dosage'] ?? ''; $instructions = $_POST['default_instructions'] ?? ''; $price = $_POST['price'] ?? 0; + $expiry_date = $_POST['expiry_date'] ?: null; + $supplier_id = $_POST['supplier_id'] ?: null; if ($id && $name_en && $name_ar) { - $stmt = $db->prepare("UPDATE drugs SET name_en = ?, name_ar = ?, group_id = ?, description_en = ?, description_ar = ?, default_dosage = ?, default_instructions = ?, price = ? WHERE id = ?"); - $stmt->execute([$name_en, $name_ar, $group_id, $desc_en, $desc_ar, $dosage, $instructions, $price, $id]); + $stmt = $db->prepare("UPDATE drugs SET name_en = ?, name_ar = ?, group_id = ?, description_en = ?, description_ar = ?, default_dosage = ?, default_instructions = ?, price = ?, expiry_date = ?, supplier_id = ? WHERE id = ?"); + $stmt->execute([$name_en, $name_ar, $group_id, $desc_en, $desc_ar, $dosage, $instructions, $price, $expiry_date, $supplier_id, $id]); $_SESSION['flash_message'] = __('edit_drug') . ' ' . __('successfully'); $redirect = true; } @@ -695,11 +698,143 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } + } elseif ($_POST['action'] === 'add_supplier') { + $name_en = $_POST['name_en'] ?? ''; + $name_ar = $_POST['name_ar'] ?? ''; + $contact = $_POST['contact_person'] ?? ''; + $phone = $_POST['phone'] ?? ''; + $email = $_POST['email'] ?? ''; + $address = $_POST['address'] ?? ''; + + if ($name_en && $name_ar) { + $stmt = $db->prepare("INSERT INTO suppliers (name_en, name_ar, contact_person, phone, email, address) VALUES (?, ?, ?, ?, ?, ?)"); + $stmt->execute([$name_en, $name_ar, $contact, $phone, $email, $address]); + $_SESSION['flash_message'] = __('add_supplier') . ' ' . __('successfully'); + $redirect = true; + } + } elseif ($_POST['action'] === 'edit_supplier') { + $id = $_POST['id'] ?? ''; + $name_en = $_POST['name_en'] ?? ''; + $name_ar = $_POST['name_ar'] ?? ''; + $contact = $_POST['contact_person'] ?? ''; + $phone = $_POST['phone'] ?? ''; + $email = $_POST['email'] ?? ''; + $address = $_POST['address'] ?? ''; + + if ($id && $name_en && $name_ar) { + $stmt = $db->prepare("UPDATE suppliers SET name_en = ?, name_ar = ?, contact_person = ?, phone = ?, email = ?, address = ? WHERE id = ?"); + $stmt->execute([$name_en, $name_ar, $contact, $phone, $email, $address, $id]); + $_SESSION['flash_message'] = __('edit_supplier') . ' ' . __('successfully'); + $redirect = true; + } + } elseif ($_POST['action'] === 'delete_supplier') { + $id = $_POST['id'] ?? ''; + if ($id) { + $stmt = $db->prepare("DELETE FROM suppliers WHERE id = ?"); + $stmt->execute([$id]); + $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); + $redirect = true; + } + } elseif ($_POST['action'] === 'import_drugs_groups') { + if (isset($_FILES['csv_file']) && $_FILES['csv_file']['error'] === UPLOAD_ERR_OK) { + $file = fopen($_FILES['csv_file']['tmp_name'], 'r'); + // Skip header + fgetcsv($file); + + $stmt = $db->prepare("INSERT INTO drugs_groups (name_en, name_ar) VALUES (?, ?)"); + $checkStmt = $db->prepare("SELECT id FROM drugs_groups WHERE name_en = ?"); + + while (($row = fgetcsv($file)) !== false) { + $name_en = $row[0] ?? ''; + $name_ar = $row[1] ?? ''; + + if ($name_en) { + // Check duplicate + $checkStmt->execute([$name_en]); + if (!$checkStmt->fetch()) { + $stmt->execute([$name_en, $name_ar]); + } + } + } + fclose($file); + $_SESSION['flash_message'] = __('import_successfully'); + $redirect = true; + } + } elseif ($_POST['action'] === 'import_drugs') { + if (isset($_FILES['csv_file']) && $_FILES['csv_file']['error'] === UPLOAD_ERR_OK) { + $file = fopen($_FILES['csv_file']['tmp_name'], 'r'); + // Skip header + fgetcsv($file); + + $stmt = $db->prepare("INSERT INTO drugs (name_en, name_ar, group_id, price, expiry_date, supplier_id) VALUES (?, ?, ?, ?, ?, ?)"); + + $groupMap = []; + $supplierMap = []; + + while (($row = fgetcsv($file)) !== false) { + $name_en = $row[0] ?? ''; + $name_ar = $row[1] ?? ''; + $group_name = $row[2] ?? ''; + $price = $row[3] ?? 0; + $expiry = $row[4] ?? null; // YYYY-MM-DD + $supplier_name = $row[5] ?? ''; + + if ($name_en) { + $group_id = null; + if ($group_name) { + if (isset($groupMap[$group_name])) { + $group_id = $groupMap[$group_name]; + } else { + $gStmt = $db->prepare("SELECT id FROM drugs_groups WHERE name_en = ? OR name_ar = ?"); + $gStmt->execute([$group_name, $group_name]); + $gRes = $gStmt->fetch(); + if ($gRes) { + $group_id = $gRes['id']; + } else { + // Create group + $cgStmt = $db->prepare("INSERT INTO drugs_groups (name_en, name_ar) VALUES (?, ?)"); + $cgStmt->execute([$group_name, $group_name]); + $group_id = $db->lastInsertId(); + } + $groupMap[$group_name] = $group_id; + } + } + + $supplier_id = null; + if ($supplier_name) { + if (isset($supplierMap[$supplier_name])) { + $supplier_id = $supplierMap[$supplier_name]; + } else { + $sStmt = $db->prepare("SELECT id FROM suppliers WHERE name_en = ? OR name_ar = ?"); + $sStmt->execute([$supplier_name, $supplier_name]); + $sRes = $sStmt->fetch(); + if ($sRes) { + $supplier_id = $sRes['id']; + } else { + // Create supplier + $csStmt = $db->prepare("INSERT INTO suppliers (name_en, name_ar) VALUES (?, ?)"); + $csStmt->execute([$supplier_name, $supplier_name]); + $supplier_id = $db->lastInsertId(); + } + $supplierMap[$supplier_name] = $supplier_id; + } + } + + // Validate date + if ($expiry && !strtotime($expiry)) $expiry = null; + $stmt->execute([$name_en, $name_ar, $group_id, $price, $expiry, $supplier_id]); + } + } + fclose($file); + $_SESSION['flash_message'] = __('import_successfully'); + $redirect = true; + } + } } if ($redirect) { header("Location: " . $_SERVER['REQUEST_URI']); exit; } -} +} \ No newline at end of file diff --git a/includes/layout/header.php b/includes/layout/header.php index df40957..96f3754 100644 --- a/includes/layout/header.php +++ b/includes/layout/header.php @@ -120,14 +120,15 @@ $site_favicon = !empty($site_settings['company_favicon']) ? $site_settings['comp - + -
+
diff --git a/includes/pages/drugs.php b/includes/pages/drugs.php index e7831b4..954c029 100644 --- a/includes/pages/drugs.php +++ b/includes/pages/drugs.php @@ -3,9 +3,10 @@ $search_name = $_GET['name'] ?? ''; $search_group = $_GET['group_id'] ?? ''; $query = " - SELECT d.*, g.name_$lang as group_name + SELECT d.*, g.name_$lang as group_name, s.name_$lang as supplier_name FROM drugs d LEFT JOIN drugs_groups g ON d.group_id = g.id + LEFT JOIN suppliers s ON d.supplier_id = s.id WHERE 1=1"; $params = []; @@ -27,13 +28,22 @@ $drugs = $stmt->fetchAll(); // Fetch all groups for filter dropdown $gStmt = $db->query("SELECT * FROM drugs_groups ORDER BY name_$lang"); $all_drug_groups = $gStmt->fetchAll(); + +// Fetch all suppliers +$sStmt = $db->query("SELECT * FROM suppliers ORDER BY name_$lang"); +$all_suppliers = $sStmt->fetchAll(); ?>

- +
+ + +
@@ -72,7 +82,8 @@ $all_drug_groups = $gStmt->fetchAll(); # - + + @@ -80,7 +91,7 @@ $all_drug_groups = $gStmt->fetchAll(); - + @@ -105,11 +116,8 @@ $all_drug_groups = $gStmt->fetchAll(); - - - - - + +
@@ -133,3 +141,188 @@ $all_drug_groups = $gStmt->fetchAll();
+ + + + + + + + + + + \ No newline at end of file diff --git a/includes/pages/drugs_groups.php b/includes/pages/drugs_groups.php index 36be019..d1319c1 100644 --- a/includes/pages/drugs_groups.php +++ b/includes/pages/drugs_groups.php @@ -6,9 +6,14 @@ $groups = $stmt->fetchAll();

- +
+ + +
@@ -59,3 +64,117 @@ $groups = $stmt->fetchAll();
+ + + + + + + + + + + \ No newline at end of file diff --git a/includes/pages/suppliers.php b/includes/pages/suppliers.php new file mode 100644 index 0000000..259911d --- /dev/null +++ b/includes/pages/suppliers.php @@ -0,0 +1,200 @@ +prepare($query); +$stmt->execute($params); +$suppliers = $stmt->fetchAll(); +?> + +
+

+ +
+ + +
+
+
+
+ + + +
+
+
+
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
#
+ + +
+
+ +
+
+ + +
+
+
+
+
+ + + + + + + + diff --git a/lang.php b/lang.php index e4e4223..1e2064d 100644 --- a/lang.php +++ b/lang.php @@ -228,7 +228,25 @@ $translations = [ 'no_drugs_found' => 'No drugs found', 'select_drug' => 'Select Drug', 'select' => 'Select', - + 'are_you_sure_delete' => 'Are you sure you want to delete?', + 'action_cannot_be_undone' => 'This action cannot be undone.', + 'select_group' => 'Select Group', + 'suppliers' => 'Suppliers', + 'add_supplier' => 'Add Supplier', + 'edit_supplier' => 'Edit Supplier', + 'delete_supplier' => 'Delete Supplier', + 'no_suppliers_found' => 'No suppliers found', + 'contact_person' => 'Contact Person', + 'select_supplier' => 'Select Supplier', + 'expiry_date' => 'Expiry Date', + 'supplier' => 'Supplier', + 'import' => 'Import', + 'import_csv' => 'Import CSV', + 'upload_csv_file' => 'Upload CSV File', + 'download_sample' => 'Download Sample', + 'import_successfully' => 'Imported successfully', + 'csv_format_drugs' => 'CSV Format: Name (EN), Name (AR), Group Name, Price, Expiry Date (YYYY-MM-DD), Supplier Name', + 'csv_format_groups' => 'CSV Format: Name (EN), Name (AR)', ], 'ar' => [ 'attachment' => 'المرفق', @@ -440,7 +458,7 @@ $translations = [ 'company_logo' => 'شعار الشركة', 'company_favicon' => 'أيقونة الشركة', 'save_changes' => 'حفظ التغييرات', - 'settings_updated_successfully' => 'تم تحديث الإعدادات بنجاح', + 'settings_updated_successfully' => 'Settings updated successfully', 'prescriptions' => 'الوصفات الطبية', 'add_drug' => 'إضافة دواء', 'drug_name' => 'اسم الدواء', @@ -460,6 +478,24 @@ $translations = [ 'no_drugs_found' => 'لم يتم العثور على أدوية', 'select_drug' => 'اختر الدواء', 'select' => 'اختيار', - + 'are_you_sure_delete' => 'Are you sure you want to delete?', + 'action_cannot_be_undone' => 'This action cannot be undone.', + 'select_group' => 'Select Group', + 'suppliers' => 'الموردين', + 'add_supplier' => 'إضافة مورد', + 'edit_supplier' => 'تعديل مورد', + 'delete_supplier' => 'حذف مورد', + 'no_suppliers_found' => 'لم يتم العثور على موردين', + 'contact_person' => 'شخص الاتصال', + 'select_supplier' => 'اختر المورد', + 'expiry_date' => 'تاريخ الانتهاء', + 'supplier' => 'المورد', + 'import' => 'استيراد', + 'import_csv' => 'استيراد ملف CSV', + 'upload_csv_file' => 'رفع ملف CSV', + 'download_sample' => 'تحميل نموذج', + 'import_successfully' => 'تم الاستيراد بنجاح', + 'csv_format_drugs' => 'تنسيق CSV: الاسم (إنجليزي)، الاسم (عربي)، اسم المجموعة، السعر، تاريخ الانتهاء، اسم المورد', + 'csv_format_groups' => 'تنسيق CSV: الاسم (إنجليزي)، الاسم (عربي)', ] ]; diff --git a/print_prescription.php b/print_prescription.php index 0367bf4..6e8aea2 100644 --- a/print_prescription.php +++ b/print_prescription.php @@ -22,7 +22,7 @@ try { SELECT v.*, p.name as patient_name, - p.age, + p.dob, p.gender, d.name_en as doctor_name_en, d.name_ar as doctor_name_ar, @@ -103,7 +103,7 @@ try { Patient Name:
- Age: + Age:
Gender: diff --git a/suppliers.php b/suppliers.php new file mode 100644 index 0000000..97f5062 --- /dev/null +++ b/suppliers.php @@ -0,0 +1,5 @@ +