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@free.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
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
39function is_empty($var, $allow_false = false, $allow_ws = false)
40{
41 if (is_null($var) || !isset($var) || ($allow_ws == false && trim($var) == "" && !is_bool($var)) || ($allow_false === false && $var === false) || (is_array($var) && empty($var))) {
42 return true;
43 }
44 return false;
45}
46
54{
55 global $langs, $conf;
56
57 $h = 0;
58 $head = array();
59
60 $head[$h][0] = DOL_URL_ROOT.'/accountancy/admin/card.php?id='.$object->id;
61 $head[$h][1] = $langs->trans("AccountAccounting");
62 $head[$h][2] = 'card';
63 $h++;
64
65 // Show more tabs from modules
66 // Entries must be declared in modules descriptor with line
67 // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
68 // $this->tabs = array('entity:-tabname); to remove a tab
69 complete_head_from_modules($conf, $langs, $object, $head, $h, 'accounting_account');
70
71 complete_head_from_modules($conf, $langs, $object, $head, $h, 'accounting_account', 'remove');
72
73 return $head;
74}
75
82function clean_account($account)
83{
84 $account = rtrim($account, "0");
85
86 return $account;
87}
88
95function length_accountg($account)
96{
97 global $conf;
98
99 if ($account < 0 || is_empty($account)) {
100 return '';
101 }
102
103 if (getDolGlobalString('ACCOUNTING_MANAGE_ZERO')) {
104 return $account;
105 }
106
107 $g = getDolGlobalInt('ACCOUNTING_LENGTH_GACCOUNT');
108 if (!is_empty($g)) {
109 // Clean parameters
110 $i = strlen($account);
111
112 if ($i >= 1) {
113 while ($i < $g) {
114 $account .= '0';
115
116 $i++;
117 }
118
119 return $account;
120 } else {
121 return $account;
122 }
123 } else {
124 return $account;
125 }
126}
127
134function length_accounta($accounta)
135{
136 global $conf;
137
138 if ($accounta < 0 || is_empty($accounta)) {
139 return '';
140 }
141
142 if (getDolGlobalString('ACCOUNTING_MANAGE_ZERO')) {
143 return $accounta;
144 }
145
146 $a = getDolGlobalInt('ACCOUNTING_LENGTH_AACCOUNT');
147 if (!is_empty($a)) {
148 // Clean parameters
149 $i = strlen($accounta);
150
151 if ($i >= 1) {
152 while ($i < $a) {
153 $accounta .= '0';
154
155 $i++;
156 }
157
158 return $accounta;
159 } else {
160 return $accounta;
161 }
162 } else {
163 return $accounta;
164 }
165}
166
167
168
185function journalHead($nom, $variant, $period, $periodlink, $description, $builddate, $exportlink = '', $moreparam = array(), $calcmode = '', $varlink = '', $moreoptions = array())
186{
187 global $langs;
188
189 print "\n\n<!-- start banner journal -->\n";
190
191 if (!is_empty($varlink)) {
192 $varlink = '?'.$varlink;
193 }
194
195 $head = array();
196 $h = 0;
197 $head[$h][0] = $_SERVER["PHP_SELF"].$varlink;
198 $head[$h][1] = $langs->trans("Journalization");
199 $head[$h][2] = 'journal';
200
201 print '<form method="POST" action="'.$_SERVER["PHP_SELF"].$varlink.'">';
202 print '<input type="hidden" name="token" value="'.newToken().'">';
203
204 print dol_get_fiche_head($head, 'journal');
205
206 foreach ($moreparam as $key => $value) {
207 print '<input type="hidden" name="'.$key.'" value="'.$value.'">';
208 }
209 print '<table class="border centpercent tableforfield">';
210
211 // Ligne de titre
212 print '<tr>';
213 print '<td class="titlefieldcreate">'.$langs->trans("Name").'</td>';
214 print '<td colspan="3">';
215 print $nom;
216 print '</td>';
217 print '</tr>';
218
219 // Calculation mode
220 if ($calcmode) {
221 print '<tr>';
222 print '<td>'.$langs->trans("CalculationMode").'</td>';
223 if (!$variant) {
224 print '<td colspan="3">';
225 } else {
226 print '<td>';
227 }
228 print $calcmode;
229 if ($variant) {
230 print '</td><td colspan="2">'.$variant;
231 }
232 print '</td>';
233 print '</tr>';
234 }
235
236 // Ligne de la periode d'analyse du rapport
237 print '<tr>';
238 print '<td>'.$langs->trans("ReportPeriod").'</td>';
239 if (!$periodlink) {
240 print '<td colspan="3">';
241 } else {
242 print '<td>';
243 }
244 if ($period) {
245 print $period;
246 }
247 if ($periodlink) {
248 print '</td><td colspan="2">'.$periodlink;
249 }
250 print '</td>';
251 print '</tr>';
252
253 // Ligne de description
254 print '<tr>';
255 print '<td>'.$langs->trans("ReportDescription").'</td>';
256 print '<td colspan="3">'.$description.'</td>';
257 print '</tr>';
258
259
260 // more options
261 foreach ($moreoptions as $key => $value) {
262 print '<tr>';
263 print '<td>'.$langs->trans($key).'</td>';
264 print '<td colspan="3">'.$value.'</td>';
265 print '</tr>';
266 }
267
268 print '</table>';
269
270 print dol_get_fiche_end();
271
272 print '<div class="center"><input type="submit" class="button" name="submit" value="'.$langs->trans("Refresh").'"></div>';
273
274 print '</form>';
275
276 print "\n<!-- end banner journal -->\n\n";
277}
278
285{
286 global $db, $conf;
287
288 $date_start = '';
289 $date_end = '';
290 $pastmonth = 0;
291 $pastmonthyear = 0;
292
293 // Period by default on transfer (0: previous month | 1: current month | 2: fiscal year)
294 $periodbydefaultontransfer = getDolGlobalInt('ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER', 0);
295 if ($periodbydefaultontransfer == 2) { // fiscal year
296 $sql = "SELECT date_start, date_end FROM ".MAIN_DB_PREFIX."accounting_fiscalyear";
297 $sql .= " WHERE date_start < '".$db->idate(dol_now())."' AND date_end > '".$db->idate(dol_now())."'";
298 $sql .= $db->plimit(1);
299 $res = $db->query($sql);
300 if ($res->num_rows > 0) {
301 $obj = $db->fetch_object($res);
302
303 $date_start = $db->jdate($obj->date_start);
304 $date_end = $db->jdate($obj->date_end);
305 } else {
306 $month_start = getDolGlobalInt('SOCIETE_FISCAL_MONTH_START', 1);
307 $year_start = (int) dol_print_date(dol_now(), '%Y');
308 if ($month_start > (int) dol_print_date(dol_now(), '%m')) {
309 $year_start -= 1;
310 }
311 $year_end = $year_start + 1;
312 $month_end = $month_start - 1;
313 if ($month_end < 1) {
314 $month_end = 12;
315 $year_end--;
316 }
317 $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
318 $date_end = dol_get_last_day($year_end, $month_end);
319 }
320 } elseif ($periodbydefaultontransfer == 1) { // current month
321 $year_current = (int) dol_print_date(dol_now('gmt'), "%Y", 'gmt');
322 $pastmonth = (int) dol_print_date(dol_now('gmt'), '%m', 'gmt');
323 $pastmonthyear = $year_current;
324 if ($pastmonth == 0) {
325 $pastmonth = 12;
326 $pastmonthyear--;
327 }
328 } else { // previous month
329 $year_current = (int) dol_print_date(dol_now('gmt'), "%Y", 'gmt');
330 $pastmonth = (int) dol_print_date(dol_now('gmt'), '%m', 'gmt') - 1;
331 $pastmonthyear = $year_current;
332 if ($pastmonth == 0) {
333 $pastmonth = 12;
334 $pastmonthyear--;
335 }
336 }
337
338 return array(
339 'date_start' => $date_start,
340 'date_end' => $date_end,
341 'pastmonthyear' => $pastmonthyear,
342 'pastmonth' => $pastmonth
343 );
344}
345
356function getCurrentPeriodOfFiscalYear($db, $conf, $from_time = null, $gm = 'tzserver', $withenddateonly = 1)
357{
358 $now = dol_now();
359 $now_arr = dol_getdate($now);
360 if ($from_time === null) {
361 $from_time = $now;
362 }
363
364 include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
365
366 // Take the first period found
367 $sql = "SELECT date_start, date_end FROM ".$db->prefix()."accounting_fiscalyear";
368 $sql .= " WHERE date_start <= '".$db->idate($from_time, $gm)."'";
369 if ($withenddateonly) {
370 $sql .= " AND (date_end >= '".$db->idate($from_time, $gm)."')";
371 } else {
372 $sql .= " AND (date_end >= '".$db->idate($from_time, $gm)."' OR date_end IS NULL)";
373 }
374 //$sql .= " AND statut = 0"
375 $sql .= " AND entity IN (".getEntity('accounting_fiscalyear').")";
376 $sql .= $db->order('date_start', 'DESC');
377 $sql .= $db->plimit(1);
378
379 $res = $db->query($sql);
380
381 if ($db->num_rows($res) > 0) {
382 // If found
383 $obj = $db->fetch_object($res);
384
385 $date_start = $db->jdate($obj->date_start, $gm);
386 $date_end = dol_get_last_hour($db->jdate($obj->date_end, $gm), $gm);
387 } else {
388 // If not found, we generate a period
389 $month_start = 1;
390 $conf_fiscal_month_start = getDolGlobalInt('SOCIETE_FISCAL_MONTH_START');
391 if ($conf_fiscal_month_start >= 1 && $conf_fiscal_month_start <= 12) {
392 $month_start = $conf_fiscal_month_start;
393 }
394 $year_start = $now_arr['year'];
395 if ($conf_fiscal_month_start > $now_arr['mon']) {
396 $year_start -= 1;
397 }
398 $year_end = $year_start + 1;
399 $month_end = $month_start - 1;
400 if ($month_end < 1) {
401 $month_end = 12;
402 $year_end--;
403 }
404 $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start, $gm);
405 $date_end = dol_get_last_day($year_end, $month_end, $gm);
406 }
407
408 return array(
409 'date_start' => $date_start,
410 'date_end' => $date_end,
411 );
412}
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)
accounting_prepare_head(AccountingAccount $object)
Prepare array with list of tabs.
getCurrentPeriodOfFiscalYear($db, $conf, $from_time=null, $gm='tzserver', $withenddateonly=1)
Get current period of fiscal year?
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_hour($date, $gm='tzserver')
Return GMT time for last hour of a given GMT date (it replaces hours, min and second part to 23:59:59...
Definition date.lib.php:642
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:615
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 a Dolibarr global constant string value.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.