Corrections (fichier, calcul de ref)

This commit is contained in:
Emmanuel Haguet 2015-01-21 16:25:13 +01:00
parent 0b3aebeac0
commit 296d947bd2
2 changed files with 27 additions and 20 deletions

View File

@ -3,7 +3,7 @@
* Script d'import des fournisseurs depuis le fichier data.csv * Script d'import des fournisseurs depuis le fichier data.csv
* @author Emmanuel Haguet <ehaguet@teclib.com> * @author Emmanuel Haguet <ehaguet@teclib.com>
*/ */
const CSV_FILENAME = "fournisseurs_et_produit.csv"; const CSV_FILENAME = "fournisseurs_et_produits.csv";
const CSV_SEPARATOR = ','; const CSV_SEPARATOR = ',';
const NB_MIN_COLONNE = 5; const NB_MIN_COLONNE = 5;
const USERNAME = 'dolibarr'; const USERNAME = 'dolibarr';

View File

@ -1,9 +1,9 @@
<?php <?php
/* /*
* Script d'import des produits depuis le fichier data.csv * Script d'import des produits depuis le fichier fournisseurs_et_produits.csv
* @author Emmanuel Haguet <ehaguet@teclib.com> * @author Emmanuel Haguet <ehaguet@teclib.com>
*/ */
const CSV_FILENAME = "fournisseurs_et_produit.csv"; const CSV_FILENAME = "fournisseurs_et_produits.csv";
const CSV_SEPARATOR = ','; const CSV_SEPARATOR = ',';
const NB_MIN_COLONNE = 5; const NB_MIN_COLONNE = 5;
const USERNAME = 'dolibarr'; const USERNAME = 'dolibarr';
@ -20,10 +20,16 @@ function getRefOfProduct($name) {
global $db; global $db;
$resql = $db->query("SELECT * $resql = $db->query("SELECT *
FROM ".MAIN_DB_PREFIX."product FROM ".MAIN_DB_PREFIX."product
WHERE ref LIKE '".$name."_%' WHERE ref LIKE '".$name."\_%'
ORDER BY ref desc"); ORDER BY ref desc");
$obj = $db->fetch_object($resql); $obj = $db->fetch_object($resql);
return $name . '_' . ($obj) ? (substr(strstr($obj->ref, '_'), 1) +1) : 0; if ($obj) {
$num = substr(strstr($obj->ref, '_'), 1);
$num++;
} else {
$num = 0;
}
return $name . '_' . str_pad($num, 4, '0', STR_PAD_LEFT);
} }
/** /**
@ -34,12 +40,11 @@ function getRefOfProduct($name) {
*/ */
function multiexplode($delimiters, $string) { function multiexplode($delimiters, $string) {
$ready = str_replace($delimiters, $delimiters[0], $string); $ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready); return explode($delimiters[0], $ready);
return $launch;
} }
function constructRef($libelle_produit) { function constructRefLetter($libelle_produit) {
$ref = ''; $ref = ''; //init
$i = 0; $i = 0;
$parts_ref = multiexplode(array(' ', '-'), $libelle_produit); $parts_ref = multiexplode(array(' ', '-'), $libelle_produit);
foreach ($parts_ref as $part_ref) { foreach ($parts_ref as $part_ref) {
@ -60,41 +65,43 @@ function import($data) {
} }
$name = ucfirst($data[1]); $name = ucfirst($data[1]);
if (empty($name)) { if (empty($name)) {
//echo "<tr><td>Nom vide</td></tr>";
return; return;
} }
$libelle_produit = $data[2]; $libelle_produit = $data[2];
$ref = getRefOfProduct(constructRef($libelle_produit)); $ref_letter = constructRefLetter($libelle_produit);
$ref = getRefOfProduct($ref_letter);
/*echo "<tr> /*echo "<tr>
<td>$name</td> <td>$name</td>
<td>$libelle_produit</td> <td>$libelle_produit</td>
<td>$ref</td> <td>$ref</td>";*/
</tr>";*/
$product = new Product($db); $product = new Product($db);
$product->ref = $ref; $product->ref = $ref;
$product->libelle = $name; $product->libelle = $name;
$product->status = 0; //tosell
//Important : tosell, tobuy $product->status_buy = 1; //tobuy
$product->status = 0;
$product->status_buy = 1;
$user = new User($db); $user = new User($db);
$user->fetch('', USERNAME); $user->fetch('', USERNAME);
$returnvalue = $product->create($user); $returnvalue = $product->create($user);
//echo "<td>";
if ($returnvalue >= 0) { if ($returnvalue >= 0) {
echo 'OK '; //echo 'OK';
} else { } else {
//TODO : Meilleur gestion des messages d'erreur //echo '<span style="color:red">KO</span>';
echo '<span style="color:red">KO</span> ';
} }
//echo "</td></tr>";
} }
//$csv = array_map('str_getcsv', file(CSV_FILENAME)); //$csv = array_map('str_getcsv', file(CSV_FILENAME));
/*echo "<table>"; /*
echo "<table>";
echo "<tr> echo "<tr>
<td>name</td> <td>name</td>
<td>libelle_produit</td> <td>libelle_produit</td>