dolibarr  19.0.0-dev
expensereportsjournal.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2010 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2007-2010 Jean Heimburger <jean@tiaris.info>
4  * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
5  * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
6  * Copyright (C) 2013-2021 Alexandre Spangaro <aspangaro@open-dsi.fr>
7  * Copyright (C) 2013-2016 Olivier Geffroy <jeff@jeffinfo.com>
8  * Copyright (C) 2013-2016 Florian Henry <florian.henry@open-concept.pro>
9  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
10  * Copyright (C) 2018 Eric Seigne <eric.seigne@cap-rel.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <https://www.gnu.org/licenses/>.
24  */
25 
31 require '../../main.inc.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
35 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
36 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
37 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
38 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
39 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/bookkeeping.class.php';
40 
41 // Load translation files required by the page
42 $langs->loadLangs(array("commercial", "compta", "bills", "other", "accountancy", "trips", "errors"));
43 
44 $id_journal = GETPOST('id_journal', 'int');
45 $action = GETPOST('action', 'aZ09');
46 
47 $date_startmonth = GETPOST('date_startmonth');
48 $date_startday = GETPOST('date_startday');
49 $date_startyear = GETPOST('date_startyear');
50 $date_endmonth = GETPOST('date_endmonth');
51 $date_endday = GETPOST('date_endday');
52 $date_endyear = GETPOST('date_endyear');
53 $in_bookkeeping = GETPOST('in_bookkeeping');
54 if ($in_bookkeeping == '') {
55  $in_bookkeeping = 'notyet';
56 }
57 
58 $now = dol_now();
59 
60 // Security check
61 if (!isModEnabled('accounting')) {
63 }
64 if ($user->socid > 0) {
66 }
67 if (!$user->hasRight('accounting', 'mouvements', 'lire')) {
69 }
70 
71 
72 /*
73  * Actions
74  */
75 
76 $accountingaccount = new AccountingAccount($db);
77 
78 // Get informations of journal
79 $accountingjournalstatic = new AccountingJournal($db);
80 $accountingjournalstatic->fetch($id_journal);
81 $journal = $accountingjournalstatic->code;
82 $journal_label = $accountingjournalstatic->label;
83 
84 $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
85 $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
86 
87 if (empty($date_startmonth) || empty($date_endmonth)) {
88  // Period by default on transfer
89  $dates = getDefaultDatesForTransfer();
90  $date_start = $dates['date_start'];
91  $date_end = $dates['date_end'];
92  $pastmonthyear = $dates['pastmonthyear'];
93  $pastmonth = $dates['pastmonth'];
94 }
95 
96 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
97  $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
98  $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
99 }
100 
101 $sql = "SELECT er.rowid, er.ref, er.date_debut as de,";
102 $sql .= " erd.rowid as erdid, erd.comments, erd.total_ht, erd.total_tva, erd.total_localtax1, erd.total_localtax2, erd.tva_tx, erd.total_ttc, erd.fk_code_ventilation, erd.vat_src_code, ";
103 $sql .= " u.rowid as uid, u.firstname, u.lastname, u.accountancy_code as user_accountancy_account,";
104 $sql .= " f.accountancy_code, aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte";
105 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport_det as erd";
106 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_fees as f ON f.id = erd.fk_c_type_fees";
107 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = erd.fk_code_ventilation";
108 $sql .= " JOIN ".MAIN_DB_PREFIX."expensereport as er ON er.rowid = erd.fk_expensereport";
109 $sql .= " JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid = er.fk_user_author";
110 $sql .= " WHERE er.fk_statut > 0";
111 $sql .= " AND erd.fk_code_ventilation > 0";
112 $sql .= " AND er.entity IN (".getEntity('expensereport', 0).")"; // We don't share object for accountancy
113 if ($date_start && $date_end) {
114  $sql .= " AND er.date_debut >= '".$db->idate($date_start)."' AND er.date_debut <= '".$db->idate($date_end)."'";
115 }
116 // Define begin binding date
117 if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
118  $sql .= " AND er.date_debut >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
119 }
120 // Already in bookkeeping or not
121 if ($in_bookkeeping == 'already') {
122  $sql .= " AND er.rowid IN (SELECT fk_doc FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as ab WHERE ab.doc_type='expense_report')";
123 }
124 if ($in_bookkeeping == 'notyet') {
125  $sql .= " AND er.rowid NOT IN (SELECT fk_doc FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as ab WHERE ab.doc_type='expense_report')";
126 }
127 $sql .= " ORDER BY er.date_debut";
128 
129 dol_syslog('accountancy/journal/expensereportsjournal.php', LOG_DEBUG);
130 $result = $db->query($sql);
131 if ($result) {
132  $taber = array();
133  $tabht = array();
134  $tabtva = array();
135  $def_tva = array();
136  $tabttc = array();
137  $tablocaltax1 = array();
138  $tablocaltax2 = array();
139  $tabuser = array();
140 
141  $num = $db->num_rows($result);
142 
143  // Variables
144  $account_salary = (!empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT)) ? $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT : 'NotDefined';
145  $account_vat = (!empty($conf->global->ACCOUNTING_VAT_BUY_ACCOUNT)) ? $conf->global->ACCOUNTING_VAT_BUY_ACCOUNT : 'NotDefined';
146 
147  $i = 0;
148  while ($i < $num) {
149  $obj = $db->fetch_object($result);
150 
151  // Controls
152  $compta_user = (!empty($obj->user_accountancy_account)) ? $obj->user_accountancy_account : $account_salary;
153  $compta_fees = $obj->compte;
154 
155  $vatdata = getTaxesFromId($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), $mysoc, $mysoc, 0);
156  $compta_tva = (!empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $account_vat);
157  $compta_localtax1 = (!empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva);
158  $compta_localtax2 = (!empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva);
159 
160  // Define array to display all VAT rates that use this accounting account $compta_tva
161  if (price2num($obj->tva_tx) || !empty($obj->vat_src_code)) {
162  $def_tva[$obj->rowid][$compta_tva][vatrate($obj->tva_tx).($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : '')] = (vatrate($obj->tva_tx).($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''));
163  }
164 
165  $taber[$obj->rowid]["date"] = $db->jdate($obj->de);
166  $taber[$obj->rowid]["ref"] = $obj->ref;
167  $taber[$obj->rowid]["comments"] = $obj->comments;
168  $taber[$obj->rowid]["fk_expensereportdet"] = $obj->erdid;
169 
170  // Avoid warnings
171  if (!isset($tabttc[$obj->rowid][$compta_user])) {
172  $tabttc[$obj->rowid][$compta_user] = 0;
173  }
174  if (!isset($tabht[$obj->rowid][$compta_fees])) {
175  $tabht[$obj->rowid][$compta_fees] = 0;
176  }
177  if (!isset($tabtva[$obj->rowid][$compta_tva])) {
178  $tabtva[$obj->rowid][$compta_tva] = 0;
179  }
180  if (!isset($tablocaltax1[$obj->rowid][$compta_localtax1])) {
181  $tablocaltax1[$obj->rowid][$compta_localtax1] = 0;
182  }
183  if (!isset($tablocaltax2[$obj->rowid][$compta_localtax2])) {
184  $tablocaltax2[$obj->rowid][$compta_localtax2] = 0;
185  }
186 
187  $tabttc[$obj->rowid][$compta_user] += $obj->total_ttc;
188  $tabht[$obj->rowid][$compta_fees] += $obj->total_ht;
189  $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
190  $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
191  $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
192  $tabuser[$obj->rowid] = array(
193  'id' => $obj->uid,
194  'name' => dolGetFirstLastname($obj->firstname, $obj->lastname),
195  'user_accountancy_code' => $obj->user_accountancy_account
196  );
197 
198  $i++;
199  }
200 } else {
201  dol_print_error($db);
202 }
203 
204 // Bookkeeping Write
205 if ($action == 'writebookkeeping') {
206  $now = dol_now();
207  $error = 0;
208 
209  $accountingaccountexpense = new AccountingAccount($db);
210  $accountingaccountexpense->fetch(null, $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT, true);
211 
212  foreach ($taber as $key => $val) { // Loop on each expense report
213  $errorforline = 0;
214 
215  $totalcredit = 0;
216  $totaldebit = 0;
217 
218  $db->begin();
219 
220  // Thirdparty
221  if (!$errorforline) {
222  foreach ($tabttc[$key] as $k => $mt) {
223  if ($mt) {
224  $bookkeeping = new BookKeeping($db);
225  $bookkeeping->doc_date = $val["date"];
226  $bookkeeping->doc_ref = $val["ref"];
227  $bookkeeping->date_creation = $now;
228  $bookkeeping->doc_type = 'expense_report';
229  $bookkeeping->fk_doc = $key;
230  $bookkeeping->fk_docdet = $val["fk_expensereportdet"];
231 
232  $bookkeeping->subledger_account = $tabuser[$key]['user_accountancy_code'];
233  $bookkeeping->subledger_label = $tabuser[$key]['name'];
234 
235  $bookkeeping->numero_compte = $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT;
236  $bookkeeping->label_compte = $accountingaccountexpense->label;
237 
238  $bookkeeping->label_operation = $tabuser[$key]['name'];
239  $bookkeeping->montant = $mt;
240  $bookkeeping->sens = ($mt >= 0) ? 'C' : 'D';
241  $bookkeeping->debit = ($mt <= 0) ? -$mt : 0;
242  $bookkeeping->credit = ($mt > 0) ? $mt : 0;
243  $bookkeeping->code_journal = $journal;
244  $bookkeeping->journal_label = $langs->transnoentities($journal_label);
245  $bookkeeping->fk_user_author = $user->id;
246  $bookkeeping->entity = $conf->entity;
247 
248  $totaldebit += $bookkeeping->debit;
249  $totalcredit += $bookkeeping->credit;
250 
251  $result = $bookkeeping->create($user);
252  if ($result < 0) {
253  if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists
254  $error++;
255  $errorforline++;
256  //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
257  } else {
258  $error++;
259  $errorforline++;
260  setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
261  }
262  }
263  }
264  }
265  }
266 
267  // Fees
268  if (!$errorforline) {
269  foreach ($tabht[$key] as $k => $mt) {
270  if ($mt) {
271  // get compte id and label
272  if ($accountingaccount->fetch(null, $k, true)) {
273  $bookkeeping = new BookKeeping($db);
274  $bookkeeping->doc_date = $val["date"];
275  $bookkeeping->doc_ref = $val["ref"];
276  $bookkeeping->date_creation = $now;
277  $bookkeeping->doc_type = 'expense_report';
278  $bookkeeping->fk_doc = $key;
279  $bookkeeping->fk_docdet = $val["fk_expensereportdet"];
280 
281  $bookkeeping->subledger_account = '';
282  $bookkeeping->subledger_label = '';
283 
284  $bookkeeping->numero_compte = $k;
285  $bookkeeping->label_compte = $accountingaccount->label;
286 
287  $bookkeeping->label_operation = $accountingaccount->label;
288  $bookkeeping->montant = $mt;
289  $bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
290  $bookkeeping->debit = ($mt > 0) ? $mt : 0;
291  $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
292  $bookkeeping->code_journal = $journal;
293  $bookkeeping->journal_label = $langs->transnoentities($journal_label);
294  $bookkeeping->fk_user_author = $user->id;
295  $bookkeeping->entity = $conf->entity;
296 
297  $totaldebit += $bookkeeping->debit;
298  $totalcredit += $bookkeeping->credit;
299 
300  $result = $bookkeeping->create($user);
301  if ($result < 0) {
302  if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists
303  $error++;
304  $errorforline++;
305  //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
306  } else {
307  $error++;
308  $errorforline++;
309  setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
310  }
311  }
312  }
313  }
314  }
315  }
316 
317  // VAT
318  if (!$errorforline) {
319  $listoftax = array(0, 1, 2);
320  foreach ($listoftax as $numtax) {
321  $arrayofvat = $tabtva;
322  if ($numtax == 1) {
323  $arrayofvat = $tablocaltax1;
324  }
325  if ($numtax == 2) {
326  $arrayofvat = $tablocaltax2;
327  }
328 
329  foreach ($arrayofvat[$key] as $k => $mt) {
330  if ($mt) {
331  $accountingaccount->fetch(null, $k, true); // TODO Use a cache for label
332  $account_label = $accountingaccount->label;
333 
334  // get compte id and label
335  $bookkeeping = new BookKeeping($db);
336  $bookkeeping->doc_date = $val["date"];
337  $bookkeeping->doc_ref = $val["ref"];
338  $bookkeeping->date_creation = $now;
339  $bookkeeping->doc_type = 'expense_report';
340  $bookkeeping->fk_doc = $key;
341  $bookkeeping->fk_docdet = $val["fk_expensereportdet"];
342 
343  $bookkeeping->subledger_account = '';
344  $bookkeeping->subledger_label = '';
345 
346  $bookkeeping->numero_compte = $k;
347  $bookkeeping->label_compte = $account_label;
348 
349  $bookkeeping->label_operation = $langs->trans("VAT").' '.join(', ', $def_tva[$key][$k]).' %';
350  $bookkeeping->montant = $mt;
351  $bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
352  $bookkeeping->debit = ($mt > 0) ? $mt : 0;
353  $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
354  $bookkeeping->code_journal = $journal;
355  $bookkeeping->journal_label = $langs->transnoentities($journal_label);
356  $bookkeeping->fk_user_author = $user->id;
357  $bookkeeping->entity = $conf->entity;
358 
359  $totaldebit += $bookkeeping->debit;
360  $totalcredit += $bookkeeping->credit;
361 
362  $result = $bookkeeping->create($user);
363  if ($result < 0) {
364  if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists
365  $error++;
366  $errorforline++;
367  //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
368  } else {
369  $error++;
370  $errorforline++;
371  setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
372  }
373  }
374  }
375  }
376  }
377  }
378 
379  // Protection against a bug on line before
380  if (price2num($totaldebit, 'MT') != price2num($totalcredit, 'MT')) {
381  $error++;
382  $errorforline++;
383  setEventMessages('Try to insert a non balanced transaction in book for '.$val["ref"].'. Canceled. Surely a bug.', null, 'errors');
384  }
385 
386  if (!$errorforline) {
387  $db->commit();
388  } else {
389  $db->rollback();
390 
391  if ($error >= 10) {
392  setEventMessages($langs->trans("ErrorTooManyErrorsProcessStopped"), null, 'errors');
393  break; // Break in the foreach
394  }
395  }
396  }
397 
398  $tabpay = $taber;
399 
400  if (empty($error) && count($tabpay) > 0) {
401  setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
402  } elseif (count($tabpay) == $error) {
403  setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
404  } else {
405  setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
406  }
407 
408  $action = '';
409 
410  // Must reload data, so we make a redirect
411  if (count($tabpay) != $error) {
412  $param = 'id_journal='.$id_journal;
413  $param .= '&date_startday='.$date_startday;
414  $param .= '&date_startmonth='.$date_startmonth;
415  $param .= '&date_startyear='.$date_startyear;
416  $param .= '&date_endday='.$date_endday;
417  $param .= '&date_endmonth='.$date_endmonth;
418  $param .= '&date_endyear='.$date_endyear;
419  $param .= '&in_bookkeeping='.$in_bookkeeping;
420 
421  header("Location: ".$_SERVER['PHP_SELF'].($param ? '?'.$param : ''));
422  exit;
423  }
424 }
425 
426 
427 /*
428  * View
429  */
430 
431 $form = new Form($db);
432 
433 $userstatic = new User($db);
434 
435 // Export
436 if ($action == 'exportcsv') { // ISO and not UTF8 !
437  $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
438 
439  $filename = 'journal';
440  $type_export = 'journal';
441  include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
442 
443  // CSV header line
444  print '"'.$langs->transnoentitiesnoconv("Date").'"'.$sep;
445  print '"'.$langs->transnoentitiesnoconv("Piece").'"'.$sep;
446  print '"'.$langs->transnoentitiesnoconv("AccountAccounting").'"'.$sep;
447  print '"'.$langs->transnoentitiesnoconv("LabelOperation").'"'.$sep;
448  print '"'.$langs->transnoentitiesnoconv("AccountingDebit").'"'.$sep;
449  print '"'.$langs->transnoentitiesnoconv("AccountingCredit").'"'.$sep;
450  print "\n";
451 
452  foreach ($taber as $key => $val) {
453  $date = dol_print_date($val["date"], 'day');
454 
455  $userstatic->id = $tabuser[$key]['id'];
456  $userstatic->name = $tabuser[$key]['name'];
457 
458  // Fees
459  foreach ($tabht[$key] as $k => $mt) {
460  $accountingaccount = new AccountingAccount($db);
461  $accountingaccount->fetch(null, $k, true);
462  if ($mt) {
463  print '"'.$date.'"'.$sep;
464  print '"'.$val["ref"].'"'.$sep;
465  print '"'.length_accountg(html_entity_decode($k)).'"'.$sep;
466  print '"'.dol_trunc($accountingaccount->label, 32).'"'.$sep;
467  print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep;
468  print '"'.($mt < 0 ? price(-$mt) : '').'"';
469  print "\n";
470  }
471  }
472  // VAT
473  foreach ($tabtva[$key] as $k => $mt) {
474  if ($mt) {
475  print '"'.$date.'"'.$sep;
476  print '"'.$val["ref"].'"'.$sep;
477  print '"'.length_accountg(html_entity_decode($k)).'"'.$sep;
478  print '"'.dol_trunc($langs->trans("VAT")).'"'.$sep;
479  print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep;
480  print '"'.($mt < 0 ? price(-$mt) : '').'"';
481  print "\n";
482  }
483  }
484 
485  // Third party
486  foreach ($tabttc[$key] as $k => $mt) {
487  print '"'.$date.'"'.$sep;
488  print '"'.$val["ref"].'"'.$sep;
489  print '"'.length_accounta(html_entity_decode($k)).'"'.$sep;
490  print '"'.dol_trunc($userstatic->name).'"'.$sep;
491  print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep;
492  print '"'.($mt >= 0 ? price($mt) : '').'"';
493  }
494  print "\n";
495  }
496 }
497 
498 if (empty($action) || $action == 'view') {
499  $title = $langs->trans("GenerationOfAccountingEntries").' - '.$accountingjournalstatic->getNomUrl(0, 2, 1, '', 1);
500 
501  llxHeader('', dol_string_nohtmltag($title));
502 
503  $nom = $title;
504  $nomlink = '';
505  $periodlink = '';
506  $exportlink = '';
507  $builddate = dol_now();
508  $description = $langs->trans("DescJournalOnlyBindedVisible").'<br>';
509 
510  $listofchoices = array('notyet'=>$langs->trans("NotYetInGeneralLedger"), 'already'=>$langs->trans("AlreadyInGeneralLedger"));
511  $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);
512  $period .= ' - '.$langs->trans("JournalizationInLedgerStatus").' '.$form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
513 
514  $varlink = 'id_journal='.$id_journal;
515 
516  journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
517 
518  // Button to write into Ledger
519  if (empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1') {
520  print '<br><div class="warning">'.img_warning().' '.$langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
521  $desc = ' : '.$langs->trans("AccountancyAreaDescMisc", 4, '{link}');
522  $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("MenuDefaultAccounts").'</strong>', $desc);
523  print $desc;
524  print '</div>';
525  }
526  print '<div class="tabsAction tabsActionNoBottom centerimp">';
527 
528  if (!empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL) && $in_bookkeeping == 'notyet') {
529  print '<input type="button" class="butAction" name="exportcsv" value="'.$langs->trans("ExportDraftJournal").'" onclick="launch_export();" />';
530  }
531  if (empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1') {
532  print '<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")).'" value="'.$langs->trans("WriteBookKeeping").'" />';
533  } else {
534  if ($in_bookkeeping == 'notyet') {
535  print '<input type="button" class="butAction" name="writebookkeeping" value="'.$langs->trans("WriteBookKeeping").'" onclick="writebookkeeping();" />';
536  } else {
537  print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">'.$langs->trans("WriteBookKeeping").'</a>';
538  }
539  }
540  print '</div>';
541 
542  // TODO Avoid using js. We can use a direct link with $param
543  print '
544  <script type="text/javascript">
545  function launch_export() {
546  $("div.fiche form input[name=\"action\"]").val("exportcsv");
547  $("div.fiche form input[type=\"submit\"]").click();
548  $("div.fiche form input[name=\"action\"]").val("");
549  }
550  function writebookkeeping() {
551  console.log("click on writebookkeeping");
552  $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
553  $("div.fiche form input[type=\"submit\"]").click();
554  $("div.fiche form input[name=\"action\"]").val("");
555  }
556  </script>';
557 
558  /*
559  * Show result array
560  */
561  print '<br>';
562 
563  $i = 0;
564  print '<div class="div-table-responsive">';
565  print "<table class=\"noborder\" width=\"100%\">";
566  print "<tr class=\"liste_titre\">";
567  print "<td>".$langs->trans("Date")."</td>";
568  print "<td>".$langs->trans("Piece").' ('.$langs->trans("ExpenseReportRef").")</td>";
569  print "<td>".$langs->trans("AccountAccounting")."</td>";
570  print "<td>".$langs->trans("SubledgerAccount")."</td>";
571  print "<td>".$langs->trans("LabelOperation")."</td>";
572  print '<td class="right">'.$langs->trans("AccountingDebit")."</td>";
573  print '<td class="right">'.$langs->trans("AccountingCredit")."</td>";
574  print "</tr>\n";
575 
576  $r = '';
577 
578  $expensereportstatic = new ExpenseReport($db);
579  $expensereportlinestatic = new ExpenseReportLine($db);
580 
581  foreach ($taber as $key => $val) {
582  $expensereportstatic->id = $key;
583  $expensereportstatic->ref = $val["ref"];
584  $expensereportlinestatic->comments = html_entity_decode(dol_trunc($val["comments"], 32));
585 
586  $date = dol_print_date($val["date"], 'day');
587 
588  // Fees
589  foreach ($tabht[$key] as $k => $mt) {
590  $accountingaccount = new AccountingAccount($db);
591  $accountingaccount->fetch(null, $k, true);
592 
593  if ($mt) {
594  print '<tr class="oddeven">';
595  print "<!-- Fees -->";
596  print "<td>".$date."</td>";
597  print "<td>".$expensereportstatic->getNomUrl(1)."</td>";
598  $userstatic->id = $tabuser[$key]['id'];
599  $userstatic->name = $tabuser[$key]['name'];
600  // Account
601  print "<td>";
602  $accountoshow = length_accountg($k);
603  if (($accountoshow == "") || $accountoshow == 'NotDefined') {
604  print '<span class="error">'.$langs->trans("FeeAccountNotDefined").'</span>';
605  } else {
606  print $accountoshow;
607  }
608  print '</td>';
609  // Subledger account
610  print "<td>";
611  print '</td>';
612  $userstatic->id = $tabuser[$key]['id'];
613  $userstatic->name = $tabuser[$key]['name'];
614  print "<td>".$userstatic->getNomUrl(0, 'user', 16).' - '.$accountingaccount->label."</td>";
615  print '<td class="right nowraponall amount">'.($mt >= 0 ? price($mt) : '')."</td>";
616  print '<td class="right nowraponall amount">'.($mt < 0 ? price(-$mt) : '')."</td>";
617  print "</tr>";
618 
619  $i++;
620  }
621  }
622 
623  // Third party
624  foreach ($tabttc[$key] as $k => $mt) {
625  $userstatic->id = $tabuser[$key]['id'];
626  $userstatic->name = $tabuser[$key]['name'];
627 
628  print '<tr class="oddeven">';
629  print "<!-- Thirdparty -->";
630  print "<td>".$date."</td>";
631  print "<td>".$expensereportstatic->getNomUrl(1)."</td>";
632  // Account
633  print "<td>";
634  $accountoshow = length_accountg($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT);
635  if (($accountoshow == "") || $accountoshow == 'NotDefined') {
636  print '<span class="error">'.$langs->trans("MainAccountForUsersNotDefined").'</span>';
637  } else {
638  print $accountoshow;
639  }
640  print "</td>";
641  // Subledger account
642  print "<td>";
643  $accountoshow = length_accounta($k);
644  if (($accountoshow == "") || $accountoshow == 'NotDefined') {
645  print '<span class="error">'.$langs->trans("UserAccountNotDefined").'</span>';
646  } else {
647  print $accountoshow;
648  }
649  print '</td>';
650  print "<td>".$userstatic->getNomUrl(0, 'user', 16).' - '.$langs->trans("SubledgerAccount")."</td>";
651  print '<td class="right nowraponall amount">'.($mt < 0 ? price(-$mt) : '')."</td>";
652  print '<td class="right nowraponall amount">'.($mt >= 0 ? price($mt) : '')."</td>";
653  print "</tr>";
654 
655  $i++;
656  }
657 
658  // VAT
659  $listoftax = array(0, 1, 2);
660  foreach ($listoftax as $numtax) {
661  $arrayofvat = $tabtva;
662  if ($numtax == 1) {
663  $arrayofvat = $tablocaltax1;
664  }
665  if ($numtax == 2) {
666  $arrayofvat = $tablocaltax2;
667  }
668 
669  foreach ($arrayofvat[$key] as $k => $mt) {
670  if ($mt) {
671  print '<tr class="oddeven">';
672  print "<!-- VAT -->";
673  print "<td>".$date."</td>";
674  print "<td>".$expensereportstatic->getNomUrl(1)."</td>";
675  // Account
676  print "<td>";
677  $accountoshow = length_accountg($k);
678  if (($accountoshow == "") || $accountoshow == 'NotDefined') {
679  print '<span class="error">'.$langs->trans("VATAccountNotDefined").'</span>';
680  } else {
681  print $accountoshow;
682  }
683  print "</td>";
684  // Subledger account
685  print "<td>";
686  print '</td>';
687  print "<td>".$userstatic->getNomUrl(0, 'user', 16).' - '.$langs->trans("VAT").' '.join(', ', $def_tva[$key][$k]).' %'.($numtax ? ' - Localtax '.$numtax : '');
688  print "</td>";
689  print '<td class="right nowraponall amount">'.($mt >= 0 ? price($mt) : '')."</td>";
690  print '<td class="right nowraponall amount">'.($mt < 0 ? price(-$mt) : '')."</td>";
691  print "</tr>";
692 
693  $i++;
694  }
695  }
696  }
697  }
698 
699  if (!$i) {
700  $colspan = 7;
701  print '<tr class="oddeven"><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
702  }
703 
704  print "</table>";
705  print '</div>';
706 
707  // End of page
708  llxFooter();
709 }
710 $db->close();
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
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.
length_accounta($accounta)
Return Auxiliary accounting account of thirdparties with defined length.
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 accounting journals.
Class to manage Ledger (General Ledger and Subledger)
Class to manage Trips and Expenses.
Class of expense report details lines.
Class to manage generation of HTML components Only common components must be here.
Class to manage Dolibarr users.
Definition: user.class.php:48
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->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') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:576
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:595
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...
vatrate($rate, $addpercent=false, $info_bits=0, $usestarfornpr=0, $html=0)
Return a string with VAT rate label formated for view output Used into pdf and HTML pages.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getTaxesFromId($vatrate, $buyer=null, $seller=null, $firstparamisid=1)
Get tax (VAT) main information from Id.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
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_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.