forked from opensourcepos/opensourcepos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpense.php
More file actions
359 lines (296 loc) · 12 KB
/
Copy pathExpense.php
File metadata and controls
359 lines (296 loc) · 12 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
namespace App\Models;
use CodeIgniter\Database\ResultInterface;
use CodeIgniter\Model;
use Config\OSPOS;
use stdClass;
/**
* Expense class
*/
class Expense extends Model
{
protected $table = 'expenses';
protected $primaryKey = 'expense_id';
protected $useAutoIncrement = true;
protected $useSoftDeletes = false;
protected $allowedFields = [
'date',
'amount',
'payment_type',
'expense_category_id',
'description',
'employee_id',
'deleted',
'supplier_tax_code',
'tax_amount',
'supplier_id'
];
/**
* Determines if a given Expense_id is an Expense
*/
public function exists(int $expense_id): bool
{
$builder = $this->db->table('expenses');
$builder->where('expense_id', $expense_id);
return ($builder->get()->getNumRows() == 1); // TODO: ===
}
/**
* Gets category info
*/
public function get_expense_category(int $expense_id): object // TODO: This function is never called in the code
{
$builder = $this->db->table('expenses');
$builder->where('expense_id', $expense_id);
$expense_category = model(Expense_category::class);
return $expense_category->get_info($builder->get()->getRow()->expense_category_id); // TODO: refactor out the nested function call.
}
/**
* Gets employee info
*/
public function get_employee(int $expense_id): object // TODO: This function is never called in the code
{
$builder = $this->db->table('expenses');
$builder->where('expense_id', $expense_id);
$employee = model(Employee::class);
return $employee->get_info($builder->get()->getRow()->employee_id); // TODO: refactor out the nested function call.
}
/**
* @param array $expense_ids
* @return ResultInterface
*/
public function get_multiple_info(array $expense_ids): ResultInterface
{
$builder = $this->db->table('expenses');
$builder->whereIn('expenses.expense_id', $expense_ids);
$builder->orderBy('expense_id', 'asc');
return $builder->get();
}
/**
* Gets rows
*/
public function get_found_rows(string $search, array $filters): int
{
return $this->search($search, $filters, 0, 0, 'expense_id', 'asc', true);
}
/**
* Searches expenses
*
* @param string $search
* @param array $filters
* @param int|null $rows
* @param int|null $limit_from
* @param string|null $sort
* @param string|null $order
* @param bool|null $count_only
* @return ResultInterface|false|string
*/
public function search(string $search, array $filters, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'expense_id', ?string $order = 'asc', ?bool $count_only = false): false|string|ResultInterface
{
// Set default values
if ($rows == null) $rows = 0;
if ($limit_from == null) $limit_from = 0;
if ($sort == null) $sort = 'expense_id';
if ($order == null) $order = 'asc';
if ($count_only == null) $count_only = false;
$config = config(OSPOS::class)->settings;
$builder = $this->db->table('expenses AS expenses');
// get_found_rows case
if ($count_only) {
$builder->select('COUNT(DISTINCT expenses.expense_id) as count');
} else {
$builder->select('
expenses.expense_id,
MAX(expenses.date) AS date,
MAX(suppliers.company_name) AS supplier_name,
MAX(expenses.supplier_tax_code) AS supplier_tax_code,
MAX(expenses.amount) AS amount,
MAX(expenses.tax_amount) AS tax_amount,
MAX(expenses.payment_type) AS payment_type,
MAX(expenses.description) AS description,
MAX(employees.first_name) AS first_name,
MAX(employees.last_name) AS last_name,
MAX(expense_categories.category_name) AS category_name
');
}
$builder->join('people AS employees', 'employees.person_id = expenses.employee_id', 'LEFT');
$builder->join('expense_categories AS expense_categories', 'expense_categories.expense_category_id = expenses.expense_category_id', 'LEFT');
$builder->join('suppliers AS suppliers', 'suppliers.person_id = expenses.supplier_id', 'LEFT');
$builder->groupStart();
$builder->like('employees.first_name', $search);
$builder->orLike('expenses.date', $search);
$builder->orLike('employees.last_name', $search);
$builder->orLike('expenses.payment_type', $search);
$builder->orLike('expenses.amount', $search);
$builder->orLike('expense_categories.category_name', $search);
$builder->orLike('CONCAT(employees.first_name, " ", employees.last_name)', $search);
$builder->groupEnd();
$builder->where('expenses.deleted', $filters['is_deleted']);
if (empty($config['date_or_time_format'])) {
$builder->where('DATE_FORMAT(expenses.date, "%Y-%m-%d") BETWEEN ' . $this->db->escape($filters['start_date']) . ' AND ' . $this->db->escape($filters['end_date']));
} else {
$builder->where('expenses.date BETWEEN ' . $this->db->escape(rawurldecode($filters['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($filters['end_date'])));
}
if ($filters['only_debit']) {
$builder->like('expenses.payment_type', lang('Expenses.debit'));
}
if ($filters['only_credit']) {
$builder->like('expenses.payment_type', lang('Expenses.credit'));
}
if ($filters['only_cash']) {
$builder->groupStart();
$builder->like('expenses.payment_type', lang('Expenses.cash'));
$builder->orWhere('expenses.payment_type IS NULL');
$builder->groupEnd();
}
if ($filters['only_due']) {
$builder->like('expenses.payment_type', lang('Expenses.due'));
}
if ($filters['only_check']) {
$builder->like('expenses.payment_type', lang('Expenses.check'));
}
if ($count_only) { // TODO: replace this with `if ($count_only)`
return $builder->get()->getRow()->count;
}
$builder->groupBy('expense_id');
$builder->orderBy($sort, $order);
if ($rows > 0) {
$builder->limit($rows, $limit_from);
}
return $builder->get();
}
/**
* Gets information about a particular expense
*/
public function get_info(int $expense_id): object
{
$builder = $this->db->table('expenses AS expenses');
$builder->select('
expenses.expense_id AS expense_id,
expenses.date AS date,
suppliers.company_name AS supplier_name,
expenses.supplier_id AS supplier_id,
expenses.supplier_tax_code AS supplier_tax_code,
expenses.amount AS amount,
expenses.tax_amount AS tax_amount,
expenses.payment_type AS payment_type,
expenses.description AS description,
expenses.employee_id AS employee_id,
expenses.deleted AS deleted,
employees.first_name AS first_name,
employees.last_name AS last_name,
expense_categories.expense_category_id AS expense_category_id,
expense_categories.category_name AS category_name
');
$builder->join('people AS employees', 'employees.person_id = expenses.employee_id', 'LEFT');
$builder->join('expense_categories AS expense_categories', 'expense_categories.expense_category_id = expenses.expense_category_id', 'LEFT');
$builder->join('suppliers AS suppliers', 'suppliers.person_id = expenses.supplier_id', 'LEFT');
$builder->where('expense_id', $expense_id);
$query = $builder->get();
if ($query->getNumRows() == 1) { // TODO: ===
return $query->getRow();
}
$empty_obj = $this->getEmptyObject('expenses');
$empty_obj->supplier_name = null;
$empty_obj->first_name = null;
$empty_obj->last_name = null;
return $empty_obj;
}
/**
* Initializes an empty object based on database definitions
* @param string $table_name
* @return object
*/
private function getEmptyObject(string $table_name): object
{
// Return an empty base parent object, as $item_id is NOT an item
$empty_obj = new stdClass();
// Iterate through field definitions to determine how the fields should be initialized
foreach ($this->db->getFieldData($table_name) as $field) {
$field_name = $field->name;
if (in_array($field->type, ['int', 'tinyint', 'decimal'])) {
$empty_obj->$field_name = ($field->primary_key == 1) ? NEW_ENTRY : 0;
} else {
$empty_obj->$field_name = null;
}
}
return $empty_obj;
}
/**
* Inserts or updates an expense
*/
public function save_value(array &$expense_data, int $expense_id = NEW_ENTRY): bool
{
$builder = $this->db->table('expenses');
if ($expense_id == NEW_ENTRY || !$this->exists($expense_id)) {
if ($builder->insert($expense_data)) {
$expense_data['expense_id'] = $this->db->insertID();
return true;
}
return false;
}
$builder->where('expense_id', $expense_id);
return $builder->update($expense_data);
}
/**
* Deletes a list of expense_category
*/
public function delete_list(array $expense_ids): bool
{
$builder = $this->db->table('expenses');
$this->db->transStart();
$builder->whereIn('expense_id', $expense_ids);
$success = $builder->update(['deleted' => 1]);
$this->db->transComplete();
$success &= $this->db->transStatus();
return $success;
}
/**
* Gets the payment summary for the expenses (expenses/manage) view
*/
public function get_payments_summary(string $search, array $filters): array // TODO: $search is passed but never used in the function
{
$config = config(OSPOS::class)->settings;
// get payment summary
$builder = $this->db->table('expenses');
$builder->select('payment_type, COUNT(amount) AS count, SUM(amount) AS amount');
$builder->where('deleted', $filters['is_deleted']);
if (empty($config['date_or_time_format'])) {
$builder->where('DATE_FORMAT(date, "%Y-%m-%d") BETWEEN ' . $this->db->escape($filters['start_date']) . ' AND ' . $this->db->escape($filters['end_date']));
} else {
$builder->where('date BETWEEN ' . $this->db->escape(rawurldecode($filters['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($filters['end_date'])));
}
if ($filters['only_cash']) {
$builder->like('payment_type', lang('Expenses.cash'));
}
if ($filters['only_due']) {
$builder->like('payment_type', lang('Expenses.due'));
}
if ($filters['only_check']) {
$builder->like('payment_type', lang('Expenses.check'));
}
if ($filters['only_credit']) {
$builder->like('payment_type', lang('Expenses.credit'));
}
if ($filters['only_debit']) {
$builder->like('payment_type', lang('Expenses.debit'));
}
$builder->groupBy('payment_type');
return $builder->get()->getResultArray();
}
/**
* Gets the payment options to show in the expense forms
*/
public function get_payment_options(): array
{
return get_payment_options();
}
/**
* Gets the expense payment
*/
public function get_expense_payment(int $expense_id): ResultInterface
{
$builder = $this->db->table('expenses');
$builder->where('expense_id', $expense_id);
return $builder->get();
}
}