| Total Complexity | 187 |
| Total Lines | 1817 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Account 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 Account, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class Account extends CommonObject |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * @var string ID to identify managed object |
||
| 43 | */ |
||
| 44 | public $element = 'bank_account'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string Name of table without prefix where object is stored |
||
| 48 | */ |
||
| 49 | public $table_element = 'bank_account'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
| 53 | */ |
||
| 54 | public $picto = 'account'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var int Use id instead of rowid |
||
| 58 | * @deprecated |
||
| 59 | * @see $id |
||
| 60 | */ |
||
| 61 | public $rowid; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Account Label |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | public $label; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Bank account type. Check TYPE_ constants |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | public $courant; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Bank account type. Check TYPE_ constants |
||
| 77 | * @var int |
||
| 78 | */ |
||
| 79 | public $type; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Bank name |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | public $bank; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Status |
||
| 89 | * @var int |
||
| 90 | */ |
||
| 91 | public $clos = self::STATUS_OPEN; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Does it need to be conciliated? |
||
| 95 | * @var int |
||
| 96 | */ |
||
| 97 | public $rappro = 1; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Webpage |
||
| 101 | * @var string |
||
| 102 | */ |
||
| 103 | public $url; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Bank number. If in SEPA area, you should move to IBAN field |
||
| 107 | * @var string |
||
| 108 | */ |
||
| 109 | public $code_banque; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Branch number. If in SEPA area, you should move to IBAN field |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | public $code_guichet; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Account number. If in SEPA area, you should move to IBAN field |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | public $number; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Bank account number control digit. If in SEPA area, you should move to IBAN field |
||
| 125 | * @var string |
||
| 126 | */ |
||
| 127 | public $cle_rib; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * BIC/Swift code |
||
| 131 | * @var string |
||
| 132 | */ |
||
| 133 | public $bic; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * IBAN number (International Bank Account Number). Stored into iban_prefix field into database (TODO Rename field in database) |
||
| 137 | * @var string |
||
| 138 | */ |
||
| 139 | public $iban; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * IBAN number |
||
| 143 | * |
||
| 144 | * @var string |
||
| 145 | * @deprecated see $iban |
||
| 146 | */ |
||
| 147 | public $iban_prefix; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * XML SEPA format: place Payment Type Information (PmtTpInf) in Credit Transfer Transaction Information (CdtTrfTxInf) |
||
| 151 | * @var int |
||
| 152 | */ |
||
| 153 | public $pti_in_ctti = 0; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Name of account holder |
||
| 157 | * @var string |
||
| 158 | */ |
||
| 159 | public $proprio; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Address of account holder |
||
| 163 | * @var string |
||
| 164 | */ |
||
| 165 | public $owner_address; |
||
| 166 | public $owner_zip; |
||
| 167 | public $owner_town; |
||
| 168 | public $owner_country_id; |
||
| 169 | public $owner_country_code; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Address of the bank account |
||
| 173 | * @var string |
||
| 174 | */ |
||
| 175 | public $domiciliation; // deprecated, use now address |
||
| 176 | public $address; |
||
| 177 | public $state_id; |
||
| 178 | public $state_code; |
||
| 179 | public $state; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Variable containing all account types with their respective translated label. |
||
| 183 | * Defined in __construct |
||
| 184 | * @var array |
||
| 185 | */ |
||
| 186 | public $type_lib = array(); |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Variable containing all account statuses with their respective translated label. |
||
| 190 | * Defined in __construct |
||
| 191 | * @var array |
||
| 192 | */ |
||
| 193 | public $status = array(); |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Accountancy code |
||
| 197 | * @var string |
||
| 198 | */ |
||
| 199 | public $account_number; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @var int ID |
||
| 203 | */ |
||
| 204 | public $fk_accountancy_journal; |
||
| 205 | /** |
||
| 206 | * @var string Label of journal |
||
| 207 | */ |
||
| 208 | public $accountancy_journal; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Currency code |
||
| 212 | * @var string |
||
| 213 | */ |
||
| 214 | public $currency_code; |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Currency code |
||
| 218 | * @var string |
||
| 219 | * @deprecated Use currency_code instead |
||
| 220 | */ |
||
| 221 | public $account_currency_code; |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Authorized minimum balance |
||
| 225 | * @var float |
||
| 226 | */ |
||
| 227 | public $min_allowed; |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Desired minimum balance |
||
| 231 | * @var float |
||
| 232 | */ |
||
| 233 | public $min_desired; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Notes |
||
| 237 | * @var string |
||
| 238 | */ |
||
| 239 | public $comment; |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Date of the initial balance. Used in Account::create |
||
| 243 | * @var int |
||
| 244 | */ |
||
| 245 | public $date_solde; |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Balance. Used in Account::create |
||
| 249 | * @var float |
||
| 250 | * @deprecated |
||
| 251 | * @see $balance |
||
| 252 | */ |
||
| 253 | public $solde; |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Balance. Used in Account::create |
||
| 257 | * @var float |
||
| 258 | */ |
||
| 259 | public $balance; |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Creditor Identifier CI. Some banks use different ICS for direct debit and bank tranfer |
||
| 263 | * @var string |
||
| 264 | */ |
||
| 265 | public $ics; |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Creditor Identifier for Bank Transfer. |
||
| 269 | * @var string |
||
| 270 | */ |
||
| 271 | public $ics_transfer; |
||
| 272 | |||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * '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') |
||
| 277 | * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)" |
||
| 278 | * 'label' the translation key. |
||
| 279 | * 'enabled' is a condition when the field must be managed. |
||
| 280 | * 'position' is the sort order of field. |
||
| 281 | * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0). |
||
| 282 | * '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) |
||
| 283 | * 'noteditable' says if field is not editable (1 or 0) |
||
| 284 | * '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. |
||
| 285 | * 'index' if we want an index in database. |
||
| 286 | * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...). |
||
| 287 | * 'searchall' is 1 if we want to search in this field when making a search from the quick search button. |
||
| 288 | * '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). |
||
| 289 | * 'css' is the CSS style to use on field. For example: 'maxwidth200' |
||
| 290 | * 'help' is a string visible as a tooltip on field |
||
| 291 | * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record |
||
| 292 | * '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. |
||
| 293 | * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel") |
||
| 294 | * 'comment' is not used. You can store here any text of your choice. It is not used by application. |
||
| 295 | * |
||
| 296 | * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor. |
||
| 297 | */ |
||
| 298 | |||
| 299 | // BEGIN MODULEBUILDER PROPERTIES |
||
| 300 | /** |
||
| 301 | * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. |
||
| 302 | */ |
||
| 303 | public $fields = array( |
||
| 304 | 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10), |
||
| 305 | 'ref' =>array('type'=>'varchar(12)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>25), |
||
| 306 | 'label' =>array('type'=>'varchar(30)', 'label'=>'Label', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>30), |
||
| 307 | 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>35, 'index'=>1), |
||
| 308 | 'bank' =>array('type'=>'varchar(60)', 'label'=>'Bank', 'enabled'=>1, 'visible'=>-1, 'position'=>40), |
||
| 309 | 'code_banque' =>array('type'=>'varchar(128)', 'label'=>'Code banque', 'enabled'=>1, 'visible'=>-1, 'position'=>45), |
||
| 310 | 'code_guichet' =>array('type'=>'varchar(6)', 'label'=>'Code guichet', 'enabled'=>1, 'visible'=>-1, 'position'=>50), |
||
| 311 | 'number' =>array('type'=>'varchar(255)', 'label'=>'Number', 'enabled'=>1, 'visible'=>-1, 'position'=>55), |
||
| 312 | 'cle_rib' =>array('type'=>'varchar(5)', 'label'=>'Cle rib', 'enabled'=>1, 'visible'=>-1, 'position'=>60), |
||
| 313 | 'bic' =>array('type'=>'varchar(11)', 'label'=>'Bic', 'enabled'=>1, 'visible'=>-1, 'position'=>65), |
||
| 314 | 'iban_prefix' =>array('type'=>'varchar(34)', 'label'=>'Iban prefix', 'enabled'=>1, 'visible'=>-1, 'position'=>70), |
||
| 315 | 'country_iban' =>array('type'=>'varchar(2)', 'label'=>'Country iban', 'enabled'=>1, 'visible'=>-1, 'position'=>75), |
||
| 316 | 'cle_iban' =>array('type'=>'varchar(2)', 'label'=>'Cle iban', 'enabled'=>1, 'visible'=>-1, 'position'=>80), |
||
| 317 | 'domiciliation' =>array('type'=>'varchar(255)', 'label'=>'Domiciliation', 'enabled'=>1, 'visible'=>-1, 'position'=>85), |
||
| 318 | 'state_id' =>array('type'=>'integer', 'label'=>'StateId', 'enabled'=>1, 'visible'=>-1, 'position'=>90), |
||
| 319 | 'fk_pays' =>array('type'=>'integer', 'label'=>'Country', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>95), |
||
| 320 | 'proprio' =>array('type'=>'varchar(60)', 'label'=>'Proprio', 'enabled'=>1, 'visible'=>-1, 'position'=>100), |
||
| 321 | 'owner_address' =>array('type'=>'varchar(255)', 'label'=>'Owner address', 'enabled'=>1, 'visible'=>-1, 'position'=>105), |
||
| 322 | 'owner_zip' =>array('type'=>'varchar(25)', 'label'=>'Owner zip', 'enabled'=>1, 'visible'=>-1, 'position'=>106), |
||
| 323 | 'owner_town' =>array('type'=>'varchar(50)', 'label'=>'Owner town', 'enabled'=>1, 'visible'=>-1, 'position'=>107), |
||
| 324 | 'owner_country_id' =>array('type'=>'integer', 'label'=>'Owner country', 'enabled'=>1, 'visible'=>-1, 'position'=>108), |
||
| 325 | 'courant' =>array('type'=>'smallint(6)', 'label'=>'Courant', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>110), |
||
| 326 | 'clos' =>array('type'=>'smallint(6)', 'label'=>'Clos', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>115), |
||
| 327 | 'rappro' =>array('type'=>'smallint(6)', 'label'=>'Rappro', 'enabled'=>1, 'visible'=>-1, 'position'=>120), |
||
| 328 | 'url' =>array('type'=>'varchar(128)', 'label'=>'Url', 'enabled'=>1, 'visible'=>-1, 'position'=>125), |
||
| 329 | 'account_number' =>array('type'=>'varchar(32)', 'label'=>'Account number', 'enabled'=>1, 'visible'=>-1, 'position'=>130), |
||
| 330 | 'fk_accountancy_journal' =>array('type'=>'integer', 'label'=>'Accountancy journal ID', 'enabled'=>1, 'visible'=>-1, 'position'=>132), |
||
| 331 | 'accountancy_journal' =>array('type'=>'varchar(20)', 'label'=>'Accountancy journal', 'enabled'=>1, 'visible'=>-1, 'position'=>135), |
||
| 332 | 'currency_code' =>array('type'=>'varchar(3)', 'label'=>'Currency code', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>140), |
||
| 333 | 'min_allowed' =>array('type'=>'integer', 'label'=>'Min allowed', 'enabled'=>1, 'visible'=>-1, 'position'=>145), |
||
| 334 | 'min_desired' =>array('type'=>'integer', 'label'=>'Min desired', 'enabled'=>1, 'visible'=>-1, 'position'=>150), |
||
| 335 | 'comment' =>array('type'=>'text', 'label'=>'Comment', 'enabled'=>1, 'visible'=>-1, 'position'=>155), |
||
| 336 | 'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>156), |
||
| 337 | 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>157), |
||
| 338 | 'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>160), |
||
| 339 | 'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>165), |
||
| 340 | 'note_public' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>170), |
||
| 341 | 'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>175), |
||
| 342 | 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>180), |
||
| 343 | 'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>185), |
||
| 344 | ); |
||
| 345 | // END MODULEBUILDER PROPERTIES |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Current account |
||
| 349 | */ |
||
| 350 | const TYPE_CURRENT = 1; |
||
| 351 | /** |
||
| 352 | * Cash account |
||
| 353 | */ |
||
| 354 | const TYPE_CASH = 2; |
||
| 355 | /** |
||
| 356 | * Savings account |
||
| 357 | */ |
||
| 358 | const TYPE_SAVINGS = 0; |
||
| 359 | |||
| 360 | |||
| 361 | const STATUS_OPEN = 0; |
||
| 362 | const STATUS_CLOSED = 1; |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * Constructor |
||
| 367 | * |
||
| 368 | * @param DoliDB $db Database handler |
||
| 369 | */ |
||
| 370 | public function __construct(DoliDB $db) |
||
| 371 | { |
||
| 372 | global $langs; |
||
| 373 | |||
| 374 | $this->db = $db; |
||
| 375 | |||
| 376 | $this->solde = 0; |
||
| 377 | |||
| 378 | $this->type_lib = array( |
||
| 379 | self::TYPE_SAVINGS => $langs->trans("BankType0"), |
||
| 380 | self::TYPE_CURRENT => $langs->trans("BankType1"), |
||
| 381 | self::TYPE_CASH => $langs->trans("BankType2"), |
||
| 382 | ); |
||
| 383 | |||
| 384 | $this->status = array( |
||
| 385 | self::STATUS_OPEN => $langs->trans("StatusAccountOpened"), |
||
| 386 | self::STATUS_CLOSED => $langs->trans("StatusAccountClosed") |
||
| 387 | ); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Shows the account number in the appropriate format |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | public function __toString() |
||
| 396 | { |
||
| 397 | $string = ''; |
||
| 398 | foreach ($this->getFieldsToShow() as $val) { |
||
| 399 | if ($val == 'BankCode') { |
||
| 400 | $string .= $this->code_banque.' '; |
||
| 401 | } elseif ($val == 'BankAccountNumber') { |
||
| 402 | $string .= $this->number.' '; |
||
| 403 | } elseif ($val == 'DeskCode') { |
||
| 404 | $string .= $this->code_guichet.' '; |
||
| 405 | } elseif ($val == 'BankAccountNumberKey') { |
||
| 406 | $string .= $this->cle_rib.' '; |
||
| 407 | } elseif ($val == 'BIC') { |
||
| 408 | $string .= $this->bic.' '; |
||
| 409 | } elseif ($val == 'IBAN') { |
||
| 410 | $string .= $this->iban.' '; |
||
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | return trim($string); |
||
| 415 | } |
||
| 416 | |||
| 417 | |||
| 418 | /** |
||
| 419 | * Return if a bank account need to be conciliated |
||
| 420 | * |
||
| 421 | * @return int 1 if need to be concialiated, < 0 otherwise. |
||
| 422 | */ |
||
| 423 | public function canBeConciliated() |
||
| 424 | { |
||
| 425 | global $conf; |
||
| 426 | |||
| 427 | if (empty($this->rappro)) { |
||
| 428 | return -1; |
||
| 429 | } |
||
| 430 | if ($this->courant == Account::TYPE_CASH && empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) { |
||
| 431 | return -2; |
||
| 432 | } |
||
| 433 | if ($this->clos) { |
||
| 434 | return -3; |
||
| 435 | } |
||
| 436 | return 1; |
||
| 437 | } |
||
| 438 | |||
| 439 | |||
| 440 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 441 | /** |
||
| 442 | * Add a link between bank line record and its source |
||
| 443 | * |
||
| 444 | * @param int $line_id Id of bank entry |
||
| 445 | * @param int $url_id Id of object related to link |
||
| 446 | * @param string $url Url (deprecated, we use now 'url_id' and 'type' instead) |
||
| 447 | * @param string $label Link label |
||
| 448 | * @param string $type Type of link ('payment', 'company', 'member', ...) |
||
| 449 | * @return int <0 if KO, id line if OK |
||
| 450 | */ |
||
| 451 | public function add_url_line($line_id, $url_id, $url, $label, $type) |
||
| 452 | { |
||
| 453 | // phpcs:enable |
||
| 454 | $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url ("; |
||
| 455 | $sql .= "fk_bank"; |
||
| 456 | $sql .= ", url_id"; |
||
| 457 | $sql .= ", url"; // deprecated |
||
| 458 | $sql .= ", label"; |
||
| 459 | $sql .= ", type"; |
||
| 460 | $sql .= ") VALUES ("; |
||
| 461 | $sql .= " ".((int) $line_id); |
||
| 462 | $sql .= ", ".((int) $url_id); |
||
| 463 | $sql .= ", '".$this->db->escape($url)."'"; // dperecated |
||
| 464 | $sql .= ", '".$this->db->escape($label)."'"; |
||
| 465 | $sql .= ", '".$this->db->escape($type)."'"; |
||
| 466 | $sql .= ")"; |
||
| 467 | |||
| 468 | dol_syslog(get_class($this)."::add_url_line", LOG_DEBUG); |
||
| 469 | if ($this->db->query($sql)) { |
||
| 470 | $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url"); |
||
| 471 | return $rowid; |
||
| 472 | } else { |
||
| 473 | $this->error = $this->db->lasterror(); |
||
| 474 | return -1; |
||
| 475 | } |
||
| 476 | } |
||
| 477 | |||
| 478 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 479 | /** |
||
| 480 | * TODO Move this into AccountLine |
||
| 481 | * Return array with links from llx_bank_url |
||
| 482 | * |
||
| 483 | * @param int $fk_bank To search using bank transaction id |
||
| 484 | * @param int $url_id To search using link to |
||
| 485 | * @param string $type To search using type |
||
| 486 | * @return array|int Array of links array('url'=>, 'url_id'=>, 'label'=>, 'type'=> 'fk_bank'=> ) or -1 on error |
||
| 487 | */ |
||
| 488 | public function get_url($fk_bank = '', $url_id = '', $type = '') |
||
| 489 | { |
||
| 490 | // phpcs:enable |
||
| 491 | $lines = array(); |
||
| 492 | |||
| 493 | // Check parameters |
||
| 494 | if (!empty($fk_bank) && (!empty($url_id) || !empty($type))) { |
||
| 495 | $this->error = "ErrorBadParameter"; |
||
| 496 | return -1; |
||
| 497 | } |
||
| 498 | |||
| 499 | $sql = "SELECT fk_bank, url_id, url, label, type"; |
||
| 500 | $sql .= " FROM ".MAIN_DB_PREFIX."bank_url"; |
||
| 501 | if ($fk_bank > 0) { |
||
| 502 | $sql .= " WHERE fk_bank = ".((int) $fk_bank); |
||
| 503 | } else { |
||
| 504 | $sql .= " WHERE url_id = ".((int) $url_id)." AND type = '".$this->db->escape($type)."'"; |
||
| 505 | } |
||
| 506 | $sql .= " ORDER BY type, label"; |
||
| 507 | |||
| 508 | dol_syslog(get_class($this)."::get_url", LOG_DEBUG); |
||
| 509 | $result = $this->db->query($sql); |
||
| 510 | if ($result) { |
||
| 511 | $i = 0; |
||
| 512 | $num = $this->db->num_rows($result); |
||
| 513 | while ($i < $num) { |
||
| 514 | $obj = $this->db->fetch_object($result); |
||
| 515 | // Anciens liens (pour compatibilite) |
||
| 516 | $lines[$i][0] = $obj->url; |
||
| 517 | $lines[$i][1] = $obj->url_id; |
||
| 518 | $lines[$i][2] = $obj->label; |
||
| 519 | $lines[$i][3] = $obj->type; |
||
| 520 | // Nouveaux liens |
||
| 521 | $lines[$i]['url'] = $obj->url; |
||
| 522 | $lines[$i]['url_id'] = $obj->url_id; |
||
| 523 | $lines[$i]['label'] = $obj->label; |
||
| 524 | $lines[$i]['type'] = $obj->type; |
||
| 525 | $lines[$i]['fk_bank'] = $obj->fk_bank; |
||
| 526 | $i++; |
||
| 527 | } |
||
| 528 | } else { |
||
| 529 | dol_print_error($this->db); |
||
| 530 | } |
||
| 531 | |||
| 532 | return $lines; |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Add an entry into table ".MAIN_DB_PREFIX."bank |
||
| 537 | * |
||
| 538 | * @param int $date Date operation |
||
| 539 | * @param string $oper 'VIR','PRE','LIQ','VAD','CB','CHQ'... |
||
| 540 | * @param string $label Descripton |
||
| 541 | * @param float $amount Amount |
||
| 542 | * @param string $num_chq Numero cheque or transfer |
||
| 543 | * @param int $categorie Category id (optionnal) |
||
| 544 | * @param User $user User that create |
||
| 545 | * @param string $emetteur Name of cheque writer |
||
| 546 | * @param string $banque Bank of cheque writer |
||
| 547 | * @param string $accountancycode When we record a free bank entry, we must provide accounting account if accountancy module is on. |
||
| 548 | * @param int $datev Date value |
||
| 549 | * @param string $num_releve Label of bank receipt for reconciliation |
||
| 550 | * @param float $amount_main_currency Amount |
||
| 551 | * @return int Rowid of added entry, <0 if KO |
||
| 552 | */ |
||
| 553 | public function addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur = '', $banque = '', $accountancycode = '', $datev = null, $num_releve = '', $amount_main_currency = null) |
||
| 554 | { |
||
| 555 | // Deprecation warning |
||
| 556 | if (is_numeric($oper)) { |
||
| 557 | dol_syslog(__METHOD__.": using numeric operations is deprecated", LOG_WARNING); |
||
| 558 | } |
||
| 559 | |||
| 560 | if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility |
||
| 561 | $this->id = $this->rowid; |
||
| 562 | } |
||
| 563 | |||
| 564 | // Clean parameters |
||
| 565 | $emetteur = trim($emetteur); |
||
| 566 | $banque = trim($banque); |
||
| 567 | $label = trim($label); |
||
| 568 | |||
| 569 | $now = dol_now(); |
||
| 570 | |||
| 571 | if (is_numeric($oper)) { // Clean operation to have a code instead of a rowid |
||
| 572 | $sql = "SELECT code FROM ".MAIN_DB_PREFIX."c_paiement"; |
||
| 573 | $sql .= " WHERE id = ".((int) $oper); |
||
| 574 | $sql .= " AND entity IN (".getEntity('c_paiement').")"; |
||
| 575 | $resql = $this->db->query($sql); |
||
| 576 | if ($resql) { |
||
| 577 | $obj = $this->db->fetch_object($resql); |
||
| 578 | $oper = $obj->code; |
||
| 579 | } else { |
||
| 580 | dol_print_error($this->db, 'Failed to get payment type code'); |
||
| 581 | return -1; |
||
| 582 | } |
||
| 583 | } |
||
| 584 | |||
| 585 | // Check parameters |
||
| 586 | if (!$oper) { |
||
| 587 | $this->error = "oper not defined"; |
||
| 588 | return -1; |
||
| 589 | } |
||
| 590 | if (!$this->id) { |
||
| 591 | $this->error = "this->id not defined"; |
||
| 592 | return -2; |
||
| 593 | } |
||
| 594 | if ($this->courant == Account::TYPE_CASH && $oper != 'LIQ') { |
||
| 595 | $this->error = "ErrorCashAccountAcceptsOnlyCashMoney"; |
||
| 596 | return -3; |
||
| 597 | } |
||
| 598 | |||
| 599 | $this->db->begin(); |
||
| 600 | |||
| 601 | if (is_null($datev) || empty($datev)) { |
||
| 602 | $datev = $date; |
||
| 603 | } |
||
| 604 | |||
| 605 | $accline = new AccountLine($this->db); |
||
| 606 | $accline->datec = $now; |
||
| 607 | $accline->dateo = $date; |
||
| 608 | $accline->datev = $datev; |
||
| 609 | $accline->label = $label; |
||
| 610 | $accline->amount = $amount; |
||
| 611 | $accline->amount_main_currency = $amount_main_currency; |
||
| 612 | $accline->fk_user_author = $user->id; |
||
| 613 | $accline->fk_account = $this->id; |
||
| 614 | $accline->fk_type = $oper; |
||
| 615 | $accline->numero_compte = $accountancycode; |
||
| 616 | $accline->num_releve = $num_releve; |
||
| 617 | |||
| 618 | if ($num_chq) { |
||
| 619 | $accline->num_chq = $num_chq; |
||
| 620 | } |
||
| 621 | |||
| 622 | if ($emetteur) { |
||
| 623 | $accline->emetteur = $emetteur; |
||
| 624 | } |
||
| 625 | |||
| 626 | if ($banque) { |
||
| 627 | $accline->bank_chq = $banque; |
||
| 628 | } |
||
| 629 | |||
| 630 | if ($accline->insert() > 0) { |
||
| 631 | if ($categorie > 0) { |
||
| 632 | $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class("; |
||
| 633 | $sql .= "lineid, fk_categ"; |
||
| 634 | $sql .= ") VALUES ("; |
||
| 635 | $sql .= ((int) $accline->id).", '".$this->db->escape($categorie)."'"; |
||
| 636 | $sql .= ")"; |
||
| 637 | |||
| 638 | $result = $this->db->query($sql); |
||
| 639 | if (!$result) { |
||
| 640 | $this->error = $this->db->lasterror(); |
||
| 641 | $this->db->rollback(); |
||
| 642 | |||
| 643 | return -4; |
||
| 644 | } |
||
| 645 | } |
||
| 646 | |||
| 647 | $this->db->commit(); |
||
| 648 | |||
| 649 | return $accline->id; |
||
| 650 | } else { |
||
| 651 | $this->error = $accline->error; |
||
| 652 | $this->errors = $accline->errors; |
||
| 653 | $this->db->rollback(); |
||
| 654 | |||
| 655 | return -5; |
||
| 656 | } |
||
| 657 | } |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Create bank account into database |
||
| 661 | * |
||
| 662 | * @param User $user Object user making creation |
||
| 663 | * @param int $notrigger 1=Disable triggers |
||
| 664 | * @return int < 0 if KO, > 0 if OK |
||
| 665 | */ |
||
| 666 | public function create(User $user, $notrigger = 0) |
||
| 667 | { |
||
| 668 | global $langs, $conf; |
||
| 669 | |||
| 670 | $error = 0; |
||
| 671 | |||
| 672 | // Clean parameters |
||
| 673 | if (!$this->min_allowed) { |
||
| 674 | $this->min_allowed = 0; |
||
| 675 | } |
||
| 676 | if (!$this->min_desired) { |
||
| 677 | $this->min_desired = 0; |
||
| 678 | } |
||
| 679 | |||
| 680 | // Check parameters |
||
| 681 | if (empty($this->country_id)) { |
||
| 682 | $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country")); |
||
| 683 | dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR); |
||
| 684 | return -1; |
||
| 685 | } |
||
| 686 | if (empty($this->ref)) { |
||
| 687 | $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref")); |
||
| 688 | dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR); |
||
| 689 | return -1; |
||
| 690 | } |
||
| 691 | if (empty($this->date_solde)) { |
||
| 692 | $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("DateInitialBalance")); |
||
| 693 | dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR); |
||
| 694 | return -1; |
||
| 695 | } |
||
| 696 | |||
| 697 | // Chargement librairie pour acces fonction controle RIB |
||
| 698 | require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php'; |
||
| 699 | |||
| 700 | $now = dol_now(); |
||
| 701 | |||
| 702 | $this->db->begin(); |
||
| 703 | |||
| 704 | $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account ("; |
||
| 705 | $sql .= "datec"; |
||
| 706 | $sql .= ", ref"; |
||
| 707 | $sql .= ", label"; |
||
| 708 | $sql .= ", entity"; |
||
| 709 | $sql .= ", account_number"; |
||
| 710 | $sql .= ", fk_accountancy_journal"; |
||
| 711 | $sql .= ", bank"; |
||
| 712 | $sql .= ", code_banque"; |
||
| 713 | $sql .= ", code_guichet"; |
||
| 714 | $sql .= ", number"; |
||
| 715 | $sql .= ", cle_rib"; |
||
| 716 | $sql .= ", bic"; |
||
| 717 | $sql .= ", iban_prefix"; |
||
| 718 | $sql .= ", domiciliation"; |
||
| 719 | $sql .= ", pti_in_ctti"; |
||
| 720 | $sql .= ", proprio"; |
||
| 721 | $sql .= ", owner_address"; |
||
| 722 | $sql .= ", owner_zip"; |
||
| 723 | $sql .= ", owner_town"; |
||
| 724 | $sql .= ", owner_country_id"; |
||
| 725 | $sql .= ", currency_code"; |
||
| 726 | $sql .= ", rappro"; |
||
| 727 | $sql .= ", min_allowed"; |
||
| 728 | $sql .= ", min_desired"; |
||
| 729 | $sql .= ", comment"; |
||
| 730 | $sql .= ", state_id"; |
||
| 731 | $sql .= ", fk_pays"; |
||
| 732 | $sql .= ", ics"; |
||
| 733 | $sql .= ", ics_transfer"; |
||
| 734 | $sql .= ") VALUES ("; |
||
| 735 | $sql .= "'".$this->db->idate($now)."'"; |
||
| 736 | $sql .= ", '".$this->db->escape($this->ref)."'"; |
||
| 737 | $sql .= ", '".$this->db->escape($this->label)."'"; |
||
| 738 | $sql .= ", ".((int) $conf->entity); |
||
| 739 | $sql .= ", '".$this->db->escape($this->account_number)."'"; |
||
| 740 | $sql .= ", ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null"); |
||
| 741 | $sql .= ", '".$this->db->escape($this->bank)."'"; |
||
| 742 | $sql .= ", '".$this->db->escape($this->code_banque)."'"; |
||
| 743 | $sql .= ", '".$this->db->escape($this->code_guichet)."'"; |
||
| 744 | $sql .= ", '".$this->db->escape($this->number)."'"; |
||
| 745 | $sql .= ", '".$this->db->escape($this->cle_rib)."'"; |
||
| 746 | $sql .= ", '".$this->db->escape($this->bic)."'"; |
||
| 747 | $sql .= ", '".$this->db->escape($this->iban)."'"; |
||
| 748 | $sql .= ", '".$this->db->escape($this->domiciliation)."'"; |
||
| 749 | $sql .= ", ".((int) $this->pti_in_ctti); |
||
| 750 | $sql .= ", '".$this->db->escape($this->proprio)."'"; |
||
| 751 | $sql .= ", '".$this->db->escape($this->owner_address)."'"; |
||
| 752 | $sql .= ", '".$this->db->escape($this->owner_zip)."'"; |
||
| 753 | $sql .= ", '".$this->db->escape($this->owner_town)."'"; |
||
| 754 | $sql .= ", ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null"); |
||
| 755 | $sql .= ", '".$this->db->escape($this->currency_code)."'"; |
||
| 756 | $sql .= ", ".((int) $this->rappro); |
||
| 757 | $sql .= ", ".price2num($this->min_allowed, 'MT'); |
||
| 758 | $sql .= ", ".price2num($this->min_desired, 'MT'); |
||
| 759 | $sql .= ", '".$this->db->escape($this->comment)."'"; |
||
| 760 | $sql .= ", ".($this->state_id > 0 ? ((int) $this->state_id) : "null"); |
||
| 761 | $sql .= ", ".($this->country_id > 0 ? ((int) $this->country_id) : "null"); |
||
| 762 | $sql .= ", '".$this->db->escape($this->ics)."'"; |
||
| 763 | $sql .= ", '".$this->db->escape($this->ics_transfer)."'"; |
||
| 764 | $sql .= ")"; |
||
| 765 | |||
| 766 | dol_syslog(get_class($this)."::create", LOG_DEBUG); |
||
| 767 | $resql = $this->db->query($sql); |
||
| 768 | if ($resql) { |
||
| 769 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account"); |
||
| 770 | |||
| 771 | $result = $this->update($user, 1); |
||
| 772 | if ($result > 0) { |
||
| 773 | $accline = new AccountLine($this->db); |
||
| 774 | $accline->datec = $this->db->idate($now); |
||
| 775 | $accline->label = '('.$langs->trans("InitialBankBalance").')'; |
||
| 776 | $accline->amount = price2num($this->solde); |
||
| 777 | $accline->fk_user_author = $user->id; |
||
| 778 | $accline->fk_account = $this->id; |
||
| 779 | $accline->datev = $this->db->idate($this->date_solde); |
||
| 780 | $accline->dateo = $this->db->idate($this->date_solde); |
||
| 781 | $accline->fk_type = 'SOLD'; |
||
| 782 | |||
| 783 | if ($accline->insert() < 0) { |
||
| 784 | $error++; |
||
| 785 | $this->error = $accline->error; |
||
| 786 | $this->errors = $accline->errors; |
||
| 787 | } |
||
| 788 | |||
| 789 | if (!$error) { |
||
| 790 | $result = $this->insertExtraFields(); |
||
| 791 | if ($result < 0) { |
||
| 792 | $error++; |
||
| 793 | } |
||
| 794 | } |
||
| 795 | |||
| 796 | if (!$error && !$notrigger) { |
||
| 797 | // Call trigger |
||
| 798 | $result = $this->call_trigger('BANKACCOUNT_CREATE', $user); |
||
| 799 | if ($result < 0) { |
||
| 800 | $error++; |
||
| 801 | } |
||
| 802 | // End call triggers |
||
| 803 | } |
||
| 804 | } else { |
||
| 805 | $error++; |
||
| 806 | } |
||
| 807 | } else { |
||
| 808 | if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') { |
||
| 809 | $this->error = $langs->trans("ErrorBankLabelAlreadyExists"); |
||
| 810 | $error++; |
||
| 811 | } else { |
||
| 812 | $this->error = $this->db->error()." sql=".$sql; |
||
| 813 | $error++; |
||
| 814 | } |
||
| 815 | } |
||
| 816 | |||
| 817 | if (!$error) { |
||
| 818 | $this->db->commit(); |
||
| 819 | return $this->id; |
||
| 820 | } else { |
||
| 821 | $this->db->rollback(); |
||
| 822 | return -1 * $error; |
||
| 823 | } |
||
| 824 | } |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Update bank account card |
||
| 828 | * |
||
| 829 | * @param User $user Object user making action |
||
| 830 | * @param int $notrigger 1=Disable triggers |
||
| 831 | * @return int <0 if KO, >0 if OK |
||
| 832 | */ |
||
| 833 | public function update(User $user, $notrigger = 0) |
||
| 834 | { |
||
| 835 | global $langs, $conf; |
||
| 836 | |||
| 837 | $error = 0; |
||
| 838 | |||
| 839 | $this->db->begin(); |
||
| 840 | |||
| 841 | // Check parameters |
||
| 842 | if (empty($this->country_id)) { |
||
| 843 | $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country")); |
||
| 844 | dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR); |
||
| 845 | return -1; |
||
| 846 | } |
||
| 847 | if (empty($this->ref)) { |
||
| 848 | $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref")); |
||
| 849 | dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR); |
||
| 850 | return -1; |
||
| 851 | } |
||
| 852 | if (!$this->label) { |
||
| 853 | $this->label = "???"; |
||
| 854 | } |
||
| 855 | |||
| 856 | $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET "; |
||
| 857 | |||
| 858 | $sql .= " ref = '".$this->db->escape($this->ref)."'"; |
||
| 859 | $sql .= ",label = '".$this->db->escape($this->label)."'"; |
||
| 860 | |||
| 861 | $sql .= ",courant = ".((int) $this->courant); |
||
| 862 | $sql .= ",clos = ".((int) $this->clos); |
||
| 863 | $sql .= ",rappro = ".((int) $this->rappro); |
||
| 864 | $sql .= ",url = ".($this->url ? "'".$this->db->escape($this->url)."'" : "null"); |
||
| 865 | $sql .= ",account_number = '".$this->db->escape($this->account_number)."'"; |
||
| 866 | $sql .= ",fk_accountancy_journal = ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null"); |
||
| 867 | $sql .= ",bank = '".$this->db->escape($this->bank)."'"; |
||
| 868 | $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'"; |
||
| 869 | $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'"; |
||
| 870 | $sql .= ",number='".$this->db->escape($this->number)."'"; |
||
| 871 | $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'"; |
||
| 872 | $sql .= ",bic='".$this->db->escape($this->bic)."'"; |
||
| 873 | $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'"; |
||
| 874 | $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'"; |
||
| 875 | $sql .= ",pti_in_ctti=".((int) $this->pti_in_ctti); |
||
| 876 | $sql .= ",proprio = '".$this->db->escape($this->proprio)."'"; |
||
| 877 | $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'"; |
||
| 878 | $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'"; |
||
| 879 | $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'"; |
||
| 880 | $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null"); |
||
| 881 | |||
| 882 | $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'"; |
||
| 883 | |||
| 884 | $sql .= ",min_allowed = ".($this->min_allowed != '' ? price2num($this->min_allowed) : "null"); |
||
| 885 | $sql .= ",min_desired = ".($this->min_desired != '' ? price2num($this->min_desired) : "null"); |
||
| 886 | $sql .= ",comment = '".$this->db->escape($this->comment)."'"; |
||
| 887 | |||
| 888 | $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null"); |
||
| 889 | $sql .= ",fk_pays = ".($this->country_id > 0 ? ((int) $this->country_id) : "null"); |
||
| 890 | $sql .= ",ics = '".$this->db->escape($this->ics)."'"; |
||
| 891 | $sql .= ",ics_transfer = '".$this->db->escape($this->ics_transfer)."'"; |
||
| 892 | |||
| 893 | $sql .= " WHERE rowid = ".((int) $this->id); |
||
| 894 | |||
| 895 | dol_syslog(get_class($this)."::update", LOG_DEBUG); |
||
| 896 | $result = $this->db->query($sql); |
||
| 897 | if ($result) { |
||
| 898 | // Actions on extra fields (by external module or standard code) |
||
| 899 | if (!$error) { |
||
| 900 | $result = $this->insertExtraFields(); |
||
| 901 | if ($result < 0) { |
||
| 902 | $error++; |
||
| 903 | } |
||
| 904 | } |
||
| 905 | |||
| 906 | if (!$error && !$notrigger) { |
||
| 907 | // Call trigger |
||
| 908 | $result = $this->call_trigger('BANKACCOUNT_MODIFY', $user); |
||
| 909 | if ($result < 0) { |
||
| 910 | $error++; |
||
| 911 | } |
||
| 912 | // End call triggers |
||
| 913 | } |
||
| 914 | } else { |
||
| 915 | $error++; |
||
| 916 | $this->error = $this->db->lasterror(); |
||
| 917 | dol_print_error($this->db); |
||
| 918 | } |
||
| 919 | |||
| 920 | if (!$error) { |
||
| 921 | $this->db->commit(); |
||
| 922 | return $this->id; |
||
| 923 | } else { |
||
| 924 | $this->db->rollback(); |
||
| 925 | return -1 * $error; |
||
| 926 | } |
||
| 927 | } |
||
| 928 | |||
| 929 | |||
| 930 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 931 | /** |
||
| 932 | * Update BBAN (RIB) account fields |
||
| 933 | * |
||
| 934 | * @param User $user Object user making update |
||
| 935 | * @return int <0 if KO, >0 if OK |
||
| 936 | */ |
||
| 937 | public function update_bban(User $user = null) |
||
| 938 | { |
||
| 939 | // phpcs:enable |
||
| 940 | global $conf, $langs; |
||
| 941 | |||
| 942 | // Load library to get BAN control function |
||
| 943 | require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php'; |
||
| 944 | |||
| 945 | dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban"); |
||
| 946 | |||
| 947 | // Check parameters |
||
| 948 | if (!$this->ref) { |
||
| 949 | $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->trans("Ref")); |
||
| 950 | return -2; |
||
| 951 | } |
||
| 952 | |||
| 953 | $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET "; |
||
| 954 | $sql .= " bank = '".$this->db->escape($this->bank)."'"; |
||
| 955 | $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'"; |
||
| 956 | $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'"; |
||
| 957 | $sql .= ",number='".$this->db->escape($this->number)."'"; |
||
| 958 | $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'"; |
||
| 959 | $sql .= ",bic='".$this->db->escape($this->bic)."'"; |
||
| 960 | $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'"; |
||
| 961 | $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'"; |
||
| 962 | $sql .= ",proprio = '".$this->db->escape($this->proprio)."'"; |
||
| 963 | $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'"; |
||
| 964 | $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'"; |
||
| 965 | $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'"; |
||
| 966 | $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null"); |
||
| 967 | $sql .= ",state_id = ".($this->state_id > 0 ? $this->state_id : "null"); |
||
| 968 | $sql .= ",fk_pays = ".($this->country_id > 0 ? $this->country_id : "null"); |
||
| 969 | $sql .= " WHERE rowid = ".((int) $this->id); |
||
| 970 | $sql .= " AND entity = ".((int) $conf->entity); |
||
| 971 | |||
| 972 | dol_syslog(get_class($this)."::update_bban", LOG_DEBUG); |
||
| 973 | |||
| 974 | $result = $this->db->query($sql); |
||
| 975 | if ($result) { |
||
| 976 | return 1; |
||
| 977 | } else { |
||
| 978 | $this->error = $this->db->lasterror(); |
||
| 979 | dol_print_error($this->db); |
||
| 980 | return -1; |
||
| 981 | } |
||
| 982 | } |
||
| 983 | |||
| 984 | |||
| 985 | /** |
||
| 986 | * Load a bank account into memory from database |
||
| 987 | * |
||
| 988 | * @param int $id Id of bank account to get |
||
| 989 | * @param string $ref Ref of bank account to get |
||
| 990 | * @return int <0 if KO, >0 if OK |
||
| 991 | */ |
||
| 992 | public function fetch($id, $ref = '') |
||
| 993 | { |
||
| 994 | global $conf; |
||
| 995 | |||
| 996 | if (empty($id) && empty($ref)) { |
||
| 997 | $this->error = "ErrorBadParameters"; |
||
| 998 | return -1; |
||
| 999 | } |
||
| 1000 | |||
| 1001 | $sql = "SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant, ba.clos, ba.rappro, ba.url,"; |
||
| 1002 | $sql .= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,"; |
||
| 1003 | $sql .= " ba.domiciliation as address, ba.pti_in_ctti, ba.proprio, ba.owner_address, ba.owner_zip, ba.owner_town, ba.owner_country_id, ba.state_id, ba.fk_pays as country_id,"; |
||
| 1004 | $sql .= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code,"; |
||
| 1005 | $sql .= " ba.min_allowed, ba.min_desired, ba.comment,"; |
||
| 1006 | $sql .= " ba.datec as date_creation, ba.tms as date_update, ba.ics, ba.ics_transfer,"; |
||
| 1007 | $sql .= ' c.code as country_code, c.label as country,'; |
||
| 1008 | $sql .= ' d.code_departement as state_code, d.nom as state,'; |
||
| 1009 | $sql .= ' aj.code as accountancy_journal'; |
||
| 1010 | $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba"; |
||
| 1011 | $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid'; |
||
| 1012 | $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid'; |
||
| 1013 | $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'accounting_journal as aj ON aj.rowid=ba.fk_accountancy_journal'; |
||
| 1014 | $sql .= " WHERE ba.entity IN (".getEntity($this->element).")"; |
||
| 1015 | if ($id) { |
||
| 1016 | $sql .= " AND ba.rowid = ".((int) $id); |
||
| 1017 | } |
||
| 1018 | if ($ref) { |
||
| 1019 | $sql .= " AND ba.ref = '".$this->db->escape($ref)."'"; |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | dol_syslog(get_class($this)."::fetch", LOG_DEBUG); |
||
| 1023 | $result = $this->db->query($sql); |
||
| 1024 | if ($result) { |
||
| 1025 | if ($this->db->num_rows($result)) { |
||
| 1026 | $obj = $this->db->fetch_object($result); |
||
| 1027 | |||
| 1028 | $this->id = $obj->rowid; |
||
| 1029 | $this->rowid = $obj->rowid; |
||
| 1030 | $this->ref = $obj->ref; |
||
| 1031 | $this->label = $obj->label; |
||
| 1032 | $this->type = $obj->courant; |
||
| 1033 | $this->courant = $obj->courant; |
||
| 1034 | $this->bank = $obj->bank; |
||
| 1035 | $this->clos = $obj->clos; |
||
| 1036 | $this->rappro = $obj->rappro; |
||
| 1037 | $this->url = $obj->url; |
||
| 1038 | |||
| 1039 | $this->code_banque = $obj->code_banque; |
||
| 1040 | $this->code_guichet = $obj->code_guichet; |
||
| 1041 | $this->number = $obj->number; |
||
| 1042 | $this->cle_rib = $obj->cle_rib; |
||
| 1043 | $this->bic = $obj->bic; |
||
| 1044 | $this->iban = $obj->iban; |
||
| 1045 | $this->domiciliation = $obj->address; |
||
| 1046 | $this->address = $obj->address; |
||
| 1047 | $this->pti_in_ctti = $obj->pti_in_ctti; |
||
| 1048 | $this->proprio = $obj->proprio; |
||
| 1049 | $this->owner_address = $obj->owner_address; |
||
| 1050 | $this->owner_zip = $obj->owner_zip; |
||
| 1051 | $this->owner_town = $obj->owner_town; |
||
| 1052 | $this->owner_country_id = $obj->owner_country_id; |
||
| 1053 | |||
| 1054 | $this->state_id = $obj->state_id; |
||
| 1055 | $this->state_code = $obj->state_code; |
||
| 1056 | $this->state = $obj->state; |
||
| 1057 | |||
| 1058 | $this->country_id = $obj->country_id; |
||
| 1059 | $this->country_code = $obj->country_code; |
||
| 1060 | $this->country = $obj->country; |
||
| 1061 | |||
| 1062 | $this->account_number = $obj->account_number; |
||
| 1063 | $this->fk_accountancy_journal = $obj->fk_accountancy_journal; |
||
| 1064 | $this->accountancy_journal = $obj->accountancy_journal; |
||
| 1065 | |||
| 1066 | $this->currency_code = $obj->currency_code; |
||
| 1067 | $this->account_currency_code = $obj->currency_code; |
||
| 1068 | $this->min_allowed = $obj->min_allowed; |
||
| 1069 | $this->min_desired = $obj->min_desired; |
||
| 1070 | $this->comment = $obj->comment; |
||
| 1071 | |||
| 1072 | $this->date_creation = $this->db->jdate($obj->date_creation); |
||
| 1073 | $this->date_update = $this->db->jdate($obj->date_update); |
||
| 1074 | |||
| 1075 | $this->ics = $obj->ics; |
||
| 1076 | $this->ics_transfer = $obj->ics_transfer; |
||
| 1077 | |||
| 1078 | // Retrieve all extrafield |
||
| 1079 | // fetch optionals attributes and labels |
||
| 1080 | $this->fetch_optionals(); |
||
| 1081 | |||
| 1082 | return 1; |
||
| 1083 | } else { |
||
| 1084 | return 0; |
||
| 1085 | } |
||
| 1086 | } else { |
||
| 1087 | $this->error = $this->db->lasterror(); |
||
| 1088 | $this->errors[] = $this->error; |
||
| 1089 | return -1; |
||
| 1090 | } |
||
| 1091 | } |
||
| 1092 | |||
| 1093 | /** |
||
| 1094 | * Sets object to supplied categories. |
||
| 1095 | * |
||
| 1096 | * Deletes object from existing categories not supplied. |
||
| 1097 | * Adds it to non existing supplied categories. |
||
| 1098 | * Existing categories are left untouch. |
||
| 1099 | * |
||
| 1100 | * @param int[]|int $categories Category or categories IDs |
||
| 1101 | * @return int <0 if KO, >0 if OK |
||
| 1102 | */ |
||
| 1103 | public function setCategories($categories) |
||
| 1104 | { |
||
| 1105 | require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; |
||
| 1106 | return parent::setCategoriesCommon($categories, Categorie::TYPE_ACCOUNT); |
||
| 1107 | } |
||
| 1108 | |||
| 1109 | /** |
||
| 1110 | * Delete bank account from database |
||
| 1111 | * |
||
| 1112 | * @param User $user User deleting |
||
| 1113 | * @return int <0 if KO, >0 if OK |
||
| 1114 | */ |
||
| 1115 | public function delete(User $user = null) |
||
| 1116 | { |
||
| 1117 | $error = 0; |
||
| 1118 | |||
| 1119 | $this->db->begin(); |
||
| 1120 | |||
| 1121 | // Delete link between tag and bank account |
||
| 1122 | if (!$error) { |
||
| 1123 | $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account"; |
||
| 1124 | $sql .= " WHERE fk_account = ".((int) $this->id); |
||
| 1125 | |||
| 1126 | $resql = $this->db->query($sql); |
||
| 1127 | if (!$resql) { |
||
| 1128 | $error++; |
||
| 1129 | $this->error = "Error ".$this->db->lasterror(); |
||
| 1130 | } |
||
| 1131 | } |
||
| 1132 | |||
| 1133 | if (!$error) { |
||
| 1134 | $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element; |
||
| 1135 | $sql .= " WHERE rowid = ".((int) $this->id); |
||
| 1136 | |||
| 1137 | dol_syslog(get_class($this)."::delete", LOG_DEBUG); |
||
| 1138 | $result = $this->db->query($sql); |
||
| 1139 | if ($result) { |
||
| 1140 | // Remove extrafields |
||
| 1141 | if (!$error) { |
||
| 1142 | $result = $this->deleteExtraFields(); |
||
| 1143 | if ($result < 0) { |
||
| 1144 | $error++; |
||
| 1145 | dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR); |
||
| 1146 | } |
||
| 1147 | } |
||
| 1148 | } else { |
||
| 1149 | $error++; |
||
| 1150 | $this->error = "Error ".$this->db->lasterror(); |
||
| 1151 | } |
||
| 1152 | } |
||
| 1153 | |||
| 1154 | if (!$error) { |
||
| 1155 | $this->db->commit(); |
||
| 1156 | return 1; |
||
| 1157 | } else { |
||
| 1158 | $this->db->rollback(); |
||
| 1159 | return -1; |
||
| 1160 | } |
||
| 1161 | } |
||
| 1162 | |||
| 1163 | |||
| 1164 | /** |
||
| 1165 | * Return label of object status |
||
| 1166 | * |
||
| 1167 | * @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 |
||
| 1168 | * @return string Label |
||
| 1169 | */ |
||
| 1170 | public function getLibStatut($mode = 0) |
||
| 1171 | { |
||
| 1172 | return $this->LibStatut($this->clos, $mode); |
||
| 1173 | } |
||
| 1174 | |||
| 1175 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 1176 | /** |
||
| 1177 | * Return label of given object status |
||
| 1178 | * |
||
| 1179 | * @param int $status Id status |
||
| 1180 | * @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 |
||
| 1181 | * @return string Label |
||
| 1182 | */ |
||
| 1183 | public function LibStatut($status, $mode = 0) |
||
| 1184 | { |
||
| 1185 | // phpcs:enable |
||
| 1186 | global $langs; |
||
| 1187 | $langs->load('banks'); |
||
| 1188 | |||
| 1189 | if ($status == self::STATUS_OPEN) { |
||
| 1190 | $label = $langs->transnoentitiesnoconv("StatusAccountOpened"); |
||
| 1191 | $labelshort = $langs->transnoentitiesnoconv("StatusAccountOpened"); |
||
| 1192 | $statusType = 'status4'; |
||
| 1193 | } else { |
||
| 1194 | $label = $langs->transnoentitiesnoconv("StatusAccountClosed"); |
||
| 1195 | $labelshort = $langs->transnoentitiesnoconv("StatusAccountClosed"); |
||
| 1196 | $statusType = 'status5'; |
||
| 1197 | } |
||
| 1198 | |||
| 1199 | return dolGetStatus($label, $labelshort, '', $statusType, $mode); |
||
| 1200 | } |
||
| 1201 | |||
| 1202 | |||
| 1203 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 1204 | /** |
||
| 1205 | * Renvoi si un compte peut etre supprimer ou non (sans mouvements) |
||
| 1206 | * |
||
| 1207 | * @return boolean vrai si peut etre supprime, faux sinon |
||
| 1208 | */ |
||
| 1209 | public function can_be_deleted() |
||
| 1210 | { |
||
| 1211 | // phpcs:enable |
||
| 1212 | $can_be_deleted = false; |
||
| 1213 | |||
| 1214 | $sql = "SELECT COUNT(rowid) as nb"; |
||
| 1215 | $sql .= " FROM ".MAIN_DB_PREFIX."bank"; |
||
| 1216 | $sql .= " WHERE fk_account = ".((int) $this->id); |
||
| 1217 | |||
| 1218 | $resql = $this->db->query($sql); |
||
| 1219 | if ($resql) { |
||
| 1220 | $obj = $this->db->fetch_object($resql); |
||
| 1221 | if ($obj->nb <= 1) { |
||
| 1222 | $can_be_deleted = true; // Juste le solde |
||
| 1223 | } |
||
| 1224 | } else { |
||
| 1225 | dol_print_error($this->db); |
||
| 1226 | } |
||
| 1227 | return $can_be_deleted; |
||
| 1228 | } |
||
| 1229 | |||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * Return error |
||
| 1233 | * |
||
| 1234 | * @return string Error string |
||
| 1235 | */ |
||
| 1236 | public function error() |
||
| 1237 | { |
||
| 1238 | return $this->error; |
||
| 1239 | } |
||
| 1240 | |||
| 1241 | /** |
||
| 1242 | * Return current sold |
||
| 1243 | * |
||
| 1244 | * @param int $option 1=Exclude future operation date (this is to exclude input made in advance and have real account sold) |
||
| 1245 | * @param int $date_end Date until we want to get bank account sold |
||
| 1246 | * @param string $field dateo or datev |
||
| 1247 | * @return int current sold (value date <= today) |
||
| 1248 | */ |
||
| 1249 | public function solde($option = 0, $date_end = '', $field = 'dateo') |
||
| 1250 | { |
||
| 1251 | $solde = 0; |
||
| 1252 | |||
| 1253 | $sql = "SELECT sum(amount) as amount"; |
||
| 1254 | $sql .= " FROM ".MAIN_DB_PREFIX."bank"; |
||
| 1255 | $sql .= " WHERE fk_account = ".((int) $this->id); |
||
| 1256 | if ($option == 1) { |
||
| 1257 | $sql .= " AND ".$this->db->escape($field)." <= '".(!empty($date_end) ? $this->db->idate($date_end) : $this->db->idate(dol_now()))."'"; |
||
| 1258 | } |
||
| 1259 | |||
| 1260 | $resql = $this->db->query($sql); |
||
| 1261 | if ($resql) { |
||
| 1262 | if ($this->db->num_rows($resql)) { |
||
| 1263 | $obj = $this->db->fetch_object($resql); |
||
| 1264 | $solde = $obj->amount; |
||
| 1265 | } |
||
| 1266 | $this->db->free($resql); |
||
| 1267 | } else { |
||
| 1268 | $this->errors[] = $this->db->lasterror; |
||
| 1269 | return -1; |
||
| 1270 | } |
||
| 1271 | |||
| 1272 | return price2num($solde, 'MU'); |
||
| 1273 | } |
||
| 1274 | |||
| 1275 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 1276 | /** |
||
| 1277 | * Load indicators for dashboard (this->nbtodo and this->nbtodolate) |
||
| 1278 | * |
||
| 1279 | * @param User $user Objet user |
||
| 1280 | * @param int $filteraccountid To get info for a particular account id |
||
| 1281 | * @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK |
||
| 1282 | */ |
||
| 1283 | public function load_board(User $user, $filteraccountid = 0) |
||
| 1284 | { |
||
| 1285 | // phpcs:enable |
||
| 1286 | global $conf, $langs; |
||
| 1287 | |||
| 1288 | if ($user->socid) { |
||
| 1289 | return -1; // protection pour eviter appel par utilisateur externe |
||
| 1290 | } |
||
| 1291 | |||
| 1292 | $sql = "SELECT b.rowid, b.datev as datefin"; |
||
| 1293 | $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,"; |
||
| 1294 | $sql .= " ".MAIN_DB_PREFIX."bank_account as ba"; |
||
| 1295 | $sql .= " WHERE b.rappro=0"; |
||
| 1296 | $sql .= " AND b.fk_account = ba.rowid"; |
||
| 1297 | $sql .= " AND ba.entity IN (".getEntity('bank_account').")"; |
||
| 1298 | $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable |
||
| 1299 | $sql .= " AND clos = 0"; |
||
| 1300 | if ($filteraccountid) { |
||
| 1301 | $sql .= " AND ba.rowid = ".((int) $filteraccountid); |
||
| 1302 | } |
||
| 1303 | |||
| 1304 | $resql = $this->db->query($sql); |
||
| 1305 | if ($resql) { |
||
| 1306 | $langs->load("banks"); |
||
| 1307 | $now = dol_now(); |
||
| 1308 | |||
| 1309 | require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php'; |
||
| 1310 | |||
| 1311 | $response = new WorkboardResponse(); |
||
| 1312 | $response->warning_delay = $conf->bank->rappro->warning_delay / 60 / 60 / 24; |
||
| 1313 | $response->label = $langs->trans("TransactionsToConciliate"); |
||
| 1314 | $response->labelShort = $langs->trans("TransactionsToConciliateShort"); |
||
| 1315 | $response->url = DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&mainmenu=bank'; |
||
| 1316 | $response->img = img_object('', "payment"); |
||
| 1317 | |||
| 1318 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 1319 | $response->nbtodo++; |
||
| 1320 | if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) { |
||
| 1321 | $response->nbtodolate++; |
||
| 1322 | } |
||
| 1323 | } |
||
| 1324 | |||
| 1325 | return $response; |
||
| 1326 | } else { |
||
| 1327 | dol_print_error($this->db); |
||
| 1328 | $this->error = $this->db->error(); |
||
| 1329 | return -1; |
||
| 1330 | } |
||
| 1331 | } |
||
| 1332 | |||
| 1333 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 1334 | /** |
||
| 1335 | * Charge indicateurs this->nb de tableau de bord |
||
| 1336 | * @param int $filteraccountid To get info for a particular account id |
||
| 1337 | * @return int <0 if ko, >0 if ok |
||
| 1338 | */ |
||
| 1339 | public function load_state_board($filteraccountid = 0) |
||
| 1340 | { |
||
| 1341 | // phpcs:enable |
||
| 1342 | global $user; |
||
| 1343 | |||
| 1344 | if ($user->socid) { |
||
| 1345 | return -1; // protection pour eviter appel par utilisateur externe |
||
| 1346 | } |
||
| 1347 | |||
| 1348 | $sql = "SELECT count(b.rowid) as nb"; |
||
| 1349 | $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,"; |
||
| 1350 | $sql .= " ".MAIN_DB_PREFIX."bank_account as ba"; |
||
| 1351 | $sql .= " WHERE b.fk_account = ba.rowid"; |
||
| 1352 | $sql .= " AND ba.entity IN (".getEntity('bank_account').")"; |
||
| 1353 | $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable |
||
| 1354 | $sql .= " AND clos = 0"; |
||
| 1355 | if ($filteraccountid) { |
||
| 1356 | $sql .= " AND ba.rowid = ".((int) $filteraccountid); |
||
| 1357 | } |
||
| 1358 | |||
| 1359 | $resql = $this->db->query($sql); |
||
| 1360 | if ($resql) { |
||
| 1361 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 1362 | $this->nb["banklines"] = $obj->nb; |
||
| 1363 | } |
||
| 1364 | $this->db->free($resql); |
||
| 1365 | } else { |
||
| 1366 | dol_print_error($this->db); |
||
| 1367 | $this->error = $this->db->error(); |
||
| 1368 | return -1; |
||
| 1369 | } |
||
| 1370 | } |
||
| 1371 | |||
| 1372 | |||
| 1373 | /** |
||
| 1374 | * Load indicators for dashboard (this->nbtodo and this->nbtodolate) |
||
| 1375 | * |
||
| 1376 | * @return int Nb of account we can reconciliate |
||
| 1377 | */ |
||
| 1378 | public function countAccountToReconcile() |
||
| 1405 | } |
||
| 1406 | |||
| 1407 | /** |
||
| 1408 | * getTooltipContentArray |
||
| 1409 | * @param array $params params to construct tooltip data |
||
| 1410 | * @since v18 |
||
| 1411 | * @return array |
||
| 1412 | */ |
||
| 1413 | public function getTooltipContentArray($params) |
||
| 1414 | { |
||
| 1415 | global $langs; |
||
| 1416 | include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php'; |
||
| 1417 | |||
| 1418 | $datas = []; |
||
| 1419 | |||
| 1420 | $nofetch = empty($params['nofetch']) ? false : true; |
||
| 1421 | $pictos = img_picto('', $this->picto).' <u class="paddingrightnow">'.$langs->trans("BankAccount").'</u>'; |
||
| 1422 | if (isset($this->status)) { |
||
| 1423 | $pictos .= ' '.$this->getLibStatut(5); |
||
| 1424 | } |
||
| 1425 | $datas['picto'] = $pictos; |
||
| 1426 | $datas['label'] = '<br><b>'.$langs->trans('Label').':</b> '.$this->label; |
||
| 1427 | $datas['accountnumber'] = '<br><b>'.$langs->trans('AccountNumber').':</b> '.$this->number; |
||
| 1428 | $datas['iban'] = '<br><b>'.$langs->trans('IBAN').':</b> '.getIbanHumanReadable($this); |
||
| 1429 | $datas['bic'] = '<br><b>'.$langs->trans('BIC').':</b> '.$this->bic; |
||
| 1430 | $datas['accountcurrency'] = '<br><b>'.$langs->trans("AccountCurrency").':</b> '.$this->currency_code; |
||
| 1431 | |||
| 1432 | if (isModEnabled('accounting')) { |
||
| 1433 | include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php'; |
||
| 1434 | $langs->load("accountancy"); |
||
| 1435 | $datas['accountaccounting'] = '<br><b>'.$langs->trans('AccountAccounting').':</b> '.length_accountg($this->account_number); |
||
| 1436 | $datas['accountancyjournal'] = '<br><b>'.$langs->trans('AccountancyJournal').':</b> '.$this->accountancy_journal; |
||
| 1437 | } |
||
| 1438 | // show categories for this record only in ajax to not overload lists |
||
| 1439 | if (isModEnabled('categorie') && !$nofetch) { |
||
| 1440 | require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php'; |
||
| 1441 | $form = new Form($this->db); |
||
| 1442 | $datas['categories'] = '<br>' . $form->showCategories($this->id, Categorie::TYPE_ACCOUNT, 1); |
||
| 1443 | } |
||
| 1444 | |||
| 1445 | return $datas; |
||
| 1446 | } |
||
| 1447 | |||
| 1448 | /** |
||
| 1449 | * Return clicable name (with picto eventually) |
||
| 1450 | * |
||
| 1451 | * @param int $withpicto Include picto into link |
||
| 1452 | * @param string $mode ''=Link to card, 'transactions'=Link to transactions card |
||
| 1453 | * @param string $option ''=Show ref, 'reflabel'=Show ref+label |
||
| 1454 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
| 1455 | * @param int $notooltip 1=Disable tooltip |
||
| 1456 | * @return string Chaine avec URL |
||
| 1457 | */ |
||
| 1458 | public function getNomUrl($withpicto = 0, $mode = '', $option = '', $save_lastsearch_value = -1, $notooltip = 0) |
||
| 1459 | { |
||
| 1460 | global $conf, $langs, $user; |
||
| 1461 | include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php'; |
||
| 1462 | |||
| 1463 | $result = ''; |
||
| 1464 | $classfortooltip = 'classfortooltip'; |
||
| 1465 | $dataparams = ''; |
||
| 1466 | $params = [ |
||
| 1467 | 'id' => $this->id, |
||
| 1468 | 'objecttype' => $this->element, |
||
| 1469 | 'option' => $option, |
||
| 1470 | 'nofetch' => 1, |
||
| 1471 | ]; |
||
| 1472 | if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) { |
||
| 1473 | $classfortooltip = 'classforajaxtooltip'; |
||
| 1474 | $dataparams = ' data-params='.json_encode($params); |
||
| 1475 | } |
||
| 1476 | $label = implode($this->getTooltipContentArray($params)); |
||
| 1477 | |||
| 1478 | $linkclose = '"'.$dataparams.' title="'.dol_escape_htmltag($label, 1).'" class="'.$classfortooltip.'">'; |
||
| 1479 | |||
| 1480 | $url = DOL_URL_ROOT.'/compta/bank/card.php?id='.$this->id; |
||
| 1481 | if ($mode == 'transactions') { |
||
| 1482 | $url = DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id; |
||
| 1483 | } elseif ($mode == 'receipts') { |
||
| 1484 | $url = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$this->id; |
||
| 1485 | } |
||
| 1486 | |||
| 1487 | if ($option != 'nolink') { |
||
| 1488 | // Add param to save lastsearch_values or not |
||
| 1489 | $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0); |
||
| 1490 | if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { |
||
| 1491 | $add_save_lastsearch_values = 1; |
||
| 1492 | } |
||
| 1493 | if ($add_save_lastsearch_values) { |
||
| 1494 | $url .= '&save_lastsearch_values=1'; |
||
| 1495 | } |
||
| 1496 | } |
||
| 1497 | |||
| 1498 | $linkstart = '<a href="'.$url.$linkclose; |
||
| 1499 | $linkend = '</a>'; |
||
| 1500 | |||
| 1501 | if ($option == 'nolink') { |
||
| 1502 | $linkstart = ''; |
||
| 1503 | $linkend = ''; |
||
| 1504 | } |
||
| 1505 | |||
| 1506 | $result .= $linkstart; |
||
| 1507 | if ($withpicto) { |
||
| 1508 | $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1); |
||
| 1509 | } |
||
| 1510 | if ($withpicto != 2) { |
||
| 1511 | $result .= $this->ref.($option == 'reflabel' && $this->label ? ' - '.$this->label : ''); |
||
| 1512 | } |
||
| 1513 | $result .= $linkend; |
||
| 1514 | |||
| 1515 | return $result; |
||
| 1516 | } |
||
| 1517 | |||
| 1518 | |||
| 1519 | // Method after here are common to Account and CompanyBankAccount |
||
| 1520 | |||
| 1521 | |||
| 1522 | /** |
||
| 1523 | * Return if an account has valid information for Direct debit payment |
||
| 1524 | * |
||
| 1525 | * @return int 1 if correct, <=0 if wrong |
||
| 1526 | */ |
||
| 1527 | public function verif() |
||
| 1528 | { |
||
| 1529 | require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php'; |
||
| 1530 | |||
| 1531 | $this->error_number = 0; |
||
| 1532 | |||
| 1533 | // Call function to check BAN |
||
| 1534 | |||
| 1535 | if (!checkIbanForAccount($this)) { |
||
| 1536 | $this->error_number = 12; |
||
| 1537 | $this->error_message = 'IBANNotValid'; |
||
| 1538 | } |
||
| 1539 | if (!checkSwiftForAccount($this)) { |
||
| 1540 | $this->error_number = 12; |
||
| 1541 | $this->error_message = 'SwiftNotValid'; |
||
| 1542 | } |
||
| 1543 | /*if (! checkBanForAccount($this)) |
||
| 1544 | { |
||
| 1545 | $this->error_number = 12; |
||
| 1546 | $this->error_message = 'BANControlError'; |
||
| 1547 | }*/ |
||
| 1548 | |||
| 1549 | if ($this->error_number == 0) { |
||
| 1550 | return 1; |
||
| 1551 | } else { |
||
| 1552 | return 0; |
||
| 1553 | } |
||
| 1554 | } |
||
| 1555 | |||
| 1556 | /** |
||
| 1557 | * Return account country code |
||
| 1558 | * |
||
| 1559 | * @return string country code |
||
| 1560 | */ |
||
| 1561 | public function getCountryCode() |
||
| 1595 | } |
||
| 1596 | |||
| 1597 | /** |
||
| 1598 | * Return if a bank account is defined with detailed information (bank code, desk code, number and key). |
||
| 1599 | * More information on codes used by countries on page http://en.wikipedia.org/wiki/Bank_code |
||
| 1600 | * |
||
| 1601 | * @return int 0=No bank code need + Account number is enough |
||
| 1602 | * 1=Need 2 fields for bank code: Bank, Desk (France, Spain, ...) + Account number and key |
||
| 1603 | * 2=Need 1 field for bank code: Bank only (Sort code for Great Britain, BSB for Australia) + Account number |
||
| 1604 | */ |
||
| 1605 | public function useDetailedBBAN() |
||
| 1606 | { |
||
| 1607 | $country_code = $this->getCountryCode(); |
||
| 1608 | |||
| 1609 | if (in_array($country_code, array('FR', 'ES', 'GA', 'IT', 'NC'))) { |
||
| 1610 | return 1; // France, Spain, Gabon, ... - Not valid for CH |
||
| 1611 | } |
||
| 1612 | if (in_array($country_code, array('AD', 'AU', 'BE', 'CA', 'DE', 'DK', 'GR', 'GB', 'ID', 'IE', 'IR', 'KR', 'NL', 'NZ', 'UK', 'US'))) { |
||
| 1613 | return 2; // Australia, England... |
||
| 1614 | } |
||
| 1615 | return 0; |
||
| 1616 | } |
||
| 1617 | |||
| 1618 | /** |
||
| 1619 | * Return 1 if IBAN / BIC is mandatory (otherwise option) |
||
| 1620 | * |
||
| 1621 | * @return int 1 = mandatory / 0 = Not mandatory |
||
| 1622 | */ |
||
| 1623 | public function needIBAN() |
||
| 1624 | { |
||
| 1625 | global $conf; |
||
| 1626 | |||
| 1627 | if (!empty($conf->global->MAIN_IBAN_IS_NEVER_MANDATORY)) { |
||
| 1628 | return 0; |
||
| 1629 | } |
||
| 1630 | |||
| 1631 | $country_code = $this->getCountryCode(); |
||
| 1632 | |||
| 1633 | $country_code_in_EEC = array( |
||
| 1634 | 'AT', // Austria |
||
| 1635 | 'BE', // Belgium |
||
| 1636 | 'BG', // Bulgaria |
||
| 1637 | 'CY', // Cyprus |
||
| 1638 | 'CZ', // Czech republic |
||
| 1639 | 'DE', // Germany |
||
| 1640 | 'DK', // Danemark |
||
| 1641 | 'EE', // Estonia |
||
| 1642 | 'ES', // Spain |
||
| 1643 | 'FI', // Finland |
||
| 1644 | 'FR', // France |
||
| 1645 | 'GB', // United Kingdom |
||
| 1646 | 'GR', // Greece |
||
| 1647 | 'HR', // Croatia |
||
| 1648 | 'NL', // Holland |
||
| 1649 | 'HU', // Hungary |
||
| 1650 | 'IE', // Ireland |
||
| 1651 | 'IM', // Isle of Man - Included in UK |
||
| 1652 | 'IT', // Italy |
||
| 1653 | 'LT', // Lithuania |
||
| 1654 | 'LU', // Luxembourg |
||
| 1655 | 'LV', // Latvia |
||
| 1656 | 'MC', // Monaco - Included in France |
||
| 1657 | 'MT', // Malta |
||
| 1658 | //'NO', // Norway |
||
| 1659 | 'PL', // Poland |
||
| 1660 | 'PT', // Portugal |
||
| 1661 | 'RO', // Romania |
||
| 1662 | 'SE', // Sweden |
||
| 1663 | 'SK', // Slovakia |
||
| 1664 | 'SI', // Slovenia |
||
| 1665 | 'UK', // United Kingdom |
||
| 1666 | //'CH', // Switzerland - No. Swizerland in not in EEC |
||
| 1667 | ); |
||
| 1668 | |||
| 1669 | if (in_array($country_code, $country_code_in_EEC)) { |
||
| 1670 | return 1; // France, Spain, ... |
||
| 1671 | } |
||
| 1672 | return 0; |
||
| 1673 | } |
||
| 1674 | |||
| 1675 | /** |
||
| 1676 | * Load miscellaneous information for tab "Info" |
||
| 1677 | * |
||
| 1678 | * @param int $id Id of object to load |
||
| 1679 | * @return void |
||
| 1680 | */ |
||
| 1681 | public function info($id) |
||
| 1682 | { |
||
| 1683 | } |
||
| 1684 | |||
| 1685 | /** |
||
| 1686 | * Returns the fields in order that this bank account should show to the user |
||
| 1687 | * Will return an array with the following values: |
||
| 1688 | * - BankAccountNumber |
||
| 1689 | * - BankCode |
||
| 1690 | * - BankAccountNumberKey |
||
| 1691 | * - DeskCode |
||
| 1692 | * |
||
| 1693 | * Some countries show less or more bank account properties to the user |
||
| 1694 | * |
||
| 1695 | * @param int $includeibanbic 1=Return also key for IBAN and BIC |
||
| 1696 | * @return array Array of fields to show |
||
| 1697 | * @see useDetailedBBAN() |
||
| 1698 | */ |
||
| 1699 | public function getFieldsToShow($includeibanbic = 0) |
||
| 1700 | { |
||
| 1701 | //Get the required properties depending on the country |
||
| 1702 | $detailedBBAN = $this->useDetailedBBAN(); |
||
| 1703 | |||
| 1704 | if ($detailedBBAN == 0) { |
||
| 1705 | $fieldarray = array( |
||
| 1706 | 'BankAccountNumber' |
||
| 1707 | ); |
||
| 1708 | } elseif ($detailedBBAN == 2) { |
||
| 1709 | $fieldarray = array( |
||
| 1710 | 'BankCode', |
||
| 1711 | 'BankAccountNumber' |
||
| 1712 | ); |
||
| 1713 | } else { |
||
| 1714 | $fieldarray = self::getAccountNumberOrder(); |
||
| 1715 | } |
||
| 1716 | |||
| 1717 | //if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour) |
||
| 1718 | if ($includeibanbic) { |
||
| 1719 | $fieldarray[] = 'IBAN'; |
||
| 1720 | $fieldarray[] = 'BIC'; |
||
| 1721 | } |
||
| 1722 | //} |
||
| 1723 | |||
| 1724 | //Get the order the properties are shown |
||
| 1725 | return $fieldarray; |
||
| 1726 | } |
||
| 1727 | |||
| 1728 | /** |
||
| 1729 | * Returns the components of the bank account in order. |
||
| 1730 | * Will return an array with the following values: |
||
| 1731 | * - BankAccountNumber |
||
| 1732 | * - BankCode |
||
| 1733 | * - BankAccountNumberKey |
||
| 1734 | * - DeskCode |
||
| 1735 | * |
||
| 1736 | * @return array |
||
| 1737 | */ |
||
| 1738 | public static function getAccountNumberOrder() |
||
| 1739 | { |
||
| 1740 | global $conf; |
||
| 1741 | |||
| 1742 | $fieldlists = array( |
||
| 1743 | 'BankCode', |
||
| 1744 | 'DeskCode', |
||
| 1745 | 'BankAccountNumber', |
||
| 1746 | 'BankAccountNumberKey' |
||
| 1747 | ); |
||
| 1748 | |||
| 1749 | if (!empty($conf->global->BANK_SHOW_ORDER_OPTION)) { |
||
| 1750 | if (is_numeric($conf->global->BANK_SHOW_ORDER_OPTION)) { |
||
| 1751 | if ($conf->global->BANK_SHOW_ORDER_OPTION == '1') { |
||
| 1752 | $fieldlists = array( |
||
| 1753 | 'BankCode', |
||
| 1754 | 'DeskCode', |
||
| 1755 | 'BankAccountNumberKey', |
||
| 1756 | 'BankAccountNumber' |
||
| 1757 | ); |
||
| 1758 | } |
||
| 1759 | } else { |
||
| 1760 | //Replace the old AccountNumber key with the new BankAccountNumber key |
||
| 1761 | $fieldlists = explode( |
||
| 1762 | ' ', |
||
| 1763 | preg_replace('/ ?[^Bank]AccountNumber ?/', 'BankAccountNumber', $conf->global->BANK_SHOW_ORDER_OPTION) |
||
| 1764 | ); |
||
| 1765 | } |
||
| 1766 | } |
||
| 1767 | |||
| 1768 | return $fieldlists; |
||
| 1769 | } |
||
| 1770 | |||
| 1771 | |||
| 1772 | /** |
||
| 1773 | * Initialise an instance with random values. |
||
| 1774 | * Used to build previews or test instances. |
||
| 1775 | * id must be 0 if object instance is a specimen. |
||
| 1776 | * |
||
| 1777 | * @return void |
||
| 1778 | */ |
||
| 1779 | public function initAsSpecimen() |
||
| 1780 | { |
||
| 1781 | // Example of IBAN FR7630001007941234567890185 |
||
| 1782 | $this->specimen = 1; |
||
| 1783 | $this->ref = 'MBA'; |
||
| 1784 | $this->label = 'My Big Company Bank account'; |
||
| 1785 | $this->bank = 'MyBank'; |
||
| 1786 | $this->courant = Account::TYPE_CURRENT; |
||
| 1787 | $this->clos = Account::STATUS_OPEN; |
||
| 1788 | $this->code_banque = '30001'; |
||
| 1789 | $this->code_guichet = '00794'; |
||
| 1790 | $this->number = '12345678901'; |
||
| 1791 | $this->cle_rib = '85'; |
||
| 1792 | $this->bic = 'AA12'; |
||
| 1793 | $this->iban = 'FR7630001007941234567890185'; |
||
| 1794 | $this->domiciliation = 'Banque de France'; |
||
| 1795 | $this->proprio = 'Owner'; |
||
| 1796 | $this->owner_address = 'Owner address'; |
||
| 1797 | $this->owner_zip = 'Owner zip'; |
||
| 1798 | $this->owner_town = 'Owner town'; |
||
| 1799 | $this->owner_country_id = 'Owner country_id'; |
||
| 1800 | $this->country_id = 1; |
||
| 1801 | } |
||
| 1802 | |||
| 1803 | /** |
||
| 1804 | * Function used to replace a thirdparty id with another one. |
||
| 1805 | * |
||
| 1806 | * @param DoliDB $dbs Database handler |
||
| 1807 | * @param int $origin_id Old thirdparty id |
||
| 1808 | * @param int $dest_id New thirdparty id |
||
| 1809 | * @return bool True=SQL success, False=SQL error |
||
| 1810 | */ |
||
| 1811 | public static function replaceThirdparty($dbs, $origin_id, $dest_id) |
||
| 1812 | { |
||
| 1813 | $sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type='company'"; |
||
| 1814 | |||
| 1815 | if ($dbs->query($sql)) { |
||
| 1816 | return true; |
||
| 1817 | } else { |
||
| 1818 | //if ($ignoreerrors) return true; // TODO Not enough. If there is A-B on kept thirdparty and B-C on old one, we must get A-B-C after merge. Not A-B. |
||
| 1819 | //$this->errors = $dbs->lasterror(); |
||
| 1820 | return false; |
||
| 1821 | } |
||
| 1822 | } |
||
| 1823 | |||
| 1824 | /** |
||
| 1825 | * Return clicable link of object (with eventually picto) |
||
| 1826 | * |
||
| 1827 | * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link) |
||
| 1828 | * @param array $arraydata Array of data |
||
| 1829 | * @return string HTML Code for Kanban thumb. |
||
| 1830 | */ |
||
| 1831 | public function getKanbanView($option = '', $arraydata = null) |
||
| 1856 | } |
||
| 1857 | } |
||
| 1858 | |||
| 1859 | |||
| 1860 | /** |
||
| 1861 | * Class to manage bank transaction lines |
||
| 1862 | */ |
||
| 1863 | class AccountLine extends CommonObjectLine |
||
| 1864 | { |
||
| 1865 | /** |
||
| 1866 | * @var string Error code (or message) |
||
| 2663 |