server_order.php 30 KB

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