112 if (
$object->code_compta_client ==
'411CUSTCODE') {
113 $object->code_compta_client =
'';
116 if (
$object->code_compta_fournisseur ==
'401SUPPCODE') {
117 $object->code_compta_fournisseur =
'';
123 $sql =
"SELECT DISTINCT bk.rowid, bk.doc_date, bk.doc_type, bk.doc_ref, bk.subledger_account, ";
124 $sql .=
" bk.numero_compte , bk.label_compte, bk.debit , bk.credit, bk.montant ";
125 $sql .=
" , bk.sens , bk.code_journal , bk.piece_num, bk.date_lettering, bu.url_id , bu.type ";
126 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as bk";
127 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"bank_url as bu ON(bk.fk_doc = bu.fk_bank AND bu.type IN ('payment', 'payment_supplier') ) ";
129 if (
$object->code_compta_client !=
"") {
130 $sql .=
" bk.subledger_account = '".$this->db->escape(
$object->code_compta_client).
"' ";
132 if (
$object->code_compta_client !=
"" &&
$object->code_compta_fournisseur !=
"") {
135 if (
$object->code_compta_fournisseur !=
"") {
136 $sql .=
" bk.subledger_account = '".$this->db->escape(
$object->code_compta_fournisseur).
"' ";
139 $sql .=
" ) AND (bk.date_lettering ='' OR bk.date_lettering IS NULL) ";
140 $sql .=
" AND (bk.lettering_code != '' OR bk.lettering_code IS NULL) ";
141 $sql .=
' AND bk.date_validated IS NULL ';
142 $sql .= $this->db->order(
'bk.doc_date',
'DESC');
146 $resql = $this->db->query($sql);
148 $num = $this->db->num_rows($resql);
150 while ($obj = $this->db->fetch_object($resql)) {
154 if ($obj->type ==
'payment_supplier') {
155 $sql =
'SELECT DISTINCT bk.rowid, facf.ref, facf.ref_supplier, payf.fk_bank, facf.rowid as fact_id';
156 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn facf ";
157 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid";
158 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid";
159 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"accounting_bookkeeping as bk ON (bk.fk_doc = payf.fk_bank AND bk.code_journal='".$this->db->escape($obj->code_journal).
"')";
160 $sql .=
" WHERE payfacf.fk_paiementfourn = '".$this->db->escape($obj->url_id).
"' ";
161 $sql .=
" AND facf.entity = ".$conf->entity;
162 $sql .=
" AND code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX.
"accounting_journal WHERE nature=4 AND entity=".$conf->entity.
") ";
164 if (
$object->code_compta_client !=
"") {
165 $sql .=
" bk.subledger_account = '".$this->db->escape(
$object->code_compta_client).
"' ";
167 if (
$object->code_compta_client !=
"" &&
$object->code_compta_fournisseur !=
"") {
170 if (
$object->code_compta_fournisseur !=
"") {
171 $sql .=
" bk.subledger_account = '".$this->db->escape(
$object->code_compta_fournisseur).
"' ";
175 $resql2 = $this->db->query($sql);
177 while ($obj2 = $this->db->fetch_object($resql2)) {
178 $ids[$obj2->rowid] = $obj2->rowid;
179 $ids_fact[] = $obj2->fact_id;
181 $this->db->free($resql2);
183 $this->errors[] = $this->db->lasterror;
186 if (count($ids_fact)) {
187 $sql =
'SELECT bk.rowid, facf.ref, facf.ref_supplier ';
188 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn facf ";
189 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"accounting_bookkeeping as bk ON( bk.fk_doc = facf.rowid AND facf.rowid IN (".$this->db->sanitize(implode(
',', $ids_fact)).
"))";
190 $sql .=
" WHERE bk.code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX.
"accounting_journal WHERE nature=3 AND entity=".$conf->entity.
") ";
191 $sql .=
" AND facf.entity = ".$conf->entity;
193 if (
$object->code_compta_client !=
"") {
194 $sql .=
" bk.subledger_account = '".$this->db->escape(
$object->code_compta_client).
"' ";
196 if (
$object->code_compta_client !=
"" &&
$object->code_compta_fournisseur !=
"") {
199 if (
$object->code_compta_fournisseur !=
"") {
200 $sql .=
" bk.subledger_account = '".$this->db->escape(
$object->code_compta_fournisseur).
"' ";
204 $resql2 = $this->db->query($sql);
206 while ($obj2 = $this->db->fetch_object($resql2)) {
207 $ids[$obj2->rowid] = $obj2->rowid;
209 $this->db->free($resql2);
211 $this->errors[] = $this->db->lasterror;
215 } elseif ($obj->type ==
'payment') {
216 $sql =
'SELECT DISTINCT bk.rowid, fac.ref, fac.ref, pay.fk_bank, fac.rowid as fact_id';
217 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture fac ";
218 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"paiement_facture as payfac ON payfac.fk_facture=fac.rowid";
219 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"paiement as pay ON payfac.fk_paiement=pay.rowid";
220 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"accounting_bookkeeping as bk ON (bk.fk_doc = pay.fk_bank AND bk.code_journal='".$this->db->escape($obj->code_journal).
"')";
221 $sql .=
" WHERE payfac.fk_paiement = '".$this->db->escape($obj->url_id).
"' ";
222 $sql .=
" AND bk.code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX.
"accounting_journal WHERE nature=4 AND entity=".$conf->entity.
") ";
223 $sql .=
" AND fac.entity IN (".getEntity(
'invoice', 0).
")";
225 if (
$object->code_compta_client !=
"") {
226 $sql .=
" bk.subledger_account = '".$this->db->escape(
$object->code_compta_client).
"' ";
228 if (
$object->code_compta_client !=
"" &&
$object->code_compta_fournisseur !=
"") {
231 if (
$object->code_compta_fournisseur !=
"") {
232 $sql .=
" bk.subledger_account = '".$this->db->escape(
$object->code_compta_fournisseur).
"' ";
236 $resql2 = $this->db->query($sql);
238 while ($obj2 = $this->db->fetch_object($resql2)) {
239 $ids[$obj2->rowid] = $obj2->rowid;
240 $ids_fact[] = $obj2->fact_id;
243 $this->errors[] = $this->db->lasterror;
246 if (count($ids_fact)) {
247 $sql =
'SELECT bk.rowid, fac.ref, fac.ref_supplier ';
248 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture fac ";
249 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"accounting_bookkeeping as bk ON( bk.fk_doc = fac.rowid AND fac.rowid IN (".$this->db->sanitize(implode(
',', $ids_fact)).
"))";
250 $sql .=
" WHERE code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX.
"accounting_journal WHERE nature=2 AND entity=".$conf->entity.
") ";
251 $sql .=
" AND fac.entity IN (".getEntity(
'invoice', 0).
")";
253 if (
$object->code_compta_client !=
"") {
254 $sql .=
" bk.subledger_account = '".$this->db->escape(
$object->code_compta_client).
"' ";
256 if (
$object->code_compta_client !=
"" &&
$object->code_compta_fournisseur !=
"") {
259 if (
$object->code_compta_fournisseur !=
"") {
260 $sql .=
" bk.subledger_account = '".$this->db->escape(
$object->code_compta_fournisseur).
"' ";
264 $resql2 = $this->db->query($sql);
266 while ($obj2 = $this->db->fetch_object($resql2)) {
267 $ids[$obj2->rowid] = $obj2->rowid;
269 $this->db->free($resql2);
271 $this->errors[] = $this->db->lasterror;
277 if (count($ids) > 1) {
281 $this->db->free($resql);
284 foreach ($this->errors as $errmsg) {
286 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
307 $letter = str_pad(
"",
getDolGlobalInt(
'ACCOUNTING_LETTERING_NBLETTERS', 3), $partial ?
'a' :
'A');
312 $sql =
"SELECT ab.lettering_code, GROUP_CONCAT(DISTINCT ab.rowid SEPARATOR ',') AS bookkeeping_ids";
313 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
314 $sql .=
" WHERE ab.rowid IN (" . $this->db->sanitize(implode(
',', $ids)) .
")";
315 $sql .=
" GROUP BY ab.lettering_code";
316 $sql .=
" ORDER BY ab.lettering_code DESC";
318 dol_syslog(__METHOD__ .
" - Check partial / normal lettering case", LOG_DEBUG);
319 $resql = $this->db->query($sql);
321 while ($obj = $this->db->fetch_object($resql)) {
322 if (empty($obj->lettering_code)) {
327 if ($partial && preg_match(
'/^[A-Z]+$/', $obj->lettering_code)) {
328 if (!empty($obj->bookkeeping_ids)) {
329 $ids = array_diff($ids, explode(
',', $obj->bookkeeping_ids));
331 } elseif (!$partial && preg_match(
'/^[a-z]+$/', $obj->lettering_code)) {
333 $sql2 =
"UPDATE " . MAIN_DB_PREFIX .
"accounting_bookkeeping SET";
334 $sql2 .=
" lettering_code = NULL";
335 $sql2 .=
", date_lettering = NULL";
336 $sql2 .=
" WHERE entity IN (" .
getEntity(
'accountancy') .
")";
337 $sql2 .=
" AND lettering_code = '" . $this->db->escape($obj->lettering_code) .
"'";
339 dol_syslog(__METHOD__ .
" - Remove partial lettering", LOG_DEBUG);
340 $resql2 = $this->db->query($sql2);
342 $this->errors[] =
'Error' . $this->db->lasterror();
348 $this->db->free($resql);
350 $this->errors[] =
'Error' . $this->db->lasterror();
354 if (!$error && !empty($ids)) {
356 $sql =
"SELECT DISTINCT ab2.lettering_code";
357 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
358 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab2 ON ab2.subledger_account = ab.subledger_account";
359 $sql .=
" WHERE ab.rowid IN (" . $this->db->sanitize(implode(
',', $ids)) .
")";
360 $sql .=
" AND ab2.lettering_code != ''";
361 $sql .=
" ORDER BY ab2.lettering_code DESC";
363 dol_syslog(__METHOD__ .
" - Get next code", LOG_DEBUG);
364 $resql = $this->db->query($sql);
366 while ($obj = $this->db->fetch_object($resql)) {
367 if (!empty($obj->lettering_code) &&
368 (($partial && preg_match(
'/^[a-z]+$/', $obj->lettering_code)) ||
369 (!$partial && preg_match(
'/^[A-Z]+$/', $obj->lettering_code)))
371 $letter = $obj->lettering_code;
376 $this->db->free($resql);
378 $this->errors[] =
'Error' . $this->db->lasterror();
383 if (!$error && !$partial) {
384 $sql =
"SELECT SUM(ABS(debit)) as deb, SUM(ABS(credit)) as cred FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping WHERE ";
385 $sql .=
" rowid IN (" . $this->db->sanitize(implode(
',', $ids)) .
") AND lettering_code IS NULL AND subledger_account != ''";
387 dol_syslog(__METHOD__ .
" - Test amount integrity", LOG_DEBUG);
388 $resql = $this->db->query($sql);
390 if ($obj = $this->db->fetch_object($resql)) {
391 if (!(round(abs($obj->deb), 2) === round(abs($obj->cred), 2))) {
392 $this->errors[] =
'Total not exacts ' . round(abs($obj->deb), 2) .
' vs ' . round(abs($obj->cred), 2);
396 $this->db->free($resql);
398 $this->errors[] =
'Erreur sql' . $this->db->lasterror();
405 $sql =
"UPDATE " . MAIN_DB_PREFIX .
"accounting_bookkeeping SET";
406 $sql .=
" lettering_code='" . $this->db->escape($letter) .
"'";
407 $sql .=
", date_lettering = '" . $this->db->idate($now) .
"'";
408 $sql .=
" WHERE rowid IN (" . $this->db->sanitize(implode(
',', $ids)) .
") AND lettering_code IS NULL AND subledger_account != ''";
410 dol_syslog(__METHOD__ .
" - Update lettering code", LOG_DEBUG);
411 $resql = $this->db->query($sql);
414 $this->errors[] =
"Error " . $this->db->lasterror();
416 $affected_rows = $this->db->affected_rows($resql);
423 $this->db->rollback();
424 foreach ($this->errors as $errmsg) {
425 dol_syslog(get_class($this) .
"::update " . $errmsg, LOG_ERR);
426 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
431 return $affected_rows;
512 $this->errors = array();
515 $bookkeeping_ids = is_array($bookkeeping_ids) ? $bookkeeping_ids : array();
520 if (!is_array($grouped_lines)) {
524 foreach ($grouped_lines as $lines) {
527 $do_it = !$unlettering;
528 $lettering_code =
null;
529 $piece_num_lines = array();
530 $bookkeeping_lines = array();
531 foreach ($lines as $line_infos) {
532 $bookkeeping_lines[$line_infos[
'id']] = $line_infos[
'id'];
533 $piece_num_lines[$line_infos[
'piece_num']] = $line_infos[
'piece_num'];
534 $total += ($line_infos[
'credit'] > 0 ? $line_infos[
'credit'] : -$line_infos[
'debit']);
538 if (isset($lettering_code) && $lettering_code != $line_infos[
'lettering_code']) {
539 $this->errors[] = $langs->trans(
'AccountancyErrorMismatchLetteringCode');
543 if (!isset($lettering_code)) {
544 $lettering_code = (string) $line_infos[
'lettering_code'];
546 if (!empty($line_infos[
'lettering_code'])) {
549 } elseif (!empty($line_infos[
'lettering_code'])) {
555 if (!$group_error && !$unlettering &&
price2num($total) != 0) {
556 $this->errors[] = $langs->trans(
'AccountancyErrorMismatchBalanceAmount', $total);
561 if (!$group_error && $do_it) {
569 } elseif ($result > 0) {
575 $this->errors[] = $langs->trans(
'AccountancyErrorLetteringBookkeeping', implode(
', ', $piece_num_lines));
581 return -2 - $nb_lettering;
583 return $nb_lettering;
594 public function getLinkedLines($bookkeeping_ids, $only_has_subledger_account =
true)
596 global $conf, $langs;
597 $this->errors = array();
600 $bookkeeping_ids = is_array($bookkeeping_ids) ? $bookkeeping_ids : array();
603 $sql =
"SELECT DISTINCT ab.doc_type, ab.fk_doc";
604 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
605 $sql .=
" WHERE ab.entity IN (" .
getEntity(
'accountancy') .
")";
606 $sql .=
" AND ab.fk_doc > 0";
607 if (!empty($bookkeeping_ids)) {
609 $sql .=
" AND EXISTS (";
610 $sql .=
" SELECT rowid";
611 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS pn";
612 $sql .=
" WHERE pn.entity IN (" .
getEntity(
'accountancy') .
")";
613 $sql .=
" AND pn.rowid IN (" . $this->db->sanitize(implode(
',', $bookkeeping_ids)) .
")";
614 $sql .=
" AND pn.piece_num = ab.piece_num";
617 if ($only_has_subledger_account) {
618 $sql .=
" AND ab.subledger_account != ''";
621 dol_syslog(__METHOD__ .
" - Get all bookkeeping lines", LOG_DEBUG);
622 $resql = $this->db->query($sql);
624 $this->errors[] =
"Error " . $this->db->lasterror();
628 $bookkeeping_lines_by_type = array();
629 while ($obj = $this->db->fetch_object($resql)) {
630 $bookkeeping_lines_by_type[$obj->doc_type][$obj->fk_doc] = $obj->fk_doc;
632 $this->db->free($resql);
634 if (empty($bookkeeping_lines_by_type)) {
638 if (!empty($bookkeeping_lines_by_type[
'bank'])) {
640 if (!is_array($new_bookkeeping_lines_by_type)) {
643 foreach ($new_bookkeeping_lines_by_type as $doc_type => $fk_docs) {
644 foreach ($fk_docs as $fk_doc) {
645 $bookkeeping_lines_by_type[$doc_type][$fk_doc] = $fk_doc;
650 $grouped_lines = array();
651 foreach (self::$doc_type_infos as $doc_type => $doc_type_info) {
652 if (empty($bookkeeping_lines_by_type[$doc_type]) || !is_array($bookkeeping_lines_by_type[$doc_type])) {
658 if (!is_array($doc_grouped)) {
663 foreach ($doc_grouped as $doc_ids) {
665 if (!is_array($bank_ids)) {
670 $sql =
"SELECT DISTINCT ab.rowid, ab.piece_num, ab.debit, ab.credit, ab.lettering_code";
671 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
672 $sql .=
" WHERE ab.entity IN (" .
getEntity(
'accountancy') .
")";
674 if (!empty($bank_ids)) {
676 $sql .=
" SELECT bpn.rowid";
677 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS bpn";
678 $sql .=
" WHERE bpn.entity IN (" .
getEntity(
'accountancy') .
")";
679 $sql .=
" AND bpn.doc_type = 'bank'";
680 $sql .=
" AND bpn.fk_doc IN (" . $this->db->sanitize(implode(
',', $bank_ids)) .
")";
681 $sql .=
" AND bpn.piece_num = ab.piece_num";
685 $sql .=
" SELECT dpn.rowid";
686 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS dpn";
687 $sql .=
" WHERE dpn.entity IN (" .
getEntity(
'accountancy') .
")";
688 $sql .=
" AND dpn.doc_type = '" . $this->db->escape($doc_type) .
"'";
689 $sql .=
" AND dpn.fk_doc IN (" . $this->db->sanitize(implode(
',', $doc_ids)) .
")";
690 $sql .=
" AND dpn.piece_num = ab.piece_num";
693 if ($only_has_subledger_account) {
694 $sql .=
" AND ab.subledger_account != ''";
697 dol_syslog(__METHOD__ .
" - Get all bookkeeping lines linked", LOG_DEBUG);
698 $resql = $this->db->query($sql);
700 $this->errors[] =
"Error " . $this->db->lasterror();
705 while ($obj = $this->db->fetch_object($resql)) {
706 $group[$obj->rowid] = array(
708 'piece_num' => $obj->piece_num,
709 'debit' => $obj->debit,
710 'credit' => $obj->credit,
711 'lettering_code' => $obj->lettering_code,
714 $this->db->free($resql);
716 if (!empty($group)) {
717 $grouped_lines[] = $group;
722 return $grouped_lines;
733 dol_syslog(__METHOD__ .
" - bank_ids=".json_encode($bank_ids), LOG_DEBUG);
736 $bank_ids = is_array($bank_ids) ? $bank_ids : array();
738 if (empty($bank_ids)) {
742 $bookkeeping_lines_by_type = array();
743 foreach (self::$doc_type_infos as $doc_type => $doc_type_info) {
745 $sql =
"SELECT DISTINCT dp." . $this->db->sanitize($doc_type_info[
'doc_payment_table_fk_doc']) .
" AS fk_doc";
746 $sql .=
" FROM " . MAIN_DB_PREFIX . $this->db->sanitize($doc_type_info[
'payment_table']) .
" AS p";
747 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX . $this->db->sanitize($doc_type_info[
'doc_payment_table']) .
" AS dp ON dp." . $this->db->sanitize($doc_type_info[
'doc_payment_table_fk_payment']) .
" = p.rowid";
748 $sql .=
" WHERE p." . $this->db->sanitize($doc_type_info[
'payment_table_fk_bank']) .
" IN (" . $this->db->sanitize(implode(
',', $bank_ids)) .
")";
749 $sql .=
" AND dp." . $this->db->sanitize($doc_type_info[
'doc_payment_table_fk_doc']) .
" > 0";
751 dol_syslog(__METHOD__ .
" - Get all fk_doc by doc_type from list of bank ids for '" . $doc_type .
"'", LOG_DEBUG);
752 $resql = $this->db->query($sql);
754 $this->errors[] =
"Error " . $this->db->lasterror();
758 while ($obj = $this->db->fetch_object($resql)) {
759 $bookkeeping_lines_by_type[$doc_type][$obj->fk_doc] = $obj->fk_doc;
761 $this->db->free($resql);
764 return $bookkeeping_lines_by_type;
778 dol_syslog(__METHOD__ .
" - bank_ids=".json_encode($document_ids) .
", doc_type=$doc_type", LOG_DEBUG);
781 $document_ids = is_array($document_ids) ? $document_ids : array();
783 $document_ids = array_filter($document_ids);
785 $doc_type = trim($doc_type);
787 if (empty($document_ids)) {
790 if (!is_array(self::$doc_type_infos[$doc_type])) {
791 $langs->load(
'errors');
792 $this->errors[] = $langs->trans(
'ErrorBadParameters');
796 $doc_type_info = self::$doc_type_infos[$doc_type];
800 $sql =
"SELECT DISTINCT p." . $this->db->sanitize($doc_type_info[
'payment_table_fk_bank']) .
" AS fk_doc";
801 $sql .=
" FROM " . MAIN_DB_PREFIX . $this->db->sanitize($doc_type_info[
'payment_table']) .
" AS p";
802 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX . $this->db->sanitize($doc_type_info[
'doc_payment_table']) .
" AS dp ON dp." . $this->db->sanitize($doc_type_info[
'doc_payment_table_fk_payment']) .
" = p.rowid";
804 $sql .=
" WHERE dp." . $this->db->sanitize($doc_type_info[
'doc_payment_table_fk_doc']) .
" IN (" . $this->db->sanitize(implode(
',', $document_ids)) .
")";
805 $sql .=
" AND p." . $this->db->sanitize($doc_type_info[
'payment_table_fk_bank']) .
" > 0";
807 dol_syslog(__METHOD__ .
" - Get all bank ids from list of document ids of a type '" . $doc_type .
"'", LOG_DEBUG);
808 $resql = $this->db->query($sql);
810 $this->errors[] =
"Error " . $this->db->lasterror();
814 while ($obj = $this->db->fetch_object($resql)) {
815 $bank_ids[$obj->fk_doc] = $obj->fk_doc;
817 $this->db->free($resql);
834 $document_ids = is_array($document_ids) ? $document_ids : array();
835 $doc_type = trim($doc_type);
837 $document_ids = array_filter($document_ids);
839 if (empty($document_ids)) {
843 if (!is_array(self::$doc_type_infos[$doc_type])) {
844 $langs->load(
'errors');
845 $this->errors[] = $langs->trans(
'ErrorBadParameters');
849 $doc_type_info = self::$doc_type_infos[$doc_type];
852 $current_document_ids = array();
853 $link_by_element = array();
854 $element_by_link = array();
855 foreach ($doc_type_info[
'linked_info'] as $linked_info) {
856 if (empty($linked_info[
'fk_line_link'])) {
857 $sql =
"SELECT DISTINCT tl2.".$this->db->sanitize($linked_info[
'fk_link']).
" AS fk_link, tl2.".$this->db->sanitize($linked_info[
'fk_doc']).
" AS fk_doc";
858 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->db->sanitize($linked_info[
'table']).
" AS tl";
859 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$this->db->sanitize($linked_info[
'table']).
" AS tl2 ON tl2.".$this->db->sanitize($linked_info[
'fk_link']).
" = tl.".$this->db->sanitize($linked_info[
'fk_link']);
860 $sql .=
" WHERE tl.".$this->db->sanitize($linked_info[
'fk_doc']).
" IN (".$this->db->sanitize(implode(
',', $document_ids)).
")";
862 $sql =
"SELECT DISTINCT tl2.fk_link, tl2.fk_doc";
865 $sql .=
" SELECT DISTINCT " . $this->db->ifsql(
"tll.".$this->db->sanitize($linked_info[
'fk_table_link_line_parent']).
" IS NOT NULL",
"tll.".$this->db->sanitize($linked_info[
'fk_table_link_line_parent']),
"tl.".$this->db->sanitize($linked_info[
'fk_link'])).
" AS fk_link, tl.".$this->db->sanitize($linked_info[
'fk_doc']).
" AS fk_doc";
866 $sql .=
" FROM " . MAIN_DB_PREFIX .$this->db->sanitize($linked_info[
'table']).
" AS tl";
868 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX . $this->db->sanitize($linked_info[
'table_link_line']) .
" AS tll ON tll.".$this->db->sanitize($linked_info[
'fk_table_link_line']) .
" = tl.".$this->db->sanitize($linked_info[
'fk_line_link']);
870 $sql .=
" LEFT JOIN (";
872 $sql .=
" SELECT DISTINCT " . $this->db->ifsql(
"tll.".$this->db->sanitize($linked_info[
'fk_table_link_line_parent']).
" IS NOT NULL",
"tll.".$this->db->sanitize($linked_info[
'fk_table_link_line_parent']),
"tl.".$this->db->sanitize($linked_info[
'fk_link'])).
" AS fk_link, tl.".$this->db->sanitize($linked_info[
'fk_doc']).
" AS fk_doc";
873 $sql .=
" FROM " . MAIN_DB_PREFIX .$this->db->sanitize($linked_info[
'table']).
" AS tl";
875 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX . $this->db->sanitize($linked_info[
'table_link_line']) .
" AS tll ON tll.".$this->db->sanitize($linked_info[
'fk_table_link_line']) .
" = tl.".$this->db->sanitize($linked_info[
'fk_line_link']);
876 $sql .=
") AS tl2 ON tl2.fk_link = tl.fk_link";
877 $sql .=
" WHERE tl.fk_doc IN (" . $this->db->sanitize(implode(
',', $document_ids)) .
")";
878 $sql .=
" AND tl2.fk_doc IS NOT NULL";
881 dol_syslog(__METHOD__ .
" - Get document lines", LOG_DEBUG);
882 $resql = $this->db->query($sql);
884 $this->errors[] =
"Error " . $this->db->lasterror();
888 $is_fk_link_is_also_fk_doc = !empty($linked_info[
'is_fk_link_is_also_fk_doc']);
889 while ($obj = $this->db->fetch_object($resql)) {
890 $current_document_ids[$obj->fk_doc] = $obj->fk_doc;
892 $link_key = (string) $linked_info[
'prefix'] . (
string) $obj->fk_link;
893 $element_by_link[$link_key][$obj->fk_doc] = $obj->fk_doc;
894 $link_by_element[(int) $obj->fk_doc][$link_key] = $link_key;
895 if ($is_fk_link_is_also_fk_doc) {
896 $element_by_link[$link_key][$obj->fk_link] = $obj->fk_link;
897 $link_by_element[(int) $obj->fk_link][$link_key] = $link_key;
900 $this->db->free($resql);
903 if (count(array_diff($document_ids, $current_document_ids))) {
919 public function getGroupElements(&$link_by_element, &$element_by_link, $link_key =
'', &$current_group = array())
921 $grouped_elements = array();
922 if (!empty($link_key) && !isset($element_by_link[$link_key])) {
924 return $grouped_elements;
928 if (empty($link_key)) {
930 $save_link_by_element = $link_by_element;
931 $save_element_by_link = $element_by_link;
934 $save_link_by_element =
null;
935 $save_element_by_link =
null;
940 $current_link_key = !empty($link_key) ? $link_key : array_keys($element_by_link)[0];
941 $element_ids = $element_by_link[$current_link_key];
942 unset($element_by_link[$current_link_key]);
944 foreach ($element_ids as $element_id) {
946 if (!isset($link_by_element[$element_id])) {
951 $current_group[$element_id] = $element_id;
954 $link_keys = $link_by_element[$element_id];
955 unset($link_by_element[$element_id]);
958 foreach ($link_keys as $key) {
959 $this->
getGroupElements($link_by_element, $element_by_link, (
string) $key, $current_group);
963 if (empty($link_key)) {
965 $grouped_elements[] = $current_group;
966 $current_group = array();
968 }
while (!empty($element_by_link) && empty($link_key));
970 if (empty($link_key)) {
972 $link_by_element = $save_link_by_element;
973 $element_by_link = $save_element_by_link;
976 return $grouped_elements;