generate-product.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. * Copyright (C) 2004-2010 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-product.php
  23. * \brief Script example to inject random products (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-produit.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. /*
  44. * Parameters
  45. */
  46. define('GEN_NUMBER_PRODUIT', $argv[1] ?? 100);
  47. $ret=$user->fetch('', 'admin');
  48. if (! $ret > 0) {
  49. print 'A user with login "admin" and all permissions must be created to use this script.'."\n";
  50. exit;
  51. }
  52. $user->getrights();
  53. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product"; $productsid = array();
  54. $resql=$db->query($sql);
  55. if ($resql) {
  56. $num = $db->num_rows($resql);
  57. $i = 0;
  58. while ($i < $num) {
  59. $row = $db->fetch_row($resql);
  60. $productsid[$i] = $row[0];
  61. $i++;
  62. }
  63. }
  64. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe"; $societesid = array();
  65. $resql=$db->query($sql);
  66. if ($resql) {
  67. $num = $db->num_rows($resql);
  68. $i = 0;
  69. while ($i < $num) {
  70. $row = $db->fetch_row($resql);
  71. $societesid[$i] = $row[0];
  72. $i++;
  73. }
  74. } else {
  75. print "err";
  76. }
  77. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."commande"; $commandesid = array();
  78. $resql=$db->query($sql);
  79. if ($resql) {
  80. $num = $db->num_rows($resql);
  81. $i = 0;
  82. while ($i < $num) {
  83. $row = $db->fetch_row($resql);
  84. $commandesid[$i] = $row[0];
  85. $i++;
  86. }
  87. } else {
  88. print "err";
  89. }
  90. print "Generates ".GEN_NUMBER_PRODUIT." products\n";
  91. for ($s = 0; $s < GEN_NUMBER_PRODUIT; $s++) {
  92. print "Product ".$s;
  93. $produit = new Product($db);
  94. $produit->type = mt_rand(0, 1);
  95. $produit->status = 1;
  96. $produit->ref = ($produit->type ? 'S' : 'P').time().$s;
  97. $produit->label = 'Label '.time().$s;
  98. $produit->description = 'Description '.time().$s;
  99. $produit->price = mt_rand(1, 999.99);
  100. $produit->tva_tx = "20.0";
  101. $ret=$produit->create($user);
  102. if ($ret < 0) {
  103. print "Error $ret - ".$produit->error."\n";
  104. } else {
  105. print " OK with ref ".$produit->ref."\n";
  106. }
  107. }