expensereportsjournal.php 26 KB

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