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);
308 $letter = str_pad(
"",
getDolGlobalInt(
'ACCOUNTING_LETTERING_NBLETTERS', 3), $partial ?
'a' :
'A');
313 $sql =
"SELECT ab.lettering_code, GROUP_CONCAT(DISTINCT ab.rowid SEPARATOR ',') AS bookkeeping_ids";
314 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
315 $sql .=
" WHERE ab.rowid IN (" . $this->db->sanitize(implode(
',', $ids)) .
")";
316 $sql .=
" GROUP BY ab.lettering_code";
317 $sql .=
" ORDER BY ab.lettering_code DESC";
319 dol_syslog(__METHOD__ .
" - Check partial / normal lettering case", LOG_DEBUG);
320 $resql = $this->db->query($sql);
322 while ($obj = $this->db->fetch_object($resql)) {
323 if (empty($obj->lettering_code)) {
328 if ($partial && preg_match(
'/^[A-Z]+$/', $obj->lettering_code)) {
329 if (!empty($obj->bookkeeping_ids)) {
330 $ids = array_diff($ids, explode(
',', $obj->bookkeeping_ids));
332 } elseif (!$partial && preg_match(
'/^[a-z]+$/', $obj->lettering_code)) {
334 $sql2 =
"UPDATE " . MAIN_DB_PREFIX .
"accounting_bookkeeping SET";
335 $sql2 .=
" lettering_code = NULL";
336 $sql2 .=
", date_lettering = NULL";
337 $sql2 .=
" WHERE entity IN (" .
getEntity(
'accountancy') .
")";
338 $sql2 .=
" AND lettering_code = '" . $this->db->escape($obj->lettering_code) .
"'";
340 dol_syslog(__METHOD__ .
" - Remove partial lettering", LOG_DEBUG);
341 $resql2 = $this->db->query($sql2);
343 $this->errors[] =
'Error' . $this->db->lasterror();
349 $this->db->free($resql);
351 $this->errors[] =
'Error' . $this->db->lasterror();
355 if (!$error && !empty($ids)) {
357 $sql =
"SELECT DISTINCT ab2.lettering_code";
358 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
359 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab2 ON ab2.subledger_account = ab.subledger_account";
360 $sql .=
" WHERE ab.rowid IN (" . $this->db->sanitize(implode(
',', $ids)) .
")";
361 $sql .=
" AND ab2.lettering_code != ''";
362 $sql .=
" ORDER BY ab2.lettering_code DESC";
364 dol_syslog(__METHOD__ .
" - Get next code", LOG_DEBUG);
365 $resql = $this->db->query($sql);
367 while ($obj = $this->db->fetch_object($resql)) {
368 if (!empty($obj->lettering_code) &&
369 (($partial && preg_match(
'/^[a-z]+$/', $obj->lettering_code)) ||
370 (!$partial && preg_match(
'/^[A-Z]+$/', $obj->lettering_code)))
372 $letter = $obj->lettering_code;
377 $this->db->free($resql);
379 $this->errors[] =
'Error' . $this->db->lasterror();
384 if (!$error && !$partial) {
385 $sql =
"SELECT SUM(ABS(debit)) as deb, SUM(ABS(credit)) as cred FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping WHERE ";
386 $sql .=
" rowid IN (" . $this->db->sanitize(implode(
',', $ids)) .
") AND lettering_code IS NULL AND subledger_account != ''";
388 dol_syslog(__METHOD__ .
" - Test amount integrity", LOG_DEBUG);
389 $resql = $this->db->query($sql);
391 if ($obj = $this->db->fetch_object($resql)) {
392 if (!(round(abs($obj->deb), 2) === round(abs($obj->cred), 2))) {
393 $this->errors[] =
'Total not exacts ' . round(abs($obj->deb), 2) .
' vs ' . round(abs($obj->cred), 2);
397 $this->db->free($resql);
399 $this->errors[] =
'Erreur sql' . $this->db->lasterror();
406 $sql =
"UPDATE " . MAIN_DB_PREFIX .
"accounting_bookkeeping SET";
407 $sql .=
" lettering_code='" . $this->db->escape($letter) .
"'";
408 $sql .=
", date_lettering = '" . $this->db->idate($now) .
"'";
409 $sql .=
" WHERE rowid IN (" . $this->db->sanitize(implode(
',', $ids)) .
") AND lettering_code IS NULL AND subledger_account != ''";
411 dol_syslog(__METHOD__ .
" - Update lettering code", LOG_DEBUG);
412 $resql = $this->db->query($sql);
415 $this->errors[] =
"Error " . $this->db->lasterror();
417 $affected_rows = $this->db->affected_rows($resql);
424 $this->db->rollback();
425 foreach ($this->errors as $errmsg) {
426 dol_syslog(get_class($this) .
"::update " . $errmsg, LOG_ERR);
427 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
432 return $affected_rows;
514 $this->errors = array();
517 $bookkeeping_ids = is_array($bookkeeping_ids) ? $bookkeeping_ids : array();
522 if (!is_array($grouped_lines)) {
526 foreach ($grouped_lines as $lines) {
529 $do_it = !$unlettering;
530 $lettering_code =
null;
531 $piece_num_lines = array();
532 $bookkeeping_lines = array();
533 foreach ($lines as $line_infos) {
534 $bookkeeping_lines[$line_infos[
'id']] = $line_infos[
'id'];
535 $piece_num_lines[$line_infos[
'piece_num']] = $line_infos[
'piece_num'];
536 $total += ($line_infos[
'credit'] > 0 ? $line_infos[
'credit'] : -$line_infos[
'debit']);
540 if (isset($lettering_code) && $lettering_code != $line_infos[
'lettering_code']) {
541 $this->errors[] = $langs->trans(
'AccountancyErrorMismatchLetteringCode');
545 if (!isset($lettering_code)) {
546 $lettering_code = (string) $line_infos[
'lettering_code'];
548 if (!empty($line_infos[
'lettering_code'])) {
551 } elseif (!empty($line_infos[
'lettering_code'])) {
557 if (!$group_error && !$unlettering &&
price2num($total) != 0) {
558 $this->errors[] = $langs->trans(
'AccountancyErrorMismatchBalanceAmount', $total);
563 if (!$group_error && $do_it) {
571 } elseif ($result > 0) {
577 $this->errors[] = $langs->trans(
'AccountancyErrorLetteringBookkeeping', implode(
', ', $piece_num_lines));
583 return -2 - $nb_lettering;
585 return $nb_lettering;
596 public function getLinkedLines($bookkeeping_ids, $only_has_subledger_account =
true)
598 global $conf, $langs;
599 $this->errors = array();
602 $bookkeeping_ids = is_array($bookkeeping_ids) ? $bookkeeping_ids : array();
605 $sql =
"SELECT DISTINCT ab.doc_type, ab.fk_doc";
606 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
607 $sql .=
" WHERE ab.entity IN (" .
getEntity(
'accountancy') .
")";
608 $sql .=
" AND ab.fk_doc > 0";
609 if (!empty($bookkeeping_ids)) {
611 $sql .=
" AND EXISTS (";
612 $sql .=
" SELECT rowid";
613 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS pn";
614 $sql .=
" WHERE pn.entity IN (" .
getEntity(
'accountancy') .
")";
615 $sql .=
" AND pn.rowid IN (" . $this->db->sanitize(implode(
',', $bookkeeping_ids)) .
")";
616 $sql .=
" AND pn.piece_num = ab.piece_num";
619 if ($only_has_subledger_account) {
620 $sql .=
" AND ab.subledger_account != ''";
623 dol_syslog(__METHOD__ .
" - Get all bookkeeping lines", LOG_DEBUG);
624 $resql = $this->db->query($sql);
626 $this->errors[] =
"Error " . $this->db->lasterror();
630 $bookkeeping_lines_by_type = array();
631 while ($obj = $this->db->fetch_object($resql)) {
632 $bookkeeping_lines_by_type[$obj->doc_type][$obj->fk_doc] = $obj->fk_doc;
634 $this->db->free($resql);
636 if (empty($bookkeeping_lines_by_type)) {
640 if (!empty($bookkeeping_lines_by_type[
'bank'])) {
642 if (!is_array($new_bookkeeping_lines_by_type)) {
645 foreach ($new_bookkeeping_lines_by_type as $doc_type => $fk_docs) {
646 foreach ($fk_docs as $fk_doc) {
647 $bookkeeping_lines_by_type[$doc_type][$fk_doc] = $fk_doc;
652 $grouped_lines = array();
653 foreach (self::$doc_type_infos as $doc_type => $doc_type_info) {
654 if (!is_array($bookkeeping_lines_by_type[$doc_type])) {
660 if (!is_array($doc_grouped)) {
665 foreach ($doc_grouped as $doc_ids) {
667 if (!is_array($bank_ids)) {
672 $sql =
"SELECT DISTINCT ab.rowid, ab.piece_num, ab.debit, ab.credit, ab.lettering_code";
673 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
674 $sql .=
" WHERE ab.entity IN (" .
getEntity(
'accountancy') .
")";
676 if (!empty($bank_ids)) {
678 $sql .=
" SELECT bpn.rowid";
679 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS bpn";
680 $sql .=
" WHERE bpn.entity IN (" .
getEntity(
'accountancy') .
")";
681 $sql .=
" AND bpn.doc_type = 'bank'";
682 $sql .=
" AND bpn.fk_doc IN (" . $this->db->sanitize(implode(
',', $bank_ids)) .
")";
683 $sql .=
" AND bpn.piece_num = ab.piece_num";
687 $sql .=
" SELECT dpn.rowid";
688 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS dpn";
689 $sql .=
" WHERE dpn.entity IN (" .
getEntity(
'accountancy') .
")";
690 $sql .=
" AND dpn.doc_type = '" . $this->db->escape($doc_type) .
"'";
691 $sql .=
" AND dpn.fk_doc IN (" . $this->db->sanitize(implode(
',', $doc_ids)) .
")";
692 $sql .=
" AND dpn.piece_num = ab.piece_num";
695 if ($only_has_subledger_account) {
696 $sql .=
" AND ab.subledger_account != ''";
699 dol_syslog(__METHOD__ .
" - Get all bookkeeping lines linked", LOG_DEBUG);
700 $resql = $this->db->query($sql);
702 $this->errors[] =
"Error " . $this->db->lasterror();
707 while ($obj = $this->db->fetch_object($resql)) {
708 $group[$obj->rowid] = array(
710 'piece_num' => $obj->piece_num,
711 'debit' => $obj->debit,
712 'credit' => $obj->credit,
713 'lettering_code' => $obj->lettering_code,
716 $this->db->free($resql);
718 if (!empty($group)) {
719 $grouped_lines[] = $group;
724 return $grouped_lines;
735 dol_syslog(__METHOD__ .
" - bank_ids=".json_encode($bank_ids), LOG_DEBUG);
738 $bank_ids = is_array($bank_ids) ? $bank_ids : array();
740 if (empty($bank_ids)) {
744 $bookkeeping_lines_by_type = array();
745 foreach (self::$doc_type_infos as $doc_type => $doc_type_info) {
747 $sql =
"SELECT DISTINCT dp." . $this->db->sanitize($doc_type_info[
'doc_payment_table_fk_doc']) .
" AS fk_doc";
748 $sql .=
" FROM " . MAIN_DB_PREFIX . $this->db->sanitize($doc_type_info[
'payment_table']) .
" AS p";
749 $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";
750 $sql .=
" WHERE p." . $this->db->sanitize($doc_type_info[
'payment_table_fk_bank']) .
" IN (" . $this->db->sanitize(implode(
',', $bank_ids)) .
")";
751 $sql .=
" AND dp." . $this->db->sanitize($doc_type_info[
'doc_payment_table_fk_doc']) .
" > 0";
753 dol_syslog(__METHOD__ .
" - Get all fk_doc by doc_type from list of bank ids for '" . $doc_type .
"'", LOG_DEBUG);
754 $resql = $this->db->query($sql);
756 $this->errors[] =
"Error " . $this->db->lasterror();
760 while ($obj = $this->db->fetch_object($resql)) {
761 $bookkeeping_lines_by_type[$doc_type][$obj->fk_doc] = $obj->fk_doc;
763 $this->db->free($resql);
766 return $bookkeeping_lines_by_type;
780 dol_syslog(__METHOD__ .
" - bank_ids=".json_encode($document_ids) .
", doc_type=$doc_type", LOG_DEBUG);
783 $document_ids = is_array($document_ids) ? $document_ids : array();
785 $document_ids = array_filter($document_ids);
787 $doc_type = trim($doc_type);
789 if (empty($document_ids)) {
792 if (!is_array(self::$doc_type_infos[$doc_type])) {
793 $langs->load(
'errors');
794 $this->errors[] = $langs->trans(
'ErrorBadParameters');
798 $doc_type_info = self::$doc_type_infos[$doc_type];
802 $sql =
"SELECT DISTINCT p." . $this->db->sanitize($doc_type_info[
'payment_table_fk_bank']) .
" AS fk_doc";
803 $sql .=
" FROM " . MAIN_DB_PREFIX . $this->db->sanitize($doc_type_info[
'payment_table']) .
" AS p";
804 $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";
806 $sql .=
" WHERE dp." . $this->db->sanitize($doc_type_info[
'doc_payment_table_fk_doc']) .
" IN (" . $this->db->sanitize(implode(
',', $document_ids)) .
")";
807 $sql .=
" AND p." . $this->db->sanitize($doc_type_info[
'payment_table_fk_bank']) .
" > 0";
809 dol_syslog(__METHOD__ .
" - Get all bank ids from list of document ids of a type '" . $doc_type .
"'", LOG_DEBUG);
810 $resql = $this->db->query($sql);
812 $this->errors[] =
"Error " . $this->db->lasterror();
816 while ($obj = $this->db->fetch_object($resql)) {
817 $bank_ids[$obj->fk_doc] = $obj->fk_doc;
819 $this->db->free($resql);
836 $document_ids = is_array($document_ids) ? $document_ids : array();
837 $doc_type = trim($doc_type);
839 $document_ids = array_filter($document_ids);
841 if (empty($document_ids)) {
845 if (!is_array(self::$doc_type_infos[$doc_type])) {
846 $langs->load(
'errors');
847 $this->errors[] = $langs->trans(
'ErrorBadParameters');
851 $doc_type_info = self::$doc_type_infos[$doc_type];
854 $current_document_ids = array();
855 $link_by_element = array();
856 $element_by_link = array();
857 foreach ($doc_type_info[
'linked_info'] as $linked_info) {
858 if (empty($linked_info[
'fk_line_link'])) {
859 $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";
860 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->db->sanitize($linked_info[
'table']).
" AS tl";
861 $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']);
862 $sql .=
" WHERE tl.".$this->db->sanitize($linked_info[
'fk_doc']).
" IN (".$this->db->sanitize(implode(
',', $document_ids)).
")";
864 $sql =
"SELECT DISTINCT tl2.fk_link, tl2.fk_doc";
867 $sql .=
" SELECT DISTINCT " . $this->db->ifsql(
"tll.".$this->db->sanitize($linked_info[
'fk_table_link_line_parent']),
"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";
868 $sql .=
" FROM " . MAIN_DB_PREFIX .$this->db->sanitize($linked_info[
'table']).
" AS tl";
870 $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']);
872 $sql .=
" LEFT JOIN (";
874 $sql .=
" SELECT DISTINCT " . $this->db->ifsql(
"tll.".$this->db->sanitize($linked_info[
'fk_table_link_line_parent']),
"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";
875 $sql .=
" FROM " . MAIN_DB_PREFIX .$this->db->sanitize($linked_info[
'table']).
" AS tl";
877 $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']);
878 $sql .=
") AS tl2 ON tl2.fk_link = tl.fk_link";
879 $sql .=
" WHERE tl.fk_doc IN (" . $this->db->sanitize(implode(
',', $document_ids)) .
")";
880 $sql .=
" AND tl2.fk_doc IS NOT NULL";
883 dol_syslog(__METHOD__ .
" - Get document lines", LOG_DEBUG);
884 $resql = $this->db->query($sql);
886 $this->errors[] =
"Error " . $this->db->lasterror();
890 $is_fk_link_is_also_fk_doc = !empty($linked_info[
'is_fk_link_is_also_fk_doc']);
891 while ($obj = $this->db->fetch_object($resql)) {
892 $current_document_ids[$obj->fk_doc] = $obj->fk_doc;
894 $link_key = $linked_info[
'prefix'] . $obj->fk_link;
895 $element_by_link[$link_key][$obj->fk_doc] = $obj->fk_doc;
896 $link_by_element[$obj->fk_doc][$link_key] = $link_key;
897 if ($is_fk_link_is_also_fk_doc) {
898 $element_by_link[$link_key][$obj->fk_link] = $obj->fk_link;
899 $link_by_element[$obj->fk_link][$link_key] = $link_key;
902 $this->db->free($resql);
905 if (count(array_diff($document_ids, $current_document_ids))) {
921 public function getGroupElements(&$link_by_element, &$element_by_link, $link_key =
'', &$current_group = array())
923 $grouped_elements = array();
924 if (!empty($link_key) && !isset($element_by_link[$link_key])) {
926 return $grouped_elements;
930 if (empty($link_key)) {
932 $save_link_by_element = $link_by_element;
933 $save_element_by_link = $element_by_link;
938 $current_link_key = !empty($link_key) ? $link_key : array_keys($element_by_link)[0];
939 $element_ids = $element_by_link[$current_link_key];
940 unset($element_by_link[$current_link_key]);
942 foreach ($element_ids as $element_id) {
944 if (!isset($link_by_element[$element_id])) {
949 $current_group[$element_id] = $element_id;
952 $link_keys = $link_by_element[$element_id];
953 unset($link_by_element[$element_id]);
956 foreach ($link_keys as $key) {
957 $this->
getGroupElements($link_by_element, $element_by_link, $key, $current_group);
961 if (empty($link_key)) {
963 $grouped_elements[] = $current_group;
964 $current_group = array();
966 }
while (!empty($element_by_link) && empty($link_key));
968 if (empty($link_key)) {
970 $link_by_element = $save_link_by_element;
971 $element_by_link = $save_element_by_link;
974 return $grouped_elements;