dolibarr 18.0.6
lettering.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2013 Olivier Geffroy <jeff@jeffinfo.com>
4 * Copyright (C) 2013-2019 Alexandre Spangaro <aspangaro@open-dsi.fr>
5 * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27include_once DOL_DOCUMENT_ROOT."/accountancy/class/bookkeeping.class.php";
28include_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
29include_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
30
35{
36 public static $doc_type_infos = array(
37 'customer_invoice' => array(
38 'payment_table' => 'paiement',
39 'payment_table_fk_bank' => 'fk_bank',
40 'doc_payment_table' => 'paiement_facture',
41 'doc_payment_table_fk_payment' => 'fk_paiement',
42 'doc_payment_table_fk_doc' => 'fk_facture',
43 'linked_info' => array(
44 array(
45 'table' => 'paiement_facture',
46 'fk_doc' => 'fk_facture',
47 'fk_link' => 'fk_paiement',
48 'prefix' => 'p',
49 ),
50 array(
51 'table' => 'societe_remise_except',
52 'fk_doc' => 'fk_facture_source',
53 'fk_link' => 'fk_facture',
54 'fk_line_link' => 'fk_facture_line',
55 'table_link_line' => 'facturedet',
56 'fk_table_link_line' => 'rowid',
57 'fk_table_link_line_parent' => 'fk_facture',
58 'prefix' => 'a',
59 'is_fk_link_is_also_fk_doc' => true,
60 ),
61 ),
62 ),
63 'supplier_invoice' => array(
64 'payment_table' => 'paiementfourn',
65 'payment_table_fk_bank' => 'fk_bank',
66 'doc_payment_table' => 'paiementfourn_facturefourn',
67 'doc_payment_table_fk_payment' => 'fk_paiementfourn',
68 'doc_payment_table_fk_doc' => 'fk_facturefourn',
69 'linked_info' => array(
70 array(
71 'table' => 'paiementfourn_facturefourn',
72 'fk_doc' => 'fk_facturefourn',
73 'fk_link' => 'fk_paiementfourn',
74 'prefix' => 'p',
75 ),
76 array(
77 'table' => 'societe_remise_except',
78 'fk_doc' => 'fk_invoice_supplier_source',
79 'fk_link' => 'fk_invoice_supplier',
80 'fk_line_link' => 'fk_invoice_supplier_line',
81 'table_link_line' => 'facture_fourn_det',
82 'fk_table_link_line' => 'rowid',
83 'fk_table_link_line_parent' => 'fk_facture_fourn',
84 'prefix' => 'a',
85 'is_fk_link_is_also_fk_doc' => true,
86 ),
87 ),
88 ),
89 );
90
97 public function letteringThirdparty($socid)
98 {
99 global $conf;
100
101 $error = 0;
102
103 $object = new Societe($this->db);
104 $object->id = $socid;
105 $object->fetch($socid);
106
107
108 if ($object->code_compta == '411CUSTCODE') {
109 $object->code_compta = '';
110 }
111
112 if ($object->code_compta_fournisseur == '401SUPPCODE') {
113 $object->code_compta_fournisseur = '';
114 }
115
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') ) ";
124 $sql .= " WHERE ( ";
125 if ($object->code_compta != "") {
126 $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
127 }
128 if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
129 $sql .= " OR ";
130 }
131 if ($object->code_compta_fournisseur != "") {
132 $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
133 }
134
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');
139
140 // echo $sql;
141 //
142 $resql = $this->db->query($sql);
143 if ($resql) {
144 $num = $this->db->num_rows($resql);
145
146 while ($obj = $this->db->fetch_object($resql)) {
147 $ids = array();
148 $ids_fact = array();
149
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.") ";
159 $sql .= " AND ( ";
160 if ($object->code_compta != "") {
161 $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
162 }
163 if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
164 $sql .= " OR ";
165 }
166 if ($object->code_compta_fournisseur != "") {
167 $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
168 }
169 $sql .= " ) ";
170
171 $resql2 = $this->db->query($sql);
172 if ($resql2) {
173 while ($obj2 = $this->db->fetch_object($resql2)) {
174 $ids[$obj2->rowid] = $obj2->rowid;
175 $ids_fact[] = $obj2->fact_id;
176 }
177 $this->db->free($resql2);
178 } else {
179 $this->errors[] = $this->db->lasterror;
180 return -1;
181 }
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;
188 $sql .= " AND ( ";
189 if ($object->code_compta != "") {
190 $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
191 }
192 if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
193 $sql .= " OR ";
194 }
195 if ($object->code_compta_fournisseur != "") {
196 $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
197 }
198 $sql .= ") ";
199
200 $resql2 = $this->db->query($sql);
201 if ($resql2) {
202 while ($obj2 = $this->db->fetch_object($resql2)) {
203 $ids[$obj2->rowid] = $obj2->rowid;
204 }
205 $this->db->free($resql2);
206 } else {
207 $this->errors[] = $this->db->lasterror;
208 return -1;
209 }
210 }
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).")"; // We don't share object for accountancy
220 $sql .= " AND ( ";
221 if ($object->code_compta != "") {
222 $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
223 }
224 if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
225 $sql .= " OR ";
226 }
227 if ($object->code_compta_fournisseur != "") {
228 $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
229 }
230 $sql .= " )";
231
232 $resql2 = $this->db->query($sql);
233 if ($resql2) {
234 while ($obj2 = $this->db->fetch_object($resql2)) {
235 $ids[$obj2->rowid] = $obj2->rowid;
236 $ids_fact[] = $obj2->fact_id;
237 }
238 } else {
239 $this->errors[] = $this->db->lasterror;
240 return -1;
241 }
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).")"; // We don't share object for accountancy
248 $sql .= " AND ( ";
249 if ($object->code_compta != "") {
250 $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
251 }
252 if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
253 $sql .= " OR ";
254 }
255 if ($object->code_compta_fournisseur != "") {
256 $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
257 }
258 $sql .= " ) ";
259
260 $resql2 = $this->db->query($sql);
261 if ($resql2) {
262 while ($obj2 = $this->db->fetch_object($resql2)) {
263 $ids[$obj2->rowid] = $obj2->rowid;
264 }
265 $this->db->free($resql2);
266 } else {
267 $this->errors[] = $this->db->lasterror;
268 return -1;
269 }
270 }
271 }
272
273 if (count($ids) > 1) {
274 $result = $this->updateLettering($ids);
275 }
276 }
277 $this->db->free($resql);
278 }
279 if ($error) {
280 foreach ($this->errors as $errmsg) {
281 dol_syslog(__METHOD__.' '.$errmsg, LOG_ERR);
282 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
283 }
284 return -1 * $error;
285 } else {
286 return 1;
287 }
288 }
289
296 public function updateLettering($ids = array(), $notrigger = false)
297 {
298 $error = 0;
299 $lettre = 'AAA';
300
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";
307 $sql .= " LIMIT 1 ";
308
309 $resqla = $this->db->query($sql);
310 if ($resqla) {
311 $obj = $this->db->fetch_object($resqla);
312 $lettre = (empty($obj->lettering_code) ? $lettre : $obj->lettering_code);
313 if (!empty($obj->lettering_code)) {
314 $lettre++;
315 }
316 $this->db->free($resqla);
317 } else {
318 $this->errors[] = 'Error'.$this->db->lasterror();
319 $error++;
320 }
321
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);
325 if ($resqlb) {
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);
329 $error++;
330 }
331 $this->db->free($resqlb);
332 } else {
333 $this->errors[] = 'Erreur sql'.$this->db->lasterror();
334 $error++;
335 }
336
337 // Update request
338
339 $now = dol_now();
340 $affected_rows = 0;
341
342 if (!$error) {
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)."'"; // todo correct date it's false
346 $sql .= " WHERE rowid IN (".$this->db->sanitize(implode(',', $ids)).") AND lettering_code IS NULL AND subledger_account != ''";
347
348 dol_syslog(get_class($this)."::update", LOG_DEBUG);
349 $resql = $this->db->query($sql);
350 if (!$resql) {
351 $error++;
352 $this->errors[] = "Error ".$this->db->lasterror();
353 } else {
354 $affected_rows = $this->db->affected_rows($resql);
355 }
356 }
357
358 // Commit or rollback
359 if ($error) {
360 foreach ($this->errors as $errmsg) {
361 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
362 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
363 }
364 return -1 * $error;
365 } else {
366 return $affected_rows;
367 }
368 }
369
376 public function deleteLettering($ids, $notrigger = false)
377 {
378 $error = 0;
379
380 $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_bookkeeping SET";
381 $sql .= " lettering_code = NULL";
382 $sql .= ", date_lettering = NULL";
383 $sql .= " WHERE rowid IN (".$this->db->sanitize(implode(',', $ids)).")";
384 $sql .= " AND subledger_account != ''";
385
386 dol_syslog(get_class($this)."::update", LOG_DEBUG);
387 $resql = $this->db->query($sql);
388 if (!$resql) {
389 $error++;
390 $this->errors[] = "Error ".$this->db->lasterror();
391 }
392
393 // Commit or rollback
394 if ($error) {
395 foreach ($this->errors as $errmsg) {
396 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
397 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
398 }
399 return -1 * $error;
400 } else {
401 return $this->db->affected_rows($resql);
402 }
403 }
404
412 public function bookkeepingLetteringAll($bookkeeping_ids, $unlettering = false)
413 {
414 dol_syslog(__METHOD__ . " - ", LOG_DEBUG);
415
416 $error = 0;
417 $errors = array();
418 $nb_lettering = 0;
419
420 $result = $this->bookkeepingLettering($bookkeeping_ids, $unlettering);
421 if ($result < 0) {
422 $error++;
423 $errors = array_merge($errors, $this->errors);
424 $nb_lettering += abs($result) - 2;
425 } else {
426 $nb_lettering += $result;
427 }
428
429 if ($error) {
430 $this->errors = $errors;
431 return -2 - $nb_lettering;
432 } else {
433 return $nb_lettering;
434 }
435 }
436
444 public function bookkeepingLettering($bookkeeping_ids, $unlettering = false)
445 {
446 global $langs;
447
448 $this->errors = array();
449
450 // Clean parameters
451 $bookkeeping_ids = is_array($bookkeeping_ids) ? $bookkeeping_ids : array();
452
453 $error = 0;
454 $nb_lettering = 0;
455 $grouped_lines = $this->getLinkedLines($bookkeeping_ids);
456 if (!is_array($grouped_lines)) {
457 return -2;
458 }
459
460 foreach ($grouped_lines as $lines) {
461 $group_error = 0;
462 $total = 0;
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']);
471
472 // Check lettering code
473 if ($unlettering) {
474 if (isset($lettering_code) && $lettering_code != $line_infos['lettering_code']) {
475 $this->errors[] = $langs->trans('AccountancyErrorMismatchLetteringCode');
476 $group_error++;
477 break;
478 }
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;
482 }
483
484 // Check balance amount
485 if (!$group_error && !$unlettering && price2num($total) != 0) {
486 $this->errors[] = $langs->trans('AccountancyErrorMismatchBalanceAmount', $total);
487 $group_error++;
488 }
489
490 // Lettering/Unlettering the group of bookkeeping lines
491 if (!$group_error && $do_it) {
492 if ($unlettering) $result = $this->deleteLettering($bookkeeping_lines);
493 else $result = $this->updateLettering($bookkeeping_lines);
494 if ($result < 0) {
495 $group_error++;
496 } elseif ($result > 0) {
497 $nb_lettering++;
498 }
499 }
500
501 if ($group_error) {
502 $this->errors[] = $langs->trans('AccountancyErrorLetteringBookkeeping', implode(', ', $piece_num_lines));
503 $error++;
504 }
505 }
506
507 if ($error) {
508 return -2 - $nb_lettering;
509 } else {
510 return $nb_lettering;
511 }
512 }
513
521 public function getLinkedLines($bookkeeping_ids, $only_has_subledger_account = true)
522 {
523 global $conf, $langs;
524 $this->errors = array();
525
526 // Clean parameters
527 $bookkeeping_ids = is_array($bookkeeping_ids) ? $bookkeeping_ids : array();
528
529 // Get all bookkeeping lines
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)) {
535 // Get all bookkeeping lines of piece number
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";
542 $sql .= " )";
543 }
544 if ($only_has_subledger_account) $sql .= " AND ab.subledger_account != ''";
545
546 dol_syslog(__METHOD__ . " - Get all bookkeeping lines", LOG_DEBUG);
547 $resql = $this->db->query($sql);
548 if (!$resql) {
549 $this->errors[] = "Error " . $this->db->lasterror();
550 return -1;
551 }
552
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;
556 }
557 $this->db->free($resql);
558
559 if (empty($bookkeeping_lines_by_type)) {
560 return array();
561 }
562
563 if (!empty($bookkeeping_lines_by_type['bank'])) {
564 $new_bookkeeping_lines_by_type = $this->getDocTypeAndFkDocFromBankLines($bookkeeping_lines_by_type['bank']);
565 if (!is_array($new_bookkeeping_lines_by_type)) {
566 return -1;
567 }
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;
571 }
572 }
573 }
574
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])) {
578 continue;
579 }
580
581 // Get all document ids grouped
582 $doc_grouped = $this->getLinkedDocumentByGroup($bookkeeping_lines_by_type[$doc_type], $doc_type);
583 if (!is_array($doc_grouped)) {
584 return -1;
585 }
586
587 // Group all lines by document/piece number
588 foreach ($doc_grouped as $doc_ids) {
589 $bank_ids = $this->getBankLinesFromFkDocAndDocType($doc_ids, $doc_type);
590 if (!is_array($bank_ids)) {
591 return -1;
592 }
593
594 // Get all bookkeeping lines linked
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') . ")";
598 $sql .= " AND (";
599 if (!empty($bank_ids)) {
600 $sql .= " EXISTS (";
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";
607 $sql .= " ) OR ";
608 }
609 $sql .= " EXISTS (";
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";
616 $sql .= " )";
617 $sql .= ")";
618 if ($only_has_subledger_account) $sql .= " AND ab.subledger_account != ''";
619
620 dol_syslog(__METHOD__ . " - Get all bookkeeping lines linked", LOG_DEBUG);
621 $resql = $this->db->query($sql);
622 if (!$resql) {
623 $this->errors[] = "Error " . $this->db->lasterror();
624 return -1;
625 }
626
627 $group = array();
628 while ($obj = $this->db->fetch_object($resql)) {
629 $group[$obj->rowid] = array(
630 'id' => $obj->rowid,
631 'piece_num' => $obj->piece_num,
632 'debit' => $obj->debit,
633 'credit' => $obj->credit,
634 'lettering_code' => $obj->lettering_code,
635 );
636 }
637 $this->db->free($resql);
638
639 if (!empty($group)) $grouped_lines[] = $group;
640 }
641 }
642
643 return $grouped_lines;
644 }
645
652 public function getDocTypeAndFkDocFromBankLines($bank_ids)
653 {
654 dol_syslog(__METHOD__ . " - bank_ids=".json_encode($bank_ids), LOG_DEBUG);
655
656 // Clean parameters
657 $bank_ids = is_array($bank_ids) ? $bank_ids : array();
658
659 if (empty($bank_ids)) {
660 return array();
661 }
662
663 $bookkeeping_lines_by_type = array();
664 foreach (self::$doc_type_infos as $doc_type => $doc_type_info) {
665 // Get all fk_doc by doc_type from bank ids
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";
671
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);
674 if (!$resql) {
675 $this->errors[] = "Error " . $this->db->lasterror();
676 return -1;
677 }
678
679 while ($obj = $this->db->fetch_object($resql)) {
680 $bookkeeping_lines_by_type[$doc_type][$obj->fk_doc] = $obj->fk_doc;
681 }
682 $this->db->free($resql);
683 }
684
685 return $bookkeeping_lines_by_type;
686 }
687
695 public function getBankLinesFromFkDocAndDocType($document_ids, $doc_type)
696 {
697 global $langs;
698
699 dol_syslog(__METHOD__ . " - bank_ids=".json_encode($document_ids) . ", doc_type=$doc_type", LOG_DEBUG);
700
701 // Clean parameters
702 $document_ids = is_array($document_ids) ? $document_ids : array();
703 //remove empty entries
704 $document_ids = array_filter($document_ids);
705
706 $doc_type = trim($doc_type);
707
708 if (empty($document_ids)) {
709 return array();
710 }
711 if (!is_array(self::$doc_type_infos[$doc_type])) {
712 $langs->load('errors');
713 $this->errors[] = $langs->trans('ErrorBadParameters');
714 return -1;
715 }
716
717 $doc_type_info = self::$doc_type_infos[$doc_type];
718 $bank_ids = array();
719
720 // Get all fk_doc by doc_type from bank ids
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";
726
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);
729 if (!$resql) {
730 $this->errors[] = "Error " . $this->db->lasterror();
731 return -1;
732 }
733
734 while ($obj = $this->db->fetch_object($resql)) {
735 $bank_ids[$obj->fk_doc] = $obj->fk_doc;
736 }
737 $this->db->free($resql);
738
739 return $bank_ids;
740 }
741
749 public function getLinkedDocumentByGroup($document_ids, $doc_type)
750 {
751 global $langs;
752
753 // Clean parameters
754 $document_ids = is_array($document_ids) ? $document_ids : array();
755 $doc_type = trim($doc_type);
756 //remove empty entries
757 $document_ids = array_filter($document_ids);
758
759 if (empty($document_ids)) {
760 return array();
761 }
762
763 if (!is_array(self::$doc_type_infos[$doc_type])) {
764 $langs->load('errors');
765 $this->errors[] = $langs->trans('ErrorBadParameters');
766 return -1;
767 }
768
769 $doc_type_info = self::$doc_type_infos[$doc_type];
770
771 // Get document lines
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)).")";
781 } else {
782 $sql = "SELECT DISTINCT tl2.fk_link, tl2.fk_doc";
783 $sql .= " FROM (";
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'];
787 $sql .= ") AS tl";
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";
795 }
796
797 dol_syslog(__METHOD__ . " - Get document lines", LOG_DEBUG);
798 $resql = $this->db->query($sql);
799 if (!$resql) {
800 $this->errors[] = "Error " . $this->db->lasterror();
801 return -1;
802 }
803
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;
807
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;
814 }
815 }
816 $this->db->free($resql);
817 }
818
819 if (count(array_diff($document_ids, $current_document_ids))) {
820 return $this->getLinkedDocumentByGroup($current_document_ids, $doc_type);
821 }
822
823 return $this->getGroupElements($link_by_element, $element_by_link);
824 }
825
835 public function getGroupElements(&$link_by_element, &$element_by_link, $link_key = '', &$current_group = array())
836 {
837 $grouped_elements = array();
838 if (!empty($link_key) && !isset($element_by_link[$link_key])) {
839 // Return if specific link key not found
840 return $grouped_elements;
841 }
842
843 if (empty($link_key)) {
844 // Save list when is the begin of recursive function
845 $save_link_by_element = $link_by_element;
846 $save_element_by_link = $element_by_link;
847 }
848
849 do {
850 // Get current element id, get this payment id list and delete the entry
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]);
854
855 foreach ($element_ids as $element_id) {
856 // Continue if element id in not found
857 if (!isset($link_by_element[$element_id])) continue;
858
859 // Set the element in the current group
860 $current_group[$element_id] = $element_id;
861
862 // Get current link keys, get this element id list and delete the entry
863 $link_keys = $link_by_element[$element_id];
864 unset($link_by_element[$element_id]);
865
866 // Set element id on the current group for each link key of the element
867 foreach ($link_keys as $key) {
868 $this->getGroupElements($link_by_element, $element_by_link, $key, $current_group);
869 }
870 }
871
872 if (empty($link_key)) {
873 // Save current group and reset the current group when is the begin of recursive function
874 $grouped_elements[] = $current_group;
875 $current_group = array();
876 }
877 } while (!empty($element_by_link) && empty($link_key));
878
879 if (empty($link_key)) {
880 // Restore list when is the begin of recursive function
881 $link_by_element = $save_link_by_element;
882 $element_by_link = $save_element_by_link;
883 }
884
885 return $grouped_elements;
886 }
887}
Class to manage Ledger (General Ledger and Subledger)
Class Lettering.
updateLettering($ids=array(), $notrigger=false)
getDocTypeAndFkDocFromBankLines($bank_ids)
Get all fk_doc by doc_type from list of bank ids.
letteringThirdparty($socid)
letteringThirdparty
bookkeepingLetteringAll($bookkeeping_ids, $unlettering=false)
Lettering bookkeeping lines all types.
getGroupElements(&$link_by_element, &$element_by_link, $link_key='', &$current_group=array())
Get element ids grouped by link or element in common.
bookkeepingLettering($bookkeeping_ids, $unlettering=false)
Lettering bookkeeping lines.
getLinkedDocumentByGroup($document_ids, $doc_type)
Get all linked document ids by group and type.
deleteLettering($ids, $notrigger=false)
getBankLinesFromFkDocAndDocType($document_ids, $doc_type)
Get all bank ids from list of document ids of a type.
getLinkedLines($bookkeeping_ids, $only_has_subledger_account=true)
Lettering bookkeeping lines.
Class to manage third parties objects (customers, suppliers, prospects...)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_now($mode='auto')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.