commonincoterm.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 '.MAIN_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. $out .= $res->code;
  60. }
  61. }
  62. $out .= (($out && $this->location_incoterms) ? ' - ' : '').$this->location_incoterms;
  63. return $out;
  64. }
  65. /**
  66. * Return incoterms informations for pdf display
  67. *
  68. * @return string incoterms info
  69. */
  70. public function getIncotermsForPDF()
  71. {
  72. $sql = 'SELECT code FROM '.MAIN_DB_PREFIX.'c_incoterms WHERE rowid = '.(int) $this->fk_incoterms;
  73. $resql = $this->db->query($sql);
  74. if ($resql) {
  75. $num = $this->db->num_rows($resql);
  76. if ($num > 0) {
  77. $res = $this->db->fetch_object($resql);
  78. return 'Incoterm : '.$res->code.' - '.$this->location_incoterms;
  79. } else {
  80. return '';
  81. }
  82. } else {
  83. $this->errors[] = $this->db->lasterror();
  84. return false;
  85. }
  86. }
  87. /**
  88. * Define incoterms values of current object
  89. *
  90. * @param int $id_incoterm Id of incoterm to set or '' to remove
  91. * @param string $location location of incoterm
  92. * @return int <0 if KO, >0 if OK
  93. */
  94. public function setIncoterms($id_incoterm, $location)
  95. {
  96. if ($this->id && $this->table_element) {
  97. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
  98. $sql .= " SET fk_incoterms = ".($id_incoterm > 0 ? $id_incoterm : "null");
  99. $sql .= ", location_incoterms = ".($id_incoterm > 0 ? "'".$this->db->escape($location)."'" : "null");
  100. $sql .= " WHERE rowid = ".$this->id;
  101. dol_syslog(get_class($this).'::setIncoterms', LOG_DEBUG);
  102. $resql = $this->db->query($sql);
  103. if ($resql) {
  104. $this->fk_incoterms = $id_incoterm;
  105. $this->location_incoterms = $location;
  106. $sql = 'SELECT libelle as label_incotermsFROM '.MAIN_DB_PREFIX.'c_incoterms WHERE rowid = '.(int) $this->fk_incoterms;
  107. $res = $this->db->query($sql);
  108. if ($res) {
  109. $obj = $this->db->fetch_object($res);
  110. $this->label_incoterms = $obj->label_incoterms;
  111. }
  112. return 1;
  113. } else {
  114. $this->errors[] = $this->db->lasterror();
  115. return -1;
  116. }
  117. } else {
  118. return -1;
  119. }
  120. }
  121. }