import-thirdparties.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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-thirdparties.php
  23. * \brief Script example to insert thirdparties 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-thirdparties.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[3])?1:$argv[3];
  58. $endlinenb = empty($argv[4])?0:$argv[4];
  59. if (empty($mode) || ! in_array($mode,array('test','confirm','confirmforced')) || empty($filepath)) {
  60. print "Usage: $script_file (test|confirm|confirmforced) filepath.csv [startlinenb] [endlinenb]\n";
  61. print "Usage: $script_file test myfilepath.csv 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=0;
  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. $object = new Societe($db);
  108. $object->state = $fields[6];
  109. $object->client = $fields[7];
  110. $object->fournisseur = $fields[8];
  111. $object->name = $fields[13]?trim($fields[13]):$fields[0];
  112. $object->name_alias = $fields[0]!=$fields[13]?trim($fields[0]):'';
  113. $object->address = trim($fields[14]);
  114. $object->zip = trim($fields[15]);
  115. $object->town = trim($fields[16]);
  116. $object->country_id = dol_getIdFromCode($db, trim($fields[21]), 'c_country', 'code', 'rowid');
  117. $object->phone = trim($fields[22]);
  118. $object->fax = trim($fields[23]);
  119. $object->email = trim($fields[26]);
  120. $object->idprof2 = trim($fields[29]);
  121. $object->tva_intra = trim($fields[34]);
  122. $object->default_lang = trim($fields[43]);
  123. //$condpayment = dol_string_unaccent(trim($fields[36]));
  124. if ($fields[36])
  125. {
  126. $condpayment = trim($fields[36]);
  127. if ($condpayment == 'A la commande') $condpayment = 'A réception de commande';
  128. if ($condpayment == 'A reception facture') $condpayment = 'Réception de facture';
  129. $object->cond_reglement_id = dol_getIdFromCode($db, $condpayment, 'c_payment_term', 'libelle_facture', 'rowid', 1);
  130. if (empty($object->cond_reglement_id))
  131. {
  132. print " - Error cant find payment mode for ".$condpayment."\n";
  133. $errorrecord++;
  134. }
  135. }
  136. $object->code_client = $fields[9];
  137. $object->code_fournisseur = $fields[10];
  138. $labeltype = trim($fields[1]);
  139. $object->typent_id = dol_getIdFromCode($db, $labeltype, 'c_typent', 'libelle');
  140. // Set price level
  141. $object->price_level = 1;
  142. if ($labeltype == 'Revendeur') $object->price_level = 2;
  143. print "Process line nb ".$i.", name ".$object->name;
  144. // Extrafields
  145. $object->array_options['options_anastate']=price2num($fields[20]);
  146. $object->array_options['options_anaregion']=price2num($fields[17]);
  147. if (! $errorrecord)
  148. {
  149. $ret=$object->create($user);
  150. if ($ret < 0)
  151. {
  152. print " - Error in create result code = ".$ret." - ".$object->errorsToString();
  153. $errorrecord++;
  154. }
  155. else
  156. {
  157. print " - Creation OK with name ".$object->name." - id = ".$ret;
  158. }
  159. }
  160. if (! $errorrecord)
  161. {
  162. dol_syslog("Set price level");
  163. $object->set_price_level($object->price_level, $user);
  164. }
  165. // Assign sales representative
  166. if (! $errorrecord && $fields[3])
  167. {
  168. $salesrep=new User($db);
  169. $tmp=explode(' ',$fields[3],2);
  170. $salesrep->firstname = trim($tmp[0]);
  171. $salesrep->lastname = trim($tmp[1]);
  172. if ($salesrep->lastname) $salesrep->login = strtolower(substr($salesrep->firstname, 0, 1)) . strtolower(substr($salesrep->lastname, 0));
  173. else $salesrep->login=strtolower($salesrep->firstname);
  174. $salesrep->login=preg_replace('/ /','',$salesrep->login);
  175. $salesrep->fetch(0,$salesrep->login);
  176. $result = $object->add_commercial($user, $salesrep->id);
  177. if ($result < 0)
  178. {
  179. print " - Error in create link with sale representative result code = ".$result." - ".$object->errorsToString();
  180. $errorrecord++;
  181. }
  182. else
  183. {
  184. print " - create link sale representative OK";
  185. }
  186. }
  187. dol_syslog("Add invoice contacts");
  188. // Insert an invoice contact if there is an invoice email != standard email
  189. if (! $errorrecord && $fields[27] && $fields[26] != $fields[27])
  190. {
  191. $ret1=$ret2=0;
  192. $contact = new Contact($db);
  193. $contact->lastname = $object->name;
  194. $contact->address=$object->address;
  195. $contact->zip=$object->zip;
  196. $contact->town=$object->town;
  197. $contact->country_id=$object->country_id;
  198. $contact->email=$fields[27];
  199. $contact->socid=$object->id;
  200. $ret1=$contact->create($user);
  201. if ($ret1 > 0)
  202. {
  203. //$ret2=$contact->add_contact($object->id, 'BILLING');
  204. }
  205. if ($ret1 < 0 || $ret2 < 0)
  206. {
  207. print " - Error in create contact result code = ".$ret1." ".$ret2." - ".$object->errorsToString();
  208. $errorrecord++;
  209. }
  210. else
  211. {
  212. print " - create contact OK";
  213. }
  214. }
  215. dol_syslog("Add delivery contacts");
  216. // Insert a delivery contact
  217. if (! $errorrecord && $fields[47])
  218. {
  219. $ret1=$ret2=0;
  220. $contact2 = new Contact($db);
  221. $contact2->lastname = 'Service livraison - '.$fields[47];
  222. $contact2->address = $fields[48];
  223. $contact2->zip = $fields[50];
  224. $contact2->town = $fields[51];
  225. $contact2->country_id=dol_getIdFromCode($db, trim($fields[52]), 'c_country', 'code', 'rowid');
  226. $contact2->note_public=$fields[54];
  227. $contact2->socid=$object->id;
  228. // Extrafields
  229. $contact2->array_options['options_anazoneliv']=price2num($fields[53]);
  230. $ret1=$contact2->create($user);
  231. if ($ret1 > 0)
  232. {
  233. //$ret2=$contact2->add_contact($object->id, 'SHIPPING');
  234. }
  235. if ($ret1 < 0 || $ret2 < 0)
  236. {
  237. print " - Error in create contact result code = ".$ret1." ".$ret2." - ".$object->errorsToString();
  238. $errorrecord++;
  239. }
  240. else
  241. {
  242. print " - create contact OK";
  243. }
  244. }
  245. print "\n";
  246. if ($errorrecord)
  247. {
  248. fwrite($fhandleerr, 'Error on record nb '.$i." - ".$object->errorsToString()."\n");
  249. $error++; // $errorrecord will be reset
  250. }
  251. }
  252. // commit or rollback
  253. print "Nb of lines qualified: ".$nboflines."\n";
  254. print "Nb of errors: ".$error."\n";
  255. if ($mode != 'confirmforced' && ($error || $mode != 'confirm'))
  256. {
  257. print "Rollback any changes.\n";
  258. $db->rollback();
  259. }
  260. else
  261. {
  262. print "Commit all changes.\n";
  263. $db->commit();
  264. }
  265. $db->close();
  266. fclose($fhandle);
  267. fclose($fhandleerr);
  268. exit($error);