generate-proposal.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. * Copyright (C) 2007 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-proposal.php
  23. * \brief Script example to inject random proposals (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-propale.php/i','',$_SERVER["PHP_SELF"]);
  36. require __DIR__. '/../../htdocs/master.inc.php';
  37. require_once DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php";
  38. require_once DOL_DOCUMENT_ROOT."/comm/propal/class/propal.class.php";
  39. require_once DOL_DOCUMENT_ROOT."/commande/class/commande.class.php";
  40. require_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
  41. /*
  42. * Parameters
  43. */
  44. define('GEN_NUMBER_PROPAL', $argv[1] ?? 10);
  45. $year = 2016;
  46. $dates = array(mktime(12, 0, 0, 1, 3, $year),
  47. mktime(12, 0, 0, 1, 9, $year),
  48. mktime(12, 0, 0, 2, 13, $year),
  49. mktime(12, 0, 0, 2, 23, $year),
  50. mktime(12, 0, 0, 3, 30, $year),
  51. mktime(12, 0, 0, 4, 3, $year),
  52. mktime(12, 0, 0, 4, 3, $year),
  53. mktime(12, 0, 0, 5, 9, $year),
  54. mktime(12, 0, 0, 5, 1, $year),
  55. mktime(12, 0, 0, 5, 13, $year),
  56. mktime(12, 0, 0, 5, 19, $year),
  57. mktime(12, 0, 0, 5, 23, $year),
  58. mktime(12, 0, 0, 6, 3, $year),
  59. mktime(12, 0, 0, 6, 19, $year),
  60. mktime(12, 0, 0, 6, 24, $year),
  61. mktime(12, 0, 0, 7, 3, $year),
  62. mktime(12, 0, 0, 7, 9, $year),
  63. mktime(12, 0, 0, 7, 23, $year),
  64. mktime(12, 0, 0, 7, 30, $year),
  65. mktime(12, 0, 0, 8, 9, $year),
  66. mktime(12, 0, 0, 9, 23, $year),
  67. mktime(12, 0, 0, 10, 3, $year),
  68. mktime(12, 0, 0, 11, 12, $year),
  69. mktime(12, 0, 0, 11, 13, $year),
  70. mktime(12, 0, 0, 1, 3, ($year - 1)),
  71. mktime(12, 0, 0, 1, 9, ($year - 1)),
  72. mktime(12, 0, 0, 2, 13, ($year - 1)),
  73. mktime(12, 0, 0, 2, 23, ($year - 1)),
  74. mktime(12, 0, 0, 3, 30, ($year - 1)),
  75. mktime(12, 0, 0, 4, 3, ($year - 1)),
  76. mktime(12, 0, 0, 4, 3, ($year - 1)),
  77. mktime(12, 0, 0, 5, 9, ($year - 1)),
  78. mktime(12, 0, 0, 5, 1, ($year - 1)),
  79. mktime(12, 0, 0, 5, 13, ($year - 1)),
  80. mktime(12, 0, 0, 5, 19, ($year - 1)),
  81. mktime(12, 0, 0, 5, 23, ($year - 1)),
  82. mktime(12, 0, 0, 6, 3, ($year - 1)),
  83. mktime(12, 0, 0, 6, 19, ($year - 1)),
  84. mktime(12, 0, 0, 6, 24, ($year - 1)),
  85. mktime(12, 0, 0, 7, 3, ($year - 1)),
  86. mktime(12, 0, 0, 7, 9, ($year - 1)),
  87. mktime(12, 0, 0, 7, 23, ($year - 1)),
  88. mktime(12, 0, 0, 7, 30, ($year - 1)),
  89. mktime(12, 0, 0, 8, 9, ($year - 1)),
  90. mktime(12, 0, 0, 9, 23, ($year - 1)),
  91. mktime(12, 0, 0, 10, 3, ($year - 1)),
  92. mktime(12, 0, 0, 11, 12, $year),
  93. mktime(12, 0, 0, 11, 13, $year),
  94. mktime(12, 0, 0, 12, 12, $year),
  95. mktime(12, 0, 0, 12, 13, $year),
  96. );
  97. $ret=$user->fetch('', 'admin');
  98. if (! $ret > 0) {
  99. print 'A user with login "admin" and all permissions must be created to use this script.'."\n";
  100. exit;
  101. }
  102. $user->getrights();
  103. $socids = array();
  104. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe WHERE client in (1,3)";
  105. $resql = $db->query($sql);
  106. if ($resql) {
  107. $num_thirdparties = $db->num_rows($resql);
  108. $i = 0;
  109. while ($i < $num_thirdparties) {
  110. $i++;
  111. $row = $db->fetch_row($resql);
  112. $socids[$i] = $row[0];
  113. }
  114. }
  115. $contids = array();
  116. $sql = "SELECT rowid, fk_soc FROM ".MAIN_DB_PREFIX."socpeople";
  117. $resql = $db->query($sql);
  118. if ($resql) {
  119. $num_conts = $db->num_rows($resql);
  120. $i = 0;
  121. while ($i < $num_conts) {
  122. $i++;
  123. $row = $db->fetch_row($resql);
  124. $contids[$row[1]][0] = $row[0]; // A ameliorer
  125. }
  126. }
  127. $prodids = array();
  128. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product WHERE tosell=1";
  129. $resql = $db->query($sql);
  130. if ($resql) {
  131. $num_prods = $db->num_rows($resql);
  132. $i = 0;
  133. while ($i < $num_prods) {
  134. $i++;
  135. $row = $db->fetch_row($resql);
  136. $prodids[$i] = $row[0];
  137. }
  138. }
  139. $user->rights->propal->creer=1;
  140. $user->rights->propal->propal_advance->validate=1;
  141. if (getDolGlobalString('PROPALE_ADDON') && is_readable(DOL_DOCUMENT_ROOT ."/core/modules/propale/" . getDolGlobalString('PROPALE_ADDON').".php")) {
  142. require_once DOL_DOCUMENT_ROOT ."/core/modules/propale/" . getDolGlobalString('PROPALE_ADDON').".php";
  143. }
  144. $i=0;
  145. $result=0;
  146. while ($i < GEN_NUMBER_PROPAL && $result >= 0) {
  147. $i++;
  148. $socid = mt_rand(1, $num_thirdparties);
  149. print "Proposal ".$i." for socid ".$socid;
  150. $soc = new Societe($db);
  151. $object = new Propal($db);
  152. $fuser = new User($db);
  153. $fuser->fetch(mt_rand(1, 2));
  154. $fuser->getRights();
  155. $object->contactid = $contids[$socids[$socid]][0];
  156. $object->socid = $socids[$socid];
  157. $object->datep = $dates[mt_rand(1, count($dates)-1)];
  158. $object->cond_reglement_id = 3;
  159. $object->mode_reglement_id = 3;
  160. $result=$object->create($fuser);
  161. if ($result >= 0) {
  162. $nbp = mt_rand(2, 5);
  163. $xnbp = 0;
  164. while ($xnbp < $nbp) {
  165. $prodid = mt_rand(1, $num_prods);
  166. $product=new Product($db);
  167. $result=$product->fetch($prodids[$prodid]);
  168. $result=$object->addline($product->description, $product->price, mt_rand(1, 5), 0, 0, 0, $prodids[$prodid], 0);
  169. if ($result < 0) {
  170. dol_print_error($db, $object->error);
  171. }
  172. $xnbp++;
  173. }
  174. $result=$object->valid($fuser);
  175. if ($result > 0) {
  176. $db->commit();
  177. print " OK with ref ".$object->ref."\n";
  178. } else {
  179. print " KO\n";
  180. $db->rollback();
  181. dol_print_error($db, $object->error);
  182. }
  183. } else {
  184. dol_print_error($db, $object->error);
  185. }
  186. }