Passed
Branch develop (944e2a)
by
unknown
27:19
created

Thirdparties::_fetch()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 22
c 0
b 0
f 0
nc 9
nop 12
dl 0
loc 33
rs 8.9457

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/* Copyright (C) 2015   Jean-François Ferry     <[email protected]>
3
 * Copyright (C) 2018   Pierre Chéné            <[email protected]>
4
 * Copyright (C) 2019   Cedric Ancelin          <[email protected]>
5
 * Copyright (C) 2020       Frédéric France     <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
use Luracast\Restler\RestException;
22
23
24
/**
25
 * API class for thirdparties
26
 *
27
 * @access protected
28
 * @class  DolibarrApiAccess {@requires user,external}
29
 *
30
 */
31
class Thirdparties extends DolibarrApi
32
{
33
	/**
34
	 *
35
	 * @var array   $FIELDS     Mandatory fields, checked when create and update object
36
	 */
37
	static $FIELDS = array(
38
		'name'
39
	);
40
41
	/**
42
	 * @var Societe $company {@type Societe}
43
	 */
44
	public $company;
45
46
	/**
47
	 * Constructor
48
	 */
49
	public function __construct()
50
	{
51
		global $db, $conf;
52
		$this->db = $db;
53
54
		require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
55
		require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
56
		require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
57
		require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
58
59
		$this->company = new Societe($this->db);
60
61
		if (!empty($conf->global->SOCIETE_EMAIL_MANDATORY)) {
62
			static::$FIELDS[] = 'email';
63
		}
64
	}
65
66
	/**
67
	 * Get properties of a thirdparty object
68
	 *
69
	 * Return an array with thirdparty informations
70
	 *
71
	 * @param 	int 	$id Id of third party to load
72
	 * @return 	Object Cleaned Societe object
73
	 *
74
	 * @throws 	RestException
75
	 */
76
	public function get($id)
77
	{
78
		return $this->_fetch($id);
79
	}
80
81
	/**
82
	 * Get properties of a thirdparty object by email.
83
	 *
84
	 * Return an array with thirdparty informations
85
	 *
86
	 * @param string    $email  Email of third party to load
87
	 * @return Object Cleaned Societe object
88
	 *
89
	 * @url     GET email/{email}
90
	 *
91
	 * @throws RestException
92
	 */
93
	public function getByEmail($email)
94
	{
95
		return $this->_fetch('', '', '', '', '', '', '', '', '', '', $email);
96
	}
97
98
	/**
99
	 * Get properties of a thirdparty object by barcode.
100
	 *
101
	 * Return an array with thirdparty informations
102
	 *
103
	 * @param string    $barcode  Barcode of third party to load
104
	 * @return Object Cleaned Societe object
105
	 *
106
	 * @url     GET barcode/{barcode}
107
	 *
108
	 * @throws RestException
109
	 */
110
	public function getByBarcode($barcode)
111
	{
112
		return $this->_fetch('', '', '', $barcode);
113
	}
114
115
	/**
116
	 * List thirdparties
117
	 *
118
	 * Get a list of thirdparties
119
	 *
120
	 * @param   string  $sortfield  Sort field
121
	 * @param   string  $sortorder  Sort order
122
	 * @param   int     $limit      Limit for list
123
	 * @param   int     $page       Page number
124
	 * @param   int     $mode       Set to 1 to show only customers
125
	 *                              Set to 2 to show only prospects
126
	 *                              Set to 3 to show only those are not customer neither prospect
127
	 *								Set to 4 to show only suppliers
128
	 * @param  int    $category   Use this param to filter list by category
129
	 * @param   string  $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.nom:like:'TheCompany%') and (t.date_creation:<:'20160101')"
130
	 * @return  array               Array of thirdparty objects
131
	 */
132
	public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '')
133
	{
134
		$obj_ret = array();
135
136
		// case of external user, we force socids
137
		$socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
138
139
		// If the internal user must only see his customers, force searching by him
140
		$search_sale = 0;
141
		if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
142
143
		$sql = "SELECT t.rowid";
144
		if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $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)
145
		$sql .= " FROM ".MAIN_DB_PREFIX."societe as t";
146
		if ($category > 0) {
147
			if ($mode != 4) $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c";
148
			if (!in_array($mode, array(1, 2, 3))) $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as cc";
149
		}
150
		if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $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
151
		$sql .= ", ".MAIN_DB_PREFIX."c_stcomm as st";
152
		$sql .= " WHERE t.entity IN (".getEntity('societe').")";
153
		$sql .= " AND t.fk_stcomm = st.id";
154
155
		if ($mode == 1) $sql .= " AND t.client IN (1, 3)";
156
		elseif ($mode == 2) $sql .= " AND t.client IN (2, 3)";
157
		elseif ($mode == 3) $sql .= " AND t.client IN (0)";
158
		elseif ($mode == 4) $sql .= " AND t.fournisseur IN (1)";
159
160
		// Select thirdparties of given category
161
		if ($category > 0) {
162
			if (!empty($mode) && $mode != 4) { $sql .= " AND c.fk_categorie = ".$this->db->escape($category)." AND c.fk_soc = t.rowid"; }
163
			elseif (!empty($mode) && $mode == 4) { $sql .= " AND cc.fk_categorie = ".$this->db->escape($category)." AND cc.fk_soc = t.rowid"; }
164
			else { $sql .= " AND ((c.fk_categorie = ".$this->db->escape($category)." AND c.fk_soc = t.rowid) OR (cc.fk_categorie = ".$this->db->escape($category)." AND cc.fk_soc = t.rowid))"; }
165
		}
166
167
		if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc";
168
		//if ($email != NULL) $sql.= " AND s.email = \"".$email."\"";
169
		if ($socids) $sql .= " AND t.rowid IN (".$socids.")";
170
		if ($search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
171
		// Insert sale filter
172
		if ($search_sale > 0)
173
		{
174
			$sql .= " AND sc.fk_user = ".$search_sale;
175
		}
176
		// Add sql filters
177
		if ($sqlfilters)
178
		{
179
			if (!DolibarrApi::_checkFilters($sqlfilters))
180
			{
181
				throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
182
			}
183
			$regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
184
			$sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
185
		}
186
187
		$sql .= $this->db->order($sortfield, $sortorder);
188
189
		if ($limit) {
190
			if ($page < 0) {
191
				$page = 0;
192
			}
193
			$offset = $limit * $page;
194
195
			$sql .= $this->db->plimit($limit + 1, $offset);
196
		}
197
198
		$result = $this->db->query($sql);
199
		if ($result)
200
		{
201
			$num = $this->db->num_rows($result);
202
			$min = min($num, ($limit <= 0 ? $num : $limit));
203
			$i = 0;
204
			while ($i < $min)
205
			{
206
				$obj = $this->db->fetch_object($result);
207
				$soc_static = new Societe($this->db);
208
				if ($soc_static->fetch($obj->rowid)) {
209
					$obj_ret[] = $this->_cleanObjectDatas($soc_static);
210
				}
211
				$i++;
212
			}
213
		} else {
214
			throw new RestException(503, 'Error when retrieve thirdparties : '.$this->db->lasterror());
215
		}
216
		if (!count($obj_ret)) {
217
			throw new RestException(404, 'Thirdparties not found');
218
		}
219
		return $obj_ret;
220
	}
221
222
	/**
223
	 * Create thirdparty object
224
	 *
225
	 * @param array $request_data   Request datas
226
	 * @return int  ID of thirdparty
227
	 */
228
	public function post($request_data = null)
229
	{
230
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
231
			throw new RestException(401);
232
		}
233
		// Check mandatory fields
234
		$result = $this->_validate($request_data);
235
236
		foreach ($request_data as $field => $value) {
237
			$this->company->$field = $value;
238
		}
239
		if ($this->company->create(DolibarrApiAccess::$user) < 0)
240
			throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
241
242
		return $this->company->id;
243
	}
244
245
	/**
246
	 * Update thirdparty
247
	 *
248
	 * @param int   $id             Id of thirdparty to update
249
	 * @param array $request_data   Datas
250
	 * @return Object|boolean
251
	 */
252
	public function put($id, $request_data = null)
253
	{
254
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
255
			throw new RestException(401);
256
		}
257
258
		$result = $this->company->fetch($id);
259
		if (!$result) {
260
			throw new RestException(404, 'Thirdparty not found');
261
		}
262
263
		if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
264
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
265
		}
266
267
		foreach ($request_data as $field => $value) {
268
			if ($field == 'id') continue;
269
			$this->company->$field = $value;
270
		}
271
272
		if ($this->company->update($id, DolibarrApiAccess::$user, 1, '', '', 'update')) {
273
			return $this->get($id);
274
		}
275
276
		return false;
277
	}
278
279
	/**
280
	 * Merge a thirdparty into another one.
281
	 *
282
	 * Merge content (properties, notes) and objects (like invoices, events, orders, proposals, ...) of a thirdparty into a target thirdparty,
283
	 * then delete the merged thirdparty.
284
	 * If a property has a defined value both in thirdparty to delete and thirdparty to keep, the value into the thirdparty to
285
	 * delete will be ignored, the value of target thirdparty will remain, except for notes (content is concatenated).
286
	 *
287
	 * @param int   $id             ID of thirdparty to keep (the target thirdparty)
288
	 * @param int   $idtodelete     ID of thirdparty to remove (the thirdparty to delete), once data has been merged into the target thirdparty.
289
	 * @return int
290
	 *
291
	 * @url PUT {id}/merge/{idtodelete}
292
	 */
293
	public function merge($id, $idtodelete)
294
	{
295
		global $hookmanager;
296
297
		$error = 0;
298
299
		if ($id == $idtodelete)
300
		{
301
			throw new RestException(400, 'Try to merge a thirdparty into itself');
302
		}
303
304
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
305
			throw new RestException(401);
306
		}
307
308
		$result = $this->company->fetch($id); // include the fetch of extra fields
309
		if (!$result) {
310
			throw new RestException(404, 'Thirdparty not found');
311
		}
312
313
		if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
314
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
315
		}
316
317
		$this->companytoremove = new Societe($this->db);
318
319
		$result = $this->companytoremove->fetch($idtodelete); // include the fetch of extra fields
320
		if (!$result) {
321
			throw new RestException(404, 'Thirdparty not found');
322
		}
323
324
		if (!DolibarrApi::_checkAccessToResource('societe', $this->companytoremove->id)) {
325
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
326
		}
327
328
		$soc_origin = $this->companytoremove;
329
		$object = $this->company;
330
		$user = DolibarrApiAccess::$user;
331
332
333
		// Call same code than into action 'confirm_merge'
334
335
336
		$this->db->begin();
337
338
		// Recopy some data
339
		$object->client = $object->client | $soc_origin->client;
340
		$object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
341
		$listofproperties = array(
342
			'address', 'zip', 'town', 'state_id', 'country_id', 'phone', 'phone_pro', 'fax', 'email', 'skype', 'url', 'barcode',
343
			'idprof1', 'idprof2', 'idprof3', 'idprof4', 'idprof5', 'idprof6',
344
			'tva_intra', 'effectif_id', 'forme_juridique', 'remise_percent', 'remise_supplier_percent', 'mode_reglement_supplier_id', 'cond_reglement_supplier_id', 'name_bis',
345
			'stcomm_id', 'outstanding_limit', 'price_level', 'parent', 'default_lang', 'ref', 'ref_ext', 'import_key', 'fk_incoterms', 'fk_multicurrency',
346
			'code_client', 'code_fournisseur', 'code_compta', 'code_compta_fournisseur',
347
			'model_pdf', 'fk_projet'
348
		);
349
		foreach ($listofproperties as $property)
350
		{
351
			if (empty($object->$property)) $object->$property = $soc_origin->$property;
352
		}
353
354
		// Concat some data
355
		$listofproperties = array(
356
			'note_public', 'note_private'
357
		);
358
		foreach ($listofproperties as $property)
359
		{
360
			$object->$property = dol_concatdesc($object->$property, $soc_origin->$property);
361
		}
362
363
		// Merge extrafields
364
		if (is_array($soc_origin->array_options))
365
		{
366
			foreach ($soc_origin->array_options as $key => $val)
367
			{
368
				if (empty($object->array_options[$key])) $object->array_options[$key] = $val;
369
			}
370
		}
371
372
		// Merge categories
373
		$static_cat = new Categorie($this->db);
374
		$custcats = $static_cat->containing($soc_origin->id, 'customer', 'id');
375
		$object->setCategories($custcats, 'customer');
376
		$suppcats = $static_cat->containing($soc_origin->id, 'supplier', 'id');
377
		$object->setCategories($suppcats, 'supplier');
378
379
		// If thirdparty has a new code that is same than origin, we clean origin code to avoid duplicate key from database unique keys.
380
		if ($soc_origin->code_client == $object->code_client
381
			|| $soc_origin->code_fournisseur == $object->code_fournisseur
382
			|| $soc_origin->barcode == $object->barcode)
383
		{
384
			dol_syslog("We clean customer and supplier code so we will be able to make the update of target");
385
			$soc_origin->code_client = '';
386
			$soc_origin->code_fournisseur = '';
387
			$soc_origin->barcode = '';
388
			$soc_origin->update($soc_origin->id, $user, 0, 1, 1, 'merge');
389
		}
390
391
		// Update
392
		$result = $object->update($object->id, $user, 0, 1, 1, 'merge');
393
		if ($result < 0)
394
		{
395
			$error++;
396
		}
397
398
		// Move links
399
		if (!$error) {
400
			// This list is also into the societe/card.php file
401
			// TODO Mutualise the list into object societe.class.php
402
			$objects = array(
403
				'Adherent' => '/adherents/class/adherent.class.php',
404
				'Societe' => '/societe/class/societe.class.php',
405
				'Categorie' => '/categories/class/categorie.class.php',
406
				'ActionComm' => '/comm/action/class/actioncomm.class.php',
407
				'Propal' => '/comm/propal/class/propal.class.php',
408
				'Commande' => '/commande/class/commande.class.php',
409
				'Facture' => '/compta/facture/class/facture.class.php',
410
				'FactureRec' => '/compta/facture/class/facture-rec.class.php',
411
				'LignePrelevement' => '/compta/prelevement/class/ligneprelevement.class.php',
412
				'Mo' => '/mrp/class/mo.class.php',
413
				'Contact' => '/contact/class/contact.class.php',
414
				'Contrat' => '/contrat/class/contrat.class.php',
415
				'Expedition' => '/expedition/class/expedition.class.php',
416
				'Fichinter' => '/fichinter/class/fichinter.class.php',
417
				'CommandeFournisseur' => '/fourn/class/fournisseur.commande.class.php',
418
				'FactureFournisseur' => '/fourn/class/fournisseur.facture.class.php',
419
				'SupplierProposal' => '/supplier_proposal/class/supplier_proposal.class.php',
420
				'ProductFournisseur' => '/fourn/class/fournisseur.product.class.php',
421
				'Livraison' => '/delivery/class/delivery.class.php',
422
				'Product' => '/product/class/product.class.php',
423
				'Project' => '/projet/class/project.class.php',
424
				'Ticket' => '/ticket/class/ticket.class.php',
425
				'User' => '/user/class/user.class.php'
426
			);
427
428
			//First, all core objects must update their tables
429
			foreach ($objects as $object_name => $object_file)
430
			{
431
				require_once DOL_DOCUMENT_ROOT.$object_file;
432
433
				if (!$error && !$object_name::replaceThirdparty($this->db, $soc_origin->id, $object->id)) {
434
					$error++;
435
					//setEventMessages($this->db->lasterror(), null, 'errors');
436
				}
437
			}
438
		}
439
440
		// External modules should update their ones too
441
		if (!$error) {
442
			$reshook = $hookmanager->executeHooks('replaceThirdparty', array(
443
				'soc_origin' => $soc_origin->id,
444
				'soc_dest' => $object->id
445
			), $soc_dest, $action);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $soc_dest seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $action seems to be never defined.
Loading history...
446
447
			if ($reshook < 0) {
448
				//setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
449
				$error++;
450
			}
451
		}
452
453
454
		if (!$error) {
455
			$object->context = array('merge'=>1, 'mergefromid'=>$soc_origin->id);
456
457
			// Call trigger
458
			$result = $object->call_trigger('COMPANY_MODIFY', $user);
459
			if ($result < 0) {
460
				//setEventMessages($object->error, $object->errors, 'errors');
461
				$error++;
462
			}
463
			// End call triggers
464
		}
465
466
		if (!$error) {
467
			//We finally remove the old thirdparty
468
			if ($soc_origin->delete($soc_origin->id, $user) < 1) {
469
				$error++;
470
			}
471
		}
472
473
		// End of merge
474
475
		if ($error) {
476
			$this->db->rollback();
477
478
			throw new RestException(500, 'Error failed to merged thirdparty '.$this->companytoremove->id.' into '.$id.'. Enable and read log file for more information.');
479
		} else {
480
			$this->db->commit();
481
		}
482
483
		return $this->get($id);
484
	}
485
486
	/**
487
	 * Delete thirdparty
488
	 *
489
	 * @param int $id   Thirparty ID
490
	 * @return integer
491
	 */
492
	public function delete($id)
493
	{
494
		if (!DolibarrApiAccess::$user->rights->societe->supprimer) {
495
			throw new RestException(401);
496
		}
497
		$result = $this->company->fetch($id);
498
		if (!$result) {
499
			throw new RestException(404, 'Thirdparty not found');
500
		}
501
		if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
502
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
503
		}
504
		$this->company->oldcopy = clone $this->company;
505
		return $this->company->delete($id);
506
	}
507
508
	/**
509
	 * Set new price level for the given thirdparty
510
	 *
511
	 * @param	int		$id				ID of thirdparty
512
	 * @param	int		$priceLevel		Price level to apply to thirdparty
513
	 * @return	object					Thirdparty data without useless information
514
	 *
515
	 * @url PUT {id}/setpricelevel
516
	 *
517
	 * @throws RestException 400 Price level out of bounds
518
	 * @throws RestException 401 Access not allowed for your login
519
	 * @throws RestException 404 Thirdparty not found
520
	 * @throws RestException 500 Error fetching/setting price level
521
	 * @throws RestException 501 Request needs modules "Thirdparties" and "Products" and setting Multiprices activated
522
	 */
523
	public function setThirdpartyPriceLevel($id, $priceLevel)
524
	{
525
		global $conf;
526
527
		if (empty($conf->societe->enabled)) {
528
			throw new RestException(501, 'Module "Thirdparties" needed for this request');
529
		}
530
531
		if (empty($conf->product->enabled)) {
532
			throw new RestException(501, 'Module "Products" needed for this request');
533
		}
534
535
		if (empty($conf->global->PRODUIT_MULTIPRICES)) {
536
			throw new RestException(501, 'Multiprices features activation needed for this request');
537
		}
538
539
		if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
540
			throw new RestException(400, 'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
541
		}
542
543
		if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
544
			throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
545
		}
546
547
		$result = $this->company->fetch($id);
548
		if ($result < 0) {
549
			throw new RestException(404, 'Thirdparty '.$id.' not found');
550
		}
551
552
		if (empty($result)) {
553
			throw new RestException(500, 'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
554
		}
555
556
		if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) {
557
			throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
558
		}
559
560
		$result = $this->company->set_price_level($priceLevel, DolibarrApiAccess::$user);
561
		if ($result <= 0) {
562
			throw new RestException(500, 'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
563
		}
564
565
		return $this->_cleanObjectDatas($this->company);
566
	}
567
568
	/**
569
	 * Get customer categories for a thirdparty
570
	 *
571
	 * @param int		$id         ID of thirdparty
572
	 * @param string	$sortfield	Sort field
573
	 * @param string	$sortorder	Sort order
574
	 * @param int		$limit		Limit for list
575
	 * @param int		$page		Page number
576
	 *
577
	 * @return mixed
578
	 *
579
	 * @url GET {id}/categories
580
	 */
581
	public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
582
	{
583
		if (!DolibarrApiAccess::$user->rights->categorie->lire) {
584
			throw new RestException(401);
585
		}
586
587
		$result = $this->company->fetch($id);
588
		if (!$result)
589
		{
590
			throw new RestException(404, 'Thirdparty not found');
591
		}
592
593
		$categories = new Categorie($this->db);
594
595
		$result = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
596
597
		if (is_numeric($result) && $result < 0)
598
		{
599
			throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
600
		}
601
602
		if (is_numeric($result) && $result == 0)	// To fix a return of 0 instead of empty array of method getListForItem
603
		{
604
			return array();
605
		}
606
607
		return $result;
608
	}
609
610
	/**
611
	 * Add a customer category to a thirdparty
612
	 *
613
	 * @param int		$id				Id of thirdparty
614
	 * @param int       $category_id	Id of category
615
	 *
616
	 * @return mixed
617
	 *
618
	 * @url POST {id}/categories/{category_id}
619
	 */
620
	public function addCategory($id, $category_id)
621
	{
622
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
623
			throw new RestException(401);
624
		}
625
626
		$result = $this->company->fetch($id);
627
		if (!$result) {
628
			throw new RestException(404, 'Thirdparty not found');
629
		}
630
		$category = new Categorie($this->db);
631
		$result = $category->fetch($category_id);
632
		if (!$result) {
633
			throw new RestException(404, 'category not found');
634
		}
635
636
		if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
637
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
638
		}
639
		if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
640
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
641
		}
642
643
		$category->add_type($this->company, 'customer');
644
645
		return $this->_cleanObjectDatas($this->company);
646
	}
647
648
	/**
649
	 * Remove the link between a customer category and the thirdparty
650
	 *
651
	 * @param int		$id				Id of thirdparty
652
	 * @param int		$category_id	Id of category
653
	 *
654
	 * @return mixed
655
	 *
656
	 * @url DELETE {id}/categories/{category_id}
657
	 */
658
	public function deleteCategory($id, $category_id)
659
	{
660
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
661
			throw new RestException(401);
662
		}
663
664
		$result = $this->company->fetch($id);
665
		if (!$result) {
666
			throw new RestException(404, 'Thirdparty not found');
667
		}
668
		$category = new Categorie($this->db);
669
		$result = $category->fetch($category_id);
670
		if (!$result) {
671
			throw new RestException(404, 'category not found');
672
		}
673
674
		if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
675
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
676
		}
677
		if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
678
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
679
		}
680
681
		$category->del_type($this->company, 'customer');
682
683
		return $this->_cleanObjectDatas($this->company);
684
	}
685
686
	/**
687
	 * Get supplier categories for a thirdparty
688
	 *
689
	 * @param int		$id         ID of thirdparty
690
	 * @param string	$sortfield	Sort field
691
	 * @param string	$sortorder	Sort order
692
	 * @param int		$limit		Limit for list
693
	 * @param int		$page		Page number
694
	 *
695
	 * @return mixed
696
	 *
697
	 * @url GET {id}/supplier_categories
698
	 */
699
	public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
700
	{
701
		if (!DolibarrApiAccess::$user->rights->categorie->lire) {
702
			throw new RestException(401);
703
		}
704
705
		$result = $this->company->fetch($id);
706
		if (!$result)
707
		{
708
			throw new RestException(404, 'Thirdparty not found');
709
		}
710
711
		$categories = new Categorie($this->db);
712
713
		$result = $categories->getListForItem($id, 'supplier', $sortfield, $sortorder, $limit, $page);
714
715
		if (is_numeric($result) && $result < 0)
716
		{
717
			throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
718
		}
719
720
		if (is_numeric($result) && $result == 0)	// To fix a return of 0 instead of empty array of method getListForItem
721
		{
722
			return array();
723
		}
724
725
		return $result;
726
	}
727
728
	/**
729
	 * Add a supplier category to a thirdparty
730
	 *
731
	 * @param int		$id				Id of thirdparty
732
	 * @param int       $category_id	Id of category
733
	 *
734
	 * @return mixed
735
	 *
736
	 * @url POST {id}/supplier_categories/{category_id}
737
	 */
738
	public function addSupplierCategory($id, $category_id)
739
	{
740
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
741
			throw new RestException(401);
742
		}
743
744
		$result = $this->company->fetch($id);
745
		if (!$result) {
746
			throw new RestException(404, 'Thirdparty not found');
747
		}
748
		$category = new Categorie($this->db);
749
		$result = $category->fetch($category_id);
750
		if (!$result) {
751
			throw new RestException(404, 'category not found');
752
		}
753
754
		if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
755
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
756
		}
757
		if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
758
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
759
		}
760
761
		$category->add_type($this->company, 'supplier');
762
763
		return $this->_cleanObjectDatas($this->company);
764
	}
765
766
	/**
767
	 * Remove the link between a category and the thirdparty
768
	 *
769
	 * @param int		$id				Id of thirdparty
770
	 * @param int		$category_id	Id of category
771
	 *
772
	 * @return mixed
773
	 *
774
	 * @url DELETE {id}/supplier_categories/{category_id}
775
	 */
776
	public function deleteSupplierCategory($id, $category_id)
777
	{
778
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
779
			throw new RestException(401);
780
		}
781
782
		$result = $this->company->fetch($id);
783
		if (!$result) {
784
			throw new RestException(404, 'Thirdparty not found');
785
		}
786
		$category = new Categorie($this->db);
787
		$result = $category->fetch($category_id);
788
		if (!$result) {
789
			throw new RestException(404, 'category not found');
790
		}
791
792
		if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
793
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
794
		}
795
		if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
796
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
797
		}
798
799
		$category->del_type($this->company, 'supplier');
800
801
		return $this->_cleanObjectDatas($this->company);
802
	}
803
804
805
	/**
806
	 * Get outstanding proposals of thirdparty
807
	 *
808
	 * @param 	int 	$id			ID of the thirdparty
809
	 * @param 	string 	$mode		'customer' or 'supplier'
810
	 *
811
	 * @url     GET {id}/outstandingproposals
812
	 *
813
	 * @return array  				List of outstandings proposals of thirdparty
814
	 *
815
	 * @throws RestException 400
816
	 * @throws RestException 401
817
	 * @throws RestException 404
818
	 */
819
	public function getOutStandingProposals($id, $mode = 'customer')
820
	{
821
		if (!DolibarrApiAccess::$user->rights->societe->lire) {
822
			throw new RestException(401);
823
		}
824
825
		if (empty($id)) {
826
			throw new RestException(400, 'Thirdparty ID is mandatory');
827
		}
828
829
		if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
830
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
831
		}
832
833
		$result = $this->company->fetch($id);
834
		if (!$result) {
835
			throw new RestException(404, 'Thirdparty not found');
836
		}
837
838
		$result = $this->company->getOutstandingProposals($mode);
839
840
		unset($result['total_ht']);
841
		unset($result['total_ttc']);
842
843
		return $result;
844
	}
845
846
847
	/**
848
	 * Get outstanding orders of thirdparty
849
	 *
850
	 * @param 	int 	$id			ID of the thirdparty
851
	 * @param 	string 	$mode		'customer' or 'supplier'
852
	 *
853
	 * @url     GET {id}/outstandingorders
854
	 *
855
	 * @return array  				List of outstandings orders of thirdparty
856
	 *
857
	 * @throws RestException 400
858
	 * @throws RestException 401
859
	 * @throws RestException 404
860
	 */
861
	public function getOutStandingOrder($id, $mode = 'customer')
862
	{
863
		if (!DolibarrApiAccess::$user->rights->societe->lire) {
864
			throw new RestException(401);
865
		}
866
867
		if (empty($id)) {
868
			throw new RestException(400, 'Thirdparty ID is mandatory');
869
		}
870
871
		if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
872
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
873
		}
874
875
		$result = $this->company->fetch($id);
876
		if (!$result) {
877
			throw new RestException(404, 'Thirdparty not found');
878
		}
879
880
		$result = $this->company->getOutstandingOrders($mode);
881
882
		unset($result['total_ht']);
883
		unset($result['total_ttc']);
884
885
		return $result;
886
	}
887
888
	/**
889
	 * Get outstanding invoices of thirdparty
890
	 *
891
	 * @param 	int 	$id			ID of the thirdparty
892
	 * @param 	string 	$mode		'customer' or 'supplier'
893
	 *
894
	 * @url     GET {id}/outstandinginvoices
895
	 *
896
	 * @return array  				List of outstandings invoices of thirdparty
897
	 *
898
	 * @throws RestException 400
899
	 * @throws RestException 401
900
	 * @throws RestException 404
901
	 */
902
	public function getOutStandingInvoices($id, $mode = 'customer')
903
	{
904
		if (!DolibarrApiAccess::$user->rights->societe->lire) {
905
			throw new RestException(401);
906
		}
907
908
		if (empty($id)) {
909
			throw new RestException(400, 'Thirdparty ID is mandatory');
910
		}
911
912
		if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
913
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
914
		}
915
916
		$result = $this->company->fetch($id);
917
		if (!$result) {
918
			throw new RestException(404, 'Thirdparty not found');
919
		}
920
921
		$result = $this->company->getOutstandingBills($mode);
922
923
		unset($result['total_ht']);
924
		unset($result['total_ttc']);
925
926
		return $result;
927
	}
928
929
	/**
930
	 * Get representatives of thirdparty
931
	 *
932
	 * @param 	int 	$id			ID of the thirdparty
933
	 * @param 	string 	$mode		0=Array with properties, 1=Array of id.
934
	 *
935
	 * @url     GET {id}/representatives
936
	 *
937
	 * @return array  				List of representatives of thirdparty
938
	 *
939
	 * @throws RestException 400
940
	 * @throws RestException 401
941
	 * @throws RestException 404
942
	 */
943
	public function getSalesRepresentatives($id, $mode = 0)
944
	{
945
		if (!DolibarrApiAccess::$user->rights->societe->lire) {
946
			throw new RestException(401);
947
		}
948
949
		if (empty($id)) {
950
			throw new RestException(400, 'Thirdparty ID is mandatory');
951
		}
952
953
		if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
954
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
955
		}
956
957
		$result = $this->company->fetch($id);
958
		if (!$result) {
959
			throw new RestException(404, 'Thirdparty not found');
960
		}
961
962
		$result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
963
964
		return $result;
965
	}
966
967
	/**
968
	 * Get fixed amount discount of a thirdparty (all sources: deposit, credit note, commercial offers...)
969
	 *
970
	 * @param 	int 	$id             ID of the thirdparty
971
	 * @param 	string 	$filter    	Filter exceptional discount. "none" will return every discount, "available" returns unapplied discounts, "used" returns applied discounts   {@choice none,available,used}
972
	 * @param   string  $sortfield  	Sort field
973
	 * @param   string  $sortorder  	Sort order
974
	 *
975
	 * @url     GET {id}/fixedamountdiscounts
976
	 *
977
	 * @return array  List of fixed discount of thirdparty
978
	 *
979
	 * @throws RestException 400
980
	 * @throws RestException 401
981
	 * @throws RestException 404
982
	 * @throws RestException 503
983
	 */
984
	public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC')
985
	{
986
		$obj_ret = array();
987
988
		if (!DolibarrApiAccess::$user->rights->societe->lire) {
989
			throw new RestException(401);
990
		}
991
992
		if (empty($id)) {
993
			throw new RestException(400, 'Thirdparty ID is mandatory');
994
		}
995
996
		if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
997
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
998
		}
999
1000
		$result = $this->company->fetch($id);
1001
		if (!$result) {
1002
			throw new RestException(404, 'Thirdparty not found');
1003
		}
1004
1005
1006
		$sql = "SELECT f.ref, f.type as factype, re.fk_facture_source, re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc, re.description, re.fk_facture, re.fk_facture_line";
1007
		$sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re, ".MAIN_DB_PREFIX."facture as f";
1008
		$sql .= " WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".$id;
1009
		if ($filter == "available")  $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1010
		if ($filter == "used")  $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1011
1012
		$sql .= $this->db->order($sortfield, $sortorder);
1013
1014
		$result = $this->db->query($sql);
1015
		if (!$result) {
1016
			throw new RestException(503, $this->db->lasterror());
1017
		} else {
1018
			$num = $this->db->num_rows($result);
1019
			while ($obj = $this->db->fetch_object($result)) {
1020
				$obj_ret[] = $obj;
1021
			}
1022
		}
1023
1024
		return $obj_ret;
1025
	}
1026
1027
1028
1029
	/**
1030
	 * Return list of invoices qualified to be replaced by another invoice.
1031
	 *
1032
	 * @param int   $id             Id of thirdparty
1033
	 *
1034
	 * @url     GET {id}/getinvoicesqualifiedforreplacement
1035
	 *
1036
	 * @return array
1037
	 * @throws RestException 400
1038
	 * @throws RestException 401
1039
	 * @throws RestException 404
1040
	 * @throws RestException 405
1041
	 */
1042
	public function getInvoicesQualifiedForReplacement($id)
1043
	{
1044
		if (!DolibarrApiAccess::$user->rights->facture->lire) {
1045
			throw new RestException(401);
1046
		}
1047
		if (empty($id)) {
1048
			throw new RestException(400, 'Thirdparty ID is mandatory');
1049
		}
1050
1051
		if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1052
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1053
		}
1054
1055
		/*$result = $this->thirdparty->fetch($id);
1056
		 if( ! $result ) {
1057
		 throw new RestException(404, 'Thirdparty not found');
1058
		 }*/
1059
1060
		$invoice = new Facture($this->db);
1061
		$result = $invoice->list_replacable_invoices($id);
1062
		if ($result < 0) {
1063
			throw new RestException(405, $this->thirdparty->error);
0 ignored issues
show
Bug Best Practice introduced by
The property thirdparty does not exist on Thirdparties. Did you maybe forget to declare it?
Loading history...
1064
		}
1065
1066
		return $result;
1067
	}
1068
1069
	/**
1070
	 * Return list of invoices qualified to be corrected by a credit note.
1071
	 * Invoices matching the following rules are returned
1072
	 * (validated + payment on process) or classified (payed completely or payed partialy) + not already replaced + not already a credit note
1073
	 *
1074
	 * @param int   $id             Id of thirdparty
1075
	 *
1076
	 * @url     GET {id}/getinvoicesqualifiedforcreditnote
1077
	 *
1078
	 * @return array
1079
	 *
1080
	 * @throws RestException 400
1081
	 * @throws RestException 401
1082
	 * @throws RestException 404
1083
	 * @throws RestException 405
1084
	 */
1085
	public function getInvoicesQualifiedForCreditNote($id)
1086
	{
1087
		if (!DolibarrApiAccess::$user->rights->facture->lire) {
1088
			throw new RestException(401);
1089
		}
1090
		if (empty($id)) {
1091
			throw new RestException(400, 'Thirdparty ID is mandatory');
1092
		}
1093
1094
		if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1095
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1096
		}
1097
1098
		/*$result = $this->thirdparty->fetch($id);
1099
		 if( ! $result ) {
1100
		 throw new RestException(404, 'Thirdparty not found');
1101
		 }*/
1102
1103
		$invoice = new Facture($this->db);
1104
		$result = $invoice->list_qualified_avoir_invoices($id);
1105
		if ($result < 0) {
1106
			throw new RestException(405, $this->thirdparty->error);
0 ignored issues
show
Bug Best Practice introduced by
The property thirdparty does not exist on Thirdparties. Did you maybe forget to declare it?
Loading history...
1107
		}
1108
1109
		return $result;
1110
	}
1111
1112
	/**
1113
	 * Get CompanyBankAccount objects for thirdparty
1114
	 *
1115
	 * @param int $id ID of thirdparty
1116
	 *
1117
	 * @return array
1118
	 *
1119
	 * @url GET {id}/bankaccounts
1120
	 */
1121
	public function getCompanyBankAccount($id)
1122
	{
1123
		if (!DolibarrApiAccess::$user->rights->facture->lire) {
1124
			throw new RestException(401);
1125
		}
1126
		if (empty($id)) {
1127
			throw new RestException(400, 'Thirdparty ID is mandatory');
1128
		}
1129
1130
		if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1131
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1132
		}
1133
1134
		/**
1135
		 * We select all the records that match the socid
1136
		 */
1137
1138
		$sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1139
		$sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1140
		$sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1141
		if ($id) $sql .= " WHERE fk_soc  = ".$id." ";
1142
1143
1144
		$result = $this->db->query($sql);
1145
1146
		if ($result->num_rows == 0) {
1147
			throw new RestException(404, 'Account not found');
1148
		}
1149
1150
		$i = 0;
1151
1152
		$accounts = array();
1153
1154
		if ($result)
1155
		{
1156
			$num = $this->db->num_rows($result);
1157
			while ($i < $num)
1158
			{
1159
				$obj = $this->db->fetch_object($result);
1160
				$account = new CompanyBankAccount($this->db);
1161
				if ($account->fetch($obj->rowid)) {
1162
					$accounts[] = $account;
1163
				}
1164
				$i++;
1165
			}
1166
		} else {
1167
			throw new RestException(404, 'Account not found');
1168
		}
1169
1170
1171
		$fields = array('socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id', 'rum');
1172
1173
		$returnAccounts = array();
1174
1175
		foreach ($accounts as $account) {
1176
			$object = array();
1177
			foreach ($account as $key => $value) {
1178
				if (in_array($key, $fields)) {
1179
					$object[$key] = $value;
1180
				}
1181
			}
1182
			$returnAccounts[] = $object;
1183
		}
1184
1185
		return $returnAccounts;
1186
	}
1187
1188
	/**
1189
	 * Create CompanyBankAccount object for thirdparty
1190
	 * @param int  $id ID of thirdparty
1191
	 * @param array $request_data Request data
1192
	 *
1193
	 * @return object  BankAccount of thirdparty
1194
	 *
1195
	 * @url POST {id}/bankaccounts
1196
	 */
1197
	public function createCompanyBankAccount($id, $request_data = null)
1198
	{
1199
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
1200
			throw new RestException(401);
1201
		}
1202
		if ($this->company->fetch($id) <= 0) {
1203
			throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1204
		}
1205
		$account = new CompanyBankAccount($this->db);
1206
1207
		$account->socid = $id;
1208
1209
		foreach ($request_data as $field => $value) {
1210
			$account->$field = $value;
1211
		}
1212
1213
		if ($account->create(DolibarrApiAccess::$user) < 0)
1214
			throw new RestException(500, 'Error creating Company Bank account');
1215
1216
		if (empty($account->rum)) {
1217
			require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1218
			$prelevement = new BonPrelevement($this->db);
1219
			$account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1220
			$account->date_rum = dol_now();
1221
		}
1222
1223
		if ($account->update(DolibarrApiAccess::$user) < 0)
1224
			throw new RestException(500, 'Error updating values');
1225
1226
		return $this->_cleanObjectDatas($account);
1227
	}
1228
1229
	/**
1230
	 * Update CompanyBankAccount object for thirdparty
1231
	 *
1232
	 * @param int $id ID of thirdparty
1233
	 * @param int  $bankaccount_id ID of CompanyBankAccount
1234
	 * @param array $request_data Request data
1235
	 *
1236
	 * @return object  BankAccount of thirdparty
1237
	 *
1238
	 * @url PUT {id}/bankaccounts/{bankaccount_id}
1239
	 */
1240
	public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
1241
	{
1242
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
1243
			throw new RestException(401);
1244
		}
1245
		if ($this->company->fetch($id) <= 0) {
1246
			throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1247
		}
1248
		$account = new CompanyBankAccount($this->db);
1249
1250
		$account->fetch($bankaccount_id, $id, -1, '');
1251
1252
		if ($account->socid != $id) {
1253
			throw new RestException(401);
1254
		}
1255
1256
1257
		foreach ($request_data as $field => $value) {
1258
			$account->$field = $value;
1259
		}
1260
1261
		if (empty($account->rum)) {
1262
			require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1263
			$prelevement = new BonPrelevement($this->db);
1264
			$account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1265
			$account->date_rum = dol_now();
1266
		}
1267
1268
		if ($account->update(DolibarrApiAccess::$user) < 0)
1269
			throw new RestException(500, 'Error updating values');
1270
1271
		return $this->_cleanObjectDatas($account);
1272
	}
1273
1274
	/**
1275
	 * Delete a bank account attached to a thirdparty
1276
	 *
1277
	 * @param int $id ID of thirdparty
1278
	 * @param int $bankaccount_id ID of CompanyBankAccount
1279
	 *
1280
	 * @return int -1 if error 1 if correct deletion
1281
	 *
1282
	 * @url DELETE {id}/bankaccounts/{bankaccount_id}
1283
	 */
1284
	public function deleteCompanyBankAccount($id, $bankaccount_id)
1285
	{
1286
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
1287
			throw new RestException(401);
1288
		}
1289
1290
		$account = new CompanyBankAccount($this->db);
1291
1292
		$account->fetch($bankaccount_id);
1293
1294
		if (!$account->socid == $id)
1295
			throw new RestException(401);
1296
1297
		return $account->delete(DolibarrApiAccess::$user);
1298
	}
1299
1300
	/**
1301
	 * Generate a Document from a bank account record (like SEPA mandate)
1302
	 *
1303
	 * @param int 		$id 			Thirdparty id
1304
	 * @param int 		$companybankid 	Companybank id
1305
	 * @param string 	$model 			Model of document to generate
1306
	 * @return void
1307
	 *
1308
	 * @url GET {id}/generateBankAccountDocument/{companybankid}/{model}
1309
	 */
1310
	public function generateBankAccountDocument($id, $companybankid = null, $model = 'sepamandate')
1311
	{
1312
		global $conf, $langs;
1313
1314
		$langs->loadLangs(array("main", "dict", "commercial", "products", "companies", "banks", "bills", "withdrawals"));
1315
1316
		if ($this->company->fetch($id) <= 0) {
1317
			throw new RestException(404, 'Thirdparty not found');
1318
		}
1319
1320
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
1321
			throw new RestException(401);
1322
		}
1323
1324
		$this->company->setDocModel(DolibarrApiAccess::$user, $model);
1325
1326
		$this->company->fk_bank = $this->company->fk_account;
1 ignored issue
show
Deprecated Code introduced by
The property CommonObject::$fk_bank has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1326
		/** @scrutinizer ignore-deprecated */ $this->company->fk_bank = $this->company->fk_account;
Loading history...
1327
1328
		$outputlangs = $langs;
1329
		$newlang = '';
1330
1331
		//if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
1332
		if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang)) {
1333
			if (isset($this->company->thirdparty->default_lang)) {
1334
				$newlang = $this->company->thirdparty->default_lang; // for proposal, order, invoice, ...
1335
			} elseif (isset($this->company->default_lang)) {
1336
				$newlang = $this->company->default_lang; // for thirdparty
1337
			}
1338
		}
1339
		if (!empty($newlang)) {
1340
			$outputlangs = new Translate("", $conf);
1341
			$outputlangs->setDefaultLang($newlang);
1342
		}
1343
1344
		$sql = "SELECT rowid";
1345
		$sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1346
		if ($id) $sql .= " WHERE fk_soc  = ".$id." ";
1347
		if ($companybankid) $sql .= " AND rowid = ".$companybankid."";
1348
1349
		$i = 0;
1350
		$accounts = array();
1351
1352
		$result = $this->db->query($sql);
1353
		if ($result) {
1354
			if ($this->db->num_rows($result) == 0) {
1355
				throw new RestException(404, 'Bank account not found');
1356
			}
1357
1358
			$num = $this->db->num_rows($result);
1359
			while ($i < $num) {
1360
				$obj = $this->db->fetch_object($result);
1361
1362
				$account = new CompanyBankAccount($this->db);
1363
				if ($account->fetch($obj->rowid)) {
1364
					$accounts[] = $account;
1365
				}
1366
				$i++;
1367
			}
1368
		} else {
1369
			throw new RestException(500, 'Sql error '.$this->db->lasterror());
1370
		}
1371
1372
		$moreparams = array(
1373
			'use_companybankid' => $accounts[0]->id,
1374
			'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].'/'.dol_sanitizeFileName($this->company->id)
1375
		);
1376
1377
		$result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1378
1379
		if ($result > 0) {
1380
			return array("success" => $result);
1381
		} else {
1382
			throw new RestException(500);
1383
		}
1384
	}
1385
1386
	/**
1387
	 * Get a specific gateway attached to a thirdparty (by specifying the site key)
1388
	 *
1389
	 * @param int $id ID of thirdparty
1390
	 * @param string $site Site key
1391
	 *
1392
	 * @return SocieteAccount[]
1393
	 * @throws RestException 401 Unauthorized: User does not have permission to read thirdparties
1394
	 * @throws RestException 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty
1395
	 *
1396
	 * @url GET {id}/gateways/
1397
	 */
1398
	public function getSocieteAccounts($id, $site = null)
1399
	{
1400
		if (!DolibarrApiAccess::$user->rights->societe->lire) {
1401
			throw new RestException(401);
1402
		}
1403
1404
		if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1405
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1406
		}
1407
1408
		/**
1409
		 * We select all the records that match the socid
1410
		 */
1411
		$sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
1412
		$sql .= " WHERE fk_soc = $id";
1413
		if ($site) $sql .= " AND site ='$site'";
1414
1415
		$result = $this->db->query($sql);
1416
1417
		if ($result && $this->db->num_rows($result) == 0) {
1418
			throw new RestException(404, 'This thirdparty does not have any gateway attached or does not exist.');
1419
		}
1420
1421
		$i = 0;
1422
1423
		$accounts = array();
1424
1425
		$num = $this->db->num_rows($result);
1426
		while ($i < $num)
1427
		{
1428
			$obj = $this->db->fetch_object($result);
1429
			$account = new SocieteAccount($this->db);
1430
1431
			if ($account->fetch($obj->rowid)) {
1432
				$accounts[] = $account;
1433
			}
1434
			$i++;
1435
		}
1436
1437
		$fields = array('id', 'fk_soc', 'key_account', 'site', 'date_creation', 'tms');
1438
1439
		$returnAccounts = array();
1440
1441
		foreach ($accounts as $account) {
1442
			$object = array();
1443
			foreach ($account as $key => $value) {
1444
				if (in_array($key, $fields)) {
1445
					$object[$key] = $value;
1446
				}
1447
			}
1448
			$returnAccounts[] = $object;
1449
		}
1450
1451
		return $returnAccounts;
1452
	}
1453
1454
	/**
1455
	 * Create and attach a new gateway to an existing thirdparty
1456
	 *
1457
	 * Possible fields for request_data (request body) are specified in <code>llx_societe_account</code> table.<br>
1458
	 * See <a href="https://wiki.dolibarr.org/index.php/Table_llx_societe_account">Table llx_societe_account</a> wiki page for more information<br><br>
1459
	 * <u>Example body payload :</u> <pre>{"key_account": "cus_DAVkLSs1LYyYI", "site": "stripe"}</pre>
1460
	 *
1461
	 * @param int $id ID of thirdparty
1462
	 * @param array $request_data Request data
1463
	 *
1464
	 * @return SocieteAccount
1465
	 *
1466
	 * @throws RestException 401 Unauthorized: User does not have permission to read thirdparties
1467
	 * @throws RestException 409 Conflict: A SocieteAccount entity (gateway) already exists for this company and site.
1468
	 * @throws RestException 422 Unprocessable Entity: You must pass the site attribute in your request data !
1469
	 * @throws RestException 500 Internal Server Error: Error creating SocieteAccount account
1470
	 *
1471
	 * @url POST {id}/gateways
1472
	 */
1473
	public function createSocieteAccount($id, $request_data = null)
1474
	{
1475
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
1476
			throw new RestException(401);
1477
		}
1478
1479
		if (!isset($request_data['site'])) {
1480
			throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !');
1481
		}
1482
1483
		$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc  = ".$id." AND site = '".$this->db->escape($request_data['site'])."'";
1484
		$result = $this->db->query($sql);
1485
1486
		if ($result && $this->db->num_rows($result) == 0) {
1487
			$account = new SocieteAccount($this->db);
1488
			if (!isset($request_data['login'])) {
1489
				$account->login = "";
1490
			}
1491
			$account->fk_soc = $id;
1492
1493
			foreach ($request_data as $field => $value) {
1494
				$account->$field = $value;
1495
			}
1496
1497
			if ($account->create(DolibarrApiAccess::$user) < 0)
1498
				throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1499
1500
			$this->_cleanObjectDatas($account);
1501
1502
			return $account;
1503
		} else {
1504
			throw new RestException(409, 'A SocieteAccount entity already exists for this company and site.');
1505
		}
1506
	}
1507
1508
	/**
1509
	 * Create and attach a new (or replace an existing) specific site gateway to a thirdparty
1510
	 *
1511
	 * You <strong>MUST</strong> pass all values to keep (otherwise, they will be deleted) !<br>
1512
	 * If you just need to update specific fields prefer <code>PATCH /thirdparties/{id}/gateways/{site}</code> endpoint.<br><br>
1513
	 * When a <strong>SocieteAccount</strong> entity does not exist for the <code>id</code> and <code>site</code>
1514
	 * supplied, a new one will be created. In that case <code>fk_soc</code> and <code>site</code> members form
1515
	 * request body payload will be ignored and <code>id</code> and <code>site</code> query strings parameters
1516
	 * will be used instead.
1517
	 *
1518
	 * @param int $id ID of thirdparty
1519
	 * @param string $site Site key
1520
	 * @param array $request_data Request data
1521
	 *
1522
	 * @return SocieteAccount
1523
	 *
1524
	 * @throws RestException 401 Unauthorized: User does not have permission to read thirdparties
1525
	 * @throws RestException 422 Unprocessable Entity: You must pass the site attribute in your request data !
1526
	 * @throws RestException 500 Internal Server Error: Error updating SocieteAccount entity
1527
	 *
1528
	 * @url PUT {id}/gateways/{site}
1529
	 */
1530
	public function putSocieteAccount($id, $site, $request_data = null)
1531
	{
1532
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
1533
			throw new RestException(401);
1534
		}
1535
1536
		$sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1537
		$result = $this->db->query($sql);
1538
1539
		// We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
1540
		if ($result && $this->db->num_rows == 0) {
0 ignored issues
show
Bug introduced by
The property num_rows does not seem to exist on DoliDB.
Loading history...
1541
			if (!isset($request_data['key_account'])) {
1542
				throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1543
			}
1544
			$account = new SocieteAccount($this->db);
1545
			if (!isset($request_data['login'])) {
1546
				$account->login = "";
1547
			}
1548
1549
			foreach ($request_data as $field => $value) {
1550
				$account->$field = $value;
1551
			}
1552
1553
			$account->fk_soc = $id;
1554
			$account->site = $site;
1555
1556
			if ($account->create(DolibarrApiAccess::$user) < 0) {
1557
				throw new RestException(500, 'Error creating SocieteAccount entity.');
1558
			}
1559
			// We found an existing SocieteAccount entity, we are replacing it
1560
		} else {
1561
			if (isset($request_data['site']) && $request_data['site'] !== $site) {
1562
				$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc  = ".$id." AND site = '".$this->db->escape($request_data['site'])."' ";
1563
				$result = $this->db->query($sql);
1564
1565
				if ($result && $this->db->num_rows($result) !== 0) {
1566
					throw new RestException(409, "You are trying to update this thirdparty SocieteAccount (gateway record) from $site to ".$request_data['site']." but another SocieteAccount entity already exists with this site key.");
1567
				}
1568
			}
1569
1570
			$obj = $this->db->fetch_object($result);
1571
1572
			$account = new SocieteAccount($this->db);
1573
			$account->id = $obj->rowid;
1574
			$account->fk_soc = $id;
1575
			$account->site = $site;
1576
			if (!isset($request_data['login'])) {
1577
				$account->login = "";
1578
			}
1579
			$account->fk_user_creat = $obj->fk_user_creat;
1580
			$account->date_creation = $obj->date_creation;
1581
1582
			foreach ($request_data as $field => $value) {
1583
				$account->$field = $value;
1584
			}
1585
1586
			if ($account->update(DolibarrApiAccess::$user) < 0)
1587
				throw new RestException(500, 'Error updating SocieteAccount entity.');
1588
		}
1589
1590
		$this->_cleanObjectDatas($account);
1591
1592
		return $account;
1593
	}
1594
1595
	/**
1596
	 * Update specified values of a specific site gateway attached to a thirdparty
1597
	 *
1598
	 * @param int $id Id of thirdparty
1599
	 * @param string  $site Site key
1600
	 * @param array $request_data Request data
1601
	 *
1602
	 * @return SocieteAccount
1603
	 *
1604
	 * @throws RestException 401 Unauthorized: User does not have permission to read thirdparties
1605
	 * @throws RestException 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty
1606
	 * @throws RestException 409 Conflict: Another SocieteAccount entity already exists for this thirdparty with this site key.
1607
	 * @throws RestException 500 Internal Server Error: Error updating SocieteAccount entity
1608
	 *
1609
	 * @url PATCH {id}/gateways/{site}
1610
	 */
1611
	public function patchSocieteAccount($id, $site, $request_data = null)
1612
	{
1613
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
1614
			throw new RestException(401);
1615
		}
1616
1617
		$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc  = $id AND site = '$site' ";
1618
		$result = $this->db->query($sql);
1619
1620
		if ($result && $this->db->num_rows($result) == 0) {
1621
			throw new RestException(404, "This thirdparty does not have $site gateway attached or does not exist.");
1622
		} else {
1623
			// If the user tries to edit the site member, we check first if
1624
			if (isset($request_data['site']) && $request_data['site'] !== $site) {
1625
				$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc  = ".$id." AND site = '".$this->db->escape($request_data['site'])."' ";
1626
				$result = $this->db->query($sql);
1627
1628
				if ($result && $this->db->num_rows($result) !== 0)
1629
					throw new RestException(409, "You are trying to update this thirdparty SocieteAccount (gateway record) site member from $site to ".$request_data['site']." but another SocieteAccount entity already exists for this thirdparty with this site key.");
1630
			}
1631
1632
			$obj = $this->db->fetch_object($result);
1633
			$account = new SocieteAccount($this->db);
1634
			$account->fetch($obj->rowid);
1635
1636
			foreach ($request_data as $field => $value) {
1637
				$account->$field = $value;
1638
			}
1639
1640
			if ($account->update(DolibarrApiAccess::$user) < 0)
1641
				throw new RestException(500, 'Error updating SocieteAccount account');
1642
1643
			$this->_cleanObjectDatas($account);
1644
1645
			return $account;
1646
		}
1647
	}
1648
1649
	/**
1650
	 * Delete a specific site gateway attached to a thirdparty (by gateway id)
1651
	 *
1652
	 * @param int $id ID of thirdparty
1653
	 * @param int $site Site key
1654
	 *
1655
	 * @return void
1656
	 * @throws RestException 401 Unauthorized: User does not have permission to delete thirdparties gateways
1657
	 * @throws RestException 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty
1658
	 * @throws RestException 500 Internal Server Error: Error deleting SocieteAccount entity
1659
	 *
1660
	 * @url DELETE {id}/gateways/{site}
1661
	 */
1662
	public function deleteSocieteAccount($id, $site)
1663
	{
1664
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
1665
			throw new RestException(401);
1666
		}
1667
1668
		$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc  = $id AND site = '".$this->db->escape($site)."'";
1669
		$result = $this->db->query($sql);
1670
1671
		if ($result && $this->db->num_rows($result) == 0) {
1672
			throw new RestException(404);
1673
		} else {
1674
			$obj = $this->db->fetch_object($result);
1675
			$account = new SocieteAccount($this->db);
1676
			$account->fetch($obj->rowid);
1677
1678
			if ($account->delete(DolibarrApiAccess::$user) < 0) {
1679
				throw new RestException(500, "Error while deleting $site gateway attached to this third party");
1680
			}
1681
		}
1682
	}
1683
1684
	/**
1685
	 * Delete all gateways attached to a thirdparty
1686
	 *
1687
	 * @param int $id ID of thirdparty
1688
	 *
1689
	 * @return void
1690
	 * @throws RestException 401 Unauthorized: User does not have permission to delete thirdparties gateways
1691
	 * @throws RestException 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty
1692
	 * @throws RestException 500 Internal Server Error: Error deleting SocieteAccount entity
1693
	 *
1694
	 * @url DELETE {id}/gateways
1695
	 */
1696
	public function deleteSocieteAccounts($id)
1697
	{
1698
		if (!DolibarrApiAccess::$user->rights->societe->creer) {
1699
			throw new RestException(401);
1700
		}
1701
1702
		/**
1703
		 * We select all the records that match the socid
1704
		 */
1705
1706
		$sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1707
		$sql .= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".$id;
1708
1709
		$result = $this->db->query($sql);
1710
1711
		if ($result && $this->db->num_rows($result) == 0) {
1712
			throw new RestException(404, 'This third party does not have any gateway attached or does not exist.');
1713
		} else {
1714
			$i = 0;
1715
1716
			$num = $this->db->num_rows($result);
1717
			while ($i < $num)
1718
			{
1719
				$obj = $this->db->fetch_object($result);
1720
				$account = new SocieteAccount($this->db);
1721
				$account->fetch($obj->rowid);
1722
1723
				if ($account->delete(DolibarrApiAccess::$user) < 0) {
1724
					throw new RestException(500, 'Error while deleting gateways attached to this third party');
1725
				}
1726
				$i++;
1727
			}
1728
		}
1729
	}
1730
1731
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1732
	/**
1733
	 * Clean sensible object datas
1734
	 *
1735
	 * @param   Object  $object     Object to clean
1736
	 * @return  Object              Object with cleaned properties
1737
	 */
1738
	protected function _cleanObjectDatas($object)
1739
	{
1740
		// phpcs:enable
1741
		$object = parent::_cleanObjectDatas($object);
1742
1743
		unset($object->nom); // ->name already defined and nom deprecated
1744
		unset($object->name_bis); // ->name_alias already defined
1745
		unset($object->note); // ->note_private and note_public already defined
1746
		unset($object->departement);
1747
		unset($object->departement_code);
1748
		unset($object->pays);
1749
		unset($object->particulier);
1750
		unset($object->prefix_comm);
1751
1752
		unset($object->commercial_id); // This property is used in create/update only. It does not exists in read mode because there is several sales representatives.
1753
1754
		unset($object->total_ht);
1755
		unset($object->total_tva);
1756
		unset($object->total_localtax1);
1757
		unset($object->total_localtax2);
1758
		unset($object->total_ttc);
1759
1760
		unset($object->lines);
1761
		unset($object->thirdparty);
1762
1763
		unset($object->fk_delivery_address); // deprecated feature
1764
1765
		return $object;
1766
	}
1767
1768
	/**
1769
	 * Validate fields before create or update object
1770
	 *
1771
	 * @param array $data   Datas to validate
1772
	 * @return array
1773
	 *
1774
	 * @throws RestException
1775
	 */
1776
	private function _validate($data)
1777
	{
1778
		$thirdparty = array();
1779
		foreach (Thirdparties::$FIELDS as $field) {
1780
			if (!isset($data[$field]))
1781
				throw new RestException(400, "$field field missing");
1782
			$thirdparty[$field] = $data[$field];
1783
		}
1784
		return $thirdparty;
1785
	}
1786
1787
	/**
1788
	 * Fetch properties of a thirdparty object.
1789
	 *
1790
	 * Return an array with thirdparty informations
1791
	 *
1792
	 * @param    int	$rowid      Id of third party to load
1793
	 * @param    string	$ref        Reference of third party, name (Warning, this can return several records)
1794
	 * @param    string	$ref_ext    External reference of third party (Warning, this information is a free field not provided by Dolibarr)
1795
	 * @param    string	$barcode    Barcode of third party to load
1796
	 * @param    string	$idprof1		Prof id 1 of third party (Warning, this can return several records)
1797
	 * @param    string	$idprof2		Prof id 2 of third party (Warning, this can return several records)
1798
	 * @param    string	$idprof3		Prof id 3 of third party (Warning, this can return several records)
1799
	 * @param    string	$idprof4		Prof id 4 of third party (Warning, this can return several records)
1800
	 * @param    string	$idprof5		Prof id 5 of third party (Warning, this can return several records)
1801
	 * @param    string	$idprof6		Prof id 6 of third party (Warning, this can return several records)
1802
	 * @param    string	$email   		Email of third party (Warning, this can return several records)
1803
	 * @param    string	$ref_alias  Name_alias of third party (Warning, this can return several records)
1804
	 * @return Object cleaned Societe object
1805
	 *
1806
	 * @throws RestException
1807
	 */
1808
	private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
1809
	{
1810
		global $conf;
1811
		if (!DolibarrApiAccess::$user->rights->societe->lire) {
1812
			throw new RestException(401);
1813
		}
1814
		if ($rowid == 0) {
1815
			$result = $this->company->initAsSpecimen();
1816
		} else {
1817
			$result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1818
		}
1819
		if (!$result) {
1820
			throw new RestException(404, 'Thirdparty not found');
1821
		}
1822
1823
		if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
1824
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1825
		}
1826
1827
		if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1828
			$filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1829
			$filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1830
		} else {
1831
			$filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1832
			$filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1833
		}
1834
1835
		$absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount);
1836
		$absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote);
1837
		$this->company->absolute_discount = price2num($absolute_discount, 'MT');
0 ignored issues
show
Bug introduced by
The property absolute_discount does not seem to exist on Societe.
Loading history...
1838
		$this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT');
0 ignored issues
show
Bug introduced by
The property absolute_creditnote does not seem to exist on Societe.
Loading history...
1839
1840
		return $this->_cleanObjectDatas($this->company);
1841
	}
1842
}
1843