Test Failed
Push — main ( 1be143...29a1e7 )
by Rafael
51:40
created

CompanyBankAccount   F

Complexity

Total Complexity 65

Size/Duplication

Total Lines 671
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 321
dl 0
loc 671
rs 3.2
c 0
b 0
f 0
wmc 65

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B getRibLabel() 0 13 9
C create() 0 64 13
B fetch() 0 67 9
B delete() 0 33 7
B setAsDefault() 0 38 7
A initAsSpecimen() 0 29 1
F update() 0 79 18

How to fix   Complexity   

Complex Class

Complex classes like CompanyBankAccount often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CompanyBankAccount, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/* Copyright (C) 2004       Rodolphe Quiedeville    <[email protected]>
4
 * Copyright (C) 2010-2023	Laurent Destailleur		<[email protected]>
5
 * Copyright (C) 2012		Regis Houssin			<[email protected]>
6
 * Copyright (C) 2013   	Peter Fontaine          <[email protected]>
7
 * Copyright (C) 2016       Marcos García           <[email protected]>
8
 * Copyright (C) 2024       Frédéric France             <[email protected]>
9
 * Copyright (C) 2024		MDW						<[email protected]>
10
 * Copyright (C) 2024       Rafael San José         <[email protected]>
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 3 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24
 */
25
26
/**
27
 *  \file       htdocs/societe/class/companybankaccount.class.php
28
 *  \ingroup    societe
29
 *  \brief      File of class to manage bank accounts description of third parties
30
 */
31
32
33
namespace DoliModules\Company\Model;
34
35
use DoliModules\Bank\Model\Account;
36
use DoliModules\User\Model\User;
37
use DoliDB;
38
39
/**
40
 *  Class to manage bank accounts description of third parties
41
 */
42
class CompanyBankAccount extends Account
43
{
44
    /**
45
     * @var string TRIGGER_PREFIX  Dolibarr 16.0 and above use the prefix to prevent the creation of inconsistently
46
     *                             named triggers
47
     * @see CommonObject::call_trigger()
48
     */
49
    const TRIGGER_PREFIX = 'COMPANY_RIB';
50
    /**
51
     * @var string ID to identify managed object.
52
     */
53
    public $element = 'societe_rib';
54
55
    /**
56
     *  'type' field format:
57
     *      'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',
58
     *      'select' (list of values are in 'options'),
59
     *      'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter[:Sortfield]]]]',
60
     *      'chkbxlst:...',
61
     *      'varchar(x)',
62
     *      'text', 'text:none', 'html',
63
     *      'double(24,8)', 'real', 'price',
64
     *      'date', 'datetime', 'timestamp', 'duration',
65
     *      'boolean', 'checkbox', 'radio', 'array',
66
     *      'mail', 'phone', 'url', 'password', 'ip'
67
     *      Note: Filter must be a Dolibarr Universal Filter syntax string. Example: "(t.ref:like:'SO-%') or
68
     *      (t.date_creation:<:'20160101') or (t.status:!=:0) or (t.nature:is:NULL)"
69
     *  'label' the translation key.
70
     *  'picto' is code of a picto to show before value in forms
71
     *  'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM' or
72
     *  'isModEnabled("multicurrency")' ...)
73
     *  'position' is the sort order of field.
74
     *  'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
75
     *  'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view
76
     *  forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and
77
     *  update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative
78
     *  value means field is not shown by default on list but can be selected for viewing)
79
     *  'noteditable' says if field is not editable (1 or 0)
80
     *  'alwayseditable' says if field can be modified also when status is not draft ('1' or '0')
81
     *  'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is
82
     *  editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be
83
     *  set to '(PROVid)' where id is rowid when a new record is created.
84
     *  'index' if we want an index in database.
85
     *  'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommended to name the field fk_...).
86
     *  'searchall' is 1 if we want to search in this field when making a search from the quick search button.
87
     *  'isameasure' must be set to 1 or 2 if field can be used for measure. Field type must be summable like integer
88
     *  or double(24,8). Use 1 in most cases, or 2 if you don't want to see the column total into list (for example for
89
     *  percentage)
90
     *  'css' and 'cssview' and 'csslist' is the CSS style to use on field. 'css' is used in creation and update.
91
     *  'cssview' is used in view mode. 'csslist' is used for columns in lists. For example: 'css'=>'minwidth300
92
     *  maxwidth500 widthcentpercentminusx', 'cssview'=>'wordbreak', 'csslist'=>'tdoverflowmax200'
93
     *  'help' and 'helplist' is a 'TranslationString' to use to show a tooltip on field. You can also use
94
     *  'TranslationString:keyfortooltiponlick' for a tooltip on click.
95
     *  'showoncombobox' if value of the field must be visible into the label of the combobox that list record
96
     *  'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set
97
     *  into the definition of $fields into class, but is set dynamically by some part of code.
98
     *  'arrayofkeyval' to set a list of values if type is a list of predefined values. For example:
99
     *  array("0"=>"Draft","1"=>"Active","-1"=>"Cancel"). Note that type can be 'integer' or 'varchar'
100
     *  'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set
101
     *  to 1.
102
     *  'comment' is not used. You can store here any text of your choice. It is not used by application.
103
     *  'validate' is 1 if need to validate with $this->validateField()
104
     *  'copytoclipboard' is 1 or 2 to allow to add a picto to copy value into clipboard (1=picto after label, 2=picto
105
     *  after value)
106
     *
107
     *  Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the
108
     *  constructor.
109
     */
110
111
    // BEGIN MODULEBUILDER PROPERTIES
112
    /**
113
     * @var string Name of table without prefix where object is stored. This is also the key used for extrafields
114
     *      management.
115
     */
116
    public $table_element = 'societe_rib';
117
    /**
118
     * @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}>
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...
119
     *      Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
120
     */
121
    public $fields = [
122
        'rowid' => ['type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'position' => 10, 'notnull' => 1, 'visible' => -1,],
123
        'type' => ['type' => 'varchar(32)', 'label' => 'Type', 'enabled' => 1, 'position' => 15, 'notnull' => 1, 'visible' => -1,],
124
        'fk_soc' => ['type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'picto' => 'company', 'enabled' => 1, 'position' => 20, 'notnull' => 1, 'visible' => -1, 'css' => 'maxwidth500 widthcentpercentminusxx', 'csslist' => 'tdoverflowmax150',],
125
        'datec' => ['type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'position' => 25, 'notnull' => 0, 'visible' => -1,],
126
        'tms' => ['type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'position' => 30, 'notnull' => 1, 'visible' => -1,],
127
        'label' => ['type' => 'varchar(200)', 'label' => 'Label', 'enabled' => 1, 'position' => 35, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1, 'css' => 'minwidth300', 'cssview' => 'wordbreak', 'csslist' => 'tdoverflowmax150',],
128
        'bank' => ['type' => 'varchar(255)', 'label' => 'Bank', 'enabled' => 1, 'position' => 40, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
129
        'code_banque' => ['type' => 'varchar(128)', 'label' => 'Codebanque', 'enabled' => 1, 'position' => 45, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
130
        'code_guichet' => ['type' => 'varchar(6)', 'label' => 'Codeguichet', 'enabled' => 1, 'position' => 50, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
131
        'number' => ['type' => 'varchar(255)', 'label' => 'Number', 'enabled' => 1, 'position' => 55, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
132
        'cle_rib' => ['type' => 'varchar(5)', 'label' => 'Clerib', 'enabled' => 1, 'position' => 60, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
133
        'bic' => ['type' => 'varchar(20)', 'label' => 'Bic', 'enabled' => 1, 'position' => 65, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
134
        'iban_prefix' => ['type' => 'varchar(34)', 'label' => 'Ibanprefix', 'enabled' => 1, 'position' => 70, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
135
        'domiciliation' => ['type' => 'varchar(255)', 'label' => 'Domiciliation', 'enabled' => 1, 'position' => 75, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
136
        'proprio' => ['type' => 'varchar(60)', 'label' => 'Proprio', 'enabled' => 1, 'position' => 80, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
137
        'owner_address' => ['type' => 'varchar(255)', 'label' => 'Owneraddress', 'enabled' => 1, 'position' => 85, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
138
        'default_rib' => ['type' => 'smallint(6)', 'label' => 'Defaultrib', 'enabled' => 1, 'position' => 90, 'notnull' => 1, 'visible' => -1, 'alwayseditable' => 1,],
139
        'state_id' => ['type' => 'integer', 'label' => 'Stateid', 'enabled' => 1, 'position' => 95, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
140
        'fk_country' => ['type' => 'integer', 'label' => 'Fkcountry', 'enabled' => 1, 'position' => 100, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1, 'css' => 'maxwidth500 widthcentpercentminusxx',],
141
        'currency_code' => ['type' => 'varchar(3)', 'label' => 'Currencycode', 'enabled' => 1, 'position' => 105, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
142
        'rum' => ['type' => 'varchar(32)', 'label' => 'Rum', 'enabled' => 1, 'position' => 110, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
143
        'date_rum' => ['type' => 'date', 'label' => 'Daterum', 'enabled' => 1, 'position' => 115, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
144
        'frstrecur' => ['type' => 'varchar(16)', 'label' => 'Frstrecur', 'enabled' => 1, 'position' => 120, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
145
        'import_key' => ['type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'position' => 900, 'notnull' => 0, 'visible' => -2, 'alwayseditable' => 1,],
146
        'last_four' => ['type' => 'varchar(4)', 'label' => 'Lastfour', 'enabled' => 1, 'position' => 130, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
147
        'card_type' => ['type' => 'varchar(255)', 'label' => 'Cardtype', 'enabled' => 1, 'position' => 135, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
148
        'cvn' => ['type' => 'varchar(255)', 'label' => 'Cvn', 'enabled' => 1, 'position' => 140, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
149
        'exp_date_month' => ['type' => 'integer', 'label' => 'Expdatemonth', 'enabled' => 1, 'position' => 145, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
150
        'exp_date_year' => ['type' => 'integer', 'label' => 'Expdateyear', 'enabled' => 1, 'position' => 150, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
151
        'country_code' => ['type' => 'varchar(10)', 'label' => 'Countrycode', 'enabled' => 1, 'position' => 155, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
152
        'approved' => ['type' => 'integer', 'label' => 'Approved', 'enabled' => 1, 'position' => 160, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
153
        'email' => ['type' => 'varchar(255)', 'label' => 'Email', 'enabled' => 1, 'position' => 165, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
154
        'ending_date' => ['type' => 'date', 'label' => 'Endingdate', 'enabled' => 1, 'position' => 170, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
155
        'max_total_amount_of_all_payments' => ['type' => 'double(24,8)', 'label' => 'Maxtotalamountofallpayments', 'enabled' => 1, 'position' => 175, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
156
        'preapproval_key' => ['type' => 'varchar(255)', 'label' => 'Preapprovalkey', 'enabled' => 1, 'position' => 180, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
157
        'starting_date' => ['type' => 'date', 'label' => 'Startingdate', 'enabled' => 1, 'position' => 185, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
158
        'total_amount_of_all_payments' => ['type' => 'double(24,8)', 'label' => 'Totalamountofallpayments', 'enabled' => 1, 'position' => 190, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
159
        'stripe_card_ref' => ['type' => 'varchar(128)', 'label' => 'Stripecardref', 'enabled' => 1, 'position' => 195, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
160
        'status' => ['type' => 'integer', 'label' => 'Status', 'enabled' => 1, 'position' => 500, 'notnull' => 1, 'visible' => -1, 'alwayseditable' => 1,],
161
        'comment' => ['type' => 'varchar(255)', 'label' => 'Comment', 'enabled' => 1, 'position' => 205, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
162
        'ipaddress' => ['type' => 'varchar(68)', 'label' => 'Ipaddress', 'enabled' => 1, 'position' => 210, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
163
        'stripe_account' => ['type' => 'varchar(128)', 'label' => 'Stripeaccount', 'enabled' => 1, 'position' => 215, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,],
164
        'last_main_doc' => ['type' => 'varchar(255)', 'label' => 'LastMainDoc', 'enabled' => 1, 'visible' => 0, 'position' => 230],
165
    ];
166
    /**
167
     * @var int ID
168
     */
169
    public $id;
170
    /**
171
     * @var string type
172
     */
173
    public $type;
174
    /**
175
     * @var int     Thirdparty ID
176
     * @deprecated  Use socid
177
     */
178
    public $fk_soc;
179
    /**
180
     * @var int     Thirdparty ID
181
     */
182
    public $socid;
183
    /**
184
     * Date creation record (datec)
185
     *
186
     * @var integer
187
     */
188
    public $datec;
189
    /**
190
     * @var string label
191
     */
192
    public $label;
193
    public $code_banque;
194
    public $code_guichet;
195
    public $number;
196
    public $cle_rib;
197
    public $bic;
198
    public $iban_prefix;
199
    public $bank;
200
    /**
201
     * @var string  Bank address
202
     * @deprecated Replaced with address
203
     */
204
    public $domiciliation;
205
    public $address;
206
    /**
207
     * @var int state id
208
     */
209
    public $state_id;
210
    /**
211
     * @var int country id
212
     */
213
    public $fk_country;
214
    public $country_code;
215
    /**
216
     * @var string owner
217
     */
218
    public $proprio;
219
    /**
220
     * @var string owner address
221
     */
222
    public $owner_address;
223
    /**
224
     * @var int $default_rib 1 = this object is the third party's default bank information, 0 if not
225
     */
226
    public $default_rib;
227
    /**
228
     * @var string currency code
229
     */
230
    public $currency_code;
231
    /**
232
     * @var string rum
233
     */
234
    public $rum;
235
    /**
236
     * @var int date rum
237
     */
238
    public $date_rum;
239
    /**
240
     * Value 'FRST' or 'RCUR' (For SEPA mandate)
241
     *
242
     * @var string
243
     */
244
    public $frstrecur;
245
    public $import_key;
246
    public $last_four;
247
    public $card_type;
248
    public $cvn;
249
    public $exp_date_month;
250
    public $exp_date_year;
251
    public $approved;
252
    /**
253
     * @var string email
254
     */
255
    public $email;
256
    public $ending_date;
257
    public $max_total_amount_of_all_payments;
258
    public $preapproval_key;
259
    public $starting_date;
260
    public $total_amount_of_all_payments;   // Name of the external payment system ('StripeLive', 'StripeTest', 'StancerLive', 'StancerTest', ...)
261
    /**
262
     * @var string external payment site
263
     */
264
    public $ext_payment_site;
265
    /**
266
     * @var string comment
267
     */
268
    public $comment;
269
    /**
270
     * @var string ip address
271
     */
272
    public $ipaddress;
273
    /**
274
     * Account of the external payment system
275
     *
276
     * @var string
277
     */
278
    public $stripe_account;
279
    /**
280
     * ID of BAN into an external payment system
281
     *
282
     * @var string
283
     */
284
    public $stripe_card_ref;
285
    /**
286
     * Date modification record (tms)
287
     *
288
     * @var integer
289
     */
290
    public $datem;
291
    /**
292
     * @var string
293
     * @see SetDocModel()
294
     */
295
    public $model_pdf;
296
297
    /**
298
     *  Constructor
299
     *
300
     * @param DoliDB $db Database handler
301
     */
302
    public function __construct(DoliDB $db)
303
    {
304
        $this->db = $db;
305
306
        $this->socid = 0;
307
        $this->solde = 0;
308
        $this->balance = 0;
309
        $this->default_rib = 0;
310
        $this->type = "ban";
311
    }
312
313
314
    /**
315
     * Create bank information record.
316
     *
317
     * @param User|null $user      User
318
     * @param int       $notrigger 1=Disable triggers
319
     *
320
     * @return  int                     Return integer <0 if KO, > 0 if OK (ID of newly created company bank account
321
     *                                  information)
322
     */
323
    public function create(User $user = null, $notrigger = 0)
324
    {
325
        $now = dol_now();
326
327
        $error = 0;
328
329
        // Check parameters
330
        if (empty($this->socid)) {
331
            $this->error = 'BadValueForParameter';
332
            $this->errors[] = $this->error;
333
            return -1;
334
        }
335
336
        if (empty($this->datec)) {
337
            $this->datec = $now;
338
        }
339
340
        // Correct ->default_rib to not set the new account as default, if there is already 1. We want to be sure to have always 1 default for type = 'ban'.
341
        // If we really want the new bank account to be the default, we must set it by calling setDefault() after creation.
342
        $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "societe_rib where fk_soc = " . ((int) $this->socid) . " AND default_rib = 1 AND type = 'ban'";
343
        $result = $this->db->query($sql);
344
        if ($result) {
345
            $numrows = $this->db->num_rows($result);
346
            if ($this->default_rib && $numrows > 0) {
347
                $this->default_rib = 0;
348
            }
349
            if (empty($this->default_rib) && $numrows == 0) {
350
                $this->default_rib = 1;
351
            }
352
        }
353
354
355
        $this->db->begin();
356
357
        $sql = "INSERT INTO " . MAIN_DB_PREFIX . "societe_rib (fk_soc, type, datec, model_pdf)";
358
        $sql .= " VALUES (" . ((int) $this->socid) . ", '" . $this->type . "', '" . $this->db->idate($this->datec) . "',";
359
        $sql .= " '" . $this->db->escape(getDolGlobalString("BANKADDON_PDF")) . "'";
360
        $sql .= ")";
361
        $resql = $this->db->query($sql);
362
        if ($resql) {
363
            if ($this->db->affected_rows($resql)) {
364
                $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "societe_rib");
365
366
                if (!$notrigger) {
367
                    // Call trigger
368
                    $result = $this->call_trigger('COMPANY_RIB_CREATE', $user);
369
                    if ($result < 0) {
370
                        $error++;
371
                    }
372
                    // End call triggers
373
                }
374
            }
375
        } else {
376
            $error++;
377
            $this->error = $this->db->lasterror();
378
            $this->errors[] = $this->error;
379
        }
380
381
        if (!$error) {
382
            $this->db->commit();
383
            return $this->id;
384
        } else {
385
            $this->db->rollback();
386
            return -1;
387
        }
388
    }
389
390
    /**
391
     *  Update bank account
392
     *
393
     * @param User|null $user      Object user
394
     * @param int       $notrigger 1=Disable triggers
395
     *
396
     * @return int                      Return integer <=0 if KO, >0 if OK
397
     */
398
    public function update(User $user = null, $notrigger = 0)
399
    {
400
        global $langs;
401
402
        $error = 0;
403
404
        if (!$this->id) {
405
            return -1;
406
        }
407
408
        if (!empty($this->domiciliation) && dol_strlen($this->domiciliation) > 255) {
0 ignored issues
show
Deprecated Code introduced by
The property DoliModules\Company\Mode...Account::$domiciliation has been deprecated: Replaced with address ( Ignorable by Annotation )

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

408
        if (!empty($this->domiciliation) && dol_strlen(/** @scrutinizer ignore-deprecated */ $this->domiciliation) > 255) {

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...
409
            $this->domiciliation = dol_trunc($this->domiciliation, 254, 'right', 'UTF-8', 1);
0 ignored issues
show
Deprecated Code introduced by
The property DoliModules\Company\Mode...Account::$domiciliation has been deprecated: Replaced with address ( Ignorable by Annotation )

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

409
            /** @scrutinizer ignore-deprecated */ $this->domiciliation = dol_trunc($this->domiciliation, 254, 'right', 'UTF-8', 1);

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...
410
        }
411
        if (!empty($this->address) && dol_strlen($this->address) > 255) {
412
            $this->address = dol_trunc($this->address, 254, 'right', 'UTF-8', 1);
413
        }
414
        if (dol_strlen($this->owner_address) > 255) {
415
            $this->owner_address = dol_trunc($this->owner_address, 254, 'right', 'UTF-8', 1);
416
        }
417
418
        if (isset($this->model_pdf)) {
419
            $this->model_pdf = trim($this->model_pdf);
420
        }
421
422
        $this->db->begin();
423
424
        $sql = "UPDATE " . MAIN_DB_PREFIX . "societe_rib SET";
425
        $sql .= " bank = '" . $this->db->escape($this->bank) . "'";
426
        $sql .= ",code_banque='" . $this->db->escape($this->code_banque) . "'";
427
        $sql .= ",code_guichet='" . $this->db->escape($this->code_guichet) . "'";
428
        $sql .= ",number='" . $this->db->escape($this->number) . "'";
429
        $sql .= ",cle_rib='" . $this->db->escape($this->cle_rib) . "'";
430
        $sql .= ",bic='" . $this->db->escape($this->bic) . "'";
431
        $sql .= ",iban_prefix = '" . $this->db->escape($this->iban) . "'";
432
        $sql .= ",domiciliation = '" . $this->db->escape($this->address ? $this->address : $this->domiciliation) . "'";
433
        $sql .= ",proprio = '" . $this->db->escape($this->proprio) . "'";
434
        $sql .= ",owner_address = '" . $this->db->escape($this->owner_address) . "'";
435
        $sql .= ",default_rib = " . ((int) $this->default_rib);
436
        if (isModEnabled('prelevement')) {
437
            $sql .= ",frstrecur = '" . $this->db->escape($this->frstrecur) . "'";
438
            $sql .= ",rum = '" . $this->db->escape($this->rum) . "'";
439
            $sql .= ",date_rum = " . ($this->date_rum ? "'" . $this->db->idate($this->date_rum) . "'" : "null");
440
        }
441
        if (trim($this->label) != '') {
442
            $sql .= ",label = '" . $this->db->escape($this->label) . "'";
443
        } else {
444
            $sql .= ",label = NULL";
445
        }
446
        $sql .= ",stripe_card_ref = '" . $this->db->escape($this->stripe_card_ref) . "'";
447
        $sql .= ",stripe_account = '" . $this->db->escape($this->stripe_account) . "'";
448
        $sql .= ",model_pdf=" . (isset($this->model_pdf) ? "'" . $this->db->escape($this->model_pdf) . "'" : "null");
449
        $sql .= " WHERE rowid = " . ((int) $this->id);
450
451
        $result = $this->db->query($sql);
452
        if ($result) {
453
            if (!$notrigger) {
454
                // Call trigger
455
                $result = $this->call_trigger('COMPANY_RIB_MODIFY', $user);
456
                if ($result < 0) {
457
                    $error++;
458
                }
459
                // End call triggers
460
            }
461
        } else {
462
            $error++;
463
            if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
464
                $this->error = $langs->trans('ErrorDuplicateField');
465
            } else {
466
                $this->error = $this->db->lasterror();
467
            }
468
            $this->errors[] = $this->error;
469
        }
470
471
        if (!$error) {
472
            $this->db->commit();
473
            return 1;
474
        } else {
475
            $this->db->rollback();
476
            return -1;
477
        }
478
    }
479
480
    /**
481
     *  Load record from database
482
     *
483
     * @param int    $id      Id of record
484
     * @param int    $socid   Id of company. If this is filled, function will return the first entry found (matching
485
     *                        $default and $type)
486
     * @param int    $default If id of company filled, we say if we want first record among all (-1), default record
487
     *                        (1) or non default record (0)
488
     * @param string $type    If id of company filled, we say if we want record of this type only
489
     *
490
     * @return int                 Return integer <0 if KO, >0 if OK
491
     */
492
    public function fetch($id, $socid = 0, $default = 1, $type = 'ban')
493
    {
494
        if (empty($id) && empty($socid)) {
495
            return -1;
496
        }
497
498
        $sql = "SELECT rowid, label, type, fk_soc as socid, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban,";
499
        $sql .= " domiciliation as address,";
500
        $sql .= " proprio, owner_address, default_rib, datec, tms as datem, rum, frstrecur, date_rum,";
501
        $sql .= " stripe_card_ref, stripe_account, ext_payment_site,";
502
        $sql .= " last_main_doc, model_pdf";
503
        $sql .= " FROM " . MAIN_DB_PREFIX . "societe_rib";
504
505
        if ($id) {
506
            $sql .= " WHERE rowid = " . ((int) $id);
507
        } elseif ($socid > 0) {
508
            $sql .= " WHERE fk_soc  = " . ((int) $socid);
509
            if ($default > -1) {
510
                $sql .= " AND default_rib = " . ((int) $default);
511
            }
512
            if ($type) {
513
                $sql .= " AND type = '" . $this->db->escape($type) . "'";
514
            }
515
        }
516
517
        $resql = $this->db->query($sql);
518
        if ($resql) {
519
            if ($this->db->num_rows($resql)) {
520
                $obj = $this->db->fetch_object($resql);
521
522
                $this->ref = $obj->socid . '-' . $obj->label; // Generate an artificial ref
523
524
                $this->id = $obj->rowid;
525
                $this->type = $obj->type;
526
                $this->socid = $obj->socid;
527
                $this->bank = $obj->bank;
528
                $this->code_banque = $obj->code_banque;
529
                $this->code_guichet = $obj->code_guichet;
530
                $this->number = $obj->number;
531
                $this->cle_rib = $obj->cle_rib;
532
                $this->bic = $obj->bic;
533
                $this->iban = $obj->iban;
534
535
                $this->domiciliation = $obj->address;
0 ignored issues
show
Deprecated Code introduced by
The property DoliModules\Company\Mode...Account::$domiciliation has been deprecated: Replaced with address ( Ignorable by Annotation )

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

535
                /** @scrutinizer ignore-deprecated */ $this->domiciliation = $obj->address;

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...
536
                $this->address = $obj->address;
537
538
                $this->proprio = $obj->proprio;
539
                $this->owner_address = $obj->owner_address;
540
                $this->label = $obj->label;
541
                $this->default_rib = $obj->default_rib;
542
                $this->datec = $this->db->jdate($obj->datec);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->db->jdate($obj->datec) can also be of type string. However, the property $datec 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...
543
                $this->datem = $this->db->jdate($obj->datem);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->db->jdate($obj->datem) can also be of type string. However, the property $datem 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...
544
                $this->rum = $obj->rum;
545
                $this->frstrecur = $obj->frstrecur;
546
                $this->date_rum = $this->db->jdate($obj->date_rum);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->db->jdate($obj->date_rum) can also be of type string. However, the property $date_rum 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...
547
                $this->stripe_card_ref = $obj->stripe_card_ref;     // External system payment mode ID
548
                $this->stripe_account = $obj->stripe_account;      // External system customer ID
549
                $this->ext_payment_site = $obj->ext_payment_site;   // External system name ('StripeLive', 'StripeTest', 'StancerLive', 'StancerTest', ...)
550
                $this->last_main_doc = $obj->last_main_doc;
551
                $this->model_pdf = $obj->model_pdf;
552
            }
553
            $this->db->free($resql);
554
555
            return 1;
556
        } else {
557
            dol_print_error($this->db);
558
            return -1;
559
        }
560
    }
561
562
    /**
563
     *  Delete a rib from database
564
     *
565
     * @param User|null $user      User deleting
566
     * @param int       $notrigger 1=Disable triggers
567
     *
568
     * @return     int                     Return integer <0 if KO, >0 if OK
569
     */
570
    public function delete(User $user = null, $notrigger = 0)
571
    {
572
        $error = 0;
573
574
        dol_syslog(get_class($this) . "::delete " . $this->id, LOG_DEBUG);
575
576
        $this->db->begin();
577
578
        if (!$error && !$notrigger) {
579
            // Call trigger
580
            $result = $this->call_trigger('COMPANY_RIB_DELETE', $user);
581
            if ($result < 0) {
582
                $error++;
583
            }
584
            // End call triggers
585
        }
586
587
        if (!$error) {
588
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "societe_rib";
589
            $sql .= " WHERE rowid = " . ((int) $this->id);
590
591
            if (!$this->db->query($sql)) {
592
                $error++;
593
                $this->errors[] = $this->db->lasterror();
594
            }
595
        }
596
597
        if (!$error) {
598
            $this->db->commit();
599
            return 1;
600
        } else {
601
            $this->db->rollback();
602
            return -1 * $error;
603
        }
604
    }
605
606
    /**
607
     * Return RIB
608
     *
609
     * @param boolean $displayriblabel Prepend or Hide Label
610
     *
611
     * @return  string      RIB
612
     */
613
    public function getRibLabel($displayriblabel = true)
614
    {
615
        $rib = '';
616
617
        if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib || $this->iban || $this->bic) {
618
            if ($this->label && $displayriblabel) {
619
                $rib = $this->label . " : ";
620
            }
621
622
            $rib .= $this->iban;
623
        }
624
625
        return $rib;
626
    }
627
628
    /**
629
     * Set a BAN as Default
630
     *
631
     * @param int    $rib                RIB id
632
     * @param string $resetolddefaultfor Reset if we have already a default value for type = 'ban'
633
     *
634
     * @return  int                         0 if KO, 1 if OK
635
     */
636
    public function setAsDefault($rib = 0, $resetolddefaultfor = 'ban')
637
    {
638
        $sql1 = "SELECT rowid as id, fk_soc as socid FROM " . MAIN_DB_PREFIX . "societe_rib";
639
        $sql1 .= " WHERE rowid = " . ($rib ? $rib : $this->id);
640
641
        dol_syslog(get_class($this) . '::setAsDefault', LOG_DEBUG);
642
        $result1 = $this->db->query($sql1);
643
        if ($result1) {
644
            if ($this->db->num_rows($result1) == 0) {
645
                return 0;
646
            } else {
647
                $obj = $this->db->fetch_object($result1);
648
649
                $this->db->begin();
650
651
                $sql2 = "UPDATE " . MAIN_DB_PREFIX . "societe_rib SET default_rib = 0";
652
                $sql2 .= " WHERE fk_soc = " . ((int) $obj->socid);
653
                if ($resetolddefaultfor) {
654
                    $sql2 .= " AND type = '" . $this->db->escape($resetolddefaultfor) . "'";
655
                }
656
                $result2 = $this->db->query($sql2);
657
658
                $sql3 = "UPDATE " . MAIN_DB_PREFIX . "societe_rib SET default_rib = 1";
659
                $sql3 .= " WHERE rowid = " . ((int) $obj->id);
660
                $result3 = $this->db->query($sql3);
661
662
                if (!$result2 || !$result3) {
663
                    dol_print_error($this->db);
664
                    $this->db->rollback();
665
                    return -1;
666
                } else {
667
                    $this->db->commit();
668
                    return 1;
669
                }
670
            }
671
        } else {
672
            dol_print_error($this->db);
673
            return -1;
674
        }
675
    }
676
677
    /**
678
     *  Initialise an instance with random values.
679
     *  Used to build previews or test instances.
680
     *  id must be 0 if object instance is a specimen.
681
     *
682
     * @return int
683
     */
684
    public function initAsSpecimen()
685
    {
686
        $this->specimen = 1;
687
        $this->ref = 'CBA';
688
        $this->label = 'CustomerCorp Bank account';
689
        $this->bank = 'CustomerCorp Bank';
690
        $this->courant = Account::TYPE_CURRENT;
691
        $this->clos = Account::STATUS_OPEN;
0 ignored issues
show
Deprecated Code introduced by
The property DoliModules\Bank\Model\Account::$clos has been deprecated: Duplicate field. We already have the field $this->status ( Ignorable by Annotation )

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

691
        /** @scrutinizer ignore-deprecated */ $this->clos = Account::STATUS_OPEN;

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...
692
        $this->code_banque = '123';
693
        $this->code_guichet = '456';
694
        $this->number = 'CUST12345';
695
        $this->cle_rib = '50';
696
        $this->bic = 'CC12';
697
        $this->iban = 'FR999999999';
698
699
        $this->address = 'Rue de Paris';
700
        $this->country_id = 1;
701
702
        $this->proprio = 'Owner';
703
        $this->owner_address = 'Owner address';
704
        $this->owner_country_id = 1;
705
706
        $this->rum = 'UMR-CU1212-0007-5-1475405262';
707
        $this->date_rum = dol_now() - 10000;
708
        $this->frstrecur = 'FRST';
709
710
        $this->socid = 1;
711
712
        return 1;
713
    }
714
}
715