dolibarr 20.0.0
initdatesforvat.inc.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2021 Laurent Destailleur <eldy@users.sourceforge.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 * or see https://www.gnu.org/
17 */
18
24$now = dol_now();
25$current_date = dol_getdate($now);
26if (!getDolGlobalInt('SOCIETE_FISCAL_MONTH_START')) {
27 $conf->global->SOCIETE_FISCAL_MONTH_START = 1;
28}
29
30// Date range
31$year = GETPOSTINT("year");
32if (empty($year)) {
33 $year_current = $current_date['year'];
34 $year_start = $year_current;
35} else {
36 $year_current = $year;
37 $year_start = $year;
38}
39$date_start = dol_mktime(0, 0, 0, GETPOST("date_startmonth"), GETPOST("date_startday"), GETPOST("date_startyear"), 'tzserver');
40$date_end = dol_mktime(23, 59, 59, GETPOST("date_endmonth"), GETPOST("date_endday"), GETPOST("date_endyear"), 'tzserver');
41// Set default period if not defined
42if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
43 $q = GETPOSTINT("q");
44 if (empty($q)) {
45 if (GETPOSTINT("month")) {
46 $date_start = dol_get_first_day($year_start, GETPOSTINT("month"), 'tzserver');
47 $date_end = dol_get_last_day($year_start, GETPOSTINT("month"), 'tzserver');
48 } else {
49 if (!getDolGlobalString('MAIN_INFO_VAT_RETURN') || getDolGlobalInt('MAIN_INFO_VAT_RETURN') == 2) { // quaterly vat, we take last past complete quarter
50 $date_start = dol_time_plus_duree(dol_get_first_day($year_start, $current_date['mon'], false), -3 - (($current_date['mon'] - $conf->global->SOCIETE_FISCAL_MONTH_START) % 3), 'm');
51 $date_end = dol_time_plus_duree($date_start, 3, 'm') - 1;
52 } elseif (getDolGlobalInt('MAIN_INFO_VAT_RETURN') == 3) { // yearly vat
53 if ($current_date['mon'] < $conf->global->SOCIETE_FISCAL_MONTH_START) {
54 if (($conf->global->SOCIETE_FISCAL_MONTH_START - $current_date['mon']) > 6) { // If period started from less than 6 years, we show past year
55 $year_start--;
56 }
57 } else {
58 if (($current_date['mon'] - $conf->global->SOCIETE_FISCAL_MONTH_START) < 6) { // If perdio started from less than 6 years, we show past year
59 $year_start--;
60 }
61 }
62 $date_start = dol_get_first_day($year_start, $conf->global->SOCIETE_FISCAL_MONTH_START, 'tzserver');
63 $date_end = dol_time_plus_duree($date_start, 1, 'y') - 1;
64 } elseif (getDolGlobalInt('MAIN_INFO_VAT_RETURN') == 1) { // monthly vat, we take last past complete month
65 $date_start = dol_time_plus_duree(dol_get_first_day($year_start, $current_date['mon'], false), -1, 'm');
66 $date_end = dol_time_plus_duree($date_start, 1, 'm') - 1;
67 }
68 }
69 } else {
70 if ($q == 1) {
71 $date_start = dol_get_first_day($year_start, 1, 'tzserver');
72 $date_end = dol_get_last_day($year_start, 3, 'tzserver');
73 }
74 if ($q == 2) {
75 $date_start = dol_get_first_day($year_start, 4, 'tzserver');
76 $date_end = dol_get_last_day($year_start, 6, 'tzserver');
77 }
78 if ($q == 3) {
79 $date_start = dol_get_first_day($year_start, 7, 'tzserver');
80 $date_end = dol_get_last_day($year_start, 9, 'tzserver');
81 }
82 if ($q == 4) {
83 $date_start = dol_get_first_day($year_start, 10, 'tzserver');
84 $date_end = dol_get_last_day($year_start, 12, 'tzserver');
85 }
86 }
87}
88
89//print dol_print_date($date_start, 'day').' '.dol_print_date($date_end, 'day');
90
91$tmp = dol_getdate($date_start);
92$date_start_day = $tmp['mday'];
93$date_start_month = $tmp['mon'];
94$date_start_year = $tmp['year'];
95$tmp = dol_getdate($date_end);
96$date_end_day = $tmp['mday'];
97$date_end_month = $tmp['mon'];
98$date_end_year = $tmp['year'];
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:594
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:124
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...
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.