api_supplier_invoices.class.php 22 KB

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