212 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle =
'', $notooltip = 0)
214 global $langs,
$conf, $hookmanager;
216 if (!empty(
$conf->dol_no_mouse_hover)) {
222 $url =
dolBuildUrl(DOL_URL_ROOT.
'/accountancy/admin/journals_list.php', [
'id' => 35]);
224 $label =
'<u>'.$langs->trans(
"ShowAccountingJournal").
'</u>';
225 if (!empty($this->code)) {
226 $label .=
'<br><b>'.$langs->trans(
'Code').
':</b> '.$this->code;
228 if (!empty($this->label)) {
229 $label .=
'<br><b>'.$langs->trans(
'Label').
':</b> '.$langs->transnoentities($this->label);
232 $label .=
' - '.$moretitle;
236 if (empty($notooltip)) {
238 $label = $langs->trans(
"ShowAccountingJournal");
239 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
241 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
242 $linkclose .=
' class="classfortooltip"';
245 $linkstart =
'<a href="'.$url.
'"';
246 $linkstart .= $linkclose.
'>';
255 $label_link = $this->code;
256 if ($withlabel == 1 && !empty($this->label)) {
257 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$langs->transnoentities($this->label).($nourl ?
'</span>' :
'');
259 if ($withlabel == 2 && !empty($this->nature)) {
260 $key = $langs->trans(
"AccountingJournalType".$this->nature);
261 $transferlabel = ($key !=
"AccountingJournalType".strtoupper($langs->trans((
string) $this->nature)) ? $key : $this->label);
262 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$transferlabel.($nourl ?
'</span>' :
'');
265 $result .= $linkstart;
267 $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);
269 if ($withpicto != 2) {
270 $result .= $label_link;
275 $hookmanager->initHooks(array(
'accountingjournaldao'));
276 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
277 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
279 $result = $hookmanager->resPrint;
281 $result .= $hookmanager->resPrint;
427 global
$conf, $langs;
433 require_once DOL_DOCUMENT_ROOT .
'/core/lib/accounting.lib.php';
434 require_once DOL_DOCUMENT_ROOT .
'/asset/class/asset.class.php';
435 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
436 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetdepreciationoptions.class.php';
438 $langs->loadLangs(array(
"assets"));
444 if (empty($in_bookkeeping)) {
445 $in_bookkeeping =
'notyet';
449 $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";
450 $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";
451 $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";
452 $sql .=
" FROM " . MAIN_DB_PREFIX .
"asset_depreciation as ad";
453 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"asset as a ON a.rowid = ad.fk_asset";
454 $sql .=
" WHERE a.entity IN (" .
getEntity(
'asset', 0) .
')';
455 $sql .=
" AND a.status > 0";
456 if ($in_bookkeeping ==
'already') {
457 $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')";
458 } elseif ($in_bookkeeping ==
'notyet') {
459 $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')";
461 $sql .=
" AND ad.ref != ''";
463 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate(
$date_start) .
"' AND ad.depreciation_date <= '" . $this->db->idate($date_end) .
"'";
467 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate(
getDolGlobalInt(
'ACCOUNTING_DATE_START_BINDING')) .
"'";
469 $sql .=
" ORDER BY ad.depreciation_date";
472 $resql = $this->db->query($sql);
474 $this->errors[] = $this->db->lasterror();
479 'elements' => array(),
481 while ($obj = $this->db->fetch_object($resql)) {
482 if (!isset($pre_data[
'elements'][$obj->rowid])) {
483 $pre_data[
'elements'][$obj->rowid] = array(
484 'ref' => $obj->asset_ref,
485 'label' => $obj->asset_label,
486 'acquisition_value_ht' => $obj->asset_acquisition_value_ht,
487 'depreciation' => array(),
491 if (isset($obj->asset_disposal_date)) {
492 $pre_data[
'elements'][$obj->rowid][
'disposal'] = array(
493 'date' => $this->db->jdate($obj->asset_disposal_date),
494 'amount' => $obj->asset_disposal_amount_ht,
495 'subject_to_vat' => !empty($obj->asset_disposal_subject_to_vat),
500 $compta_debit = empty($obj->accountancy_code_debit) ?
'NotDefined' : $obj->accountancy_code_debit;
501 $compta_credit = empty($obj->accountancy_code_credit) ?
'NotDefined' : $obj->accountancy_code_credit;
503 $pre_data[
'elements'][$obj->rowid][
'depreciation'][$obj->depreciation_id] = array(
504 'date' => $this->db->jdate($obj->depreciation_date),
505 'ref' => $obj->depreciation_ref,
507 $compta_debit => -$obj->depreciation_ht,
508 $compta_credit => $obj->depreciation_ht,
513 $disposal_ref = $langs->transnoentitiesnoconv(
'AssetDisposal');
514 $journal = $this->code;
515 $journal_label = $this->label;
516 $journal_label_formatted = $langs->transnoentities($journal_label);
519 $element_static =
new Asset($this->db);
521 $journal_data = array();
522 foreach ($pre_data[
'elements'] as $pre_data_id => $pre_data_info) {
523 $element_static->id = (int) $pre_data_id;
524 $element_static->ref = (
string) $pre_data_info[
"ref"];
525 $element_static->label = (
string) $pre_data_info[
"label"];
526 $element_static->acquisition_value_ht = $pre_data_info[
"acquisition_value_ht"];
527 $element_link = $element_static->getNomUrl(1,
'with_label');
529 $element_name_formatted_0 =
dol_trunc($element_static->label, 16);
530 $label_operation = $element_static->getNomUrl(0,
'label', 16);
533 'ref' =>
dol_trunc($element_static->ref, 16,
'right',
'UTF-8', 1),
534 'error' => array_key_exists(
'error', $pre_data_info) ? $pre_data_info[
'error'] :
'',
540 foreach ($pre_data_info[
'depreciation'] as $depreciation_id => $line) {
541 $depreciation_ref = $line[
"ref"];
542 $depreciation_date = $line[
"date"];
543 $depreciation_date_formatted =
dol_print_date($depreciation_date,
'day');
547 foreach ($line[
'lines'] as $account => $mt) {
550 if ($type ==
'view') {
552 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
553 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
557 'date' => $depreciation_date_formatted,
558 'piece' => $element_link,
559 'account_accounting' => $account_to_show,
560 'subledger_account' =>
'',
561 'label_operation' => $label_operation .
' - ' . $depreciation_ref,
562 'debit' => $mt < 0 ?
price(-$mt) :
'',
563 'credit' => $mt >= 0 ?
price($mt) :
'',
565 } elseif ($type ==
'bookkeeping') {
566 if ($account_infos[
'found']) {
568 'doc_date' => $depreciation_date,
569 'date_lim_reglement' =>
'',
570 'doc_ref' => $element_static->ref,
571 'date_creation' => $now,
572 'doc_type' =>
'asset',
573 'fk_doc' => $element_static->id,
574 'fk_docdet' => (
int) $depreciation_id,
575 'thirdparty_code' =>
'',
576 'subledger_account' =>
'',
577 'subledger_label' =>
'',
578 'numero_compte' => (
string) $account,
579 'label_compte' => $account_infos[
'label'],
580 'label_operation' => $element_name_formatted_0 .
' - ' . $depreciation_ref,
581 'montant' => (
float) $mt,
582 'sens' => $mt < 0 ?
'D' :
'C',
583 'debit' => $mt < 0 ? -$mt : 0,
584 'credit' => $mt >= 0 ? $mt : 0,
585 'code_journal' => $journal,
586 'journal_label' => $journal_label_formatted,
589 'fk_user_author' => $user->id,
590 'entity' =>
$conf->entity,
596 $element_static->ref,
597 $account_infos[
'code_formatted_1'],
598 $element_name_formatted_0 .
' - ' . $depreciation_ref,
599 $mt < 0 ?
price(-$mt) :
'',
600 $mt >= 0 ?
price($mt) :
'',
604 $element[
'blocks'][] = $blocks;
609 if (!empty($pre_data_info[
'disposal'])) {
610 $disposal_date = $pre_data_info[
'disposal'][
'date'];
615 $disposal_amount = $pre_data_info[
'disposal'][
'amount'];
616 $disposal_subject_to_vat = $pre_data_info[
'disposal'][
'subject_to_vat'];
622 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
624 $result = $accountancy_codes->fetchAccountancyCodes($element_static->id);
626 $element[
'error'] = $accountancy_codes->errorsToString();
629 $element_static->fetchDepreciationLines();
630 foreach ($element_static->depreciation_lines as $mode_key => $depreciation_lines) {
631 $accountancy_codes_list = $accountancy_codes->accountancy_codes[$mode_key];
633 if (!isset($accountancy_codes_list[
'value_asset_sold'])) {
637 $accountancy_code_value_asset_sold = empty($accountancy_codes_list[
'value_asset_sold']) ?
'NotDefined' : $accountancy_codes_list[
'value_asset_sold'];
638 $accountancy_code_depreciation_asset = empty($accountancy_codes_list[
'depreciation_asset']) ?
'NotDefined' : $accountancy_codes_list[
'depreciation_asset'];
639 $accountancy_code_asset = empty($accountancy_codes_list[
'asset']) ?
'NotDefined' : $accountancy_codes_list[
'asset'];
640 $accountancy_code_receivable_on_assignment = empty($accountancy_codes_list[
'receivable_on_assignment']) ?
'NotDefined' : $accountancy_codes_list[
'receivable_on_assignment'];
641 $accountancy_code_vat_collected = empty($accountancy_codes_list[
'vat_collected']) ?
'NotDefined' : $accountancy_codes_list[
'vat_collected'];
642 $accountancy_code_proceeds_from_sales = empty($accountancy_codes_list[
'proceeds_from_sales']) ?
'NotDefined' : $accountancy_codes_list[
'proceeds_from_sales'];
644 $last_cumulative_amount_ht = 0;
645 $depreciated_ids = array_keys($pre_data_info[
'depreciation']);
646 foreach ($depreciation_lines as $line) {
647 $last_cumulative_amount_ht = $line[
'cumulative_depreciation_ht'];
648 if (!in_array($line[
'id'], $depreciated_ids) && empty($line[
'bookkeeping']) && !empty($line[
'ref'])) {
654 $lines[0][$accountancy_code_value_asset_sold] = -((float) $element_static->acquisition_value_ht - $last_cumulative_amount_ht);
655 $lines[0][$accountancy_code_depreciation_asset] = - (float) $last_cumulative_amount_ht;
656 $lines[0][$accountancy_code_asset] = $element_static->acquisition_value_ht;
658 $disposal_amount_vat = $disposal_subject_to_vat ? (float)
price2num($disposal_amount * $disposal_vat / 100,
'MT') : 0;
659 $lines[1][$accountancy_code_receivable_on_assignment] = -($disposal_amount + $disposal_amount_vat);
660 if ($disposal_subject_to_vat) {
661 $lines[1][$accountancy_code_vat_collected] = $disposal_amount_vat;
663 $lines[1][$accountancy_code_proceeds_from_sales] = $disposal_amount;
665 foreach ($lines as $lines_block) {
667 foreach ($lines_block as $account => $mt) {
670 if ($type ==
'view') {
672 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
673 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
677 'date' => $disposal_date_formatted,
678 'piece' => $element_link,
679 'account_accounting' => $account_to_show,
680 'subledger_account' =>
'',
681 'label_operation' => $label_operation .
' - ' . $disposal_ref,
682 'debit' => $mt < 0 ?
price(-$mt) :
'',
683 'credit' => $mt >= 0 ?
price($mt) :
'',
685 } elseif ($type ==
'bookkeeping') {
686 if ($account_infos[
'found']) {
688 'doc_date' => $disposal_date,
689 'date_lim_reglement' =>
'',
690 'doc_ref' => $element_static->ref,
691 'date_creation' => $now,
692 'doc_type' =>
'asset',
693 'fk_doc' => $element_static->id,
695 'thirdparty_code' =>
'',
696 'subledger_account' =>
'',
697 'subledger_label' =>
'',
698 'numero_compte' => (
string) $account,
699 'label_compte' => $account_infos[
'label'],
700 'label_operation' => $element_name_formatted_0 .
' - ' . $disposal_ref,
701 'montant' => (
float) $mt,
702 'sens' => $mt < 0 ?
'D' :
'C',
703 'debit' => $mt < 0 ? -$mt : 0,
704 'credit' => $mt >= 0 ? $mt : 0,
705 'code_journal' => $journal,
706 'journal_label' => $journal_label_formatted,
709 'fk_user_author' => $user->id,
710 'entity' =>
$conf->entity,
716 $element_static->ref,
717 $account_infos[
'code_formatted_1'],
718 $element_name_formatted_0 .
' - ' . $disposal_ref,
719 $mt < 0 ?
price(-$mt) :
'',
720 $mt >= 0 ?
price($mt) :
'',
724 $element[
'blocks'][] = $blocks;
731 $journal_data[(int) $pre_data_id] = $element;
735 return $journal_data;
750 global
$conf, $langs;
752 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
753 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
754 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
756 $langs->loadLangs(array(
'bills'));
762 if (empty($in_bookkeeping)) {
763 $in_bookkeeping =
'notyet';
767 $sql =
"SELECT f.rowid, f.ref, f.datef, f.date_closing, f.fk_soc, f.total_ttc";
768 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
769 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice', 0).
')';
770 $sql .=
" AND f.fk_statut > 0";
776 $sql .=
" AND f.close_code = 'discount_vat'";
778 $sql .=
" AND f.date_closing >= '".$this->db->idate(
$date_start).
"' AND f.date_closing <= '".$this->db->idate($date_end).
"'";
781 $sql .=
" AND f.date_closing >= '".$this->db->idate(
getDolGlobalInt(
'ACCOUNTING_DATE_START_BINDING')).
"'";
783 if ($in_bookkeeping ==
'already') {
784 $sql .=
" AND EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
785 $sql .=
" WHERE ab.doc_type = 'customer_invoice' AND ab.fk_doc = f.rowid";
786 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
787 } elseif ($in_bookkeeping ==
'notyet') {
788 $sql .=
" AND NOT EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
789 $sql .=
" WHERE ab.doc_type = 'customer_invoice' AND ab.fk_doc = f.rowid";
790 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
792 $sql .=
" ORDER BY f.date_closing";
795 $resql = $this->db->query($sql);
797 $this->errors[] = $this->db->lasterror();
801 $journal = $this->code;
802 $journal_label_formatted = $langs->transnoentities($this->label);
805 $journal_data = array();
806 $invoice_static =
new Facture($this->db);
807 $customer_static =
new Societe($this->db);
813 while ($obj = $this->db->fetch_object($resql)) {
814 if ($invoice_static->fetch((
int) $obj->rowid) <= 0) {
818 $customer_static->fetch($invoice_static->socid);
819 $account_customer_general = !empty($customer_static->accountancy_code_customer_general) ? $customer_static->accountancy_code_customer_general :
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER');
820 $account_customer_subsidiary = !empty($customer_static->code_compta_client) ? $customer_static->code_compta_client :
'';
822 $piece_link = $invoice_static->getNomUrl(1,
'withlabel');
825 $paid = (float)
price2num($invoice_static->getSommePaiement(),
'MT');
826 $usedcn = (float)
price2num($invoice_static->getSumCreditNotesUsed(),
'MT');
827 $useddep = (float)
price2num($invoice_static->getSumDepositsUsed(),
'MT');
828 $ttc_inv = (float)
price2num($invoice_static->total_ttc,
'MT');
829 $escompte_ttc = (float)
price2num(max(0, $ttc_inv - $paid - $usedcn - $useddep),
'MT');
830 if ($escompte_ttc <= 0) {
835 $thirdpartyname = (
string) $customer_static->name;
836 $label_discount = $bookkeeping_static->accountingLabelForOperation($thirdpartyname, $invoice_static->ref, $langs->trans(
'DiscountGranted'));
839 $ttcByRate = array();
841 foreach ((array) $invoice_static->lines as $li) {
842 $ttc = (float) $li->total_ttc;
846 $key = number_format((
float) $li->tva_tx, 3,
'.',
'');
847 if (!isset($ttcByRate[$key])) {
848 $ttcByRate[$key] = 0.0;
850 $ttcByRate[$key] += $ttc;
853 if ($totalTTC <= 0) {
854 $ttcByRate = array(
"0.000" => $escompte_ttc);
855 $totalTTC = $escompte_ttc;
859 'ref' =>
dol_trunc($invoice_static->ref, 16,
'right',
'UTF-8', 1),
864 $closingdate = !empty($obj->date_closing) ? $obj->date_closing : $obj->datef;
866 $docdate = $this->db->jdate($closingdate);
871 $n = count($ttcByRate);
872 foreach ($ttcByRate as $rateStr => $ttcRateOnInvoice) {
874 $rate = (float) $rateStr;
876 $ttc_part = (float) $escompte_ttc * ($ttcRateOnInvoice / $totalTTC);
878 $ttc_part = (float)
price2num($escompte_ttc - $sumTTC,
'MT');
880 $ttc_part = (float)
price2num($ttc_part,
'MT');
881 $sumTTC = (float)
price2num($sumTTC + $ttc_part,
'MT');
885 $ht_part = (float)
price2num($ttc_part / (1 + $rate / 100),
'MT');
886 $tva_part = (float)
price2num($ttc_part - $ht_part,
'MT');
888 $ht_part = $ttc_part;
894 $acc_vat_coll = $acc_vat_coll_def;
896 $lines_view = array();
897 $lines_book = array();
901 if ($type ==
'view') {
902 $lines_view[] = array(
903 'date' => $docdate_fmt,
904 'piece' => $piece_link,
906 'subledger_account' =>
'',
907 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
908 'debit' =>
price($ht_part),
911 } elseif ($type ==
'bookkeeping' && $acc_info_discountgranted[
'found']) {
912 $lines_book[] = array(
913 'doc_date' => $docdate,
914 'date_lim_reglement' =>
'',
915 'doc_ref' => $invoice_static->ref,
916 'date_creation' => $now,
917 'doc_type' =>
'customer_invoice',
918 'fk_doc' => $invoice_static->id,
920 'thirdparty_code' => $customer_static->code_client,
921 'subledger_account' =>
'',
922 'subledger_label' =>
'',
923 'numero_compte' => (
string) $acc_disc_granted,
924 'label_compte' => $acc_info_discountgranted[
'label'],
925 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
926 'montant' => $ht_part,
930 'code_journal' => $journal,
931 'journal_label' => $journal_label_formatted,
932 'piece_num' =>
'OD-ESC-'.$invoice_static->ref,
934 'fk_user_author' => $user->id,
935 'entity' =>
$conf->entity,
942 if ($type ==
'view') {
943 $lines_view[] = array(
944 'date' => $docdate_fmt,
945 'piece' => $piece_link,
947 'subledger_account' =>
'',
948 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
949 'debit' =>
price($tva_part),
952 } elseif ($type ==
'bookkeeping' && $acc_info_vatbuy[
'found']) {
953 $lines_book[] = array(
954 'doc_date' => $docdate,
955 'date_lim_reglement' =>
'',
956 'doc_ref' => $invoice_static->ref,
957 'date_creation' => $now,
958 'doc_type' =>
'customer_invoice',
959 'fk_doc' => $invoice_static->id,
961 'thirdparty_code' => $customer_static->code_client,
962 'subledger_account' =>
'',
963 'subledger_label' =>
'',
964 'numero_compte' => (
string) $acc_vat_coll,
965 'label_compte' => $acc_info_vatbuy[
'label'],
966 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
967 'montant' => $tva_part,
969 'debit' => $tva_part,
971 'code_journal' => $journal,
972 'journal_label' => $journal_label_formatted,
973 'piece_num' =>
'OD-ESC-'.$invoice_static->ref,
975 'fk_user_author' => $user->id,
976 'entity' =>
$conf->entity,
983 if ($type ==
'view') {
984 $lines_view[] = array(
985 'date' => $docdate_fmt,
986 'piece' => $piece_link,
989 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Customer'),
991 'credit' =>
price($ttc_part),
993 $element[
'blocks'][] = $lines_view;
994 } elseif ($type ==
'bookkeeping' && $acc_info_customeraccount[
'found']) {
995 $lines_book[] = array(
996 'doc_date' => $docdate,
997 'date_lim_reglement' =>
'',
998 'doc_ref' => $invoice_static->ref,
999 'date_creation' => $now,
1000 'doc_type' =>
'customer_invoice',
1001 'fk_doc' => $invoice_static->id,
1003 'thirdparty_code' => $customer_static->code_client,
1004 'subledger_account' => $account_customer_subsidiary,
1005 'subledger_label' => $customer_static->name,
1006 'numero_compte' => (
string) $account_customer_general,
1007 'label_compte' => $acc_info_customeraccount[
'label'],
1008 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Customer'),
1009 'montant' => $ttc_part,
1012 'credit' => $ttc_part,
1013 'code_journal' => $journal,
1014 'journal_label' => $journal_label_formatted,
1015 'piece_num' =>
'OD-ESC-'.$invoice_static->ref,
1017 'fk_user_author' => $user->id,
1018 'entity' =>
$conf->entity,
1020 $element[
'blocks'][] = $lines_book;
1022 $element[
'blocks'][] = array(
1024 $invoice_static->ref,
1026 $label_discount.
" (".$rateStr.
"%)",
1030 if ($tva_part > 0) {
1031 $element[
'blocks'][] = array(
1032 $docdate, $invoice_static->ref,
length_accountg($acc_vat_coll), $label_discount.
" ". $langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
price($tva_part),
''
1035 $element[
'blocks'][] = array(
1036 $docdate, $invoice_static->ref,
length_accountg($account_customer_general), $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Customer'),
'',
price($ttc_part)
1041 $journal_data[(int) $invoice_static->id] = $element;
1044 return $journal_data;
1059 global
$conf, $langs;
1061 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
1062 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
1063 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
1065 $langs->loadLangs(array(
'suppliers'));
1071 if (empty($in_bookkeeping)) {
1072 $in_bookkeeping =
'notyet';
1076 $sql =
"SELECT ff.rowid, ff.ref, ff.datef, ff.date_closing, ff.fk_soc, ff.total_ttc";
1077 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as ff";
1078 $sql .=
" WHERE ff.entity IN (".getEntity(
'facture_fourn', 0).
")";
1079 $sql .=
" AND ff.fk_statut > 0";
1085 $sql .=
" AND ff.close_code = 'discount_vat'";
1087 $sql .=
" AND ff.date_closing >= '".$this->db->idate(
$date_start).
"' AND ff.date_closing <= '".$this->db->idate($date_end).
"'";
1090 $sql .=
" AND ff.date_closing >= '".$this->db->idate(
getDolGlobalInt(
'ACCOUNTING_DATE_START_BINDING')).
"'";
1092 if ($in_bookkeeping ==
'already') {
1093 $sql .=
" AND EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
1094 $sql .=
" WHERE ab.doc_type = 'supplier_invoice' AND ab.fk_doc = ff.rowid";
1095 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
1096 } elseif ($in_bookkeeping ==
'notyet') {
1097 $sql .=
" AND NOT EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
1098 $sql .=
" WHERE ab.doc_type = 'supplier_invoice' AND ab.fk_doc = ff.rowid";
1099 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
1101 $sql .=
" ORDER BY ff.date_closing";
1104 $resql = $this->db->query($sql);
1106 $this->errors[] = $this->db->lasterror();
1110 $journal = $this->code;
1111 $journal_label_formatted = $langs->transnoentities($this->label);
1114 $journal_data = array();
1116 $supplier_static =
new Societe($this->db);
1122 while ($obj = $this->db->fetch_object($resql)) {
1123 if ($invoicesupplier_static->fetch((
int) $obj->rowid) <= 0) {
1127 $supplier_static->fetch($invoicesupplier_static->socid);
1128 $account_supplier_general = !empty($supplier_static->accountancy_code_supplier_general) ? $supplier_static->accountancy_code_supplier_general :
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER');
1129 $account_supplier_subsidiary = !empty($supplier_static->code_compta_fournisseur) ? $supplier_static->code_compta_fournisseur :
'';
1131 $piece_link = $invoicesupplier_static->getNomUrl(1,
'withlabel');
1134 $paid = (float)
price2num($invoicesupplier_static->getSommePaiement(),
'MT');
1135 $usedcn = (float)
price2num($invoicesupplier_static->getSumCreditNotesUsed(),
'MT');
1136 $useddep = (float)
price2num($invoicesupplier_static->getSumDepositsUsed(),
'MT');
1137 $ttc_inv = (float)
price2num($invoicesupplier_static->total_ttc,
'MT');
1138 $escompte_ttc = (float)
price2num(max(0, $ttc_inv - $paid - $usedcn - $useddep),
'MT');
1139 if ($escompte_ttc <= 0) {
1144 $thirdpartyname = (
string) $supplier_static->name;
1145 $label_discount = $bookkeeping_static->accountingLabelForOperation($thirdpartyname, $invoicesupplier_static->ref, $langs->trans(
'DiscountReceived'));
1148 $ttcByRate = array();
1150 foreach ((array) $invoicesupplier_static->lines as $li) {
1151 $ttc = (float) $li->total_ttc;
1155 $key = number_format((
float) $li->tva_tx, 3,
'.',
'');
1156 if (!isset($ttcByRate[$key])) {
1157 $ttcByRate[$key] = 0.0;
1159 $ttcByRate[$key] += $ttc;
1162 if ($totalTTC <= 0) {
1163 $ttcByRate = array(
"0.000" => $escompte_ttc);
1164 $totalTTC = $escompte_ttc;
1168 'ref' =>
dol_trunc($invoicesupplier_static->ref, 16,
'right',
'UTF-8', 1),
1170 'blocks' => array(),
1173 $closingdate = !empty($obj->date_closing) ? $obj->date_closing : $obj->datef;
1175 $docdate = $this->db->jdate($closingdate);
1180 $n = count($ttcByRate);
1181 foreach ($ttcByRate as $rateStr => $ttcRateOnInvoice) {
1183 $rate = (float) $rateStr;
1185 $ttc_part = $escompte_ttc * ($ttcRateOnInvoice / $totalTTC);
1187 $ttc_part = (float)
price2num($escompte_ttc - $sumTTC,
'MT');
1189 $ttc_part = (float)
price2num($ttc_part,
'MT');
1190 $sumTTC = (float)
price2num($sumTTC + $ttc_part,
'MT');
1194 $ht_part = (float)
price2num($ttc_part / (1 + $rate / 100),
'MT');
1195 $tva_part = (float)
price2num($ttc_part - $ht_part,
'MT');
1197 $ht_part = $ttc_part;
1203 $acc_vat_ded = $acc_vat_ded_def;
1205 $lines_view = array();
1206 $lines_book = array();
1210 if ($type ==
'view') {
1211 $lines_view[] = array(
1212 'date' => $docdate_fmt,
1213 'piece' => $piece_link,
1216 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Supplier'),
1217 'debit' =>
price($ttc_part),
1220 } elseif ($type ==
'bookkeeping' && $acc_info_supplieraccount[
'found']) {
1221 $lines_book[] = array(
1222 'doc_date' => $docdate,
1223 'date_lim_reglement' =>
'',
1224 'doc_ref' => $invoicesupplier_static->ref,
1225 'date_creation' => $now,
1226 'doc_type' =>
'supplier_invoice',
1227 'fk_doc' => $invoicesupplier_static->id,
1229 'thirdparty_code' => $supplier_static->code_fournisseur,
1230 'subledger_account' => $account_supplier_subsidiary,
1231 'subledger_label' => $supplier_static->name,
1232 'numero_compte' => $account_supplier_general,
1233 'label_compte' => $acc_info_supplieraccount[
'label'],
1234 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Supplier'),
1235 'montant' => $ttc_part,
1237 'debit' => $ttc_part,
1239 'code_journal' => $journal,
1240 'journal_label' => $journal_label_formatted,
1241 'piece_num' =>
'OD-ESC-FRS-'.$invoicesupplier_static->ref,
1243 'fk_user_author' => $user->id,
1244 'entity' =>
$conf->entity,
1250 if ($type ==
'view') {
1251 $lines_view[] = array(
1252 'date' => $docdate_fmt,
1253 'piece' => $piece_link,
1255 'subledger_account' =>
'',
1256 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
1258 'credit' =>
price($ht_part),
1260 } elseif ($type ==
'bookkeeping' && $acc_info_discountreceived[
'found']) {
1261 $lines_book[] = array(
1262 'doc_date' => $docdate,
1263 'date_lim_reglement' =>
'',
1264 'doc_ref' => $invoicesupplier_static->ref,
1265 'date_creation' => $now,
1266 'doc_type' =>
'supplier_invoice',
1267 'fk_doc' => $invoicesupplier_static->id,
1269 'thirdparty_code' => $supplier_static->code_fournisseur,
1270 'subledger_account' =>
'',
1271 'subledger_label' =>
'',
1272 'numero_compte' => $acc_disc_recv,
1273 'label_compte' => $acc_info_discountreceived[
'label'],
1274 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
1275 'montant' => $ht_part,
1278 'credit' => $ht_part,
1279 'code_journal' => $journal,
1280 'journal_label' => $journal_label_formatted,
1281 'piece_num' =>
'OD-ESC-FRS-'.$invoicesupplier_static->ref,
1283 'fk_user_author' => $user->id,
1284 'entity' =>
$conf->entity,
1289 if ($tva_part > 0) {
1291 if ($type ==
'view') {
1292 $lines_view[] = array(
1293 'date' => $docdate_fmt,
1294 'piece' => $piece_link,
1296 'subledger_account' =>
'',
1297 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
1299 'credit' =>
price($tva_part),
1301 $element[
'blocks'][] = $lines_view;
1302 } elseif ($type ==
'bookkeeping' && $acc_info_vatbuy[
'found']) {
1303 $lines_book[] = array(
1304 'doc_date' => $docdate,
1305 'date_lim_reglement' =>
'',
1306 'doc_ref' => $invoicesupplier_static->ref,
1307 'date_creation' => $now,
1308 'doc_type' =>
'supplier_invoice',
1309 'fk_doc' => $invoicesupplier_static->id,
1311 'thirdparty_code' => $supplier_static->code_fournisseur,
1312 'subledger_account' =>
'',
1313 'subledger_label' =>
'',
1314 'numero_compte' => $acc_vat_ded,
1315 'label_compte' => $acc_info_vatbuy[
'label'],
1316 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
1317 'montant' => $tva_part,
1320 'credit' => $tva_part,
1321 'code_journal' => $journal,
1322 'journal_label' => $journal_label_formatted,
1323 'piece_num' =>
'OD-ESC-FRS-'.$invoicesupplier_static->ref,
1325 'fk_user_author' => $user->id,
1326 'entity' =>
$conf->entity,
1328 $element[
'blocks'][] = $lines_book;
1332 if ($type ==
'view') {
1333 $element[
'blocks'][] = $lines_view;
1334 } elseif ($type ==
'bookkeeping') {
1335 $element[
'blocks'][] = $lines_book;
1337 $element[
'blocks'][] = array($docdate, $invoicesupplier_static->ref,
length_accountg($account_supplier_general), $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Supplier'),
price($ttc_part),
'');
1338 $element[
'blocks'][] = array($docdate, $invoicesupplier_static->ref,
length_accountg($acc_disc_recv), $label_discount.
' ('.$rateStr.
'%)',
'',
price($ht_part));
1343 if ($type ==
'csv') {
1344 $element[
'blocks'][] = array(
1345 $docdate, $invoicesupplier_static->ref,
length_accountg($acc_vat_ded), $label_discount.
" ". $langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
'', $tva_part > 0 ?
price($tva_part) :
''
1350 $journal_data[(int) $invoicesupplier_static->id] = $element;
1353 return $journal_data;
1401 global
$conf, $langs, $hookmanager;
1402 require_once DOL_DOCUMENT_ROOT .
'/accountancy/class/bookkeeping.class.php';
1406 $hookmanager->initHooks(array(
'accountingjournaldao'));
1407 $parameters = array(
'journal_data' => &$journal_data);
1408 $reshook = $hookmanager->executeHooks(
'writeBookkeeping', $parameters, $this);
1410 $this->error = $hookmanager->error;
1411 $this->errors = $hookmanager->errors;
1413 } elseif (empty($reshook)) {
1415 if (!is_array($journal_data)) {
1416 $journal_data = array();
1419 foreach ($journal_data as $element_id => $element) {
1420 $error_for_line = 0;
1426 if ($element[
'error'] ==
'somelinesarenotbound') {
1429 $this->errors[] = $langs->trans(
'ErrorInvoiceContainsLinesNotYetBounded', $element[
'ref']);
1432 if (!$error_for_line) {
1433 foreach ($element[
'blocks'] as $lines) {
1434 foreach ($lines as $line) {
1436 $bookkeeping->doc_date = (int) $line[
'doc_date'];
1437 $bookkeeping->date_lim_reglement = (int) $line[
'date_lim_reglement'];
1438 $bookkeeping->doc_ref = $line[
'doc_ref'];
1439 $bookkeeping->date_creation = $line[
'date_creation'];
1440 $bookkeeping->doc_type = $line[
'doc_type'];
1441 $bookkeeping->fk_doc = $line[
'fk_doc'];
1442 $bookkeeping->fk_docdet = $line[
'fk_docdet'];
1443 $bookkeeping->thirdparty_code = $line[
'thirdparty_code'];
1444 $bookkeeping->subledger_account = $line[
'subledger_account'];
1445 $bookkeeping->subledger_label = $line[
'subledger_label'];
1446 $bookkeeping->numero_compte = $line[
'numero_compte'];
1447 $bookkeeping->label_compte = $line[
'label_compte'];
1448 $bookkeeping->label_operation = $line[
'label_operation'];
1449 $bookkeeping->montant = $line[
'montant'];
1450 $bookkeeping->sens = $line[
'sens'];
1451 $bookkeeping->debit = (float) $line[
'debit'];
1452 $bookkeeping->credit = (float) $line[
'credit'];
1453 $bookkeeping->code_journal = $line[
'code_journal'];
1454 $bookkeeping->journal_label = $line[
'journal_label'];
1455 $bookkeeping->piece_num = (int) $line[
'piece_num'];
1456 $bookkeeping->import_key = $line[
'import_key'];
1457 $bookkeeping->fk_user_author = $user->id;
1458 $bookkeeping->entity =
$conf->entity;
1460 $total_debit += $bookkeeping->debit;
1461 $total_credit += $bookkeeping->credit;
1463 $result = $bookkeeping->create($user);
1465 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
1468 $journal_data[$element_id][
'error'] =
'alreadyjournalized';
1472 $journal_data[$element_id][
'error'] =
'other';
1473 $this->errors[] = $bookkeeping->errorsToString();
1491 if ($error_for_line) {
1498 if (!$error_for_line && (
price2num($total_debit,
'MT') !=
price2num($total_credit,
'MT'))) {
1501 $journal_data[$element_id][
'error'] =
'amountsnotbalanced';
1502 $this->errors[] =
'Try to insert a non balanced transaction in book for ' . json_encode($element[
'blocks']) .
'. Canceled. Surely a bug.';
1505 if (!$error_for_line) {
1506 $this->db->commit();
1508 $this->db->rollback();
1510 if ($error >= $max_nb_errors) {
1511 $this->errors[] = $langs->trans(
"ErrorTooManyErrorsProcessStopped");
1518 return $error ? -$error : 1;
1542 public function exportCsv(&$journal_data = array(), $search_date_end = 0, $sep =
'')
1544 global
$conf, $langs, $hookmanager;
1552 $hookmanager->initHooks(array(
'accountingjournaldao'));
1553 $parameters = array(
'journal_data' => &$journal_data,
'search_date_end' => &$search_date_end,
'sep' => &$sep,
'out' => &$out);
1554 $reshook = $hookmanager->executeHooks(
'exportCsv', $parameters, $this);
1556 $this->error = $hookmanager->error;
1557 $this->errors = $hookmanager->errors;
1559 } elseif (empty($reshook)) {
1561 $journal_data = is_array($journal_data) ? $journal_data : array();
1565 if ($this->nature == 4) {
1567 $langs->transnoentitiesnoconv(
"BankId"),
1568 $langs->transnoentitiesnoconv(
"Date"),
1569 $langs->transnoentitiesnoconv(
"PaymentMode"),
1570 $langs->transnoentitiesnoconv(
"AccountAccounting"),
1571 $langs->transnoentitiesnoconv(
"LedgerAccount"),
1572 $langs->transnoentitiesnoconv(
"SubledgerAccount"),
1573 $langs->transnoentitiesnoconv(
"Label"),
1574 $langs->transnoentitiesnoconv(
"AccountingDebit"),
1575 $langs->transnoentitiesnoconv(
"AccountingCredit"),
1576 $langs->transnoentitiesnoconv(
"Journal"),
1577 $langs->transnoentitiesnoconv(
"Note"),
1579 } elseif ($this->nature == 5) {
1581 $langs->transnoentitiesnoconv(
"Date"),
1582 $langs->transnoentitiesnoconv(
"Piece"),
1583 $langs->transnoentitiesnoconv(
"AccountAccounting"),
1584 $langs->transnoentitiesnoconv(
"LabelOperation"),
1585 $langs->transnoentitiesnoconv(
"AccountingDebit"),
1586 $langs->transnoentitiesnoconv(
"AccountingCredit"),
1588 } elseif ($this->nature == 1) {
1590 $langs->transnoentitiesnoconv(
"Date"),
1591 $langs->transnoentitiesnoconv(
"Piece"),
1592 $langs->transnoentitiesnoconv(
"AccountAccounting"),
1593 $langs->transnoentitiesnoconv(
"LabelOperation"),
1594 $langs->transnoentitiesnoconv(
"AccountingDebit"),
1595 $langs->transnoentitiesnoconv(
"AccountingCredit"),
1599 if (!empty($header)) {
1600 $out .=
'"' . implode(
'"' . $sep .
'"', $header) .
'"' .
"\n";
1602 foreach ($journal_data as $element_id => $element) {
1603 foreach ($element[
'blocks'] as $lines) {
1604 foreach ($lines as $line) {
1605 $out .=
'"' . implode(
'"' . $sep .
'"', $line) .
'"' .
"\n";