server_order.php 30 KB

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