-
Notifications
You must be signed in to change notification settings - Fork 0
/
MicroBalance.php
590 lines (472 loc) · 17.5 KB
/
MicroBalance.php
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?php
// Prevent users from directly accessing this page.
defined( 'ABSPATH' ) or die( 'Not Even Close, Baby!' );
/**
* Plugin Name: MicroBalance
* Plugin URI:
* Description: Small till manager.
* Version: 0.1.1
* Author: Adriaan Knapen <a.d.knapen@student.tue.nl>
* Author URI:
* License:
*/
add_action('admin_head', 'hook_javascript');
function hook_javascript() {
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-ui-tooltip');
?>
<script>
var MicroBalanceTooltips = jQuery( "[tooltip]" ).tooltip({
position: {
my: "left top",
at: "right+5 top-5"
}
});
</script>
<?php }
add_action( 'admin_init', 'myplugin_scripts' ); // Enque style if user is in the admin panel.
function myplugin_scripts() {
wp_register_style( 'jQuery-UI-style', plugin_dir_url( __FILE__ ) . 'css/jquery-ui-black-tie.css' );
wp_enqueue_style('jQuery-UI-style');
wp_register_style( 'MB-style', plugin_dir_url( __FILE__ ) . 'css/style.css' );
wp_enqueue_style( 'MB-style' );
}
add_action('admin_menu', 'plugin_menu');
function plugin_menu() {
$primary_menu = "MicroBalance";
add_utility_page(__('Overview', 'MicroBalance'), __('Overview', 'MicroBalance'), 'administrator', $primary_menu, 'home_page', 'dashicons-admin-generic');
add_submenu_page($primary_menu, __('New transaction', 'MicroBalance'), __('New transaction', 'MicroBalance'), 'administrator', 'new-transaction', 'plugin_new_transactions');
add_submenu_page($primary_menu, __('Transaction overview', 'MicroBalance'), __('Transaction overview', 'MicroBalance'), 'administrator', 'transactions', 'plugin_transactions');
}
function plugin_new_transactions() {
require_once("PHP/new_transaction.php");
}
function plugin_transactions() {
require_once("PHP/transactions.php");
}
function settings_page() {
?>
<h1>MicroBalance Settings</h1>
<div class="wrap">
<h2>Staff Details</h2>
<form method="post" action="options.php">
<?php settings_fields( 'my-plugin-settings-group' ); ?>
<?php do_settings_sections( 'my-plugin-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Accountant Name</th>
<td><input type="text" name="accountant_name" value="<?php echo esc_attr( get_option('accountant_name') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Accountant Phone Number</th>
<td><input type="text" name="accountant_phone" value="<?php echo esc_attr( get_option('accountant_phone') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Accountant Email</th>
<td><input type="text" name="accountant_email" value="<?php echo esc_attr( get_option('accountant_email') ); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
function add_new_mb_user() {
require_once("PHP/add_user.php");
}
add_action('admin_init', 'set_settings');
function set_settings() {
register_setting( 'my-plugin-settings-group', 'accountant_name' );
register_setting( 'my-plugin-settings-group', 'accountant_phone' );
register_setting( 'my-plugin-settings-group', 'accountant_email' );
}
register_activation_hook(__FILE__, 'db_setup');
function db_setup() {
require("PHP/db_setup.php");
}
function home_page() {
require_once("PHP/manage_users.php");
require_once("PHP/manage_till.php");
}
add_action( 'plugins_loaded', 'my_plugin_load_plugin_textdomain' );
function my_plugin_load_plugin_textdomain() {
load_plugin_textdomain( 'MicroBalance', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
// Non hook-functions here
function format($string) {
return ucfirst(strtolower($string));
}
// Returns a button which acts as link when clicked.
function redirect_button($title, $target = "", $type = "secondary") {
echo "<form method='post' action='$target'>\n";
submit_button($title, $type, 'submit', false);
echo "\n</form>\n";
}
function new_inventory_purchase($debtor_id, $amount, $description = "", $authorid = "") {
global $wpdb;
$transaction_table = get_table('transaction');
if($amount <= 0 || $debtor_id == 0) { // Check if the inventory increases or if the till is the debtor.
return -1; // Inventory value has to increase. Else throw error.
}
if($authorid == "") {
$authorid = get_current_user_id();
}
$data = [
'type' => 'inventory purchase',
'state' => 'new',
'description' => $description,
'authorid' => $authorid
];
$transaction_id = new_transaction('inventory purchase', $description, $authorid);
if($transaction_id < 0) {
return -2;
}
echo $inventory_journal_id = new_journal($amount, 0, 'debit', $transaction_id); // Create journal entry for the inventory.
echo $debtor_journal_id = new_journal(-$amount, $debtor_id, 'credit', $transaction_id); // Create journal entry for the debtor.
// Check if both journals where created.
if($inventory_journal_id > 0 && $debtor_journal_id > 0) {
change_transaction_state($transaction_id, 'not payed');
} else {
echo "<p>Test</p>";
change_transaction_state($transaction_id, 'error');
return -3;
}
// Check if both journals where succesfully payed.
if(pay_journal($inventory_journal_id) && pay_journal($debtor_journal_id)) {
change_transaction_state($transaction_id, 'unapproved');
return $transaction_id;
} else {
change_transaction_state($transaction_id, 'error');
return -4;
}
}
function new_payment($payed_by, $received_payment, $amount, $description = "", $authorid = "") {
global $wpdb;
$transaction_table = get_table('transaction');
if($amount == 0 || $payed_by == 0 || $received_payment == 0) { // Till can't pay or receive directly. And money has to be transfered.
return -1;
}
if($authorid == "") {
$authorid = get_current_user();
}
if($amount > 0) {
$type = 'upgrade';
} else {
$type = 'payout';
}
$transaction_id = new_transaction($type, $description, $authorid);
if($transaction_id < 0) {
return -2;
}
$payed_by_journal = new_journal($amount, $payed_by, 'debit', $transaction_id);
$received_payment_journal = new_journal($amount, $received_payment, 'credit', $transaction_id);
if($payed_by_journal > 0 && $received_payment_journal > 0) {
change_transaction_state($transaction_id, 'not payed');
} else {
change_transaction_state($transaction_id, 'error');
return -3;
}
if(pay_journal($payed_by_journal) && pay_journal($received_payment_journal)) {
change_transaction_state($transaction_id, 'unapproved');
return $transaction_id;
} else {
change_transaction_state($transaction_id, 'error');
return -4;
}
}
function new_transaction($type,$description = "", $authorid = "") {
global $wpdb;
// Check if the type is correct.
switch($type) {
case 'inventory purchase':
case 'purchase':
case 'decleration':
case 'refund':
case 'upgrade':
case 'unknown':
break;
default:
return -1;
}
if($authorid == "") {
$authorid = get_current_user_id();
}
$data = [
'type' => $type,
'state' => 'new',
'description' => $description,
'authorid' => $authorid
];
if($wpdb->insert(get_table('transaction'), $data) == null) {
return -2;
} else {
return $wpdb->insert_id;
}
}
function change_transaction_state($transaction_id, $state) {
global $wpdb;
return $wpdb->update(get_table('transaction'), array('state' => $state), array('transactionid' => $transaction_id));
}
function set_transaction_state($state) {
$data = [
'state' => $state
];
$wpdb->update($transaction_table, $data, array('transactionid' => $id));
}
/*
* @Desciprion: Creates a new journal entry
*
* @param amount: The value which gets transfered.
* @param account: Which account ID changes.
* @param cd: 'credit' or 'debit'.
* param transactionID : ID of the transaction.
*
* @return: journalID on succes, -1 if row insertion failed, -2 if cd wasn't set properly.
*/
function new_journal($amount, $account, $cd, $transactionID) {
global $wpdb;
$journal_table = get_table('journal');
if($cd != 'credit' && $cd != 'debit') {
return -2;
}
$data = [
'amount' => $amount,
'accountid' => $account,
'cd' => $cd,
'transactionid' => $transactionID
];
if(!$wpdb->insert($journal_table, $data)) {
return -1;
}
return $wpdb->insert_id;
}
function pay_journal($journal_id) {
global $wpdb;
$journal_table = get_table('journal');
$users_table = get_table('users');
$sql = "SELECT accountid, cd, amount, payed FROM $journal_table WHERE journalid = $journal_id";
$journal_result = $wpdb->get_row($sql);
if($journal_result == null) {
return -1; // Throw an error if the journal entry wasn't found.
}
if($journal_result->payed != null) {
return 0; // Abort if the journal was already payed.
}
$cd = $journal_result->cd;
$user_id = $journal_result->accountid;
$sql = "SELECT $cd FROM $users_table WHERE id = $user_id";
$user_result = $wpdb->get_row($sql);
if($user_result == null) {
return -2; // Throw an error if the user wasn't found;
}
$old_balance = $user_result->$cd;
$new_balance = $old_balance + $journal_result->amount;
$data = [
$cd => $new_balance
];
if($wpdb->update($users_table, $data, array('id' => $user_id))) {
$date = current_time('mysql', 0);
return $wpdb->update($journal_table, array('payed' => $date, 'new_balance' => $new_balance), array('journalid' => $journal_id));
}
}
/*
* @description: Returns the full name of the table.
* @param table: Name of the table.
* @returns: Full name of the table if found, false if it wasn't found.
*/
function get_table($table) {
switch(strtolower($table)) {
case 'user':
case 'users':
return get_option('MB_user_table');
case 'transaction':
case 'transactions':
return get_option('MB_transaction_table');
case 'journal':
return get_option('MB_journal_table');
default:
return false;
}
}
function get_till() {
global $wpdb;
$table = get_table('users');
$sql = "SELECT credit FROM $table WHERE id = '0'";
$result = $wpdb->get_row($sql);
return $result->credit;
}
function get_inventory() {
global $wpdb;
$table = get_table('users');
$sql = "SELECT debit FROM $table WHERE id = '0'";
$result = $wpdb->get_row($sql);
return $result->debit;
}
function get_user_selector($exclude_self, $group_name) {
global $wpdb;
// Import jQuery and jQuery UI.
wp_enqueue_script('jquery-ui-accordion');
$users = get_MB_users(true,'id','asc');
$id = get_current_user_id();
$name = id_to_name($id);?>
<script>
jQuery(document).ready(function() {
jQuery("#<?php echo $group_name;?>_accordion").accordion({
collapsible: true,
heightStyle: 'content',
animate: 100
});
});
</script>
<?php
if(!$exclude_self) {
echo "<div id='$group_name" . "_accordion'>\n";
echo "<h3>" . __('Yourself', 'MicroBalance') . "</h3>\n";
echo "<div><label>\n\t<input type='radio' name='$group_name' value='$id' checked>$name\n</label></div>\n";
echo "<h3><b>" . __('Other', 'MicroBalance') . "</b></h3>\n";
} else {
echo "<div id='$group_name" . "_accordion'>\n";
echo "<h3><b>" . __('Users', 'MicroBalance') . "</b></h3>\n";
}
echo "<div>";
if(count($users) == 0) {
echo "<div><p>" . __("No other users found", "MicroBalance") . "</p></div>";
} else {
foreach($users as $user) {
$name = id_to_name($user->id);
echo "\n<div>\n\t<label><input type='radio' name='$group_name' value='$user->id'>$name</label>\n</div>\n";
}
}
echo "</div></div>"; // Close the accordion's div.
}
function get_MB_users($exclude_self = true, $order = "id", $asc_desc = "ASC", $include_till = true) {
global $wpdb;
if(strtolower($asc_desc) != "asc" && strtolower($asc_desc) != "desc") {
return -1;
}
$user_table = get_table('users');
$sql = "SELECT * FROM $user_table";
if($include_till || $exclude_self) {
$sql .= " WHERE ";
}
if($include_till) {
$sql .= "id != '0'";
if($exclude_self) {
$sql .= " &&";
}
}
if($exclude_self) {
$id = get_current_user_id();
$sql .= " id != '$id'";
}
switch($order) {
case "id":
$sql .= " ORDER BY id $asc_desc";
break;
}
return $wpdb->get_results($sql);
}
/*
* @description: Returns the full name of the user with the given ID.
* @param id: ID from the user
* @returns: Full name of the user with the parsed ID.
*/
function id_to_name($id) {
global $wpdb;
$table = $wpdb->prefix . "usermeta";
$sql_firstname = "SELECT meta_value FROM $table WHERE user_id = '$id' && meta_key='first_name'";
$sql_lastname = "SELECT meta_value FROM $table WHERE user_id = '$id' && meta_key='last_name'";
$firstname = $wpdb->get_row($sql_firstname)->meta_value;
$lastname = $wpdb->get_row($sql_lastname)->meta_value;
if($firstname != "" && $lastname != "") {
return $firstname . " " . $lastname;
} else {
return $firstname . $lastname;
}
}
function transaction_type_to_text($type) {
switch($type) {
case 'inventory purchase':
return __('Inventory purchase', 'MicroBalance');
case 'purchase':
return __('Purchase', 'MicroBalance');
case 'decleration':
return __('Declaration', 'MicroBalance');
case 'payout':
return __('Payout', 'MicroBalance');
case 'refund':
return __('Refund', 'MicroBalance');
case 'unknown':
return __('Unknown', 'MicroBalance');
case 'error':
return __('Error', 'MicroBalance');
}
}
function echo_transaction($transaction_id, $header = "h2") {
global $wpdb;
$table = get_table('transactions');
if(is_numeric($transaction_id) && is_integer($transaction_id + 0)) {
echo "<$header>" . sprintf(__('Transaction %s', 'MicroBalance'), $transaction_id) . "</$header>\n";
$sql = "SELECT * FROM $table WHERE transactionid = '$transaction_id'";
$result = $wpdb->get_row($sql);
if($result == null) {
echo "<p>" . sprintf(__('Transaction %s not found.', 'MicroBalance'), $transaction_id) . "</p>";
return false;
} else {
$description = $result->description == "" ? "-" : $result->description;
$rows = [
__('Author','MicroBalance') => id_to_name($result->authorid),
__('Description', 'MicroBalance') => $description,
__('Type', 'MicroBalance') => transaction_type_to_text($result->type),
__('Added on', 'MicroBalance') => $result->cdate,
__('Edited on', 'MicroBalance') => $result->edate
];
two_row_table($rows);
return true;
}
}
}
function echo_journal_entry($journal_id, $header = "h2") {
global $wpdb;
$table = get_table('journal');
echo "<$header>" . sprintf(__('Journal entry %s', 'MicroBalance'), $journal_id) . "</$header>\n";
if(!is_numeric($journal_id) || !is_integer($journal_id + 0)) {
echo "<p>" . __('Journal entry not found', 'MicroBalance') . "</p>\n";
return false;
} else {
$result = $wpdb->get_row("SELECT * FROM $table WHERE journalid = '$journal_id'");
if($result == null) {
echo "<p>" . __('Journal entry not found', 'MicroBalance') . "</p>\n";
return false;
} else {
if($result->accountid != 0) {
$account = id_to_name($result->accountid);
} else {
if($result->cd == 'debit') {
$account = __('Inventory', 'MicroBalance');
} else {
$account = __('Till', 'MicroBalance');
}
}
$rows = [
__('Account', 'MicroBalance') => $account . " ($result->accountid)",
__('Amount', 'MicroBalance') => "€" . $result->amount,
__('Credit or debit', 'MicroBalance') => __(ucfirst($result->cd), 'MicroBalance'),
__('Added on', 'MicroBalance') => $result->cdate,
__('Edited on', 'MicroBalance') => $result->edate
];
two_row_table($rows);
return true;
}
}
}
function two_row_table($rows) {
echo "<table>\n";
foreach($rows as $title => $value) {
echo "<tr>\n";
echo "<td><b>$title</b><td>$value</td>";
echo "</tr>\n";
}
echo "</table>\n";
}