generate-proposal.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <http://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. // Test si mode batch
  26. $sapi_type = php_sapi_name();
  27. if (substr($sapi_type, 0, 3) == 'cgi') {
  28. 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";
  29. exit;
  30. }
  31. // Recupere root dolibarr
  32. //$path=preg_replace('/generate-propale.php/i','',$_SERVER["PHP_SELF"]);
  33. require __DIR__. '/../../htdocs/master.inc.php';
  34. require_once DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php";
  35. require_once DOL_DOCUMENT_ROOT."/comm/propal/class/propal.class.php";
  36. require_once DOL_DOCUMENT_ROOT."/commande/class/commande.class.php";
  37. require_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
  38. /*
  39. * Parameters
  40. */
  41. define(GEN_NUMBER_PROPAL, 10);
  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. {
  97. print 'A user with login "admin" and all permissions must be created to use this script.'."\n";
  98. exit;
  99. }
  100. $user->getrights();
  101. $socids = array();
  102. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe WHERE client in (1,3)";
  103. $resql = $db->query($sql);
  104. if ($resql)
  105. {
  106. $num_thirdparties = $db->num_rows($resql);
  107. $i = 0;
  108. while ($i < $num_thirdparties)
  109. {
  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. {
  120. $num_conts = $db->num_rows($resql);
  121. $i = 0;
  122. while ($i < $num_conts)
  123. {
  124. $i++;
  125. $row = $db->fetch_row($resql);
  126. $contids[$row[1]][0] = $row[0]; // A ameliorer
  127. }
  128. }
  129. $prodids = array();
  130. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product WHERE tosell=1";
  131. $resql = $db->query($sql);
  132. if ($resql)
  133. {
  134. $num_prods = $db->num_rows($resql);
  135. $i = 0;
  136. while ($i < $num_prods)
  137. {
  138. $i++;
  139. $row = $db->fetch_row($resql);
  140. $prodids[$i] = $row[0];
  141. }
  142. }
  143. $user->rights->propal->creer=1;
  144. $user->rights->propal->propal_advance->validate=1;
  145. if (! empty($conf->global->PROPALE_ADDON) && is_readable(DOL_DOCUMENT_ROOT ."/core/modules/propale/".$conf->global->PROPALE_ADDON.".php"))
  146. {
  147. require_once DOL_DOCUMENT_ROOT ."/core/modules/propale/".$conf->global->PROPALE_ADDON.".php";
  148. }
  149. $i=0;
  150. $result=0;
  151. while ($i < GEN_NUMBER_PROPAL && $result >= 0)
  152. {
  153. $i++;
  154. $socid = mt_rand(1, $num_thirdparties);
  155. print "Proposal ".$i." for socid ".$socid;
  156. $soc = new Societe($db);
  157. $object = new Propal($db);
  158. $fuser = new User($db);
  159. $fuser->fetch(mt_rand(1,2));
  160. $fuser->getRights();
  161. $object->contactid = $contids[$socids[$socid]][0];
  162. $object->socid = $socids[$socid];
  163. $object->datep = $dates[mt_rand(1, count($dates)-1)];
  164. $object->cond_reglement_id = 3;
  165. $object->mode_reglement_id = 3;
  166. $result=$object->create($fuser);
  167. if ($result >= 0)
  168. {
  169. $nbp = mt_rand(2, 5);
  170. $xnbp = 0;
  171. while ($xnbp < $nbp)
  172. {
  173. $prodid = mt_rand(1, $num_prods);
  174. $product=new Product($db);
  175. $result=$product->fetch($prodids[$prodid]);
  176. $result=$object->addline($product->description, $product->price, mt_rand(1,5), 0, 0, 0, $prodids[$prodid], 0);
  177. if ($result < 0)
  178. {
  179. dol_print_error($db,$object->error);
  180. }
  181. $xnbp++;
  182. }
  183. $result=$object->valid($fuser);
  184. if ($result > 0)
  185. {
  186. $db->commit();
  187. print " OK with ref ".$object->ref."\n";
  188. }
  189. else
  190. {
  191. print " KO\n";
  192. $db->rollback();
  193. dol_print_error($db,$object->error);
  194. }
  195. }
  196. else
  197. {
  198. dol_print_error($db,$object->error);
  199. }
  200. }