/var/www/(Del)pathways.org.hk/MIS20140127/old20140314/Payroll/PayRoll_Sql.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
<?php

include_once '../include/DBConnect.php';

// ### PayRoll Search Date Range ### //
function Search_daterange($start$end) {
    global 
$dbh;
    
$payroll_Search_daterange "SELECT concat( last_name, ' ', first_name ) staffname, ch_name, staff_code,root_id  FROM lesson_attendance la,staff s 
                            WHERE la.actived = 1 and la.deleted = 0 and s.actived = 1 and s.deleted = 0 and payroll_gened = 0 and la.date <= ? and la.date >= ?
                            and s.root_id = la.staff_id group by la.staff_id"
;
    
$sth $dbh->prepare($payroll_Search_daterange);
    
$sth->bindParam(1$end);
    
$sth->bindParam(2$start);
    
$sth->execute();
    return 
$sth;
}

// ### PayRoll Generate page show the info of staff ### //
function Payroll_staff_rootid($root_id) {
    global 
$dbh;
    
$payroll_staff_rootid_sql "SELECT staff_code,concat(last_name, ' ',first_name,' ( ',ch_name,' ) ') name  FROM `staff` 
        WHERE actived = 1 and deleted = 0 and root_id = ?"
;
    
$sth $dbh->prepare($payroll_staff_rootid_sql);
    
$sth->bindParam(1$root_id);
    
$sth->execute();
    return 
$sth;
}

// ### PayRoll Generate page search lesson use staff info and date range ### //
function Payroll_lesson_rootid($root_id$start$end) {
    global 
$dbh;
    
$payroll_lesson_date_rootid_sql "SELECT la.date,e.event_code,l.start_time,l.end_time,la.attendance,
        la.rate_type,la.is_charge,la.is_substitute,l.lesson_id"
;
    return 
Sub_function($dbh$payroll_lesson_date_rootid_sql$root_id$start$end"");
}

// ### Find date range ### //
function Find_dateRange($root_id$start$end) {
    global 
$dbh;
    
$Find_dateRange "SELECT Max(l.date) Maxd, Min(l.date) Mind  ";
    
$step "group by e.event_id";
    
$sth Sub_function($dbh$Find_dateRange$root_id$start$end$step);
    while (
$result $sth->fetch(PDO::FETCH_ASSOC)) {
        
$MAXD $result['maxd'];
        
$MIND $result['mind'];
    }
    return array(
$MAXD$MIND);
}

// ### Sub-function of above two function ### //
function Sub_function($dbh$sql$root_id$start$end$step) {
    
$Sub_function_sql $sql " FROM lesson_attendance la,event e,lesson l
             WHERE 
             la.actived = 1 and la.deleted = 0 and
             e.actived = 1 and e.deleted = 0 and
             l.actived = 1 and l.deleted = 0
             and
             la.date <= ? and la.date >= ? and la.staff_id = ?
             and
             la.lesson_id = l.lesson_id and e.event_id in (
             select event_id from event_lesson where lesson_id = l.lesson_id
             ) 
$step order by 1,2";

    
$sth $dbh->prepare($Sub_function_sql);
    
$sth->bindParam(1$end);
    
$sth->bindParam(2$start);
    
$sth->bindParam(3$root_id);
    
$sth->execute();
    return 
$sth;
}

// ### show rate text ### //
function Rate_display($rate_type$is_substitute) {
    switch (
$rate_type) {
        case 
0:
            if (
$is_substitute == 1)
                return 
"Monthly ( Substitute ) ";
            else
                return 
"Monthly ";
            break;
        case 
1:
            if (
$is_substitute == 1)
                return 
'Teacher ( In House ) ( Substitute ) ';
            else
                return 
'Teacher ( In House )';
            break;
        case 
2:
            if (
$is_substitute == 1)
                return 
'Teacher ( Outside School ) ( Substitute )';
            else
                return 
'Teacher ( Outside School ) ';
            break;
        case 
3:
            if (
$is_substitute == 1)
                return
'Teacher Assistant ( In House ) ( Substitute )';
            else
                return
'Teacher Assistant ( In House ) ';
            break;
        case 
4:
            if (
$is_substitute == 1)
                return
'Teacher Assistant ( Outside School ) ( Substitute )';
            else
                return
'Teacher Assistant ( Outside School ) ';
            break;
        case 
5:
            if (
$is_substitute == 1)
                return
'Traveling ( Substitute )';
            else
                return
'Traveling ';
            break;
        case 
6:
            if (
$is_substitute == 1)
                return
'Other ( Substitute )';
            else
                return
'Other';
            break;
    }
}

// ### Search The salary of staff by staff root id ### //
function Salary_rootid($root_id) {
    global 
$dbh;
    
$salary_rootid 'SELECT salary, rate1, rate2, rate3, rate4, rate5, rate6 FROM `staff_salary_history` 
        WHERE actived = 1 and deleted = 0 and staff_id = ? and version = 
        (select max(version) from staff_salary_history where staff_id = ? group by staff_id ORDER BY version DESC LIMIT 1)'
;
    
$sth $dbh->prepare($salary_rootid);
    
$sth->bindParam(1$root_id);
    
$sth->bindParam(2$root_id);
    
$sth->execute();
    return 
$sth;
}

// ### Calculate salary  use "second" to cal ### //
function Cal_Salary($salary$rate$hour) {
    return (
Find_Ratetype($salary$rate) * $hour);
}

function 
Find_Ratetype($salary$rate) {
    return 
$salary[$rate];
}

// ### Insert Payroll ### //
function Insert_Payroll($staff_root_id$create_payroll_date$total_amount$start_date$end_date) {
    global 
$dbh;
    
$code find_Payroll_code();
    
$insert_Payroll "INSERT INTO `payroll`(`payroll_code`, `createby`, `createdate`, `lastupby`, `lastupdate`, `actived`, `deleted`, `staff_id`, `payroll_date`, `total_amount`, `is_paid`, `remarks`, `start_date`, `end_date`)
        VALUES(?,1,now(),1,now(),1,0,?,?,?,0,0,?,?)"
;
    
$sth $dbh->prepare($insert_Payroll);
    
$sth->bindParam(1$code);
    
$sth->bindParam(2$staff_root_id);
    
$sth->bindParam(3$create_payroll_date);
    
$sth->bindParam(4$total_amount);
    
$sth->bindParam(5$start_date);
    
$sth->bindParam(6$end_date);
    
$sth->execute();
}

// ### find Payroll code ### //
function find_Payroll_code() {
    global 
$dbh;
    
$find_code_sql "SELECT `payroll_code` FROM `payroll` WHERE createdate in( select Max(createdate) from payroll)";
    
$sth $dbh->query($find_code_sql);
    
$Old_code $sth->fetch(PDO::FETCH_ASSOC)['payroll_code'];
    
$New_code = ((int) str_replace("PR""00"$Old_code)) + 1;
    return (
$New_code 10) ? "PR00$New_code: ($New_code 100 && $New_code >= 10 "PR0$New_code"PR$New_code");
}

// ### select payroll ID ### //
function Select_Payroll_ID($staff_root_id$create_payroll_date$total_amount$start_date$end_date) {
    global 
$dbh;
    
$is_paid 0;
    
$remarks 0;
    
$Select_Payroll_ID "SELECT payroll_id FROM `payroll` WHERE actived = 1 and deleted = 0 and 
        staff_id = ? and payroll_date = ? and total_amount = ? and is_paid = ? and remarks = ? and start_date = ? and end_date = ? "
;
    
$sth $dbh->prepare($Select_Payroll_ID);
    
$sth->bindParam(1$staff_root_id);
    
$sth->bindParam(2$create_payroll_date);
    
$sth->bindParam(3$total_amount);
    
$sth->bindParam(4$is_paid);
    
$sth->bindParam(5$remarks);
    
$sth->bindParam(6$start_date);
    
$sth->bindParam(7$end_date);
    
$sth->execute();
    
//var_dump($staff_root_id." ".$create_payroll_date." ".$total_amount." ".$is_paid." ".$remarks);
    
$Result $sth->fetch(PDO::FETCH_ASSOC);
    return 
$Result['payroll_id'];
}

// ### insert payroll line ### //
function Insert_Payrollline($payroll_id$rate_type$rate$hour$amount) {
    global 
$dbh;
    
$Insert_Payrollline "INSERT INTO `payroll_line`(`createby`, `createdate`, `lastupby`, `lastupdate`, `actived`, `deleted`, 
        `payroll_id`, `rate_type`, `rate`, `hour`, `amount`, `remarks`) 
        VALUES (1,now(),1,now(),1,0,?,?,?,?,?,0);"
;
    
$sth $dbh->prepare($Insert_Payrollline);
    
$sth->bindParam(1$payroll_id);
    
$sth->bindParam(2$rate_type);
    
$sth->bindParam(3$rate);
    
$sth->bindParam(4$hour);
    
$sth->bindParam(5$amount);
    
$sth->execute();
}

// ### update payroll_gen = 1 ### //
function Change_lessonattendance_payrollgenerate($staff_id$lesson_id$gened) {
    global 
$dbh;
    
$change_lessonattendance_payrollgenerate "UPDATE `lesson_attendance` SET `lastupdate`=now() ,`payroll_gened`=? 
        WHERE actived = 1 and deleted = 0 and staff_id = ? and lesson_id = ?;"
;
    
$sth $dbh->prepare($change_lessonattendance_payrollgenerate);
    
$sth->bindParam(2$staff_id);
    
$sth->bindParam(3$lesson_id);
    
$sth->bindParam(1$gened);
    
$sth->execute();
}

// ### Seaerch for staff ### //
function Search_Staff_Name() {
    global 
$dbh;
    
$search_staff_name "SELECT concat(last_name,' ',first_name,' ( ',ch_name,' ) ') name,root_id FROM `staff` WHERE actived = 1 and deleted = 0";
    return 
$sth $dbh->query($search_staff_name);
    
//var_dump($sth->fetch());
}

// ### staffid/startdate/enddate find the payroll ### //
function Find_Payrollby_date_staffid($staff_name$start_date$end_date) {
    global 
$dbh;
    
$staff_id_sql = (empty($staff_name)) ? "" " and p.staff_id = $staff_name ";
    
$daterange_sql = (!empty($start_date) && !empty($end_date)) ? "and ('$start_date' <= start_date and '$end_date' >= start_date or '$end_date' >= end_date and '$start_date' <= end_date or '$start_date' <= end_date and '$end_date' >= start_date)" "";
    
$Find_Payrollby_date_staffid "SELECT `payroll_id`,`payroll_code`,`payroll_date`,`total_amount`,concat(last_name,' ',first_name,' ( ',ch_name,' ) ') name,start_date,end_date,s.root_id 
        FROM `payroll` p,`staff` s 
        Where p.actived = 1 and p.deleted = 0 
        and s.actived = 1 and s.deleted = 0 
        and p.staff_id = s.root_id 
$staff_id_sql $daterange_sql order by 1";
    return 
$sth $dbh->query($Find_Payrollby_date_staffid);
}

// ### Search Payroll info ### //
function Search_Payroll_info($payroll_id) {
    global 
$dbh;
    
$Search_Payroll_info_sql "SELECT pl.`payroll_id`,`rate_type`,`rate`,`hour`, `amount`,`total_amount` FROM `payroll_line` pl, `payroll` p 
        where pl.actived = 1 and pl.deleted = 0 and p.actived = 1 and p.deleted = 0 and pl.payroll_id = p.payroll_id and pl.payroll_id = ?"
;
    
$sth $dbh->prepare($Search_Payroll_info_sql);
    
$sth->bindParam(1$payroll_id);
    
$sth->execute();
    return 
$sth;
}

// ### Update payroll deleted=0 ### //
function Update_Paryroll_Payrollline_deleted($payroll_id) {
    global 
$dbh;
    
$update_payroll_deleted "UPDATE `payroll` SET `lastupby`=0,`lastupdate`=now(),`deleted`=1 WHERE payroll_id = $payroll_id and actived = 1 and deleted = 0";
    
$dbh->query($update_payroll_deleted);
    
$update_payrollline_deleted "UPDATE `payroll_line` SET `lastupby`=0, `lastupdate`=now(),`deleted`=1 WHERE payroll_id = $payroll_id and actived = 1 and deleted = 0";
    
$dbh->query($update_payrollline_deleted);
}

?>