dolibarr  16.0.5
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 require '../../main.inc.php';
25 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
27 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
28 
29 // Load translation files required by the page
30 $langs->loadLangs(array("banks", "accountancy", "compta", "other", "errors"));
31 
32 $id_journal = GETPOST('id_journal', 'int');
33 $action = GETPOST('action', 'aZ09');
34 
35 $date_startmonth = GETPOST('date_startmonth');
36 $date_startday = GETPOST('date_startday');
37 $date_startyear = GETPOST('date_startyear');
38 $date_endmonth = GETPOST('date_endmonth');
39 $date_endday = GETPOST('date_endday');
40 $date_endyear = GETPOST('date_endyear');
41 $in_bookkeeping = GETPOST('in_bookkeeping');
42 if ($in_bookkeeping == '') {
43  $in_bookkeeping = 'notyet';
44 }
45 
46 // Get information of journal
47 $object = new AccountingJournal($db);
48 $result = $object->fetch($id_journal);
49 if ($result > 0) {
50  $id_journal = $object->id;
51 } elseif ($result < 0) {
52  dol_print_error('', $object->error, $object->errors);
53 } elseif ($result == 0) {
54  accessforbidden($langs->trans('ErrorRecordNotFound'));
55 }
56 
57 $hookmanager->initHooks(array('globaljournal', $object->nature.'journal'));
58 $parameters = array();
59 
60 $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
61 $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
62 
63 if (empty($date_startmonth) || empty($date_endmonth)) {
64  // Period by default on transfer
65  $dates = getDefaultDatesForTransfer();
66  $date_start = $dates['date_start'];
67  $date_end = $dates['date_end'];
68  $pastmonthyear = $dates['pastmonthyear'];
69  $pastmonth = $dates['pastmonth'];
70 }
71 
72 if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) { // We define date_start and date_end, only if we did not submit the form
73  $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
74  $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
75 }
76 
77 $data_type = 'view';
78 if ($action == 'writebookkeeping') $data_type = 'bookkeeping';
79 if ($action == 'exportcsv') $data_type = 'csv';
80 $journal_data = $object->getData($user, $data_type, $date_start, $date_end, $in_bookkeeping);
81 if (!is_array($journal_data)) {
82  setEventMessages($object->error, $object->errors, 'errors');
83 }
84 
85 // Security check
86 if (!isModEnabled('accounting')) {
88 }
89 if ($user->socid > 0) {
91 }
92 if (empty($user->rights->accounting->mouvements->lire)) {
94 }
95 
96 
97 /*
98  * Actions
99  */
100 
101 $reshook = $hookmanager->executeHooks('doActions', $parameters, $user, $action); // Note that $action and $object may have been modified by some hooks
102 
103 $reload = false;
104 
105 // Bookkeeping Write
106 if ($action == 'writebookkeeping') {
107  $error = 0;
108 
109  $result = $object->writeIntoBookkeeping($user, $journal_data);
110  if ($result < 0) {
111  setEventMessages($object->error, $object->errors, 'errors');
112  $error = abs($result);
113  }
114 
115  $nb_elements = count($journal_data);
116  if (empty($error) && $nb_elements > 0) {
117  setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
118  } elseif ($nb_elements == $error) {
119  setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
120  } else {
121  setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
122  }
123 
124  $reload = true;
125 } elseif ($action == 'exportcsv') {
126  // Export CSV
127  $result = $object->exportCsv($journal_data, $date_end);
128  if ($result < 0) {
129  setEventMessages($object->error, $object->errors, 'errors');
130  $reload = true;
131  } else {
132  $filename = 'journal';
133  $type_export = 'journal';
134 
135  require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
136  include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
137 
138  print $result;
139 
140  $db->close();
141  exit();
142  }
143 }
144 
145 // Must reload data, so we make a redirect
146 if ($reload) {
147  $param = 'id_journal=' . $id_journal;
148  $param .= '&date_startday=' . $date_startday;
149  $param .= '&date_startmonth=' . $date_startmonth;
150  $param .= '&date_startyear=' . $date_startyear;
151  $param .= '&date_endday=' . $date_endday;
152  $param .= '&date_endmonth=' . $date_endmonth;
153  $param .= '&date_endyear=' . $date_endyear;
154  $param .= '&in_bookkeeping=' . $in_bookkeeping;
155  header("Location: " . $_SERVER['PHP_SELF'] . ($param ? '?' . $param : ''));
156  exit;
157 }
158 
159 
160 /*
161  * View
162  */
163 
164 $form = new Form($db);
165 
166 if ($object->nature == 2) {
167  $title = $langs->trans("SellsJournal");
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  $title = $langs->trans("PurchasesJournal");
172  $some_mandatory_steps_of_setup_were_not_done = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1';
173  $account_accounting_not_defined = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1';
174 } elseif ($object->nature == 4) {
175  $title = $langs->trans("FinanceJournal");
176  $some_mandatory_steps_of_setup_were_not_done = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1'
177  || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1'
178  || empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1';
179  $account_accounting_not_defined = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1'
180  || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1';
181 } elseif ($object->nature == 5) {
182  $title = $langs->trans("ExpenseReportsJournal");
183  $some_mandatory_steps_of_setup_were_not_done = empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1';
184  $account_accounting_not_defined = empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1';
185 } else {
186  $title = $object->getLibType();
187  $some_mandatory_steps_of_setup_were_not_done = false;
188  $account_accounting_not_defined = false;
189 }
190 
191 
192 $nom = $title . ' | ' . $object->getNomUrl(0, 1, 1, '', 1);
193 $nomlink = '';
194 $periodlink = '';
195 $exportlink = '';
196 $builddate = dol_now();
197 $description = $langs->trans("DescJournalOnlyBindedVisible") . '<br>';
198 if ($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 
215 llxHeader('', $title);
216 
217 journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
218 
219 if ($object->nature == 4) { // Bank journal
220  // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed)
221  $sql = "SELECT COUNT(rowid) as nb";
222  $sql .= " FROM " . MAIN_DB_PREFIX . "bank_account";
223  $sql .= " WHERE entity = " . (int) $conf->entity;
224  $sql .= " AND fk_accountancy_journal IS NULL";
225  $sql .= " AND clos=0";
226  $resql = $db->query($sql);
227  if ($resql) {
228  $obj = $db->fetch_object($resql);
229  if ($obj->nb > 0) {
230  print '<br>' . img_warning() . ' ' . $langs->trans("TheJournalCodeIsNotDefinedOnSomeBankAccount");
231  print ' : ' . $langs->trans("AccountancyAreaDescBank", 9, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("BankAccounts") . '</strong>');
232  }
233  } else dol_print_error($db);
234 }
235 
236 // Button to write into Ledger
237 if ($some_mandatory_steps_of_setup_were_not_done) {
238  print '<br><div class="warning">' . img_warning() . ' ' . $langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
239  print ' : ' . $langs->trans("AccountancyAreaDescMisc", 4, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("MenuDefaultAccounts") . '</strong>');
240  print '</div>';
241 }
242 print '<div class="tabsAction tabsActionNoBottom centerimp">';
243 if (!empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL) && $in_bookkeeping == 'notyet') {
244  print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />';
245 }
246 if ($account_accounting_not_defined) {
247  print '<input type="button" class="butActionRefused classfortooltip" title="' . dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")) . '" value="' . $langs->trans("WriteBookKeeping") . '" />';
248 } else {
249  if ($in_bookkeeping == 'notyet') {
250  print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
251  } else {
252  print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs->trans("WriteBookKeeping") . '</a>';
253  }
254 }
255 print '</div>';
256 
257 // TODO Avoid using js. We can use a direct link with $param
258 print '
259  <script type="text/javascript">
260  function launch_export() {
261  $("div.fiche form input[name=\"action\"]").val("exportcsv");
262  $("div.fiche form input[type=\"submit\"]").click();
263  $("div.fiche form input[name=\"action\"]").val("");
264  }
265  function writebookkeeping() {
266  console.log("click on writebookkeeping");
267  $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
268  $("div.fiche form input[type=\"submit\"]").click();
269  $("div.fiche form input[name=\"action\"]").val("");
270  }
271  </script>';
272 
273 $object_label = $langs->trans("ObjectsRef");
274 if ($object->nature == 2 || $object->nature == 3) $object_label = $langs->trans("InvoiceRef");
275 if ($object->nature == 5) $object_label = $langs->trans("ExpenseReportRef");
276 
277 /*
278  * Show result array
279  */
280 print '<br>';
281 
282 print '<div class="div-table-responsive">';
283 print '<table class="noborder centpercent">';
284 print '<tr class="liste_titre">';
285 print '<td>' . $langs->trans("Date") . '</td>';
286 print '<td>' . $langs->trans("Piece") . ' (' . $object_label . ')</td>';
287 print '<td>' . $langs->trans("AccountAccounting") . '</td>';
288 print '<td>' . $langs->trans("SubledgerAccount") . '</td>';
289 print '<td>' . $langs->trans("LabelOperation") . '</td>';
290 if ($object->nature == 4) print '<td class="center">' . $langs->trans("PaymentMode") . '</td>'; // bank
291 print '<td class="right">' . $langs->trans("Debit") . '</td>';
292 print '<td class="right">' . $langs->trans("Credit") . '</td>';
293 print "</tr>\n";
294 
295 if (is_array($journal_data) && !empty($journal_data)) {
296  foreach ($journal_data as $element_id => $element) {
297  foreach ($element['blocks'] as $lines) {
298  foreach ($lines as $line) {
299  print '<tr class="oddeven">';
300  print '<td>' . $line['date'] . '</td>';
301  print '<td>' . $line['piece'] . '</td>';
302  print '<td>' . $line['account_accounting'] . '</td>';
303  print '<td>' . $line['subledger_account'] . '</td>';
304  print '<td>' . $line['label_operation'] . '</td>';
305  if ($object->nature == 4) print '<td class="center">' . $line['payment_mode'] . '</td>';
306  print '<td class="right nowraponall">' . $line['debit'] . '</td>';
307  print '<td class="right nowraponall">' . $line['credit'] . '</td>';
308  print '</tr>';
309  }
310  }
311  }
312 }
313 
314 print '</table>';
315 print '</div>';
316 
317 llxFooter();
318 
319 $db->close();
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1468
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
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:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
img_warning
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
Definition: functions.lib.php:4521
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
AccountingJournal
Class to manage accounting accounts.
Definition: accountingjournal.class.php:27
dol_get_first_day
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:551
journalHead
journalHead($nom, $variante, $period, $periodlink, $description, $builddate, $exportlink='', $moreparam=array(), $calcmode='', $varlink='')
Show header of a page used to transfer/dispatch data in accounting.
Definition: accounting.lib.php:183
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:570
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:105
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
getDefaultDatesForTransfer
getDefaultDatesForTransfer()
Return Default dates for transfer based on periodicity option in accountancy setup.
Definition: accounting.lib.php:273
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
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:2757
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59