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