| Total Complexity | 132 |
| Total Lines | 1058 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Don 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 Don, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Don extends CommonObject |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var string ID to identify managed object |
||
| 41 | */ |
||
| 42 | public $element = 'don'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string Name of table without prefix where object is stored |
||
| 46 | */ |
||
| 47 | public $table_element = 'don'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int Field with ID of parent key if this field has a parent |
||
| 51 | */ |
||
| 52 | public $fk_element = 'fk_donation'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | public $ismultientitymanaged = 1; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string String with name of icon for object don. Must be the part after the 'object_' into object_myobject.png |
||
| 62 | */ |
||
| 63 | public $picto = 'donation'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string Date of the donation |
||
| 67 | */ |
||
| 68 | public $date; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * amount of donation |
||
| 72 | * @var double |
||
| 73 | */ |
||
| 74 | public $amount; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string Thirdparty name |
||
| 78 | */ |
||
| 79 | public $societe; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string Address |
||
| 83 | */ |
||
| 84 | public $address; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string Zipcode |
||
| 88 | */ |
||
| 89 | public $zip; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var string Town |
||
| 93 | */ |
||
| 94 | public $town; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var string Email |
||
| 98 | */ |
||
| 99 | public $email; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var int 0 or 1 |
||
| 103 | */ |
||
| 104 | public $public; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var int project ID |
||
| 108 | */ |
||
| 109 | public $fk_project; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var int type payment ID |
||
| 113 | */ |
||
| 114 | public $fk_typepayment; |
||
| 115 | |||
| 116 | public $num_payment; |
||
| 117 | public $date_valid; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var int payment mode id |
||
| 121 | */ |
||
| 122 | public $modepaymentid = 0; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var array Array of status label |
||
| 126 | */ |
||
| 127 | public $labelStatus; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var array Array of status label short |
||
| 131 | */ |
||
| 132 | public $labelStatusShort; |
||
| 133 | |||
| 134 | |||
| 135 | const STATUS_DRAFT = 0; |
||
| 136 | const STATUS_VALIDATED = 1; |
||
| 137 | const STATUS_PAID = 2; |
||
| 138 | const STATUS_CANCELED = -1; |
||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * Constructor |
||
| 143 | * |
||
| 144 | * @param DoliDB $db Database handler |
||
| 145 | */ |
||
| 146 | public function __construct($db) |
||
| 147 | { |
||
| 148 | $this->db = $db; |
||
| 149 | } |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns the donation status label (draft, valid, abandoned, paid) |
||
| 154 | * |
||
| 155 | * @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 |
||
| 156 | * @return string Label of status |
||
| 157 | */ |
||
| 158 | public function getLibStatut($mode = 0) |
||
| 159 | { |
||
| 160 | return $this->LibStatut($this->statut, $mode); |
||
| 161 | } |
||
| 162 | |||
| 163 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 164 | /** |
||
| 165 | * Return the label of a given status |
||
| 166 | * |
||
| 167 | * @param int $status Id statut |
||
| 168 | * @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 |
||
| 169 | * @return string Label of status |
||
| 170 | */ |
||
| 171 | public function LibStatut($status, $mode = 0) |
||
| 172 | { |
||
| 173 | // phpcs:enable |
||
| 174 | if (empty($this->labelStatus) || empty($this->labelStatusShort)) |
||
| 175 | { |
||
| 176 | global $langs; |
||
| 177 | $langs->load("donations"); |
||
| 178 | $this->labelStatus[-1] = $langs->transnoentitiesnoconv("Canceled"); |
||
| 179 | $this->labelStatus[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidated"); |
||
| 180 | $this->labelStatus[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidated"); |
||
| 181 | $this->labelStatus[2] = $langs->transnoentitiesnoconv("DonationStatusPaid"); |
||
| 182 | $this->labelStatusShort[-1] = $langs->transnoentitiesnoconv("Canceled"); |
||
| 183 | $this->labelStatusShort[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidatedShort"); |
||
| 184 | $this->labelStatusShort[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidatedShort"); |
||
| 185 | $this->labelStatusShort[2] = $langs->transnoentitiesnoconv("DonationStatusPaidShort"); |
||
| 186 | } |
||
| 187 | |||
| 188 | $statusType = 'status'.$status; |
||
| 189 | if ($status == self::STATUS_CANCELED) $statusType = 'status9'; |
||
| 190 | if ($status == self::STATUS_PAID) $statusType = 'status6'; |
||
| 191 | |||
| 192 | return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode); |
||
| 193 | } |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * Initialise an instance with random values. |
||
| 198 | * Used to build previews or test instances. |
||
| 199 | * id must be 0 if object instance is a specimen. |
||
| 200 | * |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function initAsSpecimen() |
||
| 204 | { |
||
| 205 | global $conf, $user, $langs; |
||
| 206 | |||
| 207 | $now = dol_now(); |
||
| 208 | |||
| 209 | // Charge tableau des id de societe socids |
||
| 210 | $socids = array(); |
||
| 211 | |||
| 212 | $sql = "SELECT rowid"; |
||
| 213 | $sql .= " FROM ".MAIN_DB_PREFIX."societe"; |
||
| 214 | $sql .= " WHERE client IN (1, 3)"; |
||
| 215 | $sql .= " AND entity = ".$conf->entity; |
||
| 216 | $sql .= " LIMIT 10"; |
||
| 217 | |||
| 218 | $resql = $this->db->query($sql); |
||
| 219 | if ($resql) |
||
| 220 | { |
||
| 221 | $num_socs = $this->db->num_rows($resql); |
||
| 222 | $i = 0; |
||
| 223 | while ($i < $num_socs) |
||
| 224 | { |
||
| 225 | $i++; |
||
| 226 | |||
| 227 | $row = $this->db->fetch_row($resql); |
||
| 228 | $socids[$i] = $row[0]; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | // Initialise parametres |
||
| 233 | $this->id = 0; |
||
| 234 | $this->ref = 'SPECIMEN'; |
||
| 235 | $this->specimen = 1; |
||
| 236 | $this->lastname = 'Doe'; |
||
| 237 | $this->firstname = 'John'; |
||
| 238 | $this->socid = 1; |
||
| 239 | $this->date = $now; |
||
| 240 | $this->date_valid = $now; |
||
| 241 | $this->amount = 100.90; |
||
| 242 | $this->public = 1; |
||
| 243 | $this->societe = 'The Company'; |
||
| 244 | $this->address = 'Twist road'; |
||
| 245 | $this->zip = '99999'; |
||
| 246 | $this->town = 'Town'; |
||
| 247 | $this->note_private = 'Private note'; |
||
| 248 | $this->note_public = 'Public note'; |
||
| 249 | $this->email = '[email protected]'; |
||
| 250 | $this->phone = '0123456789'; |
||
| 251 | $this->phone_mobile = '0606060606'; |
||
| 252 | $this->statut = 1; |
||
| 253 | } |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Check params and init ->errors array. |
||
| 258 | * TODO This function seems to not be used by core code. |
||
| 259 | * |
||
| 260 | * @param int $minimum Minimum |
||
| 261 | * @return int 0 if KO, >0 if OK |
||
| 262 | */ |
||
| 263 | public function check($minimum = 0) |
||
| 264 | { |
||
| 265 | global $langs; |
||
| 266 | $langs->load('main'); |
||
| 267 | $langs->load('companies'); |
||
| 268 | |||
| 269 | $error_string = array(); |
||
| 270 | $err = 0; |
||
| 271 | |||
| 272 | if (dol_strlen(trim($this->societe)) == 0) |
||
| 273 | { |
||
| 274 | if ((dol_strlen(trim($this->lastname)) + dol_strlen(trim($this->firstname))) == 0) |
||
| 275 | { |
||
| 276 | $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Company').'/'.$langs->transnoentitiesnoconv('Firstname').'-'.$langs->transnoentitiesnoconv('Lastname')); |
||
| 277 | $err++; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | if (dol_strlen(trim($this->address)) == 0) |
||
| 282 | { |
||
| 283 | $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Address')); |
||
| 284 | $err++; |
||
| 285 | } |
||
| 286 | |||
| 287 | if (dol_strlen(trim($this->zip)) == 0) |
||
| 288 | { |
||
| 289 | $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Zip')); |
||
| 290 | $err++; |
||
| 291 | } |
||
| 292 | |||
| 293 | if (dol_strlen(trim($this->town)) == 0) |
||
| 294 | { |
||
| 295 | $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Town')); |
||
| 296 | $err++; |
||
| 297 | } |
||
| 298 | |||
| 299 | if (dol_strlen(trim($this->email)) == 0) |
||
| 300 | { |
||
| 301 | $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('EMail')); |
||
| 302 | $err++; |
||
| 303 | } |
||
| 304 | |||
| 305 | $this->amount = trim($this->amount); |
||
| 306 | |||
| 307 | $map = range(0, 9); |
||
| 308 | $len = dol_strlen($this->amount); |
||
| 309 | for ($i = 0; $i < $len; $i++) |
||
| 310 | { |
||
| 311 | if (!isset($map[substr($this->amount, $i, 1)])) |
||
| 312 | { |
||
| 313 | $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount')); |
||
| 314 | $err++; |
||
| 315 | $amount_invalid = 1; |
||
| 316 | break; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | if (!$amount_invalid) |
||
|
|
|||
| 321 | { |
||
| 322 | if ($this->amount == 0) |
||
| 323 | { |
||
| 324 | $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount')); |
||
| 325 | $err++; |
||
| 326 | } else { |
||
| 327 | if ($this->amount < $minimum && $minimum > 0) |
||
| 328 | { |
||
| 329 | $error_string[] = $langs->trans('MinimumAmount', $langs->transnoentitiesnoconv('$minimum')); |
||
| 330 | $err++; |
||
| 331 | } |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | if ($err) |
||
| 336 | { |
||
| 337 | $this->errors = $error_string; |
||
| 338 | return 0; |
||
| 339 | } else { |
||
| 340 | return 1; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Create donation record into database |
||
| 346 | * |
||
| 347 | * @param User $user User who created the donation |
||
| 348 | * @param int $notrigger Disable triggers |
||
| 349 | * @return int <0 if KO, id of created donation if OK |
||
| 350 | * TODO add numbering module for Ref |
||
| 351 | */ |
||
| 352 | public function create($user, $notrigger = 0) |
||
| 353 | { |
||
| 354 | global $conf, $langs; |
||
| 355 | |||
| 356 | $error = 0; |
||
| 357 | $ret = 0; |
||
| 358 | $now = dol_now(); |
||
| 359 | |||
| 360 | // Clean parameters |
||
| 361 | $this->address = ($this->address > 0 ? $this->address : $this->address); |
||
| 362 | $this->zip = ($this->zip > 0 ? $this->zip : $this->zip); |
||
| 363 | $this->town = ($this->town > 0 ? $this->town : $this->town); |
||
| 364 | $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id); |
||
| 365 | $this->country = ($this->country ? $this->country : $this->country); |
||
| 366 | |||
| 367 | $this->db->begin(); |
||
| 368 | |||
| 369 | $sql = "INSERT INTO ".MAIN_DB_PREFIX."don ("; |
||
| 370 | $sql .= "datec"; |
||
| 371 | $sql .= ", entity"; |
||
| 372 | $sql .= ", amount"; |
||
| 373 | $sql .= ", fk_payment"; |
||
| 374 | $sql .= ", fk_soc"; |
||
| 375 | $sql .= ", firstname"; |
||
| 376 | $sql .= ", lastname"; |
||
| 377 | $sql .= ", societe"; |
||
| 378 | $sql .= ", address"; |
||
| 379 | $sql .= ", zip"; |
||
| 380 | $sql .= ", town"; |
||
| 381 | $sql .= ", fk_country"; |
||
| 382 | $sql .= ", public"; |
||
| 383 | $sql .= ", fk_projet"; |
||
| 384 | $sql .= ", note_private"; |
||
| 385 | $sql .= ", note_public"; |
||
| 386 | $sql .= ", fk_user_author"; |
||
| 387 | $sql .= ", fk_user_valid"; |
||
| 388 | $sql .= ", datedon"; |
||
| 389 | $sql .= ", email"; |
||
| 390 | $sql .= ", phone"; |
||
| 391 | $sql .= ", phone_mobile"; |
||
| 392 | $sql .= ") VALUES ("; |
||
| 393 | $sql .= "'".$this->db->idate($now)."'"; |
||
| 394 | $sql .= ", ".$conf->entity; |
||
| 395 | $sql .= ", ".price2num($this->amount); |
||
| 396 | $sql .= ", ".($this->modepaymentid ? $this->modepaymentid : "null"); |
||
| 397 | $sql .= ", ".($this->socid > 0 ? $this->socid : "null"); |
||
| 398 | $sql .= ", '".$this->db->escape($this->firstname)."'"; |
||
| 399 | $sql .= ", '".$this->db->escape($this->lastname)."'"; |
||
| 400 | $sql .= ", '".$this->db->escape($this->societe)."'"; |
||
| 401 | $sql .= ", '".$this->db->escape($this->address)."'"; |
||
| 402 | $sql .= ", '".$this->db->escape($this->zip)."'"; |
||
| 403 | $sql .= ", '".$this->db->escape($this->town)."'"; |
||
| 404 | $sql .= ", ".(int) ($this->country_id > 0 ? $this->country_id : 0); |
||
| 405 | $sql .= ", ".(int) $this->public; |
||
| 406 | $sql .= ", ".($this->fk_project > 0 ? (int) $this->fk_project : "null"); |
||
| 407 | $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL"); |
||
| 408 | $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL"); |
||
| 409 | $sql .= ", ".$user->id; |
||
| 410 | $sql .= ", null"; |
||
| 411 | $sql .= ", '".$this->db->idate($this->date)."'"; |
||
| 412 | $sql .= ", '".$this->db->escape(trim($this->email))."'"; |
||
| 413 | $sql .= ", '".$this->db->escape(trim($this->phone))."'"; |
||
| 414 | $sql .= ", '".$this->db->escape(trim($this->phone_mobile))."'"; |
||
| 415 | $sql .= ")"; |
||
| 416 | |||
| 417 | $resql = $this->db->query($sql); |
||
| 418 | if ($resql) |
||
| 419 | { |
||
| 420 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."don"); |
||
| 421 | $ret = $this->id; |
||
| 422 | |||
| 423 | if (!$notrigger) |
||
| 424 | { |
||
| 425 | // Call trigger |
||
| 426 | $result = $this->call_trigger('DON_CREATE', $user); |
||
| 427 | if ($result < 0) { $error++; } |
||
| 428 | // End call triggers |
||
| 429 | } |
||
| 430 | } else { |
||
| 431 | $this->error = $this->db->lasterror(); |
||
| 432 | $this->errno = $this->db->lasterrno(); |
||
| 433 | $error++; |
||
| 434 | } |
||
| 435 | |||
| 436 | // Update extrafield |
||
| 437 | if (!$error) { |
||
| 438 | $result = $this->insertExtraFields(); |
||
| 439 | if ($result < 0) { |
||
| 440 | $error++; |
||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | if (!$error && !empty($conf->global->MAIN_DISABLEDRAFTSTATUS)) |
||
| 445 | { |
||
| 446 | //$res = $this->setValid($user); |
||
| 447 | //if ($res < 0) $error++; |
||
| 448 | } |
||
| 449 | |||
| 450 | if (!$error) |
||
| 451 | { |
||
| 452 | $this->db->commit(); |
||
| 453 | return $ret; |
||
| 454 | } else { |
||
| 455 | $this->db->rollback(); |
||
| 456 | return -1; |
||
| 457 | } |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Update a donation record |
||
| 462 | * |
||
| 463 | * @param User $user Objet utilisateur qui met a jour le don |
||
| 464 | * @param int $notrigger Disable triggers |
||
| 465 | * @return int >0 if OK, <0 if KO |
||
| 466 | */ |
||
| 467 | public function update($user, $notrigger = 0) |
||
| 468 | { |
||
| 469 | global $langs, $conf; |
||
| 470 | |||
| 471 | $error = 0; |
||
| 472 | |||
| 473 | // Clean parameters |
||
| 474 | $this->address = ($this->address > 0 ? $this->address : $this->address); |
||
| 475 | $this->zip = ($this->zip > 0 ? $this->zip : $this->zip); |
||
| 476 | $this->town = ($this->town > 0 ? $this->town : $this->town); |
||
| 477 | $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id); |
||
| 478 | $this->country = ($this->country ? $this->country : $this->country); |
||
| 479 | |||
| 480 | $this->db->begin(); |
||
| 481 | |||
| 482 | $sql = "UPDATE ".MAIN_DB_PREFIX."don SET "; |
||
| 483 | $sql .= "amount = ".price2num($this->amount); |
||
| 484 | $sql .= ",fk_payment = ".($this->modepaymentid ? $this->modepaymentid : "null"); |
||
| 485 | $sql .= ",firstname = '".$this->db->escape($this->firstname)."'"; |
||
| 486 | $sql .= ",lastname='".$this->db->escape($this->lastname)."'"; |
||
| 487 | $sql .= ",societe='".$this->db->escape($this->societe)."'"; |
||
| 488 | $sql .= ",address='".$this->db->escape($this->address)."'"; |
||
| 489 | $sql .= ",zip='".$this->db->escape($this->zip)."'"; |
||
| 490 | $sql .= ",town='".$this->db->escape($this->town)."'"; |
||
| 491 | $sql .= ",fk_country = ".($this->country_id > 0 ? $this->country_id : '0'); |
||
| 492 | $sql .= ",public=".$this->public; |
||
| 493 | $sql .= ",fk_projet=".($this->fk_project > 0 ? $this->fk_project : 'null'); |
||
| 494 | $sql .= ",note_private=".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL"); |
||
| 495 | $sql .= ",note_public=".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL"); |
||
| 496 | $sql .= ",datedon='".$this->db->idate($this->date)."'"; |
||
| 497 | $sql .= ",date_valid=".($this->date_valid ? "'".$this->db->idate($this->date)."'" : "null"); |
||
| 498 | $sql .= ",email='".$this->db->escape(trim($this->email))."'"; |
||
| 499 | $sql .= ",phone='".$this->db->escape(trim($this->phone))."'"; |
||
| 500 | $sql .= ",phone_mobile='".$this->db->escape(trim($this->phone_mobile))."'"; |
||
| 501 | $sql .= ",fk_statut=".$this->statut; |
||
| 502 | $sql .= " WHERE rowid = ".$this->id; |
||
| 503 | |||
| 504 | dol_syslog(get_class($this)."::Update", LOG_DEBUG); |
||
| 505 | $resql = $this->db->query($sql); |
||
| 506 | if ($resql) |
||
| 507 | { |
||
| 508 | if (!$notrigger) |
||
| 509 | { |
||
| 510 | // Call trigger |
||
| 511 | $result = $this->call_trigger('DON_MODIFY', $user); |
||
| 512 | if ($result < 0) { $error++; } |
||
| 513 | // End call triggers |
||
| 514 | } |
||
| 515 | |||
| 516 | // Update extrafield |
||
| 517 | if (!$error) |
||
| 518 | { |
||
| 519 | $result = $this->insertExtraFields(); |
||
| 520 | if ($result < 0) |
||
| 521 | { |
||
| 522 | $error++; |
||
| 523 | } |
||
| 524 | } |
||
| 525 | |||
| 526 | if (!$error) |
||
| 527 | { |
||
| 528 | $this->db->commit(); |
||
| 529 | $result = 1; |
||
| 530 | } else { |
||
| 531 | $this->db->rollback(); |
||
| 532 | $result = -1; |
||
| 533 | } |
||
| 534 | } else { |
||
| 535 | $this->error = $this->db->lasterror(); |
||
| 536 | $this->errors[] = $this->error; |
||
| 537 | $this->db->rollback(); |
||
| 538 | dol_syslog(get_class($this)."::Update error -2 ".$this->error, LOG_ERR); |
||
| 539 | $result = -2; |
||
| 540 | } |
||
| 541 | return $result; |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Delete a donation from database |
||
| 546 | * |
||
| 547 | * @param User $user User |
||
| 548 | * @param int $notrigger Disable triggers |
||
| 549 | * @return int <0 if KO, 0 if not possible, >0 if OK |
||
| 550 | */ |
||
| 551 | public function delete($user, $notrigger = 0) |
||
| 552 | { |
||
| 553 | global $user, $conf, $langs; |
||
| 554 | require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; |
||
| 555 | |||
| 556 | $error = 0; |
||
| 557 | |||
| 558 | $this->db->begin(); |
||
| 559 | |||
| 560 | if (!$error && !$notrigger) { |
||
| 561 | // Call trigger |
||
| 562 | $result = $this->call_trigger('DON_DELETE', $user); |
||
| 563 | |||
| 564 | if ($result < 0) { |
||
| 565 | $error++; |
||
| 566 | } |
||
| 567 | // End call triggers |
||
| 568 | } |
||
| 569 | |||
| 570 | // Delete donation |
||
| 571 | if (!$error) |
||
| 572 | { |
||
| 573 | $sql = "DELETE FROM ".MAIN_DB_PREFIX."don_extrafields"; |
||
| 574 | $sql .= " WHERE fk_object=".$this->id; |
||
| 575 | |||
| 576 | $resql = $this->db->query($sql); |
||
| 577 | if (!$resql) |
||
| 578 | { |
||
| 579 | $this->errors[] = $this->db->lasterror(); |
||
| 580 | $error++; |
||
| 581 | } |
||
| 582 | } |
||
| 583 | |||
| 584 | if (!$error) |
||
| 585 | { |
||
| 586 | $sql = "DELETE FROM ".MAIN_DB_PREFIX."don"; |
||
| 587 | $sql .= " WHERE rowid=".$this->id; |
||
| 588 | |||
| 589 | $resql = $this->db->query($sql); |
||
| 590 | if (!$resql) |
||
| 591 | { |
||
| 592 | $this->errors[] = $this->db->lasterror(); |
||
| 593 | $error++; |
||
| 594 | } |
||
| 595 | } |
||
| 596 | |||
| 597 | if (!$error) |
||
| 598 | { |
||
| 599 | $this->db->commit(); |
||
| 600 | return 1; |
||
| 601 | } else { |
||
| 602 | foreach ($this->errors as $errmsg) |
||
| 603 | { |
||
| 604 | dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR); |
||
| 605 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 606 | } |
||
| 607 | dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR); |
||
| 608 | $this->db->rollback(); |
||
| 609 | return -1; |
||
| 610 | } |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Load donation from database |
||
| 615 | * |
||
| 616 | * @param int $id Id of donation to load |
||
| 617 | * @param string $ref Ref of donation to load |
||
| 618 | * @return int <0 if KO, >0 if OK |
||
| 619 | */ |
||
| 620 | public function fetch($id, $ref = '') |
||
| 698 | } |
||
| 699 | } |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Validate a intervention |
||
| 703 | * |
||
| 704 | * @param User $user User that validate |
||
| 705 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 706 | * @return int <0 if KO, >0 if OK |
||
| 707 | */ |
||
| 708 | public function setValid($user, $notrigger = 0) |
||
| 709 | { |
||
| 710 | return $this->valid_promesse($this->id, $user->id, $notrigger); |
||
| 711 | } |
||
| 712 | |||
| 713 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 714 | /** |
||
| 715 | * Validate a promise of donation |
||
| 716 | * |
||
| 717 | * @param int $id id of donation |
||
| 718 | * @param int $userid User who validate the donation/promise |
||
| 719 | * @param int $notrigger Disable triggers |
||
| 720 | * @return int <0 if KO, >0 if OK |
||
| 721 | */ |
||
| 722 | public function valid_promesse($id, $userid, $notrigger = 0) |
||
| 723 | { |
||
| 724 | // phpcs:enable |
||
| 725 | global $langs, $user; |
||
| 726 | |||
| 727 | $error = 0; |
||
| 728 | |||
| 729 | $this->db->begin(); |
||
| 730 | |||
| 731 | $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 1, fk_user_valid = ".$userid." WHERE rowid = ".$id." AND fk_statut = 0"; |
||
| 732 | |||
| 733 | $resql = $this->db->query($sql); |
||
| 734 | if ($resql) |
||
| 735 | { |
||
| 736 | if ($this->db->affected_rows($resql)) |
||
| 737 | { |
||
| 738 | if (!$notrigger) |
||
| 739 | { |
||
| 740 | // Call trigger |
||
| 741 | $result = $this->call_trigger('DON_VALIDATE', $user); |
||
| 742 | if ($result < 0) { $error++; } |
||
| 743 | // End call triggers |
||
| 744 | } |
||
| 745 | } |
||
| 746 | } else { |
||
| 747 | $error++; |
||
| 748 | $this->error = $this->db->lasterror(); |
||
| 749 | } |
||
| 750 | |||
| 751 | if (!$error) |
||
| 752 | { |
||
| 753 | $this->db->commit(); |
||
| 754 | return 1; |
||
| 755 | } else { |
||
| 756 | $this->db->rollback(); |
||
| 757 | return -1; |
||
| 758 | } |
||
| 759 | } |
||
| 760 | |||
| 761 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 762 | /** |
||
| 763 | * Classify the donation as paid, the donation was received |
||
| 764 | * |
||
| 765 | * @param int $id id of donation |
||
| 766 | * @param int $modepayment mode of payment |
||
| 767 | * @return int <0 if KO, >0 if OK |
||
| 768 | */ |
||
| 769 | public function set_paid($id, $modepayment = 0) |
||
| 791 | } |
||
| 792 | } |
||
| 793 | |||
| 794 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 795 | /** |
||
| 796 | * Set donation to status cancelled |
||
| 797 | * |
||
| 798 | * @param int $id id of donation |
||
| 799 | * @return int <0 if KO, >0 if OK |
||
| 800 | */ |
||
| 801 | public function set_cancel($id) |
||
| 818 | } |
||
| 819 | } |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Set cancel status |
||
| 823 | * |
||
| 824 | * @param User $user Object user that modify |
||
| 825 | * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers |
||
| 826 | * @return int <0 if KO, 0=Nothing done, >0 if OK |
||
| 827 | */ |
||
| 828 | public function reopen($user, $notrigger = 0) |
||
| 829 | { |
||
| 830 | // Protection |
||
| 831 | if ($this->statut != self::STATUS_CANCELED) |
||
| 832 | { |
||
| 833 | return 0; |
||
| 834 | } |
||
| 835 | |||
| 836 | /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->write)) |
||
| 837 | || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->bom_advance->validate)))) |
||
| 838 | { |
||
| 839 | $this->error='Permission denied'; |
||
| 840 | return -1; |
||
| 841 | }*/ |
||
| 842 | |||
| 843 | return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'DON_REOPEN'); |
||
| 844 | } |
||
| 845 | |||
| 846 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 847 | /** |
||
| 848 | * Sum of donations |
||
| 849 | * |
||
| 850 | * @param string $param 1=promesses de dons validees , 2=xxx, 3=encaisses |
||
| 851 | * @return int Summ of donations |
||
| 852 | */ |
||
| 853 | public function sum_donations($param) |
||
| 854 | { |
||
| 855 | // phpcs:enable |
||
| 856 | global $conf; |
||
| 857 | |||
| 858 | $result = 0; |
||
| 859 | |||
| 860 | $sql = "SELECT sum(amount) as total"; |
||
| 861 | $sql .= " FROM ".MAIN_DB_PREFIX."don"; |
||
| 862 | $sql .= " WHERE fk_statut = ".$param; |
||
| 863 | $sql .= " AND entity = ".$conf->entity; |
||
| 864 | |||
| 865 | $resql = $this->db->query($sql); |
||
| 866 | if ($resql) |
||
| 867 | { |
||
| 868 | $obj = $this->db->fetch_object($resql); |
||
| 869 | $result = $obj->total; |
||
| 870 | } |
||
| 871 | |||
| 872 | return $result; |
||
| 873 | } |
||
| 874 | |||
| 875 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 876 | /** |
||
| 877 | * Charge indicateurs this->nb pour le tableau de bord |
||
| 878 | * |
||
| 879 | * @return int <0 if KO, >0 if OK |
||
| 880 | */ |
||
| 881 | public function load_state_board() |
||
| 906 | } |
||
| 907 | } |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Return clicable name (with picto eventually) |
||
| 911 | * |
||
| 912 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
||
| 913 | * @param int $notooltip 1=Disable tooltip |
||
| 914 | * @param string $moretitle Add more text to title tooltip |
||
| 915 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
| 916 | * @return string Chaine avec URL |
||
| 917 | */ |
||
| 918 | public function getNomUrl($withpicto = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1) |
||
| 950 | } |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Information on record |
||
| 954 | * |
||
| 955 | * @param int $id Id of record |
||
| 956 | * @return void |
||
| 957 | */ |
||
| 958 | public function info($id) |
||
| 992 | } |
||
| 993 | } |
||
| 994 | |||
| 995 | |||
| 996 | /** |
||
| 997 | * Create a document onto disk according to template module. |
||
| 998 | * |
||
| 999 | * @param string $modele Force template to use ('' to not force) |
||
| 1000 | * @param Translate $outputlangs objet lang a utiliser pour traduction |
||
| 1001 | * @param int $hidedetails Hide details of lines |
||
| 1002 | * @param int $hidedesc Hide description |
||
| 1003 | * @param int $hideref Hide ref |
||
| 1004 | * @return int 0 if KO, 1 if OK |
||
| 1005 | */ |
||
| 1006 | public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0) |
||
| 1098 |