dolibarr 24.0.0-beta
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';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
31require_once DOL_DOCUMENT_ROOT.'/accountancy/class/bookkeeping.class.php';
32
42// Load translation files required by the page
43$langs->loadLangs(array("banks", "accountancy", "compta", "other", "errors"));
44
45$id_journal = GETPOSTINT('id_journal');
46$action = GETPOST('action', 'aZ09');
47
48$date_startmonth = GETPOSTINT('date_startmonth');
49$date_startday = GETPOSTINT('date_startday');
50$date_startyear = GETPOSTINT('date_startyear');
51$date_endmonth = GETPOSTINT('date_endmonth');
52$date_endday = GETPOSTINT('date_endday');
53$date_endyear = GETPOSTINT('date_endyear');
54$in_bookkeeping = GETPOST('in_bookkeeping');
55if ($in_bookkeeping == '') {
56 $in_bookkeeping = 'notyet';
57}
58
59// Get information of a journal
61$result = $object->fetch($id_journal);
62if ($result > 0) {
63 $id_journal = $object->id;
64} elseif ($result < 0) {
65 dol_print_error(null, $object->error, $object->errors);
66} elseif ($result == 0) {
67 accessforbidden('ErrorRecordNotFound');
68}
69
70$hookmanager->initHooks(array('globaljournal', $object->nature.'journal'));
71$parameters = array();
72
73$date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
74$date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
75
76$pastmonth = null; // Initialise, could be unset
77$pastmonthyear = null; // Initialise, could be unset
78
79if (empty($date_startmonth)) {
80 // Period by default on transfer
82 $date_start = $dates['date_start'];
83 $pastmonthyear = $dates['pastmonthyear'];
84 $pastmonth = $dates['pastmonth'];
85}
86if (empty($date_endmonth)) {
87 // Period by default on transfer
89 $date_end = $dates['date_end'];
90 $pastmonthyear = $dates['pastmonthyear'];
91 $pastmonth = $dates['pastmonth'];
92}
93
94if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) { // We define date_start and date_end, only if we did not submit the form
95 $date_start = dol_get_first_day((int) $pastmonthyear, (int) $pastmonth, false);
96 $date_end = dol_get_last_day((int) $pastmonthyear, (int) $pastmonth, false);
97}
98
99$data_type = 'view';
100if ($action == 'writebookkeeping') {
101 $data_type = 'bookkeeping';
102}
103if ($action == 'exportcsv') {
104 $data_type = 'csv';
105}
106$journal_data = $object->getData($user, $data_type, $date_start, $date_end, $in_bookkeeping);
107if (!is_array($journal_data)) {
108 setEventMessages($object->error, $object->errors, 'errors');
109}
110
111// Security check
112if (!isModEnabled('accounting')) {
114}
115if ($user->socid > 0) {
117}
118if (!$user->hasRight('accounting', 'bind', 'write')) {
120}
121
122
123/*
124 * Actions
125 */
126
127$reshook = $hookmanager->executeHooks('doActions', $parameters, $user, $action); // Note that $action and $object may have been modified by some hooks
128
129$reload = false;
130
131// Bookkeeping Write
132if ($action == 'writebookkeeping' && $user->hasRight('accounting', 'bind', 'write')) {
133 $error = 0;
134
135 $result = $object->writeIntoBookkeeping($user, $journal_data);
136 if ($result < 0) {
137 setEventMessages($object->error, $object->errors, 'errors');
138 $error = abs($result);
139 }
140
141 $nb_elements = count($journal_data);
142 if (empty($error) && $nb_elements > 0) {
143 setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
144 } elseif ($nb_elements == $error) {
145 setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
146 } else {
147 setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
148 }
149
150 $reload = true;
151} elseif ($action == 'exportcsv' && $user->hasRight('accounting', 'bind', 'write')) {
152 // Export CSV
153 $result = $object->exportCsv($journal_data, $date_end);
154 if ($result < 0) {
155 setEventMessages($object->error, $object->errors, 'errors');
156 $reload = true;
157 } else {
158 $filename = 'journal';
159 $type_export = 'journal';
160
161 require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
162 include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
163
164 print $result;
165
166 $db->close();
167 exit();
168 }
169}
170
171// Must reload data, so we make a redirect
172if ($reload) {
173 $param = 'id_journal=' . $id_journal;
174 $param .= '&date_startday=' . $date_startday;
175 $param .= '&date_startmonth=' . $date_startmonth;
176 $param .= '&date_startyear=' . $date_startyear;
177 $param .= '&date_endday=' . $date_endday;
178 $param .= '&date_endmonth=' . $date_endmonth;
179 $param .= '&date_endyear=' . $date_endyear;
180 $param .= '&in_bookkeeping=' . $in_bookkeeping;
181 header("Location: " . $_SERVER['PHP_SELF'] . ($param ? '?' . $param : ''));
182 exit;
183}
184
185
186/*
187 * View
188 */
189
190$form = new Form($db);
191
192if ($object->nature == 1) {
193 $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1'
194 || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1'
195 || !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1'
196 || !getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_GRANTED') || getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_GRANTED') == '-1'
197 || !getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_RECEIVED') || getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_RECEIVED') == '-1';
198 $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1'
199 || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1'
200 || !getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_GRANTED') || getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_GRANTED') == '-1'
201 || !getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_RECEIVED') || getDolGlobalString('ACCOUNTING_ACCOUNT_DISCOUNT_RECEIVED') == '-1';
202} elseif ($object->nature == 2) {
203 $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1';
204 $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1';
205} elseif ($object->nature == 3) {
206 $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1';
207 $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1';
208} elseif ($object->nature == 4) {
209 $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1'
210 || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1'
211 || !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1';
212 $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1'
213 || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1';
214} elseif ($object->nature == 5) {
215 $some_mandatory_steps_of_setup_were_not_done = !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1';
216 $account_accounting_not_defined = !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1';
217} else {
218 $title = $object->getLibType();
219 $some_mandatory_steps_of_setup_were_not_done = false;
220 $account_accounting_not_defined = false;
221}
222
223$title = $langs->trans("GenerationOfAccountingEntries") . ' - ' . $object->getNomUrl(0, 2, 1, '', 1);
224$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;';
225llxHeader('', dol_string_nohtmltag($title), $help_url, '', 0, 0, '', '', '', 'mod-accountancy accountancy-generation page-variousjournal');
226
227$nom = $title;
228$nomlink = '';
229$periodlink = '';
230$exportlink = '';
231$builddate = dol_now();
232$description = $langs->trans("DescJournalOnlyBindedVisible") . '<br>';
233if ($object->nature == 2 || $object->nature == 3) {
234 if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
235 $description .= $langs->trans("DepositsAreNotIncluded");
236 } else {
237 $description .= $langs->trans("DepositsAreIncluded");
238 }
239 if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
240 $description .= $langs->trans("SupplierDepositsAreNotIncluded");
241 }
242}
243
244$listofchoices = array('notyet' => $langs->trans("NotYetInGeneralLedger"), 'already' => $langs->trans("AlreadyInGeneralLedger"));
245$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);
246$period .= ' - ' . $langs->trans("JournalizationInLedgerStatus") . ' ' . $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
247
248$varlink = 'id_journal=' . $id_journal;
249
250journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
251
252if (getDolGlobalString('ACCOUNTANCY_FISCAL_PERIOD_MODE') != 'blockedonclosed') {
253 // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed)
254 // Fiscal period test
255 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_fiscalyear WHERE entity = ".((int) $conf->entity);
256 $resql = $db->query($sql);
257 if ($resql) {
258 $obj = $db->fetch_object($resql);
259 if ($obj->nb == 0) {
260 print '<br><div class="warning">'.img_warning().' '.$langs->trans("TheFiscalPeriodIsNotDefined");
261 $desc = ' : '.$langs->trans("AccountancyAreaDescFiscalPeriod", 4, '{link}');
262 $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("FiscalPeriod").'</strong>', $desc);
263 print $desc;
264 print '</div>';
265 }
266 } else {
268 }
269}
270
271if ($object->nature == 4) { // Bank journal
272 // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed)
273 $sql = "SELECT COUNT(rowid) as nb";
274 $sql .= " FROM " . MAIN_DB_PREFIX . "bank_account";
275 $sql .= " WHERE entity = " . (int) $conf->entity;
276 $sql .= " AND fk_accountancy_journal IS NULL";
277 $sql .= " AND clos=0";
278 $resql = $db->query($sql);
279 if ($resql) {
280 $obj = $db->fetch_object($resql);
281 if ($obj->nb > 0) {
282 print '<br>' . img_warning() . ' ' . $langs->trans("TheJournalCodeIsNotDefinedOnSomeBankAccount");
283 print ' : ' . $langs->trans("AccountancyAreaDescBank", 9, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("BankAccounts") . '</strong>');
284 }
285 } else {
287 }
288}
289
290// Button to write into Ledger
291if ($some_mandatory_steps_of_setup_were_not_done) {
292 print '<br><div class="warning">' . img_warning() . ' ' . $langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
293 print ' : ' . $langs->trans("AccountancyAreaDescMisc", 4, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("MenuDefaultAccounts") . '</strong>');
294 print '</div>';
295}
296print '<br><div class="tabsAction tabsActionNoBottom centerimp">';
297if (getDolGlobalString('ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL') && $in_bookkeeping == 'notyet') {
298 print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />';
299}
300if ($account_accounting_not_defined) {
301 print '<input type="button" class="butActionRefused classfortooltip" title="' . dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")) . '" value="' . $langs->trans("WriteBookKeeping") . '" />';
302} else {
303 if ($in_bookkeeping == 'notyet') {
304 print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
305 } else {
306 print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs->trans("WriteBookKeeping") . '</a>';
307 }
308}
309print '</div>';
310
311// TODO Avoid using js. We can use a direct link with $param
312print '
313 <script type="text/javascript">
314 function launch_export() {
315 $("div.fiche form input[name=\"action\"]").val("exportcsv");
316 $("div.fiche form input[type=\"submit\"]").click();
317 $("div.fiche form input[name=\"action\"]").val("");
318 }
319 function writebookkeeping() {
320 console.log("click on writebookkeeping");
321 $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
322 $("div.fiche form input[type=\"submit\"]").click();
323 $("div.fiche form input[name=\"action\"]").val("");
324 }
325 </script>';
326
327$object_label = $langs->trans("ObjectsRef");
328if ($object->nature == 2 || $object->nature == 3) {
329 $object_label = $langs->trans("InvoiceRef");
330}
331if ($object->nature == 5) {
332 $object_label = $langs->trans("ExpenseReportRef");
333}
334
335
336// Show result array
337$i = 0;
338
339print '<br>';
340
341print '<div class="div-table-responsive">';
342print '<table class="noborder centpercent">';
343print '<tr class="liste_titre">';
344print '<td>' . $langs->trans("Date") . '</td>';
345print '<td>' . $langs->trans("Piece") . ' (' . $object_label . ')</td>';
346print '<td>' . $langs->trans("AccountAccounting") . '</td>';
347print '<td>' . $langs->trans("SubledgerAccount") . '</td>';
348print '<td>' . $langs->trans("LabelOperation") . '</td>';
349if ($object->nature == 4) {
350 print '<td class="center">' . $langs->trans("PaymentMode") . '</td>';
351} // bank
352print '<td class="right">' . $langs->trans("AccountingDebit") . '</td>';
353print '<td class="right">' . $langs->trans("AccountingCredit") . '</td>';
354print "</tr>\n";
355
356if (is_array($journal_data) && !empty($journal_data)) {
357 foreach ($journal_data as $element_id => $element) {
358 foreach ($element['blocks'] as $lines) {
359 foreach ($lines as $line) {
360 print '<tr class="oddeven">';
361 print '<td>' . $line['date'] . '</td>';
362 print '<td>' . $line['piece'] . '</td>';
363 print '<td>' . $line['account_accounting'] . '</td>';
364 print '<td>' . $line['subledger_account'] . '</td>';
365 print '<td>' . $line['label_operation'] . '</td>';
366 if ($object->nature == 4) {
367 print '<td class="center">' . $line['payment_mode'] . '</td>';
368 }
369 print '<td class="right nowraponall">' . $line['debit'] . '</td>';
370 print '<td class="right nowraponall">' . $line['credit'] . '</td>';
371 print '</tr>';
372
373 $i++;
374 }
375 }
376 }
377}
378
379if (!$i) {
380 $colspan = 7;
381 if ($object->nature == 4) {
382 $colspan++;
383 }
384 print '<tr class="oddeven"><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
385}
386
387print '</table>';
388print '</div>';
389
390llxFooter();
391
392$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
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:604
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:623
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
$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...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.