accountancycategory.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. <?php
  2. /* Copyright (C) 2016 Jamal Elbaz <jamelbaz@gmail.pro>
  3. * Copyright (C) 2016-2017 Alexandre Spangaro <aspangaro@zendsi.com>
  4. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  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/accountancycategory.class.php
  21. * \ingroup Advanced accountancy
  22. * \brief File of class to manage categories of an accounting category_type
  23. */
  24. // Class
  25. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  26. /**
  27. * Class to manage categories of an accounting account
  28. */
  29. class AccountancyCategory // extends CommonObject
  30. {
  31. /**
  32. * @var DoliDB Database handler.
  33. */
  34. public $db;
  35. /**
  36. * @var string Error string
  37. * @see errors
  38. */
  39. public $error;
  40. /**
  41. * @var string[] Error codes (or messages)
  42. */
  43. public $errors = array();
  44. /**
  45. * @var string ID to identify managed object
  46. */
  47. public $element='c_accounting_category';
  48. /**
  49. * @var string Name of table without prefix where object is stored
  50. */
  51. public $table_element='c_accounting_category';
  52. /**
  53. * @var int ID
  54. */
  55. public $id;
  56. /**
  57. * @var mixed Sample property 1
  58. */
  59. public $code;
  60. /**
  61. * @var string Accountancy Category label
  62. */
  63. public $label;
  64. /**
  65. * @var mixed Sample property 1
  66. */
  67. public $range_account;
  68. /**
  69. * @var mixed Sample property 1
  70. */
  71. public $sens;
  72. /**
  73. * @var mixed Sample property 1
  74. */
  75. public $category_type;
  76. /**
  77. * @var mixed Sample property 1
  78. */
  79. public $formula;
  80. /**
  81. * @var mixed Sample property 1
  82. */
  83. public $position;
  84. /**
  85. * @var mixed Sample property 1
  86. */
  87. public $fk_country;
  88. /**
  89. * @var mixed Sample property 1
  90. */
  91. public $active;
  92. /**
  93. * @var mixed Sample property 1
  94. */
  95. public $lines_cptbk;
  96. /**
  97. * @var mixed Sample property 1
  98. */
  99. public $lines_display;
  100. /**
  101. * @var mixed Sample property 1
  102. */
  103. public $sdc;
  104. /**
  105. * Constructor
  106. *
  107. * @param DoliDb $db Database handler
  108. */
  109. function __construct($db)
  110. {
  111. $this->db = $db;
  112. }
  113. /**
  114. * Create object into database
  115. *
  116. * @param User $user User that create
  117. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  118. * @return int <0 if KO, Id of created object if OK
  119. */
  120. function create($user, $notrigger=0)
  121. {
  122. global $conf, $langs;
  123. $error=0;
  124. // Clean parameters
  125. if (isset($this->code)) $this->code=trim($this->code);
  126. if (isset($this->label)) $this->label=trim($this->label);
  127. if (isset($this->range_account)) $this->range_account=trim($this->range_account);
  128. if (isset($this->sens)) $this->sens=trim($this->sens);
  129. if (isset($this->category_type)) $this->category_type=trim($this->category_type);
  130. if (isset($this->formula)) $this->formula=trim($this->formula);
  131. if (isset($this->position)) $this->position=trim($this->position);
  132. if (isset($this->fk_country)) $this->fk_country=trim($this->fk_country);
  133. if (isset($this->active)) $this->active=trim($this->active);
  134. // Check parameters
  135. // Put here code to add control on parameters values
  136. // Insert request
  137. $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_accounting_category(";
  138. if ($this->rowid > 0) $sql.= "rowid,";
  139. $sql.= "code,";
  140. $sql.= "label,";
  141. $sql.= "range_account,";
  142. $sql.= "sens,";
  143. $sql.= "category_type,";
  144. $sql.= "formula,";
  145. $sql.= "position,";
  146. $sql.= "fk_country,";
  147. $sql.= "active";
  148. $sql.= ") VALUES (";
  149. if ($this->rowid > 0) $sql.= " ".$this->rowid.",";
  150. $sql.= " ".(! isset($this->code)?'NULL':"'".$this->db->escape($this->code)."'").",";
  151. $sql.= " ".(! isset($this->label)?'NULL':"'".$this->db->escape($this->label)."'").",";
  152. $sql.= " ".(! isset($this->range_account)?'NULL':"'".$this->db->escape($this->range_account)."'").",";
  153. $sql.= " ".(! isset($this->sens)?'NULL':"'".$this->db->escape($this->sens)."'").",";
  154. $sql.= " ".(! isset($this->category_type)?'NULL':"'".$this->db->escape($this->category_type)."'").",";
  155. $sql.= " ".(! isset($this->formula)?'NULL':"'".$this->db->escape($this->formula)."'").",";
  156. $sql.= " ".(! isset($this->position)?'NULL':$this->db->escape($this->position)).",";
  157. $sql.= " ".(! isset($this->fk_country)?'NULL':$this->db->escape($this->fk_country)).",";
  158. $sql.= " ".(! isset($this->active)?'NULL':$this->db->escape($this->active));
  159. $sql.= ")";
  160. $this->db->begin();
  161. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  162. $resql=$this->db->query($sql);
  163. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  164. if (! $error)
  165. {
  166. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_accounting_category");
  167. // Uncomment this and change MYOBJECT to your own tag if you
  168. // want this action call a trigger.
  169. //if (! $notrigger)
  170. //{
  171. // // Call triggers
  172. // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  173. // $interface=new Interfaces($this->db);
  174. // $result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  175. // if ($result < 0) { $error++; $this->errors=$interface->errors; }
  176. // // End call triggers
  177. //}
  178. }
  179. // Commit or rollback
  180. if ($error)
  181. {
  182. foreach($this->errors as $errmsg)
  183. {
  184. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  185. $this->error.=($this->error?', '.$errmsg:$errmsg);
  186. }
  187. $this->db->rollback();
  188. return -1*$error;
  189. }
  190. else
  191. {
  192. $this->db->commit();
  193. return $this->id;
  194. }
  195. }
  196. /**
  197. * Load object in memory from database
  198. *
  199. * @param int $id Id object
  200. * @param string $code Code
  201. * @param string $label Label
  202. * @return int <0 if KO, >0 if OK
  203. */
  204. function fetch($id,$code='',$label='')
  205. {
  206. global $langs;
  207. $sql = "SELECT";
  208. $sql.= " t.rowid,";
  209. $sql.= " t.code,";
  210. $sql.= " t.label,";
  211. $sql.= " t.range_account,";
  212. $sql.= " t.sens,";
  213. $sql.= " t.category_type,";
  214. $sql.= " t.formula,";
  215. $sql.= " t.position,";
  216. $sql.= " t.fk_country,";
  217. $sql.= " t.active";
  218. $sql.= " FROM ".MAIN_DB_PREFIX."c_accounting_category as t";
  219. if ($id) $sql.= " WHERE t.rowid = ".$id;
  220. elseif ($code) $sql.= " WHERE t.code = '".$this->db->escape($code)."'";
  221. elseif ($label) $sql.= " WHERE t.label = '".$this->db->escape($label)."'";
  222. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  223. $resql=$this->db->query($sql);
  224. if ($resql)
  225. {
  226. if ($this->db->num_rows($resql))
  227. {
  228. $obj = $this->db->fetch_object($resql);
  229. $this->id = $obj->rowid;
  230. $this->code = $obj->code;
  231. $this->label = $obj->label;
  232. $this->range_account = $obj->range_account;
  233. $this->sens = $obj->sens;
  234. $this->category_type = $obj->category_type;
  235. $this->formula = $obj->formula;
  236. $this->position = $obj->position;
  237. $this->fk_country = $obj->fk_country;
  238. $this->active = $obj->active;
  239. }
  240. $this->db->free($resql);
  241. return 1;
  242. }
  243. else
  244. {
  245. $this->error="Error ".$this->db->lasterror();
  246. return -1;
  247. }
  248. }
  249. /**
  250. * Update object into database
  251. *
  252. * @param User $user User that modify
  253. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  254. * @return int <0 if KO, >0 if OK
  255. */
  256. function update($user=null, $notrigger=0)
  257. {
  258. global $conf, $langs;
  259. $error=0;
  260. // Clean parameters
  261. if (isset($this->code)) $this->code=trim($this->code);
  262. if (isset($this->label)) $this->label=trim($this->label);
  263. if (isset($this->range_account)) $this->range_account=trim($this->range_account);
  264. if (isset($this->sens)) $this->sens=trim($this->sens);
  265. if (isset($this->category_type)) $this->category_type=trim($this->category_type);
  266. if (isset($this->formula)) $this->formula=trim($this->formula);
  267. if (isset($this->position)) $this->position=trim($this->position);
  268. if (isset($this->fk_country)) $this->fk_country=trim($this->fk_country);
  269. if (isset($this->active)) $this->active=trim($this->active);
  270. // Check parameters
  271. // Put here code to add control on parameters values
  272. // Update request
  273. $sql = "UPDATE ".MAIN_DB_PREFIX."c_accounting_category SET";
  274. $sql.= " code=".(isset($this->code)?"'".$this->db->escape($this->code)."'":"null").",";
  275. $sql.= " label=".(isset($this->label)?"'".$this->db->escape($this->label)."'":"null").",";
  276. $sql.= " range_account=".(isset($this->range_account)?"'".$this->db->escape($this->range_account)."'":"null").",";
  277. $sql.= " sens=".(isset($this->sens)?$this->sens:"null").",";
  278. $sql.= " category_type=".(isset($this->category_type)?$this->category_type:"null").",";
  279. $sql.= " formula=".(isset($this->formula)?"'".$this->db->escape($this->formula)."'":"null").",";
  280. $sql.= " position=".(isset($this->position)?$this->position:"null").",";
  281. $sql.= " fk_country=".(isset($this->fk_country)?$this->fk_country:"null").",";
  282. $sql.= " active=".(isset($this->active)?$this->active:"null")."";
  283. $sql.= " WHERE rowid=".$this->id;
  284. $this->db->begin();
  285. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  286. $resql = $this->db->query($sql);
  287. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  288. if (! $error)
  289. {
  290. // Uncomment this and change MYOBJECT to your own tag if you
  291. // want this action call a trigger.
  292. //if (! $notrigger)
  293. //{
  294. // // Call triggers
  295. // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  296. // $interface=new Interfaces($this->db);
  297. // $result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  298. // if ($result < 0) { $error++; $this->errors=$interface->errors; }
  299. // // End call triggers
  300. //}
  301. }
  302. // Commit or rollback
  303. if ($error)
  304. {
  305. foreach($this->errors as $errmsg)
  306. {
  307. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  308. $this->error.=($this->error?', '.$errmsg:$errmsg);
  309. }
  310. $this->db->rollback();
  311. return -1*$error;
  312. }
  313. else
  314. {
  315. $this->db->commit();
  316. return 1;
  317. }
  318. }
  319. /**
  320. * Delete object in database
  321. *
  322. * @param User $user User that delete
  323. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  324. * @return int <0 if KO, >0 if OK
  325. */
  326. function delete($user, $notrigger=0)
  327. {
  328. global $conf, $langs;
  329. $error=0;
  330. $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_accounting_category";
  331. $sql.= " WHERE rowid=".$this->id;
  332. $this->db->begin();
  333. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  334. $resql = $this->db->query($sql);
  335. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  336. if (! $error)
  337. {
  338. // Uncomment this and change MYOBJECT to your own tag if you
  339. // want this action call a trigger.
  340. //if (! $notrigger)
  341. //{
  342. // // Call triggers
  343. // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  344. // $interface=new Interfaces($this->db);
  345. // $result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  346. // if ($result < 0) { $error++; $this->errors=$interface->errors; }
  347. // // End call triggers
  348. //}
  349. }
  350. // Commit or rollback
  351. if ($error)
  352. {
  353. foreach($this->errors as $errmsg)
  354. {
  355. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  356. $this->error.=($this->error?', '.$errmsg:$errmsg);
  357. }
  358. $this->db->rollback();
  359. return -1*$error;
  360. }
  361. else
  362. {
  363. $this->db->commit();
  364. return 1;
  365. }
  366. }
  367. /**
  368. * Function to select all accounting accounts from an accounting category
  369. *
  370. * @param int $id Id
  371. * @return int <0 if KO, 0 if not found, >0 if OK
  372. */
  373. public function display($id)
  374. {
  375. global $conf;
  376. $sql = "SELECT t.rowid, t.account_number, t.label";
  377. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t";
  378. $sql .= " WHERE t.fk_accounting_category = " . $id;
  379. $sql .= " AND t.entity = " . $conf->entity;
  380. $this->lines_display = array();
  381. dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG);
  382. $resql = $this->db->query($sql);
  383. if ($resql) {
  384. $num = $this->db->num_rows($resql);
  385. if ($num) {
  386. while ( $obj = $this->db->fetch_object($resql) ) {
  387. $this->lines_display[] = $obj;
  388. }
  389. }
  390. return $num;
  391. } else {
  392. $this->error = "Error " . $this->db->lasterror();
  393. $this->errors[] = $this->error;
  394. dol_syslog(__METHOD__ . " " . implode(',' . $this->errors), LOG_ERR);
  395. return - 1;
  396. }
  397. }
  398. /**
  399. * Function to select accounting category of an accounting account present in chart of accounts
  400. *
  401. * @param int $id Id category
  402. *
  403. * @return int <0 if KO, 0 if not found, >0 if OK
  404. */
  405. public function getCptBK($id)
  406. {
  407. global $conf;
  408. $sql = "SELECT t.numero_compte, t.label_operation, t.doc_ref";
  409. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as t";
  410. $sql .= " WHERE t.numero_compte NOT IN (";
  411. $sql .= " SELECT t.account_number";
  412. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t";
  413. $sql .= " WHERE t.fk_accounting_category = " . $id . " AND t.entity = " . $conf->entity.")";
  414. $sql .= " AND t.numero_compte IN (";
  415. $sql .= " SELECT DISTINCT aa.account_number";
  416. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
  417. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  418. $sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
  419. $sql .= " AND aa.active = 1";
  420. $sql .= " AND aa.entity = = " . $conf->entity . ")";
  421. $sql .= " GROUP BY t.numero_compte, t.label_operation, t.doc_ref";
  422. $sql .= " ORDER BY t.numero_compte";
  423. $this->lines_CptBk = array ();
  424. dol_syslog(__METHOD__, LOG_DEBUG);
  425. $resql = $this->db->query($sql);
  426. if ($resql) {
  427. $num = $this->db->num_rows($resql);
  428. if ($num) {
  429. while ( $obj = $this->db->fetch_object($resql) ) {
  430. $this->lines_cptbk[] = $obj;
  431. }
  432. }
  433. return $num;
  434. } else {
  435. $this->error = "Error " . $this->db->lasterror();
  436. $this->errors[] = $this->error;
  437. dol_syslog(__METHOD__ . " " . implode(',' . $this->errors), LOG_ERR);
  438. return - 1;
  439. }
  440. }
  441. /**
  442. * Function to select accounting category of an accounting account present in chart of accounts
  443. *
  444. * @param int $id Id category
  445. *
  446. * @return int <0 if KO, 0 if not found, >0 if OK
  447. */
  448. public function getAccountsWithNoCategory($id)
  449. {
  450. global $conf;
  451. $sql = "SELECT aa.account_number as numero_compte, aa.label as label_compte";
  452. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
  453. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  454. $sql .= " WHERE (aa.fk_accounting_category != ".$id." OR aa.fk_accounting_category IS NULL)";
  455. $sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
  456. $sql .= " AND aa.active = 1";
  457. $sql .= " AND aa.entity = " . $conf->entity;
  458. $sql .= " GROUP BY aa.account_number, aa.label";
  459. $sql .= " ORDER BY aa.account_number, aa.label";
  460. $this->lines_CptBk = array ();
  461. dol_syslog(__METHOD__, LOG_DEBUG);
  462. $resql = $this->db->query($sql);
  463. if ($resql) {
  464. $num = $this->db->num_rows($resql);
  465. if ($num) {
  466. while ( $obj = $this->db->fetch_object($resql) ) {
  467. $this->lines_cptbk[] = $obj;
  468. }
  469. }
  470. return $num;
  471. } else {
  472. $this->error = "Error " . $this->db->lasterror();
  473. $this->errors[] = $this->error;
  474. dol_syslog(__METHOD__ . " " . implode(',' . $this->errors), LOG_ERR);
  475. return - 1;
  476. }
  477. }
  478. /**
  479. * Function to add an accounting account in an accounting category
  480. *
  481. * @param int $id_cat Id category
  482. * @param array $cpts list of accounts array
  483. *
  484. * @return int <0 if KO, >0 if OK
  485. */
  486. public function updateAccAcc($id_cat, $cpts = array())
  487. {
  488. global $conf;
  489. $error = 0;
  490. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  491. $sql = "SELECT aa.rowid,aa.account_number ";
  492. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
  493. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  494. $sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
  495. $sql .= " AND aa.active = 1";
  496. $sql .= " AND aa.entity = " . $conf->entity;
  497. $this->db->begin();
  498. dol_syslog(__METHOD__, LOG_DEBUG);
  499. $resql = $this->db->query($sql);
  500. if (! $resql) {
  501. $error ++;
  502. $this->errors[] = "Error " . $this->db->lasterror();
  503. $this->db->rollback();
  504. return -1;
  505. }
  506. while ( $obj = $this->db->fetch_object($resql))
  507. {
  508. if (array_key_exists(length_accountg($obj->account_number), $cpts))
  509. {
  510. $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account";
  511. $sql .= " SET fk_accounting_category=" . $id_cat;
  512. $sql .= " WHERE rowid=".$obj->rowid;
  513. dol_syslog(__METHOD__, LOG_DEBUG);
  514. $resqlupdate = $this->db->query($sql);
  515. if (! $resqlupdate) {
  516. $error ++;
  517. $this->errors[] = "Error " . $this->db->lasterror();
  518. }
  519. }
  520. }
  521. // Commit or rollback
  522. if ($error) {
  523. foreach ( $this->errors as $errmsg ) {
  524. dol_syslog(__METHOD__ . " " . $errmsg, LOG_ERR);
  525. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  526. }
  527. $this->db->rollback();
  528. return - 1 * $error;
  529. } else {
  530. $this->db->commit();
  531. return 1;
  532. }
  533. }
  534. /**
  535. * Function to delete an accounting account from an accounting category
  536. *
  537. * @param int $cpt_id Id of accounting account
  538. *
  539. * @return int <0 if KO, >0 if OK
  540. */
  541. public function deleteCptCat($cpt_id)
  542. {
  543. $error = 0;
  544. $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account as aa";
  545. $sql .= " SET fk_accounting_category= 0";
  546. $sql .= " WHERE aa.rowid= " . $cpt_id;
  547. $this->db->begin();
  548. dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG);
  549. $resql = $this->db->query($sql);
  550. if (! $resql) {
  551. $error ++;
  552. $this->errors[] = "Error " . $this->db->lasterror();
  553. }
  554. // Commit or rollback
  555. if ($error) {
  556. foreach ( $this->errors as $errmsg ) {
  557. dol_syslog(__METHOD__ . " " . $errmsg, LOG_ERR);
  558. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  559. }
  560. $this->db->rollback();
  561. return - 1 * $error;
  562. } else {
  563. $this->db->commit();
  564. return 1;
  565. }
  566. }
  567. /**
  568. * Function to know all category from accounting account
  569. *
  570. * @return array Result in table
  571. */
  572. public function getCatsCpts()
  573. {
  574. global $mysoc,$conf;
  575. $sql = "";
  576. if (empty($mysoc->country_id)) {
  577. dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined');
  578. exit();
  579. }
  580. $sql = "SELECT t.rowid, t.account_number, t.label as account_label, cat.code, cat.position, cat.label as name_cat, cat.sens ";
  581. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t, " . MAIN_DB_PREFIX . "c_accounting_category as cat";
  582. $sql .= " WHERE t.fk_accounting_category IN ( SELECT c.rowid ";
  583. $sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c";
  584. $sql .= " WHERE c.active = 1";
  585. $sql .= " AND c.entity = " . $conf->entity;
  586. $sql .= " AND (c.fk_country = ".$mysoc->country_id." OR c.fk_country = 0)";
  587. $sql .= " AND cat.rowid = t.fk_accounting_category";
  588. $sql .= " AND t.entity = " . $conf->entity;
  589. $sql .= " ORDER BY cat.position ASC";
  590. $resql = $this->db->query($sql);
  591. if ($resql) {
  592. $i = 0;
  593. $obj = '';
  594. $num = $this->db->num_rows($resql);
  595. $data = array ();
  596. if ($num) {
  597. while ( $obj = $this->db->fetch_object($resql) ) {
  598. $name_cat = $obj->name_cat;
  599. $data[$name_cat][$i] = array (
  600. 'id' => $obj->rowid,
  601. 'code' => $obj->code,
  602. 'position' => $obj->position,
  603. 'account_number' => $obj->account_number,
  604. 'account_label' => $obj->account_label,
  605. 'sens' => $obj->sens
  606. );
  607. $i ++;
  608. }
  609. }
  610. return $data;
  611. } else {
  612. $this->error = "Error " . $this->db->lasterror();
  613. dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR);
  614. return -1;
  615. }
  616. }
  617. /**
  618. * Function to show result of an accounting account from the ledger with a direction and a period
  619. *
  620. * @param int $cpt Id accounting account
  621. * @param string $month Specifig month - Can be empty
  622. * @param string $date_start Date start
  623. * @param string $date_end Date end
  624. * @param int $sens Sens of the account: 0: credit - debit, 1: debit - credit
  625. * @param string $thirdparty_code Thirdparty code
  626. * @return integer Result in table
  627. */
  628. public function getResult($cpt, $month, $date_start, $date_end, $sens, $thirdparty_code='nofilter')
  629. {
  630. $sql = "SELECT SUM(t.debit) as debit, SUM(t.credit) as credit";
  631. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as t";
  632. $sql .= " WHERE t.numero_compte = '" . $cpt."'";
  633. if (! empty($date_start) && ! empty($date_end))
  634. $sql.= " AND t.doc_date >= '".$this->db->idate($date_start)."' AND t.doc_date <= '".$this->db->idate($date_end)."'";
  635. if (! empty($month)) {
  636. $sql .= " AND MONTH(t.doc_date) = " . $month;
  637. }
  638. if ($thirdparty_code != 'nofilter')
  639. {
  640. $sql .= " AND thirdparty_code = '".$this->db->escape($thirdparty_code)."'";
  641. }
  642. dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG);
  643. $resql = $this->db->query($sql);
  644. if ($resql) {
  645. $num = $this->db->num_rows($resql);
  646. $this->sdc = 0;
  647. if ($num) {
  648. $obj = $this->db->fetch_object($resql);
  649. if ($sens == 1) {
  650. $this->sdc = $obj->debit - $obj->credit;
  651. } else {
  652. $this->sdc = $obj->credit - $obj->debit;
  653. }
  654. }
  655. return $num;
  656. } else {
  657. $this->error = "Error " . $this->db->lasterror();
  658. dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR);
  659. return - 1;
  660. }
  661. }
  662. /**
  663. * Return list of personalized groups that are active
  664. *
  665. * @param int $categorytype -1=All, 0=Only non computed groups, 1=Only computed groups
  666. * @return array|int Array of groups or -1 if error
  667. */
  668. public function getCats($categorytype=-1)
  669. {
  670. global $db, $langs, $user, $mysoc, $conf;
  671. if (empty($mysoc->country_id)) {
  672. dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined');
  673. exit();
  674. }
  675. $sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position, c.category_type";
  676. $sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c";
  677. $sql .= " WHERE c.active = 1 ";
  678. $sql .= " AND c.entity = " . $conf->entity;
  679. if ($categorytype >= 0) $sql.=" AND c.category_type = 1";
  680. $sql .= " AND (c.fk_country = ".$mysoc->country_id." OR c.fk_country = 0)";
  681. $sql .= " ORDER BY c.position ASC";
  682. $resql = $this->db->query($sql);
  683. if ($resql) {
  684. $i = 0;
  685. $obj = '';
  686. $num = $this->db->num_rows($resql);
  687. $data = array ();
  688. if ($num) {
  689. while ( $i < $num ) {
  690. $obj = $this->db->fetch_object($resql);
  691. $data[] = array (
  692. 'rowid' => $obj->rowid,
  693. 'code' => $obj->code,
  694. 'label' => $obj->label,
  695. 'formula' => $obj->formula,
  696. 'position' => $obj->position,
  697. 'category_type' => $obj->category_type
  698. );
  699. $i++;
  700. }
  701. }
  702. return $data;
  703. } else {
  704. $this->error = "Error " . $this->db->lasterror();
  705. $this->errors[] = $this->error;
  706. dol_syslog(__METHOD__ . " " . implode(',', $this->errors), LOG_ERR);
  707. return - 1;
  708. }
  709. }
  710. /**
  711. * Get all accounting account of a group.
  712. * You must choose between first parameter (personalized group) or the second (free criteria filter)
  713. *
  714. * @param int $cat_id Id if personalized accounting group/category
  715. * @param string $predefinedgroupwhere Sql criteria filter to select accounting accounts
  716. * @return array|int Array of accounting accounts or -1 if error
  717. */
  718. public function getCptsCat($cat_id, $predefinedgroupwhere='')
  719. {
  720. global $mysoc;
  721. $sql = '';
  722. if (empty($mysoc->country_id) && empty($mysoc->country_code)) {
  723. dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined');
  724. exit();
  725. }
  726. if (! empty($cat_id))
  727. {
  728. $sql = "SELECT t.rowid, t.account_number, t.label as account_label";
  729. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t";
  730. $sql .= " WHERE t.fk_accounting_category = ".$cat_id;
  731. $sql .= " ORDER BY t.account_number";
  732. }
  733. else
  734. {
  735. $sql = "SELECT t.rowid, t.account_number, t.label as account_label";
  736. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t";
  737. $sql .= " WHERE ".$predefinedgroupwhere;
  738. $sql .= " ORDER BY t.account_number";
  739. }
  740. //echo $sql;
  741. $resql = $this->db->query($sql);
  742. if ($resql) {
  743. $i = 0;
  744. $obj = '';
  745. $num = $this->db->num_rows($resql);
  746. $data = array();
  747. if ($num) {
  748. while ($obj = $this->db->fetch_object($resql))
  749. {
  750. $name_cat = $obj->name_cat;
  751. $data[] = array (
  752. 'id' => $obj->rowid,
  753. 'account_number' => $obj->account_number,
  754. 'account_label' => $obj->account_label,
  755. );
  756. $i ++;
  757. }
  758. }
  759. return $data;
  760. } else {
  761. $this->error = "Error " . $this->db->lasterror();
  762. dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR);
  763. return -1;
  764. }
  765. }
  766. }