Newer
Older
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
<?php
namespace QUI\ERP\BankAccounts;
use QUI;
/**
* Class Handler.
*
* Main handler for bank accounts.
*/
class Handler
{
/**
* Add bank account to list.
*
* @param array $data
* @return array - New bank account data
*
* @throws QUI\Exception
*/
public static function addBankAccount(array $data): array
{
$fields = [
'title' => true,
'name' => true,
'iban' => true,
'bic' => true,
'accountHolder' => true,
'creditorId' => false,
'default' => false,
'financialAccountNo' => false
];
$bankAccount = [];
foreach ($fields as $field => $isRequired) {
if ($isRequired && empty($data[$field])) {
throw new QUI\Exception('Cannot add bank account. Required field "'.$field.'" is empty.');
}
$bankAccount[$field] = !empty($data[$field]) ? $data[$field] : '';
}
$list = self::getList();
do {
$id = \mt_rand(10000, 99999);
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
} while (!empty($list[$id]));
$Conf = QUI::getPackage('quiqqer/erp')->getConfig();
$bankAccount['id'] = $id;
$list[$id] = $bankAccount;
$Conf->setValue('bankAccounts', 'accounts', \json_encode($list));
$Conf->save();
return $bankAccount;
}
/**
* Get data of the bank account that is set as the company default.
*
* @return array|false
*/
public static function getCompanyBankAccount()
{
try {
$bankAccounts = self::getList();
$bankAccountId = QUI::getPackage('quiqqer/erp')->getConfig()->get('company', 'bankAccountId');
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return false;
}
if (!empty($bankAccounts[$bankAccountId])) {
return $bankAccounts[$bankAccountId];
}
return self::getDefaultBankAccount();
}
/**
* Get the bank account data of the default bank account.
*
* @return array|false
*/
public static function getDefaultBankAccount()
{
try {
$bankAccounts = self::getList();
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return false;
}
foreach ($bankAccounts as $bankAccount) {
if (!empty($bankAccount['default'])) {
return $bankAccount;
}
}
return false;
}
/**
* Get the bank account data by id.
*
* @return array|false
*/
public static function getBankAccountById(int $id)
{
try {
$bankAccounts = self::getList();
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return false;
}
foreach ($bankAccounts as $bankAccount) {
if ((int)$bankAccount['id'] === $id) {
return $bankAccount;
}
}
return false;
}
/**
* Get list of bank accounts.
*
* @return array
*/
public static function getList(): array
{
try {
$config = self::getConfig();
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return [];
}
$bankAccounts = $config['accounts'];
if (empty($bankAccounts)) {
return [];
}
return \json_decode($bankAccounts, true);
}
/**
* @return array
* @throws QUI\Exception
*/
protected static function getConfig(): array
{
$Conf = QUI::getPackage('quiqqer/erp')->getConfig();
return $Conf->getSection('bankAccounts');
}
}