variousjournal.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /* Copyright (C) 2021-2022 Open-DSI <support@open-dsi.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/accountancy/journal/variousjournal.php
  19. * \ingroup Accountancy (Double entries)
  20. * \brief Page of a journal
  21. */
  22. require '../../main.inc.php';
  23. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
  26. // Load translation files required by the page
  27. $langs->loadLangs(array("banks", "accountancy", "compta", "other", "errors"));
  28. $id_journal = GETPOST('id_journal', 'int');
  29. $action = GETPOST('action', 'aZ09');
  30. $date_startmonth = GETPOST('date_startmonth');
  31. $date_startday = GETPOST('date_startday');
  32. $date_startyear = GETPOST('date_startyear');
  33. $date_endmonth = GETPOST('date_endmonth');
  34. $date_endday = GETPOST('date_endday');
  35. $date_endyear = GETPOST('date_endyear');
  36. $in_bookkeeping = GETPOST('in_bookkeeping');
  37. if ($in_bookkeeping == '') {
  38. $in_bookkeeping = 'notyet';
  39. }
  40. // Get information of journal
  41. $object = new AccountingJournal($db);
  42. $result = $object->fetch($id_journal);
  43. if ($result > 0) {
  44. $id_journal = $object->id;
  45. } elseif ($result < 0) {
  46. dol_print_error('', $object->error, $object->errors);
  47. } elseif ($result == 0) {
  48. accessforbidden($langs->trans('ErrorRecordNotFound'));
  49. }
  50. $hookmanager->initHooks(array('globaljournal', $object->nature.'journal'));
  51. $parameters = array();
  52. $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  53. $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  54. if (empty($date_startmonth) || empty($date_endmonth)) {
  55. // Period by default on transfer
  56. $dates = getDefaultDatesForTransfer();
  57. $date_start = $dates['date_start'];
  58. $date_end = $dates['date_end'];
  59. $pastmonthyear = $dates['pastmonthyear'];
  60. $pastmonth = $dates['pastmonth'];
  61. }
  62. 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
  63. $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
  64. $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
  65. }
  66. $data_type = 'view';
  67. if ($action == 'writebookkeeping') $data_type = 'bookkeeping';
  68. if ($action == 'exportcsv') $data_type = 'csv';
  69. $journal_data = $object->getData($user, $data_type, $date_start, $date_end, $in_bookkeeping);
  70. if (!is_array($journal_data)) {
  71. setEventMessages($object->error, $object->errors, 'errors');
  72. }
  73. // Security check
  74. if (empty($conf->accounting->enabled)) {
  75. accessforbidden();
  76. }
  77. if ($user->socid > 0) {
  78. accessforbidden();
  79. }
  80. if (empty($user->rights->accounting->mouvements->lire)) {
  81. accessforbidden();
  82. }
  83. /*
  84. * Actions
  85. */
  86. $reshook = $hookmanager->executeHooks('doActions', $parameters, $user, $action); // Note that $action and $object may have been modified by some hooks
  87. $reload = false;
  88. // Bookkeeping Write
  89. if ($action == 'writebookkeeping') {
  90. $error = 0;
  91. $result = $object->writeIntoBookkeeping($user, $journal_data);
  92. if ($result < 0) {
  93. setEventMessages($object->error, $object->errors, 'errors');
  94. $error = abs($result);
  95. }
  96. $nb_elements = count($journal_data);
  97. if (empty($error) && $nb_elements > 0) {
  98. setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
  99. } elseif ($nb_elements == $error) {
  100. setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
  101. } else {
  102. setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
  103. }
  104. $reload = true;
  105. } elseif ($action == 'exportcsv') {
  106. // Export CSV
  107. $result = $object->exportCsv($journal_data, $date_end);
  108. if ($result < 0) {
  109. setEventMessages($object->error, $object->errors, 'errors');
  110. $reload = true;
  111. } else {
  112. $filename = 'journal';
  113. $type_export = 'journal';
  114. require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
  115. include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
  116. print $result;
  117. $db->close();
  118. exit();
  119. }
  120. }
  121. // Must reload data, so we make a redirect
  122. if ($reload) {
  123. $param = 'id_journal=' . $id_journal;
  124. $param .= '&date_startday=' . $date_startday;
  125. $param .= '&date_startmonth=' . $date_startmonth;
  126. $param .= '&date_startyear=' . $date_startyear;
  127. $param .= '&date_endday=' . $date_endday;
  128. $param .= '&date_endmonth=' . $date_endmonth;
  129. $param .= '&date_endyear=' . $date_endyear;
  130. $param .= '&in_bookkeeping=' . $in_bookkeeping;
  131. header("Location: " . $_SERVER['PHP_SELF'] . ($param ? '?' . $param : ''));
  132. exit;
  133. }
  134. /*
  135. * View
  136. */
  137. $form = new Form($db);
  138. if ($object->nature == 2) {
  139. $title = $langs->trans("SellsJournal");
  140. $some_mandatory_steps_of_setup_were_not_done = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1';
  141. $account_accounting_not_defined = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1';
  142. } elseif ($object->nature == 3) {
  143. $title = $langs->trans("PurchasesJournal");
  144. $some_mandatory_steps_of_setup_were_not_done = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1';
  145. $account_accounting_not_defined = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1';
  146. } elseif ($object->nature == 4) {
  147. $title = $langs->trans("FinanceJournal");
  148. $some_mandatory_steps_of_setup_were_not_done = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1'
  149. || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1'
  150. || empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1';
  151. $account_accounting_not_defined = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1'
  152. || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1';
  153. } elseif ($object->nature == 5) {
  154. $title = $langs->trans("ExpenseReportsJournal");
  155. $some_mandatory_steps_of_setup_were_not_done = empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1';
  156. $account_accounting_not_defined = empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1';
  157. } else {
  158. $title = $object->getLibType();
  159. $some_mandatory_steps_of_setup_were_not_done = false;
  160. $account_accounting_not_defined = false;
  161. }
  162. $nom = $title . ' | ' . $object->getNomUrl(0, 1, 1, '', 1);
  163. $nomlink = '';
  164. $periodlink = '';
  165. $exportlink = '';
  166. $builddate = dol_now();
  167. $description = $langs->trans("DescJournalOnlyBindedVisible") . '<br>';
  168. if ($object->nature == 2 || $object->nature == 3) {
  169. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  170. $description .= $langs->trans("DepositsAreNotIncluded");
  171. } else {
  172. $description .= $langs->trans("DepositsAreIncluded");
  173. }
  174. }
  175. $listofchoices = array('notyet' => $langs->trans("NotYetInGeneralLedger"), 'already' => $langs->trans("AlreadyInGeneralLedger"));
  176. $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);
  177. $period .= ' - ' . $langs->trans("JournalizationInLedgerStatus") . ' ' . $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
  178. $varlink = 'id_journal=' . $id_journal;
  179. llxHeader('', $title);
  180. journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
  181. if ($object->nature == 4) { // Bank journal
  182. // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed)
  183. $sql = "SELECT COUNT(rowid) as nb";
  184. $sql .= " FROM " . MAIN_DB_PREFIX . "bank_account";
  185. $sql .= " WHERE entity = " . (int) $conf->entity;
  186. $sql .= " AND fk_accountancy_journal IS NULL";
  187. $sql .= " AND clos=0";
  188. $resql = $db->query($sql);
  189. if ($resql) {
  190. $obj = $db->fetch_object($resql);
  191. if ($obj->nb > 0) {
  192. print '<br>' . img_warning() . ' ' . $langs->trans("TheJournalCodeIsNotDefinedOnSomeBankAccount");
  193. print ' : ' . $langs->trans("AccountancyAreaDescBank", 9, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("BankAccounts") . '</strong>');
  194. }
  195. } else dol_print_error($db);
  196. }
  197. // Button to write into Ledger
  198. if ($some_mandatory_steps_of_setup_were_not_done) {
  199. print '<br><div class="warning">' . img_warning() . ' ' . $langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
  200. print ' : ' . $langs->trans("AccountancyAreaDescMisc", 4, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("MenuDefaultAccounts") . '</strong>');
  201. print '</div>';
  202. }
  203. print '<div class="tabsAction tabsActionNoBottom">';
  204. if (!empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL) && $in_bookkeeping == 'notyet') {
  205. print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />';
  206. }
  207. if ($account_accounting_not_defined) {
  208. print '<input type="button" class="butActionRefused classfortooltip" title="' . dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")) . '" value="' . $langs->trans("WriteBookKeeping") . '" />';
  209. } else {
  210. if ($in_bookkeeping == 'notyet') {
  211. print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
  212. } else {
  213. print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs->trans("WriteBookKeeping") . '</a>';
  214. }
  215. }
  216. print '</div>';
  217. // TODO Avoid using js. We can use a direct link with $param
  218. print '
  219. <script type="text/javascript">
  220. function launch_export() {
  221. $("div.fiche form input[name=\"action\"]").val("exportcsv");
  222. $("div.fiche form input[type=\"submit\"]").click();
  223. $("div.fiche form input[name=\"action\"]").val("");
  224. }
  225. function writebookkeeping() {
  226. console.log("click on writebookkeeping");
  227. $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
  228. $("div.fiche form input[type=\"submit\"]").click();
  229. $("div.fiche form input[name=\"action\"]").val("");
  230. }
  231. </script>';
  232. $object_label = $langs->trans("ObjectsRef");
  233. if ($object->nature == 2 || $object->nature == 3) $object_label = $langs->trans("InvoiceRef");
  234. if ($object->nature == 5) $object_label = $langs->trans("ExpenseReportRef");
  235. /*
  236. * Show result array
  237. */
  238. print '<br>';
  239. print '<div class="div-table-responsive">';
  240. print '<table class="noborder centpercent">';
  241. print '<tr class="liste_titre">';
  242. print '<td>' . $langs->trans("Date") . '</td>';
  243. print '<td>' . $langs->trans("Piece") . ' (' . $object_label . ')</td>';
  244. print '<td>' . $langs->trans("AccountAccounting") . '</td>';
  245. print '<td>' . $langs->trans("SubledgerAccount") . '</td>';
  246. print '<td>' . $langs->trans("LabelOperation") . '</td>';
  247. if ($object->nature == 4) print '<td class="center">' . $langs->trans("PaymentMode") . '</td>'; // bank
  248. print '<td class="right">' . $langs->trans("Debit") . '</td>';
  249. print '<td class="right">' . $langs->trans("Credit") . '</td>';
  250. print "</tr>\n";
  251. foreach ($journal_data as $element_id => $element) {
  252. foreach ($element['blocks'] as $lines) {
  253. foreach ($lines as $line) {
  254. print '<tr class="oddeven">';
  255. print '<td>' . $line['date'] . '</td>';
  256. print '<td>' . $line['piece'] . '</td>';
  257. print '<td>' . $line['account_accounting'] . '</td>';
  258. print '<td>' . $line['subledger_account'] . '</td>';
  259. print '<td>' . $line['label_operation'] . '</td>';
  260. if ($object->nature == 4) print '<td class="center">' . $line['payment_mode'] . '</td>';
  261. print '<td class="right nowraponall">' . $line['debit'] . '</td>';
  262. print '<td class="right nowraponall">' . $line['credit'] . '</td>';
  263. print '</tr>';
  264. }
  265. }
  266. }
  267. print '</table>';
  268. print '</div>';
  269. llxFooter();
  270. $db->close();