
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Fred -- Fred</title>
</head>

<body>
<?php 

require_once("../../config/constant.inc.php");
require_once("../lib/fonctions_divers.inc.php");
require_once("../lib/fonctions_commande.inc.php");
include ("include/functions.php");

if(isset($_REQUEST['f_annee']))$f_annee=$_REQUEST['f_annee']; else $f_annee=date('Y');
if(isset($_REQUEST['f_mois']))$f_mois=$_REQUEST['f_mois']; else $f_mois=date('m');
if(isset($_REQUEST['s_jourDebut']))$s_jourDebut=$_REQUEST['s_jourDebut']; else $s_jourDebut=date('d');
if(isset($_REQUEST['s_jourFin']))$s_jourFin=$_REQUEST['s_jourFin']; else $s_jourFin=date('d');
if(isset($_REQUEST['E_annee']))$E_annee=$_REQUEST['E_annee']; else $E_annee=date('Y');
if(isset($_REQUEST['S_mois']))$S_mois=$_REQUEST['S_mois']; else $S_mois=date('m');
if(isset($_REQUEST['E_mois']))$E_mois=$_REQUEST['E_mois']; else $E_mois=date('m');

if(isset($_POST['v_action']))$v_action=($_POST['v_action']);else $v_action='';
if(isset($_POST['f_affiche']))$f_affiche=($_POST['f_affiche']);else $f_affiche='a_single';

$server = 'localhost';
$login = 'webbike';
$password = 'orangepower';
$base= 'webbike';

?>
<table border=0 cellpadding=0 cellspacing=1>
  <tr><form method=post action=rapport_mensuel.php?v_action=showRapport>
  <td><select class="selectStyle" name=s_jourDebut value='$s_jourDebut'>
    <?php for($l=0;$l<sizeof($ljours);$l++){ 
      if($s_jourDebut==$ljours[$l]){
      echo '<option style="optionStyle" selected=selected>'; 
      echo $ljours[$l];
      continue;
      }
      echo '<option style="optionStyle">'; 
      echo $ljours[$l];
      }
    ?>
    </select></td> 
     <td><b>Au:</b></td>
    <td><select class="selectStyle" name=s_jourFin value='$s_jourFin'>
    <?php for($l=0;$l<sizeof($ljours);$l++){ 
      if($s_jourFin==$ljours[$l]){
      echo '<option style="optionStyle" selected=selected>'; 
      echo $ljours[$l];
      continue;
      }
      echo '<option style="optionStyle">'; 
      echo $ljours[$l];
      }
    ?>
    </select></td>
    <td><b>Mois: </b></td>
    <td><select class="selectStyle" name=f_mois value='$f_mois'>
    <?php for($k=0;$k<sizeof($lmois);$k++){ 
      if($f_mois==$lmois[$k]){
      echo '<option style="optionStyle" selected=selected>';
      echo $lmois[$k];
      continue;
      }
      echo '<option style="optionStyle">'; 
      echo $lmois[$k];
      }
    ?>
    </select></td>
    <td><select class="selectStyle2" width="100" name=f_annee value='$f_annee'>
    <?php for($k=0;$k<sizeof($lannee);$k++){ 
      if($f_annee==$lannee[$k]){
      echo '<option style="optionStyle" selected=selected>';
      echo $lannee[$k];
      continue;
      }
      echo '<option style="optionStyle">'; 
      echo $lannee[$k];
      }
    ?>
    </select></td>
    <td>
        <input type="hidden" name="f_affiche" value="a_single">
        <input name=form type=submit class=f-submit value=Submit>
        </form>
    </td>
    </tr>

   <tr><td><b>&nbsp; &nbsp; &nbsp; &nbsp;</b></td></tr>

  <tr><form method=post action=rapport_mensuel.php?v_action=showRapport>
   <td><b>du mois : </b></td>
    <td><select class="selectStyle" name=S_mois value='$S_mois'>
    <?php for($k=0;$k<sizeof($lmois);$k++){ 
      if($S_mois==$lmois[$k]){
      echo '<option style="optionStyle" selected=selected>';
      echo $lmois[$k];
      continue;
      }
      echo '<option style="optionStyle">'; 
      echo $lmois[$k];
      }
    ?>
    </select></td>
    <td><b>Au mois : </b></td>
    <td><select class="selectStyle" name=E_mois value='$E_mois'>
    <?php for($k=0;$k<sizeof($lmois);$k++){ 
      if($E_mois==$lmois[$k]){
      echo '<option style="optionStyle" selected=selected>';
      echo $lmois[$k];
      continue;
      }
      echo '<option style="optionStyle">'; 
      echo $lmois[$k];
      }
    ?>
    </select></td>
    <td><select class="selectStyle2" width="100" name=E_annee value='$E_annee'>
    <?php for($k=0;$k<sizeof($lannee);$k++){ 
      if($E_annee==$lannee[$k]){
      echo '<option style="optionStyle" selected=selected>';
      echo $lannee[$k];
      continue;
      }
      echo '<option style="optionStyle">'; 
      echo $lannee[$k];
      }
    ?>
    </select></td>

    <td>
         <input type="hidden" name="f_affiche" value="a_periode"> 
        <input name=form type=submit class=f-submit value=Submit>
        </form>
    </td>
  </tr>
</table>
<br><br>

<?php
if ($f_affiche == 'a_single'){
    print"<table>" ;
    $montant_total_factures=0;

   // $jour_debut = "01";    
   // $jour_fin = date("t",mktime(0,0,0,$f_mois,1,$f_annee));

    if ($v_action='showRapport'){
      connect();
      mysql_query("SET NAMES 'utf8'");
      echo "<TR class='rapport_hebdo'><TD COLSPAN='2'>Rapport Mensuel</TD><TD COLSPAN='2'><TABLE><TR><TH>Mecano</TH><TH>Prévu</TH><TH>Potientiel</TH><TH>Facturé</TH><TH>Taux</TH></TR>";
        	//$c_dates = "";
          /* affiche les heures par mécanos */
          $requette_mecano="SELECT employes".ID_GARAGE.".NoClient, clients.Prenom FROM employes".ID_GARAGE.", clients 
                              WHERE employes".ID_GARAGE.".Actif = 1 
                              AND employes".ID_GARAGE.".HeureAtelier > 0 
                              AND employes".ID_GARAGE.".NoClient = clients.NoClient 
                              ORDER BY clients.Prenom";
    	    $resultat_mecano=mysql_query($requette_mecano);
         $totalMEC=0;
         $totalFACT=0;
          /******    boucle while  ******/
    	    while($ligne_mecano=mysql_fetch_array($resultat_mecano)){
              $tot="0.00";
              $req="SELECT SUM(Temps) FROM atelier".ID_GARAGE." 
                      WHERE Date>='$f_annee-".Chiffre_2_Car($f_mois)."-".Chiffre_2_Car($s_jourDebut)."' 
                      AND Date<='$f_annee-".Chiffre_2_Car($f_mois)."-".Chiffre_2_Car($s_jourFin)."' 
                      AND Mecano = ".$ligne_mecano["NoClient"]."
                      AND Mecano != '0'";

              if($result=mysql_query($req)){
                if($lign=mysql_fetch_array($result)){
                  $tot=$lign[0];
                }
              }
              if(empty($tot)) $tot="0.00";
              
              $fact=0;
    		      $req="SELECT atelier".ID_GARAGE.".NoCommande, commande".ID_GARAGE.".Type, commande".ID_GARAGE.".MontantTotal FROM atelier".ID_GARAGE.", commande".ID_GARAGE." 
                      WHERE atelier".ID_GARAGE.".Date>='$f_annee-".Chiffre_2_Car($f_mois)."-".Chiffre_2_Car($s_jourDebut)."' 
                      AND atelier".ID_GARAGE.".Date<='$f_annee-".Chiffre_2_Car($f_mois)."-".Chiffre_2_Car($s_jourFin)."' 
                      AND atelier".ID_GARAGE.".NoCommande>0 AND atelier".ID_GARAGE.".Mecano = ".$ligne_mecano["NoClient"]." 
                      AND atelier".ID_GARAGE.".NoCommande = commande".ID_GARAGE.".NoCommande 
                      AND (commande".ID_GARAGE.".Genre = 'F' OR commande".ID_GARAGE.".Genre = 'G')";

              if($result=mysql_query($req)){
                while($lign=mysql_fetch_array($result)){
                  $req2="SELECT SUM(Quantite) FROM commandearticles".ID_GARAGE." 
                          WHERE NoCommande=".$lign[0]." 
                          AND GM=".Config::getGMMO(); // GM_MO = 10

                  if($result2=mysql_query($req2)){
                    if($lign2=mysql_fetch_array($result2)){
                      $fact+=$lign2[0];
                    }
                  }
                }
              }
              $fact=Arrondi_1_ct($fact);
              
              /**** GENUIX count *****/
              $totalMEC+=$tot;
              $totalFACT+=$fact;
              /***********************/

              $pot = 0;
              $cpt = date("t",mktime(0,0,0,$f_mois,1,$f_annee));
              $cpt = $jour_fin - $jour_debut ;
              for ($c_cpt=-$cpt; $c_cpt <= 0; $c_cpt++){
                $c_date=mktime(0,0,0,$f_mois,$s_jourDebut+$c_cpt,$f_annee);
                $c_jour = date("d",$c_date);
                $c_mois = date("m",$c_date);
                $c_annee = date("Y",$c_date);
                $pot += get_potentiel_atelier($c_jour, $c_mois, $c_annee, $ligne_mecano["NoClient"]);
        	     }
        	     $pot=Arrondi_1_ct($pot);
        	     if ($pot != 0){
                echo "<TR><TD>".$ligne_mecano["Prenom"]."</TD><TD>$tot</TD><TD>$pot</TD><TD>$fact</TD><TD>".round(100*$fact/$pot)."</TD></TR>\n";
               }
            }
            /* fin boucle while ***/
            /* Fin de par mecano */

            $req="SELECT SUM(Temps) FROM atelier".ID_GARAGE." WHERE Date>='$f_annee-".Chiffre_2_Car($f_mois)."-".Chiffre_2_Car($s_jourDebut)."' AND Date<='$f_annee-".Chiffre_2_Car($f_mois)."-".Chiffre_2_Car($s_jourFin)."'";
            if($result=mysql_query($req)){
              if($lign=mysql_fetch_array($result)){
                $tot=$lign[0];
              }
            }
            if(empty($tot)) $tot="0.00";
            $fact=0;
            $req="SELECT atelier".ID_GARAGE.".NoCommande, commande".ID_GARAGE.".Type, commande".ID_GARAGE.".MontantTotal FROM atelier".ID_GARAGE.", commande".ID_GARAGE." WHERE atelier".ID_GARAGE.".Date>='$f_annee-".Chiffre_2_Car($f_mois)."-".Chiffre_2_Car($s_jourDebut)."' AND atelier".ID_GARAGE.".Date<='$f_annee-".Chiffre_2_Car($f_mois)."-".Chiffre_2_Car($s_jourFin)."' AND atelier".ID_GARAGE.".NoCommande>0 AND atelier".ID_GARAGE.".NoCommande = commande".ID_GARAGE.".NoCommande AND (commande".ID_GARAGE.".Genre = 'F' OR commande".ID_GARAGE.".Genre = 'G')";
            if($result=mysql_query($req)){
              while($lign=mysql_fetch_array($result)){
    		    if ($lign[1]=="FD" || $lign[1]=="GF" || $lign[1]=="FV" || $lign[1]=="FO"){
              $montant_total_factures+=$lign[2];
            }
            $req2="SELECT SUM(Quantite) FROM commandearticles".ID_GARAGE." WHERE NoCommande=".$lign[0]." AND GM=".Config::getGMMO();
            if($result2=mysql_query($req2)){
              if($lign2=mysql_fetch_array($result2)){
                $fact+=$lign2[0];
              }
            }
          }
        }
        $fact=Arrondi_1_ct($fact);
        $pot = 0;
        $cpt = date("t",mktime(0,0,0,$f_mois,1,$f_annee));
        $cpt = $jour_fin - $jour_debut ;
          for ($c_cpt=-$cpt; $c_cpt <= 0; $c_cpt++){
              $c_date=mktime(0,0,0,$f_mois,$s_jourDebut+$c_cpt,$f_annee);
              $c_jour = date("d",$c_date);
              $c_mois = date("m",$c_date);
              $c_annee = date("Y",$c_date);
              $pot += get_potentiel_atelier_tous($c_jour, $c_mois, $c_annee);
        	}
        	$pot=Arrondi_1_ct($pot);
        	if ($pot != 0){
        	  echo "<TR><TH>Total</TH><TH>$totalMEC</TH><TH>$pot</TH><TH>$totalFACT</TH><TH>".round(100*$fact/$pot)."</TH></TR>\n";
        	}
          echo "</TABLE></TD><TD COLSPAN='".(1+$nb_pret)."'>";
    			echo "</TD></TR>\n";
        echo "<TR class='tr_sep_jour'><TD class='td_sep_jour' COLSPAN='2".(5+$nb_pret)."'></TD></TR>\n";
        }
    echo "</TABLE>\n";
}
if ($f_affiche=='a_periode'){
    print"<table>" ;
    $montant_total_factures=0;

    $jour_debut = "01";
    $jour_fin = date("t",mktime(0,0,0,$S_mois,1,$E_annee));

    $dif_mois = $E_mois - $S_mois +1 ; 

    if ($v_action='showRapport'){
      connect();
      mysql_query("SET NAMES 'utf8'");
      echo "<TR class='rapport_hebdo'><TD COLSPAN='2'>Rapport Mensuel</TD><TD COLSPAN='2'><TABLE><TR><TH>Mecano</TH><TH>Prévu</TH><TH>Potientiel</TH><TH>Facturé</TH><TH>Taux</TH></TR>";
          /* affiche les heures par mécanos */
          $requette_mecano="SELECT employes".ID_GARAGE.".NoClient, clients.Prenom FROM employes".ID_GARAGE.", clients WHERE employes".ID_GARAGE.".Actif = 1 AND employes".ID_GARAGE.".HeureAtelier > 0 AND employes".ID_GARAGE.".NoClient = clients.NoClient ORDER BY clients.Prenom";
          $resultat_mecano=mysql_query($requette_mecano);
          while($ligne_mecano=mysql_fetch_array($resultat_mecano)){
              $tot="0.00";
              $req="SELECT SUM(Temps) FROM atelier".ID_GARAGE." WHERE Date>='$E_annee-".Chiffre_2_Car($S_mois)."-".Chiffre_2_Car($jour_debut)."' AND Date<='$E_annee-".Chiffre_2_Car($E_mois)."-".Chiffre_2_Car($jour_fin)."' AND Mecano = ".$ligne_mecano["NoClient"]."
                      AND Mecano != '0'";
              if($result=mysql_query($req)){
                if($lign=mysql_fetch_array($result)){
                  $tot=$lign[0];
                }
              }
              if(empty($tot)) $tot="0.00";
              $fact=0;
          
              $req="SELECT atelier".ID_GARAGE.".NoCommande, commande".ID_GARAGE.".Type, commande".ID_GARAGE.".MontantTotal FROM atelier".ID_GARAGE.", commande".ID_GARAGE." WHERE atelier".ID_GARAGE.".Date>='$E_annee-".Chiffre_2_Car($S_mois)."-".Chiffre_2_Car($jour_debut)."' AND atelier".ID_GARAGE.".Date<='$E_annee-".Chiffre_2_Car($E_mois)."-".Chiffre_2_Car($jour_fin)."' AND atelier".ID_GARAGE.".NoCommande>0 AND atelier".ID_GARAGE.".Mecano = ".$ligne_mecano["NoClient"]." AND atelier".ID_GARAGE.".NoCommande = commande".ID_GARAGE.".NoCommande AND (commande".ID_GARAGE.".Genre = 'F' OR commande".ID_GARAGE.".Genre = 'G')";
              if($result=mysql_query($req)){
                while($lign=mysql_fetch_array($result)){
                  if ($lign[1]=="FD"){
                    //$montant_total_factures+=$lign[2];
                  }
                  $req2="SELECT SUM(Quantite) FROM commandearticles".ID_GARAGE." WHERE NoCommande=".$lign[0]." AND GM=".Config::getGMMO();
                  if($result2=mysql_query($req2)){
                    if($lign2=mysql_fetch_array($result2)){
                      $fact+=$lign2[0];
                    }
                  }
                }
              }
              $fact=Arrondi_1_ct($fact);
              $pot = 0;
              $cpt = date("t",mktime(0,0,0,$S_mois,1,$E_annee));
              for ($c_cpt=-$cpt; $c_cpt <= 0; $c_cpt++){
                $c_date=mktime(0,0,0,$S_mois,$jour_debut+$c_cpt,$E_annee);
                $c_jour = date("d",$c_date);
                $c_mois = date("m",$c_date);
                $c_annee = date("Y",$c_date);
                $pot += get_potentiel_atelier($c_jour, $c_mois, $c_annee, $ligne_mecano["NoClient"]);
               }
               $pot=Arrondi_1_ct($pot);
               if ($pot != 0){
                echo "<TR><TD>".$ligne_mecano["Prenom"]."</TD><TD>$tot</TD><TD>$pot</TD><TD>$fact</TD><TD>".(round(100*$fact/$pot))/$dif_mois."</TD></TR>\n";
               }
            }
            /* Fin de par mecano */

            $req="SELECT SUM(Temps) FROM atelier".ID_GARAGE." WHERE Date>='$E_annee-".Chiffre_2_Car($S_mois)."-".Chiffre_2_Car($jour_debut)."' AND Date<='$E_annee-".Chiffre_2_Car($E_mois)."-".Chiffre_2_Car($jour_fin)."'";
            if($result=mysql_query($req)){
              if($lign=mysql_fetch_array($result)){
                $tot=$lign[0];
              }
            }
            if(empty($tot)) $tot="0.00";
            $fact=0;
            $req="SELECT atelier".ID_GARAGE.".NoCommande, commande".ID_GARAGE.".Type, commande".ID_GARAGE.".MontantTotal FROM atelier".ID_GARAGE.", commande".ID_GARAGE." WHERE atelier".ID_GARAGE.".Date>='$E_annee-".Chiffre_2_Car($S_mois)."-".Chiffre_2_Car($jour_debut)."' AND atelier".ID_GARAGE.".Date<='$E_annee-".Chiffre_2_Car($E_mois)."-".Chiffre_2_Car($jour_fin)."' AND atelier".ID_GARAGE.".NoCommande>0 AND atelier".ID_GARAGE.".NoCommande = commande".ID_GARAGE.".NoCommande AND (commande".ID_GARAGE.".Genre = 'F' OR commande".ID_GARAGE.".Genre = 'G')";
            if($result=mysql_query($req)){
              while($lign=mysql_fetch_array($result)){
            if ($lign[1]=="FD" || $lign[1]=="GF" || $lign[1]=="FV" || $lign[1]=="FO"){
              $montant_total_factures+=$lign[2];
            }
            $req2="SELECT SUM(Quantite) FROM commandearticles".ID_GARAGE." WHERE NoCommande=".$lign[0]." AND GM=".Config::getGMMO();
            if($result2=mysql_query($req2)){
              if($lign2=mysql_fetch_array($result2)){
                $fact+=$lign2[0];
              }
            }
          }
        }
        $fact=Arrondi_1_ct($fact);
        $pot = 0;            
        $cpt = date("t",mktime(0,0,0,$S_mois,1,$E_annee));
          for ($c_cpt=-$cpt; $c_cpt <= 0; $c_cpt++){
              $c_date=mktime(0,0,0,$S_mois,$jour_debut+$c_cpt,$E_annee);
              $c_jour = date("d",$c_date);
              $c_mois = date("m",$c_date);
              $c_annee = date("Y",$c_date);
              $pot += get_potentiel_atelier_tous($c_jour, $c_mois, $c_annee);
          }
          $pot=Arrondi_1_ct($pot);
          if ($pot != 0){
            echo "<TR><TH>Total</TH><TH>$tot</TH><TH>$pot</TH><TH>$fact</TH><TH>".(round(100*$fact/$pot))/$dif_mois."</TH></TR>\n";
          }
          echo "</TABLE></TD><TD COLSPAN='".(1+$nb_pret)."'>";
          echo "</TD></TR>\n";
        echo "<TR class='tr_sep_jour'><TD class='td_sep_jour' COLSPAN='2".(5+$nb_pret)."'></TD></TR>\n";
        }
    echo "</TABLE>\n";
}
?>
</body>
</html>
