update lpos

This commit is contained in:
Flatlogic Bot 2026-03-21 09:40:17 +00:00
parent bab61e3570
commit fd41c8937a
2 changed files with 94 additions and 13 deletions

View File

@ -55,13 +55,32 @@ try {
} }
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') { } elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
if ($action === 'get_lpos') { if ($action === 'get_lpos') {
$stmt = $pdo->query(" $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 20;
$offset = ($page - 1) * $limit;
// Count total
$countStmt = $pdo->query("SELECT COUNT(*) FROM pharmacy_lpos");
$total = $countStmt->fetchColumn();
$stmt = $pdo->prepare("
SELECT l.*, s.name_en as supplier_name SELECT l.*, s.name_en as supplier_name
FROM pharmacy_lpos l FROM pharmacy_lpos l
LEFT JOIN suppliers s ON l.supplier_id = s.id LEFT JOIN suppliers s ON l.supplier_id = s.id
ORDER BY l.created_at DESC ORDER BY l.created_at DESC
LIMIT :limit OFFSET :offset
"); ");
echo json_encode($stmt->fetchAll()); $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
echo json_encode([
'data' => $stmt->fetchAll(PDO::FETCH_ASSOC),
'total' => $total,
'page' => $page,
'limit' => $limit,
'pages' => ceil($total / $limit)
]);
} elseif ($action === 'get_lpo_details') { } elseif ($action === 'get_lpo_details') {
$id = $_GET['id'] ?? 0; $id = $_GET['id'] ?? 0;
@ -72,15 +91,15 @@ try {
WHERE i.lpo_id = ? WHERE i.lpo_id = ?
"); ");
$stmt->execute([$id]); $stmt->execute([$id]);
echo json_encode($stmt->fetchAll()); echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
} elseif ($action === 'get_suppliers') { } elseif ($action === 'get_suppliers') {
$stmt = $pdo->query("SELECT id, name_en, name_ar FROM suppliers ORDER BY name_en ASC"); $stmt = $pdo->query("SELECT id, name_en, name_ar FROM suppliers ORDER BY name_en ASC");
echo json_encode($stmt->fetchAll()); echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
} elseif ($action === 'get_drugs') { } elseif ($action === 'get_drugs') {
$stmt = $pdo->query("SELECT id, name_en, name_ar, sku, price FROM drugs ORDER BY name_en ASC"); $stmt = $pdo->query("SELECT id, name_en, name_ar, sku, price FROM drugs ORDER BY name_en ASC");
echo json_encode($stmt->fetchAll()); echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
} }
} }
@ -90,4 +109,4 @@ try {
} }
http_response_code(500); http_response_code(500);
echo json_encode(['error' => $e->getMessage()]); echo json_encode(['error' => $e->getMessage()]);
} }

View File

@ -24,6 +24,8 @@
</tbody> </tbody>
</table> </table>
</div> </div>
<!-- Pagination -->
<nav id="paginationContainer" class="mt-3" aria-label="Page navigation"></nav>
</div> </div>
</div> </div>
@ -161,9 +163,10 @@
<script> <script>
let allDrugs = []; let allDrugs = [];
let allSuppliers = []; let allSuppliers = [];
let currentPage = 1;
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
loadLPOs(); loadLPOs(1);
fetchSuppliers(); fetchSuppliers();
fetchDrugs(); fetchDrugs();
@ -184,19 +187,26 @@ document.addEventListener('DOMContentLoaded', function() {
}); });
}); });
function loadLPOs() { function loadLPOs(page = 1) {
fetch('api/pharmacy_lpo.php?action=get_lpos') currentPage = page;
fetch('api/pharmacy_lpo.php?action=get_lpos&page=' + page)
.then(response => response.json()) .then(response => response.json())
.then(data => { .then(data => {
const tbody = document.getElementById('lpoTableBody'); const tbody = document.getElementById('lpoTableBody');
const paginationContainer = document.getElementById('paginationContainer');
tbody.innerHTML = ''; tbody.innerHTML = '';
if (data.length === 0) { // Handle new response structure (paginated) vs old (array)
const lpos = data.data || (Array.isArray(data) ? data : []);
const totalPages = data.pages || 1;
if (lpos.length === 0) {
tbody.innerHTML = '<tr><td colspan="6" class="text-center py-4 text-muted"><?php echo __('no_data_found'); ?></td></tr>'; tbody.innerHTML = '<tr><td colspan="6" class="text-center py-4 text-muted"><?php echo __('no_data_found'); ?></td></tr>';
paginationContainer.innerHTML = '';
return; return;
} }
data.forEach(lpo => { lpos.forEach(lpo => {
const tr = document.createElement('tr'); const tr = document.createElement('tr');
let statusBadge = ''; let statusBadge = '';
switch(lpo.status) { switch(lpo.status) {
@ -223,8 +233,60 @@ function loadLPOs() {
`; `;
tbody.appendChild(tr); tbody.appendChild(tr);
}); });
renderPagination(currentPage, totalPages);
}) })
.catch(err => console.error(err)); .catch(err => {
console.error(err);
document.getElementById('lpoTableBody').innerHTML = '<tr><td colspan="6" class="text-center text-danger">Error loading data</td></tr>';
});
}
function renderPagination(current, total) {
const container = document.getElementById('paginationContainer');
if (total <= 1) {
container.innerHTML = '';
return;
}
let html = '<ul class="pagination justify-content-center">';
// Previous
html += `<li class="page-item ${current <= 1 ? 'disabled' : ''}">
<a class="page-link" href="#" onclick="event.preventDefault(); loadLPOs(${current - 1})"><?php echo __('previous'); ?></a>
</li>`;
// Page Numbers (Show max 5 pages for simplicity)
let start = Math.max(1, current - 2);
let end = Math.min(total, start + 4);
if (end - start < 4) {
start = Math.max(1, end - 4);
}
if (start > 1) {
html += `<li class="page-item"><a class="page-link" href="#" onclick="event.preventDefault(); loadLPOs(1)">1</a></li>`;
if (start > 2) html += `<li class="page-item disabled"><span class="page-link">...</span></li>`;
}
for (let i = start; i <= end; i++) {
html += `<li class="page-item ${current === i ? 'active' : ''}">
<a class="page-link" href="#" onclick="event.preventDefault(); loadLPOs(${i})">${i}</a>
</li>`;
}
if (end < total) {
if (end < total - 1) html += `<li class="page-item disabled"><span class="page-link">...</span></li>`;
html += `<li class="page-item"><a class="page-link" href="#" onclick="event.preventDefault(); loadLPOs(${total})">${total}</a></li>`;
}
// Next
html += `<li class="page-item ${current >= total ? 'disabled' : ''}">
<a class="page-link" href="#" onclick="event.preventDefault(); loadLPOs(${current + 1})"><?php echo __('next'); ?></a>
</li>`;
html += '</ul>';
container.innerHTML = html;
} }
function fetchSuppliers() { function fetchSuppliers() {
@ -428,4 +490,4 @@ function updateStatus(id, newStatus) {
} }
}); });
} }
</script> </script>