accountingaccount.class.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2021 Alexandre Spangaro <aspangaro@open-dsi.fr>
  4. * Copyright (C) 2013-2021 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2015 Ari Elbaz (elarifr) <github@accedinfo.com>
  7. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/accountancy/class/accountingaccount.class.php
  24. * \ingroup Accountancy (Double entries)
  25. * \brief File of class to manage accounting accounts
  26. */
  27. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  30. /**
  31. * Class to manage accounting accounts
  32. */
  33. class AccountingAccount extends CommonObject
  34. {
  35. /**
  36. * @var string Name of element
  37. */
  38. public $element = 'accounting_account';
  39. /**
  40. * @var string Name of table without prefix where object is stored
  41. */
  42. public $table_element = 'accounting_account';
  43. /**
  44. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  45. */
  46. public $picto = 'billr';
  47. /**
  48. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  49. * @var int
  50. */
  51. public $ismultientitymanaged = 1;
  52. /**
  53. * 0=Default, 1=View may be restricted to sales representative only if no permission to see all or to company of external user if external user
  54. * @var integer
  55. */
  56. public $restrictiononfksoc = 1;
  57. /**
  58. * @var DoliDB Database handler.
  59. */
  60. public $db;
  61. /**
  62. * @var int ID
  63. */
  64. public $id;
  65. /**
  66. * @var int ID
  67. */
  68. public $rowid;
  69. /**
  70. * Date creation record (datec)
  71. *
  72. * @var integer
  73. */
  74. public $datec;
  75. /**
  76. * @var string pcg version
  77. */
  78. public $fk_pcg_version;
  79. /**
  80. * @var string pcg type
  81. */
  82. public $pcg_type;
  83. /**
  84. * @var string account number
  85. */
  86. public $account_number;
  87. /**
  88. * @var int ID parent account
  89. */
  90. public $account_parent;
  91. /**
  92. * @var int ID category account
  93. */
  94. public $account_category;
  95. /**
  96. * @var int Label category account
  97. */
  98. public $account_category_label;
  99. /**
  100. * @var int Status
  101. */
  102. public $status;
  103. /**
  104. * @var string Label of account
  105. */
  106. public $label;
  107. /**
  108. * @var string Label short of account
  109. */
  110. public $labelshort;
  111. /**
  112. * @var int ID
  113. */
  114. public $fk_user_author;
  115. /**
  116. * @var int ID
  117. */
  118. public $fk_user_modif;
  119. /**
  120. * @var int active (duplicate with status)
  121. */
  122. public $active;
  123. /**
  124. * @var int reconcilable
  125. */
  126. public $reconcilable;
  127. /**
  128. * @var array cache array
  129. */
  130. private $accountingaccount_codetotid_cache = array();
  131. const STATUS_ENABLED = 1;
  132. const STATUS_DISABLED = 0;
  133. /**
  134. * Constructor
  135. *
  136. * @param DoliDB $db Database handle
  137. */
  138. public function __construct($db)
  139. {
  140. global $conf;
  141. $this->db = $db;
  142. $this->next_prev_filter = "fk_pcg_version IN (SELECT pcg_version FROM ".MAIN_DB_PREFIX."accounting_system WHERE rowid = ".((int) $conf->global->CHARTOFACCOUNTS).")"; // Used to add a filter in Form::showrefnav method
  143. }
  144. /**
  145. * Load record in memory
  146. *
  147. * @param int $rowid Id
  148. * @param string $account_number Account number
  149. * @param int|boolean $limittocurrentchart 1 or true=Load record only if it is into current active chart of account
  150. * @param string $limittoachartaccount 'ABC'=Load record only if it is into chart account with code 'ABC' (better and faster than previous parameter if you have chart of account code).
  151. * @return int <0 if KO, 0 if not found, Id of record if OK and found
  152. */
  153. public function fetch($rowid = null, $account_number = null, $limittocurrentchart = 0, $limittoachartaccount = '')
  154. {
  155. global $conf;
  156. if ($rowid || $account_number) {
  157. $sql = "SELECT a.rowid as rowid, a.datec, a.tms, a.fk_pcg_version, a.pcg_type, a.account_number, a.account_parent, a.label, a.labelshort, a.fk_accounting_category, a.fk_user_author, a.fk_user_modif, a.active, a.reconcilable";
  158. $sql .= ", ca.label as category_label";
  159. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_account as a";
  160. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_accounting_category as ca ON a.fk_accounting_category = ca.rowid";
  161. $sql .= " WHERE";
  162. if ($rowid) {
  163. $sql .= " a.rowid = ".(int) $rowid;
  164. } elseif ($account_number) {
  165. $sql .= " a.account_number = '".$this->db->escape($account_number)."'";
  166. $sql .= " AND a.entity = ".$conf->entity;
  167. }
  168. if (!empty($limittocurrentchart)) {
  169. $sql .= ' AND a.fk_pcg_version IN (SELECT pcg_version FROM '.MAIN_DB_PREFIX.'accounting_system WHERE rowid = '.((int) $conf->global->CHARTOFACCOUNTS).')';
  170. }
  171. if (!empty($limittoachartaccount)) {
  172. $sql .= " AND a.fk_pcg_version = '".$this->db->escape($limittoachartaccount)."'";
  173. }
  174. dol_syslog(get_class($this)."::fetch rowid=".$rowid." account_number=".$account_number, LOG_DEBUG);
  175. $result = $this->db->query($sql);
  176. if ($result) {
  177. $obj = $this->db->fetch_object($result);
  178. if ($obj) {
  179. $this->id = $obj->rowid;
  180. $this->rowid = $obj->rowid;
  181. $this->ref = $obj->account_number;
  182. $this->datec = $obj->datec;
  183. $this->tms = $obj->tms;
  184. $this->fk_pcg_version = $obj->fk_pcg_version;
  185. $this->pcg_type = $obj->pcg_type;
  186. $this->account_number = $obj->account_number;
  187. $this->account_parent = $obj->account_parent;
  188. $this->label = $obj->label;
  189. $this->labelshort = $obj->labelshort;
  190. $this->account_category = $obj->fk_accounting_category;
  191. $this->account_category_label = $obj->category_label;
  192. $this->fk_user_author = $obj->fk_user_author;
  193. $this->fk_user_modif = $obj->fk_user_modif;
  194. $this->active = $obj->active;
  195. $this->status = $obj->active;
  196. $this->reconcilable = $obj->reconcilable;
  197. return $this->id;
  198. } else {
  199. return 0;
  200. }
  201. } else {
  202. $this->error = "Error ".$this->db->lasterror();
  203. $this->errors[] = "Error ".$this->db->lasterror();
  204. }
  205. }
  206. return -1;
  207. }
  208. /**
  209. * Insert new accounting account in chart of accounts
  210. *
  211. * @param User $user User making action
  212. * @param int $notrigger Disable triggers
  213. * @return int <0 if KO, >0 if OK
  214. */
  215. public function create($user, $notrigger = 0)
  216. {
  217. global $conf;
  218. $error = 0;
  219. $now = dol_now();
  220. // Clean parameters
  221. if (isset($this->fk_pcg_version)) {
  222. $this->fk_pcg_version = trim($this->fk_pcg_version);
  223. }
  224. if (isset($this->pcg_type)) {
  225. $this->pcg_type = trim($this->pcg_type);
  226. }
  227. if (isset($this->account_number)) {
  228. $this->account_number = trim($this->account_number);
  229. }
  230. if (isset($this->label)) {
  231. $this->label = trim($this->label);
  232. }
  233. if (isset($this->labelshort)) {
  234. $this->labelshort = trim($this->labelshort);
  235. }
  236. if (empty($this->pcg_type) || $this->pcg_type == '-1') {
  237. $this->pcg_type = 'XXXXXX';
  238. }
  239. // Check parameters
  240. // Put here code to add control on parameters values
  241. // Insert request
  242. $sql = "INSERT INTO " . MAIN_DB_PREFIX . "accounting_account(";
  243. $sql .= "datec";
  244. $sql .= ", entity";
  245. $sql .= ", fk_pcg_version";
  246. $sql .= ", pcg_type";
  247. $sql .= ", account_number";
  248. $sql .= ", account_parent";
  249. $sql .= ", label";
  250. $sql .= ", labelshort";
  251. $sql .= ", fk_accounting_category";
  252. $sql .= ", fk_user_author";
  253. $sql .= ", active";
  254. $sql .= ", reconcilable";
  255. $sql .= ") VALUES (";
  256. $sql .= " '".$this->db->idate($now)."'";
  257. $sql .= ", ".((int) $conf->entity);
  258. $sql .= ", ".(empty($this->fk_pcg_version) ? 'NULL' : "'".$this->db->escape($this->fk_pcg_version)."'");
  259. $sql .= ", ".(empty($this->pcg_type) ? 'NULL' : "'".$this->db->escape($this->pcg_type)."'");
  260. $sql .= ", ".(empty($this->account_number) ? 'NULL' : "'".$this->db->escape($this->account_number)."'");
  261. $sql .= ", ".(empty($this->account_parent) ? 0 : (int) $this->account_parent);
  262. $sql .= ", ".(empty($this->label) ? "''" : "'".$this->db->escape($this->label)."'");
  263. $sql .= ", ".(empty($this->labelshort) ? "''" : "'".$this->db->escape($this->labelshort)."'");
  264. $sql .= ", ".(empty($this->account_category) ? 0 : (int) $this->account_category);
  265. $sql .= ", ".((int) $user->id);
  266. $sql .= ", ".(int) $this->active;
  267. $sql .= ", ".(int) $this->reconcilable;
  268. $sql .= ")";
  269. $this->db->begin();
  270. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  271. $resql = $this->db->query($sql);
  272. if (!$resql) {
  273. $error++;
  274. $this->errors[] = "Error " . $this->db->lasterror();
  275. }
  276. if (!$error) {
  277. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "accounting_account");
  278. // Uncomment this and change MYOBJECT to your own tag if you
  279. // want this action to call a trigger.
  280. //if (! $error && ! $notrigger) {
  281. // // Call triggers
  282. // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
  283. // if ($result < 0) $error++;
  284. // // End call triggers
  285. //}
  286. }
  287. // Commit or rollback
  288. if ($error) {
  289. foreach ($this->errors as $errmsg) {
  290. dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
  291. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  292. }
  293. $this->db->rollback();
  294. return -1 * $error;
  295. } else {
  296. $this->db->commit();
  297. return $this->id;
  298. }
  299. }
  300. /**
  301. * Update record
  302. *
  303. * @param User $user User making update
  304. * @return int <0 if KO (-2 = duplicate), >0 if OK
  305. */
  306. public function update($user)
  307. {
  308. // Check parameters
  309. if (empty($this->pcg_type) || $this->pcg_type == '-1') {
  310. $this->pcg_type = 'XXXXXX';
  311. }
  312. $this->db->begin();
  313. $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account ";
  314. $sql .= " SET fk_pcg_version = " . ($this->fk_pcg_version ? "'" . $this->db->escape($this->fk_pcg_version) . "'" : "null");
  315. $sql .= " , pcg_type = " . ($this->pcg_type ? "'" . $this->db->escape($this->pcg_type) . "'" : "null");
  316. $sql .= " , account_number = '" . $this->db->escape($this->account_number) . "'";
  317. $sql .= " , account_parent = " . (int) $this->account_parent;
  318. $sql .= " , label = " . ($this->label ? "'" . $this->db->escape($this->label) . "'" : "''");
  319. $sql .= " , labelshort = " . ($this->labelshort ? "'" . $this->db->escape($this->labelshort) . "'" : "''");
  320. $sql .= " , fk_accounting_category = " . (empty($this->account_category) ? 0 : (int) $this->account_category);
  321. $sql .= " , fk_user_modif = " . ((int) $user->id);
  322. $sql .= " , active = " . (int) $this->active;
  323. $sql .= " , reconcilable = " . (int) $this->reconcilable;
  324. $sql .= " WHERE rowid = " . ((int) $this->id);
  325. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  326. $result = $this->db->query($sql);
  327. if ($result) {
  328. $this->db->commit();
  329. return 1;
  330. } else {
  331. if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  332. $this->error = $this->db->lasterror();
  333. $this->db->rollback();
  334. return -2;
  335. }
  336. $this->error = $this->db->lasterror();
  337. $this->db->rollback();
  338. return -1;
  339. }
  340. }
  341. /**
  342. * Check usage of accounting code
  343. *
  344. * @return int <0 if KO, >0 if OK
  345. */
  346. public function checkUsage()
  347. {
  348. global $langs;
  349. $sql = "(SELECT fk_code_ventilation FROM ".MAIN_DB_PREFIX."facturedet";
  350. $sql .= " WHERE fk_code_ventilation=".((int) $this->id).")";
  351. $sql .= "UNION";
  352. $sql .= " (SELECT fk_code_ventilation FROM ".MAIN_DB_PREFIX."facture_fourn_det";
  353. $sql .= " WHERE fk_code_ventilation=".((int) $this->id).")";
  354. dol_syslog(get_class($this)."::checkUsage", LOG_DEBUG);
  355. $resql = $this->db->query($sql);
  356. if ($resql) {
  357. $num = $this->db->num_rows($resql);
  358. if ($num > 0) {
  359. $this->error = $langs->trans('ErrorAccountancyCodeIsAlreadyUse');
  360. return 0;
  361. } else {
  362. return 1;
  363. }
  364. } else {
  365. $this->error = $this->db->lasterror();
  366. return -1;
  367. }
  368. }
  369. /**
  370. * Delete object in database
  371. *
  372. * @param User $user User that deletes
  373. * @param int $notrigger 0=triggers after, 1=disable triggers
  374. * @return int <0 if KO, >0 if OK
  375. */
  376. public function delete($user, $notrigger = 0)
  377. {
  378. $error = 0;
  379. $result = $this->checkUsage();
  380. if ($result > 0) {
  381. $this->db->begin();
  382. if (!$error) {
  383. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "accounting_account";
  384. $sql .= " WHERE rowid=" . ((int) $this->id);
  385. dol_syslog(get_class($this) . "::delete sql=" . $sql);
  386. $resql = $this->db->query($sql);
  387. if (!$resql) {
  388. $error++;
  389. $this->errors[] = "Error " . $this->db->lasterror();
  390. }
  391. }
  392. // Commit or rollback
  393. if ($error) {
  394. foreach ($this->errors as $errmsg) {
  395. dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
  396. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  397. }
  398. $this->db->rollback();
  399. return -1 * $error;
  400. } else {
  401. $this->db->commit();
  402. return 1;
  403. }
  404. } else {
  405. return -1;
  406. }
  407. }
  408. /**
  409. * Return clicable name (with picto eventually)
  410. *
  411. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  412. * @param int $withlabel 0=No label, 1=Include label of account
  413. * @param int $nourl 1=Disable url
  414. * @param string $moretitle Add more text to title tooltip
  415. * @param int $notooltip 1=Disable tooltip
  416. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  417. * @param int $withcompletelabel 0=Short label (field short label), 1=Complete label (field label)
  418. * @param string $option 'ledger', 'journals', 'accountcard'
  419. * @return string String with URL
  420. */
  421. public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle = '', $notooltip = 0, $save_lastsearch_value = -1, $withcompletelabel = 0, $option = '')
  422. {
  423. global $langs, $conf, $hookmanager;
  424. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  425. if (!empty($conf->dol_no_mouse_hover)) {
  426. $notooltip = 1; // Force disable tooltips
  427. }
  428. $result = '';
  429. $url = '';
  430. $labelurl = '';
  431. if (empty($option) || $option == 'ledger') {
  432. $url = DOL_URL_ROOT . '/accountancy/bookkeeping/listbyaccount.php?search_accountancy_code_start=' . urlencode($this->account_number) . '&search_accountancy_code_end=' . urlencode($this->account_number);
  433. $labelurl = $langs->trans("ShowAccountingAccountInLedger");
  434. } elseif ($option == 'journals') {
  435. $url = DOL_URL_ROOT . '/accountancy/bookkeeping/list.php?search_accountancy_code_start=' . urlencode($this->account_number) . '&search_accountancy_code_end=' . urlencode($this->account_number);
  436. $labelurl = $langs->trans("ShowAccountingAccountInJournals");
  437. } elseif ($option == 'accountcard') {
  438. $url = DOL_URL_ROOT . '/accountancy/admin/card.php?id=' . urlencode($this->id);
  439. $labelurl = $langs->trans("ShowAccountingAccount");
  440. }
  441. // Add param to save lastsearch_values or not
  442. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  443. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  444. $add_save_lastsearch_values = 1;
  445. }
  446. if ($add_save_lastsearch_values) {
  447. $url .= '&save_lastsearch_values=1';
  448. }
  449. $picto = 'accounting_account';
  450. $label = '';
  451. if (empty($this->labelshort) || $withcompletelabel == 1) {
  452. $labeltoshow = $this->label;
  453. } else {
  454. $labeltoshow = $this->labelshort;
  455. }
  456. $label = '<u>' . $labelurl . '</u>';
  457. if (!empty($this->account_number)) {
  458. $label .= '<br><b>' . $langs->trans('AccountAccounting') . ':</b> ' . length_accountg($this->account_number);
  459. }
  460. if (!empty($labeltoshow)) {
  461. $label .= '<br><b>' . $langs->trans('Label') . ':</b> ' . $labeltoshow;
  462. }
  463. if ($moretitle) {
  464. $label .= ' - ' . $moretitle;
  465. }
  466. $linkclose = '';
  467. if (empty($notooltip)) {
  468. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  469. $label = $labelurl;
  470. $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"';
  471. }
  472. $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"';
  473. $linkclose .= ' class="classfortooltip"';
  474. }
  475. $linkstart = '<a href="' . $url . '"';
  476. $linkstart .= $linkclose . '>';
  477. $linkend = '</a>';
  478. if ($nourl) {
  479. $linkstart = '';
  480. $linkclose = '';
  481. $linkend = '';
  482. }
  483. $label_link = length_accountg($this->account_number);
  484. if ($withlabel) {
  485. $label_link .= ' - ' . ($nourl ? '<span class="opacitymedium">' : '') . $labeltoshow . ($nourl ? '</span>' : '');
  486. }
  487. if ($withpicto) {
  488. $result .= ($linkstart . img_object(($notooltip ? '' : $label), $picto, ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1) . $linkend);
  489. }
  490. if ($withpicto && $withpicto != 2) {
  491. $result .= ' ';
  492. }
  493. if ($withpicto != 2) {
  494. $result .= $linkstart . $label_link . $linkend;
  495. }
  496. global $action;
  497. $hookmanager->initHooks(array($this->element . 'dao'));
  498. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  499. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  500. if ($reshook > 0) {
  501. $result = $hookmanager->resPrint;
  502. } else {
  503. $result .= $hookmanager->resPrint;
  504. }
  505. return $result;
  506. }
  507. /**
  508. * Information on record
  509. *
  510. * @param int $id ID of record
  511. * @return void
  512. */
  513. public function info($id)
  514. {
  515. $sql = 'SELECT a.rowid, a.datec, a.fk_user_author, a.fk_user_modif, a.tms';
  516. $sql .= ' FROM ' . MAIN_DB_PREFIX . 'accounting_account as a';
  517. $sql .= ' WHERE a.rowid = ' . ((int) $id);
  518. dol_syslog(get_class($this) . '::info sql=' . $sql);
  519. $resql = $this->db->query($sql);
  520. if ($resql) {
  521. if ($this->db->num_rows($resql)) {
  522. $obj = $this->db->fetch_object($resql);
  523. $this->id = $obj->rowid;
  524. $this->user_creation_id = $obj->fk_user_author;
  525. $this->user_modification_id = $obj->fk_user_modif;
  526. $this->date_creation = $this->db->jdate($obj->datec);
  527. $this->date_modification = $this->db->jdate($obj->tms);
  528. }
  529. $this->db->free($resql);
  530. } else {
  531. dol_print_error($this->db);
  532. }
  533. }
  534. /**
  535. * Deactivate an account (for status active or status reconcilable)
  536. *
  537. * @param int $id Id
  538. * @param int $mode 0=field active, 1=field reconcilable
  539. * @return int <0 if KO, >0 if OK
  540. */
  541. public function accountDeactivate($id, $mode = 0)
  542. {
  543. $result = $this->checkUsage();
  544. $fieldtouse = 'active';
  545. if ($mode == 1) {
  546. $fieldtouse = 'reconcilable';
  547. }
  548. if ($result > 0) {
  549. $this->db->begin();
  550. $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_account ";
  551. $sql .= "SET ".$fieldtouse." = '0'";
  552. $sql .= " WHERE rowid = ".((int) $id);
  553. dol_syslog(get_class($this)."::accountDeactivate ".$fieldtouse, LOG_DEBUG);
  554. $result = $this->db->query($sql);
  555. if ($result) {
  556. $this->db->commit();
  557. return 1;
  558. } else {
  559. $this->error = $this->db->lasterror();
  560. $this->db->rollback();
  561. return -1;
  562. }
  563. } else {
  564. return -1;
  565. }
  566. }
  567. /**
  568. * Account activated
  569. *
  570. * @param int $id Id
  571. * @param int $mode 0=field active, 1=field reconcilable
  572. * @return int <0 if KO, >0 if OK
  573. */
  574. public function accountActivate($id, $mode = 0)
  575. {
  576. // phpcs:enable
  577. $this->db->begin();
  578. $fieldtouse = 'active';
  579. if ($mode == 1) {
  580. $fieldtouse = 'reconcilable';
  581. }
  582. $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_account";
  583. $sql .= " SET ".$fieldtouse." = '1'";
  584. $sql .= " WHERE rowid = ".((int) $id);
  585. dol_syslog(get_class($this)."::account_activate ".$fieldtouse, LOG_DEBUG);
  586. $result = $this->db->query($sql);
  587. if ($result) {
  588. $this->db->commit();
  589. return 1;
  590. } else {
  591. $this->error = $this->db->lasterror();
  592. $this->db->rollback();
  593. return -1;
  594. }
  595. }
  596. /**
  597. * Retourne le libelle du statut d'un user (actif, inactif)
  598. *
  599. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  600. * @return string Label of status
  601. */
  602. public function getLibStatut($mode = 0)
  603. {
  604. return $this->LibStatut($this->status, $mode);
  605. }
  606. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  607. /**
  608. * Renvoi le libelle d'un statut donne
  609. *
  610. * @param int $status Id status
  611. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  612. * @return string Label of status
  613. */
  614. public function LibStatut($status, $mode = 0)
  615. {
  616. // phpcs:enable
  617. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  618. global $langs;
  619. $langs->load("users");
  620. $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
  621. $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
  622. $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
  623. $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
  624. }
  625. $statusType = 'status4';
  626. if ($status == self::STATUS_DISABLED) {
  627. $statusType = 'status5';
  628. }
  629. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  630. }
  631. /**
  632. * Return a suggested account (from chart of accounts) to bind
  633. *
  634. * @param Societe $buyer Object buyer
  635. * @param Societe $seller Object seller
  636. * @param Product $product Product object sell or buy
  637. * @param Facture|FactureFournisseur $facture Facture
  638. * @param FactureLigne|SupplierInvoiceLine $factureDet Facture Det
  639. * @param array $accountingAccount Array of Accounting account
  640. * @param string $type Customer / Supplier
  641. * @return array|int Accounting accounts suggested or < 0 if technical error.
  642. * 'suggestedaccountingaccountbydefaultfor'=>Will be used for the label to show on tooltip for account by default on any product
  643. * 'suggestedaccountingaccountfor'=>Is the account suggested for this product
  644. */
  645. public function getAccountingCodeToBind(Societe $buyer, Societe $seller, Product $product, $facture, $factureDet, $accountingAccount = array(), $type = '')
  646. {
  647. global $conf;
  648. global $hookmanager;
  649. // Instantiate hooks for external modules
  650. $hookmanager->initHooks(array('accountancyBindingCalculation'));
  651. // Execute hook accountancyBindingCalculation
  652. $parameters = array('buyer' => $buyer, 'seller' => $seller, 'product' => $product, 'facture' => $facture, 'factureDet' => $factureDet ,'accountingAccount'=>$accountingAccount, $type);
  653. $reshook = $hookmanager->executeHooks('accountancyBindingCalculation', $parameters); // Note that $action and $object may have been modified by some hooks
  654. if (empty($reshook)) {
  655. $const_name = '';
  656. if ($type == 'customer') {
  657. $const_name = "SOLD";
  658. } elseif ($type == 'supplier') {
  659. $const_name = "BUY";
  660. }
  661. require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php';
  662. $isBuyerInEEC = isInEEC($buyer);
  663. $isSellerInEEC = isInEEC($seller);
  664. $code_l = ''; // Default value for generic product/service
  665. $code_p = ''; // Value for the product/service in parameter ($product)
  666. $code_t = ''; // Default value of product account for the thirdparty
  667. $suggestedid = '';
  668. // Level 1 (define $code_l): Search suggested default account for product/service
  669. $suggestedaccountingaccountbydefaultfor = '';
  670. if ($factureDet->product_type == 1) {
  671. if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) { // If buyer in same country than seller (if not defined, we assume it is same country)
  672. $code_l = (!empty($conf->global->{'ACCOUNTING_SERVICE_' . $const_name . '_ACCOUNT'}) ? $conf->global->{'ACCOUNTING_SERVICE_' . $const_name . '_ACCOUNT'} : '');
  673. $suggestedaccountingaccountbydefaultfor = '';
  674. } else {
  675. if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) { // European intravat sale, but with a VAT
  676. $code_l = (!empty($conf->global->{'ACCOUNTING_SERVICE_' . $const_name . '_ACCOUNT'}) ? $conf->global->{'ACCOUNTING_SERVICE_' . $const_name . '_ACCOUNT'} : '');
  677. $suggestedaccountingaccountbydefaultfor = 'eecwithvat';
  678. } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) { // European intravat sale, without VAT intra community number
  679. $code_l = (!empty($conf->global->{'ACCOUNTING_SERVICE_' . $const_name . '_ACCOUNT'}) ? $conf->global->{'ACCOUNTING_SERVICE_' . $const_name . '_ACCOUNT'} : '');
  680. $suggestedaccountingaccountbydefaultfor = 'eecwithoutvatnumber';
  681. } elseif ($isSellerInEEC && $isBuyerInEEC) { // European intravat sale
  682. $code_l = (!empty($conf->global->{'ACCOUNTING_SERVICE_' . $const_name . '_INTRA_ACCOUNT'}) ? $conf->global->{'ACCOUNTING_SERVICE_' . $const_name . '_INTRA_ACCOUNT'} : '');
  683. $suggestedaccountingaccountbydefaultfor = 'eec';
  684. } else { // Foreign sale
  685. $code_l = (!empty($conf->global->{'ACCOUNTING_SERVICE_' . $const_name . '_EXPORT_ACCOUNT'}) ? $conf->global->{'ACCOUNTING_SERVICE_' . $const_name . '_EXPORT_ACCOUNT'} : '');
  686. $suggestedaccountingaccountbydefaultfor = 'export';
  687. }
  688. }
  689. } elseif ($factureDet->product_type == 0) {
  690. if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) { // If buyer in same country than seller (if not defined, we assume it is same country)
  691. $code_l = (!empty($conf->global->{'ACCOUNTING_PRODUCT_' . $const_name . '_ACCOUNT'}) ? $conf->global->{'ACCOUNTING_PRODUCT_' . $const_name . '_ACCOUNT'} : '');
  692. $suggestedaccountingaccountbydefaultfor = '';
  693. } else {
  694. if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) { // European intravat sale, but with a VAT
  695. $code_l = (!empty($conf->global->{'ACCOUNTING_PRODUCT_' . $const_name . '_ACCOUNT'}) ? $conf->global->{'ACCOUNTING_PRODUCT_' . $const_name . '_ACCOUNT'} : '');
  696. $suggestedaccountingaccountbydefaultfor = 'eecwithvat';
  697. } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) { // European intravat sale, without VAT intra community number
  698. $code_l = (!empty($conf->global->{'ACCOUNTING_PRODUCT_' . $const_name . '_ACCOUNT'}) ? $conf->global->{'ACCOUNTING_PRODUCT_' . $const_name . '_ACCOUNT'} : '');
  699. $suggestedaccountingaccountbydefaultfor = 'eecwithoutvatnumber';
  700. } elseif ($isSellerInEEC && $isBuyerInEEC) { // European intravat sale
  701. $code_l = (!empty($conf->global->{'ACCOUNTING_PRODUCT_' . $const_name . '_INTRA_ACCOUNT'}) ? $conf->global->{'ACCOUNTING_PRODUCT_' . $const_name . '_INTRA_ACCOUNT'} : '');
  702. $suggestedaccountingaccountbydefaultfor = 'eec';
  703. } else {
  704. $code_l = (!empty($conf->global->{'ACCOUNTING_PRODUCT_' . $const_name . '_EXPORT_ACCOUNT'}) ? $conf->global->{'ACCOUNTING_PRODUCT_' . $const_name . '_EXPORT_ACCOUNT'} : '');
  705. $suggestedaccountingaccountbydefaultfor = 'export';
  706. }
  707. }
  708. }
  709. if ($code_l == -1) {
  710. $code_l = '';
  711. }
  712. // Level 2 (define $code_p): Search suggested account for product/service (similar code exists in page index.php to make automatic binding)
  713. $suggestedaccountingaccountfor = '';
  714. if ((($buyer->country_code == $seller->country_code) || empty($buyer->country_code))) {
  715. // If buyer in same country than seller (if not defined, we assume it is same country)
  716. if ($type == 'customer' && !empty($product->accountancy_code_sell)) {
  717. $code_p = $product->accountancy_code_sell;
  718. } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy)) {
  719. $code_p = $product->accountancy_code_buy;
  720. }
  721. $suggestedid = $accountingAccount['dom'];
  722. $suggestedaccountingaccountfor = 'prodserv';
  723. } else {
  724. if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
  725. // European intravat sale, but with VAT
  726. if ($type == 'customer' && !empty($product->accountancy_code_sell)) {
  727. $code_p = $product->accountancy_code_sell;
  728. } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy)) {
  729. $code_p = $product->accountancy_code_buy;
  730. }
  731. $suggestedid = $accountingAccount['dom'];
  732. $suggestedaccountingaccountfor = 'eecwithvat';
  733. } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
  734. // European intravat sale, without VAT intra community number
  735. if ($type == 'customer' && !empty($product->accountancy_code_sell)) {
  736. $code_p = $product->accountancy_code_sell;
  737. } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy)) {
  738. $code_p = $product->accountancy_code_buy;
  739. }
  740. $suggestedid = $accountingAccount['dom']; // There is a doubt for this case. Is it an error on vat or we just forgot to fill vat number ?
  741. $suggestedaccountingaccountfor = 'eecwithoutvatnumber';
  742. } elseif ($isSellerInEEC && $isBuyerInEEC && !empty($product->accountancy_code_sell_intra)) {
  743. // European intravat sale
  744. if ($type == 'customer' && !empty($product->accountancy_code_sell_intra)) {
  745. $code_p = $product->accountancy_code_sell_intra;
  746. } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy_intra)) {
  747. $code_p = $product->accountancy_code_buy_intra;
  748. }
  749. $suggestedid = $accountingAccount['intra'];
  750. $suggestedaccountingaccountfor = 'eec';
  751. } else {
  752. // Foreign sale
  753. if ($type == 'customer' && !empty($product->accountancy_code_sell_export)) {
  754. $code_p = $product->accountancy_code_sell_export;
  755. } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy_export)) {
  756. $code_p = $product->accountancy_code_buy_export;
  757. }
  758. $suggestedid = $accountingAccount['export'];
  759. $suggestedaccountingaccountfor = 'export';
  760. }
  761. }
  762. // Level 3 (define $code_t): Search suggested account for this thirdparty (similar code exists in page index.php to make automatic binding)
  763. if (!empty($conf->global->ACCOUNTANCY_USE_PRODUCT_ACCOUNT_ON_THIRDPARTY)) {
  764. if (!empty($buyer->code_compta_product)) {
  765. $code_t = $buyer->code_compta_product;
  766. $suggestedid = $accountingAccount['thirdparty'];
  767. $suggestedaccountingaccountfor = 'thirdparty';
  768. }
  769. }
  770. // Manage Deposit
  771. if (getDolGlobalString('ACCOUNTING_ACCOUNT_' . strtoupper($type) . '_DEPOSIT')) {
  772. if ($factureDet->desc == "(DEPOSIT)" || $facture->type == $facture::TYPE_DEPOSIT) {
  773. $accountdeposittoventilated = new self($this->db);
  774. if ($type == 'customer') {
  775. $result = $accountdeposittoventilated->fetch('', $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT, 1);
  776. } elseif ($type == 'supplier') {
  777. $result = $accountdeposittoventilated->fetch('', $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT, 1);
  778. }
  779. if (isset($result) && $result < 0) {
  780. return -1;
  781. }
  782. $code_l = $accountdeposittoventilated->ref;
  783. $code_p = '';
  784. $code_t = '';
  785. $suggestedid = $accountdeposittoventilated->rowid;
  786. $suggestedaccountingaccountfor = 'deposit';
  787. }
  788. // For credit note invoice, if origin invoice is a deposit invoice, force also on specific customer/supplier deposit account
  789. if (!empty($facture->fk_facture_source)) {
  790. $invoiceSource = new $facture($this->db);
  791. $invoiceSource->fetch($facture->fk_facture_source);
  792. if ($facture->type == $facture::TYPE_CREDIT_NOTE && $invoiceSource->type == $facture::TYPE_DEPOSIT) {
  793. $accountdeposittoventilated = new self($this->db);
  794. if ($type == 'customer') {
  795. $accountdeposittoventilated->fetch('', $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT, 1);
  796. } elseif ($type == 'supplier') {
  797. $accountdeposittoventilated->fetch('', $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT, 1);
  798. }
  799. $code_l = $accountdeposittoventilated->ref;
  800. $code_p = '';
  801. $code_t = '';
  802. $suggestedid = $accountdeposittoventilated->rowid;
  803. $suggestedaccountingaccountfor = 'deposit';
  804. }
  805. }
  806. }
  807. // If $suggestedid could not be guessed yet, we set it from the generic default accounting code $code_l
  808. if (empty($suggestedid) && empty($code_p) && !empty($code_l) && empty($conf->global->ACCOUNTANCY_DO_NOT_AUTOFILL_ACCOUNT_WITH_GENERIC)) {
  809. if (empty($this->accountingaccount_codetotid_cache[$code_l])) {
  810. $tmpaccount = new self($this->db);
  811. $result = $tmpaccount->fetch(0, $code_l, 1);
  812. if ($result < 0) {
  813. return -1;
  814. }
  815. if ($tmpaccount->id > 0) {
  816. $suggestedid = $tmpaccount->id;
  817. }
  818. $this->accountingaccount_codetotid_cache[$code_l] = $tmpaccount->id;
  819. } else {
  820. $suggestedid = $this->accountingaccount_codetotid_cache[$code_l];
  821. }
  822. }
  823. return array(
  824. 'suggestedaccountingaccountbydefaultfor' => $suggestedaccountingaccountbydefaultfor,
  825. 'suggestedaccountingaccountfor' => $suggestedaccountingaccountfor,
  826. 'suggestedid' => $suggestedid,
  827. 'code_l' => $code_l,
  828. 'code_p' => $code_p,
  829. 'code_t' => $code_t,
  830. );
  831. } else {
  832. if (is_array($hookmanager->resArray) && !empty($hookmanager->resArray)) {
  833. return $hookmanager->resArray;
  834. }
  835. }
  836. }
  837. }