211 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle =
'', $notooltip = 0)
213 global $langs, $conf, $hookmanager;
215 if (!empty($conf->dol_no_mouse_hover)) {
221 $url =
dolBuildUrl(DOL_URL_ROOT.
'/accountancy/admin/journals_list.php', [
'id' => 35]);
223 $label =
'<u>'.$langs->trans(
"ShowAccountingJournal").
'</u>';
224 if (!empty($this->code)) {
225 $label .=
'<br><b>'.$langs->trans(
'Code').
':</b> '.$this->code;
227 if (!empty($this->label)) {
228 $label .=
'<br><b>'.$langs->trans(
'Label').
':</b> '.$langs->transnoentities($this->label);
231 $label .=
' - '.$moretitle;
235 if (empty($notooltip)) {
237 $label = $langs->trans(
"ShowAccountingJournal");
238 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
240 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
241 $linkclose .=
' class="classfortooltip"';
244 $linkstart =
'<a href="'.$url.
'"';
245 $linkstart .= $linkclose.
'>';
254 $label_link = $this->code;
255 if ($withlabel == 1 && !empty($this->label)) {
256 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$langs->transnoentities($this->label).($nourl ?
'</span>' :
'');
258 if ($withlabel == 2 && !empty($this->nature)) {
259 $key = $langs->trans(
"AccountingJournalType".$this->nature);
260 $transferlabel = ($key !=
"AccountingJournalType".strtoupper($langs->trans((
string) $this->nature)) ? $key : $this->label);
261 $label_link .=
' - '.($nourl ?
'<span class="opacitymedium">' :
'').$transferlabel.($nourl ?
'</span>' :
'');
264 $result .= $linkstart;
266 $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);
268 if ($withpicto != 2) {
269 $result .= $label_link;
274 $hookmanager->initHooks(array(
'accountingjournaldao'));
275 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
276 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
278 $result = $hookmanager->resPrint;
280 $result .= $hookmanager->resPrint;
420 public function getAssetData(
User $user, $type =
'view', $date_start =
null, $date_end =
null, $in_bookkeeping =
'notyet')
422 global $conf, $langs;
428 require_once DOL_DOCUMENT_ROOT .
'/core/lib/accounting.lib.php';
429 require_once DOL_DOCUMENT_ROOT .
'/asset/class/asset.class.php';
430 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
431 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetdepreciationoptions.class.php';
433 $langs->loadLangs(array(
"assets"));
439 if (empty($in_bookkeeping)) {
440 $in_bookkeeping =
'notyet';
444 $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";
445 $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";
446 $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";
447 $sql .=
" FROM " . MAIN_DB_PREFIX .
"asset_depreciation as ad";
448 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"asset as a ON a.rowid = ad.fk_asset";
449 $sql .=
" WHERE a.entity IN (" .
getEntity(
'asset', 0) .
')';
450 $sql .=
" AND a.status > 0";
451 if ($in_bookkeeping ==
'already') {
452 $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')";
453 } elseif ($in_bookkeeping ==
'notyet') {
454 $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')";
456 $sql .=
" AND ad.ref != ''";
457 if ($date_start && $date_end) {
458 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate($date_start) .
"' AND ad.depreciation_date <= '" . $this->db->idate($date_end) .
"'";
462 $sql .=
" AND ad.depreciation_date >= '" . $this->db->idate(
getDolGlobalInt(
'ACCOUNTING_DATE_START_BINDING')) .
"'";
464 $sql .=
" ORDER BY ad.depreciation_date";
467 $resql = $this->db->query($sql);
469 $this->errors[] = $this->db->lasterror();
474 'elements' => array(),
476 while ($obj = $this->db->fetch_object($resql)) {
477 if (!isset($pre_data[
'elements'][$obj->rowid])) {
478 $pre_data[
'elements'][$obj->rowid] = array(
479 'ref' => $obj->asset_ref,
480 'label' => $obj->asset_label,
481 'acquisition_value_ht' => $obj->asset_acquisition_value_ht,
482 'depreciation' => array(),
486 if (isset($obj->asset_disposal_date)) {
487 $pre_data[
'elements'][$obj->rowid][
'disposal'] = array(
488 'date' => $this->db->jdate($obj->asset_disposal_date),
489 'amount' => $obj->asset_disposal_amount_ht,
490 'subject_to_vat' => !empty($obj->asset_disposal_subject_to_vat),
495 $compta_debit = empty($obj->accountancy_code_debit) ?
'NotDefined' : $obj->accountancy_code_debit;
496 $compta_credit = empty($obj->accountancy_code_credit) ?
'NotDefined' : $obj->accountancy_code_credit;
498 $pre_data[
'elements'][$obj->rowid][
'depreciation'][$obj->depreciation_id] = array(
499 'date' => $this->db->jdate($obj->depreciation_date),
500 'ref' => $obj->depreciation_ref,
502 $compta_debit => -$obj->depreciation_ht,
503 $compta_credit => $obj->depreciation_ht,
508 $disposal_ref = $langs->transnoentitiesnoconv(
'AssetDisposal');
509 $journal = $this->code;
510 $journal_label = $this->label;
511 $journal_label_formatted = $langs->transnoentities($journal_label);
514 $element_static =
new Asset($this->db);
516 $journal_data = array();
517 foreach ($pre_data[
'elements'] as $pre_data_id => $pre_data_info) {
518 $element_static->id = $pre_data_id;
519 $element_static->ref = (string) $pre_data_info[
"ref"];
520 $element_static->label = (string) $pre_data_info[
"label"];
521 $element_static->acquisition_value_ht = $pre_data_info[
"acquisition_value_ht"];
522 $element_link = $element_static->getNomUrl(1,
'with_label');
524 $element_name_formatted_0 =
dol_trunc($element_static->label, 16);
525 $label_operation = $element_static->getNomUrl(0,
'label', 16);
528 'ref' =>
dol_trunc($element_static->ref, 16,
'right',
'UTF-8', 1),
529 'error' => array_key_exists(
'error', $pre_data_info) ? $pre_data_info[
'error'] :
'',
535 foreach ($pre_data_info[
'depreciation'] as $depreciation_id => $line) {
536 $depreciation_ref = $line[
"ref"];
537 $depreciation_date = $line[
"date"];
538 $depreciation_date_formatted =
dol_print_date($depreciation_date,
'day');
542 foreach ($line[
'lines'] as $account => $mt) {
545 if ($type ==
'view') {
547 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
548 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
552 'date' => $depreciation_date_formatted,
553 'piece' => $element_link,
554 'account_accounting' => $account_to_show,
555 'subledger_account' =>
'',
556 'label_operation' => $label_operation .
' - ' . $depreciation_ref,
557 'debit' => $mt < 0 ?
price(-$mt) :
'',
558 'credit' => $mt >= 0 ?
price($mt) :
'',
560 } elseif ($type ==
'bookkeeping') {
561 if ($account_infos[
'found']) {
563 'doc_date' => $depreciation_date,
564 'date_lim_reglement' =>
'',
565 'doc_ref' => $element_static->ref,
566 'date_creation' => $now,
567 'doc_type' =>
'asset',
568 'fk_doc' => $element_static->id,
569 'fk_docdet' => $depreciation_id,
570 'thirdparty_code' =>
'',
571 'subledger_account' =>
'',
572 'subledger_label' =>
'',
573 'numero_compte' => $account,
574 'label_compte' => $account_infos[
'label'],
575 'label_operation' => $element_name_formatted_0 .
' - ' . $depreciation_ref,
577 'sens' => $mt < 0 ?
'D' :
'C',
578 'debit' => $mt < 0 ? -$mt : 0,
579 'credit' => $mt >= 0 ? $mt : 0,
580 'code_journal' => $journal,
581 'journal_label' => $journal_label_formatted,
584 'fk_user_author' => $user->id,
585 'entity' => $conf->entity,
591 $element_static->ref,
592 $account_infos[
'code_formatted_1'],
593 $element_name_formatted_0 .
' - ' . $depreciation_ref,
594 $mt < 0 ?
price(-$mt) :
'',
595 $mt >= 0 ?
price($mt) :
'',
599 $element[
'blocks'][] = $blocks;
604 if (!empty($pre_data_info[
'disposal'])) {
605 $disposal_date = $pre_data_info[
'disposal'][
'date'];
607 if ((!($date_start && $date_end) || ($date_start <= $disposal_date && $disposal_date <= $date_end)) &&
610 $disposal_amount = $pre_data_info[
'disposal'][
'amount'];
611 $disposal_subject_to_vat = $pre_data_info[
'disposal'][
'subject_to_vat'];
617 require_once DOL_DOCUMENT_ROOT .
'/asset/class/assetaccountancycodes.class.php';
619 $result = $accountancy_codes->fetchAccountancyCodes($element_static->id);
621 $element[
'error'] = $accountancy_codes->errorsToString();
624 $element_static->fetchDepreciationLines();
625 foreach ($element_static->depreciation_lines as $mode_key => $depreciation_lines) {
626 $accountancy_codes_list = $accountancy_codes->accountancy_codes[$mode_key];
628 if (!isset($accountancy_codes_list[
'value_asset_sold'])) {
632 $accountancy_code_value_asset_sold = empty($accountancy_codes_list[
'value_asset_sold']) ?
'NotDefined' : $accountancy_codes_list[
'value_asset_sold'];
633 $accountancy_code_depreciation_asset = empty($accountancy_codes_list[
'depreciation_asset']) ?
'NotDefined' : $accountancy_codes_list[
'depreciation_asset'];
634 $accountancy_code_asset = empty($accountancy_codes_list[
'asset']) ?
'NotDefined' : $accountancy_codes_list[
'asset'];
635 $accountancy_code_receivable_on_assignment = empty($accountancy_codes_list[
'receivable_on_assignment']) ?
'NotDefined' : $accountancy_codes_list[
'receivable_on_assignment'];
636 $accountancy_code_vat_collected = empty($accountancy_codes_list[
'vat_collected']) ?
'NotDefined' : $accountancy_codes_list[
'vat_collected'];
637 $accountancy_code_proceeds_from_sales = empty($accountancy_codes_list[
'proceeds_from_sales']) ?
'NotDefined' : $accountancy_codes_list[
'proceeds_from_sales'];
639 $last_cumulative_amount_ht = 0;
640 $depreciated_ids = array_keys($pre_data_info[
'depreciation']);
641 foreach ($depreciation_lines as $line) {
642 $last_cumulative_amount_ht = $line[
'cumulative_depreciation_ht'];
643 if (!in_array($line[
'id'], $depreciated_ids) && empty($line[
'bookkeeping']) && !empty($line[
'ref'])) {
649 $lines[0][$accountancy_code_value_asset_sold] = -((float) $element_static->acquisition_value_ht - $last_cumulative_amount_ht);
650 $lines[0][$accountancy_code_depreciation_asset] = - (float) $last_cumulative_amount_ht;
651 $lines[0][$accountancy_code_asset] = $element_static->acquisition_value_ht;
653 $disposal_amount_vat = $disposal_subject_to_vat ? (float)
price2num($disposal_amount * $disposal_vat / 100,
'MT') : 0;
654 $lines[1][$accountancy_code_receivable_on_assignment] = -($disposal_amount + $disposal_amount_vat);
655 if ($disposal_subject_to_vat) {
656 $lines[1][$accountancy_code_vat_collected] = $disposal_amount_vat;
658 $lines[1][$accountancy_code_proceeds_from_sales] = $disposal_amount;
660 foreach ($lines as $lines_block) {
662 foreach ($lines_block as $account => $mt) {
665 if ($type ==
'view') {
667 if (($account_to_show ==
"") || $account_to_show ==
'NotDefined') {
668 $account_to_show =
'<span class="error">' . $langs->trans(
"AssetInAccountNotDefined") .
'</span>';
672 'date' => $disposal_date_formatted,
673 'piece' => $element_link,
674 'account_accounting' => $account_to_show,
675 'subledger_account' =>
'',
676 'label_operation' => $label_operation .
' - ' . $disposal_ref,
677 'debit' => $mt < 0 ?
price(-$mt) :
'',
678 'credit' => $mt >= 0 ?
price($mt) :
'',
680 } elseif ($type ==
'bookkeeping') {
681 if ($account_infos[
'found']) {
683 'doc_date' => $disposal_date,
684 'date_lim_reglement' =>
'',
685 'doc_ref' => $element_static->ref,
686 'date_creation' => $now,
687 'doc_type' =>
'asset',
688 'fk_doc' => $element_static->id,
690 'thirdparty_code' =>
'',
691 'subledger_account' =>
'',
692 'subledger_label' =>
'',
693 'numero_compte' => $account,
694 'label_compte' => $account_infos[
'label'],
695 'label_operation' => $element_name_formatted_0 .
' - ' . $disposal_ref,
697 'sens' => $mt < 0 ?
'D' :
'C',
698 'debit' => $mt < 0 ? -$mt : 0,
699 'credit' => $mt >= 0 ? $mt : 0,
700 'code_journal' => $journal,
701 'journal_label' => $journal_label_formatted,
704 'fk_user_author' => $user->id,
705 'entity' => $conf->entity,
711 $element_static->ref,
712 $account_infos[
'code_formatted_1'],
713 $element_name_formatted_0 .
' - ' . $disposal_ref,
714 $mt < 0 ?
price(-$mt) :
'',
715 $mt >= 0 ?
price($mt) :
'',
719 $element[
'blocks'][] = $blocks;
726 $journal_data[(int) $pre_data_id] = $element;
730 return $journal_data;
743 public function getDiscountCustomer(
User $user, $type =
'view', $date_start =
null, $date_end =
null, $in_bookkeeping =
'notyet')
745 global $conf, $langs;
747 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
748 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
749 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
751 $langs->loadLangs(array(
'bills'));
757 if (empty($in_bookkeeping)) {
758 $in_bookkeeping =
'notyet';
762 $sql =
"SELECT f.rowid, f.ref, f.datef, f.date_closing, f.fk_soc, f.total_ttc";
763 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
764 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice', 0).
')';
765 $sql .=
" AND f.fk_statut > 0";
771 $sql .=
" AND f.close_code = 'discount_vat'";
772 if ($date_start && $date_end) {
773 $sql .=
" AND f.date_closing >= '".$this->db->idate($date_start).
"' AND f.date_closing <= '".$this->db->idate($date_end).
"'";
776 $sql .=
" AND f.date_closing >= '".$this->db->idate(
getDolGlobalInt(
'ACCOUNTING_DATE_START_BINDING')).
"'";
778 if ($in_bookkeeping ==
'already') {
779 $sql .=
" AND EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
780 $sql .=
" WHERE ab.doc_type = 'customer_invoice' AND ab.fk_doc = f.rowid";
781 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
782 } elseif ($in_bookkeeping ==
'notyet') {
783 $sql .=
" AND NOT EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
784 $sql .=
" WHERE ab.doc_type = 'customer_invoice' AND ab.fk_doc = f.rowid";
785 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
787 $sql .=
" ORDER BY f.date_closing";
790 $resql = $this->db->query($sql);
792 $this->errors[] = $this->db->lasterror();
796 $journal = $this->code;
797 $journal_label_formatted = $langs->transnoentities($this->label);
800 $journal_data = array();
801 $invoice_static =
new Facture($this->db);
802 $customer_static =
new Societe($this->db);
808 while ($obj = $this->db->fetch_object($resql)) {
809 if ($invoice_static->fetch((
int) $obj->rowid) <= 0) {
813 $customer_static->fetch($invoice_static->socid);
814 $account_customer_general = !empty($customer_static->accountancy_code_customer_general) ? $customer_static->accountancy_code_customer_general :
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER');
815 $account_customer_subsidiary = !empty($customer_static->code_compta_client) ? $customer_static->code_compta_client :
'';
817 $piece_link = $invoice_static->getNomUrl(1,
'withlabel');
820 $paid = (float)
price2num($invoice_static->getSommePaiement(),
'MT');
821 $usedcn = (float)
price2num($invoice_static->getSumCreditNotesUsed(),
'MT');
822 $useddep = (float)
price2num($invoice_static->getSumDepositsUsed(),
'MT');
823 $ttc_inv = (float)
price2num($invoice_static->total_ttc,
'MT');
824 $escompte_ttc = (float)
price2num(max(0, $ttc_inv - $paid - $usedcn - $useddep),
'MT');
825 if ($escompte_ttc <= 0) {
830 $thirdpartyname = (string) $customer_static->name;
831 $label_discount = $bookkeeping_static->accountingLabelForOperation($thirdpartyname, $invoice_static->ref, $langs->trans(
'DiscountGranted'));
834 $ttcByRate = array();
836 foreach ((array) $invoice_static->lines as $li) {
837 $ttc = (float) $li->total_ttc;
841 $key = number_format((
float) $li->tva_tx, 3,
'.',
'');
842 if (!isset($ttcByRate[$key])) {
843 $ttcByRate[$key] = 0.0;
845 $ttcByRate[$key] += $ttc;
848 if ($totalTTC <= 0) {
849 $ttcByRate = array(
"0.000" => $escompte_ttc);
850 $totalTTC = $escompte_ttc;
854 'ref' =>
dol_trunc($invoice_static->ref, 16,
'right',
'UTF-8', 1),
859 $closingdate = !empty($obj->date_closing) ? $obj->date_closing : $obj->datef;
861 $docdate = $this->db->jdate($closingdate);
866 $n = count($ttcByRate);
867 foreach ($ttcByRate as $rateStr => $ttcRateOnInvoice) {
869 $rate = (float) $rateStr;
871 $ttc_part = (float) $escompte_ttc * ($ttcRateOnInvoice / $totalTTC);
873 $ttc_part = (float)
price2num($escompte_ttc - $sumTTC,
'MT');
875 $ttc_part = (float)
price2num($ttc_part,
'MT');
876 $sumTTC = (float)
price2num($sumTTC + $ttc_part,
'MT');
880 $ht_part = (float)
price2num($ttc_part / (1 + $rate / 100),
'MT');
881 $tva_part = (float)
price2num($ttc_part - $ht_part,
'MT');
883 $ht_part = $ttc_part;
889 $acc_vat_coll = $acc_vat_coll_def;
891 $lines_view = array();
892 $lines_book = array();
896 if ($type ==
'view') {
897 $lines_view[] = array(
898 'date' => $docdate_fmt,
899 'piece' => $piece_link,
901 'subledger_account' =>
'',
902 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
903 'debit' =>
price($ht_part),
906 } elseif ($type ==
'bookkeeping' && $acc_info_discountgranted[
'found']) {
907 $lines_book[] = array(
908 'doc_date' => $docdate,
909 'date_lim_reglement' =>
'',
910 'doc_ref' => $invoice_static->ref,
911 'date_creation' => $now,
912 'doc_type' =>
'customer_invoice',
913 'fk_doc' => $invoice_static->id,
915 'thirdparty_code' => $customer_static->code_client,
916 'subledger_account' =>
'',
917 'subledger_label' =>
'',
918 'numero_compte' => $acc_disc_granted,
919 'label_compte' => $acc_info_discountgranted[
'label'],
920 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
921 'montant' => $ht_part,
925 'code_journal' => $journal,
926 'journal_label' => $journal_label_formatted,
927 'piece_num' =>
'OD-ESC-'.$invoice_static->ref,
929 'fk_user_author' => $user->id,
930 'entity' => $conf->entity,
937 if ($type ==
'view') {
938 $lines_view[] = array(
939 'date' => $docdate_fmt,
940 'piece' => $piece_link,
942 'subledger_account' =>
'',
943 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
944 'debit' =>
price($tva_part),
947 } elseif ($type ==
'bookkeeping' && $acc_info_vatbuy[
'found']) {
948 $lines_book[] = array(
949 'doc_date' => $docdate,
950 'date_lim_reglement' =>
'',
951 'doc_ref' => $invoice_static->ref,
952 'date_creation' => $now,
953 'doc_type' =>
'customer_invoice',
954 'fk_doc' => $invoice_static->id,
956 'thirdparty_code' => $customer_static->code_client,
957 'subledger_account' =>
'',
958 'subledger_label' =>
'',
959 'numero_compte' => $acc_vat_coll,
960 'label_compte' => $acc_info_vatbuy[
'label'],
961 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
962 'montant' => $tva_part,
964 'debit' => $tva_part,
966 'code_journal' => $journal,
967 'journal_label' => $journal_label_formatted,
968 'piece_num' =>
'OD-ESC-'.$invoice_static->ref,
970 'fk_user_author' => $user->id,
971 'entity' => $conf->entity,
978 if ($type ==
'view') {
979 $lines_view[] = array(
980 'date' => $docdate_fmt,
981 'piece' => $piece_link,
984 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Customer'),
986 'credit' =>
price($ttc_part),
988 $element[
'blocks'][] = $lines_view;
989 } elseif ($type ==
'bookkeeping' && $acc_info_customeraccount[
'found']) {
990 $lines_book[] = array(
991 'doc_date' => $docdate,
992 'date_lim_reglement' =>
'',
993 'doc_ref' => $invoice_static->ref,
994 'date_creation' => $now,
995 'doc_type' =>
'customer_invoice',
996 'fk_doc' => $invoice_static->id,
998 'thirdparty_code' => $customer_static->code_client,
999 'subledger_account' => $account_customer_subsidiary,
1000 'subledger_label' => $customer_static->name,
1001 'numero_compte' => $account_customer_general,
1002 'label_compte' => $acc_info_customeraccount[
'label'],
1003 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Customer'),
1004 'montant' => $ttc_part,
1007 'credit' => $ttc_part,
1008 'code_journal' => $journal,
1009 'journal_label' => $journal_label_formatted,
1010 'piece_num' =>
'OD-ESC-'.$invoice_static->ref,
1012 'fk_user_author' => $user->id,
1013 'entity' => $conf->entity,
1015 $element[
'blocks'][] = $lines_book;
1017 $element[
'blocks'][] = array(
1019 $invoice_static->ref,
1021 $label_discount.
" (".$rateStr.
"%)",
1025 if ($tva_part > 0) {
1026 $element[
'blocks'][] = array(
1027 $docdate, $invoice_static->ref,
length_accountg($acc_vat_coll), $label_discount.
" ". $langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
price($tva_part),
''
1030 $element[
'blocks'][] = array(
1031 $docdate, $invoice_static->ref,
length_accountg($account_customer_general), $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Customer'),
'',
price($ttc_part)
1036 $journal_data[(int) $invoice_static->id] = $element;
1039 return $journal_data;
1054 global $conf, $langs;
1056 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
1057 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
1058 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
1060 $langs->loadLangs(array(
'suppliers'));
1066 if (empty($in_bookkeeping)) {
1067 $in_bookkeeping =
'notyet';
1071 $sql =
"SELECT ff.rowid, ff.ref, ff.datef, ff.date_closing, ff.fk_soc, ff.total_ttc";
1072 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as ff";
1073 $sql .=
" WHERE ff.entity IN (".getEntity(
'facture_fourn', 0).
")";
1074 $sql .=
" AND ff.fk_statut > 0";
1080 $sql .=
" AND ff.close_code = 'discount_vat'";
1081 if ($date_start && $date_end) {
1082 $sql .=
" AND ff.date_closing >= '".$this->db->idate($date_start).
"' AND ff.date_closing <= '".$this->db->idate($date_end).
"'";
1085 $sql .=
" AND ff.date_closing >= '".$this->db->idate(
getDolGlobalInt(
'ACCOUNTING_DATE_START_BINDING')).
"'";
1087 if ($in_bookkeeping ==
'already') {
1088 $sql .=
" AND EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
1089 $sql .=
" WHERE ab.doc_type = 'supplier_invoice' AND ab.fk_doc = ff.rowid";
1090 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
1091 } elseif ($in_bookkeeping ==
'notyet') {
1092 $sql .=
" AND NOT EXISTS (SELECT 1 FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping ab";
1093 $sql .=
" WHERE ab.doc_type = 'supplier_invoice' AND ab.fk_doc = ff.rowid";
1094 $sql .=
" AND ab.code_journal = '".$this->db->escape($this->code).
"')";
1096 $sql .=
" ORDER BY ff.date_closing";
1099 $resql = $this->db->query($sql);
1101 $this->errors[] = $this->db->lasterror();
1105 $journal = $this->code;
1106 $journal_label_formatted = $langs->transnoentities($this->label);
1109 $journal_data = array();
1111 $supplier_static =
new Societe($this->db);
1117 while ($obj = $this->db->fetch_object($resql)) {
1118 if ($invoicesupplier_static->fetch((
int) $obj->rowid) <= 0) {
1122 $supplier_static->fetch($invoicesupplier_static->socid);
1123 $account_supplier_general = !empty($supplier_static->accountancy_code_supplier_general) ? $supplier_static->accountancy_code_supplier_general :
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER');
1124 $account_supplier_subsidiary = !empty($supplier_static->code_compta_fournisseur) ? $supplier_static->code_compta_fournisseur :
'';
1126 $piece_link = $invoicesupplier_static->getNomUrl(1,
'withlabel');
1129 $paid = (float)
price2num($invoicesupplier_static->getSommePaiement(),
'MT');
1130 $usedcn = (float)
price2num($invoicesupplier_static->getSumCreditNotesUsed(),
'MT');
1131 $useddep = (float)
price2num($invoicesupplier_static->getSumDepositsUsed(),
'MT');
1132 $ttc_inv = (float)
price2num($invoicesupplier_static->total_ttc,
'MT');
1133 $escompte_ttc = (float)
price2num(max(0, $ttc_inv - $paid - $usedcn - $useddep),
'MT');
1134 if ($escompte_ttc <= 0) {
1139 $thirdpartyname = (string) $supplier_static->name;
1140 $label_discount = $bookkeeping_static->accountingLabelForOperation($thirdpartyname, $invoicesupplier_static->ref, $langs->trans(
'DiscountReceived'));
1143 $ttcByRate = array();
1145 foreach ((array) $invoicesupplier_static->lines as $li) {
1146 $ttc = (float) $li->total_ttc;
1150 $key = number_format((
float) $li->tva_tx, 3,
'.',
'');
1151 if (!isset($ttcByRate[$key])) {
1152 $ttcByRate[$key] = 0.0;
1154 $ttcByRate[$key] += $ttc;
1157 if ($totalTTC <= 0) {
1158 $ttcByRate = array(
"0.000" => $escompte_ttc);
1159 $totalTTC = $escompte_ttc;
1163 'ref' =>
dol_trunc($invoicesupplier_static->ref, 16,
'right',
'UTF-8', 1),
1165 'blocks' => array(),
1168 $closingdate = !empty($obj->date_closing) ? $obj->date_closing : $obj->datef;
1170 $docdate = $this->db->jdate($closingdate);
1175 $n = count($ttcByRate);
1176 foreach ($ttcByRate as $rateStr => $ttcRateOnInvoice) {
1178 $rate = (float) $rateStr;
1180 $ttc_part = $escompte_ttc * ($ttcRateOnInvoice / $totalTTC);
1182 $ttc_part = (float)
price2num($escompte_ttc - $sumTTC,
'MT');
1184 $ttc_part = (float)
price2num($ttc_part,
'MT');
1185 $sumTTC = (float)
price2num($sumTTC + $ttc_part,
'MT');
1189 $ht_part = (float)
price2num($ttc_part / (1 + $rate / 100),
'MT');
1190 $tva_part = (float)
price2num($ttc_part - $ht_part,
'MT');
1192 $ht_part = $ttc_part;
1198 $acc_vat_ded = $acc_vat_ded_def;
1200 $lines_view = array();
1201 $lines_book = array();
1205 if ($type ==
'view') {
1206 $lines_view[] = array(
1207 'date' => $docdate_fmt,
1208 'piece' => $piece_link,
1211 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Supplier'),
1212 'debit' =>
price($ttc_part),
1215 } elseif ($type ==
'bookkeeping' && $acc_info_supplieraccount[
'found']) {
1216 $lines_book[] = array(
1217 'doc_date' => $docdate,
1218 'date_lim_reglement' =>
'',
1219 'doc_ref' => $invoicesupplier_static->ref,
1220 'date_creation' => $now,
1221 'doc_type' =>
'supplier_invoice',
1222 'fk_doc' => $invoicesupplier_static->id,
1224 'thirdparty_code' => $supplier_static->code_fournisseur,
1225 'subledger_account' => $account_supplier_subsidiary,
1226 'subledger_label' => $supplier_static->name,
1227 'numero_compte' => $account_supplier_general,
1228 'label_compte' => $acc_info_supplieraccount[
'label'],
1229 'label_operation' => $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Supplier'),
1230 'montant' => $ttc_part,
1232 'debit' => $ttc_part,
1234 'code_journal' => $journal,
1235 'journal_label' => $journal_label_formatted,
1236 'piece_num' =>
'OD-ESC-FRS-'.$invoicesupplier_static->ref,
1238 'fk_user_author' => $user->id,
1239 'entity' => $conf->entity,
1245 if ($type ==
'view') {
1246 $lines_view[] = array(
1247 'date' => $docdate_fmt,
1248 'piece' => $piece_link,
1250 'subledger_account' =>
'',
1251 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
1253 'credit' =>
price($ht_part),
1255 } elseif ($type ==
'bookkeeping' && $acc_info_discountreceived[
'found']) {
1256 $lines_book[] = array(
1257 'doc_date' => $docdate,
1258 'date_lim_reglement' =>
'',
1259 'doc_ref' => $invoicesupplier_static->ref,
1260 'date_creation' => $now,
1261 'doc_type' =>
'supplier_invoice',
1262 'fk_doc' => $invoicesupplier_static->id,
1264 'thirdparty_code' => $supplier_static->code_fournisseur,
1265 'subledger_account' =>
'',
1266 'subledger_label' =>
'',
1267 'numero_compte' => $acc_disc_recv,
1268 'label_compte' => $acc_info_discountreceived[
'label'],
1269 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'HT') .
" (".$rateStr.
"%)",
1270 'montant' => $ht_part,
1273 'credit' => $ht_part,
1274 'code_journal' => $journal,
1275 'journal_label' => $journal_label_formatted,
1276 'piece_num' =>
'OD-ESC-FRS-'.$invoicesupplier_static->ref,
1278 'fk_user_author' => $user->id,
1279 'entity' => $conf->entity,
1284 if ($tva_part > 0) {
1286 if ($type ==
'view') {
1287 $lines_view[] = array(
1288 'date' => $docdate_fmt,
1289 'piece' => $piece_link,
1291 'subledger_account' =>
'',
1292 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
1294 'credit' =>
price($tva_part),
1296 $element[
'blocks'][] = $lines_view;
1297 } elseif ($type ==
'bookkeeping' && $acc_info_vatbuy[
'found']) {
1298 $lines_book[] = array(
1299 'doc_date' => $docdate,
1300 'date_lim_reglement' =>
'',
1301 'doc_ref' => $invoicesupplier_static->ref,
1302 'date_creation' => $now,
1303 'doc_type' =>
'supplier_invoice',
1304 'fk_doc' => $invoicesupplier_static->id,
1306 'thirdparty_code' => $supplier_static->code_fournisseur,
1307 'subledger_account' =>
'',
1308 'subledger_label' =>
'',
1309 'numero_compte' => $acc_vat_ded,
1310 'label_compte' => $acc_info_vatbuy[
'label'],
1311 'label_operation' => $label_discount.
" - " .$langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
1312 'montant' => $tva_part,
1315 'credit' => $tva_part,
1316 'code_journal' => $journal,
1317 'journal_label' => $journal_label_formatted,
1318 'piece_num' =>
'OD-ESC-FRS-'.$invoicesupplier_static->ref,
1320 'fk_user_author' => $user->id,
1321 'entity' => $conf->entity,
1323 $element[
'blocks'][] = $lines_book;
1327 if ($type ==
'view') {
1328 $element[
'blocks'][] = $lines_view;
1329 } elseif ($type ==
'bookkeeping') {
1330 $element[
'blocks'][] = $lines_book;
1332 $element[
'blocks'][] = array($docdate, $invoicesupplier_static->ref,
length_accountg($account_supplier_general), $label_discount.
' - '.$langs->transnoentitiesnoconv(
'Supplier'),
price($ttc_part),
'');
1333 $element[
'blocks'][] = array($docdate, $invoicesupplier_static->ref,
length_accountg($acc_disc_recv), $label_discount.
' ('.$rateStr.
'%)',
'',
price($ht_part));
1338 if ($type ==
'csv') {
1339 $element[
'blocks'][] = array(
1340 $docdate, $invoicesupplier_static->ref,
length_accountg($acc_vat_ded), $label_discount.
" ". $langs->transnoentitiesnoconv(
'VAT') .
" (".$rateStr.
"%)",
'', $tva_part > 0 ?
price($tva_part) :
''
1345 $journal_data[(int) $invoicesupplier_static->id] = $element;
1348 return $journal_data;
1396 global $conf, $langs, $hookmanager;
1397 require_once DOL_DOCUMENT_ROOT .
'/accountancy/class/bookkeeping.class.php';
1401 $hookmanager->initHooks(array(
'accountingjournaldao'));
1402 $parameters = array(
'journal_data' => &$journal_data);
1403 $reshook = $hookmanager->executeHooks(
'writeBookkeeping', $parameters, $this);
1405 $this->error = $hookmanager->error;
1406 $this->errors = $hookmanager->errors;
1408 } elseif (empty($reshook)) {
1410 if (!is_array($journal_data)) {
1411 $journal_data = array();
1414 foreach ($journal_data as $element_id => $element) {
1415 $error_for_line = 0;
1421 if ($element[
'error'] ==
'somelinesarenotbound') {
1424 $this->errors[] = $langs->trans(
'ErrorInvoiceContainsLinesNotYetBounded', $element[
'ref']);
1427 if (!$error_for_line) {
1428 foreach ($element[
'blocks'] as $lines) {
1429 foreach ($lines as $line) {
1431 $bookkeeping->doc_date = (int) $line[
'doc_date'];
1432 $bookkeeping->date_lim_reglement = (int) $line[
'date_lim_reglement'];
1433 $bookkeeping->doc_ref = $line[
'doc_ref'];
1434 $bookkeeping->date_creation = $line[
'date_creation'];
1435 $bookkeeping->doc_type = $line[
'doc_type'];
1436 $bookkeeping->fk_doc = $line[
'fk_doc'];
1437 $bookkeeping->fk_docdet = $line[
'fk_docdet'];
1438 $bookkeeping->thirdparty_code = $line[
'thirdparty_code'];
1439 $bookkeeping->subledger_account = $line[
'subledger_account'];
1440 $bookkeeping->subledger_label = $line[
'subledger_label'];
1441 $bookkeeping->numero_compte = $line[
'numero_compte'];
1442 $bookkeeping->label_compte = $line[
'label_compte'];
1443 $bookkeeping->label_operation = $line[
'label_operation'];
1444 $bookkeeping->montant = $line[
'montant'];
1445 $bookkeeping->sens = $line[
'sens'];
1446 $bookkeeping->debit = (float) $line[
'debit'];
1447 $bookkeeping->credit = (float) $line[
'credit'];
1448 $bookkeeping->code_journal = $line[
'code_journal'];
1449 $bookkeeping->journal_label = $line[
'journal_label'];
1450 $bookkeeping->piece_num = (int) $line[
'piece_num'];
1451 $bookkeeping->import_key = $line[
'import_key'];
1452 $bookkeeping->fk_user_author = $user->id;
1453 $bookkeeping->entity = $conf->entity;
1455 $total_debit += $bookkeeping->debit;
1456 $total_credit += $bookkeeping->credit;
1458 $result = $bookkeeping->create($user);
1460 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
1463 $journal_data[$element_id][
'error'] =
'alreadyjournalized';
1467 $journal_data[$element_id][
'error'] =
'other';
1468 $this->errors[] = $bookkeeping->errorsToString();
1486 if ($error_for_line) {
1493 if (!$error_for_line && (
price2num($total_debit,
'MT') !=
price2num($total_credit,
'MT'))) {
1496 $journal_data[$element_id][
'error'] =
'amountsnotbalanced';
1497 $this->errors[] =
'Try to insert a non balanced transaction in book for ' . json_encode($element[
'blocks']) .
'. Canceled. Surely a bug.';
1500 if (!$error_for_line) {
1501 $this->db->commit();
1503 $this->db->rollback();
1505 if ($error >= $max_nb_errors) {
1506 $this->errors[] = $langs->trans(
"ErrorTooManyErrorsProcessStopped");
1513 return $error ? -$error : 1;
1537 public function exportCsv(&$journal_data = array(), $search_date_end = 0, $sep =
'')
1539 global $conf, $langs, $hookmanager;
1547 $hookmanager->initHooks(array(
'accountingjournaldao'));
1548 $parameters = array(
'journal_data' => &$journal_data,
'search_date_end' => &$search_date_end,
'sep' => &$sep,
'out' => &$out);
1549 $reshook = $hookmanager->executeHooks(
'exportCsv', $parameters, $this);
1551 $this->error = $hookmanager->error;
1552 $this->errors = $hookmanager->errors;
1554 } elseif (empty($reshook)) {
1556 $journal_data = is_array($journal_data) ? $journal_data : array();
1560 if ($this->nature == 4) {
1562 $langs->transnoentitiesnoconv(
"BankId"),
1563 $langs->transnoentitiesnoconv(
"Date"),
1564 $langs->transnoentitiesnoconv(
"PaymentMode"),
1565 $langs->transnoentitiesnoconv(
"AccountAccounting"),
1566 $langs->transnoentitiesnoconv(
"LedgerAccount"),
1567 $langs->transnoentitiesnoconv(
"SubledgerAccount"),
1568 $langs->transnoentitiesnoconv(
"Label"),
1569 $langs->transnoentitiesnoconv(
"AccountingDebit"),
1570 $langs->transnoentitiesnoconv(
"AccountingCredit"),
1571 $langs->transnoentitiesnoconv(
"Journal"),
1572 $langs->transnoentitiesnoconv(
"Note"),
1574 } elseif ($this->nature == 5) {
1576 $langs->transnoentitiesnoconv(
"Date"),
1577 $langs->transnoentitiesnoconv(
"Piece"),
1578 $langs->transnoentitiesnoconv(
"AccountAccounting"),
1579 $langs->transnoentitiesnoconv(
"LabelOperation"),
1580 $langs->transnoentitiesnoconv(
"AccountingDebit"),
1581 $langs->transnoentitiesnoconv(
"AccountingCredit"),
1583 } elseif ($this->nature == 1) {
1585 $langs->transnoentitiesnoconv(
"Date"),
1586 $langs->transnoentitiesnoconv(
"Piece"),
1587 $langs->transnoentitiesnoconv(
"AccountAccounting"),
1588 $langs->transnoentitiesnoconv(
"LabelOperation"),
1589 $langs->transnoentitiesnoconv(
"AccountingDebit"),
1590 $langs->transnoentitiesnoconv(
"AccountingCredit"),
1594 if (!empty($header)) {
1595 $out .=
'"' . implode(
'"' . $sep .
'"', $header) .
'"' .
"\n";
1597 foreach ($journal_data as $element_id => $element) {
1598 foreach ($element[
'blocks'] as $lines) {
1599 foreach ($lines as $line) {
1600 $out .=
'"' . implode(
'"' . $sep .
'"', $line) .
'"' .
"\n";