2016-06-12 15:35:27 +00:00
<? php
2016-06-13 09:19:28 +00:00
/***********************************************************************
* Copyright (C) 2016 Max Mehl <max.mehl [at] fsfe [dot] org> for FSFE e.V.
************************************************************************
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*
************************************************************************
*
* This file receives input from /internal/pd.en.(x)html and calculates per
* diem charges for reimbursement claims. The amounts can be defined in
* the XHTML file. For the output of the data, it uses
* /internal/pd-result.en.(x)html as a template.
*
***********************************************************************/
2016-06-12 15:35:27 +00:00
2016-06-13 20:52:03 +00:00
$output = '' ; // create empty variable
2016-06-12 17:34:31 +00:00
2016-06-13 20:52:03 +00:00
// detect home country and set accodingly: currency, rates
$home = $_POST [ 'home' ];
if ( $home === 'de' ) {
2016-06-14 14:19:05 +00:00
$cur = " €" ; // currency
$c_b = 0.2 ; // breakfast rate
$c_l = 0.4 ; // lunch rate
$c_d = 0.4 ; // dinner rate
$c_flat = 0 ; // flat rate (money which employee gets even if all meals are paid)
2016-06-13 20:52:03 +00:00
} elseif ( $home === 'se' ) {
$cur = " SEK" ;
$c_b = 0.15 ;
$c_l = 0.35 ;
$c_d = 0.35 ;
2016-06-14 14:19:05 +00:00
$c_flat = 0.15 ;
2017-01-16 21:18:24 +00:00
} elseif ( $home === 'other' ) {
$home_other = explode ( "/" , $_POST [ 'home_other' ]);
$cur = " " . $home_other [ 0 ];
$c_b = $home_other [ 1 ];
$c_l = $home_other [ 2 ];
$c_d = $home_other [ 3 ];
$c_flat = $home_other [ 4 ];
2016-06-13 20:52:03 +00:00
}
// amount per day
$epd = $_POST [ 'dest' ];
2016-06-12 17:34:31 +00:00
if ( $epd === 'other' ) {
2016-06-13 20:52:03 +00:00
$epd = $_POST [ 'dest_other' ]; // if other destination, just take this value
} else {
$pattern = "/" . $home . "=([0-9.]+)?\/([0-9.]+)?/" ; // define pattern something like "/de=12/24/"
$epd = preg_match ( $pattern , $epd , $match , PREG_OFFSET_CAPTURE ); // actually search for it
$epd = $match [ 0 ][ 0 ]; // matches are on 2nd level inn an array
$epd = explode ( '=' , $epd ); // now separate at the "="
$epd = $epd [ 1 ]; // take the second half of it "12/24"
2016-06-12 17:34:31 +00:00
}
2016-06-12 15:35:27 +00:00
2016-06-13 20:52:03 +00:00
$epd = explode ( '/' , $epd ); // separate at "/"
$epd_trav = $epd [ 0 ]; // first half
$epd_full = $epd [ 1 ]; // second hald
2017-01-16 20:54:30 +00:00
$output .= "<h2>Detailled overview of per diems</h2>" ;
2016-06-13 20:52:03 +00:00
$output .= "<p>Travel day(s): " . $epd_trav . $cur . " per day <br />" ;
$output .= "Full day(s): " . $epd_full . $cur . " per day <br />" ;
$output .= "Own country: " . $home . " <br />" ;
2016-06-14 14:19:05 +00:00
$output .= "Percentage rate for breakfast/lunch/dinner: " . $c_b . "/" . $c_l . "/" . $c_d . " <br />" ;
$output .= "Flat rate (percentage which employee gets even if all meals are paid): " . $c_flat . " <br /></p>" ;
2016-06-12 15:35:27 +00:00
2016-06-12 17:34:31 +00:00
$output .= '<table class="table table-striped">
2016-06-12 15:35:27 +00:00
<tr>
<th>Date</th>
2016-06-12 17:34:31 +00:00
<th>Breakfast</th>
<th>Lunch</th>
<th>Dinner</th>
2016-06-14 14:19:05 +00:00
<th>Flat reimbursement</th>
<th>Your total reimbursement</th>
2016-06-12 17:34:31 +00:00
</tr>' ;
2016-06-12 15:35:27 +00:00
2020-05-28 16:54:12 +02:00
$use = $_POST [ 'use' ];
$date = $_POST [ 'date' ];
$break = $_POST [ 'break' ];
$lunch = $_POST [ 'lunch' ];
$dinner = $_POST [ 'dinner' ];
2016-06-12 15:35:27 +00:00
// set variables
$r_total = 0 ; // total reimbursement
2020-05-28 16:54:12 +02:00
foreach ( $use as $d => $day ) { // calculate for each day
// set "no" as value for day's variable if empty
$use [ $d ] = $use [ $d ] ?? 'no' ;
$date [ $d ] = $date [ $d ] ?? 'no' ;
$break [ $d ] = $break [ $d ] ?? 'no' ;
$lunch [ $d ] = $lunch [ $d ] ?? 'no' ;
$dinner [ $d ] = $dinner [ $d ] ?? 'no' ;
2016-06-12 15:35:27 +00:00
// set variables
2017-01-16 20:54:30 +00:00
$r_day [ $d ] = 0 ; // reimbursement for this day
2016-06-12 15:35:27 +00:00
2017-01-16 20:54:30 +00:00
if ( $use [ $d ] === 'yes' ) { // only calculate if checkbox has been activated (day in use)
if ( $d === 'out' || $d === 'return' ) { // set amount of € for travel or full day
2016-06-14 14:19:05 +00:00
$eur = $epd_trav ; // total reimburseable amount for this day
2016-06-12 15:35:27 +00:00
$desc = " (travel)" ;
} else {
2016-06-14 14:19:05 +00:00
$eur = $epd_full ; // total reimburseable amount for this day
2016-06-12 15:35:27 +00:00
$desc = " (full)" ;
}
// open row
2016-06-12 17:34:31 +00:00
$output .= "<tr>" ;
2016-06-12 15:35:27 +00:00
// date
2017-01-16 20:54:30 +00:00
if ( $date [ $d ] === '' ) {
$date [ $d ] = "Day " . $d ;
2020-01-30 10:16:06 +01:00
} else {
$date [ $d ] = date ( "d.m.Y" , strtotime ( $date [ $d ]));
2016-06-12 22:12:37 +00:00
}
2017-01-16 20:54:30 +00:00
$output .= "<td>" . $date [ $d ] . $desc . "</td>" ;
2016-06-12 15:35:27 +00:00
// breakfast ($r_b)
2017-01-16 20:54:30 +00:00
if ( $break [ $d ] === "yes" ) {
2016-06-14 09:18:15 +00:00
$r_b = $eur * $c_b ;
2017-01-16 20:54:30 +00:00
$r_day [ $d ] = $r_day [ $d ] + $r_b ;
2016-06-13 20:52:03 +00:00
$output .= "<td>yes (" . $r_b . $cur . ")</td>" ;
2016-06-12 15:35:27 +00:00
} else {
2016-06-12 17:34:31 +00:00
$output .= "<td>no</td>" ;
2016-06-12 15:35:27 +00:00
}
// lunch ($r_l)
2017-01-16 20:54:30 +00:00
if ( $lunch [ $d ] === "yes" ) {
2016-06-14 09:18:15 +00:00
$r_l = $eur * $c_l ;
2017-01-16 20:54:30 +00:00
$r_day [ $d ] = $r_day [ $d ] + $r_l ;
2016-06-13 20:52:03 +00:00
$output .= "<td>yes (" . $r_l . $cur . ")</td>" ;
2016-06-12 15:35:27 +00:00
} else {
2016-06-12 17:34:31 +00:00
$output .= "<td>no</td>" ;
2016-06-12 15:35:27 +00:00
}
// breakfast ($r_d)
2017-01-16 20:54:30 +00:00
if ( $dinner [ $d ] === "yes" ) {
2016-06-14 09:18:15 +00:00
$r_d = $eur * $c_d ;
2017-01-16 20:54:30 +00:00
$r_day [ $d ] = $r_day [ $d ] + $r_d ;
2016-06-13 20:52:03 +00:00
$output .= "<td>yes (" . $r_d . $cur . ")</td>" ;
2016-06-12 15:35:27 +00:00
} else {
2016-06-12 17:34:31 +00:00
$output .= "<td>no</td>" ;
2016-06-12 15:35:27 +00:00
}
2016-06-14 14:19:05 +00:00
// flat rate
$r_flat = $eur * $c_flat ;
2017-01-16 20:54:30 +00:00
$r_day [ $d ] = $r_day [ $d ] + $r_flat ;
2016-06-14 14:19:05 +00:00
$output .= "<td>" . $r_flat . $cur . "</td>" ;
2016-06-12 15:35:27 +00:00
// reimbursement for the single day
2017-01-16 20:54:30 +00:00
$output .= "<td>" . $r_day [ $d ] . $cur . "</td>" ;
$r_total = $r_total + $r_day [ $d ];
2016-06-12 15:35:27 +00:00
// close row (day)
2016-06-12 17:34:31 +00:00
$output .= "</tr>" ;
2016-06-12 15:35:27 +00:00
} // if day is used
2017-01-16 20:54:30 +00:00
} // foreach
2016-06-12 15:35:27 +00:00
2016-11-14 12:52:24 +00:00
$output .= "<tr><td></td><td></td><td></td><td></td><td></td>" ;
2016-06-13 20:52:03 +00:00
$output .= "<td><strong>Total per diem: " . $r_total . $cur . "</strong></td></tr></table>" ;
2016-06-12 15:35:27 +00:00
2017-01-16 20:54:30 +00:00
/* ------------------------------------
* --- PRINT FOR COPY IN SPREADSHEET ---
* -----------------------------------*/
$output .= "<hr />" ;
$output .= "<h2>Spreadsheet copy&paste</h2>" ;
$output .= "<p>The following section can be copied to your Reimbursement Claim spreadsheet. Please mark and copy the content of the table (except the headings), and paste it in your spreadsheet application.<br /><em>Hint: In LibreOffice, press Ctrl+Shift+V in order to paste it as unformatted text instead of HTML code. This avoids conflicting formatting.</em></p>" ;
$output .= '<table class="table">' ;
$output .= '<tr><th>Employee name</th><th>Date</th><th>Amount (EUR)</th><th>Recipient Name</th><th>ER number</th><th>Catchphrase</th><th>Receipt / per diem</th><th>Remarks</th></tr>' ;
2020-05-28 16:54:12 +02:00
foreach ( $use as $d => $day ) { // calculate for each day
// set "no" as value for day's variable if empty
$use [ $d ] = $use [ $d ] ?? 'no' ;
$date [ $d ] = $date [ $d ] ?? 'no' ;
$break [ $d ] = $break [ $d ] ?? 'no' ;
$lunch [ $d ] = $lunch [ $d ] ?? 'no' ;
$dinner [ $d ] = $dinner [ $d ] ?? 'no' ;
$remark [ $d ] = $remark [ $d ] ?? '' ;
2017-01-16 20:54:30 +00:00
if ( $use [ $d ] === 'yes' ) {
$rc_name = $_POST [ 'rc_name' ];
$rc_er = $_POST [ 'rc_er' ];
$rc_catch = $_POST [ 'rc_catch' ];
if ( $rc_name === '' ) { $rc_name = "YOUR-NAME" ; }
if ( $rc_er === '' ) { $rc_er = "ER-NUMBER" ; }
if ( $rc_catch === '' ) { $rc_catch = "CATCHPHRASE" ; }
2017-04-04 10:02:27 +00:00
$output .= "<tr>" ;
// 1. Name
$output .= "<td>" . $rc_name . "</td>" ;
// 2. Date
$output .= "<td>" . $date [ $d ] . "</td>" ;
// 3. Amount
$r_day [ $d ] = preg_replace ( "/\./" , "," , $r_day [ $d ]); // replace . by , in amount to make it compatible with the used spreadsheet template
$output .= "<td>" . $r_day [ $d ] . "</td>" ;
// 4. Recipient Name (empty)
$output .= "<td></td>" ;
// 5. ER number
$output .= "<td>" . $rc_er . "</td>" ;
// 6. Catchphrase
$output .= "<td>" . $rc_catch . "</td>" ;
// 7. Receipt/per diem
$output .= "<td>per diem</td>" ;
// 8. Remarks
if ( $break [ $d ] === "yes" ) {
$remark [ $d ] = "breakfast+" ;
2017-01-16 20:54:30 +00:00
}
2017-04-04 10:02:27 +00:00
if ( $lunch [ $d ] === "yes" ) {
$remark [ $d ] .= "lunch+" ;
}
if ( $dinner [ $d ] === "yes" ) {
$remark [ $d ] .= "dinner" ;
}
if ( $break [ $d ] != "yes" && $lunch [ $d ] != "yes" && $dinner [ $d ] != "yes" ) {
$remark [ $d ] = "nothing" ;
}
if ( $break [ $d ] === "yes" && $lunch [ $d ] === "yes" && $dinner [ $d ] === "yes" ) {
$remark [ $d ] = "everything" ;
}
$remark [ $d ] = preg_replace ( "/\+$/" , "" , $remark [ $d ]);
$remark [ $d ] .= " self-paid" ;
$output .= "<td>" . $remark [ $d ] . "</td>" ;
$output .= "</tr>" ;
2017-01-16 20:54:30 +00:00
}
}
$output .= '</table>' ;
// --- PRINT OUTPUT IN TEMPLATE FILE ---
2016-06-12 15:35:27 +00:00
2016-06-12 17:34:31 +00:00
function replace_page ( $temp , $content ){
2016-06-12 22:12:37 +00:00
$vars = array ( ':RESULT:' => $content );
2016-06-12 17:34:31 +00:00
return str_replace ( array_keys ( $vars ), $vars , $temp );
}
2016-06-12 15:35:27 +00:00
2020-05-28 16:54:12 +02:00
$template = file_get_contents ( $_SERVER [ 'REQUEST_SCHEME' ] . "://" . $_SERVER [ 'HTTP_HOST' ] . '/internal/pd-result.en.html' , true );
2016-06-12 15:35:27 +00:00
2016-06-12 17:34:31 +00:00
echo replace_page ( $template , $output );
?>