Total Complexity | 114 |
Total Lines | 920 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Tva 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 Tva, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | * Put here description of your class |
||
39 | */ |
||
40 | class Tva extends CommonObject |
||
41 | { |
||
42 | /** |
||
43 | * @var string ID to identify managed object |
||
44 | */ |
||
45 | public $element = 'tva'; |
||
46 | |||
47 | /** |
||
48 | * @var string Name of table without prefix where object is stored |
||
49 | */ |
||
50 | public $table_element = 'tva'; |
||
51 | |||
52 | /** |
||
53 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
54 | */ |
||
55 | public $picto = 'payment'; |
||
56 | |||
57 | /** |
||
58 | * @deprecated |
||
59 | * @see $amount |
||
60 | */ |
||
61 | public $total; |
||
62 | |||
63 | public $datep; |
||
64 | public $datev; |
||
65 | public $amount; |
||
66 | public $type_payment; |
||
67 | |||
68 | /** |
||
69 | * @var string Payment reference |
||
70 | * (Cheque or bank transfer reference. Can be "ABC123") |
||
71 | */ |
||
72 | public $num_payment; |
||
73 | |||
74 | /** |
||
75 | * @var int Creation date |
||
76 | */ |
||
77 | public $datec; |
||
78 | |||
79 | /** |
||
80 | * @var int ID |
||
81 | */ |
||
82 | public $fk_type; |
||
83 | |||
84 | /** |
||
85 | * @var int |
||
86 | */ |
||
87 | public $paye; |
||
88 | |||
89 | /** |
||
90 | * @var int |
||
91 | */ |
||
92 | public $rappro; |
||
93 | |||
94 | /** |
||
95 | * @var string label |
||
96 | */ |
||
97 | public $label; |
||
98 | |||
99 | /** |
||
100 | * @var int ID |
||
101 | */ |
||
102 | public $fk_bank; |
||
103 | |||
104 | /** |
||
105 | * @var int accountid |
||
106 | */ |
||
107 | public $accountid; |
||
108 | |||
109 | /** |
||
110 | * @var int ID |
||
111 | */ |
||
112 | public $fk_user_creat; |
||
113 | |||
114 | /** |
||
115 | * @var int ID |
||
116 | */ |
||
117 | public $fk_user_modif; |
||
118 | |||
119 | /** |
||
120 | * @var integer|string paiementtype |
||
121 | */ |
||
122 | public $paiementtype; |
||
123 | |||
124 | |||
125 | const STATUS_UNPAID = 0; |
||
126 | const STATUS_PAID = 1; |
||
127 | |||
128 | /** |
||
129 | * Constructor |
||
130 | * |
||
131 | * @param DoliDB $db Database handler |
||
|
|||
132 | */ |
||
133 | public function __construct($db) |
||
134 | { |
||
135 | $this->db = $db; |
||
136 | } |
||
137 | |||
138 | |||
139 | /** |
||
140 | * Create in database |
||
141 | * |
||
142 | * @param User $user User that create |
||
143 | * @return int Return integer <0 if KO, >0 if OK |
||
144 | */ |
||
145 | public function create($user) |
||
146 | { |
||
147 | global $conf, $langs; |
||
148 | |||
149 | $error = 0; |
||
150 | $now = dol_now(); |
||
151 | |||
152 | // Clean parameters |
||
153 | $this->amount = trim($this->amount); |
||
154 | $this->label = trim($this->label); |
||
155 | $this->type_payment = (int) $this->type_payment; |
||
156 | $this->note = trim($this->note); |
||
157 | $this->fk_account = (int) $this->fk_account; |
||
158 | $this->fk_user_creat = (int) $this->fk_user_creat; |
||
159 | $this->fk_user_modif = (int) $this->fk_user_modif; |
||
160 | |||
161 | // Check parameters |
||
162 | // Put here code to add control on parameters values |
||
163 | |||
164 | $this->db->begin(); |
||
165 | |||
166 | // Insert request |
||
167 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "tva("; |
||
168 | $sql .= "entity,"; |
||
169 | $sql .= "datec,"; |
||
170 | $sql .= "datep,"; |
||
171 | $sql .= "datev,"; |
||
172 | $sql .= "amount,"; |
||
173 | $sql .= "label,"; |
||
174 | $sql .= "note,"; |
||
175 | $sql .= "fk_account,"; |
||
176 | $sql .= "fk_typepayment,"; |
||
177 | $sql .= "fk_user_creat,"; |
||
178 | $sql .= "fk_user_modif"; |
||
179 | $sql .= ") VALUES ("; |
||
180 | $sql .= " " . ((int) $conf->entity) . ", "; |
||
181 | $sql .= " '" . $this->db->idate($now) . "',"; |
||
182 | $sql .= " '" . $this->db->idate($this->datep) . "',"; |
||
183 | $sql .= " '" . $this->db->idate($this->datev) . "',"; |
||
184 | $sql .= " '" . $this->db->escape($this->amount) . "',"; |
||
185 | $sql .= " '" . $this->db->escape($this->label) . "',"; |
||
186 | $sql .= " '" . $this->db->escape($this->note) . "',"; |
||
187 | $sql .= " '" . $this->db->escape($this->fk_account) . "',"; |
||
188 | $sql .= " '" . $this->db->escape($this->type_payment) . "',"; |
||
189 | $sql .= " " . ($this->fk_user_creat > 0 ? (int) $this->fk_user_creat : (int) $user->id) . ","; |
||
190 | $sql .= " " . ($this->fk_user_modif > 0 ? (int) $this->fk_user_modif : (int) $user->id); |
||
191 | $sql .= ")"; |
||
192 | |||
193 | dol_syslog(get_class($this) . "::create", LOG_DEBUG); |
||
194 | $resql = $this->db->query($sql); |
||
195 | if ($resql) { |
||
196 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "tva"); |
||
197 | |||
198 | // Call trigger |
||
199 | $result = $this->call_trigger('TVA_CREATE', $user); |
||
200 | if ($result < 0) { |
||
201 | $error++; |
||
202 | } |
||
203 | // End call triggers |
||
204 | |||
205 | if (!$error) { |
||
206 | $this->db->commit(); |
||
207 | return $this->id; |
||
208 | } else { |
||
209 | $this->db->rollback(); |
||
210 | return -1; |
||
211 | } |
||
212 | } else { |
||
213 | $this->error = "Error " . $this->db->lasterror(); |
||
214 | $this->db->rollback(); |
||
215 | return -1; |
||
216 | } |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Update database |
||
221 | * |
||
222 | * @param User $user User that modify |
||
223 | * @param int $notrigger 0=no, 1=yes (no update trigger) |
||
224 | * @return int Return integer <0 if KO, >0 if OK |
||
225 | */ |
||
226 | public function update($user, $notrigger = 0) |
||
227 | { |
||
228 | global $conf, $langs; |
||
229 | |||
230 | $error = 0; |
||
231 | |||
232 | // Clean parameters |
||
233 | $this->amount = trim($this->amount); |
||
234 | $this->label = trim($this->label); |
||
235 | $this->note = trim($this->note); |
||
236 | $this->fk_user_creat = (int) $this->fk_user_creat; |
||
237 | $this->fk_user_modif = (int) $this->fk_user_modif; |
||
238 | |||
239 | // Check parameters |
||
240 | // Put here code to add control on parameters values |
||
241 | |||
242 | $this->db->begin(); |
||
243 | |||
244 | // Update request |
||
245 | $sql = "UPDATE " . MAIN_DB_PREFIX . "tva SET"; |
||
246 | $sql .= " tms='" . $this->db->idate($this->tms) . "',"; |
||
247 | $sql .= " datep='" . $this->db->idate($this->datep) . "',"; |
||
248 | $sql .= " datev='" . $this->db->idate($this->datev) . "',"; |
||
249 | $sql .= " amount=" . price2num($this->amount) . ","; |
||
250 | $sql .= " label='" . $this->db->escape($this->label) . "',"; |
||
251 | $sql .= " note='" . $this->db->escape($this->note) . "',"; |
||
252 | $sql .= " fk_user_creat=" . ((int) $this->fk_user_creat) . ","; |
||
253 | $sql .= " fk_user_modif=" . ((int) ($this->fk_user_modif > 0 ? $this->fk_user_modif : $user->id)); |
||
254 | $sql .= " WHERE rowid=" . ((int) $this->id); |
||
255 | |||
256 | dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
||
257 | $resql = $this->db->query($sql); |
||
258 | if (!$resql) { |
||
259 | $this->error = "Error " . $this->db->lasterror(); |
||
260 | $error++; |
||
261 | } |
||
262 | |||
263 | if (!$error && !$notrigger) { |
||
264 | // Call trigger |
||
265 | $result = $this->call_trigger('TVA_MODIFY', $user); |
||
266 | if ($result < 0) { |
||
267 | $error++; |
||
268 | } |
||
269 | // End call triggers |
||
270 | } |
||
271 | |||
272 | if (!$error) { |
||
273 | $this->db->commit(); |
||
274 | return 1; |
||
275 | } else { |
||
276 | $this->db->rollback(); |
||
277 | return -1; |
||
278 | } |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Tag TVA as paid completely |
||
283 | * |
||
284 | * @param User $user Object user making change |
||
285 | * @return int Return integer <0 if KO, >0 if OK |
||
286 | */ |
||
287 | public function setPaid($user) |
||
288 | { |
||
289 | // phpcs:enable |
||
290 | $sql = "UPDATE " . MAIN_DB_PREFIX . "tva SET"; |
||
291 | $sql .= " paye = 1"; |
||
292 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
293 | $resql = $this->db->query($sql); |
||
294 | if ($resql) { |
||
295 | return 1; |
||
296 | } else { |
||
297 | return -1; |
||
298 | } |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Remove tag paid on TVA |
||
303 | * |
||
304 | * @param User $user Object user making change |
||
305 | * @return int Return integer <0 if KO, >0 if OK |
||
306 | */ |
||
307 | public function setUnpaid($user) |
||
308 | { |
||
309 | // phpcs:enable |
||
310 | $sql = "UPDATE " . MAIN_DB_PREFIX . "tva SET"; |
||
311 | $sql .= " paye = 0"; |
||
312 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
313 | $resql = $this->db->query($sql); |
||
314 | if ($resql) { |
||
315 | return 1; |
||
316 | } else { |
||
317 | return -1; |
||
318 | } |
||
319 | } |
||
320 | |||
321 | |||
322 | /** |
||
323 | * Load object in memory from database |
||
324 | * |
||
325 | * @param int $id id object |
||
326 | * @param string $ref Ref of VAT (not used yet) |
||
327 | * @return int Return integer <0 if KO, >0 if OK |
||
328 | */ |
||
329 | public function fetch($id, $ref = '') |
||
330 | { |
||
331 | $sql = "SELECT"; |
||
332 | $sql .= " t.rowid,"; |
||
333 | $sql .= " t.tms,"; |
||
334 | $sql .= " t.datep,"; |
||
335 | $sql .= " t.datev,"; |
||
336 | $sql .= " t.amount,"; |
||
337 | $sql .= " t.fk_typepayment,"; |
||
338 | $sql .= " t.num_payment,"; |
||
339 | $sql .= " t.label,"; |
||
340 | $sql .= " t.note,"; |
||
341 | $sql .= " t.paye,"; |
||
342 | $sql .= " t.fk_user_creat,"; |
||
343 | $sql .= " t.fk_user_modif,"; |
||
344 | $sql .= " t.fk_account"; |
||
345 | $sql .= " FROM " . MAIN_DB_PREFIX . "tva as t"; |
||
346 | $sql .= " WHERE t.rowid = " . ((int) $id); |
||
347 | |||
348 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
||
349 | |||
350 | $resql = $this->db->query($sql); |
||
351 | if ($resql) { |
||
352 | if ($this->db->num_rows($resql)) { |
||
353 | $obj = $this->db->fetch_object($resql); |
||
354 | |||
355 | $this->id = $obj->rowid; |
||
356 | $this->ref = $obj->rowid; |
||
357 | $this->tms = $this->db->jdate($obj->tms); |
||
358 | $this->datep = $this->db->jdate($obj->datep); |
||
359 | $this->datev = $this->db->jdate($obj->datev); |
||
360 | $this->amount = $obj->amount; |
||
361 | $this->type_payment = $obj->fk_typepayment; |
||
362 | $this->num_payment = $obj->num_payment; |
||
363 | $this->label = $obj->label; |
||
364 | $this->paye = $obj->paye; |
||
365 | $this->note = $obj->note; |
||
366 | $this->fk_user_creat = $obj->fk_user_creat; |
||
367 | $this->fk_user_modif = $obj->fk_user_modif; |
||
368 | $this->fk_account = $obj->fk_account; |
||
369 | $this->fk_type = empty($obj->fk_type) ? "" : $obj->fk_type; |
||
370 | $this->rappro = empty($obj->rappro) ? "" : $obj->rappro; |
||
371 | } |
||
372 | $this->db->free($resql); |
||
373 | |||
374 | return 1; |
||
375 | } else { |
||
376 | $this->error = "Error " . $this->db->lasterror(); |
||
377 | return -1; |
||
378 | } |
||
379 | } |
||
380 | |||
381 | |||
382 | /** |
||
383 | * Delete object in database |
||
384 | * |
||
385 | * @param User $user User that delete |
||
386 | * @return int Return integer <0 if KO, >0 if OK |
||
387 | */ |
||
388 | public function delete($user) |
||
389 | { |
||
390 | global $conf, $langs; |
||
391 | |||
392 | $error = 0; |
||
393 | |||
394 | // Call trigger |
||
395 | $result = $this->call_trigger('TVA_DELETE', $user); |
||
396 | if ($result < 0) { |
||
397 | return -1; |
||
398 | } |
||
399 | // End call triggers |
||
400 | |||
401 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "tva"; |
||
402 | $sql .= " WHERE rowid=" . ((int) $this->id); |
||
403 | |||
404 | dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
||
405 | $resql = $this->db->query($sql); |
||
406 | if (!$resql) { |
||
407 | $this->error = "Error " . $this->db->lasterror(); |
||
408 | return -1; |
||
409 | } |
||
410 | |||
411 | |||
412 | return 1; |
||
413 | } |
||
414 | |||
415 | |||
416 | /** |
||
417 | * Initialise an instance with random values. |
||
418 | * Used to build previews or test instances. |
||
419 | * id must be 0 if object instance is a specimen. |
||
420 | * |
||
421 | * @return int |
||
422 | */ |
||
423 | public function initAsSpecimen() |
||
424 | { |
||
425 | $this->id = 0; |
||
426 | |||
427 | $this->tms = dol_now(); |
||
428 | $this->datep = ''; |
||
429 | $this->datev = ''; |
||
430 | $this->amount = ''; |
||
431 | $this->label = ''; |
||
432 | $this->note = ''; |
||
433 | $this->fk_bank = 0; |
||
434 | $this->fk_user_creat = 0; |
||
435 | $this->fk_user_modif = 0; |
||
436 | |||
437 | return 1; |
||
438 | } |
||
439 | |||
440 | |||
441 | /** |
||
442 | * Balance of VAT |
||
443 | * |
||
444 | * @param int $year Year |
||
445 | * @return double Amount |
||
446 | */ |
||
447 | public function solde($year = 0) |
||
448 | { |
||
449 | $reglee = $this->tva_sum_reglee($year); |
||
450 | |||
451 | $payee = $this->tva_sum_payee($year); |
||
452 | $collectee = $this->tva_sum_collectee($year); |
||
453 | |||
454 | $solde = $reglee - ($collectee - $payee); |
||
455 | |||
456 | return $solde; |
||
457 | } |
||
458 | |||
459 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
460 | /** |
||
461 | * Total of the VAT from invoices emitted by the thirdparty. |
||
462 | * |
||
463 | * @param int $year Year |
||
464 | * @return double Amount |
||
465 | */ |
||
466 | public function tva_sum_collectee($year = 0) |
||
490 | } |
||
491 | } |
||
492 | |||
493 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
494 | /** |
||
495 | * VAT paid |
||
496 | * |
||
497 | * @param int $year Year |
||
498 | * @return double Amount |
||
499 | */ |
||
500 | public function tva_sum_payee($year = 0) |
||
501 | { |
||
502 | // phpcs:enable |
||
503 | |||
504 | $sql = "SELECT sum(f.total_tva) as total_tva"; |
||
505 | $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn as f"; |
||
506 | if ($year) { |
||
507 | $sql .= " WHERE f.datef >= '" . $this->db->escape($year) . "-01-01' AND f.datef <= '" . $this->db->escape($year) . "-12-31' "; |
||
508 | } |
||
509 | |||
510 | $result = $this->db->query($sql); |
||
511 | if ($result) { |
||
512 | if ($this->db->num_rows($result)) { |
||
513 | $obj = $this->db->fetch_object($result); |
||
514 | $ret = $obj->total_tva; |
||
515 | $this->db->free($result); |
||
516 | return $ret; |
||
517 | } else { |
||
518 | $this->db->free($result); |
||
519 | return 0; |
||
520 | } |
||
521 | } else { |
||
522 | print $this->db->lasterror(); |
||
523 | return -1; |
||
524 | } |
||
525 | } |
||
526 | |||
527 | |||
528 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
529 | /** |
||
530 | * Total of the VAT paid |
||
531 | * |
||
532 | * @param int $year Year |
||
533 | * @return double Amount |
||
534 | */ |
||
535 | public function tva_sum_reglee($year = 0) |
||
536 | { |
||
537 | // phpcs:enable |
||
538 | |||
539 | $sql = "SELECT sum(f.amount) as amount"; |
||
540 | $sql .= " FROM " . MAIN_DB_PREFIX . "tva as f"; |
||
541 | |||
542 | if ($year) { |
||
543 | $sql .= " WHERE f.datev >= '" . $this->db->escape($year) . "-01-01' AND f.datev <= '" . $this->db->escape($year) . "-12-31' "; |
||
544 | } |
||
545 | |||
546 | $result = $this->db->query($sql); |
||
547 | if ($result) { |
||
548 | if ($this->db->num_rows($result)) { |
||
549 | $obj = $this->db->fetch_object($result); |
||
550 | $ret = $obj->amount; |
||
551 | $this->db->free($result); |
||
552 | return $ret; |
||
553 | } else { |
||
554 | $this->db->free($result); |
||
555 | return 0; |
||
556 | } |
||
557 | } else { |
||
558 | print $this->db->lasterror(); |
||
559 | return -1; |
||
560 | } |
||
561 | } |
||
562 | |||
563 | |||
564 | /** |
||
565 | * Create in database |
||
566 | * |
||
567 | * @param User $user Object user that insert |
||
568 | * @return int Return integer <0 if KO, rowid in tva table if OK |
||
569 | */ |
||
570 | public function addPayment($user) |
||
571 | { |
||
572 | global $conf, $langs; |
||
573 | |||
574 | $this->db->begin(); |
||
575 | |||
576 | // Clean parameters |
||
577 | $this->amount = price2num(trim($this->amount)); |
||
578 | $this->label = trim($this->label); |
||
579 | $this->note = trim($this->note); |
||
580 | $this->num_payment = trim($this->num_payment); |
||
581 | $this->fk_bank = (int) $this->fk_bank; |
||
582 | $this->fk_user_creat = (int) $this->fk_user_creat; |
||
583 | $this->fk_user_modif = (int) $this->fk_user_modif; |
||
584 | if (empty($this->datec)) { |
||
585 | $this->datec = dol_now(); |
||
586 | } |
||
587 | |||
588 | // Check parameters |
||
589 | if (!$this->label) { |
||
590 | $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")); |
||
591 | return -3; |
||
592 | } |
||
593 | if ($this->amount == '') { |
||
594 | $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")); |
||
595 | return -4; |
||
596 | } |
||
597 | if (isModEnabled("bank") && (empty($this->accountid) || $this->accountid <= 0)) { |
||
598 | $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("BankAccount")); |
||
599 | return -5; |
||
600 | } |
||
601 | if (isModEnabled("bank") && (empty($this->type_payment) || $this->type_payment <= 0)) { |
||
602 | $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")); |
||
603 | return -5; |
||
604 | } |
||
605 | |||
606 | // Insert into llx_tva |
||
607 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "tva ("; |
||
608 | $sql .= "datec"; |
||
609 | $sql .= ", datep"; |
||
610 | $sql .= ", datev"; |
||
611 | $sql .= ", amount"; |
||
612 | $sql .= ", fk_typepayment"; |
||
613 | $sql .= ", num_payment"; |
||
614 | if ($this->note) { |
||
615 | $sql .= ", note"; |
||
616 | } |
||
617 | if ($this->label) { |
||
618 | $sql .= ", label"; |
||
619 | } |
||
620 | $sql .= ", fk_user_creat"; |
||
621 | $sql .= ", fk_bank"; |
||
622 | $sql .= ", entity"; |
||
623 | $sql .= ") "; |
||
624 | $sql .= " VALUES ("; |
||
625 | $sql .= " '" . $this->db->idate($this->datec) . "'"; |
||
626 | $sql .= ", '" . $this->db->idate($this->datep) . "'"; |
||
627 | $sql .= ", '" . $this->db->idate($this->datev) . "'"; |
||
628 | $sql .= ", " . ((float) $this->amount); |
||
629 | $sql .= ", '" . $this->db->escape($this->type_payment) . "'"; |
||
630 | $sql .= ", '" . $this->db->escape($this->num_payment) . "'"; |
||
631 | if ($this->note) { |
||
632 | $sql .= ", '" . $this->db->escape($this->note) . "'"; |
||
633 | } |
||
634 | if ($this->label) { |
||
635 | $sql .= ", '" . $this->db->escape($this->label) . "'"; |
||
636 | } |
||
637 | $sql .= ", '" . $this->db->escape($user->id) . "'"; |
||
638 | $sql .= ", NULL"; |
||
639 | $sql .= ", " . ((int) $conf->entity); |
||
640 | $sql .= ")"; |
||
641 | |||
642 | dol_syslog(get_class($this) . "::addPayment", LOG_DEBUG); |
||
643 | $result = $this->db->query($sql); |
||
644 | if ($result) { |
||
645 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "tva"); // TODO should be called 'payment_vat' |
||
646 | |||
647 | // Call trigger |
||
648 | //XXX: Should be done just before commit no ? |
||
649 | $result = $this->call_trigger('TVA_ADDPAYMENT', $user); |
||
650 | if ($result < 0) { |
||
651 | $this->id = 0; |
||
652 | $ok = 0; |
||
653 | } |
||
654 | // End call triggers |
||
655 | |||
656 | if ($this->id > 0) { |
||
657 | $ok = 1; |
||
658 | if (isModEnabled("bank") && !empty($this->amount)) { |
||
659 | // Insert into llx_bank |
||
660 | |||
661 | $acc = new Account($this->db); |
||
662 | $result = $acc->fetch($this->accountid); |
||
663 | if ($result <= 0) { |
||
664 | dol_print_error($this->db); |
||
665 | } |
||
666 | |||
667 | if ($this->amount > 0) { |
||
668 | $bank_line_id = $acc->addline($this->datep, $this->type_payment, $this->label, -abs((float) $this->amount), $this->num_payment, '', $user); |
||
669 | } else { |
||
670 | $bank_line_id = $acc->addline($this->datep, $this->type_payment, $this->label, abs((float) $this->amount), $this->num_payment, '', $user); |
||
671 | } |
||
672 | |||
673 | // Update fk_bank into llx_tva. So we know vat line used to generate bank transaction |
||
674 | if ($bank_line_id > 0) { |
||
675 | $this->update_fk_bank($bank_line_id); |
||
676 | } else { |
||
677 | $this->error = $acc->error; |
||
678 | $ok = 0; |
||
679 | } |
||
680 | |||
681 | // Update links |
||
682 | $result = $acc->add_url_line($bank_line_id, $this->id, constant('BASE_URL') . '/compta/tva/card.php?id=', "(VATPayment)", "payment_vat"); |
||
683 | if ($result < 0) { |
||
684 | $this->error = $acc->error; |
||
685 | $ok = 0; |
||
686 | } |
||
687 | } |
||
688 | |||
689 | if ($ok) { |
||
690 | $this->db->commit(); |
||
691 | return $this->id; |
||
692 | } else { |
||
693 | $this->db->rollback(); |
||
694 | return -3; |
||
695 | } |
||
696 | } else { |
||
697 | $this->db->rollback(); |
||
698 | return -2; |
||
699 | } |
||
700 | } else { |
||
701 | $this->error = $this->db->error(); |
||
702 | $this->db->rollback(); |
||
703 | return -1; |
||
704 | } |
||
705 | } |
||
706 | |||
707 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
708 | /** |
||
709 | * Update link between payment tva and line generate into llx_bank |
||
710 | * |
||
711 | * @param int $id_bank Id bank account |
||
712 | * @return int Return integer <0 if KO, >0 if OK |
||
713 | */ |
||
714 | public function update_fk_bank($id_bank) |
||
725 | } |
||
726 | } |
||
727 | |||
728 | /** |
||
729 | * Send name clicable (with possibly the picto) |
||
730 | * |
||
731 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
||
732 | * @param string $option link option |
||
733 | * @param int $notooltip 1=Disable tooltip |
||
734 | * @param string $morecss More CSS |
||
735 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
736 | * @return string Chaine with URL |
||
737 | */ |
||
738 | public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1) |
||
796 | } |
||
797 | |||
798 | /** |
||
799 | * Return amount of payments already done |
||
800 | * |
||
801 | * @return int Amount of payment already done, <0 if KO |
||
802 | */ |
||
803 | public function getSommePaiement() |
||
804 | { |
||
805 | $table = 'payment_vat'; |
||
806 | $field = 'fk_tva'; |
||
807 | |||
808 | $sql = 'SELECT sum(amount) as amount'; |
||
809 | $sql .= ' FROM ' . MAIN_DB_PREFIX . $table; |
||
810 | $sql .= " WHERE " . $field . " = " . ((int) $this->id); |
||
811 | |||
812 | dol_syslog(get_class($this) . "::getSommePaiement", LOG_DEBUG); |
||
813 | $resql = $this->db->query($sql); |
||
814 | if ($resql) { |
||
815 | $amount = 0; |
||
816 | |||
817 | $obj = $this->db->fetch_object($resql); |
||
818 | if ($obj) { |
||
819 | $amount = $obj->amount ? $obj->amount : 0; |
||
820 | } |
||
821 | |||
822 | $this->db->free($resql); |
||
823 | return $amount; |
||
824 | } else { |
||
825 | return -1; |
||
826 | } |
||
827 | } |
||
828 | |||
829 | /** |
||
830 | * Information of vat payment object |
||
831 | * |
||
832 | * @param int $id Id of vat payment |
||
833 | * @return void |
||
834 | */ |
||
835 | public function info($id) |
||
858 | } |
||
859 | } |
||
860 | |||
861 | /** |
||
862 | * Return the label of the VAT status f object |
||
863 | * |
||
864 | * @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 |
||
865 | * @param double $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommend to put here amount paid if you have it, 1 otherwise) |
||
866 | * @return string Label |
||
867 | */ |
||
868 | public function getLibStatut($mode = 0, $alreadypaid = -1) |
||
869 | { |
||
870 | return $this->LibStatut($this->paye, $mode, $alreadypaid); |
||
871 | } |
||
872 | |||
873 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
874 | /** |
||
875 | * Return the label of a given VAT status |
||
876 | * |
||
877 | * @param int $status Id status |
||
878 | * @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 |
||
879 | * @param double $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommend to put here amount paid if you have it, 1 otherwise) |
||
880 | * @return string Label |
||
881 | */ |
||
882 | public function LibStatut($status, $mode = 0, $alreadypaid = -1) |
||
918 | } |
||
919 | |||
920 | /** |
||
921 | * Return clicable link of object (with eventually picto) |
||
922 | * |
||
923 | * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link) |
||
924 | * @param array $arraydata Array of data |
||
925 | * @return string HTML Code for Kanban thumb. |
||
926 | */ |
||
927 | public function getKanbanView($option = '', $arraydata = null) |
||
928 | { |
||
929 | global $langs; |
||
930 | |||
931 | $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']); |
||
932 | |||
933 | $return = '<div class="box-flex-item box-flex-grow-zero">'; |
||
934 | $return .= '<div class="info-box info-box-sm">'; |
||
935 | $return .= '<span class="info-box-icon bg-infobox-action">'; |
||
936 | $return .= img_picto('', $this->picto); |
||
937 | //$return .= '<i class="fa fa-dol-action"></i>'; // Can be image |
||
938 | $return .= '</span>'; |
||
939 | $return .= '<div class="info-box-content">'; |
||
940 | $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . (method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref) . '</span>'; |
||
941 | if ($selected >= 0) { |
||
942 | $return .= '<input id="cb' . $this->id . '" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="' . $this->id . '"' . ($selected ? ' checked="checked"' : '') . '>'; |
||
943 | } |
||
944 | if (property_exists($this, 'amount')) { |
||
945 | $return .= ' | <span class="opacitymedium">' . $langs->trans("Amount") . '</span> : <span class="info-box-label amount">' . price($this->amount) . '</span>'; |
||
946 | } |
||
947 | if (property_exists($this, 'type_payment')) { |
||
948 | $return .= '<br><span class="opacitymedium">' . $langs->trans("Payement") . '</span> : <span class="info-box-label">' . $this->type_payment . '</span>'; |
||
949 | } |
||
950 | if (property_exists($this, 'datev')) { |
||
951 | $return .= '<br><span class="opacitymedium">' . $langs->trans("DateEnd") . '</span> : <span class="info-box-label" >' . dol_print_date($this->datev) . '</span>'; |
||
952 | } |
||
953 | if (method_exists($this, 'LibStatut')) { |
||
954 | $return .= '<br><div class="info-box-status margintoponly">' . $this->getLibStatut(3, $this->alreadypaid) . '</div>'; |
||
955 | } |
||
956 | $return .= '</div>'; |
||
957 | $return .= '</div>'; |
||
958 | $return .= '</div>'; |
||
962 |