expensereportsjournal.php 25 KB

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