generate-thirdparty.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. * Copyright (C) 2006-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  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 <https://www.gnu.org/licenses/>.
  18. *
  19. * ATTENTION DE PAS EXECUTER CE SCRIPT SUR UNE INSTALLATION DE PRODUCTION
  20. */
  21. /**
  22. * \file dev/initdata/generate-thirdparty.php
  23. * \brief Script example to inject random thirdparties (for load tests)
  24. */
  25. $sapi_type = php_sapi_name();
  26. $script_file = basename(__FILE__);
  27. $path=dirname(__FILE__).'/';
  28. // Test si mode batch
  29. $sapi_type = php_sapi_name();
  30. if (substr($sapi_type, 0, 3) == 'cgi') {
  31. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  32. exit;
  33. }
  34. // Recupere root dolibarr
  35. //$path=preg_replace('/generate-societe.php/i','',$_SERVER["PHP_SELF"]);
  36. require __DIR__. '/../../htdocs/master.inc.php';
  37. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  38. include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  39. include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  40. include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  41. include_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  42. include_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  43. $listoftown = array("Auray","Baden","Vannes","Pirouville","Haguenau","Souffelweiersheim","Illkirch-Graffenstaden","Lauterbourg","Picauville","Sainte-Mère Eglise","Le Bono");
  44. $listoflastname = array("Joe","Marc","Steve","Laurent","Nico","Isabelle","Dorothee","Saby","Brigitte","Karine","Jose-Anne","Celine","Virginie");
  45. /*
  46. * Parametre
  47. */
  48. define('GEN_NUMBER_SOCIETE', $argv[1] ?? 10);
  49. $ret=$user->fetch('', 'admin');
  50. if (! $ret > 0) {
  51. print 'A user with login "admin" and all permissions must be created to use this script.'."\n";
  52. exit;
  53. }
  54. $user->getrights();
  55. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product"; $productsid = array();
  56. $resql=$db->query($sql);
  57. if ($resql) {
  58. $num = $db->num_rows($resql);
  59. $i = 0;
  60. while ($i < $num) {
  61. $row = $db->fetch_row($resql);
  62. $productsid[$i] = $row[0];
  63. $i++;
  64. }
  65. }
  66. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe"; $societesid = array();
  67. $resql=$db->query($sql);
  68. if ($resql) {
  69. $num = $db->num_rows($resql);
  70. $i = 0;
  71. while ($i < $num) {
  72. $row = $db->fetch_row($resql);
  73. $societesid[$i] = $row[0];
  74. $i++;
  75. }
  76. } else {
  77. print "err";
  78. }
  79. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."commande"; $commandesid = array();
  80. $resql=$db->query($sql);
  81. if ($resql) {
  82. $num = $db->num_rows($resql);
  83. $i = 0;
  84. while ($i < $num) {
  85. $row = $db->fetch_row($resql);
  86. $commandesid[$i] = $row[0];
  87. $i++;
  88. }
  89. } else {
  90. print "err";
  91. }
  92. print "Generates ".GEN_NUMBER_SOCIETE." companies\n";
  93. for ($s = 0; $s < GEN_NUMBER_SOCIETE; $s++) {
  94. print "Company $s\n";
  95. $soc = new Societe($db);
  96. $soc->name = "Company num ".time()."$s";
  97. $soc->town = $listoftown[mt_rand(0, count($listoftown)-1)];
  98. $soc->client = mt_rand(1, 2); // Une societe sur 2 est prospect, l'autre client
  99. $soc->fournisseur = mt_rand(0, 1); // Une societe sur 2 est fournisseur
  100. $soc->code_client='CU'.time()."$s";
  101. $soc->code_fournisseur='SU'.time()."$s";
  102. $soc->tva_assuj=1;
  103. $soc->country_id=1;
  104. $soc->country_code='FR';
  105. // Un client sur 3 a une remise de 5%
  106. $user_remise=mt_rand(1, 3);
  107. if ($user_remise==3) {
  108. $soc->remise_percent=5;
  109. }
  110. print "> client=".$soc->client.", fournisseur=".$soc->fournisseur.", remise=".$soc->remise_percent."\n";
  111. $soc->note_private = 'Company created by the script generate-societe.php';
  112. $socid = $soc->create($user);
  113. if ($socid >= 0) {
  114. $rand = mt_rand(1, 4);
  115. print "> Generates $rand contact(s)\n";
  116. for ($c = 0; $c < $rand; $c++) {
  117. $contact = new Contact($db);
  118. $contact->socid = $soc->id;
  119. $contact->lastname = "Lastname".$c;
  120. $contact->firstname = $listoflastname[mt_rand(0, count($listoflastname)-1)];
  121. if ($contact->create($user)) {
  122. }
  123. }
  124. print "Company ".$s." created nom=".$soc->name."\n";
  125. } else {
  126. print "Error: ".$soc->error."\n";
  127. }
  128. }