| Total Complexity | 98 |
| Total Lines | 806 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PaymentSocialContribution 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 PaymentSocialContribution, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class PaymentSocialContribution extends CommonObject |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var string ID to identify managed object |
||
| 42 | */ |
||
| 43 | public $element = 'paiementcharge'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string Name of table without prefix where object is stored |
||
| 47 | */ |
||
| 48 | public $table_element = 'paiementcharge'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
| 52 | */ |
||
| 53 | public $picto = 'payment'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string Label |
||
| 57 | */ |
||
| 58 | public $label; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var int ID |
||
| 62 | */ |
||
| 63 | public $fk_charge; |
||
| 64 | |||
| 65 | public $datec = ''; |
||
| 66 | public $datep = ''; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | public $type_code; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | public $type_label; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var int |
||
| 80 | */ |
||
| 81 | public $bank_account; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | public $bank_line; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @deprecated Use $amount instead. |
||
| 90 | * @see $amount |
||
| 91 | * @var float|int |
||
| 92 | */ |
||
| 93 | public $total; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var float|int |
||
| 97 | */ |
||
| 98 | public $amount; // Total amount of payment |
||
| 99 | /** |
||
| 100 | * @var array<float|int> |
||
| 101 | */ |
||
| 102 | public $amounts = array(); // Array of amounts |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var int ID |
||
| 106 | */ |
||
| 107 | public $fk_typepaiement; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var string |
||
| 111 | * @deprecated Use $num_payment instead |
||
| 112 | * @see $num_payment |
||
| 113 | */ |
||
| 114 | public $num_paiement; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var string Payment reference |
||
| 118 | * (Cheque or bank transfer reference. Can be "ABC123") |
||
| 119 | */ |
||
| 120 | public $num_payment; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var int ID |
||
| 124 | */ |
||
| 125 | public $fk_bank; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var int ID |
||
| 129 | */ |
||
| 130 | public $fk_user_creat; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var int ID |
||
| 134 | */ |
||
| 135 | public $fk_user_modif; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var int ID |
||
| 139 | */ |
||
| 140 | public $chid; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var integer|string datepaye |
||
| 144 | */ |
||
| 145 | public $datepaye; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var integer|string paiementtype |
||
| 149 | */ |
||
| 150 | public $paiementtype; |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * Constructor |
||
| 155 | * |
||
| 156 | * @param DoliDB $db Database handler |
||
| 157 | */ |
||
| 158 | public function __construct(DoliDB $db) |
||
|
|
|||
| 159 | { |
||
| 160 | $this->db = $db; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Create payment of social contribution into database. |
||
| 165 | * Use this->amounts to have list of lines for the payment |
||
| 166 | * |
||
| 167 | * @param User $user User making payment |
||
| 168 | * @param int $closepaidcontrib 1=Also close paid contributions to paid, 0=Do nothing more |
||
| 169 | * @return int Return integer <0 if KO, id of payment if OK |
||
| 170 | */ |
||
| 171 | public function create($user, $closepaidcontrib = 0) |
||
| 172 | { |
||
| 173 | global $conf, $langs; |
||
| 174 | |||
| 175 | $error = 0; |
||
| 176 | |||
| 177 | $now = dol_now(); |
||
| 178 | |||
| 179 | dol_syslog(get_class($this) . "::create", LOG_DEBUG); |
||
| 180 | |||
| 181 | // Validate parameters |
||
| 182 | if (!$this->datepaye) { |
||
| 183 | $this->error = 'ErrorBadValueForParameterCreatePaymentSocialContrib'; |
||
| 184 | return -1; |
||
| 185 | } |
||
| 186 | |||
| 187 | // Clean parameters |
||
| 188 | if (isset($this->fk_charge)) { |
||
| 189 | $this->fk_charge = (int) $this->fk_charge; |
||
| 190 | } |
||
| 191 | if (isset($this->amount)) { |
||
| 192 | $this->amount = (float) $this->amount; |
||
| 193 | } |
||
| 194 | if (isset($this->fk_typepaiement)) { |
||
| 195 | $this->fk_typepaiement = (int) $this->fk_typepaiement; |
||
| 196 | } |
||
| 197 | if (isset($this->num_payment)) { |
||
| 198 | $this->num_payment = trim($this->num_payment); |
||
| 199 | } |
||
| 200 | if (isset($this->note_private)) { |
||
| 201 | $this->note_private = trim($this->note_private); |
||
| 202 | } |
||
| 203 | if (isset($this->fk_bank)) { |
||
| 204 | $this->fk_bank = (int) $this->fk_bank; |
||
| 205 | } |
||
| 206 | if (isset($this->fk_user_creat)) { |
||
| 207 | $this->fk_user_creat = (int) $this->fk_user_creat; |
||
| 208 | } |
||
| 209 | if (isset($this->fk_user_modif)) { |
||
| 210 | $this->fk_user_modif = (int) $this->fk_user_modif; |
||
| 211 | } |
||
| 212 | |||
| 213 | $totalamount = 0; |
||
| 214 | foreach ($this->amounts as $key => $value) { // How payment is dispatch |
||
| 215 | $newvalue = (float) price2num($value, 'MT'); |
||
| 216 | $this->amounts[$key] = $newvalue; |
||
| 217 | $totalamount += $newvalue; |
||
| 218 | } |
||
| 219 | $totalamount = (float) price2num($totalamount); |
||
| 220 | |||
| 221 | // Check parameters |
||
| 222 | if ($totalamount == 0) { |
||
| 223 | return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null |
||
| 224 | } |
||
| 225 | |||
| 226 | |||
| 227 | $this->db->begin(); |
||
| 228 | |||
| 229 | if ($totalamount != 0) { |
||
| 230 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "paiementcharge (fk_charge, datec, datep, amount,"; |
||
| 231 | $sql .= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)"; |
||
| 232 | $sql .= " VALUES ($this->chid, '" . $this->db->idate($now) . "',"; |
||
| 233 | $sql .= " '" . $this->db->idate($this->datepaye) . "',"; |
||
| 234 | $sql .= " " . ((float) $totalamount) . ","; |
||
| 235 | $sql .= " " . ((int) $this->paiementtype) . ", '" . $this->db->escape($this->num_payment) . "', '" . $this->db->escape($this->note) . "', " . $user->id . ","; |
||
| 236 | $sql .= " 0)"; |
||
| 237 | |||
| 238 | $resql = $this->db->query($sql); |
||
| 239 | if ($resql) { |
||
| 240 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "paiementcharge"); |
||
| 241 | |||
| 242 | // Insere tableau des montants / factures |
||
| 243 | foreach ($this->amounts as $key => $amount) { |
||
| 244 | $contribid = $key; |
||
| 245 | if (is_numeric($amount) && $amount != 0) { |
||
| 246 | $amount = (float) price2num($amount); |
||
| 247 | |||
| 248 | // If we want to closed paid invoices |
||
| 249 | if ($closepaidcontrib) { |
||
| 250 | $contrib = new ChargeSociales($this->db); |
||
| 251 | $contrib->fetch($contribid); |
||
| 252 | $paiement = $contrib->getSommePaiement(); |
||
| 253 | //$creditnotes=$contrib->getSumCreditNotesUsed(); |
||
| 254 | $creditnotes = 0; |
||
| 255 | //$deposits=$contrib->getSumDepositsUsed(); |
||
| 256 | $deposits = 0; |
||
| 257 | $alreadypayed = (float) price2num($paiement + $creditnotes + $deposits, 'MT'); |
||
| 258 | $remaintopay = (float) price2num($contrib->amount - $paiement - $creditnotes - $deposits, 'MT'); |
||
| 259 | if ($remaintopay == 0) { |
||
| 260 | $result = $contrib->setPaid($user); |
||
| 261 | } else { |
||
| 262 | dol_syslog("Remain to pay for conrib " . $contribid . " not null. We do nothing."); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } else { |
||
| 268 | $error++; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | $result = $this->call_trigger('PAYMENTSOCIALCONTRIBUTION_CREATE', $user); |
||
| 273 | if ($result < 0) { |
||
| 274 | $error++; |
||
| 275 | } |
||
| 276 | |||
| 277 | if ($totalamount != 0 && !$error) { |
||
| 278 | $this->amount = $totalamount; |
||
| 279 | $this->total = $totalamount; // deprecated |
||
| 280 | $this->db->commit(); |
||
| 281 | return $this->id; |
||
| 282 | } else { |
||
| 283 | $this->error = $this->db->error(); |
||
| 284 | $this->db->rollback(); |
||
| 285 | return -1; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Load object in memory from database |
||
| 291 | * |
||
| 292 | * @param int $id Id object |
||
| 293 | * @return int Return integer <0 if KO, >0 if OK |
||
| 294 | */ |
||
| 295 | public function fetch($id) |
||
| 296 | { |
||
| 297 | global $langs; |
||
| 298 | $sql = "SELECT"; |
||
| 299 | $sql .= " t.rowid,"; |
||
| 300 | $sql .= " t.fk_charge,"; |
||
| 301 | $sql .= " t.datec,"; |
||
| 302 | $sql .= " t.tms,"; |
||
| 303 | $sql .= " t.datep,"; |
||
| 304 | $sql .= " t.amount,"; |
||
| 305 | $sql .= " t.fk_typepaiement,"; |
||
| 306 | $sql .= " t.num_paiement as num_payment,"; |
||
| 307 | $sql .= " t.note,"; |
||
| 308 | $sql .= " t.fk_bank,"; |
||
| 309 | $sql .= " t.fk_user_creat,"; |
||
| 310 | $sql .= " t.fk_user_modif,"; |
||
| 311 | $sql .= " pt.code as type_code, pt.libelle as type_label,"; |
||
| 312 | $sql .= ' b.fk_account'; |
||
| 313 | $sql .= " FROM " . MAIN_DB_PREFIX . "paiementcharge as t LEFT JOIN " . MAIN_DB_PREFIX . "c_paiement as pt ON t.fk_typepaiement = pt.id"; |
||
| 314 | $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank as b ON t.fk_bank = b.rowid'; |
||
| 315 | $sql .= " WHERE t.rowid = " . ((int) $id); |
||
| 316 | // TODO link on entity of tax; |
||
| 317 | |||
| 318 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
||
| 319 | $resql = $this->db->query($sql); |
||
| 320 | if ($resql) { |
||
| 321 | if ($this->db->num_rows($resql)) { |
||
| 322 | $obj = $this->db->fetch_object($resql); |
||
| 323 | |||
| 324 | $this->id = $obj->rowid; |
||
| 325 | $this->ref = $obj->rowid; |
||
| 326 | |||
| 327 | $this->fk_charge = $obj->fk_charge; |
||
| 328 | $this->datec = $this->db->jdate($obj->datec); |
||
| 329 | $this->tms = $this->db->jdate($obj->tms); |
||
| 330 | $this->datep = $this->db->jdate($obj->datep); |
||
| 331 | $this->amount = $obj->amount; |
||
| 332 | $this->total = $obj->amount; |
||
| 333 | $this->fk_typepaiement = $obj->fk_typepaiement; |
||
| 334 | $this->num_payment = $obj->num_payment; |
||
| 335 | $this->num_paiement = $obj->num_payment; |
||
| 336 | $this->note_private = $obj->note; |
||
| 337 | $this->fk_bank = $obj->fk_bank; |
||
| 338 | $this->fk_user_creat = $obj->fk_user_creat; |
||
| 339 | $this->fk_user_modif = $obj->fk_user_modif; |
||
| 340 | |||
| 341 | $this->type_code = $obj->type_code; |
||
| 342 | $this->type_label = $obj->type_label; |
||
| 343 | |||
| 344 | $this->bank_account = $obj->fk_account; |
||
| 345 | $this->bank_line = $obj->fk_bank; |
||
| 346 | } |
||
| 347 | $this->db->free($resql); |
||
| 348 | |||
| 349 | return 1; |
||
| 350 | } else { |
||
| 351 | $this->error = "Error " . $this->db->lasterror(); |
||
| 352 | return -1; |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * Update database |
||
| 359 | * |
||
| 360 | * @param User $user User that modify |
||
| 361 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 362 | * @return int Return integer <0 if KO, >0 if OK |
||
| 363 | */ |
||
| 364 | public function update($user = null, $notrigger = 0) |
||
| 365 | { |
||
| 366 | global $conf, $langs; |
||
| 367 | $error = 0; |
||
| 368 | |||
| 369 | // Clean parameters |
||
| 370 | |||
| 371 | if (isset($this->fk_charge)) { |
||
| 372 | $this->fk_charge = (int) $this->fk_charge; |
||
| 373 | } |
||
| 374 | if (isset($this->amount)) { |
||
| 375 | $this->amount = (float) $this->amount; |
||
| 376 | } |
||
| 377 | if (isset($this->fk_typepaiement)) { |
||
| 378 | $this->fk_typepaiement = (int) $this->fk_typepaiement; |
||
| 379 | } |
||
| 380 | if (isset($this->num_payment)) { |
||
| 381 | $this->num_payment = trim($this->num_payment); |
||
| 382 | } |
||
| 383 | if (isset($this->note_private)) { |
||
| 384 | $this->note_private = trim($this->note_private); |
||
| 385 | } |
||
| 386 | if (isset($this->fk_bank)) { |
||
| 387 | $this->fk_bank = (int) $this->fk_bank; |
||
| 388 | } |
||
| 389 | if (isset($this->fk_user_creat)) { |
||
| 390 | $this->fk_user_creat = (int) $this->fk_user_creat; |
||
| 391 | } |
||
| 392 | if (isset($this->fk_user_modif)) { |
||
| 393 | $this->fk_user_modif = (int) $this->fk_user_modif; |
||
| 394 | } |
||
| 395 | |||
| 396 | |||
| 397 | |||
| 398 | // Check parameters |
||
| 399 | // Put here code to add control on parameters values |
||
| 400 | |||
| 401 | // Update request |
||
| 402 | $sql = "UPDATE " . MAIN_DB_PREFIX . "paiementcharge SET"; |
||
| 403 | $sql .= " fk_charge=" . (isset($this->fk_charge) ? ((int) $this->fk_charge) : "null") . ","; |
||
| 404 | $sql .= " datec=" . (dol_strlen($this->datec) != 0 ? "'" . $this->db->idate($this->datec) . "'" : 'null') . ","; |
||
| 405 | $sql .= " tms=" . (dol_strlen($this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : 'null') . ","; |
||
| 406 | $sql .= " datep=" . (dol_strlen($this->datep) != 0 ? "'" . $this->db->idate($this->datep) . "'" : 'null') . ","; |
||
| 407 | $sql .= " amount=" . (isset($this->amount) ? price2num($this->amount) : "null") . ","; |
||
| 408 | $sql .= " fk_typepaiement=" . (isset($this->fk_typepaiement) ? ((int) $this->fk_typepaiement) : "null") . ","; |
||
| 409 | $sql .= " num_paiement=" . (isset($this->num_payment) ? "'" . $this->db->escape($this->num_payment) . "'" : "null") . ","; |
||
| 410 | $sql .= " note=" . (isset($this->note) ? "'" . $this->db->escape($this->note) . "'" : "null") . ","; |
||
| 411 | $sql .= " fk_bank=" . (isset($this->fk_bank) ? ((int) $this->fk_bank) : "null") . ","; |
||
| 412 | $sql .= " fk_user_creat=" . (isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null") . ","; |
||
| 413 | $sql .= " fk_user_modif=" . (isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null"); |
||
| 414 | $sql .= " WHERE rowid=" . ((int) $this->id); |
||
| 415 | |||
| 416 | $this->db->begin(); |
||
| 417 | |||
| 418 | dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
||
| 419 | $resql = $this->db->query($sql); |
||
| 420 | if (!$resql) { |
||
| 421 | $error++; |
||
| 422 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
| 423 | } |
||
| 424 | |||
| 425 | // Commit or rollback |
||
| 426 | if ($error) { |
||
| 427 | foreach ($this->errors as $errmsg) { |
||
| 428 | dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR); |
||
| 429 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
| 430 | } |
||
| 431 | $this->db->rollback(); |
||
| 432 | return -1 * $error; |
||
| 433 | } else { |
||
| 434 | $this->db->commit(); |
||
| 435 | return 1; |
||
| 436 | } |
||
| 437 | } |
||
| 438 | |||
| 439 | |||
| 440 | /** |
||
| 441 | * Delete object in database |
||
| 442 | * |
||
| 443 | * @param User $user User that delete |
||
| 444 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 445 | * @return int Return integer <0 if KO, >0 if OK |
||
| 446 | */ |
||
| 447 | public function delete($user, $notrigger = 0) |
||
| 448 | { |
||
| 449 | $error = 0; |
||
| 450 | |||
| 451 | dol_syslog(get_class($this) . "::delete"); |
||
| 452 | |||
| 453 | $this->db->begin(); |
||
| 454 | |||
| 455 | if ($this->bank_line > 0) { |
||
| 456 | $accline = new AccountLine($this->db); |
||
| 457 | $accline->fetch($this->bank_line); |
||
| 458 | $result = $accline->delete($user); |
||
| 459 | if ($result < 0) { |
||
| 460 | $this->errors[] = $accline->error; |
||
| 461 | $error++; |
||
| 462 | } |
||
| 463 | } |
||
| 464 | |||
| 465 | if (!$error) { |
||
| 466 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "paiementcharge"; |
||
| 467 | $sql .= " WHERE rowid=" . ((int) $this->id); |
||
| 468 | |||
| 469 | dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
||
| 470 | $resql = $this->db->query($sql); |
||
| 471 | if (!$resql) { |
||
| 472 | $error++; |
||
| 473 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
| 474 | } |
||
| 475 | } |
||
| 476 | |||
| 477 | // Commit or rollback |
||
| 478 | if ($error) { |
||
| 479 | foreach ($this->errors as $errmsg) { |
||
| 480 | dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR); |
||
| 481 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
| 482 | } |
||
| 483 | $this->db->rollback(); |
||
| 484 | return -1 * $error; |
||
| 485 | } else { |
||
| 486 | $this->db->commit(); |
||
| 487 | return 1; |
||
| 488 | } |
||
| 489 | } |
||
| 490 | |||
| 491 | |||
| 492 | |||
| 493 | /** |
||
| 494 | * Load an object from its id and create a new one in database |
||
| 495 | * |
||
| 496 | * @param User $user User making the clone |
||
| 497 | * @param int $fromid Id of object to clone |
||
| 498 | * @return int New id of clone |
||
| 499 | */ |
||
| 500 | public function createFromClone(User $user, $fromid) |
||
| 501 | { |
||
| 502 | $error = 0; |
||
| 503 | |||
| 504 | $object = new PaymentSocialContribution($this->db); |
||
| 505 | |||
| 506 | $this->db->begin(); |
||
| 507 | |||
| 508 | // Load source object |
||
| 509 | $object->fetch($fromid); |
||
| 510 | $object->id = 0; |
||
| 511 | $object->statut = 0; |
||
| 512 | |||
| 513 | // Clear fields |
||
| 514 | // ... |
||
| 515 | |||
| 516 | // Create clone |
||
| 517 | $object->context['createfromclone'] = 'createfromclone'; |
||
| 518 | $result = $object->create($user); |
||
| 519 | |||
| 520 | // Other options |
||
| 521 | if ($result < 0) { |
||
| 522 | $this->error = $object->error; |
||
| 523 | $error++; |
||
| 524 | } |
||
| 525 | |||
| 526 | unset($object->context['createfromclone']); |
||
| 527 | |||
| 528 | // End |
||
| 529 | if (!$error) { |
||
| 530 | $this->db->commit(); |
||
| 531 | return $object->id; |
||
| 532 | } else { |
||
| 533 | $this->db->rollback(); |
||
| 534 | return -1; |
||
| 535 | } |
||
| 536 | } |
||
| 537 | |||
| 538 | |||
| 539 | /** |
||
| 540 | * Initialise an instance with random values. |
||
| 541 | * Used to build previews or test instances. |
||
| 542 | * id must be 0 if object instance is a specimen. |
||
| 543 | * |
||
| 544 | * @return int |
||
| 545 | */ |
||
| 546 | public function initAsSpecimen() |
||
| 547 | { |
||
| 548 | $this->id = 0; |
||
| 549 | $this->fk_charge = 0; |
||
| 550 | $this->datec = dol_now(); |
||
| 551 | $this->tms = dol_now(); |
||
| 552 | $this->datep = dol_now(); |
||
| 553 | $this->amount = 100; |
||
| 554 | $this->fk_typepaiement = 0; |
||
| 555 | $this->num_payment = 'ABC123'; |
||
| 556 | $this->note_private = ''; |
||
| 557 | $this->note_public = ''; |
||
| 558 | $this->fk_bank = 0; |
||
| 559 | $this->fk_user_creat = 0; |
||
| 560 | $this->fk_user_modif = 0; |
||
| 561 | |||
| 562 | return 1; |
||
| 563 | } |
||
| 564 | |||
| 565 | |||
| 566 | /** |
||
| 567 | * Add record into bank for payment with links between this bank record and invoices of payment. |
||
| 568 | * All payment properties must have been set first like after a call to create(). |
||
| 569 | * |
||
| 570 | * @param User $user Object of user making payment |
||
| 571 | * @param string $mode 'payment_sc' |
||
| 572 | * @param string $label Label to use in bank record |
||
| 573 | * @param int $accountid Id of bank account to do link with |
||
| 574 | * @param string $emetteur_nom Name of transmitter |
||
| 575 | * @param string $emetteur_banque Name of bank |
||
| 576 | * @return int Return integer <0 if KO, >0 if OK |
||
| 577 | */ |
||
| 578 | public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque) |
||
| 579 | { |
||
| 580 | global $conf, $langs; |
||
| 581 | |||
| 582 | // Clean data |
||
| 583 | $this->num_payment = trim($this->num_payment); |
||
| 584 | |||
| 585 | $error = 0; |
||
| 586 | |||
| 587 | if (isModEnabled("bank")) { |
||
| 588 | |||
| 589 | $acc = new Account($this->db); |
||
| 590 | $acc->fetch($accountid); |
||
| 591 | |||
| 592 | $total = $this->amount; |
||
| 593 | if ($mode == 'payment_sc') { |
||
| 594 | $total = -$total; |
||
| 595 | } |
||
| 596 | |||
| 597 | // Insert payment into llx_bank |
||
| 598 | $bank_line_id = $acc->addline( |
||
| 599 | $this->datepaye, |
||
| 600 | $this->paiementtype, // Payment mode id or code ("CHQ or VIR for example") |
||
| 601 | $label, |
||
| 602 | $total, |
||
| 603 | $this->num_payment, |
||
| 604 | '', |
||
| 605 | $user, |
||
| 606 | $emetteur_nom, |
||
| 607 | $emetteur_banque |
||
| 608 | ); |
||
| 609 | |||
| 610 | // Mise a jour fk_bank dans llx_paiement. |
||
| 611 | // On connait ainsi le paiement qui a genere l'ecriture bancaire |
||
| 612 | if ($bank_line_id > 0) { |
||
| 613 | $result = $this->update_fk_bank($bank_line_id); |
||
| 614 | if ($result <= 0) { |
||
| 615 | $error++; |
||
| 616 | dol_print_error($this->db); |
||
| 617 | } |
||
| 618 | |||
| 619 | // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction |
||
| 620 | $url = ''; |
||
| 621 | if ($mode == 'payment_sc') { |
||
| 622 | $url = constant('BASE_URL') . '/compta/payment_sc/card.php?id='; |
||
| 623 | } |
||
| 624 | if ($url) { |
||
| 625 | $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode); |
||
| 626 | if ($result <= 0) { |
||
| 627 | $error++; |
||
| 628 | dol_print_error($this->db); |
||
| 629 | } |
||
| 630 | } |
||
| 631 | |||
| 632 | // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment) |
||
| 633 | $linkaddedforthirdparty = array(); |
||
| 634 | foreach ($this->amounts as $key => $value) { |
||
| 635 | if ($mode == 'payment_sc') { |
||
| 636 | $socialcontrib = new ChargeSociales($this->db); |
||
| 637 | $socialcontrib->fetch($key); |
||
| 638 | $result = $acc->add_url_line($bank_line_id, $socialcontrib->id, constant('BASE_URL') . '/compta/charges.php?id=', $socialcontrib->type_label . (($socialcontrib->lib && $socialcontrib->lib != $socialcontrib->type_label) ? ' (' . $socialcontrib->lib . ')' : ''), 'sc'); |
||
| 639 | if ($result <= 0) { |
||
| 640 | dol_print_error($this->db); |
||
| 641 | } |
||
| 642 | |||
| 643 | if ($socialcontrib->fk_user) { |
||
| 644 | $fuser = new User($this->db); |
||
| 645 | $fuser->fetch($socialcontrib->fk_user); |
||
| 646 | |||
| 647 | // Add link 'user' in bank_url between operation and bank transaction |
||
| 648 | $result = $acc->add_url_line( |
||
| 649 | $bank_line_id, |
||
| 650 | $socialcontrib->fk_user, |
||
| 651 | constant('BASE_URL') . '/user/card.php?id=', |
||
| 652 | $fuser->getFullName($langs), |
||
| 653 | 'user' |
||
| 654 | ); |
||
| 655 | |||
| 656 | if ($result <= 0) { |
||
| 657 | $this->error = $acc->error; |
||
| 658 | $error++; |
||
| 659 | } |
||
| 660 | } |
||
| 661 | } |
||
| 662 | } |
||
| 663 | } else { |
||
| 664 | $this->error = $acc->error; |
||
| 665 | $error++; |
||
| 666 | } |
||
| 667 | } |
||
| 668 | |||
| 669 | if (!$error) { |
||
| 670 | return 1; |
||
| 671 | } else { |
||
| 672 | return -1; |
||
| 673 | } |
||
| 674 | } |
||
| 675 | |||
| 676 | |||
| 677 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 678 | /** |
||
| 679 | * Mise a jour du lien entre le paiement de charge et la ligne dans llx_bank generee |
||
| 680 | * |
||
| 681 | * @param int $id_bank Id if bank |
||
| 682 | * @return int >0 if OK, <=0 if KO |
||
| 683 | */ |
||
| 684 | public function update_fk_bank($id_bank) |
||
| 696 | } |
||
| 697 | } |
||
| 698 | |||
| 699 | |||
| 700 | /** |
||
| 701 | * Return the label of the status |
||
| 702 | * |
||
| 703 | * @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 |
||
| 704 | * @return string Label of status |
||
| 705 | */ |
||
| 706 | public function getLibStatut($mode = 0) |
||
| 707 | { |
||
| 708 | return $this->LibStatut($this->statut, $mode); |
||
| 709 | } |
||
| 710 | |||
| 711 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 712 | /** |
||
| 713 | * Return the label of a given status |
||
| 714 | * |
||
| 715 | * @param int $status Id status |
||
| 716 | * @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 |
||
| 717 | * @return string Label of status |
||
| 718 | */ |
||
| 719 | public function LibStatut($status, $mode = 0) |
||
| 720 | { |
||
| 721 | // phpcs:enable |
||
| 722 | global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage |
||
| 723 | |||
| 724 | $langs->load('compta'); |
||
| 725 | /*if ($mode == 0) |
||
| 726 | { |
||
| 727 | if ($status == 0) return $langs->trans('ToValidate'); |
||
| 728 | if ($status == 1) return $langs->trans('Validated'); |
||
| 729 | } |
||
| 730 | if ($mode == 1) |
||
| 731 | { |
||
| 732 | if ($status == 0) return $langs->trans('ToValidate'); |
||
| 733 | if ($status == 1) return $langs->trans('Validated'); |
||
| 734 | } |
||
| 735 | if ($mode == 2) |
||
| 736 | { |
||
| 737 | if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate'); |
||
| 738 | if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated'); |
||
| 739 | } |
||
| 740 | if ($mode == 3) |
||
| 741 | { |
||
| 742 | if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1'); |
||
| 743 | if ($status == 1) return img_picto($langs->trans('Validated'),'statut4'); |
||
| 744 | } |
||
| 745 | if ($mode == 4) |
||
| 746 | { |
||
| 747 | if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate'); |
||
| 748 | if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated'); |
||
| 749 | } |
||
| 750 | if ($mode == 5) |
||
| 751 | { |
||
| 752 | if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1'); |
||
| 753 | if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4'); |
||
| 754 | } |
||
| 755 | if ($mode == 6) |
||
| 756 | { |
||
| 757 | if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1'); |
||
| 758 | if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4'); |
||
| 759 | }*/ |
||
| 760 | return ''; |
||
| 761 | } |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Return clicable name (with picto eventually) |
||
| 765 | * |
||
| 766 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
||
| 767 | * @param int $maxlen Longueur max libelle |
||
| 768 | * @return string Chaine avec URL |
||
| 769 | */ |
||
| 770 | public function getNomUrl($withpicto = 0, $maxlen = 0) |
||
| 814 | } |
||
| 815 | |||
| 816 | |||
| 817 | /** |
||
| 818 | * Return if object was dispatched into bookkeeping |
||
| 819 | * |
||
| 820 | * @return int Return integer <0 if KO, 0=no, 1=yes |
||
| 821 | */ |
||
| 822 | public function getVentilExportCompta() |
||
| 844 | } |
||
| 845 | } |
||
| 846 |