server_order.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 (empty($conf->global->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 = '', $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 = ''; $errorlabel = '';
  350. $error = 0;
  351. $socid = 0;
  352. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  353. if ($fuser->socid) {
  354. $socid = $fuser->socid;
  355. }
  356. // Check parameters
  357. if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
  358. $error++;
  359. $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
  360. }
  361. if (!$error) {
  362. $fuser->getrights();
  363. if ($fuser->hasRight('commande', 'lire')) {
  364. $order = new Commande($db);
  365. $result = $order->fetch($id, $ref, $ref_ext);
  366. if ($result > 0) {
  367. // Security for external user
  368. if ($socid && $socid != $order->socid) {
  369. $error++;
  370. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  371. }
  372. if (!$error) {
  373. $linesresp = array();
  374. $i = 0;
  375. foreach ($order->lines as $line) {
  376. //var_dump($line); exit;
  377. $linesresp[] = array(
  378. 'id'=>$line->rowid,
  379. 'fk_commande'=>$line->fk_commande,
  380. 'fk_parent_line'=>$line->fk_parent_line,
  381. 'desc'=>$line->desc,
  382. 'qty'=>$line->qty,
  383. 'price'=>$line->price,
  384. 'unitprice'=>$line->subprice,
  385. 'vat_rate'=>$line->tva_tx,
  386. 'remise'=>$line->remise,
  387. 'remise_percent'=>$line->remise_percent,
  388. 'product_id'=>$line->fk_product,
  389. 'product_type'=>$line->product_type,
  390. 'total_net'=>$line->total_ht,
  391. 'total_vat'=>$line->total_tva,
  392. 'total'=>$line->total_ttc,
  393. 'date_start'=>$line->date_start,
  394. 'date_end'=>$line->date_end,
  395. 'product_ref'=>$line->product_ref,
  396. 'product_label'=>$line->product_label,
  397. 'product_desc'=>$line->product_desc
  398. );
  399. $i++;
  400. }
  401. // Create order
  402. $objectresp = array(
  403. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  404. 'order'=>array(
  405. 'id' => $order->id,
  406. 'ref' => $order->ref,
  407. 'ref_client' => $order->ref_client,
  408. 'ref_ext' => $order->ref_ext,
  409. 'thirdparty_id' => $order->socid,
  410. 'status' => $order->statut,
  411. 'total_net' => $order->total_ht,
  412. 'total_vat' => $order->total_tva,
  413. 'total_localtax1' => $order->total_localtax1,
  414. 'total_localtax2' => $order->total_localtax2,
  415. 'total' => $order->total_ttc,
  416. 'project_id' => $order->fk_project,
  417. 'date' => $order->date ?dol_print_date($order->date, 'dayrfc') : '',
  418. 'date_creation' => $order->date_creation ?dol_print_date($order->date_creation, 'dayhourrfc') : '',
  419. 'date_validation' => $order->date_validation ?dol_print_date($order->date_creation, 'dayhourrfc') : '',
  420. 'date_modification' => $order->date_modification ?dol_print_date($order->date_modification, 'dayhourrfc') : '',
  421. 'source' => $order->source,
  422. 'billed' => $order->billed,
  423. 'note_private' => $order->note_private,
  424. 'note_public' => $order->note_public,
  425. 'cond_reglement_id' => $order->cond_reglement_id,
  426. 'cond_reglement_code' => $order->cond_reglement_code,
  427. 'cond_reglement' => $order->cond_reglement,
  428. 'mode_reglement_id' => $order->mode_reglement_id,
  429. 'mode_reglement_code' => $order->mode_reglement_code,
  430. 'mode_reglement' => $order->mode_reglement,
  431. 'date_livraison' => $order->delivery_date,
  432. 'demand_reason_id' => $order->demand_reason_id,
  433. 'demand_reason_code' => $order->demand_reason_code,
  434. 'lines' => $linesresp
  435. ));
  436. }
  437. } else {
  438. $error++;
  439. $errorcode = 'NOT_FOUND';
  440. $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
  441. }
  442. } else {
  443. $error++;
  444. $errorcode = 'PERMISSION_DENIED';
  445. $errorlabel = 'User does not have permission for this request';
  446. }
  447. }
  448. if ($error) {
  449. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  450. }
  451. return $objectresp;
  452. }
  453. /**
  454. * Get list of orders for third party
  455. *
  456. * @param array $authentication Array of authentication information
  457. * @param int $idthirdparty Id of thirdparty
  458. * @return array Array result
  459. */
  460. function getOrdersForThirdParty($authentication, $idthirdparty)
  461. {
  462. global $db, $conf;
  463. dol_syslog("Function: getOrdersForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
  464. if ($authentication['entity']) {
  465. $conf->entity = $authentication['entity'];
  466. }
  467. // Init and check authentication
  468. $objectresp = array();
  469. $errorcode = ''; $errorlabel = '';
  470. $error = 0;
  471. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  472. if ($fuser->socid) {
  473. $socid = $fuser->socid;
  474. }
  475. // Check parameters
  476. if (!$error && empty($idthirdparty)) {
  477. $error++;
  478. $errorcode = 'BAD_PARAMETERS'; $errorlabel = 'Parameter id is not provided';
  479. }
  480. if (!$error) {
  481. $linesorders = array();
  482. $sql = 'SELECT c.rowid as orderid';
  483. $sql .= ' FROM '.MAIN_DB_PREFIX.'commande as c';
  484. $sql .= " WHERE c.entity = ".$conf->entity;
  485. if ($idthirdparty != 'all') {
  486. $sql .= " AND c.fk_soc = ".((int) $idthirdparty);
  487. }
  488. $resql = $db->query($sql);
  489. if ($resql) {
  490. $num = $db->num_rows($resql);
  491. $i = 0;
  492. while ($i < $num) {
  493. // En attendant remplissage par boucle
  494. $obj = $db->fetch_object($resql);
  495. $order = new Commande($db);
  496. $order->fetch($obj->orderid);
  497. // Sécurité pour utilisateur externe
  498. if ($socid && ($socid != $order->socid)) {
  499. $error++;
  500. $errorcode = 'PERMISSION_DENIED';
  501. $errorlabel = $order->socid.' User does not have permission for this request';
  502. }
  503. if (!$error) {
  504. // Define lines of invoice
  505. $linesresp = array();
  506. foreach ($order->lines as $line) {
  507. $linesresp[] = array(
  508. 'id'=>$line->rowid,
  509. 'type'=>$line->product_type,
  510. 'fk_commande'=>$line->fk_commande,
  511. 'fk_parent_line'=>$line->fk_parent_line,
  512. 'desc'=>$line->desc,
  513. 'qty'=>$line->qty,
  514. 'price'=>$line->price,
  515. 'unitprice'=>$line->subprice,
  516. 'tva_tx'=>$line->tva_tx,
  517. 'remise'=>$line->remise,
  518. 'remise_percent'=>$line->remise_percent,
  519. 'total_net'=>$line->total_ht,
  520. 'total_vat'=>$line->total_tva,
  521. 'total'=>$line->total_ttc,
  522. 'date_start'=>$line->date_start,
  523. 'date_end'=>$line->date_end,
  524. 'product_id'=>$line->fk_product,
  525. 'product_ref'=>$line->product_ref,
  526. 'product_label'=>$line->product_label,
  527. 'product_desc'=>$line->product_desc
  528. );
  529. }
  530. // Now define invoice
  531. $linesorders[] = array(
  532. 'id' => $order->id,
  533. 'ref' => $order->ref,
  534. 'ref_client' => $order->ref_client,
  535. 'ref_ext' => $order->ref_ext,
  536. 'socid' => $order->socid,
  537. 'status' => $order->statut,
  538. 'total_net' => $order->total_ht,
  539. 'total_vat' => $order->total_tva,
  540. 'total_localtax1' => $order->total_localtax1,
  541. 'total_localtax2' => $order->total_localtax2,
  542. 'total' => $order->total_ttc,
  543. 'project_id' => $order->fk_project,
  544. 'date' => $order->date_commande ?dol_print_date($order->date_commande, 'dayrfc') : '',
  545. 'source' => $order->source,
  546. 'billed' => $order->billed,
  547. 'note_private' => $order->note_private,
  548. 'note_public' => $order->note_public,
  549. 'cond_reglement_id' => $order->cond_reglement_id,
  550. 'cond_reglement' => $order->cond_reglement,
  551. 'cond_reglement_doc' => $order->cond_reglement_doc,
  552. 'cond_reglement_code' => $order->cond_reglement_code,
  553. 'mode_reglement_id' => $order->mode_reglement_id,
  554. 'mode_reglement' => $order->mode_reglement,
  555. 'mode_reglement_code' => $order->mode_reglement_code,
  556. 'date_livraison' => $order->delivery_date,
  557. 'demand_reason_id' => $order->demand_reason_id,
  558. 'demand_reason_code' => $order->demand_reason_code,
  559. 'lines' => $linesresp
  560. );
  561. }
  562. $i++;
  563. }
  564. $objectresp = array(
  565. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  566. 'orders'=>$linesorders
  567. );
  568. } else {
  569. $error++;
  570. $errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
  571. }
  572. }
  573. if ($error) {
  574. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  575. }
  576. return $objectresp;
  577. }
  578. /**
  579. * Create order
  580. *
  581. * @param array $authentication Array of authentication information
  582. * @param array $order Order info
  583. * @return array array of new order
  584. */
  585. function createOrder($authentication, $order)
  586. {
  587. global $db, $conf, $langs;
  588. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  589. $now = dol_now();
  590. dol_syslog("Function: createOrder login=".$authentication['login']." socid :".$order['socid']);
  591. if ($authentication['entity']) {
  592. $conf->entity = $authentication['entity'];
  593. }
  594. // Init and check authentication
  595. $objectresp = array();
  596. $errorcode = '';
  597. $errorlabel = '';
  598. $error = 0;
  599. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  600. // Check parameters
  601. if (!$error) {
  602. $newobject = new Commande($db);
  603. $newobject->socid = $order['thirdparty_id'];
  604. $newobject->type = $order['type'];
  605. $newobject->ref_ext = $order['ref_ext'];
  606. $newobject->date = dol_stringtotime($order['date'], 'dayrfc');
  607. $newobject->date_lim_reglement = dol_stringtotime($order['date_due'], 'dayrfc');
  608. $newobject->note_private = $order['note_private'];
  609. $newobject->note_public = $order['note_public'];
  610. $newobject->statut = Commande::STATUS_DRAFT; // We start with status draft
  611. $newobject->billed = $order['billed'];
  612. $newobject->fk_project = $order['project_id'];
  613. $newobject->cond_reglement_id = $order['cond_reglement_id'];
  614. $newobject->demand_reason_id = $order['demand_reason_id'];
  615. $newobject->date_creation = $now;
  616. $elementtype = 'commande';
  617. // Retrieve all extrafield for order
  618. // fetch optionals attributes and labels
  619. $extrafields = new ExtraFields($db);
  620. $extrafields->fetch_name_optionals_label($elementtype, true);
  621. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  622. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  623. $key = 'options_'.$key;
  624. $newobject->array_options[$key] = $order[$key];
  625. }
  626. }
  627. // Trick because nusoap does not store data with same structure if there is one or several lines
  628. $arrayoflines = array();
  629. if (isset($order['lines']['line'][0])) {
  630. $arrayoflines = $order['lines']['line'];
  631. } else {
  632. $arrayoflines = $order['lines'];
  633. }
  634. foreach ($arrayoflines as $key => $line) {
  635. // $key can be 'line' or '0','1',...
  636. $newline = new OrderLine($db);
  637. $newline->type = $line['type'];
  638. $newline->desc = $line['desc'];
  639. $newline->fk_product = $line['product_id'];
  640. $newline->tva_tx = $line['vat_rate'];
  641. $newline->qty = $line['qty'];
  642. $newline->price = $line['price'];
  643. $newline->subprice = $line['unitprice'];
  644. $newline->total_ht = $line['total_net'];
  645. $newline->total_tva = $line['total_vat'];
  646. $newline->total_ttc = $line['total'];
  647. $newline->date_start = $line['date_start'];
  648. $newline->date_end = $line['date_end'];
  649. $elementtype = 'commandedet';
  650. // Retrieve all extrafield for lines
  651. // fetch optionals attributes and labels
  652. $extrafields = new ExtraFields($db);
  653. $extrafields->fetch_name_optionals_label($elementtype, true);
  654. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  655. foreach ($extrafields->attributes[$elementtype]['label'] as $tmpkey => $tmplabel) {
  656. $tmpkey = 'options_'.$tmpkey;
  657. $newline->array_options[$tmpkey] = $line[$tmpkey];
  658. }
  659. }
  660. $newobject->lines[] = $newline;
  661. }
  662. $db->begin();
  663. dol_syslog("Webservice server_order:: order creation start", LOG_DEBUG);
  664. $result = $newobject->create($fuser);
  665. dol_syslog('Webservice server_order:: order creation done with $result='.$result, LOG_DEBUG);
  666. if ($result < 0) {
  667. dol_syslog("Webservice server_order:: order creation failed", LOG_ERR);
  668. $error++;
  669. }
  670. if ($order['status'] == 1) { // We want order to have status validated
  671. dol_syslog("Webservice server_order:: order validation start", LOG_DEBUG);
  672. $result = $newobject->valid($fuser);
  673. if ($result < 0) {
  674. dol_syslog("Webservice server_order:: order validation failed", LOG_ERR);
  675. $error++;
  676. }
  677. }
  678. if ($result >= 0) {
  679. dol_syslog("Webservice server_order:: order creation & validation succeeded, commit", LOG_DEBUG);
  680. $db->commit();
  681. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
  682. } else {
  683. dol_syslog("Webservice server_order:: order creation or validation failed, rollback", LOG_ERR);
  684. $db->rollback();
  685. $error++;
  686. $errorcode = 'KO';
  687. $errorlabel = $newobject->error;
  688. }
  689. }
  690. if ($error) {
  691. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  692. }
  693. return $objectresp;
  694. }
  695. /**
  696. * Valid an order
  697. *
  698. * @param array $authentication Array of authentication information
  699. * @param int $id Id of order to validate
  700. * @param int $id_warehouse Id of warehouse to use for stock decrease
  701. * @return array Array result
  702. */
  703. function validOrder($authentication, $id = '', $id_warehouse = 0)
  704. {
  705. global $db, $conf, $langs;
  706. dol_syslog("Function: validOrder login=".$authentication['login']." id=".$id." id_warehouse=".$id_warehouse);
  707. // Init and check authentication
  708. $objectresp = array();
  709. $errorcode = '';
  710. $errorlabel = '';
  711. $error = 0;
  712. if ($authentication['entity']) {
  713. $conf->entity = $authentication['entity'];
  714. }
  715. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  716. if (!$error) {
  717. $fuser->getrights();
  718. if ($fuser->hasRight('commande', 'lire')) {
  719. $order = new Commande($db);
  720. $result = $order->fetch($id);
  721. $order->fetch_thirdparty();
  722. $db->begin();
  723. if ($result > 0) {
  724. $result = $order->valid($fuser, $id_warehouse);
  725. if ($result >= 0) {
  726. // Define output language
  727. $outputlangs = $langs;
  728. $order->generateDocument($order->model_pdf, $outputlangs);
  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. } else {
  742. $db->rollback();
  743. $error++;
  744. $errorcode = 'KO';
  745. $errorlabel = 'Bad permission';
  746. }
  747. }
  748. if ($error) {
  749. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  750. } else {
  751. $db->commit();
  752. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''));
  753. }
  754. return $objectresp;
  755. }
  756. /**
  757. * Update an order
  758. *
  759. * @param array $authentication Array of authentication information
  760. * @param array $order Order info
  761. * @return array Array result
  762. */
  763. function updateOrder($authentication, $order)
  764. {
  765. global $db, $conf, $langs;
  766. dol_syslog("Function: updateOrder login=".$authentication['login']);
  767. if ($authentication['entity']) {
  768. $conf->entity = $authentication['entity'];
  769. }
  770. // Init and check authentication
  771. $objectresp = array();
  772. $errorcode = ''; $errorlabel = '';
  773. $error = 0;
  774. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  775. // Check parameters
  776. if (empty($order['id']) && empty($order['ref']) && empty($order['ref_ext'])) {
  777. $error++; $errorcode = 'KO'; $errorlabel = "Order id or ref or ref_ext is mandatory.";
  778. }
  779. if (!$error) {
  780. $objectfound = false;
  781. include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  782. $object = new Commande($db);
  783. $result = $object->fetch($order['id'], (empty($order['id']) ? $order['ref'] : ''), (empty($order['id']) && empty($order['ref']) ? $order['ref_ext'] : ''));
  784. if (!empty($object->id)) {
  785. $objectfound = true;
  786. $db->begin();
  787. if (isset($order['status'])) {
  788. if ($order['status'] == -1) {
  789. $result = $object->cancel($fuser);
  790. }
  791. if ($order['status'] == 1) {
  792. $result = $object->valid($fuser);
  793. if ($result >= 0) {
  794. // Define output language
  795. $outputlangs = $langs;
  796. $object->generateDocument($order->model_pdf, $outputlangs);
  797. }
  798. }
  799. if ($order['status'] == 0) {
  800. $result = $object->set_reopen($fuser);
  801. }
  802. if ($order['status'] == 3) {
  803. $result = $object->cloture($fuser);
  804. }
  805. }
  806. if (isset($order['billed'])) {
  807. if ($order['billed']) {
  808. $result = $object->classifyBilled($fuser);
  809. }
  810. if (!$order['billed']) {
  811. $result = $object->classifyUnBilled($fuser);
  812. }
  813. }
  814. $elementtype = 'commande';
  815. //Retrieve all extrafield for object
  816. // fetch optionals attributes and labels
  817. $extrafields = new ExtraFields($db);
  818. $extrafields->fetch_name_optionals_label($elementtype, true);
  819. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  820. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  821. $key = 'options_'.$key;
  822. if (isset($order[$key])) {
  823. $result = $object->setValueFrom($key, $order[$key], 'commande_extrafields');
  824. }
  825. }
  826. }
  827. if ($result <= 0) {
  828. $error++;
  829. }
  830. }
  831. if ((!$error) && ($objectfound)) {
  832. $db->commit();
  833. $objectresp = array(
  834. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  835. 'id'=>$object->id,
  836. 'ref'=>$object->ref,
  837. 'ref_ext'=>$object->ref_ext
  838. );
  839. } elseif ($objectfound) {
  840. $db->rollback();
  841. $error++;
  842. $errorcode = 'KO';
  843. $errorlabel = $object->error;
  844. } else {
  845. $error++;
  846. $errorcode = 'NOT_FOUND';
  847. $errorlabel = 'Order id='.$order['id'].' ref='.$order['ref'].' ref_ext='.$order['ref_ext'].' cannot be found';
  848. }
  849. }
  850. if ($error) {
  851. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  852. }
  853. return $objectresp;
  854. }
  855. // Return the results.
  856. $server->service(file_get_contents("php://input"));