/var/www/(Del)pathways.org.hk/MIS20140127/old20140127/student_invoice/Generate_invoice.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
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <?php
        
require_once '../include/head.php';
        require_once 
'../include/checkuser.php';
        require_once 
'../include/Nav_bar.php';
        include_once 
'../include/DBConnect.php';
        
?>
        <script>
            function check_all(obj,cName){
                var checkboxs = document.getElementsByName(cName);
                for(var i=0;i<checkboxs.length;i++){
                    checkboxs[i].checked = obj.checked;
                }
            };
            $(function(){
                $('.date-picker').datetimepicker({ pickTime: false,format: 'yyyy-MM-dd'});
            });
        </script>
    </head>
    <body>
        <!-- line between nav bar and content -->
        <div class="text-right">
            <ul class="breadcrumb">
                <li class="active"></li>
            </ul>
        </div>
        <!-- line between nav bar and content -->
        <div class="container-fluid pathways-container">
            <form action="" method="POST">
            <table border="0">
                <tr>
                    <td colspan="5"><h2><b>Generate Invoice</b></h2></td>
                </tr>
                <tr>
                    <td>Date</td>
                    <td>
                        <div class="input-append date-picker">
                                <input type="text" name="from_date" class="dateISO" required="true">
                                <span class="add-on">
                                    <i class="icon-calendar" data-date-icon="icon-calendar" data-time-icon="icon-time"></i>
                                </span>
                        </div>
                    </td>
                    <td>To</td>
                    <td>
                        <div class="input-append date-picker">
                                <input type="text" name="to_date" class="dateISO" required="true">
                                <span class="add-on">
                                    <i class="icon-calendar" data-date-icon="icon-calendar" data-time-icon="icon-time"></i>
                                </span>
                        </div>
                    </td>
                    <td><input type="submit" name="gene" class="btn" value="Generate"></td>
                </tr>
            </table>
            <table border="1" class="table table-striped table-bordered table-hover table-condensed">
                <thead>
                    <tr>
                        <th>Edit</th>
                        <th>Name</th>
                        <th>Chinese Name</th>
                        <th>Year/Grade</th>
                        <th>Emergency Contact Person</th>
                        <th>Emergency Contact No. </th>
                    </tr>
                </thead>
                <?php
                
if(isset($_POST['gene'])&&$_POST['gene']=="Generate"){
                    
$from_date=$_POST['from_date'];
                    
$to_date=$_POST['to_date'];
                    
$total_percent;
                    
$query="Select e.event_id,ss.subsidy_id,s.subsidy_type,s.percent,l.start_time,l.end_time,l.date,stu.first_name,stu.last_name,stu.linking_id,stu.root_id,
                        e.programme_fee_type,e.primary_individual_price,e.primary_group_price,e.secondary_individual_price,e.secondary_group_price,
                        e.package_fee,e.package_discount,e.is_in_school,g.grade from event as e, lesson_attendance as la,student_grade as sg,grade as g,
                            student as stu,student_subsidy as ss,subsidy as s,lesson as l,event_lesson as el where e.event_id=el.event_id and
                            el.lesson_id=l.lesson_id and l.lesson_id=la.lesson_id and l.date>='
$from_date' and l.date<='$to_date' and 
                            stu.linking_id=ss.stu_subsidy_id and ss.subsidy_id=s.Subsidy_ID and sg.student_grade_id=stu.linking_id and sg.grade_id=g.grade_id
                            and e.deleted=0 and e.actived=1 and la.deleted=0 and la.actived=1 and stu.deleted=0 and stu.actived=1
                            and ss.deleted=0 and ss.actived=1 and s.deleted=0 and s.actived=1 and l.deleted=0 and l.actived=1 
                            and el.deleted=0 and el.actived=1 group by ss.stu_subsidy_id"
;
                    
$result=$dbh->query($query);//get invoice data
                    
if ($result) {
                        
$result->setFetchMode(PDO::FETCH_OBJ);
                        
                        
$query1 "Select ss.stu_subsidy_id,Sum(s.percent) from student_subsidy as ss, subsidy as s where ss.subsidy_id = s.subsidy_id and ss.deleted=0 and s.deleted=0
                            group by ss.stu_subsidy_id"
;
                        
$result1=$dbh->query($query1);//get percent
                        
$result1->setFetchMode(PDO::FETCH_OBJ);
                        
                        while (
$row $result->fetch()){
                            while(
$row1=$result1->fetch()){
                                if(
$row->linking_id==$row1->stu_subsidy_id){
                                    
$percent=$row1->percent;
                                }
                            }
                            
$query2 "Insert into invoice set createby=0,createdate=Now(),lastupby=0,lastupdate=Now(),actived=1,deleted=0,
                                   stu_linking_id='
$row->root_id',event_id='$row->event_id',remarks='',subsidy_type='$row->subsidy_type'";
                            
$result2 $dbh->query($query2);
                            
$query4="Select Max(invoice_id) from invoice";
                            
$result4=$dbh->query($query4);
                            
$result4->setFetchMode(PDO::FETCH_OBJ);
                            
$row4=$result4->fetch();
                            
$query5="Select count(*) as no_of_stu from lesson_attendance where lesson_id IN 
                                    (Select lesson_id from lesson_attendance where stu_linking_id='
$row->root_id')";
                            
$result5=$dbh->query($query5);
                            
$result5->setFetchMode(PDO::FETCH_OBJ);
                            
$row5=$result5->fetch();
                            
$price=0;
                            
                            if(
$row->programme_fee_type==1){//1=hour , 2=package
                                
if($row5->no_of_stu>1){//group hr
                                    
switch($row->grade){
                                        case 
P1:
                                        case 
P2:
                                        case 
P3:
                                        case 
P4:
                                        case 
P5:
                                        case 
P6:
                                            
$price+=$row->primary_group_price;
                                            break;
                                        case 
F1:
                                        case 
F2:
                                        case 
F3:
                                            
$price+=$row->secondary_group_price;
                                            break;
                                    }
                                }
                                else{
                                    switch(
$row->grade){//induva hr
                                        
case P1:
                                        case 
P2:
                                        case 
P3:
                                        case 
P4:
                                        case 
P5:
                                        case 
P6:
                                            
$price+=$row->primary_individual_price;
                                            break;
                                        case 
F1:
                                        case 
F2:
                                        case 
F3:
                                            
$price+=$row->secondary_individual_price;
                                            break;
                                    }
                                    
$total=($price*$percent/100);
                                    
$discount=$price-($total);
                                }
                                
$query3 "Insert into invoice_inline set createby=0,createdate=Now(),lastupby,0,lastupdate=Now(),actived=1,
                                deleted=0,invoice_id='
$row4->invoice_id',student_linking_id='$row->root_id',event_programme_fee_type='$row->programme_fee_type',
                                     amount='
$total',original_fee='$price',discounted_fee='$discount',hour='($row->end_time-$row->start_time)',subsidy_percent='$percent',remarks=''";
                            }
                            else{
//package
                                
$total=$row->package_fee-$row->package_discount;
                                
$query3 "Insert into invoice_inline set createby=0,createdate=Now(),lastupby,0,lastupdate=Now(),actived=1,
                                    deleted=0,invoice_id='
$row4->invoice_id',student_linking_id='$row->linking_id',event_programme_fee_type='$row->programme_fee_type',
                                        amount='
$total',original_fee='$row->package_fee',discounted_fee='$row->package_discount',hour='',subsidy_percent='$percent',remarks=''";
                            }
                            
$result3 $dbh->query($query3);
                            
$result3->setFetchMode(PDO::FETCH_OBJ);
                            
$percent=0;
                        }
                    }
                }
                
?>
            </table>   
            </form>
        </div>
    </body>
</html>