<?php
function connect(){
 $host = 'localhost';
 $login = 'webbike';
 $passe = 'honda';
 $base= 'webbike';
//include("include/admin.php");
 $link=@mysql_connect("$host",MYSQLLOGIN,MYSQLPASSWORD);
 $select_base=@mysql_selectdb("$base"); 
 if (!$select_base) 
    echo "<b>Probl�me de connection � la base!!! </b>"; 
}

$lannee = array(1 => '1999','2000','2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020','2021','2022','2023',);
$ljours = array(1 =>  '01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32');
$lmois = array(1 => '01','02','03','04','05','06','07','08','09','10','11','12','24');	

function get_NoCommande($s_searchStartDate,$s_searchEndDate){
	connect();
	$request="SELECT DISTINCT NoCommande FROM commande".ID_GARAGE." WHERE DateCommande BETWEEN '$s_searchStartDate' AND '$s_searchEndDate' ORDER BY DateCommande ASC";
	$result = mysql_query($request) or die('Erreur SQL !<br>'.$request.'<BR>'.Connection::getConnection()->errorInfo()); error_log("$request");
	$output = mysql_fetch_assoc($result);
	mysql_close();
	return $output ;
}

function get_NoArt_4Sum($NoArt){
	connect();
	$compag = $c_compagnie ;
	$sqlreq = "SELECT DISTINCT Description FROM articles WHERE NoArticle='$NoArt' AND GroupeMarchandise = '10'";
	error_log("$sqlreq");
	$result = mysql_query($sqlreq) or die('Erreur SQL !<br>'.$sqlreq.'<BR>'.Connection::getConnection()->errorInfo());
	$output = mysql_fetch_assoc($result);
	mysql_close();
	return $output['Description'];
}

function get_NoArt($NoArt){
	connect();
	$compag = $c_compagnie ;
	$sqlreq = "SELECT Description FROM articles WHERE NoArticle='$NoArt' AND GroupeMarchandise = '10'";
	error_log("$sqlreq");
	$result = mysql_query($sqlreq) or die('Erreur SQL !<br>'.$sqlreq.'<BR>'.Connection::getConnection()->errorInfo());
	$output = mysql_fetch_assoc($result);
	mysql_close();
	return $output['Description'];
}

function Sum_Quant($NoCom,$NoArt){
	connect();
 	$sqlreq = "SELECT SUM(Quantite) FROM commandearticles".ID_GARAGE." WHERE NoCommande='$NoCom' AND NoArticle='$NoArt' AND GM='10'";
	$sumquant = mysql_query($sqlreq) or die('Erreur SQL !<br>'.$sqlreq.'<BR>'.Connection::getConnection()->errorInfo()); error_log("$sqlreq");
	$SumQuant = mysql_fetch_assoc($sumquant);
	$Quant = $SumQuant['SUM(Quantite)'];
	mysql_close();
	return $Quant ;
}

function Sum_TotMulti($NoCom,$NoArt){
	connect();
 	$sqlreq = "SELECT SUM(PrixMulti) FROM commandearticles".ID_GARAGE." WHERE NoCommande='$NoCom' AND NoArticle='$NoArt' AND GM='10'";
	$sumprx = mysql_query($sqlreq) or die('Erreur SQL !<br>'.$sqlreq.'<BR>'.Connection::getConnection()->errorInfo()); error_log("$sqlreq");
	$SumPrx = mysql_fetch_assoc($sumprx);
	$PrxM = $SumPrx['SUM(PrixMulti)'];
	mysql_close();
	return $PrxM ;
}

function Sum_Tot_Mecano($var_name){
	connect();
 	$sqlreq = "SELECT SUM(var_prx) FROM variables_temporaires WHERE var_name='$var_name'";
	$sumprx = mysql_query($sqlreq) or die('Erreur SQL !<br>'.$sqlreq.'<BR>'.Connection::getConnection()->errorInfo()); error_log("$sqlreq");
	$SumPrx = mysql_fetch_assoc($sumprx);
	$PrxM = $SumPrx['SUM(var_prx)'];
	mysql_close();
	return $PrxM ;
}

function Sum_Tothrs_Mecano($var_name){
	connect();
 	$sqlreq = "SELECT SUM(var_quant) FROM variables_temporaires WHERE var_name='$var_name'";
	$sumprx = mysql_query($sqlreq) or die('Erreur SQL !<br>'.$sqlreq.'<BR>'.Connection::getConnection()->errorInfo()); error_log("$sqlreq");
	$SumPrx = mysql_fetch_assoc($sumprx);
	$PrxM = $SumPrx['SUM(var_quant)'];
	mysql_close();
	return $PrxM ;
}

// Fonction bool�enne indiquant si la variable $var_nom existe.
function existe_var($var_name) {
	connect();
	// Recherche dans la base de donn�e
	$sql = "SELECT varid FROM variables_temporaires WHERE `var_name` = '$var_name'";
	$resultat = mysql_query($sql);
	
	if(!mysql_num_rows($resultat)) return false;
	else return true;
	mysql_close();
}

// Retourne la valeur de la variable $var_nom de type $var_type.
// Si elle n'existe pas, cette fonction retourne false.
function aquerir_var($var_name) {
	connect();
	// Recherche dans la base de donn�e
	$sql = "SELECT `var_prx` FROM variables_temporaires WHERE var_name = '$var_name'";
	$resultat = mysql_query($sql);
	if(!mysql_num_rows($resultat)) return false;

	// Retour de la valeur
	$variable = mysql_fetch_array($resultat);
	mysql_close();
	return $variable[$var_prx];
}

// Attribue une valeur pour une variable.
// Si la variable existe d�j�, la valeur sera mise � jour.
// La fonction retourne $var_valeur
function definir_var($var_name,$var_prx,$var_quant) {
	connect();
	// Si la variable n'existe pas, on ajoute une ligne vide
	if( !existe_var($var_name) ) {
		$sql = "INSERT INTO `variables_temporaires` VALUES ('','$var_name','$var_prx','$var_quant')";
		$resultat = mysql_query($sql);error_log("$sql");
	}

	// Mise � jour de la valeur
	$sql = "UPDATE `variables_temporaires` SET `var_prx` = '$var_prx', `var_quant` = '$var_quant' WHERE `var_name` = '$var_name'";
	$resultat = mysql_query($sql);error_log("$sql");
	mysql_close();
	// Renvoi de la valeur
	return $var_valeur;
}

// Fonction bool�enne indiquant si la variable $var_nom existe.
function vide_table_vars() {
	connect();
	// Recherche dans la base de donn�e
	$sql = "TRUNCATE TABLE `variables_temporaires`";
	$resultat = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<BR>'.Connection::getConnection()->errorInfo()); error_log("$sql");
	
	mysql_close();
}

function get_potentiel_atelier_tous($jour, $mois, $annee)
  {
    $date=mktime(0,0,0,$mois,$jour,$annee);
    $a_jour = date("d",$date);
    $a_mois = date("m",$date);
    $a_annee = date("Y",$date);
  	if (jour_ferie($jour, $mois, $annee, 2)==0)
  	{
  		return "0.00";
  	}
    $pot="0.00";
    $req="SELECT SUM(HeureAtelier) FROM employes".ID_GARAGE." WHERE Actif = 1";
    if($result=mysql_query($req))
    {
      if($lign=mysql_fetch_array($result))
      {
        $pot=$lign[0];
        //$tab_debug[$tmp_date]["tous"]=$lign[0];
      }
    }
    if(empty($pot))
    $pot="0.00";
    $pot2="0.00";
    $pot3="0.00";
    $tous=0;
    $req="SELECT * FROM absenceemploye".ID_GARAGE." WHERE Debut<='$a_annee-".Chiffre_2_Car($a_mois)."-".Chiffre_2_Car($a_jour)."' AND Fin>='$a_annee-".Chiffre_2_Car($a_mois)."-".Chiffre_2_Car($a_jour)."'";
    if($result=mysql_query($req))
    {
      while($lign=mysql_fetch_array($result))
      {
        if($lign['NoClient']==0)
        {
          $tous=1;
          $Debut=$lign['Debut'];
          $Fin=$lign['Fin'];
          $Actual=$a_annee."-".$a_mois."-".$a_jour;
          if(($Debut<$Actual)&&($Fin>$Actual))
          {
            $pot3+=$pot;
          }
          else if($Debut==$Actual)
          {
            if($Fin==$Actual)
            {
              if($lign['DebutMA']<$lign['FinMA'])
              {
                $pot3+=$pot;
              }
              else
              {
                $pot3+=$pot/2;
              }
            }
            else
            {
              if($lign['DebutMA']==0)
              {
                $pot3+=$pot;
              }
              else
              {
                $pot3+=$pot/2;
              }
            }
          }
          else if($Fin==$Actual)
          {
            if($lign['FinMA']==1)
            {
              $pot3+=$pot;
            }
            else
            {
              $pot3+=$pot/2;
            }
          }
          //$tab_debug[$tmp_date]["0"]=$pot3;
        }
        else
        {
          $req2="SELECT * FROM employes".ID_GARAGE." WHERE NoClient=".$lign['NoClient']." AND Actif = 1";
          if($result2=mysql_query($req2))
          {
            if($lign2=mysql_fetch_array($result2))
            {
              $Debut=$lign['Debut'];
              $Fin=$lign['Fin'];
              $Actual=$a_annee."-".$a_mois."-".$a_jour;
              if(($Debut<$Actual)&&($Fin>$Actual))
              {
                $pot2+=$lign2['HeureAtelier'];
              }
              else if($Debut==$Actual)
              {
                if($Fin==$Actual)
                {
                  if($lign['DebutMA']<$lign['FinMA'])
                  {
                    $pot2+=$lign2['HeureAtelier'];
                  }
                  else
                  {
                    $pot2+=$lign2['HeureAtelier']/2;
                  }
                }
                else
                {
                  if($lign['DebutMA']==0)
                  {
                    $pot2+=$lign2['HeureAtelier'];
                  }
                  else
                  {
                    $pot2+=$lign2['HeureAtelier']/2;
                  }
                }
              }
              else if($Fin==$Actual)
              {
                if($lign['FinMA']==1)
                {
                  $pot2+=$lign2['HeureAtelier'];
                }
                else
                {
                  $pot2+=$lign2['HeureAtelier']/2;
                }
              }
              //$tab_debug[$tmp_date][$lign["NoClient"]]=$lign2['HeureAtelier'];
            }
          }
        }
      }
    }
    if($tous==1)
    {
      $pot4=$pot-$pot3;
    }
    else
    {
      $pot4=$pot-$pot2;
    }
    $pot4=Arrondi_1_ct($pot4);
    return $pot4;  	
  }
  function get_potentiel_atelier($jour, $mois, $annee, $no_client)
  {
    $date=mktime(0,0,0,$mois,$jour,$annee);
    $a_jour = date("d",$date);
    $a_mois = date("m",$date);
    $a_annee = date("Y",$date);
  	if (jour_ferie($jour, $mois, $annee, 2)==0)
  	{
  		return "0.00";
  	}
  	$pot="0.00";
      $req="SELECT HeureAtelier FROM employes".ID_GARAGE." WHERE NoClient = ".$no_client;
      if($result=mysql_query($req))
        if($lign=mysql_fetch_array($result))
          $pot=$lign[0];
      if(empty($pot))
        $pot="0.00";
      $pot2="0.00";
      $pot3="0.00";
      $tous=0;
      $req="SELECT * FROM absenceemploye".ID_GARAGE." WHERE NoClient IN (0, ".$no_client.") AND Debut<='$a_annee-".Chiffre_2_Car($a_mois)."-".Chiffre_2_Car($a_jour)."' AND Fin>='$a_annee-".Chiffre_2_Car($a_mois)."-".Chiffre_2_Car($a_jour)."'";
      if($result=mysql_query($req))
      {
        while($lign=mysql_fetch_array($result))
        {
          if($lign['NoClient']==0)
          {
            $tous=1;
            $Debut=$lign['Debut'];
            $Fin=$lign['Fin'];
            $Actual=$a_annee."-".$a_mois."-".$a_jour;
            if(($Debut<$Actual)&&($Fin>$Actual))
            {
              $pot3+=$pot;
            }
            else if($Debut==$Actual)
            {
              if($Fin==$Actual)
              {
                if($lign['DebutMA']<$lign['FinMA'])
                {
                  $pot3+=$pot;
                }
                else
                {
                  $pot3+=$pot/2;
                }
              }
              else
              {
                if($lign['DebutMA']==0)
                {
                  $pot3+=$pot;
                }
                else
                {
                  $pot3+=$pot/2;
                }
              }
            }
            else if($Fin==$Actual)
            {
              if($lign['FinMA']==1)
              {
                $pot3+=$pot;
              }
              else
              {
                $pot3+=$pot/2;
              }
            }
          }
          else
          {
            $Debut=$lign['Debut'];
            $Fin=$lign['Fin'];
            $Actual=$a_annee."-".$a_mois."-".$a_jour;
            if(($Debut<$Actual)&&($Fin>$Actual))
            {
              $pot2+=$pot;
            }
            else if($Debut==$Actual)
            {
              if($Fin==$Actual)
              {
                if($lign['DebutMA']<$lign['FinMA'])
                {
                  $pot2+=$pot;
                }
                else
                {
                  $pot2+=$pot/2;
                }
              }
              else
              {
                if($lign['DebutMA']==0)
                {
                  $pot2+=$pot;
                }
                else
                {
                  $pot2+=$pot/2;
                }
              }
            }
            else if($Fin==$Actual)
            {
              if($lign['FinMA']==1)
              {
                $pot2+=$pot;
              }
              else
              {
                $pot2+=$pot/2;
              }
            }
          }
        }
      }
      if($tous==1)
      {
        $pot4=$pot-$pot3;
      }
      else
      {
        $pot4=$pot-$pot2;
      }
      $pot4=Arrondi_1_ct($pot4);
  	  return $pot4;
  }
/*
function get_potentiel_atelier($jour, $mois, $annee, $no_client)
  {
    $date=mktime(0,0,0,$mois,$jour,$annee);
    $a_jour = date("d",$date);
    $a_mois = date("m",$date);
    $a_annee = date("Y",$date);
  	if (jour_ferie($jour, $mois, $annee, 2)==0)
  	{
  		return "0.00";
  	}
  	$pot="0.00";
      $req="SELECT HeureAtelier FROM employes".ID_GARAGE." WHERE NoClient = ".$no_client;
      if($result=mysql_query($req))
        if($lign=mysql_fetch_array($result))
          $pot=$lign[0];
      if(empty($pot))
        $pot="0.00";
      $pot2="0.00";
      $pot3="0.00";
      $tous=0;
      $req="SELECT * FROM absenceemploye".ID_GARAGE." WHERE NoClient IN (0, ".$no_client.") AND Debut<='$a_annee-".Chiffre_2_Car($a_mois)."-".Chiffre_2_Car($a_jour)."' AND Fin>='$a_annee-".Chiffre_2_Car($a_mois)."-".Chiffre_2_Car($a_jour)."'";
      if($result=mysql_query($req))
      {
        while($lign=mysql_fetch_array($result))
        {
          if($lign['NoClient']==0)
          {
            $tous=1;
            $Debut=$lign['Debut'];
            $Fin=$lign['Fin'];
            $Actual=$a_annee."-".$a_mois."-".$a_jour;
            if(($Debut<$Actual)&&($Fin>$Actual))
            {
              $pot3+=$pot;
            }
            else if($Debut==$Actual)
            {
              if($Fin==$Actual)
              {
                if($lign['DebutMA']<$lign['FinMA'])
                {
                  $pot3+=$pot;
                }
                else
                {
                  $pot3+=$pot/2;
                }
              }
              else
              {
                if($lign['DebutMA']==0)
                {
                  $pot3+=$pot;
                }
                else
                {
                  $pot3+=$pot/2;
                }
              }
            }
            else if($Fin==$Actual)
            {
              if($lign['FinMA']==1)
              {
                $pot3+=$pot;
              }
              else
              {
                $pot3+=$pot/2;
              }
            }
          }
          else
          {
            $Debut=$lign['Debut'];
            $Fin=$lign['Fin'];
            $Actual=$a_annee."-".$a_mois."-".$a_jour;
            if(($Debut<$Actual)&&($Fin>$Actual))
            {
              $pot2+=$pot;
            }
            else if($Debut==$Actual)
            {
              if($Fin==$Actual)
              {
                if($lign['DebutMA']<$lign['FinMA'])
                {
                  $pot2+=$pot;
                }
                else
                {
                  $pot2+=$pot/2;
                }
              }
              else
              {
                if($lign['DebutMA']==0)
                {
                  $pot2+=$pot;
                }
                else
                {
                  $pot2+=$pot/2;
                }
              }
            }
            else if($Fin==$Actual)
            {
              if($lign['FinMA']==1)
              {
                $pot2+=$pot;
              }
              else
              {
                $pot2+=$pot/2;
              }
            }
          }
        }
      }
      if($tous==1)
      {
        $pot4=$pot-$pot3;
      }
      else
      {
        $pot4=$pot-$pot2;
      }
      $pot4=Arrondi_1_ct($pot4);
  	  return $pot4;
  }
  function get_potentiel_atelier_tous($jour, $mois, $annee)
  {
    $date=mktime(0,0,0,$mois,$jour,$annee);
    $a_jour = date("d",$date);
    $a_mois = date("m",$date);
    $a_annee = date("Y",$date);
  	if (jour_ferie($jour, $mois, $annee, 2)==0)
  	{
  		return "0.00";
  	}
    $pot="0.00";
    $req="SELECT SUM(HeureAtelier) FROM employes".ID_GARAGE." WHERE Actif = 1";
    if($result=mysql_query($req))
    {
      if($lign=mysql_fetch_array($result))
      {
        $pot=$lign[0];
        //$tab_debug[$tmp_date]["tous"]=$lign[0];
      }
    }
    if(empty($pot))
    $pot="0.00";
    $pot2="0.00";
    $pot3="0.00";
    $tous=0;
    $req="SELECT * FROM absenceemploye".ID_GARAGE." WHERE Debut<='$a_annee-".Chiffre_2_Car($a_mois)."-".Chiffre_2_Car($a_jour)."' AND Fin>='$a_annee-".Chiffre_2_Car($a_mois)."-".Chiffre_2_Car($a_jour)."'";
    if($result=mysql_query($req))
    {
      while($lign=mysql_fetch_array($result))
      {
        if($lign['NoClient']==0)
        {
          $tous=1;
          $Debut=$lign['Debut'];
          $Fin=$lign['Fin'];
          $Actual=$a_annee."-".$a_mois."-".$a_jour;
          if(($Debut<$Actual)&&($Fin>$Actual))
          {
            $pot3+=$pot;
          }
          else if($Debut==$Actual)
          {
            if($Fin==$Actual)
            {
              if($lign['DebutMA']<$lign['FinMA'])
              {
                $pot3+=$pot;
              }
              else
              {
                $pot3+=$pot/2;
              }
            }
            else
            {
              if($lign['DebutMA']==0)
              {
                $pot3+=$pot;
              }
              else
              {
                $pot3+=$pot/2;
              }
            }
          }
          else if($Fin==$Actual)
          {
            if($lign['FinMA']==1)
            {
              $pot3+=$pot;
            }
            else
            {
              $pot3+=$pot/2;
            }
          }
          //$tab_debug[$tmp_date]["0"]=$pot3;
        }
        else
        {
          $req2="SELECT * FROM employes".ID_GARAGE." WHERE NoClient=".$lign['NoClient']." AND Actif = 1";
          if($result2=mysql_query($req2))
          {
            if($lign2=mysql_fetch_array($result2))
            {
              $Debut=$lign['Debut'];
              $Fin=$lign['Fin'];
              $Actual=$a_annee."-".$a_mois."-".$a_jour;
              if(($Debut<$Actual)&&($Fin>$Actual))
              {
                $pot2+=$lign2['HeureAtelier'];
              }
              else if($Debut==$Actual)
              {
                if($Fin==$Actual)
                {
                  if($lign['DebutMA']<$lign['FinMA'])
                  {
                    $pot2+=$lign2['HeureAtelier'];
                  }
                  else
                  {
                    $pot2+=$lign2['HeureAtelier']/2;
                  }
                }
                else
                {
                  if($lign['DebutMA']==0)
                  {
                    $pot2+=$lign2['HeureAtelier'];
                  }
                  else
                  {
                    $pot2+=$lign2['HeureAtelier']/2;
                  }
                }
              }
              else if($Fin==$Actual)
              {
                if($lign['FinMA']==1)
                {
                  $pot2+=$lign2['HeureAtelier'];
                }
                else
                {
                  $pot2+=$lign2['HeureAtelier']/2;
                }
              }
              //$tab_debug[$tmp_date][$lign["NoClient"]]=$lign2['HeureAtelier'];
            }
          }
        }
      }
    }
    if($tous==1)
    {
      $pot4=$pot-$pot3;
    }
    else
    {
      $pot4=$pot-$pot2;
    }
    $pot4=Arrondi_1_ct($pot4);
    return $pot4;  	
  }
  */
?>
