165 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle =
'', $notooltip = 0)
167 global $langs, $conf, $hookmanager;
169 if (!empty($conf->dol_no_mouse_hover)) {
175 $url = DOL_URL_ROOT.
'/accountancy/admin/journals_list.php?id=35';
177 $label =
'<u>'.$langs->trans(
"ShowAccountingJournal").
'</u>';
178 if (!empty($this->code)) {
179 $label .=
'<br><b>'.$langs->trans(
'Code').
':</b> '.$this->code;
181 if (!empty($this->label)) {
182 $label .=
'<br><b>'.$langs->trans(
'Label').
':</b> '.$langs->transnoentities($this->label);
185 $label .=
' - '.$moretitle;
189 if (empty($notooltip)) {
191 $label = $langs->trans(
"ShowAccountingJournal");
192 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
194 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
195 $linkclose .=
' class="classfortooltip"';
198 $linkstart =
'<a href="'.$url.
'"';
199 $linkstart .= $linkclose.
'>';
208 $label_link = $this->code;
209 if ($withlabel == 1 && !empty($this->label)) {
210 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$langs->transnoentities($this->label).($nourl ?
'</span>' :
'');
212 if ($withlabel == 2 && !empty($this->nature)) {
213 $key = $langs->trans(
"AccountingJournalType".$this->nature);
214 $transferlabel = ($this->nature && $key !=
"AccountingJournalType".strtoupper($langs->trans($this->nature)) ? $key : $this->label);
215 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$transferlabel.($nourl ?
'</span>' :
'');
218 $result .= $linkstart;
220 $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);
222 if ($withpicto != 2) {
223 $result .= $label_link;
228 $hookmanager->initHooks(array(
'accountingjournaldao'));
229 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
230 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
232 $result = $hookmanager->resPrint;
234 $result .= $hookmanager->resPrint;
357 public function getAssetData(
User $user, $type =
'view', $date_start =
null, $date_end =
null, $in_bookkeeping =
'notyet')
359 global $conf, $langs;
361 if (!isModEnabled(
'asset')) {
365 require_once DOL_DOCUMENT_ROOT .
'/core/lib/accounting.lib.php';
366 require_once DOL_DOCUMENT_ROOT .
'/asset/class/asset.class.php';
367 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
368 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetdepreciationoptions.class.php';
370 $langs->loadLangs(array(
"assets"));
376 if (empty($in_bookkeeping)) {
377 $in_bookkeeping =
'notyet';
381 $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";
382 $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";
383 $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";
384 $sql .=
" FROM " . MAIN_DB_PREFIX .
"asset_depreciation as ad";
385 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"asset as a ON a.rowid = ad.fk_asset";
386 $sql .=
" WHERE a.entity IN (" .
getEntity(
'asset', 0) .
')';
387 if ($in_bookkeeping ==
'already') {
388 $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')";
389 } elseif ($in_bookkeeping ==
'notyet') {
390 $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')";
392 $sql .=
" AND ad.ref != ''";
393 if ($date_start && $date_end) {
394 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate($date_start) .
"' AND ad.depreciation_date <= '" . $this->db->idate($date_end) .
"'";
398 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate(
getDolGlobalString(
'ACCOUNTING_DATE_START_BINDING')) .
"'";
400 $sql .=
" ORDER BY ad.depreciation_date";
403 $resql = $this->db->query($sql);
405 $this->errors[] = $this->db->lasterror();
410 'elements' => array(),
412 while ($obj = $this->db->fetch_object($resql)) {
413 if (!isset($pre_data[
'elements'][$obj->rowid])) {
414 $pre_data[
'elements'][$obj->rowid] = array(
415 'ref' => $obj->asset_ref,
416 'label' => $obj->asset_label,
417 'acquisition_value_ht' => $obj->asset_acquisition_value_ht,
418 'depreciation' => array(),
422 if (isset($obj->asset_disposal_date)) {
423 $pre_data[
'elements'][$obj->rowid][
'disposal'] = array(
424 'date' => $this->db->jdate($obj->asset_disposal_date),
425 'amount' => $obj->asset_disposal_amount_ht,
426 'subject_to_vat' => !empty($obj->asset_disposal_subject_to_vat),
431 $compta_debit = empty($obj->accountancy_code_debit) ?
'NotDefined' : $obj->accountancy_code_debit;
432 $compta_credit = empty($obj->accountancy_code_credit) ?
'NotDefined' : $obj->accountancy_code_credit;
434 $pre_data[
'elements'][$obj->rowid][
'depreciation'][$obj->depreciation_id] = array(
435 'date' => $this->db->jdate($obj->depreciation_date),
436 'ref' => $obj->depreciation_ref,
438 $compta_debit => -$obj->depreciation_ht,
439 $compta_credit => $obj->depreciation_ht,
444 $disposal_ref = $langs->transnoentitiesnoconv(
'AssetDisposal');
445 $journal = $this->code;
446 $journal_label = $this->label;
447 $journal_label_formatted = $langs->transnoentities($journal_label);
450 $element_static =
new Asset($this->db);
452 $journal_data = array();
453 foreach ($pre_data[
'elements'] as $pre_data_id => $pre_data_info) {
454 $element_static->id = $pre_data_id;
455 $element_static->ref = (string) $pre_data_info[
"ref"];
456 $element_static->label = (string) $pre_data_info[
"label"];
457 $element_static->acquisition_value_ht = $pre_data_info[
"acquisition_value_ht"];
458 $element_link = $element_static->getNomUrl(1,
'with_label');
460 $element_name_formatted_0 =
dol_trunc($element_static->label, 16);
461 $label_operation = $element_static->getNomUrl(0,
'label', 16);
464 'ref' =>
dol_trunc($element_static->ref, 16,
'right',
'UTF-8', 1),
465 'error' => array_key_exists(
'error', $pre_data_info) ? $pre_data_info[
'error'] :
'',
471 foreach ($pre_data_info[
'depreciation'] as $depreciation_id => $line) {
472 $depreciation_ref = $line[
"ref"];
473 $depreciation_date = $line[
"date"];
474 $depreciation_date_formatted =
dol_print_date($depreciation_date,
'day');
478 foreach ($line[
'lines'] as $account => $mt) {
481 if ($type ==
'view') {
483 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
484 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
488 'date' => $depreciation_date_formatted,
489 'piece' => $element_link,
490 'account_accounting' => $account_to_show,
491 'subledger_account' =>
'',
492 'label_operation' => $label_operation .
' - ' . $depreciation_ref,
493 'debit' => $mt < 0 ?
price(-$mt) :
'',
494 'credit' => $mt >= 0 ?
price($mt) :
'',
496 } elseif ($type ==
'bookkeeping') {
497 if ($account_infos[
'found']) {
499 'doc_date' => $depreciation_date,
500 'date_lim_reglement' =>
'',
501 'doc_ref' => $element_static->ref,
502 'date_creation' => $now,
503 'doc_type' =>
'asset',
504 'fk_doc' => $element_static->id,
505 'fk_docdet' => $depreciation_id,
506 'thirdparty_code' =>
'',
507 'subledger_account' =>
'',
508 'subledger_label' =>
'',
509 'numero_compte' => $account,
510 'label_compte' => $account_infos[
'label'],
511 'label_operation' => $element_name_formatted_0 .
' - ' . $depreciation_ref,
513 'sens' => $mt < 0 ?
'D' :
'C',
514 'debit' => $mt < 0 ? -$mt : 0,
515 'credit' => $mt >= 0 ? $mt : 0,
516 'code_journal' => $journal,
517 'journal_label' => $journal_label_formatted,
520 'fk_user_author' => $user->id,
521 'entity' => $conf->entity,
527 $element_static->ref,
528 $account_infos[
'code_formatted_1'],
529 $element_name_formatted_0 .
' - ' . $depreciation_ref,
530 $mt < 0 ?
price(-$mt) :
'',
531 $mt >= 0 ?
price($mt) :
'',
535 $element[
'blocks'][] = $blocks;
540 if (!empty($pre_data_info[
'disposal'])) {
541 $disposal_date = $pre_data_info[
'disposal'][
'date'];
543 if ((!($date_start && $date_end) || ($date_start <= $disposal_date && $disposal_date <= $date_end)) &&
546 $disposal_amount = $pre_data_info[
'disposal'][
'amount'];
547 $disposal_subject_to_vat = $pre_data_info[
'disposal'][
'subject_to_vat'];
553 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
555 $result = $accountancy_codes->fetchAccountancyCodes($element_static->id);
557 $element[
'error'] = $accountancy_codes->errorsToString();
560 $element_static->fetchDepreciationLines();
561 foreach ($element_static->depreciation_lines as $mode_key => $depreciation_lines) {
562 $accountancy_codes_list = $accountancy_codes->accountancy_codes[$mode_key];
564 if (!isset($accountancy_codes_list[
'value_asset_sold'])) {
568 $accountancy_code_value_asset_sold = empty($accountancy_codes_list[
'value_asset_sold']) ?
'NotDefined' : $accountancy_codes_list[
'value_asset_sold'];
569 $accountancy_code_depreciation_asset = empty($accountancy_codes_list[
'depreciation_asset']) ?
'NotDefined' : $accountancy_codes_list[
'depreciation_asset'];
570 $accountancy_code_asset = empty($accountancy_codes_list[
'asset']) ?
'NotDefined' : $accountancy_codes_list[
'asset'];
571 $accountancy_code_receivable_on_assignment = empty($accountancy_codes_list[
'receivable_on_assignment']) ?
'NotDefined' : $accountancy_codes_list[
'receivable_on_assignment'];
572 $accountancy_code_vat_collected = empty($accountancy_codes_list[
'vat_collected']) ?
'NotDefined' : $accountancy_codes_list[
'vat_collected'];
573 $accountancy_code_proceeds_from_sales = empty($accountancy_codes_list[
'proceeds_from_sales']) ?
'NotDefined' : $accountancy_codes_list[
'proceeds_from_sales'];
575 $last_cumulative_amount_ht = 0;
576 $depreciated_ids = array_keys($pre_data_info[
'depreciation']);
577 foreach ($depreciation_lines as $line) {
578 $last_cumulative_amount_ht = $line[
'cumulative_depreciation_ht'];
579 if (!in_array($line[
'id'], $depreciated_ids) && empty($line[
'bookkeeping']) && !empty($line[
'ref'])) {
585 $lines[0][$accountancy_code_value_asset_sold] = -((float) $element_static->acquisition_value_ht - $last_cumulative_amount_ht);
586 $lines[0][$accountancy_code_depreciation_asset] = - (float) $last_cumulative_amount_ht;
587 $lines[0][$accountancy_code_asset] = $element_static->acquisition_value_ht;
589 $disposal_amount_vat = $disposal_subject_to_vat ? (float)
price2num($disposal_amount * $disposal_vat / 100,
'MT') : 0;
590 $lines[1][$accountancy_code_receivable_on_assignment] = -($disposal_amount + $disposal_amount_vat);
591 if ($disposal_subject_to_vat) {
592 $lines[1][$accountancy_code_vat_collected] = $disposal_amount_vat;
594 $lines[1][$accountancy_code_proceeds_from_sales] = $disposal_amount;
596 foreach ($lines as $lines_block) {
598 foreach ($lines_block as $account => $mt) {
601 if ($type ==
'view') {
603 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
604 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
608 'date' => $disposal_date_formatted,
609 'piece' => $element_link,
610 'account_accounting' => $account_to_show,
611 'subledger_account' =>
'',
612 'label_operation' => $label_operation .
' - ' . $disposal_ref,
613 'debit' => $mt < 0 ?
price(-$mt) :
'',
614 'credit' => $mt >= 0 ?
price($mt) :
'',
616 } elseif ($type ==
'bookkeeping') {
617 if ($account_infos[
'found']) {
619 'doc_date' => $disposal_date,
620 'date_lim_reglement' =>
'',
621 'doc_ref' => $element_static->ref,
622 'date_creation' => $now,
623 'doc_type' =>
'asset',
624 'fk_doc' => $element_static->id,
626 'thirdparty_code' =>
'',
627 'subledger_account' =>
'',
628 'subledger_label' =>
'',
629 'numero_compte' => $account,
630 'label_compte' => $account_infos[
'label'],
631 'label_operation' => $element_name_formatted_0 .
' - ' . $disposal_ref,
633 'sens' => $mt < 0 ?
'D' :
'C',
634 'debit' => $mt < 0 ? -$mt : 0,
635 'credit' => $mt >= 0 ? $mt : 0,
636 'code_journal' => $journal,
637 'journal_label' => $journal_label_formatted,
640 'fk_user_author' => $user->id,
641 'entity' => $conf->entity,
647 $element_static->ref,
648 $account_infos[
'code_formatted_1'],
649 $element_name_formatted_0 .
' - ' . $disposal_ref,
650 $mt < 0 ?
price(-$mt) :
'',
651 $mt >= 0 ?
price($mt) :
'',
655 $element[
'blocks'][] = $blocks;
662 $journal_data[(int) $pre_data_id] = $element;
666 return $journal_data;
714 global $conf, $langs, $hookmanager;
715 require_once DOL_DOCUMENT_ROOT .
'/accountancy/class/bookkeeping.class.php';
719 $hookmanager->initHooks(array(
'accountingjournaldao'));
720 $parameters = array(
'journal_data' => &$journal_data);
721 $reshook = $hookmanager->executeHooks(
'writeBookkeeping', $parameters, $this);
723 $this->error = $hookmanager->error;
724 $this->errors = $hookmanager->errors;
726 } elseif (empty($reshook)) {
728 if (!is_array($journal_data)) {
729 $journal_data = array();
732 foreach ($journal_data as $element_id => $element) {
739 if ($element[
'error'] ==
'somelinesarenotbound') {
742 $this->errors[] = $langs->trans(
'ErrorInvoiceContainsLinesNotYetBounded', $element[
'ref']);
745 if (!$error_for_line) {
746 foreach ($element[
'blocks'] as $lines) {
747 foreach ($lines as $line) {
749 $bookkeeping->doc_date = (int) $line[
'doc_date'];
750 $bookkeeping->date_lim_reglement = (int) $line[
'date_lim_reglement'];
751 $bookkeeping->doc_ref = $line[
'doc_ref'];
752 $bookkeeping->date_creation = $line[
'date_creation'];
753 $bookkeeping->doc_type = $line[
'doc_type'];
754 $bookkeeping->fk_doc = $line[
'fk_doc'];
755 $bookkeeping->fk_docdet = $line[
'fk_docdet'];
756 $bookkeeping->thirdparty_code = $line[
'thirdparty_code'];
757 $bookkeeping->subledger_account = $line[
'subledger_account'];
758 $bookkeeping->subledger_label = $line[
'subledger_label'];
759 $bookkeeping->numero_compte = $line[
'numero_compte'];
760 $bookkeeping->label_compte = $line[
'label_compte'];
761 $bookkeeping->label_operation = $line[
'label_operation'];
762 $bookkeeping->montant = $line[
'montant'];
763 $bookkeeping->sens = $line[
'sens'];
764 $bookkeeping->debit = (float) $line[
'debit'];
765 $bookkeeping->credit = (float) $line[
'credit'];
766 $bookkeeping->code_journal = $line[
'code_journal'];
767 $bookkeeping->journal_label = $line[
'journal_label'];
768 $bookkeeping->piece_num = (int) $line[
'piece_num'];
769 $bookkeeping->import_key = $line[
'import_key'];
770 $bookkeeping->fk_user_author = $user->id;
771 $bookkeeping->entity = $conf->entity;
773 $total_debit += $bookkeeping->debit;
774 $total_credit += $bookkeeping->credit;
776 $result = $bookkeeping->create($user);
778 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
781 $journal_data[$element_id][
'error'] =
'alreadyjournalized';
785 $journal_data[$element_id][
'error'] =
'other';
786 $this->errors[] = $bookkeeping->errorsToString();
804 if ($error_for_line) {
811 if (!$error_for_line && (
price2num($total_debit,
'MT') !=
price2num($total_credit,
'MT'))) {
814 $journal_data[$element_id][
'error'] =
'amountsnotbalanced';
815 $this->errors[] =
'Try to insert a non balanced transaction in book for ' . json_encode($element[
'blocks']) .
'. Canceled. Surely a bug.';
818 if (!$error_for_line) {
821 $this->db->rollback();
823 if ($error >= $max_nb_errors) {
824 $this->errors[] = $langs->trans(
"ErrorTooManyErrorsProcessStopped");
831 return $error ? -$error : 1;
855 public function exportCsv(&$journal_data = array(), $search_date_end = 0, $sep =
'')
857 global $conf, $langs, $hookmanager;
865 $hookmanager->initHooks(array(
'accountingjournaldao'));
866 $parameters = array(
'journal_data' => &$journal_data,
'search_date_end' => &$search_date_end,
'sep' => &$sep,
'out' => &$out);
867 $reshook = $hookmanager->executeHooks(
'exportCsv', $parameters, $this);
869 $this->error = $hookmanager->error;
870 $this->errors = $hookmanager->errors;
872 } elseif (empty($reshook)) {
874 $journal_data = is_array($journal_data) ? $journal_data : array();
878 if ($this->nature == 4) {
880 $langs->transnoentitiesnoconv(
"BankId"),
881 $langs->transnoentitiesnoconv(
"Date"),
882 $langs->transnoentitiesnoconv(
"PaymentMode"),
883 $langs->transnoentitiesnoconv(
"AccountAccounting"),
884 $langs->transnoentitiesnoconv(
"LedgerAccount"),
885 $langs->transnoentitiesnoconv(
"SubledgerAccount"),
886 $langs->transnoentitiesnoconv(
"Label"),
887 $langs->transnoentitiesnoconv(
"AccountingDebit"),
888 $langs->transnoentitiesnoconv(
"AccountingCredit"),
889 $langs->transnoentitiesnoconv(
"Journal"),
890 $langs->transnoentitiesnoconv(
"Note"),
892 } elseif ($this->nature == 5) {
894 $langs->transnoentitiesnoconv(
"Date"),
895 $langs->transnoentitiesnoconv(
"Piece"),
896 $langs->transnoentitiesnoconv(
"AccountAccounting"),
897 $langs->transnoentitiesnoconv(
"LabelOperation"),
898 $langs->transnoentitiesnoconv(
"AccountingDebit"),
899 $langs->transnoentitiesnoconv(
"AccountingCredit"),
901 } elseif ($this->nature == 1) {
903 $langs->transnoentitiesnoconv(
"Date"),
904 $langs->transnoentitiesnoconv(
"Piece"),
905 $langs->transnoentitiesnoconv(
"AccountAccounting"),
906 $langs->transnoentitiesnoconv(
"LabelOperation"),
907 $langs->transnoentitiesnoconv(
"AccountingDebit"),
908 $langs->transnoentitiesnoconv(
"AccountingCredit"),
912 if (!empty($header)) {
913 $out .=
'"' . implode(
'"' . $sep .
'"', $header) .
'"' .
"\n";
915 foreach ($journal_data as $element_id => $element) {
916 foreach ($element[
'blocks'] as $lines) {
917 foreach ($lines as $line) {
918 $out .=
'"' . implode(
'"' . $sep .
'"', $line) .
'"' .
"\n";