server_invoice.php 26 KB

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