Total Complexity | 70 |
Total Lines | 662 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Localtax 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 Localtax, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class Localtax extends CommonObject |
||
36 | { |
||
37 | /** |
||
38 | * @var string ID to identify managed object |
||
39 | */ |
||
40 | public $element = 'localtax'; |
||
41 | |||
42 | /** |
||
43 | * @var string Name of table without prefix where object is stored |
||
44 | */ |
||
45 | public $table_element = 'localtax'; |
||
46 | |||
47 | /** |
||
48 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
49 | */ |
||
50 | public $picto = 'payment'; |
||
51 | |||
52 | public $ltt; |
||
53 | |||
54 | public $datep; |
||
55 | public $datev; |
||
56 | public $amount; |
||
57 | |||
58 | /** |
||
59 | * @var int |
||
60 | */ |
||
61 | public $accountid; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | public $fk_type; |
||
67 | |||
68 | public $paymenttype; |
||
69 | |||
70 | /** |
||
71 | * @var int |
||
72 | */ |
||
73 | public $rappro; |
||
74 | |||
75 | |||
76 | /** |
||
77 | * @var string local tax |
||
78 | */ |
||
79 | public $label; |
||
80 | |||
81 | /** |
||
82 | * @var int ID |
||
83 | */ |
||
84 | public $fk_bank; |
||
85 | |||
86 | /** |
||
87 | * @var int ID |
||
88 | */ |
||
89 | public $fk_user_creat; |
||
90 | |||
91 | /** |
||
92 | * @var int ID |
||
93 | */ |
||
94 | public $fk_user_modif; |
||
95 | |||
96 | /** |
||
97 | * Constructor |
||
98 | * |
||
99 | * @param DoliDB $db Database handler |
||
|
|||
100 | */ |
||
101 | public function __construct($db) |
||
102 | { |
||
103 | $this->db = $db; |
||
104 | } |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Create in database |
||
109 | * |
||
110 | * @param User $user User that create |
||
111 | * @return int Return integer <0 if KO, >0 if OK |
||
112 | */ |
||
113 | public function create($user) |
||
172 | } |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Update database |
||
177 | * |
||
178 | * @param User $user User that modify |
||
179 | * @param int $notrigger 0=no, 1=yes (no update trigger) |
||
180 | * @return int Return integer <0 if KO, >0 if OK |
||
181 | */ |
||
182 | public function update(User $user, $notrigger = 0) |
||
183 | { |
||
184 | global $conf, $langs; |
||
185 | |||
186 | $error = 0; |
||
187 | |||
188 | // Clean parameters |
||
189 | $this->amount = trim($this->amount); |
||
190 | $this->label = trim($this->label); |
||
191 | $this->note = trim($this->note); |
||
192 | |||
193 | $this->db->begin(); |
||
194 | |||
195 | // Update request |
||
196 | $sql = "UPDATE " . MAIN_DB_PREFIX . "localtax SET"; |
||
197 | $sql .= " localtaxtype=" . ((int) $this->ltt) . ","; |
||
198 | $sql .= " tms='" . $this->db->idate($this->tms) . "',"; |
||
199 | $sql .= " datep='" . $this->db->idate($this->datep) . "',"; |
||
200 | $sql .= " datev='" . $this->db->idate($this->datev) . "',"; |
||
201 | $sql .= " amount=" . price2num($this->amount) . ","; |
||
202 | $sql .= " label='" . $this->db->escape($this->label) . "',"; |
||
203 | $sql .= " note='" . $this->db->escape($this->note) . "',"; |
||
204 | $sql .= " fk_bank=" . (int) $this->fk_bank . ","; |
||
205 | $sql .= " fk_user_creat=" . (int) $this->fk_user_creat . ","; |
||
206 | $sql .= " fk_user_modif=" . (int) $this->fk_user_modif; |
||
207 | $sql .= " WHERE rowid=" . ((int) $this->id); |
||
208 | |||
209 | dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
||
210 | $resql = $this->db->query($sql); |
||
211 | if (!$resql) { |
||
212 | $this->error = "Error " . $this->db->lasterror(); |
||
213 | $error++; |
||
214 | } |
||
215 | |||
216 | if (!$error && !$notrigger) { |
||
217 | // Call trigger |
||
218 | $result = $this->call_trigger('LOCALTAX_MODIFY', $user); |
||
219 | if ($result < 0) { |
||
220 | $error++; |
||
221 | } |
||
222 | // End call triggers |
||
223 | } |
||
224 | |||
225 | if (!$error) { |
||
226 | $this->db->commit(); |
||
227 | return 1; |
||
228 | } else { |
||
229 | $this->db->rollback(); |
||
230 | return -1; |
||
231 | } |
||
232 | } |
||
233 | |||
234 | |||
235 | /** |
||
236 | * Load object in memory from database |
||
237 | * |
||
238 | * @param int $id Object id |
||
239 | * @return int Return integer <0 if KO, >0 if OK |
||
240 | */ |
||
241 | public function fetch($id) |
||
242 | { |
||
243 | $sql = "SELECT"; |
||
244 | $sql .= " t.rowid,"; |
||
245 | $sql .= " t.localtaxtype,"; |
||
246 | $sql .= " t.tms,"; |
||
247 | $sql .= " t.datep,"; |
||
248 | $sql .= " t.datev,"; |
||
249 | $sql .= " t.amount,"; |
||
250 | $sql .= " t.label,"; |
||
251 | $sql .= " t.note as note_private,"; |
||
252 | $sql .= " t.fk_bank,"; |
||
253 | $sql .= " t.fk_user_creat,"; |
||
254 | $sql .= " t.fk_user_modif,"; |
||
255 | $sql .= " b.fk_account,"; |
||
256 | $sql .= " b.fk_type,"; |
||
257 | $sql .= " b.rappro"; |
||
258 | $sql .= " FROM " . MAIN_DB_PREFIX . "localtax as t"; |
||
259 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank as b ON t.fk_bank = b.rowid"; |
||
260 | $sql .= " WHERE t.rowid = " . ((int) $id); |
||
261 | |||
262 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
||
263 | $resql = $this->db->query($sql); |
||
264 | if ($resql) { |
||
265 | if ($this->db->num_rows($resql)) { |
||
266 | $obj = $this->db->fetch_object($resql); |
||
267 | |||
268 | $this->id = $obj->rowid; |
||
269 | $this->ref = $obj->rowid; |
||
270 | $this->ltt = $obj->localtaxtype; |
||
271 | $this->tms = $this->db->jdate($obj->tms); |
||
272 | $this->datep = $this->db->jdate($obj->datep); |
||
273 | $this->datev = $this->db->jdate($obj->datev); |
||
274 | $this->amount = $obj->amount; |
||
275 | $this->label = $obj->label; |
||
276 | $this->note = $obj->note_private; |
||
277 | $this->note_private = $obj->note_private; |
||
278 | $this->fk_bank = $obj->fk_bank; |
||
279 | $this->fk_user_creat = $obj->fk_user_creat; |
||
280 | $this->fk_user_modif = $obj->fk_user_modif; |
||
281 | $this->fk_account = $obj->fk_account; |
||
282 | $this->fk_type = $obj->fk_type; |
||
283 | $this->rappro = $obj->rappro; |
||
284 | } |
||
285 | $this->db->free($resql); |
||
286 | |||
287 | return 1; |
||
288 | } else { |
||
289 | $this->error = "Error " . $this->db->lasterror(); |
||
290 | return -1; |
||
291 | } |
||
292 | } |
||
293 | |||
294 | |||
295 | /** |
||
296 | * Delete object in database |
||
297 | * |
||
298 | * @param User $user User that delete |
||
299 | * @return int Return integer <0 if KO, >0 if OK |
||
300 | */ |
||
301 | public function delete($user) |
||
302 | { |
||
303 | // Call trigger |
||
304 | $result = $this->call_trigger('LOCALTAX_DELETE', $user); |
||
305 | if ($result < 0) { |
||
306 | return -1; |
||
307 | } |
||
308 | // End call triggers |
||
309 | |||
310 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "localtax"; |
||
311 | $sql .= " WHERE rowid=" . ((int) $this->id); |
||
312 | |||
313 | dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
||
314 | $resql = $this->db->query($sql); |
||
315 | if (!$resql) { |
||
316 | $this->error = "Error " . $this->db->lasterror(); |
||
317 | return -1; |
||
318 | } |
||
319 | |||
320 | return 1; |
||
321 | } |
||
322 | |||
323 | |||
324 | /** |
||
325 | * Initialise an instance with random values. |
||
326 | * Used to build previews or test instances. |
||
327 | * id must be 0 if object instance is a specimen. |
||
328 | * |
||
329 | * @return int |
||
330 | */ |
||
331 | public function initAsSpecimen() |
||
332 | { |
||
333 | global $user; |
||
334 | |||
335 | $this->id = 0; |
||
336 | |||
337 | $this->tms = dol_now(); |
||
338 | $this->ltt = 0; |
||
339 | $this->datep = ''; |
||
340 | $this->datev = ''; |
||
341 | $this->amount = ''; |
||
342 | $this->label = ''; |
||
343 | $this->note = ''; |
||
344 | $this->fk_bank = 0; |
||
345 | $this->fk_user_creat = $user->id; |
||
346 | $this->fk_user_modif = $user->id; |
||
347 | |||
348 | return 1; |
||
349 | } |
||
350 | |||
351 | |||
352 | /** |
||
353 | * Hum la function s'appelle 'Solde' elle doit a mon avis calcluer le solde de localtax, non ? |
||
354 | * |
||
355 | * @param int $year Year |
||
356 | * @return int ??? |
||
357 | */ |
||
358 | public function solde($year = 0) |
||
359 | { |
||
360 | $reglee = $this->localtax_sum_reglee($year); |
||
361 | |||
362 | $payee = $this->localtax_sum_payee($year); |
||
363 | $collectee = $this->localtax_sum_collectee($year); |
||
364 | |||
365 | $solde = $reglee - ($collectee - $payee); |
||
366 | |||
367 | return $solde; |
||
368 | } |
||
369 | |||
370 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
371 | /** |
||
372 | * Total de la localtax des factures emises par la societe. |
||
373 | * |
||
374 | * @param int $year Year |
||
375 | * @return int ??? |
||
376 | */ |
||
377 | public function localtax_sum_collectee($year = 0) |
||
378 | { |
||
379 | // phpcs:enable |
||
380 | $sql = "SELECT sum(f.localtax) as amount"; |
||
381 | $sql .= " FROM " . MAIN_DB_PREFIX . "facture as f"; |
||
382 | $sql .= " WHERE f.paye = 1"; |
||
383 | if ($year) { |
||
384 | $sql .= " AND f.datef BETWEEN '" . $this->db->idate(dol_get_first_day($year, 1, 'gmt')) . "' AND '" . $this->db->idate(dol_get_last_day($year, 1, 'gmt')) . "'"; |
||
385 | } |
||
386 | |||
387 | $result = $this->db->query($sql); |
||
388 | if ($result) { |
||
389 | if ($this->db->num_rows($result)) { |
||
390 | $obj = $this->db->fetch_object($result); |
||
391 | $ret = $obj->amount; |
||
392 | $this->db->free($result); |
||
393 | return $ret; |
||
394 | } else { |
||
395 | $this->db->free($result); |
||
396 | return 0; |
||
397 | } |
||
398 | } else { |
||
399 | print $this->db->lasterror(); |
||
400 | return -1; |
||
401 | } |
||
402 | } |
||
403 | |||
404 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
405 | /** |
||
406 | * Total of localtax paid in invoice |
||
407 | * |
||
408 | * @param int $year Year |
||
409 | * @return int ??? |
||
410 | */ |
||
411 | public function localtax_sum_payee($year = 0) |
||
412 | { |
||
413 | // phpcs:enable |
||
414 | |||
415 | $sql = "SELECT sum(f.total_localtax) as total_localtax"; |
||
416 | $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn as f"; |
||
417 | if ($year) { |
||
418 | $sql .= " WHERE f.datef BETWEEN '" . $this->db->idate(dol_get_first_day($year, 1, 'gmt')) . "' AND '" . $this->db->idate(dol_get_last_day($year, 1, 'gmt')) . "'"; |
||
419 | } |
||
420 | |||
421 | $result = $this->db->query($sql); |
||
422 | if ($result) { |
||
423 | if ($this->db->num_rows($result)) { |
||
424 | $obj = $this->db->fetch_object($result); |
||
425 | $ret = $obj->total_localtax; |
||
426 | $this->db->free($result); |
||
427 | return $ret; |
||
428 | } else { |
||
429 | $this->db->free($result); |
||
430 | return 0; |
||
431 | } |
||
432 | } else { |
||
433 | print $this->db->lasterror(); |
||
434 | return -1; |
||
435 | } |
||
436 | } |
||
437 | |||
438 | |||
439 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
440 | /** |
||
441 | * Total of localtax paid |
||
442 | * |
||
443 | * @param int $year Year |
||
444 | * @return int ??? |
||
445 | */ |
||
446 | public function localtax_sum_reglee($year = 0) |
||
447 | { |
||
448 | // phpcs:enable |
||
449 | |||
450 | $sql = "SELECT sum(f.amount) as amount"; |
||
451 | $sql .= " FROM " . MAIN_DB_PREFIX . "localtax as f"; |
||
452 | if ($year) { |
||
453 | $sql .= " WHERE f.datev BETWEEN '" . $this->db->idate(dol_get_first_day($year, 1, 'gmt')) . "' AND '" . $this->db->idate(dol_get_last_day($year, 1, 'gmt')) . "'"; |
||
454 | } |
||
455 | |||
456 | $result = $this->db->query($sql); |
||
457 | if ($result) { |
||
458 | if ($this->db->num_rows($result)) { |
||
459 | $obj = $this->db->fetch_object($result); |
||
460 | $ret = $obj->amount; |
||
461 | $this->db->free($result); |
||
462 | return $ret; |
||
463 | } else { |
||
464 | $this->db->free($result); |
||
465 | return 0; |
||
466 | } |
||
467 | } else { |
||
468 | print $this->db->lasterror(); |
||
469 | return -1; |
||
470 | } |
||
471 | } |
||
472 | |||
473 | |||
474 | /** |
||
475 | * Add a payment of localtax |
||
476 | * |
||
477 | * @param User $user Object user that insert |
||
478 | * @return int Return integer <0 if KO, rowid in localtax table if OK |
||
479 | */ |
||
480 | public function addPayment($user) |
||
481 | { |
||
482 | global $conf, $langs; |
||
483 | |||
484 | $this->db->begin(); |
||
485 | |||
486 | // Check parameters |
||
487 | $this->amount = price2num($this->amount); |
||
488 | if (!$this->label) { |
||
489 | $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")); |
||
490 | return -3; |
||
491 | } |
||
492 | if ($this->amount <= 0) { |
||
493 | $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")); |
||
494 | return -4; |
||
495 | } |
||
496 | if (isModEnabled("bank") && (empty($this->accountid) || $this->accountid <= 0)) { |
||
497 | $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("BankAccount")); |
||
498 | return -5; |
||
499 | } |
||
500 | if (isModEnabled("bank") && (empty($this->paymenttype) || $this->paymenttype <= 0)) { |
||
501 | $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")); |
||
502 | return -5; |
||
503 | } |
||
504 | |||
505 | // Insertion dans table des paiement localtax |
||
506 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "localtax (localtaxtype, datep, datev, amount"; |
||
507 | if ($this->note) { |
||
508 | $sql .= ", note"; |
||
509 | } |
||
510 | if ($this->label) { |
||
511 | $sql .= ", label"; |
||
512 | } |
||
513 | $sql .= ", fk_user_creat, fk_bank"; |
||
514 | $sql .= ") "; |
||
515 | $sql .= " VALUES (" . $this->ltt . ", '" . $this->db->idate($this->datep) . "',"; |
||
516 | $sql .= "'" . $this->db->idate($this->datev) . "'," . $this->amount; |
||
517 | if ($this->note) { |
||
518 | $sql .= ", '" . $this->db->escape($this->note) . "'"; |
||
519 | } |
||
520 | if ($this->label) { |
||
521 | $sql .= ", '" . $this->db->escape($this->label) . "'"; |
||
522 | } |
||
523 | $sql .= ", " . ((int) $user->id) . ", NULL"; |
||
524 | $sql .= ")"; |
||
525 | |||
526 | dol_syslog(get_class($this) . "::addPayment", LOG_DEBUG); |
||
527 | $result = $this->db->query($sql); |
||
528 | if ($result) { |
||
529 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "localtax"); // TODO devrait s'appeler paiementlocaltax |
||
530 | if ($this->id > 0) { |
||
531 | $ok = 1; |
||
532 | if (isModEnabled("bank")) { |
||
533 | // Insertion dans llx_bank |
||
534 | |||
535 | $acc = new Account($this->db); |
||
536 | $result = $acc->fetch($this->accountid); |
||
537 | if ($result <= 0) { |
||
538 | dol_print_error($this->db); |
||
539 | } |
||
540 | |||
541 | $bank_line_id = $acc->addline($this->datep, $this->paymenttype, $this->label, -abs((float) $this->amount), '', '', $user); |
||
542 | |||
543 | // Update fk_bank into llx_localtax so we know the line of localtax used to generate the bank entry. |
||
544 | if ($bank_line_id > 0) { |
||
545 | $this->update_fk_bank($bank_line_id); |
||
546 | } else { |
||
547 | $this->error = $acc->error; |
||
548 | $ok = 0; |
||
549 | } |
||
550 | |||
551 | // Mise a jour liens |
||
552 | $result = $acc->add_url_line($bank_line_id, $this->id, constant('BASE_URL') . '/compta/localtax/card.php?id=', "(VATPayment)", "payment_vat"); |
||
553 | if ($result < 0) { |
||
554 | $this->error = $acc->error; |
||
555 | $ok = 0; |
||
556 | } |
||
557 | } |
||
558 | |||
559 | if ($ok) { |
||
560 | $this->db->commit(); |
||
561 | return $this->id; |
||
562 | } else { |
||
563 | $this->db->rollback(); |
||
564 | return -3; |
||
565 | } |
||
566 | } else { |
||
567 | $this->error = $this->db->lasterror(); |
||
568 | $this->db->rollback(); |
||
569 | return -2; |
||
570 | } |
||
571 | } else { |
||
572 | $this->error = $this->db->lasterror(); |
||
573 | $this->db->rollback(); |
||
574 | return -1; |
||
575 | } |
||
576 | } |
||
577 | |||
578 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
579 | /** |
||
580 | * Update the link between localtax payment and the line into llx_bank |
||
581 | * |
||
582 | * @param int $id Id bank account |
||
583 | * @return int Return integer <0 if KO, >0 if OK |
||
584 | */ |
||
585 | public function update_fk_bank($id) |
||
586 | { |
||
587 | // phpcs:enable |
||
588 | $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'localtax SET fk_bank = ' . ((int) $id); |
||
589 | $sql .= ' WHERE rowid = ' . ((int) $this->id); |
||
590 | $result = $this->db->query($sql); |
||
591 | if ($result) { |
||
592 | return 1; |
||
593 | } else { |
||
594 | dol_print_error($this->db); |
||
595 | return -1; |
||
596 | } |
||
597 | } |
||
598 | |||
599 | |||
600 | /** |
||
601 | * Returns clickable name |
||
602 | * |
||
603 | * @param int $withpicto 0=Link, 1=Picto into link, 2=Picto |
||
604 | * @param string $option Sur quoi pointe le lien |
||
605 | * @return string Chaine avec URL |
||
606 | */ |
||
607 | public function getNomUrl($withpicto = 0, $option = '') |
||
608 | { |
||
609 | global $langs; |
||
610 | |||
611 | $result = ''; |
||
612 | $label = $langs->trans("ShowVatPayment") . ': ' . $this->ref; |
||
613 | |||
614 | $link = '<a href="' . constant('BASE_URL') . '/compta/localtax/card.php?id=' . $this->id . '" title="' . dol_escape_htmltag($label, 1) . '" class="classfortooltip">'; |
||
615 | $linkend = '</a>'; |
||
616 | |||
617 | $picto = 'payment'; |
||
618 | |||
619 | if ($withpicto) { |
||
620 | $result .= ($link . img_object($label, $picto, 'class="classfortooltip"') . $linkend); |
||
621 | } |
||
622 | if ($withpicto && $withpicto != 2) { |
||
623 | $result .= ' '; |
||
624 | } |
||
625 | if ($withpicto != 2) { |
||
626 | $result .= $link . $this->ref . $linkend; |
||
627 | } |
||
628 | return $result; |
||
629 | } |
||
630 | |||
631 | /** |
||
632 | * Return the label of the status |
||
633 | * |
||
634 | * @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 |
||
635 | * @return string Label of status |
||
636 | */ |
||
637 | public function getLibStatut($mode = 0) |
||
638 | { |
||
639 | return $this->LibStatut($this->statut, $mode); |
||
640 | } |
||
641 | |||
642 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
643 | /** |
||
644 | * Return the label of a given status |
||
645 | * |
||
646 | * @param int $status Id status |
||
647 | * @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 |
||
648 | * @return string Label of status |
||
649 | */ |
||
650 | public function LibStatut($status, $mode = 0) |
||
651 | { |
||
652 | // phpcs:enable |
||
653 | //global $langs; |
||
654 | |||
655 | return ''; |
||
656 | } |
||
657 | |||
658 | /** |
||
659 | * Return clicable link of object (with eventually picto) |
||
660 | * |
||
661 | * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link) |
||
662 | * @param array $arraydata Array of data |
||
663 | * @return string HTML Code for Kanban thumb. |
||
664 | */ |
||
665 | public function getKanbanView($option = '', $arraydata = null) |
||
697 | } |
||
698 | } |
||
699 |