expensereportsjournal.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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@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. /**
  26. * \file htdocs/accountancy/journal/expensereportsjournal.php
  27. * \ingroup Accountancy (Double entries)
  28. * \brief Page with expense reports journal
  29. */
  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. // Load translation files required by the page
  40. $langs->loadLangs(array("commercial", "compta","bills","other","accountancy","trips","errors"));
  41. $id_journal = GETPOST('id_journal', 'int');
  42. $action = GETPOST('action', 'aZ09');
  43. $date_startmonth = GETPOST('date_startmonth');
  44. $date_startday = GETPOST('date_startday');
  45. $date_startyear = GETPOST('date_startyear');
  46. $date_endmonth = GETPOST('date_endmonth');
  47. $date_endday = GETPOST('date_endday');
  48. $date_endyear = GETPOST('date_endyear');
  49. $in_bookkeeping = GETPOST('in_bookkeeping');
  50. if ($in_bookkeeping == '') $in_bookkeeping = 'notyet';
  51. $now = dol_now();
  52. // Security check
  53. if ($user->societe_id > 0)
  54. accessforbidden();
  55. /*
  56. * Actions
  57. */
  58. $accountingaccount = new AccountingAccount($db);
  59. // Get informations of journal
  60. $accountingjournalstatic = new AccountingJournal($db);
  61. $accountingjournalstatic->fetch($id_journal);
  62. $journal = $accountingjournalstatic->code;
  63. $journal_label = $accountingjournalstatic->label;
  64. $year_current = strftime("%Y", dol_now());
  65. $pastmonth = strftime("%m", dol_now()) - 1;
  66. $pastmonthyear = $year_current;
  67. if ($pastmonth == 0) {
  68. $pastmonth = 12;
  69. $pastmonthyear --;
  70. }
  71. $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  72. $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  73. 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
  74. {
  75. $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
  76. $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
  77. }
  78. $sql = "SELECT er.rowid, er.ref, er.date_debut as de,";
  79. $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, ";
  80. $sql .= " u.rowid as uid, u.firstname, u.lastname, u.accountancy_code as user_accountancy_account,";
  81. $sql .= " f.accountancy_code, aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte";
  82. $sql .= " FROM " . MAIN_DB_PREFIX . "expensereport_det as erd";
  83. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_type_fees as f ON f.id = erd.fk_c_type_fees";
  84. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = erd.fk_code_ventilation";
  85. $sql .= " JOIN " . MAIN_DB_PREFIX . "expensereport as er ON er.rowid = erd.fk_expensereport";
  86. $sql .= " JOIN " . MAIN_DB_PREFIX . "user as u ON u.rowid = er.fk_user_author";
  87. $sql .= " WHERE er.fk_statut > 0";
  88. $sql .= " AND erd.fk_code_ventilation > 0";
  89. $sql .= " AND er.entity IN (" . getEntity('expensereport', 0) . ")"; // We don't share object for accountancy
  90. if ($date_start && $date_end)
  91. $sql .= " AND er.date_debut >= '" . $db->idate($date_start) . "' AND er.date_debut <= '" . $db->idate($date_end) . "'";
  92. // Already in bookkeeping or not
  93. if ($in_bookkeeping == 'already')
  94. {
  95. $sql .= " AND er.rowid IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='expense_report')";
  96. }
  97. if ($in_bookkeeping == 'notyet')
  98. {
  99. $sql .= " AND er.rowid NOT IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='expense_report')";
  100. }
  101. $sql .= " ORDER BY er.date_debut";
  102. dol_syslog('accountancy/journal/expensereportsjournal.php', LOG_DEBUG);
  103. $result = $db->query($sql);
  104. if ($result) {
  105. $taber = array ();
  106. $tabht = array ();
  107. $tabtva = array ();
  108. $def_tva = array ();
  109. $tabttc = array ();
  110. $tablocaltax1 = array ();
  111. $tablocaltax2 = array ();
  112. $tabuser = array ();
  113. $num = $db->num_rows($result);
  114. // Variables
  115. $account_salary = (! empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT)) ? $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT : 'NotDefined';
  116. $account_vat = (! empty($conf->global->ACCOUNTING_VAT_BUY_ACCOUNT)) ? $conf->global->ACCOUNTING_VAT_BUY_ACCOUNT : 'NotDefined';
  117. $i = 0;
  118. while ( $i < $num ) {
  119. $obj = $db->fetch_object($result);
  120. // Controls
  121. $compta_user = (! empty($obj->user_accountancy_account)) ? $obj->user_accountancy_account : $account_salary;
  122. $compta_fees = $obj->compte;
  123. $vatdata = getTaxesFromId($obj->tva_tx.($obj->vat_src_code?' ('.$obj->vat_src_code.')':''), $mysoc, $mysoc, 0);
  124. $compta_tva = (! empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $account_vat);
  125. $compta_localtax1 = (! empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva);
  126. $compta_localtax2 = (! empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva);
  127. // Define array to display all VAT rates that use this accounting account $compta_tva
  128. if (price2num($obj->tva_tx) || ! empty($obj->vat_src_code))
  129. {
  130. $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.')':''));
  131. }
  132. $taber[$obj->rowid]["date"] = $db->jdate($obj->de);
  133. $taber[$obj->rowid]["ref"] = $obj->ref;
  134. $taber[$obj->rowid]["comments"] = $obj->comments;
  135. $taber[$obj->rowid]["fk_expensereportdet"] = $obj->erdid;
  136. // Avoid warnings
  137. if (! isset($tabttc[$obj->rowid][$compta_user])) $tabttc[$obj->rowid][$compta_user] = 0;
  138. if (! isset($tabht[$obj->rowid][$compta_fees])) $tabht[$obj->rowid][$compta_fees] = 0;
  139. if (! isset($tabtva[$obj->rowid][$compta_tva])) $tabtva[$obj->rowid][$compta_tva] = 0;
  140. if (! isset($tablocaltax1[$obj->rowid][$compta_localtax1])) $tablocaltax1[$obj->rowid][$compta_localtax1] = 0;
  141. if (! isset($tablocaltax2[$obj->rowid][$compta_localtax2])) $tablocaltax2[$obj->rowid][$compta_localtax2] = 0;
  142. $tabttc[$obj->rowid][$compta_user] += $obj->total_ttc;
  143. $tabht[$obj->rowid][$compta_fees] += $obj->total_ht;
  144. $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
  145. $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
  146. $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
  147. $tabuser[$obj->rowid] = array (
  148. 'id' => $obj->uid,
  149. 'name' => dolGetFirstLastname($obj->firstname, $obj->lastname),
  150. 'user_accountancy_code' => $obj->user_accountancy_account
  151. );
  152. $i ++;
  153. }
  154. } else {
  155. dol_print_error($db);
  156. }
  157. // Bookkeeping Write
  158. if ($action == 'writebookkeeping') {
  159. $now = dol_now();
  160. $error = 0;
  161. foreach ($taber as $key => $val) // Loop on each expense report
  162. {
  163. $errorforline = 0;
  164. $totalcredit = 0;
  165. $totaldebit = 0;
  166. $db->begin();
  167. // Thirdparty
  168. if (! $errorforline)
  169. {
  170. foreach ($tabttc[$key] as $k => $mt) {
  171. if ($mt) {
  172. $bookkeeping = new BookKeeping($db);
  173. $bookkeeping->doc_date = $val["date"];
  174. $bookkeeping->doc_ref = $val["ref"];
  175. $bookkeeping->date_creation = $now;
  176. $bookkeeping->doc_type = 'expense_report';
  177. $bookkeeping->fk_doc = $key;
  178. $bookkeeping->fk_docdet = $val["fk_expensereportdet"];
  179. $bookkeeping->subledger_account = $tabuser[$key]['user_accountancy_code'];
  180. $bookkeeping->subledger_label = $tabuser[$key]['name'];
  181. $bookkeeping->numero_compte = $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT;
  182. $accountingaccount->fetch(null, $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT, true);
  183. $bookkeeping->label_compte = $accountingaccount->label;
  184. $bookkeeping->label_operation = $tabuser[$key]['name'];
  185. $bookkeeping->montant = $mt;
  186. $bookkeeping->sens = ($mt >= 0) ? 'C' : 'D';
  187. $bookkeeping->debit = ($mt <= 0) ? -$mt : 0;
  188. $bookkeeping->credit = ($mt > 0) ? $mt : 0;
  189. $bookkeeping->code_journal = $journal;
  190. $bookkeeping->journal_label = $journal_label;
  191. $bookkeeping->fk_user_author = $user->id;
  192. $bookkeeping->entity = $conf->entity;
  193. $totaldebit += $bookkeeping->debit;
  194. $totalcredit += $bookkeeping->credit;
  195. $result = $bookkeeping->create($user);
  196. if ($result < 0) {
  197. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  198. {
  199. $error++;
  200. $errorforline++;
  201. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  202. }
  203. else
  204. {
  205. $error++;
  206. $errorforline++;
  207. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  208. }
  209. }
  210. }
  211. }
  212. }
  213. // Fees
  214. if (! $errorforline)
  215. {
  216. foreach ($tabht[$key] as $k => $mt) {
  217. if ($mt) {
  218. // get compte id and label
  219. if ($accountingaccount->fetch(null, $k, true)) {
  220. $bookkeeping = new BookKeeping($db);
  221. $bookkeeping->doc_date = $val["date"];
  222. $bookkeeping->doc_ref = $val["ref"];
  223. $bookkeeping->date_creation = $now;
  224. $bookkeeping->doc_type = 'expense_report';
  225. $bookkeeping->fk_doc = $key;
  226. $bookkeeping->fk_docdet = $val["fk_expensereportdet"];
  227. $bookkeeping->subledger_account = '';
  228. $bookkeeping->subledger_label = '';
  229. $bookkeeping->numero_compte = $k;
  230. $bookkeeping->label_compte = $accountingaccount->label;
  231. $bookkeeping->label_operation = $accountingaccount->label;
  232. $bookkeeping->montant = $mt;
  233. $bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
  234. $bookkeeping->debit = ($mt > 0) ? $mt : 0;
  235. $bookkeeping->credit = ($mt <= 0) ? $mt : 0;
  236. $bookkeeping->code_journal = $journal;
  237. $bookkeeping->journal_label = $journal_label;
  238. $bookkeeping->fk_user_author = $user->id;
  239. $bookkeeping->entity = $conf->entity;
  240. $totaldebit += $bookkeeping->debit;
  241. $totalcredit += $bookkeeping->credit;
  242. $result = $bookkeeping->create($user);
  243. if ($result < 0) {
  244. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  245. {
  246. $error++;
  247. $errorforline++;
  248. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  249. }
  250. else
  251. {
  252. $error++;
  253. $errorforline++;
  254. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }
  261. // VAT
  262. if (! $errorforline)
  263. {
  264. $listoftax=array(0, 1, 2);
  265. foreach($listoftax as $numtax)
  266. {
  267. $arrayofvat = $tabtva;
  268. if ($numtax == 1) $arrayofvat = $tablocaltax1;
  269. if ($numtax == 2) $arrayofvat = $tablocaltax2;
  270. foreach ($arrayofvat[$key] as $k => $mt) {
  271. if ($mt) {
  272. // get compte id and label
  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. $bookkeeping->subledger_account = '';
  281. $bookkeeping->subledger_label = '';
  282. $bookkeeping->numero_compte = $k;
  283. $accountingaccount->fetch($k, null, true);
  284. $bookkeeping->label_compte = $accountingaccount->label;
  285. $bookkeeping->label_operation = $langs->trans("VAT"). ' '.join(', ', $def_tva[$key][$k]).' %';
  286. $bookkeeping->montant = $mt;
  287. $bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
  288. $bookkeeping->debit = ($mt > 0) ? $mt : 0;
  289. $bookkeeping->credit = ($mt <= 0) ? $mt : 0;
  290. $bookkeeping->code_journal = $journal;
  291. $bookkeeping->journal_label = $journal_label;
  292. $bookkeeping->fk_user_author = $user->id;
  293. $bookkeeping->entity = $conf->entity;
  294. $totaldebit += $bookkeeping->debit;
  295. $totalcredit += $bookkeeping->credit;
  296. $result = $bookkeeping->create($user);
  297. if ($result < 0) {
  298. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  299. {
  300. $error++;
  301. $errorforline++;
  302. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  303. }
  304. else
  305. {
  306. $error++;
  307. $errorforline++;
  308. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  309. }
  310. }
  311. }
  312. }
  313. }
  314. }
  315. // Protection against a bug on line before
  316. if (price2num($totaldebit) != price2num($totalcredit))
  317. {
  318. $error++;
  319. $errorforline++;
  320. setEventMessages('Try to insert a non balanced transaction in book for '.$val["ref"].'. Canceled. Surely a bug.', null, 'errors');
  321. }
  322. if (! $errorforline)
  323. {
  324. $db->commit();
  325. }
  326. else
  327. {
  328. $db->rollback();
  329. if ($error >= 10)
  330. {
  331. setEventMessages($langs->trans("ErrorTooManyErrorsProcessStopped"), null, 'errors');
  332. break; // Break in the foreach
  333. }
  334. }
  335. }
  336. $tabpay = $taber;
  337. if (empty($error) && count($tabpay) > 0) {
  338. setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
  339. }
  340. elseif (count($tabpay) == $error)
  341. {
  342. setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
  343. }
  344. else
  345. {
  346. setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
  347. }
  348. $action='';
  349. // Must reload data, so we make a redirect
  350. if (count($tabpay) != $error)
  351. {
  352. $param='id_journal='.$id_journal;
  353. $param.='&date_startday='.$date_startday;
  354. $param.='&date_startmonth='.$date_startmonth;
  355. $param.='&date_startyear='.$date_startyear;
  356. $param.='&date_endday='.$date_endday;
  357. $param.='&date_endmonth='.$date_endmonth;
  358. $param.='&date_endyear='.$date_endyear;
  359. $param.='&in_bookkeeping='.$in_bookkeeping;
  360. header("Location: ".$_SERVER['PHP_SELF'].($param?'?'.$param:''));
  361. exit;
  362. }
  363. }
  364. /*
  365. * View
  366. */
  367. $form = new Form($db);
  368. $userstatic = new User($db);
  369. // Export
  370. if ($action == 'exportcsv') { // ISO and not UTF8 !
  371. $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  372. $filename = 'journal';
  373. $type_export = 'journal';
  374. include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
  375. // CSV header line
  376. print '"' . $langs->transnoentitiesnoconv("Date") . '"' . $sep;
  377. print '"' . $langs->transnoentitiesnoconv("Piece") . '"' . $sep;
  378. print '"' . $langs->transnoentitiesnoconv("AccountAccounting") . '"' . $sep;
  379. print '"' . $langs->transnoentitiesnoconv("LabelOperation") . '"' . $sep;
  380. print '"' . $langs->transnoentitiesnoconv("Debit") . '"' . $sep;
  381. print '"' . $langs->transnoentitiesnoconv("Credit") . '"' . $sep;
  382. print "\n";
  383. foreach ($taber as $key => $val) {
  384. $date = dol_print_date($val["date"], 'day');
  385. $userstatic->id = $tabuser[$key]['id'];
  386. $userstatic->name = $tabuser[$key]['name'];
  387. // Fees
  388. foreach ($tabht[$key] as $k => $mt) {
  389. $accountingaccount = new AccountingAccount($db);
  390. $accountingaccount->fetch(null, $k, true);
  391. if ($mt) {
  392. print '"' . $date . '"' . $sep;
  393. print '"' . $val["ref"] . '"' . $sep;
  394. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  395. print '"' . dol_trunc($accountingaccount->label, 32) . '"' . $sep;
  396. print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep;
  397. print '"' . ($mt < 0 ? price(- $mt) : '') . '"';
  398. print "\n";
  399. }
  400. }
  401. // VAT
  402. foreach ($tabtva[$key] as $k => $mt) {
  403. if ($mt) {
  404. print '"' . $date . '"' . $sep;
  405. print '"' . $val["ref"] . '"' . $sep;
  406. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  407. print '"' . dol_trunc($langs->trans("VAT")) . '"' . $sep;
  408. print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep;
  409. print '"' . ($mt < 0 ? price(- $mt) : '') . '"';
  410. print "\n";
  411. }
  412. }
  413. // Third party
  414. foreach ($tabttc[$key] as $k => $mt) {
  415. print '"' . $date . '"' . $sep;
  416. print '"' . $val["ref"] . '"' . $sep;
  417. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  418. print '"' . dol_trunc($userstatic->name) . '"' . $sep;
  419. print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
  420. print '"' . ($mt >= 0 ? price($mt) : '') . '"';
  421. }
  422. print "\n";
  423. }
  424. }
  425. if (empty($action) || $action == 'view') {
  426. llxHeader('', $langs->trans("ExpenseReportsJournal"));
  427. $nom = $langs->trans("ExpenseReportsJournal") . ' | ' . $accountingjournalstatic->getNomUrl(0, 1, 1, '', 1);
  428. $nomlink = '';
  429. $periodlink = '';
  430. $exportlink = '';
  431. $builddate=dol_now();
  432. $description.= $langs->trans("DescJournalOnlyBindedVisible").'<br>';
  433. $listofchoices=array('notyet'=>$langs->trans("NotYetInGeneralLedger"), 'already'=>$langs->trans("AlreadyInGeneralLedger"));
  434. $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);
  435. $period .= ' - ' .$langs->trans("JournalizationInLedgerStatus").' '. $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
  436. $varlink = 'id_journal=' . $id_journal;
  437. journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
  438. // Button to write into Ledger
  439. if (empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1') {
  440. print '<br>'.img_warning().' '.$langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
  441. print ' : '.$langs->trans("AccountancyAreaDescMisc", 4, '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("MenuDefaultAccounts").'</strong>');
  442. }
  443. print '<div class="tabsAction tabsActionNoBottom">';
  444. if (! empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL)) print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />';
  445. if (empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1') {
  446. print '<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")).'" value="' . $langs->trans("WriteBookKeeping") . '" />';
  447. }
  448. else {
  449. if ($in_bookkeeping == 'notyet') print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
  450. else print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs->trans("WriteBookKeeping") . '</a>';
  451. }
  452. print '</div>';
  453. // TODO Avoid using js. We can use a direct link with $param
  454. print '
  455. <script type="text/javascript">
  456. function launch_export() {
  457. $("div.fiche form input[name=\"action\"]").val("exportcsv");
  458. $("div.fiche form input[type=\"submit\"]").click();
  459. $("div.fiche form input[name=\"action\"]").val("");
  460. }
  461. function writebookkeeping() {
  462. console.log("click on writebookkeeping");
  463. $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
  464. $("div.fiche form input[type=\"submit\"]").click();
  465. $("div.fiche form input[name=\"action\"]").val("");
  466. }
  467. </script>';
  468. /*
  469. * Show result array
  470. */
  471. print '<br>';
  472. $i = 0;
  473. print '<div class="div-table-responsive">';
  474. print "<table class=\"noborder\" width=\"100%\">";
  475. print "<tr class=\"liste_titre\">";
  476. print "<td>" . $langs->trans("Date") . "</td>";
  477. print "<td>" . $langs->trans("Piece") . ' (' . $langs->trans("ExpenseReportRef") . ")</td>";
  478. print "<td>" . $langs->trans("AccountAccounting") . "</td>";
  479. print "<td>" . $langs->trans("SubledgerAccount") . "</td>";
  480. print "<td>" . $langs->trans("LabelOperation") . "</td>";
  481. print '<td class="right">' . $langs->trans("Debit") . "</td>";
  482. print '<td class="right">' . $langs->trans("Credit") . "</td>";
  483. print "</tr>\n";
  484. $r = '';
  485. $expensereportstatic = new ExpenseReport($db);
  486. $expensereportlinestatic = new ExpenseReportLine($db);
  487. foreach ($taber as $key => $val) {
  488. $expensereportstatic->id = $key;
  489. $expensereportstatic->ref = $val["ref"];
  490. $expensereportlinestatic->comments = html_entity_decode(dol_trunc($val["comments"], 32));
  491. $date = dol_print_date($val["date"], 'day');
  492. // Fees
  493. foreach ($tabht[$key] as $k => $mt) {
  494. $accountingaccount = new AccountingAccount($db);
  495. $accountingaccount->fetch(null, $k, true);
  496. if ($mt) {
  497. print '<tr class="oddeven">';
  498. print "<!-- Fees -->";
  499. print "<td>" . $date . "</td>";
  500. print "<td>" . $expensereportstatic->getNomUrl(1) . "</td>";
  501. $userstatic->id = $tabuser[$key]['id'];
  502. $userstatic->name = $tabuser[$key]['name'];
  503. // Account
  504. print "<td>";
  505. $accountoshow = length_accountg($k);
  506. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  507. {
  508. print '<span class="error">'.$langs->trans("FeeAccountNotDefined").'</span>';
  509. }
  510. else print $accountoshow;
  511. print '</td>';
  512. // Subledger account
  513. print "<td>";
  514. print '</td>';
  515. $userstatic->id = $tabuser[$key]['id'];
  516. $userstatic->name = $tabuser[$key]['name'];
  517. print "<td>" . $userstatic->getNomUrl(0, 'user', 16) . ' - ' . $accountingaccount->label . "</td>";
  518. print '<td class="right nowraponall">' . ($mt >= 0 ? price($mt) : '') . "</td>";
  519. print '<td class="right nowraponall">' . ($mt < 0 ? price(- $mt) : '') . "</td>";
  520. print "</tr>";
  521. }
  522. }
  523. // Third party
  524. foreach ($tabttc[$key] as $k => $mt) {
  525. $userstatic->id = $tabuser[$key]['id'];
  526. $userstatic->name = $tabuser[$key]['name'];
  527. print '<tr class="oddeven">';
  528. print "<!-- Thirdparty -->";
  529. print "<td>" . $date . "</td>";
  530. print "<td>" . $expensereportstatic->getNomUrl(1) . "</td>";
  531. // Account
  532. print "<td>";
  533. $accountoshow = length_accounta($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT);
  534. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  535. {
  536. print '<span class="error">'.$langs->trans("MainAccountForUsersNotDefined").'</span>';
  537. }
  538. else print $accountoshow;
  539. print "</td>";
  540. // Subledger account
  541. print "<td>";
  542. $accountoshow = length_accounta($k);
  543. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  544. {
  545. print '<span class="error">'.$langs->trans("UserAccountNotDefined").'</span>';
  546. }
  547. else print $accountoshow;
  548. print '</td>';
  549. print "<td>" . $userstatic->getNomUrl(0, 'user', 16) . ' - ' . $langs->trans("SubledgerAccount") . "</td>";
  550. print '<td class="right nowraponall">' . ($mt < 0 ? - price(- $mt) : '') . "</td>";
  551. print '<td class="right nowraponall">' . ($mt >= 0 ? price($mt) : '') . "</td>";
  552. print "</tr>";
  553. }
  554. // VAT
  555. $listoftax = array(0, 1, 2);
  556. foreach ($listoftax as $numtax) {
  557. $arrayofvat = $tabtva;
  558. if ($numtax == 1) $arrayofvat = $tablocaltax1;
  559. if ($numtax == 2) $arrayofvat = $tablocaltax2;
  560. foreach ($arrayofvat[$key] as $k => $mt) {
  561. if ($mt) {
  562. print '<tr class="oddeven">';
  563. print "<!-- VAT -->";
  564. print "<td>" . $date . "</td>";
  565. print "<td>" . $expensereportstatic->getNomUrl(1) . "</td>";
  566. // Account
  567. print "<td>";
  568. $accountoshow = length_accountg($k);
  569. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  570. {
  571. print '<span class="error">'.$langs->trans("VATAccountNotDefined").'</span>';
  572. }
  573. else print $accountoshow;
  574. print "</td>";
  575. // Subledger account
  576. print "<td>";
  577. print '</td>';
  578. print "<td>" . $userstatic->getNomUrl(0, 'user', 16) . ' - ' . $langs->trans("VAT"). ' '.join(', ', $def_tva[$key][$k]).' %'.($numtax?' - Localtax '.$numtax:'');
  579. print "</td>";
  580. print '<td class="right nowraponall">' . ($mt >= 0 ? price($mt) : '') . "</td>";
  581. print '<td class="right nowraponall">' . ($mt < 0 ? price(- $mt) : '') . "</td>";
  582. print "</tr>";
  583. }
  584. }
  585. }
  586. }
  587. print "</table>";
  588. print '</div>';
  589. // End of page
  590. llxFooter();
  591. }
  592. $db->close();