Total Complexity | 82 |
Total Lines | 545 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Subscription 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 Subscription, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class Subscription extends CommonObject |
||
40 | { |
||
41 | /** |
||
42 | * @var string ID to identify managed object |
||
43 | */ |
||
44 | public $element = 'subscription'; |
||
45 | |||
46 | /** |
||
47 | * @var string Name of table without prefix where object is stored |
||
48 | */ |
||
49 | public $table_element = 'subscription'; |
||
50 | |||
51 | /** |
||
52 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
53 | */ |
||
54 | public $picto = 'payment'; |
||
55 | |||
56 | /** |
||
57 | * Date creation record (datec) |
||
58 | * |
||
59 | * @var integer |
||
60 | */ |
||
61 | public $datec; |
||
62 | |||
63 | /** |
||
64 | * Date modification record (tms) |
||
65 | * |
||
66 | * @var integer |
||
67 | */ |
||
68 | public $datem; |
||
69 | |||
70 | /** |
||
71 | * Subscription start date (date subscription) |
||
72 | * |
||
73 | * @var integer |
||
74 | */ |
||
75 | public $dateh; |
||
76 | |||
77 | /** |
||
78 | * Subscription end date |
||
79 | * |
||
80 | * @var integer |
||
81 | */ |
||
82 | public $datef; |
||
83 | |||
84 | /** |
||
85 | * @var int ID |
||
86 | */ |
||
87 | public $fk_type; |
||
88 | |||
89 | /** |
||
90 | * @var int Member ID |
||
91 | */ |
||
92 | public $fk_adherent; |
||
93 | |||
94 | /** |
||
95 | * @var double amount subscription |
||
96 | */ |
||
97 | public $amount; |
||
98 | |||
99 | /** |
||
100 | * @var int ID of bank in llx_bank |
||
101 | */ |
||
102 | public $fk_bank; |
||
103 | |||
104 | /** |
||
105 | * @var array<string,array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array<int,string>,comment?:string}> Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. |
||
|
|||
106 | */ |
||
107 | public $fields = array( |
||
108 | 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10), |
||
109 | 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 15), |
||
110 | 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 20), |
||
111 | 'fk_adherent' => array('type' => 'integer', 'label' => 'Member', 'enabled' => 1, 'visible' => -1, 'position' => 25), |
||
112 | 'dateadh' => array('type' => 'datetime', 'label' => 'DateSubscription', 'enabled' => 1, 'visible' => -1, 'position' => 30), |
||
113 | 'datef' => array('type' => 'datetime', 'label' => 'DateEndSubscription', 'enabled' => 1, 'visible' => -1, 'position' => 35), |
||
114 | 'subscription' => array('type' => 'double(24,8)', 'label' => 'Amount', 'enabled' => 1, 'visible' => -1, 'position' => 40, 'isameasure' => 1), |
||
115 | 'fk_bank' => array('type' => 'integer', 'label' => 'BankId', 'enabled' => 1, 'visible' => -1, 'position' => 45), |
||
116 | 'note' => array('type' => 'html', 'label' => 'Note', 'enabled' => 1, 'visible' => -1, 'position' => 50), |
||
117 | 'fk_type' => array('type' => 'integer', 'label' => 'MemberType', 'enabled' => 1, 'visible' => -1, 'position' => 55), |
||
118 | 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -2, 'position' => 60), |
||
119 | 'fk_user_valid' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserValidation', 'enabled' => 1, 'visible' => -1, 'position' => 65), |
||
120 | ); |
||
121 | |||
122 | |||
123 | /** |
||
124 | * Constructor |
||
125 | * |
||
126 | * @param DoliDB $db Database handler |
||
127 | */ |
||
128 | public function __construct($db) |
||
129 | { |
||
130 | $this->db = $db; |
||
131 | |||
132 | $this->ismultientitymanaged = 'fk_adherent@adherent'; |
||
133 | } |
||
134 | |||
135 | |||
136 | /** |
||
137 | * Function who permitted creation of the subscription |
||
138 | * |
||
139 | * @param User $user User that create |
||
140 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
141 | * @return int Return integer <0 if KO, Id subscription created if OK |
||
142 | */ |
||
143 | public function create($user, $notrigger = 0) |
||
144 | { |
||
145 | global $langs; |
||
146 | |||
147 | $error = 0; |
||
148 | |||
149 | $now = dol_now(); |
||
150 | |||
151 | // Check parameters |
||
152 | if ($this->datef <= $this->dateh) { |
||
153 | $this->error = $langs->trans("ErrorBadValueForDate"); |
||
154 | return -1; |
||
155 | } |
||
156 | if (empty($this->datec)) { |
||
157 | $this->datec = $now; |
||
158 | } |
||
159 | |||
160 | $this->db->begin(); |
||
161 | |||
162 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "subscription (fk_adherent, fk_type, datec, dateadh, datef, subscription, note)"; |
||
163 | |||
164 | require_once constant('DOL_DOCUMENT_ROOT') . '/adherents/class/adherent.class.php'; |
||
165 | $member = new Adherent($this->db); |
||
166 | $result = $member->fetch($this->fk_adherent); |
||
167 | |||
168 | if ($this->fk_type == null) { // If type not defined, we use the type of member |
||
169 | $type = $member->typeid; |
||
170 | } else { |
||
171 | $type = $this->fk_type; |
||
172 | } |
||
173 | $sql .= " VALUES (" . ((int) $this->fk_adherent) . ", '" . $this->db->escape($type) . "', '" . $this->db->idate($now) . "',"; |
||
174 | $sql .= " '" . $this->db->idate($this->dateh) . "',"; |
||
175 | $sql .= " '" . $this->db->idate($this->datef) . "',"; |
||
176 | $sql .= " " . ((float) $this->amount) . ","; |
||
177 | $sql .= " '" . $this->db->escape($this->note_public ? $this->note_public : $this->note) . "')"; |
||
178 | |||
179 | $resql = $this->db->query($sql); |
||
180 | if (!$resql) { |
||
181 | $error++; |
||
182 | $this->errors[] = $this->db->lasterror(); |
||
183 | } |
||
184 | |||
185 | if (!$error) { |
||
186 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element); |
||
187 | $this->fk_type = $type; |
||
188 | } |
||
189 | |||
190 | if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects |
||
191 | $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds |
||
192 | } |
||
193 | |||
194 | // Add object linked |
||
195 | if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) { |
||
196 | foreach ($this->linked_objects as $origin => $tmp_origin_id) { |
||
197 | if (is_array($tmp_origin_id)) { // New behaviour, if linked_object can have several links per type, so is something like array('contract'=>array(id1, id2, ...)) |
||
198 | foreach ($tmp_origin_id as $origin_id) { |
||
199 | $ret = $this->add_object_linked($origin, $origin_id); |
||
200 | if (!$ret) { |
||
201 | $this->error = $this->db->lasterror(); |
||
202 | $error++; |
||
203 | } |
||
204 | } |
||
205 | } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1)) |
||
206 | $origin_id = $tmp_origin_id; |
||
207 | $ret = $this->add_object_linked($origin, $origin_id); |
||
208 | if (!$ret) { |
||
209 | $this->error = $this->db->lasterror(); |
||
210 | $error++; |
||
211 | } |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | |||
216 | if (!$error && !$notrigger) { |
||
217 | $this->context = array('member' => $member); |
||
218 | // Call triggers |
||
219 | $result = $this->call_trigger('MEMBER_SUBSCRIPTION_CREATE', $user); |
||
220 | if ($result < 0) { |
||
221 | $error++; |
||
222 | } |
||
223 | // End call triggers |
||
224 | } |
||
225 | |||
226 | // Commit or rollback |
||
227 | if ($error) { |
||
228 | $this->db->rollback(); |
||
229 | return -1; |
||
230 | } else { |
||
231 | $this->db->commit(); |
||
232 | return $this->id; |
||
233 | } |
||
234 | } |
||
235 | |||
236 | |||
237 | /** |
||
238 | * Method to load a subscription |
||
239 | * |
||
240 | * @param int $rowid Id subscription |
||
241 | * @return int Return integer <0 if KO, =0 if not found, >0 if OK |
||
242 | */ |
||
243 | public function fetch($rowid) |
||
244 | { |
||
245 | $sql = "SELECT rowid, fk_type, fk_adherent, datec,"; |
||
246 | $sql .= " tms,"; |
||
247 | $sql .= " dateadh as dateh,"; |
||
248 | $sql .= " datef,"; |
||
249 | $sql .= " subscription, note as note_public, fk_bank"; |
||
250 | $sql .= " FROM " . MAIN_DB_PREFIX . "subscription"; |
||
251 | $sql .= " WHERE rowid = " . ((int) $rowid); |
||
252 | |||
253 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
||
254 | $resql = $this->db->query($sql); |
||
255 | if ($resql) { |
||
256 | if ($this->db->num_rows($resql)) { |
||
257 | $obj = $this->db->fetch_object($resql); |
||
258 | |||
259 | $this->id = $obj->rowid; |
||
260 | $this->ref = $obj->rowid; |
||
261 | |||
262 | $this->fk_type = $obj->fk_type; |
||
263 | $this->fk_adherent = $obj->fk_adherent; |
||
264 | $this->datec = $this->db->jdate($obj->datec); |
||
265 | $this->datem = $this->db->jdate($obj->tms); |
||
266 | $this->dateh = $this->db->jdate($obj->dateh); |
||
267 | $this->datef = $this->db->jdate($obj->datef); |
||
268 | $this->amount = $obj->subscription; |
||
269 | $this->note = $obj->note_public; // deprecated |
||
270 | $this->note_public = $obj->note_public; |
||
271 | $this->fk_bank = $obj->fk_bank; |
||
272 | return 1; |
||
273 | } else { |
||
274 | return 0; |
||
275 | } |
||
276 | } else { |
||
277 | $this->error = $this->db->lasterror(); |
||
278 | return -1; |
||
279 | } |
||
280 | } |
||
281 | |||
282 | |||
283 | /** |
||
284 | * Update subscription |
||
285 | * |
||
286 | * @param User $user User who updated |
||
287 | * @param int $notrigger 0=Disable triggers |
||
288 | * @return int Return integer <0 if KO, >0 if OK |
||
289 | */ |
||
290 | public function update($user, $notrigger = 0) |
||
291 | { |
||
292 | $error = 0; |
||
293 | |||
294 | $this->db->begin(); |
||
295 | |||
296 | if (!is_numeric($this->amount)) { |
||
297 | $this->error = 'BadValueForParameterAmount'; |
||
298 | return -1; |
||
299 | } |
||
300 | |||
301 | if (empty($this->note_public) && !empty($this->note)) { // For backward compatibility |
||
302 | $this->note_public = $this->note; |
||
303 | } |
||
304 | |||
305 | $sql = "UPDATE " . MAIN_DB_PREFIX . "subscription SET "; |
||
306 | $sql .= " fk_type = " . ((int) $this->fk_type) . ","; |
||
307 | $sql .= " fk_adherent = " . ((int) $this->fk_adherent) . ","; |
||
308 | $sql .= " note = " . ($this->note_public ? "'" . $this->db->escape($this->note_public) . "'" : 'null') . ","; |
||
309 | $sql .= " subscription = " . (float) price2num($this->amount) . ","; |
||
310 | $sql .= " dateadh = '" . $this->db->idate($this->dateh) . "',"; |
||
311 | $sql .= " datef = '" . $this->db->idate($this->datef) . "',"; |
||
312 | $sql .= " datec = '" . $this->db->idate($this->datec) . "',"; |
||
313 | $sql .= " fk_bank = " . ($this->fk_bank ? ((int) $this->fk_bank) : 'null'); |
||
314 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
315 | |||
316 | dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
||
317 | $resql = $this->db->query($sql); |
||
318 | if ($resql) { |
||
319 | require_once constant('DOL_DOCUMENT_ROOT') . '/adherents/class/adherent.class.php'; |
||
320 | $member = new Adherent($this->db); |
||
321 | $result = $member->fetch($this->fk_adherent); |
||
322 | $result = $member->update_end_date($user); |
||
323 | |||
324 | if (!$error && !$notrigger) { |
||
325 | $this->context = array('member' => $member); |
||
326 | // Call triggers |
||
327 | $result = $this->call_trigger('MEMBER_SUBSCRIPTION_MODIFY', $user); |
||
328 | if ($result < 0) { |
||
329 | $error++; |
||
330 | } //Do also here what you must do to rollback action if trigger fail |
||
331 | // End call triggers |
||
332 | } |
||
333 | } else { |
||
334 | $error++; |
||
335 | $this->error = $this->db->lasterror(); |
||
336 | } |
||
337 | |||
338 | // Commit or rollback |
||
339 | if ($error) { |
||
340 | $this->db->rollback(); |
||
341 | return -1; |
||
342 | } else { |
||
343 | $this->db->commit(); |
||
344 | return $this->id; |
||
345 | } |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Delete a subscription |
||
350 | * |
||
351 | * @param User $user User that delete |
||
352 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
353 | * @return int Return integer <0 if KO, 0 if not found, >0 if OK |
||
354 | */ |
||
355 | public function delete($user, $notrigger = 0) |
||
356 | { |
||
357 | $error = 0; |
||
358 | |||
359 | // It subscription is linked to a bank transaction, we get it |
||
360 | if ($this->fk_bank > 0) { |
||
361 | require_once constant('DOL_DOCUMENT_ROOT') . '/compta/bank/class/account.class.php'; |
||
362 | $accountline = new AccountLine($this->db); |
||
363 | $result = $accountline->fetch($this->fk_bank); |
||
364 | } |
||
365 | |||
366 | $this->db->begin(); |
||
367 | |||
368 | if (!$error) { |
||
369 | if (!$notrigger) { |
||
370 | // Call triggers |
||
371 | $result = $this->call_trigger('MEMBER_SUBSCRIPTION_DELETE', $user); |
||
372 | if ($result < 0) { |
||
373 | $error++; |
||
374 | } // Do also here what you must do to rollback action if trigger fail |
||
375 | // End call triggers |
||
376 | } |
||
377 | } |
||
378 | |||
379 | if (!$error) { |
||
380 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "subscription WHERE rowid = " . ((int) $this->id); |
||
381 | dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
||
382 | $resql = $this->db->query($sql); |
||
383 | if ($resql) { |
||
384 | $num = $this->db->affected_rows($resql); |
||
385 | if ($num) { |
||
386 | require_once constant('DOL_DOCUMENT_ROOT') . '/adherents/class/adherent.class.php'; |
||
387 | $member = new Adherent($this->db); |
||
388 | $result = $member->fetch($this->fk_adherent); |
||
389 | $result = $member->update_end_date($user); |
||
390 | |||
391 | if ($this->fk_bank > 0 && is_object($accountline) && $accountline->id > 0) { // If we found bank account line (this means this->fk_bank defined) |
||
392 | $result = $accountline->delete($user); // Return false if refused because line is reconciled |
||
393 | if ($result > 0) { |
||
394 | $this->db->commit(); |
||
395 | return 1; |
||
396 | } else { |
||
397 | $this->error = $accountline->error; |
||
398 | $this->db->rollback(); |
||
399 | return -1; |
||
400 | } |
||
401 | } else { |
||
402 | $this->db->commit(); |
||
403 | return 1; |
||
404 | } |
||
405 | } else { |
||
406 | $this->db->commit(); |
||
407 | return 0; |
||
408 | } |
||
409 | } else { |
||
410 | $error++; |
||
411 | $this->error = $this->db->lasterror(); |
||
412 | } |
||
413 | } |
||
414 | |||
415 | // Commit or rollback |
||
416 | if ($error) { |
||
417 | $this->db->rollback(); |
||
418 | return -1; |
||
419 | } else { |
||
420 | $this->db->commit(); |
||
421 | return 1; |
||
422 | } |
||
423 | } |
||
424 | |||
425 | |||
426 | /** |
||
427 | * Return clicable name (with picto eventually) |
||
428 | * |
||
429 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
||
430 | * @param int $notooltip 1=Disable tooltip |
||
431 | * @param string $option Page for link ('', 'nolink', ...) |
||
432 | * @param string $morecss Add more css on link |
||
433 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
434 | * @return string Chaine avec URL |
||
435 | */ |
||
436 | public function getNomUrl($withpicto = 0, $notooltip = 0, $option = '', $morecss = '', $save_lastsearch_value = -1) |
||
437 | { |
||
438 | global $langs; |
||
439 | |||
440 | $result = ''; |
||
441 | |||
442 | $langs->load("members"); |
||
443 | |||
444 | $label = img_picto('', $this->picto) . ' <u class="paddingrightonly">' . $langs->trans("Subscription") . '</u>'; |
||
445 | /*if (isset($this->statut)) { |
||
446 | $label .= ' '.$this->getLibStatut(5); |
||
447 | }*/ |
||
448 | $label .= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref; |
||
449 | if (!empty($this->dateh)) { |
||
450 | $label .= '<br><b>' . $langs->trans('DateStart') . ':</b> ' . dol_print_date($this->dateh, 'day'); |
||
451 | } |
||
452 | if (!empty($this->datef)) { |
||
453 | $label .= '<br><b>' . $langs->trans('DateEnd') . ':</b> ' . dol_print_date($this->datef, 'day'); |
||
454 | } |
||
455 | |||
456 | $url = constant('BASE_URL') . '/adherents/subscription/card.php?rowid=' . ((int) $this->id); |
||
457 | |||
458 | if ($option != 'nolink') { |
||
459 | // Add param to save lastsearch_values or not |
||
460 | $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0); |
||
461 | if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { |
||
462 | $add_save_lastsearch_values = 1; |
||
463 | } |
||
464 | if ($add_save_lastsearch_values) { |
||
465 | $url .= '&save_lastsearch_values=1'; |
||
466 | } |
||
467 | } |
||
468 | |||
469 | $linkstart = '<a href="' . $url . '" class="classfortooltip" title="' . dol_escape_htmltag($label, 1) . '">'; |
||
470 | $linkend = '</a>'; |
||
471 | |||
472 | $result .= $linkstart; |
||
473 | if ($withpicto) { |
||
474 | $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="' . (($withpicto != 2) ? 'paddingright ' : '') . 'classfortooltip"'), 0, 0, $notooltip ? 0 : 1); |
||
475 | } |
||
476 | if ($withpicto != 2) { |
||
477 | $result .= $this->ref; |
||
478 | } |
||
479 | $result .= $linkend; |
||
480 | |||
481 | return $result; |
||
482 | } |
||
483 | |||
484 | |||
485 | /** |
||
486 | * Return the label of the status |
||
487 | * |
||
488 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto |
||
489 | * @return string Label of status |
||
490 | */ |
||
491 | public function getLibStatut($mode = 0) |
||
494 | } |
||
495 | |||
496 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
497 | /** |
||
498 | * Return the label of a given status |
||
499 | * |
||
500 | * @param int $status Id status |
||
501 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto |
||
502 | * @return string Label of status |
||
503 | */ |
||
504 | public function LibStatut($status, $mode = 0) |
||
505 | { |
||
506 | // phpcs:enable |
||
507 | |||
508 | //$langs->load("members"); |
||
509 | |||
510 | return ''; |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Load information of the subscription object |
||
515 | * |
||
516 | * @param int $id Id subscription |
||
517 | * @return void |
||
518 | */ |
||
519 | public function info($id) |
||
520 | { |
||
521 | $sql = 'SELECT c.rowid, c.datec,'; |
||
522 | $sql .= ' c.tms as datem'; |
||
523 | $sql .= ' FROM ' . MAIN_DB_PREFIX . 'subscription as c'; |
||
524 | $sql .= ' WHERE c.rowid = ' . ((int) $id); |
||
525 | |||
526 | $resql = $this->db->query($sql); |
||
527 | if ($resql) { |
||
528 | if ($this->db->num_rows($resql)) { |
||
529 | $obj = $this->db->fetch_object($resql); |
||
530 | $this->id = $obj->rowid; |
||
531 | |||
532 | $this->date_creation = $this->db->jdate($obj->datec); |
||
533 | $this->date_modification = $this->db->jdate($obj->datem); |
||
534 | } |
||
535 | |||
536 | $this->db->free($resql); |
||
537 | } else { |
||
538 | dol_print_error($this->db); |
||
539 | } |
||
540 | } |
||
541 | |||
542 | /** |
||
543 | * Return clicable link of object (with eventually picto) |
||
544 | * |
||
545 | * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link) |
||
546 | * @param array $arraydata Array of data |
||
547 | * @return string HTML Code for Kanban thumb. |
||
548 | */ |
||
549 | public function getKanbanView($option = '', $arraydata = null) |
||
584 | } |
||
585 | } |
||
586 |