dolibarr  17.0.4
variousjournal.php
Go to the documentation of this file.
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 
24 // Load Dolibarr environment
25 require '../../main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
29 
30 // Load translation files required by the page
31 $langs->loadLangs(array("banks", "accountancy", "compta", "other", "errors"));
32 
33 $id_journal = GETPOST('id_journal', 'int');
34 $action = GETPOST('action', 'aZ09');
35 
36 $date_startmonth = GETPOST('date_startmonth');
37 $date_startday = GETPOST('date_startday');
38 $date_startyear = GETPOST('date_startyear');
39 $date_endmonth = GETPOST('date_endmonth');
40 $date_endday = GETPOST('date_endday');
41 $date_endyear = GETPOST('date_endyear');
42 $in_bookkeeping = GETPOST('in_bookkeeping');
43 if ($in_bookkeeping == '') {
44  $in_bookkeeping = 'notyet';
45 }
46 
47 // Get information of journal
48 $object = new AccountingJournal($db);
49 $result = $object->fetch($id_journal);
50 if ($result > 0) {
51  $id_journal = $object->id;
52 } elseif ($result < 0) {
53  dol_print_error('', $object->error, $object->errors);
54 } elseif ($result == 0) {
55  accessforbidden('ErrorRecordNotFound');
56 }
57 
58 $hookmanager->initHooks(array('globaljournal', $object->nature.'journal'));
59 $parameters = array();
60 
61 $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
62 $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
63 
64 if (empty($date_startmonth) || empty($date_endmonth)) {
65  // Period by default on transfer
66  $dates = getDefaultDatesForTransfer();
67  $date_start = $dates['date_start'];
68  $date_end = $dates['date_end'];
69  $pastmonthyear = $dates['pastmonthyear'];
70  $pastmonth = $dates['pastmonth'];
71 }
72 
73 if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) { // We define date_start and date_end, only if we did not submit the form
74  $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
75  $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
76 }
77 
78 $data_type = 'view';
79 if ($action == 'writebookkeeping') $data_type = 'bookkeeping';
80 if ($action == 'exportcsv') $data_type = 'csv';
81 $journal_data = $object->getData($user, $data_type, $date_start, $date_end, $in_bookkeeping);
82 if (!is_array($journal_data)) {
83  setEventMessages($object->error, $object->errors, 'errors');
84 }
85 
86 // Security check
87 if (!isModEnabled('accounting')) {
89 }
90 if ($user->socid > 0) {
92 }
93 if (empty($user->rights->accounting->mouvements->lire)) {
95 }
96 
97 
98 /*
99  * Actions
100  */
101 
102 $reshook = $hookmanager->executeHooks('doActions', $parameters, $user, $action); // Note that $action and $object may have been modified by some hooks
103 
104 $reload = false;
105 
106 // Bookkeeping Write
107 if ($action == 'writebookkeeping') {
108  $error = 0;
109 
110  $result = $object->writeIntoBookkeeping($user, $journal_data);
111  if ($result < 0) {
112  setEventMessages($object->error, $object->errors, 'errors');
113  $error = abs($result);
114  }
115 
116  $nb_elements = count($journal_data);
117  if (empty($error) && $nb_elements > 0) {
118  setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
119  } elseif ($nb_elements == $error) {
120  setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
121  } else {
122  setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
123  }
124 
125  $reload = true;
126 } elseif ($action == 'exportcsv') {
127  // Export CSV
128  $result = $object->exportCsv($journal_data, $date_end);
129  if ($result < 0) {
130  setEventMessages($object->error, $object->errors, 'errors');
131  $reload = true;
132  } else {
133  $filename = 'journal';
134  $type_export = 'journal';
135 
136  require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
137  include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
138 
139  print $result;
140 
141  $db->close();
142  exit();
143  }
144 }
145 
146 // Must reload data, so we make a redirect
147 if ($reload) {
148  $param = 'id_journal=' . $id_journal;
149  $param .= '&date_startday=' . $date_startday;
150  $param .= '&date_startmonth=' . $date_startmonth;
151  $param .= '&date_startyear=' . $date_startyear;
152  $param .= '&date_endday=' . $date_endday;
153  $param .= '&date_endmonth=' . $date_endmonth;
154  $param .= '&date_endyear=' . $date_endyear;
155  $param .= '&in_bookkeeping=' . $in_bookkeeping;
156  header("Location: " . $_SERVER['PHP_SELF'] . ($param ? '?' . $param : ''));
157  exit;
158 }
159 
160 
161 /*
162  * View
163  */
164 
165 $form = new Form($db);
166 
167 if ($object->nature == 2) {
168  $some_mandatory_steps_of_setup_were_not_done = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1';
169  $account_accounting_not_defined = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1';
170 } elseif ($object->nature == 3) {
171  $some_mandatory_steps_of_setup_were_not_done = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1';
172  $account_accounting_not_defined = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1';
173 } elseif ($object->nature == 4) {
174  $some_mandatory_steps_of_setup_were_not_done = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1'
175  || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1'
176  || empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1';
177  $account_accounting_not_defined = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "" || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1'
178  || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "" || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1';
179 } elseif ($object->nature == 5) {
180  $some_mandatory_steps_of_setup_were_not_done = empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1';
181  $account_accounting_not_defined = empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1';
182 } else {
183  $title = $object->getLibType();
184  $some_mandatory_steps_of_setup_were_not_done = false;
185  $account_accounting_not_defined = false;
186 }
187 
188 $title = $langs->trans("GenerationOfAccountingEntries") . ' - ' . $object->getNomUrl(0, 2, 1, '', 1);
189 
190 llxHeader('', dol_string_nohtmltag($title));
191 
192 $nom = $title;
193 $nomlink = '';
194 $periodlink = '';
195 $exportlink = '';
196 $builddate = dol_now();
197 $description = $langs->trans("DescJournalOnlyBindedVisible") . '<br>';
198 if ($object->nature == 2 || $object->nature == 3) {
199  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
200  $description .= $langs->trans("DepositsAreNotIncluded");
201  } else {
202  $description .= $langs->trans("DepositsAreIncluded");
203  }
204  if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
205  $description .= $langs->trans("SupplierDepositsAreNotIncluded");
206  }
207 }
208 
209 $listofchoices = array('notyet' => $langs->trans("NotYetInGeneralLedger"), 'already' => $langs->trans("AlreadyInGeneralLedger"));
210 $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);
211 $period .= ' - ' . $langs->trans("JournalizationInLedgerStatus") . ' ' . $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
212 
213 $varlink = 'id_journal=' . $id_journal;
214 
215 journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
216 
217 if ($object->nature == 4) { // Bank journal
218  // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed)
219  $sql = "SELECT COUNT(rowid) as nb";
220  $sql .= " FROM " . MAIN_DB_PREFIX . "bank_account";
221  $sql .= " WHERE entity = " . (int) $conf->entity;
222  $sql .= " AND fk_accountancy_journal IS NULL";
223  $sql .= " AND clos=0";
224  $resql = $db->query($sql);
225  if ($resql) {
226  $obj = $db->fetch_object($resql);
227  if ($obj->nb > 0) {
228  print '<br>' . img_warning() . ' ' . $langs->trans("TheJournalCodeIsNotDefinedOnSomeBankAccount");
229  print ' : ' . $langs->trans("AccountancyAreaDescBank", 9, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("BankAccounts") . '</strong>');
230  }
231  } else dol_print_error($db);
232 }
233 
234 // Button to write into Ledger
235 if ($some_mandatory_steps_of_setup_were_not_done) {
236  print '<br><div class="warning">' . img_warning() . ' ' . $langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
237  print ' : ' . $langs->trans("AccountancyAreaDescMisc", 4, '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("MenuDefaultAccounts") . '</strong>');
238  print '</div>';
239 }
240 print '<div class="tabsAction tabsActionNoBottom centerimp">';
241 if (!empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL) && $in_bookkeeping == 'notyet') {
242  print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />';
243 }
244 if ($account_accounting_not_defined) {
245  print '<input type="button" class="butActionRefused classfortooltip" title="' . dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")) . '" value="' . $langs->trans("WriteBookKeeping") . '" />';
246 } else {
247  if ($in_bookkeeping == 'notyet') {
248  print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
249  } else {
250  print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs->trans("WriteBookKeeping") . '</a>';
251  }
252 }
253 print '</div>';
254 
255 // TODO Avoid using js. We can use a direct link with $param
256 print '
257  <script type="text/javascript">
258  function launch_export() {
259  $("div.fiche form input[name=\"action\"]").val("exportcsv");
260  $("div.fiche form input[type=\"submit\"]").click();
261  $("div.fiche form input[name=\"action\"]").val("");
262  }
263  function writebookkeeping() {
264  console.log("click on writebookkeeping");
265  $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
266  $("div.fiche form input[type=\"submit\"]").click();
267  $("div.fiche form input[name=\"action\"]").val("");
268  }
269  </script>';
270 
271 $object_label = $langs->trans("ObjectsRef");
272 if ($object->nature == 2 || $object->nature == 3) $object_label = $langs->trans("InvoiceRef");
273 if ($object->nature == 5) $object_label = $langs->trans("ExpenseReportRef");
274 
275 /*
276  * Show result array
277  */
278 print '<br>';
279 
280 print '<div class="div-table-responsive">';
281 print '<table class="noborder centpercent">';
282 print '<tr class="liste_titre">';
283 print '<td>' . $langs->trans("Date") . '</td>';
284 print '<td>' . $langs->trans("Piece") . ' (' . $object_label . ')</td>';
285 print '<td>' . $langs->trans("AccountAccounting") . '</td>';
286 print '<td>' . $langs->trans("SubledgerAccount") . '</td>';
287 print '<td>' . $langs->trans("LabelOperation") . '</td>';
288 if ($object->nature == 4) print '<td class="center">' . $langs->trans("PaymentMode") . '</td>'; // bank
289 print '<td class="right">' . $langs->trans("AccountingDebit") . '</td>';
290 print '<td class="right">' . $langs->trans("AccountingCredit") . '</td>';
291 print "</tr>\n";
292 
293 if (is_array($journal_data) && !empty($journal_data)) {
294  foreach ($journal_data as $element_id => $element) {
295  foreach ($element['blocks'] as $lines) {
296  foreach ($lines as $line) {
297  print '<tr class="oddeven">';
298  print '<td>' . $line['date'] . '</td>';
299  print '<td>' . $line['piece'] . '</td>';
300  print '<td>' . $line['account_accounting'] . '</td>';
301  print '<td>' . $line['subledger_account'] . '</td>';
302  print '<td>' . $line['label_operation'] . '</td>';
303  if ($object->nature == 4) print '<td class="center">' . $line['payment_mode'] . '</td>';
304  print '<td class="right nowraponall">' . $line['debit'] . '</td>';
305  print '<td class="right nowraponall">' . $line['credit'] . '</td>';
306  print '</tr>';
307  }
308  }
309  }
310 }
311 
312 print '</table>';
313 print '</div>';
314 
315 llxFooter();
316 
317 $db->close();
getDefaultDatesForTransfer()
Return Default dates for transfer based on periodicity option in accountancy setup.
journalHead($nom, $variante, $period, $periodlink, $description, $builddate, $exportlink='', $moreparam=array(), $calcmode='', $varlink='')
Show header of a page used to transfer/dispatch data in accounting.
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
llxFooter()
Empty footer.
Definition: wrapper.php:70
Class to manage accounting accounts.
Class to manage generation of HTML components Only common components must be here.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:575
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:594
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
dol_now($mode='auto')
Return date for now.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
isModEnabled($module)
Is Dolibarr module enabled.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.