server_invoice.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 (empty($conf->global->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 = '', $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 = ''; $errorlabel = '';
  297. $error = 0;
  298. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  299. // Check parameters
  300. if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
  301. $error++;
  302. $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
  303. }
  304. if (!$error) {
  305. $fuser->getrights();
  306. if ($fuser->hasRight('facture', 'lire')) {
  307. $invoice = new Facture($db);
  308. $result = $invoice->fetch($id, $ref, $ref_ext);
  309. if ($result > 0) {
  310. $linesresp = array();
  311. $i = 0;
  312. foreach ($invoice->lines as $line) {
  313. //var_dump($line); exit;
  314. $linesresp[] = array(
  315. 'id'=>$line->id,
  316. 'type'=>$line->product_type,
  317. 'desc'=>dol_htmlcleanlastbr($line->desc),
  318. 'total_net'=>$line->total_ht,
  319. 'total_vat'=>$line->total_tva,
  320. 'total'=>$line->total_ttc,
  321. 'vat_rate'=>$line->tva_tx,
  322. 'qty'=>$line->qty,
  323. 'unitprice'=> $line->subprice,
  324. 'date_start'=> $line->date_start ?dol_print_date($line->date_start, 'dayrfc') : '',
  325. 'date_end'=> $line->date_end ?dol_print_date($line->date_end, 'dayrfc') : '',
  326. 'product_id'=>$line->fk_product,
  327. 'product_ref'=>$line->product_ref,
  328. 'product_label'=>$line->product_label,
  329. 'product_desc'=>$line->product_desc,
  330. );
  331. $i++;
  332. }
  333. // Create invoice
  334. $objectresp = array(
  335. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  336. 'invoice'=>array(
  337. 'id' => $invoice->id,
  338. 'ref' => $invoice->ref,
  339. 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
  340. 'thirdparty_id' => $invoice->socid,
  341. 'fk_user_author' => $invoice->fk_user_author ? $invoice->fk_user_author : '',
  342. 'fk_user_valid' => $invoice->user_validation_id ? $invoice->user_validation_id : '',
  343. 'date' => $invoice->date ?dol_print_date($invoice->date, 'dayrfc') : '',
  344. 'date_due' => $invoice->date_lim_reglement ?dol_print_date($invoice->date_lim_reglement, 'dayrfc') : '',
  345. 'date_creation' => $invoice->date_creation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
  346. 'date_validation' => $invoice->date_validation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
  347. 'date_modification' => $invoice->datem ?dol_print_date($invoice->datem, 'dayhourrfc') : '',
  348. 'type' => $invoice->type,
  349. 'total_net' => $invoice->total_ht,
  350. 'total_vat' => $invoice->total_tva,
  351. 'total' => $invoice->total_ttc,
  352. 'note_private' => $invoice->note_private ? $invoice->note_private : '',
  353. 'note_public' => $invoice->note_public ? $invoice->note_public : '',
  354. 'status' => $invoice->statut,
  355. 'project_id' => $invoice->fk_project,
  356. 'close_code' => $invoice->close_code ? $invoice->close_code : '',
  357. 'close_note' => $invoice->close_note ? $invoice->close_note : '',
  358. 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id : '',
  359. 'lines' => $linesresp
  360. ));
  361. } else {
  362. $error++;
  363. $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
  364. }
  365. } else {
  366. $error++;
  367. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  368. }
  369. }
  370. if ($error) {
  371. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  372. }
  373. return $objectresp;
  374. }
  375. /**
  376. * Get list of invoices for third party
  377. *
  378. * @param array $authentication Array of authentication information
  379. * @param int $idthirdparty Id thirdparty
  380. * @return array Array result
  381. */
  382. function getInvoicesForThirdParty($authentication, $idthirdparty)
  383. {
  384. global $db, $conf;
  385. dol_syslog("Function: getInvoicesForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
  386. if ($authentication['entity']) {
  387. $conf->entity = $authentication['entity'];
  388. }
  389. // Init and check authentication
  390. $objectresp = array();
  391. $errorcode = ''; $errorlabel = '';
  392. $error = 0;
  393. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  394. if ($fuser->socid) {
  395. $socid = $fuser->socid;
  396. }
  397. // Check parameters
  398. if (!$error && empty($idthirdparty)) {
  399. $error++;
  400. $errorcode = 'BAD_PARAMETERS'; $errorlabel = 'Parameter idthirdparty is not provided';
  401. }
  402. if (!$error) {
  403. $linesinvoice = array();
  404. $sql = 'SELECT f.rowid as facid, ref as ref, ref_ext, type, fk_statut as status, total_ttc, total, tva';
  405. $sql .= ' FROM '.MAIN_DB_PREFIX.'facture as f';
  406. $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
  407. if ($idthirdparty != 'all') {
  408. $sql .= " AND f.fk_soc = ".((int) $idthirdparty);
  409. }
  410. $resql = $db->query($sql);
  411. if ($resql) {
  412. $num = $db->num_rows($resql);
  413. $i = 0;
  414. while ($i < $num) {
  415. // En attendant remplissage par boucle
  416. $obj = $db->fetch_object($resql);
  417. $invoice = new Facture($db);
  418. $invoice->fetch($obj->facid);
  419. // Sécurité pour utilisateur externe
  420. if ($socid && ($socid != $invoice->socid)) {
  421. $error++;
  422. $errorcode = 'PERMISSION_DENIED'; $errorlabel = $invoice->socid.' User does not have permission for this request';
  423. }
  424. if (!$error) {
  425. // Define lines of invoice
  426. $linesresp = array();
  427. foreach ($invoice->lines as $line) {
  428. $linesresp[] = array(
  429. 'id'=>$line->id,
  430. 'type'=>$line->product_type,
  431. 'total_net'=>$line->total_ht,
  432. 'total_vat'=>$line->total_tva,
  433. 'total'=>$line->total_ttc,
  434. 'vat_rate'=>$line->tva_tx,
  435. 'qty'=>$line->qty,
  436. 'unitprice'=> $line->subprice,
  437. 'date_start'=> $line->date_start ?dol_print_date($line->date_start, 'dayrfc') : '',
  438. 'date_end'=> $line->date_end ?dol_print_date($line->date_end, 'dayrfc') : '',
  439. 'product_id'=>$line->fk_product,
  440. 'product_ref'=>$line->product_ref,
  441. 'product_label'=>$line->product_label,
  442. 'product_desc'=>$line->product_desc,
  443. );
  444. }
  445. // Now define invoice
  446. $linesinvoice[] = array(
  447. 'id' => $invoice->id,
  448. 'ref' => $invoice->ref,
  449. 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
  450. 'fk_user_author' => $invoice->fk_user_author ? $invoice->fk_user_author : '',
  451. 'fk_user_valid' => $invoice->user_validation_id ? $invoice->user_validation_id : '',
  452. 'date' => $invoice->date ?dol_print_date($invoice->date, 'dayrfc') : '',
  453. 'date_due' => $invoice->date_lim_reglement ?dol_print_date($invoice->date_lim_reglement, 'dayrfc') : '',
  454. 'date_creation' => $invoice->date_creation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
  455. 'date_validation' => $invoice->date_validation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
  456. 'date_modification' => $invoice->datem ?dol_print_date($invoice->datem, 'dayhourrfc') : '',
  457. 'type' => $invoice->type,
  458. 'total_net' => $invoice->total_ht,
  459. 'total_vat' => $invoice->total_tva,
  460. 'total' => $invoice->total_ttc,
  461. 'note_private' => $invoice->note_private ? $invoice->note_private : '',
  462. 'note_public' => $invoice->note_public ? $invoice->note_public : '',
  463. 'status'=> $invoice->statut,
  464. 'project_id' => $invoice->fk_project,
  465. 'close_code' => $invoice->close_code ? $invoice->close_code : '',
  466. 'close_note' => $invoice->close_note ? $invoice->close_note : '',
  467. 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id : '',
  468. 'lines' => $linesresp
  469. );
  470. }
  471. $i++;
  472. }
  473. $objectresp = array(
  474. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  475. 'invoices'=>$linesinvoice
  476. );
  477. } else {
  478. $error++;
  479. $errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
  480. }
  481. }
  482. if ($error) {
  483. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  484. }
  485. return $objectresp;
  486. }
  487. /**
  488. * Create an invoice
  489. *
  490. * @param array $authentication Array of authentication information
  491. * @param array $invoice Invoice
  492. * @return array Array result
  493. */
  494. function createInvoice($authentication, $invoice)
  495. {
  496. global $db, $conf;
  497. $now = dol_now();
  498. dol_syslog("Function: createInvoice login=".$authentication['login']." id=".$invoice['id'].", ref=".$invoice['ref'].", ref_ext=".$invoice['ref_ext']);
  499. if ($authentication['entity']) {
  500. $conf->entity = $authentication['entity'];
  501. }
  502. // Init and check authentication
  503. $objectresp = array();
  504. $errorcode = ''; $errorlabel = '';
  505. $error = 0;
  506. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  507. // Check parameters
  508. if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
  509. $error++; $errorcode = 'KO'; $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
  510. }
  511. if (!$error) {
  512. $new_invoice = new Facture($db);
  513. $new_invoice->socid = $invoice['thirdparty_id'];
  514. $new_invoice->type = $invoice['type'];
  515. $new_invoice->ref_ext = $invoice['ref_ext'];
  516. $new_invoice->date = dol_stringtotime($invoice['date'], 'dayrfc');
  517. $new_invoice->note_private = $invoice['note_private'];
  518. $new_invoice->note_public = $invoice['note_public'];
  519. $new_invoice->statut = Facture::STATUS_DRAFT; // We start with status draft
  520. $new_invoice->fk_project = $invoice['project_id'];
  521. $new_invoice->date_creation = $now;
  522. //take mode_reglement and cond_reglement from thirdparty
  523. $soc = new Societe($db);
  524. $res = $soc->fetch($new_invoice->socid);
  525. if ($res > 0) {
  526. $new_invoice->mode_reglement_id = !empty($invoice['payment_mode_id']) ? $invoice['payment_mode_id'] : $soc->mode_reglement_id;
  527. $new_invoice->cond_reglement_id = $soc->cond_reglement_id;
  528. } else {
  529. $new_invoice->mode_reglement_id = $invoice['payment_mode_id'];
  530. }
  531. // Trick because nusoap does not store data with same structure if there is one or several lines
  532. $arrayoflines = array();
  533. if (isset($invoice['lines']['line'][0])) {
  534. $arrayoflines = $invoice['lines']['line'];
  535. } else {
  536. $arrayoflines = $invoice['lines'];
  537. }
  538. foreach ($arrayoflines as $line) {
  539. // $key can be 'line' or '0','1',...
  540. $newline = new FactureLigne($db);
  541. $newline->product_type = $line['type'];
  542. $newline->desc = $line['desc'];
  543. $newline->fk_product = $line['product_id'];
  544. $newline->tva_tx = isset($line['vat_rate']) ? $line['vat_rate'] : 0;
  545. $newline->qty = $line['qty'];
  546. $newline->subprice = isset($line['unitprice']) ? $line['unitprice'] : null;
  547. $newline->total_ht = $line['total_net'];
  548. $newline->total_tva = $line['total_vat'];
  549. $newline->total_ttc = $line['total'];
  550. $newline->date_start = dol_stringtotime($line['date_start']);
  551. $newline->date_end = dol_stringtotime($line['date_end']);
  552. $new_invoice->lines[] = $newline;
  553. }
  554. //var_dump($newobject->date_lim_reglement); exit;
  555. //var_dump($invoice['lines'][0]['type']);
  556. $db->begin();
  557. $result = $new_invoice->create($fuser, 0, dol_stringtotime($invoice['date_due'], 'dayrfc'));
  558. if ($result < 0) {
  559. $error++;
  560. }
  561. if (!$error && $invoice['status'] == Facture::STATUS_VALIDATED) { // We want invoice to have status validated
  562. $result = $new_invoice->validate($fuser);
  563. if ($result < 0) {
  564. $error++;
  565. }
  566. }
  567. if (!$error) {
  568. $db->commit();
  569. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$new_invoice->id,
  570. 'ref'=>$new_invoice->ref, 'ref_ext'=>$new_invoice->ref_ext);
  571. } else {
  572. $db->rollback();
  573. $error++;
  574. $errorcode = 'KO';
  575. $errorlabel = $new_invoice->error;
  576. dol_syslog("Function: createInvoice error while creating".$errorlabel);
  577. }
  578. }
  579. if ($error) {
  580. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  581. }
  582. return $objectresp;
  583. }
  584. /**
  585. * Create an invoice from an order
  586. *
  587. * @param array $authentication Array of authentication information
  588. * @param string $id_order id of order to copy invoice from
  589. * @param string $ref_order ref of order to copy invoice from
  590. * @param string $ref_ext_order ref_ext of order to copy invoice from
  591. * @return array Array result
  592. */
  593. function createInvoiceFromOrder($authentication, $id_order = '', $ref_order = '', $ref_ext_order = '')
  594. {
  595. global $db, $conf;
  596. dol_syslog("Function: createInvoiceFromOrder login=".$authentication['login']." id=".$id_order.", ref=".$ref_order.", ref_ext=".$ref_ext_order);
  597. if ($authentication['entity']) {
  598. $conf->entity = $authentication['entity'];
  599. }
  600. // Init and check authentication
  601. $objectresp = array();
  602. $errorcode = ''; $errorlabel = '';
  603. $error = 0;
  604. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  605. if ($fuser->socid) {
  606. $socid = $fuser->socid;
  607. }
  608. // Check parameters
  609. if (empty($id_order) && empty($ref_order) && empty($ref_ext_order)) {
  610. $error++; $errorcode = 'KO'; $errorlabel = "order id or ref or ref_ext is mandatory.";
  611. }
  612. //////////////////////
  613. if (!$error) {
  614. $fuser->getrights();
  615. if ($fuser->hasRight('commande', 'lire')) {
  616. $order = new Commande($db);
  617. $result = $order->fetch($id_order, $ref_order, $ref_ext_order);
  618. if ($result > 0) {
  619. // Security for external user
  620. if ($socid && ($socid != $order->socid)) {
  621. $error++;
  622. $errorcode = 'PERMISSION_DENIED'; $errorlabel = $order->socid.'User does not have permission for this request';
  623. }
  624. if (!$error) {
  625. $newobject = new Facture($db);
  626. $result = $newobject->createFromOrder($order, $fuser);
  627. if ($result < 0) {
  628. $error++;
  629. dol_syslog("Webservice server_invoice:: invoice creation from order failed", LOG_ERR);
  630. }
  631. }
  632. } else {
  633. $error++;
  634. $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id_order.' nor ref='.$ref_order.' nor ref_ext='.$ref_ext_order;
  635. }
  636. } else {
  637. $error++;
  638. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  639. }
  640. }
  641. if ($error) {
  642. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  643. } else {
  644. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref, 'ref_ext'=>$newobject->ref_ext);
  645. }
  646. return $objectresp;
  647. }
  648. /**
  649. * Uddate an invoice, only change the state of an invoice
  650. *
  651. * @param array $authentication Array of authentication information
  652. * @param Facture $invoice Invoice
  653. * @return array Array result
  654. */
  655. function updateInvoice($authentication, $invoice)
  656. {
  657. global $db, $conf, $langs;
  658. dol_syslog("Function: updateInvoice login=".$authentication['login']." id=".$invoice['id'].
  659. ", ref=".$invoice['ref'].", ref_ext=".$invoice['ref_ext']);
  660. if ($authentication['entity']) {
  661. $conf->entity = $authentication['entity'];
  662. }
  663. // Init and check authentication
  664. $objectresp = array();
  665. $errorcode = ''; $errorlabel = '';
  666. $error = 0;
  667. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  668. // Check parameters
  669. if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
  670. $error++; $errorcode = 'KO'; $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
  671. }
  672. if (!$error) {
  673. $objectfound = false;
  674. $object = new Facture($db);
  675. $result = $object->fetch($invoice['id'], $invoice['ref'], $invoice['ref_ext'], '');
  676. if (!empty($object->id)) {
  677. $objectfound = true;
  678. $db->begin();
  679. if (isset($invoice['status'])) {
  680. if ($invoice['status'] == Facture::STATUS_DRAFT) {
  681. $result = $object->setDraft($fuser);
  682. }
  683. if ($invoice['status'] == Facture::STATUS_VALIDATED) {
  684. $result = $object->validate($fuser);
  685. if ($result >= 0) {
  686. // Define output language
  687. $outputlangs = $langs;
  688. $object->generateDocument($object->model_pdf, $outputlangs);
  689. }
  690. }
  691. if ($invoice['status'] == Facture::STATUS_CLOSED) {
  692. $result = $object->setPaid($fuser, $invoice['close_code'], $invoice['close_note']);
  693. }
  694. if ($invoice['status'] == Facture::STATUS_ABANDONED) {
  695. $result = $object->setCanceled($fuser, $invoice['close_code'], $invoice['close_note']);
  696. }
  697. }
  698. }
  699. if ((!$error) && ($objectfound)) {
  700. $db->commit();
  701. $objectresp = array(
  702. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  703. 'id'=>$object->id,
  704. 'ref'=>$object->ref,
  705. 'ref_ext'=>$object->ref_ext
  706. );
  707. } elseif ($objectfound) {
  708. $db->rollback();
  709. $error++;
  710. $errorcode = 'KO';
  711. $errorlabel = $object->error;
  712. } else {
  713. $error++;
  714. $errorcode = 'NOT_FOUND';
  715. $errorlabel = 'Invoice id='.$invoice['id'].' ref='.$invoice['ref'].' ref_ext='.$invoice['ref_ext'].' cannot be found';
  716. }
  717. }
  718. if ($error) {
  719. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  720. }
  721. return $objectresp;
  722. }
  723. // Return the results.
  724. $server->service(file_get_contents("php://input"));