Browse Source

FIX #6156

Laurent Destailleur 8 years ago
parent
commit
7600a10f2d
2 changed files with 50 additions and 25 deletions
  1. 38 16
      htdocs/holiday/class/holiday.class.php
  2. 12 9
      htdocs/hrm/index.php

+ 38 - 16
htdocs/holiday/class/holiday.class.php

@@ -849,25 +849,49 @@ class Holiday extends CommonObject
      *  Return value of a conf parameterfor leave module
      *  TODO Move this into llx_const table
      *
-     *  @param	string	$name       name of parameter
-     *  @return string      		value of parameter
+     *  @param	string	$name                 Name of parameter
+     *  @param  string  $createifnotfound     'stringvalue'=Create entry with string value if not found. For example 'YYYYMMDDHHMMSS'.
+     *  @return string      		          Value of parameter. Example: 'YYYYMMDDHHMMSS' or < 0 if error
      */
-    function getConfCP($name)
+    function getConfCP($name, $createifnotfound='')
     {
         $sql = "SELECT value";
         $sql.= " FROM ".MAIN_DB_PREFIX."holiday_config";
         $sql.= " WHERE name = '".$name."'";
 
-        dol_syslog(get_class($this).'::getConfCP name='.$name.'', LOG_DEBUG);
+        dol_syslog(get_class($this).'::getConfCP name='.$name.' createifnotfound='.$createifnotfound, LOG_DEBUG);
         $result = $this->db->query($sql);
 
-        // Si pas d'erreur
         if($result) {
 
-            $objet = $this->db->fetch_object($result);
-            // Retourne la valeur
-            return $objet->value;
-
+            $obj = $this->db->fetch_object($result);
+            // Return value
+            if (empty($obj))
+            {
+                if ($createifnotfound)
+                {
+                    $sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday_config(name, value)";
+                    $sql.= " VALUES('".$name."', '".$createifnotfound."')";
+                    $result = $this->db->query($sql);
+                    if ($result) 
+                    {
+                        return $createifnotfound;
+                    }
+                    else 
+                    {
+                        $this->error=$this->db->lasterror();
+                        return -2;
+                    }
+                }
+                else
+                {
+                    return '';
+                }
+            }
+            else 
+            {
+                return $obj->value;
+            }
         } else {
 
             // Erreur SQL
@@ -896,26 +920,24 @@ class Holiday extends CommonObject
 			$now=dol_now();
 
             $month = date('m',$now);
-
+            $newdateforlastupdate = dol_print_date($now, '%Y%m%d%H%M%S');
+            
             // Get month of last update
-            $lastUpdate = $this->getConfCP('lastUpdate');
+            $lastUpdate = $this->getConfCP('lastUpdate', $newdateforlastupdate);
             $monthLastUpdate = $lastUpdate[4].$lastUpdate[5];
-			//print 'month: '.$month.' '.$lastUpdate.' '.$monthLastUpdate;exit;
+			//print 'month: '.$month.' lastUpdate:'.$lastUpdate.' monthLastUpdate:'.$monthLastUpdate;exit;
 
             // Si la date du mois n'est pas la même que celle sauvegardée, on met à jour le timestamp
             if ($month != $monthLastUpdate)
             {
-                dol_syslog("A new moth was detected (month=".$month.", monthLastUpdate=".$monthLastUpdate.". We update leave balance.");
-                
             	$this->db->begin();
 
             	$users = $this->fetchUsers(false,false);
 	            $nbUser = count($users);
 
                 $sql = "UPDATE ".MAIN_DB_PREFIX."holiday_config SET";
-                $sql.= " value = '".dol_print_date($now,'%Y%m%d%H%M%S')."'";
+                $sql.= " value = '".$newdateforlastupdate."'";
                 $sql.= " WHERE name = 'lastUpdate'";
-
                 $result = $this->db->query($sql);
 
 				$typeleaves=$this->getTypes(1,1);

+ 12 - 9
htdocs/hrm/index.php

@@ -62,6 +62,12 @@ if ($user->societe_id > 0) accessforbidden();
 $holiday = new Holiday($db);
 $holidaystatic=new Holiday($db);
 
+// Update sold
+if (! empty($conf->holiday->enabled))
+{
+    $result = $holiday->updateBalance();
+}
+
 $childids = $user->getAllChildIds();
 $childids[]=$user->id;
 
@@ -145,8 +151,7 @@ $langs->load("boxes");
 // Last leave requests
 if (! empty($conf->holiday->enabled) && $user->rights->holiday->read)
 {
-    $sql = "SELECT u.rowid as uid, u.lastname, u.firstname, u.login, u.photo, u.statut,";
-    $sql.= " x.rowid, x.rowid as ref, x.fk_type, x.date_debut as date_start, x.date_fin as date_end, x.halfday, x.tms as dm, x.statut as status";
+    $sql = "SELECT u.rowid as uid, u.lastname, u.firstname, u.login, u.photo, u.statut, x.rowid, x.rowid as ref, x.fk_type, x.date_debut as date_start, x.date_fin as date_end, x.halfday, x.tms as dm, x.statut as status";
     $sql.= " FROM ".MAIN_DB_PREFIX."holiday as x, ".MAIN_DB_PREFIX."user as u";
     $sql.= " WHERE u.rowid = x.fk_user";
     $sql.= " AND x.entity = ".$conf->entity;
@@ -183,25 +188,23 @@ if (! empty($conf->holiday->enabled) && $user->rights->holiday->read)
             while ($i < $num && $i < $max)
             {
                 $obj = $db->fetch_object($result);
-                
                 $holidaystatic->id=$obj->rowid;
                 $holidaystatic->ref=$obj->ref;
-                
                 $userstatic->id=$obj->uid;
                 $userstatic->lastname=$obj->lastname;
                 $userstatic->firstname=$obj->firstname;
                 $userstatic->login=$obj->login;
                 $userstatic->photo=$obj->photo;
                 $userstatic->statut=$obj->statut;
-
-                $starthalfday=($obj->halfday == -1 || $obj->halfday == 2)?'afternoon':'morning';
-                $endhalfday=($obj->halfday == 1 || $obj->halfday == 2)?'morning':'afternoon';
-                
                 print '<tr '.$bc[$var].'>';
                 print '<td>'.$holidaystatic->getNomUrl(1).'</td>';
                 print '<td>'.$userstatic->getNomUrl(-1, 'leave').'</td>';
                 print '<td>'.$typeleaves[$obj->fk_type]['label'].'</td>';
-                print '<td>'.dol_print_date($obj->date_start,'day').' '.$langs->trans($listhalfday[$starthalfday]);
+                
+                $starthalfday=($obj->halfday == -1 || $obj->halfday == 2)?'afternoon':'morning';
+                $endhalfday=($obj->halfday == 1 || $obj->halfday == 2)?'morning':'afternoon';
+                
+                print '<td>'.dol_print_date($obj->date_start,'day').' '.$langs->trans($listhalfday[$endhalfday]);
                 print '<td>'.dol_print_date($obj->date_end,'day').' '.$langs->trans($listhalfday[$endhalfday]);
                 print '<td align="right">'.dol_print_date($db->jdate($obj->dm),'day').'</td>';
                 print '<td>'.$holidaystatic->LibStatut($obj->status,3).'</td>';