dolibarr 25.0.0-alpha
variousjournal.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2021-2026 Alexandre Spangaro <alexandre@inovea-conseil.com>
3 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// Load Dolibarr environment
27require '../../main.inc.php';
36require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
37require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
38require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
39require_once DOL_DOCUMENT_ROOT.'/accountancy/class/bookkeeping.class.php';
40
41// Load translation files required by the page
42$langs->loadLangs(array("banks", "accountancy", "compta", "other"));
43
44$id_journal = GETPOSTINT('id_journal');
45$action = GETPOST('action', 'aZ09');
46
47$date_startmonth = GETPOSTINT('date_startmonth');
48$date_startday = GETPOSTINT('date_startday');
49$date_startyear = GETPOSTINT('date_startyear');
50$date_endmonth = GETPOSTINT('date_endmonth');
51$date_endday = GETPOSTINT('date_endday');
52$date_endyear = GETPOSTINT('date_endyear');
53$in_bookkeeping = GETPOST('in_bookkeeping');
54if ($in_bookkeeping == '') {
55 $in_bookkeeping = 'notyet';
56}
57
58// Get information of a journal
59$object = new AccountingJournal($db);
60$result = $object->fetch($id_journal);
61if ($result > 0) {
62 $id_journal = $object->id;
63} elseif ($result < 0) {
64 dol_print_error(null, $object->error, $object->errors);
65} elseif ($result == 0) {
66 accessforbidden('ErrorRecordNotFound');
67}
68
69$hookmanager->initHooks(array('globaljournal', $object->nature.'journal'));
70$parameters = array();
71
72$date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
73$date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
74
75$pastmonth = null; // Initialise, could be unset
76$pastmonthyear = null; // Initialise, could be unset
77
78if (empty($date_startmonth)) {
79 // Period by default on transfer
81 $date_start = $dates['date_start'];
82 $pastmonthyear = $dates['pastmonthyear'];
83 $pastmonth = $dates['pastmonth'];
84}
85if (empty($date_endmonth)) {
86 // Period by default on transfer
88 $date_end = $dates['date_end'];
89 $pastmonthyear = $dates['pastmonthyear'];
90 $pastmonth = $dates['pastmonth'];
91}
92
93if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) { // We define date_start and date_end, only if we did not submit the form
94 $date_start = dol_get_first_day((int) $pastmonthyear, (int) $pastmonth, false);
95 $date_end = dol_get_last_day((int) $pastmonthyear, (int) $pastmonth, false);
96}
97
98$data_type = 'view';
99if ($action == 'writebookkeeping') {
100 $data_type = 'bookkeeping';
101}
102if ($action == 'exportcsv') {
103 $data_type = 'csv';
104}
105$journal_data = $object->getData($user, $data_type, $date_start, $date_end, $in_bookkeeping);
106if (!is_array($journal_data)) {
107 setEventMessages($object->error, $object->errors, 'errors');
108}
109
110// Security check
111if (!isModEnabled('accounting')) {
113}
114if ($user->socid > 0) {
116}
117if (!$user->hasRight('accounting', 'bind', 'write')) {
119}
120
121
122/*
123 * Actions
124 */
125
126$reshook = $hookmanager->executeHooks('doActions', $parameters, $user, $action); // Note that $action and $object may have been modified by some hooks
127
128$reload = false;
129
130// Bookkeeping Write
131if ($action == 'writebookkeeping' && $user->hasRight('accounting', 'bind', 'write')) {
132 $error = 0;
133
134 $result = $object->writeIntoBookkeeping($user, $journal_data);
135 if ($result < 0) {
136 setEventMessages($object->error, $object->errors, 'errors');
137 $error = abs($result);
138 }
139
140 $nb_elements = count($journal_data);
141 if (empty($error) && $nb_elements > 0) {
142 setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
143 } elseif ($nb_elements == $error) {
144 setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
145 } else {
146 setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
147 }
148
149 $reload = true;
150} elseif ($action == 'exportcsv' && $user->hasRight('accounting', 'bind', 'write')) {
151 // Export CSV
152 $result = $object->exportCsv($journal_data, $date_end);
153 if ($result < 0) {
154 setEventMessages($object->error, $object->errors, 'errors');
155 $reload = true;
156 } else {
157 $filename = 'journal';
158 $type_export = 'journal';
159
160 require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
161 include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
162
163 print $result;
164
165 $db->close();
166 exit();
167 }
168}
169
170// Must reload data, so we make a redirect
171if ($reload) {
172 $param = 'id_journal=' . $id_journal;
173 $param .= '&date_startday=' . $date_startday;
174 $param .= '&date_startmonth=' . $date_startmonth;
175 $param .= '&date_startyear=' . $date_startyear;
176 $param .= '&date_endday=' . $date_endday;
177 $param .= '&date_endmonth=' . $date_endmonth;
178 $param .= '&date_endyear=' . $date_endyear;
179 $param .= '&in_bookkeeping=' . $in_bookkeeping;
180 header("Location: " . $_SERVER['PHP_SELF'] . ($param ? '?' . $param : ''));
181 exit;
182}
183
184
185/*
186 * View
187 */
188
189$form = new Form($db);
190
191if ($object->nature == 1) {
192 $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1'
193 || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1'
194 || !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1'
195 || !getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_GRANTED') || getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_GRANTED') == '-1'
196 || !getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_RECEIVED') || getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_RECEIVED') == '-1';
197 $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1'
198 || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1'
199 || !getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_GRANTED') || getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_GRANTED') == '-1'
200 || !getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_RECEIVED') || getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_RECEIVED') == '-1';
201} elseif ($object->nature == 2) {
202 $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1';
203 $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1';
204} elseif ($object->nature == 3) {
205 $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1';
206 $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1';
207} elseif ($object->nature == 4) {
208 $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1'
209 || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1'
210 || !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1';
211 $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1'
212 || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1';
213} elseif ($object->nature == 5) {
214 $some_mandatory_steps_of_setup_were_not_done = !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1';
215 $account_accounting_not_defined = !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1';
216} else {
217 $title = $object->getLibType();
218 $some_mandatory_steps_of_setup_were_not_done = false;
219 $account_accounting_not_defined = false;
220}
221
222$title = $langs->trans("GenerationOfAccountingEntries") . ' - ' . $object->getNomUrl(0, 2, 1, '', 1);
223$help_url = 'EN:Module_Double_Entry_Accounting|FR:Module_Comptabilit&eacute;_en_Partie_Double#G&eacute;n&eacute;ration_des_&eacute;critures_en_comptabilit&eacute;';
224llxHeader('', dol_string_nohtmltag($title), $help_url, '', 0, 0, '', '', '', 'mod-accountancy accountancy-generation page-variousjournal');
225
226$nom = $title;
227$nomlink = '';
228$periodlink = '';
229$exportlink = '';
230$builddate = dol_now();
231$description = $langs->trans("DescJournalOnlyBindedVisible") . '<br>';
232if ($object->nature == 2 || $object->nature == 3) {
233 if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
234 $description .= $langs->trans("DepositsAreNotIncluded");
235 } else {
236 $description .= $langs->trans("DepositsAreIncluded");
237 }
238 if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
239 $description .= $langs->trans("SupplierDepositsAreNotIncluded");
240 }
241}
242
243$listofchoices = array('notyet' => $langs->trans("NotYetInGeneralLedger"), 'already' => $langs->trans("AlreadyInGeneralLedger"));
244$period = $form->selectDate($date_start ? $date_start : -1, 'date_start', 0, 0, 0, '', 1, 0) . ' - ' . $form->selectDate($date_end ? $date_end : -1, 'date_end', 0, 0, 0, '', 1, 0);
245$period .= ' - ' . $langs->trans("JournalizationInLedgerStatus") . ' ' . $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
246
247$varlink = 'id_journal=' . $id_journal;
248
249journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
250
251if (getDolGlobalString('ACCOUNTANCY_FISCAL_PERIOD_MODE') != 'blockedonclosed') {
252 // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed)
253 // Fiscal period test
254 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_fiscalyear WHERE entity = ".((int) $conf->entity);
255 $resql = $db->query($sql);
256 if ($resql) {
257 $obj = $db->fetch_object($resql);
258 if ($obj->nb == 0) {
259 print '<br><div class="warning">'.img_warning().' '.$langs->trans("TheFiscalPeriodIsNotDefined");
260 $desc = ' : '.$langs->trans("AccountancyAreaDescFiscalPeriod", 4, '{link}');
261 $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("FiscalPeriod").'</strong>', $desc);
262 print $desc;
263 print '</div>';
264 }
265 } else {
266 dol_print_error($db);
267 }
268}
269
270if ($object->nature == 4) { // Bank journal
271 // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed)
272 $sql = "SELECT COUNT(rowid) as nb";
273 $sql .= " FROM " . MAIN_DB_PREFIX . "bank_account";
274 $sql .= " WHERE entity = " . (int) $conf->entity;
275 $sql .= " AND fk_accountancy_journal IS NULL";
276 $sql .= " AND clos=0";
277 $resql = $db->query($sql);
278 if ($resql) {
279 $obj = $db->fetch_object($resql);
280 if ($obj->nb > 0) {
281 print '<br>' . img_warning() . ' ' . $langs->trans("TheJournalCodeIsNotDefinedOnSomeBankAccount");
282 print ' : ' . $langs->trans("AccountancyAreaDescBank", 9, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("BankAccounts") . '</strong>');
283 }
284 } else {
285 dol_print_error($db);
286 }
287}
288
289// Button to write into Ledger
290if ($some_mandatory_steps_of_setup_were_not_done) {
291 print '<br><div class="warning">' . img_warning() . ' ' . $langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
292 print ' : ' . $langs->trans("AccountancyAreaDescMisc", 4, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("MenuDefaultAccounts") . '</strong>');
293 print '</div>';
294}
295print '<br><div class="tabsAction tabsActionNoBottom centerimp">';
296if (getDolGlobalString('ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL') && $in_bookkeeping == 'notyet') {
297 print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />';
298}
299if ($account_accounting_not_defined) {
300 print '<input type="button" class="butActionRefused classfortooltip" title="' . dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")) . '" value="' . $langs->trans("WriteBookKeeping") . '" />';
301} else {
302 if ($in_bookkeeping == 'notyet') {
303 print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
304 } else {
305 print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs->trans("WriteBookKeeping") . '</a>';
306 }
307}
308print '</div>';
309
310// TODO Avoid using js. We can use a direct link with $param
311print '
312 <script type="text/javascript">
313 function launch_export() {
314 $("div.fiche form input[name=\"action\"]").val("exportcsv");
315 $("div.fiche form input[type=\"submit\"]").click();
316 $("div.fiche form input[name=\"action\"]").val("");
317 }
318 function writebookkeeping() {
319 console.log("click on writebookkeeping");
320 $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
321 $("div.fiche form input[type=\"submit\"]").click();
322 $("div.fiche form input[name=\"action\"]").val("");
323 }
324 </script>';
325
326$object_label = $langs->trans("ObjectsRef");
327if ($object->nature == 2 || $object->nature == 3) {
328 $object_label = $langs->trans("InvoiceRef");
329}
330if ($object->nature == 5) {
331 $object_label = $langs->trans("ExpenseReportRef");
332}
333
334
335// Show result array
336$i = 0;
337
338print '<br>';
339
340print '<div class="div-table-responsive">';
341print '<table class="noborder centpercent">';
342print '<tr class="liste_titre">';
343print '<td>' . $langs->trans("Date") . '</td>';
344print '<td>' . $langs->trans("Piece") . ' (' . $object_label . ')</td>';
345print '<td>' . $langs->trans("AccountAccounting") . '</td>';
346print '<td>' . $langs->trans("SubledgerAccount") . '</td>';
347print '<td>' . $langs->trans("LabelOperation") . '</td>';
348if ($object->nature == 4) {
349 print '<td class="center">' . $langs->trans("PaymentMode") . '</td>';
350} // bank
351print '<td class="right">' . $langs->trans("AccountingDebit") . '</td>';
352print '<td class="right">' . $langs->trans("AccountingCredit") . '</td>';
353print "</tr>\n";
354
355if (is_array($journal_data) && !empty($journal_data)) {
356 foreach ($journal_data as $element_id => $element) {
357 foreach ($element['blocks'] as $lines) {
358 foreach ($lines as $line) {
359 print '<tr class="oddeven">';
360 print '<td>' . $line['date'] . '</td>';
361 print '<td>' . $line['piece'] . '</td>';
362 print '<td>' . $line['account_accounting'] . '</td>';
363 print '<td>' . $line['subledger_account'] . '</td>';
364 print '<td>' . $line['label_operation'] . '</td>';
365 if ($object->nature == 4) {
366 print '<td class="center">' . $line['payment_mode'] . '</td>';
367 }
368 print '<td class="right nowraponall">' . $line['debit'] . '</td>';
369 print '<td class="right nowraponall">' . $line['credit'] . '</td>';
370 print '</tr>';
371
372 $i++;
373 }
374 }
375 }
376}
377
378if (!$i) {
379 $colspan = 7;
380 if ($object->nature == 4) {
381 $colspan++;
382 }
383 print '<tr class="oddeven"><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
384}
385
386print '</table>';
387print '</div>';
388
389llxFooter();
390
391$db->close();
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
journalHead($nom, $variant, $period, $periodlink, $description, $builddate, $exportlink='', $moreparam=array(), $calcmode='', $varlink='', $moreoptions=array())
Show header of a page used to transfer/dispatch data in accounting.
getDefaultDatesForTransfer()
Return Default dates for transfer based on periodicity option in accountancy setup.
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage accounting journals.
Class to manage generation of HTML components Only common components must be here.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:626
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:645
$date_start
Variables from include:
dol_now($mode='gmt')
Return date for now.
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...
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:183
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.