<?php
  switch($Etape)
  {
    case 0:
      echo "<form method='POST' ENCTYPE='multipart/form-data'><input type='hidden' name='Type' value=$Type><input type='hidden' name='Sous_Type' value=$Sous_Type><input type='hidden' name='Etape' value=1>";
      echo "<table border=1>";
      echo "<tr><th>Groupe</th><th>Groupe marchandise</th><th>rabais</th></tr>";
      echo "<tr><th>AA Réservoirs</th><td><input name=GMAA value='55'></td><td><input name=RAA value='20.0'></td></tr>";
      echo "<tr><th>BB EPC Datas</th><td><input name=GMBB value='61'></td><td><input name=RBB value='0.0'></td></tr>";
      echo "<tr><th>CC Pièces HRC</th><td><input name=GMCC value='56'></td><td><input name=RCC value='10.0'></td></tr>";
      echo "<tr><th>JJ Accessoires audio</th><td><input name=GMJJ value='60'></td><td><input name=RJJ value='24.2'></td></tr>";
      echo "<tr><th>MJ Divers</th><td><input name=GMMJ value='58'></td><td><input name=RMJ value='15.0'></td></tr>";
      echo "<tr><th>MK Divers</th><td><input name=GMMK value='59'></td><td><input name=RMK value='20.0'></td></tr>";
      echo "<tr><th>MM Basic</th><td><input name=GMMM value='50'></td><td><input name=RMM value='30.0'></td></tr>";
      echo "<tr><th>MM Casques accessoires</th><td><input name=GMMM2 value='57'></td><td><input name=RMM2 value='30.0'></td></tr>";
      echo "<tr><th>RR Casques</th><td><input name=GMRR value='16'></td><td><input name=RRR value='40.0'></td></tr>";
      echo "<tr><th>TT Filtre air + Plaquettes frein</th><td><input name=GMTT value='51'></td><td><input name=RTT value='45.0'></td></tr>";
      echo "<tr><th>VV Filtre huile + Kits chaine + Ampoules</th><td><input name=GMVV value='52'></td><td><input name=RVV value='45.0'></td></tr>";
      echo "<tr><th>WW Batteries</th><td><input name=GMWW value='53'></td><td><input name=RWW value='55.0'></td></tr>";
      echo "<tr><th>XX Bougies</th><td><input name=GMXX value='54'></td><td><input name=RXX value='60.0'></td></tr>";
      echo "</table>";
      echo "<INPUT TYPE='file' name='liste'><button type='submit'>Importer</button></form>\n";
    break;
    case 1:
      set_time_limit ( 3600 );
      $file=fopen($liste["tmp_name"],'r');
      echo "salut ".print_r($liste)."<BR>";
      echo "<BIG>Veuillez patienter svp</BIG><BR>";
      $nbart=0;
      $nbinc=0;
      $poscpt=0;
      while($ligne=fgets($file,300))
      {
        $poscpt++;
        if(($poscpt%10000)==0)
        {
          echo "en cours<BR>\n";
        }
        $NoArticleRR=substr($ligne,0,15);
        $NoArticleRR=str_replace(" ", "", $NoArticleRR);
        $EditCode=substr($ligne,15,1);
        $NoArticle="bof";
        switch($EditCode)
        {
          case 'A':
            $NoArticle=$NoArticleRR;
          break;
          case 'B':
            $NoArticle=substr($NoArticleRR,0,5)." ".substr($NoArticleRR,5,3)." ".substr($NoArticleRR,8);
          break;
          case 'C':
            $NoArticle=substr($NoArticleRR,0,5)." ".substr($NoArticleRR,5);
          break;
          case 'D':
            $NoArticle=substr($NoArticleRR,0,5)." ".substr($NoArticleRR,5,5)." ".substr($NoArticleRR,10);
          break;
          case 'E':
            $NoArticle=substr($NoArticleRR,0,5)." ".substr($NoArticleRR,5,4)." ".substr($NoArticleRR,9);
          break;
          case 'F':
            $NoArticle=substr($NoArticleRR,0,3)." ".substr($NoArticleRR,3);
          break;
          case 'G':
            $NoArticle=substr($NoArticleRR,0,4)." ".substr($NoArticleRR,4,3)." ".substr($NoArticleRR,7);
          break;
          case 'H':
            $NoArticle=substr($NoArticleRR,0,5)." ".substr($NoArticleRR,5,1)." ".substr($NoArticleRR,6);
          break;
          case 'I':
            $NoArticle=substr($NoArticleRR,0,4)." ".substr($NoArticleRR,4);
          break;
          case 'M':
            $NoArticle=substr($NoArticleRR,0,4)." ".substr($NoArticleRR,4);
          break;
          case 'N':
            $NoArticle=substr($NoArticleRR,0,5)." ".substr($NoArticleRR,5,4)." ".substr($NoArticleRR,9,4)." ".substr($NoArticleRR,13);
          break;
          case 'P':
            $NoArticle=$NoArticleRR;
          break;
          case 'Q':
            $NoArticle=substr($NoArticleRR,0,3)." ".substr($NoArticleRR,3,1)." ".substr($NoArticleRR,4,2)." ".substr($NoArticleRR,6);
          break;
          case 'R':
            $NoArticle=substr($NoArticleRR,0,3)." ".substr($NoArticleRR,3,1)." ".substr($NoArticleRR,4);
          break;
          case 'S':
            $NoArticle=substr($NoArticleRR,0,3)." ".substr($NoArticleRR,3,2)." ".substr($NoArticleRR,5);
          break;
        }
        if(substr($ligne,21,1)!=" ")
        {
          $Renplacement=1;
          $Description=substr($ligne,23,15);
              $Description=str_replace(" ", "", $Description);
        }
        else
        {
          $Renplacement=0;
          $Description=substr($ligne,177,25);
          while(substr($Description,strlen($Description)-1,1)==' ')
          {
            $Description=substr($Description,0,strlen($Description)-1);
          }
        }
        $PrixHT=substr($ligne,38,13);
        $PrixHT=$PrixHT/1000;
        $Tax=substr($ligne,53,5);
        $Tax=$Tax/100;
        $PrixTTC=substr($ligne,108,13);
        $PrixTTC=$PrixTTC/1000;
        $CodeRabais=substr($ligne,58,2);
        $Rabais=0;
        $GroupeMarchandise=0;
        $GroupeS=substr($ligne,16,1);
        $GroupeM=substr($ligne,18,1);
        switch ($CodeRabais)
        {
          case "AA":
            $Rabais=$RAA;
            $GroupeMarchandise=$GMAA;
          break;
          case "BB":
            $Rabais=$RBB;
            $GroupeMarchandise=$GMBB;
          break;
          case "CC":
            $Rabais=$RCC;
            $GroupeMarchandise=$GMCC;
          break;
          case "JJ":
            $Rabais=$RJJ;
            $GroupeMarchandise=$GMJJ;
          break;
          case "MJ":
            $Rabais=$RMJ;
            $GroupeMarchandise=$GMMJ;
          break;
          case "MK":
            $Rabais=$RMK;
            $GroupeMarchandise=$GMMK;
          break;
          case "MM":
            if(($GroupeS=="M")&&($GroupeM=="A"))
            {
              $Rabais=$RMM2;
              $GroupeMarchandise=$GMMM2;
            }
            else
            {
              $Rabais=$RMM;
              $GroupeMarchandise=$GMMM;
            }
          break;
          case "RR":
            $Rabais=$RRR;
            $GroupeMarchandise=$GMRR;
          break;
          case "TT":
            $Rabais=$RTT;
            $GroupeMarchandise=$GMTT;
          break;
          case "VV":
            $Rabais=$RVV;
            $GroupeMarchandise=$GMVV;
          break;
          case "WW":
            $Rabais=$RWW;
            $GroupeMarchandise=$GMWW;
          break;
          case "XX":
            $Rabais=$RXX;
            $GroupeMarchandise=$GMXX;
          break;
          default:
            $Rabais=29.6;
            $GroupeMarchandise=50;
            $CodeRabais="MM";
            switch(substr($NoArticleRR,0,2))
            {
              case "10":
              case "11":
              case "12":
              case "13":
              case "14":
              case "15":
              case "16":
              case "17":
              case "18":
              case "19":
              case "20":
              case "21":
              case "22":
                $Rabais=$RRR;
                $GroupeMarchandise=$GMRR;
                $CodeRabais="RR";
              break;
              case "41":
              case "42":
              case "43":
              case "44":
              case "45":
              case "46":
              case "47":
              case "48":
              case "49":
              case "50":
              case "51":
              case "52":
              case "53":
                $Rabais=$RRR;
                $GroupeMarchandise=39;
                $CodeRabais="RR";
              break;
            }
          break;
        }
        $QuantiteOpt=substr($ligne,61,7)*1;
        $resultat=Article::getArticleByNoArticleRR(addslashes($NoArticleRR));
        $nb_resultats=count($resultat);
        $requette2="";
        if ($nb_resultats==0)
        {
          $requette2="INSERT INTO articles (NoArticle, NoArticleRR, Description, Type, PrixImportateur, Rabais, CodeRabais, GroupeMarchandise, TVA, DateModif, QuantiteMinimum, DateSaisie, PersonneModif) VALUES ";
          $requette2.="(".
                      "'".addslashes($NoArticle)."'".
                      ", '".addslashes($NoArticleRR)."'".
                      ", '".addslashes($Description)."'".
                      ", 'HONDA'".
                      ", $PrixTTC".
                      ", $Rabais".
                      ", '".addslashes($CodeRabais)."'".
                      ", $GroupeMarchandise".
                      ", -$Tax".
                      ", '".date("Y-m-d")."'".
                      ", $QuantiteOpt".
                      ", '".date("Y-m-d")."'".
                      ", 'import'".
                      ")";
          $requette3="INSERT INTO articlesfournisseurs (NoArticleRR, IdFournisseur) VALUES ('".addslashes($NoArticleRR)."', 1)";
        }
        else if ($nb_resultats==1)
        {
          $requette2="UPDATE articles SET ".
                     "NoArticle='".addslashes($NoArticle)."'".
               ", Description='".addslashes($Description)."'".
               ", Type='HONDA'".
               ", PrixImportateur=$PrixTTC".
               ", Rabais=$Rabais".
               ", CodeRabais='".addslashes($CodeRabais)."'".
               ", GroupeMarchandise=$GroupeMarchandise".
               ", TVA=-$Tax".
               ", DateModif='".date("Y-m-d")."'".
               ", QuantiteMinimum=$QuantiteOpt".
               ", PersonneModif='import'".
               " WHERE NoArticleRR='".addslashes($NoArticleRR)."'";
          $requette3="INSERT INTO articlesfournisseurs (NoArticleRR, IdFournisseur) VALUES ('".addslashes($NoArticleRR)."', 1)";
        }
        else
        {
          $create=1;
          foreach($resultat as $ligner)
          {
            if($ligner['NoArticle']==$NoArticle)
            {
              $create=0;
            }
            else
            {
              Connection::executeRawQuery("DELETE FROM articles WHERE NoArticle='".addslashes($NoArticle)."'");
            }
          }
          if($create==1)
          {
          $requette2="INSERT INTO articles (NoArticle, NoArticleRR, Description, Type, PrixImportateur, Rabais, CodeRabais, GroupeMarchandise, TVA, DateModif, QuantiteMinimum, DateSaisie, PersonneModif) VALUES ";
          $requette2.="(".
                      "'".addslashes($NoArticle)."'".
                      ", '".addslashes($NoArticleRR)."'".
                      ", '".addslashes($Description)."'".
                      ", 'HONDA'".
                      ", $PrixTTC".
                      ", $Rabais".
                      ", '".addslashes($CodeRabais)."'".
                      ", $GroupeMarchandise".
                      ", -$Tax".
                      ", '".date("Y-m-d")."'".
                      ", $QuantiteOpt".
                      ", '".date("Y-m-d")."'".
                      ", 'import'".
                      ")";
          }
          else
          {
            $requette2="UPDATE articles SET ".
                       "NoArticle='".addslashes($NoArticle)."'".
                 ", Description='".addslashes($Description)."'".
                 ", Type='HONDA'".
                 ", PrixImportateur=$PrixHT".
                 ", Rabais=$Rabais".
                 ", CodeRabais='".addslashes($CodeRabais)."'".
                 ", GroupeMarchandise=$GroupeMarchandise".
                 ", TVA=$Tax".
                 ", DateModif='".date("Y-m-d")."'".
                 ", QuantiteMinimum=$QuantiteOpt".
                 ", PersonneModif='import'".
                 " WHERE NoArticleRR='".addslashes($NoArticleRR)."'";
          }
        }
        if($GroupeS!="P")
        {
          if($GroupeMarchandise==0)
          {
            $nbinc++;
          }
          $nbart++;
          Connection::executeRawQuery($requette2);
          Connection::executeRawQuery($requette3);
        }
        else
        {
        }
      }
      //$requette3="DELETE FROM articles WHERE (Type='H' OR Type='HB' OR Type='HP') AND Annee1=0 AND EnStock=0";
      //mysql_query($requette3);
      echo $nbart . "<BR>";
      echo $nbinc . "<BR>";
      echo "<BIG>OK c'est fini</BIG><BR>";
    break;
  }
?>
