generate-invoice.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <http://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. // Test si mode batch
  25. $sapi_type = php_sapi_name();
  26. if (substr($sapi_type, 0, 3) == 'cgi') {
  27. 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";
  28. exit;
  29. }
  30. // Recupere root dolibarr
  31. //$path=preg_replace('/generate-produit.php/i','',$_SERVER["PHP_SELF"]);
  32. require __DIR__. '/../../htdocs/master.inc.php';
  33. require_once DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php";
  34. require_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
  35. /*
  36. * Parameters
  37. */
  38. define(GEN_NUMBER_FACTURE, 1);
  39. $year = 2016;
  40. $dates = array (mktime(12,0,0,1,3,$year),
  41. mktime(12,0,0,1,9,$year),
  42. mktime(12,0,0,2,13,$year),
  43. mktime(12,0,0,2,23,$year),
  44. mktime(12,0,0,3,30,$year),
  45. mktime(12,0,0,4,3,$year),
  46. mktime(12,0,0,4,3,$year),
  47. mktime(12,0,0,5,9,$year),
  48. mktime(12,0,0,5,1,$year),
  49. mktime(12,0,0,5,13,$year),
  50. mktime(12,0,0,5,19,$year),
  51. mktime(12,0,0,5,23,$year),
  52. mktime(12,0,0,6,3,$year),
  53. mktime(12,0,0,6,19,$year),
  54. mktime(12,0,0,6,24,$year),
  55. mktime(12,0,0,7,3,$year),
  56. mktime(12,0,0,7,9,$year),
  57. mktime(12,0,0,7,23,$year),
  58. mktime(12,0,0,7,30,$year),
  59. mktime(12,0,0,8,9,$year),
  60. mktime(12,0,0,9,23,$year),
  61. mktime(12,0,0,10,3,$year),
  62. mktime(12,0,0,11,12,$year),
  63. mktime(12,0,0,11,13,$year),
  64. mktime(12,0,0,1,3,($year - 1)),
  65. mktime(12,0,0,1,9,($year - 1)),
  66. mktime(12,0,0,2,13,($year - 1)),
  67. mktime(12,0,0,2,23,($year - 1)),
  68. mktime(12,0,0,3,30,($year - 1)),
  69. mktime(12,0,0,4,3,($year - 1)),
  70. mktime(12,0,0,4,3,($year - 1)),
  71. mktime(12,0,0,5,9,($year - 1)),
  72. mktime(12,0,0,5,1,($year - 1)),
  73. mktime(12,0,0,5,13,($year - 1)),
  74. mktime(12,0,0,5,19,($year - 1)),
  75. mktime(12,0,0,5,23,($year - 1)),
  76. mktime(12,0,0,6,3,($year - 1)),
  77. mktime(12,0,0,6,19,($year - 1)),
  78. mktime(12,0,0,6,24,($year - 1)),
  79. mktime(12,0,0,7,3,($year - 1)),
  80. mktime(12,0,0,7,9,($year - 1)),
  81. mktime(12,0,0,7,23,($year - 1)),
  82. mktime(12,0,0,7,30,($year - 1)),
  83. mktime(12,0,0,8,9,($year - 1)),
  84. mktime(12,0,0,9,23,($year - 1)),
  85. mktime(12,0,0,10,3,($year - 1)),
  86. mktime(12,0,0,11,12,$year),
  87. mktime(12,0,0,11,13,$year),
  88. mktime(12,0,0,12,12,$year),
  89. mktime(12,0,0,12,13,$year),
  90. );
  91. $ret=$user->fetch('','admin');
  92. if (! $ret > 0)
  93. {
  94. print 'A user with login "admin" and all permissions must be created to use this script.'."\n";
  95. exit;
  96. }
  97. $user->getrights();
  98. $socids = array();
  99. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe WHERE client in (1, 3)";
  100. $resql = $db->query($sql);
  101. if ($resql)
  102. {
  103. $num_thirdparties = $db->num_rows($resql);
  104. $i = 0;
  105. while ($i < $num_thirdparties)
  106. {
  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. {
  117. $num_prods = $db->num_rows($resql);
  118. $i = 0;
  119. while ($i < $num_prods)
  120. {
  121. $i++;
  122. $row = $db->fetch_row($resql);
  123. $prodids[$i] = $row[0];
  124. }
  125. }
  126. $i=0;
  127. $result=0;
  128. while ($i < GEN_NUMBER_FACTURE && $result >= 0)
  129. {
  130. $i++;
  131. $socid = mt_rand(1, $num_thirdparties);
  132. print "Invoice ".$i." for socid ".$socid;
  133. $object = new Facture($db);
  134. $object->socid = $socids[$socid];
  135. $object->date = $dates[mt_rand(1, count($dates)-1)];
  136. $object->cond_reglement_id = 3;
  137. $object->mode_reglement_id = 3;
  138. $fuser = new User($db);
  139. $fuser->fetch(mt_rand(1,2));
  140. $fuser->getRights();
  141. $result=$object->create($fuser);
  142. if ($result >= 0)
  143. {
  144. $nbp = mt_rand(2, 5);
  145. $xnbp = 0;
  146. while ($xnbp < $nbp)
  147. {
  148. $prodid = mt_rand(1, $num_prods);
  149. $product=new Product($db);
  150. $result=$product->fetch($prodids[$prodid]);
  151. $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);
  152. if ($result < 0)
  153. {
  154. dol_print_error($db,$propal->error);
  155. }
  156. $xnbp++;
  157. }
  158. $result=$object->validate($fuser);
  159. if ($result)
  160. {
  161. print " OK with ref ".$object->ref."\n";;
  162. }
  163. else
  164. {
  165. dol_print_error($db,$object->error);
  166. }
  167. }
  168. else
  169. {
  170. dol_print_error($db,$object->error);
  171. }
  172. }