| Total Complexity | 44 |
| Total Lines | 243 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AssetAccountancyCodes 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 AssetAccountancyCodes, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class AssetAccountancyCodes extends CommonObject |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var array Array with all accountancy codes info by mode. |
||
| 37 | * Note : 'economic' mode is mandatory and is the primary accountancy codes |
||
| 38 | * 'depreciation_asset' and 'depreciation_expense' is mandatory and is used for write depreciation in bookkeeping |
||
| 39 | */ |
||
| 40 | public $accountancy_codes_fields = array( |
||
| 41 | 'economic' => array( |
||
| 42 | 'label' => 'AssetAccountancyCodeDepreciationEconomic', |
||
| 43 | 'table' => 'asset_accountancy_codes_economic', |
||
| 44 | 'depreciation_debit' => 'depreciation_asset', |
||
| 45 | 'depreciation_credit' => 'depreciation_expense', |
||
| 46 | 'fields' => array( |
||
| 47 | 'asset' => array('label' => 'AssetAccountancyCodeAsset'), |
||
| 48 | 'depreciation_asset' => array('label' => 'AssetAccountancyCodeDepreciationAsset'), |
||
| 49 | 'depreciation_expense' => array('label' => 'AssetAccountancyCodeDepreciationExpense'), |
||
| 50 | 'value_asset_sold' => array('label' => 'AssetAccountancyCodeValueAssetSold'), |
||
| 51 | 'receivable_on_assignment' => array('label' => 'AssetAccountancyCodeReceivableOnAssignment'), |
||
| 52 | 'proceeds_from_sales' => array('label' => 'AssetAccountancyCodeProceedsFromSales'), |
||
| 53 | 'vat_collected' => array('label' => 'AssetAccountancyCodeVatCollected'), |
||
| 54 | 'vat_deductible' => array('label' => 'AssetAccountancyCodeVatDeductible','column_break' => true), |
||
| 55 | ), |
||
| 56 | ), |
||
| 57 | 'accelerated_depreciation' => array( |
||
| 58 | 'label' => 'AssetAccountancyCodeDepreciationAcceleratedDepreciation', |
||
| 59 | 'table' => 'asset_accountancy_codes_fiscal', |
||
| 60 | 'depreciation_debit' => 'accelerated_depreciation', |
||
| 61 | 'depreciation_credit' => 'endowment_accelerated_depreciation', |
||
| 62 | 'fields' => array( |
||
| 63 | 'accelerated_depreciation' => array('label' => 'AssetAccountancyCodeAcceleratedDepreciation'), |
||
| 64 | 'endowment_accelerated_depreciation' => array('label' => 'AssetAccountancyCodeEndowmentAcceleratedDepreciation'), |
||
| 65 | 'provision_accelerated_depreciation' => array('label' => 'AssetAccountancyCodeProvisionAcceleratedDepreciation'), |
||
| 66 | ), |
||
| 67 | ), |
||
| 68 | ); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array Array with all accountancy codes by mode. |
||
| 72 | */ |
||
| 73 | public $accountancy_codes = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Constructor |
||
| 77 | * |
||
| 78 | * @param DoliDB $db Database handler |
||
| 79 | */ |
||
| 80 | public function __construct(DoliDB $db) |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Fill accountancy_codes property of object (using for data sent by forms) |
||
| 87 | * |
||
| 88 | * @return array Array of values |
||
| 89 | */ |
||
| 90 | public function setAccountancyCodesFromPost() |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Load accountancy codes of a asset or a asset model |
||
| 108 | * |
||
| 109 | * @param int $asset_id Asset ID to set |
||
| 110 | * @param int $asset_model_id Asset model ID to set |
||
| 111 | * @return int Return integer <0 if KO, >0 if OK |
||
| 112 | */ |
||
| 113 | public function fetchAccountancyCodes($asset_id = 0, $asset_model_id = 0) |
||
| 114 | { |
||
| 115 | global $langs, $hookmanager; |
||
| 116 | dol_syslog(__METHOD__ . " asset_id=$asset_id, asset_model_id=$asset_model_id"); |
||
| 117 | |||
| 118 | $error = 0; |
||
| 119 | $this->errors = array(); |
||
| 120 | $this->accountancy_codes = array(); |
||
| 121 | |||
| 122 | // Clean parameters |
||
| 123 | $asset_id = $asset_id > 0 ? $asset_id : 0; |
||
| 124 | $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0; |
||
| 125 | |||
| 126 | $hookmanager->initHooks(array('assetaccountancycodesdao')); |
||
| 127 | $parameters = array('asset_id' => $asset_id, 'asset_model_id' => $asset_model_id); |
||
| 128 | $reshook = $hookmanager->executeHooks('fetchAccountancyCodes', $parameters, $this); // Note that $action and $object may have been modified by some hooks |
||
| 129 | if (!empty($reshook)) { |
||
| 130 | return $reshook; |
||
| 131 | } |
||
| 132 | |||
| 133 | // Check parameters |
||
| 134 | if (empty($asset_id) && empty($asset_model_id)) { |
||
| 135 | $this->errors[] = $langs->trans('AssetErrorAssetOrAssetModelIDNotProvide'); |
||
| 136 | $error++; |
||
| 137 | } |
||
| 138 | if ($error) { |
||
| 139 | dol_syslog(__METHOD__ . " Error check parameters: " . $this->errorsToString(), LOG_ERR); |
||
| 140 | return -1; |
||
| 141 | } |
||
| 142 | |||
| 143 | $accountancy_codes = array(); |
||
| 144 | foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) { |
||
| 145 | $sql = "SELECT " . implode(',', array_keys($mode_info['fields'])); |
||
| 146 | $sql .= " FROM " . MAIN_DB_PREFIX . $mode_info['table']; |
||
| 147 | $sql .= " WHERE " . ($asset_id > 0 ? " fk_asset = " . (int) $asset_id : " fk_asset_model = " . (int) $asset_model_id); |
||
| 148 | |||
| 149 | $resql = $this->db->query($sql); |
||
| 150 | if ($resql) { |
||
| 151 | if ($obj = $this->db->fetch_object($resql)) { |
||
| 152 | $accountancy_codes[$mode_key] = array(); |
||
| 153 | foreach ($mode_info['fields'] as $field_key => $field_info) { |
||
| 154 | $accountancy_codes[$mode_key][$field_key] = $obj->$field_key; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } else { |
||
| 158 | $this->errors[] = $langs->trans('AssetErrorFetchAccountancyCodesForMode', $mode_key) . ': ' . $this->db->lasterror(); |
||
| 159 | $error++; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | if ($error) { |
||
| 164 | dol_syslog(__METHOD__ . " Error fetch accountancy codes: " . $this->errorsToString(), LOG_ERR); |
||
| 165 | return -1; |
||
| 166 | } else { |
||
| 167 | $this->accountancy_codes = $accountancy_codes; |
||
| 168 | return 1; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Update accountancy codes of a asset or a asset model |
||
| 174 | * |
||
| 175 | * @param User $user User making update |
||
| 176 | * @param int $asset_id Asset ID to set |
||
| 177 | * @param int $asset_model_id Asset model ID to set |
||
| 178 | * @param int $notrigger 1=disable trigger UPDATE (when called by create) |
||
| 179 | * @return int Return integer <0 if KO, >0 if OK |
||
| 180 | */ |
||
| 181 | public function updateAccountancyCodes($user, $asset_id = 0, $asset_model_id = 0, $notrigger = 0) |
||
| 276 | } |
||
| 277 | } |
||
| 279 |