index.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /* Copyright (C) 2019 Open-DSI <support@open-dsi.fr>
  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. */
  17. /**
  18. * \file htdocs/accountancy/closure/index.php
  19. * \ingroup Accountancy
  20. * \brief Home closure page
  21. */
  22. // Load Dolibarr environment
  23. require '../../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/bookkeeping.class.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array("compta", "bills", "other", "accountancy"));
  30. $validatemonth = GETPOST('validatemonth', 'int');
  31. $validateyear = GETPOST('validateyear', 'int');
  32. $action = GETPOST('action', 'aZ09');
  33. $object = new BookKeeping($db);
  34. $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
  35. if (GETPOST("year", 'int')) {
  36. $year_start = GETPOST("year", 'int');
  37. } else {
  38. $year_start = dol_print_date(dol_now(), '%Y');
  39. if (dol_print_date(dol_now(), '%m') < $month_start) {
  40. $year_start--; // If current month is lower that starting fiscal month, we start last year
  41. }
  42. }
  43. $year_end = $year_start + 1;
  44. $month_end = $month_start - 1;
  45. if ($month_end < 1) {
  46. $month_end = 12;
  47. $year_end--;
  48. }
  49. $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
  50. $search_date_end = dol_get_last_day($year_end, $month_end);
  51. $year_current = $year_start;
  52. // Security check
  53. if (!isModEnabled('accounting')) {
  54. accessforbidden();
  55. }
  56. if ($user->socid > 0) {
  57. accessforbidden();
  58. }
  59. if (!$user->hasRight('accounting', 'fiscalyear', 'write')) {
  60. accessforbidden();
  61. }
  62. /*
  63. * Actions
  64. */
  65. $now = dol_now();
  66. if ($action == 'validate_movements_confirm' && $user->hasRight('accounting', 'fiscalyear', 'write')) {
  67. $date_start = dol_mktime(0, 0, 0, GETPOST('date_startmonth', 'int'), GETPOST('date_startday', 'int'), GETPOST('date_startyear', 'int'));
  68. $date_end = dol_mktime(23, 59, 59, GETPOST('date_endmonth', 'int'), GETPOST('date_endday', 'int'), GETPOST('date_endyear', 'int'));
  69. $error = 0;
  70. $db->begin();
  71. // Specify as export : update field date_validated on selected month/year
  72. $sql = " UPDATE ".MAIN_DB_PREFIX."accounting_bookkeeping";
  73. $sql .= " SET date_validated = '".$db->idate($now)."'";
  74. $sql .= " WHERE entity = " . ((int) $conf->entity);
  75. $sql .= " AND doc_date >= '" . $db->idate($date_start) . "'";
  76. $sql .= " AND doc_date <= '" . $db->idate($date_end) . "'";
  77. $sql .= " AND date_validated IS NULL";
  78. dol_syslog("/accountancy/closure/index.php action=validate_movement_confirm -> Set movements as validated", LOG_DEBUG);
  79. $result = $db->query($sql);
  80. if (!$result) {
  81. $error++;
  82. }
  83. if (!$error) {
  84. $db->commit();
  85. setEventMessages($langs->trans("AllMovementsWereRecordedAsValidated"), null, 'mesgs');
  86. header("Location: ".$_SERVER['PHP_SELF']."?year=".$year_start);
  87. exit;
  88. } else {
  89. $db->rollback();
  90. setEventMessages($langs->trans("NotAllMovementsCouldBeRecordedAsValidated"), null, 'errors');
  91. $action = '';
  92. }
  93. }
  94. /*
  95. * View
  96. */
  97. $form = new Form($db);
  98. $formaccounting = new FormAccounting($db);
  99. $title = $langs->trans('Closure');
  100. $help_url ='EN:Module_Double_Entry_Accounting';
  101. llxHeader('', $title, $help_url);
  102. if ($action == 'validate_movements') {
  103. $form_question = array();
  104. $month = isset($conf->global->SOCIETE_FISCAL_MONTH_START) ? intval($conf->global->SOCIETE_FISCAL_MONTH_START) : 1;
  105. $date_start = new DateTime(sprintf('%04d-%02d-%02d', $year_start, $month, 1));
  106. $date_end = new DateTime(sprintf('%04d-%02d-%02d', $year_start, $month, 1));
  107. $date_end->add(new DateInterval('P1Y'));
  108. $date_end->sub(new DateInterval('P1D'));
  109. $form_question['date_start'] = array(
  110. 'name' => 'date_start',
  111. 'type' => 'date',
  112. 'label' => $langs->trans('DateStart'),
  113. 'value' => $date_start->format('Y-m-d')
  114. );
  115. $form_question['date_end'] = array(
  116. 'name' => 'date_end',
  117. 'type' => 'date',
  118. 'label' => $langs->trans('DateEnd'),
  119. 'value' => $date_end->format('Y-m-d')
  120. );
  121. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?year='.$year_start, $langs->trans('ValidateMovements'), $langs->trans('DescValidateMovements', $langs->transnoentitiesnoconv("RegistrationInAccounting")), 'validate_movements_confirm', $form_question, '', 1, 300);
  122. print $formconfirm;
  123. }
  124. $textprevyear = '<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current - 1).'">'.img_previous().'</a>';
  125. $textnextyear = '&nbsp;<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current + 1).'">'.img_next().'</a>';
  126. print load_fiche_titre($langs->trans("Closure")." ".$textprevyear." ".$langs->trans("Year")." ".$year_start." ".$textnextyear, '', 'title_accountancy');
  127. print '<span class="opacitymedium">'.$langs->trans("DescClosure").'</span><br>';
  128. print '<br>';
  129. $y = $year_current;
  130. $buttonvalidate = '<a class="butAction" name="button_validate_movements" href="'.$_SERVER["PHP_SELF"].'?action=validate_movements&year='.$year_start.'">'.$langs->trans("ValidateMovements").'</a>';
  131. print_barre_liste($langs->trans("OverviewOfMovementsNotValidated"), '', '', '', '', '', '', -1, '', '', 0, $buttonvalidate, '', 0, 1, 0);
  132. print '<div class="div-table-responsive-no-min">';
  133. print '<table class="noborder centpercent">';
  134. for ($i = 1; $i <= 12; $i++) {
  135. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  136. if ($j > 12) {
  137. $j -= 12;
  138. }
  139. print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
  140. }
  141. print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
  142. if (getDolGlobalString("ACCOUNTANCY_DISABLE_CLOSURE_LINE_BY_LINE")) {
  143. // TODO Analyse is done by finding record not into a closed period
  144. $sql = "SELECT COUNT(b.rowid) as detail,";
  145. for ($i = 1; $i <= 12; $i++) {
  146. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  147. if ($j > 12) {
  148. $j -= 12;
  149. }
  150. $sql .= " SUM(".$db->ifsql("MONTH(b.doc_date)=".$j, "1", "0").") AS month".str_pad($j, 2, "0", STR_PAD_LEFT).",";
  151. }
  152. $sql .= " COUNT(b.rowid) as total";
  153. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as b";
  154. $sql .= " WHERE b.doc_date >= '".$db->idate($search_date_start)."'";
  155. $sql .= " AND b.doc_date <= '".$db->idate($search_date_end)."'";
  156. $sql .= " AND b.entity IN (".getEntity('bookkeeping', 0).")"; // We don't share object for accountancy
  157. // Loop on each closed period
  158. $sql .= " AND b.doc_date BETWEEN 0 AND 0";
  159. } else {
  160. // Analyse closed record using the unitary flag/date on each record
  161. $sql = "SELECT COUNT(b.rowid) as detail,";
  162. for ($i = 1; $i <= 12; $i++) {
  163. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  164. if ($j > 12) {
  165. $j -= 12;
  166. }
  167. $sql .= " SUM(".$db->ifsql("MONTH(b.doc_date)=".$j, "1", "0").") AS month".str_pad($j, 2, "0", STR_PAD_LEFT).",";
  168. }
  169. $sql .= " COUNT(b.rowid) as total";
  170. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as b";
  171. $sql .= " WHERE b.doc_date >= '".$db->idate($search_date_start)."'";
  172. $sql .= " AND b.doc_date <= '".$db->idate($search_date_end)."'";
  173. $sql .= " AND b.entity IN (".getEntity('bookkeeping', 0).")"; // We don't share object for accountancy
  174. $sql .= " AND date_validated IS NULL";
  175. }
  176. dol_syslog('htdocs/accountancy/closure/index.php', LOG_DEBUG);
  177. $resql = $db->query($sql);
  178. if ($resql) {
  179. $num = $db->num_rows($resql);
  180. while ($row = $db->fetch_row($resql)) {
  181. print '<tr class="oddeven">';
  182. for ($i = 1; $i <= 12; $i++) {
  183. print '<td class="right">'.$row[$i].'</td>';
  184. }
  185. print '<td class="right"><b>'.$row[13].'</b></td>';
  186. print '</tr>';
  187. }
  188. $db->free($resql);
  189. } else {
  190. print $db->lasterror(); // Show last sql error
  191. }
  192. print "</table>\n";
  193. print '</div>';
  194. // End of page
  195. llxFooter();
  196. $db->close();