-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_account.php
More file actions
114 lines (102 loc) · 4.08 KB
/
Copy pathprocess_account.php
File metadata and controls
114 lines (102 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
define('INCLUDED_FROM_ANOTHER_SCRIPT', true);
require_once 'includes/config.php';
require_once 'includes/functions.php';
require_login();
$action = $_REQUEST['action'] ?? '';
// Allowed HMRC categories whitelist — prevents arbitrary values being saved
$allowed_hmrc_categories = [
'',
'costOfGoods',
'constructionCosts',
'staffCosts',
'travelCosts',
'premisesRunningCosts',
'maintenanceCosts',
'adminCosts',
'advertisingCosts',
'businessEntertainmentCosts',
'interestOnBankLoans',
'financeCharges',
'irrecoverableDebts',
'professionalFees',
'depreciation',
'otherExpenses',
];
switch ($action) {
case 'create':
$code = trim($_POST['code']);
$name = trim($_POST['name']);
$type = $_POST['type'];
$description = trim($_POST['description']);
$vat_rate = !empty($_POST['vat_rate']) ? (float) $_POST['vat_rate'] : 0;
$hmrc_category = $_POST['hmrc_category'] ?? '';
// Only save hmrc_category for expense accounts, and only if it's a valid value
if ($type !== 'expense' || !in_array($hmrc_category, $allowed_hmrc_categories)) {
$hmrc_category = null;
}
if ($hmrc_category === '') {
$hmrc_category = null;
}
try {
$stmt = $pdo->prepare("
INSERT INTO chart_of_accounts (code, name, type, description, vat_rate, hmrc_category)
VALUES (?, ?, ?, ?, ?, ?)
");
$stmt->execute([$code, $name, $type, $description, $vat_rate, $hmrc_category]);
$_SESSION['flash_message'] = ['type' => 'success', 'message' => "Account '$name' created successfully."];
} catch (PDOException $e) {
$_SESSION['flash_message'] = ['type' => 'danger', 'message' => 'Error: ' . $e->getMessage()];
}
header('Location: chart_of_accounts.php');
exit;
case 'update':
$id = (int) $_POST['id'];
$code = trim($_POST['code']);
$name = trim($_POST['name']);
$type = $_POST['type'];
$description = trim($_POST['description']);
$is_active = isset($_POST['is_active']) ? 1 : 0;
$vat_rate = !empty($_POST['vat_rate']) ? (float) $_POST['vat_rate'] : 0;
$hmrc_category = $_POST['hmrc_category'] ?? '';
// Only save hmrc_category for expense accounts, and only if it's a valid value
if ($type !== 'expense' || !in_array($hmrc_category, $allowed_hmrc_categories)) {
$hmrc_category = null;
}
if ($hmrc_category === '') {
$hmrc_category = null;
}
try {
$stmt = $pdo->prepare("
UPDATE chart_of_accounts
SET code = ?,
name = ?,
type = ?,
description = ?,
is_active = ?,
vat_rate = ?,
hmrc_category = ?
WHERE id = ?
");
$stmt->execute([$code, $name, $type, $description, $is_active, $vat_rate, $hmrc_category, $id]);
$_SESSION['flash_message'] = ['type' => 'success', 'message' => "Account updated successfully."];
} catch (PDOException $e) {
$_SESSION['flash_message'] = ['type' => 'danger', 'message' => 'Error: ' . $e->getMessage()];
}
header('Location: chart_of_accounts.php');
exit;
case 'delete':
$id = (int) $_GET['id'];
try {
$stmt = $pdo->prepare("DELETE FROM chart_of_accounts WHERE id = ? AND is_system = 0");
$stmt->execute([$id]);
$_SESSION['flash_message'] = ['type' => 'success', 'message' => 'Account deleted.'];
} catch (PDOException $e) {
$_SESSION['flash_message'] = ['type' => 'danger', 'message' => 'Error: ' . $e->getMessage()];
}
header('Location: chart_of_accounts.php');
exit;
default:
header('Location: chart_of_accounts.php');
exit;
}