forked from webERP-team/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PcClaimExpensesFromTab.php
793 lines (780 loc) · 34.3 KB
/
PcClaimExpensesFromTab.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
<?php
include('includes/session.php');
$Title = _('Claim Petty Cash Expenses From Tab');
/* webERP manual links before header.php */
$ViewTopic = 'PettyCash';
$BookMark = 'ExpenseClaim';
include('includes/header.php');
if (isset($_POST['SelectedTabs'])) {
$SelectedTabs = mb_strtoupper($_POST['SelectedTabs']);
} elseif (isset($_GET['SelectedTabs'])) {
$SelectedTabs = mb_strtoupper($_GET['SelectedTabs']);
}
if (isset($_POST['SelectedIndex'])) {
$SelectedIndex = $_POST['SelectedIndex'];
} elseif (isset($_GET['SelectedIndex'])) {
$SelectedIndex = $_GET['SelectedIndex'];
}
if (isset($_POST['Days'])) {
$Days = filter_number_format($_POST['Days']);
} elseif (isset($_GET['Days'])) {
$Days = filter_number_format($_GET['Days']);
}
if (isset($_POST['Cancel'])) {
unset($SelectedTabs);
unset($SelectedIndex);
unset($Days);
unset($_POST['Amount']);
unset($_POST['Purpose']);
unset($_POST['Notes']);
unset($_FILES['Receipt']);
}
if (isset($_POST['Process'])) {
if ($_POST['SelectedTabs'] == '') {
echo prnMsg(_('You have not selected a tab to claim the expenses on'), 'error');
unset($SelectedTabs);
}
}
if (isset($_POST['Go'])) {
if ($Days <= 0) {
prnMsg(_('The number of days must be a positive number'), 'error');
$Days = 30;
}
}
//Define receipt attachment upload functions and variables which are used in various places within script
$ReceiptSupportedExt = array('png','jpg','jpeg','pdf','doc','docx','xls','xlsx'); //Supported file extensions
$ReceiptDir = $PathPrefix . 'companies/' . $_SESSION['DatabaseName'] . '/expenses_receipts/'; //Receipts upload directory
if (isset($_POST['submit'])) {
//initialise no input errors assumed initially before we test
$InputError = 0;
/* actions to take once the user has clicked the submit button
ie the page has called itself with some user input */
//first off validate inputs sensible
if ($_POST['SelectedExpense'] == '') {
$InputError = 1;
prnMsg(_('You have not selected an expense to claim on this tab'), 'error');
} elseif ($_POST['Amount'] == 0) {
$InputError = 1;
prnMsg(_('The amount must be greater than 0'), 'error');
}
if (!is_date($_POST['Date'])) {
$InputError = 1;
prnMsg(_('The date input is not in the correct format'), 'error');
}
if (isset($SelectedIndex) and $InputError != 1) { //Edit
$SQL = "UPDATE pcashdetails
SET date = '" . FormatDateForSQL($_POST['Date']) . "',
tag = '" . $_POST['Tag'] . "',
codeexpense = '" . $_POST['SelectedExpense'] . "',
amount = '" . -filter_number_format($_POST['Amount']) . "',
notes = '" . $_POST['Notes'] . "'
WHERE counterindex = '" . $SelectedIndex . "'";
$Msg = _('The expense record on tab') . ' ' . $SelectedTabs . ' ' . _('has been updated');
$Result = DB_query($SQL);
foreach ($_POST as $Index => $Value) {
if (substr($Index, 0, 5) == 'index') {
$Index = $Value;
$SQL = "UPDATE pcashdetailtaxes SET pccashdetail='" . $_POST['PcCashDetail' . $Index] . "',
calculationorder='" . $_POST['CalculationOrder' . $Index] . "',
description='" . $_POST['Description' . $Index] . "',
taxauthid='" . $_POST['TaxAuthority' . $Index] . "',
purchtaxglaccount='" . $_POST['TaxGLAccount' . $Index] . "',
taxontax='" . $_POST['TaxOnTax' . $Index] . "',
taxrate='" . $_POST['TaxRate' . $Index] . "',
amount='" . -$_POST['TaxAmount' . $Index] . "'
WHERE counterindex='" . $Index ."'";
$Result = DB_query($SQL);
}
}
if (isset($_FILES['Receipt']) and $_FILES['Receipt']['name'] != '') {
$UploadOriginalName = $_FILES['Receipt']['name'];
$UploadTempName = $_FILES['Receipt']['tmp_name'];
$UploadSize = $_FILES['Receipt']['size'];
$UploadType = $_FILES['Receipt']['type'];
$UploadError = $_FILES['Receipt']['error'];
$UploadTheFile = 'Yes'; //Assume all is well to start off with, but check for the worst
$ReceiptSupportedMime = array('image/png','image/jpeg','application/pdf','application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/vnd.ms-excel','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //list of support mime types, corresponding to the list of support file extensions in $ReceiptSupportedExt
if ($UploadSize > ($_SESSION['MaxImageSize'] * 1024)) { //Server-side file size check. This will usually be caught by $UploadError == 2 (MAX_FILE_SIZE), but we must not trust the user.
prnMsg(_('The uploaded file exceeds the maximum file size of') . ' ' . $_SESSION['MaxImageSize'] . 'KB', 'warn');
$UploadTheFile = 'No';
} elseif (!in_array($UploadType, $ReceiptSupportedMime) and $UploadError != 2) { //File type check. If $UploadError == 2, then $UploadType will be empty.
prnMsg(_('File type not accepted. Only the following file types can be attached') . ': ' . implode(', ', $ReceiptSupportedExt), 'warn');
$UploadTheFile = 'No';
} elseif ($UploadError == 1 ) { //upload_max_filesize error check
prnMsg(_('The uploaded file exceeds the upload_max_filesize directive in php.ini. Please contact your system administrator.'), 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 2 ) { //Client-side file size error check (MAX_FILE_SIZE)
prnMsg(_('The uploaded file exceeds the maximum file size of') . ' ' . $_SESSION['MaxImageSize'] . 'KB', 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 3 ) { //Partial upload error check
prnMsg( _('The uploaded file was only partially uploaded. Please try again.'), 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 4 ) { //No file uploaded error check
prnMsg( _('No file was uploaded'), 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 5 ) { //Undefined error check
prnMsg( _('Undefined error'), 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 6 ) { //Temp directory error check
prnMsg( _('A necessary temporary folder is missing. Please contact your system administrator.'), 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 7 ) { //Disk write failure error check
prnMsg( _('Cannot write file to disk. Please contact your system administrator.'), 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 8 ) { //Upload stopped by PHP extension error check
prnMsg( _('The file upload was stopped by a PHP extension. Please contact your system administrator.'), 'warn');
$UploadTheFile ='No';
}
if ($UploadTheFile == 'Yes') { //Passed all the above validation
$ReceiptSQL = "SELECT hashfile,
extension
FROM pcreceipts
WHERE pccashdetail='" . $SelectedIndex . "'
LIMIT 1";
$ReceiptResult = DB_query($ReceiptSQL);
$ReceiptRow = DB_fetch_assoc($ReceiptResult);
if (DB_num_rows($ReceiptResult) > 0) { //If expenses record already has an uploaded receipt
//Delete existing receipt files from directory
$ReceiptHash = $ReceiptRow['hashfile'];
$ReceiptExt = $ReceiptRow['extension'];
$ReceiptFileName = $ReceiptHash . '.' . $ReceiptExt;
$ReceiptPath = $ReceiptDir . $ReceiptFileName;
unlink($ReceiptPath);
//Upload the new receipt file.
if (!file_exists($ReceiptDir)) { //Create the receipts directory if it doesn't already exist
mkdir($ReceiptDir, 0775, true);
}
$ReceiptHash = md5(md5_file($UploadTempName) . microtime()); //MD5 hash of uploaded file with timestamp
$ReceiptExt = strtolower(pathinfo($UploadOriginalName, PATHINFO_EXTENSION)); //Grab the file extension of the uploaded file
$ReceiptFileName = $ReceiptHash . '.' . $ReceiptExt; //Rename the uploaded file with the expenses index number
$ReceiptPath = $ReceiptDir . $ReceiptFileName;
move_uploaded_file($UploadTempName, $ReceiptPath); //Move the uploaded file from the temp directory to the receipts directory
//Update receipt file info in database
$ReceiptSQL = "UPDATE pcreceipts SET hashfile='" . $ReceiptHash . "',
type='" . $UploadType . "',
extension='" . $ReceiptExt . "',
size=" . $UploadSize . "
WHERE pccashdetail='" . $SelectedIndex . "'";
$ReceiptResult = DB_query($ReceiptSQL);
} else { //If expenses record does not already have an uploaded receipt
if (!file_exists($ReceiptDir)) { //Create the receipts directory if it doesn't already exist
mkdir($ReceiptDir, 0775, true);
}
$ReceiptExt = strtolower(pathinfo($UploadOriginalName, PATHINFO_EXTENSION)); //Grab the file extension of the uploaded file
$ReceiptHash = md5(md5_file($UploadTempName) . microtime()); //MD5 hash of uploaded file with timestamp
$ReceiptFileName = $ReceiptHash . '.' . $ReceiptExt; //Rename the uploaded file with the expenses index number
$ReceiptPath = $ReceiptDir . $ReceiptFileName;
move_uploaded_file($UploadTempName, $ReceiptPath); //Move the uploaded file from the temp directory to the receipts directory
$ReceiptSQL = "INSERT INTO pcreceipts (counterindex,
pccashdetail,
hashfile,
type,
extension,
size
) VALUES (
NULL,
'" . $SelectedIndex . "',
'" . $ReceiptHash . "',
'" . $UploadType . "',
'" . $ReceiptExt . "',
" . $UploadSize . "
)";
$ReceiptResult = DB_query($ReceiptSQL);
}
}
}
prnMsg($Msg, 'success');
} elseif ($InputError != 1) {
// First check the type is not being duplicated
// Add new record on submit
$SQL = "INSERT INTO pcashdetails (counterindex,
tabcode,
tag,
date,
codeexpense,
amount,
authorized,
posted,
purpose,
notes)
VALUES (NULL,
'" . $_POST['SelectedTabs'] . "',
'" . $_POST['Tag'] . "',
'" . FormatDateForSQL($_POST['Date']) . "',
'" . $_POST['SelectedExpense'] . "',
'" . -filter_number_format($_POST['Amount']) . "',
0,
0,
'" . $_POST['Purpose'] . "',
'" . $_POST['Notes'] . "'
)";
$Msg = _('The expense claim on tab') . ' ' . $_POST['SelectedTabs'] . ' ' . _('has been created');
$Result = DB_query($SQL);
$SelectedIndex = DB_Last_Insert_ID('pcashdetails', 'counterindex');
foreach ($_POST as $Index => $Value) {
if (substr($Index, 0, 5) == 'index') {
$Index = $Value;
$SQL = "INSERT INTO pcashdetailtaxes (counterindex,
pccashdetail,
calculationorder,
description,
taxauthid,
purchtaxglaccount,
taxontax,
taxrate,
amount
) VALUES (
NULL,
'" . $SelectedIndex . "',
'" . $_POST['CalculationOrder' . $Index] . "',
'" . $_POST['Description' . $Index] . "',
'" . $_POST['TaxAuthority' . $Index] . "',
'" . $_POST['TaxGLAccount' . $Index] . "',
'" . $_POST['TaxOnTax' . $Index] . "',
'" . $_POST['TaxRate' . $Index] . "',
'" . -$_POST['TaxAmount' . $Index] . "'
)";
$Result = DB_query($SQL);
}
}
if (isset($_FILES['Receipt']) and $_FILES['Receipt']['name'] != '') {
$UploadOriginalName = $_FILES['Receipt']['name'];
$UploadTempName = $_FILES['Receipt']['tmp_name'];
$UploadSize = $_FILES['Receipt']['size'];
$UploadType = $_FILES['Receipt']['type'];
$UploadError = $_FILES['Receipt']['error'];
$UploadTheFile = 'Yes'; //Assume all is well to start off with, but check for the worst
$ReceiptSupportedMime = array('image/png','image/jpeg','application/pdf','application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/vnd.ms-excel','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //list of support mime types, corresponding to the list of support file extensions in $ReceiptSupportedExt
if ($UploadSize > ($_SESSION['MaxImageSize'] * 1024)) { //Server-side file size check. This will usually be caught by $UploadError == 2 (MAX_FILE_SIZE), but we must not trust the user.
prnMsg(_('The uploaded file exceeds the maximum file size of') . ' ' . $_SESSION['MaxImageSize'] . 'KB', 'warn');
$UploadTheFile = 'No';
} elseif (!in_array($UploadType, $ReceiptSupportedMime) and $UploadError != 2) { //File type check. If $UploadError == 2, then $UploadType will be empty.
prnMsg(_('File type not accepted. Only the following file types can be attached') . ': ' . implode(', ', $ReceiptSupportedExt), 'warn');
$UploadTheFile = 'No';
} elseif ($UploadError == 1 ) { //upload_max_filesize error check
prnMsg(_('The uploaded file exceeds the upload_max_filesize directive in php.ini. Please contact your system administrator.'), 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 2 ) { //Client-side file size error check (MAX_FILE_SIZE)
prnMsg(_('The uploaded file exceeds the maximum file size of') . ' ' . $_SESSION['MaxImageSize'] . 'KB', 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 3 ) { //Partial upload error check
prnMsg( _('The uploaded file was only partially uploaded. Please try again.'), 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 4 ) { //No file uploaded error check
prnMsg( _('No file was uploaded'), 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 5 ) { //Undefined error check
prnMsg( _('Undefined error'), 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 6 ) { //Temp directory error check
prnMsg( _('A necessary temporary folder is missing. Please contact your system administrator.'), 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 7 ) { //Disk write failure error check
prnMsg( _('Cannot write file to disk. Please contact your system administrator.'), 'warn');
$UploadTheFile ='No';
} elseif ($UploadError == 8 ) { //Upload stopped by PHP extension error check
prnMsg( _('The file upload was stopped by a PHP extension. Please contact your system administrator.'), 'warn');
$UploadTheFile ='No';
}
if ($UploadTheFile == 'Yes') { //Passed all the above validation
if (!file_exists($ReceiptDir)) { //Create the receipts directory if it doesn't already exist
mkdir($ReceiptDir, 0775, true);
}
$ReceiptHash = md5(md5_file($UploadTempName) . microtime()); //MD5 hash of uploaded file with timestamp
$ReceiptExt = strtolower(pathinfo($UploadOriginalName, PATHINFO_EXTENSION)); //Grab the file extension of the uploaded file
$ReceiptFileName = $ReceiptHash . '.' . $ReceiptExt; //Rename the uploaded file with the expenses index number
$ReceiptPath = $ReceiptDir . $ReceiptFileName;
move_uploaded_file($UploadTempName, $ReceiptPath); //Move the uploaded file from the temp directory to the receipts directory
$ReceiptSQL = "INSERT INTO pcreceipts (counterindex,
pccashdetail,
hashfile,
type,
extension,
size
) VALUES (
NULL,
'" . $SelectedIndex . "',
'" . $ReceiptHash . "',
'" . $UploadType . "',
'" . $ReceiptExt . "',
" . $UploadSize . "
)";
$ReceiptResult = DB_query($ReceiptSQL);
}
}
prnMsg($Msg, 'success');
}
if ($InputError != 1) {
unset($_POST['SelectedExpense']);
unset($_POST['Amount']);
unset($_POST['Tag']);
unset($_POST['Date']);
unset($_POST['Purpose']);
unset($_POST['Notes']);
unset($_FILES['Receipt']);
}
} elseif (isset($_GET['delete'])) {
$ReceiptSQL = "SELECT hashfile,
extension
FROM pcreceipts
WHERE pccashdetail='" . $SelectedIndex . "'
LIMIT 1";
$ReceiptResult = DB_query($ReceiptSQL);
$ReceiptRow = DB_fetch_assoc($ReceiptResult);
if (DB_num_rows($ReceiptResult) > 0) {
//Delete receipt files from directory
$ReceiptHash = $ReceiptRow['hashfile'];
$ReceiptExt = $ReceiptRow['extension'];
$ReceiptFileName = $ReceiptHash . '.' . $ReceiptExt;
$ReceiptPath = $ReceiptDir . $ReceiptFileName;
unlink($ReceiptPath);
//Delete receipt file info from database
$SQL = "DELETE FROM pcreceipts
WHERE pccashdetail='" . $SelectedIndex . "'";
$ErrMsg = _('Petty Cash Expense record could not be deleted because');
$Result = DB_query($SQL, $ErrMsg);
}
//Delete expenses record & associated taxes
$SQL = "DELETE FROM pcashdetails, pcashdetailtaxes
USING pcashdetails
INNER JOIN pcashdetailtaxes
ON pcashdetails.counterindex = pcashdetailtaxes.pccashdetail
WHERE pcashdetails.counterindex = '" . $SelectedIndex . "'";
$Result = DB_query($SQL, $ErrMsg);
prnMsg(_('The expense record on tab') . ' ' . $SelectedTabs . ' ' . _('has been deleted'), 'success');
unset($_GET['delete']);
} //end of get delete
if (!isset($SelectedTabs)) {
/* It could still be the first time the page has been run and a record has been selected for modification - SelectedTabs will exist because it was sent with the new call. If its the first time the page has been displayed with no parameters
then none of the above are true and the list of sales types will be displayed with
links to delete or edit each. These will call the same page again and allow update/input
or deletion of the records*/
echo '<p class="page_title_text">
<img src="', $RootPath, '/css/', $_SESSION['Theme'], '/images/money_add.png" title="', _('Payment Entry'), '" alt="" />', ' ', $Title, '
</p>';
echo '<form method="post" action="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'), '" enctype="multipart/form-data">';
echo '<input type="hidden" name="FormID" value="', $_SESSION['FormID'], '" />';
echo '<table class="selection">
<tr>
<td>', _('Clain expenses on petty cash tab'), ':</td>
<td><select required="required" name="SelectedTabs">';
$SQL = "SELECT tabcode
FROM pctabs
WHERE usercode='" . $_SESSION['UserID'] . "'";
$Result = DB_query($SQL);
echo '<option value="">', _('Not Yet Selected'), '</option>';
while ($MyRow = DB_fetch_array($Result)) {
if (isset($_POST['SelectTabs']) and $MyRow['tabcode'] == $_POST['SelectTabs']) {
echo '<option selected="selected" value="', $MyRow['tabcode'], '">', $MyRow['tabcode'], '</option>';
} else {
echo '<option value="', $MyRow['tabcode'], '">', $MyRow['tabcode'], '</option>';
}
} //end while loop
echo '</select>
</td>
</tr>';
echo '</table>'; // close main table
echo '<div class="centre">
<input type="submit" name="Process" value="', _('Accept'), '" />
<input type="submit" name="Cancel" value="', _('Cancel'), '" />
</div>';
echo '</form>';
} else { // isset($SelectedTabs)
echo '<div class="centre">
<a href="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'), '">', _('Select another tab'), '</a>
</div>';
echo '<p class="page_title_text">
<img src="', $RootPath, '/css/', $_SESSION['Theme'], '/images/money_add.png" title="', _('Petty Cash Claim Entry'), '" alt="" />', ' ', $Title, '
</p>';
if (!isset($_GET['edit']) or isset($_POST['GO'])) {
if (!isset($Days)) {
$Days = 30;
}
/* Retrieve decimal places to display */
$SQLDecimalPlaces = "SELECT decimalplaces
FROM currencies,pctabs
WHERE currencies.currabrev = pctabs.currency
AND tabcode='" . $SelectedTabs . "'";
$Result = DB_query($SQLDecimalPlaces);
$MyRow = DB_fetch_array($Result);
$CurrDecimalPlaces = $MyRow['decimalplaces'];
echo '<form method="post" action="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'), '" enctype="multipart/form-data">';
echo '<input type="hidden" name="FormID" value="', $_SESSION['FormID'], '" />';
echo '<br /><table class="selection">';
echo '<tr>
<td>' . _('Petty Cash Tab') . ':</td>
<td>' . $SelectedTabs . '</td>
</tr>';
echo '</table>';
//Limit expenses history to X days
echo '<table class="selection">
<tr>
<td>', _('Detail of Tab Movements For Last '), ':
<input type="hidden" name="SelectedTabs" value="' . $SelectedTabs . '" />
<input type="text" class="number" name="Days" value="', $Days, '" required="required" maxlength="3" size="4" /> ', _('Days'), '
<input type="submit" name="Go" value="', _('Go'), '" />
</td>
</tr>
</table>';
if (isset($_POST['Cancel'])) {
unset($_POST['SelectedExpense']);
unset($_POST['Amount']);
unset($_POST['Date']);
unset($_POST['Purpose']);
unset($_POST['Notes']);
unset($_FILES['Receipt']);
}
$SQL = "SELECT counterindex,
tabcode,
tag,
date,
codeexpense,
amount,
authorized,
posted,
purpose,
notes
FROM pcashdetails
WHERE tabcode='" . $SelectedTabs . "'
AND date >=DATE_SUB(CURDATE(), INTERVAL " . $Days . " DAY)
ORDER BY date,
counterindex ASC";
$Result = DB_query($SQL);
echo '<table class="selection">
<thead>
<tr>
<th class="ascending">', _('Date of Expense'), '</th>
<th class="ascending">', _('Expense Code'), '</th>
<th class="ascending">', _('Gross Amount'), '</th>
<th>', _('Tax'), '</th>
<th>', _('Tax Group'), '</th>
<th>', _('Tag'), '</th>
<th>', _('Business Purpose'), '</th>
<th>', _('Notes'), '</th>
<th>', _('Receipt Attachment'), '</th>
<th class="ascending">', _('Date Authorised'), '</th>
</tr>
</thead>
<tbody>';
while ($MyRow = DB_fetch_array($Result)) {
$SQLDes = "SELECT description
FROM pcexpenses
WHERE codeexpense='" . $MyRow['codeexpense'] . "'";
$ResultDes = DB_query($SQLDes);
$Description = DB_fetch_array($ResultDes);
if (!isset($Description[0])) {
$ExpenseCodeDes = 'ASSIGNCASH';
} else {
$ExpenseCodeDes = $MyRow['codeexpense'] . ' - ' . $Description[0];
}
if ($MyRow['authorized'] == '0000-00-00') {
$AuthorisedDate = _('Unauthorised');
} else {
$AuthorisedDate = ConvertSQLDate($MyRow['authorized']);
}
//Generate download link for expense receipt, or show text if no receipt file is found.
$ReceiptSQL = "SELECT hashfile,
extension
FROM pcreceipts
WHERE pccashdetail='" . $MyRow['counterindex'] . "'";
$ReceiptResult = DB_query($ReceiptSQL);
$ReceiptRow = DB_fetch_array($ReceiptResult);
if (DB_num_rows($ReceiptResult) > 0) { //If receipt exists in database
$ReceiptHash = $ReceiptRow['hashfile'];
$ReceiptExt = $ReceiptRow['extension'];
$ReceiptFileName = $ReceiptHash . '.' . $ReceiptExt;
$ReceiptPath = $ReceiptDir . $ReceiptFileName;
$ReceiptText = '<a href="' . $ReceiptPath . '" download="ExpenseReceipt-' . mb_strtolower($SelectedTabs) . '-[' . $MyRow['date'] . ']-[' . $MyRow['counterindex'] . ']">' . _('Download attachment') . '</a>';
} elseif ($ExpenseCodeDes == 'ASSIGNCASH') {
$ReceiptText = '';
} else {
$ReceiptText = _('No attachment');
}
$TagSQL = "SELECT tagdescription FROM tags WHERE tagref='" . $MyRow['tag'] . "'";
$TagResult = DB_query($TagSQL);
$TagRow = DB_fetch_array($TagResult);
if ($MyRow['tag'] == 0) {
$TagRow['tagdescription'] = _('None');
}
$TagTo = $MyRow['tag'];
if ($ExpenseCodeDes == 'ASSIGNCASH') {
$TagDescription = '';
} else {
$TagDescription = $TagTo . ' - ' . $TagRow['tagdescription'];
}
$TaxesDescription = '';
$TaxesTaxAmount = '';
$TaxSQL = "SELECT counterindex,
pccashdetail,
calculationorder,
description,
taxauthid,
purchtaxglaccount,
taxontax,
taxrate,
amount
FROM pcashdetailtaxes
WHERE pccashdetail='" . $MyRow['counterindex'] . "'";
$TaxResult = DB_query($TaxSQL);
while ($MyTaxRow = DB_fetch_array($TaxResult)) {
$TaxesDescription .= $MyTaxRow['description'] . '<br />';
$TaxesTaxAmount .= locale_number_format($MyTaxRow['amount'], $CurrDecimalPlaces) . '<br />';
}
if (($MyRow['authorized'] == '0000-00-00') and ($ExpenseCodeDes != 'ASSIGNCASH')) {
// only movements NOT authorised can be modified or deleted
echo '<tr class="striped_row">
<td>', ConvertSQLDate($MyRow['date']), '</td>
<td>', $ExpenseCodeDes, '</td>
<td class="number">', locale_number_format($MyRow['amount'], $CurrDecimalPlaces), '</td>
<td class="number">', $TaxesTaxAmount, '</td>
<td>', $TaxesDescription, '</td>
<td>', $TagDescription, '</td>
<td>', $MyRow['purpose'], '</td>
<td>', $MyRow['notes'], '</td>
<td>', $ReceiptText, '</td>
<td>', $AuthorisedDate, '</td>
<td><a href="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '?SelectedIndex=', $MyRow['counterindex'], '&SelectedTabs=' . $SelectedTabs . '&Days=' . $Days . '&edit=yes">' . _('Edit') . '</a></td>
<td><a href="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '?SelectedIndex=', $MyRow['counterindex'], '&SelectedTabs=' . $SelectedTabs . '&Days=' . $Days . '&delete=yes" onclick=\'return confirm("' . _('Are you sure you wish to delete this expense?') . '");\'>' . _('Delete') . '</a></td>
</tr>';
} else {
echo '<tr class="striped_row">
<td>', ConvertSQLDate($MyRow['date']), '</td>
<td>', $ExpenseCodeDes, '</td>
<td class="number">', locale_number_format($MyRow['amount'], $CurrDecimalPlaces), '</td>
<td class="number">', $TaxesTaxAmount, '</td>
<td>', $TaxesDescription, '</td>
<td>', $TagDescription, '</td>
<td>', $MyRow['purpose'], '</td>
<td>', $MyRow['notes'], '</td>
<td>', $ReceiptText, '</td>
<td>', $AuthorisedDate, '</td>
</tr>';
}
}
//END WHILE LIST LOOP
$SQLAmount = "SELECT sum(amount)
FROM pcashdetails
WHERE tabcode='" . $SelectedTabs . "'";
$ResultAmount = DB_query($SQLAmount);
$Amount = DB_fetch_array($ResultAmount);
if (!isset($Amount['0'])) {
$Amount['0'] = 0;
}
echo '</tbody>
<tfoot>
<tr>
<td colspan="2" class="number">', _('Current balance'), ':</td>
<td class="number">', locale_number_format($Amount['0'], $CurrDecimalPlaces), '</td>
</tr>
</tfoot>';
echo '</table>';
echo '</form>';
}
if (!isset($_GET['delete'])) {
echo '<form method="post" action="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'), '" enctype="multipart/form-data">';
echo '<input type="hidden" name="FormID" value="', $_SESSION['FormID'], '" />';
if (isset($_GET['edit'])) {
$SQL = "SELECT counterindex,
tabcode,
tag,
date,
codeexpense,
amount,
authorized,
posted,
purpose,
notes
FROM pcashdetails
WHERE counterindex='" . $SelectedIndex . "'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_array($Result);
$_POST['Date'] = ConvertSQLDate($MyRow['date']);
$_POST['SelectedExpense'] = $MyRow['codeexpense'];
$_POST['Amount'] = -$MyRow['amount'];
$_POST['Purpose'] = $MyRow['purpose'];
$_POST['Notes'] = $MyRow['notes'];
$_POST['Tag'] = $MyRow['tag'];
echo '<input type="hidden" name="SelectedTabs" value="', $SelectedTabs, '" />';
echo '<input type="hidden" name="SelectedIndex" value="', $SelectedIndex, '" />';
echo '<input type="hidden" name="Days" value="', $Days, '" />';
} //end of Get Edit
if (!isset($_POST['Date'])) {
$_POST['Date'] = Date($_SESSION['DefaultDateFormat']);
}
echo '<table class="selection">';
if (isset($_GET['SelectedIndex'])) {
echo '<tr>
<th colspan="2"><h3>', _('Update Expense'), '</h3></th>
</tr>';
} else {
echo '<tr>
<th colspan="2"><h3>', _('New Expense'), '</h3></th>
</tr>';
}
echo '<tr>
<td>', _('Date of Expense'), ':</td>
<td>
<input type="text" class="date" name="Date" size="11" required="required" maxlength="10" value="', $_POST['Date'], '" />
</td>
</tr>
<tr>
<td>', _('Expense Code'), ':</td>
<td>
<select required="required" name="SelectedExpense">';
DB_free_result($Result);
$SQL = "SELECT pcexpenses.codeexpense,
pcexpenses.description,
pctabs.defaulttag
FROM pctabexpenses, pcexpenses, pctabs
WHERE pctabexpenses.codeexpense = pcexpenses.codeexpense
AND pctabexpenses.typetabcode = pctabs.typetabcode
AND pctabs.tabcode = '" . $SelectedTabs . "'
ORDER BY pcexpenses.codeexpense ASC";
$Result = DB_query($SQL);
echo '<option value="">', _('Not Yet Selected'), '</option>';
while ($MyRow = DB_fetch_array($Result)) {
if (isset($_POST['SelectedExpense']) and $MyRow['codeexpense'] == $_POST['SelectedExpense']) {
echo '<option selected="selected" value="', $MyRow['codeexpense'], '">', $MyRow['codeexpense'], ' - ', $MyRow['description'], '</option>';
} else {
echo '<option value="', $MyRow['codeexpense'], '">', $MyRow['codeexpense'], ' - ', $MyRow['description'], '</option>';
}
$DefaultTag = $MyRow['defaulttag'];
} //end while loop
echo '</select>
</td>
</tr>';
if (!isset($_POST['Amount'])) {
$_POST['Amount'] = 0;
}
echo '<tr>
<td>', _('Gross Amount'), ':</td>
<td><input type="text" class="number" name="Amount" size="12" required="required" maxlength="11" value="', $_POST['Amount'], '" /></td>
</tr>';
if (isset($_GET['edit'])) {
$SQL = "SELECT counterindex,
pccashdetail,
calculationorder,
description,
taxauthid,
purchtaxglaccount,
taxontax,
taxrate,
amount
FROM pcashdetailtaxes
WHERE pccashdetail='" . $SelectedIndex . "'";
$TaxesResult = DB_query($SQL);
while ($MyTaxRow = DB_fetch_array($TaxesResult)) {
echo '<input type="hidden" name="index', $MyTaxRow['counterindex'], '" value="', $MyTaxRow['counterindex'], '" />';
echo '<input type="hidden" name="PcCashDetail', $MyTaxRow['counterindex'], '" value="', $MyTaxRow['pccashdetail'], '" />';
echo '<input type="hidden" name="CalculationOrder', $MyTaxRow['counterindex'], '" value="', $MyTaxRow['calculationorder'], '" />';
echo '<input type="hidden" name="Description', $MyTaxRow['counterindex'], '" value="', $MyTaxRow['description'], '" />';
echo '<input type="hidden" name="TaxAuthority', $MyTaxRow['counterindex'], '" value="', $MyTaxRow['taxauthid'], '" />';
echo '<input type="hidden" name="TaxGLAccount', $MyTaxRow['counterindex'], '" value="', $MyTaxRow['purchtaxglaccount'], '" />';
echo '<input type="hidden" name="TaxOnTax', $MyTaxRow['counterindex'], '" value="', $MyTaxRow['taxontax'], '" />';
echo '<input type="hidden" name="TaxRate', $MyTaxRow['counterindex'], '" value="', $MyTaxRow['taxrate'], '" />';
echo '<tr>
<td>', $MyTaxRow['description'], ' - ', ($MyTaxRow['taxrate'] * 100), '%</td>
<td><input type="text" class="number" size="12" name="TaxAmount', $MyTaxRow['counterindex'], '" value="', -$MyTaxRow['amount'], '" /></td>
</tr>';
}
} else {
$SQL = "SELECT taxgrouptaxes.calculationorder,
taxauthorities.description,
taxgrouptaxes.taxauthid,
taxauthorities.purchtaxglaccount,
taxgrouptaxes.taxontax,
taxauthrates.taxrate
FROM taxauthrates
INNER JOIN taxgrouptaxes
ON taxauthrates.taxauthority=taxgrouptaxes.taxauthid
INNER JOIN taxauthorities
ON taxauthrates.taxauthority=taxauthorities.taxid
INNER JOIN taxgroups
ON taxgroups.taxgroupid=taxgrouptaxes.taxgroupid
INNER JOIN pctabs
ON pctabs.taxgroupid=taxgroups.taxgroupid
WHERE taxauthrates.taxcatid = " . $_SESSION['DefaultTaxCategory'] . "
AND pctabs.tabcode='" . $SelectedTabs . "'
ORDER BY taxgrouptaxes.calculationorder";
$TaxResult = DB_query($SQL);
$i = 0;
while ($MyTaxRow = DB_fetch_array($TaxResult)) {
echo '<input type="hidden" name="index', $i, '" value="', $i, '" />';
echo '<input type="hidden" name="CalculationOrder', $i, '" value="', $MyTaxRow['calculationorder'], '" />';
echo '<input type="hidden" name="Description', $i, '" value="', $MyTaxRow['description'], '" />';
echo '<input type="hidden" name="TaxAuthority', $i, '" value="', $MyTaxRow['taxauthid'], '" />';
echo '<input type="hidden" name="TaxGLAccount', $i, '" value="', $MyTaxRow['purchtaxglaccount'], '" />';
echo '<input type="hidden" name="TaxOnTax', $i, '" value="', $MyTaxRow['taxontax'], '" />';
echo '<input type="hidden" name="TaxRate', $i, '" value="', $MyTaxRow['taxrate'], '" />';
echo '<tr>
<td>', $MyTaxRow['description'], ' - ', ($MyTaxRow['taxrate'] * 100), '%:</td>
<td><input type="text" class="number" size="12" name="TaxAmount', $i, '" value="0" /></td>
</tr>';
++$i;
}
}
//Select the tag
echo '<tr>
<td>', _('Tag'), ':</td>
<td><select name="Tag">';
$SQL = "SELECT tagref,
tagdescription
FROM tags
ORDER BY tagref";
$Result = DB_query($SQL);
if (!isset($_POST['Tag'])) {
$_POST['Tag'] = $DefaultTag;
}
echo '<option value="0">0 - ', _('None'), '</option>';
while ($MyRow = DB_fetch_array($Result)) {
if ($_POST['Tag'] == $MyRow['tagref']) {
echo '<option selected="selected" value="', $MyRow['tagref'], '">', $MyRow['tagref'], ' - ', $MyRow['tagdescription'], '</option>';
} else {
echo '<option value="', $MyRow['tagref'], '">', $MyRow['tagref'], ' - ', $MyRow['tagdescription'], '</option>';
}
}
echo '</select>
</td>
</tr>';
// End select tag
//For the accept attribute of the file element, prefix dots to the front of each supported file extension.
$ReceiptSupportedExtDotPrefix = array_map(function($ReceiptSupportedExt) {
return '.' . $ReceiptSupportedExt;
}, $ReceiptSupportedExt);
echo '<tr>
<td>', _('Attach Receipt'), ':</td>
<td>
<input type="hidden" name="MAX_FILE_SIZE" value="' . $_SESSION['MaxImageSize'] * 1024 . '" />
<input type="file" name="Receipt" id="Receipt" accept="' . implode(',', $ReceiptSupportedExtDotPrefix) . '" title="', _('Accepted file types'), ': ', implode(', ', $ReceiptSupportedExt), '" />
</td>
</tr>';
if (!isset($_POST['Purpose'])) {
$_POST['Purpose'] = '';
}
echo '<tr>
<td>', _('Business Purpose'), ':</td>
<td>
<input type="text" name="Purpose" size="50" maxlength="49" required="required" value="', $_POST['Purpose'], '" />
</td>
</tr>';
if (!isset($_POST['Notes'])) {
$_POST['Notes'] = '';
}
echo '<tr>
<td>', _('Notes'), ':</td>
<td>
<input type="text" name="Notes" size="50" maxlength="49" value="', $_POST['Notes'], '" />
</td>
</tr>';
echo '</table>'; // close main table
echo '<input type="hidden" name="SelectedTabs" value="', $SelectedTabs, '" />';
echo '<input type="hidden" name="Days" value="', $Days, '" />';
echo '<div class="centre">
<input type="submit" name="submit" value="', _('Accept'), '" />
<input type="submit" name="Cancel" value="', _('Cancel'), '" />
</div>';
echo '</form>';
} // end if user wish to delete
}
include('includes/footer.php');
?>