expensereportsjournal.php 26 KB

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