accounting.lib.php 8.8 KB

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