240 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle =
'', $notooltip = 0)
242 global $langs, $conf, $user, $hookmanager;
244 if (!empty($conf->dol_no_mouse_hover)) {
250 $url = DOL_URL_ROOT.
'/accountancy/admin/journals_list.php?id=35';
252 $label =
'<u>'.$langs->trans(
"ShowAccountingJournal").
'</u>';
253 if (!empty($this->code)) {
254 $label .=
'<br><b>'.$langs->trans(
'Code').
':</b> '.$this->code;
256 if (!empty($this->label)) {
257 $label .=
'<br><b>'.$langs->trans(
'Label').
':</b> '.$langs->transnoentities($this->label);
260 $label .=
' - '.$moretitle;
264 if (empty($notooltip)) {
265 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
266 $label = $langs->trans(
"ShowAccountingJournal");
267 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
269 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
270 $linkclose .=
' class="classfortooltip"';
273 $linkstart =
'<a href="'.$url.
'"';
274 $linkstart .= $linkclose.
'>';
283 $label_link = $this->code;
284 if ($withlabel != 2 && !empty($this->label)) {
285 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$langs->transnoentities($this->label).($nourl ?
'</span>' :
'');
287 if ($withlabel == 2 && !empty($this->nature)) {
288 $key = $langs->trans(
"AccountingJournalType".strtoupper($this->nature));
289 $transferlabel = ($this->nature && $key !=
"AccountingJournalType".strtoupper($langs->trans($this->nature)) ? $key : $this->label);
290 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$transferlabel.($nourl ?
'</span>' :
'');
293 $result .= $linkstart;
295 $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);
297 if ($withpicto != 2) {
298 $result .= $label_link;
303 $hookmanager->initHooks(array(
'accountingjournaldao'));
304 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
305 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
307 $result = $hookmanager->resPrint;
309 $result .= $hookmanager->resPrint;
428 public function getAssetData(
User $user, $type =
'view', $date_start =
null, $date_end =
null, $in_bookkeeping =
'notyet')
430 global $conf, $langs;
432 if (!isModEnabled(
'asset')) {
436 require_once DOL_DOCUMENT_ROOT .
'/core/lib/accounting.lib.php';
437 require_once DOL_DOCUMENT_ROOT .
'/asset/class/asset.class.php';
438 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
439 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetdepreciationoptions.class.php';
441 $langs->loadLangs(array(
"assets"));
447 if (empty($in_bookkeeping)) {
448 $in_bookkeeping =
'notyet';
452 $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";
453 $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";
454 $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";
455 $sql .=
" FROM " . MAIN_DB_PREFIX .
"asset_depreciation as ad";
456 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"asset as a ON a.rowid = ad.fk_asset";
457 $sql .=
" WHERE a.entity IN (" .
getEntity(
'asset', 0) .
')';
458 if ($in_bookkeeping ==
'already') {
459 $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')";
460 } elseif ($in_bookkeeping ==
'notyet') {
461 $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')";
463 $sql .=
" AND ad.ref != ''";
464 if ($date_start && $date_end) {
465 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate($date_start) .
"' AND ad.depreciation_date <= '" . $this->db->idate($date_end) .
"'";
468 if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
469 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate($conf->global->ACCOUNTING_DATE_START_BINDING) .
"'";
471 $sql .=
" ORDER BY ad.depreciation_date";
474 $resql = $this->db->query($sql);
476 $this->errors[] = $this->db->lasterror();
481 'elements' => array(),
483 while ($obj = $this->db->fetch_object($resql)) {
484 if (!isset($pre_data[
'elements'][$obj->rowid])) {
485 $pre_data[
'elements'][$obj->rowid] = array(
486 'ref' => $obj->asset_ref,
487 'label' => $obj->asset_label,
488 'acquisition_value_ht' => $obj->asset_acquisition_value_ht,
489 'depreciation' => array(),
493 if (isset($obj->asset_disposal_date)) {
494 $pre_data[
'elements'][$obj->rowid][
'disposal'] = array(
495 'date' => $this->db->jdate($obj->asset_disposal_date),
496 'amount' => $obj->asset_disposal_amount_ht,
497 'subject_to_vat' => !empty($obj->asset_disposal_subject_to_vat),
502 $compta_debit = empty($obj->accountancy_code_debit) ?
'NotDefined' : $obj->accountancy_code_debit;
503 $compta_credit = empty($obj->accountancy_code_credit) ?
'NotDefined' : $obj->accountancy_code_credit;
505 $pre_data[
'elements'][$obj->rowid][
'depreciation'][$obj->depreciation_id] = array(
506 'date' => $this->db->jdate($obj->depreciation_date),
507 'ref' => $obj->depreciation_ref,
509 $compta_debit => -$obj->depreciation_ht,
510 $compta_credit => $obj->depreciation_ht,
515 $disposal_ref = $langs->transnoentitiesnoconv(
'AssetDisposal');
516 $journal = $this->code;
517 $journal_label = $this->label;
518 $journal_label_formatted = $langs->transnoentities($journal_label);
521 $element_static =
new Asset($this->db);
523 $journal_data = array();
524 foreach ($pre_data[
'elements'] as $pre_data_id => $pre_data_info) {
525 $element_static->id = $pre_data_id;
526 $element_static->ref = (string) $pre_data_info[
"ref"];
527 $element_static->label = (string) $pre_data_info[
"label"];
528 $element_static->acquisition_value_ht = $pre_data_info[
"acquisition_value_ht"];
529 $element_link = $element_static->getNomUrl(1,
'with_label');
531 $element_name_formatted_0 =
dol_trunc($element_static->label, 16);
532 $element_name_formatted_1 = utf8_decode(
dol_trunc($element_static->label, 32));
533 $element_name_formatted_2 = utf8_decode(
dol_trunc($element_static->label, 16));
534 $label_operation = $element_static->getNomUrl(0,
'label', 16);
537 'ref' =>
dol_trunc($element_static->ref, 16,
'right',
'UTF-8', 1),
538 'error' => $pre_data_info[
'error'],
544 foreach ($pre_data_info[
'depreciation'] as $depreciation_id => $line) {
545 $depreciation_ref = $line[
"ref"];
546 $depreciation_date = $line[
"date"];
547 $depreciation_date_formatted =
dol_print_date($depreciation_date,
'day');
551 foreach ($line[
'lines'] as $account => $mt) {
554 if ($type ==
'view') {
556 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
557 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
561 'date' => $depreciation_date_formatted,
562 'piece' => $element_link,
563 'account_accounting' => $account_to_show,
564 'subledger_account' =>
'',
565 'label_operation' => $label_operation .
' - ' . $depreciation_ref,
566 'debit' => $mt < 0 ?
price(-$mt) :
'',
567 'credit' => $mt >= 0 ?
price($mt) :
'',
569 } elseif ($type ==
'bookkeeping') {
570 if ($account_infos[
'found']) {
572 'doc_date' => $depreciation_date,
573 'date_lim_reglement' =>
'',
574 'doc_ref' => $element_static->ref,
575 'date_creation' => $now,
576 'doc_type' =>
'asset',
577 'fk_doc' => $element_static->id,
578 'fk_docdet' => $depreciation_id,
579 'thirdparty_code' =>
'',
580 'subledger_account' =>
'',
581 'subledger_label' =>
'',
582 'numero_compte' => $account,
583 'label_compte' => $account_infos[
'label'],
584 'label_operation' => $element_name_formatted_0 .
' - ' . $depreciation_ref,
586 'sens' => $mt < 0 ?
'D' :
'C',
587 'debit' => $mt < 0 ? -$mt : 0,
588 'credit' => $mt >= 0 ? $mt : 0,
589 'code_journal' => $journal,
590 'journal_label' => $journal_label_formatted,
593 'fk_user_author' => $user->id,
594 'entity' => $conf->entity,
600 $element_static->ref,
601 $account_infos[
'code_formatted_1'],
602 $element_name_formatted_0 .
' - ' . $depreciation_ref,
603 $mt < 0 ?
price(-$mt) :
'',
604 $mt >= 0 ?
price($mt) :
'',
608 $element[
'blocks'][] = $blocks;
613 if (!empty($pre_data_info[
'disposal'])) {
614 $disposal_date = $pre_data_info[
'disposal'][
'date'];
616 if ((!($date_start && $date_end) || ($date_start <= $disposal_date && $disposal_date <= $date_end)) &&
617 (empty($conf->global->ACCOUNTING_DATE_START_BINDING) || $conf->global->ACCOUNTING_DATE_START_BINDING <= $disposal_date)
619 $disposal_amount = $pre_data_info[
'disposal'][
'amount'];
620 $disposal_subject_to_vat = $pre_data_info[
'disposal'][
'subject_to_vat'];
622 $disposal_vat = $conf->global->ASSET_DISPOSAL_VAT > 0 ? $conf->global->ASSET_DISPOSAL_VAT : 20;
626 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
628 $result = $accountancy_codes->fetchAccountancyCodes($element_static->id);
630 $element[
'error'] = $accountancy_codes->errorsToString();
633 $element_static->fetchDepreciationLines();
634 foreach ($element_static->depreciation_lines as $mode_key => $depreciation_lines) {
635 $accountancy_codes_list = $accountancy_codes->accountancy_codes[$mode_key];
637 if (!isset($accountancy_codes_list[
'value_asset_sold'])) {
641 $accountancy_code_value_asset_sold = empty($accountancy_codes_list[
'value_asset_sold']) ?
'NotDefined' : $accountancy_codes_list[
'value_asset_sold'];
642 $accountancy_code_depreciation_asset = empty($accountancy_codes_list[
'depreciation_asset']) ?
'NotDefined' : $accountancy_codes_list[
'depreciation_asset'];
643 $accountancy_code_asset = empty($accountancy_codes_list[
'asset']) ?
'NotDefined' : $accountancy_codes_list[
'asset'];
644 $accountancy_code_receivable_on_assignment = empty($accountancy_codes_list[
'receivable_on_assignment']) ?
'NotDefined' : $accountancy_codes_list[
'receivable_on_assignment'];
645 $accountancy_code_vat_collected = empty($accountancy_codes_list[
'vat_collected']) ?
'NotDefined' : $accountancy_codes_list[
'vat_collected'];
646 $accountancy_code_proceeds_from_sales = empty($accountancy_codes_list[
'proceeds_from_sales']) ?
'NotDefined' : $accountancy_codes_list[
'proceeds_from_sales'];
648 $last_cumulative_amount_ht = 0;
649 $depreciated_ids = array_keys($pre_data_info[
'depreciation']);
650 foreach ($depreciation_lines as $line) {
651 $last_cumulative_amount_ht = $line[
'cumulative_depreciation_ht'];
652 if (!in_array($line[
'id'], $depreciated_ids) && empty($line[
'bookkeeping']) && !empty($line[
'ref'])) {
658 $lines[0][$accountancy_code_value_asset_sold] = -($element_static->acquisition_value_ht - $last_cumulative_amount_ht);
659 $lines[0][$accountancy_code_depreciation_asset] = -$last_cumulative_amount_ht;
660 $lines[0][$accountancy_code_asset] = $element_static->acquisition_value_ht;
662 $disposal_amount_vat = $disposal_subject_to_vat ? (double)
price2num($disposal_amount * $disposal_vat / 100,
'MT') : 0;
663 $lines[1][$accountancy_code_receivable_on_assignment] = -($disposal_amount + $disposal_amount_vat);
664 if ($disposal_subject_to_vat) $lines[1][$accountancy_code_vat_collected] = $disposal_amount_vat;
665 $lines[1][$accountancy_code_proceeds_from_sales] = $disposal_amount;
667 foreach ($lines as $lines_block) {
669 foreach ($lines_block as $account => $mt) {
672 if ($type ==
'view') {
674 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
675 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
679 'date' => $disposal_date_formatted,
680 'piece' => $element_link,
681 'account_accounting' => $account_to_show,
682 'subledger_account' =>
'',
683 'label_operation' => $label_operation .
' - ' . $disposal_ref,
684 'debit' => $mt < 0 ?
price(-$mt) :
'',
685 'credit' => $mt >= 0 ?
price($mt) :
'',
687 } elseif ($type ==
'bookkeeping') {
688 if ($account_infos[
'found']) {
690 'doc_date' => $disposal_date,
691 'date_lim_reglement' =>
'',
692 'doc_ref' => $element_static->ref,
693 'date_creation' => $now,
694 'doc_type' =>
'asset',
695 'fk_doc' => $element_static->id,
697 'thirdparty_code' =>
'',
698 'subledger_account' =>
'',
699 'subledger_label' =>
'',
700 'numero_compte' => $account,
701 'label_compte' => $account_infos[
'label'],
702 'label_operation' => $element_name_formatted_0 .
' - ' . $disposal_ref,
704 'sens' => $mt < 0 ?
'D' :
'C',
705 'debit' => $mt < 0 ? -$mt : 0,
706 'credit' => $mt >= 0 ? $mt : 0,
707 'code_journal' => $journal,
708 'journal_label' => $journal_label_formatted,
711 'fk_user_author' => $user->id,
712 'entity' => $conf->entity,
718 $element_static->ref,
719 $account_infos[
'code_formatted_1'],
720 $element_name_formatted_0 .
' - ' . $disposal_ref,
721 $mt < 0 ?
price(-$mt) :
'',
722 $mt >= 0 ?
price($mt) :
'',
726 $element[
'blocks'][] = $blocks;
733 $journal_data[(int) $pre_data_id] = $element;
737 return $journal_data;
785 global $conf, $langs, $hookmanager;
786 require_once DOL_DOCUMENT_ROOT .
'/accountancy/class/bookkeeping.class.php';
790 $hookmanager->initHooks(array(
'accountingjournaldao'));
791 $parameters = array(
'journal_data' => &$journal_data);
792 $reshook = $hookmanager->executeHooks(
'writeBookkeeping', $parameters, $this);
794 $this->error = $hookmanager->error;
795 $this->errors = $hookmanager->errors;
797 } elseif (empty($reshook)) {
799 $journal_data = is_array($journal_data) ? $journal_data : array();
801 foreach ($journal_data as $element_id => $element) {
808 if ($element[
'error'] ==
'somelinesarenotbound') {
811 $this->errors[] = $langs->trans(
'ErrorInvoiceContainsLinesNotYetBounded', $element[
'ref']);
814 if (!$error_for_line) {
815 foreach ($element[
'blocks'] as $lines) {
816 foreach ($lines as $line) {
818 $bookkeeping->doc_date = $line[
'doc_date'];
819 $bookkeeping->date_lim_reglement = $line[
'date_lim_reglement'];
820 $bookkeeping->doc_ref = $line[
'doc_ref'];
821 $bookkeeping->date_creation = $line[
'date_creation'];
822 $bookkeeping->doc_type = $line[
'doc_type'];
823 $bookkeeping->fk_doc = $line[
'fk_doc'];
824 $bookkeeping->fk_docdet = $line[
'fk_docdet'];
825 $bookkeeping->thirdparty_code = $line[
'thirdparty_code'];
826 $bookkeeping->subledger_account = $line[
'subledger_account'];
827 $bookkeeping->subledger_label = $line[
'subledger_label'];
828 $bookkeeping->numero_compte = $line[
'numero_compte'];
829 $bookkeeping->label_compte = $line[
'label_compte'];
830 $bookkeeping->label_operation = $line[
'label_operation'];
831 $bookkeeping->montant = $line[
'montant'];
832 $bookkeeping->sens = $line[
'sens'];
833 $bookkeeping->debit = $line[
'debit'];
834 $bookkeeping->credit = $line[
'credit'];
835 $bookkeeping->code_journal = $line[
'code_journal'];
836 $bookkeeping->journal_label = $line[
'journal_label'];
837 $bookkeeping->piece_num = $line[
'piece_num'];
838 $bookkeeping->import_key = $line[
'import_key'];
839 $bookkeeping->fk_user_author = $user->id;
840 $bookkeeping->entity = $conf->entity;
842 $total_debit += $bookkeeping->debit;
843 $total_credit += $bookkeeping->credit;
845 $result = $bookkeeping->create($user);
847 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
850 $journal_data[$element_id][
'error'] =
'alreadyjournalized';
854 $journal_data[$element_id][
'error'] =
'other';
855 $this->errors[] = $bookkeeping->errorsToString();
873 if ($error_for_line) {
880 if (!$error_for_line && (
price2num($total_debit,
'MT') !=
price2num($total_credit,
'MT'))) {
883 $journal_data[$element_id][
'error'] =
'amountsnotbalanced';
884 $this->errors[] =
'Try to insert a non balanced transaction in book for ' . $element[
'blocks'] .
'. Canceled. Surely a bug.';
887 if (!$error_for_line) {
890 $this->db->rollback();
892 if ($error >= $max_nb_errors) {
893 $this->errors[] = $langs->trans(
"ErrorTooManyErrorsProcessStopped");
900 return $error ? -$error : 1;
924 public function exportCsv(&$journal_data = array(), $search_date_end = 0, $sep =
'')
926 global $conf, $langs, $hookmanager;
928 if (empty($sep)) $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
932 $hookmanager->initHooks(array(
'accountingjournaldao'));
933 $parameters = array(
'journal_data' => &$journal_data,
'search_date_end' => &$search_date_end,
'sep' => &$sep,
'out' => &$out);
934 $reshook = $hookmanager->executeHooks(
'exportCsv', $parameters, $this);
936 $this->error = $hookmanager->error;
937 $this->errors = $hookmanager->errors;
939 } elseif (empty($reshook)) {
941 $journal_data = is_array($journal_data) ? $journal_data : array();
945 if ($this->nature == 4) {
947 $langs->transnoentitiesnoconv(
"BankId"),
948 $langs->transnoentitiesnoconv(
"Date"),
949 $langs->transnoentitiesnoconv(
"PaymentMode"),
950 $langs->transnoentitiesnoconv(
"AccountAccounting"),
951 $langs->transnoentitiesnoconv(
"LedgerAccount"),
952 $langs->transnoentitiesnoconv(
"SubledgerAccount"),
953 $langs->transnoentitiesnoconv(
"Label"),
954 $langs->transnoentitiesnoconv(
"AccountingDebit"),
955 $langs->transnoentitiesnoconv(
"AccountingCredit"),
956 $langs->transnoentitiesnoconv(
"Journal"),
957 $langs->transnoentitiesnoconv(
"Note"),
959 } elseif ($this->nature == 5) {
961 $langs->transnoentitiesnoconv(
"Date"),
962 $langs->transnoentitiesnoconv(
"Piece"),
963 $langs->transnoentitiesnoconv(
"AccountAccounting"),
964 $langs->transnoentitiesnoconv(
"LabelOperation"),
965 $langs->transnoentitiesnoconv(
"AccountingDebit"),
966 $langs->transnoentitiesnoconv(
"AccountingCredit"),
968 } elseif ($this->nature == 1) {
970 $langs->transnoentitiesnoconv(
"Date"),
971 $langs->transnoentitiesnoconv(
"Piece"),
972 $langs->transnoentitiesnoconv(
"AccountAccounting"),
973 $langs->transnoentitiesnoconv(
"LabelOperation"),
974 $langs->transnoentitiesnoconv(
"AccountingDebit"),
975 $langs->transnoentitiesnoconv(
"AccountingCredit"),
979 if (!empty($header)) $out .=
'"' . implode(
'"' . $sep .
'"', $header) .
'"' .
"\n";
980 foreach ($journal_data as $element_id => $element) {
981 foreach ($element[
'blocks'] as $lines) {
982 foreach ($lines as $line) {
983 $out .=
'"' . implode(
'"' . $sep .
'"', $line) .
'"' .
"\n";