/var/www/(Del)pathways.org.hk/MIS20140127/old20140414/Receipt/receipt_search.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
<!DOCTYPE html>
<html>
    <head>
        <?php require_once '../include/head.php'?>
        <?php require_once '../include/checkuser.php'?>
        <?php require_once '../include/Nav_bar.php'?>
        <?php include_once '../include/DBConnect.php'?>
        <script>
            var total = 0;
            $(function() {
                $('.date-picker').datetimepicker({pickTime: false});
                $('.searchstun').select2();
                $('.stun').select2();
                $('.searchsch').select2();
                $('.sch').select2();
                $('#payway').change(function() {
                    var type = $('#payway').val();
                    $.get('receipt_search_ajax.php?paytype=' + type, ff);
                });
                $('#searchtype').change(function() {
                    var searchtype = $('#searchtype').val();
                    $.get('receipt_search_type_ajax.php?searchtype=' + searchtype, stff);
                });
                function stff(res) {
                    $('#searchtype_div').html(res);
                }
                ;
                function ff(res) {
                    $('#paytype_tr').html(res);
                }
                ;
            });
            $(document).ready(function() {
                $(function() {
                    $('#form').on('submit', function(e) {
                        var array = new Array();
                        alert((document.getElementsByName("selected_checked[]")).length);
                        for (var i = 0; i < (document.getElementsByName("selected_checked[]")).length; i++) {
                            if (document.getElementsByName("selected_checked[]")[i].checked == true) {
                                array.push((document.getElementsByName("selected_checked[]")[i].value));
                                total += parseInt((document.getElementsByName("invoice_pay[]")[i].value));
                            }
                        }
                        if (total != $('#total_fee').val()) {
                            alert('Not match the total amount');
                            total = 0;
                            e.preventDefault();
                        }
                        var array2 = new Array();
                        for (var i = 0; i < (document.getElementsByName("selected_checked[]")).length; i++) {
                            if (document.getElementsByName("selected_checked[]")[i].checked == true) {
                                array2.push((document.getElementsByName("selected_checked[]")[i].value));
                                if ((parseInt((document.getElementsByName("invoice_payed[]")[i].value)) + parseInt((document.getElementsByName("invoice_pay[]")[i].value))) > parseInt((document.getElementsByName("invoice_total_amount[]")[i].value))) {
                                    alert('invoice pay larger then amount');
                                    e.preventDefault();
                                }
                                ;
                            }
                        }
                    });
                });
            });
        </script>
        <style>
            .div-background {
                border: 1px solid #E6E6E6;
                border-radius: 6px 6px 6px 6px;
                background-color: #EEEFDE;
                padding-top: 10px;
                padding-bottom: 10px;
            }
        </style>
    </head>
    <?php
    $invoice_id
;
    
$startdate date('Y-m-d');
    
$enddate date('Y-m-d');
    
$today date('Y-m-d');
    
$name;
    if (isset(
$_POST['save'])) {
        
$root_id;
        
$totalmoney 0;
        
$countinvoice 0;
        
$name $_POST['from_name'];
        
$money $_POST['total_fee'];
        
$payof '';
        foreach (
$_POST['selected_checked'] as $selected_check) {
            
$procode;
            
$subcode;
            
$proid;
            
$subid;
            
$event_code $_POST['selected_event'][$selected_check];
            
$searchevent "select program_id , subject_id from event where event_code = '$event_code' and deleted = 0";
            
$result $dbh->query($searchevent);
            
$result->setFetchMode(PDO::FETCH_OBJ);
            while (
$row $result->fetch()) {
                
$proid $row->program_id;
                
$subid $row->subject_id;
            }
            
$searchpro "select program_name from program where program_id = '$proid' and deleted = 0";
            
$result $dbh->query($searchpro);
            
$result->setFetchMode(PDO::FETCH_OBJ);
            while (
$row $result->fetch()) {
                
$procode $row->program_name;
            }
            
$searchsub "select sub_name from subject where subject_id = '$subid' and deleted = 0";
            
$result $dbh->query($searchsub);
            
$result->setFetchMode(PDO::FETCH_OBJ);
            while (
$row $result->fetch()) {
                
$subcode $row->sub_name;
            }

            
$payof .= $procode ' ' $subcode ' ' $_POST['payof'][$selected_check] . ' ' $_POST['selected_invoicedate'] . ' ';
        };
        
$payway $_POST['payway'];
        if (
$payway == 1) {
            
$bank_name $_POST['bankname'];
            
$cheque_no $_POST['cheque_no'];
        } else {
            
$bank_name '0';
            
$cheque_no '0';
        }
        
$campus $_POST['campus'];
        
$type $_POST['type'];
        
$paydate $_POST['paydate'];
        
$dontinsert false;
        
$loadname;
        if (
$campus != && $campus != 2) {
            
$dontinsert true;
            echo 
"<script>alert('plz select a campus!!!')</script>";
        }
        
$receiptNo '';
        if (
$campus == 1) {
            
$receiptNo .= 'ST';
        } else if (
$campus == 2) {
            
$receiptNo .= 'KL';
        };
        
$curr_yr date("Y");
        
$curr_mon date("m");
        if (
$curr_mon 9)
            
$curr_yr--;
        
$receiptNo .= substr($curr_yr2);
        
$countnum;
        
$searchnum "select count(receipt_id) as countnum from receipt";
        
$result $dbh->query($searchnum);
        
$result->setFetchMode(PDO::FETCH_OBJ);
        while (
$row $result->fetch()) {
            
$countnum $row->countnum;
        }
        if (
$countnum 10) {
            
$receiptNo .= '000' $countnum;
        } else if (
$countnum 100) {
            
$receiptNo .= '00' $countnum;
        } else if (
$countnum 1000) {
            
$receiptNo .= '0' $countnum;
        } else if (
$countnum 10000) {
            
$receiptNo .= $countnum;
        } else {
            echo 
"<script>alert('this campus's receipt num is full in this year')</script>";
        }
        if (
$dontinsert == false) {
            
$sth $dbh->prepare("INSERT INTO `receipt`(`receipt_code`, `createby`, `createdate`, `lastupby`, `lastupdate`, `actived`, `deleted`, `receipt_date`, `receipt_from`, `payment_of`, `total_amount`, `is_cash`, `bank_name`, `cheque_code`, `is_normal`, `campus_id`) VALUES (?,1,now(),1,now(),1,0,?,?,?,?,?,?,?,?,?)");
            
$sth->bindParam(1$receiptNo);
            
$sth->bindParam(2$paydate);
            
$sth->bindParam(3$name);
            
$sth->bindParam(4$payof);
            
$sth->bindParam(5$money);
            
$sth->bindParam(6$payway);
            
$sth->bindParam(7$bank_name);
            
$sth->bindParam(8$cheque_no);
            
$sth->bindParam(9$type);
            
$sth->bindParam(10$campus);
            
$sth->execute();

            
$result $dbh->query("select max(receipt_id) as root_id from receipt where deleted = 0");
            
$result->setFetchMode(PDO::FETCH_OBJ);
            while (
$row $result->fetch()) {
                
$root_id $row->root_id;
            }

            
$countinvoice 0;
            foreach (
$_POST['selected_checked'] as $selectedcheck) {
                
$selectedinvoice $_POST['selected_invoice'][$selectedcheck];
                
$sth $dbh->prepare("INSERT INTO `invoice_receipt`(`invoice_id`, `createby`, `createdate`, `lastupby`, `lastupdate`, `actived`, `deleted`, `receipt_id`, `amount_paid`) VALUES (?,1,now(),1,now(),1,0,?,?)");
                
$sth->bindParam(1$selectedinvoice);
                
$sth->bindParam(2$root_id);
                
$sth->bindParam(3$_POST['invoice_pay'][$selectedcheck]);
                
$sth->execute();
            };

            echo 
"<script>alert('Insert data successful.')</script>";
            echo(
"<script>location.href ='receipt_index.php';</script>");
        }
    }
    
?>
    <body>
        <!-- line between nav bar and content -->
        <div class="text-right">
            <ul class="breadcrumb">
                <li class="active">Receipt</li>
            </ul>
        </div>

        <div class="container-fluid pathways-container">
            <div>
                <table border="0" width="100%">
                    <tr>
                        <td><h2>Add Receipt</h2></td>
                    </tr>
                </table>
            </div>
            <div class="pathways-search">
                <form id="search_form" class="form-inline" action="" method="POST">
                    <table>
                        <tr>
                            <td>
                                <div class="pathways-inline-block">
                                    <select name="searchtype" id="searchtype">
                                        <option></option>
                                        <option <?php
                                        
if (isset($_POST['go'])) {
                                            if (
$_POST['searchtype'] == 1) {
                                                
?> selected="" <?php
                                                
}
                                            }
                                            
?> value="1">in-house</option>
                                        <option <?php
                                        
if (isset($_POST['go'])) {
                                            if (
$_POST['searchtype'] == 2) {
                                                
?> selected="" <?php
                                                
}
                                            }
                                            
?> value="2">in-school</option>
                                    </select>
                                </div>
                            </td>
                            <td>
                                <div id="searchtype_div">
                                    <?php
                                    
if (isset($_POST['go'])) {
                                        if (isset(
$_POST['searchstun'])) {
                                            
?> <div class="pathways-inline-block">
                                                <label>Student</label>
                                                <select name="searchstun" id="stun" class="stun" style="width:230px">                            
                                                    <option value="ALL"></option>
                                                    <?php $sth $dbh->query("SELECT `root_id` root_id,linking_id,`New_stu_ID` id,CONCAT(last_name, ' ', first_name) studentname,`ch_name`FROM `student` WHERE `actived`=1 and`deleted`=0 group by `New_stu_ID`,`Sem_yr`"); ?>
                                                    <?php while ($ResultSet $sth->fetch(PDO::FETCH_ASSOC)) { ?> 
                                                        <option <?php
                                                        
if (isset($_POST['go'])) {
                                                            if (
$_POST['searchstun'] == $ResultSet['root_id']) {
                                                                
?> selected <?php
                                                                
}
                                                            }
                                                            
?>value="<?= $ResultSet['root_id']; ?>"><?= $ResultSet['studentname'?> | <?= $ResultSet['id'?></option>
                                                        <?php ?>
                                                </select>
                                            </div><?php
                                        
}
                                        if (isset(
$_POST['searchsch'])) {
                                            
?>
                                            <div class = "pathways-inline-block">
                                                <label>School</label>
                                                <select name = "searchsch" id = "sch" class = "sch" style = "width:400px">
                                                    <option value = "ALL"></option>
                                                    <?php $sth $dbh->query("SELECT `root_id` root_id, `school_id`, `sch_name` schoolname,`sch_ch_name` school_ch_name FROM `school` WHERE `actived`=1 and`deleted`=0 group by `school_id`");
                                                    
?>
                                                    <?php while ($ResultSet $sth->fetch(PDO::FETCH_ASSOC)) { ?> 
                                                        <option <?php
                                                        
if (isset($_POST['go'])) {
                                                            if (
$_POST['searchsch'] == $ResultSet['root_id']) {
                                                                
?> selected="" <?php
                                                                
}
                                                            }
                                                            
?>value="<?= $ResultSet['root_id']; ?>"><?= $ResultSet['schoolname'?> | <?= $ResultSet['school_ch_name'?></option>
                                                        <?php ?>
                                                </select>
                                            </div>
                                            <?php
                                        
}
                                    }
                                    
?>
                                </div>
                            </td>
                            <td>
                                <div class="pathways-inline-block">
                                    <button class="btn" name="go" value="go" style="margin-left:30px" type="submit">Search</button>
                                </div>
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
            <?php
            
if (isset($_POST['go'])) {
                if (isset(
$_POST['searchstun']) || isset($_POST['searchsch'])) {
                    
$studentid;
                    
$schoolid;
                    
$countinvoice 0;

                    if (isset(
$_POST['searchstun']) && $_POST['searchstun'] == 'ALL') {
                        echo 
"<script>alert('Please select a student')</script>";
                    } else
                    if (isset(
$_POST['searchsch']) && $_POST['searchsch'] == 'ALL') {
                        echo 
"<script>alert('Please select a school')</script>";
                    } else {
                        
$query;
                        
$Tdata false;
                        if (isset(
$_POST['searchstun'])) {
                            
$studentid $_POST['searchstun'];
                            
$schoolid 'ALL';
                        } else {
                            
$schoolid $_POST['searchsch'];
                            
$studentid 'ALL';
                        }
                        if (
$schoolid == 'ALL')
                            
$query "select root_id from invoice where deleted = 0 and stu_linking_id = '$studentid'  ";
                        if (
$studentid == 'ALL')
                            
$query "select root_id from invoice where deleted = 0 and school_id = '$schoolid'  ";
                        
$result $dbh->query($query);
                        
$result->setFetchMode(PDO::FETCH_OBJ);
                        while (
$row $result->fetch()) {
                            
$Tdata true;
                        }
                        
?>
                        <form id="form" class="form-inline" action="" method="POST">
                            <?php if ($Tdata == true) { ?>
                                <?php
                                
if ($schoolid == 'ALL')
                                    
$sth $dbh->query("SELECT CONCAT(last_name, ' ', first_name) name,`ch_name`FROM `student` WHERE `actived`=1 and`deleted`=0 and `root_id` = $studentid group by `New_stu_ID`,`Sem_yr`");
                                if (
$studentid == 'ALL')
                                    
$sth $dbh->query("SELECT `sch_name` name,`sch_ch_name` school_ch_name FROM `school` WHERE `actived`=1 and`deleted`=0 and `root_id` = $schoolid group by `school_id`");
                                
?>
                                <?php while ($ResultSet $sth->fetch(PDO::FETCH_ASSOC)) { ?> 
                                    <input type='hidden' name='from_name' value='<?= $ResultSet['name'?>'>
                                <?php ?>
                                <div class="container-fluid div-background">
                                    <table>
                                        <tr>
                                            <td width="20%">Create By</td>
                                            <td><input type='text' readonly="" value="<?= $_GET '' $_SESSION['MIS_login']['username']; ?>"></td>
                                        </tr>
                                        <tr style="height: 15px"></tr>
                                        <tr>
                                            <td width="20%">Campus:</td>
                                            <td><select name="campus" required="true">
                                                    <option></option>
                                                    <?php
                                                    $query 
"Select campus_id,campus_name from campus";
                                                    
$result $dbh->query($query);
                                                    
$result->setFetchMode(PDO::FETCH_OBJ);
                                                    while (
$row $result->fetch()) {
                                                        print 
'<option value=' $row->campus_id '>' $row->campus_name '</option>';
                                                    }
                                                    
?>
                                                </select>
                                            </td>
                                            <td width="5%"></td>
                                            <!--td><label>Receipt From</label></td>
                                            <td><input type="text" name="from_name" required=""></td-->
                                            <td><label>Total Money</label>
                                            </td><td><input type="text" name="total_fee" id="total_fee" required=""></td>
                                        </tr>
                                        <tr style="height: 15px"></tr>
                                        <tr>
                                            <td width="20%"><label>Type</label></td>
                                            <td><select name="type">
                                                    <option value="0">Normal</option>
                                                    <option value="1">Community Chest</option>
                                                </select></td>
                                            <td width="5%"></td>
                                            <td width="20%"><label>Date</label></td>
                                            <td><div class="input-append date-picker">                
                                                    <input id="start_date" class="dateISO" type="text" maxlength="200" data-format="yyyy-MM-dd" name="paydate" value="<?= $today ?>" style="width:90px" required>              
                                                    <span class="add-on">
                                                        <i class="icon-calendar" data-date-icon="icon-calendar" data-time-icon="icon-time"></i>
                                                    </span>
                                                </div></td>
                                        </tr>
                                        <tr style="height: 15px"></tr>
                                        <tr>
                                            <td  width="20%">Pay Type</td>
                                            <td><select name ="payway" id="payway">
                                                    <option value="0">By cash</option>
                                                    <option value="1">By cheque</option>
                                                </select></td>
                                            <!--td  width="10%"><label>Bank Name</label></td>
                                            <td><input type="text" name="bankname"></td>
                                            <td width="10%">Cheque No</td>
                                            <td><input type="text" name="cheque_no"></td-->
                                        </tr>
                                        <tr style="height: 15px"></tr>
                                        <tbody id="paytype_tr">
                                            <tr>
                                            </tr>
                                        </tbody>
                                        <tr>
                                            <!--td width="20%"><label>Payment of:</label></td>
                                            <td><input type="text" name="payof"></td-->

                                        </tr>
                                    </table>
                                </div>
                                <table><tr style="height: 15px"></tr></table>
                            <?php ?>
                            <table class="table table-striped table-bordered table-hover table-condensed" id="tablehead">      
                                <thead>
                                    <tr>
                                        <th style="min-width:50px; width:5%"></th>
                                        <th style="min-width:50px;">Invoice_Code</th>
                                        <th style="min-width:50px;">Event_Code</th>
                                        <th style="min-width:50px; width:10%">Total Amount</th>
                                        <th style="min-width:50px; width:10%">Paid</th>
                                        <th style="min-width:50px; width:10%">Pay</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $nopayed 
true;
                                    
$payedsum 0;
                                    
$allInvoicePayed true;
                                    
//$query = "select receive_id, receive_code, is_normal from receive where deleted = 0 and invoice_id = 0 and receive_date between '$startdate' and '$enddate' ";
                                    
if ($schoolid == 'ALL')
                                        
$query "select root_id, total_amount, invoice_code, event_code, invoice_date from invoice where deleted = 0 and stu_linking_id = '$studentid' ";
                                    if (
$studentid == 'ALL')
                                        
$query "select root_id, total_amount, invoice_code, event_code, invoice_date from invoice where deleted = 0 and school_id = '$schoolid'  ";
                                    
$result $dbh->query($query);
                                    
$result->setFetchMode(PDO::FETCH_OBJ);
                                    while (
$row $result->fetch()) {
                                        
$payed $dbh->query("select sum(amount_paid) as payed from invoice_receipt where deleted = 0 and invoice_id = '$row->root_id' group by invoice_id");
                                        
$payed->setFetchMode(PDO::FETCH_OBJ);
                                        while (
$rowpayed $payed->fetch()) {
                                            
$nopayed false;
                                            
$payedsum $rowpayed->payed;
                                        }
                                        if (
$payedsum $row->total_amount) {
                                            
$allInvoicePayed false;
                                            
?>

                                            <tr>
                                                <td>                                    
                                                    <input type="checkbox" name="selected_checked[]" id="selected_checked<?= $countinvoice ?>" value="<?= $countinvoice ?>">
                                                    <input type="hidden" name="selected_invoice[]" value="<?= $row->root_id ?>">
                                                    <input type="hidden" name="selected_event[]" value="<?= $row->event_code ?>">
                                                    <input type="hidden" name="selected_invoicedate" value="<?= $row->invoice_date ?>">
                                                </td>
                                                <td>
                                                    <input type="hidden" name="payof[]" value="<?= $row->invoice_code ?>" ><h5><?= $row->invoice_code ?></h5>
                                                </td> 
                                                <td>
                                                    <h5><?= $row->event_code ?></h5>
                                                </td>
                                                <td>
                                                    <h5>$<?= $row->total_amount ?></h5>
                                                    <input type="hidden" name="invoice_total_amount[]" value="<?= $row->total_amount ?>" id="invoive_total_amount<?= $countinvoice ?>">
                                                </td>
                                                <td>
                                                    <?php
                                                    
if ($nopayed == false) {
                                                        
?>
                                                        <h5><?= $payedsum ?></h5>
                                                        <input type="hidden" name="invoice_payed[]" value="<?= $payedsum ?>" id="invoive_payed<?= $countinvoice ?>">
                                                        <?php
                                                    
}
                                                    if (
$nopayed == true) {
                                                        
?>
                                                        <h5>0.00</h5>
                                                        <input type="hidden" name="invoice_payed[]" value="0" id="invoive_payed<?= $countinvoice ?>">
                                                    <?php ?>
                                                </td>
                                                <td>
                                                    <input type="text" name="invoice_pay[]" id="invoive_pay<?= $countinvoice ?>" value="" style="width: 100px">
                                                </td>
                                            </tr>
                                            <?php
                                            $countinvoice
++;
                                        }
                                    }
                                    if (
$allInvoicePayed == TRUE) {
                                        
?>
                                        <tr><td colspan = "5"><h5>All Invoice Payed</h5></td></tr>
                                    <?php
                                    
}
                                    if (
$Tdata == FALSE) {
                                        
?>
                                        <tr><td colspan = "5"><h5>No invoice searched</h5></td></tr>
            <?php ?>
                                </tbody>
                            </table>
                            <div class="pathways-inline-block">
                                <a href="receipt_index.php" class="btn">Back</a>
                            </div>
                            <div class="pathways-inline-block">
                                <button class="btn" name="save" value="save" type="submit" style="margin-left:30px" >Save</button>
                            </div>
                        </form>
                        <?php
                    
}
                } else {
                    echo 
"<script>alert('Please select a search type!')</script>";
                }
            }
            
?>
        </div> <!-- /container -->
    </body>
</html>