server_order.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. <?php
  2. /* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2012 JF FERRY <jfefe@aternatik.fr>
  4. * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
  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. /**
  20. * \file htdocs/webservices/server_order.php
  21. * \brief File that is entry point to call Dolibarr WebServices
  22. */
  23. if (!defined('NOCSRFCHECK')) {
  24. define('NOCSRFCHECK', '1'); // Do not check anti CSRF attack test
  25. }
  26. if (!defined('NOTOKENRENEWAL')) {
  27. define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
  28. }
  29. if (!defined('NOREQUIREMENU')) {
  30. define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
  31. }
  32. if (!defined('NOREQUIREHTML')) {
  33. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  34. }
  35. if (!defined('NOREQUIREAJAX')) {
  36. define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
  37. }
  38. if (!defined("NOLOGIN")) {
  39. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  40. }
  41. if (!defined("NOSESSION")) {
  42. define("NOSESSION", '1');
  43. }
  44. require '../main.inc.php';
  45. require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
  46. require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
  47. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  48. require_once DOL_DOCUMENT_ROOT."/commande/class/commande.class.php";
  49. dol_syslog("Call Dolibarr webservices interfaces");
  50. $langs->load("main");
  51. // Enable and test if module web services is enabled
  52. if (!getDolGlobalString('MAIN_MODULE_WEBSERVICES')) {
  53. $langs->load("admin");
  54. dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
  55. print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
  56. print $langs->trans("ToActivateModule");
  57. exit;
  58. }
  59. // Create the soap Object
  60. $server = new nusoap_server();
  61. $server->soap_defencoding = 'UTF-8';
  62. $server->decode_utf8 = false;
  63. $ns = 'http://www.dolibarr.org/ns/';
  64. $server->configureWSDL('WebServicesDolibarrOrder', $ns);
  65. $server->wsdl->schemaTargetNamespace = $ns;
  66. // Define WSDL Authentication object
  67. $server->wsdl->addComplexType(
  68. 'authentication',
  69. 'complexType',
  70. 'struct',
  71. 'all',
  72. '',
  73. array(
  74. 'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
  75. 'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
  76. 'login' => array('name'=>'login', 'type'=>'xsd:string'),
  77. 'password' => array('name'=>'password', 'type'=>'xsd:string'),
  78. 'entity' => array('name'=>'entity', 'type'=>'xsd:string')
  79. )
  80. );
  81. // Define WSDL Return object
  82. $server->wsdl->addComplexType(
  83. 'result',
  84. 'complexType',
  85. 'struct',
  86. 'all',
  87. '',
  88. array(
  89. 'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
  90. 'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
  91. )
  92. );
  93. $line_fields = array(
  94. 'id' => array('name'=>'id', 'type'=>'xsd:string'),
  95. 'type' => array('name'=>'type', 'type'=>'xsd:int'),
  96. 'fk_commande' => array('name'=>'fk_commande', 'type'=>'xsd:int'),
  97. 'fk_parent_line' => array('name'=>'fk_parent_line', 'type'=>'xsd:int'),
  98. 'desc' => array('name'=>'desc', 'type'=>'xsd:string'),
  99. 'qty' => array('name'=>'qty', 'type'=>'xsd:double'),
  100. 'price' => array('name'=>'price', 'type'=>'xsd:double'),
  101. 'unitprice' => array('name'=>'unitprice', 'type'=>'xsd:double'),
  102. 'vat_rate' => array('name'=>'vat_rate', 'type'=>'xsd:double'),
  103. 'remise' => array('name'=>'remise', 'type'=>'xsd:double'),
  104. 'remise_percent' => array('name'=>'remise_percent', 'type'=>'xsd:double'),
  105. 'total_net' => array('name'=>'total_net', 'type'=>'xsd:double'),
  106. 'total_vat' => array('name'=>'total_vat', 'type'=>'xsd:double'),
  107. 'total' => array('name'=>'total', 'type'=>'xsd:double'),
  108. 'date_start' => array('name'=>'date_start', 'type'=>'xsd:date'),
  109. 'date_end' => array('name'=>'date_end', 'type'=>'xsd:date'),
  110. // From product
  111. 'product_id' => array('name'=>'product_id', 'type'=>'xsd:int'),
  112. 'product_ref' => array('name'=>'product_ref', 'type'=>'xsd:string'),
  113. 'product_label' => array('name'=>'product_label', 'type'=>'xsd:string'),
  114. 'product_desc' => array('name'=>'product_desc', 'type'=>'xsd:string')
  115. );
  116. $elementtype = 'commandedet';
  117. //Retrieve all extrafield for thirdsparty
  118. // fetch optionals attributes and labels
  119. $extrafields = new ExtraFields($db);
  120. $extrafields->fetch_name_optionals_label($elementtype, true);
  121. $extrafield_line_array = null;
  122. if (is_array($extrafields) && count($extrafields) > 0) {
  123. $extrafield_line_array = array();
  124. }
  125. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  126. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  127. //$value=$object->array_options["options_".$key];
  128. $type = $extrafields->attributes[$elementtype]['type'][$key];
  129. if ($type == 'date' || $type == 'datetime') {
  130. $type = 'xsd:dateTime';
  131. } else {
  132. $type = 'xsd:string';
  133. }
  134. $extrafield_line_array['options_'.$key] = array('name'=>'options_'.$key, 'type'=>$type);
  135. }
  136. }
  137. if (is_array($extrafield_line_array)) {
  138. $line_fields = array_merge($line_fields, $extrafield_line_array);
  139. }
  140. // Define other specific objects
  141. $server->wsdl->addComplexType(
  142. 'line',
  143. 'complexType',
  144. 'struct',
  145. 'all',
  146. '',
  147. $line_fields
  148. );
  149. /*$server->wsdl->addComplexType(
  150. 'LinesArray',
  151. 'complexType',
  152. 'array',
  153. '',
  154. 'SOAP-ENC:Array',
  155. array(),
  156. array(
  157. array(
  158. 'ref'=>'SOAP-ENC:arrayType',
  159. 'wsdl:arrayType'=>'tns:line[]'
  160. )
  161. ),
  162. 'tns:line'
  163. );*/
  164. $server->wsdl->addComplexType(
  165. 'LinesArray2',
  166. 'complexType',
  167. 'array',
  168. 'sequence',
  169. '',
  170. array(
  171. 'line' => array(
  172. 'name' => 'line',
  173. 'type' => 'tns:line',
  174. 'minOccurs' => '0',
  175. 'maxOccurs' => 'unbounded'
  176. )
  177. )
  178. );
  179. $order_fields = array(
  180. 'id' => array('name'=>'id', 'type'=>'xsd:string'),
  181. 'ref' => array('name'=>'ref', 'type'=>'xsd:string'),
  182. 'ref_client' => array('name'=>'ref_client', 'type'=>'xsd:string'),
  183. 'ref_ext' => array('name'=>'ref_ext', 'type'=>'xsd:string'),
  184. 'thirdparty_id' => array('name'=>'thirdparty_id', 'type'=>'xsd:int'),
  185. 'status' => array('name'=>'status', 'type'=>'xsd:int'),
  186. 'billed' => array('name'=>'billed', 'type'=>'xsd:string'),
  187. 'total_net' => array('name'=>'total_net', 'type'=>'xsd:double'),
  188. 'total_vat' => array('name'=>'total_vat', 'type'=>'xsd:double'),
  189. 'total_localtax1' => array('name'=>'total_localtax1', 'type'=>'xsd:double'),
  190. 'total_localtax2' => array('name'=>'total_localtax2', 'type'=>'xsd:double'),
  191. 'total' => array('name'=>'total', 'type'=>'xsd:double'),
  192. 'date' => array('name'=>'date', 'type'=>'xsd:date'),
  193. 'date_creation' => array('name'=>'date_creation', 'type'=>'xsd:dateTime'),
  194. 'date_validation' => array('name'=>'date_validation', 'type'=>'xsd:dateTime'),
  195. 'date_modification' => array('name'=>'date_modification', 'type'=>'xsd:dateTime'),
  196. 'source' => array('name'=>'source', 'type'=>'xsd:string'),
  197. 'note_private' => array('name'=>'note_private', 'type'=>'xsd:string'),
  198. 'note_public' => array('name'=>'note_public', 'type'=>'xsd:string'),
  199. 'project_id' => array('name'=>'project_id', 'type'=>'xsd:string'),
  200. 'mode_reglement_id' => array('name'=>'mode_reglement_id', 'type'=>'xsd:string'),
  201. 'mode_reglement_code' => array('name'=>'mode_reglement_code', 'type'=>'xsd:string'),
  202. 'mode_reglement' => array('name'=>'mode_reglement', 'type'=>'xsd:string'),
  203. 'cond_reglement_id' => array('name'=>'cond_reglement_id', 'type'=>'xsd:string'),
  204. 'cond_reglement_code' => array('name'=>'cond_reglement_code', 'type'=>'xsd:string'),
  205. 'cond_reglement' => array('name'=>'cond_reglement', 'type'=>'xsd:string'),
  206. 'cond_reglement_doc' => array('name'=>'cond_reglement_doc', 'type'=>'xsd:string'),
  207. 'date_livraison' => array('name'=>'date_livraison', 'type'=>'xsd:date'),
  208. 'demand_reason_id' => array('name'=>'demand_reason_id', 'type'=>'xsd:string'),
  209. 'lines' => array('name'=>'lines', 'type'=>'tns:LinesArray2')
  210. );
  211. $elementtype = 'commande';
  212. //Retrieve all extrafield for thirdsparty
  213. // fetch optionals attributes and labels
  214. $extrafields = new ExtraFields($db);
  215. $extrafields->fetch_name_optionals_label($elementtype, true);
  216. $extrafield_array = null;
  217. if (is_array($extrafields) && count($extrafields) > 0) {
  218. $extrafield_array = array();
  219. }
  220. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  221. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  222. //$value=$object->array_options["options_".$key];
  223. $type = $extrafields->attributes[$elementtype]['type'][$key];
  224. if ($type == 'date' || $type == 'datetime') {
  225. $type = 'xsd:dateTime';
  226. } else {
  227. $type = 'xsd:string';
  228. }
  229. $extrafield_array['options_'.$key] = array('name'=>'options_'.$key, 'type'=>$type);
  230. }
  231. }
  232. if (is_array($extrafield_array)) {
  233. $order_fields = array_merge($order_fields, $extrafield_array);
  234. }
  235. $server->wsdl->addComplexType(
  236. 'order',
  237. 'complexType',
  238. 'struct',
  239. 'all',
  240. '',
  241. $order_fields
  242. );
  243. /*
  244. $server->wsdl->addComplexType(
  245. 'OrdersArray',
  246. 'complexType',
  247. 'array',
  248. '',
  249. 'SOAP-ENC:Array',
  250. array(),
  251. array(
  252. array(
  253. 'ref'=>'SOAP-ENC:arrayType',
  254. 'wsdl:arrayType'=>'tns:order[]'
  255. )
  256. ),
  257. 'tns:order'
  258. );*/
  259. $server->wsdl->addComplexType(
  260. 'OrdersArray2',
  261. 'complexType',
  262. 'array',
  263. 'sequence',
  264. '',
  265. array(
  266. 'order' => array(
  267. 'name' => 'order',
  268. 'type' => 'tns:order',
  269. 'minOccurs' => '0',
  270. 'maxOccurs' => 'unbounded'
  271. )
  272. )
  273. );
  274. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  275. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  276. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  277. $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  278. $styleuse = 'encoded'; // encoded/literal/literal wrapped
  279. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  280. // Register WSDL
  281. $server->register(
  282. 'getOrder',
  283. array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'), // Entry values
  284. array('result'=>'tns:result', 'order'=>'tns:order'), // Exit values
  285. $ns,
  286. $ns.'#getOrder',
  287. $styledoc,
  288. $styleuse,
  289. 'WS to get a particular invoice'
  290. );
  291. $server->register(
  292. 'getOrdersForThirdParty',
  293. array('authentication'=>'tns:authentication', 'idthirdparty'=>'xsd:string'), // Entry values
  294. array('result'=>'tns:result', 'orders'=>'tns:OrdersArray2'), // Exit values
  295. $ns,
  296. $ns.'#getOrdersForThirdParty',
  297. $styledoc,
  298. $styleuse,
  299. 'WS to get all orders of a third party'
  300. );
  301. $server->register(
  302. 'createOrder',
  303. array('authentication'=>'tns:authentication', 'order'=>'tns:order'), // Entry values
  304. array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string'), // Exit values
  305. $ns,
  306. $ns.'#createOrder',
  307. $styledoc,
  308. $styleuse,
  309. 'WS to create an order'
  310. );
  311. $server->register(
  312. 'updateOrder',
  313. array('authentication'=>'tns:authentication', 'order'=>'tns:order'), // Entry values
  314. array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'), // Exit values
  315. $ns,
  316. $ns.'#updateOrder',
  317. $styledoc,
  318. $styleuse,
  319. 'WS to update an order'
  320. );
  321. $server->register(
  322. 'validOrder',
  323. array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'id_warehouse'=>'xsd:string'), // Entry values
  324. array('result'=>'tns:result'), // Exit values
  325. $ns,
  326. $ns.'#validOrder',
  327. $styledoc,
  328. $styleuse,
  329. 'WS to valid an order'
  330. );
  331. /**
  332. * Get order from id, ref or ref_ext.
  333. *
  334. * @param array $authentication Array of authentication information
  335. * @param int $id Id
  336. * @param string $ref Ref
  337. * @param string $ref_ext Ref_ext
  338. * @return array Array result
  339. */
  340. function getOrder($authentication, $id = 0, $ref = '', $ref_ext = '')
  341. {
  342. global $db, $conf;
  343. dol_syslog("Function: getOrder login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
  344. if ($authentication['entity']) {
  345. $conf->entity = $authentication['entity'];
  346. }
  347. // Init and check authentication
  348. $objectresp = array();
  349. $errorcode = '';
  350. $errorlabel = '';
  351. $error = 0;
  352. $socid = 0;
  353. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  354. if ($fuser->socid) {
  355. $socid = $fuser->socid;
  356. }
  357. // Check parameters
  358. if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
  359. $error++;
  360. $errorcode = 'BAD_PARAMETERS';
  361. $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
  362. }
  363. if (!$error) {
  364. $fuser->getrights();
  365. if ($fuser->hasRight('commande', 'lire')) {
  366. $order = new Commande($db);
  367. $result = $order->fetch($id, $ref, $ref_ext);
  368. if ($result > 0) {
  369. // Security for external user
  370. if ($socid && $socid != $order->socid) {
  371. $error++;
  372. $errorcode = 'PERMISSION_DENIED';
  373. $errorlabel = 'User does not have permission for this request';
  374. }
  375. if (!$error) {
  376. $linesresp = array();
  377. $i = 0;
  378. foreach ($order->lines as $line) {
  379. //var_dump($line); exit;
  380. $linesresp[] = array(
  381. 'id'=>$line->rowid,
  382. 'fk_commande'=>$line->fk_commande,
  383. 'fk_parent_line'=>$line->fk_parent_line,
  384. 'desc'=>$line->desc,
  385. 'qty'=>$line->qty,
  386. 'price'=>$line->price,
  387. 'unitprice'=>$line->subprice,
  388. 'vat_rate'=>$line->tva_tx,
  389. 'remise'=>$line->remise,
  390. 'remise_percent'=>$line->remise_percent,
  391. 'product_id'=>$line->fk_product,
  392. 'product_type'=>$line->product_type,
  393. 'total_net'=>$line->total_ht,
  394. 'total_vat'=>$line->total_tva,
  395. 'total'=>$line->total_ttc,
  396. 'date_start'=>$line->date_start,
  397. 'date_end'=>$line->date_end,
  398. 'product_ref'=>$line->product_ref,
  399. 'product_label'=>$line->product_label,
  400. 'product_desc'=>$line->product_desc
  401. );
  402. $i++;
  403. }
  404. // Create order
  405. $objectresp = array(
  406. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  407. 'order'=>array(
  408. 'id' => $order->id,
  409. 'ref' => $order->ref,
  410. 'ref_client' => $order->ref_client,
  411. 'ref_ext' => $order->ref_ext,
  412. 'thirdparty_id' => $order->socid,
  413. 'status' => $order->statut,
  414. 'total_net' => $order->total_ht,
  415. 'total_vat' => $order->total_tva,
  416. 'total_localtax1' => $order->total_localtax1,
  417. 'total_localtax2' => $order->total_localtax2,
  418. 'total' => $order->total_ttc,
  419. 'project_id' => $order->fk_project,
  420. 'date' => $order->date ? dol_print_date($order->date, 'dayrfc') : '',
  421. 'date_creation' => $order->date_creation ? dol_print_date($order->date_creation, 'dayhourrfc') : '',
  422. 'date_validation' => $order->date_validation ? dol_print_date($order->date_creation, 'dayhourrfc') : '',
  423. 'date_modification' => $order->date_modification ? dol_print_date($order->date_modification, 'dayhourrfc') : '',
  424. 'source' => $order->source,
  425. 'billed' => $order->billed,
  426. 'note_private' => $order->note_private,
  427. 'note_public' => $order->note_public,
  428. 'cond_reglement_id' => $order->cond_reglement_id,
  429. 'cond_reglement_code' => $order->cond_reglement_code,
  430. 'cond_reglement' => $order->cond_reglement,
  431. 'mode_reglement_id' => $order->mode_reglement_id,
  432. 'mode_reglement_code' => $order->mode_reglement_code,
  433. 'mode_reglement' => $order->mode_reglement,
  434. 'date_livraison' => $order->delivery_date,
  435. 'demand_reason_id' => $order->demand_reason_id,
  436. 'demand_reason_code' => $order->demand_reason_code,
  437. 'lines' => $linesresp
  438. ));
  439. }
  440. } else {
  441. $error++;
  442. $errorcode = 'NOT_FOUND';
  443. $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
  444. }
  445. } else {
  446. $error++;
  447. $errorcode = 'PERMISSION_DENIED';
  448. $errorlabel = 'User does not have permission for this request';
  449. }
  450. }
  451. if ($error) {
  452. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  453. }
  454. return $objectresp;
  455. }
  456. /**
  457. * Get list of orders for third party
  458. *
  459. * @param array $authentication Array of authentication information
  460. * @param int $idthirdparty Id of thirdparty
  461. * @return array Array result
  462. */
  463. function getOrdersForThirdParty($authentication, $idthirdparty)
  464. {
  465. global $db, $conf;
  466. dol_syslog("Function: getOrdersForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
  467. if ($authentication['entity']) {
  468. $conf->entity = $authentication['entity'];
  469. }
  470. // Init and check authentication
  471. $objectresp = array();
  472. $errorcode = '';
  473. $errorlabel = '';
  474. $error = 0;
  475. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  476. if ($fuser->socid) {
  477. $socid = $fuser->socid;
  478. }
  479. // Check parameters
  480. if (!$error && empty($idthirdparty)) {
  481. $error++;
  482. $errorcode = 'BAD_PARAMETERS';
  483. $errorlabel = 'Parameter id is not provided';
  484. }
  485. if (!$error) {
  486. $linesorders = array();
  487. $sql = 'SELECT c.rowid as orderid';
  488. $sql .= ' FROM '.MAIN_DB_PREFIX.'commande as c';
  489. $sql .= " WHERE c.entity = ".$conf->entity;
  490. if ($idthirdparty != 'all') {
  491. $sql .= " AND c.fk_soc = ".((int) $idthirdparty);
  492. }
  493. $resql = $db->query($sql);
  494. if ($resql) {
  495. $num = $db->num_rows($resql);
  496. $i = 0;
  497. while ($i < $num) {
  498. // En attendant remplissage par boucle
  499. $obj = $db->fetch_object($resql);
  500. $order = new Commande($db);
  501. $order->fetch($obj->orderid);
  502. // Sécurité pour utilisateur externe
  503. if ($socid && ($socid != $order->socid)) {
  504. $error++;
  505. $errorcode = 'PERMISSION_DENIED';
  506. $errorlabel = $order->socid.' User does not have permission for this request';
  507. }
  508. if (!$error) {
  509. // Define lines of invoice
  510. $linesresp = array();
  511. foreach ($order->lines as $line) {
  512. $linesresp[] = array(
  513. 'id'=>$line->rowid,
  514. 'type'=>$line->product_type,
  515. 'fk_commande'=>$line->fk_commande,
  516. 'fk_parent_line'=>$line->fk_parent_line,
  517. 'desc'=>$line->desc,
  518. 'qty'=>$line->qty,
  519. 'price'=>$line->price,
  520. 'unitprice'=>$line->subprice,
  521. 'tva_tx'=>$line->tva_tx,
  522. 'remise'=>$line->remise,
  523. 'remise_percent'=>$line->remise_percent,
  524. 'total_net'=>$line->total_ht,
  525. 'total_vat'=>$line->total_tva,
  526. 'total'=>$line->total_ttc,
  527. 'date_start'=>$line->date_start,
  528. 'date_end'=>$line->date_end,
  529. 'product_id'=>$line->fk_product,
  530. 'product_ref'=>$line->product_ref,
  531. 'product_label'=>$line->product_label,
  532. 'product_desc'=>$line->product_desc
  533. );
  534. }
  535. // Now define invoice
  536. $linesorders[] = array(
  537. 'id' => $order->id,
  538. 'ref' => $order->ref,
  539. 'ref_client' => $order->ref_client,
  540. 'ref_ext' => $order->ref_ext,
  541. 'socid' => $order->socid,
  542. 'status' => $order->statut,
  543. 'total_net' => $order->total_ht,
  544. 'total_vat' => $order->total_tva,
  545. 'total_localtax1' => $order->total_localtax1,
  546. 'total_localtax2' => $order->total_localtax2,
  547. 'total' => $order->total_ttc,
  548. 'project_id' => $order->fk_project,
  549. 'date' => $order->date_commande ? dol_print_date($order->date_commande, 'dayrfc') : '',
  550. 'source' => $order->source,
  551. 'billed' => $order->billed,
  552. 'note_private' => $order->note_private,
  553. 'note_public' => $order->note_public,
  554. 'cond_reglement_id' => $order->cond_reglement_id,
  555. 'cond_reglement' => $order->cond_reglement,
  556. 'cond_reglement_doc' => $order->cond_reglement_doc,
  557. 'cond_reglement_code' => $order->cond_reglement_code,
  558. 'mode_reglement_id' => $order->mode_reglement_id,
  559. 'mode_reglement' => $order->mode_reglement,
  560. 'mode_reglement_code' => $order->mode_reglement_code,
  561. 'date_livraison' => $order->delivery_date,
  562. 'demand_reason_id' => $order->demand_reason_id,
  563. 'demand_reason_code' => $order->demand_reason_code,
  564. 'lines' => $linesresp
  565. );
  566. }
  567. $i++;
  568. }
  569. $objectresp = array(
  570. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  571. 'orders'=>$linesorders
  572. );
  573. } else {
  574. $error++;
  575. $errorcode = $db->lasterrno();
  576. $errorlabel = $db->lasterror();
  577. }
  578. }
  579. if ($error) {
  580. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  581. }
  582. return $objectresp;
  583. }
  584. /**
  585. * Create order
  586. *
  587. * @param array $authentication Array of authentication information
  588. * @param array $order Order info
  589. * @return array array of new order
  590. */
  591. function createOrder($authentication, $order)
  592. {
  593. global $db, $conf, $langs;
  594. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  595. $now = dol_now();
  596. dol_syslog("Function: createOrder login=".$authentication['login']." socid :".$order['socid']);
  597. if ($authentication['entity']) {
  598. $conf->entity = $authentication['entity'];
  599. }
  600. // Init and check authentication
  601. $objectresp = array();
  602. $errorcode = '';
  603. $errorlabel = '';
  604. $error = 0;
  605. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  606. // Check parameters
  607. if (!$error) {
  608. $newobject = new Commande($db);
  609. $newobject->socid = $order['thirdparty_id'];
  610. $newobject->type = $order['type'];
  611. $newobject->ref_ext = $order['ref_ext'];
  612. $newobject->date = dol_stringtotime($order['date'], 'dayrfc');
  613. $newobject->date_lim_reglement = dol_stringtotime($order['date_due'], 'dayrfc');
  614. $newobject->note_private = $order['note_private'];
  615. $newobject->note_public = $order['note_public'];
  616. $newobject->statut = Commande::STATUS_DRAFT; // We start with status draft
  617. $newobject->billed = $order['billed'];
  618. $newobject->fk_project = $order['project_id'];
  619. $newobject->cond_reglement_id = $order['cond_reglement_id'];
  620. $newobject->demand_reason_id = $order['demand_reason_id'];
  621. $newobject->date_creation = $now;
  622. $elementtype = 'commande';
  623. // Retrieve all extrafield for order
  624. // fetch optionals attributes and labels
  625. $extrafields = new ExtraFields($db);
  626. $extrafields->fetch_name_optionals_label($elementtype, true);
  627. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  628. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  629. $key = 'options_'.$key;
  630. $newobject->array_options[$key] = $order[$key];
  631. }
  632. }
  633. // Trick because nusoap does not store data with same structure if there is one or several lines
  634. $arrayoflines = array();
  635. if (isset($order['lines']['line'][0])) {
  636. $arrayoflines = $order['lines']['line'];
  637. } else {
  638. $arrayoflines = $order['lines'];
  639. }
  640. foreach ($arrayoflines as $key => $line) {
  641. // $key can be 'line' or '0','1',...
  642. $newline = new OrderLine($db);
  643. $newline->type = $line['type'];
  644. $newline->desc = $line['desc'];
  645. $newline->fk_product = $line['product_id'];
  646. $newline->tva_tx = $line['vat_rate'];
  647. $newline->qty = $line['qty'];
  648. $newline->price = $line['price'];
  649. $newline->subprice = $line['unitprice'];
  650. $newline->total_ht = $line['total_net'];
  651. $newline->total_tva = $line['total_vat'];
  652. $newline->total_ttc = $line['total'];
  653. $newline->date_start = $line['date_start'];
  654. $newline->date_end = $line['date_end'];
  655. $elementtype = 'commandedet';
  656. // Retrieve all extrafield for lines
  657. // fetch optionals attributes and labels
  658. $extrafields = new ExtraFields($db);
  659. $extrafields->fetch_name_optionals_label($elementtype, true);
  660. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  661. foreach ($extrafields->attributes[$elementtype]['label'] as $tmpkey => $tmplabel) {
  662. $tmpkey = 'options_'.$tmpkey;
  663. $newline->array_options[$tmpkey] = $line[$tmpkey];
  664. }
  665. }
  666. $newobject->lines[] = $newline;
  667. }
  668. $db->begin();
  669. dol_syslog("Webservice server_order:: order creation start", LOG_DEBUG);
  670. $result = $newobject->create($fuser);
  671. dol_syslog('Webservice server_order:: order creation done with $result='.$result, LOG_DEBUG);
  672. if ($result < 0) {
  673. dol_syslog("Webservice server_order:: order creation failed", LOG_ERR);
  674. $error++;
  675. }
  676. if ($order['status'] == 1) { // We want order to have status validated
  677. dol_syslog("Webservice server_order:: order validation start", LOG_DEBUG);
  678. $result = $newobject->valid($fuser);
  679. if ($result < 0) {
  680. dol_syslog("Webservice server_order:: order validation failed", LOG_ERR);
  681. $error++;
  682. }
  683. }
  684. if ($result >= 0) {
  685. dol_syslog("Webservice server_order:: order creation & validation succeeded, commit", LOG_DEBUG);
  686. $db->commit();
  687. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
  688. } else {
  689. dol_syslog("Webservice server_order:: order creation or validation failed, rollback", LOG_ERR);
  690. $db->rollback();
  691. $error++;
  692. $errorcode = 'KO';
  693. $errorlabel = $newobject->error;
  694. }
  695. }
  696. if ($error) {
  697. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  698. }
  699. return $objectresp;
  700. }
  701. /**
  702. * Valid an order
  703. *
  704. * @param array $authentication Array of authentication information
  705. * @param int $id Id of order to validate
  706. * @param int $id_warehouse Id of warehouse to use for stock decrease
  707. * @return array Array result
  708. */
  709. function validOrder($authentication, $id = 0, $id_warehouse = 0)
  710. {
  711. global $db, $conf, $langs;
  712. dol_syslog("Function: validOrder login=".$authentication['login']." id=".$id." id_warehouse=".$id_warehouse);
  713. // Init and check authentication
  714. $objectresp = array();
  715. $errorcode = '';
  716. $errorlabel = '';
  717. $error = 0;
  718. if ($authentication['entity']) {
  719. $conf->entity = $authentication['entity'];
  720. }
  721. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  722. if (!$error) {
  723. $fuser->getrights();
  724. if ($fuser->hasRight('commande', 'lire')) {
  725. $order = new Commande($db);
  726. $result = $order->fetch($id);
  727. $order->fetch_thirdparty();
  728. $db->begin();
  729. if ($result > 0) {
  730. $result = $order->valid($fuser, $id_warehouse);
  731. if ($result >= 0) {
  732. // Define output language
  733. $outputlangs = $langs;
  734. $order->generateDocument($order->model_pdf, $outputlangs);
  735. } else {
  736. $db->rollback();
  737. $error++;
  738. $errorcode = 'KO';
  739. $errorlabel = $order->error;
  740. }
  741. } else {
  742. $db->rollback();
  743. $error++;
  744. $errorcode = 'KO';
  745. $errorlabel = $order->error;
  746. }
  747. } else {
  748. $db->rollback();
  749. $error++;
  750. $errorcode = 'KO';
  751. $errorlabel = 'Bad permission';
  752. }
  753. }
  754. if ($error) {
  755. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  756. } else {
  757. $db->commit();
  758. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''));
  759. }
  760. return $objectresp;
  761. }
  762. /**
  763. * Update an order
  764. *
  765. * @param array $authentication Array of authentication information
  766. * @param array $order Order info
  767. * @return array Array result
  768. */
  769. function updateOrder($authentication, $order)
  770. {
  771. global $db, $conf, $langs;
  772. dol_syslog("Function: updateOrder login=".$authentication['login']);
  773. if ($authentication['entity']) {
  774. $conf->entity = $authentication['entity'];
  775. }
  776. // Init and check authentication
  777. $objectresp = array();
  778. $errorcode = '';
  779. $errorlabel = '';
  780. $error = 0;
  781. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  782. // Check parameters
  783. if (empty($order['id']) && empty($order['ref']) && empty($order['ref_ext'])) {
  784. $error++;
  785. $errorcode = 'KO';
  786. $errorlabel = "Order id or ref or ref_ext is mandatory.";
  787. }
  788. if (!$error) {
  789. $objectfound = false;
  790. include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  791. $object = new Commande($db);
  792. $result = $object->fetch($order['id'], (empty($order['id']) ? $order['ref'] : ''), (empty($order['id']) && empty($order['ref']) ? $order['ref_ext'] : ''));
  793. if (!empty($object->id)) {
  794. $objectfound = true;
  795. $db->begin();
  796. if (isset($order['status'])) {
  797. if ($order['status'] == -1) {
  798. $result = $object->cancel($fuser);
  799. }
  800. if ($order['status'] == 1) {
  801. $result = $object->valid($fuser);
  802. if ($result >= 0) {
  803. // Define output language
  804. $outputlangs = $langs;
  805. $object->generateDocument($order->model_pdf, $outputlangs);
  806. }
  807. }
  808. if ($order['status'] == 0) {
  809. $result = $object->set_reopen($fuser);
  810. }
  811. if ($order['status'] == 3) {
  812. $result = $object->cloture($fuser);
  813. }
  814. }
  815. if (isset($order['billed'])) {
  816. if ($order['billed']) {
  817. $result = $object->classifyBilled($fuser);
  818. }
  819. if (!$order['billed']) {
  820. $result = $object->classifyUnBilled($fuser);
  821. }
  822. }
  823. $elementtype = 'commande';
  824. //Retrieve all extrafield for object
  825. // fetch optionals attributes and labels
  826. $extrafields = new ExtraFields($db);
  827. $extrafields->fetch_name_optionals_label($elementtype, true);
  828. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  829. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  830. $key = 'options_'.$key;
  831. if (isset($order[$key])) {
  832. $result = $object->setValueFrom($key, $order[$key], 'commande_extrafields');
  833. }
  834. }
  835. }
  836. if ($result <= 0) {
  837. $error++;
  838. }
  839. }
  840. if ((!$error) && ($objectfound)) {
  841. $db->commit();
  842. $objectresp = array(
  843. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  844. 'id'=>$object->id,
  845. 'ref'=>$object->ref,
  846. 'ref_ext'=>$object->ref_ext
  847. );
  848. } elseif ($objectfound) {
  849. $db->rollback();
  850. $error++;
  851. $errorcode = 'KO';
  852. $errorlabel = $object->error;
  853. } else {
  854. $error++;
  855. $errorcode = 'NOT_FOUND';
  856. $errorlabel = 'Order id='.$order['id'].' ref='.$order['ref'].' ref_ext='.$order['ref_ext'].' cannot be found';
  857. }
  858. }
  859. if ($error) {
  860. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  861. }
  862. return $objectresp;
  863. }
  864. // Return the results.
  865. $server->service(file_get_contents("php://input"));