server_supplier_invoice.php 14 KB

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