add migration 14
This commit is contained in:
parent
efe588ff68
commit
ad6d09dcf3
4
db/migrations/014_add_display_name_to_attachments.sql
Normal file
4
db/migrations/014_add_display_name_to_attachments.sql
Normal file
@ -0,0 +1,4 @@
|
||||
-- Migration: Add display_name to attachments table
|
||||
-- This column is needed for the split_mailbox migration (015) to work correctly
|
||||
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS display_name VARCHAR(255) AFTER mail_id;
|
||||
@ -127,50 +127,47 @@ CREATE TABLE IF NOT EXISTS internal_comments (
|
||||
FOREIGN KEY (referred_user_id) REFERENCES users(id) ON DELETE SET NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
-- 4. Migrate data from old mailbox table
|
||||
-- We'll use a temporary mapping for IDs if we were strict, but since we are splitting, we can just insert.
|
||||
-- Note: Original IDs will change, which means attachments and comments must be migrated carefully.
|
||||
|
||||
-- Migrate Inbound
|
||||
INSERT INTO inbound_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at)
|
||||
-- 4. Migrate data (using INSERT IGNORE to allow re-running partially failed migrations)
|
||||
INSERT IGNORE INTO inbound_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at)
|
||||
SELECT id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at
|
||||
FROM mailbox WHERE type = 'inbound';
|
||||
|
||||
INSERT INTO inbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at)
|
||||
INSERT IGNORE INTO inbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at)
|
||||
SELECT a.id, a.mail_id, a.display_name, a.file_path, a.file_name, a.file_size, a.created_at
|
||||
FROM attachments a JOIN mailbox m ON a.mail_id = m.id WHERE m.type = 'inbound';
|
||||
|
||||
INSERT INTO inbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at)
|
||||
INSERT IGNORE INTO inbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at)
|
||||
SELECT c.id, c.mail_id, c.user_id, c.comment, c.referred_user_id, c.created_at
|
||||
FROM comments c JOIN mailbox m ON c.mail_id = m.id WHERE m.type = 'inbound';
|
||||
|
||||
-- Migrate Outbound
|
||||
INSERT INTO outbound_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at)
|
||||
INSERT IGNORE INTO outbound_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at)
|
||||
SELECT id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at
|
||||
FROM mailbox WHERE type = 'outbound';
|
||||
|
||||
INSERT INTO outbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at)
|
||||
INSERT IGNORE INTO outbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at)
|
||||
SELECT a.id, a.mail_id, a.display_name, a.file_path, a.file_name, a.file_size, a.created_at
|
||||
FROM attachments a JOIN mailbox m ON a.mail_id = m.id WHERE m.type = 'outbound';
|
||||
|
||||
INSERT INTO outbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at)
|
||||
INSERT IGNORE INTO outbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at)
|
||||
SELECT c.id, c.mail_id, c.user_id, c.comment, c.referred_user_id, c.created_at
|
||||
FROM comments c JOIN mailbox m ON c.mail_id = m.id WHERE m.type = 'outbound';
|
||||
|
||||
-- Migrate Internal
|
||||
INSERT INTO internal_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at)
|
||||
INSERT IGNORE INTO internal_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at)
|
||||
SELECT id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at
|
||||
FROM mailbox WHERE type = 'internal';
|
||||
|
||||
INSERT INTO internal_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at)
|
||||
INSERT IGNORE INTO internal_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at)
|
||||
SELECT a.id, a.mail_id, a.display_name, a.file_path, a.file_name, a.file_size, a.created_at
|
||||
FROM attachments a JOIN mailbox m ON a.mail_id = m.id WHERE m.type = 'internal';
|
||||
|
||||
INSERT INTO internal_comments (id, mail_id, user_id, comment, referred_user_id, created_at)
|
||||
INSERT IGNORE INTO internal_comments (id, mail_id, user_id, comment, referred_user_id, created_at)
|
||||
SELECT c.id, c.mail_id, c.user_id, c.comment, c.referred_user_id, c.created_at
|
||||
FROM comments c JOIN mailbox m ON c.mail_id = m.id WHERE m.type = 'internal';
|
||||
|
||||
-- 5. Rename old tables instead of dropping for safety
|
||||
RENAME TABLE mailbox TO mailbox_old;
|
||||
RENAME TABLE attachments TO attachments_old;
|
||||
RENAME TABLE comments TO comments_old;
|
||||
-- Using a check because RENAME TABLE fails if the target exists
|
||||
SET @old_mailbox = (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'mailbox_old');
|
||||
SET @sql_rename = IF(@old_mailbox = 0, 'RENAME TABLE mailbox TO mailbox_old, attachments TO attachments_old, comments TO comments_old', 'SELECT 1');
|
||||
PREPARE stmt FROM @sql_rename;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
@ -264,6 +264,9 @@ if (isset($_GET['id'])) {
|
||||
<td class="text-center pe-4">
|
||||
<div class="btn-group shadow-sm rounded">
|
||||
<a href="view_mail.php?id=<?= $mail['id'] ?>&type=inbound" class="btn btn-sm btn-white text-primary border" title="عرض">
|
||||
<a href="print_inbound.php?id=<?= $mail['id' ?>" target="_blank" class="btn btn-sm btn-white text-secondary border" title="طباعة">
|
||||
<i class="fas fa-print"></i>
|
||||
</a>
|
||||
<i class="fas fa-eye"></i>
|
||||
</a>
|
||||
<?php if (canEdit('inbound')): ?>
|
||||
|
||||
313
print_inbound.php
Normal file
313
print_inbound.php
Normal file
@ -0,0 +1,313 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/db/config.php';
|
||||
require_once __DIR__ . '/includes/settings.php';
|
||||
|
||||
// Check if user is logged in
|
||||
session_start();
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
die('Access Denied');
|
||||
}
|
||||
|
||||
// Basic permission check
|
||||
function isAdmin() {
|
||||
if (isset($_SESSION['is_super_admin']) && $_SESSION['is_super_admin'] == 1) return true;
|
||||
if (isset($_SESSION['user_role']) && strtolower($_SESSION['user_role']) === 'admin') return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function canView($page) {
|
||||
if (isAdmin()) return true;
|
||||
return $_SESSION['permissions'][$page]['view'] ?? false;
|
||||
}
|
||||
|
||||
if (!canView('outbound')) {
|
||||
die('Unauthorized access');
|
||||
}
|
||||
|
||||
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
||||
if (!$id) {
|
||||
die('Invalid ID');
|
||||
}
|
||||
|
||||
// Fetch outbound mail details
|
||||
$stmt = db()->prepare("SELECT * FROM inbound_mail WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
$mail = $stmt->fetch();
|
||||
|
||||
if (!$mail) {
|
||||
die('Mail not found');
|
||||
}
|
||||
|
||||
$settings = get_settings();
|
||||
$logo = !empty($settings['site_logo']) ? $settings['site_logo'] : '';
|
||||
$site_name = $settings['site_name'];
|
||||
$site_address = $settings['site_address'];
|
||||
|
||||
/**
|
||||
* Convert Gregorian date to Hijri
|
||||
*/
|
||||
function gregorianToHijri($date) {
|
||||
if (!$date) return '';
|
||||
$time = strtotime($date);
|
||||
$m = date('m', $time);
|
||||
$d = date('d', $time);
|
||||
$y = date('Y', $time);
|
||||
|
||||
if (($y > 1582) || (($y == 1582) && ($m > 10)) || (($y == 1582) && ($m == 10) && ($d > 14))) {
|
||||
$jd = (int)((1461 * ($y + 4800 + (int)(($m - 14) / 12))) / 4) +
|
||||
(int)((367 * ($m - 2 - 12 * ((int)(($m - 14) / 12)))) / 12) -
|
||||
(int)((3 * ((int)(($y + 4900 + (int)(($m - 14) / 12)) / 100))) / 4) +
|
||||
$d - 32075;
|
||||
} else {
|
||||
$jd = 367 * $y - (int)((7 * ($y + 5001 + (int)(($m - 9) / 7))) / 4) + (int)((275 * $m) / 9) + $d + 1729777;
|
||||
}
|
||||
|
||||
$l = $jd - 1948440 + 10632;
|
||||
$n = (int)(($l - 1) / 10631);
|
||||
$l = $l - 10631 * $n + 354;
|
||||
$j = ((int)((10985 - $l) / 5316)) * ((int)((50 * $l) / 17719)) + ((int)($l / 5670)) * ((int)((43 * $l) / 15238));
|
||||
$l = $l - ((int)((30 - $j) / 15)) * ((int)((17719 * $j) / 50)) - ((int)($j / 16)) * ((int)((15238 * $j) / 43)) + 29;
|
||||
|
||||
$month = (int)((24 * $l) / 709);
|
||||
$day = $l - (int)((709 * $month) / 24);
|
||||
$year = 30 * $n + $j - 30;
|
||||
|
||||
$hijriMonths = [
|
||||
1 => "محرم", 2 => "صفر", 3 => "ربيع الأول", 4 => "ربيع الآخر",
|
||||
5 => "جمادى الأولى", 6 => "جمادى الآخرة", 7 => "رجب", 8 => "شعبان",
|
||||
9 => "رمضان", 10 => "شوال", 11 => "ذو القعدة", 12 => "ذو الحجة"
|
||||
];
|
||||
|
||||
return $day . ' ' . $hijriMonths[$month] . ' ' . $year . ' هـ';
|
||||
}
|
||||
|
||||
$hijriDate = gregorianToHijri($mail['date_registered']);
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="ar" dir="rtl">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>طباعة بريد وارد - <?= htmlspecialchars($mail['ref_no']) ?></title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;700&display=swap" rel="stylesheet">
|
||||
<style>
|
||||
@page {
|
||||
size: A4;
|
||||
margin: 0;
|
||||
}
|
||||
body {
|
||||
font-family: 'Cairo', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
background-color: #f4f7f6;
|
||||
color: #333;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.print-wrapper {
|
||||
width: 21cm;
|
||||
margin: 0 auto;
|
||||
background: #fff;
|
||||
padding: 1.5cm;
|
||||
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
||||
min-height: 29.7cm;
|
||||
position: relative;
|
||||
box-sizing: border-box;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
/* Repeating Header */
|
||||
.report-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
flex-grow: 1;
|
||||
}
|
||||
.report-header {
|
||||
display: table-header-group;
|
||||
}
|
||||
.report-footer {
|
||||
display: table-footer-group;
|
||||
}
|
||||
|
||||
/* Header Style */
|
||||
.header-container {
|
||||
border-bottom: 3px double #00827F;
|
||||
padding-bottom: 10px;
|
||||
margin-bottom: 20px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
}
|
||||
.header-logo img {
|
||||
max-height: 80px;
|
||||
}
|
||||
.header-info { text-align: left; }
|
||||
.site-name { font-size: 24px; font-weight: bold; color: #00827F; margin-bottom: 5px; }
|
||||
|
||||
/* Meta & Content */
|
||||
.mail-meta {
|
||||
margin-bottom: 25px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
background: #f9f9f9;
|
||||
padding: 12px;
|
||||
border: 1px solid #eee;
|
||||
}
|
||||
.mail-content {
|
||||
font-size: 18px;
|
||||
text-align: justify;
|
||||
color: #000;
|
||||
min-height: 12cm;
|
||||
padding-bottom: 30px;
|
||||
}
|
||||
|
||||
/* Footer Style */
|
||||
.footer-container {
|
||||
border-top: 3px double #00827F;
|
||||
padding-top: 10px;
|
||||
text-align: center;
|
||||
font-size: 11px;
|
||||
width: 100%;
|
||||
background: #fff;
|
||||
margin-top: auto;
|
||||
}
|
||||
.contact-row {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
gap: 15px;
|
||||
margin-bottom: 5px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.page-num-display {
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
/* Footer Spacer for Table */
|
||||
.footer-spacer {
|
||||
height: 80px;
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* UI Controls */
|
||||
.no-print {
|
||||
position: fixed;
|
||||
top: 20px;
|
||||
left: 20px;
|
||||
background: #00827F;
|
||||
color: #fff;
|
||||
padding: 10px 25px;
|
||||
border: none;
|
||||
border-radius: 30px;
|
||||
cursor: pointer;
|
||||
z-index: 1000;
|
||||
font-family: 'Cairo', sans-serif;
|
||||
font-weight: bold;
|
||||
box-shadow: 0 4px 10px rgba(0,0,0,0.2);
|
||||
}
|
||||
|
||||
@media print {
|
||||
@page {
|
||||
margin: 1cm 0;
|
||||
}
|
||||
body { background: #fff; }
|
||||
.no-print { display: none; }
|
||||
.print-wrapper {
|
||||
width: 100%;
|
||||
margin: 0;
|
||||
padding: 0 1.5cm;
|
||||
box-shadow: none;
|
||||
min-height: auto;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.footer-container {
|
||||
position: fixed;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
padding: 10px 1.5cm;
|
||||
box-sizing: border-box;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.footer-spacer {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<button class="no-print" onclick="window.print()">
|
||||
<i class="fas fa-print"></i> طباعة الوثيقة
|
||||
</button>
|
||||
|
||||
<div class="print-wrapper">
|
||||
<table class="report-table">
|
||||
<thead class="report-header">
|
||||
<tr>
|
||||
<td>
|
||||
<div class="header-container">
|
||||
<div class="header-logo">
|
||||
<?php if ($logo): ?>
|
||||
<img src="<?= htmlspecialchars($logo) ?>" alt="Logo">
|
||||
<?php else: ?>
|
||||
<div style="font-weight: bold; font-size: 26px; color: #00827F;"><?= htmlspecialchars($site_name) ?></div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="header-info">
|
||||
<div class="site-name"><?= htmlspecialchars($site_name) ?></div>
|
||||
<div style="font-size: 12px; color: #666;"><?= htmlspecialchars($site_address) ?></div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="mail-meta">
|
||||
<div><strong>رقم القيد:</strong> <?= htmlspecialchars($mail['ref_no']) ?></div>
|
||||
<div><strong>التاريخ:</strong> <?= htmlspecialchars($hijriDate) ?> | <?= htmlspecialchars($mail['date_registered']) ?>م</div>
|
||||
</div>
|
||||
|
||||
<div class="mail-content">
|
||||
<?= $mail['description'] ?>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
|
||||
<tfoot class="report-footer">
|
||||
<tr>
|
||||
<td>
|
||||
<div class="footer-spacer"></div>
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
|
||||
<div class="footer-container">
|
||||
<div class="contact-row">
|
||||
<span><i class="fas fa-phone"></i> 99621515</span>
|
||||
<span><i class="fas fa-envelope"></i> ahlalhkair@gmail.com</span>
|
||||
<span><i class="fas fa-globe"></i> https://alkhairteam.net/</span>
|
||||
<span class="ms-3">
|
||||
<i class="fab fa-instagram"></i>
|
||||
<i class="fab fa-twitter"></i>
|
||||
<i class="fab fa-facebook"></i>
|
||||
alkhair_team
|
||||
</span>
|
||||
</div>
|
||||
<div class="page-num-display">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@ -207,6 +207,8 @@ if ($type == 'internal') {
|
||||
<a href="<?= $type ?>.php?action=edit&id=<?= $mail['id'] ?>" class="btn btn-outline-primary">تعديل البيانات</a>
|
||||
<?php if ($type === 'outbound'): ?>
|
||||
<a href="print_outbound.php?id=<?= $mail['id'] ?>" target="_blank" class="btn btn-outline-secondary"><i class="fas fa-print me-1"></i> طباعة</a>
|
||||
<?php elseif ($type === 'inbound'): ?>
|
||||
<a href="print_inbound.php?id=<?= $mail['id'] ?>" target="_blank" class="btn btn-outline-secondary"><i class="fas fa-print me-1"></i> طباعة</a>
|
||||
<?php endif; ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
@ -510,4 +512,4 @@ if ($type == 'internal') {
|
||||
});
|
||||
</script>
|
||||
|
||||
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|
||||
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|
||||
Loading…
x
Reference in New Issue
Block a user