103 $object =
new Societe($this->db);
104 $object->id = $socid;
105 $object->fetch($socid);
108 if ($object->code_compta ==
'411CUSTCODE') {
109 $object->code_compta =
'';
112 if ($object->code_compta_fournisseur ==
'401SUPPCODE') {
113 $object->code_compta_fournisseur =
'';
119 $sql =
"SELECT DISTINCT bk.rowid, bk.doc_date, bk.doc_type, bk.doc_ref, bk.subledger_account, ";
120 $sql .=
" bk.numero_compte , bk.label_compte, bk.debit , bk.credit, bk.montant ";
121 $sql .=
" , bk.sens , bk.code_journal , bk.piece_num, bk.date_lettering, bu.url_id , bu.type ";
122 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as bk";
123 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"bank_url as bu ON(bk.fk_doc = bu.fk_bank AND bu.type IN ('payment', 'payment_supplier') ) ";
125 if ($object->code_compta !=
"") {
126 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta).
"' ";
128 if ($object->code_compta !=
"" && $object->code_compta_fournisseur !=
"") {
131 if ($object->code_compta_fournisseur !=
"") {
132 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur).
"' ";
135 $sql .=
" ) AND (bk.date_lettering ='' OR bk.date_lettering IS NULL) ";
136 $sql .=
" AND (bk.lettering_code != '' OR bk.lettering_code IS NULL) ";
137 $sql .=
' AND bk.date_validated IS NULL ';
138 $sql .= $this->db->order(
'bk.doc_date',
'DESC');
142 $resql = $this->db->query($sql);
144 $num = $this->db->num_rows($resql);
146 while ($obj = $this->db->fetch_object($resql)) {
150 if ($obj->type ==
'payment_supplier') {
151 $sql =
'SELECT DISTINCT bk.rowid, facf.ref, facf.ref_supplier, payf.fk_bank, facf.rowid as fact_id';
152 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn facf ";
153 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid";
154 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid";
155 $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).
"')";
156 $sql .=
" WHERE payfacf.fk_paiementfourn = '".$this->db->escape($obj->url_id).
"' ";
157 $sql .=
" AND facf.entity = ".$conf->entity;
158 $sql .=
" AND code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX.
"accounting_journal WHERE nature=4 AND entity=".$conf->entity.
") ";
160 if ($object->code_compta !=
"") {
161 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta).
"' ";
163 if ($object->code_compta !=
"" && $object->code_compta_fournisseur !=
"") {
166 if ($object->code_compta_fournisseur !=
"") {
167 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur).
"' ";
171 $resql2 = $this->db->query($sql);
173 while ($obj2 = $this->db->fetch_object($resql2)) {
174 $ids[$obj2->rowid] = $obj2->rowid;
175 $ids_fact[] = $obj2->fact_id;
177 $this->db->free($resql2);
179 $this->errors[] = $this->db->lasterror;
182 if (count($ids_fact)) {
183 $sql =
'SELECT bk.rowid, facf.ref, facf.ref_supplier ';
184 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn facf ";
185 $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)).
"))";
186 $sql .=
" WHERE bk.code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX.
"accounting_journal WHERE nature=3 AND entity=".$conf->entity.
") ";
187 $sql .=
" AND facf.entity = ".$conf->entity;
189 if ($object->code_compta !=
"") {
190 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta).
"' ";
192 if ($object->code_compta !=
"" && $object->code_compta_fournisseur !=
"") {
195 if ($object->code_compta_fournisseur !=
"") {
196 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur).
"' ";
200 $resql2 = $this->db->query($sql);
202 while ($obj2 = $this->db->fetch_object($resql2)) {
203 $ids[$obj2->rowid] = $obj2->rowid;
205 $this->db->free($resql2);
207 $this->errors[] = $this->db->lasterror;
211 } elseif ($obj->type ==
'payment') {
212 $sql =
'SELECT DISTINCT bk.rowid, fac.ref, fac.ref, pay.fk_bank, fac.rowid as fact_id';
213 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture fac ";
214 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"paiement_facture as payfac ON payfac.fk_facture=fac.rowid";
215 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"paiement as pay ON payfac.fk_paiement=pay.rowid";
216 $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).
"')";
217 $sql .=
" WHERE payfac.fk_paiement = '".$this->db->escape($obj->url_id).
"' ";
218 $sql .=
" AND bk.code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX.
"accounting_journal WHERE nature=4 AND entity=".$conf->entity.
") ";
219 $sql .=
" AND fac.entity IN (".getEntity(
'invoice', 0).
")";
221 if ($object->code_compta !=
"") {
222 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta).
"' ";
224 if ($object->code_compta !=
"" && $object->code_compta_fournisseur !=
"") {
227 if ($object->code_compta_fournisseur !=
"") {
228 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur).
"' ";
232 $resql2 = $this->db->query($sql);
234 while ($obj2 = $this->db->fetch_object($resql2)) {
235 $ids[$obj2->rowid] = $obj2->rowid;
236 $ids_fact[] = $obj2->fact_id;
239 $this->errors[] = $this->db->lasterror;
242 if (count($ids_fact)) {
243 $sql =
'SELECT bk.rowid, fac.ref, fac.ref_supplier ';
244 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture fac ";
245 $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)).
"))";
246 $sql .=
" WHERE code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX.
"accounting_journal WHERE nature=2 AND entity=".$conf->entity.
") ";
247 $sql .=
" AND fac.entity IN (".getEntity(
'invoice', 0).
")";
249 if ($object->code_compta !=
"") {
250 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta).
"' ";
252 if ($object->code_compta !=
"" && $object->code_compta_fournisseur !=
"") {
255 if ($object->code_compta_fournisseur !=
"") {
256 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur).
"' ";
260 $resql2 = $this->db->query($sql);
262 while ($obj2 = $this->db->fetch_object($resql2)) {
263 $ids[$obj2->rowid] = $obj2->rowid;
265 $this->db->free($resql2);
267 $this->errors[] = $this->db->lasterror;
273 if (count($ids) > 1) {
277 $this->db->free($resql);
280 foreach ($this->errors as $errmsg) {
282 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
301 $sql =
"SELECT DISTINCT ab2.lettering_code";
302 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
303 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab2 ON ab2.subledger_account = ab.subledger_account";
304 $sql .=
" WHERE ab.rowid IN (" . $this->db->sanitize(implode(
',', $ids)) .
")";
305 $sql .=
" AND ab2.lettering_code != ''";
306 $sql .=
" ORDER BY ab2.lettering_code DESC";
309 $resqla = $this->db->query($sql);
311 $obj = $this->db->fetch_object($resqla);
312 $lettre = (empty($obj->lettering_code) ? $lettre : $obj->lettering_code);
313 if (!empty($obj->lettering_code)) {
316 $this->db->free($resqla);
318 $this->errors[] =
'Error'.$this->db->lasterror();
322 $sql =
"SELECT SUM(ABS(debit)) as deb, SUM(ABS(credit)) as cred FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping WHERE ";
323 $sql .=
" rowid IN (".$this->db->sanitize(implode(
',', $ids)).
") AND lettering_code IS NULL AND subledger_account != ''";
324 $resqlb = $this->db->query($sql);
326 $obj = $this->db->fetch_object($resqlb);
327 if (!(round(abs($obj->deb), 2) === round(abs($obj->cred), 2))) {
328 $this->errors[] =
'Total not exacts '.round(abs($obj->deb), 2).
' vs '.round(abs($obj->cred), 2);
331 $this->db->free($resqlb);
333 $this->errors[] =
'Erreur sql'.$this->db->lasterror();
343 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"accounting_bookkeeping SET";
344 $sql .=
" lettering_code='".$this->db->escape($lettre).
"'";
345 $sql .=
", date_lettering = '".$this->db->idate($now).
"'";
346 $sql .=
" WHERE rowid IN (".$this->db->sanitize(implode(
',', $ids)).
") AND lettering_code IS NULL AND subledger_account != ''";
348 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
349 $resql = $this->db->query($sql);
352 $this->errors[] =
"Error ".$this->db->lasterror();
354 $affected_rows = $this->db->affected_rows($resql);
360 foreach ($this->errors as $errmsg) {
361 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
362 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
366 return $affected_rows;
448 $this->errors = array();
451 $bookkeeping_ids = is_array($bookkeeping_ids) ? $bookkeeping_ids : array();
456 if (!is_array($grouped_lines)) {
460 foreach ($grouped_lines as $lines) {
463 $do_it = !$unlettering;
464 $lettering_code =
null;
465 $piece_num_lines = array();
466 $bookkeeping_lines = array();
467 foreach ($lines as $line_infos) {
468 $bookkeeping_lines[$line_infos[
'id']] = $line_infos[
'id'];
469 $piece_num_lines[$line_infos[
'piece_num']] = $line_infos[
'piece_num'];
470 $total += ($line_infos[
'credit'] > 0 ? $line_infos[
'credit'] : -$line_infos[
'debit']);
474 if (isset($lettering_code) && $lettering_code != $line_infos[
'lettering_code']) {
475 $this->errors[] = $langs->trans(
'AccountancyErrorMismatchLetteringCode');
479 if (!isset($lettering_code)) $lettering_code = (
string) $line_infos[
'lettering_code'];
480 if (!empty($line_infos[
'lettering_code'])) $do_it =
true;
481 } elseif (!empty($line_infos[
'lettering_code'])) $do_it =
false;
485 if (!$group_error && !$unlettering &&
price2num($total) != 0) {
486 $this->errors[] = $langs->trans(
'AccountancyErrorMismatchBalanceAmount', $total);
491 if (!$group_error && $do_it) {
492 if ($unlettering) $result = $this->
deleteLettering($bookkeeping_lines);
496 } elseif ($result > 0) {
502 $this->errors[] = $langs->trans(
'AccountancyErrorLetteringBookkeeping', implode(
', ', $piece_num_lines));
508 return -2 - $nb_lettering;
510 return $nb_lettering;
521 public function getLinkedLines($bookkeeping_ids, $only_has_subledger_account =
true)
523 global $conf, $langs;
524 $this->errors = array();
527 $bookkeeping_ids = is_array($bookkeeping_ids) ? $bookkeeping_ids : array();
530 $sql =
"SELECT DISTINCT ab.doc_type, ab.fk_doc";
531 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
532 $sql .=
" WHERE ab.entity IN (" .
getEntity(
'accountancy') .
")";
533 $sql .=
" AND ab.fk_doc > 0";
534 if (!empty($bookkeeping_ids)) {
536 $sql .=
" AND EXISTS (";
537 $sql .=
" SELECT rowid";
538 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS pn";
539 $sql .=
" WHERE pn.entity IN (" .
getEntity(
'accountancy') .
")";
540 $sql .=
" AND pn.rowid IN (" . $this->db->sanitize(implode(
',', $bookkeeping_ids)) .
")";
541 $sql .=
" AND pn.piece_num = ab.piece_num";
544 if ($only_has_subledger_account) $sql .=
" AND ab.subledger_account != ''";
546 dol_syslog(__METHOD__ .
" - Get all bookkeeping lines", LOG_DEBUG);
547 $resql = $this->db->query($sql);
549 $this->errors[] =
"Error " . $this->db->lasterror();
553 $bookkeeping_lines_by_type = array();
554 while ($obj = $this->db->fetch_object($resql)) {
555 $bookkeeping_lines_by_type[$obj->doc_type][$obj->fk_doc] = $obj->fk_doc;
557 $this->db->free($resql);
559 if (empty($bookkeeping_lines_by_type)) {
563 if (!empty($bookkeeping_lines_by_type[
'bank'])) {
565 if (!is_array($new_bookkeeping_lines_by_type)) {
568 foreach ($new_bookkeeping_lines_by_type as $doc_type => $fk_docs) {
569 foreach ($fk_docs as $fk_doc) {
570 $bookkeeping_lines_by_type[$doc_type][$fk_doc] = $fk_doc;
575 $grouped_lines = array();
576 foreach (self::$doc_type_infos as $doc_type => $doc_type_info) {
577 if (!is_array($bookkeeping_lines_by_type[$doc_type])) {
583 if (!is_array($doc_grouped)) {
588 foreach ($doc_grouped as $doc_ids) {
590 if (!is_array($bank_ids)) {
595 $sql =
"SELECT DISTINCT ab.rowid, ab.piece_num, ab.debit, ab.credit, ab.lettering_code";
596 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
597 $sql .=
" WHERE ab.entity IN (" .
getEntity(
'accountancy') .
")";
599 if (!empty($bank_ids)) {
601 $sql .=
" SELECT bpn.rowid";
602 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS bpn";
603 $sql .=
" WHERE bpn.entity IN (" .
getEntity(
'accountancy') .
")";
604 $sql .=
" AND bpn.doc_type = 'bank'";
605 $sql .=
" AND bpn.fk_doc IN (" . $this->db->sanitize(implode(
',', $bank_ids)) .
")";
606 $sql .=
" AND bpn.piece_num = ab.piece_num";
610 $sql .=
" SELECT dpn.rowid";
611 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS dpn";
612 $sql .=
" WHERE dpn.entity IN (" .
getEntity(
'accountancy') .
")";
613 $sql .=
" AND dpn.doc_type = '" . $this->db->escape($doc_type) .
"'";
614 $sql .=
" AND dpn.fk_doc IN (" . $this->db->sanitize(implode(
',', $doc_ids)) .
")";
615 $sql .=
" AND dpn.piece_num = ab.piece_num";
618 if ($only_has_subledger_account) $sql .=
" AND ab.subledger_account != ''";
620 dol_syslog(__METHOD__ .
" - Get all bookkeeping lines linked", LOG_DEBUG);
621 $resql = $this->db->query($sql);
623 $this->errors[] =
"Error " . $this->db->lasterror();
628 while ($obj = $this->db->fetch_object($resql)) {
629 $group[$obj->rowid] = array(
631 'piece_num' => $obj->piece_num,
632 'debit' => $obj->debit,
633 'credit' => $obj->credit,
634 'lettering_code' => $obj->lettering_code,
637 $this->db->free($resql);
639 if (!empty($group)) $grouped_lines[] = $group;
643 return $grouped_lines;
654 dol_syslog(__METHOD__ .
" - bank_ids=".json_encode($bank_ids), LOG_DEBUG);
657 $bank_ids = is_array($bank_ids) ? $bank_ids : array();
659 if (empty($bank_ids)) {
663 $bookkeeping_lines_by_type = array();
664 foreach (self::$doc_type_infos as $doc_type => $doc_type_info) {
666 $sql =
"SELECT DISTINCT dp." . $doc_type_info[
'doc_payment_table_fk_doc'] .
" AS fk_doc";
667 $sql .=
" FROM " . MAIN_DB_PREFIX . $doc_type_info[
'payment_table'] .
" AS p";
668 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX . $doc_type_info[
'doc_payment_table'] .
" AS dp ON dp." . $doc_type_info[
'doc_payment_table_fk_payment'] .
" = p.rowid";
669 $sql .=
" WHERE p." . $doc_type_info[
'payment_table_fk_bank'] .
" IN (" . $this->db->sanitize(implode(
',', $bank_ids)) .
")";
670 $sql .=
" AND dp." . $doc_type_info[
'doc_payment_table_fk_doc'] .
" > 0";
672 dol_syslog(__METHOD__ .
" - Get all fk_doc by doc_type from list of bank ids for '" . $doc_type .
"'", LOG_DEBUG);
673 $resql = $this->db->query($sql);
675 $this->errors[] =
"Error " . $this->db->lasterror();
679 while ($obj = $this->db->fetch_object($resql)) {
680 $bookkeeping_lines_by_type[$doc_type][$obj->fk_doc] = $obj->fk_doc;
682 $this->db->free($resql);
685 return $bookkeeping_lines_by_type;
699 dol_syslog(__METHOD__ .
" - bank_ids=".json_encode($document_ids) .
", doc_type=$doc_type", LOG_DEBUG);
702 $document_ids = is_array($document_ids) ? $document_ids : array();
704 $document_ids = array_filter($document_ids);
706 $doc_type = trim($doc_type);
708 if (empty($document_ids)) {
711 if (!is_array(self::$doc_type_infos[$doc_type])) {
712 $langs->load(
'errors');
713 $this->errors[] = $langs->trans(
'ErrorBadParameters');
717 $doc_type_info = self::$doc_type_infos[$doc_type];
721 $sql =
"SELECT DISTINCT p." . $doc_type_info[
'payment_table_fk_bank'] .
" AS fk_doc";
722 $sql .=
" FROM " . MAIN_DB_PREFIX . $doc_type_info[
'payment_table'] .
" AS p";
723 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX . $doc_type_info[
'doc_payment_table'] .
" AS dp ON dp." . $doc_type_info[
'doc_payment_table_fk_payment'] .
" = p.rowid";
724 $sql .=
" WHERE dp." . $doc_type_info[
'doc_payment_table_fk_doc'] .
" IN (" . $this->db->sanitize(implode(
',', $document_ids)) .
")";
725 $sql .=
" AND p." . $doc_type_info[
'payment_table_fk_bank'] .
" > 0";
727 dol_syslog(__METHOD__ .
" - Get all bank ids from list of document ids of a type '" . $doc_type .
"'", LOG_DEBUG);
728 $resql = $this->db->query($sql);
730 $this->errors[] =
"Error " . $this->db->lasterror();
734 while ($obj = $this->db->fetch_object($resql)) {
735 $bank_ids[$obj->fk_doc] = $obj->fk_doc;
737 $this->db->free($resql);
754 $document_ids = is_array($document_ids) ? $document_ids : array();
755 $doc_type = trim($doc_type);
757 $document_ids = array_filter($document_ids);
759 if (empty($document_ids)) {
763 if (!is_array(self::$doc_type_infos[$doc_type])) {
764 $langs->load(
'errors');
765 $this->errors[] = $langs->trans(
'ErrorBadParameters');
769 $doc_type_info = self::$doc_type_infos[$doc_type];
772 $current_document_ids = array();
773 $link_by_element = array();
774 $element_by_link = array();
775 foreach ($doc_type_info[
'linked_info'] as $linked_info) {
776 if (empty($linked_info[
'fk_line_link'])) {
777 $sql =
"SELECT DISTINCT tl2.".$linked_info[
'fk_link'].
" AS fk_link, tl2.".$linked_info[
'fk_doc'].
" AS fk_doc";
778 $sql .=
" FROM ".MAIN_DB_PREFIX.$linked_info[
'table'].
" AS tl";
779 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$linked_info[
'table'].
" AS tl2 ON tl2.".$linked_info[
'fk_link'].
" = tl.".$linked_info[
'fk_link'];
780 $sql .=
" WHERE tl.".$linked_info[
'fk_doc'].
" IN (".$this->db->sanitize(implode(
',', $document_ids)).
")";
782 $sql =
"SELECT DISTINCT tl2.fk_link, tl2.fk_doc";
784 $sql .=
" SELECT DISTINCT " . $this->db->ifsql(
"tll." . $linked_info[
'fk_table_link_line_parent'],
"tll." . $linked_info[
'fk_table_link_line_parent'],
"tl." . $linked_info[
'fk_link']) .
" AS fk_link, tl." . $linked_info[
'fk_doc'] .
" AS fk_doc";
785 $sql .=
" FROM " . MAIN_DB_PREFIX . $linked_info[
'table'] .
" AS tl";
786 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX . $linked_info[
'table_link_line'] .
" AS tll ON tll." . $linked_info[
'fk_table_link_line'] .
" = tl." . $linked_info[
'fk_line_link'];
788 $sql .=
" LEFT JOIN (";
789 $sql .=
" SELECT DISTINCT " . $this->db->ifsql(
"tll." . $linked_info[
'fk_table_link_line_parent'],
"tll." . $linked_info[
'fk_table_link_line_parent'],
"tl." . $linked_info[
'fk_link']) .
" AS fk_link, tl." . $linked_info[
'fk_doc'] .
" AS fk_doc";
790 $sql .=
" FROM " . MAIN_DB_PREFIX . $linked_info[
'table'] .
" AS tl";
791 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX . $linked_info[
'table_link_line'] .
" AS tll ON tll." . $linked_info[
'fk_table_link_line'] .
" = tl." . $linked_info[
'fk_line_link'];
792 $sql .=
") AS tl2 ON tl2.fk_link = tl.fk_link";
793 $sql .=
" WHERE tl.fk_doc IN (" . $this->db->sanitize(implode(
',', $document_ids)) .
")";
794 $sql .=
" AND tl2.fk_doc IS NOT NULL";
797 dol_syslog(__METHOD__ .
" - Get document lines", LOG_DEBUG);
798 $resql = $this->db->query($sql);
800 $this->errors[] =
"Error " . $this->db->lasterror();
804 $is_fk_link_is_also_fk_doc = !empty($linked_info[
'is_fk_link_is_also_fk_doc']);
805 while ($obj = $this->db->fetch_object($resql)) {
806 $current_document_ids[$obj->fk_doc] = $obj->fk_doc;
808 $link_key = $linked_info[
'prefix'] . $obj->fk_link;
809 $element_by_link[$link_key][$obj->fk_doc] = $obj->fk_doc;
810 $link_by_element[$obj->fk_doc][$link_key] = $link_key;
811 if ($is_fk_link_is_also_fk_doc) {
812 $element_by_link[$link_key][$obj->fk_link] = $obj->fk_link;
813 $link_by_element[$obj->fk_link][$link_key] = $link_key;
816 $this->db->free($resql);
819 if (count(array_diff($document_ids, $current_document_ids))) {
835 public function getGroupElements(&$link_by_element, &$element_by_link, $link_key =
'', &$current_group = array())
837 $grouped_elements = array();
838 if (!empty($link_key) && !isset($element_by_link[$link_key])) {
840 return $grouped_elements;
843 if (empty($link_key)) {
845 $save_link_by_element = $link_by_element;
846 $save_element_by_link = $element_by_link;
851 $current_link_key = !empty($link_key) ? $link_key : array_keys($element_by_link)[0];
852 $element_ids = $element_by_link[$current_link_key];
853 unset($element_by_link[$current_link_key]);
855 foreach ($element_ids as $element_id) {
857 if (!isset($link_by_element[$element_id]))
continue;
860 $current_group[$element_id] = $element_id;
863 $link_keys = $link_by_element[$element_id];
864 unset($link_by_element[$element_id]);
867 foreach ($link_keys as $key) {
868 $this->
getGroupElements($link_by_element, $element_by_link, $key, $current_group);
872 if (empty($link_key)) {
874 $grouped_elements[] = $current_group;
875 $current_group = array();
877 }
while (!empty($element_by_link) && empty($link_key));
879 if (empty($link_key)) {
881 $link_by_element = $save_link_by_element;
882 $element_by_link = $save_element_by_link;
885 return $grouped_elements;