api_supplier_invoices.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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->oper = $paiement->paiementcode; // For backward compatibility
  420. $paiement->num_payment = $num_payment;
  421. $paiement->note_public = $comment;
  422. $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices == 'yes' ? 1 : 0)); // This include closing invoices
  423. if ($paiement_id < 0) {
  424. $this->db->rollback();
  425. throw new RestException(400, 'Payment error : '.$paiement->error);
  426. }
  427. if (!empty($conf->banque->enabled)) {
  428. $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user, 'payment_supplier', '(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
  429. if ($result < 0) {
  430. $this->db->rollback();
  431. throw new RestException(400, 'Add payment to bank error : '.$paiement->error);
  432. }
  433. }
  434. $this->db->commit();
  435. return $paiement_id;
  436. }
  437. /**
  438. * Get lines of a supplier invoice
  439. *
  440. * @param int $id Id of supplier invoice
  441. *
  442. * @url GET {id}/lines
  443. *
  444. * @return array
  445. */
  446. public function getLines($id)
  447. {
  448. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
  449. throw new RestException(401);
  450. }
  451. $result = $this->invoice->fetch($id);
  452. if (!$result) {
  453. throw new RestException(404, 'Supplier invoice not found');
  454. }
  455. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  456. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  457. }
  458. $this->invoice->fetch_lines();
  459. $result = array();
  460. foreach ($this->invoice->lines as $line) {
  461. array_push($result, $this->_cleanObjectDatas($line));
  462. }
  463. return $result;
  464. }
  465. /**
  466. * Add a line to given supplier invoice
  467. *
  468. * Note: socid = dolibarr_order_id, pu_ht = net price, remise = discount
  469. *
  470. * 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}
  471. *
  472. * @param int $id Id of supplier invoice to update
  473. * @param array $request_data supplier invoice line data
  474. *
  475. * @url POST {id}/lines
  476. *
  477. * @return int|bool
  478. */
  479. public function postLine($id, $request_data = null)
  480. {
  481. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
  482. throw new RestException(401);
  483. }
  484. $result = $this->invoice->fetch($id);
  485. if (!$result) {
  486. throw new RestException(404, 'Supplier invoice not found');
  487. }
  488. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  489. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  490. }
  491. $request_data = (object) $request_data;
  492. $request_data->description = checkVal($request_data->description, 'restricthtml');
  493. $request_data->ref_supplier = checkVal($request_data->ref_supplier);
  494. $updateRes = $this->invoice->addline(
  495. $request_data->description,
  496. $request_data->pu_ht,
  497. $request_data->tva_tx,
  498. $request_data->localtax1_tx,
  499. $request_data->localtax2_tx,
  500. $request_data->qty,
  501. $request_data->fk_product,
  502. $request_data->remise_percent,
  503. $request_data->date_start,
  504. $request_data->date_end,
  505. $request_data->ventil,
  506. $request_data->info_bits,
  507. $request_data->price_base_type ? $request_data->price_base_type : 'HT',
  508. $request_data->product_type,
  509. $request_data->rang,
  510. false,
  511. $request_data->array_options,
  512. $request_data->fk_unit,
  513. $request_data->origin_id,
  514. $request_data->multicurrency_subprice,
  515. $request_data->ref_supplier,
  516. $request_data->special_code
  517. );
  518. if ($updateRes < 0) {
  519. throw new RestException(400, 'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
  520. }
  521. return $updateRes;
  522. }
  523. /**
  524. * Update a line to a given supplier invoice
  525. *
  526. * @param int $id Id of supplier invoice to update
  527. * @param int $lineid Id of line to update
  528. * @param array $request_data InvoiceLine data
  529. *
  530. * @url PUT {id}/lines/{lineid}
  531. *
  532. * @return object
  533. *
  534. * @throws RestException 401 Not allowed
  535. * @throws RestException 404 Not found
  536. * @throws RestException 304 Error
  537. */
  538. public function putLine($id, $lineid, $request_data = null)
  539. {
  540. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
  541. throw new RestException(401);
  542. }
  543. $result = $this->invoice->fetch($id);
  544. if (!$result) {
  545. throw new RestException(404, 'Supplier invoice not found');
  546. }
  547. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  548. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  549. }
  550. $request_data = (object) $request_data;
  551. $request_data->description = checkVal($request_data->description, 'restricthtml');
  552. $request_data->ref_supplier = checkVal($request_data->ref_supplier);
  553. $updateRes = $this->invoice->updateline(
  554. $lineid,
  555. $request_data->description,
  556. $request_data->pu_ht,
  557. $request_data->tva_tx,
  558. $request_data->localtax1_tx,
  559. $request_data->localtax2_tx,
  560. $request_data->qty,
  561. $request_data->fk_product,
  562. $request_data->price_base_type ? $request_data->price_base_type : 'HT',
  563. $request_data->info_bits,
  564. $request_data->product_type,
  565. $request_data->remise_percent,
  566. false,
  567. $request_data->date_start,
  568. $request_data->date_end,
  569. $request_data->array_options,
  570. $request_data->fk_unit,
  571. $request_data->multicurrency_subprice,
  572. $request_data->ref_supplier,
  573. $request_data->rang
  574. );
  575. if ($updateRes > 0) {
  576. $result = $this->get($id);
  577. unset($result->line);
  578. return $this->_cleanObjectDatas($result);
  579. } else {
  580. throw new RestException(304, $this->invoice->error);
  581. }
  582. }
  583. /**
  584. * Deletes a line of a given supplier invoice
  585. *
  586. * @param int $id Id of supplier invoice
  587. * @param int $lineid Id of the line to delete
  588. *
  589. * @url DELETE {id}/lines/{lineid}
  590. *
  591. * @return array
  592. *
  593. * @throws RestException 400 Bad parameters
  594. * @throws RestException 401 Not allowed
  595. * @throws RestException 404 Not found
  596. * @throws RestException 405 Error
  597. */
  598. public function deleteLine($id, $lineid)
  599. {
  600. if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
  601. throw new RestException(401);
  602. }
  603. $result = $this->invoice->fetch($id);
  604. if (!$result) {
  605. throw new RestException(404, 'Supplier invoice not found');
  606. }
  607. if (empty($lineid)) {
  608. throw new RestException(400, 'Line ID is mandatory');
  609. }
  610. if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
  611. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  612. }
  613. // TODO Check the lineid $lineid is a line of ojbect
  614. $updateRes = $this->invoice->deleteline($lineid);
  615. if ($updateRes > 0) {
  616. return $this->get($id);
  617. } else {
  618. throw new RestException(405, $this->invoice->error);
  619. }
  620. }
  621. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  622. /**
  623. * Clean sensible object datas
  624. *
  625. * @param Object $object Object to clean
  626. * @return Object Object with cleaned properties
  627. */
  628. protected function _cleanObjectDatas($object)
  629. {
  630. // phpcs:enable
  631. $object = parent::_cleanObjectDatas($object);
  632. unset($object->rowid);
  633. unset($object->barcode_type);
  634. unset($object->barcode_type_code);
  635. unset($object->barcode_type_label);
  636. unset($object->barcode_type_coder);
  637. return $object;
  638. }
  639. /**
  640. * Validate fields before create or update object
  641. *
  642. * @param array $data Datas to validate
  643. * @return array
  644. *
  645. * @throws RestException
  646. */
  647. private function _validate($data)
  648. {
  649. $invoice = array();
  650. foreach (SupplierInvoices::$FIELDS as $field) {
  651. if (!isset($data[$field])) {
  652. throw new RestException(400, "$field field missing");
  653. }
  654. $invoice[$field] = $data[$field];
  655. }
  656. return $invoice;
  657. }
  658. }