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