server_invoice.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. <?php
  2. /* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/webservices/server_invoice.php
  20. * \brief File that is entry point to call Dolibarr WebServices
  21. */
  22. if (!defined("NOCSRFCHECK")) {
  23. define("NOCSRFCHECK", '1');
  24. }
  25. require '../master.inc.php';
  26. require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  32. dol_syslog("Call Dolibarr webservices interfaces");
  33. $langs->load("main");
  34. // Enable and test if module web services is enabled
  35. if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
  36. $langs->load("admin");
  37. dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
  38. print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
  39. print $langs->trans("ToActivateModule");
  40. exit;
  41. }
  42. // Create the soap Object
  43. $server = new nusoap_server();
  44. $server->soap_defencoding = 'UTF-8';
  45. $server->decode_utf8 = false;
  46. $ns = 'http://www.dolibarr.org/ns/';
  47. $server->configureWSDL('WebServicesDolibarrInvoice', $ns);
  48. $server->wsdl->schemaTargetNamespace = $ns;
  49. // Define WSDL Authentication object
  50. $server->wsdl->addComplexType(
  51. 'authentication',
  52. 'complexType',
  53. 'struct',
  54. 'all',
  55. '',
  56. array(
  57. 'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
  58. 'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
  59. 'login' => array('name'=>'login', 'type'=>'xsd:string'),
  60. 'password' => array('name'=>'password', 'type'=>'xsd:string'),
  61. 'entity' => array('name'=>'entity', 'type'=>'xsd:string')
  62. )
  63. );
  64. // Define WSDL Return object
  65. $server->wsdl->addComplexType(
  66. 'result',
  67. 'complexType',
  68. 'struct',
  69. 'all',
  70. '',
  71. array(
  72. 'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
  73. 'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
  74. )
  75. );
  76. // Define other specific objects
  77. $server->wsdl->addComplexType(
  78. 'line',
  79. 'complexType',
  80. 'struct',
  81. 'all',
  82. '',
  83. array(
  84. 'id' => array('name'=>'id', 'type'=>'xsd:string'),
  85. 'type' => array('name'=>'type', 'type'=>'xsd:int'),
  86. 'desc' => array('name'=>'desc', 'type'=>'xsd:string'),
  87. 'vat_rate' => array('name'=>'vat_rate', 'type'=>'xsd:double'),
  88. 'qty' => array('name'=>'qty', 'type'=>'xsd:double'),
  89. 'unitprice' => array('name'=>'unitprice', 'type'=>'xsd:double'),
  90. 'total_net' => array('name'=>'total_net', 'type'=>'xsd:double'),
  91. 'total_vat' => array('name'=>'total_vat', 'type'=>'xsd:double'),
  92. 'total' => array('name'=>'total', 'type'=>'xsd:double'),
  93. 'date_start' => array('name'=>'date_start', 'type'=>'xsd:date'),
  94. 'date_end' => array('name'=>'date_end', 'type'=>'xsd:date'),
  95. // From product
  96. 'product_id' => array('name'=>'product_id', 'type'=>'xsd:int'),
  97. 'product_ref' => array('name'=>'product_ref', 'type'=>'xsd:string'),
  98. 'product_label' => array('name'=>'product_label', 'type'=>'xsd:string'),
  99. 'product_desc' => array('name'=>'product_desc', 'type'=>'xsd:string')
  100. )
  101. );
  102. /*$server->wsdl->addComplexType(
  103. 'LinesArray',
  104. 'complexType',
  105. 'array',
  106. '',
  107. 'SOAP-ENC:Array',
  108. array(),
  109. array(
  110. array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:line[]')
  111. ),
  112. 'tns:line'
  113. );*/
  114. $server->wsdl->addComplexType(
  115. 'LinesArray2',
  116. 'complexType',
  117. 'array',
  118. 'sequence',
  119. '',
  120. array(
  121. 'line' => array(
  122. 'name' => 'line',
  123. 'type' => 'tns:line',
  124. 'minOccurs' => '0',
  125. 'maxOccurs' => 'unbounded'
  126. )
  127. ),
  128. null,
  129. 'tns:line'
  130. );
  131. $server->wsdl->addComplexType(
  132. 'invoice',
  133. 'complexType',
  134. 'struct',
  135. 'all',
  136. '',
  137. array(
  138. 'id' => array('name'=>'id', 'type'=>'xsd:string'),
  139. 'ref' => array('name'=>'ref', 'type'=>'xsd:string'),
  140. 'ref_ext' => array('name'=>'ref_ext', 'type'=>'xsd:string'),
  141. 'thirdparty_id' => array('name'=>'thirdparty_id', 'type'=>'xsd:int'),
  142. 'fk_user_author' => array('name'=>'fk_user_author', 'type'=>'xsd:string'),
  143. 'fk_user_valid' => array('name'=>'fk_user_valid', 'type'=>'xsd:string'),
  144. 'date' => array('name'=>'date', 'type'=>'xsd:date'),
  145. 'date_due' => array('name'=>'date_due', 'type'=>'xsd:date'),
  146. 'date_creation' => array('name'=>'date_creation', 'type'=>'xsd:dateTime'),
  147. 'date_validation' => array('name'=>'date_validation', 'type'=>'xsd:dateTime'),
  148. 'date_modification' => array('name'=>'date_modification', 'type'=>'xsd:dateTime'),
  149. 'payment_mode_id' => array('name'=>'payment_mode_id', 'type'=>'xsd:string'),
  150. 'type' => array('name'=>'type', 'type'=>'xsd:int'),
  151. 'total_net' => array('name'=>'type', 'type'=>'xsd:double'),
  152. 'total_vat' => array('name'=>'type', 'type'=>'xsd:double'),
  153. 'total' => array('name'=>'type', 'type'=>'xsd:double'),
  154. 'note_private' => array('name'=>'note_private', 'type'=>'xsd:string'),
  155. 'note_public' => array('name'=>'note_public', 'type'=>'xsd:string'),
  156. 'status' => array('name'=>'status', 'type'=>'xsd:int'),
  157. 'close_code' => array('name'=>'close_code', 'type'=>'xsd:string'),
  158. 'close_note' => array('name'=>'close_note', 'type'=>'xsd:string'),
  159. 'project_id' => array('name'=>'project_id', 'type'=>'xsd:string'),
  160. 'lines' => array('name'=>'lines', 'type'=>'tns:LinesArray2')
  161. )
  162. );
  163. /*
  164. $server->wsdl->addComplexType(
  165. 'InvoicesArray',
  166. 'complexType',
  167. 'array',
  168. '',
  169. 'SOAP-ENC:Array',
  170. array(),
  171. array(
  172. array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:invoice[]')
  173. ),
  174. 'tns:invoice'
  175. );*/
  176. $server->wsdl->addComplexType(
  177. 'InvoicesArray2',
  178. 'complexType',
  179. 'array',
  180. 'sequence',
  181. '',
  182. array(
  183. 'invoice' => array(
  184. 'name' => 'invoice',
  185. 'type' => 'tns:invoice',
  186. 'minOccurs' => '0',
  187. 'maxOccurs' => 'unbounded'
  188. )
  189. ),
  190. null,
  191. 'tns:invoice'
  192. );
  193. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  194. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  195. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  196. $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  197. $styleuse = 'encoded'; // encoded/literal/literal wrapped
  198. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  199. // Register WSDL
  200. $server->register(
  201. 'getInvoice',
  202. // Entry values
  203. array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
  204. // Exit values
  205. array('result'=>'tns:result', 'invoice'=>'tns:invoice'),
  206. $ns,
  207. $ns.'#getInvoice',
  208. $styledoc,
  209. $styleuse,
  210. 'WS to get a particular invoice'
  211. );
  212. $server->register(
  213. 'getInvoicesForThirdParty',
  214. // Entry values
  215. array('authentication'=>'tns:authentication', 'idthirdparty'=>'xsd:string'),
  216. // Exit values
  217. array('result'=>'tns:result', 'invoices'=>'tns:InvoicesArray2'),
  218. $ns,
  219. $ns.'#getInvoicesForThirdParty',
  220. $styledoc,
  221. $styleuse,
  222. 'WS to get all invoices of a third party'
  223. );
  224. $server->register(
  225. 'createInvoice',
  226. // Entry values
  227. array('authentication'=>'tns:authentication', 'invoice'=>'tns:invoice'),
  228. // Exit values
  229. array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
  230. $ns,
  231. $ns.'#createInvoice',
  232. $styledoc,
  233. $styleuse,
  234. 'WS to create an invoice'
  235. );
  236. $server->register(
  237. 'createInvoiceFromOrder',
  238. // Entry values
  239. array('authentication'=>'tns:authentication', 'id_order'=>'xsd:string', 'ref_order'=>'xsd:string', 'ref_ext_order'=>'xsd:string'),
  240. // Exit values
  241. array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
  242. $ns,
  243. $ns.'#createInvoiceFromOrder',
  244. $styledoc,
  245. $styleuse,
  246. 'WS to create an invoice from an order'
  247. );
  248. $server->register(
  249. 'updateInvoice',
  250. // Entry values
  251. array('authentication'=>'tns:authentication', 'invoice'=>'tns:invoice'),
  252. // Exit values
  253. array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
  254. $ns,
  255. $ns.'#updateInvoice',
  256. $styledoc,
  257. $styleuse,
  258. 'WS to update an invoice'
  259. );
  260. /**
  261. * Get invoice from id, ref or ref_ext.
  262. *
  263. * @param array $authentication Array of authentication information
  264. * @param int $id Id
  265. * @param string $ref Ref
  266. * @param string $ref_ext Ref_ext
  267. * @return array Array result
  268. */
  269. function getInvoice($authentication, $id = '', $ref = '', $ref_ext = '')
  270. {
  271. global $db, $conf;
  272. dol_syslog("Function: getInvoice login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
  273. if ($authentication['entity']) {
  274. $conf->entity = $authentication['entity'];
  275. }
  276. // Init and check authentication
  277. $objectresp = array();
  278. $errorcode = ''; $errorlabel = '';
  279. $error = 0;
  280. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  281. // Check parameters
  282. if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
  283. $error++;
  284. $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
  285. }
  286. if (!$error) {
  287. $fuser->getrights();
  288. if ($fuser->rights->facture->lire) {
  289. $invoice = new Facture($db);
  290. $result = $invoice->fetch($id, $ref, $ref_ext);
  291. if ($result > 0) {
  292. $linesresp = array();
  293. $i = 0;
  294. foreach ($invoice->lines as $line) {
  295. //var_dump($line); exit;
  296. $linesresp[] = array(
  297. 'id'=>$line->id,
  298. 'type'=>$line->product_type,
  299. 'desc'=>dol_htmlcleanlastbr($line->desc),
  300. 'total_net'=>$line->total_ht,
  301. 'total_vat'=>$line->total_tva,
  302. 'total'=>$line->total_ttc,
  303. 'vat_rate'=>$line->tva_tx,
  304. 'qty'=>$line->qty,
  305. 'unitprice'=> $line->subprice,
  306. 'date_start'=> $line->date_start ?dol_print_date($line->date_start, 'dayrfc') : '',
  307. 'date_end'=> $line->date_end ?dol_print_date($line->date_end, 'dayrfc') : '',
  308. 'product_id'=>$line->fk_product,
  309. 'product_ref'=>$line->product_ref,
  310. 'product_label'=>$line->product_label,
  311. 'product_desc'=>$line->product_desc,
  312. );
  313. $i++;
  314. }
  315. // Create invoice
  316. $objectresp = array(
  317. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  318. 'invoice'=>array(
  319. 'id' => $invoice->id,
  320. 'ref' => $invoice->ref,
  321. 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
  322. 'thirdparty_id' => $invoice->socid,
  323. 'fk_user_author' => $invoice->user_author ? $invoice->user_author : '',
  324. 'fk_user_valid' => $invoice->user_valid ? $invoice->user_valid : '',
  325. 'date' => $invoice->date ?dol_print_date($invoice->date, 'dayrfc') : '',
  326. 'date_due' => $invoice->date_lim_reglement ?dol_print_date($invoice->date_lim_reglement, 'dayrfc') : '',
  327. 'date_creation' => $invoice->date_creation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
  328. 'date_validation' => $invoice->date_validation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
  329. 'date_modification' => $invoice->datem ?dol_print_date($invoice->datem, 'dayhourrfc') : '',
  330. 'type' => $invoice->type,
  331. 'total_net' => $invoice->total_ht,
  332. 'total_vat' => $invoice->total_tva,
  333. 'total' => $invoice->total_ttc,
  334. 'note_private' => $invoice->note_private ? $invoice->note_private : '',
  335. 'note_public' => $invoice->note_public ? $invoice->note_public : '',
  336. 'status' => $invoice->statut,
  337. 'project_id' => $invoice->fk_project,
  338. 'close_code' => $invoice->close_code ? $invoice->close_code : '',
  339. 'close_note' => $invoice->close_note ? $invoice->close_note : '',
  340. 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id : '',
  341. 'lines' => $linesresp
  342. ));
  343. } else {
  344. $error++;
  345. $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
  346. }
  347. } else {
  348. $error++;
  349. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  350. }
  351. }
  352. if ($error) {
  353. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  354. }
  355. return $objectresp;
  356. }
  357. /**
  358. * Get list of invoices for third party
  359. *
  360. * @param array $authentication Array of authentication information
  361. * @param int $idthirdparty Id thirdparty
  362. * @return array Array result
  363. */
  364. function getInvoicesForThirdParty($authentication, $idthirdparty)
  365. {
  366. global $db, $conf;
  367. dol_syslog("Function: getInvoicesForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
  368. if ($authentication['entity']) {
  369. $conf->entity = $authentication['entity'];
  370. }
  371. // Init and check authentication
  372. $objectresp = array();
  373. $errorcode = ''; $errorlabel = '';
  374. $error = 0;
  375. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  376. if ($fuser->socid) {
  377. $socid = $fuser->socid;
  378. }
  379. // Check parameters
  380. if (!$error && empty($idthirdparty)) {
  381. $error++;
  382. $errorcode = 'BAD_PARAMETERS'; $errorlabel = 'Parameter idthirdparty is not provided';
  383. }
  384. if (!$error) {
  385. $linesinvoice = array();
  386. $sql = 'SELECT f.rowid as facid, ref as ref, ref_ext, type, fk_statut as status, total_ttc, total, tva';
  387. $sql .= ' FROM '.MAIN_DB_PREFIX.'facture as f';
  388. $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
  389. if ($idthirdparty != 'all') {
  390. $sql .= " AND f.fk_soc = ".((int) $idthirdparty);
  391. }
  392. $resql = $db->query($sql);
  393. if ($resql) {
  394. $num = $db->num_rows($resql);
  395. $i = 0;
  396. while ($i < $num) {
  397. // En attendant remplissage par boucle
  398. $obj = $db->fetch_object($resql);
  399. $invoice = new Facture($db);
  400. $invoice->fetch($obj->facid);
  401. // Sécurité pour utilisateur externe
  402. if ($socid && ($socid != $invoice->socid)) {
  403. $error++;
  404. $errorcode = 'PERMISSION_DENIED'; $errorlabel = $invoice->socid.' User does not have permission for this request';
  405. }
  406. if (!$error) {
  407. // Define lines of invoice
  408. $linesresp = array();
  409. foreach ($invoice->lines as $line) {
  410. $linesresp[] = array(
  411. 'id'=>$line->id,
  412. 'type'=>$line->product_type,
  413. 'total_net'=>$line->total_ht,
  414. 'total_vat'=>$line->total_tva,
  415. 'total'=>$line->total_ttc,
  416. 'vat_rate'=>$line->tva_tx,
  417. 'qty'=>$line->qty,
  418. 'unitprice'=> $line->subprice,
  419. 'date_start'=> $line->date_start ?dol_print_date($line->date_start, 'dayrfc') : '',
  420. 'date_end'=> $line->date_end ?dol_print_date($line->date_end, 'dayrfc') : '',
  421. 'product_id'=>$line->fk_product,
  422. 'product_ref'=>$line->product_ref,
  423. 'product_label'=>$line->product_label,
  424. 'product_desc'=>$line->product_desc,
  425. );
  426. }
  427. // Now define invoice
  428. $linesinvoice[] = array(
  429. 'id' => $invoice->id,
  430. 'ref' => $invoice->ref,
  431. 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
  432. 'fk_user_author' => $invoice->user_author ? $invoice->user_author : '',
  433. 'fk_user_valid' => $invoice->user_valid ? $invoice->user_valid : '',
  434. 'date' => $invoice->date ?dol_print_date($invoice->date, 'dayrfc') : '',
  435. 'date_due' => $invoice->date_lim_reglement ?dol_print_date($invoice->date_lim_reglement, 'dayrfc') : '',
  436. 'date_creation' => $invoice->date_creation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
  437. 'date_validation' => $invoice->date_validation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
  438. 'date_modification' => $invoice->datem ?dol_print_date($invoice->datem, 'dayhourrfc') : '',
  439. 'type' => $invoice->type,
  440. 'total_net' => $invoice->total_ht,
  441. 'total_vat' => $invoice->total_tva,
  442. 'total' => $invoice->total_ttc,
  443. 'note_private' => $invoice->note_private ? $invoice->note_private : '',
  444. 'note_public' => $invoice->note_public ? $invoice->note_public : '',
  445. 'status'=> $invoice->statut,
  446. 'project_id' => $invoice->fk_project,
  447. 'close_code' => $invoice->close_code ? $invoice->close_code : '',
  448. 'close_note' => $invoice->close_note ? $invoice->close_note : '',
  449. 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id : '',
  450. 'lines' => $linesresp
  451. );
  452. }
  453. $i++;
  454. }
  455. $objectresp = array(
  456. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  457. 'invoices'=>$linesinvoice
  458. );
  459. } else {
  460. $error++;
  461. $errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
  462. }
  463. }
  464. if ($error) {
  465. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  466. }
  467. return $objectresp;
  468. }
  469. /**
  470. * Create an invoice
  471. *
  472. * @param array $authentication Array of authentication information
  473. * @param array $invoice Invoice
  474. * @return array Array result
  475. */
  476. function createInvoice($authentication, $invoice)
  477. {
  478. global $db, $conf;
  479. $now = dol_now();
  480. dol_syslog("Function: createInvoice login=".$authentication['login']." id=".$invoice['id'].", ref=".$invoice['ref'].", ref_ext=".$invoice['ref_ext']);
  481. if ($authentication['entity']) {
  482. $conf->entity = $authentication['entity'];
  483. }
  484. // Init and check authentication
  485. $objectresp = array();
  486. $errorcode = ''; $errorlabel = '';
  487. $error = 0;
  488. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  489. // Check parameters
  490. if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
  491. $error++; $errorcode = 'KO'; $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
  492. }
  493. if (!$error) {
  494. $new_invoice = new Facture($db);
  495. $new_invoice->socid = $invoice['thirdparty_id'];
  496. $new_invoice->type = $invoice['type'];
  497. $new_invoice->ref_ext = $invoice['ref_ext'];
  498. $new_invoice->date = dol_stringtotime($invoice['date'], 'dayrfc');
  499. $new_invoice->note_private = $invoice['note_private'];
  500. $new_invoice->note_public = $invoice['note_public'];
  501. $new_invoice->statut = Facture::STATUS_DRAFT; // We start with status draft
  502. $new_invoice->fk_project = $invoice['project_id'];
  503. $new_invoice->date_creation = $now;
  504. //take mode_reglement and cond_reglement from thirdparty
  505. $soc = new Societe($db);
  506. $res = $soc->fetch($new_invoice->socid);
  507. if ($res > 0) {
  508. $new_invoice->mode_reglement_id = !empty($invoice['payment_mode_id']) ? $invoice['payment_mode_id'] : $soc->mode_reglement_id;
  509. $new_invoice->cond_reglement_id = $soc->cond_reglement_id;
  510. } else {
  511. $new_invoice->mode_reglement_id = $invoice['payment_mode_id'];
  512. }
  513. // Trick because nusoap does not store data with same structure if there is one or several lines
  514. $arrayoflines = array();
  515. if (isset($invoice['lines']['line'][0])) {
  516. $arrayoflines = $invoice['lines']['line'];
  517. } else {
  518. $arrayoflines = $invoice['lines'];
  519. }
  520. foreach ($arrayoflines as $line) {
  521. // $key can be 'line' or '0','1',...
  522. $newline = new FactureLigne($db);
  523. $newline->product_type = $line['type'];
  524. $newline->desc = $line['desc'];
  525. $newline->fk_product = $line['product_id'];
  526. $newline->tva_tx = isset($line['vat_rate']) ? $line['vat_rate'] : 0;
  527. $newline->qty = $line['qty'];
  528. $newline->subprice = isset($line['unitprice']) ? $line['unitprice'] : null;
  529. $newline->total_ht = $line['total_net'];
  530. $newline->total_tva = $line['total_vat'];
  531. $newline->total_ttc = $line['total'];
  532. $newline->date_start = dol_stringtotime($line['date_start']);
  533. $newline->date_end = dol_stringtotime($line['date_end']);
  534. $new_invoice->lines[] = $newline;
  535. }
  536. //var_dump($newobject->date_lim_reglement); exit;
  537. //var_dump($invoice['lines'][0]['type']);
  538. $db->begin();
  539. $result = $new_invoice->create($fuser, 0, dol_stringtotime($invoice['date_due'], 'dayrfc'));
  540. if ($result < 0) {
  541. $error++;
  542. }
  543. if (!$error && $invoice['status'] == Facture::STATUS_VALIDATED) { // We want invoice to have status validated
  544. $result = $new_invoice->validate($fuser);
  545. if ($result < 0) {
  546. $error++;
  547. }
  548. }
  549. if (!$error) {
  550. $db->commit();
  551. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$new_invoice->id,
  552. 'ref'=>$new_invoice->ref, 'ref_ext'=>$new_invoice->ref_ext);
  553. } else {
  554. $db->rollback();
  555. $error++;
  556. $errorcode = 'KO';
  557. $errorlabel = $new_invoice->error;
  558. dol_syslog("Function: createInvoice error while creating".$errorlabel);
  559. }
  560. }
  561. if ($error) {
  562. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  563. }
  564. return $objectresp;
  565. }
  566. /**
  567. * Create an invoice from an order
  568. *
  569. * @param array $authentication Array of authentication information
  570. * @param string $id_order id of order to copy invoice from
  571. * @param string $ref_order ref of order to copy invoice from
  572. * @param string $ref_ext_order ref_ext of order to copy invoice from
  573. * @return array Array result
  574. */
  575. function createInvoiceFromOrder($authentication, $id_order = '', $ref_order = '', $ref_ext_order = '')
  576. {
  577. global $db, $conf;
  578. dol_syslog("Function: createInvoiceFromOrder login=".$authentication['login']." id=".$id_order.", ref=".$ref_order.", ref_ext=".$ref_ext_order);
  579. if ($authentication['entity']) {
  580. $conf->entity = $authentication['entity'];
  581. }
  582. // Init and check authentication
  583. $objectresp = array();
  584. $errorcode = ''; $errorlabel = '';
  585. $error = 0;
  586. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  587. if ($fuser->socid) {
  588. $socid = $fuser->socid;
  589. }
  590. // Check parameters
  591. if (empty($id_order) && empty($ref_order) && empty($ref_ext_order)) {
  592. $error++; $errorcode = 'KO'; $errorlabel = "order id or ref or ref_ext is mandatory.";
  593. }
  594. //////////////////////
  595. if (!$error) {
  596. $fuser->getrights();
  597. if ($fuser->rights->commande->lire) {
  598. $order = new Commande($db);
  599. $result = $order->fetch($id_order, $ref_order, $ref_ext_order);
  600. if ($result > 0) {
  601. // Security for external user
  602. if ($socid && ($socid != $order->socid)) {
  603. $error++;
  604. $errorcode = 'PERMISSION_DENIED'; $errorlabel = $order->socid.'User does not have permission for this request';
  605. }
  606. if (!$error) {
  607. $newobject = new Facture($db);
  608. $result = $newobject->createFromOrder($order, $fuser);
  609. if ($result < 0) {
  610. $error++;
  611. dol_syslog("Webservice server_invoice:: invoice creation from order failed", LOG_ERR);
  612. }
  613. }
  614. } else {
  615. $error++;
  616. $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id_order.' nor ref='.$ref_order.' nor ref_ext='.$ref_ext_order;
  617. }
  618. } else {
  619. $error++;
  620. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  621. }
  622. }
  623. if ($error) {
  624. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  625. } else {
  626. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref, 'ref_ext'=>$newobject->ref_ext);
  627. }
  628. return $objectresp;
  629. }
  630. /**
  631. * Uddate an invoice, only change the state of an invoice
  632. *
  633. * @param array $authentication Array of authentication information
  634. * @param Facture $invoice Invoice
  635. * @return array Array result
  636. */
  637. function updateInvoice($authentication, $invoice)
  638. {
  639. global $db, $conf, $langs;
  640. dol_syslog("Function: updateInvoice login=".$authentication['login']." id=".$invoice['id'].
  641. ", ref=".$invoice['ref'].", ref_ext=".$invoice['ref_ext']);
  642. if ($authentication['entity']) {
  643. $conf->entity = $authentication['entity'];
  644. }
  645. // Init and check authentication
  646. $objectresp = array();
  647. $errorcode = ''; $errorlabel = '';
  648. $error = 0;
  649. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  650. // Check parameters
  651. if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
  652. $error++; $errorcode = 'KO'; $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
  653. }
  654. if (!$error) {
  655. $objectfound = false;
  656. $object = new Facture($db);
  657. $result = $object->fetch($invoice['id'], $invoice['ref'], $invoice['ref_ext'], '');
  658. if (!empty($object->id)) {
  659. $objectfound = true;
  660. $db->begin();
  661. if (isset($invoice['status'])) {
  662. if ($invoice['status'] == Facture::STATUS_DRAFT) {
  663. $result = $object->setDraft($fuser);
  664. }
  665. if ($invoice['status'] == Facture::STATUS_VALIDATED) {
  666. $result = $object->validate($fuser);
  667. if ($result >= 0) {
  668. // Define output language
  669. $outputlangs = $langs;
  670. $object->generateDocument($object->model_pdf, $outputlangs);
  671. }
  672. }
  673. if ($invoice['status'] == Facture::STATUS_CLOSED) {
  674. $result = $object->setPaid($fuser, $invoice['close_code'], $invoice['close_note']);
  675. }
  676. if ($invoice['status'] == Facture::STATUS_ABANDONED) {
  677. $result = $object->setCanceled($fuser, $invoice['close_code'], $invoice['close_note']);
  678. }
  679. }
  680. }
  681. if ((!$error) && ($objectfound)) {
  682. $db->commit();
  683. $objectresp = array(
  684. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  685. 'id'=>$object->id,
  686. 'ref'=>$object->ref,
  687. 'ref_ext'=>$object->ref_ext
  688. );
  689. } elseif ($objectfound) {
  690. $db->rollback();
  691. $error++;
  692. $errorcode = 'KO';
  693. $errorlabel = $object->error;
  694. } else {
  695. $error++;
  696. $errorcode = 'NOT_FOUND';
  697. $errorlabel = 'Invoice id='.$invoice['id'].' ref='.$invoice['ref'].' ref_ext='.$invoice['ref_ext'].' cannot be found';
  698. }
  699. }
  700. if ($error) {
  701. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  702. }
  703. return $objectresp;
  704. }
  705. // Return the results.
  706. $server->service(file_get_contents("php://input"));