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 Carbon\Carbon; |
19
|
|
|
|
20
|
|
|
class AccountingData |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
public function _construct(){ |
26
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get sum of total income |
31
|
|
|
*/ |
32
|
|
|
public function total_sum_Income() : int |
33
|
|
|
{ |
34
|
|
|
return Income::sum('amount'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param int $category |
40
|
|
|
* @return int |
41
|
|
|
* function gets sum of an income by a given category .. |
42
|
|
|
*/ |
43
|
|
|
public function sumOfIncomeByCategory(int $category) : int |
44
|
|
|
{ |
45
|
|
|
return Income::where('incomecategory',$category)->sum('amount'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param int $category |
51
|
|
|
* @return int |
52
|
|
|
* function gets sum of an expense by a given category .. |
53
|
|
|
*/ |
54
|
|
|
public function sumOfExpenseByCategory(int $category) : int |
55
|
|
|
{ |
56
|
|
|
return Expense::where('expense_category',$category)->sum('amount'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return int |
61
|
|
|
* |
62
|
|
|
* function gets the total income for the current month |
63
|
|
|
*/ |
64
|
|
|
public function total_sum_of_income_this_month() : int |
65
|
|
|
{ |
66
|
|
|
return Income::whereMonth('date', date('m'))->whereYear('date', date('Y'))->sum('amount'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return int |
71
|
|
|
* function that gets the total_sum_of_all_expense |
72
|
|
|
*/ |
73
|
|
|
public function total_sum_Expense() :int |
74
|
|
|
{ |
75
|
|
|
return Expense::sum('amount'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return int |
80
|
|
|
* function that gets total expense for the current month |
81
|
|
|
*/ |
82
|
|
|
public function totalExpenseThisMonth() : int |
83
|
|
|
{ |
84
|
|
|
return Expense::whereMonth('date', date('m'))->whereYear('date', date('Y'))->sum('amount'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return int |
90
|
|
|
* function that gets totalincome of the current year |
91
|
|
|
*/ |
92
|
|
|
public function totalIncomeThisYear() : int |
93
|
|
|
{ |
94
|
|
|
return Income::whereYear('date', date('Y'))->sum('amount'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return int |
99
|
|
|
* |
100
|
|
|
*sum_of_total_income_last_year |
101
|
|
|
*/ |
102
|
|
|
public function sum_of_total_income_last_year() : int |
103
|
|
|
{ |
104
|
|
|
$date = new Carbon(); |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
return Income::whereYear('date',$date->subYear()->format('Y'))->sum('amount'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return int |
112
|
|
|
* |
113
|
|
|
* sum_of_total_income_last_month |
114
|
|
|
*/ |
115
|
|
|
|
116
|
|
|
public function sum_of_total_income_last_month() : int |
117
|
|
|
{ |
118
|
|
|
$date = new Carbon(); |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
return Income::whereYear('date',$date->subMonth()->format('m'))->sum('amount'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return int |
127
|
|
|
* |
128
|
|
|
* function that |
129
|
|
|
*/ |
130
|
|
|
public function sum_of_expense_this_year() : int |
131
|
|
|
{ |
132
|
|
|
return Expense::whereYear('date', date('Y'))->sum('amount'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return ExpenseCategory[]|\Illuminate\Database\Eloquent\Collection |
137
|
|
|
* |
138
|
|
|
* Function to return all Expense Categories |
139
|
|
|
*/ |
140
|
|
|
public function getExpenseCategories() |
141
|
|
|
{ |
142
|
|
|
return ExpenseCategory::all(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return IncomeCategory[]|\Illuminate\Database\Eloquent\Collection |
148
|
|
|
* Function to return all IncomeCategories |
149
|
|
|
*/ |
150
|
|
|
public function getIncomeCategories() |
151
|
|
|
{ |
152
|
|
|
return IncomeCategory::all(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $incomeid |
158
|
|
|
* @return Income |
159
|
|
|
* |
160
|
|
|
* Function to return specific income instance per income if passed |
161
|
|
|
*/ |
162
|
|
|
public function getIncomeById($incomeid) : Income |
163
|
|
|
{ |
164
|
|
|
|
165
|
|
|
return Income::find($incomeid); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $expenseid |
171
|
|
|
* @return Expense |
172
|
|
|
* Function to return specific expense instance per expense id passed |
173
|
|
|
*/ |
174
|
|
|
public function getExpenseById($expenseid) : Expense |
175
|
|
|
{ |
176
|
|
|
|
177
|
|
|
return Expense::find($expenseid); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return Expense[]|\Illuminate\Database\Eloquent\Collection |
182
|
|
|
* function to get all expenses |
183
|
|
|
*/ |
184
|
|
|
public function getAllExpenses(){ |
185
|
|
|
|
186
|
|
|
return Expense::all(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return Income[]|\Illuminate\Database\Eloquent\Collection |
192
|
|
|
* function to get all incomes |
193
|
|
|
*/ |
194
|
|
|
public function getAllIncomes() |
195
|
|
|
{ |
196
|
|
|
|
197
|
|
|
return Income::all(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
public function getIncomeCategoryIdByName($categoryname){ |
202
|
|
|
|
203
|
|
|
return IncomeCategory::where('category',$categoryname)->first()->id; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
public function getExpenseCategoryIdByName($categoryname){ |
208
|
|
|
|
209
|
|
|
return ExpenseCategory::where('category',$categoryname)->first()->id; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function getIncomeOnDateRange($from_date,$to_date){ |
213
|
|
|
|
214
|
|
|
return Income::whereBetween('date',[$from_date,$to_date])->get(); |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
public function getIncomeOnDateRangeFromIncomeType($from_date,$to_date,$income_type) |
221
|
|
|
{ |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
|
225
|
|
|
return Income::whereBetween('date',[$from_date,$to_date])->where('incomecategory',$income_type)->get(); |
226
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
public function getExpensesOnDateRange($from_date,$to_date){ |
231
|
|
|
|
232
|
|
|
return Expense::whereBetween('date',[$from_date,$to_date])->get(); |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function getExpensesOnDateRangeFromExpenseType($from_date,$to_date,$expense_type) |
238
|
|
|
{ |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
|
242
|
|
|
return Expense::whereBetween('date',[$from_date,$to_date])->where('expense_category',$expense_type)->get(); |
243
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
|
248
|
|
|
} |
249
|
|
|
|