accounting.lib.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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) 2014 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2019 Eric Seigne <eric.seigne@cap-rel.fr>
  6. * Copyright (C) 2021 Frédéric France <frederic.france@netlogic.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/core/lib/accounting.lib.php
  23. * \ingroup Accountancy (Double entries)
  24. * \brief Library of accountancy functions
  25. */
  26. /**
  27. * Check if a value is empty with some options
  28. *
  29. * @author Michael - https://www.php.net/manual/fr/function.empty.php#90767
  30. * @param mixed $var Value to test
  31. * @param int|null $allow_false Setting this to true will make the function consider a boolean value of false as NOT empty. This parameter is false by default.
  32. * @param int|null $allow_ws Setting this to true will make the function consider a string with nothing but white space as NOT empty. This parameter is false by default.
  33. * @return boolean True of False
  34. */
  35. function is_empty($var, $allow_false = false, $allow_ws = false)
  36. {
  37. if (!isset($var) || is_null($var) || ($allow_ws == false && trim($var) == "" && !is_bool($var)) || ($allow_false === false && is_bool($var) && $var === false) || (is_array($var) && empty($var))) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. /**
  43. * Prepare array with list of tabs
  44. *
  45. * @param AccountingAccount $object Accounting account
  46. * @return array Array of tabs to show
  47. */
  48. function accounting_prepare_head(AccountingAccount $object)
  49. {
  50. global $langs, $conf;
  51. $h = 0;
  52. $head = array();
  53. $head[$h][0] = DOL_URL_ROOT.'/accountancy/admin/card.php?id='.$object->id;
  54. $head[$h][1] = $langs->trans("AccountAccounting");
  55. $head[$h][2] = 'card';
  56. $h++;
  57. // Show more tabs from modules
  58. // Entries must be declared in modules descriptor with line
  59. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  60. // $this->tabs = array('entity:-tabname); to remove a tab
  61. complete_head_from_modules($conf, $langs, $object, $head, $h, 'accounting_account');
  62. complete_head_from_modules($conf, $langs, $object, $head, $h, 'accounting_account', 'remove');
  63. return $head;
  64. }
  65. /**
  66. * Return accounting account without zero on the right
  67. *
  68. * @param string $account Accounting account
  69. * @return string String without zero on the right
  70. */
  71. function clean_account($account)
  72. {
  73. $account = rtrim($account, "0");
  74. return $account;
  75. }
  76. /**
  77. * Return General accounting account with defined length (used for product and miscellaneous)
  78. *
  79. * @param string $account General accounting account
  80. * @return string String with defined length
  81. */
  82. function length_accountg($account)
  83. {
  84. global $conf;
  85. if ($account < 0 || is_empty($account)) {
  86. return '';
  87. }
  88. if (!empty($conf->global->ACCOUNTING_MANAGE_ZERO)) {
  89. return $account;
  90. }
  91. $g = getDolGlobalInt('ACCOUNTING_LENGTH_GACCOUNT');
  92. if (!is_empty($g)) {
  93. // Clean parameters
  94. $i = strlen($account);
  95. if ($i >= 1) {
  96. while ($i < $g) {
  97. $account .= '0';
  98. $i++;
  99. }
  100. return $account;
  101. } else {
  102. return $account;
  103. }
  104. } else {
  105. return $account;
  106. }
  107. }
  108. /**
  109. * Return Auxiliary accounting account of thirdparties with defined length
  110. *
  111. * @param string $accounta Auxiliary accounting account
  112. * @return string String with defined length
  113. */
  114. function length_accounta($accounta)
  115. {
  116. global $conf;
  117. if ($accounta < 0 || is_empty($accounta)) {
  118. return '';
  119. }
  120. if (!empty($conf->global->ACCOUNTING_MANAGE_ZERO)) {
  121. return $accounta;
  122. }
  123. $a = getDolGlobalInt('ACCOUNTING_LENGTH_AACCOUNT');
  124. if (!is_empty($a)) {
  125. // Clean parameters
  126. $i = strlen($accounta);
  127. if ($i >= 1) {
  128. while ($i < $a) {
  129. $accounta .= '0';
  130. $i++;
  131. }
  132. return $accounta;
  133. } else {
  134. return $accounta;
  135. }
  136. } else {
  137. return $accounta;
  138. }
  139. }
  140. /**
  141. * Show header of a page used to transfer/dispatch data in accounting
  142. *
  143. * @param string $nom Name of report
  144. * @param string $variante Link for alternate report
  145. * @param string $period Period of report
  146. * @param string $periodlink Link to switch period
  147. * @param string $description Description
  148. * @param integer $builddate Date of generation
  149. * @param string $exportlink Link for export or ''
  150. * @param array $moreparam Array with list of params to add into form
  151. * @param string $calcmode Calculation mode
  152. * @param string $varlink Add a variable into the address of the page
  153. * @return void
  154. */
  155. function journalHead($nom, $variante, $period, $periodlink, $description, $builddate, $exportlink = '', $moreparam = array(), $calcmode = '', $varlink = '')
  156. {
  157. global $langs;
  158. print "\n\n<!-- start banner journal -->\n";
  159. if (!is_empty($varlink)) {
  160. $varlink = '?'.$varlink;
  161. }
  162. $head = array();
  163. $h = 0;
  164. $head[$h][0] = $_SERVER["PHP_SELF"].$varlink;
  165. $head[$h][1] = $langs->trans("Journalization");
  166. $head[$h][2] = 'journal';
  167. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].$varlink.'">';
  168. print '<input type="hidden" name="token" value="'.newToken().'">';
  169. print dol_get_fiche_head($head, 'journal');
  170. foreach ($moreparam as $key => $value) {
  171. print '<input type="hidden" name="'.$key.'" value="'.$value.'">';
  172. }
  173. print '<table class="border centpercent tableforfield">';
  174. // Ligne de titre
  175. print '<tr>';
  176. print '<td class="titlefieldcreate">'.$langs->trans("Name").'</td>';
  177. print '<td colspan="3">';
  178. print $nom;
  179. print '</td>';
  180. print '</tr>';
  181. // Calculation mode
  182. if ($calcmode) {
  183. print '<tr>';
  184. print '<td>'.$langs->trans("CalculationMode").'</td>';
  185. if (!$variante) {
  186. print '<td colspan="3">';
  187. } else {
  188. print '<td>';
  189. }
  190. print $calcmode;
  191. if ($variante) {
  192. print '</td><td colspan="2">'.$variante;
  193. }
  194. print '</td>';
  195. print '</tr>';
  196. }
  197. // Ligne de la periode d'analyse du rapport
  198. print '<tr>';
  199. print '<td>'.$langs->trans("ReportPeriod").'</td>';
  200. if (!$periodlink) {
  201. print '<td colspan="3">';
  202. } else {
  203. print '<td>';
  204. }
  205. if ($period) {
  206. print $period;
  207. }
  208. if ($periodlink) {
  209. print '</td><td colspan="2">'.$periodlink;
  210. }
  211. print '</td>';
  212. print '</tr>';
  213. // Ligne de description
  214. print '<tr>';
  215. print '<td>'.$langs->trans("ReportDescription").'</td>';
  216. print '<td colspan="3">'.$description.'</td>';
  217. print '</tr>';
  218. print '</table>';
  219. print dol_get_fiche_end();
  220. print '<div class="center"><input type="submit" class="button" name="submit" value="'.$langs->trans("Refresh").'"></div>';
  221. print '</form>';
  222. print "\n<!-- end banner journal -->\n\n";
  223. }
  224. /**
  225. * Return Default dates for transfer based on periodicity option in accountancy setup
  226. *
  227. * @return array Dates of periodicity by default
  228. */
  229. function getDefaultDatesForTransfer()
  230. {
  231. global $db, $conf;
  232. $date_start = '';
  233. $date_end = '';
  234. $pastmonth = 0;
  235. $pastmonthyear = 0;
  236. // Period by default on transfer (0: previous month | 1: current month | 2: fiscal year)
  237. $periodbydefaultontransfer = (empty($conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER) ? 0 : $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER);
  238. if ($periodbydefaultontransfer == 2) { // fiscal year
  239. $sql = "SELECT date_start, date_end FROM ".MAIN_DB_PREFIX."accounting_fiscalyear";
  240. $sql .= " WHERE date_start < '".$db->idate(dol_now())."' AND date_end > '".$db->idate(dol_now())."'";
  241. $sql .= $db->plimit(1);
  242. $res = $db->query($sql);
  243. if ($res->num_rows > 0) {
  244. $obj = $db->fetch_object($res);
  245. $date_start = $db->jdate($obj->date_start);
  246. $date_end = $db->jdate($obj->date_end);
  247. } else {
  248. $month_start = getDolGlobalInt('SOCIETE_FISCAL_MONTH_START', 1);
  249. $year_start = dol_print_date(dol_now(), '%Y');
  250. if ($month_start > dol_print_date(dol_now(), '%m')) {
  251. $year_start = $year_start - 1;
  252. }
  253. $year_end = $year_start + 1;
  254. $month_end = $month_start - 1;
  255. if ($month_end < 1) {
  256. $month_end = 12;
  257. $year_end--;
  258. }
  259. $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
  260. $date_end = dol_get_last_day($year_end, $month_end);
  261. }
  262. } elseif ($periodbydefaultontransfer == 1) { // current month
  263. $year_current = (int) dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  264. $pastmonth = (int) dol_print_date(dol_now('gmt'), '%m', 'gmt');
  265. $pastmonthyear = $year_current;
  266. if ($pastmonth == 0) {
  267. $pastmonth = 12;
  268. $pastmonthyear--;
  269. }
  270. } else { // previous month
  271. $year_current = (int) dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  272. $pastmonth = (int) dol_print_date(dol_now('gmt'), '%m', 'gmt') - 1;
  273. $pastmonthyear = $year_current;
  274. if ($pastmonth == 0) {
  275. $pastmonth = 12;
  276. $pastmonthyear--;
  277. }
  278. }
  279. return array(
  280. 'date_start' => $date_start,
  281. 'date_end' => $date_end,
  282. 'pastmonthyear' => $pastmonthyear,
  283. 'pastmonth' => $pastmonth
  284. );
  285. }