| Total Complexity | 84 |
| Total Lines | 800 |
| 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 |
||
| 35 | class Tva extends CommonObject |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var string ID to identify managed object |
||
| 39 | */ |
||
| 40 | public $element='tva'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string Name of table without prefix where object is stored |
||
| 44 | */ |
||
| 45 | public $table_element='tva'; |
||
| 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 $tms; |
||
| 53 | public $datep; |
||
| 54 | public $datev; |
||
| 55 | public $amount; |
||
| 56 | public $type_payment; |
||
| 57 | public $num_payment; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string label |
||
| 61 | */ |
||
| 62 | public $label; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var int ID |
||
| 66 | */ |
||
| 67 | public $fk_bank; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var int ID |
||
| 71 | */ |
||
| 72 | public $fk_user_creat; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var int ID |
||
| 76 | */ |
||
| 77 | public $fk_user_modif; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Constructor |
||
| 81 | * |
||
| 82 | * @param DoliDB $db Database handler |
||
| 83 | */ |
||
| 84 | public function __construct($db) |
||
| 85 | { |
||
| 86 | $this->db = $db; |
||
| 87 | } |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * Create in database |
||
| 92 | * |
||
| 93 | * @param User $user User that create |
||
| 94 | * @return int <0 if KO, >0 if OK |
||
| 95 | */ |
||
| 96 | public function create($user) |
||
| 97 | { |
||
| 98 | global $conf, $langs; |
||
| 99 | |||
| 100 | $error=0; |
||
| 101 | $now=dol_now(); |
||
| 102 | |||
| 103 | // Clean parameters |
||
| 104 | $this->amount=trim($this->amount); |
||
| 105 | $this->label=trim($this->label); |
||
| 106 | $this->note=trim($this->note); |
||
|
1 ignored issue
–
show
|
|||
| 107 | $this->fk_bank = (int) $this->fk_bank; |
||
| 108 | $this->fk_user_creat = (int) $this->fk_user_creat; |
||
| 109 | $this->fk_user_modif = (int) $this->fk_user_modif; |
||
| 110 | |||
| 111 | // Check parameters |
||
| 112 | // Put here code to add control on parameters values |
||
| 113 | |||
| 114 | $this->db->begin(); |
||
| 115 | |||
| 116 | // Insert request |
||
| 117 | $sql = "INSERT INTO ".MAIN_DB_PREFIX."tva("; |
||
| 118 | $sql.= "datec,"; |
||
| 119 | $sql.= "datep,"; |
||
| 120 | $sql.= "datev,"; |
||
| 121 | $sql.= "amount,"; |
||
| 122 | $sql.= "label,"; |
||
| 123 | $sql.= "note,"; |
||
| 124 | $sql.= "fk_bank,"; |
||
| 125 | $sql.= "fk_user_creat,"; |
||
| 126 | $sql.= "fk_user_modif"; |
||
| 127 | $sql.= ") VALUES ("; |
||
| 128 | $sql.= " '".$this->db->idate($now)."',"; |
||
| 129 | $sql.= " '".$this->db->idate($this->datep)."',"; |
||
| 130 | $sql.= " '".$this->db->idate($this->datev)."',"; |
||
| 131 | $sql.= " '".$this->db->escape($this->amount)."',"; |
||
| 132 | $sql.= " '".$this->db->escape($this->label)."',"; |
||
| 133 | $sql.= " '".$this->db->escape($this->note)."',"; |
||
|
1 ignored issue
–
show
|
|||
| 134 | $sql.= " ".($this->fk_bank <= 0 ? "NULL" : "'".$this->db->escape($this->fk_bank)."'").","; |
||
| 135 | $sql.= " '".$this->db->escape($this->fk_user_creat)."',"; |
||
| 136 | $sql.= " '".$this->db->escape($this->fk_user_modif)."'"; |
||
| 137 | $sql.= ")"; |
||
| 138 | |||
| 139 | dol_syslog(get_class($this)."::create", LOG_DEBUG); |
||
| 140 | $resql=$this->db->query($sql); |
||
| 141 | if ($resql) |
||
| 142 | { |
||
| 143 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."tva"); |
||
| 144 | |||
| 145 | // Call trigger |
||
| 146 | $result=$this->call_trigger('TVA_CREATE', $user); |
||
| 147 | if ($result < 0) $error++; |
||
| 148 | // End call triggers |
||
| 149 | |||
| 150 | if (! $error) |
||
| 151 | { |
||
| 152 | $this->db->commit(); |
||
| 153 | return $this->id; |
||
| 154 | } |
||
| 155 | else |
||
| 156 | { |
||
| 157 | $this->db->rollback(); |
||
| 158 | return -1; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | else |
||
| 162 | { |
||
| 163 | $this->error="Error ".$this->db->lasterror(); |
||
| 164 | $this->db->rollback(); |
||
| 165 | return -1; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Update database |
||
| 171 | * |
||
| 172 | * @param User $user User that modify |
||
| 173 | * @param int $notrigger 0=no, 1=yes (no update trigger) |
||
| 174 | * @return int <0 if KO, >0 if OK |
||
| 175 | */ |
||
| 176 | public function update($user, $notrigger = 0) |
||
| 177 | { |
||
| 178 | global $conf, $langs; |
||
| 179 | |||
| 180 | $error=0; |
||
| 181 | |||
| 182 | // Clean parameters |
||
| 183 | $this->amount=trim($this->amount); |
||
| 184 | $this->label=trim($this->label); |
||
| 185 | $this->note=trim($this->note); |
||
|
1 ignored issue
–
show
|
|||
| 186 | $this->fk_bank = (int) $this->fk_bank; |
||
| 187 | $this->fk_user_creat = (int) $this->fk_user_creat; |
||
| 188 | $this->fk_user_modif = (int) $this->fk_user_modif; |
||
| 189 | |||
| 190 | // Check parameters |
||
| 191 | // Put here code to add control on parameters values |
||
| 192 | |||
| 193 | $this->db->begin(); |
||
| 194 | |||
| 195 | // Update request |
||
| 196 | $sql = "UPDATE ".MAIN_DB_PREFIX."tva SET"; |
||
| 197 | $sql.= " tms='".$this->db->idate($this->tms)."',"; |
||
| 198 | $sql.= " datep='".$this->db->idate($this->datep)."',"; |
||
| 199 | $sql.= " datev='".$this->db->idate($this->datev)."',"; |
||
| 200 | $sql.= " amount=".price2num($this->amount).","; |
||
| 201 | $sql.= " label='".$this->db->escape($this->label)."',"; |
||
| 202 | $sql.= " note='".$this->db->escape($this->note)."',"; |
||
|
1 ignored issue
–
show
|
|||
| 203 | $sql.= " fk_bank=".$this->fk_bank.","; |
||
| 204 | $sql.= " fk_user_creat=".$this->fk_user_creat.","; |
||
| 205 | $sql.= " fk_user_modif=".($this->fk_user_modif>0?$this->fk_user_modif:$user->id).""; |
||
| 206 | $sql.= " WHERE rowid=".$this->id; |
||
| 207 | |||
| 208 | dol_syslog(get_class($this)."::update", LOG_DEBUG); |
||
| 209 | $resql = $this->db->query($sql); |
||
| 210 | if (! $resql) |
||
| 211 | { |
||
| 212 | $this->error="Error ".$this->db->lasterror(); |
||
| 213 | $error++; |
||
| 214 | } |
||
| 215 | |||
| 216 | if (! $error && ! $notrigger) |
||
| 217 | { |
||
| 218 | // Call trigger |
||
| 219 | $result=$this->call_trigger('TVA_MODIFY', $user); |
||
| 220 | if ($result < 0) $error++; |
||
| 221 | // End call triggers |
||
| 222 | } |
||
| 223 | |||
| 224 | if (! $error) |
||
| 225 | { |
||
| 226 | $this->db->commit(); |
||
| 227 | return 1; |
||
| 228 | } |
||
| 229 | else |
||
| 230 | { |
||
| 231 | $this->db->rollback(); |
||
| 232 | return -1; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * Load object in memory from database |
||
| 239 | * |
||
| 240 | * @param int $id id object |
||
| 241 | * @param User $user User that load |
||
| 242 | * @return int <0 if KO, >0 if OK |
||
| 243 | */ |
||
| 244 | public function fetch($id, $user = null) |
||
| 245 | { |
||
| 246 | global $langs; |
||
| 247 | $sql = "SELECT"; |
||
| 248 | $sql.= " t.rowid,"; |
||
| 249 | |||
| 250 | $sql.= " t.tms,"; |
||
| 251 | $sql.= " t.datep,"; |
||
| 252 | $sql.= " t.datev,"; |
||
| 253 | $sql.= " t.amount,"; |
||
| 254 | $sql.= " t.fk_typepayment,"; |
||
| 255 | $sql.= " t.num_payment,"; |
||
| 256 | $sql.= " t.label,"; |
||
| 257 | $sql.= " t.note,"; |
||
| 258 | $sql.= " t.fk_bank,"; |
||
| 259 | $sql.= " t.fk_user_creat,"; |
||
| 260 | $sql.= " t.fk_user_modif,"; |
||
| 261 | $sql.= " b.fk_account,"; |
||
| 262 | $sql.= " b.fk_type,"; |
||
| 263 | $sql.= " b.rappro"; |
||
| 264 | |||
| 265 | $sql.= " FROM ".MAIN_DB_PREFIX."tva as t"; |
||
| 266 | $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid"; |
||
| 267 | $sql.= " WHERE t.rowid = ".$id; |
||
| 268 | |||
| 269 | dol_syslog(get_class($this)."::fetch", LOG_DEBUG); |
||
| 270 | $resql=$this->db->query($sql); |
||
| 271 | if ($resql) |
||
| 272 | { |
||
| 273 | if ($this->db->num_rows($resql)) |
||
| 274 | { |
||
| 275 | $obj = $this->db->fetch_object($resql); |
||
| 276 | |||
| 277 | $this->id = $obj->rowid; |
||
| 278 | $this->ref = $obj->rowid; |
||
| 279 | $this->tms = $this->db->jdate($obj->tms); |
||
| 280 | $this->datep = $this->db->jdate($obj->datep); |
||
| 281 | $this->datev = $this->db->jdate($obj->datev); |
||
| 282 | $this->amount = $obj->amount; |
||
| 283 | $this->type_payment = $obj->fk_typepayment; |
||
| 284 | $this->num_payment = $obj->num_payment; |
||
| 285 | $this->label = $obj->label; |
||
| 286 | $this->note = $obj->note; |
||
| 287 | $this->fk_bank = $obj->fk_bank; |
||
| 288 | $this->fk_user_creat = $obj->fk_user_creat; |
||
| 289 | $this->fk_user_modif = $obj->fk_user_modif; |
||
| 290 | $this->fk_account = $obj->fk_account; |
||
| 291 | $this->fk_type = $obj->fk_type; |
||
| 292 | $this->rappro = $obj->rappro; |
||
| 293 | } |
||
| 294 | $this->db->free($resql); |
||
| 295 | |||
| 296 | return 1; |
||
| 297 | } |
||
| 298 | else |
||
| 299 | { |
||
| 300 | $this->error="Error ".$this->db->lasterror(); |
||
| 301 | return -1; |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * Delete object in database |
||
| 308 | * |
||
| 309 | * @param User $user User that delete |
||
| 310 | * @return int <0 if KO, >0 if OK |
||
| 311 | */ |
||
| 312 | public function delete($user) |
||
| 313 | { |
||
| 314 | global $conf, $langs; |
||
| 315 | |||
| 316 | $error=0; |
||
| 317 | |||
| 318 | // Call trigger |
||
| 319 | $result=$this->call_trigger('TVA_DELETE', $user); |
||
| 320 | if ($result < 0) return -1; |
||
| 321 | // End call triggers |
||
| 322 | |||
| 323 | $sql = "DELETE FROM ".MAIN_DB_PREFIX."tva"; |
||
| 324 | $sql.= " WHERE rowid=".$this->id; |
||
| 325 | |||
| 326 | dol_syslog(get_class($this)."::delete", LOG_DEBUG); |
||
| 327 | $resql = $this->db->query($sql); |
||
| 328 | if (! $resql) |
||
| 329 | { |
||
| 330 | $this->error="Error ".$this->db->lasterror(); |
||
| 331 | return -1; |
||
| 332 | } |
||
| 333 | |||
| 334 | |||
| 335 | return 1; |
||
| 336 | } |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * Initialise an instance with random values. |
||
| 341 | * Used to build previews or test instances. |
||
| 342 | * id must be 0 if object instance is a specimen. |
||
| 343 | * |
||
| 344 | * @return void |
||
| 345 | */ |
||
| 346 | public function initAsSpecimen() |
||
| 347 | { |
||
| 348 | $this->id=0; |
||
| 349 | |||
| 350 | $this->tms=''; |
||
| 351 | $this->datep=''; |
||
| 352 | $this->datev=''; |
||
| 353 | $this->amount=''; |
||
| 354 | $this->label=''; |
||
| 355 | $this->note=''; |
||
|
1 ignored issue
–
show
|
|||
| 356 | $this->fk_bank=''; |
||
|
1 ignored issue
–
show
|
|||
| 357 | $this->fk_user_creat=''; |
||
|
1 ignored issue
–
show
|
|||
| 358 | $this->fk_user_modif=''; |
||
|
1 ignored issue
–
show
|
|||
| 359 | } |
||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * Balance of VAT |
||
| 364 | * |
||
| 365 | * @param int $year Year |
||
| 366 | * @return double Amount |
||
| 367 | */ |
||
| 368 | public function solde($year = 0) |
||
| 369 | { |
||
| 370 | |||
| 371 | $reglee = $this->tva_sum_reglee($year); |
||
| 372 | |||
| 373 | $payee = $this->tva_sum_payee($year); |
||
| 374 | $collectee = $this->tva_sum_collectee($year); |
||
| 375 | |||
| 376 | $solde = $reglee - ($collectee - $payee); |
||
| 377 | |||
| 378 | return $solde; |
||
| 379 | } |
||
| 380 | |||
| 381 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 382 | /** |
||
| 383 | * Total of the VAT from invoices emitted by the thirdparty. |
||
| 384 | * |
||
| 385 | * @param int $year Year |
||
| 386 | * @return double Amount |
||
| 387 | */ |
||
| 388 | public function tva_sum_collectee($year = 0) |
||
| 389 | { |
||
| 390 | // phpcs:enable |
||
| 391 | |||
| 392 | $sql = "SELECT sum(f.tva) as amount"; |
||
| 393 | $sql .= " FROM ".MAIN_DB_PREFIX."facture as f WHERE f.paye = 1"; |
||
| 394 | if ($year) |
||
| 395 | { |
||
| 396 | $sql .= " AND f.datef >= '".$year."-01-01' AND f.datef <= '".$year."-12-31' "; |
||
| 397 | } |
||
| 398 | |||
| 399 | $result = $this->db->query($sql); |
||
| 400 | if ($result) |
||
| 401 | { |
||
| 402 | if ($this->db->num_rows($result)) |
||
| 403 | { |
||
| 404 | $obj = $this->db->fetch_object($result); |
||
| 405 | $ret = $obj->amount; |
||
| 406 | $this->db->free($result); |
||
| 407 | return $ret; |
||
| 408 | } |
||
| 409 | else |
||
| 410 | { |
||
| 411 | $this->db->free($result); |
||
| 412 | return 0; |
||
| 413 | } |
||
| 414 | } |
||
| 415 | else |
||
| 416 | { |
||
| 417 | print $this->db->lasterror(); |
||
| 418 | return -1; |
||
| 419 | } |
||
| 420 | } |
||
| 421 | |||
| 422 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 423 | /** |
||
| 424 | * VAT payed |
||
| 425 | * |
||
| 426 | * @param int $year Year |
||
| 427 | * @return double Amount |
||
| 428 | */ |
||
| 429 | public function tva_sum_payee($year = 0) |
||
| 430 | { |
||
| 431 | // phpcs:enable |
||
| 432 | |||
| 433 | $sql = "SELECT sum(f.total_tva) as total_tva"; |
||
| 434 | $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f"; |
||
| 435 | if ($year) |
||
| 436 | { |
||
| 437 | $sql .= " WHERE f.datef >= '".$year."-01-01' AND f.datef <= '".$year."-12-31' "; |
||
| 438 | } |
||
| 439 | |||
| 440 | $result = $this->db->query($sql); |
||
| 441 | if ($result) |
||
| 442 | { |
||
| 443 | if ($this->db->num_rows($result)) |
||
| 444 | { |
||
| 445 | $obj = $this->db->fetch_object($result); |
||
| 446 | $ret = $obj->total_tva; |
||
| 447 | $this->db->free($result); |
||
| 448 | return $ret; |
||
| 449 | } |
||
| 450 | else |
||
| 451 | { |
||
| 452 | $this->db->free($result); |
||
| 453 | return 0; |
||
| 454 | } |
||
| 455 | } |
||
| 456 | else |
||
| 457 | { |
||
| 458 | print $this->db->lasterror(); |
||
| 459 | return -1; |
||
| 460 | } |
||
| 461 | } |
||
| 462 | |||
| 463 | |||
| 464 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 465 | /** |
||
| 466 | * Total of the VAT payed |
||
| 467 | * |
||
| 468 | * @param int $year Year |
||
| 469 | * @return double Amount |
||
| 470 | */ |
||
| 471 | public function tva_sum_reglee($year = 0) |
||
| 472 | { |
||
| 473 | // phpcs:enable |
||
| 474 | |||
| 475 | $sql = "SELECT sum(f.amount) as amount"; |
||
| 476 | $sql .= " FROM ".MAIN_DB_PREFIX."tva as f"; |
||
| 477 | |||
| 478 | if ($year) |
||
| 479 | { |
||
| 480 | $sql .= " WHERE f.datev >= '".$year."-01-01' AND f.datev <= '".$year."-12-31' "; |
||
| 481 | } |
||
| 482 | |||
| 483 | $result = $this->db->query($sql); |
||
| 484 | if ($result) |
||
| 485 | { |
||
| 486 | if ($this->db->num_rows($result)) |
||
| 487 | { |
||
| 488 | $obj = $this->db->fetch_object($result); |
||
| 489 | $ret = $obj->amount; |
||
| 490 | $this->db->free($result); |
||
| 491 | return $ret; |
||
| 492 | } |
||
| 493 | else |
||
| 494 | { |
||
| 495 | $this->db->free($result); |
||
| 496 | return 0; |
||
| 497 | } |
||
| 498 | } |
||
| 499 | else |
||
| 500 | { |
||
| 501 | print $this->db->lasterror(); |
||
| 502 | return -1; |
||
| 503 | } |
||
| 504 | } |
||
| 505 | |||
| 506 | |||
| 507 | /** |
||
| 508 | * Create in database |
||
| 509 | * |
||
| 510 | * @param User $user Object user that insert |
||
| 511 | * @return int <0 if KO, rowid in tva table if OK |
||
| 512 | */ |
||
| 513 | public function addPayment($user) |
||
| 514 | { |
||
| 515 | global $conf,$langs; |
||
| 516 | |||
| 517 | $this->db->begin(); |
||
| 518 | |||
| 519 | // Clean parameters |
||
| 520 | $this->amount=price2num(trim($this->amount)); |
||
| 521 | $this->label=trim($this->label); |
||
| 522 | $this->note=trim($this->note); |
||
|
1 ignored issue
–
show
|
|||
| 523 | $this->fk_bank = (int) $this->fk_bank; |
||
| 524 | $this->fk_user_creat = (int) $this->fk_user_creat; |
||
| 525 | $this->fk_user_modif = (int) $this->fk_user_modif; |
||
| 526 | if (empty($this->datec)) $this->datec = dol_now(); |
||
| 527 | |||
| 528 | // Check parameters |
||
| 529 | if (! $this->label) |
||
| 530 | { |
||
| 531 | $this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")); |
||
| 532 | return -3; |
||
| 533 | } |
||
| 534 | if ($this->amount == '') |
||
| 535 | { |
||
| 536 | $this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")); |
||
| 537 | return -4; |
||
| 538 | } |
||
| 539 | if (! empty($conf->banque->enabled) && (empty($this->accountid) || $this->accountid <= 0)) |
||
| 540 | { |
||
| 541 | $this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentities("Account")); |
||
| 542 | return -5; |
||
| 543 | } |
||
| 544 | if (! empty($conf->banque->enabled) && (empty($this->type_payment) || $this->type_payment <= 0)) |
||
| 545 | { |
||
| 546 | $this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")); |
||
| 547 | return -5; |
||
| 548 | } |
||
| 549 | |||
| 550 | // Insert into llx_tva |
||
| 551 | $sql = "INSERT INTO ".MAIN_DB_PREFIX."tva ("; |
||
| 552 | $sql.= "datec"; |
||
| 553 | $sql.= ", datep"; |
||
| 554 | $sql.= ", datev"; |
||
| 555 | $sql.= ", amount"; |
||
| 556 | $sql.= ", fk_typepayment"; |
||
| 557 | $sql.= ", num_payment"; |
||
| 558 | if ($this->note) $sql.= ", note"; |
||
|
1 ignored issue
–
show
|
|||
| 559 | if ($this->label) $sql.= ", label"; |
||
| 560 | $sql.= ", fk_user_creat"; |
||
| 561 | $sql.= ", fk_bank"; |
||
| 562 | $sql.= ", entity"; |
||
| 563 | $sql.= ") "; |
||
| 564 | $sql.= " VALUES ("; |
||
| 565 | $sql.= " '".$this->db->idate($this->datec)."'"; |
||
| 566 | $sql.= ", '".$this->db->idate($this->datep)."'"; |
||
| 567 | $sql.= ", '".$this->db->idate($this->datev)."'"; |
||
| 568 | $sql.= ", ".$this->amount; |
||
| 569 | $sql.= ", '".$this->db->escape($this->type_payment)."'"; |
||
| 570 | $sql.= ", '".$this->db->escape($this->num_payment)."'"; |
||
| 571 | if ($this->note) $sql.=", '".$this->db->escape($this->note)."'"; |
||
|
1 ignored issue
–
show
|
|||
| 572 | if ($this->label) $sql.=", '".$this->db->escape($this->label)."'"; |
||
| 573 | $sql.= ", '".$this->db->escape($user->id)."'"; |
||
| 574 | $sql.= ", NULL"; |
||
| 575 | $sql.= ", ".$conf->entity; |
||
| 576 | $sql.= ")"; |
||
| 577 | |||
| 578 | dol_syslog(get_class($this)."::addPayment", LOG_DEBUG); |
||
| 579 | $result = $this->db->query($sql); |
||
| 580 | if ($result) |
||
| 581 | { |
||
| 582 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."tva"); // TODO should be called 'payment_vat' |
||
| 583 | |||
| 584 | // Call trigger |
||
| 585 | //XXX: Should be done just befor commit no ? |
||
| 586 | $result=$this->call_trigger('TVA_ADDPAYMENT', $user); |
||
| 587 | if ($result < 0) |
||
| 588 | { |
||
| 589 | $this->id = 0; |
||
| 590 | $ok = 0; |
||
| 591 | } |
||
| 592 | // End call triggers |
||
| 593 | |||
| 594 | if ($this->id > 0) |
||
| 595 | { |
||
| 596 | $ok=1; |
||
| 597 | if (! empty($conf->banque->enabled) && ! empty($this->amount)) |
||
| 598 | { |
||
| 599 | // Insert into llx_bank |
||
| 600 | require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; |
||
| 601 | |||
| 602 | $acc = new Account($this->db); |
||
| 603 | $result=$acc->fetch($this->accountid); |
||
| 604 | if ($result <= 0) dol_print_error($this->db); |
||
| 605 | |||
| 606 | if ($this->amount > 0) { |
||
| 607 | $bank_line_id = $acc->addline($this->datep, $this->type_payment, $this->label, -abs($this->amount), '', '', $user); |
||
| 608 | } else { |
||
| 609 | $bank_line_id = $acc->addline($this->datep, $this->type_payment, $this->label, abs($this->amount), '', '', $user); |
||
| 610 | } |
||
| 611 | |||
| 612 | // Update fk_bank into llx_tva. So we know vat line used to generate bank transaction |
||
| 613 | if ($bank_line_id > 0) |
||
| 614 | { |
||
| 615 | $this->update_fk_bank($bank_line_id); |
||
| 616 | } |
||
| 617 | else |
||
| 618 | { |
||
| 619 | $this->error=$acc->error; |
||
| 620 | $ok=0; |
||
| 621 | } |
||
| 622 | |||
| 623 | // Update links |
||
| 624 | $result=$acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/tva/card.php?id=', "(VATPayment)", "payment_vat"); |
||
| 625 | if ($result < 0) |
||
| 626 | { |
||
| 627 | $this->error=$acc->error; |
||
| 628 | $ok=0; |
||
| 629 | } |
||
| 630 | } |
||
| 631 | |||
| 632 | if ($ok) |
||
| 633 | { |
||
| 634 | $this->db->commit(); |
||
| 635 | return $this->id; |
||
| 636 | } |
||
| 637 | else |
||
| 638 | { |
||
| 639 | $this->db->rollback(); |
||
| 640 | return -3; |
||
| 641 | } |
||
| 642 | } |
||
| 643 | else |
||
| 644 | { |
||
| 645 | $this->db->rollback(); |
||
| 646 | return -2; |
||
| 647 | } |
||
| 648 | } |
||
| 649 | else |
||
| 650 | { |
||
| 651 | $this->error=$this->db->error(); |
||
| 652 | $this->db->rollback(); |
||
| 653 | return -1; |
||
| 654 | } |
||
| 655 | } |
||
| 656 | |||
| 657 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 658 | /** |
||
| 659 | * Update link between payment tva and line generate into llx_bank |
||
| 660 | * |
||
| 661 | * @param int $id_bank Id bank account |
||
| 662 | * @return int <0 if KO, >0 if OK |
||
| 663 | */ |
||
| 664 | public function update_fk_bank($id_bank) |
||
| 665 | { |
||
| 666 | // phpcs:enable |
||
| 667 | $sql = 'UPDATE '.MAIN_DB_PREFIX.'tva SET fk_bank = '.(int) $id_bank; |
||
| 668 | $sql.= ' WHERE rowid = '.(int) $this->id; |
||
| 669 | $result = $this->db->query($sql); |
||
| 670 | if ($result) |
||
| 671 | { |
||
| 672 | return 1; |
||
| 673 | } |
||
| 674 | else |
||
| 675 | { |
||
| 676 | dol_print_error($this->db); |
||
| 677 | return -1; |
||
| 678 | } |
||
| 679 | } |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Send name clicable (with possibly the picto) |
||
| 683 | * |
||
| 684 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
||
| 685 | * @param string $option link option |
||
| 686 | * @param int $notooltip 1=Disable tooltip |
||
| 687 | * @param string $morecss More CSS |
||
| 688 | * @return string Chaine with URL |
||
| 689 | */ |
||
| 690 | public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '') |
||
| 729 | } |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Return amount of payments already done |
||
| 733 | * |
||
| 734 | * @return int Amount of payment already done, <0 if KO |
||
| 735 | */ |
||
| 736 | public function getSommePaiement() |
||
| 737 | { |
||
| 738 | $table='paiementcharge'; |
||
| 739 | $field='fk_charge'; |
||
| 740 | |||
| 741 | $sql = 'SELECT sum(amount) as amount'; |
||
| 742 | $sql.= ' FROM '.MAIN_DB_PREFIX.$table; |
||
| 743 | $sql.= ' WHERE '.$field.' = '.$this->id; |
||
| 744 | |||
| 745 | dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG); |
||
| 746 | $resql=$this->db->query($sql); |
||
| 747 | if ($resql) |
||
| 748 | { |
||
| 749 | $amount=0; |
||
| 750 | |||
| 751 | $obj = $this->db->fetch_object($resql); |
||
| 752 | if ($obj) $amount=$obj->amount?$obj->amount:0; |
||
| 753 | |||
| 754 | $this->db->free($resql); |
||
| 755 | return $amount; |
||
| 756 | } |
||
| 757 | else |
||
| 758 | { |
||
| 759 | return -1; |
||
| 760 | } |
||
| 761 | } |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Informations of vat payment object |
||
| 765 | * |
||
| 766 | * @param int $id Id of vat payment |
||
| 767 | * @return int <0 if KO, >0 if OK |
||
| 768 | */ |
||
| 769 | public function info($id) |
||
| 770 | { |
||
| 771 | $sql = "SELECT t.rowid, t.tms, t.fk_user_modif, t.datec, t.fk_user_creat"; |
||
| 772 | $sql.= " FROM ".MAIN_DB_PREFIX."tva as t"; |
||
| 773 | $sql.= " WHERE t.rowid = ".(int) $id; |
||
| 774 | |||
| 775 | dol_syslog(get_class($this)."::info", LOG_DEBUG); |
||
| 776 | $result=$this->db->query($sql); |
||
| 777 | if ($result) |
||
| 778 | { |
||
| 779 | if ($this->db->num_rows($result)) |
||
| 780 | { |
||
| 781 | $obj = $this->db->fetch_object($result); |
||
| 782 | |||
| 783 | $this->id = $obj->rowid; |
||
| 784 | |||
| 785 | if ($obj->fk_user_creat) { |
||
| 786 | $cuser = new User($this->db); |
||
| 787 | $cuser->fetch($obj->fk_user_creat); |
||
| 788 | $this->user_creation = $cuser; |
||
| 789 | } |
||
| 790 | |||
| 791 | if ($obj->fk_user_modif) { |
||
| 792 | $muser = new User($this->db); |
||
| 793 | $muser->fetch($obj->fk_user_modif); |
||
| 794 | $this->user_modification = $muser; |
||
| 795 | } |
||
| 796 | |||
| 797 | $this->date_creation = $this->db->jdate($obj->datec); |
||
| 798 | $this->date_modification = $this->db->jdate($obj->tms); |
||
| 799 | $this->import_key = $obj->import_key; |
||
| 800 | } |
||
| 801 | |||
| 802 | $this->db->free($result); |
||
| 803 | } |
||
| 804 | else |
||
| 805 | { |
||
| 806 | dol_print_error($this->db); |
||
| 807 | } |
||
| 808 | } |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee) |
||
| 812 | * |
||
| 813 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
| 814 | * @return string Libelle |
||
| 815 | */ |
||
| 816 | public function getLibStatut($mode = 0) |
||
| 819 | } |
||
| 820 | |||
| 821 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 822 | /** |
||
| 823 | * Renvoi le libelle d'un statut donne |
||
| 824 | * |
||
| 825 | * @param int $status Statut |
||
| 826 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
| 827 | * @return string Libelle du statut |
||
| 828 | */ |
||
| 829 | public function LibStatut($status, $mode = 0) |
||
| 835 | } |
||
| 836 | } |
||
| 837 |