accountancysystem.class.php 4.1 KB

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