| Total Complexity | 71 |
| Total Lines | 516 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FormAccounting 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 FormAccounting, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class FormAccounting extends Form |
||
| 34 | { |
||
| 35 | |||
| 36 | private $options_cache = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var DoliDB Database handler. |
||
| 40 | */ |
||
| 41 | public $db; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string Error code (or message) |
||
| 45 | */ |
||
| 46 | public $error = ''; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Constructor |
||
| 50 | * |
||
| 51 | * @param DoliDB $db Database handler |
||
| 52 | */ |
||
| 53 | public function __construct($db) |
||
| 56 | } |
||
| 57 | |||
| 58 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 59 | /** |
||
| 60 | * Return list of journals with label by nature |
||
| 61 | * |
||
| 62 | * @param string $selectid Preselected journal code |
||
| 63 | * @param string $htmlname Name of field in html form |
||
| 64 | * @param int $nature Limit the list to a particular type of journals (1:various operations / 2:sale / 3:purchase / 4:bank / 9: has-new) |
||
| 65 | * @param int $showempty Add an empty field |
||
| 66 | * @param int $select_in 0=selectid value is the journal rowid (default) or 1=selectid is journal code |
||
| 67 | * @param int $select_out Set value returned by select. 0=rowid (default), 1=code |
||
| 68 | * @param string $morecss More css non HTML object |
||
| 69 | * @param string $usecache Key to use to store result into a cache. Next call with same key will reuse the cache. |
||
| 70 | * @param int $disabledajaxcombo Disable ajax combo box. |
||
| 71 | * @return string String with HTML select |
||
| 72 | */ |
||
| 73 | public function select_journal($selectid, $htmlname = 'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss = 'maxwidth300 maxwidthonsmartphone', $usecache = '', $disabledajaxcombo = 0) |
||
| 137 | } |
||
| 138 | |||
| 139 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 140 | /** |
||
| 141 | * Return list of journals with label by nature |
||
| 142 | * |
||
| 143 | * @param array $selectedIds Preselected journal code array |
||
| 144 | * @param string $htmlname Name of field in html form |
||
| 145 | * @param int $nature Limit the list to a particular type of journals (1:various operations / 2:sale / 3:purchase / 4:bank / 9: has-new) |
||
| 146 | * @param int $showempty Add an empty field |
||
| 147 | * @param int $select_in 0=selectid value is the journal rowid (default) or 1=selectid is journal code |
||
| 148 | * @param int $select_out Set value returned by select. 0=rowid (default), 1=code |
||
| 149 | * @param string $morecss More css non HTML object |
||
| 150 | * @param string $usecache Key to use to store result into a cache. Next call with same key will reuse the cache. |
||
| 151 | * @param int $disabledajaxcombo Disable ajax combo box. |
||
| 152 | * @return string String with HTML select |
||
| 153 | */ |
||
| 154 | public function multi_select_journal($selectedIds = array(), $htmlname = 'journal', $nature = 0, $showempty = 0, $select_in = 0, $select_out = 0, $morecss = '', $usecache = '', $disabledajaxcombo = 0) |
||
| 155 | { |
||
| 156 | // phpcs:enable |
||
| 157 | global $conf, $langs; |
||
| 158 | |||
| 159 | $out = ''; |
||
| 160 | |||
| 161 | $options = array(); |
||
| 162 | if ($usecache && !empty($this->options_cache[$usecache])) |
||
| 163 | { |
||
| 164 | $options = $this->options_cache[$usecache]; |
||
| 165 | $selected = $selectedIds; |
||
| 166 | } else { |
||
| 167 | $sql = "SELECT rowid, code, label, nature, entity, active"; |
||
| 168 | $sql .= " FROM ".MAIN_DB_PREFIX."accounting_journal"; |
||
| 169 | $sql .= " WHERE active = 1"; |
||
| 170 | $sql .= " AND entity = ".$conf->entity; |
||
| 171 | if ($nature && is_numeric($nature)) $sql .= " AND nature = ".$nature; |
||
| 172 | $sql .= " ORDER BY code"; |
||
| 173 | |||
| 174 | dol_syslog(get_class($this)."::multi_select_journal", LOG_DEBUG); |
||
| 175 | $resql = $this->db->query($sql); |
||
| 176 | |||
| 177 | if (!$resql) { |
||
| 178 | $this->error = "Error ".$this->db->lasterror(); |
||
| 179 | dol_syslog(get_class($this)."::multi_select_journal ".$this->error, LOG_ERR); |
||
| 180 | return -1; |
||
| 181 | } |
||
| 182 | |||
| 183 | $selected = array(); |
||
| 184 | $langs->load('accountancy'); |
||
| 185 | while ($obj = $this->db->fetch_object($resql)) |
||
| 186 | { |
||
| 187 | $label = $langs->trans($obj->label); |
||
| 188 | |||
| 189 | $select_value_in = $obj->rowid; |
||
| 190 | $select_value_out = $obj->rowid; |
||
| 191 | |||
| 192 | // Try to guess if we have found default value |
||
| 193 | if ($select_in == 1) { |
||
| 194 | $select_value_in = $obj->code; |
||
| 195 | } |
||
| 196 | if ($select_out == 1) { |
||
| 197 | $select_value_out = $obj->code; |
||
| 198 | } |
||
| 199 | // Remember guy's we store in database llx_accounting_bookkeeping the code of accounting_journal and not the rowid |
||
| 200 | if (!empty($selectedIds) && in_array($select_value_in, $selectedIds)) { |
||
| 201 | //var_dump("Found ".$selectid." ".$select_value_in); |
||
| 202 | $selected[] = $select_value_out; |
||
| 203 | } |
||
| 204 | $options[$select_value_out] = $label; |
||
| 205 | } |
||
| 206 | $this->db->free($resql); |
||
| 207 | |||
| 208 | if ($usecache) |
||
| 209 | { |
||
| 210 | $this->options_cache[$usecache] = $options; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | $out .= Form::multiselectarray($htmlname, $options, $selected, $showempty, 0, $morecss, 0, 0, 0, 'code_journal', '', ($disabledajaxcombo ? 0 : 1)); |
||
| 215 | |||
| 216 | return $out; |
||
| 217 | } |
||
| 218 | |||
| 219 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 220 | /** |
||
| 221 | * Return list of accounting category. |
||
| 222 | * Use mysoc->country_id or mysoc->country_code so they must be defined. |
||
| 223 | * |
||
| 224 | * @param string $selected Preselected type |
||
| 225 | * @param string $htmlname Name of field in form |
||
| 226 | * @param int $useempty Set to 1 if we want an empty value |
||
| 227 | * @param int $maxlen Max length of text in combo box |
||
| 228 | * @param int $help Add or not the admin help picto |
||
| 229 | * @param int $allcountries All countries |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | public function select_accounting_category($selected = '', $htmlname = 'account_category', $useempty = 0, $maxlen = 0, $help = 1, $allcountries = 0) |
||
| 233 | { |
||
| 234 | // phpcs:enable |
||
| 235 | global $db, $langs, $user, $mysoc; |
||
| 236 | |||
| 237 | if (empty($mysoc->country_id) && empty($mysoc->country_code) && empty($allcountries)) |
||
| 238 | { |
||
| 239 | dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined'); |
||
| 240 | exit; |
||
| 241 | } |
||
| 242 | |||
| 243 | if (!empty($mysoc->country_id)) |
||
| 244 | { |
||
| 245 | $sql = "SELECT c.rowid, c.label as type, c.range_account"; |
||
| 246 | $sql .= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c"; |
||
| 247 | $sql .= " WHERE c.active = 1"; |
||
| 248 | $sql .= " AND c.category_type = 0"; |
||
| 249 | if (empty($allcountries)) $sql .= " AND c.fk_country = ".$mysoc->country_id; |
||
| 250 | $sql .= " ORDER BY c.label ASC"; |
||
| 251 | } else { |
||
| 252 | $sql = "SELECT c.rowid, c.label as type, c.range_account"; |
||
| 253 | $sql .= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c, ".MAIN_DB_PREFIX."c_country as co"; |
||
| 254 | $sql .= " WHERE c.active = 1"; |
||
| 255 | $sql .= " AND c.category_type = 0"; |
||
| 256 | $sql .= " AND c.fk_country = co.rowid"; |
||
| 257 | if (empty($allcountries)) $sql .= " AND co.code = '".$this->db->escape($mysoc->country_code)."'"; |
||
| 258 | $sql .= " ORDER BY c.label ASC"; |
||
| 259 | } |
||
| 260 | |||
| 261 | dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG); |
||
| 262 | $resql = $this->db->query($sql); |
||
| 263 | if ($resql) |
||
| 264 | { |
||
| 265 | $num = $this->db->num_rows($resql); |
||
| 266 | if ($num) |
||
| 267 | { |
||
| 268 | $out = '<select class="flat minwidth200" id="'.$htmlname.'" name="'.$htmlname.'">'; |
||
| 269 | $i = 0; |
||
| 270 | |||
| 271 | if ($useempty) $out .= '<option value="0"> </option>'; |
||
| 272 | while ($i < $num) |
||
| 273 | { |
||
| 274 | $obj = $this->db->fetch_object($resql); |
||
| 275 | $out .= '<option value="'.$obj->rowid.'"'; |
||
| 276 | if ($obj->rowid == $selected) $out .= ' selected'; |
||
| 277 | $out .= '>'.($maxlen ? dol_trunc($obj->type, $maxlen) : $obj->type); |
||
| 278 | $out .= ' ('.$obj->range_account.')'; |
||
| 279 | $i++; |
||
| 280 | } |
||
| 281 | $out .= '</select>'; |
||
| 282 | //if ($user->admin && $help) $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1); |
||
| 283 | } else { |
||
| 284 | $out .= $langs->trans("ErrorNoAccountingCategoryForThisCountry", $mysoc->country_code); |
||
|
|
|||
| 285 | } |
||
| 286 | } else { |
||
| 287 | dol_print_error($this->db); |
||
| 288 | } |
||
| 289 | |||
| 290 | $out .= ajax_combobox($htmlname, array()); |
||
| 291 | |||
| 292 | print $out; |
||
| 293 | } |
||
| 294 | |||
| 295 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 296 | /** |
||
| 297 | * Return select filter with date of transaction |
||
| 298 | * |
||
| 299 | * @param string $htmlname Name of select field |
||
| 300 | * @param string $selectedkey Value |
||
| 301 | * @return string HTML edit field |
||
| 302 | */ |
||
| 303 | public function select_bookkeeping_importkey($htmlname = 'importkey', $selectedkey = '') |
||
| 326 | } |
||
| 327 | |||
| 328 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 329 | /** |
||
| 330 | * Return list of accounts with label by chart of accounts |
||
| 331 | * |
||
| 332 | * @param string $selectid Preselected id of accounting accounts (depends on $select_in) |
||
| 333 | * @param string $htmlname Name of HTML field id. If name start with '.', it is name of HTML css class, so several component with same name in different forms can be used. |
||
| 334 | * @param int $showempty 1=Add an empty field, 2=Add an empty field+'None' field |
||
| 335 | * @param array $event Event options |
||
| 336 | * @param int $select_in 0=selectid value is a aa.rowid (default) or 1=selectid is aa.account_number |
||
| 337 | * @param int $select_out Set value returned by select. 0=rowid (default), 1=account_number |
||
| 338 | * @param string $morecss More css non HTML object |
||
| 339 | * @param string $usecache Key to use to store result into a cache. Next call with same key will reuse the cache. |
||
| 340 | * @return string String with HTML select |
||
| 341 | */ |
||
| 342 | public function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss = 'maxwidth300 maxwidthonsmartphone', $usecache = '') |
||
| 343 | { |
||
| 344 | // phpcs:enable |
||
| 345 | global $conf, $langs; |
||
| 346 | |||
| 347 | require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php'; |
||
| 348 | |||
| 349 | $out = ''; |
||
| 350 | |||
| 351 | $options = array(); |
||
| 352 | |||
| 353 | if ($showempty == 2) |
||
| 354 | { |
||
| 355 | $options['0'] = '--- '.$langs->trans("None").' ---'; |
||
| 356 | } |
||
| 357 | |||
| 358 | if ($usecache && !empty($this->options_cache[$usecache])) |
||
| 359 | { |
||
| 360 | $options = $options + $this->options_cache[$usecache]; // We use + instead of array_merge because we don't want to reindex key from 0 |
||
| 361 | $selected = $selectid; |
||
| 362 | } else { |
||
| 363 | $trunclength = empty($conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT) ? 50 : $conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT; |
||
| 364 | |||
| 365 | $sql = "SELECT DISTINCT aa.account_number, aa.label, aa.labelshort, aa.rowid, aa.fk_pcg_version"; |
||
| 366 | $sql .= " FROM ".MAIN_DB_PREFIX."accounting_account as aa"; |
||
| 367 | $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version"; |
||
| 368 | $sql .= " AND asy.rowid = ".$conf->global->CHARTOFACCOUNTS; |
||
| 369 | $sql .= " AND aa.active = 1"; |
||
| 370 | $sql .= " AND aa.entity=".$conf->entity; |
||
| 371 | $sql .= " ORDER BY aa.account_number"; |
||
| 372 | |||
| 373 | dol_syslog(get_class($this)."::select_account", LOG_DEBUG); |
||
| 374 | $resql = $this->db->query($sql); |
||
| 375 | |||
| 376 | if (!$resql) { |
||
| 377 | $this->error = "Error ".$this->db->lasterror(); |
||
| 378 | dol_syslog(get_class($this)."::select_account ".$this->error, LOG_ERR); |
||
| 379 | return -1; |
||
| 380 | } |
||
| 381 | |||
| 382 | $selected = $selectid; // selectid can be -1, 0, 123 |
||
| 383 | while ($obj = $this->db->fetch_object($resql)) |
||
| 384 | { |
||
| 385 | if (empty($obj->labelshort)) |
||
| 386 | { |
||
| 387 | $labeltoshow = $obj->label; |
||
| 388 | } else { |
||
| 389 | $labeltoshow = $obj->labelshort; |
||
| 390 | } |
||
| 391 | |||
| 392 | $label = length_accountg($obj->account_number).' - '.$labeltoshow; |
||
| 393 | $label = dol_trunc($label, $trunclength); |
||
| 394 | |||
| 395 | $select_value_in = $obj->rowid; |
||
| 396 | $select_value_out = $obj->rowid; |
||
| 397 | |||
| 398 | // Try to guess if we have found default value |
||
| 399 | if ($select_in == 1) { |
||
| 400 | $select_value_in = $obj->account_number; |
||
| 401 | } |
||
| 402 | if ($select_out == 1) { |
||
| 403 | $select_value_out = $obj->account_number; |
||
| 404 | } |
||
| 405 | // Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number |
||
| 406 | // Because same account_number can be share between different accounting_system and do have the same meaning |
||
| 407 | if ($selectid != '' && $selectid == $select_value_in) { |
||
| 408 | //var_dump("Found ".$selectid." ".$select_value_in); |
||
| 409 | $selected = $select_value_out; |
||
| 410 | } |
||
| 411 | |||
| 412 | $options[$select_value_out] = $label; |
||
| 413 | } |
||
| 414 | $this->db->free($resql); |
||
| 415 | |||
| 416 | if ($usecache) |
||
| 417 | { |
||
| 418 | $this->options_cache[$usecache] = $options; |
||
| 419 | unset($this->options_cache[$usecache]['0']); |
||
| 420 | } |
||
| 421 | } |
||
| 422 | |||
| 423 | $out .= Form::selectarray($htmlname, $options, $selected, ($showempty > 0 ? 1 : 0), 0, 0, '', 0, 0, 0, '', $morecss, 1); |
||
| 424 | |||
| 425 | return $out; |
||
| 426 | } |
||
| 427 | |||
| 428 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 429 | /** |
||
| 430 | * Return list of auxilary thirdparty accounts |
||
| 431 | * |
||
| 432 | * @param string $selectid Preselected pcg_type |
||
| 433 | * @param string $htmlname Name of field in html form |
||
| 434 | * @param int $showempty Add an empty field |
||
| 435 | * @param string $morecss More css |
||
| 436 | * @return string String with HTML select |
||
| 437 | */ |
||
| 438 | public function select_auxaccount($selectid, $htmlname = 'account_num_aux', $showempty = 0, $morecss = 'maxwidth200') |
||
| 439 | { |
||
| 440 | // phpcs:enable |
||
| 441 | |||
| 442 | $aux_account = array(); |
||
| 443 | |||
| 444 | // Auxiliary customer account |
||
| 445 | $sql = "SELECT DISTINCT code_compta, nom "; |
||
| 446 | $sql .= " FROM ".MAIN_DB_PREFIX."societe"; |
||
| 447 | $sql .= " WHERE entity IN (".getEntity('societe').")"; |
||
| 448 | $sql .= " ORDER BY code_compta"; |
||
| 449 | |||
| 450 | dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG); |
||
| 451 | $resql = $this->db->query($sql); |
||
| 452 | if ($resql) { |
||
| 453 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 454 | if (!empty($obj->code_compta)) { |
||
| 455 | $aux_account[$obj->code_compta] = $obj->code_compta.' ('.$obj->nom.')'; |
||
| 456 | } |
||
| 457 | } |
||
| 458 | } else { |
||
| 459 | $this->error = "Error ".$this->db->lasterror(); |
||
| 460 | dol_syslog(get_class($this)."::select_auxaccount ".$this->error, LOG_ERR); |
||
| 461 | return -1; |
||
| 462 | } |
||
| 463 | $this->db->free($resql); |
||
| 464 | |||
| 465 | // Auxiliary supplier account |
||
| 466 | $sql = "SELECT DISTINCT code_compta_fournisseur, nom "; |
||
| 467 | $sql .= " FROM ".MAIN_DB_PREFIX."societe"; |
||
| 468 | $sql .= " WHERE entity IN (".getEntity('societe').")"; |
||
| 469 | $sql .= " ORDER BY code_compta_fournisseur"; |
||
| 470 | dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG); |
||
| 471 | $resql = $this->db->query($sql); |
||
| 472 | if ($resql) { |
||
| 473 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 474 | if ($obj->code_compta_fournisseur != "") { |
||
| 475 | $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.' ('.$obj->nom.')'; |
||
| 476 | } |
||
| 477 | } |
||
| 478 | } else { |
||
| 479 | $this->error = "Error ".$this->db->lasterror(); |
||
| 480 | dol_syslog(get_class($this)."::select_auxaccount ".$this->error, LOG_ERR); |
||
| 481 | return -1; |
||
| 482 | } |
||
| 483 | $this->db->free($resql); |
||
| 484 | |||
| 485 | // Auxiliary user account |
||
| 486 | $sql = "SELECT DISTINCT accountancy_code, lastname, firstname "; |
||
| 487 | $sql .= " FROM ".MAIN_DB_PREFIX."user"; |
||
| 488 | $sql .= " WHERE entity IN (".getEntity('user').")"; |
||
| 489 | $sql .= " ORDER BY accountancy_code"; |
||
| 490 | dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG); |
||
| 491 | $resql = $this->db->query($sql); |
||
| 492 | if ($resql) { |
||
| 493 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 494 | if (!empty($obj->accountancy_code)) { |
||
| 495 | $aux_account[$obj->accountancy_code] = $obj->accountancy_code.' ('.dolGetFirstLastname($obj->firstname, $obj->lastname).')'; |
||
| 496 | } |
||
| 497 | } |
||
| 498 | } else { |
||
| 499 | $this->error = "Error ".$this->db->lasterror(); |
||
| 500 | dol_syslog(get_class($this)."::select_auxaccount ".$this->error, LOG_ERR); |
||
| 501 | return -1; |
||
| 502 | } |
||
| 503 | $this->db->free($resql); |
||
| 504 | |||
| 505 | // Build select |
||
| 506 | $out .= Form::selectarray($htmlname, $aux_account, $selectid, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, 1); |
||
| 507 | |||
| 508 | return $out; |
||
| 509 | } |
||
| 510 | |||
| 511 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 512 | /** |
||
| 513 | * Return HTML combo list of years existing into book keepping |
||
| 514 | * |
||
| 515 | * @param string $selected Preselected value |
||
| 516 | * @param string $htmlname Name of HTML select object |
||
| 517 | * @param int $useempty Affiche valeur vide dans liste |
||
| 518 | * @param string $output_format (html/opton (for option html only)/array (to return options arrays |
||
| 519 | * @return string|array HTML select component or array of select options |
||
| 520 | */ |
||
| 521 | public function selectyear_accountancy_bookkepping($selected = '', $htmlname = 'yearid', $useempty = 0, $output_format = 'html') |
||
| 549 | } |
||
| 550 | } |
||
| 551 | } |
||
| 552 |