generate-invoice.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * ATTENTION DE PAS EXECUTER CE SCRIPT SUR UNE INSTALLATION DE PRODUCTION
  19. */
  20. /**
  21. * \file dev/initdata/generate-invoice.php
  22. * \brief Script example to inject random customer invoices (for load tests)
  23. */
  24. $sapi_type = php_sapi_name();
  25. $script_file = basename(__FILE__);
  26. $path=dirname(__FILE__).'/';
  27. // Test si mode batch
  28. $sapi_type = php_sapi_name();
  29. if (substr($sapi_type, 0, 3) == 'cgi') {
  30. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  31. exit;
  32. }
  33. // Recupere root dolibarr
  34. //$path=preg_replace('/generate-produit.php/i','',$_SERVER["PHP_SELF"]);
  35. require __DIR__. '/../../htdocs/master.inc.php';
  36. require_once DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php";
  37. require_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
  38. /*
  39. * Parameters
  40. */
  41. define('GEN_NUMBER_FACTURE', $argv[1] ?? 1);
  42. $year = 2016;
  43. $dates = array(mktime(12, 0, 0, 1, 3, $year),
  44. mktime(12, 0, 0, 1, 9, $year),
  45. mktime(12, 0, 0, 2, 13, $year),
  46. mktime(12, 0, 0, 2, 23, $year),
  47. mktime(12, 0, 0, 3, 30, $year),
  48. mktime(12, 0, 0, 4, 3, $year),
  49. mktime(12, 0, 0, 4, 3, $year),
  50. mktime(12, 0, 0, 5, 9, $year),
  51. mktime(12, 0, 0, 5, 1, $year),
  52. mktime(12, 0, 0, 5, 13, $year),
  53. mktime(12, 0, 0, 5, 19, $year),
  54. mktime(12, 0, 0, 5, 23, $year),
  55. mktime(12, 0, 0, 6, 3, $year),
  56. mktime(12, 0, 0, 6, 19, $year),
  57. mktime(12, 0, 0, 6, 24, $year),
  58. mktime(12, 0, 0, 7, 3, $year),
  59. mktime(12, 0, 0, 7, 9, $year),
  60. mktime(12, 0, 0, 7, 23, $year),
  61. mktime(12, 0, 0, 7, 30, $year),
  62. mktime(12, 0, 0, 8, 9, $year),
  63. mktime(12, 0, 0, 9, 23, $year),
  64. mktime(12, 0, 0, 10, 3, $year),
  65. mktime(12, 0, 0, 11, 12, $year),
  66. mktime(12, 0, 0, 11, 13, $year),
  67. mktime(12, 0, 0, 1, 3, ($year - 1)),
  68. mktime(12, 0, 0, 1, 9, ($year - 1)),
  69. mktime(12, 0, 0, 2, 13, ($year - 1)),
  70. mktime(12, 0, 0, 2, 23, ($year - 1)),
  71. mktime(12, 0, 0, 3, 30, ($year - 1)),
  72. mktime(12, 0, 0, 4, 3, ($year - 1)),
  73. mktime(12, 0, 0, 4, 3, ($year - 1)),
  74. mktime(12, 0, 0, 5, 9, ($year - 1)),
  75. mktime(12, 0, 0, 5, 1, ($year - 1)),
  76. mktime(12, 0, 0, 5, 13, ($year - 1)),
  77. mktime(12, 0, 0, 5, 19, ($year - 1)),
  78. mktime(12, 0, 0, 5, 23, ($year - 1)),
  79. mktime(12, 0, 0, 6, 3, ($year - 1)),
  80. mktime(12, 0, 0, 6, 19, ($year - 1)),
  81. mktime(12, 0, 0, 6, 24, ($year - 1)),
  82. mktime(12, 0, 0, 7, 3, ($year - 1)),
  83. mktime(12, 0, 0, 7, 9, ($year - 1)),
  84. mktime(12, 0, 0, 7, 23, ($year - 1)),
  85. mktime(12, 0, 0, 7, 30, ($year - 1)),
  86. mktime(12, 0, 0, 8, 9, ($year - 1)),
  87. mktime(12, 0, 0, 9, 23, ($year - 1)),
  88. mktime(12, 0, 0, 10, 3, ($year - 1)),
  89. mktime(12, 0, 0, 11, 12, $year),
  90. mktime(12, 0, 0, 11, 13, $year),
  91. mktime(12, 0, 0, 12, 12, $year),
  92. mktime(12, 0, 0, 12, 13, $year),
  93. );
  94. $ret=$user->fetch('', 'admin');
  95. if (! $ret > 0) {
  96. print 'A user with login "admin" and all permissions must be created to use this script.'."\n";
  97. exit;
  98. }
  99. $user->getrights();
  100. $socids = array();
  101. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe WHERE client in (1, 3)";
  102. $resql = $db->query($sql);
  103. if ($resql) {
  104. $num_thirdparties = $db->num_rows($resql);
  105. $i = 0;
  106. while ($i < $num_thirdparties) {
  107. $i++;
  108. $row = $db->fetch_row($resql);
  109. $socids[$i] = $row[0];
  110. }
  111. }
  112. $prodids = array();
  113. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product WHERE tosell=1";
  114. $resql = $db->query($sql);
  115. if ($resql) {
  116. $num_prods = $db->num_rows($resql);
  117. $i = 0;
  118. while ($i < $num_prods) {
  119. $i++;
  120. $row = $db->fetch_row($resql);
  121. $prodids[$i] = $row[0];
  122. }
  123. }
  124. $i=0;
  125. $result=0;
  126. while ($i < GEN_NUMBER_FACTURE && $result >= 0) {
  127. $i++;
  128. $socid = mt_rand(1, $num_thirdparties);
  129. print "Invoice ".$i." for socid ".$socid;
  130. $object = new Facture($db);
  131. $object->socid = $socids[$socid];
  132. $object->date = $dates[mt_rand(1, count($dates)-1)];
  133. $object->cond_reglement_id = 3;
  134. $object->mode_reglement_id = 3;
  135. $fuser = new User($db);
  136. $fuser->fetch(mt_rand(1, 2));
  137. $fuser->getRights();
  138. $result=$object->create($fuser);
  139. if ($result >= 0) {
  140. $nbp = mt_rand(2, 5);
  141. $xnbp = 0;
  142. while ($xnbp < $nbp) {
  143. $prodid = mt_rand(1, $num_prods);
  144. $product=new Product($db);
  145. $result=$product->fetch($prodids[$prodid]);
  146. $result=$object->addline($product->description, $product->price, mt_rand(1, 5), 0, 0, 0, $prodids[$prodid], 0, '', '', 0, 0, '', $product->price_base_type, $product->price_ttc, $product->type);
  147. if ($result < 0) {
  148. dol_print_error($db, $propal->error);
  149. }
  150. $xnbp++;
  151. }
  152. $result=$object->validate($fuser);
  153. if ($result) {
  154. print " OK with ref ".$object->ref."\n";
  155. ;
  156. } else {
  157. dol_print_error($db, $object->error);
  158. }
  159. } else {
  160. dol_print_error($db, $object->error);
  161. }
  162. }