function addOut($date_in, $date_out, $qty)
 {
     $formula = '';
     //$date_in  = date('Y-m-d', strtotime($date_in));
     //$date_out = date('Y-m-d', strtotime($date_out));
     //$dur = abs(strtotime($date_out) - strtotime($date_in)) / (60*60*24);
     $date_in_int = strtotime($date_in);
     $date_out_int = strtotime($date_out);
     $date_in = date('Y-m-d', $date_in_int);
     $date_out = date('Y-m-d', $date_out_int);
     $dur = countWorkingDays($date_in_int, $date_out_int);
     if ($dur == 1 && $date_in != $date_out) {
         $dur += 1;
     }
     //weekend will be consider as 1 day, +1 to set it as next day
     switch ($dur) {
         case 1:
             //finish in same day
             $formula = "products_out=products_out+{$qty}";
             break;
         case 2:
             //finish next day
             $formula = "next_day=next_day+{$qty}";
             break;
         default:
             //finish in more than 2 (working) days
             $formula = "later=later+{$qty}";
     }
     tep_db_query("UPDATE production_target SET {$formula} WHERE date_id=DATE('{$date_in}')");
     if ($dur == 0 || $dur == 1) {
         $this->queryPercentage($date_in);
     }
 }
function countWorkingDays($s, $e, $holidays = array())
{
    // If string is given, changed to Time integer
    if (is_string($s)) {
        $s = strtotime($s);
    }
    if (is_string($e)) {
        $e = strtotime($e);
    }
    // If the start and end dates are given in the wrong order, flip them.
    if ($s > $e) {
        return countWorkingDays($e, $s, $holidays);
    }
    // Find the ISO-8601 day of the week for the two dates.
    $sd = date("N", $s);
    $ed = date("N", $e);
    // Find the number of weeks between the dates.
    $w = floor(($e - $s) / (86400 * 7));
    # Divide the difference in the two times by seven days to get the number of weeks.
    if ($ed >= $sd) {
        $w--;
    }
    # If the end date falls on the same day of the week or a later day of the week than the start date, subtract a week.
    // Calculate net working days.
    $nwd = max(6 - $sd, 0);
    # If the start day is Saturday or Sunday, add zero, otherewise add six minus the weekday number.
    $nwd += min($ed, 5);
    # If the end day is Saturday or Sunday, add five, otherwise add the weekday number.
    $nwd += $w * 5;
    # Add five days for each week in between.
    // Iterate through the array of holidays. For each holiday between the start and end dates that isn't a Saturday or a Sunday, remove one day.
    foreach ($holidays as $h) {
        $h = strtotime($h);
        if ($h > $s && $h < $e && date("N", $h) < 6) {
            $nwd--;
        }
    }
    return $nwd;
}