<?php
  function Arrondi_2_Dec($str)
  {
    $out=substr($str,0,strpos($str,".")+3);
    return $out;
  }
  
  function Min_zero($str)
  {
    if(empty($str))
    {
      $tmp = "0";
    }
    else
    {
      $tmp = $str;
    }
    return $tmp;
  }

  function Arrondi_5_ct($str)
  {
    if(empty($str))
    {
      $tmp = "0.00";
    }
    else
    {
      $tmp=(round($str*20))/20;
      $taille=strlen($tmp);
      $pos=strpos($tmp,'.');
      if($pos === false)
      {
        $tmp.=".00";
      }
      else
      {
        if($taille!=$pos+3)
          $tmp.="0";
      }
    }
    return $tmp;
  }
  
  function Arrondi_1_ct($str)
  {
    $tmp=(round($str*100))/100;
    $taille=strlen($tmp);
    $pos=strpos($tmp,'.');
    if($pos === false)
    {
      $tmp.=".00";
    }
    else
    {
      if($taille!=$pos+3)
        $tmp.="0";
    }
    return $tmp;
  }
  
  function Chiffre_2_Car($str)
  {
    $out=$str;
    if (strlen($out)==1)
    {
      $out = "0" . $out;
    }
    return $out;
  }
  
  function Texte_Sans_Espace($str)
  {
    $out=$str;
    $out=str_replace("-","",$out);
    $out=str_replace(" ","",$out);
    return $out;
  }

  function Create_Select_Mois($date)
  {
    $tmp_mois=substr ($date,5,2);
    echo "<SELECT  name='mois' size='1'>\n";
    if (strcmp($tmp_mois,"01"))
    {
      $tmp_select="";
    }
    else
    {
      $tmp_select=" selected";
    }
    echo "<option value='01'$tmp_select>Janvier  (01)</option>\n";
    if (strcmp($tmp_mois,"02"))
    {
      $tmp_select="";
    }
    else
    {
      $tmp_select=" selected";
    }
    echo "<option value='02'$tmp_select>Février  (02)</option>\n";
    if (strcmp($tmp_mois,"03"))
    {
      $tmp_select="";
    }
    else
    {
      $tmp_select=" selected";
    }
    echo "<option value='03'$tmp_select>Mars     (03)</option>\n";
    if (strcmp($tmp_mois,"04"))
    {
      $tmp_select="";
    }
    else
    {
      $tmp_select=" selected";
    }
    echo "<option value='04'$tmp_select>Avril    (04)</option>\n";
    if (strcmp($tmp_mois,"05"))
    {
      $tmp_select="";
    }
    else
    {
      $tmp_select=" selected";
    }
    echo "<option value='05'$tmp_select>Mai      (05)</option>\n";
    if (strcmp($tmp_mois,"06"))
    {
      $tmp_select="";
    }
    else
    {
      $tmp_select=" selected";
    }
    echo "<option value='06'$tmp_select>Juin     (06)</option>\n";
    if (strcmp($tmp_mois,"07"))
    {
      $tmp_select="";
    }
    else
    {
      $tmp_select=" selected";
    }
    echo "<option value='07'$tmp_select>Juillet  (07)</option>\n";
    if (strcmp($tmp_mois,"08"))
    {
      $tmp_select="";
    }
    else
    {
      $tmp_select=" selected";
    }
    echo "<option value='08'$tmp_select>Aout     (08)</option>\n";
    if (strcmp($tmp_mois,"09"))
    {
      $tmp_select="";
    }
    else
    {
      $tmp_select=" selected";
    }
    echo "<option value='09'$tmp_select>Septembre(09)</option>\n";
    if (strcmp($tmp_mois,"10"))
    {
      $tmp_select="";
    }
    else
    {
      $tmp_select=" selected";
    }
    echo "<option value='10'$tmp_select>Octobre  (10)</option>\n";
    if (strcmp($tmp_mois,"11"))
    {
      $tmp_select="";
    }
    else
    {
      $tmp_select=" selected";
    }
    echo "<option value='11'$tmp_select>Novembre (11)</option>\n";
    if (strcmp($tmp_mois,"12"))
    {
      $tmp_select="";
    }
    else
    {
      $tmp_select=" selected";
    }
    echo "<option value='12'$tmp_select>Decembre (12)</option>\n";
    echo "</SELECT>\n";
  }

  function separevirgule($chaine, $position)
  {
    $out="";
    $cpt=0;
    $pos=0;
    $len=strlen($chaine);
    while($cpt<$len && $pos<=$position)
    {
      if($chaine[$cpt]==',')
      {
        $pos++;
      }
      else
      {
        if($pos==$position)
        {
          if($chaine[$cpt]!=' ')
          {
            $out.=$chaine[$cpt];
          }
        }
      }
      $cpt++;
    }
    return $out;
  }
  function joursdansmois($mois , $annee)
  {
    if ($mois<12)
      return date("d",mktime(0,0,0,$mois+1,0,$annee));
    else
      return date("d",mktime(0,0,0,1,0,$annee+1));
  }
  
  function jour_ferie($jour,$mois,$annee,$mode=0)
  {
    $date = mktime(00,00,00,$mois,$jour,$annee);
    $jour_julien = unixtojd($date);
    $jour_semaine = jddayofweek($jour_julien, 0);
    if($jour_semaine == 0 || $jour_semaine == 6) return 0; // Jour de la semaine (0 pour dimanche et 6 pour samedi)
    if($mois == 9 && $jour_semaine == 4 && $jour >= 5 && $jour <= 11) return 0; // Jeune Genevois
    if($jour == 1 && $mois == 1) return 0; // 1er janvier
    if($jour == 1 && $mois == 8) return 0; // 1er aout
    if($jour == 25 && $mois == 12) return 0; // 25 décembre
    if($jour == 31 && $mois == 12) return 0; // 31 décembre
    $date_paques = easter_date($annee);
    $date_paques_1 = mktime(0,0,0,date("m", $date_paques),date("d", $date_paques) - 2,date("Y", $date_paques));
    $jour_paques = date("d", $date_paques_1);
    $mois_paques = date("m", $date_paques_1);
    if($jour_paques == $jour && $mois_paques == $mois) return 0; // vendredi saint
    $date_paques_2 = mktime(date("H", $date_paques),date("i", $date_paques),date("s", $date_paques),date("m", $date_paques),date("d", $date_paques) + 1,date("Y", $date_paques));
    $jour_paques = date("d", $date_paques_2);
    $mois_paques = date("m", $date_paques_2);
    if($jour_paques == $jour && $mois_paques == $mois) return 0; // Lundi de Pâques
    $date_ascension = mktime(date("H", $date_paques),date("i", $date_paques),date("s", $date_paques),date("m", $date_paques),date("d", $date_paques) + 39,date("Y", $date_paques));
    $jour_ascension = date("d", $date_ascension);
    $mois_ascension = date("m", $date_ascension);
    if($jour_ascension == $jour && $mois_ascension == $mois) return 0; // Ascension
    $date_pentecote = mktime(date("H", $date_ascension),date("i", $date_ascension),date("s", $date_ascension),date("m", $date_ascension),date("d", $date_ascension) + 11, date("Y", $date_ascension));
    $jour_pentecote = date("d", $date_pentecote);
    $mois_pentecote = date("m", $date_pentecote);
    if($jour_pentecote == $jour && $mois_pentecote == $mois) return 0; // Pentecote
    if($mode==1) // FERMÉ LOCATION
    {
    	$date="$annee-$mois-$jour";
      $resultat=AbsenceEmploye::getAbsence(1, $date);
    	if (count($resultat) > 0) {
    	  return 0;
    	}
    }
    else if($mode==2) // FERMÉ ATELIER
    {
    	$date="$annee-$mois-$jour";
      $resultat=AbsenceEmploye::getAbsence(0, $date);
    	if (count($resultat) > 0) {
    	  return 0;
    	}
    }
    return 1;
  }
  
  function nom_jour($jour)
  {
    switch ($jour)
    {
      case 0:
        return "dimanche";
    	break;
    	case 1:
    	  return "lundi";
    	break;
    	case 2:
    	  return "mardi";
    	break;
    	case 3:
    	  return "mercredi";
    	break;
    	case 4:
    	  return "jeudi";
    	break;
    	case 5:
    	  return "vendredi";
    	break;
    	case 6:
    	  return "samedi";
    	break;
    }
  }
  
  function nom_mois($mois)
  {
    switch ($mois)
    {
    	case 1:
    	  return "janvier";
    	break;
    	case 2:
    	  return "février";
    	break;
    	case 3:
    	  return "mars";
    	break;
    	case 4:
    	  return "avril";
    	break;
    	case 5:
    	  return "mai";
    	break;
    	case 6:
    	  return "juin";
    	break;
    	case 7:
    	  return "juillet";
    	break;
    	case 8:
    	  return "août";
    	break;
    	case 9:
    	  return "septembre";
    	break;
    	case 10:
    	  return "octobre";
    	break;
    	case 11:
    	  return "novembre";
    	break;
    	case 12:
    	  return "décembre";
    	break;
    }
  }
  
  function affiche_mois($mois,$annee)
  {
    echo "<TABLE border=1>\n";
    echo "<TR><TD COLSPAN=7>" . nom_mois($mois) . " $annee</TD></TR>\n";
    echo "<TR><TH>Lundi</TH><TH>Mardi</TH><TH>Mercredi</TH><TH>Jeudi</TH><TH>Vendredi</TH><TH>Samedi</TH><TH>Dimanche</TH></TR>\n";
    $date = mktime(0,0,0,$mois,1,$annee);
    $jour_julien = unixtojd($date);
    $jour_semaine = jddayofweek($jour_julien, 0);
    if ($jour_semaine==0)
    {
    	$jour_semaine=7;
    }
    $jours_mois = joursdansmois($mois,$annee);
    $jour_actuel=1;
    echo "<TR>";
    while($jour_actuel<$jour_semaine)
    {
      $jour_actuel++;
      echo "<TD>&nbsp;</TD>";
    }
    $no_jour=1;
    while($jour_actuel<8)
    {
      echo "<TD>$no_jour</TD>";
      $no_jour++;
      $jour_actuel++;
    }
    echo "</TR>\n";
    while($no_jour<=$jours_mois)
    {
      echo "<TR>";
      $jour_actuel=1;
      while($jour_actuel<8)
      {
      	if($no_jour<=$jours_mois)
      	{
      		echo "<TD>$no_jour</TD>";
      	}
      	else
      	{
      		echo "<TD>&nbsp;</TD>";
      	}
        $jour_actuel++;
        $no_jour++;
      }
      echo "</TR>\n";
    }
    echo "</TABLE>\n";
  }
  
  function taille_text($text,$taille,$align)
  {
    $len=strlen($text);
    if($len>=$taille)
    {
    	return substr($text,0,$taille);
    }
    else
    {
    	$out=$text;
    	for($cpt=$len;$cpt<$taille;$cpt++)
    	{
    	  if($align==0)
    	  {
    	  	$out.=" ";
  	  }
  	  else
  	  {
  	  	$out=" ".$out;
  	  }
  	}
  	return $out;
    }
  }
  
  function liste_niveau_rappel($default,$nocommande)
  {
  	$out ="<SELECT";
  	$out.=" name='niv$nocommande'";
  	$out.=" onChange='change_status($nocommande,this.value);'";
  	$out.=">";
  	$out.="<option value='0'";
  	if($default==0) $out.=" selected";
  	$out.=">normal</option>";
  	$out.="<option value='1'";
  	if($default==1) $out.=" selected";
  	$out.=">Premier rappel</option>";
  	$out.="<option value='2'";
  	if($default==2) $out.=" selected";
  	$out.=">Dernier rappel</option>";
  	$out.="<option value='3'";
  	if($default==3) $out.=" selected";
  	$out.=">Poursuite</option>";
  	$out.="<option value='4'";
  	if($default==4) $out.=" selected";
  	$out.=">En poursuite</option>";
  	$out.="</SELECT>";
  	return $out;
  }
  
  function tableau_rappel($degre,$type_fact)
  {
    if($resultat=Commande::getRappel($degre,date("Y-m-d"),$type_fact))
    {
    }
    else
    {
      //echo Connection::getConnection()->errorInfo();
    }
    echo "$degre $type_fact<BR>\n";
    echo "<TABLE border='1'>";
    echo "<TR>";
    if($degre<3 && $degre>0)
    {
      echo "<TH><input type='checkbox' name='AAAlet$degre$type_fact' checked onclick='set_checkbox(".'"let",'.'"'.$degre.'"'.",".'"'.$type_fact.'"'.",this.checked);'>Lettre</TH>";
    }
    echo "<TH><input type='checkbox' name='AAAfac$degre$type_fact' checked onclick='set_checkbox(".'"fac",'.'"'.$degre.'"'.",".'"'.$type_fact.'"'.",this.checked);'>Facture</TH><TH>Client imprimé</TH>";
    echo "<TH>NoCommande</TH><TH>Client</TH><TH>Client2</TH><TH>Date facture</TH><TH>Date délais payement</TH><TH>Montant total</TH><TH>Montant à payer</TH><TH>Date dernier payement</TH>";
    echo "<TH>Imprimer</TH>";
    echo "<TH>PDF</TH>";
    echo "<TH>Niveau rappel</TH>";
    echo "</TR>\n";
    $nb=0;
    foreach ($resultat as $ligne)
    {
      if($nb >= 1000) {
        break;
      }  
	  $resultatadressewrong = Client::getClientByMailingPaperWrong($ligne["NoClient"], $ligne["NoClient2"]);
	  if (count($resultatadressewrong)>0)
	  {
	    echo "<TR style=\"background-color:rgba(255,255,0,1);\">";
	  }
	  else
	  {
        echo "<TR>";
	  }
      if($degre<3 && $degre>0)
      {
  	    echo "<TD><input type='checkbox' name='let".$ligne["NoCommande"]."' id='let$type_fact$degre' checked></TD>";
    	}
  	  echo "<TD><input type='checkbox' name='fac".$ligne["NoCommande"]."' id='fac$type_fact$degre' checked></TD>";
    	echo "<TD><SELECT name='cli".$ligne["NoCommande"]."'><option value='1'>Client 1</option>";
    	if ($ligne["NoClient2"]!=0)
    	{
    	  echo "<option value='2' selected>Client 2</option>";
    	}
    	echo "</SELECT></TD><TD>".Cree_Entree_Type("date",32,0,$ligne["NoCommande"],AFFICHAGE)."</TD><TD>".Cree_Entree_Type("date",14,0,$ligne["NoClient"],AFFICHAGE)."</TD><TD>";
    	if ($ligne["NoClient2"]!=0)
    	{
    	  echo Cree_Entree_Type("date",14,0,$ligne["NoClient2"],AFFICHAGE);
    	}
    	else
    	{
    	  echo "-";
    	}
    	echo "</TD><TD>".Cree_Entree_Type("date",2,0,$ligne["DateCommande"],AFFICHAGE)."</TD><TD>".Cree_Entree_Type("date",2,0,$ligne["DateDelaisPayement"],AFFICHAGE)."</TD><TD>".Cree_Entree_Type("date",19,0,$ligne["MontantTotal"],AFFICHAGE)."</TD><TD>".Cree_Entree_Type("date",19,0,$ligne["APayer"],AFFICHAGE)."</TD><TD>".Cree_Entree_Type("date",2,0,$ligne["DatePayement"],AFFICHAGE)."</TD>";
      echo "<TD><input type='button' value='Imprimer' onClick='imprime_unique(".$ligne["NoCommande"].");'></TD>";
      echo "<TD><input type='button' value='PDF' onClick='imprime_unique_pdf(".$ligne["NoCommande"].");'></TD>";
      echo "<TD>".liste_niveau_rappel($ligne['DegreSommation'],$ligne['NoCommande'])."</TD>";
      echo "</TR>\n";
      $nb++;
    }
    echo "</TABLE><BR>\n";
    echo "<button type='button' onClick='imprime_groupe($degre,".'"'.$type_fact.'"'.");'>Imprimer groupe</button><BR><BR>\n";
  }
  
  function aligne_gauche_select($txt,$taille)
  {
    $stmp=strlen($txt);
    if ($stmp>$taille)
    {
      $tmp=substr($txt,0,$taille);
    }
    else
    {
      $tmp=$txt;
      if ($stmp<$taille)
      {
        for($ptmp=$stmp;$ptmp<$taille;$ptmp++)
        {
          $tmp.="&nbsp;";
        }
      }
    }
    $tmp=str_replace(" ","&nbsp;",$tmp);
    return $tmp;
  }
  
  function aligne_droite_select($txt,$taille)
  {
    $stmp=strlen($txt);
    if ($stmp>$taille)
    {
      $tmp=substr($txt,0,$taille);
    }
    else
    {
      $tmp=$txt;
      if ($stmp<$taille)
      {
        for($ptmp=$stmp;$ptmp<$taille;$ptmp++)
        {
          $tmp="&nbsp;" . $tmp;
        }
      }
    }
    $tmp=str_replace(" ","&nbsp;",$tmp);
    return $tmp;
  }
  
?>