dolibarr  20.0.0-beta
variousjournal.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2021-2023 Alexandre Spangaro <aspangaro@easya.solutions>
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
25 require '../../main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
28 require_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 = GETPOSTINT('id_journal');
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');
43 if ($in_bookkeeping == '') {
44  $in_bookkeeping = 'notyet';
45 }
46 
47 // Get information of journal
48 $object = new AccountingJournal($db);
49 $result = $object->fetch($id_journal);
50 if ($result > 0) {
51  $id_journal = $object->id;
52 } elseif ($result < 0) {
53  dol_print_error(null, $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 
64 if (empty($date_startmonth)) {
65  // Period by default on transfer
66  $dates = getDefaultDatesForTransfer();
67  $date_start = $dates['date_start'];
68  $pastmonthyear = $dates['pastmonthyear'];
69  $pastmonth = $dates['pastmonth'];
70 }
71 if (empty($date_endmonth)) {
72  // Period by default on transfer
73  $dates = getDefaultDatesForTransfer();
74  $date_end = $dates['date_end'];
75  $pastmonthyear = $dates['pastmonthyear'];
76  $pastmonth = $dates['pastmonth'];
77 }
78 
79 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
80  $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
81  $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
82 }
83 
84 $data_type = 'view';
85 if ($action == 'writebookkeeping') {
86  $data_type = 'bookkeeping';
87 }
88 if ($action == 'exportcsv') {
89  $data_type = 'csv';
90 }
91 $journal_data = $object->getData($user, $data_type, $date_start, $date_end, $in_bookkeeping);
92 if (!is_array($journal_data)) {
93  setEventMessages($object->error, $object->errors, 'errors');
94 }
95 
96 // Security check
97 if (!isModEnabled('accounting')) {
99 }
100 if ($user->socid > 0) {
101  accessforbidden();
102 }
103 if (!$user->hasRight('accounting', 'bind', 'write')) {
104  accessforbidden();
105 }
106 
107 
108 /*
109  * Actions
110  */
111 
112 $reshook = $hookmanager->executeHooks('doActions', $parameters, $user, $action); // Note that $action and $object may have been modified by some hooks
113 
114 $reload = false;
115 
116 // Bookkeeping Write
117 if ($action == 'writebookkeeping') {
118  $error = 0;
119 
120  $result = $object->writeIntoBookkeeping($user, $journal_data);
121  if ($result < 0) {
122  setEventMessages($object->error, $object->errors, 'errors');
123  $error = abs($result);
124  }
125 
126  $nb_elements = count($journal_data);
127  if (empty($error) && $nb_elements > 0) {
128  setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
129  } elseif ($nb_elements == $error) {
130  setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
131  } else {
132  setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
133  }
134 
135  $reload = true;
136 } elseif ($action == 'exportcsv') {
137  // Export CSV
138  $result = $object->exportCsv($journal_data, $date_end);
139  if ($result < 0) {
140  setEventMessages($object->error, $object->errors, 'errors');
141  $reload = true;
142  } else {
143  $filename = 'journal';
144  $type_export = 'journal';
145 
146  require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
147  include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
148 
149  print $result;
150 
151  $db->close();
152  exit();
153  }
154 }
155 
156 // Must reload data, so we make a redirect
157 if ($reload) {
158  $param = 'id_journal=' . $id_journal;
159  $param .= '&date_startday=' . $date_startday;
160  $param .= '&date_startmonth=' . $date_startmonth;
161  $param .= '&date_startyear=' . $date_startyear;
162  $param .= '&date_endday=' . $date_endday;
163  $param .= '&date_endmonth=' . $date_endmonth;
164  $param .= '&date_endyear=' . $date_endyear;
165  $param .= '&in_bookkeeping=' . $in_bookkeeping;
166  header("Location: " . $_SERVER['PHP_SELF'] . ($param ? '?' . $param : ''));
167  exit;
168 }
169 
170 
171 /*
172  * View
173  */
174 
175 $form = new Form($db);
176 
177 if ($object->nature == 2) {
178  $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1';
179  $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1';
180 } elseif ($object->nature == 3) {
181  $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1';
182  $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1';
183 } elseif ($object->nature == 4) {
184  $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1'
185  || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1'
186  || !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1';
187  $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1'
188  || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1';
189 } elseif ($object->nature == 5) {
190  $some_mandatory_steps_of_setup_were_not_done = !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1';
191  $account_accounting_not_defined = !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1';
192 } else {
193  $title = $object->getLibType();
194  $some_mandatory_steps_of_setup_were_not_done = false;
195  $account_accounting_not_defined = false;
196 }
197 
198 $title = $langs->trans("GenerationOfAccountingEntries") . ' - ' . $object->getNomUrl(0, 2, 1, '', 1);
199 
200 llxHeader('', dol_string_nohtmltag($title));
201 
202 $nom = $title;
203 $nomlink = '';
204 $periodlink = '';
205 $exportlink = '';
206 $builddate = dol_now();
207 $description = $langs->trans("DescJournalOnlyBindedVisible") . '<br>';
208 if ($object->nature == 2 || $object->nature == 3) {
209  if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
210  $description .= $langs->trans("DepositsAreNotIncluded");
211  } else {
212  $description .= $langs->trans("DepositsAreIncluded");
213  }
214  if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
215  $description .= $langs->trans("SupplierDepositsAreNotIncluded");
216  }
217 }
218 
219 $listofchoices = array('notyet' => $langs->trans("NotYetInGeneralLedger"), 'already' => $langs->trans("AlreadyInGeneralLedger"));
220 $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);
221 $period .= ' - ' . $langs->trans("JournalizationInLedgerStatus") . ' ' . $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
222 
223 $varlink = 'id_journal=' . $id_journal;
224 
225 journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
226 
227 if (getDolGlobalString('ACCOUNTANCY_FISCAL_PERIOD_MODE') != 'blockedonclosed') {
228  // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed)
229  // Fiscal period test
230  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_fiscalyear WHERE entity = ".((int) $conf->entity);
231  $resql = $db->query($sql);
232  if ($resql) {
233  $obj = $db->fetch_object($resql);
234  if ($obj->nb == 0) {
235  print '<br><div class="warning">'.img_warning().' '.$langs->trans("TheFiscalPeriodIsNotDefined");
236  $desc = ' : '.$langs->trans("AccountancyAreaDescFiscalPeriod", 4, '{link}');
237  $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("FiscalPeriod").'</strong>', $desc);
238  print $desc;
239  print '</div>';
240  }
241  } else {
242  dol_print_error($db);
243  }
244 }
245 
246 if ($object->nature == 4) { // Bank journal
247  // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed)
248  $sql = "SELECT COUNT(rowid) as nb";
249  $sql .= " FROM " . MAIN_DB_PREFIX . "bank_account";
250  $sql .= " WHERE entity = " . (int) $conf->entity;
251  $sql .= " AND fk_accountancy_journal IS NULL";
252  $sql .= " AND clos=0";
253  $resql = $db->query($sql);
254  if ($resql) {
255  $obj = $db->fetch_object($resql);
256  if ($obj->nb > 0) {
257  print '<br>' . img_warning() . ' ' . $langs->trans("TheJournalCodeIsNotDefinedOnSomeBankAccount");
258  print ' : ' . $langs->trans("AccountancyAreaDescBank", 9, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("BankAccounts") . '</strong>');
259  }
260  } else {
261  dol_print_error($db);
262  }
263 }
264 
265 // Button to write into Ledger
266 if ($some_mandatory_steps_of_setup_were_not_done) {
267  print '<br><div class="warning">' . img_warning() . ' ' . $langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
268  print ' : ' . $langs->trans("AccountancyAreaDescMisc", 4, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("MenuDefaultAccounts") . '</strong>');
269  print '</div>';
270 }
271 print '<br><div class="tabsAction tabsActionNoBottom centerimp">';
272 if (getDolGlobalString('ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL') && $in_bookkeeping == 'notyet') {
273  print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />';
274 }
275 if ($account_accounting_not_defined) {
276  print '<input type="button" class="butActionRefused classfortooltip" title="' . dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")) . '" value="' . $langs->trans("WriteBookKeeping") . '" />';
277 } else {
278  if ($in_bookkeeping == 'notyet') {
279  print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
280  } else {
281  print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs->trans("WriteBookKeeping") . '</a>';
282  }
283 }
284 print '</div>';
285 
286 // TODO Avoid using js. We can use a direct link with $param
287 print '
288  <script type="text/javascript">
289  function launch_export() {
290  $("div.fiche form input[name=\"action\"]").val("exportcsv");
291  $("div.fiche form input[type=\"submit\"]").click();
292  $("div.fiche form input[name=\"action\"]").val("");
293  }
294  function writebookkeeping() {
295  console.log("click on writebookkeeping");
296  $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
297  $("div.fiche form input[type=\"submit\"]").click();
298  $("div.fiche form input[name=\"action\"]").val("");
299  }
300  </script>';
301 
302 $object_label = $langs->trans("ObjectsRef");
303 if ($object->nature == 2 || $object->nature == 3) {
304  $object_label = $langs->trans("InvoiceRef");
305 }
306 if ($object->nature == 5) {
307  $object_label = $langs->trans("ExpenseReportRef");
308 }
309 
310 
311 // Show result array
312 $i = 0;
313 
314 print '<br>';
315 
316 print '<div class="div-table-responsive">';
317 print '<table class="noborder centpercent">';
318 print '<tr class="liste_titre">';
319 print '<td>' . $langs->trans("Date") . '</td>';
320 print '<td>' . $langs->trans("Piece") . ' (' . $object_label . ')</td>';
321 print '<td>' . $langs->trans("AccountAccounting") . '</td>';
322 print '<td>' . $langs->trans("SubledgerAccount") . '</td>';
323 print '<td>' . $langs->trans("LabelOperation") . '</td>';
324 if ($object->nature == 4) {
325  print '<td class="center">' . $langs->trans("PaymentMode") . '</td>';
326 } // bank
327 print '<td class="right">' . $langs->trans("AccountingDebit") . '</td>';
328 print '<td class="right">' . $langs->trans("AccountingCredit") . '</td>';
329 print "</tr>\n";
330 
331 if (is_array($journal_data) && !empty($journal_data)) {
332  foreach ($journal_data as $element_id => $element) {
333  foreach ($element['blocks'] as $lines) {
334  foreach ($lines as $line) {
335  print '<tr class="oddeven">';
336  print '<td>' . $line['date'] . '</td>';
337  print '<td>' . $line['piece'] . '</td>';
338  print '<td>' . $line['account_accounting'] . '</td>';
339  print '<td>' . $line['subledger_account'] . '</td>';
340  print '<td>' . $line['label_operation'] . '</td>';
341  if ($object->nature == 4) {
342  print '<td class="center">' . $line['payment_mode'] . '</td>';
343  }
344  print '<td class="right nowraponall">' . $line['debit'] . '</td>';
345  print '<td class="right nowraponall">' . $line['credit'] . '</td>';
346  print '</tr>';
347 
348  $i++;
349  }
350  }
351  }
352 }
353 
354 if (!$i) {
355  $colspan = 7;
356  if ($object->nature == 4) {
357  $colspan++;
358  }
359  print '<tr class="oddeven"><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
360 }
361 
362 print '</table>';
363 print '</div>';
364 
365 llxFooter();
366 
367 $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()
Empty header.
Definition: wrapper.php:55
llxFooter()
Empty footer.
Definition: wrapper.php:69
Class to manage accounting journals.
Class to manage generation of HTML components Only common components must be here.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:595
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:614
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...
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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 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.