/var/www/(Del)pathways.org.hk/MIS20140127/old20140314/Payroll/PayRoll_Generate_From.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
<!DOCTYPE html>
<?php require_once 'PayRoll_Sql.php'?>
<?php
$Total_Amount 
0;
$lesson = array();
////// ### ↓Submit by Generate ###        ### ↓Submit by edit ###             ### ↓Submit by void ### /////
if (isset($_POST['btn_Submit']) || isset($_POST['btn_Submit_Save']) || isset($_POST['btn_Submit_Void'])) {

    
$root_id $_POST['root_id'];
    
$create_payroll_date $_POST['create_payroll_date'];
    
$total_amount str_replace(','''$_POST['total_amount']);
    
$rate $_POST['rate'];
    
$rate_type $_POST['rate_type'];
    
$hour $_POST['hour'];
    
$amount str_replace(','''$_POST['amount']);
    
$start_date $_POST['start_date'];
    
$end_date $_POST['end_date'];
    
$search_startdate $_POST['search_startdate'];
    
$search_enddate $_POST['search_enddate'];
    
// ### Update the Payroll/Payroll_line to deleted = 0 ( Edit / Void ) ### //
    
if (isset($_POST['btn_Submit_Save']) || isset($_POST['btn_Submit_Void'])) {
        
$payroll_id_Update $_POST['payroll_id'];
        
Update_Paryroll_Payrollline_deleted($payroll_id_Update);
    }
    
// ### change lesson_attendance table payroll_gened to 0 (Void) ### //
    
if (isset($_POST['btn_Submit_Void'])) {
        foreach (
$_POST['lesson_id'] as $lesson)
            
Change_lessonattendance_payrollgenerate($root_id$lesson0);
    } else {
        
// ### Insert new payroll to the DB (ADD/EDIT)  ### //
        
Insert_Payroll($root_id$create_payroll_date$total_amount$start_date$end_date);
        
$payroll_id Select_Payroll_ID($root_id$create_payroll_date$total_amount$start_date$end_date);

        foreach (
$_POST['lesson_id'] as $counter => $lesson) {
            
Change_lessonattendance_payrollgenerate($root_id$lesson1);
            
Insert_Payrollline($payroll_id$rate_type[$counter], $rate[$counter], $hour[$counter], $amount[$counter]);
        }
    }
    if (isset(
$_POST['btn_Submit_Save']) || isset($_POST['btn_Submit_Void']))
        
header('Location: PayRoll_List.php');
    else
        echo 
'<script>location.href = "PayRoll_Generate_List.php?search[start_date]=' $search_startdate '&search[end_date]=' $search_enddate '&search[\'search_button\']=&back=1";</script>';
} else {
    
// ### generate the payroll data before submit ### //
    
$root_id = (isset($_GET['view']) || isset($_GET['edit'])) ? $_GET['root_id'] : $_POST['root_id'];
    
$search_start_date = (isset($_GET['view']) || isset($_GET['edit'])) ? $_GET['startdate'] : $_POST['startdate'];
    
$search_end_date = (isset($_GET['view']) || isset($_GET['edit'])) ? $_GET['enddate'] : $_POST['enddate'];
    
$dateRange Find_dateRange($root_id$search_start_date$search_end_date);
    
$start_date $dateRange[1];
    
$end_date $dateRange[0];

    
$sth Payroll_staff_rootid($root_id);
    while (
$ResultSet $sth->fetch(PDO::FETCH_ASSOC)) {
        
$staff_name $ResultSet['name'];
        
$staff_code $ResultSet['staff_code'];
    }

    
$sth Payroll_lesson_rootid($root_id$start_date$end_date);
    
$lesson $sth->fetchAll(PDO::FETCH_ASSOC);

    
$sth Salary_rootid($root_id);
    
$salary $sth->fetch(PDO::FETCH_NUM);
}
if (isset(
$_GET['view']) || isset($_GET['edit'])) {
    
$payroll_id $_GET['payroll_id'];
    
$sth Search_Payroll_info($payroll_id);
    
$Payroll_info $sth->fetchAll(PDO::FETCH_ASSOC);
}
?>
<html>
    <head>
        <?php require_once '../include/head.php'?>
        <?php require_once '../include/checkuser.php'?>
        <?php require_once '../include/Nav_bar.php'?>  
        <script>
            $(function(){
                $("#start_date").datetimepicker({autoclose: true,pickTime: false, format: 'yyyy-MM-dd'})                
            })
            function Rate_change(e,newHour,root_id,id){
                var rate = $(e).val();
                var hour = newHour;                
                $.get("PayRoll_Generate_Ajax.php?rate="+rate+"&hour="+hour+"&root_id="+root_id+"&row_id="+id, Amount_change_res);
            }
            function Pay_change(e,row_id,Rate){                
                if($(e).val() == 0){
                    $("#Rate\\["+row_id+"\\]").attr("disabled", true);
                    $("#Rate\\["+row_id+"\\]").val(7);
                    $("#Rate\\["+row_id+"\\]").next().html("");
                    $("#Rate\\["+row_id+"\\]").next().next().val(0);
                    $("#btn_readonly\\["+row_id+"\\]").attr("disabled", true);
                    $("#Amount_box\\["+row_id+"\\]").val("0.00");
                    $("#Amount_box\\["+row_id+"\\]").change();
                }else{
                    $("#Rate\\["+row_id+"\\]").attr("disabled", false);
                    $("#btn_readonly\\["+row_id+"\\]").attr("disabled", false);
                    $("#Rate\\["+row_id+"\\]").val(Rate);
                    $("#Rate\\["+row_id+"\\]").change();
                }
            }
            function Amount_change_res(res){
                var result = res.split(" ")[0];
                var row_id = res.split(" ")[1];
                var rate = res.split(" ")[2]; 
                if(rate == "Other_Amount"){                        
                    $("#btn_readonly\\["+row_id+"\\]").attr("disabled", false);
                    $("#btn_readonly\\["+row_id+"\\]").click();
                }else{
                    if(result == "0.00"){
                        $("#Pay\\["+row_id+"\\]").val(0);
                        $("#Pay\\["+row_id+"\\]").change();                    
                    }else{                    
                        $("#Amount_box\\["+row_id+"\\]").val(result);
                        $("#Amount_box\\["+row_id+"\\]").change();
                        $("#Rate\\["+row_id+"\\]").next().html("$ "+toDollor(rate));
                        $("#Rate\\["+row_id+"\\]").next().next().val((rate));
                    }
                }
            }
            function Amountbox_readonly(e,row_id){                   
                var default_ratetype = $("#default_ratetype\\["+row_id+"\\]").val();                
                if($(e).val() == 1){                    
                    $(e).val(0);                             
                    $(e).next().attr("readonly",false);
                    $(e).after("<button id=\"btn_OK["+row_id+"]\" type=\"button\" class=\"btn\" onclick=\"btn_OK_onclick(this,"+row_id+");\" >OK</button>");                        
                    $("#Rate\\["+row_id+"\\]").attr("disabled",true);
                    $("#Rate\\["+row_id+"\\]").next().html("");
                    $("#Rate\\["+row_id+"\\]").next().next().val(0);
                    $("#Rate\\["+row_id+"\\]").blur();
                    $(e).next().next().select();
                }else{                    
                    $(e).next().next().attr("readonly",true);
                    $(e).val(1);    
                    $(e).attr("disabled", true);
                    $(e).next().attr("disabled", true);
                    $(e).blur();
                    $("#Rate\\["+row_id+"\\]").attr("disabled", false);
                    $("#Rate\\["+row_id+"\\]").val(default_ratetype);
                    $("#Rate\\["+row_id+"\\]").change();
                    $(e).next().remove();
                }
            }
            function Sum_totalamount(e){
                var sumRow = $("#row_num_sum").val();
                var total_amount = 0;
                for(var count=0;count<sumRow;count++){
                    var amount = $("#Amount_box\\["+count+"\\]").val();
                    amount = amount.replace(/\,/g , "");
                    total_amount += parseFloat(amount);                    
                }
                $("#total_amount").val(toDollor(total_amount));
            }
            function toDollor(amount){
                var M = parseFloat(amount).toFixed(2).split(".");
                var dollar = M[0];
                var cent = M[1];
                dollar = dollar.split('').reverse().join("").replace(/(\d{3}(?!$))/g, '$1,').split("").reverse().join("");
                return dollar + '.' + cent.slice(0,2);
            }
            function btn_OK_onclick(e,row_id){
                var amount = $(e).next().val();
                amount = amount.replace(/\,/g,"");
                $(e).next().val(toDollor(amount));
                $(e).attr("disabled", true);
                $(e).next().attr("readonly",true);
                $(e).prev().attr("disabled", true);
                $("#Rate\\["+row_id+"\\]").attr("disabled", false);
                $(e).prev().val(1);
                $(e).remove();
            }
            function Submit_input_disabled(){
                $("#table").children().each(function(){
                    $(this).find("td").each(function(){
                        $(this).find("select").attr("disabled", false);                          
                    });
                });
            }
        </script>
    </head>
    <body>
        <div class="text-right">
            <ul class="breadcrumb">
                <li class="active"><a href="PayRoll_Generate_List.php">Payroll Generate</a> <span class="divider">&gt;</span></li>
                <li class="active">Generate From</li>
            </ul>
        </div>

        <div class="container-fluid pathways-container">
            <?php if (isset($_GET['view'])) { ?>
                <h3>View From</h3>
            <?php ?>
            <?php if (isset($_GET['edit'])) { ?>
                <h3>Edit From</h3>
            <?php ?>
            <?php if (!isset($_GET['view']) && !isset($_GET['edit'])) { ?>
                <h3>Generate From</h3>
            <?php ?>
            <div style="height: 15px;"></div>
            <form method="post" action="" autocomplete="off">
                <table>
                    <tr>
                        <!-- ### Hide ### -->
                        <td style="display: none;">
                            <input name="root_id" value="<?= $root_id ?>"/>
                            <input name="search_startdate" value="<?= $search_start_date ?>"/>
                            <input name="search_enddate" value="<?= $search_end_date ?>"/>
                            <input id="row_num_sum" value="<?= count($lesson?>"/>  
                            <input name="payroll_id" value="<?= $Payroll_info[0]['payroll_id'?>"/>
                        </td>
                        <!-- ### Hide ### -->
                        <!--  ### Staff Code ###  -->
                        <td style="width: 200px;text-align: right;padding-bottom: 5px;"><label for="staff_code">Staff Code </label></td>
                        <td style="width: 30px;"></td>
                        <td><input type="text" id="staff_code" value="<?= $staff_code ?>" readonly /></td>
                        <!--  ### Staff Name ### -->
                        <td style="width: 200px;text-align: right;padding-bottom: 5px;"><label for="staff_name">Staff name </label></td>
                        <td style="width: 10px;"></td>
                        <td><input type="text" id="staff_name" value="<?= $staff_name ?>" readonly/></td>
                    </tr>

                    <tr style="height: 15px;"></tr>

                    <tr>
                        <!--  ### Start Date ###-->
                        <td style="width: 200px;text-align: right;padding-bottom: 5px;"><label for="start_date">Start Date </label></td>
                        <td style="width: 30px;"></td>
                        <td><input type="text" id="start date" name="start_date" value="<?= $start_date ?>" readonly/></td>
                        <!--  ### End Date ###-->  
                        <td style="width: 200px;text-align: right;padding-bottom: 5px;"><label for="end_date">End Date </label></td>
                        <td style="width: 30px;"></td>
                        <td><input type="text" id="end_date" name="end_date" value="<?= $end_date ?>" readonly/></td>
                    </tr>
                    <tr style="height: 15px;"></tr>
                    <tr>
                        <td style="text-align: right;padding-bottom: 5px;">Create Payroll Date</td>
                        <td></td>
                        <td>
                            <div id="start_date" class="input-append date form_datetime datetime_picker">
                                <input type="text" <?= (isset($_GET['view'])) ? "disabled" "" ?> id="start_date_input"  name="create_payroll_date" style="width: 180px;" value="<?= date("Y/m/d"time()) ?>"/>
                                <?php if (!isset($_GET['view'])) { ?> <span class="add-on"><i class="icon-calendar"></i></span> <?php ?>
                            </div>
                        </td>
                    </tr>
                </table>

                <hr/>

                <table class="table table-striped table-bordered table-hover table-condensed">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Code</th>
                            <th>Start Time</th>
                            <th>End Time</th>
                            <th>Hour</th>
                            <th>Attendance</th>
                            <th>Remark</th>
                            <th>Teaching Role</th>
                            <th style="width: 80px;">Pay</th>
                            <th style="width: 130px;">Rate</th>
                            <th style="width: 150px;">Amount</th>
                        </tr>
                    </thead>
                    <tbody  id="table">
                        <?php foreach ($lesson as $k => $array) { ?>                        
                            <?php $Total_Amount += $Amount = (Cal_Salary($salary$array['rate_type'], $hour number_format((strtotime($array['end_time']) - strtotime($array['start_time'])) / 36002))); ?>
                            <tr>
                                <!-- ### Hidden ### -->
                                <td style="display: none;">                                    
                                    <input name="hour[]" value="<?= number_format($hour 600?>"/>
                                    <input name="lesson_id[]" value="<?= $array['lesson_id'?>"/>   
                                    <input id="default_ratetype[<?= $k ?>]" value="<?= $array['rate_type'?>"/>                                    
                                </td>
                                <!-- ### Hidden ###  -->
                                <td><?= $array['date'?></td>
                                <td><?= $array['event_code'?></td>
                                <td><?= $array['start_time'?></td>
                                <td><?= $array['end_time'?></td>
                                <td><?= (number_format($hour2)) ?></td>
                                <td><?= ($array['attendance'] == 1) ? "Present" "Absent" ?></td>
                                <td> </td>
                                <td>
                                    <?php echo Rate_display($array['rate_type'], $array['is_substitute']); ?>
                                </td>
                                <td>                                    
                                    <select style="width: 100%;margin-bottom: 1px;" id="Pay[<?= $k ?>]" name="Pay[]" onchange="Pay_change(this,<?= $k ?>,<?= $array['rate_type'?>);" <?= (isset($_GET['view'])) ? "disabled" "" ?>>
                                        <option <?= ($array['is_charge'] == 1) ? "selected" "" ?> value="1">Yes</option>
                                        <option <?= ($array['is_charge'] == 0) ? "selected" "" ?> value="0">No</option>
                                    </select>
                                </td>
                                <td>
                                    <select style="width: 100%;margin-bottom: 1px;" id="Rate[<?= $k ?>]" name="rate_type[]" onchange="Rate_change(this,<?= $hour ?>,<?= $root_id ?>,<?= $k ?>);" <?= (isset($_GET['view'])) ? "disabled" "" ?> >
                                        <option <?= (isset($Payroll_info[$k]['rate_type'])) ? ($Payroll_info[$k]['rate_type'] == 0) ? "selected" ""  : ($array['rate_type'] == 0) ? "selected" ""  ?> value="0">Rate 0</option>
                                        <option <?= (isset($Payroll_info[$k]['rate_type'])) ? ($Payroll_info[$k]['rate_type'] == 1) ? "selected" ""  : ($array['rate_type'] == 1) ? "selected" ""  ?> value="1">Rate 1</option>
                                        <option <?= (isset($Payroll_info[$k]['rate_type'])) ? ($Payroll_info[$k]['rate_type'] == 2) ? "selected" ""  : ($array['rate_type'] == 2) ? "selected" ""  ?> value="2">Rate 2</option>
                                        <option <?= (isset($Payroll_info[$k]['rate_type'])) ? ($Payroll_info[$k]['rate_type'] == 3) ? "selected" ""  : ($array['rate_type'] == 3) ? "selected" ""  ?> value="3">Rate 3</option>
                                        <option <?= (isset($Payroll_info[$k]['rate_type'])) ? ($Payroll_info[$k]['rate_type'] == 4) ? "selected" ""  : ($array['rate_type'] == 4) ? "selected" ""  ?> value="4">Rate 4</option>
                                        <option <?= (isset($Payroll_info[$k]['rate_type'])) ? ($Payroll_info[$k]['rate_type'] == 5) ? "selected" ""  : ($array['rate_type'] == 5) ? "selected" ""  ?> value="5">Rate 5</option>
                                        <option <?= (isset($Payroll_info[$k]['rate_type'])) ? ($Payroll_info[$k]['rate_type'] == 6) ? "selected" ""  : ($array['rate_type'] == 6) ? "selected" ""  ?> value="6">Rate 6</option>
                                        <option <?= (isset($Payroll_info[$k]['rate_type'])) ? ($Payroll_info[$k]['rate_type'] == 8) ? "selected" ""  : ($array['rate_type'] == 8) ? "selected" ""  ?> value="8">Other Amount</option>
                                        <option <?= (isset($Payroll_info[$k]['rate_type'])) ? ($Payroll_info[$k]['rate_type'] == 7) ? "selected" ""  : ($array['rate_type'] == 7) ? "selected" ""  ?> value="7">No Pay</option>
                                    </select>
                                    <label style="margin-bottom: 1px;"><?= (isset($Payroll_info[$k]['rate'])) ? ($Payroll_info[$k]['rate'] == 0) ? "" "$ " $Payroll_info[$k]['rate']  : "$ " number_format(Find_Ratetype($salary$array['rate_type']), 2?></label>
                                    <input type="hidden" name="rate[]" value="<?= (isset($Payroll_info[$k]['rate'])) ? $Payroll_info[$k]['rate'] : (Find_Ratetype($salary$array['rate_type'])) ?>"/>
                                </td>
                                <td>
                                    <div class="input-prepend" style="margin-bottom: 1px;">
                                        <button id="btn_readonly[<?= $k ?>]" type="button" class="btn" onclick="Amountbox_readonly(this,<?= $k ?>);" value="1" disabled>$</button>
                                        <input readonly id="Amount_box[<?= $k ?>]" name="amount[]" type="text" style="width: 150px;text-align: right" value="<?= (isset($_GET['view']) || isset($_GET['edit'])) ? number_format($Payroll_info[$k]['amount'], 2) : number_format($Amount2?>" onchange="Sum_totalamount(this);" />            
                                    </div>
                                </td>
                            </tr>
                        <?php ?>                            
                        <tr>                            
                            <td colspan="10" style="text-align: right;"> Total Amount :</td>
                            <td>
                                <div class="input-prepend" style="margin-bottom: 1px;">
                                    <button type="button" class="btn" disabled >$</button>
                                    <input readonly type="text" id="total_amount" name="total_amount" style="width: 150px;text-align: right" value="<?= (isset($_GET['view']) || isset($_GET['edit'])) ? number_format($Payroll_info[$k]['total_amount'], 2"."",") : number_format($Total_Amount2"."","?>">            
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <hr/>
                <?php if (isset($_GET['edit'])) { ?>
                    <button onclick="Submit_input_disabled();"type="Submit" name="btn_Submit_Save" class="btn btn-danger">Save</button>
                    <button onclick="Submit_input_disabled();" type="Submit" name="btn_Submit_Void" class="btn btn-danger">Void</button>
                <?php } else { ?>
                    <button <?= isset($_GET['view']) ? "style=\"display:none;\"" "" ?> onclick="Submit_input_disabled();" type="Submit" name="btn_Submit" class="btn btn-danger">Generate</button>
                <?php ?>
                <button type="button" onclick="history.back();"class="btn btn-danger">Back</button>
            </form>
        </div> <!-- /container -->        
    </body>
</html>