210 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle =
'', $notooltip = 0)
212 global $langs,
$conf, $hookmanager;
214 if (!empty(
$conf->dol_no_mouse_hover)) {
220 $url = DOL_URL_ROOT.
'/accountancy/admin/journals_list.php?id=35';
222 $label =
'<u>'.$langs->trans(
"ShowAccountingJournal").
'</u>';
223 if (!empty($this->code)) {
224 $label .=
'<br><b>'.$langs->trans(
'Code').
':</b> '.$this->code;
226 if (!empty($this->label)) {
227 $label .=
'<br><b>'.$langs->trans(
'Label').
':</b> '.$langs->transnoentities($this->label);
230 $label .=
' - '.$moretitle;
234 if (empty($notooltip)) {
236 $label = $langs->trans(
"ShowAccountingJournal");
237 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
239 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
240 $linkclose .=
' class="classfortooltip"';
243 $linkstart =
'<a href="'.$url.
'"';
244 $linkstart .= $linkclose.
'>';
253 $label_link = $this->code;
254 if ($withlabel == 1 && !empty($this->label)) {
255 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$langs->transnoentities($this->label).($nourl ?
'</span>' :
'');
257 if ($withlabel == 2 && !empty($this->nature)) {
258 $key = $langs->trans(
"AccountingJournalType".$this->nature);
259 $transferlabel = ($key !=
"AccountingJournalType".strtoupper($langs->trans((
string) $this->nature)) ? $key : $this->label);
260 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$transferlabel.($nourl ?
'</span>' :
'');
263 $result .= $linkstart;
265 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
267 if ($withpicto != 2) {
268 $result .= $label_link;
273 $hookmanager->initHooks(array(
'accountingjournaldao'));
274 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
275 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
277 $result = $hookmanager->resPrint;
279 $result .= $hookmanager->resPrint;
402 public function getAssetData(
User $user, $type =
'view', $date_start =
null, $date_end =
null, $in_bookkeeping =
'notyet')
404 global
$conf, $langs;
406 if (!isModEnabled(
'asset')) {
410 require_once DOL_DOCUMENT_ROOT .
'/core/lib/accounting.lib.php';
411 require_once DOL_DOCUMENT_ROOT .
'/asset/class/asset.class.php';
412 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
413 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetdepreciationoptions.class.php';
415 $langs->loadLangs(array(
"assets"));
421 if (empty($in_bookkeeping)) {
422 $in_bookkeeping =
'notyet';
426 $sql .=
"SELECT ad.fk_asset AS rowid, a.ref AS asset_ref, a.label AS asset_label, a.acquisition_value_ht AS asset_acquisition_value_ht";
427 $sql .=
", a.disposal_date AS asset_disposal_date, a.disposal_amount_ht AS asset_disposal_amount_ht, a.disposal_subject_to_vat AS asset_disposal_subject_to_vat";
428 $sql .=
", ad.rowid AS depreciation_id, ad.depreciation_mode, ad.ref AS depreciation_ref, ad.depreciation_date, ad.depreciation_ht, ad.accountancy_code_debit, ad.accountancy_code_credit";
429 $sql .=
" FROM " . MAIN_DB_PREFIX .
"asset_depreciation as ad";
430 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"asset as a ON a.rowid = ad.fk_asset";
431 $sql .=
" WHERE a.entity IN (" .
getEntity(
'asset', 0) .
')';
432 $sql .=
" AND a.status > 0";
433 if ($in_bookkeeping ==
'already') {
434 $sql .=
" AND EXISTS (SELECT iab.fk_docdet FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS iab WHERE iab.fk_docdet = ad.rowid AND doc_type = 'asset')";
435 } elseif ($in_bookkeeping ==
'notyet') {
436 $sql .=
" AND NOT EXISTS (SELECT iab.fk_docdet FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS iab WHERE iab.fk_docdet = ad.rowid AND doc_type = 'asset')";
438 $sql .=
" AND ad.ref != ''";
439 if ($date_start && $date_end) {
440 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate($date_start) .
"' AND ad.depreciation_date <= '" . $this->db->idate($date_end) .
"'";
444 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate(
getDolGlobalInt(
'ACCOUNTING_DATE_START_BINDING')) .
"'";
446 $sql .=
" ORDER BY ad.depreciation_date";
449 $resql = $this->db->query($sql);
451 $this->errors[] = $this->db->lasterror();
456 'elements' => array(),
458 while ($obj = $this->db->fetch_object($resql)) {
459 if (!isset($pre_data[
'elements'][$obj->rowid])) {
460 $pre_data[
'elements'][$obj->rowid] = array(
461 'ref' => $obj->asset_ref,
462 'label' => $obj->asset_label,
463 'acquisition_value_ht' => $obj->asset_acquisition_value_ht,
464 'depreciation' => array(),
468 if (isset($obj->asset_disposal_date)) {
469 $pre_data[
'elements'][$obj->rowid][
'disposal'] = array(
470 'date' => $this->db->jdate($obj->asset_disposal_date),
471 'amount' => $obj->asset_disposal_amount_ht,
472 'subject_to_vat' => !empty($obj->asset_disposal_subject_to_vat),
477 $compta_debit = empty($obj->accountancy_code_debit) ?
'NotDefined' : $obj->accountancy_code_debit;
478 $compta_credit = empty($obj->accountancy_code_credit) ?
'NotDefined' : $obj->accountancy_code_credit;
480 $pre_data[
'elements'][$obj->rowid][
'depreciation'][$obj->depreciation_id] = array(
481 'date' => $this->db->jdate($obj->depreciation_date),
482 'ref' => $obj->depreciation_ref,
484 $compta_debit => -$obj->depreciation_ht,
485 $compta_credit => $obj->depreciation_ht,
490 $disposal_ref = $langs->transnoentitiesnoconv(
'AssetDisposal');
491 $journal = $this->code;
492 $journal_label = $this->label;
493 $journal_label_formatted = $langs->transnoentities($journal_label);
496 $element_static =
new Asset($this->db);
498 $journal_data = array();
499 foreach ($pre_data[
'elements'] as $pre_data_id => $pre_data_info) {
500 $element_static->id = $pre_data_id;
501 $element_static->ref = (string) $pre_data_info[
"ref"];
502 $element_static->label = (string) $pre_data_info[
"label"];
503 $element_static->acquisition_value_ht = $pre_data_info[
"acquisition_value_ht"];
504 $element_link = $element_static->getNomUrl(1,
'with_label');
506 $element_name_formatted_0 =
dol_trunc($element_static->label, 16);
507 $label_operation = $element_static->getNomUrl(0,
'label', 16);
510 'ref' =>
dol_trunc($element_static->ref, 16,
'right',
'UTF-8', 1),
511 'error' => array_key_exists(
'error', $pre_data_info) ? $pre_data_info[
'error'] :
'',
517 foreach ($pre_data_info[
'depreciation'] as $depreciation_id => $line) {
518 $depreciation_ref = $line[
"ref"];
519 $depreciation_date = $line[
"date"];
520 $depreciation_date_formatted =
dol_print_date($depreciation_date,
'day');
524 foreach ($line[
'lines'] as $account => $mt) {
527 if ($type ==
'view') {
529 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
530 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
534 'date' => $depreciation_date_formatted,
535 'piece' => $element_link,
536 'account_accounting' => $account_to_show,
537 'subledger_account' =>
'',
538 'label_operation' => $label_operation .
' - ' . $depreciation_ref,
539 'debit' => $mt < 0 ?
price(-$mt) :
'',
540 'credit' => $mt >= 0 ?
price($mt) :
'',
542 } elseif ($type ==
'bookkeeping') {
543 if ($account_infos[
'found']) {
545 'doc_date' => $depreciation_date,
546 'date_lim_reglement' =>
'',
547 'doc_ref' => $element_static->ref,
548 'date_creation' => $now,
549 'doc_type' =>
'asset',
550 'fk_doc' => $element_static->id,
551 'fk_docdet' => $depreciation_id,
552 'thirdparty_code' =>
'',
553 'subledger_account' =>
'',
554 'subledger_label' =>
'',
555 'numero_compte' => $account,
556 'label_compte' => $account_infos[
'label'],
557 'label_operation' => $element_name_formatted_0 .
' - ' . $depreciation_ref,
559 'sens' => $mt < 0 ?
'D' :
'C',
560 'debit' => $mt < 0 ? -$mt : 0,
561 'credit' => $mt >= 0 ? $mt : 0,
562 'code_journal' => $journal,
563 'journal_label' => $journal_label_formatted,
566 'fk_user_author' => $user->id,
567 'entity' =>
$conf->entity,
573 $element_static->ref,
574 $account_infos[
'code_formatted_1'],
575 $element_name_formatted_0 .
' - ' . $depreciation_ref,
576 $mt < 0 ?
price(-$mt) :
'',
577 $mt >= 0 ?
price($mt) :
'',
581 $element[
'blocks'][] = $blocks;
586 if (!empty($pre_data_info[
'disposal'])) {
587 $disposal_date = $pre_data_info[
'disposal'][
'date'];
589 if ((!($date_start && $date_end) || ($date_start <= $disposal_date && $disposal_date <= $date_end)) &&
592 $disposal_amount = $pre_data_info[
'disposal'][
'amount'];
593 $disposal_subject_to_vat = $pre_data_info[
'disposal'][
'subject_to_vat'];
599 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
601 $result = $accountancy_codes->fetchAccountancyCodes($element_static->id);
603 $element[
'error'] = $accountancy_codes->errorsToString();
606 $element_static->fetchDepreciationLines();
607 foreach ($element_static->depreciation_lines as $mode_key => $depreciation_lines) {
608 $accountancy_codes_list = $accountancy_codes->accountancy_codes[$mode_key];
610 if (!isset($accountancy_codes_list[
'value_asset_sold'])) {
614 $accountancy_code_value_asset_sold = empty($accountancy_codes_list[
'value_asset_sold']) ?
'NotDefined' : $accountancy_codes_list[
'value_asset_sold'];
615 $accountancy_code_depreciation_asset = empty($accountancy_codes_list[
'depreciation_asset']) ?
'NotDefined' : $accountancy_codes_list[
'depreciation_asset'];
616 $accountancy_code_asset = empty($accountancy_codes_list[
'asset']) ?
'NotDefined' : $accountancy_codes_list[
'asset'];
617 $accountancy_code_receivable_on_assignment = empty($accountancy_codes_list[
'receivable_on_assignment']) ?
'NotDefined' : $accountancy_codes_list[
'receivable_on_assignment'];
618 $accountancy_code_vat_collected = empty($accountancy_codes_list[
'vat_collected']) ?
'NotDefined' : $accountancy_codes_list[
'vat_collected'];
619 $accountancy_code_proceeds_from_sales = empty($accountancy_codes_list[
'proceeds_from_sales']) ?
'NotDefined' : $accountancy_codes_list[
'proceeds_from_sales'];
621 $last_cumulative_amount_ht = 0;
622 $depreciated_ids = array_keys($pre_data_info[
'depreciation']);
623 foreach ($depreciation_lines as $line) {
624 $last_cumulative_amount_ht = $line[
'cumulative_depreciation_ht'];
625 if (!in_array($line[
'id'], $depreciated_ids) && empty($line[
'bookkeeping']) && !empty($line[
'ref'])) {
631 $lines[0][$accountancy_code_value_asset_sold] = -((float) $element_static->acquisition_value_ht - $last_cumulative_amount_ht);
632 $lines[0][$accountancy_code_depreciation_asset] = - (float) $last_cumulative_amount_ht;
633 $lines[0][$accountancy_code_asset] = $element_static->acquisition_value_ht;
635 $disposal_amount_vat = $disposal_subject_to_vat ? (float)
price2num($disposal_amount * $disposal_vat / 100,
'MT') : 0;
636 $lines[1][$accountancy_code_receivable_on_assignment] = -($disposal_amount + $disposal_amount_vat);
637 if ($disposal_subject_to_vat) {
638 $lines[1][$accountancy_code_vat_collected] = $disposal_amount_vat;
640 $lines[1][$accountancy_code_proceeds_from_sales] = $disposal_amount;
642 foreach ($lines as $lines_block) {
644 foreach ($lines_block as $account => $mt) {
647 if ($type ==
'view') {
649 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
650 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
654 'date' => $disposal_date_formatted,
655 'piece' => $element_link,
656 'account_accounting' => $account_to_show,
657 'subledger_account' =>
'',
658 'label_operation' => $label_operation .
' - ' . $disposal_ref,
659 'debit' => $mt < 0 ?
price(-$mt) :
'',
660 'credit' => $mt >= 0 ?
price($mt) :
'',
662 } elseif ($type ==
'bookkeeping') {
663 if ($account_infos[
'found']) {
665 'doc_date' => $disposal_date,
666 'date_lim_reglement' =>
'',
667 'doc_ref' => $element_static->ref,
668 'date_creation' => $now,
669 'doc_type' =>
'asset',
670 'fk_doc' => $element_static->id,
672 'thirdparty_code' =>
'',
673 'subledger_account' =>
'',
674 'subledger_label' =>
'',
675 'numero_compte' => $account,
676 'label_compte' => $account_infos[
'label'],
677 'label_operation' => $element_name_formatted_0 .
' - ' . $disposal_ref,
679 'sens' => $mt < 0 ?
'D' :
'C',
680 'debit' => $mt < 0 ? -$mt : 0,
681 'credit' => $mt >= 0 ? $mt : 0,
682 'code_journal' => $journal,
683 'journal_label' => $journal_label_formatted,
686 'fk_user_author' => $user->id,
687 'entity' =>
$conf->entity,
693 $element_static->ref,
694 $account_infos[
'code_formatted_1'],
695 $element_name_formatted_0 .
' - ' . $disposal_ref,
696 $mt < 0 ?
price(-$mt) :
'',
697 $mt >= 0 ?
price($mt) :
'',
701 $element[
'blocks'][] = $blocks;
708 $journal_data[(int) $pre_data_id] = $element;
712 return $journal_data;
760 global
$conf, $langs, $hookmanager;
761 require_once DOL_DOCUMENT_ROOT .
'/accountancy/class/bookkeeping.class.php';
765 $hookmanager->initHooks(array(
'accountingjournaldao'));
766 $parameters = array(
'journal_data' => &$journal_data);
767 $reshook = $hookmanager->executeHooks(
'writeBookkeeping', $parameters, $this);
769 $this->error = $hookmanager->error;
770 $this->errors = $hookmanager->errors;
772 } elseif (empty($reshook)) {
774 if (!is_array($journal_data)) {
775 $journal_data = array();
778 foreach ($journal_data as $element_id => $element) {
785 if ($element[
'error'] ==
'somelinesarenotbound') {
788 $this->errors[] = $langs->trans(
'ErrorInvoiceContainsLinesNotYetBounded', $element[
'ref']);
791 if (!$error_for_line) {
792 foreach ($element[
'blocks'] as $lines) {
793 foreach ($lines as $line) {
795 $bookkeeping->doc_date = (int) $line[
'doc_date'];
796 $bookkeeping->date_lim_reglement = (int) $line[
'date_lim_reglement'];
797 $bookkeeping->doc_ref = $line[
'doc_ref'];
798 $bookkeeping->date_creation = $line[
'date_creation'];
799 $bookkeeping->doc_type = $line[
'doc_type'];
800 $bookkeeping->fk_doc = $line[
'fk_doc'];
801 $bookkeeping->fk_docdet = $line[
'fk_docdet'];
802 $bookkeeping->thirdparty_code = $line[
'thirdparty_code'];
803 $bookkeeping->subledger_account = $line[
'subledger_account'];
804 $bookkeeping->subledger_label = $line[
'subledger_label'];
805 $bookkeeping->numero_compte = $line[
'numero_compte'];
806 $bookkeeping->label_compte = $line[
'label_compte'];
807 $bookkeeping->label_operation = $line[
'label_operation'];
808 $bookkeeping->montant = $line[
'montant'];
809 $bookkeeping->sens = $line[
'sens'];
810 $bookkeeping->debit = (float) $line[
'debit'];
811 $bookkeeping->credit = (float) $line[
'credit'];
812 $bookkeeping->code_journal = $line[
'code_journal'];
813 $bookkeeping->journal_label = $line[
'journal_label'];
814 $bookkeeping->piece_num = (int) $line[
'piece_num'];
815 $bookkeeping->import_key = $line[
'import_key'];
816 $bookkeeping->fk_user_author = $user->id;
817 $bookkeeping->entity =
$conf->entity;
819 $total_debit += $bookkeeping->debit;
820 $total_credit += $bookkeeping->credit;
822 $result = $bookkeeping->create($user);
824 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
827 $journal_data[$element_id][
'error'] =
'alreadyjournalized';
831 $journal_data[$element_id][
'error'] =
'other';
832 $this->errors[] = $bookkeeping->errorsToString();
850 if ($error_for_line) {
857 if (!$error_for_line && (
price2num($total_debit,
'MT') !=
price2num($total_credit,
'MT'))) {
860 $journal_data[$element_id][
'error'] =
'amountsnotbalanced';
861 $this->errors[] =
'Try to insert a non balanced transaction in book for ' . json_encode($element[
'blocks']) .
'. Canceled. Surely a bug.';
864 if (!$error_for_line) {
867 $this->db->rollback();
869 if ($error >= $max_nb_errors) {
870 $this->errors[] = $langs->trans(
"ErrorTooManyErrorsProcessStopped");
877 return $error ? -$error : 1;
901 public function exportCsv(&$journal_data = array(), $search_date_end = 0, $sep =
'')
903 global
$conf, $langs, $hookmanager;
911 $hookmanager->initHooks(array(
'accountingjournaldao'));
912 $parameters = array(
'journal_data' => &$journal_data,
'search_date_end' => &$search_date_end,
'sep' => &$sep,
'out' => &$out);
913 $reshook = $hookmanager->executeHooks(
'exportCsv', $parameters, $this);
915 $this->error = $hookmanager->error;
916 $this->errors = $hookmanager->errors;
918 } elseif (empty($reshook)) {
920 $journal_data = is_array($journal_data) ? $journal_data : array();
924 if ($this->nature == 4) {
926 $langs->transnoentitiesnoconv(
"BankId"),
927 $langs->transnoentitiesnoconv(
"Date"),
928 $langs->transnoentitiesnoconv(
"PaymentMode"),
929 $langs->transnoentitiesnoconv(
"AccountAccounting"),
930 $langs->transnoentitiesnoconv(
"LedgerAccount"),
931 $langs->transnoentitiesnoconv(
"SubledgerAccount"),
932 $langs->transnoentitiesnoconv(
"Label"),
933 $langs->transnoentitiesnoconv(
"AccountingDebit"),
934 $langs->transnoentitiesnoconv(
"AccountingCredit"),
935 $langs->transnoentitiesnoconv(
"Journal"),
936 $langs->transnoentitiesnoconv(
"Note"),
938 } elseif ($this->nature == 5) {
940 $langs->transnoentitiesnoconv(
"Date"),
941 $langs->transnoentitiesnoconv(
"Piece"),
942 $langs->transnoentitiesnoconv(
"AccountAccounting"),
943 $langs->transnoentitiesnoconv(
"LabelOperation"),
944 $langs->transnoentitiesnoconv(
"AccountingDebit"),
945 $langs->transnoentitiesnoconv(
"AccountingCredit"),
947 } elseif ($this->nature == 1) {
949 $langs->transnoentitiesnoconv(
"Date"),
950 $langs->transnoentitiesnoconv(
"Piece"),
951 $langs->transnoentitiesnoconv(
"AccountAccounting"),
952 $langs->transnoentitiesnoconv(
"LabelOperation"),
953 $langs->transnoentitiesnoconv(
"AccountingDebit"),
954 $langs->transnoentitiesnoconv(
"AccountingCredit"),
958 if (!empty($header)) {
959 $out .=
'"' . implode(
'"' . $sep .
'"', $header) .
'"' .
"\n";
961 foreach ($journal_data as $element_id => $element) {
962 foreach ($element[
'blocks'] as $lines) {
963 foreach ($lines as $line) {
964 $out .=
'"' . implode(
'"' . $sep .
'"', $line) .
'"' .
"\n";