initdatesforvat.inc.php 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /**
  19. * \file htdocs/compta/tva/initdatesforvat.inc.php
  20. * \brief Set value for date_start and date_end
  21. */
  22. $now = dol_now();
  23. $current_date = dol_getdate($now);
  24. if (empty($conf->global->SOCIETE_FISCAL_MONTH_START)) {
  25. $conf->global->SOCIETE_FISCAL_MONTH_START = 1;
  26. }
  27. // Date range
  28. $year = GETPOST("year", "int");
  29. if (empty($year)) {
  30. $year_current = $current_date['year'];
  31. $year_start = $year_current;
  32. } else {
  33. $year_current = $year;
  34. $year_start = $year;
  35. }
  36. $date_start = dol_mktime(0, 0, 0, GETPOST("date_startmonth"), GETPOST("date_startday"), GETPOST("date_startyear"), 'tzserver');
  37. $date_end = dol_mktime(23, 59, 59, GETPOST("date_endmonth"), GETPOST("date_endday"), GETPOST("date_endyear"), 'tzserver');
  38. // Set default period if not defined
  39. if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
  40. $q = GETPOST("q", "int");
  41. if (empty($q)) {
  42. if (GETPOST("month", 'int')) {
  43. $date_start = dol_get_first_day($year_start, GETPOST("month", 'int'), 'tzserver');
  44. $date_end = dol_get_last_day($year_start, GETPOST("month", 'int'), 'tzserver');
  45. } else {
  46. if (empty($conf->global->MAIN_INFO_VAT_RETURN) || $conf->global->MAIN_INFO_VAT_RETURN == 2) { // quaterly vat, we take last past complete quarter
  47. $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');
  48. $date_end = dol_time_plus_duree($date_start, 3, 'm') - 1;
  49. } elseif ($conf->global->MAIN_INFO_VAT_RETURN == 3) { // yearly vat
  50. if ($current_date['mon'] < $conf->global->SOCIETE_FISCAL_MONTH_START) {
  51. if (($conf->global->SOCIETE_FISCAL_MONTH_START - $current_date['mon']) > 6) { // If period started from less than 6 years, we show past year
  52. $year_start--;
  53. }
  54. } else {
  55. if (($current_date['mon'] - $conf->global->SOCIETE_FISCAL_MONTH_START) < 6) { // If perdio started from less than 6 years, we show past year
  56. $year_start--;
  57. }
  58. }
  59. $date_start = dol_get_first_day($year_start, $conf->global->SOCIETE_FISCAL_MONTH_START, 'tzserver');
  60. $date_end = dol_time_plus_duree($date_start, 1, 'y') - 1;
  61. } elseif ($conf->global->MAIN_INFO_VAT_RETURN == 1) { // monthly vat, we take last past complete month
  62. $date_start = dol_time_plus_duree(dol_get_first_day($year_start, $current_date['mon'], false), -1, 'm');
  63. $date_end = dol_time_plus_duree($date_start, 1, 'm') - 1;
  64. }
  65. }
  66. } else {
  67. if ($q == 1) {
  68. $date_start = dol_get_first_day($year_start, 1, 'tzserver');
  69. $date_end = dol_get_last_day($year_start, 3, 'tzserver');
  70. }
  71. if ($q == 2) {
  72. $date_start = dol_get_first_day($year_start, 4, 'tzserver');
  73. $date_end = dol_get_last_day($year_start, 6, 'tzserver');
  74. }
  75. if ($q == 3) {
  76. $date_start = dol_get_first_day($year_start, 7, 'tzserver');
  77. $date_end = dol_get_last_day($year_start, 9, 'tzserver');
  78. }
  79. if ($q == 4) {
  80. $date_start = dol_get_first_day($year_start, 10, 'tzserver');
  81. $date_end = dol_get_last_day($year_start, 12, 'tzserver');
  82. }
  83. }
  84. }
  85. //print dol_print_date($date_start, 'day').' '.dol_print_date($date_end, 'day');
  86. $tmp = dol_getdate($date_start);
  87. $date_start_day = $tmp['mday'];
  88. $date_start_month = $tmp['mon'];
  89. $date_start_year = $tmp['year'];
  90. $tmp = dol_getdate($date_end);
  91. $date_end_day = $tmp['mday'];
  92. $date_end_month = $tmp['mon'];
  93. $date_end_year = $tmp['year'];