-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAccount.php
More file actions
116 lines (92 loc) · 4.46 KB
/
Account.php
File metadata and controls
116 lines (92 loc) · 4.46 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
115
116
<?php
declare(strict_types=1);
require_once 'Database.php';
require_once 'Notification.php';
require_once 'Transaction.php';
require_once 'config.php';
class Account {
private Notification $notification;
private Transaction $transaction;
public function __construct(private Database $db) {
$this->notification = new Notification($db);
$this->transaction = new Transaction($db);
}
public function createAccount(int $userId, string $userName): array {
$accountNumber = $this->generateAccountNumber();
$account = [
'userId' => $userId,
'accountNumber' => $accountNumber,
'balance' => 0.00,
'status' => 'active',
'createdAt' => date('Y-m-d H:i:s')
];
$newAccount = $this->db->insert('accounts', $account);
$this->notification->create($userId, "Welcome! Your account $accountNumber has been created.");
return $newAccount;
}
public function getAccount(int $userId): ?array {
return $this->db->find('accounts', 'userId', $userId);
}
public function getAccountByNumber(string $accountNumber): ?array {
return $this->db->find('accounts', 'accountNumber', $accountNumber);
}
public function getAllAccounts(): array {
return $this->db->findAll('accounts');
}
public function requestDeposit(int $userId, float $amount): array {
$request = [
'userId' => $userId,
'type' => TransactionType::DEPOSIT->value,
'amount' => $amount,
'status' => RequestStatus::PENDING->value,
'createdAt' => date('Y-m-d H:i:s')
];
$this->db->insert('pendingRequests', $request);
$this->notification->create($userId, "Deposit request of $$amount submitted. Awaiting admin approval.");
return ['success' => true, 'message' => 'Deposit request submitted'];
}
public function requestWithdrawal(int $userId, float $amount): array {
$account = $this->getAccount($userId);
if ($account['balance'] < $amount) {
return ['success' => false, 'message' => 'Insufficient funds'];
}
$request = [
'userId' => $userId,
'type' => TransactionType::WITHDRAWAL->value,
'amount' => $amount,
'status' => RequestStatus::PENDING->value,
'createdAt' => date('Y-m-d H:i:s')
];
$this->db->insert('pendingRequests', $request);
$this->notification->create($userId, "Withdrawal request of $$amount submitted. Awaiting admin approval.");
return ['success' => true, 'message' => 'Withdrawal request submitted'];
}
public function transfer(int $fromUserId, string $toAccountNumber, float $amount): array {
$fromAccount = $this->getAccount($fromUserId);
$toAccount = $this->getAccountByNumber($toAccountNumber);
if (!$toAccount) {
return ['success' => false, 'message' => 'Recipient account not found'];
}
if ($fromAccount['accountNumber'] === $toAccountNumber) {
return ['success' => false, 'message' => 'Cannot transfer to same account'];
}
if ($fromAccount['balance'] < $amount) {
return ['success' => false, 'message' => 'Insufficient funds'];
}
// Deduct from sender
$newFromBalance = $fromAccount['balance'] - $amount;
$this->db->update('accounts', $fromAccount['id'], ['balance' => $newFromBalance]);
$this->transaction->add($fromUserId, TransactionType::TRANSFER, $amount, "Transfer to $toAccountNumber");
$this->notification->create($fromUserId, "Sent $$amount to $toAccountNumber. New balance: $$newFromBalance");
// Add to recipient
$newToBalance = $toAccount['balance'] + $amount;
$this->db->update('accounts', $toAccount['id'], ['balance' => $newToBalance]);
$this->transaction->add($toAccount['userId'], TransactionType::RECEIVED, $amount, "Received from {$fromAccount['accountNumber']}");
$this->notification->create($toAccount['userId'], "Received $$amount from {$fromAccount['accountNumber']}");
return ['success' => true, 'balance' => $newFromBalance];
}
private function generateAccountNumber(): string {
return '30' . str_pad((string)rand(0, 99999999), 8, '0', STR_PAD_LEFT);
}
}
?>