|
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
|
|
|
|
|
20
|
|
|
class IncomeExpense extends AccountingData |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
View Code Duplication |
public function add_income(int $categoryid, string $income_title, int $amount, string $notes = "", $transaction_id = "0"): Income |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
$income = $this->newIncome($categoryid, $income_title, $amount, $notes, $transaction_id); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
if ($transaction_id == 0) |
|
30
|
|
|
$transaction_id = $this->set_transaction_id($income); |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
$ledger = $this->add_transaction_on_ledger($income, "INC", $transaction_id); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
return $income; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
View Code Duplication |
public function add_expense(int $categoryid, string $income_title, int $amount, string $notes = "", $transaction_id = "0"): Expense |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
$expense = $this->newExpense($categoryid, $income_title, $amount, $notes, $transaction_id); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
if ($transaction_id == 0) |
|
45
|
|
|
$transaction_id = $this->set_transaction_id($expense); |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
$ledger = $this->add_transaction_on_ledger($expense, "EXP", $transaction_id); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
return $expense; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function set_transaction_id($income) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$prefix = config('income-expense.transaction_id_prefix'); |
|
56
|
|
|
$length = config('income-expense.transaction_id_length'); |
|
57
|
|
|
|
|
58
|
|
|
$keyspace = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
59
|
|
|
|
|
60
|
|
|
$str = ''; |
|
61
|
|
|
|
|
62
|
|
|
$max = mb_strlen($keyspace, '8bit') - 1; |
|
63
|
|
|
|
|
64
|
|
|
for ($i = 0; $i < $length; ++$i) { |
|
65
|
|
|
$str .= $keyspace[random_int(0, $max)]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $prefix . $str; |
|
69
|
|
|
} |
|
70
|
|
|
public function newIncome(int $categoryid, string $income_title, int $amount, string $notes = ""): Income |
|
71
|
|
|
{ |
|
72
|
|
|
return Income::create([ |
|
73
|
|
|
"incomecategory" => $categoryid, |
|
74
|
|
|
"income_title" => $income_title, |
|
75
|
|
|
"amount" => $amount, |
|
76
|
|
|
"notes" => $notes, |
|
77
|
|
|
"date" => date('Y-m-d') |
|
78
|
|
|
]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function add_transaction_on_ledger($transactionObj, $type, $transaction_id): Ledger |
|
82
|
|
|
{ |
|
83
|
|
|
|
|
84
|
|
|
$ledger = new Ledger; |
|
85
|
|
|
$ledger->transaction_id = $transaction_id; |
|
86
|
|
|
$ledger->transaction_type = $type; |
|
87
|
|
|
$ledger->transaction_type_category = $transactionObj->incomecategory; |
|
88
|
|
|
$ledger->amount = $transactionObj->amount; |
|
89
|
|
|
$ledger->save(); |
|
90
|
|
|
return $ledger; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function newExpense(int $categoryid, string $expense_title, int $amount, string $notes = ""): Expense |
|
94
|
|
|
{ |
|
95
|
|
|
return Expense::create([ |
|
96
|
|
|
"expense_category" => $categoryid, |
|
97
|
|
|
"expense_title" => $expense_title, |
|
98
|
|
|
"amount" => $amount, |
|
99
|
|
|
"notes" => $notes, |
|
100
|
|
|
"date" => date('Y-m-d') |
|
101
|
|
|
]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
public function addExpenseCategory($categoryname, $description): ExpenseCategory |
|
106
|
|
|
{ |
|
107
|
|
|
return ExpenseCategory::create([ |
|
108
|
|
|
"category" => $categoryname, |
|
109
|
|
|
"description" => $description, |
|
110
|
|
|
"date" => date('Y-m-d') |
|
111
|
|
|
]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
public function addIncomeCategory($categoryname, $description): IncomeCategory |
|
116
|
|
|
{ |
|
117
|
|
|
|
|
118
|
|
|
return IncomeCategory::create([ |
|
119
|
|
|
"category" => $categoryname, |
|
120
|
|
|
"description" => $description, |
|
121
|
|
|
"date" => date('Y-m-d') |
|
122
|
|
|
]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function deleteExpenseCategory($expensecategoryid) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
public function deleteIncomeCategory($incomecategoryid) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
public function updateExpenseCategory($expensecategoryid) |
|
|
|
|
|
|
136
|
|
|
{ |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
public function updateIncomeCategory($incomecategoryid) |
|
|
|
|
|
|
141
|
|
|
{ |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.