accountancysystem.class.php 4.0 KB

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