<?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>ALP1</th><td><input name=GMALP1 value='17'></td><td><input name=RALP1 value='40.0'></td></tr>";
      echo "<tr><th>ALP2</th><td><input name=GMALP2 value='17'></td><td><input name=RALP2 value='40.0'></td></tr>";
      echo "<tr><th>ALP3</th><td><input name=GMALP3 value='17'></td><td><input name=RALP3 value='40.0'></td></tr>";
      echo "<tr><th>ARA1</th><td><input name=GMARA1 value='16'></td><td><input name=RARA1 value='40.0'></td></tr>";
      echo "<tr><th>ARA2</th><td><input name=GMARA2 value='16'></td><td><input name=RARA2 value='40.0'></td></tr>";
      echo "<tr><th>ARA3</th><td><input name=GMARA3 value='16'></td><td><input name=RARA3 value='40.0'></td></tr>";
      echo "<tr><th>BOX1</th><td><input name=GMBOX1 value='33'></td><td><input name=RBOX1 value='40.0'></td></tr>";
      echo "<tr><th>GAS1</th><td><input name=GMGAS1 value='63'></td><td><input name=RGAS1 value='40.0'></td></tr>";
      echo "<tr><th>IXO1</th><td><input name=GMIXO1 value='39'></td><td><input name=RIXO1 value='40.0'></td></tr>";
      echo "<tr><th>IXO2</th><td><input name=GMIXO2 value='39'></td><td><input name=RIXO2 value='40.0'></td></tr>";
      echo "<tr><th>KEN1</th><td><input name=GMKEN1 value='15'></td><td><input name=RKEN1 value='40.0'></td></tr>";
      echo "<tr><th>KRI1</th><td><input name=GMKRI1 value='15'></td><td><input name=RKRI1 value='40.0'></td></tr>";
      echo "<tr><th>MOT1</th><td><input name=GMMOT1 value='15'></td><td><input name=RMOT1 value='40.0'></td></tr>";
      echo "<tr><th>TOR1</th><td><input name=GMTOR1 value='22'></td><td><input name=RTOR1 value='40.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 $liste<BR>";
      echo "<BIG>Veuillez patienter svp</BIG><BR>";
      $nbart=0;
      $nbinc=0;
      $poscpt=0;
	  //echo "<table><tr><th>No Article RR</th><th>No Article</th><th>Description</th><th>Prix</th><th>Tax</th><th>Code rabais</th></tr>\n";
      while($ligne=fgets($file,760))
      {
        $poscpt++;
        if(($poscpt%10000)==0)
        {
          echo "en cours<BR>\n";
        }
        $NoArticleRR=substr($ligne,1,20);
        $NoArticleRR=str_replace(" ", "", $NoArticleRR);
        $EditCode=substr($ligne,21,1);
        $NoArticle="bof";
        switch($EditCode)
        {
          case 'T':
            $NoArticle=substr($NoArticleRR,0,3)."-".substr($NoArticleRR,3,3)."-".substr($NoArticleRR,6);
          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 'E':
            $NoArticle=substr($NoArticleRR,0,5)."-".substr($NoArticleRR,5,4)."-".substr($NoArticleRR,9);
          break;
        }
	    $Description=substr($ligne,72,50);
		$Description=str_replace(" ", "", $Description);
        $Tax=substr($ligne,718,5);
        $Tax=$Tax/100;
        $PrixTTC=substr($ligne,723,13);
        $PrixTTC=$PrixTTC/100;
        $CodeRabais=substr($ligne,632,10);
        $CodeRabais=str_replace(" ", "", $CodeRabais);
		$QuantiteOpt = 1;
		if ($CodeRabais != "")
		{
		  $Rabais = ${ "R".$CodeRabais };
		  $GroupeMarchandise = ${ "GM".$CodeRabais }; 
	    //echo "<tr><td>$NoArticleRR</td><td>$NoArticle</td><td>$Description</td><td>$PrixTTC</td><td>$Tax</td><td>$CodeRabais($Rabais)</td></tr>\n";
        $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)."'");
            }
          }
          $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)";
        }
          if($GroupeMarchandise==0)
          {
            $nbinc++;
          }
          $nbart++;
          Connection::executeRawQuery($requette2);
          Connection::executeRawQuery($requette3);
		}
      }
      echo $nbart . "<BR>";
      echo $nbinc . "<BR>";
	  //echo "</table>";
      echo "<BIG>OK c'est fini</BIG><BR>";
    break;
  }
?>
