| Total Complexity | 425 |
| Total Lines | 2501 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Contrat 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 Contrat, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class Contrat extends CommonObject |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * @var string ID to identify managed object |
||
| 47 | */ |
||
| 48 | public $element='contrat'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string Name of table without prefix where object is stored |
||
| 52 | */ |
||
| 53 | public $table_element='contrat'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var int Name of subtable line |
||
| 57 | */ |
||
| 58 | public $table_element_line='contratdet'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var int Field with ID of parent key if this field has a parent |
||
| 62 | */ |
||
| 63 | public $fk_element='fk_contrat'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
| 67 | */ |
||
| 68 | public $picto='contract'; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe |
||
| 72 | * @var int |
||
| 73 | */ |
||
| 74 | public $ismultientitymanaged = 1; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * 0=Default, 1=View may be restricted to sales representative only if no permission to see all or to company of external user if external user |
||
| 78 | * @var integer |
||
| 79 | */ |
||
| 80 | public $restrictiononfksoc = 1; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | protected $table_ref_field = 'ref'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Customer reference of the contract |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | public $ref_customer; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Supplier reference of the contract |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | public $ref_supplier; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Client id linked to the contract |
||
| 101 | * @var int |
||
| 102 | */ |
||
| 103 | public $socid; |
||
| 104 | |||
| 105 | public $societe; // Objet societe |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Status of the contract |
||
| 109 | * @var int |
||
| 110 | */ |
||
| 111 | public $statut=0; // 0=Draft, |
||
| 112 | |||
| 113 | public $product; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var int Id of user author of the contract |
||
| 117 | */ |
||
| 118 | public $fk_user_author; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * TODO: Which is the correct one? |
||
| 122 | * Author of the contract |
||
| 123 | * @var int |
||
| 124 | */ |
||
| 125 | public $user_author_id; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var User Object user that create the contract. Set by the info method. |
||
| 129 | */ |
||
| 130 | public $user_creation; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var User Object user that close the contract. Set by the info method. |
||
| 134 | */ |
||
| 135 | public $user_cloture; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var int Date of creation |
||
| 139 | */ |
||
| 140 | public $date_creation; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var int Date of last modification. Not filled until you call ->info() |
||
| 144 | */ |
||
| 145 | public $date_modification; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var int Date of validation |
||
| 149 | */ |
||
| 150 | public $date_validation; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @var int Date when contract was signed |
||
| 154 | */ |
||
| 155 | public $date_contrat; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var int Date of contract closure |
||
| 159 | * @deprecated we close contract lines, not a contract |
||
| 160 | */ |
||
| 161 | public $date_cloture; |
||
| 162 | |||
| 163 | public $commercial_signature_id; |
||
| 164 | public $commercial_suivi_id; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @deprecated Use fk_project instead |
||
| 168 | * @see $fk_project |
||
| 169 | */ |
||
| 170 | public $fk_projet; |
||
| 171 | |||
| 172 | public $extraparams=array(); |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var ContratLigne[] Contract lines |
||
| 176 | */ |
||
| 177 | public $lines=array(); |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Maps ContratLigne IDs to $this->lines indexes |
||
| 181 | * @var int[] |
||
| 182 | */ |
||
| 183 | protected $lines_id_index_mapper=array(); |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * Constructor |
||
| 188 | * |
||
| 189 | * @param DoliDB $db Database handler |
||
| 190 | */ |
||
| 191 | public function __construct($db) |
||
| 192 | { |
||
| 193 | $this->db = $db; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Return next contract ref |
||
| 198 | * |
||
| 199 | * @param Societe $soc Thirdparty object |
||
| 200 | * @return string free reference for contract |
||
| 201 | */ |
||
| 202 | public function getNextNumRef($soc) |
||
| 203 | { |
||
| 204 | global $db, $langs, $conf; |
||
| 205 | $langs->load("contracts"); |
||
| 206 | |||
| 207 | if (!empty($conf->global->CONTRACT_ADDON)) |
||
| 208 | { |
||
| 209 | $mybool = false; |
||
| 210 | |||
| 211 | $file = $conf->global->CONTRACT_ADDON.".php"; |
||
| 212 | $classname = $conf->global->CONTRACT_ADDON; |
||
| 213 | |||
| 214 | // Include file with class |
||
| 215 | $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']); |
||
| 216 | |||
| 217 | foreach ($dirmodels as $reldir) { |
||
| 218 | |||
| 219 | $dir = dol_buildpath($reldir."core/modules/contract/"); |
||
| 220 | |||
| 221 | // Load file with numbering class (if found) |
||
| 222 | $mybool|=@include_once $dir.$file; |
||
| 223 | } |
||
| 224 | |||
| 225 | if (! $mybool) |
||
|
1 ignored issue
–
show
|
|||
| 226 | { |
||
| 227 | dol_print_error('', "Failed to include file ".$file); |
||
| 228 | return ''; |
||
| 229 | } |
||
| 230 | |||
| 231 | $obj = new $classname(); |
||
| 232 | $numref = $obj->getNextValue($soc, $this); |
||
| 233 | |||
| 234 | if ( $numref != "") |
||
| 235 | { |
||
| 236 | return $numref; |
||
| 237 | } |
||
| 238 | else |
||
| 239 | { |
||
| 240 | $this->error = $obj->error; |
||
| 241 | dol_print_error($db, get_class($this)."::getNextValue ".$obj->error); |
||
| 242 | return ""; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | else |
||
| 246 | { |
||
| 247 | $langs->load("errors"); |
||
| 248 | print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete"); |
||
| 249 | return ""; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 254 | /** |
||
| 255 | * Activate a contract line |
||
| 256 | * |
||
| 257 | * @param User $user Objet User who activate contract |
||
| 258 | * @param int $line_id Id of line to activate |
||
| 259 | * @param int $date Opening date |
||
| 260 | * @param int|string $date_end Expected end date |
||
| 261 | * @param string $comment A comment typed by user |
||
| 262 | * @return int <0 if KO, >0 if OK |
||
| 263 | */ |
||
| 264 | public function active_line($user, $line_id, $date, $date_end = '', $comment = '') |
||
| 265 | { |
||
| 266 | // phpcs:enable |
||
| 267 | $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->active_line($user, $date, $date_end, $comment); |
||
| 268 | if ($result < 0) |
||
| 269 | { |
||
| 270 | $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error; |
||
| 271 | $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors; |
||
| 272 | } |
||
| 273 | return $result; |
||
| 274 | } |
||
| 275 | |||
| 276 | |||
| 277 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 278 | /** |
||
| 279 | * Close a contract line |
||
| 280 | * |
||
| 281 | * @param User $user Objet User who close contract |
||
| 282 | * @param int $line_id Id of line to close |
||
| 283 | * @param int $date_end End date |
||
| 284 | * @param string $comment A comment typed by user |
||
| 285 | * @return int <0 if KO, >0 if OK |
||
| 286 | */ |
||
| 287 | public function close_line($user, $line_id, $date_end, $comment = '') |
||
| 288 | { |
||
| 289 | // phpcs:enable |
||
| 290 | $result=$this->lines[$this->lines_id_index_mapper[$line_id]]->close_line($user, $date_end, $comment); |
||
| 291 | if ($result < 0) |
||
| 292 | { |
||
| 293 | $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error; |
||
| 294 | $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors; |
||
| 295 | } |
||
| 296 | return $result; |
||
| 297 | } |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * Open all lines of a contract |
||
| 302 | * |
||
| 303 | * @param User $user Object User making action |
||
| 304 | * @param int|string $date_start Date start (now if empty) |
||
| 305 | * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers |
||
| 306 | * @param string $comment Comment |
||
| 307 | * @return int <0 if KO, >0 if OK |
||
| 308 | * @see () |
||
| 309 | */ |
||
| 310 | public function activateAll($user, $date_start = '', $notrigger = 0, $comment = '') |
||
| 311 | { |
||
| 312 | if (empty($date_start)) $date_start = dol_now(); |
||
| 313 | |||
| 314 | $this->db->begin(); |
||
| 315 | |||
| 316 | $error=0; |
||
| 317 | |||
| 318 | // Load lines |
||
| 319 | $this->fetch_lines(); |
||
| 320 | |||
| 321 | foreach($this->lines as $contratline) |
||
| 322 | { |
||
| 323 | // Open lines not already open |
||
| 324 | if ($contratline->statut != ContratLigne::STATUS_OPEN) |
||
| 325 | { |
||
| 326 | $contratline->context = $this->context; |
||
| 327 | |||
| 328 | $result = $contratline->active_line($user, $date_start, -1, $comment); |
||
| 329 | if ($result < 0) |
||
| 330 | { |
||
| 331 | $error++; |
||
| 332 | $this->error = $contratline->error; |
||
| 333 | $this->errors = $contratline->errors; |
||
| 334 | break; |
||
| 335 | } |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | if (! $error && $this->statut == 0) |
||
| 340 | { |
||
| 341 | $result=$this->validate($user, '', $notrigger); |
||
| 342 | if ($result < 0) $error++; |
||
| 343 | } |
||
| 344 | |||
| 345 | if (! $error) |
||
| 346 | { |
||
| 347 | $this->db->commit(); |
||
| 348 | return 1; |
||
| 349 | } |
||
| 350 | else |
||
| 351 | { |
||
| 352 | $this->db->rollback(); |
||
| 353 | return -1; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Close all lines of a contract |
||
| 359 | * |
||
| 360 | * @param User $user Object User making action |
||
| 361 | * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers |
||
| 362 | * @param string $comment Comment |
||
| 363 | * @return int <0 if KO, >0 if OK |
||
| 364 | * @see activateAll() |
||
| 365 | */ |
||
| 366 | public function closeAll(User $user, $notrigger = 0, $comment = '') |
||
| 367 | { |
||
| 368 | $this->db->begin(); |
||
| 369 | |||
| 370 | // Load lines |
||
| 371 | $this->fetch_lines(); |
||
| 372 | |||
| 373 | $now = dol_now(); |
||
| 374 | |||
| 375 | $error = 0; |
||
| 376 | |||
| 377 | foreach($this->lines as $contratline) |
||
| 378 | { |
||
| 379 | // Close lines not already closed |
||
| 380 | if ($contratline->statut != ContratLigne::STATUS_CLOSED) |
||
| 381 | { |
||
| 382 | $contratline->date_cloture=$now; |
||
| 383 | $contratline->fk_user_cloture=$user->id; |
||
| 384 | $contratline->statut=ContratLigne::STATUS_CLOSED; |
||
| 385 | $result=$contratline->close_line($user, $now, $comment, $notrigger); |
||
| 386 | if ($result < 0) |
||
| 387 | { |
||
| 388 | $error++; |
||
| 389 | $this->error = $contratline->error; |
||
| 390 | $this->errors = $contratline->errors; |
||
| 391 | break; |
||
| 392 | } |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | if (! $error && $this->statut == 0) |
||
| 397 | { |
||
| 398 | $result=$this->validate($user, '', $notrigger); |
||
| 399 | if ($result < 0) $error++; |
||
| 400 | } |
||
| 401 | |||
| 402 | if (! $error) |
||
| 403 | { |
||
| 404 | $this->db->commit(); |
||
| 405 | return 1; |
||
| 406 | } |
||
| 407 | else |
||
| 408 | { |
||
| 409 | $this->db->rollback(); |
||
| 410 | return -1; |
||
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Validate a contract |
||
| 416 | * |
||
| 417 | * @param User $user Objet User |
||
| 418 | * @param string $force_number Reference to force on contract (not implemented yet) |
||
| 419 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 420 | * @return int <0 if KO, >0 if OK |
||
| 421 | */ |
||
| 422 | public function validate(User $user, $force_number = '', $notrigger = 0) |
||
| 423 | { |
||
| 424 | require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; |
||
| 425 | global $langs, $conf; |
||
| 426 | |||
| 427 | $now=dol_now(); |
||
| 428 | |||
| 429 | $error=0; |
||
| 430 | dol_syslog(get_class($this).'::validate user='.$user->id.', force_number='.$force_number); |
||
| 431 | |||
| 432 | |||
| 433 | $this->db->begin(); |
||
| 434 | |||
| 435 | $this->fetch_thirdparty(); |
||
| 436 | |||
| 437 | // A contract is validated so we can move thirdparty to status customer |
||
| 438 | if (empty($conf->global->CONTRACT_DISABLE_AUTOSET_AS_CLIENT_ON_CONTRACT_VALIDATION)) |
||
| 439 | { |
||
| 440 | $result=$this->thirdparty->set_as_client(); |
||
| 441 | } |
||
| 442 | |||
| 443 | // Define new ref |
||
| 444 | if ($force_number) |
||
| 445 | { |
||
| 446 | $num = $force_number; |
||
| 447 | } |
||
| 448 | elseif (! $error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) // empty should not happened, but when it occurs, the test save life |
||
| 449 | { |
||
| 450 | $num = $this->getNextNumRef($this->thirdparty); |
||
| 451 | } |
||
| 452 | else |
||
| 453 | { |
||
| 454 | $num = $this->ref; |
||
| 455 | } |
||
| 456 | $this->newref = $num; |
||
| 457 | |||
| 458 | if ($num) |
||
| 459 | { |
||
| 460 | $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET ref = '".$num."', statut = 1"; |
||
| 461 | //$sql.= ", fk_user_valid = ".$user->id.", date_valid = '".$this->db->idate($now)."'"; |
||
| 462 | $sql .= " WHERE rowid = ".$this->id . " AND statut = 0"; |
||
| 463 | |||
| 464 | dol_syslog(get_class($this)."::validate", LOG_DEBUG); |
||
| 465 | $resql = $this->db->query($sql); |
||
| 466 | if (! $resql) |
||
| 467 | { |
||
| 468 | dol_print_error($this->db); |
||
| 469 | $error++; |
||
| 470 | $this->error=$this->db->lasterror(); |
||
| 471 | } |
||
| 472 | |||
| 473 | // Trigger calls |
||
| 474 | if (! $error && ! $notrigger) |
||
| 475 | { |
||
| 476 | // Call trigger |
||
| 477 | $result=$this->call_trigger('CONTRACT_VALIDATE', $user); |
||
| 478 | if ($result < 0) { $error++; } |
||
| 479 | // End call triggers |
||
| 480 | } |
||
| 481 | |||
| 482 | if (! $error) |
||
| 483 | { |
||
| 484 | $this->oldref = $this->ref; |
||
| 485 | |||
| 486 | // Rename directory if dir was a temporary ref |
||
| 487 | if (preg_match('/^[\(]?PROV/i', $this->ref)) |
||
| 488 | { |
||
| 489 | // Now we rename also files into index |
||
| 490 | $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref)+1).")), filepath = 'contract/".$this->db->escape($this->newref)."'"; |
||
| 491 | $sql.= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'contract/".$this->db->escape($this->ref)."' and entity = ".$conf->entity; |
||
| 492 | $resql = $this->db->query($sql); |
||
| 493 | if (! $resql) { $error++; $this->error = $this->db->lasterror(); } |
||
| 494 | |||
| 495 | // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments |
||
| 496 | $oldref = dol_sanitizeFileName($this->ref); |
||
| 497 | $newref = dol_sanitizeFileName($num); |
||
| 498 | $dirsource = $conf->contract->dir_output.'/'.$oldref; |
||
| 499 | $dirdest = $conf->contract->dir_output.'/'.$newref; |
||
| 500 | if (! $error && file_exists($dirsource)) |
||
| 501 | { |
||
| 502 | dol_syslog(get_class($this)."::validate rename dir ".$dirsource." into ".$dirdest); |
||
| 503 | |||
| 504 | if (@rename($dirsource, $dirdest)) |
||
| 505 | { |
||
| 506 | dol_syslog("Rename ok"); |
||
| 507 | // Rename docs starting with $oldref with $newref |
||
| 508 | $listoffiles=dol_dir_list($conf->contract->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/')); |
||
| 509 | foreach($listoffiles as $fileentry) |
||
| 510 | { |
||
| 511 | $dirsource=$fileentry['name']; |
||
| 512 | $dirdest=preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource); |
||
| 513 | $dirsource=$fileentry['path'].'/'.$dirsource; |
||
| 514 | $dirdest=$fileentry['path'].'/'.$dirdest; |
||
| 515 | @rename($dirsource, $dirdest); |
||
|
1 ignored issue
–
show
|
|||
| 516 | } |
||
| 517 | } |
||
| 518 | } |
||
| 519 | } |
||
| 520 | } |
||
| 521 | |||
| 522 | // Set new ref and define current statut |
||
| 523 | if (! $error) |
||
| 524 | { |
||
| 525 | $this->ref = $num; |
||
| 526 | $this->statut = 1; |
||
| 527 | $this->brouillon = 0; |
||
| 528 | $this->date_validation = $now; |
||
| 529 | } |
||
| 530 | } |
||
| 531 | else |
||
| 532 | { |
||
| 533 | $error++; |
||
| 534 | } |
||
| 535 | |||
| 536 | if (! $error) |
||
| 537 | { |
||
| 538 | $this->db->commit(); |
||
| 539 | return 1; |
||
| 540 | } |
||
| 541 | else |
||
| 542 | { |
||
| 543 | $this->db->rollback(); |
||
| 544 | return -1; |
||
| 545 | } |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Unvalidate a contract |
||
| 550 | * |
||
| 551 | * @param User $user Object User |
||
| 552 | * @param int $notrigger 1=Does not execute triggers, 0=execute triggers |
||
| 553 | * @return int <0 if KO, >0 if OK |
||
| 554 | */ |
||
| 555 | public function reopen($user, $notrigger = 0) |
||
| 556 | { |
||
| 557 | require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; |
||
| 558 | global $langs, $conf; |
||
| 559 | |||
| 560 | $now=dol_now(); |
||
| 561 | |||
| 562 | $error=0; |
||
| 563 | dol_syslog(get_class($this).'::reopen user='.$user->id); |
||
| 564 | |||
| 565 | $this->db->begin(); |
||
| 566 | |||
| 567 | $this->fetch_thirdparty(); |
||
| 568 | |||
| 569 | $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET statut = 0"; |
||
| 570 | //$sql.= ", fk_user_valid = null, date_valid = null"; |
||
| 571 | $sql .= " WHERE rowid = ".$this->id . " AND statut = 1"; |
||
| 572 | |||
| 573 | dol_syslog(get_class($this)."::validate", LOG_DEBUG); |
||
| 574 | $resql = $this->db->query($sql); |
||
| 575 | if (! $resql) |
||
| 576 | { |
||
| 577 | dol_print_error($this->db); |
||
| 578 | $error++; |
||
| 579 | $this->error=$this->db->lasterror(); |
||
| 580 | } |
||
| 581 | |||
| 582 | // Trigger calls |
||
| 583 | if (! $error && ! $notrigger) |
||
| 584 | { |
||
| 585 | // Call trigger |
||
| 586 | $result=$this->call_trigger('CONTRACT_REOPEN', $user); |
||
| 587 | if ($result < 0) { |
||
| 588 | $error++; |
||
| 589 | } |
||
| 590 | // End call triggers |
||
| 591 | } |
||
| 592 | |||
| 593 | // Set new ref and define current status |
||
| 594 | if (! $error) |
||
| 595 | { |
||
| 596 | $this->statut=0; |
||
| 597 | $this->brouillon=1; |
||
| 598 | $this->date_validation=$now; |
||
| 599 | } |
||
| 600 | |||
| 601 | if (! $error) |
||
| 602 | { |
||
| 603 | $this->db->commit(); |
||
| 604 | return 1; |
||
| 605 | } |
||
| 606 | else |
||
| 607 | { |
||
| 608 | $this->db->rollback(); |
||
| 609 | return -1; |
||
| 610 | } |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Load a contract from database |
||
| 615 | * |
||
| 616 | * @param int $id Id of contract to load |
||
| 617 | * @param string $ref Ref |
||
| 618 | * @param string $ref_customer Customer ref |
||
| 619 | * @param string $ref_supplier Supplier ref |
||
| 620 | * @return int <0 if KO, 0 if not found, Id of contract if OK |
||
| 621 | */ |
||
| 622 | public function fetch($id, $ref = '', $ref_customer = '', $ref_supplier = '') |
||
| 623 | { |
||
| 624 | $sql = "SELECT rowid, statut, ref, fk_soc, mise_en_service as datemise,"; |
||
| 625 | $sql.= " ref_supplier, ref_customer,"; |
||
| 626 | $sql.= " ref_ext,"; |
||
| 627 | $sql.= " fk_user_mise_en_service, date_contrat as datecontrat,"; |
||
| 628 | $sql.= " fk_user_author, fin_validite, date_cloture,"; |
||
| 629 | $sql.= " fk_projet as fk_project,"; |
||
| 630 | $sql.= " fk_commercial_signature, fk_commercial_suivi,"; |
||
| 631 | $sql.= " note_private, note_public, model_pdf, extraparams"; |
||
| 632 | $sql.= " FROM ".MAIN_DB_PREFIX."contrat"; |
||
| 633 | if (! $id) $sql.=" WHERE entity IN (".getEntity('contract').")"; |
||
| 634 | else $sql.= " WHERE rowid=".$id; |
||
| 635 | if ($ref_customer) |
||
| 636 | { |
||
| 637 | $sql.= " AND ref_customer = '".$this->db->escape($ref_customer)."'"; |
||
| 638 | } |
||
| 639 | if ($ref_supplier) |
||
| 640 | { |
||
| 641 | $sql.= " AND ref_supplier = '".$this->db->escape($ref_supplier)."'"; |
||
| 642 | } |
||
| 643 | if ($ref) |
||
| 644 | { |
||
| 645 | $sql.= " AND ref='".$this->db->escape($ref)."'"; |
||
| 646 | } |
||
| 647 | |||
| 648 | dol_syslog(get_class($this)."::fetch", LOG_DEBUG); |
||
| 649 | $resql = $this->db->query($sql); |
||
| 650 | if ($resql) |
||
| 651 | { |
||
| 652 | $obj = $this->db->fetch_object($resql); |
||
| 653 | |||
| 654 | if ($obj) |
||
| 655 | { |
||
| 656 | $this->id = $obj->rowid; |
||
| 657 | $this->ref = (!isset($obj->ref) || !$obj->ref) ? $obj->rowid : $obj->ref; |
||
| 658 | $this->ref_customer = $obj->ref_customer; |
||
| 659 | $this->ref_supplier = $obj->ref_supplier; |
||
| 660 | $this->ref_ext = $obj->ref_ext; |
||
| 661 | $this->statut = $obj->statut; |
||
| 662 | $this->mise_en_service = $this->db->jdate($obj->datemise); |
||
| 663 | |||
| 664 | $this->date_contrat = $this->db->jdate($obj->datecontrat); |
||
| 665 | $this->date_creation = $this->db->jdate($obj->datecontrat); |
||
| 666 | |||
| 667 | $this->fin_validite = $this->db->jdate($obj->fin_validite); |
||
| 668 | $this->date_cloture = $this->db->jdate($obj->date_cloture); |
||
| 669 | |||
| 670 | |||
| 671 | $this->user_author_id = $obj->fk_user_author; |
||
| 672 | |||
| 673 | $this->commercial_signature_id = $obj->fk_commercial_signature; |
||
| 674 | $this->commercial_suivi_id = $obj->fk_commercial_suivi; |
||
| 675 | |||
| 676 | $this->note_private = $obj->note_private; |
||
| 677 | $this->note_public = $obj->note_public; |
||
| 678 | $this->modelpdf = $obj->model_pdf; |
||
| 679 | |||
| 680 | $this->fk_projet = $obj->fk_project; // deprecated |
||
| 681 | $this->fk_project = $obj->fk_project; |
||
| 682 | |||
| 683 | $this->socid = $obj->fk_soc; |
||
| 684 | $this->fk_soc = $obj->fk_soc; |
||
| 685 | |||
| 686 | $this->extraparams = (array) json_decode($obj->extraparams, true); |
||
| 687 | |||
| 688 | $this->db->free($resql); |
||
| 689 | |||
| 690 | // Retreive all extrafields |
||
| 691 | // fetch optionals attributes and labels |
||
| 692 | $this->fetch_optionals(); |
||
| 693 | |||
| 694 | // Lines |
||
| 695 | $result=$this->fetch_lines(); |
||
| 696 | if ($result < 0) |
||
| 697 | { |
||
| 698 | $this->error=$this->db->lasterror(); |
||
| 699 | return -3; |
||
| 700 | } |
||
| 701 | |||
| 702 | return $this->id; |
||
| 703 | } |
||
| 704 | else |
||
| 705 | { |
||
| 706 | dol_syslog(get_class($this)."::fetch Contract not found"); |
||
| 707 | $this->error="Contract not found"; |
||
| 708 | return 0; |
||
| 709 | } |
||
| 710 | } |
||
| 711 | else |
||
| 712 | { |
||
| 713 | dol_syslog(get_class($this)."::fetch Error searching contract"); |
||
| 714 | $this->error=$this->db->error(); |
||
| 715 | return -1; |
||
| 716 | } |
||
| 717 | } |
||
| 718 | |||
| 719 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 720 | /** |
||
| 721 | * Load lines array into this->lines. |
||
| 722 | * This set also nbofserviceswait, nbofservicesopened, nbofservicesexpired and nbofservicesclosed |
||
| 723 | * |
||
| 724 | * @param int $only_product Return only physical products |
||
| 725 | * @param int $loadalsotranslation Return translation for products |
||
| 726 | * |
||
| 727 | * @return ContratLigne[] Return array of contract lines |
||
| 728 | */ |
||
| 729 | public function fetch_lines($only_product = 0, $loadalsotranslation = 0) |
||
| 730 | { |
||
| 731 | global $langs, $conf; |
||
| 732 | // phpcs:enable |
||
| 733 | $this->nbofserviceswait=0; |
||
| 734 | $this->nbofservicesopened=0; |
||
| 735 | $this->nbofservicesexpired=0; |
||
| 736 | $this->nbofservicesclosed=0; |
||
| 737 | |||
| 738 | $total_ttc=0; |
||
| 739 | $total_vat=0; |
||
| 740 | $total_ht=0; |
||
| 741 | |||
| 742 | $now=dol_now(); |
||
| 743 | |||
| 744 | require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; |
||
| 745 | $extrafieldsline=new ExtraFields($this->db); |
||
| 746 | $line = new ContratLigne($this->db); |
||
| 747 | $extralabelsline=$extrafieldsline->fetch_name_optionals_label($line->table_element, true); |
||
| 748 | |||
| 749 | $this->lines=array(); |
||
| 750 | $pos = 0; |
||
| 751 | |||
| 752 | // Selects contract lines related to a product |
||
| 753 | $sql = "SELECT p.label as product_label, p.description as product_desc, p.ref as product_ref,"; |
||
| 754 | $sql.= " d.rowid, d.fk_contrat, d.statut, d.description, d.price_ht, d.vat_src_code, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.localtax1_type, d.localtax2_type, d.qty, d.remise_percent, d.subprice, d.fk_product_fournisseur_price as fk_fournprice, d.buy_price_ht as pa_ht,"; |
||
| 755 | $sql.= " d.total_ht,"; |
||
| 756 | $sql.= " d.total_tva,"; |
||
| 757 | $sql.= " d.total_localtax1,"; |
||
| 758 | $sql.= " d.total_localtax2,"; |
||
| 759 | $sql.= " d.total_ttc,"; |
||
| 760 | $sql.= " d.info_bits, d.fk_product,"; |
||
| 761 | $sql.= " d.date_ouverture_prevue, d.date_ouverture,"; |
||
| 762 | $sql.= " d.date_fin_validite, d.date_cloture,"; |
||
| 763 | $sql.= " d.fk_user_author,"; |
||
| 764 | $sql.= " d.fk_user_ouverture,"; |
||
| 765 | $sql.= " d.fk_user_cloture,"; |
||
| 766 | $sql.= " d.fk_unit"; |
||
| 767 | $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as d LEFT JOIN ".MAIN_DB_PREFIX."product as p ON d.fk_product = p.rowid"; |
||
| 768 | $sql.= " WHERE d.fk_contrat = ".$this->id; |
||
| 769 | $sql.= " ORDER by d.rowid ASC"; |
||
| 770 | |||
| 771 | dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG); |
||
| 772 | $result = $this->db->query($sql); |
||
| 773 | if ($result) |
||
| 774 | { |
||
| 775 | $num = $this->db->num_rows($result); |
||
| 776 | $i = 0; |
||
| 777 | |||
| 778 | while ($i < $num) |
||
| 779 | { |
||
| 780 | $objp = $this->db->fetch_object($result); |
||
| 781 | |||
| 782 | $line = new ContratLigne($this->db); |
||
| 783 | $line->id = $objp->rowid; |
||
| 784 | $line->ref = $objp->rowid; |
||
| 785 | $line->fk_contrat = $objp->fk_contrat; |
||
| 786 | $line->desc = $objp->description; // Description line |
||
| 787 | $line->qty = $objp->qty; |
||
| 788 | $line->vat_src_code = $objp->vat_src_code ; |
||
| 789 | $line->tva_tx = $objp->tva_tx; |
||
| 790 | $line->localtax1_tx = $objp->localtax1_tx; |
||
| 791 | $line->localtax2_tx = $objp->localtax2_tx; |
||
| 792 | $line->localtax1_type = $objp->localtax1_type; |
||
| 793 | $line->localtax2_type = $objp->localtax2_type; |
||
| 794 | $line->subprice = $objp->subprice; |
||
| 795 | $line->statut = $objp->statut; |
||
| 796 | $line->remise_percent = $objp->remise_percent; |
||
| 797 | $line->price_ht = $objp->price_ht; |
||
| 798 | $line->price = $objp->price_ht; // For backward compatibility |
||
| 799 | $line->total_ht = $objp->total_ht; |
||
| 800 | $line->total_tva = $objp->total_tva; |
||
| 801 | $line->total_localtax1 = $objp->total_localtax1; |
||
| 802 | $line->total_localtax2 = $objp->total_localtax2; |
||
| 803 | $line->total_ttc = $objp->total_ttc; |
||
| 804 | $line->fk_product = (($objp->fk_product > 0)?$objp->fk_product:0); |
||
| 805 | $line->info_bits = $objp->info_bits; |
||
| 806 | |||
| 807 | $line->fk_fournprice = $objp->fk_fournprice; |
||
| 808 | $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht); |
||
| 809 | $line->pa_ht = $marginInfos[0]; |
||
| 810 | |||
| 811 | $line->fk_user_author = $objp->fk_user_author; |
||
| 812 | $line->fk_user_ouverture= $objp->fk_user_ouverture; |
||
| 813 | $line->fk_user_cloture = $objp->fk_user_cloture; |
||
| 814 | $line->fk_unit = $objp->fk_unit; |
||
| 815 | |||
| 816 | $line->ref = $objp->product_ref; // deprecated |
||
| 817 | $line->product_ref = $objp->product_ref; // Product Ref |
||
| 818 | $line->product_desc = $objp->product_desc; // Product Description |
||
| 819 | $line->product_label = $objp->product_label; // Product Label |
||
| 820 | |||
| 821 | $line->description = $objp->description; |
||
| 822 | |||
| 823 | $line->date_start = $this->db->jdate($objp->date_ouverture_prevue); |
||
| 824 | $line->date_start_real = $this->db->jdate($objp->date_ouverture); |
||
| 825 | $line->date_end = $this->db->jdate($objp->date_fin_validite); |
||
| 826 | $line->date_end_real = $this->db->jdate($objp->date_cloture); |
||
| 827 | // For backward compatibility |
||
| 828 | $line->date_ouverture_prevue = $this->db->jdate($objp->date_ouverture_prevue); |
||
| 829 | $line->date_ouverture = $this->db->jdate($objp->date_ouverture); |
||
| 830 | $line->date_fin_validite = $this->db->jdate($objp->date_fin_validite); |
||
| 831 | $line->date_cloture = $this->db->jdate($objp->date_cloture); |
||
| 832 | $line->date_debut_prevue = $this->db->jdate($objp->date_ouverture_prevue); |
||
| 833 | $line->date_debut_reel = $this->db->jdate($objp->date_ouverture); |
||
| 834 | $line->date_fin_prevue = $this->db->jdate($objp->date_fin_validite); |
||
| 835 | $line->date_fin_reel = $this->db->jdate($objp->date_cloture); |
||
| 836 | |||
| 837 | // Retreive all extrafields for contract |
||
| 838 | // fetch optionals attributes and labels |
||
| 839 | $line->fetch_optionals(); |
||
| 840 | |||
| 841 | // multilangs |
||
| 842 | if (! empty($conf->global->MAIN_MULTILANGS) && ! empty($objp->fk_product) && ! empty($loadalsotranslation)) { |
||
| 843 | $line = new Product($this->db); |
||
| 844 | $line->fetch($objp->fk_product); |
||
| 845 | $line->getMultiLangs(); |
||
| 846 | } |
||
| 847 | |||
| 848 | $this->lines[$pos] = $line; |
||
| 849 | $this->lines_id_index_mapper[$line->id] = $pos; |
||
| 850 | |||
| 851 | //dol_syslog("1 ".$line->desc); |
||
| 852 | //dol_syslog("2 ".$line->product_desc); |
||
| 853 | |||
| 854 | if ($line->statut == ContratLigne::STATUS_INITIAL) $this->nbofserviceswait++; |
||
| 855 | if ($line->statut == ContratLigne::STATUS_OPEN && (empty($line->date_fin_prevue) || $line->date_fin_prevue >= $now)) $this->nbofservicesopened++; |
||
| 856 | if ($line->statut == ContratLigne::STATUS_OPEN && (! empty($line->date_fin_prevue) && $line->date_fin_prevue < $now)) $this->nbofservicesexpired++; |
||
| 857 | if ($line->statut == ContratLigne::STATUS_CLOSED) $this->nbofservicesclosed++; |
||
| 858 | |||
| 859 | $total_ttc+=$objp->total_ttc; // TODO Not saved into database |
||
| 860 | $total_vat+=$objp->total_tva; |
||
| 861 | $total_ht+=$objp->total_ht; |
||
| 862 | |||
| 863 | $i++; |
||
| 864 | $pos++; |
||
| 865 | } |
||
| 866 | $this->db->free($result); |
||
| 867 | } |
||
| 868 | else |
||
| 869 | { |
||
| 870 | dol_syslog(get_class($this)."::Fetch Error when reading lines of contracts linked to products"); |
||
| 871 | return -3; |
||
| 872 | } |
||
| 873 | |||
| 874 | $this->nbofservices=count($this->lines); |
||
| 875 | $this->total_ttc = price2num($total_ttc); // TODO For the moment value is false as value is not stored in database for line linked to products |
||
| 876 | $this->total_vat = price2num($total_vat); // TODO For the moment value is false as value is not stored in database for line linked to products |
||
| 877 | $this->total_ht = price2num($total_ht); // TODO For the moment value is false as value is not stored in database for line linked to products |
||
| 878 | |||
| 879 | return $this->lines; |
||
| 880 | } |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Create a contract into database |
||
| 884 | * |
||
| 885 | * @param User $user User that create |
||
| 886 | * @return int <0 if KO, id of contract if OK |
||
| 887 | */ |
||
| 888 | public function create($user) |
||
| 889 | { |
||
| 890 | global $conf,$langs,$mysoc; |
||
| 891 | |||
| 892 | // Check parameters |
||
| 893 | $paramsok=1; |
||
| 894 | if ($this->commercial_signature_id <= 0) |
||
| 895 | { |
||
| 896 | $langs->load("commercial"); |
||
| 897 | $this->error.=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("SalesRepresentativeSignature")); |
||
| 898 | $paramsok=0; |
||
| 899 | } |
||
| 900 | if ($this->commercial_suivi_id <= 0) |
||
| 901 | { |
||
| 902 | $langs->load("commercial"); |
||
| 903 | $this->error.=($this->error?"<br>":''); |
||
| 904 | $this->error.=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("SalesRepresentativeFollowUp")); |
||
| 905 | $paramsok=0; |
||
| 906 | } |
||
| 907 | if (! $paramsok) return -1; |
||
| 908 | |||
| 909 | |||
| 910 | $this->db->begin(); |
||
| 911 | |||
| 912 | $now=dol_now(); |
||
| 913 | |||
| 914 | // Insert contract |
||
| 915 | $sql = "INSERT INTO ".MAIN_DB_PREFIX."contrat (datec, fk_soc, fk_user_author, date_contrat,"; |
||
| 916 | $sql.= " fk_commercial_signature, fk_commercial_suivi, fk_projet,"; |
||
| 917 | $sql.= " ref, entity, note_private, note_public, ref_customer, ref_supplier, ref_ext)"; |
||
| 918 | $sql.= " VALUES ('".$this->db->idate($now)."',".$this->socid.",".$user->id; |
||
| 919 | $sql.= ", ".(dol_strlen($this->date_contrat)!=0 ? "'".$this->db->idate($this->date_contrat)."'" : "NULL"); |
||
| 920 | $sql.= ",".($this->commercial_signature_id>0?$this->commercial_signature_id:"NULL"); |
||
| 921 | $sql.= ",".($this->commercial_suivi_id>0?$this->commercial_suivi_id:"NULL"); |
||
| 922 | $sql.= ",".($this->fk_project>0?$this->fk_project:"NULL"); |
||
| 923 | $sql.= ", ".(dol_strlen($this->ref)<=0 ? "null" : "'".$this->db->escape($this->ref)."'"); |
||
| 924 | $sql.= ", ".$conf->entity; |
||
| 925 | $sql.= ", ".(!empty($this->note_private)?("'".$this->db->escape($this->note_private)."'"):"NULL"); |
||
| 926 | $sql.= ", ".(!empty($this->note_public)?("'".$this->db->escape($this->note_public)."'"):"NULL"); |
||
| 927 | $sql.= ", ".(!empty($this->ref_customer)?("'".$this->db->escape($this->ref_customer)."'"):"NULL"); |
||
| 928 | $sql.= ", ".(!empty($this->ref_supplier)?("'".$this->db->escape($this->ref_supplier)."'"):"NULL"); |
||
| 929 | $sql.= ", ".(!empty($this->ref_ext)?("'".$this->db->escape($this->ref_ext)."'"):"NULL"); |
||
| 930 | $sql.= ")"; |
||
| 931 | $resql=$this->db->query($sql); |
||
| 932 | |||
| 933 | if ($resql) |
||
| 934 | { |
||
| 935 | $error=0; |
||
| 936 | |||
| 937 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."contrat"); |
||
| 938 | |||
| 939 | // Load object modContract |
||
| 940 | $module=(! empty($conf->global->CONTRACT_ADDON)?$conf->global->CONTRACT_ADDON:'mod_contract_serpis'); |
||
| 941 | if (substr($module, 0, 13) == 'mod_contract_' && substr($module, -3) == 'php') |
||
| 942 | { |
||
| 943 | $module = substr($module, 0, dol_strlen($module)-4); |
||
| 944 | } |
||
| 945 | $result=dol_include_once('/core/modules/contract/'.$module.'.php'); |
||
| 946 | if ($result > 0) |
||
| 947 | { |
||
| 948 | $modCodeContract = new $module(); |
||
| 949 | |||
| 950 | if (! empty($modCodeContract->code_auto)) { |
||
| 951 | // Force the ref to a draft value if numbering module is an automatic numbering |
||
| 952 | $sql = 'UPDATE '.MAIN_DB_PREFIX."contrat SET ref='(PROV".$this->id.")' WHERE rowid=".$this->id; |
||
| 953 | if ($this->db->query($sql)) |
||
| 954 | { |
||
| 955 | if ($this->id) |
||
| 956 | { |
||
| 957 | $this->ref="(PROV".$this->id.")"; |
||
| 958 | } |
||
| 959 | } |
||
| 960 | } |
||
| 961 | } |
||
| 962 | |||
| 963 | if (! $error && empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) |
||
| 964 | { |
||
| 965 | $result=$this->insertExtraFields(); |
||
| 966 | if ($result < 0) |
||
| 967 | { |
||
| 968 | $error++; |
||
| 969 | } |
||
| 970 | } |
||
| 971 | |||
| 972 | // Insert business contacts ('SALESREPSIGN','contrat') |
||
| 973 | if (! $error) |
||
| 974 | { |
||
| 975 | $result=$this->add_contact($this->commercial_signature_id, 'SALESREPSIGN', 'internal'); |
||
| 976 | if ($result < 0) $error++; |
||
| 977 | } |
||
| 978 | |||
| 979 | // Insert business contacts ('SALESREPFOLL','contrat') |
||
| 980 | if (! $error) |
||
| 981 | { |
||
| 982 | $result=$this->add_contact($this->commercial_suivi_id, 'SALESREPFOLL', 'internal'); |
||
| 983 | if ($result < 0) $error++; |
||
| 984 | } |
||
| 985 | |||
| 986 | if (! $error) |
||
| 987 | { |
||
| 988 | if (! empty($this->linkedObjectsIds) && empty($this->linked_objects)) // To use new linkedObjectsIds instead of old linked_objects |
||
| 989 | { |
||
| 990 | $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds |
||
| 991 | } |
||
| 992 | |||
| 993 | // Add object linked |
||
| 994 | if (! $error && $this->id && is_array($this->linked_objects) && ! empty($this->linked_objects)) |
||
| 995 | { |
||
| 996 | foreach($this->linked_objects as $origin => $tmp_origin_id) |
||
| 997 | { |
||
| 998 | if (is_array($tmp_origin_id)) // New behaviour, if linked_object can have several links per type, so is something like array('contract'=>array(id1, id2, ...)) |
||
| 999 | { |
||
| 1000 | foreach($tmp_origin_id as $origin_id) |
||
| 1001 | { |
||
| 1002 | $ret = $this->add_object_linked($origin, $origin_id); |
||
| 1003 | if (! $ret) |
||
| 1004 | { |
||
| 1005 | $this->error=$this->db->lasterror(); |
||
| 1006 | $error++; |
||
| 1007 | } |
||
| 1008 | } |
||
| 1009 | } |
||
| 1010 | else // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1)) |
||
| 1011 | { |
||
| 1012 | $origin_id = $tmp_origin_id; |
||
| 1013 | $ret = $this->add_object_linked($origin, $origin_id); |
||
| 1014 | if (! $ret) |
||
| 1015 | { |
||
| 1016 | $this->error=$this->db->lasterror(); |
||
| 1017 | $error++; |
||
| 1018 | } |
||
| 1019 | } |
||
| 1020 | } |
||
| 1021 | } |
||
| 1022 | |||
| 1023 | if (! $error && $this->id && ! empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && ! empty($this->origin) && ! empty($this->origin_id)) // Get contact from origin object |
||
| 1024 | { |
||
| 1025 | $originforcontact = $this->origin; |
||
| 1026 | $originidforcontact = $this->origin_id; |
||
| 1027 | if ($originforcontact == 'shipping') // shipment and order share the same contacts. If creating from shipment we take data of order |
||
| 1028 | { |
||
| 1029 | require_once DOL_DOCUMENT_ROOT . '/expedition/class/expedition.class.php'; |
||
| 1030 | $exp = new Expedition($db); |
||
| 1031 | $exp->fetch($this->origin_id); |
||
| 1032 | $exp->fetchObjectLinked(); |
||
| 1033 | if (count($exp->linkedObjectsIds['commande']) > 0) |
||
| 1034 | { |
||
| 1035 | foreach ($exp->linkedObjectsIds['commande'] as $key => $value) |
||
| 1036 | { |
||
| 1037 | $originforcontact = 'commande'; |
||
| 1038 | $originidforcontact = $value->id; |
||
| 1039 | break; // We take first one |
||
| 1040 | } |
||
| 1041 | } |
||
| 1042 | } |
||
| 1043 | |||
| 1044 | $sqlcontact = "SELECT ctc.code, ctc.source, ec.fk_socpeople FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as ctc"; |
||
| 1045 | $sqlcontact.= " WHERE element_id = ".$originidforcontact." AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$originforcontact."'"; |
||
| 1046 | |||
| 1047 | $resqlcontact = $this->db->query($sqlcontact); |
||
| 1048 | if ($resqlcontact) |
||
| 1049 | { |
||
| 1050 | while($objcontact = $this->db->fetch_object($resqlcontact)) |
||
| 1051 | { |
||
| 1052 | if ($objcontact->source == 'internal' && in_array($objcontact->code, array('SALESREPSIGN', 'SALESREPFOLL'))) continue; // ignore this, already forced previously |
||
| 1053 | |||
| 1054 | //print $objcontact->code.'-'.$objcontact->source.'-'.$objcontact->fk_socpeople."\n"; |
||
| 1055 | $this->add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source); // May failed because of duplicate key or because code of contact type does not exists for new object |
||
| 1056 | } |
||
| 1057 | } |
||
| 1058 | else dol_print_error($resqlcontact); |
||
| 1059 | } |
||
| 1060 | } |
||
| 1061 | |||
| 1062 | if (! $error) |
||
| 1063 | { |
||
| 1064 | // Call trigger |
||
| 1065 | $result=$this->call_trigger('CONTRACT_CREATE', $user); |
||
| 1066 | if ($result < 0) { $error++; } |
||
| 1067 | // End call triggers |
||
| 1068 | |||
| 1069 | if (! $error) |
||
| 1070 | { |
||
| 1071 | $this->db->commit(); |
||
| 1072 | return $this->id; |
||
| 1073 | } |
||
| 1074 | else |
||
| 1075 | { |
||
| 1076 | dol_syslog(get_class($this)."::create - 30 - ".$this->error, LOG_ERR); |
||
| 1077 | $this->db->rollback(); |
||
| 1078 | return -3; |
||
| 1079 | } |
||
| 1080 | } |
||
| 1081 | else |
||
| 1082 | { |
||
| 1083 | $this->error="Failed to add contract"; |
||
| 1084 | dol_syslog(get_class($this)."::create - 20 - ".$this->error, LOG_ERR); |
||
| 1085 | $this->db->rollback(); |
||
| 1086 | return -2; |
||
| 1087 | } |
||
| 1088 | } |
||
| 1089 | else |
||
| 1090 | { |
||
| 1091 | $this->error=$langs->trans("UnknownError: ".$this->db->error()." -", LOG_DEBUG); |
||
| 1092 | |||
| 1093 | $this->db->rollback(); |
||
| 1094 | return -1; |
||
| 1095 | } |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | |||
| 1099 | /** |
||
| 1100 | * Supprime l'objet de la base |
||
| 1101 | * |
||
| 1102 | * @param User $user Utilisateur qui supprime |
||
| 1103 | * @return int < 0 si erreur, > 0 si ok |
||
| 1104 | */ |
||
| 1105 | public function delete($user) |
||
| 1106 | { |
||
| 1107 | global $conf, $langs; |
||
| 1108 | require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; |
||
| 1109 | |||
| 1110 | $error=0; |
||
| 1111 | |||
| 1112 | $this->db->begin(); |
||
| 1113 | |||
| 1114 | // Call trigger |
||
| 1115 | $result=$this->call_trigger('CONTRACT_DELETE', $user); |
||
| 1116 | if ($result < 0) { $error++; } |
||
| 1117 | // End call triggers |
||
| 1118 | |||
| 1119 | if (! $error) |
||
| 1120 | { |
||
| 1121 | // Delete linked contacts |
||
| 1122 | $res = $this->delete_linked_contact(); |
||
| 1123 | if ($res < 0) |
||
| 1124 | { |
||
| 1125 | dol_syslog(get_class($this)."::delete error", LOG_ERR); |
||
| 1126 | $error++; |
||
| 1127 | } |
||
| 1128 | } |
||
| 1129 | |||
| 1130 | if (! $error) |
||
| 1131 | { |
||
| 1132 | // Delete linked object |
||
| 1133 | $res = $this->deleteObjectLinked(); |
||
| 1134 | if ($res < 0) $error++; |
||
| 1135 | } |
||
| 1136 | |||
| 1137 | if (! $error) |
||
| 1138 | { |
||
| 1139 | // Delete contratdet_log |
||
| 1140 | /* |
||
| 1141 | $sql = "DELETE cdl"; |
||
| 1142 | $sql.= " FROM ".MAIN_DB_PREFIX."contratdet_log as cdl, ".MAIN_DB_PREFIX."contratdet as cd"; |
||
| 1143 | $sql.= " WHERE cdl.fk_contratdet=cd.rowid AND cd.fk_contrat=".$this->id; |
||
| 1144 | */ |
||
| 1145 | $sql = "SELECT cdl.rowid as cdlrowid "; |
||
| 1146 | $sql.= " FROM ".MAIN_DB_PREFIX."contratdet_log as cdl, ".MAIN_DB_PREFIX."contratdet as cd"; |
||
| 1147 | $sql.= " WHERE cdl.fk_contratdet=cd.rowid AND cd.fk_contrat=".$this->id; |
||
| 1148 | |||
| 1149 | dol_syslog(get_class($this)."::delete contratdet_log", LOG_DEBUG); |
||
| 1150 | $resql=$this->db->query($sql); |
||
| 1151 | if (! $resql) |
||
| 1152 | { |
||
| 1153 | $this->error=$this->db->error(); |
||
| 1154 | $error++; |
||
| 1155 | } |
||
| 1156 | $numressql=$this->db->num_rows($resql); |
||
| 1157 | if (! $error && $numressql ) |
||
| 1158 | { |
||
| 1159 | $tab_resql=array(); |
||
| 1160 | for($i=0;$i<$numressql;$i++) |
||
| 1161 | { |
||
| 1162 | $objresql=$this->db->fetch_object($resql); |
||
| 1163 | $tab_resql[]= $objresql->cdlrowid; |
||
| 1164 | } |
||
| 1165 | $this->db->free($resql); |
||
| 1166 | |||
| 1167 | $sql= "DELETE FROM ".MAIN_DB_PREFIX."contratdet_log "; |
||
| 1168 | $sql.= " WHERE ".MAIN_DB_PREFIX."contratdet_log.rowid IN (".implode(",", $tab_resql).")"; |
||
| 1169 | |||
| 1170 | dol_syslog(get_class($this)."::delete contratdet_log", LOG_DEBUG); |
||
| 1171 | $resql=$this->db->query($sql); |
||
| 1172 | if (! $resql) |
||
| 1173 | { |
||
| 1174 | $this->error=$this->db->error(); |
||
| 1175 | $error++; |
||
| 1176 | } |
||
| 1177 | } |
||
| 1178 | } |
||
| 1179 | |||
| 1180 | if (! $error) |
||
| 1181 | { |
||
| 1182 | // Delete contratdet |
||
| 1183 | $sql = "DELETE FROM ".MAIN_DB_PREFIX."contratdet"; |
||
| 1184 | $sql.= " WHERE fk_contrat=".$this->id; |
||
| 1185 | |||
| 1186 | dol_syslog(get_class($this)."::delete contratdet", LOG_DEBUG); |
||
| 1187 | $resql=$this->db->query($sql); |
||
| 1188 | if (! $resql) |
||
| 1189 | { |
||
| 1190 | $this->error=$this->db->error(); |
||
| 1191 | $error++; |
||
| 1192 | } |
||
| 1193 | } |
||
| 1194 | |||
| 1195 | if (! $error) |
||
| 1196 | { |
||
| 1197 | // Delete contrat |
||
| 1198 | $sql = "DELETE FROM ".MAIN_DB_PREFIX."contrat"; |
||
| 1199 | $sql.= " WHERE rowid=".$this->id; |
||
| 1200 | |||
| 1201 | dol_syslog(get_class($this)."::delete contrat", LOG_DEBUG); |
||
| 1202 | $resql=$this->db->query($sql); |
||
| 1203 | if (! $resql) |
||
| 1204 | { |
||
| 1205 | $this->error=$this->db->error(); |
||
| 1206 | $error++; |
||
| 1207 | } |
||
| 1208 | } |
||
| 1209 | |||
| 1210 | // Removed extrafields |
||
| 1211 | if (! $error) { |
||
| 1212 | $result=$this->deleteExtraFields(); |
||
| 1213 | if ($result < 0) |
||
| 1214 | { |
||
| 1215 | $error++; |
||
| 1216 | dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR); |
||
| 1217 | } |
||
| 1218 | } |
||
| 1219 | |||
| 1220 | if (! $error) |
||
| 1221 | { |
||
| 1222 | // We remove directory |
||
| 1223 | $ref = dol_sanitizeFileName($this->ref); |
||
| 1224 | if ($conf->contrat->dir_output) |
||
| 1225 | { |
||
| 1226 | $dir = $conf->contrat->dir_output . "/" . $ref; |
||
| 1227 | if (file_exists($dir)) |
||
| 1228 | { |
||
| 1229 | $res=@dol_delete_dir_recursive($dir); |
||
| 1230 | if (! $res) |
||
| 1231 | { |
||
| 1232 | $this->error='ErrorFailToDeleteDir'; |
||
| 1233 | $error++; |
||
| 1234 | } |
||
| 1235 | } |
||
| 1236 | } |
||
| 1237 | } |
||
| 1238 | |||
| 1239 | if (! $error) |
||
| 1240 | { |
||
| 1241 | $this->db->commit(); |
||
| 1242 | return 1; |
||
| 1243 | } |
||
| 1244 | else |
||
| 1245 | { |
||
| 1246 | $this->error=$this->db->lasterror(); |
||
| 1247 | $this->db->rollback(); |
||
| 1248 | return -1; |
||
| 1249 | } |
||
| 1250 | } |
||
| 1251 | |||
| 1252 | /** |
||
| 1253 | * Update object into database |
||
| 1254 | * |
||
| 1255 | * @param User $user User that modifies |
||
| 1256 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 1257 | * @return int <0 if KO, >0 if OK |
||
| 1258 | */ |
||
| 1259 | public function update($user, $notrigger = 0) |
||
| 1260 | { |
||
| 1261 | global $conf, $langs; |
||
| 1262 | $error=0; |
||
| 1263 | |||
| 1264 | // Clean parameters |
||
| 1265 | if (empty($this->fk_commercial_signature) && $this->commercial_signature_id > 0) $this->fk_commercial_signature = $this->commercial_signature_id; |
||
| 1266 | if (empty($this->fk_commercial_suivi) && $this->commercial_suivi_id > 0) $this->fk_commercial_suivi = $this->commercial_suivi_id; |
||
| 1267 | if (empty($this->fk_soc) && $this->socid > 0) $this->fk_soc = $this->socid; |
||
| 1268 | if (empty($this->fk_project) && $this->projet > 0) $this->fk_project = $this->projet; |
||
|
1 ignored issue
–
show
|
|||
| 1269 | |||
| 1270 | if (isset($this->ref)) $this->ref=trim($this->ref); |
||
| 1271 | if (isset($this->ref_customer)) $this->ref_customer=trim($this->ref_customer); |
||
| 1272 | if (isset($this->ref_supplier)) $this->ref_supplier=trim($this->ref_supplier); |
||
| 1273 | if (isset($this->ref_ext)) $this->ref_ext=trim($this->ref_ext); |
||
| 1274 | if (isset($this->entity)) $this->entity=trim($this->entity); |
||
| 1275 | if (isset($this->statut)) $this->statut=(int) $this->statut; |
||
| 1276 | if (isset($this->fk_soc)) $this->fk_soc=trim($this->fk_soc); |
||
| 1277 | if (isset($this->fk_commercial_signature)) $this->fk_commercial_signature=trim($this->fk_commercial_signature); |
||
| 1278 | if (isset($this->fk_commercial_suivi)) $this->fk_commercial_suivi=trim($this->fk_commercial_suivi); |
||
| 1279 | if (isset($this->fk_user_mise_en_service)) $this->fk_user_mise_en_service=trim($this->fk_user_mise_en_service); |
||
| 1280 | if (isset($this->fk_user_cloture)) $this->fk_user_cloture=trim($this->fk_user_cloture); |
||
| 1281 | if (isset($this->note_private)) $this->note_private=trim($this->note_private); |
||
| 1282 | if (isset($this->note_public)) $this->note_public=trim($this->note_public); |
||
| 1283 | if (isset($this->import_key)) $this->import_key=trim($this->import_key); |
||
| 1284 | //if (isset($this->extraparams)) $this->extraparams=trim($this->extraparams); |
||
| 1285 | |||
| 1286 | // Check parameters |
||
| 1287 | // Put here code to add a control on parameters values |
||
| 1288 | |||
| 1289 | // Update request |
||
| 1290 | $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET"; |
||
| 1291 | $sql.= " ref=".(isset($this->ref)?"'".$this->db->escape($this->ref)."'":"null").","; |
||
| 1292 | $sql.= " ref_customer=".(isset($this->ref_customer)?"'".$this->db->escape($this->ref_customer)."'":"null").","; |
||
| 1293 | $sql.= " ref_supplier=".(isset($this->ref_supplier)?"'".$this->db->escape($this->ref_supplier)."'":"null").","; |
||
| 1294 | $sql.= " ref_ext=".(isset($this->ref_ext)?"'".$this->db->escape($this->ref_ext)."'":"null").","; |
||
| 1295 | $sql.= " entity=".$conf->entity.","; |
||
| 1296 | $sql.= " date_contrat=".(dol_strlen($this->date_contrat)!=0 ? "'".$this->db->idate($this->date_contrat)."'" : 'null').","; |
||
| 1297 | $sql.= " statut=".(isset($this->statut)?$this->statut:"null").","; |
||
| 1298 | $sql.= " mise_en_service=".(dol_strlen($this->mise_en_service)!=0 ? "'".$this->db->idate($this->mise_en_service)."'" : 'null').","; |
||
| 1299 | $sql.= " fin_validite=".(dol_strlen($this->fin_validite)!=0 ? "'".$this->db->idate($this->fin_validite)."'" : 'null').","; |
||
| 1300 | $sql.= " date_cloture=".(dol_strlen($this->date_cloture)!=0 ? "'".$this->db->idate($this->date_cloture)."'" : 'null').","; |
||
| 1301 | $sql.= " fk_soc=".($this->fk_soc > 0 ? $this->fk_soc:"null").","; |
||
| 1302 | $sql.= " fk_projet=".($this->fk_project > 0 ? $this->fk_project:"null").","; |
||
| 1303 | $sql.= " fk_commercial_signature=".(isset($this->fk_commercial_signature)?$this->fk_commercial_signature:"null").","; |
||
| 1304 | $sql.= " fk_commercial_suivi=".(isset($this->fk_commercial_suivi)?$this->fk_commercial_suivi:"null").","; |
||
| 1305 | $sql.= " fk_user_mise_en_service=".(isset($this->fk_user_mise_en_service)?$this->fk_user_mise_en_service:"null").","; |
||
| 1306 | $sql.= " fk_user_cloture=".(isset($this->fk_user_cloture)?$this->fk_user_cloture:"null").","; |
||
| 1307 | $sql.= " note_private=".(isset($this->note_private)?"'".$this->db->escape($this->note_private)."'":"null").","; |
||
| 1308 | $sql.= " note_public=".(isset($this->note_public)?"'".$this->db->escape($this->note_public)."'":"null").","; |
||
| 1309 | $sql.= " import_key=".(isset($this->import_key)?"'".$this->db->escape($this->import_key)."'":"null").""; |
||
| 1310 | //$sql.= " extraparams=".(isset($this->extraparams)?"'".$this->db->escape($this->extraparams)."'":"null").""; |
||
| 1311 | $sql.= " WHERE rowid=".$this->id; |
||
| 1312 | |||
| 1313 | $this->db->begin(); |
||
| 1314 | |||
| 1315 | $resql = $this->db->query($sql); |
||
| 1316 | if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); } |
||
| 1317 | |||
| 1318 | if (! $error && empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($this->array_options) && count($this->array_options)>0) |
||
| 1319 | { |
||
| 1320 | $result=$this->insertExtraFields(); |
||
| 1321 | if ($result < 0) |
||
| 1322 | { |
||
| 1323 | $error++; |
||
| 1324 | } |
||
| 1325 | } |
||
| 1326 | |||
| 1327 | if (! $error && ! $notrigger) |
||
| 1328 | { |
||
| 1329 | // Call triggers |
||
| 1330 | $result=$this->call_trigger('CONTRACT_MODIFY', $user); |
||
| 1331 | if ($result < 0) { $error++; } |
||
| 1332 | // End call triggers |
||
| 1333 | } |
||
| 1334 | |||
| 1335 | // Commit or rollback |
||
| 1336 | if ($error) |
||
| 1337 | { |
||
| 1338 | foreach($this->errors as $errmsg) |
||
| 1339 | { |
||
| 1340 | dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR); |
||
| 1341 | $this->error.=($this->error?', '.$errmsg:$errmsg); |
||
| 1342 | } |
||
| 1343 | $this->db->rollback(); |
||
| 1344 | return -1*$error; |
||
| 1345 | } |
||
| 1346 | else |
||
| 1347 | { |
||
| 1348 | $this->db->commit(); |
||
| 1349 | return 1; |
||
| 1350 | } |
||
| 1351 | } |
||
| 1352 | |||
| 1353 | |||
| 1354 | /** |
||
| 1355 | * Ajoute une ligne de contrat en base |
||
| 1356 | * |
||
| 1357 | * @param string $desc Description de la ligne |
||
| 1358 | * @param float $pu_ht Prix unitaire HT |
||
| 1359 | * @param int $qty Quantite |
||
| 1360 | * @param float $txtva Taux tva |
||
| 1361 | * @param float $txlocaltax1 Local tax 1 rate |
||
| 1362 | * @param float $txlocaltax2 Local tax 2 rate |
||
| 1363 | * @param int $fk_product Id produit |
||
| 1364 | * @param float $remise_percent Percentage discount of the line |
||
| 1365 | * @param int $date_start Date de debut prevue |
||
| 1366 | * @param int $date_end Date de fin prevue |
||
| 1367 | * @param string $price_base_type HT or TTC |
||
| 1368 | * @param float $pu_ttc Prix unitaire TTC |
||
| 1369 | * @param int $info_bits Bits of type of lines |
||
| 1370 | * @param int $fk_fournprice Fourn price id |
||
| 1371 | * @param int $pa_ht Buying price HT |
||
| 1372 | * @param array $array_options extrafields array |
||
| 1373 | * @param string $fk_unit Code of the unit to use. Null to use the default one |
||
| 1374 | * @param string $rang Position |
||
| 1375 | * @return int <0 if KO, >0 if OK |
||
| 1376 | */ |
||
| 1377 | public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type = 'HT', $pu_ttc = 0.0, $info_bits = 0, $fk_fournprice = null, $pa_ht = 0, $array_options = 0, $fk_unit = null, $rang = 0) |
||
| 1378 | { |
||
| 1379 | global $user, $langs, $conf, $mysoc; |
||
| 1380 | $error=0; |
||
| 1381 | |||
| 1382 | dol_syslog(get_class($this)."::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type, $pu_ttc, $info_bits, $rang"); |
||
| 1383 | |||
| 1384 | // Check parameters |
||
| 1385 | if ($fk_product <= 0 && empty($desc)) |
||
| 1386 | { |
||
| 1387 | $this->error="ErrorDescRequiredForFreeProductLines"; |
||
| 1388 | return -1; |
||
| 1389 | } |
||
| 1390 | |||
| 1391 | if ($this->statut >= 0) |
||
| 1392 | { |
||
| 1393 | |||
| 1394 | // Clean parameters |
||
| 1395 | $pu_ht=price2num($pu_ht); |
||
| 1396 | $pu_ttc=price2num($pu_ttc); |
||
| 1397 | $pa_ht=price2num($pa_ht); |
||
| 1398 | if (!preg_match('/\((.*)\)/', $txtva)) { |
||
| 1399 | $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5' |
||
| 1400 | } |
||
| 1401 | $txlocaltax1=price2num($txlocaltax1); |
||
| 1402 | $txlocaltax2=price2num($txlocaltax2); |
||
| 1403 | $remise_percent=price2num($remise_percent); |
||
| 1404 | $qty=price2num($qty); |
||
| 1405 | if (empty($qty)) $qty=1; |
||
| 1406 | if (empty($info_bits)) $info_bits=0; |
||
| 1407 | if (empty($pu_ht) || ! is_numeric($pu_ht)) $pu_ht=0; |
||
| 1408 | if (empty($pu_ttc)) $pu_ttc=0; |
||
| 1409 | if (empty($txtva) || ! is_numeric($txtva)) $txtva=0; |
||
| 1410 | if (empty($txlocaltax1) || ! is_numeric($txlocaltax1)) $txlocaltax1=0; |
||
| 1411 | if (empty($txlocaltax2) || ! is_numeric($txlocaltax2)) $txlocaltax2=0; |
||
| 1412 | |||
| 1413 | if ($price_base_type=='HT') |
||
| 1414 | { |
||
| 1415 | $pu=$pu_ht; |
||
| 1416 | } |
||
| 1417 | else |
||
| 1418 | { |
||
| 1419 | $pu=$pu_ttc; |
||
| 1420 | } |
||
| 1421 | |||
| 1422 | // Check parameters |
||
| 1423 | if (empty($remise_percent)) $remise_percent=0; |
||
| 1424 | |||
| 1425 | if ($date_start && $date_end && $date_start > $date_end) { |
||
| 1426 | $langs->load("errors"); |
||
| 1427 | $this->error=$langs->trans('ErrorStartDateGreaterEnd'); |
||
| 1428 | return -1; |
||
| 1429 | } |
||
| 1430 | |||
| 1431 | $this->db->begin(); |
||
| 1432 | |||
| 1433 | $localtaxes_type=getLocalTaxesFromRate($txtva, 0, $this->societe, $mysoc); |
||
| 1434 | |||
| 1435 | // Clean vat code |
||
| 1436 | $vat_src_code=''; |
||
| 1437 | if (preg_match('/\((.*)\)/', $txtva, $reg)) |
||
| 1438 | { |
||
| 1439 | $vat_src_code = $reg[1]; |
||
| 1440 | $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate. |
||
| 1441 | } |
||
| 1442 | |||
| 1443 | // Calcul du total TTC et de la TVA pour la ligne a partir de |
||
| 1444 | // qty, pu, remise_percent et txtva |
||
| 1445 | // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker |
||
| 1446 | // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva. |
||
| 1447 | |||
| 1448 | $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type); |
||
| 1449 | $total_ht = $tabprice[0]; |
||
| 1450 | $total_tva = $tabprice[1]; |
||
| 1451 | $total_ttc = $tabprice[2]; |
||
| 1452 | $total_localtax1= $tabprice[9]; |
||
| 1453 | $total_localtax2= $tabprice[10]; |
||
| 1454 | |||
| 1455 | $localtax1_type=$localtaxes_type[0]; |
||
| 1456 | $localtax2_type=$localtaxes_type[2]; |
||
| 1457 | |||
| 1458 | // TODO A virer |
||
| 1459 | // Anciens indicateurs: $price, $remise (a ne plus utiliser) |
||
| 1460 | $remise = 0; |
||
| 1461 | $price = price2num(round($pu_ht, 2)); |
||
| 1462 | if (dol_strlen($remise_percent) > 0) |
||
| 1463 | { |
||
| 1464 | $remise = round(($pu_ht * $remise_percent / 100), 2); |
||
| 1465 | $price = $pu_ht - $remise; |
||
| 1466 | } |
||
| 1467 | |||
| 1468 | if (empty($pa_ht)) $pa_ht=0; |
||
| 1469 | |||
| 1470 | |||
| 1471 | // if buy price not defined, define buyprice as configured in margin admin |
||
| 1472 | if ($this->pa_ht == 0) |
||
| 1473 | { |
||
| 1474 | if (($result = $this->defineBuyPrice($pu_ht, $remise_percent, $fk_product)) < 0) |
||
| 1475 | { |
||
| 1476 | return $result; |
||
| 1477 | } |
||
| 1478 | else |
||
| 1479 | { |
||
| 1480 | $pa_ht = $result; |
||
| 1481 | } |
||
| 1482 | } |
||
| 1483 | |||
| 1484 | // Insertion dans la base |
||
| 1485 | $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet"; |
||
| 1486 | $sql.= " (fk_contrat, label, description, fk_product, qty, tva_tx, vat_src_code,"; |
||
| 1487 | $sql.= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,"; |
||
| 1488 | $sql.= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,"; |
||
| 1489 | $sql.= " info_bits,"; |
||
| 1490 | $sql.= " price_ht, remise, fk_product_fournisseur_price, buy_price_ht"; |
||
| 1491 | if ($date_start > 0) { $sql.= ",date_ouverture_prevue"; } |
||
| 1492 | if ($date_end > 0) { $sql.= ",date_fin_validite"; } |
||
| 1493 | $sql.= ", fk_unit"; |
||
| 1494 | $sql.= ") VALUES ("; |
||
| 1495 | $sql.= $this->id.", '', '" . $this->db->escape($desc) . "',"; |
||
| 1496 | $sql.= ($fk_product>0 ? $fk_product : "null").","; |
||
| 1497 | $sql.= " ".$qty.","; |
||
| 1498 | $sql.= " ".$txtva.","; |
||
| 1499 | $sql.= " ".($vat_src_code?"'".$vat_src_code."'":"null").","; |
||
| 1500 | $sql.= " ".$txlocaltax1.","; |
||
| 1501 | $sql.= " ".$txlocaltax2.","; |
||
| 1502 | $sql.= " '".$localtax1_type."',"; |
||
| 1503 | $sql.= " '".$localtax2_type."',"; |
||
| 1504 | $sql.= " ".price2num($remise_percent).","; |
||
| 1505 | $sql.= " ".price2num($pu_ht).","; |
||
| 1506 | $sql.= " ".price2num($total_ht).",".price2num($total_tva).",".price2num($total_localtax1).",".price2num($total_localtax2).",".price2num($total_ttc).","; |
||
| 1507 | $sql.= " '".$info_bits."',"; |
||
| 1508 | $sql.= " ".price2num($price).",".price2num($remise).","; |
||
| 1509 | if (isset($fk_fournprice)) $sql.= ' '.$fk_fournprice.','; |
||
| 1510 | else $sql.= ' null,'; |
||
| 1511 | if (isset($pa_ht)) $sql.= ' '.price2num($pa_ht); |
||
| 1512 | else $sql.= ' null'; |
||
| 1513 | if ($date_start > 0) { $sql.= ",'".$this->db->idate($date_start)."'"; } |
||
| 1514 | if ($date_end > 0) { $sql.= ",'".$this->db->idate($date_end)."'"; } |
||
| 1515 | $sql.= ", ".($fk_unit?"'".$this->db->escape($fk_unit)."'":"null"); |
||
| 1516 | $sql.= ")"; |
||
| 1517 | |||
| 1518 | $resql=$this->db->query($sql); |
||
| 1519 | if ($resql) |
||
| 1520 | { |
||
| 1521 | $contractlineid = $this->db->last_insert_id(MAIN_DB_PREFIX."contratdet"); |
||
| 1522 | |||
| 1523 | if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options)>0) // For avoid conflicts if trigger used |
||
| 1524 | { |
||
| 1525 | $contractline = new ContratLigne($this->db); |
||
| 1526 | $contractline->array_options=$array_options; |
||
| 1527 | $contractline->id=$contractlineid; |
||
| 1528 | $result=$contractline->insertExtraFields(); |
||
| 1529 | if ($result < 0) |
||
| 1530 | { |
||
| 1531 | $this->error[]=$contractline->error; |
||
| 1532 | $error++; |
||
| 1533 | } |
||
| 1534 | } |
||
| 1535 | |||
| 1536 | if (empty($error)) { |
||
| 1537 | // Call trigger |
||
| 1538 | $result=$this->call_trigger('LINECONTRACT_INSERT', $user); |
||
| 1539 | if ($result < 0) |
||
| 1540 | { |
||
| 1541 | $error++; |
||
| 1542 | } |
||
| 1543 | // End call triggers |
||
| 1544 | } |
||
| 1545 | |||
| 1546 | if ($error) |
||
| 1547 | { |
||
| 1548 | $this->db->rollback(); |
||
| 1549 | return -1; |
||
| 1550 | } |
||
| 1551 | else |
||
| 1552 | { |
||
| 1553 | $this->db->commit(); |
||
| 1554 | return $contractlineid; |
||
| 1555 | } |
||
| 1556 | } |
||
| 1557 | else |
||
| 1558 | { |
||
| 1559 | $this->db->rollback(); |
||
| 1560 | $this->error=$this->db->error()." sql=".$sql; |
||
| 1561 | return -1; |
||
| 1562 | } |
||
| 1563 | } |
||
| 1564 | else |
||
| 1565 | { |
||
| 1566 | dol_syslog(get_class($this)."::addline ErrorTryToAddLineOnValidatedContract", LOG_ERR); |
||
| 1567 | return -2; |
||
| 1568 | } |
||
| 1569 | } |
||
| 1570 | |||
| 1571 | /** |
||
| 1572 | * Mets a jour une ligne de contrat |
||
| 1573 | * |
||
| 1574 | * @param int $rowid Id de la ligne de facture |
||
| 1575 | * @param string $desc Description de la ligne |
||
| 1576 | * @param float $pu Prix unitaire |
||
| 1577 | * @param int $qty Quantite |
||
| 1578 | * @param float $remise_percent Percentage discount of the line |
||
| 1579 | * @param int $date_start Date de debut prevue |
||
| 1580 | * @param int $date_end Date de fin prevue |
||
| 1581 | * @param float $tvatx Taux TVA |
||
| 1582 | * @param float $localtax1tx Local tax 1 rate |
||
| 1583 | * @param float $localtax2tx Local tax 2 rate |
||
| 1584 | * @param int|string $date_debut_reel Date de debut reelle |
||
| 1585 | * @param int|string $date_fin_reel Date de fin reelle |
||
| 1586 | * @param string $price_base_type HT or TTC |
||
| 1587 | * @param int $info_bits Bits of type of lines |
||
| 1588 | * @param int $fk_fournprice Fourn price id |
||
| 1589 | * @param int $pa_ht Buying price HT |
||
| 1590 | * @param array $array_options extrafields array |
||
| 1591 | * @param string $fk_unit Code of the unit to use. Null to use the default one |
||
| 1592 | * @return int < 0 si erreur, > 0 si ok |
||
| 1593 | */ |
||
| 1594 | public function updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $tvatx, $localtax1tx = 0.0, $localtax2tx = 0.0, $date_debut_reel = '', $date_fin_reel = '', $price_base_type = 'HT', $info_bits = 0, $fk_fournprice = null, $pa_ht = 0, $array_options = 0, $fk_unit = null) |
||
| 1595 | { |
||
| 1596 | global $user, $conf, $langs, $mysoc; |
||
| 1597 | |||
| 1598 | $error=0; |
||
| 1599 | |||
| 1600 | // Clean parameters |
||
| 1601 | $qty=trim($qty); |
||
| 1602 | $desc=trim($desc); |
||
| 1603 | $desc=trim($desc); |
||
| 1604 | $price = price2num($pu); |
||
| 1605 | $tvatx = price2num($tvatx); |
||
| 1606 | $localtax1tx = price2num($localtax1tx); |
||
| 1607 | $localtax2tx = price2num($localtax2tx); |
||
| 1608 | $pa_ht=price2num($pa_ht); |
||
| 1609 | if (empty($fk_fournprice)) $fk_fournprice=0; |
||
| 1610 | |||
| 1611 | $subprice = $price; |
||
| 1612 | $remise = 0; |
||
| 1613 | if (dol_strlen($remise_percent) > 0) |
||
| 1614 | { |
||
| 1615 | $remise = round(($pu * $remise_percent / 100), 2); |
||
| 1616 | $price = $pu - $remise; |
||
| 1617 | } |
||
| 1618 | else |
||
| 1619 | { |
||
| 1620 | $remise_percent=0; |
||
| 1621 | } |
||
| 1622 | |||
| 1623 | if ($date_start && $date_end && $date_start > $date_end) { |
||
| 1624 | $langs->load("errors"); |
||
| 1625 | $this->error=$langs->trans('ErrorStartDateGreaterEnd'); |
||
| 1626 | return -1; |
||
| 1627 | } |
||
| 1628 | |||
| 1629 | dol_syslog(get_class($this)."::updateline $rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $date_debut_reel, $date_fin_reel, $tvatx, $localtax1tx, $localtax2tx, $price_base_type, $info_bits"); |
||
| 1630 | |||
| 1631 | $this->db->begin(); |
||
| 1632 | |||
| 1633 | // Calcul du total TTC et de la TVA pour la ligne a partir de |
||
| 1634 | // qty, pu, remise_percent et tvatx |
||
| 1635 | // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker |
||
| 1636 | // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva. |
||
| 1637 | |||
| 1638 | $localtaxes_type=getLocalTaxesFromRate($tvatx, 0, $this->societe, $mysoc); |
||
| 1639 | $tvatx = preg_replace('/\s*\(.*\)/', '', $tvatx); // Remove code into vatrate. |
||
| 1640 | |||
| 1641 | $tabprice=calcul_price_total($qty, $pu, $remise_percent, $tvatx, $localtax1tx, $localtax2tx, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type); |
||
| 1642 | $total_ht = $tabprice[0]; |
||
| 1643 | $total_tva = $tabprice[1]; |
||
| 1644 | $total_ttc = $tabprice[2]; |
||
| 1645 | $total_localtax1= $tabprice[9]; |
||
| 1646 | $total_localtax2= $tabprice[10]; |
||
| 1647 | |||
| 1648 | $localtax1_type=$localtaxes_type[0]; |
||
| 1649 | $localtax2_type=$localtaxes_type[2]; |
||
| 1650 | |||
| 1651 | // TODO A virer |
||
| 1652 | // Anciens indicateurs: $price, $remise (a ne plus utiliser) |
||
| 1653 | $remise = 0; |
||
| 1654 | $price = price2num(round($pu, 2)); |
||
| 1655 | if (dol_strlen($remise_percent) > 0) |
||
| 1656 | { |
||
| 1657 | $remise = round(($pu * $remise_percent / 100), 2); |
||
| 1658 | $price = $pu - $remise; |
||
| 1659 | } |
||
| 1660 | |||
| 1661 | if (empty($pa_ht)) $pa_ht=0; |
||
| 1662 | |||
| 1663 | // if buy price not defined, define buyprice as configured in margin admin |
||
| 1664 | if ($this->pa_ht == 0) |
||
| 1665 | { |
||
| 1666 | if (($result = $this->defineBuyPrice($pu, $remise_percent)) < 0) |
||
| 1667 | { |
||
| 1668 | return $result; |
||
| 1669 | } |
||
| 1670 | else |
||
| 1671 | { |
||
| 1672 | $pa_ht = $result; |
||
| 1673 | } |
||
| 1674 | } |
||
| 1675 | |||
| 1676 | $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet set description='".$this->db->escape($desc)."'"; |
||
| 1677 | $sql.= ",price_ht='".price2num($price)."'"; |
||
| 1678 | $sql.= ",subprice='".price2num($subprice)."'"; |
||
| 1679 | $sql.= ",remise='".price2num($remise)."'"; |
||
| 1680 | $sql.= ",remise_percent='".price2num($remise_percent)."'"; |
||
| 1681 | $sql.= ",qty='".$qty."'"; |
||
| 1682 | $sql.= ",tva_tx='".price2num($tvatx)."'"; |
||
| 1683 | $sql.= ",localtax1_tx='".price2num($localtax1tx)."'"; |
||
| 1684 | $sql.= ",localtax2_tx='".price2num($localtax2tx)."'"; |
||
| 1685 | $sql.= ",localtax1_type='".$localtax1_type."'"; |
||
| 1686 | $sql.= ",localtax2_type='".$localtax2_type."'"; |
||
| 1687 | $sql.= ", total_ht='".price2num($total_ht)."'"; |
||
| 1688 | $sql.= ", total_tva='".price2num($total_tva)."'"; |
||
| 1689 | $sql.= ", total_localtax1='".price2num($total_localtax1)."'"; |
||
| 1690 | $sql.= ", total_localtax2='".price2num($total_localtax2)."'"; |
||
| 1691 | $sql.= ", total_ttc='".price2num($total_ttc)."'"; |
||
| 1692 | $sql.= ", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice : "null"); |
||
| 1693 | $sql.= ", buy_price_ht='".price2num($pa_ht)."'"; |
||
| 1694 | if ($date_start > 0) { $sql.= ",date_ouverture_prevue='".$this->db->idate($date_start)."'"; } |
||
| 1695 | else { $sql.=",date_ouverture_prevue=null"; } |
||
| 1696 | if ($date_end > 0) { $sql.= ",date_fin_validite='".$this->db->idate($date_end)."'"; } |
||
| 1697 | else { $sql.=",date_fin_validite=null"; } |
||
| 1698 | if ($date_debut_reel > 0) { $sql.= ",date_ouverture='".$this->db->idate($date_debut_reel)."'"; } |
||
| 1699 | else { $sql.=",date_ouverture=null"; } |
||
| 1700 | if ($date_fin_reel > 0) { $sql.= ",date_cloture='".$this->db->idate($date_fin_reel)."'"; } |
||
| 1701 | else { $sql.=",date_cloture=null"; } |
||
| 1702 | $sql .= ", fk_unit=".($fk_unit?"'".$this->db->escape($fk_unit)."'":"null"); |
||
| 1703 | $sql .= " WHERE rowid = ".$rowid; |
||
| 1704 | |||
| 1705 | dol_syslog(get_class($this)."::updateline", LOG_DEBUG); |
||
| 1706 | $result = $this->db->query($sql); |
||
| 1707 | if ($result) |
||
| 1708 | { |
||
| 1709 | $result=$this->update_statut($user); |
||
| 1710 | if ($result >= 0) |
||
| 1711 | { |
||
| 1712 | |||
| 1713 | if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options)>0) // For avoid conflicts if trigger used |
||
| 1714 | { |
||
| 1715 | $contractline = new ContratLigne($this->db); |
||
| 1716 | $contractline->array_options=$array_options; |
||
| 1717 | $contractline->id= $rowid; |
||
| 1718 | $result=$contractline->insertExtraFields(); |
||
| 1719 | if ($result < 0) |
||
| 1720 | { |
||
| 1721 | $this->error[]=$contractline->error; |
||
| 1722 | $error++; |
||
| 1723 | } |
||
| 1724 | } |
||
| 1725 | |||
| 1726 | if (empty($error)) { |
||
| 1727 | // Call trigger |
||
| 1728 | $result=$this->call_trigger('LINECONTRACT_UPDATE', $user); |
||
| 1729 | if ($result < 0) |
||
| 1730 | { |
||
| 1731 | $this->db->rollback(); |
||
| 1732 | return -3; |
||
| 1733 | } |
||
| 1734 | // End call triggers |
||
| 1735 | |||
| 1736 | $this->db->commit(); |
||
| 1737 | return 1; |
||
| 1738 | } |
||
| 1739 | } |
||
| 1740 | else |
||
| 1741 | { |
||
| 1742 | $this->db->rollback(); |
||
| 1743 | dol_syslog(get_class($this)."::updateline Erreur -2"); |
||
| 1744 | return -2; |
||
| 1745 | } |
||
| 1746 | } |
||
| 1747 | else |
||
| 1748 | { |
||
| 1749 | $this->db->rollback(); |
||
| 1750 | $this->error=$this->db->error(); |
||
| 1751 | dol_syslog(get_class($this)."::updateline Erreur -1"); |
||
| 1752 | return -1; |
||
| 1753 | } |
||
| 1754 | } |
||
| 1755 | |||
| 1756 | /** |
||
| 1757 | * Delete a contract line |
||
| 1758 | * |
||
| 1759 | * @param int $idline Id of line to delete |
||
| 1760 | * @param User $user User that delete |
||
| 1761 | * @return int >0 if OK, <0 if KO |
||
| 1762 | */ |
||
| 1763 | public function deleteline($idline, User $user) |
||
| 1764 | { |
||
| 1765 | global $conf, $langs; |
||
| 1766 | |||
| 1767 | $error=0; |
||
| 1768 | |||
| 1769 | if ($this->statut >= 0) |
||
| 1770 | { |
||
| 1771 | // Call trigger |
||
| 1772 | $result=$this->call_trigger('LINECONTRACT_DELETE', $user); |
||
| 1773 | if ($result < 0) return -1; |
||
| 1774 | // End call triggers |
||
| 1775 | |||
| 1776 | $this->db->begin(); |
||
| 1777 | |||
| 1778 | $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element_line; |
||
| 1779 | $sql.= " WHERE rowid=".$idline; |
||
| 1780 | |||
| 1781 | dol_syslog(get_class($this)."::deleteline", LOG_DEBUG); |
||
| 1782 | $resql = $this->db->query($sql); |
||
| 1783 | if (! $resql) |
||
| 1784 | { |
||
| 1785 | $this->error="Error ".$this->db->lasterror(); |
||
| 1786 | $error++; |
||
| 1787 | } |
||
| 1788 | |||
| 1789 | if (empty($error)) { |
||
| 1790 | // Remove extrafields |
||
| 1791 | if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used |
||
| 1792 | { |
||
| 1793 | $contractline = new ContratLigne($this->db); |
||
| 1794 | $contractline->id= $idline; |
||
| 1795 | $result=$contractline->deleteExtraFields(); |
||
| 1796 | if ($result < 0) |
||
| 1797 | { |
||
| 1798 | $error++; |
||
| 1799 | $this->error="Error ".get_class($this)."::deleteline deleteExtraFields error -4 ".$contractline->error; |
||
| 1800 | } |
||
| 1801 | } |
||
| 1802 | } |
||
| 1803 | |||
| 1804 | if (empty($error)) { |
||
| 1805 | $this->db->commit(); |
||
| 1806 | return 1; |
||
| 1807 | } else { |
||
| 1808 | dol_syslog(get_class($this)."::deleteline ERROR:".$this->error, LOG_ERR); |
||
| 1809 | $this->db->rollback(); |
||
| 1810 | return -1; |
||
| 1811 | } |
||
| 1812 | } |
||
| 1813 | else |
||
| 1814 | { |
||
| 1815 | $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus'; |
||
| 1816 | return -2; |
||
| 1817 | } |
||
| 1818 | } |
||
| 1819 | |||
| 1820 | |||
| 1821 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 1822 | /** |
||
| 1823 | * Update statut of contract according to services |
||
| 1824 | * |
||
| 1825 | * @param User $user Object user |
||
| 1826 | * @return int <0 if KO, >0 if OK |
||
| 1827 | * @deprecated This function will never be used. Status of a contract is status of its lines. |
||
| 1828 | */ |
||
| 1829 | public function update_statut($user) |
||
| 1830 | { |
||
| 1831 | // phpcs:enable |
||
| 1832 | dol_syslog(__METHOD__ . " is deprecated", LOG_WARNING); |
||
| 1833 | |||
| 1834 | // If draft, we keep it (should not happen) |
||
| 1835 | if ($this->statut == 0) return 1; |
||
| 1836 | |||
| 1837 | // Load $this->lines array |
||
| 1838 | // $this->fetch_lines(); |
||
| 1839 | |||
| 1840 | // $newstatut=1; |
||
| 1841 | // foreach($this->lines as $key => $contractline) |
||
| 1842 | // { |
||
| 1843 | // // if ($contractline) // Loop on each service |
||
| 1844 | // } |
||
| 1845 | |||
| 1846 | return 1; |
||
| 1847 | } |
||
| 1848 | |||
| 1849 | |||
| 1850 | /** |
||
| 1851 | * Return label of a contract status |
||
| 1852 | * |
||
| 1853 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Long label of all services, 5=Libelle court + Picto, 6=Picto of all services, 7=Same than 6 with fixed length |
||
| 1854 | * @return string Label |
||
| 1855 | */ |
||
| 1856 | public function getLibStatut($mode) |
||
| 1857 | { |
||
| 1858 | return $this->LibStatut($this->statut, $mode); |
||
| 1859 | } |
||
| 1860 | |||
| 1861 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 1862 | /** |
||
| 1863 | * Renvoi label of a given contrat status |
||
| 1864 | * |
||
| 1865 | * @param int $statut Status id |
||
| 1866 | * @param int $mode 0=Long label, 1=Short label, 2=Picto + Libelle court, 3=Picto, 4=Picto + Long label of all services, 5=Libelle court + Picto, 6=Picto of all services, 7=Same than 6 with fixed length |
||
| 1867 | * @return string Label |
||
| 1868 | */ |
||
| 1869 | public function LibStatut($statut, $mode) |
||
| 1870 | { |
||
| 1871 | // phpcs:enable |
||
| 1872 | global $langs; |
||
| 1873 | $langs->load("contracts"); |
||
| 1874 | if ($mode == 0) |
||
| 1875 | { |
||
| 1876 | if ($statut == 0) { return $langs->trans("ContractStatusDraft"); } |
||
| 1877 | elseif ($statut == 1) { return $langs->trans("ContractStatusValidated"); } |
||
| 1878 | elseif ($statut == 2) { return $langs->trans("ContractStatusClosed"); } |
||
| 1879 | } |
||
| 1880 | elseif ($mode == 1) |
||
| 1881 | { |
||
| 1882 | if ($statut == 0) { return $langs->trans("ContractStatusDraft"); } |
||
| 1883 | elseif ($statut == 1) { return $langs->trans("ContractStatusValidated"); } |
||
| 1884 | elseif ($statut == 2) { return $langs->trans("ContractStatusClosed"); } |
||
| 1885 | } |
||
| 1886 | elseif ($mode == 2) |
||
| 1887 | { |
||
| 1888 | if ($statut == 0) { return img_picto($langs->trans('ContractStatusDraft'), 'statut0').' '.$langs->trans("ContractStatusDraft"); } |
||
| 1889 | elseif ($statut == 1) { return img_picto($langs->trans('ContractStatusValidated'), 'statut4').' '.$langs->trans("ContractStatusValidated"); } |
||
| 1890 | elseif ($statut == 2) { return img_picto($langs->trans('ContractStatusClosed'), 'statut6').' '.$langs->trans("ContractStatusClosed"); } |
||
| 1891 | } |
||
| 1892 | elseif ($mode == 3) |
||
| 1893 | { |
||
| 1894 | if ($statut == 0) { return img_picto($langs->trans('ContractStatusDraft'), 'statut0'); } |
||
| 1895 | elseif ($statut == 1) { return img_picto($langs->trans('ContractStatusValidated'), 'statut4'); } |
||
| 1896 | elseif ($statut == 2) { return img_picto($langs->trans('ContractStatusClosed'), 'statut6'); } |
||
| 1897 | } |
||
| 1898 | elseif ($mode == 4 || $mode == 6 || $mode == 7) |
||
| 1899 | { |
||
| 1900 | $text=''; |
||
| 1901 | if ($mode == 4) { |
||
| 1902 | $text ='<span class="hideonsmartphone">'; |
||
| 1903 | $text.=($this->nbofserviceswait+$this->nbofservicesopened+$this->nbofservicesexpired+$this->nbofservicesclosed); |
||
| 1904 | $text.=' '.$langs->trans("Services"); |
||
| 1905 | $text.=': '; |
||
| 1906 | $text.='</span>'; |
||
| 1907 | } |
||
| 1908 | $text.=($mode == 7?'<div class="inline-block">':''); |
||
| 1909 | $text.=($mode != 7 || $this->nbofserviceswait > 0) ? ($this->nbofserviceswait.ContratLigne::LibStatut(0, 3, -1, 'class="paddingleft2 inline-block valigntextbottom"')).(($mode != 7 || $this->nbofservicesopened || $this->nbofservicesexpired || $this->nbofservicesclosed)?' ':'') : ''; |
||
| 1910 | $text.=($mode == 7?'</div><div class="inline-block">':''); |
||
| 1911 | $text.=($mode != 7 || $this->nbofservicesopened > 0) ? ($this->nbofservicesopened.ContratLigne::LibStatut(4, 3, 0, 'class="paddingleft2 inline-block valigntextbottom"')).(($mode != 7 || $this->nbofservicesexpired || $this->nbofservicesclosed)?' ':'') : ''; |
||
| 1912 | $text.=($mode == 7?'</div><div class="inline-block">':''); |
||
| 1913 | $text.=($mode != 7 || $this->nbofservicesexpired > 0) ? ($this->nbofservicesexpired.ContratLigne::LibStatut(4, 3, 1, 'class="paddingleft2 inline-block valigntextbottom"')).(($mode != 7 || $this->nbofservicesclosed)?' ':'') : ''; |
||
| 1914 | $text.=($mode == 7?'</div><div class="inline-block">':''); |
||
| 1915 | $text.=($mode != 7 || $this->nbofservicesclosed > 0) ? ($this->nbofservicesclosed.ContratLigne::LibStatut(5, 3, -1, 'class="paddingleft2 inline-block valigntextbottom"')) : ''; |
||
| 1916 | $text.=($mode == 7?'</div>':''); |
||
| 1917 | return $text; |
||
| 1918 | } |
||
| 1919 | elseif ($mode == 5) |
||
| 1920 | { |
||
| 1921 | if ($statut == 0) { return $langs->trans("ContractStatusDraft").' '.img_picto($langs->trans('ContractStatusDraft'), 'statut0'); } |
||
| 1922 | elseif ($statut == 1) { return $langs->trans("ContractStatusValidated").' '.img_picto($langs->trans('ContractStatusValidated'), 'statut4'); } |
||
| 1923 | elseif ($statut == 2) { return $langs->trans("ContractStatusClosed").' '.img_picto($langs->trans('ContractStatusClosed'), 'statut6'); } |
||
| 1924 | } |
||
| 1925 | } |
||
| 1926 | |||
| 1927 | |||
| 1928 | /** |
||
| 1929 | * Return clicable name (with picto eventually) |
||
| 1930 | * |
||
| 1931 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
||
| 1932 | * @param int $maxlength Max length of ref |
||
| 1933 | * @param int $notooltip 1=Disable tooltip |
||
| 1934 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
| 1935 | * @return string Chaine avec URL |
||
| 1936 | */ |
||
| 1937 | public function getNomUrl($withpicto = 0, $maxlength = 0, $notooltip = 0, $save_lastsearch_value = -1) |
||
| 1938 | { |
||
| 1939 | global $conf, $langs, $user, $hookmanager; |
||
| 1940 | |||
| 1941 | $result=''; |
||
| 1942 | |||
| 1943 | $url = DOL_URL_ROOT.'/contrat/card.php?id='.$this->id; |
||
| 1944 | |||
| 1945 | //if ($option !== 'nolink') |
||
| 1946 | //{ |
||
| 1947 | // Add param to save lastsearch_values or not |
||
| 1948 | $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0); |
||
| 1949 | if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1; |
||
| 1950 | if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1'; |
||
| 1951 | //} |
||
| 1952 | |||
| 1953 | $label = ''; |
||
| 1954 | |||
| 1955 | if ($user->rights->contrat->lire) { |
||
| 1956 | $label = '<u>'.$langs->trans("ShowContract").'</u>'; |
||
| 1957 | $label .= '<br><b>'.$langs->trans('Ref').':</b> '.($this->ref?$this->ref:$this->id); |
||
| 1958 | $label .= '<br><b>'.$langs->trans('RefCustomer').':</b> '.($this->ref_customer ? $this->ref_customer : $this->ref_client); |
||
| 1959 | $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier; |
||
| 1960 | if (!empty($this->total_ht)) { |
||
| 1961 | $label .= '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency); |
||
| 1962 | } |
||
| 1963 | if (!empty($this->total_tva)) { |
||
| 1964 | $label .= '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency); |
||
| 1965 | } |
||
| 1966 | if (!empty($this->total_ttc)) { |
||
| 1967 | $label .= '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency); |
||
| 1968 | } |
||
| 1969 | } |
||
| 1970 | |||
| 1971 | $linkclose=''; |
||
| 1972 | if (empty($notooltip) && $user->rights->contrat->lire) |
||
| 1973 | { |
||
| 1974 | if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) |
||
| 1975 | { |
||
| 1976 | $label=$langs->trans("ShowOrder"); |
||
| 1977 | $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"'; |
||
| 1978 | } |
||
| 1979 | $linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"'; |
||
| 1980 | $linkclose.=' class="classfortooltip"'; |
||
| 1981 | } |
||
| 1982 | |||
| 1983 | $linkstart = '<a href="'.$url.'"'; |
||
| 1984 | $linkstart.=$linkclose.'>'; |
||
| 1985 | $linkend='</a>'; |
||
| 1986 | |||
| 1987 | $result .= $linkstart; |
||
| 1988 | if ($withpicto) $result.=img_object(($notooltip?'':$label), $this->picto, ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1); |
||
| 1989 | if ($withpicto != 2) $result.= ($this->ref?$this->ref:$this->id); |
||
| 1990 | $result .= $linkend; |
||
| 1991 | |||
| 1992 | global $action; |
||
| 1993 | $hookmanager->initHooks(array('contractdao')); |
||
| 1994 | $parameters=array('id'=>$this->id, 'getnomurl'=>$result); |
||
| 1995 | $reshook=$hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 1996 | if ($reshook > 0) { |
||
| 1997 | $result = $hookmanager->resPrint; |
||
| 1998 | } else { |
||
| 1999 | $result .= $hookmanager->resPrint; |
||
| 2000 | } |
||
| 2001 | |||
| 2002 | return $result; |
||
| 2003 | } |
||
| 2004 | |||
| 2005 | /** |
||
| 2006 | * Charge les informations d'ordre info dans l'objet contrat |
||
| 2007 | * |
||
| 2008 | * @param int $id id du contrat a charger |
||
| 2009 | * @return void |
||
| 2010 | */ |
||
| 2011 | public function info($id) |
||
| 2012 | { |
||
| 2013 | $sql = "SELECT c.rowid, c.ref, c.datec, c.date_cloture,"; |
||
| 2014 | $sql.= " c.tms as date_modification,"; |
||
| 2015 | $sql.= " fk_user_author, fk_user_cloture"; |
||
| 2016 | $sql.= " FROM ".MAIN_DB_PREFIX."contrat as c"; |
||
| 2017 | $sql.= " WHERE c.rowid = ".$id; |
||
| 2018 | |||
| 2019 | $result=$this->db->query($sql); |
||
| 2020 | if ($result) |
||
| 2021 | { |
||
| 2022 | if ($this->db->num_rows($result)) |
||
| 2023 | { |
||
| 2024 | $obj = $this->db->fetch_object($result); |
||
| 2025 | |||
| 2026 | $this->id = $obj->rowid; |
||
| 2027 | |||
| 2028 | if ($obj->fk_user_author) { |
||
| 2029 | $cuser = new User($this->db); |
||
| 2030 | $cuser->fetch($obj->fk_user_author); |
||
| 2031 | $this->user_creation = $cuser; |
||
| 2032 | } |
||
| 2033 | |||
| 2034 | if ($obj->fk_user_cloture) { |
||
| 2035 | $cuser = new User($this->db); |
||
| 2036 | $cuser->fetch($obj->fk_user_cloture); |
||
| 2037 | $this->user_cloture = $cuser; |
||
| 2038 | } |
||
| 2039 | $this->ref = (! $obj->ref) ? $obj->rowid : $obj->ref; |
||
| 2040 | $this->date_creation = $this->db->jdate($obj->datec); |
||
| 2041 | $this->date_modification = $this->db->jdate($obj->date_modification); |
||
| 2042 | $this->date_cloture = $this->db->jdate($obj->date_cloture); |
||
| 2043 | } |
||
| 2044 | |||
| 2045 | $this->db->free($result); |
||
| 2046 | } |
||
| 2047 | else |
||
| 2048 | { |
||
| 2049 | dol_print_error($this->db); |
||
| 2050 | } |
||
| 2051 | } |
||
| 2052 | |||
| 2053 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2054 | /** |
||
| 2055 | * Return list of line rowid |
||
| 2056 | * |
||
| 2057 | * @param int $statut Status of lines to get |
||
| 2058 | * @return array Array of line's rowid |
||
| 2059 | */ |
||
| 2060 | public function array_detail($statut = -1) |
||
| 2061 | { |
||
| 2062 | // phpcs:enable |
||
| 2063 | $tab=array(); |
||
| 2064 | |||
| 2065 | $sql = "SELECT cd.rowid"; |
||
| 2066 | $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as cd"; |
||
| 2067 | $sql.= " WHERE fk_contrat =".$this->id; |
||
| 2068 | if ($statut >= 0) $sql.= " AND statut = '$statut'"; |
||
| 2069 | |||
| 2070 | dol_syslog(get_class($this)."::array_detail()", LOG_DEBUG); |
||
| 2071 | $resql=$this->db->query($sql); |
||
| 2072 | if ($resql) |
||
| 2073 | { |
||
| 2074 | $num=$this->db->num_rows($resql); |
||
| 2075 | $i=0; |
||
| 2076 | while ($i < $num) |
||
| 2077 | { |
||
| 2078 | $obj = $this->db->fetch_object($resql); |
||
| 2079 | $tab[$i]=$obj->rowid; |
||
| 2080 | $i++; |
||
| 2081 | } |
||
| 2082 | return $tab; |
||
| 2083 | } |
||
| 2084 | else |
||
| 2085 | { |
||
| 2086 | $this->error=$this->db->error(); |
||
| 2087 | return -1; |
||
| 2088 | } |
||
| 2089 | } |
||
| 2090 | |||
| 2091 | /** |
||
| 2092 | * Return list of other contracts for same company than current contract |
||
| 2093 | * |
||
| 2094 | * @param string $option 'all' or 'others' |
||
| 2095 | * @return array Array of contracts id |
||
| 2096 | */ |
||
| 2097 | public function getListOfContracts($option = 'all') |
||
| 2098 | { |
||
| 2099 | $tab=array(); |
||
| 2100 | |||
| 2101 | $sql = "SELECT c.rowid, c.ref"; |
||
| 2102 | $sql.= " FROM ".MAIN_DB_PREFIX."contrat as c"; |
||
| 2103 | $sql.= " WHERE fk_soc =".$this->socid; |
||
| 2104 | if ($option == 'others') $sql.= " AND c.rowid != ".$this->id; |
||
| 2105 | |||
| 2106 | dol_syslog(get_class($this)."::getOtherContracts()", LOG_DEBUG); |
||
| 2107 | $resql=$this->db->query($sql); |
||
| 2108 | if ($resql) |
||
| 2109 | { |
||
| 2110 | $num=$this->db->num_rows($resql); |
||
| 2111 | $i=0; |
||
| 2112 | while ($i < $num) |
||
| 2113 | { |
||
| 2114 | $obj = $this->db->fetch_object($resql); |
||
| 2115 | $contrat=new Contrat($this->db); |
||
| 2116 | $contrat->fetch($obj->rowid); |
||
| 2117 | $tab[]=$contrat; |
||
| 2118 | $i++; |
||
| 2119 | } |
||
| 2120 | return $tab; |
||
| 2121 | } |
||
| 2122 | else |
||
| 2123 | { |
||
| 2124 | $this->error=$this->db->error(); |
||
| 2125 | return -1; |
||
| 2126 | } |
||
| 2127 | } |
||
| 2128 | |||
| 2129 | |||
| 2130 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2131 | /** |
||
| 2132 | * Load indicators for dashboard (this->nbtodo and this->nbtodolate) |
||
| 2133 | * |
||
| 2134 | * @param User $user Objet user |
||
| 2135 | * @param string $mode "inactive" pour services a activer, "expired" pour services expires |
||
| 2136 | * @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK |
||
| 2137 | */ |
||
| 2138 | public function load_board($user, $mode) |
||
| 2139 | { |
||
| 2140 | // phpcs:enable |
||
| 2141 | global $conf, $langs; |
||
| 2142 | |||
| 2143 | $this->from = " FROM ".MAIN_DB_PREFIX."contrat as c"; |
||
| 2144 | $this->from.= ", ".MAIN_DB_PREFIX."contratdet as cd"; |
||
| 2145 | $this->from.= ", ".MAIN_DB_PREFIX."societe as s"; |
||
| 2146 | if (!$user->rights->societe->client->voir && !$user->societe_id) $this->from.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; |
||
|
1 ignored issue
–
show
|
|||
| 2147 | |||
| 2148 | if ($mode == 'inactive') |
||
| 2149 | { |
||
| 2150 | $sql = "SELECT cd.rowid, cd.date_ouverture_prevue as datefin"; |
||
| 2151 | $sql.= $this->from; |
||
| 2152 | $sql.= " WHERE c.statut = 1"; |
||
| 2153 | $sql.= " AND c.rowid = cd.fk_contrat"; |
||
| 2154 | $sql.= " AND cd.statut = 0"; |
||
| 2155 | } |
||
| 2156 | elseif ($mode == 'expired') |
||
| 2157 | { |
||
| 2158 | $sql = "SELECT cd.rowid, cd.date_fin_validite as datefin"; |
||
| 2159 | $sql.= $this->from; |
||
| 2160 | $sql.= " WHERE c.statut = 1"; |
||
| 2161 | $sql.= " AND c.rowid = cd.fk_contrat"; |
||
| 2162 | $sql.= " AND cd.statut = 4"; |
||
| 2163 | $sql.= " AND cd.date_fin_validite < '".$this->db->idate(dol_now())."'"; |
||
| 2164 | } |
||
| 2165 | elseif ($mode == 'active') |
||
| 2166 | { |
||
| 2167 | $sql = "SELECT cd.rowid, cd.date_fin_validite as datefin"; |
||
| 2168 | $sql.= $this->from; |
||
| 2169 | $sql.= " WHERE c.statut = 1"; |
||
| 2170 | $sql.= " AND c.rowid = cd.fk_contrat"; |
||
| 2171 | $sql.= " AND cd.statut = 4"; |
||
| 2172 | //$datetouse = dol_now(); |
||
| 2173 | //$sql.= " AND cd.date_fin_validite < '".$this->db->idate($datetouse)."'"; |
||
| 2174 | } |
||
| 2175 | $sql.= " AND c.fk_soc = s.rowid"; |
||
| 2176 | $sql.= " AND c.entity = ".$conf->entity; |
||
| 2177 | if ($user->societe_id) $sql.=" AND c.fk_soc = ".$user->societe_id; |
||
|
1 ignored issue
–
show
|
|||
| 2178 | if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; |
||
|
1 ignored issue
–
show
|
|||
| 2179 | |||
| 2180 | $resql=$this->db->query($sql); |
||
| 2181 | if ($resql) |
||
| 2182 | { |
||
| 2183 | $langs->load("contracts"); |
||
| 2184 | $now=dol_now(); |
||
| 2185 | |||
| 2186 | if ($mode == 'inactive') { |
||
| 2187 | $warning_delay = $conf->contrat->services->inactifs->warning_delay; |
||
| 2188 | $label = $langs->trans("BoardNotActivatedServices"); |
||
| 2189 | $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&mode=0&sortfield=cd.date_fin_validite&sortorder=asc'; |
||
| 2190 | } |
||
| 2191 | elseif ($mode == 'expired') { |
||
| 2192 | $warning_delay = $conf->contrat->services->expires->warning_delay; |
||
| 2193 | $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&mode=4&filter=expired&sortfield=cd.date_fin_validite&sortorder=asc'; |
||
| 2194 | $label = $langs->trans("BoardExpiredServices"); |
||
| 2195 | } else { |
||
| 2196 | $warning_delay = $conf->contrat->services->expires->warning_delay; |
||
| 2197 | $url = DOL_URL_ROOT.'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&mode=4&sortfield=cd.date_fin_validite&sortorder=asc'; |
||
| 2198 | //$url.= '&op2day='.$arraydatetouse['mday'].'&op2month='.$arraydatetouse['mon'].'&op2year='.$arraydatetouse['year']; |
||
| 2199 | //if ($warning_delay >= 0) $url.='&filter=expired'; |
||
| 2200 | $label = $langs->trans("BoardRunningServices"); |
||
| 2201 | } |
||
| 2202 | |||
| 2203 | $response = new WorkboardResponse(); |
||
| 2204 | $response->warning_delay = $warning_delay/60/60/24; |
||
| 2205 | $response->label = $label; |
||
| 2206 | $response->url = $url; |
||
| 2207 | $response->img = img_object('', "contract"); |
||
| 2208 | |||
| 2209 | while ($obj=$this->db->fetch_object($resql)) |
||
| 2210 | { |
||
| 2211 | $response->nbtodo++; |
||
| 2212 | |||
| 2213 | if ($obj->datefin && $this->db->jdate($obj->datefin) < ($now - $warning_delay)) { |
||
| 2214 | $response->nbtodolate++; |
||
| 2215 | } |
||
| 2216 | } |
||
| 2217 | |||
| 2218 | return $response; |
||
| 2219 | } |
||
| 2220 | else |
||
| 2221 | { |
||
| 2222 | dol_print_error($this->db); |
||
| 2223 | $this->error=$this->db->error(); |
||
| 2224 | return -1; |
||
| 2225 | } |
||
| 2226 | } |
||
| 2227 | |||
| 2228 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2229 | /** |
||
| 2230 | * Charge indicateurs this->nb de tableau de bord |
||
| 2231 | * |
||
| 2232 | * @return int <0 si ko, >0 si ok |
||
| 2233 | */ |
||
| 2234 | public function load_state_board() |
||
| 2235 | { |
||
| 2236 | // phpcs:enable |
||
| 2237 | global $conf, $user; |
||
| 2238 | |||
| 2239 | $this->nb=array(); |
||
| 2240 | $clause = "WHERE"; |
||
| 2241 | |||
| 2242 | $sql = "SELECT count(c.rowid) as nb"; |
||
| 2243 | $sql.= " FROM ".MAIN_DB_PREFIX."contrat as c"; |
||
| 2244 | $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid"; |
||
| 2245 | if (!$user->rights->societe->client->voir && !$user->societe_id) |
||
| 2246 | { |
||
| 2247 | $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc"; |
||
| 2248 | $sql.= " WHERE sc.fk_user = " .$user->id; |
||
| 2249 | $clause = "AND"; |
||
| 2250 | } |
||
| 2251 | $sql.= " ".$clause." c.entity = ".$conf->entity; |
||
| 2252 | |||
| 2253 | $resql=$this->db->query($sql); |
||
| 2254 | if ($resql) |
||
| 2255 | { |
||
| 2256 | while ($obj=$this->db->fetch_object($resql)) |
||
| 2257 | { |
||
| 2258 | $this->nb["Contracts"]=$obj->nb; |
||
| 2259 | } |
||
| 2260 | $this->db->free($resql); |
||
| 2261 | return 1; |
||
| 2262 | } |
||
| 2263 | else |
||
| 2264 | { |
||
| 2265 | dol_print_error($this->db); |
||
| 2266 | $this->error=$this->db->error(); |
||
| 2267 | return -1; |
||
| 2268 | } |
||
| 2269 | } |
||
| 2270 | |||
| 2271 | |||
| 2272 | /* gestion des contacts d'un contrat */ |
||
| 2273 | |||
| 2274 | /** |
||
| 2275 | * Return id des contacts clients de facturation |
||
| 2276 | * |
||
| 2277 | * @return array Liste des id contacts facturation |
||
| 2278 | */ |
||
| 2279 | public function getIdBillingContact() |
||
| 2282 | } |
||
| 2283 | |||
| 2284 | /** |
||
| 2285 | * Return id des contacts clients de prestation |
||
| 2286 | * |
||
| 2287 | * @return array Liste des id contacts prestation |
||
| 2288 | */ |
||
| 2289 | public function getIdServiceContact() |
||
| 2290 | { |
||
| 2291 | return $this->getIdContact('external', 'SERVICE'); |
||
| 2292 | } |
||
| 2293 | |||
| 2294 | |||
| 2295 | /** |
||
| 2296 | * Initialise an instance with random values. |
||
| 2297 | * Used to build previews or test instances. |
||
| 2298 | * id must be 0 if object instance is a specimen. |
||
| 2299 | * |
||
| 2300 | * @return void |
||
| 2301 | */ |
||
| 2302 | public function initAsSpecimen() |
||
| 2303 | { |
||
| 2304 | global $user,$langs,$conf; |
||
| 2305 | |||
| 2306 | // Load array of products prodids |
||
| 2307 | $num_prods = 0; |
||
| 2308 | $prodids = array(); |
||
| 2309 | $sql = "SELECT rowid"; |
||
| 2310 | $sql.= " FROM ".MAIN_DB_PREFIX."product"; |
||
| 2311 | $sql.= " WHERE entity IN (".getEntity('product').")"; |
||
| 2312 | $sql.= " AND tosell = 1"; |
||
| 2313 | $resql = $this->db->query($sql); |
||
| 2314 | if ($resql) |
||
| 2315 | { |
||
| 2316 | $num_prods = $this->db->num_rows($resql); |
||
| 2317 | $i = 0; |
||
| 2318 | while ($i < $num_prods) |
||
| 2319 | { |
||
| 2320 | $i++; |
||
| 2321 | $row = $this->db->fetch_row($resql); |
||
| 2322 | $prodids[$i] = $row[0]; |
||
| 2323 | } |
||
| 2324 | } |
||
| 2325 | |||
| 2326 | // Initialise parametres |
||
| 2327 | $this->id=0; |
||
| 2328 | $this->specimen=1; |
||
| 2329 | |||
| 2330 | $this->ref = 'SPECIMEN'; |
||
| 2331 | $this->ref_customer = 'SPECIMENCUST'; |
||
| 2332 | $this->ref_supplier = 'SPECIMENSUPP'; |
||
| 2333 | $this->socid = 1; |
||
| 2334 | $this->statut= 0; |
||
| 2335 | $this->date_creation = (dol_now() - 3600 * 24 * 7); |
||
| 2336 | $this->date_contrat = dol_now(); |
||
| 2337 | $this->commercial_signature_id = 1; |
||
| 2338 | $this->commercial_suivi_id = 1; |
||
| 2339 | $this->note_private='This is a comment (private)'; |
||
| 2340 | $this->note_public='This is a comment (public)'; |
||
| 2341 | $this->fk_projet = 0; |
||
|
1 ignored issue
–
show
|
|||
| 2342 | // Lines |
||
| 2343 | $nbp = 5; |
||
| 2344 | $xnbp = 0; |
||
| 2345 | while ($xnbp < $nbp) |
||
| 2346 | { |
||
| 2347 | $line=new ContratLigne($this->db); |
||
| 2348 | $line->qty=1; |
||
| 2349 | $line->subprice=100; |
||
| 2350 | $line->price=100; |
||
|
1 ignored issue
–
show
|
|||
| 2351 | $line->tva_tx=19.6; |
||
| 2352 | $line->remise_percent=10; |
||
| 2353 | $line->total_ht=90; |
||
| 2354 | $line->total_ttc=107.64; // 90 * 1.196 |
||
| 2355 | $line->total_tva=17.64; |
||
| 2356 | $line->date_start = dol_now() - 500000; |
||
| 2357 | $line->date_start_real = dol_now() - 200000; |
||
| 2358 | $line->date_end = dol_now() + 500000; |
||
| 2359 | $line->date_end_real = dol_now() - 100000; |
||
| 2360 | if ($num_prods > 0) |
||
| 2361 | { |
||
| 2362 | $prodid = mt_rand(1, $num_prods); |
||
| 2363 | $line->fk_product=$prodids[$prodid]; |
||
| 2364 | } |
||
| 2365 | $this->lines[$xnbp]=$line; |
||
| 2366 | $xnbp++; |
||
| 2367 | } |
||
| 2368 | } |
||
| 2369 | |||
| 2370 | /** |
||
| 2371 | * Create an array of order lines |
||
| 2372 | * |
||
| 2373 | * @return int >0 if OK, <0 if KO |
||
| 2374 | */ |
||
| 2375 | public function getLinesArray() |
||
| 2376 | { |
||
| 2377 | return $this->fetch_lines(); |
||
| 2378 | } |
||
| 2379 | |||
| 2380 | |||
| 2381 | /** |
||
| 2382 | * Create a document onto disk according to template module. |
||
| 2383 | * |
||
| 2384 | * @param string $modele Force model to use ('' to not force) |
||
| 2385 | * @param Translate $outputlangs Object langs to use for output |
||
| 2386 | * @param int $hidedetails Hide details of lines |
||
| 2387 | * @param int $hidedesc Hide description |
||
| 2388 | * @param int $hideref Hide ref |
||
| 2389 | * @param null|array $moreparams Array to provide more information |
||
| 2390 | * @return int 0 if KO, 1 if OK |
||
| 2391 | */ |
||
| 2392 | public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null) |
||
| 2393 | { |
||
| 2394 | global $conf,$langs; |
||
| 2395 | |||
| 2396 | $langs->load("contracts"); |
||
| 2397 | |||
| 2398 | if (! dol_strlen($modele)) { |
||
| 2399 | |||
| 2400 | $modele = 'strato'; |
||
| 2401 | |||
| 2402 | if ($this->modelpdf) { |
||
| 2403 | $modele = $this->modelpdf; |
||
| 2404 | } elseif (! empty($conf->global->CONTRACT_ADDON_PDF)) { |
||
| 2405 | $modele = $conf->global->CONTRACT_ADDON_PDF; |
||
| 2406 | } |
||
| 2407 | } |
||
| 2408 | |||
| 2409 | $modelpath = "core/modules/contract/doc/"; |
||
| 2410 | |||
| 2411 | return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams); |
||
| 2412 | } |
||
| 2413 | |||
| 2414 | /** |
||
| 2415 | * Function used to replace a thirdparty id with another one. |
||
| 2416 | * |
||
| 2417 | * @param DoliDB $db Database handler |
||
| 2418 | * @param int $origin_id Old thirdparty id |
||
| 2419 | * @param int $dest_id New thirdparty id |
||
| 2420 | * @return bool |
||
| 2421 | */ |
||
| 2422 | public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id) |
||
| 2423 | { |
||
| 2424 | $tables = array( |
||
| 2425 | 'contrat' |
||
| 2426 | ); |
||
| 2427 | |||
| 2428 | return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables); |
||
| 2429 | } |
||
| 2430 | |||
| 2431 | /** |
||
| 2432 | * Load an object from its id and create a new one in database |
||
| 2433 | * |
||
| 2434 | * @param User $user User making the clone |
||
| 2435 | * @param int $socid Id of thirdparty |
||
| 2436 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 2437 | * @return int New id of clone |
||
| 2438 | */ |
||
| 2439 | public function createFromClone(User $user, $socid = 0, $notrigger = 0) |
||
| 2544 | } |
||
| 2545 | } |
||
| 2546 | } |
||
| 2547 | |||
| 2548 | |||
| 2549 | /** |
||
| 2550 | * Class to manage lines of contracts |
||
| 2551 | */ |
||
| 2552 | class ContratLigne extends CommonObjectLine |
||
| 2553 | { |
||
| 2554 | /** |
||
| 2555 | * @var string ID to identify managed object |
||
| 2556 | */ |
||
| 2557 | public $element='contratdet'; |
||
| 2558 | |||
| 2559 | /** |
||
| 2560 | * @var string Name of table without prefix where object is stored |
||
| 2561 | */ |
||
| 2562 | public $table_element='contratdet'; |
||
| 2563 | |||
| 2564 | /** |
||
| 2565 | * @var int ID |
||
| 2566 | */ |
||
| 3360 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: