214 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle =
'', $notooltip = 0)
216 global $langs,
$conf, $hookmanager;
218 if (!empty(
$conf->dol_no_mouse_hover)) {
224 $url =
dolBuildUrl(DOL_URL_ROOT.
'/accountancy/admin/journals_list.php', [
'id' => 35]);
226 $label =
'<u>'.$langs->trans(
"ShowAccountingJournal").
'</u>';
227 if (!empty($this->code)) {
228 $label .=
'<br><b>'.$langs->trans(
'Code').
':</b> '.$this->code;
230 if (!empty($this->label)) {
231 $label .=
'<br><b>'.$langs->trans(
'Label').
':</b> '.$langs->transnoentities($this->label);
234 $label .=
' - '.$moretitle;
238 if (empty($notooltip)) {
240 $label = $langs->trans(
"ShowAccountingJournal");
241 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
243 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
244 $linkclose .=
' class="classfortooltip"';
247 $linkstart =
'<a href="'.$url.
'"';
248 $linkstart .= $linkclose.
'>';
257 $label_link = $this->code;
258 if ($withlabel == 1 && !empty($this->label)) {
259 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$langs->transnoentities($this->label).($nourl ?
'</span>' :
'');
261 if ($withlabel == 2 && !empty($this->nature)) {
262 $key = $langs->trans(
"AccountingJournalType".$this->nature);
263 $transferlabel = ($key !=
"AccountingJournalType".strtoupper($langs->trans((
string) $this->nature)) ? $key : $this->label);
264 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$transferlabel.($nourl ?
'</span>' :
'');
267 $result .= $linkstart;
269 $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);
271 if ($withpicto != 2) {
272 $result .= $label_link;
277 $hookmanager->initHooks(array(
'accountingjournaldao'));
278 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
279 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
281 $result = $hookmanager->resPrint;
283 $result .= $hookmanager->resPrint;
435 require_once DOL_DOCUMENT_ROOT .
'/core/lib/accounting.lib.php';
436 require_once DOL_DOCUMENT_ROOT .
'/asset/class/asset.class.php';
437 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
438 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetdepreciationoptions.class.php';
440 $langs->loadLangs(array(
"assets"));
446 if (empty($in_bookkeeping)) {
447 $in_bookkeeping =
'notyet';
451 $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";
452 $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";
453 $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";
454 $sql .=
" FROM " . MAIN_DB_PREFIX .
"asset_depreciation as ad";
455 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"asset as a ON a.rowid = ad.fk_asset";
456 $sql .=
" WHERE a.entity IN (" .
getEntity(
'asset', 0) .
')';
457 $sql .=
" AND a.status > 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 != ''";
465 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate(
$date_start) .
"' AND ad.depreciation_date <= '" . $this->db->idate($date_end) .
"'";
469 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate(
getDolGlobalInt(
'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');
518 if (!($disposal_vat > 0)) {
521 $journal = $this->code;
522 $journal_label = $this->label;
523 $journal_label_formatted = $langs->transnoentities($journal_label);
526 $element_static =
new Asset($this->db);
528 $journal_data = array();
529 foreach ($pre_data[
'elements'] as $pre_data_id => $pre_data_info) {
530 $element_static->id = (int) $pre_data_id;
531 $element_static->ref = (string) $pre_data_info[
"ref"];
532 $element_static->label = (string) $pre_data_info[
"label"];
533 $element_static->acquisition_value_ht = $pre_data_info[
"acquisition_value_ht"];
534 $element_link = $element_static->getNomUrl(1,
'with_label');
536 $element_name_formatted_0 =
dol_trunc($element_static->label, 16);
537 $label_operation = $element_static->getNomUrl(0,
'label', 16);
540 'ref' =>
dol_trunc($element_static->ref, 16,
'right',
'UTF-8', 1),
541 'error' => array_key_exists(
'error', $pre_data_info) ? $pre_data_info[
'error'] :
'',
547 foreach ($pre_data_info[
'depreciation'] as $depreciation_id => $line) {
548 $depreciation_ref = $line[
"ref"];
549 $depreciation_date = $line[
"date"];
550 $depreciation_date_formatted =
dol_print_date($depreciation_date,
'day');
554 foreach ($line[
'lines'] as $account => $mt) {
557 if ($type ==
'view') {
559 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
560 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
564 'date' => $depreciation_date_formatted,
565 'piece' => $element_link,
566 'account_accounting' => $account_to_show,
567 'subledger_account' =>
'',
568 'label_operation' => $label_operation .
' - ' . $depreciation_ref,
569 'debit' => $mt < 0 ?
price(-$mt) :
'',
570 'credit' => $mt >= 0 ?
price($mt) :
'',
572 } elseif ($type ==
'bookkeeping') {
573 if ($account_infos[
'found']) {
575 'doc_date' => $depreciation_date,
576 'date_lim_reglement' =>
'',
577 'doc_ref' => $element_static->ref,
578 'date_creation' => $now,
579 'doc_type' =>
'asset',
580 'fk_doc' => $element_static->id,
581 'fk_docdet' => (
int) $depreciation_id,
582 'thirdparty_code' =>
'',
583 'subledger_account' =>
'',
584 'subledger_label' =>
'',
585 'numero_compte' => (
string) $account,
586 'label_compte' => $account_infos[
'label'],
587 'label_operation' => $element_name_formatted_0 .
' - ' . $depreciation_ref,
588 'montant' => (
float) $mt,
589 'sens' => $mt < 0 ?
'D' :
'C',
590 'debit' => $mt < 0 ? -$mt : 0,
591 'credit' => $mt >= 0 ? $mt : 0,
592 'code_journal' => $journal,
593 'journal_label' => $journal_label_formatted,
596 'fk_user_author' => $user->id,
597 'entity' =>
$conf->entity,
603 $element_static->ref,
604 $account_infos[
'code_formatted_1'],
605 $element_name_formatted_0 .
' - ' . $depreciation_ref,
606 $mt < 0 ?
price(-$mt) :
'',
607 $mt >= 0 ?
price($mt) :
'',
611 $element[
'blocks'][] = $blocks;
616 if (!empty($pre_data_info[
'disposal'])) {
617 $disposal_date = $pre_data_info[
'disposal'][
'date'];
622 $disposal_amount = $pre_data_info[
'disposal'][
'amount'];
623 $disposal_subject_to_vat = $pre_data_info[
'disposal'][
'subject_to_vat'];
628 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
630 $result = $accountancy_codes->fetchAccountancyCodes($element_static->id);
632 $element[
'error'] = $accountancy_codes->errorsToString();
635 $element_static->fetchDepreciationLines();
636 foreach ($element_static->depreciation_lines as $mode_key => $depreciation_lines) {
637 $accountancy_codes_list = $accountancy_codes->accountancy_codes[$mode_key];
639 if (!isset($accountancy_codes_list[
'value_asset_sold'])) {
643 $accountancy_code_value_asset_sold = empty($accountancy_codes_list[
'value_asset_sold']) ?
'NotDefined' : $accountancy_codes_list[
'value_asset_sold'];
644 $accountancy_code_depreciation_asset = empty($accountancy_codes_list[
'depreciation_asset']) ?
'NotDefined' : $accountancy_codes_list[
'depreciation_asset'];
645 $accountancy_code_asset = empty($accountancy_codes_list[
'asset']) ?
'NotDefined' : $accountancy_codes_list[
'asset'];
646 $accountancy_code_receivable_on_assignment = empty($accountancy_codes_list[
'receivable_on_assignment']) ?
'NotDefined' : $accountancy_codes_list[
'receivable_on_assignment'];
647 $accountancy_code_vat_collected = empty($accountancy_codes_list[
'vat_collected']) ?
'NotDefined' : $accountancy_codes_list[
'vat_collected'];
648 $accountancy_code_proceeds_from_sales = empty($accountancy_codes_list[
'proceeds_from_sales']) ?
'NotDefined' : $accountancy_codes_list[
'proceeds_from_sales'];
650 $last_cumulative_amount_ht = 0;
651 $depreciated_ids = array_keys($pre_data_info[
'depreciation']);
652 foreach ($depreciation_lines as $line) {
653 $last_cumulative_amount_ht = $line[
'cumulative_depreciation_ht'];
654 if (!in_array($line[
'id'], $depreciated_ids) && empty($line[
'bookkeeping']) && !empty($line[
'ref'])) {
660 $lines[0][$accountancy_code_value_asset_sold] = -((float) $element_static->acquisition_value_ht - $last_cumulative_amount_ht);
661 $lines[0][$accountancy_code_depreciation_asset] = - (float) $last_cumulative_amount_ht;
662 $lines[0][$accountancy_code_asset] = $element_static->acquisition_value_ht;
664 $disposal_amount_vat = $disposal_subject_to_vat ? (float)
price2num($disposal_amount * $disposal_vat / 100,
'MT') : 0;
665 $lines[1][$accountancy_code_receivable_on_assignment] = -($disposal_amount + $disposal_amount_vat);
666 if ($disposal_subject_to_vat) {
667 $lines[1][$accountancy_code_vat_collected] = $disposal_amount_vat;
669 $lines[1][$accountancy_code_proceeds_from_sales] = $disposal_amount;
671 foreach ($lines as $lines_block) {
673 foreach ($lines_block as $account => $mt) {
676 if ($type ==
'view') {
678 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
679 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
683 'date' => $disposal_date_formatted,
684 'piece' => $element_link,
685 'account_accounting' => $account_to_show,
686 'subledger_account' =>
'',
687 'label_operation' => $label_operation .
' - ' . $disposal_ref,
688 'debit' => $mt < 0 ?
price(-$mt) :
'',
689 'credit' => $mt >= 0 ?
price($mt) :
'',
691 } elseif ($type ==
'bookkeeping') {
692 if ($account_infos[
'found']) {
694 'doc_date' => $disposal_date,
695 'date_lim_reglement' =>
'',
696 'doc_ref' => $element_static->ref,
697 'date_creation' => $now,
698 'doc_type' =>
'asset',
699 'fk_doc' => $element_static->id,
701 'thirdparty_code' =>
'',
702 'subledger_account' =>
'',
703 'subledger_label' =>
'',
704 'numero_compte' => (
string) $account,
705 'label_compte' => $account_infos[
'label'],
706 'label_operation' => $element_name_formatted_0 .
' - ' . $disposal_ref,
707 'montant' => (
float) $mt,
708 'sens' => $mt < 0 ?
'D' :
'C',
709 'debit' => $mt < 0 ? -$mt : 0,
710 'credit' => $mt >= 0 ? $mt : 0,
711 'code_journal' => $journal,
712 'journal_label' => $journal_label_formatted,
715 'fk_user_author' => $user->id,
716 'entity' =>
$conf->entity,
722 $element_static->ref,
723 $account_infos[
'code_formatted_1'],
724 $element_name_formatted_0 .
' - ' . $disposal_ref,
725 $mt < 0 ?
price(-$mt) :
'',
726 $mt >= 0 ?
price($mt) :
'',
730 $element[
'blocks'][] = $blocks;
737 $journal_data[(int) $pre_data_id] = $element;
741 return $journal_data;
756 global
$conf, $langs, $hookmanager;
758 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
759 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
760 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
762 $langs->loadLangs(array(
'bills'));
768 if (empty($in_bookkeeping)) {
769 $in_bookkeeping =
'notyet';
773 $sql =
"SELECT f.rowid, f.ref, f.datef, f.date_closing, f.fk_soc, f.total_ttc";
774 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
775 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice', 0).
')';
776 $sql .=
" AND f.fk_statut > 0";
783 $hookmanager->initHooks(array(
'accountingjournaldao'));
784 $parameters = array(
'invoicealias' =>
'f',
'issupplier' => 0,
'datefield' =>
'datef');
785 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $this);
786 $sql .= $hookmanager->resPrint;
787 $sql .=
" AND f.close_code = 'discount_vat'";
789 $sql .=
" AND f.date_closing >= '".$this->db->idate(
$date_start).
"' AND f.date_closing <= '".$this->db->idate($date_end).
"'";
792 $sql .=
" AND f.date_closing >= '".$this->db->idate(
getDolGlobalInt(
'ACCOUNTING_DATE_START_BINDING')).
"'";
794 if ($in_bookkeeping ==
'already') {
795 $sql .=
" AND EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
796 $sql .=
" WHERE ab.doc_type = 'customer_invoice' AND ab.fk_doc = f.rowid";
797 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
798 } elseif ($in_bookkeeping ==
'notyet') {
799 $sql .=
" AND NOT EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
800 $sql .=
" WHERE ab.doc_type = 'customer_invoice' AND ab.fk_doc = f.rowid";
801 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
803 $sql .=
" ORDER BY f.date_closing";
806 $resql = $this->db->query($sql);
808 $this->errors[] = $this->db->lasterror();
812 $journal = $this->code;
813 $journal_label_formatted = $langs->transnoentities($this->label);
816 $journal_data = array();
817 $invoice_static =
new Facture($this->db);
818 $customer_static =
new Societe($this->db);
824 while ($obj = $this->db->fetch_object($resql)) {
825 if ($invoice_static->fetch((
int) $obj->rowid) <= 0) {
829 $customer_static->fetch($invoice_static->socid);
830 $account_customer_general = !empty($customer_static->accountancy_code_customer_general) ? $customer_static->accountancy_code_customer_general :
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER');
831 $account_customer_subsidiary = !empty($customer_static->code_compta_client) ? $customer_static->code_compta_client :
'';
833 $piece_link = $invoice_static->getNomUrl(1,
'withlabel');
836 $paid = (float)
price2num($invoice_static->getSommePaiement(),
'MT');
837 $usedcn = (float)
price2num($invoice_static->getSumCreditNotesUsed(),
'MT');
838 $useddep = (float)
price2num($invoice_static->getSumDepositsUsed(),
'MT');
839 $ttc_inv = (float)
price2num($invoice_static->total_ttc,
'MT');
840 $escompte_ttc = (float)
price2num(max(0, $ttc_inv - $paid - $usedcn - $useddep),
'MT');
841 if ($escompte_ttc <= 0) {
846 $thirdpartyname = (string) $customer_static->name;
847 $label_discount = $bookkeeping_static->accountingLabelForOperation($thirdpartyname, $invoice_static->ref, $langs->trans(
'DiscountGranted'));
850 $ttcByRate = array();
852 foreach ((array) $invoice_static->lines as $li) {
853 $ttc = (float) $li->total_ttc;
857 $key = number_format((
float) $li->tva_tx, 3,
'.',
'');
858 if (!isset($ttcByRate[$key])) {
859 $ttcByRate[$key] = 0.0;
861 $ttcByRate[$key] += $ttc;
864 if ($totalTTC <= 0) {
865 $ttcByRate = array(
"0.000" => $escompte_ttc);
866 $totalTTC = $escompte_ttc;
870 'ref' =>
dol_trunc($invoice_static->ref, 16,
'right',
'UTF-8', 1),
875 $closingdate = !empty($obj->date_closing) ? $obj->date_closing : $obj->datef;
877 $docdate = $this->db->jdate($closingdate);
882 $n = count($ttcByRate);
883 foreach ($ttcByRate as $rateStr => $ttcRateOnInvoice) {
885 $rate = (float) $rateStr;
887 $ttc_part = (float) $escompte_ttc * ($ttcRateOnInvoice / $totalTTC);
889 $ttc_part = (float)
price2num($escompte_ttc - $sumTTC,
'MT');
891 $ttc_part = (float)
price2num($ttc_part,
'MT');
892 $sumTTC = (float)
price2num($sumTTC + $ttc_part,
'MT');
896 $ht_part = (float)
price2num($ttc_part / (1 + $rate / 100),
'MT');
897 $tva_part = (float)
price2num($ttc_part - $ht_part,
'MT');
899 $ht_part = $ttc_part;
905 $acc_vat_coll = $acc_vat_coll_def;
907 $lines_view = array();
908 $lines_book = array();
912 if ($type ==
'view') {
913 $lines_view[] = array(
914 'date' => $docdate_fmt,
915 'piece' => $piece_link,
917 'subledger_account' =>
'',
918 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
919 'debit' =>
price($ht_part),
922 } elseif ($type ==
'bookkeeping' && $acc_info_discountgranted[
'found']) {
923 $lines_book[] = array(
924 'doc_date' => $docdate,
925 'date_lim_reglement' =>
'',
926 'doc_ref' => $invoice_static->ref,
927 'date_creation' => $now,
928 'doc_type' =>
'customer_invoice',
929 'fk_doc' => $invoice_static->id,
931 'thirdparty_code' => $customer_static->code_client,
932 'subledger_account' =>
'',
933 'subledger_label' =>
'',
934 'numero_compte' => (
string) $acc_disc_granted,
935 'label_compte' => $acc_info_discountgranted[
'label'],
936 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
937 'montant' => $ht_part,
941 'code_journal' => $journal,
942 'journal_label' => $journal_label_formatted,
943 'piece_num' =>
'OD-ESC-'.$invoice_static->ref,
945 'fk_user_author' => $user->id,
946 'entity' =>
$conf->entity,
953 if ($type ==
'view') {
954 $lines_view[] = array(
955 'date' => $docdate_fmt,
956 'piece' => $piece_link,
958 'subledger_account' =>
'',
959 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
960 'debit' =>
price($tva_part),
963 } elseif ($type ==
'bookkeeping' && $acc_info_vatbuy[
'found']) {
964 $lines_book[] = array(
965 'doc_date' => $docdate,
966 'date_lim_reglement' =>
'',
967 'doc_ref' => $invoice_static->ref,
968 'date_creation' => $now,
969 'doc_type' =>
'customer_invoice',
970 'fk_doc' => $invoice_static->id,
972 'thirdparty_code' => $customer_static->code_client,
973 'subledger_account' =>
'',
974 'subledger_label' =>
'',
975 'numero_compte' => (
string) $acc_vat_coll,
976 'label_compte' => $acc_info_vatbuy[
'label'],
977 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
978 'montant' => $tva_part,
980 'debit' => $tva_part,
982 'code_journal' => $journal,
983 'journal_label' => $journal_label_formatted,
984 'piece_num' =>
'OD-ESC-'.$invoice_static->ref,
986 'fk_user_author' => $user->id,
987 'entity' =>
$conf->entity,
994 if ($type ==
'view') {
995 $lines_view[] = array(
996 'date' => $docdate_fmt,
997 'piece' => $piece_link,
1000 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Customer'),
1002 'credit' =>
price($ttc_part),
1004 $element[
'blocks'][] = $lines_view;
1005 } elseif ($type ==
'bookkeeping' && $acc_info_customeraccount[
'found']) {
1006 $lines_book[] = array(
1007 'doc_date' => $docdate,
1008 'date_lim_reglement' =>
'',
1009 'doc_ref' => $invoice_static->ref,
1010 'date_creation' => $now,
1011 'doc_type' =>
'customer_invoice',
1012 'fk_doc' => $invoice_static->id,
1014 'thirdparty_code' => $customer_static->code_client,
1015 'subledger_account' => $account_customer_subsidiary,
1016 'subledger_label' => $customer_static->name,
1017 'numero_compte' => (
string) $account_customer_general,
1018 'label_compte' => $acc_info_customeraccount[
'label'],
1019 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Customer'),
1020 'montant' => $ttc_part,
1023 'credit' => $ttc_part,
1024 'code_journal' => $journal,
1025 'journal_label' => $journal_label_formatted,
1026 'piece_num' =>
'OD-ESC-'.$invoice_static->ref,
1028 'fk_user_author' => $user->id,
1029 'entity' =>
$conf->entity,
1031 $element[
'blocks'][] = $lines_book;
1033 $element[
'blocks'][] = array(
1035 $invoice_static->ref,
1037 $label_discount.
" (".$rateStr.
"%)",
1041 if ($tva_part > 0) {
1042 $element[
'blocks'][] = array(
1043 $docdate, $invoice_static->ref,
length_accountg($acc_vat_coll), $label_discount.
" ". $langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
price($tva_part),
''
1046 $element[
'blocks'][] = array(
1047 $docdate, $invoice_static->ref,
length_accountg($account_customer_general), $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Customer'),
'',
price($ttc_part)
1052 $journal_data[(int) $invoice_static->id] = $element;
1055 return $journal_data;
1070 global
$conf, $langs, $hookmanager;
1072 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
1073 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
1074 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
1076 $langs->loadLangs(array(
'suppliers'));
1082 if (empty($in_bookkeeping)) {
1083 $in_bookkeeping =
'notyet';
1087 $sql =
"SELECT ff.rowid, ff.ref, ff.datef, ff.date_closing, ff.fk_soc, ff.total_ttc";
1088 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as ff";
1089 $sql .=
" WHERE ff.entity IN (".getEntity(
'facture_fourn', 0).
")";
1090 $sql .=
" AND ff.fk_statut > 0";
1097 $hookmanager->initHooks(array(
'accountingjournaldao'));
1098 $parameters = array(
'invoicealias' =>
'ff',
'issupplier' => 1,
'datefield' =>
'datef');
1099 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $this);
1100 $sql .= $hookmanager->resPrint;
1101 $sql .=
" AND ff.close_code = 'discount_vat'";
1103 $sql .=
" AND ff.date_closing >= '".$this->db->idate(
$date_start).
"' AND ff.date_closing <= '".$this->db->idate($date_end).
"'";
1106 $sql .=
" AND ff.date_closing >= '".$this->db->idate(
getDolGlobalInt(
'ACCOUNTING_DATE_START_BINDING')).
"'";
1108 if ($in_bookkeeping ==
'already') {
1109 $sql .=
" AND EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
1110 $sql .=
" WHERE ab.doc_type = 'supplier_invoice' AND ab.fk_doc = ff.rowid";
1111 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
1112 } elseif ($in_bookkeeping ==
'notyet') {
1113 $sql .=
" AND NOT EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
1114 $sql .=
" WHERE ab.doc_type = 'supplier_invoice' AND ab.fk_doc = ff.rowid";
1115 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
1117 $sql .=
" ORDER BY ff.date_closing";
1120 $resql = $this->db->query($sql);
1122 $this->errors[] = $this->db->lasterror();
1126 $journal = $this->code;
1127 $journal_label_formatted = $langs->transnoentities($this->label);
1130 $journal_data = array();
1132 $supplier_static =
new Societe($this->db);
1138 while ($obj = $this->db->fetch_object($resql)) {
1139 if ($invoicesupplier_static->fetch((
int) $obj->rowid) <= 0) {
1143 $supplier_static->fetch($invoicesupplier_static->socid);
1144 $account_supplier_general = !empty($supplier_static->accountancy_code_supplier_general) ? $supplier_static->accountancy_code_supplier_general :
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER');
1145 $account_supplier_subsidiary = !empty($supplier_static->code_compta_fournisseur) ? $supplier_static->code_compta_fournisseur :
'';
1147 $piece_link = $invoicesupplier_static->getNomUrl(1,
'withlabel');
1150 $paid = (float)
price2num($invoicesupplier_static->getSommePaiement(),
'MT');
1151 $usedcn = (float)
price2num($invoicesupplier_static->getSumCreditNotesUsed(),
'MT');
1152 $useddep = (float)
price2num($invoicesupplier_static->getSumDepositsUsed(),
'MT');
1153 $ttc_inv = (float)
price2num($invoicesupplier_static->total_ttc,
'MT');
1154 $escompte_ttc = (float)
price2num(max(0, $ttc_inv - $paid - $usedcn - $useddep),
'MT');
1155 if ($escompte_ttc <= 0) {
1160 $thirdpartyname = (string) $supplier_static->name;
1161 $label_discount = $bookkeeping_static->accountingLabelForOperation($thirdpartyname, $invoicesupplier_static->ref, $langs->trans(
'DiscountReceived'));
1164 $ttcByRate = array();
1166 foreach ((array) $invoicesupplier_static->lines as $li) {
1167 $ttc = (float) $li->total_ttc;
1171 $key = number_format((
float) $li->tva_tx, 3,
'.',
'');
1172 if (!isset($ttcByRate[$key])) {
1173 $ttcByRate[$key] = 0.0;
1175 $ttcByRate[$key] += $ttc;
1178 if ($totalTTC <= 0) {
1179 $ttcByRate = array(
"0.000" => $escompte_ttc);
1180 $totalTTC = $escompte_ttc;
1184 'ref' =>
dol_trunc($invoicesupplier_static->ref, 16,
'right',
'UTF-8', 1),
1186 'blocks' => array(),
1189 $closingdate = !empty($obj->date_closing) ? $obj->date_closing : $obj->datef;
1191 $docdate = $this->db->jdate($closingdate);
1196 $n = count($ttcByRate);
1197 foreach ($ttcByRate as $rateStr => $ttcRateOnInvoice) {
1199 $rate = (float) $rateStr;
1201 $ttc_part = $escompte_ttc * ($ttcRateOnInvoice / $totalTTC);
1203 $ttc_part = (float)
price2num($escompte_ttc - $sumTTC,
'MT');
1205 $ttc_part = (float)
price2num($ttc_part,
'MT');
1206 $sumTTC = (float)
price2num($sumTTC + $ttc_part,
'MT');
1210 $ht_part = (float)
price2num($ttc_part / (1 + $rate / 100),
'MT');
1211 $tva_part = (float)
price2num($ttc_part - $ht_part,
'MT');
1213 $ht_part = $ttc_part;
1219 $acc_vat_ded = $acc_vat_ded_def;
1221 $lines_view = array();
1222 $lines_book = array();
1226 if ($type ==
'view') {
1227 $lines_view[] = array(
1228 'date' => $docdate_fmt,
1229 'piece' => $piece_link,
1232 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Supplier'),
1233 'debit' =>
price($ttc_part),
1236 } elseif ($type ==
'bookkeeping' && $acc_info_supplieraccount[
'found']) {
1237 $lines_book[] = array(
1238 'doc_date' => $docdate,
1239 'date_lim_reglement' =>
'',
1240 'doc_ref' => $invoicesupplier_static->ref,
1241 'date_creation' => $now,
1242 'doc_type' =>
'supplier_invoice',
1243 'fk_doc' => $invoicesupplier_static->id,
1245 'thirdparty_code' => $supplier_static->code_fournisseur,
1246 'subledger_account' => $account_supplier_subsidiary,
1247 'subledger_label' => $supplier_static->name,
1248 'numero_compte' => $account_supplier_general,
1249 'label_compte' => $acc_info_supplieraccount[
'label'],
1250 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Supplier'),
1251 'montant' => $ttc_part,
1253 'debit' => $ttc_part,
1255 'code_journal' => $journal,
1256 'journal_label' => $journal_label_formatted,
1257 'piece_num' =>
'OD-ESC-FRS-'.$invoicesupplier_static->ref,
1259 'fk_user_author' => $user->id,
1260 'entity' =>
$conf->entity,
1266 if ($type ==
'view') {
1267 $lines_view[] = array(
1268 'date' => $docdate_fmt,
1269 'piece' => $piece_link,
1271 'subledger_account' =>
'',
1272 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
1274 'credit' =>
price($ht_part),
1276 } elseif ($type ==
'bookkeeping' && $acc_info_discountreceived[
'found']) {
1277 $lines_book[] = array(
1278 'doc_date' => $docdate,
1279 'date_lim_reglement' =>
'',
1280 'doc_ref' => $invoicesupplier_static->ref,
1281 'date_creation' => $now,
1282 'doc_type' =>
'supplier_invoice',
1283 'fk_doc' => $invoicesupplier_static->id,
1285 'thirdparty_code' => $supplier_static->code_fournisseur,
1286 'subledger_account' =>
'',
1287 'subledger_label' =>
'',
1288 'numero_compte' => $acc_disc_recv,
1289 'label_compte' => $acc_info_discountreceived[
'label'],
1290 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
1291 'montant' => $ht_part,
1294 'credit' => $ht_part,
1295 'code_journal' => $journal,
1296 'journal_label' => $journal_label_formatted,
1297 'piece_num' =>
'OD-ESC-FRS-'.$invoicesupplier_static->ref,
1299 'fk_user_author' => $user->id,
1300 'entity' =>
$conf->entity,
1305 if ($tva_part > 0) {
1307 if ($type ==
'view') {
1308 $lines_view[] = array(
1309 'date' => $docdate_fmt,
1310 'piece' => $piece_link,
1312 'subledger_account' =>
'',
1313 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
1315 'credit' =>
price($tva_part),
1317 $element[
'blocks'][] = $lines_view;
1318 } elseif ($type ==
'bookkeeping' && $acc_info_vatbuy[
'found']) {
1319 $lines_book[] = array(
1320 'doc_date' => $docdate,
1321 'date_lim_reglement' =>
'',
1322 'doc_ref' => $invoicesupplier_static->ref,
1323 'date_creation' => $now,
1324 'doc_type' =>
'supplier_invoice',
1325 'fk_doc' => $invoicesupplier_static->id,
1327 'thirdparty_code' => $supplier_static->code_fournisseur,
1328 'subledger_account' =>
'',
1329 'subledger_label' =>
'',
1330 'numero_compte' => $acc_vat_ded,
1331 'label_compte' => $acc_info_vatbuy[
'label'],
1332 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
1333 'montant' => $tva_part,
1336 'credit' => $tva_part,
1337 'code_journal' => $journal,
1338 'journal_label' => $journal_label_formatted,
1339 'piece_num' =>
'OD-ESC-FRS-'.$invoicesupplier_static->ref,
1341 'fk_user_author' => $user->id,
1342 'entity' =>
$conf->entity,
1344 $element[
'blocks'][] = $lines_book;
1348 if ($type ==
'view') {
1349 $element[
'blocks'][] = $lines_view;
1350 } elseif ($type ==
'bookkeeping') {
1351 $element[
'blocks'][] = $lines_book;
1353 $element[
'blocks'][] = array($docdate, $invoicesupplier_static->ref,
length_accountg($account_supplier_general), $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Supplier'),
price($ttc_part),
'');
1354 $element[
'blocks'][] = array($docdate, $invoicesupplier_static->ref,
length_accountg($acc_disc_recv), $label_discount.
' ('.$rateStr.
'%)',
'',
price($ht_part));
1359 if ($type ==
'csv') {
1360 $element[
'blocks'][] = array(
1361 $docdate, $invoicesupplier_static->ref,
length_accountg($acc_vat_ded), $label_discount.
" ". $langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
'', $tva_part > 0 ?
price($tva_part) :
''
1366 $journal_data[(int) $invoicesupplier_static->id] = $element;
1369 return $journal_data;
1417 global
$conf, $langs, $hookmanager;
1418 require_once DOL_DOCUMENT_ROOT .
'/accountancy/class/bookkeeping.class.php';
1422 $hookmanager->initHooks(array(
'accountingjournaldao'));
1423 $parameters = array(
'journal_data' => &$journal_data);
1424 $reshook = $hookmanager->executeHooks(
'writeBookkeeping', $parameters, $this);
1426 $this->error = $hookmanager->error;
1427 $this->errors = $hookmanager->errors;
1429 } elseif (empty($reshook)) {
1431 if (!is_array($journal_data)) {
1432 $journal_data = array();
1435 foreach ($journal_data as $element_id => $element) {
1436 $error_for_line = 0;
1442 if ($element[
'error'] ==
'somelinesarenotbound') {
1445 $this->errors[] = $langs->trans(
'ErrorInvoiceContainsLinesNotYetBounded', $element[
'ref']);
1448 if (!$error_for_line) {
1449 foreach ($element[
'blocks'] as $lines) {
1450 foreach ($lines as $line) {
1452 $bookkeeping->doc_date = (int) $line[
'doc_date'];
1453 $bookkeeping->date_lim_reglement = (int) $line[
'date_lim_reglement'];
1454 $bookkeeping->doc_ref = $line[
'doc_ref'];
1455 $bookkeeping->date_creation = $line[
'date_creation'];
1456 $bookkeeping->doc_type = $line[
'doc_type'];
1457 $bookkeeping->fk_doc = $line[
'fk_doc'];
1458 $bookkeeping->fk_docdet = $line[
'fk_docdet'];
1459 $bookkeeping->thirdparty_code = $line[
'thirdparty_code'];
1460 $bookkeeping->subledger_account = $line[
'subledger_account'];
1461 $bookkeeping->subledger_label = $line[
'subledger_label'];
1462 $bookkeeping->numero_compte = $line[
'numero_compte'];
1463 $bookkeeping->label_compte = $line[
'label_compte'];
1464 $bookkeeping->label_operation = $line[
'label_operation'];
1465 $bookkeeping->montant = $line[
'montant'];
1466 $bookkeeping->sens = $line[
'sens'];
1467 $bookkeeping->debit = (float) $line[
'debit'];
1468 $bookkeeping->credit = (float) $line[
'credit'];
1469 $bookkeeping->code_journal = $line[
'code_journal'];
1470 $bookkeeping->journal_label = $line[
'journal_label'];
1471 $bookkeeping->piece_num = (int) $line[
'piece_num'];
1472 $bookkeeping->import_key = $line[
'import_key'];
1473 $bookkeeping->fk_user_author = $user->id;
1474 $bookkeeping->entity =
$conf->entity;
1476 $total_debit += $bookkeeping->debit;
1477 $total_credit += $bookkeeping->credit;
1479 $result = $bookkeeping->create($user);
1481 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
1484 $journal_data[$element_id][
'error'] =
'alreadyjournalized';
1488 $journal_data[$element_id][
'error'] =
'other';
1489 $this->errors[] = $bookkeeping->errorsToString();
1507 if ($error_for_line) {
1514 if (!$error_for_line && (
price2num($total_debit,
'MT') !=
price2num($total_credit,
'MT'))) {
1517 $journal_data[$element_id][
'error'] =
'amountsnotbalanced';
1518 $this->errors[] =
'Try to insert a non balanced transaction in book for ' . json_encode($element[
'blocks']) .
'. Canceled. Surely a bug.';
1521 if (!$error_for_line) {
1522 $this->db->commit();
1524 $this->db->rollback();
1526 if ($error >= $max_nb_errors) {
1527 $langs->load(
'errors');
1528 $this->errors[] = $langs->trans(
"ErrorTooManyErrorsProcessStopped");
1535 return $error ? -$error : 1;
1559 public function exportCsv(&$journal_data = array(), $search_date_end = 0, $sep =
'')
1561 global
$conf, $langs, $hookmanager;
1569 $hookmanager->initHooks(array(
'accountingjournaldao'));
1570 $parameters = array(
'journal_data' => &$journal_data,
'search_date_end' => &$search_date_end,
'sep' => &$sep,
'out' => &$out);
1571 $reshook = $hookmanager->executeHooks(
'exportCsv', $parameters, $this);
1573 $this->error = $hookmanager->error;
1574 $this->errors = $hookmanager->errors;
1576 } elseif (empty($reshook)) {
1578 $journal_data = is_array($journal_data) ? $journal_data : array();
1582 if ($this->nature == 4) {
1584 $langs->transnoentitiesnoconv(
"BankId"),
1585 $langs->transnoentitiesnoconv(
"Date"),
1586 $langs->transnoentitiesnoconv(
"PaymentMode"),
1587 $langs->transnoentitiesnoconv(
"AccountAccounting"),
1588 $langs->transnoentitiesnoconv(
"LedgerAccount"),
1589 $langs->transnoentitiesnoconv(
"SubledgerAccount"),
1590 $langs->transnoentitiesnoconv(
"Label"),
1591 $langs->transnoentitiesnoconv(
"AccountingDebit"),
1592 $langs->transnoentitiesnoconv(
"AccountingCredit"),
1593 $langs->transnoentitiesnoconv(
"Journal"),
1594 $langs->transnoentitiesnoconv(
"Note"),
1596 } elseif ($this->nature == 5) {
1598 $langs->transnoentitiesnoconv(
"Date"),
1599 $langs->transnoentitiesnoconv(
"Piece"),
1600 $langs->transnoentitiesnoconv(
"AccountAccounting"),
1601 $langs->transnoentitiesnoconv(
"LabelOperation"),
1602 $langs->transnoentitiesnoconv(
"AccountingDebit"),
1603 $langs->transnoentitiesnoconv(
"AccountingCredit"),
1605 } elseif ($this->nature == 1) {
1607 $langs->transnoentitiesnoconv(
"Date"),
1608 $langs->transnoentitiesnoconv(
"Piece"),
1609 $langs->transnoentitiesnoconv(
"AccountAccounting"),
1610 $langs->transnoentitiesnoconv(
"LabelOperation"),
1611 $langs->transnoentitiesnoconv(
"AccountingDebit"),
1612 $langs->transnoentitiesnoconv(
"AccountingCredit"),
1616 if (!empty($header)) {
1617 $out .=
'"' . implode(
'"' . $sep .
'"', $header) .
'"' .
"\n";
1619 foreach ($journal_data as $element_id => $element) {
1620 foreach ($element[
'blocks'] as $lines) {
1621 foreach ($lines as $line) {
1622 $out .=
'"' . implode(
'"' . $sep .
'"', $line) .
'"' .
"\n";