server_supplier_invoice.php 15 KB

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