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

Contracts::put()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 9
nop 2
dl 0
loc 24
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2015   Jean-François Ferry     <[email protected]>
3
 * Copyright (C) 2016	Laurent Destailleur		<[email protected]>
4
 * Copyright (C) 2018       Frédéric France         <[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.'/contrat/class/contrat.class.php';
23
24
/**
25
 * API class for contracts
26
 *
27
 * @access protected
28
 * @class  DolibarrApiAccess {@requires user,external}
29
 */
30
class Contracts extends DolibarrApi
31
{
32
33
    /**
34
     * @var array   $FIELDS     Mandatory fields, checked when create and update object
35
     */
36
    static $FIELDS = array(
37
        'socid',
38
        'date_contrat',
39
        'commercial_signature_id',
40
        'commercial_suivi_id'
41
    );
42
43
    /**
44
     * @var Contrat $contract {@type Contrat}
45
     */
46
    public $contract;
47
48
    /**
49
     * Constructor
50
     */
51
    public function __construct()
52
    {
53
        global $db, $conf;
54
        $this->db = $db;
55
        $this->contract = new Contrat($this->db);
56
    }
57
58
    /**
59
     * Get properties of a contract object
60
     *
61
     * Return an array with contract informations
62
     *
63
     * @param       int         $id         ID of contract
64
     * @return 	array|mixed data without useless information
65
     *
66
     * @throws 	RestException
67
     */
68
    public function get($id)
69
    {
70
        if (!DolibarrApiAccess::$user->rights->contrat->lire) {
71
            throw new RestException(401);
72
        }
73
74
        $result = $this->contract->fetch($id);
75
        if (!$result) {
76
            throw new RestException(404, 'Contract not found');
77
        }
78
79
        if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
80
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
81
        }
82
83
        $this->contract->fetchObjectLinked();
84
        return $this->_cleanObjectDatas($this->contract);
85
    }
86
87
88
89
    /**
90
     * List contracts
91
     *
92
     * Get a list of contracts
93
     *
94
     * @param string	       $sortfield	        Sort field
95
     * @param string	       $sortorder	        Sort order
96
     * @param int		       $limit		        Limit for list
97
     * @param int		       $page		        Page number
98
     * @param string   	       $thirdparty_ids	    Thirdparty ids to filter contracts of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i}
99
     * @param string           $sqlfilters          Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
100
     * @return  array                               Array of contract objects
101
     *
102
     * @throws RestException 404 Not found
103
     * @throws RestException 503 Error
104
     */
105
    public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
106
    {
107
        global $db, $conf;
108
109
        $obj_ret = array();
110
111
        // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
112
        $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
113
114
        // If the internal user must only see his customers, force searching by him
115
        $search_sale = 0;
116
        if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
117
118
        $sql = "SELECT t.rowid";
119
        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)
120
        $sql .= " FROM ".MAIN_DB_PREFIX."contrat as t";
121
122
        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
123
124
        $sql .= ' WHERE t.entity IN ('.getEntity('contrat').')';
125
        if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= " AND t.fk_soc = sc.fk_soc";
126
        if ($socids) $sql .= " AND t.fk_soc IN (".$socids.")";
127
        if ($search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
128
        // Insert sale filter
129
        if ($search_sale > 0)
130
        {
131
            $sql .= " AND sc.fk_user = ".$search_sale;
132
        }
133
        // Add sql filters
134
        if ($sqlfilters)
135
        {
136
            if (!DolibarrApi::_checkFilters($sqlfilters))
137
            {
138
                throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
139
            }
140
            $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
141
            $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
142
        }
143
144
        $sql .= $db->order($sortfield, $sortorder);
145
        if ($limit) {
146
            if ($page < 0)
147
            {
148
                $page = 0;
149
            }
150
            $offset = $limit * $page;
151
152
            $sql .= $db->plimit($limit + 1, $offset);
153
        }
154
155
        dol_syslog("API Rest request");
156
        $result = $db->query($sql);
157
158
        if ($result)
159
        {
160
            $num = $db->num_rows($result);
161
            $min = min($num, ($limit <= 0 ? $num : $limit));
162
            $i = 0;
163
            while ($i < $min)
164
            {
165
                $obj = $db->fetch_object($result);
166
                $contrat_static = new Contrat($db);
167
                if ($contrat_static->fetch($obj->rowid)) {
168
                    $obj_ret[] = $this->_cleanObjectDatas($contrat_static);
169
                }
170
                $i++;
171
            }
172
        } else {
173
            throw new RestException(503, 'Error when retrieve contrat list : '.$db->lasterror());
174
        }
175
        if (!count($obj_ret)) {
176
            throw new RestException(404, 'No contract found');
177
        }
178
        return $obj_ret;
179
    }
180
181
    /**
182
     * Create contract object
183
     *
184
     * @param   array   $request_data   Request data
185
     * @return  int     ID of contrat
186
     */
187
    public function post($request_data = null)
188
    {
189
        if (!DolibarrApiAccess::$user->rights->contrat->creer) {
190
            throw new RestException(401, "Insufficient rights");
191
        }
192
        // Check mandatory fields
193
        $result = $this->_validate($request_data);
194
195
        foreach ($request_data as $field => $value) {
196
            $this->contract->$field = $value;
197
        }
198
        /*if (isset($request_data["lines"])) {
199
          $lines = array();
200
          foreach ($request_data["lines"] as $line) {
201
            array_push($lines, (object) $line);
202
          }
203
          $this->contract->lines = $lines;
204
        }*/
205
        if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
206
            throw new RestException(500, "Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
207
        }
208
209
        return $this->contract->id;
210
    }
211
212
    /**
213
     * Get lines of a contract
214
     *
215
     * @param int   $id             Id of contract
216
     *
217
     * @url	GET {id}/lines
218
     *
219
     * @return array
220
     */
221
    public function getLines($id)
222
    {
223
        if (!DolibarrApiAccess::$user->rights->contrat->lire) {
224
            throw new RestException(401);
225
        }
226
227
        $result = $this->contract->fetch($id);
228
        if (!$result) {
229
            throw new RestException(404, 'Contract not found');
230
        }
231
232
        if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
233
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
234
        }
235
        $this->contract->getLinesArray();
236
        $result = array();
237
        foreach ($this->contract->lines as $line) {
238
            array_push($result, $this->_cleanObjectDatas($line));
239
        }
240
        return $result;
241
    }
242
243
    /**
244
     * Add a line to given contract
245
     *
246
     * @param int   $id             Id of contrat to update
247
     * @param array $request_data   Contractline data
248
     *
249
     * @url	POST {id}/lines
250
     *
251
     * @return int|bool
252
     */
253
    public function postLine($id, $request_data = null)
254
    {
255
        if (!DolibarrApiAccess::$user->rights->contrat->creer) {
256
            throw new RestException(401);
257
        }
258
259
        $result = $this->contract->fetch($id);
260
        if (!$result) {
261
            throw new RestException(404, 'Contract not found');
262
        }
263
264
        if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
265
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
266
        }
267
        $request_data = (object) $request_data;
268
        $updateRes = $this->contract->addline(
269
            $request_data->desc,
270
            $request_data->subprice,
271
            $request_data->qty,
272
            $request_data->tva_tx,
273
            $request_data->localtax1_tx,
274
            $request_data->localtax2_tx,
275
            $request_data->fk_product,
276
            $request_data->remise_percent,
277
            $request_data->date_start, // date_start = date planned start, date ouverture = date_start_real
278
            $request_data->date_end, // date_end = date planned end, date_cloture = date_end_real
279
            $request_data->HT,
280
            $request_data->subprice_excl_tax,
281
            $request_data->info_bits,
282
            $request_data->fk_fournprice,
283
            $request_data->pa_ht,
284
            $request_data->array_options,
285
            $request_data->fk_unit,
286
            $request_data->rang
287
        );
288
289
        if ($updateRes > 0) {
290
            return $updateRes;
291
        }
292
        return false;
293
    }
294
295
    /**
296
     * Update a line to given contract
297
     *
298
     * @param int   $id             Id of contrat to update
299
     * @param int   $lineid         Id of line to update
300
     * @param array $request_data   Contractline data
301
     *
302
     * @url	PUT {id}/lines/{lineid}
303
     *
304
     * @return array|bool
305
     */
306
    public function putLine($id, $lineid, $request_data = null)
307
    {
308
        if (!DolibarrApiAccess::$user->rights->contrat->creer) {
309
            throw new RestException(401);
310
        }
311
312
        $result = $this->contract->fetch($id);
313
        if (!$result) {
314
            throw new RestException(404, 'Contrat not found');
315
        }
316
317
        if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
318
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
319
        }
320
321
        $request_data = (object) $request_data;
322
323
        $updateRes = $this->contract->updateline(
324
            $lineid,
325
            $request_data->desc,
326
            $request_data->subprice,
327
            $request_data->qty,
328
            $request_data->remise_percent,
329
            $request_data->date_ouveture_prevue,
330
            $request_data->date_fin_validite,
331
            $request_data->tva_tx,
332
            $request_data->localtax1_tx,
333
            $request_data->localtax2_tx,
334
            $request_data->date_ouverture,
335
            $request_data->date_cloture,
336
            'HT',
337
            $request_data->info_bits,
338
            $request_data->fk_fourn_price,
339
            $request_data->pa_ht,
340
            $request_data->array_options,
341
            $request_data->fk_unit
342
        );
343
344
        if ($updateRes > 0) {
345
            $result = $this->get($id);
346
            unset($result->line);
347
            return $this->_cleanObjectDatas($result);
348
        }
349
350
        return false;
351
    }
352
353
    /**
354
     * Activate a service line of a given contract
355
     *
356
     * @param int   	$id             Id of contract to activate
357
     * @param int   	$lineid         Id of line to activate
358
     * @param string  	$datestart		{@from body}  Date start        {@type timestamp}
359
     * @param string    $dateend		{@from body}  Date end          {@type timestamp}
360
     * @param string    $comment  		{@from body}  Comment
361
     *
362
     * @url	PUT {id}/lines/{lineid}/activate
363
     *
364
     * @return array|bool
365
     */
366
    public function activateLine($id, $lineid, $datestart, $dateend = null, $comment = null)
367
    {
368
        if (!DolibarrApiAccess::$user->rights->contrat->creer) {
369
            throw new RestException(401);
370
        }
371
372
        $result = $this->contract->fetch($id);
373
        if (!$result) {
374
            throw new RestException(404, 'Contrat not found');
375
        }
376
377
        if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
378
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
379
        }
380
381
        $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, $datestart, $dateend, $comment);
382
383
        if ($updateRes > 0) {
384
            $result = $this->get($id);
385
            unset($result->line);
386
            return $this->_cleanObjectDatas($result);
387
        }
388
389
        return false;
390
    }
391
392
    /**
393
     * Unactivate a service line of a given contract
394
     *
395
     * @param int   	$id             Id of contract to activate
396
     * @param int   	$lineid         Id of line to activate
397
     * @param string  	$datestart		{@from body}  Date start        {@type timestamp}
398
     * @param string    $comment  		{@from body}  Comment
399
     *
400
     * @url	PUT {id}/lines/{lineid}/unactivate
401
     *
402
     * @return array|bool
403
     */
404
    public function unactivateLine($id, $lineid, $datestart, $comment = null)
405
    {
406
        if (!DolibarrApiAccess::$user->rights->contrat->creer) {
407
            throw new RestException(401);
408
        }
409
410
        $result = $this->contract->fetch($id);
411
        if (!$result) {
412
            throw new RestException(404, 'Contrat not found');
413
        }
414
415
        if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
416
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
417
        }
418
419
        $request_data = (object) $request_data;
420
421
        $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, $datestart, $comment);
422
423
        if ($updateRes > 0) {
424
            $result = $this->get($id);
425
            unset($result->line);
426
            return $this->_cleanObjectDatas($result);
427
        }
428
429
        return false;
430
    }
431
432
    /**
433
     * Delete a line to given contract
434
     *
435
     *
436
     * @param int   $id             Id of contract to update
437
     * @param int   $lineid         Id of line to delete
438
     *
439
     * @url	DELETE {id}/lines/{lineid}
440
     *
441
     * @return int
442
     *
443
     * @throws RestException 401
444
     * @throws RestException 404
445
     */
446
    public function deleteLine($id, $lineid)
447
    {
448
        if (!DolibarrApiAccess::$user->rights->contrat->creer) {
449
            throw new RestException(401);
450
        }
451
452
        $result = $this->contract->fetch($id);
453
        if (!$result) {
454
            throw new RestException(404, 'Contrat not found');
455
        }
456
457
        if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
458
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
459
        }
460
461
        // TODO Check the lineid $lineid is a line of object
462
463
        $updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user);
464
        if ($updateRes > 0) {
465
            return $this->get($id);
466
        } else {
467
              throw new RestException(405, $this->contract->error);
468
        }
469
    }
470
471
    /**
472
     * Update contract general fields (won't touch lines of contract)
473
     *
474
     * @param int   $id             Id of contract to update
475
     * @param array $request_data   Datas
476
     *
477
     * @return int
478
     */
479
    public function put($id, $request_data = null)
480
    {
481
        if (!DolibarrApiAccess::$user->rights->contrat->creer) {
482
            throw new RestException(401);
483
        }
484
485
        $result = $this->contract->fetch($id);
486
        if (!$result) {
487
            throw new RestException(404, 'Contrat not found');
488
        }
489
490
        if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
491
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
492
        }
493
        foreach ($request_data as $field => $value) {
494
            if ($field == 'id') continue;
495
            $this->contract->$field = $value;
496
        }
497
498
        if ($this->contract->update(DolibarrApiAccess::$user) > 0)
499
        {
500
            return $this->get($id);
501
        } else {
502
            throw new RestException(500, $this->contract->error);
503
        }
504
    }
505
506
    /**
507
     * Delete contract
508
     *
509
     * @param   int     $id         Contract ID
510
     *
511
     * @return  array
512
     */
513
    public function delete($id)
514
    {
515
        if (!DolibarrApiAccess::$user->rights->contrat->supprimer) {
516
            throw new RestException(401);
517
        }
518
        $result = $this->contract->fetch($id);
519
        if (!$result) {
520
            throw new RestException(404, 'Contract not found');
521
        }
522
523
        if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
524
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
525
        }
526
527
        if (!$this->contract->delete(DolibarrApiAccess::$user)) {
528
            throw new RestException(500, 'Error when delete contract : '.$this->contract->error);
529
        }
530
531
        return array(
532
            'success' => array(
533
                'code' => 200,
534
                'message' => 'Contract deleted'
535
            )
536
        );
537
    }
538
539
    /**
540
     * Validate a contract
541
     *
542
     * @param   int $id             Contract ID
543
     * @param   int $notrigger      1=Does not execute triggers, 0= execute triggers
544
     *
545
     * @url POST    {id}/validate
546
     *
547
     * @return  array
548
     * FIXME An error 403 is returned if the request has an empty body.
549
     * Error message: "Forbidden: Content type `text/plain` is not supported."
550
     * Workaround: send this in the body
551
     * {
552
     *   "notrigger": 0
553
     * }
554
     */
555
    public function validate($id, $notrigger = 0)
556
    {
557
        if (!DolibarrApiAccess::$user->rights->contrat->creer) {
558
            throw new RestException(401);
559
        }
560
        $result = $this->contract->fetch($id);
561
        if (!$result) {
562
            throw new RestException(404, 'Contract not found');
563
        }
564
565
        if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
566
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
567
        }
568
569
        $result = $this->contract->validate(DolibarrApiAccess::$user, '', $notrigger);
570
        if ($result == 0) {
571
            throw new RestException(304, 'Error nothing done. May be object is already validated');
572
        }
573
        if ($result < 0) {
574
            throw new RestException(500, 'Error when validating Contract: '.$this->contract->error);
575
        }
576
577
        return array(
578
            'success' => array(
579
                'code' => 200,
580
                'message' => 'Contract validated (Ref='.$this->contract->ref.')'
581
            )
582
        );
583
    }
584
585
    /**
586
     * Close all services of a contract
587
     *
588
     * @param   int $id             Contract ID
589
     * @param   int $notrigger      1=Does not execute triggers, 0= execute triggers
590
     *
591
     * @url POST    {id}/close
592
     *
593
     * @return  array
594
     * FIXME An error 403 is returned if the request has an empty body.
595
     * Error message: "Forbidden: Content type `text/plain` is not supported."
596
     * Workaround: send this in the body
597
     * {
598
     *   "notrigger": 0
599
     * }
600
     */
601
    public function close($id, $notrigger = 0)
602
    {
603
        if (!DolibarrApiAccess::$user->rights->contrat->creer) {
604
            throw new RestException(401);
605
        }
606
        $result = $this->contract->fetch($id);
607
        if (!$result) {
608
            throw new RestException(404, 'Contract not found');
609
        }
610
611
        if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
612
            throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
613
        }
614
615
        $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
616
        if ($result == 0) {
617
            throw new RestException(304, 'Error nothing done. May be object is already close');
618
        }
619
        if ($result < 0) {
620
            throw new RestException(500, 'Error when closing Contract: '.$this->contract->error);
621
        }
622
623
        return array(
624
            'success' => array(
625
                'code' => 200,
626
                'message' => 'Contract closed (Ref='.$this->contract->ref.'). All services were closed.'
627
            )
628
        );
629
    }
630
631
632
633
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
634
    /**
635
     * Clean sensible object datas
636
     *
637
     * @param   object  $object    Object to clean
638
     * @return    array    Array of cleaned object properties
639
     */
640
    protected function _cleanObjectDatas($object)
641
    {
642
        // phpcs:enable
643
        $object = parent::_cleanObjectDatas($object);
644
645
        unset($object->address);
646
647
        unset($object->date_ouverture_prevue);
648
        unset($object->date_ouverture);
649
        unset($object->date_fin_validite);
650
        unset($object->date_cloture);
651
        unset($object->date_debut_prevue);
652
        unset($object->date_debut_reel);
653
        unset($object->date_fin_prevue);
654
        unset($object->date_fin_reel);
655
        unset($object->civility_id);
656
657
        return $object;
658
    }
659
660
    /**
661
     * Validate fields before create or update object
662
     *
663
     * @param   array           $data   Array with data to verify
664
     * @return  array
665
     * @throws  RestException
666
     */
667
    private function _validate($data)
668
    {
669
        $contrat = array();
670
        foreach (Contracts::$FIELDS as $field) {
671
            if (!isset($data[$field]))
672
                throw new RestException(400, "$field field missing");
673
            $contrat[$field] = $data[$field];
674
        }
675
        return $contrat;
676
    }
677
}
678