Conditions | 67 |
Paths | > 20000 |
Total Lines | 311 |
Code Lines | 224 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
365 | public function getAssetData(User $user, $type = 'view', $date_start = null, $date_end = null, $in_bookkeeping = 'notyet') |
||
366 | { |
||
367 | global $conf, $langs; |
||
368 | |||
369 | if (!isModEnabled('asset')) { |
||
370 | return array(); |
||
371 | } |
||
372 | |||
373 | require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/accounting.lib.php'; |
||
374 | require_once constant('DOL_DOCUMENT_ROOT') . '/asset/class/asset.class.php'; |
||
375 | require_once constant('DOL_DOCUMENT_ROOT') . '/asset/class/assetaccountancycodes.class.php'; |
||
376 | require_once constant('DOL_DOCUMENT_ROOT') . '/asset/class/assetdepreciationoptions.class.php'; |
||
377 | |||
378 | $langs->loadLangs(array("assets")); |
||
379 | |||
380 | // Clean parameters |
||
381 | if (empty($type)) { |
||
382 | $type = 'view'; |
||
383 | } |
||
384 | if (empty($in_bookkeeping)) { |
||
385 | $in_bookkeeping = 'notyet'; |
||
386 | } |
||
387 | |||
388 | $sql = ""; |
||
389 | $sql .= "SELECT ad.fk_asset AS rowid, a.ref AS asset_ref, a.label AS asset_label, a.acquisition_value_ht AS asset_acquisition_value_ht"; |
||
390 | $sql .= ", a.disposal_date AS asset_disposal_date, a.disposal_amount_ht AS asset_disposal_amount_ht, a.disposal_subject_to_vat AS asset_disposal_subject_to_vat"; |
||
391 | $sql .= ", ad.rowid AS depreciation_id, ad.depreciation_mode, ad.ref AS depreciation_ref, ad.depreciation_date, ad.depreciation_ht, ad.accountancy_code_debit, ad.accountancy_code_credit"; |
||
392 | $sql .= " FROM " . MAIN_DB_PREFIX . "asset_depreciation as ad"; |
||
393 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "asset as a ON a.rowid = ad.fk_asset"; |
||
394 | $sql .= " WHERE a.entity IN (" . getEntity('asset', 0) . ')'; // We don't share object for accountancy, we use source object sharing |
||
395 | if ($in_bookkeeping == 'already') { |
||
396 | $sql .= " AND EXISTS (SELECT iab.fk_docdet FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping AS iab WHERE iab.fk_docdet = ad.rowid AND doc_type = 'asset')"; |
||
397 | } elseif ($in_bookkeeping == 'notyet') { |
||
398 | $sql .= " AND NOT EXISTS (SELECT iab.fk_docdet FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping AS iab WHERE iab.fk_docdet = ad.rowid AND doc_type = 'asset')"; |
||
399 | } |
||
400 | $sql .= " AND ad.ref != ''"; // not reversal lines |
||
401 | if ($date_start && $date_end) { |
||
402 | $sql .= " AND ad.depreciation_date >= '" . $this->db->idate($date_start) . "' AND ad.depreciation_date <= '" . $this->db->idate($date_end) . "'"; |
||
403 | } |
||
404 | // Define begin binding date |
||
405 | if (getDolGlobalString('ACCOUNTING_DATE_START_BINDING')) { |
||
406 | $sql .= " AND ad.depreciation_date >= '" . $this->db->idate(getDolGlobalString('ACCOUNTING_DATE_START_BINDING')) . "'"; |
||
407 | } |
||
408 | $sql .= " ORDER BY ad.depreciation_date"; |
||
409 | |||
410 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
411 | $resql = $this->db->query($sql); |
||
412 | if (!$resql) { |
||
413 | $this->errors[] = $this->db->lasterror(); |
||
414 | return -1; |
||
415 | } |
||
416 | |||
417 | $pre_data = array( |
||
418 | 'elements' => array(), |
||
419 | ); |
||
420 | while ($obj = $this->db->fetch_object($resql)) { |
||
421 | if (!isset($pre_data['elements'][$obj->rowid])) { |
||
422 | $pre_data['elements'][$obj->rowid] = array( |
||
423 | 'ref' => $obj->asset_ref, |
||
424 | 'label' => $obj->asset_label, |
||
425 | 'acquisition_value_ht' => $obj->asset_acquisition_value_ht, |
||
426 | 'depreciation' => array(), |
||
427 | ); |
||
428 | |||
429 | // Disposal infos |
||
430 | if (isset($obj->asset_disposal_date)) { |
||
431 | $pre_data['elements'][$obj->rowid]['disposal'] = array( |
||
432 | 'date' => $this->db->jdate($obj->asset_disposal_date), |
||
433 | 'amount' => $obj->asset_disposal_amount_ht, |
||
434 | 'subject_to_vat' => !empty($obj->asset_disposal_subject_to_vat), |
||
435 | ); |
||
436 | } |
||
437 | } |
||
438 | |||
439 | $compta_debit = empty($obj->accountancy_code_debit) ? 'NotDefined' : $obj->accountancy_code_debit; |
||
440 | $compta_credit = empty($obj->accountancy_code_credit) ? 'NotDefined' : $obj->accountancy_code_credit; |
||
441 | |||
442 | $pre_data['elements'][$obj->rowid]['depreciation'][$obj->depreciation_id] = array( |
||
443 | 'date' => $this->db->jdate($obj->depreciation_date), |
||
444 | 'ref' => $obj->depreciation_ref, |
||
445 | 'lines' => array( |
||
446 | $compta_debit => -$obj->depreciation_ht, |
||
447 | $compta_credit => $obj->depreciation_ht, |
||
448 | ), |
||
449 | ); |
||
450 | } |
||
451 | |||
452 | $disposal_ref = $langs->transnoentitiesnoconv('AssetDisposal'); |
||
453 | $journal = $this->code; |
||
454 | $journal_label = $this->label; |
||
455 | $journal_label_formatted = $langs->transnoentities($journal_label); |
||
456 | $now = dol_now(); |
||
457 | |||
458 | $element_static = new Asset($this->db); |
||
459 | |||
460 | $journal_data = array(); |
||
461 | foreach ($pre_data['elements'] as $pre_data_id => $pre_data_info) { |
||
462 | $element_static->id = $pre_data_id; |
||
463 | $element_static->ref = (string) $pre_data_info["ref"]; |
||
464 | $element_static->label = (string) $pre_data_info["label"]; |
||
465 | $element_static->acquisition_value_ht = $pre_data_info["acquisition_value_ht"]; |
||
466 | $element_link = $element_static->getNomUrl(1, 'with_label'); |
||
467 | |||
468 | $element_name_formatted_0 = dol_trunc($element_static->label, 16); |
||
469 | $label_operation = $element_static->getNomUrl(0, 'label', 16); |
||
470 | |||
471 | $element = array( |
||
472 | 'ref' => dol_trunc($element_static->ref, 16, 'right', 'UTF-8', 1), |
||
473 | 'error' => $pre_data_info['error'], |
||
474 | 'blocks' => array(), |
||
475 | ); |
||
476 | |||
477 | // Depreciation lines |
||
478 | //-------------------- |
||
479 | foreach ($pre_data_info['depreciation'] as $depreciation_id => $line) { |
||
480 | $depreciation_ref = $line["ref"]; |
||
481 | $depreciation_date = $line["date"]; |
||
482 | $depreciation_date_formatted = dol_print_date($depreciation_date, 'day'); |
||
483 | |||
484 | // lines |
||
485 | $blocks = array(); |
||
486 | foreach ($line['lines'] as $account => $mt) { |
||
487 | $account_infos = $this->getAccountingAccountInfos($account); |
||
488 | |||
489 | if ($type == 'view') { |
||
490 | $account_to_show = length_accounta($account); |
||
491 | if (($account_to_show == "") || $account_to_show == 'NotDefined') { |
||
492 | $account_to_show = '<span class="error">' . $langs->trans("AssetInAccountNotDefined") . '</span>'; |
||
493 | } |
||
494 | |||
495 | $blocks[] = array( |
||
496 | 'date' => $depreciation_date_formatted, |
||
497 | 'piece' => $element_link, |
||
498 | 'account_accounting' => $account_to_show, |
||
499 | 'subledger_account' => '', |
||
500 | 'label_operation' => $label_operation . ' - ' . $depreciation_ref, |
||
501 | 'debit' => $mt < 0 ? price(-$mt) : '', |
||
502 | 'credit' => $mt >= 0 ? price($mt) : '', |
||
503 | ); |
||
504 | } elseif ($type == 'bookkeeping') { |
||
505 | if ($account_infos['found']) { |
||
506 | $blocks[] = array( |
||
507 | 'doc_date' => $depreciation_date, |
||
508 | 'date_lim_reglement' => '', |
||
509 | 'doc_ref' => $element_static->ref, |
||
510 | 'date_creation' => $now, |
||
511 | 'doc_type' => 'asset', |
||
512 | 'fk_doc' => $element_static->id, |
||
513 | 'fk_docdet' => $depreciation_id, // Useless, can be several lines that are source of this record to add |
||
514 | 'thirdparty_code' => '', |
||
515 | 'subledger_account' => '', |
||
516 | 'subledger_label' => '', |
||
517 | 'numero_compte' => $account, |
||
518 | 'label_compte' => $account_infos['label'], |
||
519 | 'label_operation' => $element_name_formatted_0 . ' - ' . $depreciation_ref, |
||
520 | 'montant' => $mt, |
||
521 | 'sens' => $mt < 0 ? 'D' : 'C', |
||
522 | 'debit' => $mt < 0 ? -$mt : 0, |
||
523 | 'credit' => $mt >= 0 ? $mt : 0, |
||
524 | 'code_journal' => $journal, |
||
525 | 'journal_label' => $journal_label_formatted, |
||
526 | 'piece_num' => '', |
||
527 | 'import_key' => '', |
||
528 | 'fk_user_author' => $user->id, |
||
529 | 'entity' => $conf->entity, |
||
530 | ); |
||
531 | } |
||
532 | } else { // $type == 'csv' |
||
533 | $blocks[] = array( |
||
534 | $depreciation_date, // Date |
||
535 | $element_static->ref, // Piece |
||
536 | $account_infos['code_formatted_1'], // AccountAccounting |
||
537 | $element_name_formatted_0 . ' - ' . $depreciation_ref, // LabelOperation |
||
538 | $mt < 0 ? price(-$mt) : '', // Debit |
||
539 | $mt >= 0 ? price($mt) : '', // Credit |
||
540 | ); |
||
541 | } |
||
542 | } |
||
543 | $element['blocks'][] = $blocks; |
||
544 | } |
||
545 | |||
546 | // Disposal line |
||
547 | //-------------------- |
||
548 | if (!empty($pre_data_info['disposal'])) { |
||
549 | $disposal_date = $pre_data_info['disposal']['date']; |
||
550 | |||
551 | if ( |
||
552 | (!($date_start && $date_end) || ($date_start <= $disposal_date && $disposal_date <= $date_end)) && |
||
553 | (!getDolGlobalString('ACCOUNTING_DATE_START_BINDING') || getDolGlobalInt('ACCOUNTING_DATE_START_BINDING') <= $disposal_date) |
||
554 | ) { |
||
555 | $disposal_amount = $pre_data_info['disposal']['amount']; |
||
556 | $disposal_subject_to_vat = $pre_data_info['disposal']['subject_to_vat']; |
||
557 | $disposal_date_formatted = dol_print_date($disposal_date, 'day'); |
||
558 | $disposal_vat = getDolGlobalInt('ASSET_DISPOSAL_VAT') > 0 ? getDolGlobalInt('ASSET_DISPOSAL_VAT') : 20; |
||
559 | |||
560 | // Get accountancy codes |
||
561 | //--------------------------- |
||
562 | require_once constant('DOL_DOCUMENT_ROOT') . '/asset/class/assetaccountancycodes.class.php'; |
||
563 | $accountancy_codes = new AssetAccountancyCodes($this->db); |
||
564 | $result = $accountancy_codes->fetchAccountancyCodes($element_static->id); |
||
565 | if ($result < 0) { |
||
566 | $element['error'] = $accountancy_codes->errorsToString(); |
||
567 | } else { |
||
568 | // Get last depreciation cumulative amount |
||
569 | $element_static->fetchDepreciationLines(); |
||
570 | foreach ($element_static->depreciation_lines as $mode_key => $depreciation_lines) { |
||
571 | $accountancy_codes_list = $accountancy_codes->accountancy_codes[$mode_key]; |
||
572 | |||
573 | if (!isset($accountancy_codes_list['value_asset_sold'])) { |
||
574 | continue; |
||
575 | } |
||
576 | |||
577 | $accountancy_code_value_asset_sold = empty($accountancy_codes_list['value_asset_sold']) ? 'NotDefined' : $accountancy_codes_list['value_asset_sold']; |
||
578 | $accountancy_code_depreciation_asset = empty($accountancy_codes_list['depreciation_asset']) ? 'NotDefined' : $accountancy_codes_list['depreciation_asset']; |
||
579 | $accountancy_code_asset = empty($accountancy_codes_list['asset']) ? 'NotDefined' : $accountancy_codes_list['asset']; |
||
580 | $accountancy_code_receivable_on_assignment = empty($accountancy_codes_list['receivable_on_assignment']) ? 'NotDefined' : $accountancy_codes_list['receivable_on_assignment']; |
||
581 | $accountancy_code_vat_collected = empty($accountancy_codes_list['vat_collected']) ? 'NotDefined' : $accountancy_codes_list['vat_collected']; |
||
582 | $accountancy_code_proceeds_from_sales = empty($accountancy_codes_list['proceeds_from_sales']) ? 'NotDefined' : $accountancy_codes_list['proceeds_from_sales']; |
||
583 | |||
584 | $last_cumulative_amount_ht = 0; |
||
585 | $depreciated_ids = array_keys($pre_data_info['depreciation']); |
||
586 | foreach ($depreciation_lines as $line) { |
||
587 | $last_cumulative_amount_ht = $line['cumulative_depreciation_ht']; |
||
588 | if (!in_array($line['id'], $depreciated_ids) && empty($line['bookkeeping']) && !empty($line['ref'])) { |
||
589 | break; |
||
590 | } |
||
591 | } |
||
592 | |||
593 | $lines = array(); |
||
594 | $lines[0][$accountancy_code_value_asset_sold] = -($element_static->acquisition_value_ht - $last_cumulative_amount_ht); |
||
595 | $lines[0][$accountancy_code_depreciation_asset] = -$last_cumulative_amount_ht; |
||
596 | $lines[0][$accountancy_code_asset] = $element_static->acquisition_value_ht; |
||
597 | |||
598 | $disposal_amount_vat = $disposal_subject_to_vat ? (float) price2num($disposal_amount * $disposal_vat / 100, 'MT') : 0; |
||
599 | $lines[1][$accountancy_code_receivable_on_assignment] = -($disposal_amount + $disposal_amount_vat); |
||
600 | if ($disposal_subject_to_vat) { |
||
601 | $lines[1][$accountancy_code_vat_collected] = $disposal_amount_vat; |
||
602 | } |
||
603 | $lines[1][$accountancy_code_proceeds_from_sales] = $disposal_amount; |
||
604 | |||
605 | foreach ($lines as $lines_block) { |
||
606 | $blocks = array(); |
||
607 | foreach ($lines_block as $account => $mt) { |
||
608 | $account_infos = $this->getAccountingAccountInfos($account); |
||
609 | |||
610 | if ($type == 'view') { |
||
611 | $account_to_show = length_accounta($account); |
||
612 | if (($account_to_show == "") || $account_to_show == 'NotDefined') { |
||
613 | $account_to_show = '<span class="error">' . $langs->trans("AssetInAccountNotDefined") . '</span>'; |
||
614 | } |
||
615 | |||
616 | $blocks[] = array( |
||
617 | 'date' => $disposal_date_formatted, |
||
618 | 'piece' => $element_link, |
||
619 | 'account_accounting' => $account_to_show, |
||
620 | 'subledger_account' => '', |
||
621 | 'label_operation' => $label_operation . ' - ' . $disposal_ref, |
||
622 | 'debit' => $mt < 0 ? price(-$mt) : '', |
||
623 | 'credit' => $mt >= 0 ? price($mt) : '', |
||
624 | ); |
||
625 | } elseif ($type == 'bookkeeping') { |
||
626 | if ($account_infos['found']) { |
||
627 | $blocks[] = array( |
||
628 | 'doc_date' => $disposal_date, |
||
629 | 'date_lim_reglement' => '', |
||
630 | 'doc_ref' => $element_static->ref, |
||
631 | 'date_creation' => $now, |
||
632 | 'doc_type' => 'asset', |
||
633 | 'fk_doc' => $element_static->id, |
||
634 | 'fk_docdet' => 0, // Useless, can be several lines that are source of this record to add |
||
635 | 'thirdparty_code' => '', |
||
636 | 'subledger_account' => '', |
||
637 | 'subledger_label' => '', |
||
638 | 'numero_compte' => $account, |
||
639 | 'label_compte' => $account_infos['label'], |
||
640 | 'label_operation' => $element_name_formatted_0 . ' - ' . $disposal_ref, |
||
641 | 'montant' => $mt, |
||
642 | 'sens' => $mt < 0 ? 'D' : 'C', |
||
643 | 'debit' => $mt < 0 ? -$mt : 0, |
||
644 | 'credit' => $mt >= 0 ? $mt : 0, |
||
645 | 'code_journal' => $journal, |
||
646 | 'journal_label' => $journal_label_formatted, |
||
647 | 'piece_num' => '', |
||
648 | 'import_key' => '', |
||
649 | 'fk_user_author' => $user->id, |
||
650 | 'entity' => $conf->entity, |
||
651 | ); |
||
652 | } |
||
653 | } else { // $type == 'csv' |
||
654 | $blocks[] = array( |
||
655 | $disposal_date, // Date |
||
656 | $element_static->ref, // Piece |
||
657 | $account_infos['code_formatted_1'], // AccountAccounting |
||
658 | $element_name_formatted_0 . ' - ' . $disposal_ref, // LabelOperation |
||
659 | $mt < 0 ? price(-$mt) : '', // Debit |
||
660 | $mt >= 0 ? price($mt) : '', // Credit |
||
661 | ); |
||
662 | } |
||
663 | } |
||
664 | $element['blocks'][] = $blocks; |
||
665 | } |
||
666 | } |
||
667 | } |
||
668 | } |
||
669 | } |
||
670 | |||
671 | $journal_data[(int) $pre_data_id] = $element; |
||
672 | } |
||
673 | unset($pre_data); |
||
674 | |||
675 | return $journal_data; |
||
676 | } |
||
967 |