304 lines
14 KiB
PHP
304 lines
14 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
require_once __DIR__ . '/includes/header.php';
|
|
|
|
$pdo = db();
|
|
$user_id = $_SESSION['user_id'];
|
|
$errors = [];
|
|
$success_message = '';
|
|
|
|
// First, get the company_id for the logged-in user
|
|
$stmt = $pdo->prepare("SELECT id FROM companies WHERE user_id = ?");
|
|
$stmt->execute([$user_id]);
|
|
$company = $stmt->fetch();
|
|
$company_id = $company ? $company['id'] : null;
|
|
|
|
// Handle POST requests
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $company_id) {
|
|
$action = $_POST['action'] ?? '';
|
|
|
|
// ADD ACCOUNT
|
|
if ($action === 'add_account') {
|
|
$account_code = trim($_POST['account_code'] ?? '');
|
|
$account_name = trim($_POST['account_name'] ?? '');
|
|
$account_type = trim($_POST['account_type'] ?? '');
|
|
$description = trim($_POST['description'] ?? '');
|
|
|
|
if (empty($account_code) || empty($account_name) || empty($account_type)) {
|
|
$errors[] = 'Account Code, Name, and Type are required.';
|
|
}
|
|
if (empty($errors)) {
|
|
$stmt = $pdo->prepare("INSERT INTO chart_of_accounts (company_id, account_code, account_name, account_type, description) VALUES (?, ?, ?, ?, ?)");
|
|
if ($stmt->execute([$company_id, $account_code, $account_name, $account_type, $description])) {
|
|
$success_message = 'Account added successfully!';
|
|
} else {
|
|
$errors[] = 'Failed to add account. The account code may already exist.';
|
|
}
|
|
}
|
|
}
|
|
|
|
// EDIT ACCOUNT
|
|
if ($action === 'edit_account') {
|
|
$account_id = $_POST['account_id'] ?? null;
|
|
$account_code = trim($_POST['account_code'] ?? '');
|
|
$account_name = trim($_POST['account_name'] ?? '');
|
|
$account_type = trim($_POST['account_type'] ?? '');
|
|
$description = trim($_POST['description'] ?? '');
|
|
|
|
if (empty($account_id) || empty($account_code) || empty($account_name) || empty($account_type)) {
|
|
$errors[] = 'All fields are required for editing.';
|
|
}
|
|
if (empty($errors)) {
|
|
$stmt = $pdo->prepare("UPDATE chart_of_accounts SET account_code = ?, account_name = ?, account_type = ?, description = ? WHERE id = ? AND company_id = ?");
|
|
if ($stmt->execute([$account_code, $account_name, $account_type, $description, $account_id, $company_id])) {
|
|
$success_message = 'Account updated successfully!';
|
|
} else {
|
|
$errors[] = 'Failed to update account. The account code may already exist for another account.';
|
|
}
|
|
}
|
|
}
|
|
|
|
// DELETE ACCOUNT
|
|
if ($action === 'delete_account') {
|
|
$account_id = $_POST['account_id'] ?? null;
|
|
if (empty($account_id)) {
|
|
$errors[] = 'Invalid account for deletion.';
|
|
}
|
|
if (empty($errors)) {
|
|
$stmt = $pdo->prepare("DELETE FROM chart_of_accounts WHERE id = ? AND company_id = ?");
|
|
if ($stmt->execute([$account_id, $company_id])) {
|
|
$success_message = 'Account deleted successfully!';
|
|
} else {
|
|
$errors[] = 'Failed to delete account.';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fetch all accounts for the company
|
|
$accounts = [];
|
|
if ($company_id) {
|
|
$stmt = $pdo->prepare("SELECT * FROM chart_of_accounts WHERE company_id = ? ORDER BY account_code");
|
|
$stmt->execute([$company_id]);
|
|
$accounts = $stmt->fetchAll();
|
|
}
|
|
|
|
require_once __DIR__ . '/includes/sidebar.php';
|
|
?>
|
|
|
|
<div class="d-flex justify-content-between align-items-center">
|
|
<h1 class="h2">Chart of Accounts</h1>
|
|
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addAccountModal">Add New Account</button>
|
|
</div>
|
|
|
|
<p>This is the list of all financial accounts for your company.</p>
|
|
|
|
<?php if (!$company_id): ?>
|
|
<div class="alert alert-warning">Please <a href="/company_setup.php">set up your company</a> before managing accounts.</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p class="mb-0"><?php echo htmlspecialchars($error); ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (!empty($success_message)): ?>
|
|
<div class="alert alert-success">
|
|
<p class="mb-0"><?php echo htmlspecialchars($success_message); ?></p>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card mt-4">
|
|
<div class="card-body">
|
|
<table class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th>Code</th>
|
|
<th>Name</th>
|
|
<th>Type</th>
|
|
<th>Description</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (!empty($accounts)): ?>
|
|
<?php foreach ($accounts as $account): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($account['account_code']); ?></td>
|
|
<td><?php echo htmlspecialchars($account['account_name']); ?></td>
|
|
<td><?php echo htmlspecialchars($account['account_type']); ?></td>
|
|
<td><?php echo htmlspecialchars($account['description']); ?></td>
|
|
<td>
|
|
<button class="btn btn-sm btn-outline-primary edit-btn"
|
|
data-bs-toggle="modal"
|
|
data-bs-target="#editAccountModal"
|
|
data-id="<?php echo $account['id']; ?>"
|
|
data-code="<?php echo htmlspecialchars($account['account_code']); ?>"
|
|
data-name="<?php echo htmlspecialchars($account['account_name']); ?>"
|
|
data-type="<?php echo htmlspecialchars($account['account_type']); ?>"
|
|
data-description="<?php echo htmlspecialchars($account['description']); ?>">
|
|
Edit
|
|
</button>
|
|
<button class="btn btn-sm btn-outline-danger delete-btn"
|
|
data-bs-toggle="modal"
|
|
data-bs-target="#deleteAccountModal"
|
|
data-id="<?php echo $account['id']; ?>">
|
|
Delete
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php else: ?>
|
|
<tr>
|
|
<td colspan="5" class="text-center">No accounts found. Please add one.</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Add Account Modal -->
|
|
<div class="modal fade" id="addAccountModal" tabindex="-1" aria-labelledby="addAccountModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="addAccountModalLabel">Add New Account</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<form action="/chart_of_accounts.php" method="POST">
|
|
<input type="hidden" name="action" value="add_account">
|
|
<div class="mb-3">
|
|
<label for="accountCode" class="form-label">Account Code</label>
|
|
<input type="text" class="form-control" id="accountCode" name="account_code" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="accountName" class="form-label">Account Name</label>
|
|
<input type="text" class="form-control" id="accountName" name="account_name" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="accountType" class="form-label">Account Type</label>
|
|
<select class="form-select" id="accountType" name="account_type" required>
|
|
<option value="" disabled selected>Select a type</option>
|
|
<option value="Asset">Asset</option>
|
|
<option value="Liability">Liability</option>
|
|
<option value="Equity">Equity</option>
|
|
<option value="Revenue">Revenue</option>
|
|
<option value="Expense">Expense</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="accountDescription" class="form-label">Description</label>
|
|
<textarea class="form-control" id="accountDescription" name="description" rows="3"></textarea>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Save Account</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Edit Account Modal -->
|
|
<div class="modal fade" id="editAccountModal" tabindex="-1" aria-labelledby="editAccountModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="editAccountModalLabel">Edit Account</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<form action="/chart_of_accounts.php" method="POST">
|
|
<input type="hidden" name="action" value="edit_account">
|
|
<input type="hidden" name="account_id" id="editAccountId">
|
|
<div class="mb-3">
|
|
<label for="editAccountCode" class="form-label">Account Code</label>
|
|
<input type="text" class="form-control" id="editAccountCode" name="account_code" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="editAccountName" class="form-label">Account Name</label>
|
|
<input type="text" class="form-control" id="editAccountName" name="account_name" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="editAccountType" class="form-label">Account Type</label>
|
|
<select class="form-select" id="editAccountType" name="account_type" required>
|
|
<option value="Asset">Asset</option>
|
|
<option value="Liability">Liability</option>
|
|
<option value="Equity">Equity</option>
|
|
<option value="Revenue">Revenue</option>
|
|
<option value="Expense">Expense</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="editAccountDescription" class="form-label">Description</label>
|
|
<textarea class="form-control" id="editAccountDescription" name="description" rows="3"></textarea>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Save Changes</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Delete Account Modal -->
|
|
<div class="modal fade" id="deleteAccountModal" tabindex="-1" aria-labelledby="deleteAccountModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="deleteAccountModalLabel">Delete Account</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<p>Are you sure you want to delete this account? This action cannot be undone.</p>
|
|
<form action="/chart_of_accounts.php" method="POST">
|
|
<input type="hidden" name="action" value="delete_account">
|
|
<input type="hidden" name="account_id" id="deleteAccountId">
|
|
<div class="d-flex justify-content-end">
|
|
<button type="button" class="btn btn-secondary me-2" data-bs-dismiss="modal">Cancel</button>
|
|
<button type="submit" class="btn btn-danger">Delete Account</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
// Handle Edit Modal
|
|
var editModal = document.getElementById('editAccountModal');
|
|
editModal.addEventListener('show.bs.modal', function (event) {
|
|
var button = event.relatedTarget;
|
|
var id = button.getAttribute('data-id');
|
|
var code = button.getAttribute('data-code');
|
|
var name = button.getAttribute('data-name');
|
|
var type = button.getAttribute('data-type');
|
|
var description = button.getAttribute('data-description');
|
|
|
|
var modalTitle = editModal.querySelector('.modal-title');
|
|
var modalBody = editModal.querySelector('.modal-body');
|
|
|
|
modalTitle.textContent = 'Edit Account: ' + name;
|
|
modalBody.querySelector('#editAccountId').value = id;
|
|
modalBody.querySelector('#editAccountCode').value = code;
|
|
modalBody.querySelector('#editAccountName').value = name;
|
|
modalBody.querySelector('#editAccountType').value = type;
|
|
modalBody.querySelector('#editAccountDescription').value = description;
|
|
});
|
|
|
|
// Handle Delete Modal
|
|
var deleteModal = document.getElementById('deleteAccountModal');
|
|
deleteModal.addEventListener('show.bs.modal', function (event) {
|
|
var button = event.relatedTarget;
|
|
var id = button.getAttribute('data-id');
|
|
var modalBody = deleteModal.querySelector('.modal-body');
|
|
modalBody.querySelector('#deleteAccountId').value = id;
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|