Passed
Branch develop (f6c1a1)
by
unknown
28:54
created

Proposals::close()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
c 0
b 0
f 0
nc 8
nop 4
dl 0
loc 34
rs 8.4444
1
<?php
2
/* Copyright (C) 2015   Jean-François Ferry     <[email protected]>
3
 * Copyright (C) 2016   Laurent Destailleur     <[email protected]>
4
 * Copyright (C) 2020   Thibault FOUCART   		<[email protected]>
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
use Luracast\Restler\RestException;
21
22
require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
23
24
25
/**
26
 * API class for orders
27
 *
28
 * @access protected
29
 * @class  DolibarrApiAccess {@requires user,external}
30
 */
31
class Proposals extends DolibarrApi
32
{
33
    /**
34
	 * @var array   $FIELDS     Mandatory fields, checked when create and update object
35
	 */
36
	static $FIELDS = array(
37
		'socid'
38
	);
39
40
	/**
41
	 * @var Propal $propal {@type Propal}
42
	 */
43
	public $propal;
44
45
	/**
46
	 * Constructor
47
	 */
48
	public function __construct()
49
	{
50
		global $db, $conf;
51
		$this->db = $db;
52
		$this->propal = new Propal($this->db);
53
	}
54
55
	/**
56
	 * Get properties of a commercial proposal object
57
	 *
58
	 * Return an array with commercial proposal informations
59
	 *
60
	 * @param       int         $id           ID of commercial proposal
61
	 * @param       int         $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id
62
	 * @return 	array|mixed data without useless information
63
	 *
64
	 * @throws 	RestException
65
	 */
66
	public function get($id, $contact_list = 1)
67
	{
68
        return $this->_fetch($id, '', '', $contact_list);
69
	}
70
71
    /**
72
     * Get properties of an proposal object by ref
73
     *
74
     * Return an array with proposal informations
75
     *
76
     * @param       string		$ref			Ref of object
77
     * @param       int         $contact_list  0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id
78
     * @return 	array|mixed data without useless information
79
     *
80
     * @url GET    ref/{ref}
81
     *
82
     * @throws 	RestException
83
     */
84
    public function getByRef($ref, $contact_list = 1)
85
    {
86
        return $this->_fetch('', $ref, '', $contact_list);
87
    }
88
89
    /**
90
     * Get properties of an proposal object by ref_ext
91
     *
92
     * Return an array with proposal informations
93
     *
94
     * @param       string		$ref_ext			External reference of object
95
     * @param       int         $contact_list  0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id
96
     * @return 	array|mixed data without useless information
97
     *
98
     * @url GET    ref_ext/{ref_ext}
99
     *
100
     * @throws 	RestException
101
     */
102
    public function getByRefExt($ref_ext, $contact_list = 1)
103
    {
104
        return $this->_fetch('', '', $ref_ext, $contact_list);
105
    }
106
107
    /**
108
     * Get properties of an proposal object
109
     *
110
     * Return an array with proposal informations
111
     *
112
     * @param       int         $id             ID of order
113
	 * @param		string		$ref			Ref of object
114
	 * @param		string		$ref_ext		External reference of object
115
     * @param       int         $contact_list  0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id
116
     * @return 	array|mixed data without useless information
117
     *
118
     * @throws 	RestException
119
     */
120
    private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
121
    {
122
		if (!DolibarrApiAccess::$user->rights->propal->lire) {
123
			throw new RestException(401);
124
		}
125
126
		$result = $this->propal->fetch($id, $ref, $ref_ext);
127
		if (!$result) {
128
			throw new RestException(404, 'Commercial Proposal not found');
129
		}
130
131
		if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
132
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
133
		}
134
135
		// Add external contacts ids.
136
		$this->propal->contacts_ids = $this->propal->liste_contact(-1, 'external', $contact_list);
0 ignored issues
show
Bug introduced by
The property contacts_ids does not exist on Propal. Did you mean contact?
Loading history...
137
		$this->propal->fetchObjectLinked();
138
		return $this->_cleanObjectDatas($this->propal);
139
	}
140
141
	/**
142
	 * List commercial proposals
143
	 *
144
	 * Get a list of commercial proposals
145
	 *
146
	 * @param string	$sortfield	        Sort field
147
	 * @param string	$sortorder	        Sort order
148
	 * @param int		$limit		        Limit for list
149
	 * @param int		$page		        Page number
150
	 * @param string   	$thirdparty_ids	    Thirdparty ids to filter commercial proposals (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i}
151
	 * @param string    $sqlfilters         Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.datec:<:'20160101')"
152
	 * @return  array                       Array of order objects
153
	 */
154
    public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
155
    {
156
		global $db, $conf;
157
158
		$obj_ret = array();
159
160
		// case of external user, $thirdparty_ids param is ignored and replaced by user's socid
161
		$socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
162
163
		// If the internal user must only see his customers, force searching by him
164
		$search_sale = 0;
165
		if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
166
167
		$sql = "SELECT t.rowid";
168
		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)
169
		$sql .= " FROM ".MAIN_DB_PREFIX."propal as t";
170
171
		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
172
173
		$sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
174
		if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= " AND t.fk_soc = sc.fk_soc";
175
		if ($socids) $sql .= " AND t.fk_soc IN (".$socids.")";
176
		if ($search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
177
		// Insert sale filter
178
		if ($search_sale > 0)
179
		{
180
			$sql .= " AND sc.fk_user = ".$search_sale;
181
		}
182
		// Add sql filters
183
		if ($sqlfilters)
184
		{
185
			if (!DolibarrApi::_checkFilters($sqlfilters))
186
			{
187
				throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
188
			}
189
			$regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
190
			$sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
191
		}
192
193
		$sql .= $db->order($sortfield, $sortorder);
194
		if ($limit) {
195
			if ($page < 0)
196
			{
197
				$page = 0;
198
			}
199
			$offset = $limit * $page;
200
201
			$sql .= $db->plimit($limit + 1, $offset);
202
		}
203
204
        dol_syslog("API Rest request");
205
		$result = $db->query($sql);
206
207
		if ($result)
208
		{
209
			$num = $db->num_rows($result);
210
			$min = min($num, ($limit <= 0 ? $num : $limit));
211
			$i = 0;
212
			while ($i < $min)
213
			{
214
				$obj = $db->fetch_object($result);
215
				$proposal_static = new Propal($db);
216
				if ($proposal_static->fetch($obj->rowid)) {
217
					// Add external contacts ids
218
					$proposal_static->contacts_ids = $proposal_static->liste_contact(-1, 'external', 1);
0 ignored issues
show
Bug introduced by
The property contacts_ids does not exist on Propal. Did you mean contact?
Loading history...
219
					$obj_ret[] = $this->_cleanObjectDatas($proposal_static);
220
				}
221
				$i++;
222
			}
223
		} else {
224
			throw new RestException(503, 'Error when retrieve propal list : '.$db->lasterror());
225
		}
226
		if (!count($obj_ret)) {
227
			throw new RestException(404, 'No proposal found');
228
		}
229
		return $obj_ret;
230
	}
231
232
	/**
233
	 * Create commercial proposal object
234
	 *
235
	 * @param   array   $request_data   Request data
236
	 * @return  int     ID of proposal
237
	 */
238
    public function post($request_data = null)
239
    {
240
        if (!DolibarrApiAccess::$user->rights->propal->creer) {
241
            throw new RestException(401, "Insuffisant rights");
242
        }
243
        // Check mandatory fields
244
        $result = $this->_validate($request_data);
245
246
		foreach ($request_data as $field => $value) {
247
			$this->propal->$field = $value;
248
		}
249
		/*if (isset($request_data["lines"])) {
250
          $lines = array();
251
          foreach ($request_data["lines"] as $line) {
252
            array_push($lines, (object) $line);
253
          }
254
          $this->propal->lines = $lines;
255
        }*/
256
		if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
257
			throw new RestException(500, "Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
258
		}
259
260
		return $this->propal->id;
261
    }
262
263
	/**
264
	 * Get lines of a commercial proposal
265
	 *
266
	 * @param int   $id             Id of commercial proposal
267
	 *
268
	 * @url	GET {id}/lines
269
	 *
270
	 * @return int
271
	 */
272
    public function getLines($id)
273
    {
274
        if (!DolibarrApiAccess::$user->rights->propal->lire) {
275
            throw new RestException(401);
276
        }
277
278
	    $result = $this->propal->fetch($id);
279
	    if (!$result) {
280
		    throw new RestException(404, 'Commercial Proposal not found');
281
	    }
282
283
		if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
284
		    throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
285
	    }
286
	    $this->propal->getLinesArray();
287
	    $result = array();
288
	    foreach ($this->propal->lines as $line) {
289
		    array_push($result, $this->_cleanObjectDatas($line));
290
	    }
291
	    return $result;
292
	}
293
294
	/**
295
	 * Add a line to given commercial proposal
296
	 *
297
	 * @param int   $id             Id of commercial proposal to update
298
	 * @param array $request_data   Commercial proposal line data
299
	 *
300
	 * @url	POST {id}/lines
301
	 *
302
	 * @return int
303
	 */
304
	public function postLine($id, $request_data = null)
305
	{
306
		if (!DolibarrApiAccess::$user->rights->propal->creer) {
307
		  	throw new RestException(401);
308
		}
309
310
		$result = $this->propal->fetch($id);
311
		if (!$result) {
312
		    throw new RestException(404, 'Commercial Proposal not found');
313
		}
314
315
		if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
316
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
317
		}
318
319
        $request_data = (object) $request_data;
320
321
        $updateRes = $this->propal->addline(
322
                        $request_data->desc,
323
                        $request_data->subprice,
324
                        $request_data->qty,
325
                        $request_data->tva_tx,
326
                        $request_data->localtax1_tx,
327
                        $request_data->localtax2_tx,
328
                        $request_data->fk_product,
329
                        $request_data->remise_percent,
330
                        'HT',
331
                        0,
332
                        $request_data->info_bits,
333
                        $request_data->product_type,
334
                        $request_data->rang,
335
                        $request_data->special_code,
336
      					$request_data->fk_parent_line,
337
                        $request_data->fk_fournprice,
338
                        $request_data->pa_ht,
339
                        $request_data->label,
340
                        $request_data->date_start,
341
                        $request_data->date_end,
342
                        $request_data->array_options,
343
                        $request_data->fk_unit,
344
                        $request_data->origin,
345
                        $request_data->origin_id,
346
                        $request_data->multicurrency_subprice,
347
                        $request_data->fk_remise_except
348
        );
349
350
        if ($updateRes > 0) {
351
            return $updateRes;
352
        } else {
353
            throw new RestException(400, $this->propal->error);
354
        }
355
    }
356
357
	/**
358
	 * Update a line of given commercial proposal
359
	 *
360
	 * @param int   $id             Id of commercial proposal to update
361
	 * @param int   $lineid         Id of line to update
362
	 * @param array $request_data   Commercial proposal line data
363
	 *
364
	 * @url	PUT {id}/lines/{lineid}
365
	 *
366
	 * @return object
367
	 */
368
	public function putLine($id, $lineid, $request_data = null)
369
	{
370
		if (!DolibarrApiAccess::$user->rights->propal->creer) {
371
			throw new RestException(401);
372
		}
373
374
		$result = $this->propal->fetch($id);
375
		if ($result <= 0) {
376
			throw new RestException(404, 'Proposal not found');
377
		}
378
379
		if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
380
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
381
	  	}
382
383
	  	$request_data = (object) $request_data;
384
385
	  	$propalline = new PropaleLigne($this->db);
386
	  	$result = $propalline->fetch($lineid);
387
	  	if ($result <= 0) {
388
	  		throw new RestException(404, 'Proposal line not found');
389
	  	}
390
391
        $updateRes = $this->propal->updateline(
392
            $lineid,
393
            isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
394
            isset($request_data->qty) ? $request_data->qty : $propalline->qty,
395
            isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
396
            isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
397
            isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
398
            isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
399
            isset($request_data->desc) ? $request_data->desc : $propalline->desc,
400
            'HT',
401
            isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
402
            isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
403
            isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
404
            0,
405
            isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
406
            isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
407
            isset($request_data->label) ? $request_data->label : $propalline->label,
408
            isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
409
            isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
410
            isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
411
            isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
412
            isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
413
            isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice
414
        );
415
416
	    if ($updateRes > 0) {
417
		    $result = $this->get($id);
418
		    unset($result->line);
419
		    return $this->_cleanObjectDatas($result);
420
	    }
421
	    return false;
422
	}
423
424
	/**
425
	 * Delete a line of given commercial proposal
426
	 *
427
	 *
428
	 * @param int   $id             Id of commercial proposal to update
429
	 * @param int   $lineid         Id of line to delete
430
	 *
431
	 * @url	DELETE {id}/lines/{lineid}
432
	 *
433
	 * @return int
434
	 *
435
     * @throws RestException 401
436
     * @throws RestException 404
437
	 */
438
    public function deleteLine($id, $lineid)
439
    {
440
		if (!DolibarrApiAccess::$user->rights->propal->creer) {
441
            throw new RestException(401);
442
		}
443
444
		$result = $this->propal->fetch($id);
445
		if (!$result) {
446
			throw new RestException(404, 'Proposal not found');
447
		}
448
449
		if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
450
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
451
		}
452
453
		// TODO Check the lineid $lineid is a line of ojbect
454
455
		$updateRes = $this->propal->deleteline($lineid);
456
		if ($updateRes > 0) {
457
			return $this->get($id);
458
		} else {
459
			throw new RestException(405, $this->propal->error);
460
		}
461
	}
462
463
    /**
464
	 * Add a contact type of given commercial proposal
465
	 *
466
	 * @param int    $id             Id of commercial proposal to update
467
	 * @param int    $contactid      Id of contact to add
468
	 * @param string $type           Type of the contact (BILLING, SHIPPING, CUSTOMER)
469
	 *
470
	 * @url	POST {id}/contact/{contactid}/{type}
471
	 *
472
	 * @return int
473
	 *
474
     * @throws RestException 401
475
     * @throws RestException 404
476
	 */
477
    public function postContact($id, $contactid, $type)
478
    {
479
        if (!DolibarrApiAccess::$user->rights->propal->creer) {
480
            throw new RestException(401);
481
        }
482
483
        $result = $this->propal->fetch($id);
484
485
		if (!$result) {
486
			throw new RestException(404, 'Proposal not found');
487
		}
488
489
        if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
490
            throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
491
        }
492
493
        if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
494
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
495
		}
496
497
        $result = $this->propal->add_contact($contactid, $type, 'external');
498
499
        if (!$result) {
500
            throw new RestException(500, 'Error when added the contact');
501
        }
502
503
        return $this->propal;
504
    }
505
506
     /**
507
	 * Delete a contact type of given commercial proposal
508
	 *
509
	 * @param int    $id             Id of commercial proposal to update
510
	 * @param int    $rowid          Row key of the contact in the array contact_ids.
511
	 *
512
	 * @url	DELETE {id}/contact/{rowid}
513
	 *
514
	 * @return int
515
	 *
516
     * @throws RestException 401
517
     * @throws RestException 404
518
     * @throws RestException 500
519
	 */
520
    public function deleteContact($id, $rowid)
521
    {
522
        if (!DolibarrApiAccess::$user->rights->propal->creer) {
523
            throw new RestException(401);
524
        }
525
526
        $result = $this->propal->fetch($id);
527
528
		if (!$result) {
529
			throw new RestException(404, 'Proposal not found');
530
		}
531
532
        if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
533
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
534
		}
535
536
        $result = $this->propal->delete_contact($rowid);
537
538
        if (!$result) {
539
            throw new RestException(500, 'Error when deleted the contact');
540
        }
541
542
        return $this->propal;
543
    }
544
545
	/**
546
	 * Update commercial proposal general fields (won't touch lines of commercial proposal)
547
	 *
548
	 * @param int   $id             Id of commercial proposal to update
549
	 * @param array $request_data   Datas
550
	 *
551
	 * @return int
552
     */
553
    public function put($id, $request_data = null)
554
    {
555
        if (!DolibarrApiAccess::$user->rights->propal->creer) {
556
            throw new RestException(401);
557
        }
558
559
		$result = $this->propal->fetch($id);
560
		if (!$result) {
561
			throw new RestException(404, 'Proposal not found');
562
		}
563
564
		if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
565
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
566
		}
567
		foreach ($request_data as $field => $value) {
568
			if ($field == 'id') continue;
569
			$this->propal->$field = $value;
570
		}
571
572
		// update end of validity date
573
		if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation))
574
		{
575
			$this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
576
		}
577
		if (!empty($this->propal->fin_validite))
578
		{
579
			if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0)
580
			{
581
				throw new RestException(500, $this->propal->error);
582
			}
583
		}
584
585
		if ($this->propal->update(DolibarrApiAccess::$user) > 0)
586
		{
587
			return $this->get($id);
588
		} else {
589
			throw new RestException(500, $this->propal->error);
590
		}
591
	}
592
593
	/**
594
	 * Delete commercial proposal
595
	 *
596
	 * @param   int     $id         Commercial proposal ID
597
	 *
598
	 * @return  array
599
	 */
600
	public function delete($id)
601
	{
602
		if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
603
			throw new RestException(401);
604
		}
605
		$result = $this->propal->fetch($id);
606
		if (!$result) {
607
			throw new RestException(404, 'Commercial Proposal not found');
608
		}
609
610
		if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
611
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
612
		}
613
614
		if (!$this->propal->delete(DolibarrApiAccess::$user)) {
615
			throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
616
		}
617
618
		return array(
619
			'success' => array(
620
				'code' => 200,
621
				'message' => 'Commercial Proposal deleted'
622
			)
623
		);
624
	}
625
626
	/**
627
	* Set a proposal to draft
628
	*
629
	* @param   int     $id             Order ID
630
	*
631
	* @url POST    {id}/settodraft
632
	*
633
	* @return  array
634
	*/
635
	public function settodraft($id)
636
	{
637
		if (!DolibarrApiAccess::$user->rights->propal->creer) {
638
			throw new RestException(401);
639
		}
640
		$result = $this->propal->fetch($id);
641
		if (!$result) {
642
			throw new RestException(404, 'Proposal not found');
643
		}
644
645
		if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
646
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
647
		}
648
649
		$result = $this->propal->setDraft(DolibarrApiAccess::$user);
650
		if ($result == 0) {
651
			throw new RestException(304, 'Nothing done. May be object is already draft');
652
		}
653
		if ($result < 0) {
654
			throw new RestException(500, 'Error : '.$this->propal->error);
655
		}
656
657
		$result = $this->propal->fetch($id);
658
		if (!$result) {
659
			throw new RestException(404, 'Proposal not found');
660
		}
661
662
		if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
663
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
664
		}
665
666
		$this->propal->fetchObjectLinked();
667
668
		return $this->_cleanObjectDatas($this->propal);
669
	}
670
671
672
	/**
673
	 * Validate a commercial proposal
674
	 *
675
	 * If you get a bad value for param notrigger check that ou provide this in body
676
	 * {
677
	 * "notrigger": 0
678
	 * }
679
	 *
680
	 * @param   int     $id             Commercial proposal ID
681
	 * @param   int     $notrigger      1=Does not execute triggers, 0= execute triggers
682
	 *
683
	 * @url POST    {id}/validate
684
	 *
685
	 * @throws RestException 304
686
     * @throws RestException 401
687
     * @throws RestException 404
688
     * @throws RestException 500
689
     *
690
     * @return array
691
     */
692
    public function validate($id, $notrigger = 0)
693
    {
694
		if (!DolibarrApiAccess::$user->rights->propal->creer) {
695
			throw new RestException(401);
696
		}
697
		$result = $this->propal->fetch($id);
698
		if (!$result) {
699
			throw new RestException(404, 'Commercial Proposal not found');
700
		}
701
702
		if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
703
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
704
		}
705
706
		$result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
707
		if ($result == 0) {
708
			throw new RestException(304, 'Error nothing done. May be object is already validated');
709
		}
710
		if ($result < 0) {
711
			throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
712
		}
713
714
        $result = $this->propal->fetch($id);
715
        if (!$result) {
716
            throw new RestException(404, 'Commercial Proposal not found');
717
        }
718
719
        if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
720
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
721
        }
722
723
        $this->propal->fetchObjectLinked();
724
725
        return $this->_cleanObjectDatas($this->propal);
726
    }
727
728
	/**
729
	 * Close (Accept or refuse) a quote / commercial proposal
730
	 *
731
	 * @param   int     $id             Commercial proposal ID
732
	 * @param   int	    $status			Must be 2 (accepted) or 3 (refused)				{@min 2}{@max 3}
733
	 * @param   string  $note_private   Add this mention at end of private note
734
	 * @param   int     $notrigger      Disabled triggers
735
	 *
736
	 * @url POST    {id}/close
737
	 *
738
	 * @return  array
739
	 */
740
	public function close($id, $status, $note_private = '', $notrigger = 0)
741
	{
742
		if (!DolibarrApiAccess::$user->rights->propal->creer) {
743
			throw new RestException(401);
744
		}
745
		$result = $this->propal->fetch($id);
746
		if (!$result) {
747
			throw new RestException(404, 'Commercial Proposal not found');
748
		}
749
750
		if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
751
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
752
		}
753
754
		$result = $this->propal->cloture(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
755
		if ($result == 0) {
756
			throw new RestException(304, 'Error nothing done. May be object is already closed');
757
		}
758
		if ($result < 0) {
759
			throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
760
		}
761
762
		$result = $this->propal->fetch($id);
763
		if (!$result) {
764
			throw new RestException(404, 'Proposal not found');
765
		}
766
767
		if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
768
			throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
769
		}
770
771
		$this->propal->fetchObjectLinked();
772
773
		return $this->_cleanObjectDatas($this->propal);
774
	}
775
776
    /**
777
     * Set a commercial proposal billed. Could be also called setbilled
778
     *
779
     * @param   int     $id             Commercial proposal ID
780
     *
781
     * @url POST    {id}/setinvoiced
782
     *
783
     * @return  array
784
     */
785
    public function setinvoiced($id)
786
    {
787
        if (!DolibarrApiAccess::$user->rights->propal->creer) {
788
            throw new RestException(401);
789
        }
790
        $result = $this->propal->fetch($id);
791
        if (!$result) {
792
            throw new RestException(404, 'Commercial Proposal not found');
793
        }
794
795
        if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
796
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
797
        }
798
799
        $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
800
        if ($result < 0) {
801
            throw new RestException(500, 'Error : '.$this->propal->error);
802
        }
803
804
        $result = $this->propal->fetch($id);
805
        if (!$result) {
806
            throw new RestException(404, 'Proposal not found');
807
        }
808
809
        if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
810
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
811
        }
812
813
        $this->propal->fetchObjectLinked();
814
815
        return $this->_cleanObjectDatas($this->propal);
816
    }
817
818
819
	/**
820
	 * Validate fields before create or update object
821
	 *
822
	 * @param   array           $data   Array with data to verify
823
	 * @return  array
824
	 * @throws  RestException
825
	 */
826
	private function _validate($data)
827
	{
828
		$propal = array();
829
		foreach (Proposals::$FIELDS as $field) {
830
			if (!isset($data[$field]))
831
				throw new RestException(400, "$field field missing");
832
			$propal[$field] = $data[$field];
833
		}
834
		return $propal;
835
	}
836
837
838
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
839
    /**
840
     * Clean sensible object datas
841
     *
842
     * @param   object  $object    Object to clean
843
     * @return    array    Array of cleaned object properties
844
     */
845
    protected function _cleanObjectDatas($object)
846
    {
847
        // phpcs:enable
848
        $object = parent::_cleanObjectDatas($object);
849
850
        unset($object->note);
851
        unset($object->name);
852
        unset($object->lastname);
853
        unset($object->firstname);
854
        unset($object->civility_id);
855
        unset($object->address);
856
857
        return $object;
858
    }
859
}
860