| Total Complexity | 42 |
| Total Lines | 309 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like mod_codeclient_elephant 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 mod_codeclient_elephant, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class mod_codeclient_elephant extends ModeleThirdPartyCode |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var string model name |
||
| 40 | */ |
||
| 41 | public $name = 'Elephant'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int Code modifiable |
||
| 45 | */ |
||
| 46 | public $code_modifiable; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int Code modifiable si il est invalide |
||
| 50 | */ |
||
| 51 | public $code_modifiable_invalide; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var int Code modifiables si il est null |
||
| 55 | */ |
||
| 56 | public $code_modifiable_null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var int Code facultatif |
||
| 60 | */ |
||
| 61 | public $code_null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Dolibarr version of the loaded document |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr' |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var int Automatic numbering |
||
| 71 | */ |
||
| 72 | public $code_auto; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string search string |
||
| 76 | */ |
||
| 77 | public $searchcode; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var int Nombre de chiffres du compteur |
||
| 81 | */ |
||
| 82 | public $numbitcounter; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var int thirdparty prefix is required when using {pre} |
||
| 86 | */ |
||
| 87 | public $prefixIsRequired; |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * Constructor |
||
| 92 | */ |
||
| 93 | public function __construct() |
||
| 94 | { |
||
| 95 | $this->code_null = 0; |
||
| 96 | $this->code_modifiable = 1; |
||
| 97 | $this->code_modifiable_invalide = 1; |
||
| 98 | $this->code_modifiable_null = 1; |
||
| 99 | $this->code_auto = 1; |
||
| 100 | $this->prefixIsRequired = 0; |
||
| 101 | } |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * Return description of module |
||
| 106 | * |
||
| 107 | * @param Translate $langs Object langs |
||
| 108 | * @return string Description of module |
||
| 109 | */ |
||
| 110 | public function info($langs) |
||
| 111 | { |
||
| 112 | global $conf, $mc; |
||
| 113 | global $form; |
||
| 114 | |||
| 115 | $langs->load("companies"); |
||
| 116 | |||
| 117 | $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : ''); |
||
| 118 | |||
| 119 | $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n"; |
||
| 120 | $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">'; |
||
| 121 | $texte .= '<input type="hidden" name="token" value="'.newToken().'">'; |
||
| 122 | $texte .= '<input type="hidden" name="action" value="setModuleOptions">'; |
||
| 123 | $texte .= '<input type="hidden" name="param1" value="COMPANY_ELEPHANT_MASK_CUSTOMER">'; |
||
| 124 | $texte .= '<input type="hidden" name="param2" value="COMPANY_ELEPHANT_MASK_SUPPLIER">'; |
||
| 125 | $texte .= '<table class="nobordernopadding" width="100%">'; |
||
| 126 | |||
| 127 | $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("ThirdParty"), $langs->transnoentities("ThirdParty")); |
||
| 128 | //$tooltip.=$langs->trans("GenericMaskCodes2"); Not required for third party numbering |
||
| 129 | $tooltip .= $langs->trans("GenericMaskCodes3"); |
||
| 130 | $tooltip .= $langs->trans("GenericMaskCodes4b"); |
||
| 131 | $tooltip .= $langs->trans("GenericMaskCodes5"); |
||
| 132 | |||
| 133 | // Parametrage du prefix customers |
||
| 134 | $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("CustomerCodeModel").'):</td>'; |
||
| 135 | $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat" size="24" name="value1" value="'.$conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER.'"'.$disabled.'>', $tooltip, 1, 1).'</td>'; |
||
| 136 | |||
| 137 | $texte .= '<td class="left" rowspan="2"> <input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button"'.$disabled.'></td>'; |
||
| 138 | |||
| 139 | $texte .= '</tr>'; |
||
| 140 | |||
| 141 | // Parametrage du prefix suppliers |
||
| 142 | $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("SupplierCodeModel").'):</td>'; |
||
| 143 | $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat" size="24" name="value2" value="'.$conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER.'"'.$disabled.'>', $tooltip, 1, 1).'</td>'; |
||
| 144 | $texte .= '</tr>'; |
||
| 145 | |||
| 146 | $texte .= '</table>'; |
||
| 147 | $texte .= '</form>'; |
||
| 148 | |||
| 149 | return $texte; |
||
| 150 | } |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * Return an example of result returned by getNextValue |
||
| 155 | * |
||
| 156 | * @param Translate $langs Object langs |
||
| 157 | * @param societe $objsoc Object thirdparty |
||
| 158 | * @param int $type Type of third party (1:customer, 2:supplier, -1:autodetect) |
||
| 159 | * @return string Return string example |
||
| 160 | */ |
||
| 161 | public function getExample($langs, $objsoc = 0, $type = -1) |
||
| 162 | { |
||
| 163 | $examplecust = ''; |
||
| 164 | $examplesup = ''; |
||
| 165 | $errmsg = array( |
||
| 166 | "ErrorBadMask", |
||
| 167 | "ErrorCantUseRazIfNoYearInMask", |
||
| 168 | "ErrorCantUseRazInStartedYearIfNoYearMonthInMask", |
||
| 169 | ); |
||
| 170 | if ($type != 1) { |
||
| 171 | $examplecust = $this->getNextValue($objsoc, 0); |
||
| 172 | if (!$examplecust) { |
||
| 173 | $examplecust = $langs->trans('NotConfigured'); |
||
| 174 | } |
||
| 175 | if (in_array($examplecust, $errmsg)) { |
||
| 176 | $langs->load("errors"); |
||
| 177 | $examplecust = $langs->trans($examplecust); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | if ($type != 0) { |
||
| 181 | $examplesup = $this->getNextValue($objsoc, 1); |
||
| 182 | if (!$examplesup) { |
||
| 183 | $examplesup = $langs->trans('NotConfigured'); |
||
| 184 | } |
||
| 185 | if (in_array($examplesup, $errmsg)) { |
||
| 186 | $langs->load("errors"); |
||
| 187 | $examplesup = $langs->trans($examplesup); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | if ($type == 0) { |
||
| 192 | return $examplecust; |
||
| 193 | } elseif ($type == 1) { |
||
| 194 | return $examplesup; |
||
| 195 | } else { |
||
| 196 | return $examplecust.'<br>'.$examplesup; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Return next value |
||
| 202 | * |
||
| 203 | * @param Societe $objsoc Object third party |
||
| 204 | * @param int $type Client ou fournisseur (0:customer, 1:supplier) |
||
| 205 | * @return string Value if OK, '' if module not configured, <0 if KO |
||
| 206 | */ |
||
| 207 | public function getNextValue($objsoc = 0, $type = -1) |
||
| 208 | { |
||
| 209 | global $db, $conf; |
||
| 210 | |||
| 211 | require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; |
||
| 212 | |||
| 213 | // Get Mask value |
||
| 214 | $mask = ''; |
||
| 215 | if ($type == 0) $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER) ? '' : $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER; |
||
| 216 | if ($type == 1) $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER) ? '' : $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER; |
||
| 217 | if (!$mask) |
||
| 218 | { |
||
| 219 | $this->error = 'NotConfigured'; |
||
| 220 | return ''; |
||
| 221 | } |
||
| 222 | |||
| 223 | $field = ''; $where = ''; |
||
| 224 | if ($type == 0) |
||
| 225 | { |
||
| 226 | $field = 'code_client'; |
||
| 227 | //$where = ' AND client in (1,2)'; |
||
| 228 | } elseif ($type == 1) |
||
| 229 | { |
||
| 230 | $field = 'code_fournisseur'; |
||
| 231 | //$where = ' AND fournisseur = 1'; |
||
| 232 | } else return -1; |
||
| 233 | |||
| 234 | $now = dol_now(); |
||
| 235 | |||
| 236 | $numFinal = get_next_value($db, $mask, 'societe', $field, $where, '', $now); |
||
| 237 | |||
| 238 | return $numFinal; |
||
| 239 | } |
||
| 240 | |||
| 241 | |||
| 242 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 243 | /** |
||
| 244 | * Check if mask/numbering use prefix |
||
| 245 | * |
||
| 246 | * @return int 0 or 1 |
||
| 247 | */ |
||
| 248 | public function verif_prefixIsUsed() |
||
| 249 | { |
||
| 250 | // phpcs:enable |
||
| 251 | global $conf; |
||
| 252 | |||
| 253 | $mask = $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER; |
||
| 254 | if (preg_match('/\{pre\}/i', $mask)) return 1; |
||
| 255 | |||
| 256 | $mask = $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER; |
||
| 257 | if (preg_match('/\{pre\}/i', $mask)) return 1; |
||
| 258 | |||
| 259 | return 0; |
||
| 260 | } |
||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * Check validity of code according to its rules |
||
| 265 | * |
||
| 266 | * @param DoliDB $db Database handler |
||
| 267 | * @param string $code Code to check/correct |
||
| 268 | * @param Societe $soc Object third party |
||
| 269 | * @param int $type 0 = customer/prospect , 1 = supplier |
||
| 270 | * @return int 0 if OK |
||
| 271 | * -1 ErrorBadCustomerCodeSyntax |
||
| 272 | * -2 ErrorCustomerCodeRequired |
||
| 273 | * -3 ErrorCustomerCodeAlreadyUsed |
||
| 274 | * -4 ErrorPrefixRequired |
||
| 275 | * -5 NotConfigured - Setup empty so any value may be ok or not |
||
| 276 | * -6 Other (see this->error) |
||
| 277 | */ |
||
| 278 | public function verif($db, &$code, $soc, $type) |
||
| 279 | { |
||
| 280 | global $conf; |
||
| 281 | |||
| 282 | require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; |
||
| 283 | |||
| 284 | $result = 0; |
||
| 285 | $code = strtoupper(trim($code)); |
||
| 286 | |||
| 287 | if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)) |
||
| 288 | { |
||
| 289 | $result = 0; |
||
| 290 | } elseif (empty($code) && (!$this->code_null || !empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))) |
||
| 291 | { |
||
| 292 | $result = -2; |
||
| 293 | } else { |
||
| 294 | // Get Mask value |
||
| 295 | $mask = ''; |
||
| 296 | if ($type == 0) $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER) ? '' : $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER; |
||
| 297 | if ($type == 1) $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER) ? '' : $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER; |
||
| 298 | if (!$mask) |
||
| 299 | { |
||
| 300 | $this->error = 'NotConfigured'; |
||
| 301 | return -5; |
||
| 302 | } |
||
| 303 | $result = check_value($mask, $code); |
||
| 304 | if (is_string($result)) |
||
| 305 | { |
||
| 306 | $this->error = $result; |
||
| 307 | return -6; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result); |
||
| 312 | return $result; |
||
| 313 | } |
||
| 314 | |||
| 315 | |||
| 316 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 317 | /** |
||
| 318 | * Renvoi si un code est pris ou non (par autre tiers) |
||
| 319 | * |
||
| 320 | * @param DoliDB $db Handler acces base |
||
| 321 | * @param string $code Code a verifier |
||
| 322 | * @param Societe $soc Objet societe |
||
| 323 | * @param int $type 0 = customer/prospect , 1 = supplier |
||
| 324 | * @return int 0 if available, <0 if KO |
||
| 325 | */ |
||
| 326 | public function verif_dispo($db, $code, $soc, $type = 0) |
||
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 |