api_deprecated_invoice.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. use Luracast\Restler\RestException;
  18. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  19. /**
  20. * API class for invoice object
  21. *
  22. * @smart-auto-routing false
  23. * @access protected
  24. * @class DolibarrApiAccess {@requires user,external}
  25. * @deprecated Use Invoices instead (defined in api_invoices.class.php)
  26. */
  27. class InvoiceApi extends DolibarrApi
  28. {
  29. /**
  30. *
  31. * @var array $FIELDS Mandatory fields, checked when create and update object
  32. */
  33. static $FIELDS = array(
  34. 'socid'
  35. );
  36. /**
  37. * @var Facture $invoice {@type Facture}
  38. */
  39. public $invoice;
  40. /**
  41. * Constructor <b>Warning: Deprecated</b>
  42. *
  43. * @url GET invoice/
  44. *
  45. */
  46. function __construct()
  47. {
  48. global $db, $conf;
  49. $this->db = $db;
  50. $this->invoice = new Facture($this->db);
  51. }
  52. /**
  53. * Get properties of a invoice object <b>Warning: Deprecated</b>
  54. *
  55. * Return an array with invoice informations
  56. *
  57. * @param int $id ID of invoice
  58. * @return array|mixed data without useless information
  59. *
  60. * @url GET invoice/{id}
  61. * @throws RestException
  62. */
  63. function get($id)
  64. {
  65. if(! DolibarrApiAccess::$user->rights->facture->lire) {
  66. throw new RestException(401);
  67. }
  68. $result = $this->invoice->fetch($id);
  69. if( ! $result ) {
  70. throw new RestException(404, 'Facture not found');
  71. }
  72. if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
  73. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  74. }
  75. return $this->_cleanObjectDatas($this->invoice);
  76. }
  77. /**
  78. * List invoices <b>Warning: Deprecated</b>
  79. *
  80. * Get a list of invoices
  81. *
  82. * @param string $sortfield Sort field
  83. * @param string $sortorder Sort order
  84. * @param int $limit Limit for list
  85. * @param int $page Page number
  86. * @param int $socid Filter list with thirdparty ID
  87. * @param string $mode Filter by invoice status : draft | unpaid | paid | cancelled
  88. *
  89. * @return array Array of invoice objects
  90. *
  91. * @url GET invoice/list
  92. * @url GET invoice/list/{mode}
  93. * @url GET thirdparty/{socid}/invoice/list
  94. * @url GET thirdparty/{socid}/invoice/list/{mode}
  95. */
  96. function getList($sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $socid=0, $mode='') {
  97. global $db, $conf;
  98. $obj_ret = array();
  99. $socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $socid;
  100. // If the internal user must only see his customers, force searching by him
  101. if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
  102. $sql = "SELECT s.rowid";
  103. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
  104. $sql.= " FROM ".MAIN_DB_PREFIX."facture as s";
  105. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  106. $sql.= ' WHERE s.entity IN ('.getEntity('facture', 1).')';
  107. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND s.fk_soc = sc.fk_soc";
  108. if ($socid) $sql.= " AND s.fk_soc = ".$socid;
  109. if ($search_sale > 0) $sql.= " AND s.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  110. // Example of use $mode
  111. if ($mode == 'draft') $sql.= " AND s.fk_statut IN (0)";
  112. if ($mode == 'unpaid') $sql.= " AND s.fk_statut IN (1)";
  113. if ($mode == 'paid') $sql.= " AND s.fk_statut IN (2)";
  114. if ($mode == 'cancelled') $sql.= " AND s.fk_statut IN (3)";
  115. // Insert sale filter
  116. if ($search_sale > 0)
  117. {
  118. $sql .= " AND sc.fk_user = ".$search_sale;
  119. }
  120. $nbtotalofrecords = -1;
  121. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  122. {
  123. $result = $db->query($sql);
  124. $nbtotalofrecords = $db->num_rows($result);
  125. }
  126. $sql.= $db->order($sortfield, $sortorder);
  127. if ($limit) {
  128. if ($page < 0)
  129. {
  130. $page = 0;
  131. }
  132. $offset = $limit * $page;
  133. $sql.= $db->plimit($limit + 1, $offset);
  134. }
  135. $result = $db->query($sql);
  136. if ($result)
  137. {
  138. $i=0;
  139. $num = $db->num_rows($result);
  140. while ($i < min($num, ($limit <= 0 ? $num : $limit)))
  141. {
  142. $obj = $db->fetch_object($result);
  143. $invoice_static = new Facture($db);
  144. if($invoice_static->fetch($obj->rowid)) {
  145. $obj_ret[] = parent::_cleanObjectDatas($invoice_static);
  146. }
  147. $i++;
  148. }
  149. }
  150. else {
  151. throw new RestException(503, 'Error when retrieve invoice list');
  152. }
  153. if( ! count($obj_ret)) {
  154. throw new RestException(404, 'No invoice found');
  155. }
  156. return $obj_ret;
  157. }
  158. /**
  159. * Create invoice object <b>Warning: Deprecated</b>
  160. *
  161. * @param array $request_data Request datas
  162. * @return int ID of invoice
  163. *
  164. * @url POST invoice/
  165. */
  166. function post($request_data = NULL)
  167. {
  168. if(! DolibarrApiAccess::$user->rights->facture->creer) {
  169. throw new RestException(401);
  170. }
  171. // Check mandatory fields
  172. $result = $this->_validate($request_data);
  173. foreach($request_data as $field => $value) {
  174. $this->invoice->$field = $value;
  175. }
  176. if(! array_keys($request_data,'date')) {
  177. $this->invoice->date = dol_now();
  178. }
  179. if( ! $this->invoice->create(DolibarrApiAccess::$user)) {
  180. throw new RestException(500);
  181. }
  182. return $this->invoice->id;
  183. }
  184. /**
  185. * Update invoice <b>Warning: Deprecated</b>
  186. *
  187. * @param int $id Id of invoice to update
  188. * @param array $request_data Datas
  189. * @return int
  190. *
  191. * @url PUT invoice/{id}
  192. */
  193. function put($id, $request_data = NULL)
  194. {
  195. if(! DolibarrApiAccess::$user->rights->facture->creer) {
  196. throw new RestException(401);
  197. }
  198. $result = $this->invoice->fetch($id);
  199. if( ! $result ) {
  200. throw new RestException(404, 'Facture not found');
  201. }
  202. if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
  203. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  204. }
  205. foreach($request_data as $field => $value) {
  206. if ($field == 'id') continue;
  207. $this->invoice->$field = $value;
  208. }
  209. if($this->invoice->update($id, DolibarrApiAccess::$user))
  210. return $this->get ($id);
  211. return false;
  212. }
  213. /**
  214. * Delete invoice <b>Warning: Deprecated</b>
  215. *
  216. * @param int $id Invoice ID
  217. * @return type
  218. *
  219. * @url DELETE invoice/{id}
  220. */
  221. function delete($id)
  222. {
  223. if(! DolibarrApiAccess::$user->rights->facture->supprimer) {
  224. throw new RestException(401);
  225. }
  226. $result = $this->invoice->fetch($id);
  227. if( ! $result ) {
  228. throw new RestException(404, 'Facture not found');
  229. }
  230. if( ! DolibarrApi::_checkAccessToResource('facture',$this->facture->id)) {
  231. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  232. }
  233. if( !$this->invoice->delete(DolibarrApiAccess::$user))
  234. {
  235. throw new RestException(500);
  236. }
  237. return array(
  238. 'success' => array(
  239. 'code' => 200,
  240. 'message' => 'Facture deleted'
  241. )
  242. );
  243. }
  244. /**
  245. * Validate fields before create or update object
  246. *
  247. * @param array $data Datas to validate
  248. * @return array
  249. *
  250. * @throws RestException
  251. */
  252. function _validate($data)
  253. {
  254. $invoice = array();
  255. foreach (InvoiceApi::$FIELDS as $field) {
  256. if (!isset($data[$field]))
  257. throw new RestException(400, "$field field missing");
  258. $invoice[$field] = $data[$field];
  259. }
  260. return $invoice;
  261. }
  262. }