Passed
Push — EXTRACT_CLASSES ( 0382f2...c25e41 )
by Rafael
52:18
created

Fiscalyear::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2014-2020  Alexandre Spangaro          <[email protected]>
4
 * Copyright (C) 2020       OScss-Shop                  <[email protected]>
5
 * Copyright (C) 2023-2024  Frédéric France             <[email protected]>
6
 * Copyright (C) 2024		MDW							<[email protected]>
7
 * Copyright (C) 2024       Rafael San José             <[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
namespace Dolibarr\Code\Core\Classes;
24
25
use Dolibarr\Core\Base\CommonObject;
26
27
/**
28
 *      \file       htdocs/core/class/fiscalyear.class.php
29
 *      \ingroup    fiscal year
30
 *      \brief      File of class to manage fiscal years
31
 */
32
33
/**
34
 * Class to manage fiscal year
35
 */
36
class Fiscalyear extends CommonObject
37
{
38
    /**
39
     * @var string ID to identify managed object
40
     */
41
    public $element = 'fiscalyear';
42
43
    /**
44
     * @var string picto
45
     */
46
    public $picto = 'calendar';
47
48
    /**
49
     * @var string Name of table without prefix where object is stored
50
     */
51
    public $table_element = 'accounting_fiscalyear';
52
53
    /**
54
     * @var string    Name of subtable line
55
     */
56
    public $table_element_line = '';
57
58
    /**
59
     * @var string Field with ID of parent key if this field has a parent
60
     */
61
    public $fk_element = '';
62
63
    /**
64
     * @var int ID
65
     */
66
    public $rowid;
67
68
    /**
69
     * @var string fiscal year label
70
     */
71
    public $label;
72
73
    /**
74
     * Date start (date_start)
75
     *
76
     * @var integer
77
     */
78
    public $date_start;
79
80
    /**
81
     * Date end (date_end)
82
     *
83
     * @var integer
84
     */
85
    public $date_end;
86
87
    /**
88
     * Date creation record (datec)
89
     *
90
     * @var integer
91
     */
92
    public $datec;
93
94
    /**
95
     * @var int status 0=open, 1=closed
96
     * @deprecated
97
     * @see $status
98
     */
99
    public $statut;
100
101
    /**
102
     * @var int status 0=open, 1=closed
103
     */
104
    public $status;
105
106
    /**
107
     * @var int Entity
108
     */
109
    public $entity;
110
111
112
    const STATUS_OPEN = 0;
113
    const STATUS_CLOSED = 1;
114
115
116
    /**
117
     * Constructor
118
     *
119
     * @param   DoliDB      $db     Database handler
120
     */
121
    public function __construct(DoliDB $db)
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Core\Classes\DoliDB was not found. Did you mean DoliDB? If so, make sure to prefix the type with \.
Loading history...
122
    {
123
        $this->db = $db;
124
125
        $this->ismultientitymanaged = 1;
126
        $this->labelStatusShort = array(self::STATUS_OPEN => 'Opened', self::STATUS_CLOSED => 'Closed');
127
        $this->labelStatus = array(self::STATUS_OPEN => 'Opened', self::STATUS_CLOSED => 'Closed');
128
    }
129
130
    /**
131
     *  Create object in database
132
     *
133
     *  @param      User    $user   User making creation
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Core\Classes\User was not found. Did you mean User? If so, make sure to prefix the type with \.
Loading history...
134
     *  @return     int             Return integer <0 if KO, >0 if OK
135
     */
136
    public function create($user)
137
    {
138
        global $conf;
139
140
        $error = 0;
141
142
        $now = dol_now();
143
144
        $this->db->begin();
145
146
        $sql = "INSERT INTO " . $this->db->prefix() . "accounting_fiscalyear (";
147
        $sql .= "label";
148
        $sql .= ", date_start";
149
        $sql .= ", date_end";
150
        $sql .= ", statut";
151
        $sql .= ", entity";
152
        $sql .= ", datec";
153
        $sql .= ", fk_user_author";
154
        $sql .= ") VALUES (";
155
        $sql .= " '" . $this->db->escape($this->label) . "'";
156
        $sql .= ", '" . $this->db->idate($this->date_start) . "'";
157
        $sql .= ", " . ($this->date_end ? "'" . $this->db->idate($this->date_end) . "'" : "null");
158
        $sql .= ", 0";
159
        $sql .= ", " . ((int) $conf->entity);
160
        $sql .= ", '" . $this->db->idate($now) . "'";
161
        $sql .= ", " . ((int) $user->id);
162
        $sql .= ")";
163
164
        dol_syslog(get_class($this) . "::create", LOG_DEBUG);
165
        $result = $this->db->query($sql);
166
        if ($result) {
167
            $this->id = $this->db->last_insert_id($this->db->prefix() . "accounting_fiscalyear");
168
169
            $result = $this->update($user);
170
            if ($result > 0) {
171
                $this->db->commit();
172
                return $this->id;
173
            } else {
174
                $this->error = $this->db->lasterror();
175
                $this->db->rollback();
176
                return $result;
177
            }
178
        } else {
179
            $this->error = $this->db->lasterror() . " sql=" . $sql;
180
            $this->db->rollback();
181
            return -1;
182
        }
183
    }
184
185
    /**
186
     *  Update record
187
     *
188
     *  @param  User    $user       User making update
189
     *  @return int                 Return integer <0 if KO, >0 if OK
190
     */
191
    public function update($user)
192
    {
193
        // Check parameters
194
        if (empty($this->date_start) && empty($this->date_end)) {
195
            $this->error = 'ErrorBadParameter';
196
            return -1;
197
        }
198
199
        $this->db->begin();
200
201
        $sql = "UPDATE " . $this->db->prefix() . "accounting_fiscalyear";
202
        $sql .= " SET label = '" . $this->db->escape($this->label) . "'";
203
        $sql .= ", date_start = '" . $this->db->idate($this->date_start) . "'";
204
        $sql .= ", date_end = " . ($this->date_end ? "'" . $this->db->idate($this->date_end) . "'" : "null");
205
        $sql .= ", statut = '" . $this->db->escape($this->status ? $this->status : 0) . "'";
206
        $sql .= ", fk_user_modif = " . ((int) $user->id);
207
        $sql .= " WHERE rowid = " . ((int) $this->id);
208
209
        dol_syslog(get_class($this) . "::update", LOG_DEBUG);
210
        $result = $this->db->query($sql);
211
        if ($result) {
212
            $this->db->commit();
213
            return 1;
214
        } else {
215
            $this->error = $this->db->lasterror();
216
            dol_syslog($this->error, LOG_ERR);
217
            $this->db->rollback();
218
            return -1;
219
        }
220
    }
221
222
    /**
223
     * Load an object from database
224
     *
225
     * @param   int     $id     Id of record to load
226
     * @return  int             Return integer <0 if KO, >0 if OK
227
     */
228
    public function fetch($id)
229
    {
230
        $sql = "SELECT rowid, label, date_start, date_end, statut as status";
231
        $sql .= " FROM " . $this->db->prefix() . "accounting_fiscalyear";
232
        $sql .= " WHERE rowid = " . ((int) $id);
233
234
        dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
235
        $result = $this->db->query($sql);
236
        if ($result) {
237
            $obj = $this->db->fetch_object($result);
238
239
            $this->id = $obj->rowid;
240
            $this->ref = $obj->rowid;
241
            $this->date_start   = $this->db->jdate($obj->date_start);
242
            $this->date_end = $this->db->jdate($obj->date_end);
243
            $this->label = $obj->label;
244
            $this->statut = $obj->status;
245
            $this->status = $obj->status;
246
247
            return 1;
248
        } else {
249
            $this->error = $this->db->lasterror();
250
            return -1;
251
        }
252
    }
253
254
    /**
255
     *  Delete record
256
     *
257
     *  @param  User    $user   User that delete
258
     *  @return int             Return integer <0 if KO, >0 if OK
259
     */
260
    public function delete($user)
261
    {
262
        $this->db->begin();
263
264
        $sql = "DELETE FROM " . $this->db->prefix() . "accounting_fiscalyear";
265
        $sql .= " WHERE rowid = " . ((int) $this->id);
266
267
        $result = $this->db->query($sql);
268
        if ($result) {
269
            $this->db->commit();
270
            return 1;
271
        } else {
272
            $this->error = $this->db->lasterror();
273
            $this->db->rollback();
274
            return -1;
275
        }
276
    }
277
278
    /**
279
     * getTooltipContentArray
280
     *
281
     * @param array $params ex option, infologin
282
     * @since v18
283
     * @return array
284
     */
285
    public function getTooltipContentArray($params)
286
    {
287
        global $langs;
288
289
        $langs->load('compta');
290
291
        $datas = [];
292
        $datas['picto'] = img_picto('', $this->picto) . ' <b><u>' . $langs->trans("FiscalPeriod") . '</u></b>';
293
        if (isset($this->status)) {
294
            $datas['picto'] .= ' ' . $this->getLibStatut(5);
295
        }
296
        $datas['ref'] = '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
297
        if (isset($this->date_start)) {
298
            $datas['date_start'] = '<br><b>' . $langs->trans('DateStart') . ':</b> ' . dol_print_date($this->date_start, 'day');
299
        }
300
        if (isset($this->date_start)) {
301
            $datas['date_end'] = '<br><b>' . $langs->trans('DateEnd') . ':</b> ' . dol_print_date($this->date_end, 'day');
302
        }
303
304
        return $datas;
305
    }
306
307
    /**
308
     *  Return clicable link of object (with eventually picto)
309
     *
310
     *  @param      int         $withpicto                Add picto into link
311
     *  @param      int         $notooltip                1=Disable tooltip
312
     *  @param      int         $save_lastsearch_value    -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
313
     *  @return     string                                String with URL
314
     */
315
    public function getNomUrl($withpicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
316
    {
317
        global $conf, $langs, $user;
318
319
        if (empty($this->ref)) {
320
            $this->ref = (string) $this->id;
321
        }
322
323
        if (!empty($conf->dol_no_mouse_hover)) {
324
            $notooltip = 1; // Force disable tooltips
325
        }
326
        $option = '';
327
        if (!$user->hasRight('accounting', 'fiscalyear', 'write')) {
328
            $option = 'nolink';
329
        }
330
        $result = '';
331
        $params = [
332
            'id' => $this->id,
333
            'objecttype' => $this->element,
334
            'option' => $option,
335
            'nofetch' => 1,
336
        ];
337
        $classfortooltip = 'classfortooltip';
338
        $dataparams = '';
339
        if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
340
            $classfortooltip = 'classforajaxtooltip';
341
            $dataparams = ' data-params="' . dol_escape_htmltag(json_encode($params)) . '"';
342
            $label = 'ToComplete';
343
        } else {
344
            $label = implode($this->getTooltipContentArray($params));
345
        }
346
        $url = constant('BASE_URL') . '/accountancy/admin/fiscalyear_card.php?id=' . $this->id;
347
348
        if ($option !== 'nolink') {
349
            // Add param to save lastsearch_values or not
350
            $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
351
            if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
352
                $add_save_lastsearch_values = 1;
353
            }
354
            if ($add_save_lastsearch_values) {
355
                $url .= '&save_lastsearch_values=1';
356
            }
357
        }
358
359
        $linkclose = '';
360
        if (empty($notooltip) && $user->hasRight('accounting', 'fiscalyear', 'write')) {
361
            if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
362
                $label = $langs->trans("FiscalPeriod");
363
                $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"';
364
            }
365
            $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"';
366
            $linkclose .= $dataparams . ' class="' . $classfortooltip . '"';
367
        }
368
369
        $linkstart = '<a href="' . $url . '"';
370
        $linkstart .= $linkclose . '>';
371
        $linkend = '</a>';
372
373
        if ($option === 'nolink') {
374
            $linkstart = '';
375
            $linkend = '';
376
        }
377
378
        $result .= $linkstart;
379
        if ($withpicto) {
380
            $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams . ' class="' . (($withpicto != 2) ? 'paddingright ' : '') . $classfortooltip . '"'), 0, 0, $notooltip ? 0 : 1);
381
        }
382
        if ($withpicto != 2) {
383
            $result .= $this->ref;
384
        }
385
        $result .= $linkend;
386
387
        return $result;
388
    }
389
390
    /**
391
     * Give a label from a status
392
     *
393
     * @param   int     $mode       0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
394
     * @return  string              Label
395
     */
396
    public function getLibStatut($mode = 0)
397
    {
398
        return $this->LibStatut($this->status, $mode);
399
    }
400
401
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
402
    /**
403
     *  Give a label from a status
404
     *
405
     *  @param  int     $status     Id status
406
     *  @param  int     $mode       0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
407
     *  @return string              Label
408
     */
409
    public function LibStatut($status, $mode = 0)
410
    {
411
		// phpcs:enable
412
        if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
413
            global $langs;
414
            //$langs->load("mymodule@mymodule");
415
            $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Draft');
416
            $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Enabled');
417
            $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
418
            $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Disabled');
419
        }
420
421
        $statusType = 'status4';
422
        //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
423
        if ($status == self::STATUS_CLOSED) {
424
            $statusType = 'status6';
425
        }
426
427
        return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
428
    }
429
430
    /**
431
     * Information on record
432
     *
433
     * @param   int     $id      Id of record
434
     * @return  void
435
     */
436
    public function info($id)
437
    {
438
        $sql = "SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,";
439
        $sql .= " fy.tms as datem";
440
        $sql .= " FROM " . $this->db->prefix() . "accounting_fiscalyear as fy";
441
        $sql .= " WHERE fy.rowid = " . ((int) $id);
442
443
        dol_syslog(get_class($this) . "::fetch info", LOG_DEBUG);
444
        $result = $this->db->query($sql);
445
446
        if ($result) {
447
            if ($this->db->num_rows($result)) {
448
                $obj = $this->db->fetch_object($result);
449
450
                $this->id = $obj->rowid;
451
452
                $this->user_creation_id = $obj->fk_user_author;
453
                $this->user_modification_id = $obj->fk_user_modif;
454
                $this->date_creation     = $this->db->jdate($obj->datec);
455
                $this->date_modification = $this->db->jdate($obj->datem);
456
            }
457
            $this->db->free($result);
458
        } else {
459
            dol_print_error($this->db);
460
        }
461
    }
462
463
    /**
464
     *  Return the number of entries by fiscal year
465
     *
466
     *  @param  int|string      $datestart  Date start to scan
467
     *  @param  int|string      $dateend    Date end to scan
468
     *  @return string          Number of entries
469
     */
470
    public function getAccountancyEntriesByFiscalYear($datestart = '', $dateend = '')
471
    {
472
        global $conf;
473
474
        if (empty($datestart)) {
475
            $datestart = $this->date_start;
476
        }
477
        if (empty($dateend)) {
478
            $dateend = $this->date_end;
479
        }
480
481
        $sql = "SELECT count(DISTINCT piece_num) as nb";
482
        $sql .= " FROM " . $this->db->prefix() . "accounting_bookkeeping";
483
        $sql .= " WHERE entity IN (" . getEntity('bookkeeping', 0) . ")";
484
        $sql .= " AND doc_date >= '" . $this->db->idate($datestart) . "' and doc_date <= '" . $this->db->idate($dateend) . "'";
485
486
        $resql = $this->db->query($sql);
487
        if ($resql) {
488
            $obj = $this->db->fetch_object($resql);
489
            $nb = $obj->nb;
490
        } else {
491
            dol_print_error($this->db);
492
        }
493
494
        return $nb;
495
    }
496
497
    /**
498
     *  Return the number of movements by fiscal year
499
     *
500
     *  @param  int|string      $datestart  Date start to scan
501
     *  @param  int|string      $dateend    Date end to scan
502
     *  @return string              Number of movements
503
     */
504
    public function getAccountancyMovementsByFiscalYear($datestart = '', $dateend = '')
505
    {
506
        global $conf;
507
508
        if (empty($datestart)) {
509
            $datestart = $this->date_start;
510
        }
511
        if (empty($dateend)) {
512
            $dateend = $this->date_end;
513
        }
514
515
        $sql = "SELECT count(rowid) as nb";
516
        $sql .= " FROM " . $this->db->prefix() . "accounting_bookkeeping ";
517
        $sql .= " WHERE entity IN (" . getEntity('bookkeeping', 0) . ")";
518
        $sql .= " AND doc_date >= '" . $this->db->idate($datestart) . "' and doc_date <= '" . $this->db->idate($dateend) . "'";
519
520
        $resql = $this->db->query($sql);
521
        if ($resql) {
522
            $obj = $this->db->fetch_object($resql);
523
            $nb = $obj->nb;
524
        } else {
525
            dol_print_error($this->db);
526
        }
527
528
        return $nb;
529
    }
530
}
531