accountancysystem.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 string[] Array of Errors code (or messages)
  39. */
  40. public $errors = array();
  41. /**
  42. * @var int ID
  43. */
  44. public $rowid;
  45. /**
  46. * @var int ID
  47. */
  48. public $fk_pcg_version;
  49. /**
  50. * @var string pcg type
  51. */
  52. public $pcg_type;
  53. /**
  54. * @var string Accountancy System numero
  55. */
  56. public $numero;
  57. /**
  58. * @var string Accountancy System label
  59. */
  60. public $label;
  61. /**
  62. * @var string account number
  63. */
  64. public $account_number;
  65. /**
  66. * @var string account parent
  67. */
  68. public $account_parent;
  69. /**
  70. * Constructor
  71. *
  72. * @param DoliDB $db handler
  73. */
  74. public function __construct($db)
  75. {
  76. $this->db = $db;
  77. }
  78. /**
  79. * Load record in memory
  80. *
  81. * @param int $rowid Id
  82. * @param string $ref ref
  83. * @return int <0 if KO, Id of record if OK and found
  84. */
  85. public function fetch($rowid = 0, $ref = '')
  86. {
  87. global $conf;
  88. if ($rowid > 0 || $ref) {
  89. $sql = "SELECT a.rowid, a.pcg_version, a.label, a.active";
  90. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_system as a";
  91. $sql .= " WHERE";
  92. if ($rowid) {
  93. $sql .= " a.rowid = ".((int) $rowid);
  94. } elseif ($ref) {
  95. $sql .= " a.pcg_version = '".$this->db->escape($ref)."'";
  96. }
  97. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  98. $result = $this->db->query($sql);
  99. if ($result) {
  100. $obj = $this->db->fetch_object($result);
  101. if ($obj) {
  102. $this->id = $obj->rowid;
  103. $this->rowid = $obj->rowid;
  104. $this->pcg_version = $obj->pcg_version;
  105. $this->ref = $obj->pcg_version;
  106. $this->label = $obj->label;
  107. $this->active = $obj->active;
  108. return $this->id;
  109. } else {
  110. return 0;
  111. }
  112. } else {
  113. $this->error = "Error ".$this->db->lasterror();
  114. $this->errors[] = "Error ".$this->db->lasterror();
  115. }
  116. }
  117. return -1;
  118. }
  119. /**
  120. * Insert accountancy system name into database
  121. *
  122. * @param User $user making insert
  123. * @return int if KO, Id of line if OK
  124. */
  125. public function create($user)
  126. {
  127. $now = dol_now();
  128. $sql = "INSERT INTO ".MAIN_DB_PREFIX."accounting_system";
  129. $sql .= " (date_creation, fk_user_author, numero, label)";
  130. $sql .= " VALUES ('".$this->db->idate($now)."',".((int) $user->id).",'".$this->db->escape($this->numero)."','".$this->db->escape($this->label)."')";
  131. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  132. $resql = $this->db->query($sql);
  133. if ($resql) {
  134. $id = $this->db->last_insert_id(MAIN_DB_PREFIX."accounting_system");
  135. if ($id > 0) {
  136. $this->rowid = $id;
  137. $result = $this->rowid;
  138. } else {
  139. $result = - 2;
  140. $this->error = "AccountancySystem::Create Error $result";
  141. dol_syslog($this->error, LOG_ERR);
  142. }
  143. } else {
  144. $result = - 1;
  145. $this->error = "AccountancySystem::Create Error $result";
  146. dol_syslog($this->error, LOG_ERR);
  147. }
  148. return $result;
  149. }
  150. }