0

I have a script that gets an employees holiday entitlement from their current record in the holiday_entitlement table.

This entitlement figure is then used for each holiday request in the database to be subtracted from.

For example: entitlement days = 27

holiday request days = 2 >>>> entitlement days left = 25

holiday request days = 4 >>>> entitlement days left = 21

holiday request days = 1 >>>> entitlement days left = 20 and so on....

In my script the entitlement days left is $remainder

The bit i am struggling with is to be able to save this $remainder to holiday_entitlement table everytime a request is approved OR work out in SQL the original entitlement figure minus all the requests for that employee, that academic year and if it was approved.

   <?php
    $is_business_result = mysql_query('SELECT * FROM holiday_entitlement WHERE employee = \'' . $username . '\' AND academic_year = \'' . $acyear . '\' ');

    if($is_business = mysql_fetch_array($is_business_result)) {
    echo'<div style="float:left; width:400px;">';

    echo'<table width="100%">
    <tr>
    <td><strong>Name:</strong></td>
    <td>'.$is_business['employee'].'</td>
    </tr>
    <tr>
    <td><strong>Entitlement:</strong></td>
    <td>'.$is_business['new_entitlement'].' '.$is_business['units'].'</td>
    </tr>
    <tr>
    <td><strong>Department / Division:</strong></td>
    <td>'.$is_business['division'].'</td>
    </tr>
    <tr>
    <td><strong>Line Manager:</strong></td>
    <td>'.$is_business['line_manager'].'</td>
    </tr>
    </table>';

    echo'</div>';





    echo'<table class="dataTable" id="business_days_table" cellpadding="2" cellspacing="2" width="100%">
    <thead>
    <th>Start Date</th>
    <th>End Date</th>
    <th>Days to be taken</th>
    <th>Days remaining</th>
    </thead>';

    echo '<tr>';

    echo '<td>-</td>';
    echo '<td>-</td>';
    echo '<td>-</td>';
    echo '<td>'.$is_business['new_entitlement'].'</td>';

    echo '</tr>';


    $input = $is_business['new_entitlement'];
    }

    else {echo 'You currently dont have a record for this academic year. ';}


    $requests_result = mysql_query('SELECT * FROM requests WHERE employee = \'' . $username . '\' AND approved = 1 AND academic_year = \'' . $acyear . '\' ORDER BY start_date ASC');

    $remainder = 0;

    while($requests = mysql_fetch_array($requests_result)) {

    $start_date = new DateTime($requests['start_date']);
    $end_date = new DateTime($requests['end_date']);

    $timestamp_start_date = $start_date->getTimestamp();
    $timestamp_end_date = $end_date->getTimestamp();

    $formatted_start_date = date("d M Y", $timestamp_start_date);           
    $formatted_end_date = date("d M Y", $timestamp_end_date);   

    $remainder = ($remainder == 0) ? $input : $remainder;
    $out = $remainder - $requests['days'];
    if($out < 0){
          break;
    }
    $remainder = $out;


    echo'<tr>';
    echo'<td>'.$formatted_start_date.'</td>';
    echo'<td>'.$formatted_end_date.'</td>';
    echo'<td>'.$requests['days'].'</td>';
    echo'<td>'.$remainder.'</td>';
    echo'</tr>';
    }

    echo'</table>';

    ?>

1 Answer 1

1

work out in SQL the original entitlement figure minus all the requests for that employee, that academic year and if it was approved

should be this:

select new_entitlement-(select sum(days) from requests where employee="sashn" and academic_year=2012 and approved=1) from holiday_entitlement where employee="sashn";

or as update:

update holiday_entitlement set new_entitlement=27-(select sum(days) from requests where employee="sashn" and academic_year=2012 and approved=1) where employee="sashn" and academic_year=2012;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.