dolibarr 18.0.6
variousjournal.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2021-2022 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
25require '../../main.inc.php';
26require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
28require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
29
30// Load translation files required by the page
31$langs->loadLangs(array("banks", "accountancy", "compta", "other", "errors"));
32
33$id_journal = GETPOST('id_journal', 'int');
34$action = GETPOST('action', 'aZ09');
35
36$date_startmonth = GETPOST('date_startmonth');
37$date_startday = GETPOST('date_startday');
38$date_startyear = GETPOST('date_startyear');
39$date_endmonth = GETPOST('date_endmonth');
40$date_endday = GETPOST('date_endday');
41$date_endyear = GETPOST('date_endyear');
42$in_bookkeeping = GETPOST('in_bookkeeping');
43if ($in_bookkeeping == '') {
44 $in_bookkeeping = 'notyet';
45}
46
47// Get information of journal
48$object = new AccountingJournal($db);
49$result = $object->fetch($id_journal);
50if ($result > 0) {
51 $id_journal = $object->id;
52} elseif ($result < 0) {
53 dol_print_error('', $object->error, $object->errors);
54} elseif ($result == 0) {
55 accessforbidden('ErrorRecordNotFound');
56}
57
58$hookmanager->initHooks(array('globaljournal', $object->nature.'journal'));
59$parameters = array();
60
61$date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
62$date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
63
64if (empty($date_startmonth) || empty($date_endmonth)) {
65 // Period by default on transfer
67 $date_start = $dates['date_start'];
68 $date_end = $dates['date_end'];
69 $pastmonthyear = $dates['pastmonthyear'];
70 $pastmonth = $dates['pastmonth'];
71}
72
73if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) { // We define date_start and date_end, only if we did not submit the form
74 $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
75 $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
76}
77
78$data_type = 'view';
79if ($action == 'writebookkeeping') $data_type = 'bookkeeping';
80if ($action == 'exportcsv') $data_type = 'csv';
81$journal_data = $object->getData($user, $data_type, $date_start, $date_end, $in_bookkeeping);
82if (!is_array($journal_data)) {
83 setEventMessages($object->error, $object->errors, 'errors');
84}
85
86// Security check
87if (!isModEnabled('accounting')) {
89}
90if ($user->socid > 0) {
92}
93if (!$user->hasRight('accounting', 'mouvements', 'lire')) {
95}
96
97
98/*
99 * Actions
100 */
101
102$reshook = $hookmanager->executeHooks('doActions', $parameters, $user, $action); // Note that $action and $object may have been modified by some hooks
103
104$reload = false;
105
106// Bookkeeping Write
107if ($action == 'writebookkeeping') {
108 $error = 0;
109
110 $result = $object->writeIntoBookkeeping($user, $journal_data);
111 if ($result < 0) {
112 setEventMessages($object->error, $object->errors, 'errors');
113 $error = abs($result);
114 }
115
116 $nb_elements = count($journal_data);
117 if (empty($error) && $nb_elements > 0) {
118 setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
119 } elseif ($nb_elements == $error) {
120 setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
121 } else {
122 setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
123 }
124
125 $reload = true;
126} elseif ($action == 'exportcsv') {
127 // Export CSV
128 $result = $object->exportCsv($journal_data, $date_end);
129 if ($result < 0) {
130 setEventMessages($object->error, $object->errors, 'errors');
131 $reload = true;
132 } else {
133 $filename = 'journal';
134 $type_export = 'journal';
135
136 require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
137 include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
138
139 print $result;
140
141 $db->close();
142 exit();
143 }
144}
145
146// Must reload data, so we make a redirect
147if ($reload) {
148 $param = 'id_journal=' . $id_journal;
149 $param .= '&date_startday=' . $date_startday;
150 $param .= '&date_startmonth=' . $date_startmonth;
151 $param .= '&date_startyear=' . $date_startyear;
152 $param .= '&date_endday=' . $date_endday;
153 $param .= '&date_endmonth=' . $date_endmonth;
154 $param .= '&date_endyear=' . $date_endyear;
155 $param .= '&in_bookkeeping=' . $in_bookkeeping;
156 header("Location: " . $_SERVER['PHP_SELF'] . ($param ? '?' . $param : ''));
157 exit;
158}
159
160
161/*
162 * View
163 */
164
165$form = new Form($db);
166
167if ($object->nature == 2) {
168 $some_mandatory_steps_of_setup_were_not_done = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1';
169 $account_accounting_not_defined = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1';
170} elseif ($object->nature == 3) {
171 $some_mandatory_steps_of_setup_were_not_done = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1';
172 $account_accounting_not_defined = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1';
173} elseif ($object->nature == 4) {
174 $some_mandatory_steps_of_setup_were_not_done = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1'
175 || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1'
176 || empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1';
177 $account_accounting_not_defined = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1'
178 || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1';
179} elseif ($object->nature == 5) {
180 $some_mandatory_steps_of_setup_were_not_done = empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1';
181 $account_accounting_not_defined = empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1';
182} else {
183 $title = $object->getLibType();
184 $some_mandatory_steps_of_setup_were_not_done = false;
185 $account_accounting_not_defined = false;
186}
187
188$title = $langs->trans("GenerationOfAccountingEntries") . ' - ' . $object->getNomUrl(0, 2, 1, '', 1);
189
191
192$nom = $title;
193$nomlink = '';
194$periodlink = '';
195$exportlink = '';
196$builddate = dol_now();
197$description = $langs->trans("DescJournalOnlyBindedVisible") . '<br>';
198if ($object->nature == 2 || $object->nature == 3) {
199 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
200 $description .= $langs->trans("DepositsAreNotIncluded");
201 } else {
202 $description .= $langs->trans("DepositsAreIncluded");
203 }
204 if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
205 $description .= $langs->trans("SupplierDepositsAreNotIncluded");
206 }
207}
208
209$listofchoices = array('notyet' => $langs->trans("NotYetInGeneralLedger"), 'already' => $langs->trans("AlreadyInGeneralLedger"));
210$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);
211$period .= ' - ' . $langs->trans("JournalizationInLedgerStatus") . ' ' . $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
212
213$varlink = 'id_journal=' . $id_journal;
214
215journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
216
217if ($object->nature == 4) { // Bank journal
218 // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed)
219 $sql = "SELECT COUNT(rowid) as nb";
220 $sql .= " FROM " . MAIN_DB_PREFIX . "bank_account";
221 $sql .= " WHERE entity = " . (int) $conf->entity;
222 $sql .= " AND fk_accountancy_journal IS NULL";
223 $sql .= " AND clos=0";
224 $resql = $db->query($sql);
225 if ($resql) {
226 $obj = $db->fetch_object($resql);
227 if ($obj->nb > 0) {
228 print '<br>' . img_warning() . ' ' . $langs->trans("TheJournalCodeIsNotDefinedOnSomeBankAccount");
229 print ' : ' . $langs->trans("AccountancyAreaDescBank", 9, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("BankAccounts") . '</strong>');
230 }
231 } else dol_print_error($db);
232}
233
234// Button to write into Ledger
235if ($some_mandatory_steps_of_setup_were_not_done) {
236 print '<br><div class="warning">' . img_warning() . ' ' . $langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
237 print ' : ' . $langs->trans("AccountancyAreaDescMisc", 4, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("MenuDefaultAccounts") . '</strong>');
238 print '</div>';
239}
240print '<div class="tabsAction tabsActionNoBottom centerimp">';
241if (!empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL) && $in_bookkeeping == 'notyet') {
242 print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />';
243}
244if ($account_accounting_not_defined) {
245 print '<input type="button" class="butActionRefused classfortooltip" title="' . dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")) . '" value="' . $langs->trans("WriteBookKeeping") . '" />';
246} else {
247 if ($in_bookkeeping == 'notyet') {
248 print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
249 } else {
250 print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs->trans("WriteBookKeeping") . '</a>';
251 }
252}
253print '</div>';
254
255// TODO Avoid using js. We can use a direct link with $param
256print '
257 <script type="text/javascript">
258 function launch_export() {
259 $("div.fiche form input[name=\"action\"]").val("exportcsv");
260 $("div.fiche form input[type=\"submit\"]").click();
261 $("div.fiche form input[name=\"action\"]").val("");
262 }
263 function writebookkeeping() {
264 console.log("click on writebookkeeping");
265 $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
266 $("div.fiche form input[type=\"submit\"]").click();
267 $("div.fiche form input[name=\"action\"]").val("");
268 }
269 </script>';
270
271$object_label = $langs->trans("ObjectsRef");
272if ($object->nature == 2 || $object->nature == 3) $object_label = $langs->trans("InvoiceRef");
273if ($object->nature == 5) $object_label = $langs->trans("ExpenseReportRef");
274
275
276// Show result array
277$i = 0;
278
279print '<br>';
280
281print '<div class="div-table-responsive">';
282print '<table class="noborder centpercent">';
283print '<tr class="liste_titre">';
284print '<td>' . $langs->trans("Date") . '</td>';
285print '<td>' . $langs->trans("Piece") . ' (' . $object_label . ')</td>';
286print '<td>' . $langs->trans("AccountAccounting") . '</td>';
287print '<td>' . $langs->trans("SubledgerAccount") . '</td>';
288print '<td>' . $langs->trans("LabelOperation") . '</td>';
289if ($object->nature == 4) print '<td class="center">' . $langs->trans("PaymentMode") . '</td>'; // bank
290print '<td class="right">' . $langs->trans("AccountingDebit") . '</td>';
291print '<td class="right">' . $langs->trans("AccountingCredit") . '</td>';
292print "</tr>\n";
293
294if (is_array($journal_data) && !empty($journal_data)) {
295 foreach ($journal_data as $element_id => $element) {
296 foreach ($element['blocks'] as $lines) {
297 foreach ($lines as $line) {
298 print '<tr class="oddeven">';
299 print '<td>' . $line['date'] . '</td>';
300 print '<td>' . $line['piece'] . '</td>';
301 print '<td>' . $line['account_accounting'] . '</td>';
302 print '<td>' . $line['subledger_account'] . '</td>';
303 print '<td>' . $line['label_operation'] . '</td>';
304 if ($object->nature == 4) print '<td class="center">' . $line['payment_mode'] . '</td>';
305 print '<td class="right nowraponall">' . $line['debit'] . '</td>';
306 print '<td class="right nowraponall">' . $line['credit'] . '</td>';
307 print '</tr>';
308
309 $i++;
310 }
311 }
312 }
313}
314
315if (!$i) {
316 $colspan = 7;
317 if ($object->nature == 4) {
318 $colspan++;
319 }
320 print '<tr class="oddeven"><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
321}
322
323print '</table>';
324print '</div>';
325
326llxFooter();
327
328$db->close();
getDefaultDatesForTransfer()
Return Default dates for transfer based on periodicity option in accountancy setup.
journalHead($nom, $variante, $period, $periodlink, $description, $builddate, $exportlink='', $moreparam=array(), $calcmode='', $varlink='')
Show header of a page used to transfer/dispatch data in accounting.
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:56
llxFooter()
Empty footer.
Definition wrapper.php:70
Class to manage accounting accounts.
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:577
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:596
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...
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
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.