dolibarr 21.0.0-alpha
accounting.lib.php
Go to the documentation of this file.
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-2024 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
38function is_empty($var, $allow_false = false, $allow_ws = false)
39{
40 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))) {
41 return true;
42 }
43 return false;
44}
45
53{
54 global $langs, $conf;
55
56 $h = 0;
57 $head = array();
58
59 $head[$h][0] = DOL_URL_ROOT.'/accountancy/admin/card.php?id='.$object->id;
60 $head[$h][1] = $langs->trans("AccountAccounting");
61 $head[$h][2] = 'card';
62 $h++;
63
64 // Show more tabs from modules
65 // Entries must be declared in modules descriptor with line
66 // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
67 // $this->tabs = array('entity:-tabname); to remove a tab
68 complete_head_from_modules($conf, $langs, $object, $head, $h, 'accounting_account');
69
70 complete_head_from_modules($conf, $langs, $object, $head, $h, 'accounting_account', 'remove');
71
72 return $head;
73}
74
81function clean_account($account)
82{
83 $account = rtrim($account, "0");
84
85 return $account;
86}
87
94function length_accountg($account)
95{
96 global $conf;
97
98 if ($account < 0 || is_empty($account)) {
99 return '';
100 }
101
102 if (getDolGlobalString('ACCOUNTING_MANAGE_ZERO')) {
103 return $account;
104 }
105
106 $g = getDolGlobalInt('ACCOUNTING_LENGTH_GACCOUNT');
107 if (!is_empty($g)) {
108 // Clean parameters
109 $i = strlen($account);
110
111 if ($i >= 1) {
112 while ($i < $g) {
113 $account .= '0';
114
115 $i++;
116 }
117
118 return $account;
119 } else {
120 return $account;
121 }
122 } else {
123 return $account;
124 }
125}
126
133function length_accounta($accounta)
134{
135 global $conf;
136
137 if ($accounta < 0 || is_empty($accounta)) {
138 return '';
139 }
140
141 if (getDolGlobalString('ACCOUNTING_MANAGE_ZERO')) {
142 return $accounta;
143 }
144
145 $a = getDolGlobalInt('ACCOUNTING_LENGTH_AACCOUNT');
146 if (!is_empty($a)) {
147 // Clean parameters
148 $i = strlen($accounta);
149
150 if ($i >= 1) {
151 while ($i < $a) {
152 $accounta .= '0';
153
154 $i++;
155 }
156
157 return $accounta;
158 } else {
159 return $accounta;
160 }
161 } else {
162 return $accounta;
163 }
164}
165
166
167
184function journalHead($nom, $variant, $period, $periodlink, $description, $builddate, $exportlink = '', $moreparam = array(), $calcmode = '', $varlink = '', $moreoptions = array())
185{
186 global $langs;
187
188 print "\n\n<!-- start banner journal -->\n";
189
190 if (!is_empty($varlink)) {
191 $varlink = '?'.$varlink;
192 }
193
194 $head = array();
195 $h = 0;
196 $head[$h][0] = $_SERVER["PHP_SELF"].$varlink;
197 $head[$h][1] = $langs->trans("Journalization");
198 $head[$h][2] = 'journal';
199
200 print '<form method="POST" action="'.$_SERVER["PHP_SELF"].$varlink.'">';
201 print '<input type="hidden" name="token" value="'.newToken().'">';
202
203 print dol_get_fiche_head($head, 'journal');
204
205 foreach ($moreparam as $key => $value) {
206 print '<input type="hidden" name="'.$key.'" value="'.$value.'">';
207 }
208 print '<table class="border centpercent tableforfield">';
209
210 // Ligne de titre
211 print '<tr>';
212 print '<td class="titlefieldcreate">'.$langs->trans("Name").'</td>';
213 print '<td colspan="3">';
214 print $nom;
215 print '</td>';
216 print '</tr>';
217
218 // Calculation mode
219 if ($calcmode) {
220 print '<tr>';
221 print '<td>'.$langs->trans("CalculationMode").'</td>';
222 if (!$variant) {
223 print '<td colspan="3">';
224 } else {
225 print '<td>';
226 }
227 print $calcmode;
228 if ($variant) {
229 print '</td><td colspan="2">'.$variant;
230 }
231 print '</td>';
232 print '</tr>';
233 }
234
235 // Ligne de la periode d'analyse du rapport
236 print '<tr>';
237 print '<td>'.$langs->trans("ReportPeriod").'</td>';
238 if (!$periodlink) {
239 print '<td colspan="3">';
240 } else {
241 print '<td>';
242 }
243 if ($period) {
244 print $period;
245 }
246 if ($periodlink) {
247 print '</td><td colspan="2">'.$periodlink;
248 }
249 print '</td>';
250 print '</tr>';
251
252 // Ligne de description
253 print '<tr>';
254 print '<td>'.$langs->trans("ReportDescription").'</td>';
255 print '<td colspan="3">'.$description.'</td>';
256 print '</tr>';
257
258
259 // more options
260 foreach ($moreoptions as $key => $value) {
261 print '<tr>';
262 print '<td>'.$langs->trans($key).'</td>';
263 print '<td colspan="3">'.$value.'</td>';
264 print '</tr>';
265 }
266
267 print '</table>';
268
269 print dol_get_fiche_end();
270
271 print '<div class="center"><input type="submit" class="button" name="submit" value="'.$langs->trans("Refresh").'"></div>';
272
273 print '</form>';
274
275 print "\n<!-- end banner journal -->\n\n";
276}
277
284{
285 global $db, $conf;
286
287 $date_start = '';
288 $date_end = '';
289 $pastmonth = 0;
290 $pastmonthyear = 0;
291
292 // Period by default on transfer (0: previous month | 1: current month | 2: fiscal year)
293 $periodbydefaultontransfer = getDolGlobalInt('ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER', 0);
294 if ($periodbydefaultontransfer == 2) { // fiscal year
295 $sql = "SELECT date_start, date_end FROM ".MAIN_DB_PREFIX."accounting_fiscalyear";
296 $sql .= " WHERE date_start < '".$db->idate(dol_now())."' AND date_end > '".$db->idate(dol_now())."'";
297 $sql .= $db->plimit(1);
298 $res = $db->query($sql);
299 if ($res->num_rows > 0) {
300 $obj = $db->fetch_object($res);
301
302 $date_start = $db->jdate($obj->date_start);
303 $date_end = $db->jdate($obj->date_end);
304 } else {
305 $month_start = getDolGlobalInt('SOCIETE_FISCAL_MONTH_START', 1);
306 $year_start = dol_print_date(dol_now(), '%Y');
307 if ($month_start > dol_print_date(dol_now(), '%m')) {
308 $year_start = $year_start - 1;
309 }
310 $year_end = $year_start + 1;
311 $month_end = $month_start - 1;
312 if ($month_end < 1) {
313 $month_end = 12;
314 $year_end--;
315 }
316 $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
317 $date_end = dol_get_last_day($year_end, $month_end);
318 }
319 } elseif ($periodbydefaultontransfer == 1) { // current month
320 $year_current = (int) dol_print_date(dol_now('gmt'), "%Y", 'gmt');
321 $pastmonth = (int) dol_print_date(dol_now('gmt'), '%m', 'gmt');
322 $pastmonthyear = $year_current;
323 if ($pastmonth == 0) {
324 $pastmonth = 12;
325 $pastmonthyear--;
326 }
327 } else { // previous month
328 $year_current = (int) dol_print_date(dol_now('gmt'), "%Y", 'gmt');
329 $pastmonth = (int) dol_print_date(dol_now('gmt'), '%m', 'gmt') - 1;
330 $pastmonthyear = $year_current;
331 if ($pastmonth == 0) {
332 $pastmonth = 12;
333 $pastmonthyear--;
334 }
335 }
336
337 return array(
338 'date_start' => $date_start,
339 'date_end' => $date_end,
340 'pastmonthyear' => $pastmonthyear,
341 'pastmonth' => $pastmonth
342 );
343}
344
353function getCurrentPeriodOfFiscalYear($db, $conf, $from_time = null)
354{
355 $now = dol_now();
356 $now_arr = dol_getdate($now);
357 if ($from_time === null) {
358 $from_time = $now;
359 }
360 $from_db_time = $db->idate($from_time);
361
362 $sql = "SELECT date_start, date_end FROM ".$db->prefix()."accounting_fiscalyear";
363 $sql .= " WHERE date_start <= '".$db->escape($from_db_time)."' AND date_end >= '".$db->escape($from_db_time)."'";
364 $sql .= $db->order('date_start', 'DESC');
365 $sql .= $db->plimit(1);
366 $res = $db->query($sql);
367 if ($db->num_rows($res) > 0) {
368 $obj = $db->fetch_object($res);
369
370 $date_start = $db->jdate($obj->date_start);
371 $date_end = $db->jdate($obj->date_end);
372 } else {
373 $month_start = 1;
374 $conf_fiscal_month_start = (int) $conf->global->SOCIETE_FISCAL_MONTH_START;
375 if ($conf_fiscal_month_start >= 1 && $conf_fiscal_month_start <= 12) {
376 $month_start = $conf_fiscal_month_start;
377 }
378 $year_start = $now_arr['year'];
379 if ($conf_fiscal_month_start > $now_arr['mon']) {
380 $year_start = $year_start - 1;
381 }
382 $year_end = $year_start + 1;
383 $month_end = $month_start - 1;
384 if ($month_end < 1) {
385 $month_end = 12;
386 $year_end--;
387 }
388 $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
389 $date_end = dol_get_last_day($year_end, $month_end);
390 }
391
392 return array(
393 'date_start' => $date_start,
394 'date_end' => $date_end,
395 );
396}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
getCurrentPeriodOfFiscalYear($db, $conf, $from_time=null)
Get current period of fiscal year.
accounting_prepare_head(AccountingAccount $object)
Prepare array with list of tabs.
journalHead($nom, $variant, $period, $periodlink, $description, $builddate, $exportlink='', $moreparam=array(), $calcmode='', $varlink='', $moreoptions=array())
Show header of a page used to transfer/dispatch data in accounting.
is_empty($var, $allow_false=false, $allow_ws=false)
Check if a value is empty with some options.
getDefaultDatesForTransfer()
Return Default dates for transfer based on periodicity option in accountancy setup.
clean_account($account)
Return accounting account without zero on the right.
length_accounta($accounta)
Return Auxiliary accounting account of thirdparties with defined length.
Class to manage accounting accounts.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:613
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add', $filterorigmodule='')
Complete or removed entries into a head array (used to build tabs).
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.