getMessage()); } startSession(); requireLogin(); if (!hasRole(['admin', 'manager'])) { header("Location: /dashboard.php"); exit(); } $supplier_id = isset($_GET['supplier_id']) ? intval($_GET['supplier_id']) : 0; if (!$supplier_id) { header("Location: pending_payments.php"); exit(); } // Get supplier details try { $supplier = fetchSingle(" SELECT user_id, full_name, role, phone FROM users WHERE user_id = ? AND (role = 'supplier' OR role = 'manager') AND status = 'active' ", "i", [$supplier_id]); } catch (Exception $e) { $supplier = null; } if (!$supplier) { die("Supplier not found. Go back"); } // Calculate pending amount try { $pending_data = fetchSingle(" SELECT COALESCE(SUM(s.total_amount), 0) as total_supply, COALESCE(SUM(ps.amount), 0) as total_paid FROM users u LEFT JOIN supply s ON u.user_id = s.supplier_id LEFT JOIN payment_settlements ps ON u.user_id = ps.supplier_id WHERE u.user_id = ? GROUP BY u.user_id ", "i", [$supplier_id]); $total_supply = $pending_data ? floatval($pending_data['total_supply']) : 0; $total_paid = $pending_data ? floatval($pending_data['total_paid']) : 0; $pending_amount = $total_supply - $total_paid; } catch (Exception $e) { $total_supply = 0; $total_paid = 0; $pending_amount = 0; $error = "Error calculating pending amount: " . $e->getMessage(); } // Handle form submission - WITHOUT reference_number if ($_SERVER['REQUEST_METHOD'] === 'POST') { $payment_date = $_POST['payment_date']; $amount = floatval($_POST['amount']); $payment_mode = $_POST['payment_mode']; $notes = trim($_POST['notes']); $settled_by = $_SESSION['user_id']; // Validate amount if ($amount <= 0) { $error = "Payment amount must be greater than 0"; } elseif ($amount > $pending_amount) { $error = "Payment amount cannot exceed pending amount of ₹" . number_format($pending_amount, 2); } else { try { // Insert WITHOUT reference_number column $sql = "INSERT INTO payment_settlements (supplier_id, payment_date, amount, payment_mode, notes, settled_by) VALUES (?, ?, ?, ?, ?, ?)"; if (executeQuery($sql, "isdssi", [$supplier_id, $payment_date, $amount, $payment_mode, $notes, $settled_by])) { logActivity($_SESSION['user_id'], 'INSERT', 'payment_settlements', 'Settled payment to ' . $supplier['full_name']); header("Location: pending_payments.php?success=1"); exit(); } else { $error = "Failed to record payment. Please try again."; } } catch (Exception $e) { $error = "Database error: " . $e->getMessage(); } } } // Get recent payments - WITHOUT reference_number in SELECT try { $recent_payments = fetchAll(" SELECT ps.payment_date, ps.amount, ps.payment_mode, ps.notes, ps.created_at, u.full_name as settled_by_name FROM payment_settlements ps JOIN users u ON ps.settled_by = u.user_id WHERE ps.supplier_id = ? ORDER BY ps.payment_date DESC, ps.created_at DESC LIMIT 10 ", "i", [$supplier_id]); } catch (Exception $e) { $recent_payments = []; } $page_title = 'Settle Payment'; require_once __DIR__ . '/../includes/header.php'; ?>

Settle Payment

Supplier Information

Manager
✓ This supplier has no pending payments. All amounts have been settled!
← Back to Pending Payments

Record Payment Settlement

Maximum: ₹ Pay Full Amount
Cancel

Recent Payments to This Supplier

Payment Date Amount Payment Mode Notes Settled By Recorded On
format('d-m-Y') : $payment['payment_date']; ?> 50): ?>... - format('d-m-Y H:i') : $payment['created_at']; ?>
Total (Last 10):