Passed
Push — GENERAL_BUG_REVIEW_240911 ( 776d89...c757bd )
by Rafael
50:33
created

PaymentVarious::lengthAccounta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2017-2021  Alexandre Spangaro          <[email protected]>
4
 * Copyright (C) 2018-2024  Frédéric France             <[email protected]>
5
 * Copyright (C) 2024		MDW							<[email protected]>
6
 * Copyright (C) 2024       Rafael San José             <[email protected]>
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Dolibarr\Code\Compta\Classes;
23
24
/**
25
 *  \file       htdocs/compta/bank/class/paymentvarious.class.php
26
 *  \ingroup    bank
27
 *  \brief      Class for various payment
28
 */
29
30
// Put here all includes required by your class file
31
use Dolibarr\Core\Base\CommonObject;
32
use DoliDB;
33
34
35
/**
36
 *  Class to manage various payments
37
 */
38
class PaymentVarious extends CommonObject
39
{
40
    /**
41
     * @var string ID to identify managed object
42
     */
43
    public $element = 'payment_various';
44
45
    /**
46
     * @var string Name of table without prefix where object is stored
47
     */
48
    public $table_element = 'payment_various';
49
50
    /**
51
     * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
52
     */
53
    public $picto = 'payment';
54
55
    /**
56
     * @var int ID
57
     */
58
    public $id;
59
60
    /**
61
     * @var string Ref
62
     */
63
    public $ref;
64
65
    /**
66
     * @var int timestamp
67
     */
68
    public $datep;
69
70
    /**
71
     * @var int timestamp
72
     */
73
    public $datev;
74
75
    /**
76
     * @var int<0,1> Payment direction (debit or credit)
77
     */
78
    public $sens;
79
80
    /**
81
     * @var float
82
     */
83
    public $amount;
84
85
    /**
86
     * @var int Payment type (fk_typepayment)
87
     */
88
    public $type_payment;
89
90
    /**
91
     * @var string      Payment reference
92
     *                  (Cheque or bank transfer reference. Can be "ABC123")
93
     */
94
    public $num_payment;
95
96
    /**
97
     * @var string Name of cheque writer
98
     */
99
    public $chqemetteur;
100
101
    /**
102
     * @var string Bank of cheque writer
103
     */
104
    public $chqbank;
105
106
    /**
107
     * @var int Category id
108
     */
109
    public $category_transaction;
110
111
    /**
112
     * @var string various payments label
113
     */
114
    public $label;
115
116
    /**
117
     * @var string accountancy code
118
     */
119
    public $accountancy_code;
120
121
    /**
122
     * @var string subledger account
123
     */
124
    public $subledger_account;
125
126
    /**
127
     * @var int ID
128
     */
129
    public $fk_project;
130
131
    /**
132
     * @var int Bank account ID
133
     */
134
    public $fk_account;
135
136
    /**
137
     * @var int Bank account ID
138
     * @deprecated See fk_account
139
     */
140
    public $accountid;
141
142
    /**
143
     * @var int ID record into llx_bank
144
     */
145
    public $fk_bank;
146
147
    /**
148
     * @var int transaction category
149
     */
150
    public $categorie_transaction;
151
152
    /**
153
     * @var int ID
154
     */
155
    public $fk_user_author;
156
157
    /**
158
     * @var int ID
159
     */
160
    public $fk_user_modif;
161
162
163
    /**
164
     * @var int     Type of bank account if the payment is on a bank account
165
     */
166
    public $fk_type;
167
168
    /**
169
     * @var int     1 if the payment is on a bank account line that is conciliated
170
     */
171
    public $rappro;
172
173
    /**
174
     * @var string  ID of bank receipt
175
     */
176
    public $bank_num_releve;
177
178
179
    /**
180
     *  'type' if the field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
181
     *         Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
182
     *  'label' the translation key.
183
     *  'enabled' is a condition when the field must be managed (Example: 1 or 'getDolGlobalString("MY_SETUP_PARAM")'
184
     *  'position' is the sort order of field.
185
     *  'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
186
     *  'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)
187
     *  'noteditable' says if field is not editable (1 or 0)
188
     *  'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created.
189
     *  'index' if we want an index in database.
190
     *  'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommended to name the field fk_...).
191
     *  'searchall' is 1 if we want to search in this field when making a search from the quick search button.
192
     *  'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
193
     *  'css' is the CSS style to use on field. For example: 'maxwidth200'
194
     *  'help' is a string visible as a tooltip on field
195
     *  'showoncombobox' if value of the field must be visible into the label of the combobox that list record
196
     *  'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code.
197
     *  'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
198
     *  'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
199
     *  'comment' is not used. You can store here any text of your choice. It is not used by application.
200
     *
201
     *  Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
202
     */
203
204
    // BEGIN MODULEBUILDER PROPERTIES
205
    /**
206
     * @var array<string,array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array<int,string>,comment?:string}>  Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string,array{type:...ring>,comment?:string}> at position 16 could not be parsed: Expected '}' at position 16, but found 'int'.
Loading history...
207
     */
208
    public $fields = array(
209
        // TODO: fill this array
210
    );
211
    // END MODULEBUILDER PROPERTIES
212
213
    /**
214
     * Draft status
215
     */
216
    const STATUS_DRAFT = 0;
217
218
219
    /**
220
     *  Constructor
221
     *
222
     *  @param      DoliDB      $db      Database handler
223
     */
224
    public function __construct(DoliDB $db)
225
    {
226
        $this->db = $db;
227
    }
228
229
    /**
230
     * Update database
231
     *
232
     * @param   User    $user           User that modify
233
     * @param   int     $notrigger      0=no, 1=yes (no update trigger)
234
     * @return  int                     Return integer <0 if KO, >0 if OK
235
     */
236
    public function update($user = null, $notrigger = 0)
237
    {
238
        global $conf, $langs;
239
240
        $error = 0;
241
242
        // Clean parameters
243
        $this->amount = (float) price2num($this->amount);
244
        $this->label = trim($this->label);
245
        $this->note = trim($this->note);
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$note has been deprecated: Use $note_private instead. ( Ignorable by Annotation )

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

245
        /** @scrutinizer ignore-deprecated */ $this->note = trim($this->note);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
246
        $this->fk_bank = (int) $this->fk_bank;
247
        $this->fk_user_author = (int) $this->fk_user_author;
248
        $this->fk_user_modif = (int) $this->fk_user_modif;
249
250
        $this->db->begin();
251
252
        // Update request
253
        $sql = "UPDATE " . MAIN_DB_PREFIX . "payment_various SET";
254
        if ($this->tms) {
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$tms has been deprecated: Use $date_modification ( Ignorable by Annotation )

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

254
        if (/** @scrutinizer ignore-deprecated */ $this->tms) {

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
255
            $sql .= " tms='" . $this->db->idate($this->tms) . "',";
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$tms has been deprecated: Use $date_modification ( Ignorable by Annotation )

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

255
            $sql .= " tms='" . $this->db->idate(/** @scrutinizer ignore-deprecated */ $this->tms) . "',";

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
256
        }
257
        $sql .= " datep='" . $this->db->idate($this->datep) . "',";
258
        $sql .= " datev='" . $this->db->idate($this->datev) . "',";
259
        $sql .= " sens=" . (int) $this->sens . ",";
260
        $sql .= " amount=" . price2num($this->amount) . ",";
261
        $sql .= " fk_typepayment=" . (int) $this->type_payment . ",";
262
        $sql .= " num_payment='" . $this->db->escape($this->num_payment) . "',";
263
        $sql .= " label='" . $this->db->escape($this->label) . "',";
264
        $sql .= " note='" . $this->db->escape($this->note) . "',";
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$note has been deprecated: Use $note_private instead. ( Ignorable by Annotation )

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

264
        $sql .= " note='" . $this->db->escape(/** @scrutinizer ignore-deprecated */ $this->note) . "',";

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
265
        $sql .= " accountancy_code='" . $this->db->escape($this->accountancy_code) . "',";
266
        $sql .= " subledger_account='" . $this->db->escape($this->subledger_account) . "',";
267
        $sql .= " fk_projet='" . $this->db->escape($this->fk_project) . "',";
268
        $sql .= " fk_bank=" . ($this->fk_bank > 0 ? $this->fk_bank : "null") . ",";
269
        $sql .= " fk_user_author=" . (int) $this->fk_user_author . ",";
270
        $sql .= " fk_user_modif=" . (int) $this->fk_user_modif;
271
        $sql .= " WHERE rowid=" . ((int) $this->id);
272
273
        dol_syslog(get_class($this) . "::update", LOG_DEBUG);
274
        $resql = $this->db->query($sql);
275
        if (!$resql) {
276
            $this->error = "Error " . $this->db->lasterror();
277
            return -1;
278
        }
279
280
        if (!$notrigger) {
281
            // Call trigger
282
            $result = $this->call_trigger('PAYMENT_VARIOUS_MODIFY', $user);
283
            if ($result < 0) {
284
                $error++;
285
            }
286
            // End call triggers
287
        }
288
289
        if (!$error) {
290
            $this->db->commit();
291
            return 1;
292
        } else {
293
            $this->db->rollback();
294
            return -1;
295
        }
296
    }
297
298
299
    /**
300
     *  Load object in memory from database
301
     *
302
     *  @param  int     $id         id object
303
     *  @param  User    $user       User that load
304
     *  @return int                 Return integer <0 if KO, >0 if OK
305
     */
306
    public function fetch($id, $user = null)
307
    {
308
        $sql = "SELECT";
309
        $sql .= " v.rowid,";
310
        $sql .= " v.tms,";
311
        $sql .= " v.datep,";
312
        $sql .= " v.datev,";
313
        $sql .= " v.sens,";
314
        $sql .= " v.amount,";
315
        $sql .= " v.fk_typepayment,";
316
        $sql .= " v.num_payment,";
317
        $sql .= " v.label,";
318
        $sql .= " v.note as note_private,";
319
        $sql .= " v.accountancy_code,";
320
        $sql .= " v.subledger_account,";
321
        $sql .= " v.fk_projet as fk_project,";
322
        $sql .= " v.fk_bank,";
323
        $sql .= " v.fk_user_author,";
324
        $sql .= " v.fk_user_modif,";
325
        $sql .= " b.fk_account,";
326
        $sql .= " b.fk_type,";
327
        $sql .= " b.rappro,";
328
        $sql .= " b.num_releve as bank_num_releve";
329
        $sql .= " FROM " . MAIN_DB_PREFIX . "payment_various as v";
330
        $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank as b ON v.fk_bank = b.rowid";
331
        $sql .= " WHERE v.rowid = " . ((int) $id);
332
333
        dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
334
        $resql = $this->db->query($sql);
335
        if ($resql) {
336
            if ($this->db->num_rows($resql)) {
337
                $obj = $this->db->fetch_object($resql);
338
339
                $this->id                   = $obj->rowid;
340
                $this->ref                  = $obj->rowid;
341
                $this->tms                  = $this->db->jdate($obj->tms);
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$tms has been deprecated: Use $date_modification ( Ignorable by Annotation )

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

341
                /** @scrutinizer ignore-deprecated */ $this->tms                  = $this->db->jdate($obj->tms);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
Documentation Bug introduced by
It seems like $this->db->jdate($obj->tms) can also be of type string. However, the property $tms is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
342
                $this->datep                = $this->db->jdate($obj->datep);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->db->jdate($obj->datep) can also be of type string. However, the property $datep is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
343
                $this->datev                = $this->db->jdate($obj->datev);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->db->jdate($obj->datev) can also be of type string. However, the property $datev is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
344
                $this->sens                 = $obj->sens;
345
                $this->amount               = $obj->amount;
346
                $this->type_payment         = $obj->fk_typepayment;
347
                $this->num_payment          = $obj->num_payment;
348
                $this->label                = $obj->label;
349
                $this->note                 = $obj->note_private;   // For backward compatibility
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$note has been deprecated: Use $note_private instead. ( Ignorable by Annotation )

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

349
                /** @scrutinizer ignore-deprecated */ $this->note                 = $obj->note_private;   // For backward compatibility

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
350
                $this->note_private         = $obj->note_private;
351
                $this->subledger_account    = $obj->subledger_account;
352
                $this->accountancy_code     = $obj->accountancy_code;
353
                $this->fk_project           = $obj->fk_project;
354
                $this->fk_bank              = $obj->fk_bank;
355
                $this->fk_user_author       = $obj->fk_user_author;
356
                $this->fk_user_modif        = $obj->fk_user_modif;
357
                $this->fk_account           = $obj->fk_account;
358
                $this->fk_type              = $obj->fk_type;
359
                $this->rappro               = $obj->rappro;
360
                $this->bank_num_releve      = $obj->bank_num_releve;
361
            }
362
            $this->db->free($resql);
363
364
            return 1;
365
        } else {
366
            $this->error = "Error " . $this->db->lasterror();
367
            return -1;
368
        }
369
    }
370
371
372
    /**
373
     *  Delete object in database
374
     *
375
     *  @param  User    $user       User that delete
376
     *  @return int                 Return integer <0 if KO, >0 if OK
377
     */
378
    public function delete($user)
379
    {
380
        global $conf, $langs;
381
382
        $error = 0;
383
384
        // Call trigger
385
        $result = $this->call_trigger('PAYMENT_VARIOUS_DELETE', $user);
386
        if ($result < 0) {
387
            return -1;
388
        }
389
        // End call triggers
390
391
392
        $sql = "DELETE FROM " . MAIN_DB_PREFIX . "payment_various";
393
        $sql .= " WHERE rowid=" . ((int) $this->id);
394
395
        dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
396
        $resql = $this->db->query($sql);
397
        if (!$resql) {
398
            $this->error = "Error " . $this->db->lasterror();
399
            return -1;
400
        }
401
402
        return 1;
403
    }
404
405
406
    /**
407
     *  Initialise an instance with random values.
408
     *  Used to build previews or test instances.
409
     *  id must be 0 if object instance is a specimen.
410
     *
411
     *  @return int
412
     */
413
    public function initAsSpecimen()
414
    {
415
        $this->id = 0;
416
        $this->tms = dol_now();
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$tms has been deprecated: Use $date_modification ( Ignorable by Annotation )

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

416
        /** @scrutinizer ignore-deprecated */ $this->tms = dol_now();

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
417
        $this->datep = dol_now();
418
        $this->datev = dol_now();
419
        $this->sens = 0;
420
        $this->amount = 100;
421
        $this->label = 'Specimen payment';
422
        $this->accountancy_code = '';
423
        $this->subledger_account = '';
424
        $this->note = '';
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$note has been deprecated: Use $note_private instead. ( Ignorable by Annotation )

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

424
        /** @scrutinizer ignore-deprecated */ $this->note = '';

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
425
        $this->fk_bank = 0;
426
        $this->fk_user_author = 0;
427
        $this->fk_user_modif = 0;
428
429
        return 1;
430
    }
431
432
    /**
433
     * Check if a miscellaneous payment can be created into database
434
     *
435
     * @return  boolean     True or false
436
     */
437
    public function check()
438
    {
439
        $newamount = price2num($this->amount, 'MT');
440
441
        // Validation of parameters
442
        if (!($newamount) > 0 || empty($this->datep)) {
443
            return false;
444
        }
445
446
        return true;
447
    }
448
449
    /**
450
     *  Create in database
451
     *
452
     *  @param   User   $user   User that create
453
     *  @return  int            Return integer <0 if KO, >0 if OK
454
     */
455
    public function create($user)
456
    {
457
        global $conf, $langs;
458
459
        $error = 0;
460
        $now = dol_now();
461
462
        // Clean parameters
463
        $this->amount = (float) price2num($this->amount);
464
        $this->label = trim($this->label);
465
        $this->note = trim($this->note);
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$note has been deprecated: Use $note_private instead. ( Ignorable by Annotation )

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

465
        /** @scrutinizer ignore-deprecated */ $this->note = trim($this->note);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
466
        $this->fk_bank = (int) $this->fk_bank;
467
        $this->fk_user_author = (int) $this->fk_user_author;
468
        $this->fk_user_modif = (int) $this->fk_user_modif;
469
        $this->fk_account = (int) $this->fk_account;
470
        if (empty($this->fk_account) && isset($this->accountid)) {  // For compatibility
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Code\Compta\Cla...mentVarious::$accountid has been deprecated: See fk_account ( Ignorable by Annotation )

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

470
        if (empty($this->fk_account) && isset(/** @scrutinizer ignore-deprecated */ $this->accountid)) {  // For compatibility

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
471
            $this->fk_account = $this->accountid;
472
        }
473
474
        // Check parameters
475
        if (!$this->label) {
476
            $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
477
            return -3;
478
        }
479
        if ($this->amount < 0 || $this->amount == '') {
480
            $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
481
            return -5;
482
        }
483
        if (isModEnabled("bank") && (empty($this->fk_account) || $this->fk_account <= 0)) {
484
            $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("BankAccount"));
485
            return -6;
486
        }
487
        if (isModEnabled("bank") && (empty($this->type_payment) || $this->type_payment <= 0)) {
488
            $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
489
            return -7;
490
        }
491
492
        $this->db->begin();
493
494
        // Insert into llx_payment_various
495
        $sql = "INSERT INTO " . MAIN_DB_PREFIX . "payment_various (";
496
        $sql .= " datep";
497
        $sql .= ", datev";
498
        $sql .= ", sens";
499
        $sql .= ", amount";
500
        $sql .= ", fk_typepayment";
501
        $sql .= ", num_payment";
502
        if ($this->note) {
503
            $sql .= ", note";
504
        }
505
        $sql .= ", label";
506
        $sql .= ", accountancy_code";
507
        $sql .= ", subledger_account";
508
        $sql .= ", fk_projet";
509
        $sql .= ", fk_user_author";
510
        $sql .= ", datec";
511
        $sql .= ", fk_bank";
512
        $sql .= ", entity";
513
        $sql .= ")";
514
        $sql .= " VALUES (";
515
        $sql .= "'" . $this->db->idate($this->datep) . "'";
516
        $sql .= ", '" . $this->db->idate($this->datev) . "'";
517
        $sql .= ", '" . $this->db->escape($this->sens) . "'";
518
        $sql .= ", " . price2num($this->amount);
519
        $sql .= ", '" . $this->db->escape($this->type_payment) . "'";
520
        $sql .= ", '" . $this->db->escape($this->num_payment) . "'";
521
        if ($this->note) {
522
            $sql .= ", '" . $this->db->escape($this->note) . "'";
523
        }
524
        $sql .= ", '" . $this->db->escape($this->label) . "'";
525
        $sql .= ", '" . $this->db->escape($this->accountancy_code) . "'";
526
        $sql .= ", '" . $this->db->escape($this->subledger_account) . "'";
527
        $sql .= ", " . ($this->fk_project > 0 ? ((int) $this->fk_project) : 0);
528
        $sql .= ", " . ((int) $user->id);
529
        $sql .= ", '" . $this->db->idate($now) . "'";
530
        $sql .= ", NULL";   // Filled later
531
        $sql .= ", " . ((int) $conf->entity);
532
        $sql .= ")";
533
534
        dol_syslog(get_class($this) . "::create", LOG_DEBUG);
535
        $result = $this->db->query($sql);
536
        if ($result) {
537
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "payment_various");
538
            $this->ref = (string) $this->id;
539
540
            if ($this->id > 0) {
541
                if (isModEnabled("bank") && !empty($this->amount)) {
542
                    // Insert into llx_bank
543
544
                    $acc = new Account($this->db);
545
                    $result = $acc->fetch($this->fk_account);
546
                    if ($result <= 0) {
547
                        dol_print_error($this->db);
548
                    }
549
550
                    // Insert payment into llx_bank
551
                    // Add link 'payment_various' in bank_url between payment and bank transaction
552
                    $sign = 1;
553
                    if ($this->sens == '0') {
554
                        $sign = -1;
555
                    }
556
557
                    $bank_line_id = $acc->addline(
558
                        $this->datep,
559
                        $this->type_payment,
560
                        $this->label,
561
                        $sign * abs($this->amount),
562
                        $this->num_payment,
563
                        ($this->category_transaction > 0 ? $this->category_transaction : 0),
564
                        $user,
565
                        $this->chqemetteur,
566
                        $this->chqbank,
567
                        '',
568
                        $this->datev
569
                    );
570
571
                    // Update fk_bank into llx_payment_various
572
                    // So we know the payment which has generate the banking ecriture
573
                    if ($bank_line_id > 0) {
574
                        $this->update_fk_bank($bank_line_id);
575
                    } else {
576
                        $this->error = $acc->error;
577
                        $error++;
578
                    }
579
580
                    if (!$error) {
581
                        // Add link 'payment_various' in bank_url between payment and bank transaction
582
                        $url = constant('BASE_URL') . '/compta/bank/various_payment/card.php?id=';
583
584
                        $result = $acc->add_url_line($bank_line_id, $this->id, $url, "(VariousPayment)", "payment_various");
585
                        if ($result <= 0) {
586
                            $this->error = $acc->error;
587
                            $error++;
588
                        }
589
                    }
590
591
                    if ($result <= 0) {
592
                        $this->error = $acc->error;
593
                        $error++;
594
                    }
595
                }
596
597
                // Call trigger
598
                $result = $this->call_trigger('PAYMENT_VARIOUS_CREATE', $user);
599
                if ($result < 0) {
600
                    $error++;
601
                }
602
                // End call triggers
603
            } else {
604
                $error++;
605
            }
606
607
            if (!$error) {
608
                $this->db->commit();
609
                return $this->id;
610
            } else {
611
                $this->db->rollback();
612
                return -2;
613
            }
614
        } else {
615
            $this->error = $this->db->error();
616
            $this->db->rollback();
617
            return -1;
618
        }
619
    }
620
621
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
622
    /**
623
     *  Update link between payment various and line generate into llx_bank
624
     *
625
     *  @param  int     $id_bank    Id bank account
626
     *  @return int                 Return integer <0 if KO, >0 if OK
627
     */
628
    public function update_fk_bank($id_bank)
629
    {
630
		// phpcs:enable
631
        $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'payment_various SET fk_bank = ' . ((int) $id_bank);
632
        $sql .= " WHERE rowid = " . ((int) $this->id);
633
        $result = $this->db->query($sql);
634
        if ($result) {
635
            return 1;
636
        } else {
637
            dol_print_error($this->db);
638
            return -1;
639
        }
640
    }
641
642
643
    /**
644
     *  Return the label of the status
645
     *
646
     *  @param  int     $mode          0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
647
     *  @return string                 Label of status
648
     */
649
    public function getLibStatut($mode = 0)
650
    {
651
        return $this->LibStatut($this->statut, $mode);
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$statut has been deprecated: Use $status instead. ( Ignorable by Annotation )

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

651
        return $this->LibStatut(/** @scrutinizer ignore-deprecated */ $this->statut, $mode);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
652
    }
653
654
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
655
    /**
656
     *  Return the label of a given status
657
     *
658
     *  @param  int     $status        Id status
659
     *  @param  int     $mode          0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
660
     *  @return string                 Label of status
661
     */
662
    public function LibStatut($status, $mode = 0)
663
    {
664
		// phpcs:enable
665
        global $langs;
666
667
        if (empty($status)) {
668
            $status = 0;
669
        }
670
671
        if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
672
            $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
673
            //$this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
674
            //$this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
675
            $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
676
            //$this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
677
            //$this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
678
        }
679
680
        $statusType = 'status' . $status;
681
682
        return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
683
    }
684
685
686
    /**
687
     *  Send name clicable (with possibly the picto)
688
     *
689
     *  @param  int     $withpicto                  0=No picto, 1=Include picto into link, 2=Only picto
690
     *  @param  string  $option                     link option
691
     *  @param  int     $save_lastsearch_value      -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
692
     *  @param  int     $notooltip                  1=Disable tooltip
693
     *  @param  string  $morecss                    morecss string
694
     *  @return string                              String with URL
695
     */
696
    public function getNomUrl($withpicto = 0, $option = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
697
    {
698
        global $db, $conf, $langs, $hookmanager;
699
        global $langs;
700
701
        if (!empty($conf->dol_no_mouse_hover)) {
702
            $notooltip = 1; // Force disable tooltips
703
        }
704
705
        $result = '';
706
707
        $label = '<u>' . $langs->trans("ShowVariousPayment") . '</u>';
708
        $label .= '<br>';
709
        $label .= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
710
711
        $url = constant('BASE_URL') . '/compta/bank/various_payment/card.php?id=' . $this->id;
712
713
        if ($option != 'nolink') {
714
            // Add param to save lastsearch_values or not
715
            $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
716
            if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
717
                $add_save_lastsearch_values = 1;
718
            }
719
            if ($add_save_lastsearch_values) {
720
                $url .= '&save_lastsearch_values=1';
721
            }
722
        }
723
724
        $linkclose = '';
725
        if (empty($notooltip)) {
726
            if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
727
                $label = $langs->trans("ShowMyObject");
728
                $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"';
729
            }
730
            $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"';
731
            $linkclose .= ' class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"';
732
        } else {
733
            $linkclose = ($morecss ? ' class="' . $morecss . '"' : '');
734
        }
735
736
        $linkstart = '<a href="' . $url . '"';
737
        $linkstart .= $linkclose . '>';
738
        $linkend = '</a>';
739
740
        $result .= $linkstart;
741
        if ($withpicto) {
742
            $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="' . (($withpicto != 2) ? 'paddingright ' : '') . 'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
743
        }
744
        if ($withpicto != 2) {
745
            $result .= $this->ref;
746
        }
747
        $result .= $linkend;
748
        //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
749
750
        global $action;
751
        $hookmanager->initHooks(array('variouspayment'));
752
        $parameters = array('id' => $this->id, 'getnomurl' => &$result);
753
        $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
754
        if ($reshook > 0) {
755
            $result = $hookmanager->resPrint;
756
        } else {
757
            $result .= $hookmanager->resPrint;
758
        }
759
760
        return $result;
761
    }
762
763
    /**
764
     * Information on record
765
     *
766
     * @param  int      $id      Id of record
767
     * @return void
768
     */
769
    public function info($id)
770
    {
771
        $sql = 'SELECT v.rowid, v.datec, v.fk_user_author';
772
        $sql .= ' FROM ' . MAIN_DB_PREFIX . 'payment_various as v';
773
        $sql .= ' WHERE v.rowid = ' . ((int) $id);
774
775
        dol_syslog(get_class($this) . '::info', LOG_DEBUG);
776
        $result = $this->db->query($sql);
777
778
        if ($result) {
779
            if ($this->db->num_rows($result)) {
780
                $obj = $this->db->fetch_object($result);
781
782
                $this->id = $obj->rowid;
783
                $this->user_creation = $obj->fk_user_author;
784
                $this->user_modification_id = $obj->fk_user_modif;
785
                $this->date_creation = $this->db->jdate($obj->datec);
786
                $this->date_modification = $this->db->jdate($obj->tms);
787
            }
788
            $this->db->free($result);
789
        } else {
790
            dol_print_error($this->db);
791
        }
792
    }
793
794
    /**
795
     *  Return if a various payment linked to a bank line id was dispatched into bookkeeping
796
     *
797
     *  @return     int         Return integer <0 if KO, 0=no, 1=yes
798
     */
799
    public function getVentilExportCompta()
800
    {
801
        $banklineid = $this->fk_bank;
802
803
        $alreadydispatched = 0;
804
805
        $type = 'bank';
806
807
        $sql = " SELECT COUNT(ab.rowid) as nb FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='" . $this->db->escape($type) . "' AND ab.fk_doc = " . ((int) $banklineid);
808
        $resql = $this->db->query($sql);
809
        if ($resql) {
810
            $obj = $this->db->fetch_object($resql);
811
            if ($obj) {
812
                $alreadydispatched = $obj->nb;
813
            }
814
        } else {
815
            $this->error = $this->db->lasterror();
816
            return -1;
817
        }
818
819
        if ($alreadydispatched) {
820
            return 1;
821
        }
822
        return 0;
823
    }
824
825
    /**
826
     *  Return clicable link of object (with eventually picto)
827
     *
828
     *  @param      string      $option                 Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link)
829
     *  @param      array       $arraydata              Array of data
830
     *  @return     string                              HTML Code for Kanban thumb.
831
     */
832
    public function getKanbanView($option = '', $arraydata = null)
833
    {
834
        global $langs;
835
836
        $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
837
838
        $return = '<div class="box-flex-item box-flex-grow-zero">';
839
        $return .= '<div class="info-box info-box-sm">';
840
        $return .= '<span class="info-box-icon bg-infobox-action">';
841
        $return .= img_picto('', $this->picto);
842
        $return .= '</span>';
843
        $return .= '<div class="info-box-content">';
844
        $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . (method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref) . '</span>';
845
        if ($selected >= 0) {
846
            $return .= '<input id="cb' . $this->id . '" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="' . $this->id . '"' . ($selected ? ' checked="checked"' : '') . '>';
847
        }
848
        if (property_exists($this, 'fk_bank')) {
849
            $return .= ' | <span class="info-box-status ">' . $this->fk_bank . '</span>';
850
        }
851
        if (property_exists($this, 'datep')) {
852
            $return .= '<br><span class="opacitymedium">' . $langs->trans("Date") . '</span> : <span class="info-box-label">' . dol_print_date($this->db->jdate($this->datep), 'day') . '</span>';
853
        }
854
        if (property_exists($this, 'type_payment') && !empty($this->type_payment)) {
855
            $return .= '<br><span class="opacitymedium">' . $langs->trans("Payment", $this->type_payment) . '</span> : <span class="info-box-label">' . $this->type_payment . '</span>';
856
        }
857
        if (property_exists($this, 'accountancy_code')) {
858
            $return .= '<br><span class="opacitymedium">' . $langs->trans("Account") . '</span> : <span class="info-box-label" title="' . $this->accountancy_code . '">' . $this->accountancy_code . '</span>';
859
        }
860
        if (property_exists($this, 'amount')) {
861
            $return .= '<br><span class="opacitymedium">' . $langs->trans("Debit") . '</span> : <span class="info-box-label amount">' . price($this->amount) . '</span>';
862
        }
863
        $return .= '</div>';
864
        $return .= '</div>';
865
        $return .= '</div>';
866
        return $return;
867
    }
868
869
    /**
870
     * Return General accounting account with defined length (used for product and miscellaneous)
871
     *
872
     * @param   string  $account        General accounting account
873
     * @return  string                  String with defined length
874
     */
875
    public function lengthAccountg($account)
876
    {
877
        include_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
878
879
        /*
880
        if (isModEnabled('accounting')) {
881
            $accountingaccount = new AccountingAccount($db);
882
            $accountingaccount->fetch('', $valuetoshow, 1);
883
        }*/
884
885
        return length_accountg($account);
886
    }
887
888
    /**
889
     * Return Auxiliary accounting account of thirdparties with defined length
890
     *
891
     * @param   string  $account        Auxiliary accounting account
892
     * @return  string                  String with defined length
893
     */
894
    public function lengthAccounta($account)
895
    {
896
        include_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
897
898
        return length_accounta($account);
899
    }
900
}
901