1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Author: Emmanuel Paul Mnzava |
6
|
|
|
* Twitter: @epmnzava |
7
|
|
|
* Github:https://github.com/dbrax/income-expense |
8
|
|
|
* Email: [email protected] |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Epmnzava\IncomeExpense; |
13
|
|
|
|
14
|
|
|
use Epmnzava\IncomeExpense\Models\Expense; |
15
|
|
|
use Epmnzava\IncomeExpense\Models\ExpenseCategory; |
16
|
|
|
use Epmnzava\IncomeExpense\Models\Income; |
17
|
|
|
use Epmnzava\IncomeExpense\Models\IncomeCategory; |
18
|
|
|
use Epmnzava\IncomeExpense\Models\Ledger; |
19
|
|
|
use Illuminate\Support\Str; |
20
|
|
|
|
21
|
|
|
class IncomeExpense extends AccountingData |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param int $categoryid |
27
|
|
|
* @param string $income_title |
28
|
|
|
* @param int $amount |
29
|
|
|
* @param string $notes |
30
|
|
|
* @param string $transaction_id |
31
|
|
|
* @return Income |
32
|
|
|
* |
33
|
|
|
* function to add an income and ledger at the same time |
34
|
|
|
*/ |
35
|
|
|
public function add_income(int $categoryid, string $income_title, int $amount, string $notes = "", $transaction_id = "0"): Income |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
$income = $this->newIncome($categoryid, $income_title, $amount, $notes, $transaction_id); |
|
|
|
|
39
|
|
|
|
40
|
|
|
if ($transaction_id == 0) |
41
|
|
|
$transaction_id = $this->set_transaction_id($income); |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
$ledger = $this->add_transaction_on_ledger($income, "INC", $transaction_id); |
|
|
|
|
45
|
|
|
|
46
|
|
|
return $income; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param int $categoryid |
51
|
|
|
* @param string $income_title |
52
|
|
|
* @param int $amount |
|
|
|
|
53
|
|
|
* @param string $notes |
54
|
|
|
* @param string $transaction_id |
55
|
|
|
* @return Expense |
56
|
|
|
* |
57
|
|
|
* function to add expense and to a ledger at the sametime |
58
|
|
|
*/ |
59
|
|
|
|
60
|
|
|
public function add_expense(int $categoryid, string $expense_title, int $amount, string $notes = "", $transaction_id = "0"): Expense |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
$expense = $this->newExpense($categoryid, $expense_title, $amount, $notes, $transaction_id); |
|
|
|
|
64
|
|
|
|
65
|
|
|
if ($transaction_id == 0) |
66
|
|
|
$transaction_id = $this->set_transaction_id($expense); |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
$ledger = $this->add_transaction_on_ledger($expense, "EXP", $transaction_id); |
|
|
|
|
70
|
|
|
|
71
|
|
|
return $expense; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $income |
77
|
|
|
* @return string |
78
|
|
|
* @throws \Exception |
79
|
|
|
* |
80
|
|
|
* function that sets a transaction id |
81
|
|
|
*/ |
82
|
|
|
|
83
|
|
|
public function set_transaction_id($income) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$prefix = config('income-expense.transaction_id_prefix'); |
86
|
|
|
$length = config('income-expense.transaction_id_length'); |
87
|
|
|
|
88
|
|
|
$keyspace = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
89
|
|
|
|
90
|
|
|
$str = ''; |
91
|
|
|
|
92
|
|
|
$max = mb_strlen($keyspace, '8bit') - 1; |
93
|
|
|
|
94
|
|
|
for ($i = 0; $i < $length; ++$i) { |
95
|
|
|
$str .= $keyspace[random_int(0, $max)]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $prefix . $str; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param int $categoryid |
103
|
|
|
* @param string $income_title |
104
|
|
|
* @param int $amount |
105
|
|
|
* @param string $notes |
106
|
|
|
* @return Income |
107
|
|
|
* |
108
|
|
|
* Unit function that adds income |
109
|
|
|
*/ |
110
|
|
|
private function newIncome(int $categoryid, string $income_title, int $amount, string $notes = ""): Income |
111
|
|
|
{ |
112
|
|
|
return Income::create([ |
113
|
|
|
"incomecategory" => $categoryid, |
114
|
|
|
"income_title" => $income_title, |
115
|
|
|
"amount" => $amount, |
116
|
|
|
"notes" => $notes, |
117
|
|
|
"date" => date('Y-m-d') |
118
|
|
|
]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $transactionObj |
123
|
|
|
* @param $type |
124
|
|
|
* @param $transaction_id |
125
|
|
|
* @return Ledger |
126
|
|
|
* |
127
|
|
|
* unit function that adds a ledger transaction |
128
|
|
|
*/ |
129
|
|
|
public function add_transaction_on_ledger($transactionObj, $type, $transaction_id): Ledger |
130
|
|
|
{ |
131
|
|
|
|
132
|
|
|
$ledger = new Ledger; |
133
|
|
|
$ledger->transaction_id = $transaction_id; |
134
|
|
|
$ledger->transaction_type = $type; |
135
|
|
|
$ledger->transaction_type_category = $transactionObj->incomecategory; |
136
|
|
|
$ledger->amount = $transactionObj->amount; |
137
|
|
|
$ledger->save(); |
138
|
|
|
return $ledger; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param int $categoryid |
143
|
|
|
* @param string $expense_title |
144
|
|
|
* @param int $amount |
145
|
|
|
* @param string $notes |
146
|
|
|
* @return Expense |
147
|
|
|
* |
148
|
|
|
* A unit function that adds new expense |
149
|
|
|
*/ |
150
|
|
|
|
151
|
|
|
private function newExpense(int $categoryid, string $expense_title, int $amount, string $notes = ""): Expense |
152
|
|
|
{ |
153
|
|
|
return Expense::create([ |
154
|
|
|
"expense_category" => $categoryid, |
155
|
|
|
"expense_title" => $expense_title, |
156
|
|
|
"amount" => $amount, |
157
|
|
|
"notes" => $notes, |
158
|
|
|
"date" => date('Y-m-d') |
159
|
|
|
]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param $categoryname |
165
|
|
|
* @param $description |
166
|
|
|
* @return ExpenseCategory |
167
|
|
|
* |
168
|
|
|
* A unit function that adds expense category |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function addExpenseCategory($categoryname, $description): ExpenseCategory |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
return ExpenseCategory::create([ |
173
|
|
|
"category" => $categoryname, |
174
|
|
|
"description" => $description, |
175
|
|
|
"slug"=>Str::slug($categoryname), |
176
|
|
|
"date" => date('Y-m-d') |
177
|
|
|
]); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param $categoryname |
182
|
|
|
* @param $description |
183
|
|
|
* @return IncomeCategory |
184
|
|
|
* |
185
|
|
|
* A unit function that adds income category |
186
|
|
|
*/ |
187
|
|
|
|
188
|
|
|
|
189
|
|
View Code Duplication |
public function addIncomeCategory($categoryname, $description): IncomeCategory |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
|
192
|
|
|
return IncomeCategory::create([ |
193
|
|
|
"category" => $categoryname, |
194
|
|
|
"description" => $description, |
195
|
|
|
"slug"=>Str::slug('($categoryname', '-'), |
196
|
|
|
"date" => date('Y-m-d') |
197
|
|
|
]); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
public function getExpenseCategoryById($expensecategoryid) |
202
|
|
|
{ |
203
|
|
|
return ExpenseCategory::find($expensecategoryid); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
|
208
|
|
|
public function getIncomeCategoryById($incomecategoryid) |
209
|
|
|
{ |
210
|
|
|
return IncomeCategory::find($incomecategoryid); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function deleteExpenseCategory($expensecategoryid) |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
public function deleteIncomeCategory($incomecategoryid) |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
public function updateExpenseCategory($expensecategoryid) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
public function updateIncomeCategory($incomecategoryid) |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.