Fix: Number of day calculation. Add PHPUnit case.

This commit is contained in:
Laurent Destailleur 2014-03-05 20:52:27 +01:00
parent dd3169e1ed
commit 774017672f
12 changed files with 134 additions and 102 deletions

View File

@ -382,13 +382,15 @@ interface Database
);
/**
* Convert (by PHP) a PHP server TZ string date into a GM Timestamps date
* 19700101020000 -> 3600 with TZ+1
*
* @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
* @return date Date TMS
*/
function jdate($string);
* Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true)
* 19700101020000 -> 3600 with TZ+1 and gmt=0
* 19700101020000 -> 7200 whaterver is TZ if gmt=1
*
* @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
* @param int $gm 1=Input informations are GMT values, otherwise local to server TZ
* @return date Date TMS
*/
function jdate($string, $gm=false);
/**
* Encrypt sensitive data in database

View File

@ -553,20 +553,22 @@ class DoliDBMssql extends DoliDB
return dol_print_date($param,"%Y-%m-%d %H:%M:%S");
}
/**
* Convert (by PHP) a PHP server TZ string date into a GM Timestamps date
* 19700101020000 -> 3600 with TZ+1
*
* @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
* @return date Date TMS
*/
function jdate($string)
{
$string=preg_replace('/([^0-9])/i','',$string);
$tmp=$string.'000000';
$date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4));
return $date;
}
/**
* Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true)
* 19700101020000 -> 3600 with TZ+1 and gmt=0
* 19700101020000 -> 7200 whaterver is TZ if gmt=1
*
* @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
* @param int $gm 1=Input informations are GMT values, otherwise local to server TZ
* @return date Date TMS
*/
function jdate($string, $gm=false)
{
$string=preg_replace('/([^0-9])/i','',$string);
$tmp=$string.'000000';
$date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4),$gm);
return $date;
}
/**
* Format a SQL IF

View File

@ -531,20 +531,22 @@ class DoliDBMysql extends DoliDB
return dol_print_date($param,"%Y%m%d%H%M%S");
}
/**
* Convert (by PHP) a PHP server TZ string date into a GM Timestamps date
* 19700101020000 -> 3600 with TZ+1
*
* @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
* @return date Date TMS
*/
function jdate($string)
{
$string=preg_replace('/([^0-9])/i','',$string);
$tmp=$string.'000000';
$date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4));
return $date;
}
/**
* Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true)
* 19700101020000 -> 3600 with TZ+1 and gmt=0
* 19700101020000 -> 7200 whaterver is TZ if gmt=1
*
* @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
* @param int $gm 1=Input informations are GMT values, otherwise local to server TZ
* @return date Date TMS
*/
function jdate($string, $gm=false)
{
$string=preg_replace('/([^0-9])/i','',$string);
$tmp=$string.'000000';
$date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4),$gm);
return $date;
}
/**
* Format a SQL IF

View File

@ -542,17 +542,19 @@ class DoliDBMysqli extends DoliDB
}
/**
* Convert (by PHP) a PHP server TZ string date into a GM Timestamps date
* 19700101020000 -> 3600 with TZ+1
* Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true)
* 19700101020000 -> 3600 with TZ+1 and gmt=0
* 19700101020000 -> 7200 whaterver is TZ if gmt=1
*
* @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
* @return date Date TMS
* @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
* @param int $gm 1=Input informations are GMT values, otherwise local to server TZ
* @return date Date TMS
*/
function jdate($string)
function jdate($string, $gm=false)
{
$string=preg_replace('/([^0-9])/i','',$string);
$tmp=$string.'000000';
$date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4));
$date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4),$gm);
return $date;
}

View File

@ -752,20 +752,22 @@ class DoliDBPgsql extends DoliDB
return dol_print_date($param,"%Y-%m-%d %H:%M:%S");
}
/**
* Convert (by PHP) a PHP server TZ string date into a GM Timestamps date
* 19700101020000 -> 3600 with TZ+1
*
* @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
* @return date Date TMS
*/
function jdate($string)
{
$string=preg_replace('/([^0-9])/i','',$string);
$tmp=$string.'000000';
$date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4));
return $date;
}
/**
* Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true)
* 19700101020000 -> 3600 with TZ+1 and gmt=0
* 19700101020000 -> 7200 whaterver is TZ if gmt=1
*
* @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
* @param int $gm 1=Input informations are GMT values, otherwise local to server TZ
* @return date Date TMS
*/
function jdate($string, $gm=false)
{
$string=preg_replace('/([^0-9])/i','',$string);
$tmp=$string.'000000';
$date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4),$gm);
return $date;
}
/**
* Format a SQL IF

View File

@ -675,20 +675,23 @@ class DoliDBSqlite extends DoliDB
}
/**
* Convert (by PHP) a PHP server TZ string date into a GM Timestamps date
* 19700101020000 -> 3600 with TZ+1
* Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true)
* 19700101020000 -> 3600 with TZ+1 and gmt=0
* 19700101020000 -> 7200 whaterver is TZ if gmt=1
*
* @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
* @return date Date TMS
* @param string $string Date in a string (YYYYMMDDHHMMSS, YYYYMMDD, YYYY-MM-DD HH:MM:SS)
* @param int $gm 1=Input informations are GMT values, otherwise local to server TZ
* @return date Date TMS
*/
function jdate($string)
function jdate($string, $gmt=false)
{
$string=preg_replace('/([^0-9])/i','',$string);
$tmp=$string.'000000';
$date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4));
$date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4),$gm);
return $date;
}
/**
* Format a SQL IF
*

View File

@ -716,11 +716,11 @@ function num_public_holiday($timestampStart, $timestampEnd, $countrycode='FR')
}
/**
* Fonction retournant le nombre de jour entre deux dates
* Function to return number of days between two dates (date must be UTC date !)
* Example: 2012-01-01 2012-01-02 => 1 if lastday=0, 2 if lastday=1
*
* @param timestamp $timestampStart Timestamp de debut
* @param timestamp $timestampEnd Timestamp de fin
* @param timestamp $timestampStart Timestamp start UTC
* @param timestamp $timestampEnd Timestamp end UTC
* @param int $lastday Last day is included, 0: non, 1:oui
* @return int Number of days
*/
@ -745,9 +745,9 @@ function num_between_day($timestampStart, $timestampEnd, $lastday=0)
/**
* Function to return number of working days (and text of units) between two dates (working days)
*
* @param timestamp $timestampStart Timestamp for start date
* @param timestamp $timestampEnd Timestamp for end date
* @param int $inhour 0: return number of days, 1: return number of hours (72h max)
* @param timestamp $timestampStart Timestamp for start date (date must be UTC to avoid calculation errors)
* @param timestamp $timestampEnd Timestamp for end date (date must be UTC to avoid calculation errors)
* @param int $inhour 0: return number of days, 1: return number of hours
* @param int $lastday We include last day, 0: no, 1:yes
* @param int $halfday Tag to define half day when holiday start and end
* @return int Number of days or hours

View File

@ -44,8 +44,10 @@ class Holiday extends CommonObject
var $fk_user;
var $date_create='';
var $description;
var $date_debut='';
var $date_fin='';
var $date_debut=''; // Date start in PHP server TZ
var $date_fin=''; // Date end in PHP server TZ
var $date_debut_gmt=''; // Date start in GMT
var $date_fin_gmt=''; // Date end in GMT
var $halfday='';
var $statut=''; // 1=draft, 2=validated, 3=approved
var $fk_validator;
@ -214,6 +216,8 @@ class Holiday extends CommonObject
$this->description = $obj->description;
$this->date_debut = $this->db->jdate($obj->date_debut);
$this->date_fin = $this->db->jdate($obj->date_fin);
$this->date_debut_gmt = $this->db->jdate($obj->date_debut,1);
$this->date_fin_gmt = $this->db->jdate($obj->date_fin,1);
$this->halfday = $obj->halfday;
$this->statut = $obj->statut;
$this->fk_validator = $obj->fk_validator;
@ -317,6 +321,8 @@ class Holiday extends CommonObject
$tab_result[$i]['description'] = $obj->description;
$tab_result[$i]['date_debut'] = $this->db->jdate($obj->date_debut);
$tab_result[$i]['date_fin'] = $this->db->jdate($obj->date_fin);
$tab_result[$i]['date_debut_gmt'] = $this->db->jdate($obj->date_debut,1);
$tab_result[$i]['date_fin_gmt'] = $this->db->jdate($obj->date_fin,1);
$tab_result[$i]['halfday'] = $obj->halfday;
$tab_result[$i]['statut'] = $obj->statut;
$tab_result[$i]['fk_validator'] = $obj->fk_validator;
@ -426,6 +432,8 @@ class Holiday extends CommonObject
$tab_result[$i]['description'] = $obj->description;
$tab_result[$i]['date_debut'] = $this->db->jdate($obj->date_debut);
$tab_result[$i]['date_fin'] = $this->db->jdate($obj->date_fin);
$tab_result[$i]['date_debut_gmt'] = $this->db->jdate($obj->date_debut,1);
$tab_result[$i]['date_fin_gmt'] = $this->db->jdate($obj->date_fin,1);
$tab_result[$i]['halfday'] = $obj->halfday;
$tab_result[$i]['statut'] = $obj->statut;
$tab_result[$i]['fk_validator'] = $obj->fk_validator;

View File

@ -64,6 +64,8 @@ if ($action == 'create')
$date_debut = dol_mktime(0, 0, 0, GETPOST('date_debut_month'), GETPOST('date_debut_day'), GETPOST('date_debut_year'));
$date_fin = dol_mktime(0, 0, 0, GETPOST('date_fin_month'), GETPOST('date_fin_day'), GETPOST('date_fin_year'));
$date_debut_gmt = dol_mktime(0, 0, 0, GETPOST('date_debut_month'), GETPOST('date_debut_day'), GETPOST('date_debut_year'), 1);
$date_fin_gmt = dol_mktime(0, 0, 0, GETPOST('date_fin_month'), GETPOST('date_fin_day'), GETPOST('date_fin_year'), 1);
$starthalfday=GETPOST('starthalfday');
$endhalfday=GETPOST('endhalfday');
$halfday=0;
@ -105,7 +107,7 @@ if ($action == 'create')
}
// Si aucun jours ouvrés dans la demande
$nbopenedday=num_open_day($date_debut, $date_fin, 0, 1, $halfday);
$nbopenedday=num_open_day($date_debut_gmt, $date_fin_gmt, 0, 1, $halfday);
if($nbopenedday < 1)
{
header('Location: fiche.php?action=request&error=DureeHoliday');
@ -147,6 +149,8 @@ if ($action == 'update')
{
$date_debut = dol_mktime(0, 0, 0, GETPOST('date_debut_month'), GETPOST('date_debut_day'), GETPOST('date_debut_year'));
$date_fin = dol_mktime(0, 0, 0, GETPOST('date_fin_month'), GETPOST('date_fin_day'), GETPOST('date_fin_year'));
$date_debut_gmt = dol_mktime(0, 0, 0, GETPOST('date_debut_month'), GETPOST('date_debut_day'), GETPOST('date_debut_year'), 1);
$date_fin_gmt = dol_mktime(0, 0, 0, GETPOST('date_fin_month'), GETPOST('date_fin_day'), GETPOST('date_fin_year'), 1);
$starthalfday=GETPOST('starthalfday');
$endhalfday=GETPOST('endhalfday');
$halfday=0;
@ -198,7 +202,7 @@ if ($action == 'update')
}
// Si pas de jours ouvrés dans la demande
$nbopenedday=num_open_day($date_debut, $date_fin, 0, 1, $halfday);
$nbopenedday=num_open_day($date_debut_gmt, $date_fin_gmt, 0, 1, $halfday);
if ($nbopenedday < 1)
{
header('Location: fiche.php?id='.$_POST['holiday_id'].'&action=edit&error=DureeHoliday');
@ -237,9 +241,9 @@ if ($action == 'confirm_delete' && GETPOST('confirm') == 'yes')
if($user->rights->holiday->delete)
{
$error=0;
$db->begin();
$cp = new Holiday($db);
$cp->fetch($id);
@ -251,12 +255,12 @@ if ($action == 'confirm_delete' && GETPOST('confirm') == 'yes')
{
$result=$cp->delete($id);
}
else
else
{
$error = $langs->trans('ErrorCantDeleteCP');
}
}
if (! $error)
{
$db->commit();
@ -265,7 +269,7 @@ if ($action == 'confirm_delete' && GETPOST('confirm') == 'yes')
}
else
{
$db->rollback();
$db->rollback();
}
}
}
@ -331,7 +335,7 @@ if ($action == 'confirm_send')
// Si l'option pour avertir le valideur en cas de solde inférieur à la demande
if ($cp->getConfCP('AlertValidatorSolde'))
{
$nbopenedday=num_open_day($cp->date_debut,$cp->date_fin,0,1,$cp->halfday);
$nbopenedday=num_open_day($cp->date_debut_gmt,$cp->date_fin_gmt,0,1,$cp->halfday);
if ($nbopenedday > $cp->getCPforUser($cp->fk_user))
{
$message.= "\n";
@ -384,10 +388,10 @@ if($action == 'confirm_valid')
$verif = $cp->update($user->id);
// Si pas d'erreur SQL on redirige vers la fiche de la demande
if ($verif > 0)
if ($verif > 0)
{
// Calculcate number of days consummed
$nbopenedday=num_open_day($cp->date_debut,$cp->date_fin,0,1);
$nbopenedday=num_open_day($cp->date_debut_gmt,$cp->date_fin_gmt,0,1);
$soldeActuel = $cp->getCpforUser($cp->fk_user);
$newSolde = $soldeActuel - ($nbopenedday * $cp->getConfCP('nbHolidayDeducted'));
@ -470,7 +474,7 @@ if ($action == 'confirm_refuse')
$verif = $cp->update($user->id);
// Si pas d'erreur SQL on redirige vers la fiche de la demande
if ($verif > 0)
if ($verif > 0)
{
// To
$destinataire = new User($db);
@ -541,7 +545,7 @@ if ($action == 'confirm_cancel' && GETPOST('confirm') == 'yes')
if (($cp->statut == 2 || $cp->statut == 3) && ($user->id == $cp->fk_validator || $user->id == $cp->fk_user))
{
$db->begin();
$oldstatus = $cp->statut;
$cp->date_cancel = dol_now();
$cp->fk_user_cancel = $user->id;
@ -552,7 +556,7 @@ if ($action == 'confirm_cancel' && GETPOST('confirm') == 'yes')
if ($result >= 0 && $oldstatus == 3) // holiday was already validated, status 3, so we must increase back sold
{
// Calculcate number of days consummed
$nbopenedday=num_open_day($cp->date_debut,$cp->date_fin,0,1,$cp->halfday);
$nbopenedday=num_open_day($cp->date_debut_gmt,$cp->date_fin_gmt,0,1,$cp->halfday);
$soldeActuel = $cp->getCpforUser($cp->fk_user);
$newSolde = $soldeActuel + ($nbopenedday * $cp->getConfCP('nbHolidayDeducted'));
@ -568,16 +572,16 @@ if ($action == 'confirm_cancel' && GETPOST('confirm') == 'yes')
$error = $langs->trans('ErrorCantDeleteCP');
}
}
if (! $error)
{
$db->commit();
$db->commit();
}
else
{
$db->rollback();
}
// Si pas d'erreur SQL on redirige vers la fiche de la demande
if (! $error && $result > 0)
{
@ -874,7 +878,7 @@ else
if($user->id == $cp->fk_user || $user->rights->holiday->lire_tous)
{
if ($action == 'delete')
if ($action == 'delete')
{
if($user->rights->holiday->delete)
{
@ -943,8 +947,8 @@ else
print $langs->trans($listhalfday[$starthalfday]);
print '</td>';
print '</tr>';
}
else
}
else
{
print '<tr>';
print '<td>'.$langs->trans('DateDebCP').' ('.$langs->trans("FirstDayOfHoliday").')</td>';
@ -965,8 +969,8 @@ else
print $langs->trans($listhalfday[$endhalfday]);
print '</td>';
print '</tr>';
}
else
}
else
{
print '<tr>';
print '<td>'.$langs->trans('DateFinCP').' ('.$langs->trans("LastDayOfHoliday").')</td>';
@ -979,7 +983,7 @@ else
}
print '<tr>';
print '<td>'.$langs->trans('NbUseDaysCP').'</td>';
print '<td>'.num_open_day($cp->date_debut, $cp->date_fin, 0, 1, $cp->halfday).'</td>';
print '<td>'.num_open_day($cp->date_debut_gmt, $cp->date_fin_gmt, 0, 1, $cp->halfday).'</td>';
print '</tr>';
// Status

View File

@ -249,7 +249,7 @@ if ($id > 0)
}
else {
print '</div>';
}
}
print '<form method="get" action="'.$_SERVER["PHP_SELF"].'">'."\n";
print '<table class="noborder" width="100%;">';
@ -299,7 +299,7 @@ if($user->rights->holiday->lire_tous)
$form->select_users($search_valideur,"search_valideur",1,"",0,$valideurarray,'');
print '</td>';
}
else
else
{
print '<td class="liste_titre">&nbsp;</td>';
}
@ -366,7 +366,7 @@ if (! empty($holiday->holiday))
print '<td align="center">'.dol_print_date($infos_CP['date_debut'],'day').'</td>';
print '<td align="center">'.dol_print_date($infos_CP['date_fin'],'day').'</td>';
print '<td align="right">';
$nbopenedday=num_open_day($infos_CP['date_debut'], $infos_CP['date_fin'], 0, 1, $infos_CP['halfday']);
$nbopenedday=num_open_day($infos_CP['date_debut_gmt'], $infos_CP['date_fin_gmt'], 0, 1, $infos_CP['halfday']);
print $nbopenedday.' '.$langs->trans('DurationDays');
print '<td align="right" colspan="2">'.$holidaystatic->LibStatut($infos_CP['statut'],5).'</td>';
print '</tr>'."\n";

View File

@ -119,13 +119,8 @@ if($num == '0') {
$start_date=$db->jdate($holiday['date_debut']);
$end_date=$db->jdate($holiday['date_fin']);
/*if(substr($holiday['date_debut'],5,2)==$month-1){
$holiday['date_debut'] = date('Y-'.$month.'-01');
}
if(substr($holiday['date_fin'],5,2)==$month+1){
$holiday['date_fin'] = date('Y-'.$month.'-t');
}*/
$start_date_gmt=$db->jdate($holiday['date_debut'],1);
$end_date_gmt=$db->jdate($holiday['date_fin'],1);
print '<tr '.$bc[$var].'>';
print '<td>'.$holidaystatic->getNomUrl(1).'</td>';
@ -135,7 +130,7 @@ if($num == '0') {
print '<td>'.dol_print_date($end_date,'day');
print '</td>';
print '<td align="right">';
$nbopenedday=num_open_day($start_date, $end_date, 0, 1, $holiday['halfday']);
$nbopenedday=num_open_day($start_date_gmt, $end_date_gmt, 0, 1, $holiday['halfday']);
print $nbopenedday;
print '</td>';
print '</tr>';

View File

@ -150,6 +150,18 @@ class DateLibTest extends PHPUnit_Framework_TestCase
print __METHOD__." result=".$result."\n";
$this->assertEquals(1,$result);
// With different date before and after sunlight hour (day to change is 2014-03-30)
$date1=dol_mktime(0, 0, 0, 3, 28, 2014, true);
$date2=dol_mktime(0, 0, 0, 3, 31, 2014, true);
$result=num_between_day($date1,$date2,1);
print __METHOD__." result=".$result."\n";
$this->assertEquals(4,$result);
$result=num_between_day($date1,$date2,0);
print __METHOD__." result=".$result."\n";
$this->assertEquals(3,$result);
return $result;
}