accountancysystem.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2014 Alexandre Spangaro <aspangaro@open-dsi.fr>
  4. * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/accountancy/class/accountancysystem.class.php
  21. * \ingroup Accountancy (Double entries)
  22. * \brief File of class to manage accountancy systems
  23. */
  24. /**
  25. * Class to manage accountancy systems
  26. */
  27. class AccountancySystem
  28. {
  29. /**
  30. * @var DoliDB Database handler.
  31. */
  32. public $db;
  33. /**
  34. * @var string Error code (or message)
  35. */
  36. public $error = '';
  37. /**
  38. * @var int ID
  39. */
  40. public $rowid;
  41. /**
  42. * @var int ID
  43. */
  44. public $fk_pcg_version;
  45. /**
  46. * @var string pcg type
  47. */
  48. public $pcg_type;
  49. /**
  50. * @var string Accountancy System numero
  51. */
  52. public $numero;
  53. /**
  54. * @var string Accountancy System label
  55. */
  56. public $label;
  57. /**
  58. * @var string account number
  59. */
  60. public $account_number;
  61. /**
  62. * @var string account parent
  63. */
  64. public $account_parent;
  65. /**
  66. * Constructor
  67. *
  68. * @param DoliDB $db handler
  69. */
  70. public function __construct($db)
  71. {
  72. $this->db = $db;
  73. }
  74. /**
  75. * Load record in memory
  76. *
  77. * @param int $rowid Id
  78. * @param string $ref ref
  79. * @return int <0 if KO, Id of record if OK and found
  80. */
  81. public function fetch($rowid = 0, $ref = '')
  82. {
  83. global $conf;
  84. if ($rowid > 0 || $ref)
  85. {
  86. $sql = "SELECT a.rowid, a.pcg_version, a.label, a.active";
  87. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_system as a";
  88. $sql .= " WHERE";
  89. if ($rowid) {
  90. $sql .= " a.rowid = '".$rowid."'";
  91. } elseif ($ref) {
  92. $sql .= " a.pcg_version = '".$this->db->escape($ref)."'";
  93. }
  94. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  95. $result = $this->db->query($sql);
  96. if ($result) {
  97. $obj = $this->db->fetch_object($result);
  98. if ($obj) {
  99. $this->id = $obj->rowid;
  100. $this->rowid = $obj->rowid;
  101. $this->pcg_version = $obj->pcg_version;
  102. $this->ref = $obj->pcg_version;
  103. $this->label = $obj->label;
  104. $this->active = $obj->active;
  105. return $this->id;
  106. } else {
  107. return 0;
  108. }
  109. } else {
  110. $this->error = "Error ".$this->db->lasterror();
  111. $this->errors[] = "Error ".$this->db->lasterror();
  112. }
  113. }
  114. return -1;
  115. }
  116. /**
  117. * Insert accountancy system name into database
  118. *
  119. * @param User $user making insert
  120. * @return int if KO, Id of line if OK
  121. */
  122. public function create($user)
  123. {
  124. $now = dol_now();
  125. $sql = "INSERT INTO ".MAIN_DB_PREFIX."accounting_system";
  126. $sql .= " (date_creation, fk_user_author, numero, label)";
  127. $sql .= " VALUES ('".$this->db->idate($now)."',".$user->id.",'".$this->db->escape($this->numero)."','".$this->db->escape($this->label)."')";
  128. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  129. $resql = $this->db->query($sql);
  130. if ($resql) {
  131. $id = $this->db->last_insert_id(MAIN_DB_PREFIX."accounting_system");
  132. if ($id > 0) {
  133. $this->rowid = $id;
  134. $result = $this->rowid;
  135. } else {
  136. $result = - 2;
  137. $this->error = "AccountancySystem::Create Error $result";
  138. dol_syslog($this->error, LOG_ERR);
  139. }
  140. } else {
  141. $result = - 1;
  142. $this->error = "AccountancySystem::Create Error $result";
  143. dol_syslog($this->error, LOG_ERR);
  144. }
  145. return $result;
  146. }
  147. }