dolibarr 22.0.5
accountancycategory.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2016 Jamal Elbaz <jamelbaz@gmail.pro>
3 * Copyright (C) 2016-2025 Alexandre Spangaro <alexandre@inovea-conseil.com>
4 * Copyright (C) 2018-2024 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
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
27// Class
28require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
29
33class AccountancyCategory // extends CommonObject
34{
38 public $db;
39
43 public $error;
44
48 public $errors = array();
49
53 public $element = 'c_accounting_category';
54
58 public $table_element = 'c_accounting_category';
59
64 public $rowid;
65
69 public $id;
70
74 public $code;
75
79 public $label;
80
84 public $range_account;
85
89 public $sens;
90
94 public $category_type;
95
99 public $formula;
100
104 public $position;
105
109 public $fk_country;
110
114 public $active;
115
119 public $lines_cptbk;
120
124 public $lines_display;
125
129 public $sdc;
130
134 public $sdcpermonth;
135
139 public $sdcperaccount;
140
146 public function __construct($db)
147 {
148 $this->db = $db;
149 }
150
151
159 public function create($user, $notrigger = 0)
160 {
161 global $conf, $langs;
162 $error = 0;
163
164 // Clean parameters
165 if (isset($this->code)) {
166 $this->code = trim($this->code);
167 }
168 if (isset($this->label)) {
169 $this->label = trim($this->label);
170 }
171 if (isset($this->range_account)) {
172 $this->range_account = trim($this->range_account);
173 }
174 if (isset($this->sens)) {
175 $this->sens = (int) $this->sens;
176 }
177 if (isset($this->category_type)) {
178 $this->category_type = (int) $this->category_type;
179 }
180 if (isset($this->formula)) {
181 $this->formula = trim($this->formula);
182 }
183 if (isset($this->position)) {
184 $this->position = (int) $this->position;
185 }
186 if (isset($this->fk_country)) {
187 $this->fk_country = (int) $this->fk_country;
188 }
189 if (isset($this->active)) {
190 $this->active = (int) $this->active;
191 }
192
193 // Check parameters
194 // Put here code to add control on parameters values
195
196 // Insert request
197 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
198 if ($this->rowid > 0) {
199 $sql .= "rowid, ";
200 }
201 $sql .= "code, ";
202 $sql .= "label, ";
203 $sql .= "range_account, ";
204 $sql .= "sens, ";
205 $sql .= "category_type, ";
206 $sql .= "formula, ";
207 $sql .= "position, ";
208 $sql .= "fk_country, ";
209 $sql .= "active, ";
210 $sql .= "entity";
211 $sql .= ") VALUES (";
212 if ($this->rowid > 0) {
213 $sql .= " ".((int) $this->rowid).",";
214 }
215 $sql .= " ".(!isset($this->code) ? "NULL" : "'".$this->db->escape($this->code)."'").",";
216 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
217 $sql .= " ".(!isset($this->range_account) ? 'NULL' : "'".$this->db->escape($this->range_account)."'").",";
218 $sql .= " ".(!isset($this->sens) ? 'NULL' : "'".$this->db->escape((string) $this->sens)."'").",";
219 $sql .= " ".(!isset($this->category_type) ? 'NULL' : "'".$this->db->escape((string) $this->category_type)."'").",";
220 $sql .= " ".(!isset($this->formula) ? 'NULL' : "'".$this->db->escape($this->formula)."'").",";
221 $sql .= " ".(!isset($this->position) ? 'NULL' : ((int) $this->position)).",";
222 $sql .= " ".(!isset($this->fk_country) ? 'NULL' : ((int) $this->fk_country)).",";
223 $sql .= " ".(!isset($this->active) ? 'NULL' : ((int) $this->active));
224 $sql .= ", ".((int) $conf->entity);
225 $sql .= ")";
226
227 $this->db->begin();
228
229 dol_syslog(get_class($this)."::create", LOG_DEBUG);
230 $resql = $this->db->query($sql);
231 if (!$resql) {
232 $error++;
233 $this->errors[] = "Error ".$this->db->lasterror();
234 }
235
236 // Commit or rollback
237 if ($error) {
238 foreach ($this->errors as $errmsg) {
239 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
240 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
241 }
242 $this->db->rollback();
243 return -1 * $error;
244 } else {
245 $this->db->commit();
246 return $this->id;
247 }
248 }
249
250
259 public function fetch($id, $code = '', $label = '')
260 {
261 $sql = "SELECT";
262 $sql .= " t.rowid,";
263 $sql .= " t.code,";
264 $sql .= " t.label,";
265 $sql .= " t.range_account,";
266 $sql .= " t.sens,";
267 $sql .= " t.category_type,";
268 $sql .= " t.formula,";
269 $sql .= " t.position,";
270 $sql .= " t.fk_country,";
271 $sql .= " t.active";
272 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
273 if ($id) {
274 $sql .= " WHERE t.rowid = ".((int) $id);
275 } else {
276 $sql .= " WHERE t.entity IN (".getEntity('c_accounting_category').")"; // Don't use entity if you use rowid
277 if ($code) {
278 $sql .= " AND t.code = '".$this->db->escape($code)."'";
279 } elseif ($label) {
280 $sql .= " AND t.label = '".$this->db->escape($label)."'";
281 }
282 }
283
284 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
285 $resql = $this->db->query($sql);
286 if ($resql) {
287 if ($this->db->num_rows($resql)) {
288 $obj = $this->db->fetch_object($resql);
289
290 $this->id = $obj->rowid;
291 $this->code = $obj->code;
292 $this->label = $obj->label;
293 $this->range_account = $obj->range_account;
294 $this->sens = $obj->sens;
295 $this->category_type = $obj->category_type;
296 $this->formula = $obj->formula;
297 $this->position = $obj->position;
298 $this->fk_country = $obj->fk_country;
299 $this->active = $obj->active;
300 }
301 $this->db->free($resql);
302
303 return 1;
304 } else {
305 $this->error = "Error ".$this->db->lasterror();
306 return -1;
307 }
308 }
309
310
318 public function update($user = null, $notrigger = 0)
319 {
320 global $conf, $langs;
321 $error = 0;
322
323 // Clean parameters
324 if (isset($this->code)) {
325 $this->code = trim($this->code);
326 }
327 if (isset($this->label)) {
328 $this->label = trim($this->label);
329 }
330 if (isset($this->range_account)) {
331 $this->range_account = trim($this->range_account);
332 }
333 if (isset($this->sens)) {
334 $this->sens = (int) $this->sens;
335 }
336 if (isset($this->category_type)) {
337 $this->category_type = (int) $this->category_type;
338 }
339 if (isset($this->formula)) {
340 $this->formula = trim($this->formula);
341 }
342 if (isset($this->position)) {
343 $this->position = (int) $this->position;
344 }
345 if (isset($this->fk_country)) {
346 $this->fk_country = (int) $this->fk_country;
347 }
348 if (isset($this->active)) {
349 $this->active = (int) $this->active;
350 }
351
352
353 // Check parameters
354 // Put here code to add control on parameters values
355
356 // Update request
357 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
358 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
359 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
360 $sql .= " range_account=".(isset($this->range_account) ? "'".$this->db->escape($this->range_account)."'" : "null").",";
361 $sql .= " sens=".(isset($this->sens) ? ((int) $this->sens) : "null").",";
362 $sql .= " category_type=".(isset($this->category_type) ? ((int) $this->category_type) : "null").",";
363 $sql .= " formula=".(isset($this->formula) ? "'".$this->db->escape($this->formula)."'" : "null").",";
364 $sql .= " position=".(isset($this->position) ? ((int) $this->position) : "null").",";
365 $sql .= " fk_country=".(isset($this->fk_country) ? ((int) $this->fk_country) : "null").",";
366 $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null");
367 $sql .= " WHERE rowid=".((int) $this->id);
368
369 $this->db->begin();
370
371 dol_syslog(get_class($this)."::update", LOG_DEBUG);
372 $resql = $this->db->query($sql);
373 if (!$resql) {
374 $error++;
375 $this->errors[] = "Error ".$this->db->lasterror();
376 }
377
378 // Commit or rollback
379 if ($error) {
380 foreach ($this->errors as $errmsg) {
381 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
382 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
383 }
384 $this->db->rollback();
385 return -1 * $error;
386 } else {
387 $this->db->commit();
388 return 1;
389 }
390 }
391
392
400 public function delete($user, $notrigger = 0)
401 {
402 global $conf, $langs;
403 $error = 0;
404
405 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
406 $sql .= " WHERE rowid=".((int) $this->id);
407
408 $this->db->begin();
409
410 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
411 $resql = $this->db->query($sql);
412 if (!$resql) {
413 $error++;
414 $this->errors[] = "Error ".$this->db->lasterror();
415 }
416
417 // Commit or rollback
418 if ($error) {
419 foreach ($this->errors as $errmsg) {
420 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
421 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
422 }
423 $this->db->rollback();
424 return -1 * $error;
425 } else {
426 $this->db->commit();
427 return 1;
428 }
429 }
430
431
438 public function display($id)
439 {
440 global $conf;
441
442 $pcgver = getDolGlobalInt('CHARTOFACCOUNTS');
443
444 $sql = "SELECT t.rowid, t.account_number, t.label";
445 $sql .= " FROM ".$this->db->prefix()."accounting_account as t";
446 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON t.fk_pcg_version = asy.pcg_version AND asy.rowid = ".((int) $pcgver);
447 $sql .= " WHERE t.fk_accounting_category = ".((int) $id);
448 $sql .= " AND t.entity = ".$conf->entity;
449
450 $this->lines_display = array();
451
452 dol_syslog(__METHOD__, LOG_DEBUG);
453 $resql = $this->db->query($sql);
454 if ($resql) {
455 $num = $this->db->num_rows($resql);
456 if ($num) {
457 while ($obj = $this->db->fetch_object($resql)) {
458 $this->lines_display[] = $obj;
459 }
460 }
461 return $num;
462 } else {
463 $this->error = "Error ".$this->db->lasterror();
464 $this->errors[] = $this->error;
465 dol_syslog(__METHOD__." ".implode(',', $this->errors), LOG_ERR);
466
467 return -1;
468 }
469 }
470
477 public function getAccountsWithNoCategory($id)
478 {
479 global $conf;
480
481 $sql = "SELECT aa.account_number as numero_compte, aa.label as label_compte";
482 $sql .= " FROM ".$this->db->prefix()."accounting_account as aa";
483 $sql .= " INNER JOIN ".$this->db->prefix()."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
484 $sql .= " WHERE (aa.fk_accounting_category <> ".((int) $id)." OR aa.fk_accounting_category IS NULL)";
485 $sql .= " AND asy.rowid = ".((int) getDolGlobalInt('CHARTOFACCOUNTS'));
486 $sql .= " AND aa.active = 1";
487 $sql .= " AND aa.entity = ".$conf->entity;
488 $sql .= " GROUP BY aa.account_number, aa.label";
489 $sql .= " ORDER BY aa.account_number, aa.label";
490
491 $this->lines_cptbk = array();
492
493 dol_syslog(__METHOD__, LOG_DEBUG);
494 $resql = $this->db->query($sql);
495 if ($resql) {
496 $num = $this->db->num_rows($resql);
497 if ($num) {
498 while ($obj = $this->db->fetch_object($resql)) {
499 $this->lines_cptbk[] = $obj;
500 }
501 }
502
503 return $num;
504 } else {
505 $this->error = "Error ".$this->db->lasterror();
506 $this->errors[] = $this->error;
507 dol_syslog(__METHOD__." ".implode(',', $this->errors), LOG_ERR);
508
509 return -1;
510 }
511 }
512
521 public function updateAccAcc($id_cat, $cpts = array())
522 {
523 global $conf;
524 $error = 0;
525
526 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
527
528 $sql = "SELECT aa.rowid, aa.account_number";
529 $sql .= " FROM ".$this->db->prefix()."accounting_account as aa";
530 $sql .= " INNER JOIN ".$this->db->prefix()."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
531 $sql .= " AND asy.rowid = ".((int) getDolGlobalInt('CHARTOFACCOUNTS'));
532 $sql .= " AND aa.active = 1";
533 $sql .= " AND aa.entity = ".$conf->entity;
534 $sql .= " ORDER BY LENGTH(aa.account_number) DESC;"; // LENGTH is ok with mysql and postgresql
535
536 $this->db->begin();
537
538 dol_syslog(__METHOD__, LOG_DEBUG);
539 $resql = $this->db->query($sql);
540 if (!$resql) {
541 $error++;
542 $this->errors[] = "Error ".$this->db->lasterror();
543 $this->db->rollback();
544 return -1;
545 }
546
547 $accountincptsadded = array();
548 while ($obj = $this->db->fetch_object($resql)) {
549 $account_number_formated = length_accountg($obj->account_number);
550 if (!empty($accountincptsadded[$account_number_formated])) {
551 continue;
552 }
553
554 if (array_key_exists($account_number_formated, $cpts)) {
555 $accountincptsadded[$account_number_formated] = 1;
556 // We found an account number that is in list $cpts of account to add
557 $sql = "UPDATE ".$this->db->prefix()."accounting_account";
558 $sql .= " SET fk_accounting_category=".((int) $id_cat);
559 $sql .= " WHERE rowid=".((int) $obj->rowid);
560 dol_syslog(__METHOD__, LOG_DEBUG);
561 $resqlupdate = $this->db->query($sql);
562 if (!$resqlupdate) {
563 $error++;
564 $this->errors[] = "Error ".$this->db->lasterror();
565 }
566 }
567 }
568
569 // Commit or rollback
570 if ($error) {
571 foreach ($this->errors as $errmsg) {
572 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
573 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
574 }
575 $this->db->rollback();
576
577 return -1 * $error;
578 } else {
579 $this->db->commit();
580
581 return 1;
582 }
583 }
584
592 public function deleteCptCat($cpt_id)
593 {
594 $error = 0;
595
596 $sql = "UPDATE ".$this->db->prefix()."accounting_account as aa";
597 $sql .= " SET fk_accounting_category= 0";
598 $sql .= " WHERE aa.rowid = ".((int) $cpt_id);
599 $this->db->begin();
600
601 dol_syslog(__METHOD__, LOG_DEBUG);
602 $resql = $this->db->query($sql);
603 if (!$resql) {
604 $error++;
605 $this->errors[] = "Error ".$this->db->lasterror();
606 }
607
608 // Commit or rollback
609 if ($error) {
610 foreach ($this->errors as $errmsg) {
611 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
612 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
613 }
614 $this->db->rollback();
615
616 return -1 * $error;
617 } else {
618 $this->db->commit();
619
620 return 1;
621 }
622 }
623
636 public function getSumDebitCredit($cpt, $date_start, $date_end, $sens, $thirdparty_code = 'nofilter', $month = 0, $year = 0)
637 {
638 global $conf;
639
640 $this->sdc = 0;
641 $this->sdcpermonth = array();
642
643 $listofaccount = '';
644
645 if (is_array($cpt)) {
646 foreach ($cpt as $cptcursor) {
647 if (! is_null($cptcursor)) {
648 if ($listofaccount) {
649 $listofaccount .= ",";
650 }
651 $listofaccount .= "'".$cptcursor."'";
652 }
653 }
654 if (empty($listofaccount)) {
655 // List of account is empty, so we do no try sql request, we can say result is empty.
656 return 0;
657 }
658 }
659
660 $sql = "SELECT SUM(t.debit) as debit, SUM(t.credit) as credit";
661 if (is_array($cpt)) {
662 $sql .= ", t.numero_compte as accountancy_account";
663 }
664 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping as t";
665 //if (in_array($this->db->type, array('mysql', 'mysqli'))) $sql.=' USE INDEX idx_accounting_bookkeeping_doc_date';
666 $sql .= " WHERE t.entity = ".((int) $conf->entity);
667 if (is_array($cpt)) {
668 $sql .= " AND t.numero_compte IN (".$this->db->sanitize($listofaccount, 1).")";
669 } else {
670 $sql .= " AND t.numero_compte = '".$this->db->escape((string) $cpt)."'";
671 }
672 if (!empty($date_start) && !empty($date_end) && (empty($month) || empty($year))) { // If month/year provided, it is stronger than filter date_start/date_end
673 $sql .= " AND (t.doc_date BETWEEN '".$this->db->idate($date_start)."' AND '".$this->db->idate($date_end)."')";
674 }
675 if (!empty($month) && !empty($year)) {
676 $sql .= " AND (t.doc_date BETWEEN '".$this->db->idate(dol_get_first_day($year, $month))."' AND '".$this->db->idate(dol_get_last_day($year, $month))."')";
677 }
678 if ($thirdparty_code != 'nofilter') {
679 $sql .= " AND t.thirdparty_code = '".$this->db->escape($thirdparty_code)."'";
680 }
681 if (is_array($cpt)) {
682 $sql .= " GROUP BY t.numero_compte";
683 }
684
685 $resql = $this->db->query($sql);
686 if ($resql) {
687 $num = $this->db->num_rows($resql);
688 if ($num) {
689 $i = 0;
690 while ($i < $num) {
691 $obj = $this->db->fetch_object($resql);
692 if ($obj) {
693 if ($sens == 1) {
694 $this->sdc = $obj->debit - $obj->credit;
695 } else {
696 $this->sdc = $obj->credit - $obj->debit;
697 }
698 if (is_array($cpt)) {
699 $this->sdcperaccount[$obj->accountancy_account] = $this->sdc;
700 }
701 }
702 $i++;
703 }
704 }
705
706 return $num;
707 } else {
708 $this->error = "Error ".$this->db->lasterror();
709 $this->errors[] = $this->error;
710 dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
711 return -1;
712 }
713 }
714
722 public function getCatsCpts($catid = 0)
723 {
724 global $mysoc, $conf;
725
726 if (empty($mysoc->country_id)) {
727 $this->error = "Error ".$this->db->lasterror();
728 dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
729 return -1;
730 }
731
732 $sql = "SELECT t.rowid, t.account_number, t.label as account_label,";
733 $sql .= " cat.code, cat.position, cat.label as name_cat, cat.sens, cat.category_type, cat.formula";
734 $sql .= " FROM ".$this->db->prefix()."accounting_account as t, ".$this->db->prefix()."c_accounting_category as cat";
735 $sql .= " WHERE t.fk_accounting_category IN (SELECT c.rowid";
736 $sql .= " FROM ".$this->db->prefix().$this->table_element." as c";
737 $sql .= " WHERE c.active = 1";
738 $sql .= " AND c.entity = ".$conf->entity;
739 $sql .= " AND (c.fk_country = ".((int) $mysoc->country_id)." OR c.fk_country = 0)";
740 $sql .= " AND cat.rowid = t.fk_accounting_category";
741 $sql .= " AND t.entity = ".$conf->entity;
742 if ($catid > 0) {
743 $sql .= " AND cat.rowid = ".((int) $catid);
744 }
745 $sql .= " ORDER BY cat.position ASC";
746
747 $resql = $this->db->query($sql);
748 if ($resql) {
749 $obj = '';
750 $num = $this->db->num_rows($resql);
751 $data = array();
752 if ($num) {
753 while ($obj = $this->db->fetch_object($resql)) {
754 $name_cat = $obj->name_cat;
755 $data[$name_cat][$obj->rowid] = array(
756 'id' => $obj->rowid,
757 'code' => $obj->code,
758 'label' => $obj->label,
759 'position' => $obj->position,
760 'category_type' => $obj->category_type,
761 'formula' => $obj->formula,
762 'sens' => $obj->sens,
763 'dc' => $obj->sens,
764 'account_number' => $obj->account_number,
765 'account_label' => $obj->account_label
766 );
767 }
768 }
769 return $data;
770 } else {
771 $this->error = "Error ".$this->db->lasterror();
772 dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
773 return -1;
774 }
775 }
776
787 public function getCats($categorytype = -1, $active = 1, $id_report = 1)
788 {
789 global $conf, $mysoc;
790
791 if (empty($mysoc->country_id)) {
792 dol_print_error(null, 'Call to getCats with mysoc country not yet defined');
793 exit();
794 }
795
796 $sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position, c.category_type, c.sens, c.fk_report";
797 $sql .= " FROM ".$this->db->prefix().$this->table_element." as c";
798 $sql .= " WHERE c.active = " . (int) $active;
799 $sql .= " AND c.fk_report=".((int) $id_report);
800 $sql .= " AND c.entity = ".$conf->entity;
801 if ($categorytype >= 0) {
802 $sql .= " AND c.category_type = 1";
803 }
804 $sql .= " AND (c.fk_country = ".((int) $mysoc->country_id)." OR c.fk_country = 0)";
805 $sql .= " ORDER BY c.position ASC";
806
807 $resql = $this->db->query($sql);
808 if ($resql) {
809 $i = 0;
810 $obj = '';
811 $num = $this->db->num_rows($resql);
812 $data = array();
813 if ($num) {
814 while ($i < $num) {
815 $obj = $this->db->fetch_object($resql);
816
817 $data[] = array(
818 'rowid' => $obj->rowid,
819 'code' => $obj->code,
820 'label' => $obj->label,
821 'position' => $obj->position,
822 'category_type' => $obj->category_type,
823 'formula' => $obj->formula,
824 'sens' => $obj->sens,
825 'dc' => $obj->sens
826 );
827 $i++;
828 }
829 }
830 return $data;
831 } else {
832 $this->error = "Error ".$this->db->lasterror();
833 $this->errors[] = $this->error;
834 dol_syslog(__METHOD__." ".implode(',', $this->errors), LOG_ERR);
835
836 return -1;
837 }
838 }
839
840
852 public function getCptsCat($cat_id, $predefinedgroupwhere = '')
853 {
854 global $conf, $mysoc;
855 $sql = '';
856
857 if (empty($mysoc->country_id) && empty($mysoc->country_code)) {
858 dol_print_error(null, 'Call to select_accounting_account with mysoc country not yet defined');
859 exit();
860 }
861
862 $pcgverid = getDolGlobalInt('CHARTOFACCOUNTS');
863 $pcgvercode = dol_getIdFromCode($this->db, (string) $pcgverid, 'accounting_system', 'rowid', 'pcg_version');
864 if (empty($pcgvercode)) {
865 $pcgvercode = $pcgverid;
866 }
867
868 if (!empty($cat_id)) {
869 $sql = "SELECT t.rowid, t.account_number, t.label as account_label";
870 $sql .= " FROM ".$this->db->prefix()."accounting_account as t";
871 $sql .= " WHERE t.fk_accounting_category = ".((int) $cat_id);
872 $sql .= " AND t.entity = ".$conf->entity;
873 $sql .= " AND t.active = 1";
874 $sql .= " AND t.fk_pcg_version = '".$this->db->escape($pcgvercode)."'";
875 $sql .= " ORDER BY t.account_number";
876 } else {
877 $sql = "SELECT t.rowid, t.account_number, t.label as account_label";
878 $sql .= " FROM ".$this->db->prefix()."accounting_account as t";
879 $sql .= " WHERE ".$predefinedgroupwhere;
880 $sql .= " AND t.entity = ".$conf->entity;
881 $sql .= ' AND t.active = 1';
882 $sql .= " AND t.fk_pcg_version = '".$this->db->escape($pcgvercode)."'";
883 $sql .= " ORDER BY t.account_number";
884 }
885
886 $resql = $this->db->query($sql);
887 if ($resql) {
888 $i = 0;
889 $obj = '';
890 $num = $this->db->num_rows($resql);
891 $data = array();
892 if ($num) {
893 while ($obj = $this->db->fetch_object($resql)) {
894 $data[] = array(
895 'id' => $obj->rowid,
896 'account_number' => $obj->account_number,
897 'account_label' => $obj->account_label,
898 );
899 $i++;
900 }
901 }
902 return $data;
903 } else {
904 $this->error = "Error ".$this->db->lasterror();
905 dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
906
907 return -1;
908 }
909 }
910}
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
print $object position
Definition edit.php:206
Class to manage categories of an accounting account.
getCptsCat($cat_id, $predefinedgroupwhere='')
Get all accounting account of a given custom group (or a list of custom groups).
getCats($categorytype=-1, $active=1, $id_report=1)
Return list of custom groups.
update($user=null, $notrigger=0)
Update object into database.
getAccountsWithNoCategory($id)
Function to fill ->lines_cptbk with accounting account (defined in chart of account) and not yet into...
create($user, $notrigger=0)
Create object into database.
getCatsCpts($catid=0)
Function to get an array of all active custom groups (llx_c_accunting_categories) with their accounts...
fetch($id, $code='', $label='')
Load object in memory from database.
deleteCptCat($cpt_id)
Function to delete an accounting account from an accounting category.
display($id)
Function to select into ->lines_display all accounting accounts for a given custom accounting group.
getSumDebitCredit($cpt, $date_start, $date_end, $sens, $thirdparty_code='nofilter', $month=0, $year=0)
Function to set the property ->sdc (and ->sdcperaccount) that is the result of an accounting account ...
updateAccAcc($id_cat, $cpts=array())
Function to add an accounting account in an accounting category.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:600
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:619
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79