213 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle =
'', $notooltip = 0)
215 global $langs,
$conf, $hookmanager;
217 if (!empty(
$conf->dol_no_mouse_hover)) {
223 $url =
dolBuildUrl(DOL_URL_ROOT.
'/accountancy/admin/journals_list.php', [
'id' => 35]);
225 $label =
'<u>'.$langs->trans(
"ShowAccountingJournal").
'</u>';
226 if (!empty($this->code)) {
227 $label .=
'<br><b>'.$langs->trans(
'Code').
':</b> '.$this->code;
229 if (!empty($this->label)) {
230 $label .=
'<br><b>'.$langs->trans(
'Label').
':</b> '.$langs->transnoentities($this->label);
233 $label .=
' - '.$moretitle;
237 if (empty($notooltip)) {
239 $label = $langs->trans(
"ShowAccountingJournal");
240 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
242 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
243 $linkclose .=
' class="classfortooltip"';
246 $linkstart =
'<a href="'.$url.
'"';
247 $linkstart .= $linkclose.
'>';
256 $label_link = $this->code;
257 if ($withlabel == 1 && !empty($this->label)) {
258 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$langs->transnoentities($this->label).($nourl ?
'</span>' :
'');
260 if ($withlabel == 2 && !empty($this->nature)) {
261 $key = $langs->trans(
"AccountingJournalType".$this->nature);
262 $transferlabel = ($key !=
"AccountingJournalType".strtoupper($langs->trans((
string) $this->nature)) ? $key : $this->label);
263 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$transferlabel.($nourl ?
'</span>' :
'');
266 $result .= $linkstart;
268 $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);
270 if ($withpicto != 2) {
271 $result .= $label_link;
276 $hookmanager->initHooks(array(
'accountingjournaldao'));
277 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
278 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
280 $result = $hookmanager->resPrint;
282 $result .= $hookmanager->resPrint;
428 global
$conf, $langs;
434 require_once DOL_DOCUMENT_ROOT .
'/core/lib/accounting.lib.php';
435 require_once DOL_DOCUMENT_ROOT .
'/asset/class/asset.class.php';
436 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
437 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetdepreciationoptions.class.php';
439 $langs->loadLangs(array(
"assets"));
445 if (empty($in_bookkeeping)) {
446 $in_bookkeeping =
'notyet';
450 $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";
451 $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";
452 $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";
453 $sql .=
" FROM " . MAIN_DB_PREFIX .
"asset_depreciation as ad";
454 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"asset as a ON a.rowid = ad.fk_asset";
455 $sql .=
" WHERE a.entity IN (" .
getEntity(
'asset', 0) .
')';
456 $sql .=
" AND a.status > 0";
457 if ($in_bookkeeping ==
'already') {
458 $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')";
459 } elseif ($in_bookkeeping ==
'notyet') {
460 $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')";
462 $sql .=
" AND ad.ref != ''";
464 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate(
$date_start) .
"' AND ad.depreciation_date <= '" . $this->db->idate($date_end) .
"'";
468 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate(
getDolGlobalInt(
'ACCOUNTING_DATE_START_BINDING')) .
"'";
470 $sql .=
" ORDER BY ad.depreciation_date";
473 $resql = $this->db->query($sql);
475 $this->errors[] = $this->db->lasterror();
480 'elements' => array(),
482 while ($obj = $this->db->fetch_object($resql)) {
483 if (!isset($pre_data[
'elements'][$obj->rowid])) {
484 $pre_data[
'elements'][$obj->rowid] = array(
485 'ref' => $obj->asset_ref,
486 'label' => $obj->asset_label,
487 'acquisition_value_ht' => $obj->asset_acquisition_value_ht,
488 'depreciation' => array(),
492 if (isset($obj->asset_disposal_date)) {
493 $pre_data[
'elements'][$obj->rowid][
'disposal'] = array(
494 'date' => $this->db->jdate($obj->asset_disposal_date),
495 'amount' => $obj->asset_disposal_amount_ht,
496 'subject_to_vat' => !empty($obj->asset_disposal_subject_to_vat),
501 $compta_debit = empty($obj->accountancy_code_debit) ?
'NotDefined' : $obj->accountancy_code_debit;
502 $compta_credit = empty($obj->accountancy_code_credit) ?
'NotDefined' : $obj->accountancy_code_credit;
504 $pre_data[
'elements'][$obj->rowid][
'depreciation'][$obj->depreciation_id] = array(
505 'date' => $this->db->jdate($obj->depreciation_date),
506 'ref' => $obj->depreciation_ref,
508 $compta_debit => -$obj->depreciation_ht,
509 $compta_credit => $obj->depreciation_ht,
514 $disposal_ref = $langs->transnoentitiesnoconv(
'AssetDisposal');
515 $journal = $this->code;
516 $journal_label = $this->label;
517 $journal_label_formatted = $langs->transnoentities($journal_label);
520 $element_static =
new Asset($this->db);
522 $journal_data = array();
523 foreach ($pre_data[
'elements'] as $pre_data_id => $pre_data_info) {
524 $element_static->id = (int) $pre_data_id;
525 $element_static->ref = (string) $pre_data_info[
"ref"];
526 $element_static->label = (string) $pre_data_info[
"label"];
527 $element_static->acquisition_value_ht = $pre_data_info[
"acquisition_value_ht"];
528 $element_link = $element_static->getNomUrl(1,
'with_label');
530 $element_name_formatted_0 =
dol_trunc($element_static->label, 16);
531 $label_operation = $element_static->getNomUrl(0,
'label', 16);
534 'ref' =>
dol_trunc($element_static->ref, 16,
'right',
'UTF-8', 1),
535 'error' => array_key_exists(
'error', $pre_data_info) ? $pre_data_info[
'error'] :
'',
541 foreach ($pre_data_info[
'depreciation'] as $depreciation_id => $line) {
542 $depreciation_ref = $line[
"ref"];
543 $depreciation_date = $line[
"date"];
544 $depreciation_date_formatted =
dol_print_date($depreciation_date,
'day');
548 foreach ($line[
'lines'] as $account => $mt) {
551 if ($type ==
'view') {
553 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
554 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
558 'date' => $depreciation_date_formatted,
559 'piece' => $element_link,
560 'account_accounting' => $account_to_show,
561 'subledger_account' =>
'',
562 'label_operation' => $label_operation .
' - ' . $depreciation_ref,
563 'debit' => $mt < 0 ?
price(-$mt) :
'',
564 'credit' => $mt >= 0 ?
price($mt) :
'',
566 } elseif ($type ==
'bookkeeping') {
567 if ($account_infos[
'found']) {
569 'doc_date' => $depreciation_date,
570 'date_lim_reglement' =>
'',
571 'doc_ref' => $element_static->ref,
572 'date_creation' => $now,
573 'doc_type' =>
'asset',
574 'fk_doc' => $element_static->id,
575 'fk_docdet' => (
int) $depreciation_id,
576 'thirdparty_code' =>
'',
577 'subledger_account' =>
'',
578 'subledger_label' =>
'',
579 'numero_compte' => (
string) $account,
580 'label_compte' => $account_infos[
'label'],
581 'label_operation' => $element_name_formatted_0 .
' - ' . $depreciation_ref,
582 'montant' => (
float) $mt,
583 'sens' => $mt < 0 ?
'D' :
'C',
584 'debit' => $mt < 0 ? -$mt : 0,
585 'credit' => $mt >= 0 ? $mt : 0,
586 'code_journal' => $journal,
587 'journal_label' => $journal_label_formatted,
590 'fk_user_author' => $user->id,
591 'entity' =>
$conf->entity,
597 $element_static->ref,
598 $account_infos[
'code_formatted_1'],
599 $element_name_formatted_0 .
' - ' . $depreciation_ref,
600 $mt < 0 ?
price(-$mt) :
'',
601 $mt >= 0 ?
price($mt) :
'',
605 $element[
'blocks'][] = $blocks;
610 if (!empty($pre_data_info[
'disposal'])) {
611 $disposal_date = $pre_data_info[
'disposal'][
'date'];
616 $disposal_amount = $pre_data_info[
'disposal'][
'amount'];
617 $disposal_subject_to_vat = $pre_data_info[
'disposal'][
'subject_to_vat'];
623 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
625 $result = $accountancy_codes->fetchAccountancyCodes($element_static->id);
627 $element[
'error'] = $accountancy_codes->errorsToString();
630 $element_static->fetchDepreciationLines();
631 foreach ($element_static->depreciation_lines as $mode_key => $depreciation_lines) {
632 $accountancy_codes_list = $accountancy_codes->accountancy_codes[$mode_key];
634 if (!isset($accountancy_codes_list[
'value_asset_sold'])) {
638 $accountancy_code_value_asset_sold = empty($accountancy_codes_list[
'value_asset_sold']) ?
'NotDefined' : $accountancy_codes_list[
'value_asset_sold'];
639 $accountancy_code_depreciation_asset = empty($accountancy_codes_list[
'depreciation_asset']) ?
'NotDefined' : $accountancy_codes_list[
'depreciation_asset'];
640 $accountancy_code_asset = empty($accountancy_codes_list[
'asset']) ?
'NotDefined' : $accountancy_codes_list[
'asset'];
641 $accountancy_code_receivable_on_assignment = empty($accountancy_codes_list[
'receivable_on_assignment']) ?
'NotDefined' : $accountancy_codes_list[
'receivable_on_assignment'];
642 $accountancy_code_vat_collected = empty($accountancy_codes_list[
'vat_collected']) ?
'NotDefined' : $accountancy_codes_list[
'vat_collected'];
643 $accountancy_code_proceeds_from_sales = empty($accountancy_codes_list[
'proceeds_from_sales']) ?
'NotDefined' : $accountancy_codes_list[
'proceeds_from_sales'];
645 $last_cumulative_amount_ht = 0;
646 $depreciated_ids = array_keys($pre_data_info[
'depreciation']);
647 foreach ($depreciation_lines as $line) {
648 $last_cumulative_amount_ht = $line[
'cumulative_depreciation_ht'];
649 if (!in_array($line[
'id'], $depreciated_ids) && empty($line[
'bookkeeping']) && !empty($line[
'ref'])) {
655 $lines[0][$accountancy_code_value_asset_sold] = -((float) $element_static->acquisition_value_ht - $last_cumulative_amount_ht);
656 $lines[0][$accountancy_code_depreciation_asset] = - (float) $last_cumulative_amount_ht;
657 $lines[0][$accountancy_code_asset] = $element_static->acquisition_value_ht;
659 $disposal_amount_vat = $disposal_subject_to_vat ? (float)
price2num($disposal_amount * $disposal_vat / 100,
'MT') : 0;
660 $lines[1][$accountancy_code_receivable_on_assignment] = -($disposal_amount + $disposal_amount_vat);
661 if ($disposal_subject_to_vat) {
662 $lines[1][$accountancy_code_vat_collected] = $disposal_amount_vat;
664 $lines[1][$accountancy_code_proceeds_from_sales] = $disposal_amount;
666 foreach ($lines as $lines_block) {
668 foreach ($lines_block as $account => $mt) {
671 if ($type ==
'view') {
673 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
674 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
678 'date' => $disposal_date_formatted,
679 'piece' => $element_link,
680 'account_accounting' => $account_to_show,
681 'subledger_account' =>
'',
682 'label_operation' => $label_operation .
' - ' . $disposal_ref,
683 'debit' => $mt < 0 ?
price(-$mt) :
'',
684 'credit' => $mt >= 0 ?
price($mt) :
'',
686 } elseif ($type ==
'bookkeeping') {
687 if ($account_infos[
'found']) {
689 'doc_date' => $disposal_date,
690 'date_lim_reglement' =>
'',
691 'doc_ref' => $element_static->ref,
692 'date_creation' => $now,
693 'doc_type' =>
'asset',
694 'fk_doc' => $element_static->id,
696 'thirdparty_code' =>
'',
697 'subledger_account' =>
'',
698 'subledger_label' =>
'',
699 'numero_compte' => (
string) $account,
700 'label_compte' => $account_infos[
'label'],
701 'label_operation' => $element_name_formatted_0 .
' - ' . $disposal_ref,
702 'montant' => (
float) $mt,
703 'sens' => $mt < 0 ?
'D' :
'C',
704 'debit' => $mt < 0 ? -$mt : 0,
705 'credit' => $mt >= 0 ? $mt : 0,
706 'code_journal' => $journal,
707 'journal_label' => $journal_label_formatted,
710 'fk_user_author' => $user->id,
711 'entity' =>
$conf->entity,
717 $element_static->ref,
718 $account_infos[
'code_formatted_1'],
719 $element_name_formatted_0 .
' - ' . $disposal_ref,
720 $mt < 0 ?
price(-$mt) :
'',
721 $mt >= 0 ?
price($mt) :
'',
725 $element[
'blocks'][] = $blocks;
732 $journal_data[(int) $pre_data_id] = $element;
736 return $journal_data;
751 global
$conf, $langs, $hookmanager;
753 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
754 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
755 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
757 $langs->loadLangs(array(
'bills'));
763 if (empty($in_bookkeeping)) {
764 $in_bookkeeping =
'notyet';
768 $sql =
"SELECT f.rowid, f.ref, f.datef, f.date_closing, f.fk_soc, f.total_ttc";
769 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
770 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice', 0).
')';
771 $sql .=
" AND f.fk_statut > 0";
778 $hookmanager->initHooks(array(
'accountingjournaldao'));
779 $parameters = array(
'invoicealias' =>
'f',
'issupplier' => 0,
'datefield' =>
'datef');
780 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $this);
781 $sql .= $hookmanager->resPrint;
782 $sql .=
" AND f.close_code = 'discount_vat'";
784 $sql .=
" AND f.date_closing >= '".$this->db->idate(
$date_start).
"' AND f.date_closing <= '".$this->db->idate($date_end).
"'";
787 $sql .=
" AND f.date_closing >= '".$this->db->idate(
getDolGlobalInt(
'ACCOUNTING_DATE_START_BINDING')).
"'";
789 if ($in_bookkeeping ==
'already') {
790 $sql .=
" AND EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
791 $sql .=
" WHERE ab.doc_type = 'customer_invoice' AND ab.fk_doc = f.rowid";
792 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
793 } elseif ($in_bookkeeping ==
'notyet') {
794 $sql .=
" AND NOT EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
795 $sql .=
" WHERE ab.doc_type = 'customer_invoice' AND ab.fk_doc = f.rowid";
796 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
798 $sql .=
" ORDER BY f.date_closing";
801 $resql = $this->db->query($sql);
803 $this->errors[] = $this->db->lasterror();
807 $journal = $this->code;
808 $journal_label_formatted = $langs->transnoentities($this->label);
811 $journal_data = array();
812 $invoice_static =
new Facture($this->db);
813 $customer_static =
new Societe($this->db);
819 while ($obj = $this->db->fetch_object($resql)) {
820 if ($invoice_static->fetch((
int) $obj->rowid) <= 0) {
824 $customer_static->fetch($invoice_static->socid);
825 $account_customer_general = !empty($customer_static->accountancy_code_customer_general) ? $customer_static->accountancy_code_customer_general :
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER');
826 $account_customer_subsidiary = !empty($customer_static->code_compta_client) ? $customer_static->code_compta_client :
'';
828 $piece_link = $invoice_static->getNomUrl(1,
'withlabel');
831 $paid = (float)
price2num($invoice_static->getSommePaiement(),
'MT');
832 $usedcn = (float)
price2num($invoice_static->getSumCreditNotesUsed(),
'MT');
833 $useddep = (float)
price2num($invoice_static->getSumDepositsUsed(),
'MT');
834 $ttc_inv = (float)
price2num($invoice_static->total_ttc,
'MT');
835 $escompte_ttc = (float)
price2num(max(0, $ttc_inv - $paid - $usedcn - $useddep),
'MT');
836 if ($escompte_ttc <= 0) {
841 $thirdpartyname = (string) $customer_static->name;
842 $label_discount = $bookkeeping_static->accountingLabelForOperation($thirdpartyname, $invoice_static->ref, $langs->trans(
'DiscountGranted'));
845 $ttcByRate = array();
847 foreach ((array) $invoice_static->lines as $li) {
848 $ttc = (float) $li->total_ttc;
852 $key = number_format((
float) $li->tva_tx, 3,
'.',
'');
853 if (!isset($ttcByRate[$key])) {
854 $ttcByRate[$key] = 0.0;
856 $ttcByRate[$key] += $ttc;
859 if ($totalTTC <= 0) {
860 $ttcByRate = array(
"0.000" => $escompte_ttc);
861 $totalTTC = $escompte_ttc;
865 'ref' =>
dol_trunc($invoice_static->ref, 16,
'right',
'UTF-8', 1),
870 $closingdate = !empty($obj->date_closing) ? $obj->date_closing : $obj->datef;
872 $docdate = $this->db->jdate($closingdate);
877 $n = count($ttcByRate);
878 foreach ($ttcByRate as $rateStr => $ttcRateOnInvoice) {
880 $rate = (float) $rateStr;
882 $ttc_part = (float) $escompte_ttc * ($ttcRateOnInvoice / $totalTTC);
884 $ttc_part = (float)
price2num($escompte_ttc - $sumTTC,
'MT');
886 $ttc_part = (float)
price2num($ttc_part,
'MT');
887 $sumTTC = (float)
price2num($sumTTC + $ttc_part,
'MT');
891 $ht_part = (float)
price2num($ttc_part / (1 + $rate / 100),
'MT');
892 $tva_part = (float)
price2num($ttc_part - $ht_part,
'MT');
894 $ht_part = $ttc_part;
900 $acc_vat_coll = $acc_vat_coll_def;
902 $lines_view = array();
903 $lines_book = array();
907 if ($type ==
'view') {
908 $lines_view[] = array(
909 'date' => $docdate_fmt,
910 'piece' => $piece_link,
912 'subledger_account' =>
'',
913 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
914 'debit' =>
price($ht_part),
917 } elseif ($type ==
'bookkeeping' && $acc_info_discountgranted[
'found']) {
918 $lines_book[] = array(
919 'doc_date' => $docdate,
920 'date_lim_reglement' =>
'',
921 'doc_ref' => $invoice_static->ref,
922 'date_creation' => $now,
923 'doc_type' =>
'customer_invoice',
924 'fk_doc' => $invoice_static->id,
926 'thirdparty_code' => $customer_static->code_client,
927 'subledger_account' =>
'',
928 'subledger_label' =>
'',
929 'numero_compte' => (
string) $acc_disc_granted,
930 'label_compte' => $acc_info_discountgranted[
'label'],
931 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
932 'montant' => $ht_part,
936 'code_journal' => $journal,
937 'journal_label' => $journal_label_formatted,
938 'piece_num' =>
'OD-ESC-'.$invoice_static->ref,
940 'fk_user_author' => $user->id,
941 'entity' =>
$conf->entity,
948 if ($type ==
'view') {
949 $lines_view[] = array(
950 'date' => $docdate_fmt,
951 'piece' => $piece_link,
953 'subledger_account' =>
'',
954 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
955 'debit' =>
price($tva_part),
958 } elseif ($type ==
'bookkeeping' && $acc_info_vatbuy[
'found']) {
959 $lines_book[] = array(
960 'doc_date' => $docdate,
961 'date_lim_reglement' =>
'',
962 'doc_ref' => $invoice_static->ref,
963 'date_creation' => $now,
964 'doc_type' =>
'customer_invoice',
965 'fk_doc' => $invoice_static->id,
967 'thirdparty_code' => $customer_static->code_client,
968 'subledger_account' =>
'',
969 'subledger_label' =>
'',
970 'numero_compte' => (
string) $acc_vat_coll,
971 'label_compte' => $acc_info_vatbuy[
'label'],
972 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
973 'montant' => $tva_part,
975 'debit' => $tva_part,
977 'code_journal' => $journal,
978 'journal_label' => $journal_label_formatted,
979 'piece_num' =>
'OD-ESC-'.$invoice_static->ref,
981 'fk_user_author' => $user->id,
982 'entity' =>
$conf->entity,
989 if ($type ==
'view') {
990 $lines_view[] = array(
991 'date' => $docdate_fmt,
992 'piece' => $piece_link,
995 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Customer'),
997 'credit' =>
price($ttc_part),
999 $element[
'blocks'][] = $lines_view;
1000 } elseif ($type ==
'bookkeeping' && $acc_info_customeraccount[
'found']) {
1001 $lines_book[] = array(
1002 'doc_date' => $docdate,
1003 'date_lim_reglement' =>
'',
1004 'doc_ref' => $invoice_static->ref,
1005 'date_creation' => $now,
1006 'doc_type' =>
'customer_invoice',
1007 'fk_doc' => $invoice_static->id,
1009 'thirdparty_code' => $customer_static->code_client,
1010 'subledger_account' => $account_customer_subsidiary,
1011 'subledger_label' => $customer_static->name,
1012 'numero_compte' => (
string) $account_customer_general,
1013 'label_compte' => $acc_info_customeraccount[
'label'],
1014 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Customer'),
1015 'montant' => $ttc_part,
1018 'credit' => $ttc_part,
1019 'code_journal' => $journal,
1020 'journal_label' => $journal_label_formatted,
1021 'piece_num' =>
'OD-ESC-'.$invoice_static->ref,
1023 'fk_user_author' => $user->id,
1024 'entity' =>
$conf->entity,
1026 $element[
'blocks'][] = $lines_book;
1028 $element[
'blocks'][] = array(
1030 $invoice_static->ref,
1032 $label_discount.
" (".$rateStr.
"%)",
1036 if ($tva_part > 0) {
1037 $element[
'blocks'][] = array(
1038 $docdate, $invoice_static->ref,
length_accountg($acc_vat_coll), $label_discount.
" ". $langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
price($tva_part),
''
1041 $element[
'blocks'][] = array(
1042 $docdate, $invoice_static->ref,
length_accountg($account_customer_general), $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Customer'),
'',
price($ttc_part)
1047 $journal_data[(int) $invoice_static->id] = $element;
1050 return $journal_data;
1065 global
$conf, $langs, $hookmanager;
1067 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
1068 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
1069 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
1071 $langs->loadLangs(array(
'suppliers'));
1077 if (empty($in_bookkeeping)) {
1078 $in_bookkeeping =
'notyet';
1082 $sql =
"SELECT ff.rowid, ff.ref, ff.datef, ff.date_closing, ff.fk_soc, ff.total_ttc";
1083 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as ff";
1084 $sql .=
" WHERE ff.entity IN (".getEntity(
'facture_fourn', 0).
")";
1085 $sql .=
" AND ff.fk_statut > 0";
1092 $hookmanager->initHooks(array(
'accountingjournaldao'));
1093 $parameters = array(
'invoicealias' =>
'ff',
'issupplier' => 1,
'datefield' =>
'datef');
1094 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $this);
1095 $sql .= $hookmanager->resPrint;
1096 $sql .=
" AND ff.close_code = 'discount_vat'";
1098 $sql .=
" AND ff.date_closing >= '".$this->db->idate(
$date_start).
"' AND ff.date_closing <= '".$this->db->idate($date_end).
"'";
1101 $sql .=
" AND ff.date_closing >= '".$this->db->idate(
getDolGlobalInt(
'ACCOUNTING_DATE_START_BINDING')).
"'";
1103 if ($in_bookkeeping ==
'already') {
1104 $sql .=
" AND EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
1105 $sql .=
" WHERE ab.doc_type = 'supplier_invoice' AND ab.fk_doc = ff.rowid";
1106 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
1107 } elseif ($in_bookkeeping ==
'notyet') {
1108 $sql .=
" AND NOT EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
1109 $sql .=
" WHERE ab.doc_type = 'supplier_invoice' AND ab.fk_doc = ff.rowid";
1110 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
1112 $sql .=
" ORDER BY ff.date_closing";
1115 $resql = $this->db->query($sql);
1117 $this->errors[] = $this->db->lasterror();
1121 $journal = $this->code;
1122 $journal_label_formatted = $langs->transnoentities($this->label);
1125 $journal_data = array();
1127 $supplier_static =
new Societe($this->db);
1133 while ($obj = $this->db->fetch_object($resql)) {
1134 if ($invoicesupplier_static->fetch((
int) $obj->rowid) <= 0) {
1138 $supplier_static->fetch($invoicesupplier_static->socid);
1139 $account_supplier_general = !empty($supplier_static->accountancy_code_supplier_general) ? $supplier_static->accountancy_code_supplier_general :
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER');
1140 $account_supplier_subsidiary = !empty($supplier_static->code_compta_fournisseur) ? $supplier_static->code_compta_fournisseur :
'';
1142 $piece_link = $invoicesupplier_static->getNomUrl(1,
'withlabel');
1145 $paid = (float)
price2num($invoicesupplier_static->getSommePaiement(),
'MT');
1146 $usedcn = (float)
price2num($invoicesupplier_static->getSumCreditNotesUsed(),
'MT');
1147 $useddep = (float)
price2num($invoicesupplier_static->getSumDepositsUsed(),
'MT');
1148 $ttc_inv = (float)
price2num($invoicesupplier_static->total_ttc,
'MT');
1149 $escompte_ttc = (float)
price2num(max(0, $ttc_inv - $paid - $usedcn - $useddep),
'MT');
1150 if ($escompte_ttc <= 0) {
1155 $thirdpartyname = (string) $supplier_static->name;
1156 $label_discount = $bookkeeping_static->accountingLabelForOperation($thirdpartyname, $invoicesupplier_static->ref, $langs->trans(
'DiscountReceived'));
1159 $ttcByRate = array();
1161 foreach ((array) $invoicesupplier_static->lines as $li) {
1162 $ttc = (float) $li->total_ttc;
1166 $key = number_format((
float) $li->tva_tx, 3,
'.',
'');
1167 if (!isset($ttcByRate[$key])) {
1168 $ttcByRate[$key] = 0.0;
1170 $ttcByRate[$key] += $ttc;
1173 if ($totalTTC <= 0) {
1174 $ttcByRate = array(
"0.000" => $escompte_ttc);
1175 $totalTTC = $escompte_ttc;
1179 'ref' =>
dol_trunc($invoicesupplier_static->ref, 16,
'right',
'UTF-8', 1),
1181 'blocks' => array(),
1184 $closingdate = !empty($obj->date_closing) ? $obj->date_closing : $obj->datef;
1186 $docdate = $this->db->jdate($closingdate);
1191 $n = count($ttcByRate);
1192 foreach ($ttcByRate as $rateStr => $ttcRateOnInvoice) {
1194 $rate = (float) $rateStr;
1196 $ttc_part = $escompte_ttc * ($ttcRateOnInvoice / $totalTTC);
1198 $ttc_part = (float)
price2num($escompte_ttc - $sumTTC,
'MT');
1200 $ttc_part = (float)
price2num($ttc_part,
'MT');
1201 $sumTTC = (float)
price2num($sumTTC + $ttc_part,
'MT');
1205 $ht_part = (float)
price2num($ttc_part / (1 + $rate / 100),
'MT');
1206 $tva_part = (float)
price2num($ttc_part - $ht_part,
'MT');
1208 $ht_part = $ttc_part;
1214 $acc_vat_ded = $acc_vat_ded_def;
1216 $lines_view = array();
1217 $lines_book = array();
1221 if ($type ==
'view') {
1222 $lines_view[] = array(
1223 'date' => $docdate_fmt,
1224 'piece' => $piece_link,
1227 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Supplier'),
1228 'debit' =>
price($ttc_part),
1231 } elseif ($type ==
'bookkeeping' && $acc_info_supplieraccount[
'found']) {
1232 $lines_book[] = array(
1233 'doc_date' => $docdate,
1234 'date_lim_reglement' =>
'',
1235 'doc_ref' => $invoicesupplier_static->ref,
1236 'date_creation' => $now,
1237 'doc_type' =>
'supplier_invoice',
1238 'fk_doc' => $invoicesupplier_static->id,
1240 'thirdparty_code' => $supplier_static->code_fournisseur,
1241 'subledger_account' => $account_supplier_subsidiary,
1242 'subledger_label' => $supplier_static->name,
1243 'numero_compte' => $account_supplier_general,
1244 'label_compte' => $acc_info_supplieraccount[
'label'],
1245 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Supplier'),
1246 'montant' => $ttc_part,
1248 'debit' => $ttc_part,
1250 'code_journal' => $journal,
1251 'journal_label' => $journal_label_formatted,
1252 'piece_num' =>
'OD-ESC-FRS-'.$invoicesupplier_static->ref,
1254 'fk_user_author' => $user->id,
1255 'entity' =>
$conf->entity,
1261 if ($type ==
'view') {
1262 $lines_view[] = array(
1263 'date' => $docdate_fmt,
1264 'piece' => $piece_link,
1266 'subledger_account' =>
'',
1267 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
1269 'credit' =>
price($ht_part),
1271 } elseif ($type ==
'bookkeeping' && $acc_info_discountreceived[
'found']) {
1272 $lines_book[] = array(
1273 'doc_date' => $docdate,
1274 'date_lim_reglement' =>
'',
1275 'doc_ref' => $invoicesupplier_static->ref,
1276 'date_creation' => $now,
1277 'doc_type' =>
'supplier_invoice',
1278 'fk_doc' => $invoicesupplier_static->id,
1280 'thirdparty_code' => $supplier_static->code_fournisseur,
1281 'subledger_account' =>
'',
1282 'subledger_label' =>
'',
1283 'numero_compte' => $acc_disc_recv,
1284 'label_compte' => $acc_info_discountreceived[
'label'],
1285 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
1286 'montant' => $ht_part,
1289 'credit' => $ht_part,
1290 'code_journal' => $journal,
1291 'journal_label' => $journal_label_formatted,
1292 'piece_num' =>
'OD-ESC-FRS-'.$invoicesupplier_static->ref,
1294 'fk_user_author' => $user->id,
1295 'entity' =>
$conf->entity,
1300 if ($tva_part > 0) {
1302 if ($type ==
'view') {
1303 $lines_view[] = array(
1304 'date' => $docdate_fmt,
1305 'piece' => $piece_link,
1307 'subledger_account' =>
'',
1308 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
1310 'credit' =>
price($tva_part),
1312 $element[
'blocks'][] = $lines_view;
1313 } elseif ($type ==
'bookkeeping' && $acc_info_vatbuy[
'found']) {
1314 $lines_book[] = array(
1315 'doc_date' => $docdate,
1316 'date_lim_reglement' =>
'',
1317 'doc_ref' => $invoicesupplier_static->ref,
1318 'date_creation' => $now,
1319 'doc_type' =>
'supplier_invoice',
1320 'fk_doc' => $invoicesupplier_static->id,
1322 'thirdparty_code' => $supplier_static->code_fournisseur,
1323 'subledger_account' =>
'',
1324 'subledger_label' =>
'',
1325 'numero_compte' => $acc_vat_ded,
1326 'label_compte' => $acc_info_vatbuy[
'label'],
1327 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
1328 'montant' => $tva_part,
1331 'credit' => $tva_part,
1332 'code_journal' => $journal,
1333 'journal_label' => $journal_label_formatted,
1334 'piece_num' =>
'OD-ESC-FRS-'.$invoicesupplier_static->ref,
1336 'fk_user_author' => $user->id,
1337 'entity' =>
$conf->entity,
1339 $element[
'blocks'][] = $lines_book;
1343 if ($type ==
'view') {
1344 $element[
'blocks'][] = $lines_view;
1345 } elseif ($type ==
'bookkeeping') {
1346 $element[
'blocks'][] = $lines_book;
1348 $element[
'blocks'][] = array($docdate, $invoicesupplier_static->ref,
length_accountg($account_supplier_general), $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Supplier'),
price($ttc_part),
'');
1349 $element[
'blocks'][] = array($docdate, $invoicesupplier_static->ref,
length_accountg($acc_disc_recv), $label_discount.
' ('.$rateStr.
'%)',
'',
price($ht_part));
1354 if ($type ==
'csv') {
1355 $element[
'blocks'][] = array(
1356 $docdate, $invoicesupplier_static->ref,
length_accountg($acc_vat_ded), $label_discount.
" ". $langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
'', $tva_part > 0 ?
price($tva_part) :
''
1361 $journal_data[(int) $invoicesupplier_static->id] = $element;
1364 return $journal_data;
1412 global
$conf, $langs, $hookmanager;
1413 require_once DOL_DOCUMENT_ROOT .
'/accountancy/class/bookkeeping.class.php';
1417 $hookmanager->initHooks(array(
'accountingjournaldao'));
1418 $parameters = array(
'journal_data' => &$journal_data);
1419 $reshook = $hookmanager->executeHooks(
'writeBookkeeping', $parameters, $this);
1421 $this->error = $hookmanager->error;
1422 $this->errors = $hookmanager->errors;
1424 } elseif (empty($reshook)) {
1426 if (!is_array($journal_data)) {
1427 $journal_data = array();
1430 foreach ($journal_data as $element_id => $element) {
1431 $error_for_line = 0;
1437 if ($element[
'error'] ==
'somelinesarenotbound') {
1440 $this->errors[] = $langs->trans(
'ErrorInvoiceContainsLinesNotYetBounded', $element[
'ref']);
1443 if (!$error_for_line) {
1444 foreach ($element[
'blocks'] as $lines) {
1445 foreach ($lines as $line) {
1447 $bookkeeping->doc_date = (int) $line[
'doc_date'];
1448 $bookkeeping->date_lim_reglement = (int) $line[
'date_lim_reglement'];
1449 $bookkeeping->doc_ref = $line[
'doc_ref'];
1450 $bookkeeping->date_creation = $line[
'date_creation'];
1451 $bookkeeping->doc_type = $line[
'doc_type'];
1452 $bookkeeping->fk_doc = $line[
'fk_doc'];
1453 $bookkeeping->fk_docdet = $line[
'fk_docdet'];
1454 $bookkeeping->thirdparty_code = $line[
'thirdparty_code'];
1455 $bookkeeping->subledger_account = $line[
'subledger_account'];
1456 $bookkeeping->subledger_label = $line[
'subledger_label'];
1457 $bookkeeping->numero_compte = $line[
'numero_compte'];
1458 $bookkeeping->label_compte = $line[
'label_compte'];
1459 $bookkeeping->label_operation = $line[
'label_operation'];
1460 $bookkeeping->montant = $line[
'montant'];
1461 $bookkeeping->sens = $line[
'sens'];
1462 $bookkeeping->debit = (float) $line[
'debit'];
1463 $bookkeeping->credit = (float) $line[
'credit'];
1464 $bookkeeping->code_journal = $line[
'code_journal'];
1465 $bookkeeping->journal_label = $line[
'journal_label'];
1466 $bookkeeping->piece_num = (int) $line[
'piece_num'];
1467 $bookkeeping->import_key = $line[
'import_key'];
1468 $bookkeeping->fk_user_author = $user->id;
1469 $bookkeeping->entity =
$conf->entity;
1471 $total_debit += $bookkeeping->debit;
1472 $total_credit += $bookkeeping->credit;
1474 $result = $bookkeeping->create($user);
1476 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
1479 $journal_data[$element_id][
'error'] =
'alreadyjournalized';
1483 $journal_data[$element_id][
'error'] =
'other';
1484 $this->errors[] = $bookkeeping->errorsToString();
1502 if ($error_for_line) {
1509 if (!$error_for_line && (
price2num($total_debit,
'MT') !=
price2num($total_credit,
'MT'))) {
1512 $journal_data[$element_id][
'error'] =
'amountsnotbalanced';
1513 $this->errors[] =
'Try to insert a non balanced transaction in book for ' . json_encode($element[
'blocks']) .
'. Canceled. Surely a bug.';
1516 if (!$error_for_line) {
1517 $this->db->commit();
1519 $this->db->rollback();
1521 if ($error >= $max_nb_errors) {
1522 $this->errors[] = $langs->trans(
"ErrorTooManyErrorsProcessStopped");
1529 return $error ? -$error : 1;
1553 public function exportCsv(&$journal_data = array(), $search_date_end = 0, $sep =
'')
1555 global
$conf, $langs, $hookmanager;
1563 $hookmanager->initHooks(array(
'accountingjournaldao'));
1564 $parameters = array(
'journal_data' => &$journal_data,
'search_date_end' => &$search_date_end,
'sep' => &$sep,
'out' => &$out);
1565 $reshook = $hookmanager->executeHooks(
'exportCsv', $parameters, $this);
1567 $this->error = $hookmanager->error;
1568 $this->errors = $hookmanager->errors;
1570 } elseif (empty($reshook)) {
1572 $journal_data = is_array($journal_data) ? $journal_data : array();
1576 if ($this->nature == 4) {
1578 $langs->transnoentitiesnoconv(
"BankId"),
1579 $langs->transnoentitiesnoconv(
"Date"),
1580 $langs->transnoentitiesnoconv(
"PaymentMode"),
1581 $langs->transnoentitiesnoconv(
"AccountAccounting"),
1582 $langs->transnoentitiesnoconv(
"LedgerAccount"),
1583 $langs->transnoentitiesnoconv(
"SubledgerAccount"),
1584 $langs->transnoentitiesnoconv(
"Label"),
1585 $langs->transnoentitiesnoconv(
"AccountingDebit"),
1586 $langs->transnoentitiesnoconv(
"AccountingCredit"),
1587 $langs->transnoentitiesnoconv(
"Journal"),
1588 $langs->transnoentitiesnoconv(
"Note"),
1590 } elseif ($this->nature == 5) {
1592 $langs->transnoentitiesnoconv(
"Date"),
1593 $langs->transnoentitiesnoconv(
"Piece"),
1594 $langs->transnoentitiesnoconv(
"AccountAccounting"),
1595 $langs->transnoentitiesnoconv(
"LabelOperation"),
1596 $langs->transnoentitiesnoconv(
"AccountingDebit"),
1597 $langs->transnoentitiesnoconv(
"AccountingCredit"),
1599 } elseif ($this->nature == 1) {
1601 $langs->transnoentitiesnoconv(
"Date"),
1602 $langs->transnoentitiesnoconv(
"Piece"),
1603 $langs->transnoentitiesnoconv(
"AccountAccounting"),
1604 $langs->transnoentitiesnoconv(
"LabelOperation"),
1605 $langs->transnoentitiesnoconv(
"AccountingDebit"),
1606 $langs->transnoentitiesnoconv(
"AccountingCredit"),
1610 if (!empty($header)) {
1611 $out .=
'"' . implode(
'"' . $sep .
'"', $header) .
'"' .
"\n";
1613 foreach ($journal_data as $element_id => $element) {
1614 foreach ($element[
'blocks'] as $lines) {
1615 foreach ($lines as $line) {
1616 $out .=
'"' . implode(
'"' . $sep .
'"', $line) .
'"' .
"\n";