commonincoterm.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /* Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/class/commonincoterm.class.php
  19. * \ingroup core
  20. * \brief File of the superclass of object classes that support incoterm (customer and supplier)
  21. */
  22. /**
  23. * Superclass for incoterm classes
  24. */
  25. trait CommonIncoterm
  26. {
  27. /**
  28. * @var int ID incoterm.
  29. * @see setIncoterms()
  30. */
  31. public $fk_incoterms;
  32. /**
  33. * @var string Label of incoterm. Used for tooltip.
  34. * @see SetIncoterms()
  35. */
  36. public $label_incoterms;
  37. /**
  38. * @var string Location of incoterm.
  39. * @see display_incoterms()
  40. */
  41. public $location_incoterms;
  42. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  43. /**
  44. * Return incoterms informations
  45. * TODO Use a cache for label get
  46. *
  47. * @return string incoterms info
  48. */
  49. public function display_incoterms()
  50. {
  51. // phpcs:enable
  52. $out = '';
  53. $this->label_incoterms = '';
  54. if (!empty($this->fk_incoterms)) {
  55. $sql = "SELECT code FROM ".$this->db->prefix()."c_incoterms WHERE rowid = ".(int) $this->fk_incoterms;
  56. $result = $this->db->query($sql);
  57. if ($result) {
  58. $res = $this->db->fetch_object($result);
  59. if ($res) {
  60. $out .= $res->code;
  61. }
  62. }
  63. }
  64. $out .= (($out && $this->location_incoterms) ? ' - ' : '').$this->location_incoterms;
  65. return $out;
  66. }
  67. /**
  68. * Return incoterms informations for pdf display
  69. *
  70. * @return string|boolean Incoterms info or false
  71. */
  72. public function getIncotermsForPDF()
  73. {
  74. $sql = "SELECT code FROM ".$this->db->prefix()."c_incoterms WHERE rowid = ".(int) $this->fk_incoterms;
  75. $resql = $this->db->query($sql);
  76. if ($resql) {
  77. $num = $this->db->num_rows($resql);
  78. if ($num > 0) {
  79. $res = $this->db->fetch_object($resql);
  80. if ($res) {
  81. return 'Incoterm : '.$res->code.' - '.$this->location_incoterms;
  82. } else {
  83. return $res;
  84. }
  85. } else {
  86. return '';
  87. }
  88. } else {
  89. $this->errors[] = $this->db->lasterror();
  90. return false;
  91. }
  92. }
  93. /**
  94. * Define incoterms values of current object
  95. *
  96. * @param int $id_incoterm Id of incoterm to set or '' to remove
  97. * @param string $location location of incoterm
  98. * @return int <0 if KO, >0 if OK
  99. */
  100. public function setIncoterms($id_incoterm, $location)
  101. {
  102. if ($this->id && $this->table_element) {
  103. $sql = "UPDATE ".$this->db->prefix().$this->table_element;
  104. $sql .= " SET fk_incoterms = ".($id_incoterm > 0 ? ((int) $id_incoterm) : "null");
  105. $sql .= ", location_incoterms = ".($id_incoterm > 0 ? "'".$this->db->escape($location)."'" : "null");
  106. $sql .= " WHERE rowid = ".((int) $this->id);
  107. dol_syslog(get_class($this).'::setIncoterms', LOG_DEBUG);
  108. $resql = $this->db->query($sql);
  109. if ($resql) {
  110. $this->fk_incoterms = $id_incoterm;
  111. $this->location_incoterms = $location;
  112. $sql = "SELECT libelle as label_incoterms FROM ".$this->db->prefix()."c_incoterms WHERE rowid = ".(int) $this->fk_incoterms;
  113. $res = $this->db->query($sql);
  114. if ($res) {
  115. $obj = $this->db->fetch_object($res);
  116. $this->label_incoterms = $obj->label_incoterms;
  117. }
  118. return 1;
  119. } else {
  120. $this->errors[] = $this->db->lasterror();
  121. return -1;
  122. }
  123. } else {
  124. return -1;
  125. }
  126. }
  127. }