api_supplier_invoices.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  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. use Luracast\Restler\RestException;
  19. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  20. require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
  21. /**
  22. * API class for supplier invoices
  23. *
  24. * @property DoliDB db
  25. * @access protected
  26. * @class DolibarrApiAccess {@requires user,external}
  27. */
  28. class SupplierInvoices extends DolibarrApi
  29. {
  30. /**
  31. *
  32. * @var array $FIELDS Mandatory fields, checked when create and update object
  33. */
  34. static $FIELDS = array(
  35. 'socid',
  36. );
  37. /**
  38. * @var FactureFournisseur $invoice {@type FactureFournisseur}
  39. */
  40. public $invoice;
  41. /**
  42. * Constructor
  43. */
  44. public function __construct()
  45. {
  46. global $db;
  47. $this->db = $db;
  48. $this->invoice = new FactureFournisseur($this->db);
  49. }
  50. /**
  51. * Get properties of a supplier invoice object
  52. *
  53. * Return an array with supplier invoice information
  54. *
  55. * @param int $id ID of supplier invoice
  56. * @return array|mixed data without useless information
  57. *
  58. * @throws RestException
  59. */
  60. public function get($id)
  61. {
  62. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->lire) {
  63. throw new RestException(401);
  64. }
  65. $result = $this->invoice->fetch($id);
  66. if (!$result) {
  67. throw new RestException(404, 'Supplier invoice not found');
  68. }
  69. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  70. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  71. }
  72. $this->invoice->fetchObjectLinked();
  73. return $this->_cleanObjectDatas($this->invoice);
  74. }
  75. /**
  76. * List invoices
  77. *
  78. * Get a list of supplier invoices
  79. *
  80. * @param string $sortfield Sort field
  81. * @param string $sortorder Sort order
  82. * @param int $limit Limit for list
  83. * @param int $page Page number
  84. * @param string $thirdparty_ids Thirdparty ids to filter invoices of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i}
  85. * @param string $status Filter by invoice status : draft | unpaid | paid | cancelled
  86. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.datec:<:'20160101')"
  87. * @return array Array of invoice objects
  88. *
  89. * @throws RestException
  90. */
  91. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $status = '', $sqlfilters = '')
  92. {
  93. global $db;
  94. $obj_ret = array();
  95. // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
  96. $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
  97. // If the internal user must only see his customers, force searching by him
  98. $search_sale = 0;
  99. if (!DolibarrApiAccess::$user->rights->societe->client->voir) $search_sale = DolibarrApiAccess::$user->id;
  100. $sql = "SELECT t.rowid";
  101. // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
  102. if (!DolibarrApiAccess::$user->rights->societe->client->voir || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user";
  103. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as t";
  104. // We need this table joined to the select in order to filter by sale
  105. if (!DolibarrApiAccess::$user->rights->societe->client->voir || $search_sale > 0) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  106. $sql .= ' WHERE t.entity IN ('.getEntity('supplier_invoice').')';
  107. if (!DolibarrApiAccess::$user->rights->societe->client->voir || $search_sale > 0) $sql .= " AND t.fk_soc = sc.fk_soc";
  108. if ($socids) $sql .= " AND t.fk_soc IN (".$socids.")";
  109. if ($search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  110. // Filter by status
  111. if ($status == 'draft') {
  112. $sql .= " AND t.fk_statut IN (0)";
  113. }
  114. if ($status == 'unpaid') {
  115. $sql .= " AND t.fk_statut IN (1)";
  116. }
  117. if ($status == 'paid') {
  118. $sql .= " AND t.fk_statut IN (2)";
  119. }
  120. if ($status == 'cancelled') {
  121. $sql .= " AND t.fk_statut IN (3)";
  122. }
  123. // Insert sale filter
  124. if ($search_sale > 0) {
  125. $sql .= " AND sc.fk_user = ".$search_sale;
  126. }
  127. // Add sql filters
  128. if ($sqlfilters)
  129. {
  130. if (!DolibarrApi::_checkFilters($sqlfilters))
  131. {
  132. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  133. }
  134. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  135. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  136. }
  137. $sql .= $this->db->order($sortfield, $sortorder);
  138. if ($limit) {
  139. if ($page < 0)
  140. {
  141. $page = 0;
  142. }
  143. $offset = $limit * $page;
  144. $sql .= $this->db->plimit($limit + 1, $offset);
  145. }
  146. $result = $this->db->query($sql);
  147. if ($result) {
  148. $i = 0;
  149. $num = $this->db->num_rows($result);
  150. $min = min($num, ($limit <= 0 ? $num : $limit));
  151. while ($i < $min)
  152. {
  153. $obj = $this->db->fetch_object($result);
  154. $invoice_static = new FactureFournisseur($this->db);
  155. if ($invoice_static->fetch($obj->rowid)) {
  156. $obj_ret[] = $this->_cleanObjectDatas($invoice_static);
  157. }
  158. $i++;
  159. }
  160. } else {
  161. throw new RestException(503, 'Error when retrieve supplier invoice list : '.$this->db->lasterror());
  162. }
  163. if (!count($obj_ret)) {
  164. throw new RestException(404, 'No supplier invoice found');
  165. }
  166. return $obj_ret;
  167. }
  168. /**
  169. * Create supplier invoice object
  170. *
  171. * @param array $request_data Request datas
  172. *
  173. * @return int ID of supplier invoice
  174. *
  175. * @throws RestException 401
  176. * @throws RestException 500
  177. */
  178. public function post($request_data = null)
  179. {
  180. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
  181. throw new RestException(401, "Insuffisant rights");
  182. }
  183. // Check mandatory fields
  184. $result = $this->_validate($request_data);
  185. foreach ($request_data as $field => $value) {
  186. $this->invoice->$field = $value;
  187. }
  188. if (!array_key_exists('date', $request_data)) {
  189. $this->invoice->date = dol_now();
  190. }
  191. if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
  192. throw new RestException(500, "Error creating order", array_merge(array($this->invoice->error), $this->invoice->errors));
  193. }
  194. return $this->invoice->id;
  195. }
  196. /**
  197. * Update supplier invoice
  198. *
  199. * @param int $id Id of supplier invoice to update
  200. * @param array $request_data Datas
  201. *
  202. * @return int
  203. *
  204. * @throws RestException 401
  205. * @throws RestException 404
  206. */
  207. public function put($id, $request_data = null)
  208. {
  209. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
  210. throw new RestException(401);
  211. }
  212. $result = $this->invoice->fetch($id);
  213. if (!$result) {
  214. throw new RestException(404, 'Supplier invoice not found');
  215. }
  216. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  217. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  218. }
  219. foreach ($request_data as $field => $value) {
  220. if ($field == 'id') continue;
  221. $this->invoice->$field = $value;
  222. }
  223. if ($this->invoice->update($id, DolibarrApiAccess::$user))
  224. return $this->get($id);
  225. return false;
  226. }
  227. /**
  228. * Delete supplier invoice
  229. *
  230. * @param int $id Supplier invoice ID
  231. *
  232. * @return array
  233. *
  234. * @throws RestException 401
  235. * @throws RestException 404
  236. * @throws RestException 500
  237. */
  238. public function delete($id)
  239. {
  240. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->supprimer) {
  241. throw new RestException(401);
  242. }
  243. $result = $this->invoice->fetch($id);
  244. if (!$result) {
  245. throw new RestException(404, 'Supplier invoice not found');
  246. }
  247. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  248. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  249. }
  250. if ($this->invoice->delete(DolibarrApiAccess::$user) < 0)
  251. {
  252. throw new RestException(500);
  253. }
  254. return array(
  255. 'success' => array(
  256. 'code' => 200,
  257. 'message' => 'Supplier invoice deleted'
  258. )
  259. );
  260. }
  261. /**
  262. * Validate an invoice
  263. *
  264. * @param int $id Invoice ID
  265. * @param int $idwarehouse Warehouse ID
  266. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  267. *
  268. * @url POST {id}/validate
  269. *
  270. * @return array
  271. *
  272. * @throws RestException 304
  273. * @throws RestException 401
  274. * @throws RestException 404
  275. * @throws RestException 405
  276. * @throws RestException 500
  277. */
  278. public function validate($id, $idwarehouse = 0, $notrigger = 0)
  279. {
  280. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
  281. throw new RestException(401);
  282. }
  283. $result = $this->invoice->fetch($id);
  284. if (!$result) {
  285. throw new RestException(404, 'Invoice not found');
  286. }
  287. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  288. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  289. }
  290. $result = $this->invoice->validate(DolibarrApiAccess::$user, '', $idwarehouse, $notrigger);
  291. if ($result == 0) {
  292. throw new RestException(304, 'Error nothing done. The invoice is already validated');
  293. }
  294. if ($result < 0) {
  295. throw new RestException(500, 'Error when validating Invoice: '.$this->invoice->error);
  296. }
  297. return array(
  298. 'success' => array(
  299. 'code' => 200,
  300. 'message' => 'Invoice validated (Ref='.$this->invoice->ref.')'
  301. )
  302. );
  303. }
  304. /**
  305. * Get list of payments of a given supplier invoice
  306. *
  307. * @param int $id Id of SupplierInvoice
  308. *
  309. * @url GET {id}/payments
  310. *
  311. * @return array
  312. * @throws RestException 400
  313. * @throws RestException 401
  314. * @throws RestException 404
  315. * @throws RestException 405
  316. */
  317. public function getPayments($id)
  318. {
  319. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->lire) {
  320. throw new RestException(401);
  321. }
  322. if (empty($id)) {
  323. throw new RestException(400, 'Invoice ID is mandatory');
  324. }
  325. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  326. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  327. }
  328. $result = $this->invoice->fetch($id);
  329. if (!$result) {
  330. throw new RestException(404, 'Invoice not found');
  331. }
  332. $result = $this->invoice->getListOfPayments();
  333. if ($result < 0) {
  334. throw new RestException(405, $this->invoice->error);
  335. }
  336. return $result;
  337. }
  338. /**
  339. * Add payment line to a specific supplier invoice with the remain to pay as amount.
  340. *
  341. * @param int $id Id of invoice
  342. * @param string $datepaye {@from body} Payment date {@type timestamp}
  343. * @param int $payment_mode_id {@from body} Payment mode ID (look it up via REST GET to /setup/dictionary/payment_types) {@min 1}
  344. * @param string $closepaidinvoices {@from body} Close paid invoices {@choice yes,no}
  345. * @param int $accountid {@from body} Bank account ID (look it up via REST GET to /bankaccounts) {@min 1}
  346. * @param string $num_payment {@from body} Payment number (optional)
  347. * @param string $comment {@from body} Note (optional)
  348. * @param string $chqemetteur {@from body} Payment issuer (mandatory if payment_mode_id corresponds to 'CHQ'-payment type)
  349. * @param string $chqbank {@from body} Issuer bank name (optional)
  350. *
  351. * @url POST {id}/payments
  352. *
  353. * @return int Payment ID
  354. * @throws RestException 400
  355. * @throws RestException 401
  356. * @throws RestException 404
  357. */
  358. public function addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment = '', $comment = '', $chqemetteur = '', $chqbank = '')
  359. {
  360. global $conf;
  361. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
  362. throw new RestException(403);
  363. }
  364. if (empty($id)) {
  365. throw new RestException(400, 'Invoice ID is mandatory');
  366. }
  367. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  368. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  369. }
  370. if (!empty($conf->banque->enabled)) {
  371. if (empty($accountid)) {
  372. throw new RestException(400, 'Bank account ID is mandatory');
  373. }
  374. }
  375. if (empty($payment_mode_id)) {
  376. throw new RestException(400, 'Payment mode ID is mandatory');
  377. }
  378. $result = $this->invoice->fetch($id);
  379. if (!$result) {
  380. throw new RestException(404, 'Invoice not found');
  381. }
  382. // Calculate amount to pay
  383. $totalpaye = $this->invoice->getSommePaiement();
  384. $totaldeposits = $this->invoice->getSumDepositsUsed();
  385. $resteapayer = price2num($this->invoice->total_ttc - $totalpaye - $totaldeposits, 'MT');
  386. $this->db->begin();
  387. $amounts = array();
  388. $multicurrency_amounts = array();
  389. $resteapayer = price2num($resteapayer, 'MT');
  390. $amounts[$id] = $resteapayer;
  391. // Multicurrency
  392. $newvalue = price2num($this->invoice->multicurrency_total_ttc, 'MT');
  393. $multicurrency_amounts[$id] = $newvalue;
  394. // Creation of payment line
  395. $paiement = new PaiementFourn($this->db);
  396. $paiement->datepaye = $datepaye;
  397. $paiement->amounts = $amounts; // Array with all payments dispatching with invoice id
  398. $paiement->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching
  399. $paiement->paiementid = $payment_mode_id;
  400. $paiement->paiementcode = dol_getIdFromCode($this->db, $payment_mode_id, 'c_paiement', 'id', 'code', 1);
  401. $paiement->oper = $paiement->paiementcode; // For backward compatibility
  402. $paiement->num_payment = $num_payment;
  403. $paiement->note_public = $comment;
  404. $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices == 'yes' ? 1 : 0)); // This include closing invoices
  405. if ($paiement_id < 0)
  406. {
  407. $this->db->rollback();
  408. throw new RestException(400, 'Payment error : '.$paiement->error);
  409. }
  410. if (!empty($conf->banque->enabled)) {
  411. $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user, 'payment_supplier', '(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
  412. if ($result < 0)
  413. {
  414. $this->db->rollback();
  415. throw new RestException(400, 'Add payment to bank error : '.$paiement->error);
  416. }
  417. }
  418. $this->db->commit();
  419. return $paiement_id;
  420. }
  421. /**
  422. * Get lines of a supplier invoice
  423. *
  424. * @param int $id Id of supplier invoice
  425. *
  426. * @url GET {id}/lines
  427. *
  428. * @return array
  429. */
  430. public function getLines($id)
  431. {
  432. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
  433. throw new RestException(401);
  434. }
  435. $result = $this->invoice->fetch($id);
  436. if (!$result) {
  437. throw new RestException(404, 'Supplier invoice not found');
  438. }
  439. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  440. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  441. }
  442. $this->invoice->fetch_lines();
  443. $result = array();
  444. foreach ($this->invoice->lines as $line) {
  445. array_push($result, $this->_cleanObjectDatas($line));
  446. }
  447. return $result;
  448. }
  449. /**
  450. * Add a line to given supplier invoice
  451. *
  452. * @param int $id Id of supplier invoice to update
  453. * @param array $request_data supplier invoice line data
  454. *
  455. * @url POST {id}/lines
  456. *
  457. * @return int|bool
  458. */
  459. public function postLine($id, $request_data = null)
  460. {
  461. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
  462. throw new RestException(401);
  463. }
  464. $result = $this->invoice->fetch($id);
  465. if (!$result) {
  466. throw new RestException(404, 'Supplier invoice not found');
  467. }
  468. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  469. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  470. }
  471. $request_data = (object) $request_data;
  472. $updateRes = $this->invoice->addline(
  473. $request_data->description,
  474. $request_data->pu_ht,
  475. $request_data->tva_tx,
  476. $request_data->localtax1_tx,
  477. $request_data->localtax2_tx,
  478. $request_data->qty,
  479. $request_data->fk_product,
  480. $request_data->remise_percent,
  481. $request_data->date_start,
  482. $request_data->date_end,
  483. $request_data->ventil,
  484. $request_data->info_bits,
  485. 'HT',
  486. $request_data->product_type,
  487. $request_data->rang,
  488. false,
  489. $request_data->array_options,
  490. $request_data->fk_unit,
  491. $request_data->origin_id,
  492. $request_data->multicurrency_subprice,
  493. $request_data->ref_supplier,
  494. $request_data->special_code
  495. );
  496. if ($updateRes < 0) {
  497. throw new RestException(400, 'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
  498. }
  499. return $updateRes;
  500. }
  501. /**
  502. * Update a line to a given supplier invoice
  503. *
  504. * @param int $id Id of supplier invoice to update
  505. * @param int $lineid Id of line to update
  506. * @param array $request_data InvoiceLine data
  507. *
  508. * @url PUT {id}/lines/{lineid}
  509. *
  510. * @return object
  511. *
  512. * @throws RestException 401 Not allowed
  513. * @throws RestException 404 Not found
  514. * @throws RestException 304 Error
  515. */
  516. public function putLine($id, $lineid, $request_data = null)
  517. {
  518. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
  519. throw new RestException(401);
  520. }
  521. $result = $this->invoice->fetch($id);
  522. if (!$result) {
  523. throw new RestException(404, 'Supplier invoice not found');
  524. }
  525. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  526. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  527. }
  528. $request_data = (object) $request_data;
  529. $updateRes = $this->invoice->updateline(
  530. $lineid,
  531. $request_data->description,
  532. $request_data->pu_ht,
  533. $request_data->tva_tx,
  534. $request_data->localtax1_tx,
  535. $request_data->localtax2_tx,
  536. $request_data->qty,
  537. $request_data->fk_product,
  538. 'HT',
  539. $request_data->info_bits,
  540. $request_data->product_type,
  541. $request_data->remise_percent,
  542. false,
  543. $request_data->date_start,
  544. $request_data->date_end,
  545. $request_data->array_options,
  546. $request_data->fk_unit,
  547. $request_data->multicurrency_subprice,
  548. $request_data->ref_supplier
  549. );
  550. if ($updateRes > 0) {
  551. $result = $this->get($id);
  552. unset($result->line);
  553. return $this->_cleanObjectDatas($result);
  554. } else {
  555. throw new RestException(304, $this->invoice->error);
  556. }
  557. }
  558. /**
  559. * Deletes a line of a given supplier invoice
  560. *
  561. * @param int $id Id of supplier invoice
  562. * @param int $lineid Id of the line to delete
  563. *
  564. * @url DELETE {id}/lines/{lineid}
  565. *
  566. * @return array
  567. *
  568. * @throws RestException 400 Bad parameters
  569. * @throws RestException 401 Not allowed
  570. * @throws RestException 404 Not found
  571. * @throws RestException 405 Error
  572. */
  573. public function deleteLine($id, $lineid)
  574. {
  575. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
  576. throw new RestException(401);
  577. }
  578. $result = $this->invoice->fetch($id);
  579. if (!$result) {
  580. throw new RestException(404, 'Supplier invoice not found');
  581. }
  582. if (empty($lineid)) {
  583. throw new RestException(400, 'Line ID is mandatory');
  584. }
  585. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  586. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  587. }
  588. // TODO Check the lineid $lineid is a line of ojbect
  589. $updateRes = $this->invoice->deleteline($lineid);
  590. if ($updateRes > 0) {
  591. return $this->get($id);
  592. } else {
  593. throw new RestException(405, $this->invoice->error);
  594. }
  595. }
  596. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  597. /**
  598. * Clean sensible object datas
  599. *
  600. * @param Object $object Object to clean
  601. * @return array Array of cleaned object properties
  602. */
  603. protected function _cleanObjectDatas($object)
  604. {
  605. // phpcs:enable
  606. $object = parent::_cleanObjectDatas($object);
  607. unset($object->rowid);
  608. unset($object->barcode_type);
  609. unset($object->barcode_type_code);
  610. unset($object->barcode_type_label);
  611. unset($object->barcode_type_coder);
  612. return $object;
  613. }
  614. /**
  615. * Validate fields before create or update object
  616. *
  617. * @param array $data Datas to validate
  618. * @return array
  619. *
  620. * @throws RestException
  621. */
  622. private function _validate($data)
  623. {
  624. $invoice = array();
  625. foreach (SupplierInvoices::$FIELDS as $field) {
  626. if (!isset($data[$field]))
  627. throw new RestException(400, "$field field missing");
  628. $invoice[$field] = $data[$field];
  629. }
  630. return $invoice;
  631. }
  632. }