import-products.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * WARNING, THIS WILL LOAD MASS DATA ON YOUR INSTANCE
  20. */
  21. /**
  22. * \file dev/initdata/import-product.php
  23. * \brief Script example to insert products from a csv file.
  24. * To purge data, you can have a look at purge-data.php
  25. */
  26. // Test si mode batch
  27. $sapi_type = php_sapi_name();
  28. $script_file = basename(__FILE__);
  29. $path=dirname(__FILE__).'/';
  30. if (substr($sapi_type, 0, 3) == 'cgi') {
  31. echo "Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer mailing-send.php en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI.\n";
  32. exit;
  33. }
  34. // Recupere root dolibarr
  35. $path=preg_replace('/import-products.php/i','',$_SERVER["PHP_SELF"]);
  36. require $path."../../htdocs/master.inc.php";
  37. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  38. include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  39. $delimiter=',';
  40. $enclosure='"';
  41. $linelength=10000;
  42. $escape='/';
  43. // Global variables
  44. $version=DOL_VERSION;
  45. $confirmed=1;
  46. $error=0;
  47. /*
  48. * Main
  49. */
  50. @set_time_limit(0);
  51. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
  52. dol_syslog($script_file." launched with arg ".implode(',',$argv));
  53. $mode = $argv[1];
  54. $filepath = $argv[2];
  55. $filepatherr = $filepath.'.err';
  56. $defaultlang = empty($argv[3])?'en_US':$argv[3];
  57. $startlinenb = empty($argv[4])?1:$argv[4];
  58. $endlinenb = empty($argv[5])?0:$argv[5];
  59. if (empty($mode) || ! in_array($mode,array('test','confirm','confirmforced')) || empty($filepath)) {
  60. print "Usage: $script_file (test|confirm|confirmforced) filepath.csv [defaultlang] [startlinenb] [endlinenb]\n";
  61. print "Usage: $script_file test myfilepath.csv fr_FR 2 1002\n";
  62. print "\n";
  63. exit(-1);
  64. }
  65. if (! file_exists($filepath)) {
  66. print "Error: File ".$filepath." not found.\n";
  67. print "\n";
  68. exit(-1);
  69. }
  70. $ret=$user->fetch('','admin');
  71. if (! $ret > 0)
  72. {
  73. print 'A user with login "admin" and all permissions must be created to use this script.'."\n";
  74. exit;
  75. }
  76. $user->getrights();
  77. // Ask confirmation
  78. if (! $confirmed)
  79. {
  80. print "Hit Enter to continue or CTRL+C to stop...\n";
  81. $input = trim(fgets(STDIN));
  82. }
  83. // Open input and ouput files
  84. $fhandle = fopen($filepath, 'r');
  85. if (! $fhandle)
  86. {
  87. print 'Error: Failed to open file '.$filepath."\n";
  88. exit(1);
  89. }
  90. $fhandleerr = fopen($filepatherr, 'w');
  91. if (! $fhandleerr)
  92. {
  93. print 'Error: Failed to open file '.$filepatherr."\n";
  94. exit(1);
  95. }
  96. $langs->setDefaultLang($defaultlang);
  97. $db->begin();
  98. $i=0;
  99. $nboflines++;
  100. while ($fields=fgetcsv($fhandle, $linelength, $delimiter, $enclosure, $escape))
  101. {
  102. $i++;
  103. $errorrecord=0;
  104. if ($startlinenb && $i < $startlinenb) continue;
  105. if ($endlinenb && $i > $endlinenb) continue;
  106. $nboflines++;
  107. $produit = new Product($db);
  108. $produit->type = 0;
  109. $produit->status = 1;
  110. $produit->ref = trim($fields[0]);
  111. print "Process line nb ".$i.", ref ".$produit->ref;
  112. $produit->label = trim($fields[2]);
  113. $produit->description = trim($fields[4]."\n".($fields[5] ? $fields[5].' x '.$fields[6].' x '.$fields[7] : ''));
  114. $produit->volume = price2num($fields[8]);
  115. $produit->volume_unit = 0;
  116. $produit->weight = price2num($fields[9]);
  117. $produit->weight_units = 0; // -3 = g
  118. $produit->customcode = $fields[10];
  119. $produit->barcode = $fields[1];
  120. $produit->status = 1;
  121. $produit->status_buy = 1;
  122. $produit->finished = 1;
  123. $produit->price_min = null;
  124. $produit->price_min_ttc = null;
  125. $produit->price = price2num($fields[11]);
  126. $produit->price_ttc = price2num($fields[12]);
  127. $produit->price_base_type = 'TTC';
  128. $produit->tva_tx = price2num($fields[13]);
  129. $produit->tva_npr = 0;
  130. $produit->cost_price = price2num($fields[16]);
  131. // Extrafields
  132. $produit->array_options['options_ecotaxdeee']=price2num($fields[17]);
  133. $ret=$produit->create($user);
  134. if ($ret < 0)
  135. {
  136. print " - Error in create result code = ".$ret." - ".$produit->errorsToString();
  137. $errorrecord++;
  138. }
  139. else
  140. {
  141. print " - Creation OK with ref ".$produit->ref." - id = ".$ret;
  142. }
  143. dol_syslog("Add prices");
  144. // If we use price level, insert price for each level
  145. if (! $errorrecord && 1)
  146. {
  147. $ret1=$produit->updatePrice($produit->price_ttc, $produit->price_base_type, $user, $produit->tva_tx, $produit->price_min, 1, $produit->tva_npr, 0, 0, array());
  148. $ret2=$produit->updatePrice(price2num($fields[14]), 'HT', $user, $produit->tva_tx, $produit->price_min, 2, $produit->tva_npr, 0, 0, array());
  149. if ($ret1 < 0 || $ret2 < 0)
  150. {
  151. print " - Error in updatePrice result code = ".$ret1." ".$ret2." - ".$produit->errorsToString();
  152. $errorrecord++;
  153. }
  154. else
  155. {
  156. print " - updatePrice OK";
  157. }
  158. }
  159. dol_syslog("Add multilangs");
  160. // Add alternative languages
  161. if (! $errorrecord && 1)
  162. {
  163. $produit->multilangs['fr_FR']=array('label'=>$produit->label, 'description'=>$produit->description, 'note'=>$produit->note_private);
  164. $produit->multilangs['en_US']=array('label'=>$fields[3], 'description'=>$produit->description, 'note'=>$produit->note_private);
  165. $ret=$produit->setMultiLangs($user);
  166. if ($ret < 0)
  167. {
  168. print " - Error in setMultiLangs result code = ".$ret." - ".$produit->errorsToString();
  169. $errorrecord++;
  170. }
  171. else
  172. {
  173. print " - setMultiLangs OK";
  174. }
  175. }
  176. print "\n";
  177. if ($errorrecord)
  178. {
  179. fwrite($fhandleerr, 'Error on record nb '.$i." - ".$produit->errorsToString()."\n");
  180. $error++; // $errorrecord will be reset
  181. }
  182. }
  183. // commit or rollback
  184. print "Nb of lines qualified: ".$nboflines."\n";
  185. print "Nb of errors: ".$error."\n";
  186. if ($mode != 'confirmforced' && ($error || $mode != 'confirm'))
  187. {
  188. print "Rollback any changes.\n";
  189. $db->rollback();
  190. }
  191. else
  192. {
  193. print "Commit all changes.\n";
  194. $db->commit();
  195. }
  196. $db->close();
  197. fclose($fhandle);
  198. fclose($fhandleerr);
  199. exit($error);