/var/www/(Del)pathways.org.hk/MIS_bk/staff_payroll/form.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
<?php
require_once(__DIR__ '/../checkuser.php');
function 
details() {
    global 
$dbh;
    
    
$message $_GET['message'];
    
    
$payroll_lines = array();
    
    
$id = isset($_GET['id']) ? $_GET['id'] : null;
    if (empty(
$id)) {
        
// redirect to main if no id
        
redirectAndExit(Util::link(__DIR__ '/../main.php'));
    } else {
        
$sql "SELECT * FROM mis_payroll payroll WHERE id = ? AND deleted = ?";
        
$parameters = array($id0);
        
$sth Db\Util::execute($dbh$sql$parameters);
        
$payroll $sth->fetch(PDO::FETCH_ASSOC);
        
        
$sql "
SELECT latest_staff.*
FROM mis_staff staff
INNER JOIN mis_staff latest_staff ON latest_staff.root_id = staff.root_id AND latest_staff.is_latest = 1
WHERE staff.id = ?"
;
        
$parameters = array($payroll['staff_id']);
        
$sth Db\Util::execute($dbh$sql$parameters);
        
$payroll['staff'] = $sth->fetch(PDO::FETCH_ASSOC);
        
        
$lesson_staff_columns Db\Util::columns($dbh'mis_lesson_staff');
        
$lesson_columns Db\Util::columns($dbh'mis_event_lesson');
        
$event_columns Db\Util::columns($dbh'mis_event');
        
        
$sql "SELECT payroll_line.*";
        foreach (
__::pluck($lesson_staff_columns'column_name') as $column_name) {
            
$sql .= ", lesson_staff.`$column_name` AS lesson_staff_$column_name";
        }
        foreach (
__::pluck($lesson_columns'column_name') as $column_name) {
            
$sql .= ", lesson.`$column_name` AS lesson_$column_name";
        }
        foreach (
__::pluck($event_columns'column_name') as $column_name) {
            
$sql .= ", evt.`$column_name` AS event_$column_name";
        }
        
$sql .= ",
    program.code AS program_code,
    subject.code AS subject_code,
    staff_position.title AS position_title
FROM mis_payroll_line payroll_line
INNER JOIN mis_lesson_staff lesson_staff ON lesson_staff.id = payroll_line.lesson_staff_id
INNER JOIN mis_staff_position staff_position ON staff_position.id = lesson_staff.staff_position_id
INNER JOIN mis_event_lesson lesson ON lesson.id = lesson_staff.lesson_id
INNER JOIN mis_event evt ON evt.id = lesson.event_id
LEFT JOIN mis_program program ON program.id = evt.program_id
LEFT JOIN mis_subject subject ON subject.id = evt.subject_id
WHERE payroll_line.payroll_id = ? AND payroll_line.deleted = 0
ORDER BY lesson.`date`, evt.code, program.code, subject.code"
;
        
$parameters = array($payroll['id']);
        
$sth Db\Util::execute($dbh$sql$parameters);
        
$payroll_lines $sth->fetchAll();
    }
    
    if (
isPost()) {
        
// save
        
Db\Util::transaction($dbh, function() use ($dbh, &$payroll, &$payroll_lines) {
            
$columns Db\Util::columns($dbh'mis_payroll_line');
            
            
$sql "SELECT * FROM mis_payroll WHERE id = ? FOR UPDATE";
            
$parameters = array($payroll['id']);
            
$sth Db\Util::execute($dbh$sql$parameters);
            
            foreach (
$_POST['payroll_line'] ?: array() as $id => $attributes) {
                
$payroll_line __::find($payroll_lines, function($line) use ($id) { return $line['id'] == $id; });
                
$payroll_line array_merge($payroll_line$attributes);
                
Db\Util::update($dbh'mis_payroll_line'$payroll_line$columns);
            }
        });
        
        
$data = array(
            
'message' => 'Saved',
        );
        
redirectAndExit(Util::link(__DIR__ '/index.php') . '?' http_build_query($data));
    }
    
    
$json_payroll_lines __::map($payroll_lines, function($payroll_line$index$payroll_lines) {
        return 
json_encode($payroll_line);
    });
    
    return array(
        
'message' => $message,
        
        
'payroll' => $payroll,
        
'payroll_lines' => $payroll_lines,
        
'event_programme_fee_type_options' => Event::programmeFeeTypeOptions(),
        
'json_payroll_lines' => $json_payroll_lines,
    );
}
extract(details());
?><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ng="http://angularjs.org">
<head>
    
    <?php require(__DIR__ '/../inc/_head_meta.php'); ?>
    
    <?php require(__DIR__ '/../inc/_head_css.php'); ?>
    
    <?php require(__DIR__ '/../inc/_head_script.php'); ?>
    
    <style type="text/css">
        .popover {
            min-width: 400px
        }
    </style>
    
    <script type="text/javascript">
        function PayrollLineControl($scope) {
            function resetModel() {
                //$.extend($scope, { });
            }
            
            resetModel();
            $.extend($scope, {
                removeLine: function(index) {
                    var line = $scope.lines[index];
                    line.deleted = 1;
                },
                showCount: function() {
                    var count = 0;
                    _.each($scope.lines, function(line) {
                        if (line.deleted != 1) {
                            count++;
                        }
                    });
                    return count;
                },
                getEventProgrammeFeeTypeLabel: function(value) {
                    switch (value) {
                        <?php foreach ($event_programme_fee_type_options as $value => $label): ?>
                        case '<?=$value?>':
                        case <?=$value?>:
                            return '<?=$label?>';
                        <?php endforeach; ?>
                        default:
                            return '';
                    }
                },
                getAmountPopoverContent: function(index) {
                    var line = $scope.lines[index];
                    return 'Original amount: '+(_.isNull(line.original_fee) ? 'N/A' : line.original_fee)+'<br />'+
                            'Staff salary: <?=$payroll['staff']['salary'] ?: 'N/A'?> By Month<br />'+
                            'Staff rate 1: <?=$payroll['staff']['rate1'] ?: 'N/A'?> Teacher (In House)<br />'+
                            'Staff rate 2: <?=$payroll['staff']['rate2'] ?: 'N/A'?> Teacher (Outside School)<br />'+
                            'Staff rate 3: <?=$payroll['staff']['rate3'] ?: 'N/A'?> Teaching Assistant (In House)<br />'+
                            'Staff rate 4: <?=$payroll['staff']['rate4'] ?: 'N/A'?> Teaching Assistant (Outside School)<br />'+
                            'Staff rate 5: <?=$payroll['staff']['rate5'] ?: 'N/A'?> Traveling<br />'+
                            'Staff rate 6: <?=$payroll['staff']['rate6'] ?: 'N/A'?> Other';
                },
                lines: [ <?=isset($json_payroll_lines) ? implode(', '$json_payroll_lines) : ''?> ]
            });
        }
    </script>
    
</head>
<body>
    
    <?php require( __DIR__ '/../inc/_navbar.php'); ?>
    
    <div class="text-right">
        <ul class="breadcrumb">
            <li><a href="#">Master</a> <span class="divider">&gt;</span></li>
            <li><a href="#">Staff</a> <span class="divider">&gt;</span></li>
            <li class="active">Payroll</li>
        </ul>
    </div>
    
    <div class="container-fluid pathways-container">
        
        <?php if (isset($message) && !empty($message)): ?>
        <div class="alert alert-info">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <h5 class="alert-heading">Note:</h5>
            <p><?=$message?></p>
        </div>
        <?php endif; ?>

        <h3><?=empty($payroll['id']) ? 'Add' 'Modify'?> Staff Payroll</h3>
        
        <div class="form-horizontal">
            <div class="control-group">
                <?php $attribute 'name'$label 'Staff'?>
                <label class="control-label" for="<?=$attribute?>"><?=$label?></label>
                <div class="controls form-inline">
                    <input type="text" id="<?=$attribute?>" name="" class="" value="<?=h($payroll['staff']['last_name'])?> <?=h($payroll['staff']['first_name'])?>" placeholder="<?=h($label)?>" maxlength="200" readonly="readonly" />
                </div>
            </div>
            
            <?php if (!empty($payroll['id'])): ?>
            <div class="control-group">
                <?php $attribute 'code'$label 'Payroll No.'?>
                <label class="control-label" for="<?=$attribute?>"><?=$label?></label>
                <div class="controls form-inline">
                    <input type="text" id="<?=$attribute?>" name="" class="" value="<?=h($payroll[$attribute])?>" placeholder="<?=h($label)?>" maxlength="200" readonly="readonly" />
                </div>
            </div>
            <?php endif; ?>
        </div>
        
        <hr />
        
        <form id="form" class="form-horizontal" method="post" enctype="multipart/form-data">
            <?php $attribute 'id'?>
            <input type="hidden" name="payroll[<?=$attribute?>]" value="<?=h($payroll[$attribute])?>" />
            
            <div id="ng-app" ng:app="" ng:controller="PayrollLineControl">
                <table class="table table-striped table-bordered table-hover table-condensed">
                    <thead>
                        <tr>
                            <?php $column_count 0?>
                            <th style="min-width:50px; width:50px">Delete</th><?php $column_count++; ?>
                            <th style="min-width:100px; width:100px">Lesson Date</th><?php $column_count++; ?>
                            <th>Event Code</th><?php $column_count++; ?>
                            <th>Program</th><?php $column_count++; ?>
                            <th>Subject</th><?php $column_count++; ?>
                            <th>Fee Type</th><?php $column_count++; ?>
                            <th>Hours</th><?php $column_count++; ?>
                            <th>Teaching Role</th><?php $column_count++; ?>
                            <th>Amount</th><?php $column_count++; ?>
                            <th>Attend</th><?php $column_count++; ?>
                            <th>Remarks</th><?php $column_count++; ?>
                        </tr>
                    </thead>
                    <tfoot ng:hide="showCount()">
                        <tr>
                            <td colspan="<?=$column_count?>">There are no records.</td>
                        </tr>
                    </tfoot>
                    <tbody>
                        <tr ng:repeat="line in lines" ng:hide="line.deleted == 1">
                            <td>
                                <button type="button" class="btn" ng:click="removeLine($index)"><i class="icon-remove"></i></button>
                                <?php $attribute 'deleted'?>
                                <input type="hidden" name="{{line.id && 'payroll_line['+line.id+'][<?=$attribute?>]' || 'new_payroll_line['+$index+'][<?=$attribute?>]'}}" value="{{line.<?=$attribute?>}}" />
                            </td>
                            <td>{{line.lesson_date}}</td>
                            <td>{{line.event_code}}</td>
                            <td>{{line.program_code}}</td>
                            <td>{{line.subject_code}}</td>
                            <td><span ng:bind="getEventProgrammeFeeTypeLabel(line.event_programme_fee_type)">Loading...</span></td>
                            <td>{{line.hour}}</td>
                            <td>{{line.position_title}}</td>
                            <td>
                                $
                                <?php $attribute 'amount'$label 'Amount'?>
                                <input type="text" name="{{line.id && 'payroll_line['+line.id+'][<?=$attribute?>]' || 'new_payroll_line['+$index+'][<?=$attribute?>]'}}"
                                       class="number input-mini popover-amount" value="{{line.<?=$attribute?>}}" placeholder="<?=h($label)?>" maxlength="200"
                                       data-toggle="popover" data-html="true" data-placement="bottom" data-trigger="focus" data-title="Amount information" data-content="{{getAmountPopoverContent($index)}}" />
                            </td>
                            <td>{{line.lesson_staff_attendance == 1 && 'Present' || 'Absent'}}</td>
                            <td>
                                <?php $attribute 'remarks'$label 'Remarks'?>
                                <input type="text" name="{{line.id && 'payroll_line['+line.id+'][<?=$attribute?>]' || 'new_payroll_line['+$index+'][<?=$attribute?>]'}}"
                                       class="" value="{{line.<?=$attribute?>}}" placeholder="<?=h($label)?>" maxlength="200" />
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            
            <?php if (count($payroll_lines) > 0): ?>
            <button type="submit" class="btn btn-danger">Save</button>
            <?php endif; ?>
        </form>
        
        <script type="text/javascript">
            $(function() {
                $('.date-picker').datetimepicker({ pickTime: false });
                $('#form').validate();
                setTimeout(function() {
                    $('.popover-amount').popover();
                }, 500);
            });
        </script>
        
        
        <?php require( __DIR__ '/../inc/_footer.php'); ?>

    </div> <!-- /container -->
</body>
</html>