generate-order.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. * Copyright (C) 2004-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-order.php
  23. * \brief Script example to inject random orders (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-commande.php/i','',$_SERVER["PHP_SELF"]);
  33. require __DIR__. '/../../htdocs/master.inc.php';
  34. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  35. include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  36. include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  37. include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  38. include_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  39. include_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  40. require_once DOL_DOCUMENT_ROOT."/commande/class/commande.class.php";
  41. /*
  42. * Parametre
  43. */
  44. define(GEN_NUMBER_COMMANDE, 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. {
  100. print 'A user with login "admin" and all permissions must be created to use this script.'."\n";
  101. exit;
  102. }
  103. $user->getrights();
  104. $societesid = array();
  105. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe where client in (1, 3)";
  106. $resql=$db->query($sql);
  107. if ($resql) {
  108. $num_thirdparties = $db->num_rows($resql);
  109. $i = 0;
  110. while ($i < $num_thirdparties) {
  111. $i++;
  112. $row = $db->fetch_row($resql);
  113. $societesid[$i] = $row[0];
  114. }
  115. }
  116. else { print "err"; }
  117. $commandesid = array();
  118. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."commande";
  119. $resql=$db->query($sql);
  120. if ($resql) {
  121. $num = $db->num_rows($resql);
  122. $i = 0;
  123. while ($i < $num) {
  124. $i++;
  125. $row = $db->fetch_row($resql);
  126. $commandesid[$i] = $row[0];
  127. }
  128. }
  129. else { print "err"; }
  130. $prodids = array();
  131. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product WHERE tosell=1";
  132. $resql = $db->query($sql);
  133. if ($resql)
  134. {
  135. $num_prods = $db->num_rows($resql);
  136. $i = 0;
  137. while ($i < $num_prods)
  138. {
  139. $i++;
  140. $row = $db->fetch_row($resql);
  141. $prodids[$i] = $row[0];
  142. }
  143. }
  144. print "Build ".GEN_NUMBER_COMMANDE." orders\n";
  145. for ($s = 0 ; $s < GEN_NUMBER_COMMANDE ; $s++)
  146. {
  147. print "Process order ".$s."\n";
  148. $object = new Commande($db);
  149. $object->socid = $societesid[mt_rand(1, $num_thirdparties)];
  150. $object->date_commande = $dates[mt_rand(1, count($dates)-1)];
  151. $object->note = 'My small comment about this order. Hum. Nothing.';
  152. $object->source = 1;
  153. $object->fk_project = 0;
  154. $object->remise_percent = 0;
  155. $object->shipping_method_id = mt_rand(1, 2);
  156. $object->cond_reglement_id = mt_rand(0, 2);
  157. $object->more_reglement_id = mt_rand(0, 7);
  158. $object->availability_id = mt_rand(0, 1);
  159. $listofuserid=array(12,13,16);
  160. $fuser = new User($db);
  161. $fuser->fetch($listofuserid[mt_rand(0,2)]);
  162. $fuser->getRights();
  163. $db->begin();
  164. $result=$object->create($fuser);
  165. if ($result >= 0)
  166. {
  167. $nbp = mt_rand(2, 5);
  168. $xnbp = 0;
  169. while ($xnbp < $nbp)
  170. {
  171. $prodid = mt_rand(1, $num_prods);
  172. $product=new Product($db);
  173. $result=$product->fetch($prodids[$prodid]);
  174. $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);
  175. if ($result <= 0)
  176. {
  177. dol_print_error($db,$object->error);
  178. }
  179. $xnbp++;
  180. }
  181. $result=$object->valid($fuser);
  182. if ($result > 0)
  183. {
  184. $db->commit();
  185. print " OK with ref ".$object->ref."\n";
  186. }
  187. else
  188. {
  189. print " KO\n";
  190. $db->rollback();
  191. dol_print_error($db,$object->error);
  192. }
  193. }
  194. else
  195. {
  196. print " KO\n";
  197. $db->rollback();
  198. dol_print_error($db,$object->error);
  199. }
  200. }