api_supplier_invoices.class.php 22 KB

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