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