server_order.php 29 KB

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