Total Complexity | 43 |
Total Lines | 298 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Cliente 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 Cliente, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Cliente extends Base\ComercialContact |
||
38 | { |
||
39 | use Base\ModelTrait; |
||
|
|||
40 | |||
41 | const SPECIAL_ACCOUNT = 'CLIENT'; |
||
42 | |||
43 | /** @var string */ |
||
44 | public $codagente; |
||
45 | |||
46 | /** @var string */ |
||
47 | public $codgrupo; |
||
48 | |||
49 | /** @var string */ |
||
50 | public $codtarifa; |
||
51 | |||
52 | /** @var string */ |
||
53 | public $diaspago; |
||
54 | |||
55 | /** @var integer */ |
||
56 | public $idcontactoenv; |
||
57 | |||
58 | /** @var integer */ |
||
59 | public $idcontactofact; |
||
60 | |||
61 | /** @var float */ |
||
62 | public $riesgoalcanzado; |
||
63 | |||
64 | /** @var float */ |
||
65 | public $riesgomax; |
||
66 | |||
67 | public function checkVies(): bool |
||
68 | { |
||
69 | $codiso = Paises::get($this->getDefaultAddress()->codpais)->codiso ?? ''; |
||
70 | return Vies::check($this->cifnif ?? '', $codiso) === 1; |
||
71 | } |
||
72 | |||
73 | public function clear() |
||
74 | { |
||
75 | parent::clear(); |
||
76 | $this->codretencion = Tools::settings('default', 'codretencion'); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param string $query |
||
81 | * @param string $fieldCode |
||
82 | * @param DataBaseWhere[] $where |
||
83 | * |
||
84 | * @return CodeModel[] |
||
85 | */ |
||
86 | public function codeModelSearch(string $query, string $fieldCode = '', array $where = []): array |
||
87 | { |
||
88 | $field = empty($fieldCode) ? $this->primaryColumn() : $fieldCode; |
||
89 | $fields = 'cifnif|codcliente|email|nombre|observaciones|razonsocial|telefono1|telefono2'; |
||
90 | $where[] = new DataBaseWhere($fields, mb_strtolower($query, 'UTF8'), 'LIKE'); |
||
91 | $where[] = new DataBaseWhere('fechabaja', null, 'IS'); |
||
92 | return CodeModel::all($this->tableName(), $field, $this->primaryDescriptionColumn(), false, $where); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Returns an array with the addresses associated with this customer. |
||
97 | * |
||
98 | * @return DinContacto[] |
||
99 | */ |
||
100 | public function getAddresses(): array |
||
101 | { |
||
102 | $contactModel = new DinContacto(); |
||
103 | $where = [new DataBaseWhere($this->primaryColumn(), $this->primaryColumnValue())]; |
||
104 | return $contactModel->all($where, [], 0, 0); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Returns the bank accounts associated with this customer. |
||
109 | * |
||
110 | * @return DinCuentaBancoCliente[] |
||
111 | */ |
||
112 | public function getBankAccounts(): array |
||
113 | { |
||
114 | $contactAccounts = new DinCuentaBancoCliente(); |
||
115 | $where = [new DataBaseWhere($this->primaryColumn(), $this->primaryColumnValue())]; |
||
116 | return $contactAccounts->all($where, [], 0, 0); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Return the default billing or shipping address. |
||
121 | * |
||
122 | * @return DinContacto |
||
123 | */ |
||
124 | public function getDefaultAddress($type = 'billing'): Contacto |
||
125 | { |
||
126 | $contact = new DinContacto(); |
||
127 | $idcontacto = $type === 'shipping' ? $this->idcontactoenv : $this->idcontactofact; |
||
128 | $contact->loadFromCode($idcontacto); |
||
129 | return $contact; |
||
130 | } |
||
131 | |||
132 | public function getGroup(): GrupoClientes |
||
133 | { |
||
134 | $group = new DinGrupoClientes(); |
||
135 | $group->loadFromCode($this->codgrupo); |
||
136 | return $group; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Returns the preferred payment days for this customer. |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | public function getPaymentDays(): array |
||
145 | { |
||
146 | $days = []; |
||
147 | foreach (explode(',', $this->diaspago . ',') as $str) { |
||
148 | if (is_numeric(trim($str))) { |
||
149 | $days[] = trim($str); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | return $days; |
||
154 | } |
||
155 | |||
156 | public function getSubcuenta(string $codejercicio, bool $crear): Subcuenta |
||
157 | { |
||
158 | // ya tiene una subcuenta asignada |
||
159 | if ($this->codsubcuenta) { |
||
160 | // buscamos la subcuenta para el ejercicio |
||
161 | $subAccount = new DinSubcuenta(); |
||
162 | $where = [ |
||
163 | new DataBaseWhere('codsubcuenta', $this->codsubcuenta), |
||
164 | new DataBaseWhere('codejercicio', $codejercicio), |
||
165 | ]; |
||
166 | if ($subAccount->loadFromCode('', $where)) { |
||
167 | return $subAccount; |
||
168 | } |
||
169 | |||
170 | // no hemos encontrado la subcuenta |
||
171 | // si no queremos crearla, devolvemos una vacía |
||
172 | if (false === $crear) { |
||
173 | return new DinSubcuenta(); |
||
174 | } |
||
175 | |||
176 | // buscamos la cuenta especial |
||
177 | $special = new DinCuentaEspecial(); |
||
178 | if (false === $special->loadFromCode(static::SPECIAL_ACCOUNT)) { |
||
179 | return new DinSubcuenta(); |
||
180 | } |
||
181 | |||
182 | // ahora creamos la subcuenta |
||
183 | return $special->getCuenta($codejercicio)->createSubcuenta($this->codsubcuenta, $this->razonsocial); |
||
184 | } |
||
185 | |||
186 | // ¿El grupo tiene subcuenta? |
||
187 | $group = $this->getGroup(); |
||
188 | if ($group->codsubcuenta) { |
||
189 | return $group->getSubcuenta($codejercicio, $crear); |
||
190 | } |
||
191 | |||
192 | // si no creamos la subcuenta, devolvemos una vacía |
||
193 | return $crear ? |
||
194 | $this->createSubcuenta($codejercicio) : |
||
195 | new DinSubcuenta(); |
||
196 | } |
||
197 | |||
198 | public function install(): string |
||
199 | { |
||
200 | // we need exits Contacto before, but we can't check it because it would create a cyclic check |
||
201 | // we need to check Agente and GrupoClientes models before |
||
202 | new Agente(); |
||
203 | new GrupoClientes(); |
||
204 | |||
205 | return parent::install(); |
||
206 | } |
||
207 | |||
208 | public static function primaryColumn(): string |
||
211 | } |
||
212 | |||
213 | public function primaryDescriptionColumn(): string |
||
216 | } |
||
217 | |||
218 | public static function tableName(): string |
||
219 | { |
||
220 | return 'clientes'; |
||
221 | } |
||
222 | |||
223 | public function test(): bool |
||
250 | } |
||
251 | |||
252 | protected function createSubcuenta(string $codejercicio): Subcuenta |
||
253 | { |
||
254 | // buscamos la cuenta especial |
||
255 | $special = new DinCuentaEspecial(); |
||
256 | if (false === $special->loadFromCode(static::SPECIAL_ACCOUNT)) { |
||
257 | return new DinSubcuenta(); |
||
258 | } |
||
283 | } |
||
284 | |||
285 | protected function saveInsert(array $values = []): bool |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Devuelve las settings de la empresa |
||
321 | * |
||
322 | * @param string $idCompany |
||
323 | * @return array |
||
324 | */ |
||
325 | public function getSettings(string $idCompany) |
||
337 |