103 $object =
new Societe($this->db);
104 $object->id = $socid;
105 $object->fetch($socid);
108 if ($object->code_compta_client ==
'411CUSTCODE') {
109 $object->code_compta_client =
'';
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_client !=
"") {
126 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_client).
"' ";
128 if ($object->code_compta_client !=
"" && $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_client !=
"") {
161 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_client).
"' ";
163 if ($object->code_compta_client !=
"" && $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_client !=
"") {
190 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_client).
"' ";
192 if ($object->code_compta_client !=
"" && $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_client !=
"") {
222 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_client).
"' ";
224 if ($object->code_compta_client !=
"" && $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_client !=
"") {
250 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_client).
"' ";
252 if ($object->code_compta_client !=
"" && $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 $lettre = str_pad(
"",
getDolGlobalInt(
'ACCOUNTING_LETTERING_NBLETTERS', 3),
"A");
303 $sql =
"SELECT DISTINCT ab2.lettering_code";
304 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
305 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab2 ON ab2.subledger_account = ab.subledger_account";
306 $sql .=
" WHERE ab.rowid IN (" . $this->db->sanitize(implode(
',', $ids)) .
")";
307 $sql .=
" AND ab2.lettering_code != ''";
308 $sql .=
" ORDER BY ab2.lettering_code DESC";
311 $resqla = $this->db->query($sql);
313 $obj = $this->db->fetch_object($resqla);
314 $lettre = (empty($obj->lettering_code) ? $lettre : $obj->lettering_code);
315 if (!empty($obj->lettering_code)) {
318 $this->db->free($resqla);
320 $this->errors[] =
'Error'.$this->db->lasterror();
324 $sql =
"SELECT SUM(ABS(debit)) as deb, SUM(ABS(credit)) as cred FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping WHERE ";
325 $sql .=
" rowid IN (".$this->db->sanitize(implode(
',', $ids)).
") AND lettering_code IS NULL AND subledger_account != ''";
326 $resqlb = $this->db->query($sql);
328 $obj = $this->db->fetch_object($resqlb);
329 if (!(round(abs($obj->deb), 2) === round(abs($obj->cred), 2))) {
330 $this->errors[] =
'Total not exacts '.round(abs($obj->deb), 2).
' vs '.round(abs($obj->cred), 2);
333 $this->db->free($resqlb);
335 $this->errors[] =
'Erreur sql'.$this->db->lasterror();
344 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"accounting_bookkeeping SET";
345 $sql .=
" lettering_code='".$this->db->escape($lettre).
"'";
346 $sql .=
", date_lettering = '".$this->db->idate($now).
"'";
347 $sql .=
" WHERE rowid IN (".$this->db->sanitize(implode(
',', $ids)).
") AND lettering_code IS NULL AND subledger_account != ''";
349 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
350 $resql = $this->db->query($sql);
353 $this->errors[] =
"Error ".$this->db->lasterror();
355 $affected_rows = $this->db->affected_rows($resql);
361 foreach ($this->errors as $errmsg) {
362 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
363 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
367 return $affected_rows;
449 $this->errors = array();
452 $bookkeeping_ids = is_array($bookkeeping_ids) ? $bookkeeping_ids : array();
457 if (!is_array($grouped_lines)) {
461 foreach ($grouped_lines as $lines) {
464 $do_it = !$unlettering;
465 $lettering_code =
null;
466 $piece_num_lines = array();
467 $bookkeeping_lines = array();
468 foreach ($lines as $line_infos) {
469 $bookkeeping_lines[$line_infos[
'id']] = $line_infos[
'id'];
470 $piece_num_lines[$line_infos[
'piece_num']] = $line_infos[
'piece_num'];
471 $total += ($line_infos[
'credit'] > 0 ? $line_infos[
'credit'] : -$line_infos[
'debit']);
475 if (isset($lettering_code) && $lettering_code != $line_infos[
'lettering_code']) {
476 $this->errors[] = $langs->trans(
'AccountancyErrorMismatchLetteringCode');
480 if (!isset($lettering_code)) {
481 $lettering_code = (string) $line_infos[
'lettering_code'];
483 if (!empty($line_infos[
'lettering_code'])) {
486 } elseif (!empty($line_infos[
'lettering_code'])) {
492 if (!$group_error && !$unlettering &&
price2num($total) != 0) {
493 $this->errors[] = $langs->trans(
'AccountancyErrorMismatchBalanceAmount', $total);
498 if (!$group_error && $do_it) {
506 } elseif ($result > 0) {
512 $this->errors[] = $langs->trans(
'AccountancyErrorLetteringBookkeeping', implode(
', ', $piece_num_lines));
518 return -2 - $nb_lettering;
520 return $nb_lettering;
531 public function getLinkedLines($bookkeeping_ids, $only_has_subledger_account =
true)
533 global $conf, $langs;
534 $this->errors = array();
537 $bookkeeping_ids = is_array($bookkeeping_ids) ? $bookkeeping_ids : array();
540 $sql =
"SELECT DISTINCT ab.doc_type, ab.fk_doc";
541 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
542 $sql .=
" WHERE ab.entity IN (" .
getEntity(
'accountancy') .
")";
543 $sql .=
" AND ab.fk_doc > 0";
544 if (!empty($bookkeeping_ids)) {
546 $sql .=
" AND EXISTS (";
547 $sql .=
" SELECT rowid";
548 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS pn";
549 $sql .=
" WHERE pn.entity IN (" .
getEntity(
'accountancy') .
")";
550 $sql .=
" AND pn.rowid IN (" . $this->db->sanitize(implode(
',', $bookkeeping_ids)) .
")";
551 $sql .=
" AND pn.piece_num = ab.piece_num";
554 if ($only_has_subledger_account) {
555 $sql .=
" AND ab.subledger_account != ''";
558 dol_syslog(__METHOD__ .
" - Get all bookkeeping lines", LOG_DEBUG);
559 $resql = $this->db->query($sql);
561 $this->errors[] =
"Error " . $this->db->lasterror();
565 $bookkeeping_lines_by_type = array();
566 while ($obj = $this->db->fetch_object($resql)) {
567 $bookkeeping_lines_by_type[$obj->doc_type][$obj->fk_doc] = $obj->fk_doc;
569 $this->db->free($resql);
571 if (empty($bookkeeping_lines_by_type)) {
575 if (!empty($bookkeeping_lines_by_type[
'bank'])) {
577 if (!is_array($new_bookkeeping_lines_by_type)) {
580 foreach ($new_bookkeeping_lines_by_type as $doc_type => $fk_docs) {
581 foreach ($fk_docs as $fk_doc) {
582 $bookkeeping_lines_by_type[$doc_type][$fk_doc] = $fk_doc;
587 $grouped_lines = array();
588 foreach (self::$doc_type_infos as $doc_type => $doc_type_info) {
589 if (!is_array($bookkeeping_lines_by_type[$doc_type])) {
595 if (!is_array($doc_grouped)) {
600 foreach ($doc_grouped as $doc_ids) {
602 if (!is_array($bank_ids)) {
607 $sql =
"SELECT DISTINCT ab.rowid, ab.piece_num, ab.debit, ab.credit, ab.lettering_code";
608 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS ab";
609 $sql .=
" WHERE ab.entity IN (" .
getEntity(
'accountancy') .
")";
611 if (!empty($bank_ids)) {
613 $sql .=
" SELECT bpn.rowid";
614 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS bpn";
615 $sql .=
" WHERE bpn.entity IN (" .
getEntity(
'accountancy') .
")";
616 $sql .=
" AND bpn.doc_type = 'bank'";
617 $sql .=
" AND bpn.fk_doc IN (" . $this->db->sanitize(implode(
',', $bank_ids)) .
")";
618 $sql .=
" AND bpn.piece_num = ab.piece_num";
622 $sql .=
" SELECT dpn.rowid";
623 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping AS dpn";
624 $sql .=
" WHERE dpn.entity IN (" .
getEntity(
'accountancy') .
")";
625 $sql .=
" AND dpn.doc_type = '" . $this->db->escape($doc_type) .
"'";
626 $sql .=
" AND dpn.fk_doc IN (" . $this->db->sanitize(implode(
',', $doc_ids)) .
")";
627 $sql .=
" AND dpn.piece_num = ab.piece_num";
630 if ($only_has_subledger_account) {
631 $sql .=
" AND ab.subledger_account != ''";
634 dol_syslog(__METHOD__ .
" - Get all bookkeeping lines linked", LOG_DEBUG);
635 $resql = $this->db->query($sql);
637 $this->errors[] =
"Error " . $this->db->lasterror();
642 while ($obj = $this->db->fetch_object($resql)) {
643 $group[$obj->rowid] = array(
645 'piece_num' => $obj->piece_num,
646 'debit' => $obj->debit,
647 'credit' => $obj->credit,
648 'lettering_code' => $obj->lettering_code,
651 $this->db->free($resql);
653 if (!empty($group)) {
654 $grouped_lines[] = $group;
659 return $grouped_lines;
670 dol_syslog(__METHOD__ .
" - bank_ids=".json_encode($bank_ids), LOG_DEBUG);
673 $bank_ids = is_array($bank_ids) ? $bank_ids : array();
675 if (empty($bank_ids)) {
679 $bookkeeping_lines_by_type = array();
680 foreach (self::$doc_type_infos as $doc_type => $doc_type_info) {
682 $sql =
"SELECT DISTINCT dp." . $doc_type_info[
'doc_payment_table_fk_doc'] .
" AS fk_doc";
683 $sql .=
" FROM " . MAIN_DB_PREFIX . $doc_type_info[
'payment_table'] .
" AS p";
684 $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";
685 $sql .=
" WHERE p." . $doc_type_info[
'payment_table_fk_bank'] .
" IN (" . $this->db->sanitize(implode(
',', $bank_ids)) .
")";
686 $sql .=
" AND dp." . $doc_type_info[
'doc_payment_table_fk_doc'] .
" > 0";
688 dol_syslog(__METHOD__ .
" - Get all fk_doc by doc_type from list of bank ids for '" . $doc_type .
"'", LOG_DEBUG);
689 $resql = $this->db->query($sql);
691 $this->errors[] =
"Error " . $this->db->lasterror();
695 while ($obj = $this->db->fetch_object($resql)) {
696 $bookkeeping_lines_by_type[$doc_type][$obj->fk_doc] = $obj->fk_doc;
698 $this->db->free($resql);
701 return $bookkeeping_lines_by_type;
715 dol_syslog(__METHOD__ .
" - bank_ids=".json_encode($document_ids) .
", doc_type=$doc_type", LOG_DEBUG);
718 $document_ids = is_array($document_ids) ? $document_ids : array();
720 $document_ids = array_filter($document_ids);
722 $doc_type = trim($doc_type);
724 if (empty($document_ids)) {
727 if (!is_array(self::$doc_type_infos[$doc_type])) {
728 $langs->load(
'errors');
729 $this->errors[] = $langs->trans(
'ErrorBadParameters');
733 $doc_type_info = self::$doc_type_infos[$doc_type];
737 $sql =
"SELECT DISTINCT p." . $doc_type_info[
'payment_table_fk_bank'] .
" AS fk_doc";
738 $sql .=
" FROM " . MAIN_DB_PREFIX . $doc_type_info[
'payment_table'] .
" AS p";
739 $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";
740 $sql .=
" WHERE dp." . $doc_type_info[
'doc_payment_table_fk_doc'] .
" IN (" . $this->db->sanitize(implode(
',', $document_ids)) .
")";
741 $sql .=
" AND p." . $doc_type_info[
'payment_table_fk_bank'] .
" > 0";
743 dol_syslog(__METHOD__ .
" - Get all bank ids from list of document ids of a type '" . $doc_type .
"'", LOG_DEBUG);
744 $resql = $this->db->query($sql);
746 $this->errors[] =
"Error " . $this->db->lasterror();
750 while ($obj = $this->db->fetch_object($resql)) {
751 $bank_ids[$obj->fk_doc] = $obj->fk_doc;
753 $this->db->free($resql);
770 $document_ids = is_array($document_ids) ? $document_ids : array();
771 $doc_type = trim($doc_type);
773 $document_ids = array_filter($document_ids);
775 if (empty($document_ids)) {
779 if (!is_array(self::$doc_type_infos[$doc_type])) {
780 $langs->load(
'errors');
781 $this->errors[] = $langs->trans(
'ErrorBadParameters');
785 $doc_type_info = self::$doc_type_infos[$doc_type];
788 $current_document_ids = array();
789 $link_by_element = array();
790 $element_by_link = array();
791 foreach ($doc_type_info[
'linked_info'] as $linked_info) {
792 if (empty($linked_info[
'fk_line_link'])) {
793 $sql =
"SELECT DISTINCT tl2.".$linked_info[
'fk_link'].
" AS fk_link, tl2.".$linked_info[
'fk_doc'].
" AS fk_doc";
794 $sql .=
" FROM ".MAIN_DB_PREFIX.$linked_info[
'table'].
" AS tl";
795 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$linked_info[
'table'].
" AS tl2 ON tl2.".$linked_info[
'fk_link'].
" = tl.".$linked_info[
'fk_link'];
796 $sql .=
" WHERE tl.".$linked_info[
'fk_doc'].
" IN (".$this->db->sanitize(implode(
',', $document_ids)).
")";
798 $sql =
"SELECT DISTINCT tl2.fk_link, tl2.fk_doc";
800 $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";
801 $sql .=
" FROM " . MAIN_DB_PREFIX . $linked_info[
'table'] .
" AS tl";
802 $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'];
804 $sql .=
" LEFT JOIN (";
805 $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";
806 $sql .=
" FROM " . MAIN_DB_PREFIX . $linked_info[
'table'] .
" AS tl";
807 $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'];
808 $sql .=
") AS tl2 ON tl2.fk_link = tl.fk_link";
809 $sql .=
" WHERE tl.fk_doc IN (" . $this->db->sanitize(implode(
',', $document_ids)) .
")";
810 $sql .=
" AND tl2.fk_doc IS NOT NULL";
813 dol_syslog(__METHOD__ .
" - Get document lines", LOG_DEBUG);
814 $resql = $this->db->query($sql);
816 $this->errors[] =
"Error " . $this->db->lasterror();
820 $is_fk_link_is_also_fk_doc = !empty($linked_info[
'is_fk_link_is_also_fk_doc']);
821 while ($obj = $this->db->fetch_object($resql)) {
822 $current_document_ids[$obj->fk_doc] = $obj->fk_doc;
824 $link_key = $linked_info[
'prefix'] . $obj->fk_link;
825 $element_by_link[$link_key][$obj->fk_doc] = $obj->fk_doc;
826 $link_by_element[$obj->fk_doc][$link_key] = $link_key;
827 if ($is_fk_link_is_also_fk_doc) {
828 $element_by_link[$link_key][$obj->fk_link] = $obj->fk_link;
829 $link_by_element[$obj->fk_link][$link_key] = $link_key;
832 $this->db->free($resql);
835 if (count(array_diff($document_ids, $current_document_ids))) {
851 public function getGroupElements(&$link_by_element, &$element_by_link, $link_key =
'', &$current_group = array())
853 $grouped_elements = array();
854 if (!empty($link_key) && !isset($element_by_link[$link_key])) {
856 return $grouped_elements;
859 if (empty($link_key)) {
861 $save_link_by_element = $link_by_element;
862 $save_element_by_link = $element_by_link;
867 $current_link_key = !empty($link_key) ? $link_key : array_keys($element_by_link)[0];
868 $element_ids = $element_by_link[$current_link_key];
869 unset($element_by_link[$current_link_key]);
871 foreach ($element_ids as $element_id) {
873 if (!isset($link_by_element[$element_id])) {
878 $current_group[$element_id] = $element_id;
881 $link_keys = $link_by_element[$element_id];
882 unset($link_by_element[$element_id]);
885 foreach ($link_keys as $key) {
886 $this->
getGroupElements($link_by_element, $element_by_link, $key, $current_group);
890 if (empty($link_key)) {
892 $grouped_elements[] = $current_group;
893 $current_group = array();
895 }
while (!empty($element_by_link) && empty($link_key));
897 if (empty($link_key)) {
899 $link_by_element = $save_link_by_element;
900 $element_by_link = $save_element_by_link;
903 return $grouped_elements;