forked from opensourcepos/opensourcepos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTax_code.php
More file actions
312 lines (256 loc) · 8.7 KB
/
Copy pathTax_code.php
File metadata and controls
312 lines (256 loc) · 8.7 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
<?php
namespace App\Models;
use CodeIgniter\Database\ResultInterface;
use CodeIgniter\Model;
use Config\OSPOS;
use stdClass;
/**
* Tax Code class
*/
class Tax_code extends Model
{
protected $table = 'tax_codes';
protected $primaryKey = 'tax_code_id';
protected $useAutoIncrement = true;
protected $useSoftDeletes = false;
protected $allowedFields = [
'tax_code',
'tax_code_name',
'city',
'state',
'deleted'
];
/**
* Determines if it exists in the table
*/
public function exists(string $tax_code): bool
{
$builder = $this->db->table('tax_codes');
$builder->where('tax_code', $tax_code);
return ($builder->get()->getNumRows() == 1); // TODO: this should be === since getNumRows returns an int
}
/**
* Gets total of rows
*/
public function get_total_rows(): int
{
$builder = $this->db->table('tax_codes');
$builder->where('deleted', 0);
return $builder->countAllResults();
}
/**
* Gets information about the particular record
*/
public function get_info(?int $tax_code_id): object
{
if ($tax_code_id != null) {
$builder = $this->db->table('tax_codes');
$builder->where('tax_code_id', $tax_code_id);
$builder->where('deleted', 0);
$query = $builder->get();
}
if ($tax_code_id != null && $query->getNumRows() === 1) {
return $query->getRow();
} else {
// Get empty base parent object
$tax_code_obj = new stdClass();
// Get all the fields from the table
foreach ($this->db->getFieldNames('tax_codes') as $field) {
$tax_code_obj->$field = null;
}
return $tax_code_obj;
}
}
/**
* Returns all rows from the table
*/
public function get_all(int $rows = 0, int $limit_from = 0, bool $no_deleted = true): ResultInterface // TODO: $no_deleted should be something like $is_deleted and flip the logic.
{
$builder = $this->db->table('tax_codes');
if ($no_deleted) {
$builder->where('deleted', 0);
}
$builder->orderBy('tax_code_name', 'asc');
if ($rows > 0) {
$builder->limit($rows, $limit_from);
}
return $builder->get();
}
/**
* Returns multiple rows
*/
public function get_multiple_info(array $tax_codes): ResultInterface
{
$builder = $this->db->table('tax_codes');
$builder->whereIn('tax_code', $tax_codes);
$builder->orderBy('tax_code_name', 'asc');
return $builder->get();
}
/**
* Inserts or updates a row
*/
public function save($tax_code_data): bool
{
$builder = $this->db->table('tax_codes');
if (!$this->exists($tax_code_data['tax_code'])) {
if ($builder->insert($tax_code_data)) { // TODO: this should be refactored to return $builder->insert($tax_code_data); in the same way that $builder->update() below is the return. Look for this in the other save functions as well.
return true;
}
return false;
}
$builder->where('tax_code', $tax_code_data['tax_code']);
return $builder->update($tax_code_data);
}
/**
* Saves changes to the tax codes table
*/
public function save_tax_codes(array $array_save): bool // TODO: Need to rename $array_save to probably $tax_codes
{
$this->db->transStart();
$not_to_delete = [];
foreach ($array_save as $key => $value) {
// Save or update
$tax_code_data = [
'tax_code' => $value['tax_code'],
'tax_code_name' => $value['tax_code_name'],
'city' => $value['city'],
'state' => $value['state'],
'deleted' => '0'
];
$this->save($tax_code_data);
$not_to_delete[] = $tax_code_data['tax_code'];
}
// All entries not available in post will be deleted now
$deleted_tax_codes = $this->get_all()->getResultArray();
foreach ($deleted_tax_codes as $key => $tax_code_data) {
if (!in_array($tax_code_data['tax_code'], $not_to_delete)) {
$this->delete($tax_code_data['tax_code']);
}
}
$this->db->transComplete();
return $this->db->transStatus();
}
/**
* Deletes a specific tax code
*/
public function delete($tax_code = null, bool $purge = false): bool
{
$builder = $this->db->table('tax_codes');
$builder->where('tax_code', $tax_code);
return $builder->update(['deleted' => 1]);
}
/**
* Deletes a list of rows
*/
public function delete_list(array $tax_codes): bool
{
$builder = $this->db->table('tax_codes');
$builder->whereIn('tax_code', $tax_codes);
return $builder->update(['deleted' => 1]);
}
/**
* Gets rows
*/
public function get_found_rows(string $search): int
{
return $this->search($search, 0, 0, 'tax_code_name', 'asc', true);
}
/**
* Perform a search for a set of rows
*/
public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'tax_code_name', ?string $order = 'asc', ?bool $count_only = false)
{
// Set default values
if ($rows == null) $rows = 0;
if ($limit_from == null) $limit_from = 0;
if ($sort == null) $sort = 'tax_code_name';
if ($order == null) $order = 'asc';
if ($count_only == null) $count_only = false;
$builder = $this->db->table('tax_codes AS tax_codes');
// get_found_rows case
if ($count_only) {
$builder->select('COUNT(tax_codes.tax_code) as count');
}
$builder->groupStart();
$builder->like('tax_code_name', $search);
$builder->orLike('tax_code', $search);
$builder->groupEnd();
$builder->where('deleted', 0);
// get_found_rows case
if ($count_only) {
return $builder->get()->getRow()->count;
}
$builder->orderBy($sort, $order);
if ($rows > 0) {
$builder->limit($rows, $limit_from);
}
return $builder->get();
}
/**
* Gets the tax code to use for a given customer
*/
public function get_sales_tax_code(string $city = '', string $state = '')
{
$config = config(OSPOS::class)->settings;
// If tax code using both city and state cannot be found then try again using just the state
// If the state tax code cannot be found then try again using blanks for both
$builder = $this->db->table('tax_codes');
$builder->where('city', $city);
$builder->where('state', $state);
$builder->where('deleted', 0);
$query = $builder->get();
if ($query->getNumRows() == 1) { // TODO: ===
return $query->getRow()->tax_code_id;
}
$builder = $this->db->table('tax_codes');
$builder->where('city', '');
$builder->where('state', $state);
$builder->where('deleted', 0);
$query = $builder->get();
if ($query->getNumRows() == 1) { // TODO: this should be ===
return $query->getRow()->tax_code_id;
} else {
return $config['default_tax_code'];
}
}
/**
* @param string $search
* @param int $limit
* @return array
*/
public function get_tax_codes_search_suggestions(string $search, int $limit = 25): array
{
$suggestions = [];
$builder = $this->db->table('tax_codes');
if (!empty($search)) {
$builder->like('tax_code', $search);
$builder->orLike('tax_code_name', $search);
}
$builder->where('deleted', 0);
$builder->orderBy('tax_code_name', 'asc');
foreach ($builder->get()->getResult() as $row) {
$suggestions[] = ['value' => $row->tax_code_id, 'label' => ($row->tax_code . ' ' . $row->tax_code_name)];
}
// Only return $limit suggestions
if (count($suggestions) > $limit) {
$suggestions = array_slice($suggestions, 0, $limit);
}
return $suggestions;
}
/**
* @return array[]
*/
public function get_empty_row(): array
{
return [
'0' => [
'tax_code_id' => NEW_ENTRY,
'tax_code' => '',
'tax_code_name' => '',
'city' => '',
'state' => '',
'deleted' => 0
]
];
}
}