server_invoice.php 27 KB

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