dolibarr  16.0.5
index.php
Go to the documentation of this file.
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 
24 require '../../main.inc.php';
25 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/bookkeeping.class.php';
29 
30 // Load translation files required by the page
31 $langs->loadLangs(array("compta", "bills", "other", "accountancy"));
32 
33 $validatemonth = GETPOST('validatemonth', 'int');
34 $validateyear = GETPOST('validateyear', 'int');
35 
36 $action = GETPOST('action', 'aZ09');
37 
38 $object = new BookKeeping($db);
39 
40 $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
41 if (GETPOST("year", 'int')) {
42  $year_start = GETPOST("year", 'int');
43 } else {
44  $year_start = dol_print_date(dol_now(), '%Y');
45  if (dol_print_date(dol_now(), '%m') < $month_start) {
46  $year_start--; // If current month is lower that starting fiscal month, we start last year
47  }
48 }
49 $year_end = $year_start + 1;
50 $month_end = $month_start - 1;
51 if ($month_end < 1) {
52  $month_end = 12;
53  $year_end--;
54 }
55 $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
56 $search_date_end = dol_get_last_day($year_end, $month_end);
57 $year_current = $year_start;
58 
59 // Security check
60 if (!isModEnabled('accounting')) {
62 }
63 if ($user->socid > 0) {
65 }
66 if (empty($user->rights->accounting->fiscalyear->write)) {
68 }
69 
70 
71 
72 /*
73  * Actions
74  */
75 
76 $now = dol_now();
77 
78 if ($action == 'validate_movements_confirm' && !empty($user->rights->accounting->fiscalyear->write)) {
79  $date_start = dol_mktime(0, 0, 0, GETPOST('date_startmonth', 'int'), GETPOST('date_startday', 'int'), GETPOST('date_startyear', 'int'));
80  $date_end = dol_mktime(23, 59, 59, GETPOST('date_endmonth', 'int'), GETPOST('date_endday', 'int'), GETPOST('date_endyear', 'int'));
81 
82  $error = 0;
83 
84  $db->begin();
85 
86  // Specify as export : update field date_validated on selected month/year
87  $sql = " UPDATE ".MAIN_DB_PREFIX."accounting_bookkeeping";
88  $sql .= " SET date_validated = '".$db->idate($now)."'";
89  $sql .= " WHERE entity = " . ((int) $conf->entity);
90  $sql .= " AND doc_date >= '" . $db->idate($date_start) . "'";
91  $sql .= " AND doc_date <= '" . $db->idate($date_end) . "'";
92  $sql .= " AND date_validated IS NULL";
93 
94  dol_syslog("/accountancy/closure/index.php action=validate_movement_confirm -> Set movements as validated", LOG_DEBUG);
95  $result = $db->query($sql);
96  if (!$result) {
97  $error++;
98  }
99 
100  if (!$error) {
101  $db->commit();
102 
103  setEventMessages($langs->trans("AllMovementsWereRecordedAsValidated"), null, 'mesgs');
104 
105  header("Location: ".$_SERVER['PHP_SELF']."?year=".$year_start);
106  exit;
107  } else {
108  $db->rollback();
109 
110  setEventMessages($langs->trans("NotAllMovementsCouldBeRecordedAsValidated"), null, 'errors');
111  $action = '';
112  }
113 }
114 
115 
116 /*
117  * View
118  */
119 
120 $form = new Form($db);
121 $formaccounting = new FormAccounting($db);
122 
123 $title = $langs->trans('Closure');
124 
125 $help_url ='EN:Module_Double_Entry_Accounting';
126 
127 llxHeader('', $title, $help_url);
128 
129 if ($action == 'validate_movements') {
130  $form_question = array();
131 
132  $month = isset($conf->global->SOCIETE_FISCAL_MONTH_START) ? intval($conf->global->SOCIETE_FISCAL_MONTH_START) : 1;
133  $date_start = new DateTime(sprintf('%04d-%02d-%02d', $year_start, $month, 1));
134  $date_end = new DateTime(sprintf('%04d-%02d-%02d', $year_start, $month, 1));
135  $date_end->add(new DateInterval('P1Y'));
136  $date_end->sub(new DateInterval('P1D'));
137 
138  $form_question['date_start'] = array(
139  'name' => 'date_start',
140  'type' => 'date',
141  'label' => $langs->trans('DateStart'),
142  'value' => $date_start->format('Y-m-d')
143  );
144  $form_question['date_end'] = array(
145  'name' => 'date_end',
146  'type' => 'date',
147  'label' => $langs->trans('DateEnd'),
148  'value' => $date_end->format('Y-m-d')
149  );
150 
151  $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);
152  print $formconfirm;
153 }
154 
155 $textprevyear = '<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current - 1).'">'.img_previous().'</a>';
156 $textnextyear = '&nbsp;<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current + 1).'">'.img_next().'</a>';
157 
158 
159 print load_fiche_titre($langs->trans("Closure")." ".$textprevyear." ".$langs->trans("Year")." ".$year_start." ".$textnextyear, '', 'title_accountancy');
160 
161 print '<span class="opacitymedium">'.$langs->trans("DescClosure").'</span><br>';
162 print '<br>';
163 
164 
165 $y = $year_current;
166 
167 $buttonvalidate = '<a class="butAction" name="button_validate_movements" href="'.$_SERVER["PHP_SELF"].'?action=validate_movements&year='.$year_start.'">'.$langs->trans("ValidateMovements").'</a>';
168 
169 print_barre_liste($langs->trans("OverviewOfMovementsNotValidated"), '', '', '', '', '', '', -1, '', '', 0, $buttonvalidate, '', 0, 1, 1);
170 
171 print '<div class="div-table-responsive-no-min">';
172 print '<table class="noborder centpercent">';
173 for ($i = 1; $i <= 12; $i++) {
174  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
175  if ($j > 12) {
176  $j -= 12;
177  }
178  print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
179 }
180 print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
181 
182 if (getDolGlobalString("ACCOUNTANCY_DISABLE_CLOSURE_LINE_BY_LINE")) {
183  // TODO Analyse is done by finding record not into a closed period
184  $sql = "SELECT COUNT(b.rowid) as detail,";
185  for ($i = 1; $i <= 12; $i++) {
186  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
187  if ($j > 12) {
188  $j -= 12;
189  }
190  $sql .= " SUM(".$db->ifsql("MONTH(b.doc_date)=".$j, "1", "0").") AS month".str_pad($j, 2, "0", STR_PAD_LEFT).",";
191  }
192  $sql .= " COUNT(b.rowid) as total";
193  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as b";
194  $sql .= " WHERE b.doc_date >= '".$db->idate($search_date_start)."'";
195  $sql .= " AND b.doc_date <= '".$db->idate($search_date_end)."'";
196  $sql .= " AND b.entity IN (".getEntity('bookkeeping', 0).")"; // We don't share object for accountancy
197  // Loop on each closed period
198  $sql .= " AND b.doc_date BETWEEN 0 AND 0";
199 } else {
200  // Analyse closed record using the unitary flag/date on each record
201  $sql = "SELECT COUNT(b.rowid) as detail,";
202  for ($i = 1; $i <= 12; $i++) {
203  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
204  if ($j > 12) {
205  $j -= 12;
206  }
207  $sql .= " SUM(".$db->ifsql("MONTH(b.doc_date)=".$j, "1", "0").") AS month".str_pad($j, 2, "0", STR_PAD_LEFT).",";
208  }
209  $sql .= " COUNT(b.rowid) as total";
210  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as b";
211  $sql .= " WHERE b.doc_date >= '".$db->idate($search_date_start)."'";
212  $sql .= " AND b.doc_date <= '".$db->idate($search_date_end)."'";
213  $sql .= " AND b.entity IN (".getEntity('bookkeeping', 0).")"; // We don't share object for accountancy
214  $sql .= " AND date_validated IS NULL";
215 }
216 
217 dol_syslog('htdocs/accountancy/closure/index.php', LOG_DEBUG);
218 $resql = $db->query($sql);
219 if ($resql) {
220  $num = $db->num_rows($resql);
221 
222  while ($row = $db->fetch_row($resql)) {
223  print '<tr class="oddeven">';
224  for ($i = 1; $i <= 12; $i++) {
225  print '<td class="right">'.$row[$i].'</td>';
226  }
227  print '<td class="right"><b>'.$row[13].'</b></td>';
228  print '</tr>';
229  }
230 
231  $db->free($resql);
232 } else {
233  print $db->lasterror(); // Show last sql error
234 }
235 print "</table>\n";
236 print '</div>';
237 
238 // End of page
239 llxFooter();
240 $db->close();
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
FormAccounting
Class to manage generation of HTML components for accounting management.
Definition: html.formaccounting.class.php:33
$help_url
if(GETPOST('button_removefilter_x', 'alpha')||GETPOST('button_removefilter.x', 'alpha')||GETPOST('button_removefilter', 'alpha')) if(GETPOST('button_search_x', 'alpha')||GETPOST('button_search.x', 'alpha')||GETPOST('button_search', 'alpha')) if($action=="save" &&empty($cancel)) $help_url
View.
Definition: agenda.php:116
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
llxFooter
llxFooter()
Footer empty.
Definition: index.php:71
img_next
img_next($titlealt='default', $moreatt='')
Show next logo.
Definition: functions.lib.php:4557
$formconfirm
$formconfirm
if ($action == 'delbookkeepingyear') {
Definition: listbyaccount.php:576
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
print_barre_liste
print_barre_liste($titre, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $hideselectlimit=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
Definition: functions.lib.php:5257
llxHeader
if(!defined('NOTOKENRENEWAL')) if(!defined('NOLOGIN')) if(!defined('NOCSRFCHECK')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) if(!defined('NOIPCHECK')) if(!defined('NOBROWSERNOTIF')) llxHeader()
Header empty.
Definition: index.php:63
getDolGlobalString
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:80
dol_get_last_day
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:570
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:105
BookKeeping
Class to manage Ledger (General Ledger and Subledger)
Definition: bookkeeping.class.php:33
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
dol_mktime
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
Definition: functions.lib.php:2757
img_previous
img_previous($titlealt='default', $moreatt='')
Show previous logo.
Definition: functions.lib.php:4576