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' => $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] = -($element_static->acquisition_value_ht - $last_cumulative_amount_ht);
586 $lines[0][$accountancy_code_depreciation_asset] = -$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 $journal_data = is_array($journal_data) ? $journal_data : array();
730 foreach ($journal_data as $element_id => $element) {
737 if ($element[
'error'] ==
'somelinesarenotbound') {
740 $this->errors[] = $langs->trans(
'ErrorInvoiceContainsLinesNotYetBounded', $element[
'ref']);
743 if (!$error_for_line) {
744 foreach ($element[
'blocks'] as $lines) {
745 foreach ($lines as $line) {
747 $bookkeeping->doc_date = $line[
'doc_date'];
748 $bookkeeping->date_lim_reglement = $line[
'date_lim_reglement'];
749 $bookkeeping->doc_ref = $line[
'doc_ref'];
750 $bookkeeping->date_creation = $line[
'date_creation'];
751 $bookkeeping->doc_type = $line[
'doc_type'];
752 $bookkeeping->fk_doc = $line[
'fk_doc'];
753 $bookkeeping->fk_docdet = $line[
'fk_docdet'];
754 $bookkeeping->thirdparty_code = $line[
'thirdparty_code'];
755 $bookkeeping->subledger_account = $line[
'subledger_account'];
756 $bookkeeping->subledger_label = $line[
'subledger_label'];
757 $bookkeeping->numero_compte = $line[
'numero_compte'];
758 $bookkeeping->label_compte = $line[
'label_compte'];
759 $bookkeeping->label_operation = $line[
'label_operation'];
760 $bookkeeping->montant = $line[
'montant'];
761 $bookkeeping->sens = $line[
'sens'];
762 $bookkeeping->debit = $line[
'debit'];
763 $bookkeeping->credit = $line[
'credit'];
764 $bookkeeping->code_journal = $line[
'code_journal'];
765 $bookkeeping->journal_label = $line[
'journal_label'];
766 $bookkeeping->piece_num = $line[
'piece_num'];
767 $bookkeeping->import_key = $line[
'import_key'];
768 $bookkeeping->fk_user_author = $user->id;
769 $bookkeeping->entity = $conf->entity;
771 $total_debit += $bookkeeping->debit;
772 $total_credit += $bookkeeping->credit;
774 $result = $bookkeeping->create($user);
776 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
779 $journal_data[$element_id][
'error'] =
'alreadyjournalized';
783 $journal_data[$element_id][
'error'] =
'other';
784 $this->errors[] = $bookkeeping->errorsToString();
802 if ($error_for_line) {
809 if (!$error_for_line && (
price2num($total_debit,
'MT') !=
price2num($total_credit,
'MT'))) {
812 $journal_data[$element_id][
'error'] =
'amountsnotbalanced';
813 $this->errors[] =
'Try to insert a non balanced transaction in book for ' . $element[
'blocks'] .
'. Canceled. Surely a bug.';
816 if (!$error_for_line) {
819 $this->db->rollback();
821 if ($error >= $max_nb_errors) {
822 $this->errors[] = $langs->trans(
"ErrorTooManyErrorsProcessStopped");
829 return $error ? -$error : 1;
853 public function exportCsv(&$journal_data = array(), $search_date_end = 0, $sep =
'')
855 global $conf, $langs, $hookmanager;
863 $hookmanager->initHooks(array(
'accountingjournaldao'));
864 $parameters = array(
'journal_data' => &$journal_data,
'search_date_end' => &$search_date_end,
'sep' => &$sep,
'out' => &$out);
865 $reshook = $hookmanager->executeHooks(
'exportCsv', $parameters, $this);
867 $this->error = $hookmanager->error;
868 $this->errors = $hookmanager->errors;
870 } elseif (empty($reshook)) {
872 $journal_data = is_array($journal_data) ? $journal_data : array();
876 if ($this->nature == 4) {
878 $langs->transnoentitiesnoconv(
"BankId"),
879 $langs->transnoentitiesnoconv(
"Date"),
880 $langs->transnoentitiesnoconv(
"PaymentMode"),
881 $langs->transnoentitiesnoconv(
"AccountAccounting"),
882 $langs->transnoentitiesnoconv(
"LedgerAccount"),
883 $langs->transnoentitiesnoconv(
"SubledgerAccount"),
884 $langs->transnoentitiesnoconv(
"Label"),
885 $langs->transnoentitiesnoconv(
"AccountingDebit"),
886 $langs->transnoentitiesnoconv(
"AccountingCredit"),
887 $langs->transnoentitiesnoconv(
"Journal"),
888 $langs->transnoentitiesnoconv(
"Note"),
890 } elseif ($this->nature == 5) {
892 $langs->transnoentitiesnoconv(
"Date"),
893 $langs->transnoentitiesnoconv(
"Piece"),
894 $langs->transnoentitiesnoconv(
"AccountAccounting"),
895 $langs->transnoentitiesnoconv(
"LabelOperation"),
896 $langs->transnoentitiesnoconv(
"AccountingDebit"),
897 $langs->transnoentitiesnoconv(
"AccountingCredit"),
899 } elseif ($this->nature == 1) {
901 $langs->transnoentitiesnoconv(
"Date"),
902 $langs->transnoentitiesnoconv(
"Piece"),
903 $langs->transnoentitiesnoconv(
"AccountAccounting"),
904 $langs->transnoentitiesnoconv(
"LabelOperation"),
905 $langs->transnoentitiesnoconv(
"AccountingDebit"),
906 $langs->transnoentitiesnoconv(
"AccountingCredit"),
910 if (!empty($header)) {
911 $out .=
'"' . implode(
'"' . $sep .
'"', $header) .
'"' .
"\n";
913 foreach ($journal_data as $element_id => $element) {
914 foreach ($element[
'blocks'] as $lines) {
915 foreach ($lines as $line) {
916 $out .=
'"' . implode(
'"' . $sep .
'"', $line) .
'"' .
"\n";