create_order.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/php
  2. <?php
  3. /* Copyright (C) 2009 Laurent Destailleur <eldy@users.sourceforge.net>
  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 2 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. /**
  19. * \file dev/samples/manage_order.php
  20. * \brief This file is an example for a command line script
  21. * \author Put author name here
  22. * \remarks Put here some comments
  23. */
  24. $sapi_type = php_sapi_name();
  25. $script_file = basename(__FILE__);
  26. $path=dirname(__FILE__).'/';
  27. // Test if batch mode
  28. if (substr($sapi_type, 0, 3) == 'cgi') {
  29. echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  30. exit;
  31. }
  32. // Global variables
  33. $version='$Revision: 1.11 $';
  34. $error=0;
  35. // -------------------- START OF YOUR CODE HERE --------------------
  36. // Include Dolibarr environment
  37. require_once($path."../../htdocs/master.inc.php");
  38. // After this $db, $mysoc, $langs and $conf->entity are defined. Opened handler to database will be closed at end of file.
  39. //$langs->setDefaultLang('en_US'); // To change default language of $langs
  40. $langs->load("main"); // To load language file for default language
  41. @set_time_limit(0);
  42. // Load user and its permissions
  43. $result=$user->fetch('','admin'); // Load user for login 'admin'. Comment line to run as anonymous user.
  44. if (! $result > 0) { dol_print_error('',$user->error); exit; }
  45. $user->getrights();
  46. print "***** ".$script_file." (".$version.") *****\n";
  47. if (! isset($argv[1])) { // Check parameters
  48. print "Usage: ".$script_file." param1 param2 ...\n";
  49. exit;
  50. }
  51. print '--- start'."\n";
  52. print 'Argument 1='.$argv[1]."\n";
  53. print 'Argument 2='.$argv[2]."\n";
  54. // Start of transaction
  55. $db->begin();
  56. require_once(DOL_DOCUMENT_ROOT."/commande/class/commande.class.php");
  57. // Create order object
  58. $com = new Commande($db);
  59. $com->ref = 'ABCDE';
  60. $com->socid = 4; // Put id of third party (rowid in llx_societe table)
  61. $com->date_commande = mktime();
  62. $com->note = 'A comment';
  63. $com->source = 1;
  64. $com->remise_percent = 0;
  65. $orderline1=new OrderLine($db);
  66. $orderline1->tva_tx=10.0;
  67. $orderline1->remise_percent=0;
  68. $orderline1->qty=1;
  69. $com->lines[]=$orderline1;
  70. // Create order
  71. $idobject=$com->create($user);
  72. if ($idobject > 0)
  73. {
  74. // Change status to validated
  75. $result=$com->valid($user);
  76. if ($result > 0) print "OK Object created with id ".$idobject."\n";
  77. else
  78. {
  79. $error++;
  80. dol_print_error($db,$com->error);
  81. }
  82. }
  83. else
  84. {
  85. $error++;
  86. dol_print_error($db,$com->error);
  87. }
  88. // -------------------- END OF YOUR CODE --------------------
  89. if (! $error)
  90. {
  91. $db->commit();
  92. print '--- end ok'."\n";
  93. }
  94. else
  95. {
  96. print '--- end error code='.$error."\n";
  97. $db->rollback();
  98. }
  99. $db->close();
  100. return $error;
  101. ?>