commonobjectline.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /* Copyright (C) 2006-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/class/commonobjectline.class.php
  20. * \ingroup core
  21. * \brief File of the superclass of classes of lines of business objects (invoice, contract, proposal, orders, etc. ...)
  22. */
  23. /**
  24. * Parent class for class inheritance lines of business objects
  25. * This class is useless for the moment so no inherit are done on it
  26. */
  27. abstract class CommonObjectLine extends CommonObject
  28. {
  29. /**
  30. * Id of the line
  31. * @var int
  32. */
  33. public $id;
  34. /**
  35. * Id of the line
  36. * @var int
  37. * @deprecated Try to use id property as possible (even if field into database is still rowid)
  38. * @see $id
  39. */
  40. public $rowid;
  41. /**
  42. * Product/service unit code ('km', 'm', 'p', ...)
  43. * @var string
  44. */
  45. public $fk_unit;
  46. /**
  47. * Constructor
  48. *
  49. * @param DoliDB $db Database handler
  50. */
  51. public function __construct($db)
  52. {
  53. $this->db = $db;
  54. }
  55. /**
  56. * Returns the label, shot_label or code found in units dictionary from ->fk_unit.
  57. * A langs->trans() must be called on result to get translated value.
  58. *
  59. * @param string $type Label type ('long', 'short' or 'code'). This can be a translation key.
  60. * @return string|int <0 if KO, label if OK (Example: 'long', 'short' or 'unitCODE')
  61. */
  62. public function getLabelOfUnit($type = 'long')
  63. {
  64. global $langs;
  65. if (!$this->fk_unit) {
  66. return '';
  67. }
  68. $langs->load('products');
  69. $label_type = 'label';
  70. $label_type = 'label';
  71. if ($type == 'short') {
  72. $label_type = 'short_label';
  73. } elseif ($type == 'code') {
  74. $label_type = 'code';
  75. }
  76. $sql = "SELECT ".$label_type.", code from ".MAIN_DB_PREFIX."c_units where rowid = ".((int) $this->fk_unit);
  77. $resql = $this->db->query($sql);
  78. if ($resql && $this->db->num_rows($resql) > 0) {
  79. $res = $this->db->fetch_array($resql);
  80. if ($label_type == 'code') {
  81. $label = 'unit'.$res['code'];
  82. } else {
  83. $label = $res[$label_type];
  84. }
  85. $this->db->free($resql);
  86. return $label;
  87. } else {
  88. $this->error = $this->db->lasterror();
  89. dol_syslog(get_class($this)."::getLabelOfUnit Error ".$this->error, LOG_ERR);
  90. return -1;
  91. }
  92. }
  93. // Currently we need function at end of file CommonObject for all object lines. Should find a way to avoid duplicate code.
  94. // For the moment we use the extends on CommonObject until PHP min is 5.4 so use Traits.
  95. }