<?php
function Categorie_Location($nom,$valeur)
{
  echo "<SELECT name='$nom'>\n";
  echo "<OPTION value='0'";
  if ($valeur==0)
  {
    echo " selected";
  }
  echo ">Toutes</OPTION>\n";
  $requette = "SELECT * FROM catlocation ORDER BY No";
  echo "</SELECT>\n";
}
function nom_tarif($no_tarif)
{
  switch($no_tarif)
  {
  	case 1:
  	  return "Location 1 à 2 jours";
  	case 2:
  	  return "Location 3 à 6 jours";
  	case 3:
  	  return "Location 7 à 12 jours";
  	case 4:
  	  return "Location 13 + jours";
  	case 5:
//  	  return "Location 13 à 15 jours";
  	break;
  	case 6:
  	  return "Location mensuelle";
  	case 7:
  	  return "Location WE Mini";
  	case 8:
  	  return "Location WE Standard";
  	case 9:
  	  return "Location WE Maxi";
  }
}
function db_tarif($no_tarif)
{
  switch($no_tarif)
  {
  	case 1:
  	  return "Prix1_2";
  	case 2:
  	  return "Prix3_5";
  	case 3:
  	  return "Prix6_9";
  	case 4:
  	  return "Prix10_12";
  	case 5:
  	  return "Prix13_15";
  	case 6:
  	  return "PrixMois";
  	case 7:
  	  return "PrixWE1";
  	case 8:
  	  return "PrixWE2";
  	case 9:
  	  return "PrixWE3";
  }
}	
function db_kil($no_tarif)
{
  switch($no_tarif)
  {
  	case 1:
  	  return "Kil1_2";
  	case 2:
  	  return "Kil3_5";
  	case 3:
  	  return "Kil6_9";
  	case 4:
  	  return "Kil10_12";
  	case 5:
  	  return "Kil13_15";
  	case 6:
  	  return "KilMois";
  	case 7:
  	  return "KilWE1";
  	case 8:
  	  return "KilWE2";
  	case 9:
  	  return "KilWE3";
  }
}
function db_sup($no_tarif)
{
  switch($no_tarif)
  {
  	case 1:
  	  return "Sup1_2";
  	case 2:
  	  return "Sup3_5";
  	case 3:
  	  return "Sup6_9";
  	case 4:
  	  return "Sup10_12";
  	case 5:
  	  return "Sup13_15";
  	case 6:
  	  return "SupMois";
  	case 7:
  	  return "SupWE1";
  	case 8:
  	  return "SupWE2";
  	case 9:
  	  return "SupWE3";
  }
}

  function location_bloc_vehicule($ligne)
  {
    if ($_GET["new"]==1)
    {
      $resultat2=VehiculesLocation::getVehiculeLocation();
    }
    else 
    {
      $resultat2=VehiculesLocation::getVehiculeLocationByNoChassisAndCategoryMultiNot3($ligne['NoChasis']);
    }
    echo "<TR><TH>Vehicule</TH><TD colspan='3'><select name='new_vhc' onChange='modif_vhc();'>";
    $tarif_actuel=0;
	//$no_tarif=0;
    if ($resultat2)
    {
      foreach($resultat2 as $ligne2)
      {
          if($ligne3 = Vehicule::getVehiculeByNoChassis($ligne2["NoChasis"]))
      	  {
			      if (!strcmp($ligne3['NoChasis'],$_GET['vhc']))
      	    {
      	      echo "\n<script language=javascript>\n";
              echo "document.tarifs=new Array;\n";
              //echo "tabTarifs.push(tarifs);\n";
              echo "document.tarifs[0]=".$ligne2['Prix1_2'].";\n";
              echo "document.tarifs[1]=".$ligne2['Prix3_5'].";\n";
              echo "document.tarifs[2]=".$ligne2['Prix6_9'].";\n";
              echo "document.tarifs[3]=".$ligne2['Prix10_12'].";\n";
              echo "document.tarifs[4]=".$ligne2['Prix13_15'].";\n";
              echo "document.tarifs[5]=".$ligne2['PrixMois'].";\n";
              echo "document.tarifs[6]=".$ligne2['PrixWE1'].";\n";
              echo "document.tarifs[7]=".$ligne2['PrixWE2'].";\n";
              echo "document.tarifs[8]=".$ligne2['PrixWE3'].";\n";
	          $etatVhc = $ligne2['EtatVhc'];
	          $etatVhc = str_replace("\n", "\\n", $etatVhc);
              echo "document.etatActuel=".'"'.$etatVhc.'"'.";\n";
              echo "document.kmActuel=".$ligne2['KmActuel'].";\n";
              echo "</script>\n";
      	    }
      	    echo "<option value='" . addslashes($ligne3['NoChasis']) . "'";
      	    if (!strcmp($ligne3['NoChasis'],$_GET['vhc']))
      	    {
      	      echo " selected";
              if ($ligne!=null)
              {
                if (is_null($ligne['EtatDepart']))
              {
                $etat = $ligne2['EtatVhc'];
              }
              else
              {
                $etat = $ligne['EtatDepart'];
              }
              }
              else
              {
                      $etat = $ligne2['EtatVhc'];
              }
      	      //$tarif_actuel=$no_tarif;
              if ($_GET["new"]==1)
              {
                $GLOBALS['tarif'] = $ligne2[db_tarif(1)];                
              }
              else 
              {
                $GLOBALS['tarif'] = $ligne2[db_tarif($ligne['TypeTarif'])];                
              }
              $GLOBALS['kmactuel'] = $ligne2['KmActuel'];
      	    }
			      echo ">" . str_replace(" ","&nbsp;",taille_text($ligne3['Model'], 10, 0)) . " " . str_replace(" ","&nbsp;",taille_text($ligne2['NoPlaque'], 10, 0)) . " " . taille_text($ligne2['NoChasis'], 20, 0) ."</option>";
          }
        //$no_tarif++;
      }
	}
    echo "\n<script language=javascript>\n";
    echo "var tarif_actuel=$tarif_actuel;\n";
    echo "</script>\n";
    echo "</select>&nbsp;";
    if ($_GET["new"]==1)
    {
      echo "<SELECT name='Pret'><OPTION value='0'>Location</OPTION><OPTION value='1'>Pret</OPTION></SELECT>";
    }
    else 
    {
      echo "<SELECT name='Pret'><OPTION value='0'";
      if($ligne['Pret']==0)
      {
        echo " selected";
      }	
      echo ">Location</OPTION><OPTION value='1'";
      if($ligne['Pret']==1)
      {
        echo " selected";
      }
      echo ">Pret</OPTION></SELECT>";
    }
	echo "</TD></TR><TR><TH>Etat départ</TH><TD colspan='3'><textarea cols='50' rows='4' name='etat'>$etat</textarea><button type='button' style='width:150px;' onClick='update_etat_depart_vhc();'>MAJ Etat Vhc location</button></TD></TR>";
  }
  
  function location_bloc_client($ligne)
  {
    if ($_GET["new"]==1)
    {
      if(isset($_GET["NoClient"]))
      {
        $noclient=$_GET["NoClient"];
        $nom_client=$_GET["Nom"];
        $prenom_client=$_GET["Prenom"];
        $no_telephone=$_GET["Telephone"];
      }
      else 
      {
        $noclient="";
        $nom_client="";
        $prenom_client="";
        $no_telephone="";
      }
      $cc=6;
      $ncc="";
      $ecc="";
      $scc="";
	  $enccc="";
    }
    else
    {
      $noclient=$ligne['NoClient'];
      $cc=$ligne['TypeCaution'];
      $ncc=$ligne['NoCC'];
      $ecc=$ligne['ExpCC'];
      $scc=$ligne['SecCC'];
      $enccc=$ligne['EncCC'];
      $nom_client=$ligne['Nom'];
      $prenom_client=$ligne['Prenom'];
      $no_telephone=$ligne['Telephone'];
	  
	  $hasCreditCard = false;
	  if ($enccc != "")
	  {
		$hasCreditCard = true;  
	  }
	  
	  // $encrypted2 = base64_decode($enccc);
      // $fp=fopen("/opt/webbike/www/private.key","r");
      // $priv_key=fread($fp,8192);
      // fclose($fp);	  
      // $res = openssl_get_privatekey($priv_key);
      // openssl_private_decrypt($encrypted2,$newsource,$res);
	  
	}
    echo "<TR><TH>No Client</TH><TD colspan='3'><Input type='text' name='NoClient' value='$noclient' readonly><BUTTON type='button' onClick='recherche_client_ajax(0);'>Recherche</BUTTON></TD></TR>";
    echo "<TR><TH>Nom</TH><TD><input name='Nom' value='".htmlspecialchars($nom_client)."'></TD>";
    echo "<TH>Prenom</TH><TD><input name='Prenom' value='".htmlspecialchars($prenom_client)."' onchange='recherche_client_ajax(0);'></TD></TR>";
    $clicktocall = "";
      if($ligne2=Poste::getPostesByIP())
  	  {
  	    $interne=$ligne2['NoInterne'];
        if ($interne != 0)
        {
        $clicktocall .= " <a href=\"javascript:appel_avec_formatage('Telephone', $interne);\"><img src=\"images/phone.gif\"/></a>";
        }
	    }
    echo "<TR><TH>Telephone</TH><TD><input name='Telephone' value='".htmlspecialchars($no_telephone)."'>$clicktocall</TD>";
    echo "<TH>Caution</TH><TD><SELECT name='caution' onchange='change_caution();'><OPTION value='6'>-</OPTION>";
    echo "<OPTION value='0'";
    if($cc==0)
      echo " selected";
    echo ">Cash</OPTION><OPTION value='1'";
    if($cc==1)
      echo " selected";
    echo ">Visa</OPTION><OPTION value='2'";
    if($cc==2)
      echo " selected";
    echo ">Master card</OPTION><OPTION value='3'";
    if($cc==3)
      echo " selected";
    echo ">Amex</OPTION><OPTION value='4'";
    if($cc==4)
      echo " selected";
    echo ">Diners card</OPTION><OPTION value='5'";
    if($cc==5)
      echo " selected";
    echo ">Sans</OPTION></SELECT></TD></TR>\n";
	if ($hasCreditCard)
	{
      echo "<TR><TH>No de carte de crédit</TH><TD><input name='ncc' value='****************' size='16' maxlength='16'></TD>\n";
      if($ecc=="0000-00-00" || $ecc=="2000-00-01")
        echo "<TH>Expiration</TH><TD><input name='ecc1' size=2 onKeyUp='tab_date();'><input name='ecc2' size=2></TD></TR>\n";
      else
        echo "<TH>Expiration</TH><TD><input name='ecc1' size=2 value='**' onKeyUp='tab_date();'><input name='ecc2' size=2 value='**'></TD></TR>\n";
      echo "<TR><TH>No sécurité de carte de crédit</TH><TD colspan='3'><input name='scc' value='***' size='3' maxlength='3'> <input type=\"file\" class=\"KeyFile\"><input type=\"hidden\" class=\"EncText\" value=\"$enccc\"> <button class=\"BtDecrypt\" type=\"button\">Decrypt</button></TD></TR>\n";
	}
	else
	{
      echo "<TR><TH>No de carte de crédit</TH><TD><input name='ncc' value='$ncc' size='16' maxlength='16'></TD>\n";
      if($ecc=="0000-00-00" || $ecc=="2000-00-01")
        echo "<TH>Expiration</TH><TD><input name='ecc1' size=2 onKeyUp='tab_date();'><input name='ecc2' size=2></TD></TR>\n";
      else
        echo "<TH>Expiration</TH><TD><input name='ecc1' size=2 value='".substr($ecc,5,2)."' onKeyUp='tab_date();'><input name='ecc2' size=2 value='".substr($ecc,2,2)."'></TD></TR>\n";
      echo "<TR><TH>No sécurité de carte de crédit</TH><TD colspan='3'><input name='scc' value='$scc' size='3' maxlength='3'></TD></TR>\n";
	}
    if ($_GET["new"]==1)
    {
      echo "<script language=JavaScript>\n";
      echo "this.document.forms['info'].elements['Nom'].focus();\n";
      echo "</script>\n";
    }
  }

  function bloc_date_15min($sufix, $date)
  {
    $annee = date("Y");
    $a_jour = date("d",$date);
    $a_mois = date("m",$date);
    $a_annee = date("Y",$date);
    $a_heure = date("H",$date);
    echo "<b>Date</b>\n<select name='new_jour_$sufix' onchange='identifie_tarif();'>\n";
    for($cpt=1;$cpt<=31;$cpt++)
    {
      echo "<option value='$cpt'";
   	  if ($cpt==$a_jour)
  	    echo " selected";
      echo ">$cpt</option>\n";
    }
    echo "</select>-";
    echo "<select name='new_mois_$sufix' onchange='identifie_tarif();'>";
    for($cpt=1;$cpt<=12;$cpt++)
    {
      echo "<option value='$cpt'";
      if ($cpt==$a_mois)
        echo " selected";
      echo ">$cpt " . nom_mois($cpt) . "</option>";
    }
    echo "</select>-";
    echo "<select name='new_annee_$sufix' onchange='identifie_tarif();'>";
    for($cpt=$annee-2;$cpt<=$annee+2;$cpt++)
    {
      echo "<option value='$cpt'";
      if ($cpt==$a_annee)
        echo " selected";
      echo ">$cpt</option>\n";
    }
    echo "</select>";
    echo "<b>Heure</b><select name='new_heure_$sufix' onchange='identifie_tarif();'>";
    for($cpt=0;$cpt<=23;$cpt++)
    {
      echo "<option value='$cpt'";
      if ($cpt==$a_heure)
        echo " selected";
      echo ">";
      if ($cpt<10)
        echo "0";
      echo "$cpt</option>\n";
    }
    echo "</select>:";
    echo "<select name='new_minutes_$sufix' onchange='identifie_tarif();'>";
    for($cpt=0;$cpt<=45;$cpt+=15)
    {
   	  echo "<option value='$cpt'";
      if ($cpt==0)
        echo " selected";
      echo ">";
      if ($cpt<10)
        echo "0";
      echo "$cpt</option>\n";
    }
    echo "</select>";
  }

  function bloc_date_15min_new($sufix, $date)
  {
    $annee = date("Y");
    $a_jour = date("d",$date);
    $a_mois = date("m",$date);
    $a_annee = date("Y",$date);
    $a_heure = date("H",$date);
	$a_date = "$a_annee-$a_mois-$a_jour";
	echo "<b>Date<b>\n".Cree_Entree_Type("date_$sufix",2,0,$a_date,MODIFICATION)."\n";
    echo "<b>Heure</b><select name='new_heure_$sufix' onchange='identifie_tarif();'>";
    for($cpt=0;$cpt<=23;$cpt++)
    {
      echo "<option value='$cpt'";
      if ($cpt==$a_heure)
        echo " selected";
      echo ">";
      if ($cpt<10)
        echo "0";
      echo "$cpt</option>\n";
    }
    echo "</select>:";
    echo "<select name='new_minutes_$sufix' onchange='identifie_tarif();'>";
    for($cpt=0;$cpt<=45;$cpt+=15)
    {
   	  echo "<option value='$cpt'";
      if ($cpt==0)
        echo " selected";
      echo ">";
      if ($cpt<10)
        echo "0";
      echo "$cpt</option>\n";
    }
    echo "</select><input type=\"hidden\" name=\"date_".$sufix."_new\" value=\"1\"/>\n";
	echo "<script>\n$(function() { $('input[name=\"date_$sufix\"]').change(function() { identifie_tarif(); }); });\n</script>";
  }
  
  function location_bloc_tarif($ligne)
  {
    $tarif = $GLOBALS['tarif'];
    if ($ligne == null)
    {
      $tarif_type = 1;
      $nb_unites = 2;
      $rabais = 0;
      $total = $nb_unites * $tarif; 
    }
    else 
    {
      $tarif_type = $ligne['TypeTarif'];
      $nb_unites = $ligne['NbUnites'];
      $rabais = $ligne['Rabais'];
      $total = $ligne['Total'];
    }
    echo "<TR><TH>Tarif</TH><TD><select name='new_tarif_type' onChange='modif_tarif();'>";
    for($cpt=1;$cpt<=9;$cpt++)
    {
      echo "<option value='$cpt'";
      if($cpt==$tarif_type)
        echo " selected";
      echo ">" . nom_tarif($cpt) . "</option>\n";
    }
    echo "</select></TD><TH>Prix</TH><TD><input name='tarif' value='$tarif' readonly></TD></TR>";
    echo "<TR><TH>Nombre d'unités</TH><TD><input name='nb_jours' value='$nb_unites' onChange='modif_prix();'></TD>";
    echo "<TH>Rabais %</TH><TD><input name='rabais' value='$rabais' onChange='modif_prix();'></TD></TR>";
    echo "<TR><TH>Total</TH><TD><input name='total' value='$total' onChange='calcule_rabais();'></TD>";
  }
  
  function location_bloc_acompte($ligne)
  {
    echo "<TH>Acompte</TH><TD><input name='acompte' value='0'>";
    if(As_Right_Groupe(6))
    {
      $resultat2=ModePayement::getVisibleModePayment();
    }
    else
    {
      $no_caisse=1;
        if($ligne1=Poste::getPostesByIP())
        {
          $no_caisse=$ligne1['NoCaisse'];
        }
      $resultat2=ModePayement::getModePayementsByNoCaisse($no_caisse);
    }
    echo " <SELECT name='ModePayement' size='1'>\n";
    if ($resultat2)
    {
      foreach ( $resultat2 as $ligne2)
      {
        if(($ligne2['Type']==2) && (!Is_Banque()))
        {
        }
        else
        {
          if(As_Right_Groupe(6))
          {
            echo "  <option value='" . $ligne2['NoMode'] . " " . $ligne2['NoCaisse'] . "'>" . $ligne2['Description'] . " caisse " . $ligne2['NoCaisse'] . "</option>\n";
          }
          else
          {
            echo "  <option value='" . $ligne2['NoMode'] . " " . $ligne2['NoCaisse'] . "'>" . $ligne2['Description'] . "</option>\n";
          }
        }
      }
    }
    echo"</TD></TR>";
  }
  
  function location_km_date_heure_depart($ligne)
  {
    if($ligne == null)
    {
      echo "<TR><TH>Départ effectif</TH><TD>".Input_Date_Heure("de",date("Y"),date("m"),date("j"),date("H"),date("i"))."</TD>\n";
    }
    else if(empty($ligne['DebutEffectif']))
    {
      echo "<TR><TH>Départ effectif</TH><TD>".Input_Date_Heure("de",date("Y"),date("m"),date("j"),date("H"),date("i"))."</TD>\n";
    }
    else
    {
      $dde=$ligne['DebutEffectif'];
      echo "<TR><TH>Départ effectif</TH><TD>".Input_Date_Heure("de",substr($dde,0,4),substr($dde,5,2),substr($dde,8,2),substr($dde,11,2),substr($dde,14,2))."</TD>\n";
    }
    if($ligne == null)
    {
      $value=$GLOBALS['kmactuel'];
    }
    else if(empty($ligne['KmDepart']))
    {
      $value=$GLOBALS['kmactuel'];
    }
    else
    {
      $value=$ligne['KmDepart'];
    }
    echo "<TH>Km départ</TH><TD><input type='text' name='KmDepart' value='$value'></TD></TR>\n";
  }
  
  function location_km_date_heure_retour($ligne)
  {
    if(empty($ligne['FinEffectif']))
    {
      echo "<TR><TH>Retour effectif</TH><TD>".Input_Date_Heure("re",date("Y"),date("m"),date("j"),date("H"),date("i"))."</TD>\n";
    }
    else
    {
      $dde=$ligne['FinEffectif'];
      echo "<TR><TH>Retour effectif</TH><TD>".Input_Date_Heure("re",substr($dde,0,4),substr($dde,5,2),substr($dde,8,2),substr($dde,11,2),substr($dde,14,2))."</TD>\n";
    }
    if(empty($ligne['KmRetour']))
    {
      $value=$GLOBALS['kmactuel'];
    }
    else
    {
      $value=$ligne['KmRetour'];
    }
    echo "<TH>Km retour</TH><TD><input type='text' name='KmRetour' value='$value'></TD></TR>\n";
    $etat_retour = $ligne['RemarqueRetour'];
    if (strlen($etat_retour)==0)
    {
      $ligne2 = VehiculesLocation::getVehiculeLocationByNoChassis($ligne["NoChasis"]);
      $etat_retour = $ligne2["EtatVhc"];
    }
    echo "<TR><TH>Etat Retour</TH><TD colspan='3'><textarea cols='50' rows='4' name='EtatRetour'>$etat_retour</textarea><button type='button' style='width:150px;' onClick='update_etat_vhc();'>MAJ Etat Vhc location</button></TD></TR>\n";
  }
  
  function location_personne_rdv_depart($ligne)
  {
    if($ligne == null)
    {
	  $noRDV = 0;
	  $noDepart = 0;
    }
	else
	{
      if(empty($ligne['EmployeRdv']))
	  {
	    $noRDV = 0;
	  }
	  else
	  {
	    $noRDV = $ligne['EmployeRdv'];
	  }
      if(empty($ligne['EmployeDepart']))
	  {
	    $noDepart = 0;
	  }
	  else
	  {
	    $noDepart = $ligne['EmployeDepart'];
	  }
	}
	echo "<tr><th>RDV</th><td>";
	$resultat2=Employee::getActifEmployee();
	echo "<SELECT name='EmployeRDV'>\n";
	echo "<option value='0'>-</option>\n";
	foreach($resultat2 as $ligne2)
	{
	  if($ligne3=Client::getClientByNoClient($ligne2['NoClient']))
	  {
	    echo "<option value='".$ligne3['NoClient']."'";
		if ($noRDV == $ligne3['NoClient'])
		{
		  echo " selected='selected'";
		}
        echo ">".$ligne3['Prenom']." ".$ligne3['Nom']."</option>\n";
      }
    }
    echo "</SELECT></td><th>Départ</th><td>\n";
	$resultat2=Employee::getActifEmployee();
	echo "<SELECT name='EmployeDepart'>\n";
	echo "<option value='0'>-</option>\n";
	foreach($resultat2 as $ligne2)
	{
	  if($ligne3=Client::getClientByNoClient($ligne2['NoClient']))
	  {
	    echo "<option value='".$ligne3['NoClient']."'";
		if ($noDepart == $ligne3['NoClient'])
		{
		  echo " selected='selected'";
		}
        echo ">".$ligne3['Prenom']." ".$ligne3['Nom']."</option>\n";
      }
    }
    echo "</SELECT></td></tr>\n";
  }

  function location_bouton_valider($ligne)
  {
    echo "<BUTTON TYPE='button' onClick='valider();'>Valider</button>\n";
  }
  
  function location_bouton_imprimer_contrat($ligne)
  {
    echo "<BUTTON TYPE='button' onClick='imprimer_contrat();' style='width:150px'>Imprimer Contrat</button>\n";
    echo "<BUTTON TYPE='button' onClick='imprimer_contrat_pdf();' style='width:150px'>Imprimer Contrat PDF</button>\n";
  }
  
  function location_bouton_demarrer_contrat_facture($ligne)
  {
    echo "<BUTTON TYPE='button' onClick='demarrer_imprimer_contrat_facture();' style='width:200px'>Imprimer Contrat et facture</button>\n";
    echo "<BUTTON TYPE='button' onClick='demarrer_imprimer_contrat_pdf_facture();' style='width:200px'>Imprimer Contrat PDF et facture</button>\n";
  }
  
  function location_bouton_debut_pret($ligne)
  {
    echo "<BUTTON TYPE='button' onClick='debut_pret();' style='width:150px'>Debut prêt</button>\n";
  }
  
  function location_bouton_retour_sans_facture($ligne)
  {
    echo "<BUTTON TYPE='button' onClick='retour_sans_facture();' style='width:150px'>Retour sans facture</button>\n";
  }
  
  function location_bouton_retour_avec_facture($ligne)
  {
    echo "<BUTTON TYPE='button' onClick='retour_avec_facture();' style='width:150px'>Retour avec facture</button>\n";
  }
  
  function location_bouton_retour_location($ligne)
  {
    echo "<BUTTON TYPE='button' onClick='retour_location();' style='width:150px'>Retour location</button>\n";
  }
  
  function location_bouton_demarrer_contrat($ligne)
  {
    echo "<BUTTON TYPE='button' onClick='demarrer_imprimer_contrat();' style='width:150px'>Imprimer Contrat</button>\n";
    echo "<BUTTON TYPE='button' onClick='demarrer_imprimer_contrat_pdf();' style='width:150px'>Imprimer Contrat PDF</button>\n";
  }
  
  function location_bouton_creer_facture($ligne)
  {
    echo "<BUTTON TYPE='button' onClick='creer_facture();' style='width:150px'>Créer facture</button>\n";
  }
  
?>