* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/accountancy/journal/variousjournal.php * \ingroup Accountancy (Double entries) * \brief Page of a journal */ // Load Dolibarr environment require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php'; require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php'; // Load translation files required by the page $langs->loadLangs(array("banks", "accountancy", "compta", "other", "errors")); $id_journal = GETPOSTINT('id_journal'); $action = GETPOST('action', 'aZ09'); $date_startmonth = GETPOST('date_startmonth'); $date_startday = GETPOST('date_startday'); $date_startyear = GETPOST('date_startyear'); $date_endmonth = GETPOST('date_endmonth'); $date_endday = GETPOST('date_endday'); $date_endyear = GETPOST('date_endyear'); $in_bookkeeping = GETPOST('in_bookkeeping'); if ($in_bookkeeping == '') { $in_bookkeeping = 'notyet'; } // Get information of a journal $object = new AccountingJournal($db); $result = $object->fetch($id_journal); if ($result > 0) { $id_journal = $object->id; } elseif ($result < 0) { dol_print_error(null, $object->error, $object->errors); } elseif ($result == 0) { accessforbidden('ErrorRecordNotFound'); } $hookmanager->initHooks(array('globaljournal', $object->nature.'journal')); $parameters = array(); $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); if (empty($date_startmonth)) { // Period by default on transfer $dates = getDefaultDatesForTransfer(); $date_start = $dates['date_start']; $pastmonthyear = $dates['pastmonthyear']; $pastmonth = $dates['pastmonth']; } if (empty($date_endmonth)) { // Period by default on transfer $dates = getDefaultDatesForTransfer(); $date_end = $dates['date_end']; $pastmonthyear = $dates['pastmonthyear']; $pastmonth = $dates['pastmonth']; } 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 $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false); $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false); } $data_type = 'view'; if ($action == 'writebookkeeping') { $data_type = 'bookkeeping'; } if ($action == 'exportcsv') { $data_type = 'csv'; } $journal_data = $object->getData($user, $data_type, $date_start, $date_end, $in_bookkeeping); if (!is_array($journal_data)) { setEventMessages($object->error, $object->errors, 'errors'); } // Security check if (!isModEnabled('accounting')) { accessforbidden(); } if ($user->socid > 0) { accessforbidden(); } if (!$user->hasRight('accounting', 'bind', 'write')) { accessforbidden(); } /* * Actions */ $reshook = $hookmanager->executeHooks('doActions', $parameters, $user, $action); // Note that $action and $object may have been modified by some hooks $reload = false; // Bookkeeping Write if ($action == 'writebookkeeping' && $user->hasRight('accounting', 'bind', 'write')) { $error = 0; $result = $object->writeIntoBookkeeping($user, $journal_data); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); $error = abs($result); } $nb_elements = count($journal_data); if (empty($error) && $nb_elements > 0) { setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs'); } elseif ($nb_elements == $error) { setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings'); } else { setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings'); } $reload = true; } elseif ($action == 'exportcsv' && $user->hasRight('accounting', 'bind', 'write')) { // Export CSV $result = $object->exportCsv($journal_data, $date_end); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); $reload = true; } else { $filename = 'journal'; $type_export = 'journal'; require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php'; include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php'; print $result; $db->close(); exit(); } } // Must reload data, so we make a redirect if ($reload) { $param = 'id_journal=' . $id_journal; $param .= '&date_startday=' . $date_startday; $param .= '&date_startmonth=' . $date_startmonth; $param .= '&date_startyear=' . $date_startyear; $param .= '&date_endday=' . $date_endday; $param .= '&date_endmonth=' . $date_endmonth; $param .= '&date_endyear=' . $date_endyear; $param .= '&in_bookkeeping=' . $in_bookkeeping; header("Location: " . $_SERVER['PHP_SELF'] . ($param ? '?' . $param : '')); exit; } /* * View */ $form = new Form($db); if ($object->nature == 2) { $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1'; $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1'; } elseif ($object->nature == 3) { $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1'; $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1'; } elseif ($object->nature == 4) { $some_mandatory_steps_of_setup_were_not_done = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1' || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1' || !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1'; $account_accounting_not_defined = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1' || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1'; } elseif ($object->nature == 5) { $some_mandatory_steps_of_setup_were_not_done = !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1'; $account_accounting_not_defined = !getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1'; } else { $title = $object->getLibType(); $some_mandatory_steps_of_setup_were_not_done = false; $account_accounting_not_defined = false; } $title = $langs->trans("GenerationOfAccountingEntries") . ' - ' . $object->getNomUrl(0, 2, 1, '', 1); $help_url ='EN:Module_Double_Entry_Accounting|FR:Module_Comptabilité_en_Partie_Double#Génération_des_écritures_en_comptabilité'; llxHeader('', dol_string_nohtmltag($title), $help_url, '', 0, 0, '', '', '', 'mod-accountancy accountancy-generation page-variousjournal'); $nom = $title; $nomlink = ''; $periodlink = ''; $exportlink = ''; $builddate = dol_now(); $description = $langs->trans("DescJournalOnlyBindedVisible") . '
'; if ($object->nature == 2 || $object->nature == 3) { if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) { $description .= $langs->trans("DepositsAreNotIncluded"); } else { $description .= $langs->trans("DepositsAreIncluded"); } if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) { $description .= $langs->trans("SupplierDepositsAreNotIncluded"); } } $listofchoices = array('notyet' => $langs->trans("NotYetInGeneralLedger"), 'already' => $langs->trans("AlreadyInGeneralLedger")); $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); $period .= ' - ' . $langs->trans("JournalizationInLedgerStatus") . ' ' . $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1); $varlink = 'id_journal=' . $id_journal; journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink); if (getDolGlobalString('ACCOUNTANCY_FISCAL_PERIOD_MODE') != 'blockedonclosed') { // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed) // Fiscal period test $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_fiscalyear WHERE entity = ".((int) $conf->entity); $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); if ($obj->nb == 0) { print '
'.img_warning().' '.$langs->trans("TheFiscalPeriodIsNotDefined"); $desc = ' : '.$langs->trans("AccountancyAreaDescFiscalPeriod", 4, '{link}'); $desc = str_replace('{link}', ''.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("FiscalPeriod").'', $desc); print $desc; print '
'; } } else { dol_print_error($db); } } if ($object->nature == 4) { // Bank journal // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed) $sql = "SELECT COUNT(rowid) as nb"; $sql .= " FROM " . MAIN_DB_PREFIX . "bank_account"; $sql .= " WHERE entity = " . (int) $conf->entity; $sql .= " AND fk_accountancy_journal IS NULL"; $sql .= " AND clos=0"; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); if ($obj->nb > 0) { print '
' . img_warning() . ' ' . $langs->trans("TheJournalCodeIsNotDefinedOnSomeBankAccount"); print ' : ' . $langs->trans("AccountancyAreaDescBank", 9, '' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("BankAccounts") . ''); } } else { dol_print_error($db); } } // Button to write into Ledger if ($some_mandatory_steps_of_setup_were_not_done) { print '
' . img_warning() . ' ' . $langs->trans("SomeMandatoryStepsOfSetupWereNotDone"); print ' : ' . $langs->trans("AccountancyAreaDescMisc", 4, '' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("MenuDefaultAccounts") . ''); print '
'; } print '
'; if (getDolGlobalString('ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL') && $in_bookkeeping == 'notyet') { print ''; } if ($account_accounting_not_defined) { print ''; } else { if ($in_bookkeeping == 'notyet') { print ''; } else { print '' . $langs->trans("WriteBookKeeping") . ''; } } print '
'; // TODO Avoid using js. We can use a direct link with $param print ' '; $object_label = $langs->trans("ObjectsRef"); if ($object->nature == 2 || $object->nature == 3) { $object_label = $langs->trans("InvoiceRef"); } if ($object->nature == 5) { $object_label = $langs->trans("ExpenseReportRef"); } // Show result array $i = 0; print '
'; print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; if ($object->nature == 4) { print ''; } // bank print ''; print ''; print "\n"; if (is_array($journal_data) && !empty($journal_data)) { foreach ($journal_data as $element_id => $element) { foreach ($element['blocks'] as $lines) { foreach ($lines as $line) { print ''; print ''; print ''; print ''; print ''; print ''; if ($object->nature == 4) { print ''; } print ''; print ''; print ''; $i++; } } } } if (!$i) { $colspan = 7; if ($object->nature == 4) { $colspan++; } print ''; } print '
' . $langs->trans("Date") . '' . $langs->trans("Piece") . ' (' . $object_label . ')' . $langs->trans("AccountAccounting") . '' . $langs->trans("SubledgerAccount") . '' . $langs->trans("LabelOperation") . '' . $langs->trans("PaymentMode") . '' . $langs->trans("AccountingDebit") . '' . $langs->trans("AccountingCredit") . '
' . $line['date'] . '' . $line['piece'] . '' . $line['account_accounting'] . '' . $line['subledger_account'] . '' . $line['label_operation'] . '' . $line['payment_mode'] . '' . $line['debit'] . '' . $line['credit'] . '
'.$langs->trans("NoRecordFound").'
'; print '
'; llxFooter(); $db->close();