server_supplier_invoice.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. /* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/webservices/server_supplier_invoice.php
  19. * \brief File that is entry point to call Dolibarr WebServices
  20. */
  21. if (!defined('NOCSRFCHECK')) {
  22. define('NOCSRFCHECK', '1'); // Do not check anti CSRF attack test
  23. }
  24. if (!defined('NOTOKENRENEWAL')) {
  25. define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
  26. }
  27. if (!defined('NOREQUIREMENU')) {
  28. define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
  29. }
  30. if (!defined('NOREQUIREHTML')) {
  31. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  32. }
  33. if (!defined('NOREQUIREAJAX')) {
  34. define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
  35. }
  36. if (!defined("NOLOGIN")) {
  37. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  38. }
  39. if (!defined("NOSESSION")) {
  40. define("NOSESSION", '1');
  41. }
  42. require '../main.inc.php';
  43. require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
  44. require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
  45. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  46. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  47. dol_syslog("Call Dolibarr webservices interfaces");
  48. $langs->load("main");
  49. // Enable and test if module web services is enabled
  50. if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
  51. $langs->load("admin");
  52. dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
  53. print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
  54. print $langs->trans("ToActivateModule");
  55. exit;
  56. }
  57. // Create the soap Object
  58. $server = new nusoap_server();
  59. $server->soap_defencoding = 'UTF-8';
  60. $server->decode_utf8 = false;
  61. $ns = 'http://www.dolibarr.org/ns/';
  62. $server->configureWSDL('WebServicesDolibarrSupplierInvoice', $ns);
  63. $server->wsdl->schemaTargetNamespace = $ns;
  64. // Define WSDL Authentication object
  65. $server->wsdl->addComplexType(
  66. 'authentication',
  67. 'complexType',
  68. 'struct',
  69. 'all',
  70. '',
  71. array(
  72. 'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
  73. 'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
  74. 'login' => array('name'=>'login', 'type'=>'xsd:string'),
  75. 'password' => array('name'=>'password', 'type'=>'xsd:string'),
  76. 'entity' => array('name'=>'entity', 'type'=>'xsd:string'),
  77. )
  78. );
  79. // Define WSDL Return object
  80. $server->wsdl->addComplexType(
  81. 'result',
  82. 'complexType',
  83. 'struct',
  84. 'all',
  85. '',
  86. array(
  87. 'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
  88. 'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
  89. )
  90. );
  91. // Define other specific objects
  92. $server->wsdl->addComplexType(
  93. 'line',
  94. 'element',
  95. 'struct',
  96. 'all',
  97. '',
  98. array(
  99. 'id' => array('name'=>'id', 'type'=>'xsd:string'),
  100. 'type' => array('name'=>'type', 'type'=>'xsd:int'),
  101. 'desc' => array('name'=>'desc', 'type'=>'xsd:string'),
  102. 'fk_product' => array('name'=>'fk_product', 'type'=>'xsd:int'),
  103. 'total_net' => array('name'=>'total_net', 'type'=>'xsd:double'),
  104. 'total_vat' => array('name'=>'total_vat', 'type'=>'xsd:double'),
  105. 'total' => array('name'=>'total', 'type'=>'xsd:double'),
  106. 'vat_rate' => array('name'=>'vat_rate', 'type'=>'xsd:double'),
  107. 'qty' => array('name'=>'qty', 'type'=>'xsd:double'),
  108. 'date_start' => array('name'=>'date_start', 'type'=>'xsd:date'),
  109. 'date_end' => array('name'=>'date_end', 'type'=>'xsd:date'),
  110. // From product
  111. 'product_ref' => array('name'=>'product_ref', 'type'=>'xsd:string'),
  112. 'product_label' => array('name'=>'product_label', 'type'=>'xsd:string'),
  113. 'product_desc' => array('name'=>'product_desc', 'type'=>'xsd:string')
  114. )
  115. );
  116. $server->wsdl->addComplexType(
  117. 'LinesArray',
  118. 'complexType',
  119. 'array',
  120. '',
  121. 'SOAP-ENC:Array',
  122. array(),
  123. array(
  124. array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType'=>'tns:line[]')
  125. ),
  126. 'tns:line'
  127. );
  128. $server->wsdl->addComplexType(
  129. 'invoice',
  130. 'element', // If we put element here instead of complexType to have tag called invoice in getInvoicesForThirdParty we brek getInvoice
  131. 'struct',
  132. 'all',
  133. '',
  134. array(
  135. 'id' => array('name'=>'id', 'type'=>'xsd:string'),
  136. 'ref' => array('name'=>'ref', 'type'=>'xsd:string'),
  137. 'ref_ext' => array('name'=>'ref_ext', 'type'=>'xsd:string'),
  138. 'ref_supplier' => array('name'=>'ref_supplier', 'type'=>'xsd:string'),
  139. 'fk_user_author' => array('name'=>'fk_user_author', 'type'=>'xsd:int'),
  140. 'fk_user_valid' => array('name'=>'fk_user_valid', 'type'=>'xsd:int'),
  141. 'fk_thirdparty' => array('name'=>'fk_thirdparty', 'type'=>'xsd:int'),
  142. 'date_creation' => array('name'=>'date_creation', 'type'=>'xsd:dateTime'),
  143. 'date_validation' => array('name'=>'date_validation', 'type'=>'xsd:dateTime'),
  144. 'date_modification' => array('name'=>'date_modification', 'type'=>'xsd:dateTime'),
  145. 'date_invoice' => array('name'=>'date_invoice', 'type'=>'xsd:date'),
  146. 'date_term' => array('name'=>'date_modification', 'type'=>'xsd:date'),
  147. 'label' => array('name'=>'label', 'type'=>'xsd:date'),
  148. 'type' => array('name'=>'type', 'type'=>'xsd:int'),
  149. 'total_net' => array('name'=>'type', 'type'=>'xsd:double'),
  150. 'total_vat' => array('name'=>'type', 'type'=>'xsd:double'),
  151. 'total' => array('name'=>'type', 'type'=>'xsd:double'),
  152. 'note_private' => array('name'=>'note_private', 'type'=>'xsd:string'),
  153. 'note_public' => array('name'=>'note_public', 'type'=>'xsd:string'),
  154. 'status' => array('name'=>'status', 'type'=>'xsd:int'),
  155. 'close_code' => array('name'=>'close_code', 'type'=>'xsd:string'),
  156. 'close_note' => array('name'=>'close_note', 'type'=>'xsd:string'),
  157. 'lines' => array('name'=>'lines', 'type'=>'tns:LinesArray')
  158. )
  159. );
  160. $server->wsdl->addComplexType(
  161. 'InvoicesArray',
  162. 'complexType',
  163. 'array',
  164. '',
  165. 'SOAP-ENC:Array',
  166. array(),
  167. array(
  168. array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType'=>'tns:invoice[]')
  169. ),
  170. 'tns:invoice'
  171. );
  172. $server->wsdl->addComplexType(
  173. 'invoices',
  174. 'complexType',
  175. 'array',
  176. '',
  177. 'SOAP-ENC:Array',
  178. array(),
  179. array(
  180. array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType'=>'tns:invoice[]')
  181. ),
  182. 'tns:invoice'
  183. );
  184. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  185. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  186. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  187. $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  188. $styleuse = 'encoded'; // encoded/literal/literal wrapped
  189. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  190. // Register WSDL
  191. $server->register(
  192. 'getSupplierInvoice',
  193. // Entry values
  194. array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
  195. // Exit values
  196. array('result'=>'tns:result', 'invoice'=>'tns:invoice'),
  197. $ns,
  198. $ns.'#getSupplierInvoice',
  199. $styledoc,
  200. $styleuse,
  201. 'WS to get SupplierInvoice'
  202. );
  203. $server->register(
  204. 'getSupplierInvoicesForThirdParty',
  205. // Entry values
  206. array('authentication'=>'tns:authentication', 'idthirdparty'=>'xsd:string'),
  207. // Exit values
  208. array('result'=>'tns:result', 'invoices'=>'tns:invoices'),
  209. $ns,
  210. $ns.'#getSupplierInvoicesForThirdParty',
  211. $styledoc,
  212. $styleuse,
  213. 'WS to get SupplierInvoicesForThirdParty'
  214. );
  215. /**
  216. * Get invoice from id, ref or ref_ext
  217. *
  218. * @param array $authentication Array of authentication information
  219. * @param int $id Id
  220. * @param string $ref Ref
  221. * @param string $ref_ext Ref_ext
  222. * @return array Array result
  223. */
  224. function getSupplierInvoice($authentication, $id = '', $ref = '', $ref_ext = '')
  225. {
  226. global $db, $conf;
  227. dol_syslog("Function: getSupplierInvoice login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
  228. if ($authentication['entity']) {
  229. $conf->entity = $authentication['entity'];
  230. }
  231. // Init and check authentication
  232. $objectresp = array();
  233. $errorcode = ''; $errorlabel = '';
  234. $error = 0;
  235. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  236. // Check parameters
  237. if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
  238. $error++;
  239. $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
  240. }
  241. if (!$error) {
  242. $fuser->getrights();
  243. if ($fuser->rights->fournisseur->facture->lire) {
  244. $invoice = new FactureFournisseur($db);
  245. $result = $invoice->fetch($id, $ref, $ref_ext);
  246. if ($result > 0) {
  247. $linesresp = array();
  248. $i = 0;
  249. foreach ($invoice->lines as $line) {
  250. //var_dump($line); exit;
  251. $linesresp[] = array(
  252. 'id'=>$line->rowid,
  253. 'type'=>$line->product_type,
  254. 'total_net'=>$line->total_ht,
  255. 'total_vat'=>$line->total_tva,
  256. 'total'=>$line->total_ttc,
  257. 'vat_rate'=>$line->tva_tx,
  258. 'qty'=>$line->qty
  259. );
  260. $i++;
  261. }
  262. // Create invoice
  263. $objectresp = array(
  264. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  265. 'invoice'=>array(
  266. 'id' => $invoice->id,
  267. 'ref' => $invoice->ref,
  268. 'ref_supplier'=>$invoice->ref_supplier,
  269. 'ref_ext' => $invoice->ref_ext,
  270. 'fk_user_author' => $invoice->fk_user_author,
  271. 'fk_user_valid' => $invoice->fk_user_valid,
  272. 'fk_thirdparty' => $invoice->fk_soc,
  273. 'type'=>$invoice->type,
  274. 'status'=>$invoice->statut,
  275. 'total_net'=>$invoice->total_ht,
  276. 'total_vat'=>$invoice->total_tva,
  277. 'total'=>$invoice->total_ttc,
  278. 'date_creation'=>dol_print_date($invoice->datec, 'dayhourrfc'),
  279. 'date_modification'=>dol_print_date($invoice->tms, 'dayhourrfc'),
  280. 'date_invoice'=>dol_print_date($invoice->date, 'dayhourrfc'),
  281. 'date_term'=>dol_print_date($invoice->date_echeance, 'dayhourrfc'),
  282. 'label'=>$invoice->label,
  283. 'paid'=>$invoice->paid,
  284. 'note_private'=>$invoice->note_private,
  285. 'note_public'=>$invoice->note_public,
  286. 'close_code'=>$invoice->close_code,
  287. 'close_note'=>$invoice->close_note,
  288. 'lines' => $linesresp,
  289. // 'lines' => array('0'=>array('id'=>222,'type'=>1),
  290. // '1'=>array('id'=>333,'type'=>1)),
  291. ));
  292. } else {
  293. $error++;
  294. $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
  295. }
  296. } else {
  297. $error++;
  298. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  299. }
  300. }
  301. if ($error) {
  302. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  303. }
  304. return $objectresp;
  305. }
  306. /**
  307. * Get list of invoices for third party
  308. *
  309. * @param array $authentication Array of authentication information
  310. * @param int $idthirdparty Id thirdparty
  311. * @return array Array result
  312. */
  313. function getSupplierInvoicesForThirdParty($authentication, $idthirdparty)
  314. {
  315. global $db, $conf;
  316. dol_syslog("Function: getSupplierInvoicesForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
  317. if ($authentication['entity']) {
  318. $conf->entity = $authentication['entity'];
  319. }
  320. // Init and check authentication
  321. $objectresp = array();
  322. $errorcode = ''; $errorlabel = '';
  323. $error = 0;
  324. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  325. // Check parameters
  326. if (!$error && empty($idthirdparty)) {
  327. $error++;
  328. $errorcode = 'BAD_PARAMETERS'; $errorlabel = 'Parameter id is not provided';
  329. }
  330. if (!$error) {
  331. $linesinvoice = array();
  332. $sql = "SELECT f.rowid as facid";
  333. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  334. $sql .= " WHERE f.entity = ".((int) $conf->entity);
  335. if ($idthirdparty != 'all') {
  336. $sql .= " AND f.fk_soc = ".((int) $idthirdparty);
  337. }
  338. $resql = $db->query($sql);
  339. if ($resql) {
  340. $num = $db->num_rows($resql);
  341. $i = 0;
  342. while ($i < $num) {
  343. // En attendant remplissage par boucle
  344. $obj = $db->fetch_object($resql);
  345. $invoice = new FactureFournisseur($db);
  346. $result = $invoice->fetch($obj->facid);
  347. if ($result < 0) {
  348. $error++;
  349. $errorcode = $result; $errorlabel = $invoice->error;
  350. break;
  351. }
  352. // Define lines of invoice
  353. $linesresp = array();
  354. foreach ($invoice->lines as $line) {
  355. $linesresp[] = array(
  356. 'id'=>$line->rowid,
  357. 'type'=>$line->product_type,
  358. 'desc'=>dol_htmlcleanlastbr($line->description),
  359. 'total_net'=>$line->total_ht,
  360. 'total_vat'=>$line->total_tva,
  361. 'total'=>$line->total_ttc,
  362. 'vat_rate'=>$line->tva_tx,
  363. 'qty'=>$line->qty,
  364. 'product_ref'=>$line->product_ref,
  365. 'product_label'=>$line->product_label,
  366. 'product_desc'=>$line->product_desc,
  367. );
  368. }
  369. // Now define invoice
  370. $linesinvoice[] = array(
  371. 'id'=>$invoice->id,
  372. 'ref'=>$invoice->ref,
  373. 'ref_supplier'=>$invoice->ref_supplier,
  374. 'ref_ext'=>$invoice->ref_ext,
  375. 'fk_user_author' => $invoice->fk_user_author,
  376. 'fk_user_valid' => $invoice->fk_user_valid,
  377. 'fk_thirdparty' => $invoice->fk_soc,
  378. 'type'=>$invoice->type,
  379. 'status'=>$invoice->statut,
  380. 'total_net'=>$invoice->total_ht,
  381. 'total_vat'=>$invoice->total_tva,
  382. 'total'=>$invoice->total_ttc,
  383. 'date_creation'=>dol_print_date($invoice->datec, 'dayhourrfc'),
  384. 'date_modification'=>dol_print_date($invoice->tms, 'dayhourrfc'),
  385. 'date_invoice'=>dol_print_date($invoice->date, 'dayhourrfc'),
  386. 'date_term'=>dol_print_date($invoice->date_echeance, 'dayhourrfc'),
  387. 'label'=>$invoice->label,
  388. 'paid'=>$invoice->paid,
  389. 'note_private'=>$invoice->note_private,
  390. 'note_public'=>$invoice->note_public,
  391. 'close_code'=>$invoice->close_code,
  392. 'close_note'=>$invoice->close_note,
  393. 'lines' => $linesresp
  394. );
  395. $i++;
  396. }
  397. $objectresp = array(
  398. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  399. 'invoices'=>$linesinvoice
  400. );
  401. } else {
  402. $error++;
  403. $errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
  404. }
  405. }
  406. if ($error) {
  407. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  408. }
  409. return $objectresp;
  410. }
  411. // Return the results.
  412. $server->service(file_get_contents("php://input"));