dolibarr 23.0.3
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-2025 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 $error = 0;
403
404 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
405 $sql .= " WHERE rowid=".((int) $this->id);
406
407 $this->db->begin();
408
409 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
410 $resql = $this->db->query($sql);
411 if (!$resql) {
412 $error++;
413 $this->errors[] = "Error ".$this->db->lasterror();
414 }
415
416 // Commit or rollback
417 if ($error) {
418 foreach ($this->errors as $errmsg) {
419 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
420 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
421 }
422 $this->db->rollback();
423 return -1 * $error;
424 } else {
425 $this->db->commit();
426 return 1;
427 }
428 }
429
430
437 public function display($id)
438 {
439 global $conf;
440
441 $pcgver = getDolGlobalInt('CHARTOFACCOUNTS');
442
443 $sql = "SELECT t.rowid, t.account_number, t.label";
444 $sql .= " FROM ".$this->db->prefix()."accounting_account as t";
445 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON t.fk_pcg_version = asy.pcg_version AND asy.rowid = ".((int) $pcgver);
446 $sql .= " WHERE t.fk_accounting_category = ".((int) $id);
447 $sql .= " AND t.entity = ".$conf->entity;
448
449 $this->lines_display = array();
450
451 dol_syslog(__METHOD__, LOG_DEBUG);
452 $resql = $this->db->query($sql);
453 if ($resql) {
454 $num = $this->db->num_rows($resql);
455 if ($num) {
456 while ($obj = $this->db->fetch_object($resql)) {
457 $this->lines_display[] = $obj;
458 }
459 }
460 return $num;
461 } else {
462 $this->error = "Error ".$this->db->lasterror();
463 $this->errors[] = $this->error;
464 dol_syslog(__METHOD__." ".implode(',', $this->errors), LOG_ERR);
465
466 return -1;
467 }
468 }
469
476 public function getAccountsWithNoCategory($id)
477 {
478 global $conf;
479
480 $sql = "SELECT aa.account_number as numero_compte, aa.label as label_compte";
481 $sql .= " FROM ".$this->db->prefix()."accounting_account as aa";
482 $sql .= " INNER JOIN ".$this->db->prefix()."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
483 $sql .= " WHERE (aa.fk_accounting_category <> ".((int) $id)." OR aa.fk_accounting_category IS NULL)";
484 $sql .= " AND asy.rowid = ".((int) getDolGlobalInt('CHARTOFACCOUNTS'));
485 $sql .= " AND aa.active = 1";
486 $sql .= " AND aa.entity = ".$conf->entity;
487 $sql .= " GROUP BY aa.account_number, aa.label";
488 $sql .= " ORDER BY aa.account_number, aa.label";
489
490 $this->lines_cptbk = array();
491
492 dol_syslog(__METHOD__, LOG_DEBUG);
493 $resql = $this->db->query($sql);
494 if ($resql) {
495 $num = $this->db->num_rows($resql);
496 if ($num) {
497 while ($obj = $this->db->fetch_object($resql)) {
498 $this->lines_cptbk[] = $obj;
499 }
500 }
501
502 return $num;
503 } else {
504 $this->error = "Error ".$this->db->lasterror();
505 $this->errors[] = $this->error;
506 dol_syslog(__METHOD__." ".implode(',', $this->errors), LOG_ERR);
507
508 return -1;
509 }
510 }
511
520 public function updateAccAcc($id_cat, $cpts = array())
521 {
522 global $conf;
523 $error = 0;
524
525 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
526
527 $sql = "SELECT aa.rowid, aa.account_number";
528 $sql .= " FROM ".$this->db->prefix()."accounting_account as aa";
529 $sql .= " INNER JOIN ".$this->db->prefix()."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
530 $sql .= " AND asy.rowid = ".((int) getDolGlobalInt('CHARTOFACCOUNTS'));
531 $sql .= " AND aa.active = 1";
532 $sql .= " AND aa.entity = ".$conf->entity;
533 $sql .= " ORDER BY LENGTH(aa.account_number) DESC;"; // LENGTH is ok with mysql and postgresql
534
535 $this->db->begin();
536
537 dol_syslog(__METHOD__, LOG_DEBUG);
538 $resql = $this->db->query($sql);
539 if (!$resql) {
540 $error++;
541 $this->errors[] = "Error ".$this->db->lasterror();
542 $this->db->rollback();
543 return -1;
544 }
545
546 $accountincptsadded = array();
547 while ($obj = $this->db->fetch_object($resql)) {
548 $account_number_formated = length_accountg($obj->account_number);
549 if (!empty($accountincptsadded[$account_number_formated])) {
550 continue;
551 }
552
553 if (array_key_exists($account_number_formated, $cpts)) {
554 $accountincptsadded[$account_number_formated] = 1;
555 // We found an account number that is in list $cpts of account to add
556 $sql = "UPDATE ".$this->db->prefix()."accounting_account";
557 $sql .= " SET fk_accounting_category=".((int) $id_cat);
558 $sql .= " WHERE rowid=".((int) $obj->rowid);
559 dol_syslog(__METHOD__, LOG_DEBUG);
560 $resqlupdate = $this->db->query($sql);
561 if (!$resqlupdate) {
562 $error++;
563 $this->errors[] = "Error ".$this->db->lasterror();
564 }
565 }
566 }
567
568 // Commit or rollback
569 if ($error) {
570 foreach ($this->errors as $errmsg) {
571 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
572 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
573 }
574 $this->db->rollback();
575
576 return -1 * $error;
577 } else {
578 $this->db->commit();
579
580 return 1;
581 }
582 }
583
591 public function deleteCptCat($cpt_id)
592 {
593 $error = 0;
594
595 $sql = "UPDATE ".$this->db->prefix()."accounting_account as aa";
596 $sql .= " SET fk_accounting_category= 0";
597 $sql .= " WHERE aa.rowid = ".((int) $cpt_id);
598 $this->db->begin();
599
600 dol_syslog(__METHOD__, LOG_DEBUG);
601 $resql = $this->db->query($sql);
602 if (!$resql) {
603 $error++;
604 $this->errors[] = "Error ".$this->db->lasterror();
605 }
606
607 // Commit or rollback
608 if ($error) {
609 foreach ($this->errors as $errmsg) {
610 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
611 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
612 }
613 $this->db->rollback();
614
615 return -1 * $error;
616 } else {
617 $this->db->commit();
618
619 return 1;
620 }
621 }
622
635 public function getSumDebitCredit($cpt, $date_start, $date_end, $sens, $thirdparty_code = 'nofilter', $month = 0, $year = 0)
636 {
637 global $conf;
638
639 $this->sdc = 0;
640 $this->sdcpermonth = array();
641
642 $listofaccount = '';
643
644 if (is_array($cpt)) {
645 foreach ($cpt as $cptcursor) {
646 if (! is_null($cptcursor)) {
647 if ($listofaccount) {
648 $listofaccount .= ",";
649 }
650 $listofaccount .= "'".$cptcursor."'";
651 }
652 }
653 if (empty($listofaccount)) {
654 // List of account is empty, so we do no try sql request, we can say result is empty.
655 return 0;
656 }
657 }
658
659 $sql = "SELECT SUM(t.debit) as debit, SUM(t.credit) as credit";
660 if (is_array($cpt)) {
661 $sql .= ", t.numero_compte as accountancy_account";
662 }
663 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping as t";
664 //if (in_array($this->db->type, array('mysql', 'mysqli'))) $sql.=' USE INDEX idx_accounting_bookkeeping_doc_date';
665 $sql .= " WHERE t.entity = ".((int) $conf->entity);
666 if (is_array($cpt)) {
667 $sql .= " AND t.numero_compte IN (".$this->db->sanitize($listofaccount, 1).")";
668 } else {
669 $sql .= " AND t.numero_compte = '".$this->db->escape((string) $cpt)."'";
670 }
671 if (!empty($date_start) && !empty($date_end) && (empty($month) || empty($year))) { // If month/year provided, it is stronger than filter date_start/date_end
672 $sql .= " AND (t.doc_date BETWEEN '".$this->db->idate($date_start)."' AND '".$this->db->idate($date_end)."')";
673 }
674 if (!empty($month) && !empty($year)) {
675 $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))."')";
676 }
677 if ($thirdparty_code != 'nofilter') {
678 $sql .= " AND t.thirdparty_code = '".$this->db->escape($thirdparty_code)."'";
679 }
680 if (is_array($cpt)) {
681 $sql .= " GROUP BY t.numero_compte";
682 }
683
684 $resql = $this->db->query($sql);
685 if ($resql) {
686 $num = $this->db->num_rows($resql);
687 if ($num) {
688 $i = 0;
689 while ($i < $num) {
690 $obj = $this->db->fetch_object($resql);
691 if ($obj) {
692 if ($sens == 1) {
693 $this->sdc = $obj->debit - $obj->credit;
694 } else {
695 $this->sdc = $obj->credit - $obj->debit;
696 }
697 if (is_array($cpt)) {
698 $this->sdcperaccount[$obj->accountancy_account] = $this->sdc;
699 }
700 }
701 $i++;
702 }
703 }
704
705 return $num;
706 } else {
707 $this->error = "Error ".$this->db->lasterror();
708 $this->errors[] = $this->error;
709 dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
710 return -1;
711 }
712 }
713
721 public function getCatsCpts($catid = 0)
722 {
723 global $mysoc, $conf;
724
725 if (empty($mysoc->country_id)) {
726 $this->error = "Error ".$this->db->lasterror();
727 dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
728 return -1;
729 }
730
731 $sql = "SELECT t.rowid, t.account_number, t.label as account_label,";
732 $sql .= " cat.code, cat.position, cat.label as name_cat, cat.sens, cat.category_type, cat.formula";
733 $sql .= " FROM ".$this->db->prefix()."accounting_account as t, ".$this->db->prefix()."c_accounting_category as cat";
734 $sql .= " WHERE t.fk_accounting_category IN (SELECT c.rowid";
735 $sql .= " FROM ".$this->db->prefix().$this->table_element." as c";
736 $sql .= " WHERE c.active = 1";
737 $sql .= " AND c.entity = ".$conf->entity;
738 $sql .= " AND (c.fk_country = ".((int) $mysoc->country_id)." OR c.fk_country = 0)";
739 $sql .= " AND cat.rowid = t.fk_accounting_category";
740 $sql .= " AND t.entity = ".$conf->entity;
741 if ($catid > 0) {
742 $sql .= " AND cat.rowid = ".((int) $catid);
743 }
744 $sql .= " ORDER BY cat.position ASC";
745
746 $resql = $this->db->query($sql);
747 if ($resql) {
748 $obj = '';
749 $num = $this->db->num_rows($resql);
750 $data = array();
751 if ($num) {
752 while ($obj = $this->db->fetch_object($resql)) {
753 $name_cat = $obj->name_cat;
754 $data[$name_cat][$obj->rowid] = array(
755 'id' => $obj->rowid,
756 'code' => $obj->code,
757 'label' => $obj->label,
758 'position' => $obj->position,
759 'category_type' => $obj->category_type,
760 'formula' => $obj->formula,
761 'sens' => $obj->sens,
762 'dc' => $obj->sens,
763 'account_number' => $obj->account_number,
764 'account_label' => $obj->account_label
765 );
766 }
767 }
768 return $data;
769 } else {
770 $this->error = "Error ".$this->db->lasterror();
771 dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
772 return -1;
773 }
774 }
775
786 public function getCats($categorytype = -1, $active = 1, $id_report = 1)
787 {
788 global $conf, $mysoc;
789
790 if (empty($mysoc->country_id)) {
791 dol_print_error(null, 'Call to getCats with mysoc country not yet defined');
792 exit();
793 }
794
795 $sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position, c.category_type, c.sens, c.fk_report";
796 $sql .= " FROM ".$this->db->prefix().$this->table_element." as c";
797 $sql .= " WHERE c.active = " . (int) $active;
798 $sql .= " AND c.fk_report=".((int) $id_report);
799 $sql .= " AND c.entity = ".$conf->entity;
800 if ($categorytype >= 0) {
801 $sql .= " AND c.category_type = 1";
802 }
803 $sql .= " AND (c.fk_country = ".((int) $mysoc->country_id)." OR c.fk_country = 0)";
804 $sql .= " ORDER BY c.position ASC";
805
806 $resql = $this->db->query($sql);
807 if ($resql) {
808 $i = 0;
809 $obj = '';
810 $num = $this->db->num_rows($resql);
811 $data = array();
812 if ($num) {
813 while ($i < $num) {
814 $obj = $this->db->fetch_object($resql);
815
816 $data[] = array(
817 'rowid' => $obj->rowid,
818 'code' => $obj->code,
819 'label' => $obj->label,
820 'position' => $obj->position,
821 'category_type' => $obj->category_type,
822 'formula' => $obj->formula,
823 'sens' => $obj->sens,
824 'dc' => $obj->sens
825 );
826 $i++;
827 }
828 }
829 return $data;
830 } else {
831 $this->error = "Error ".$this->db->lasterror();
832 $this->errors[] = $this->error;
833 dol_syslog(__METHOD__." ".implode(',', $this->errors), LOG_ERR);
834
835 return -1;
836 }
837 }
838
839
851 public function getCptsCat($cat_id, $predefinedgroupwhere = '')
852 {
853 global $conf, $mysoc;
854 $sql = '';
855
856 if (empty($mysoc->country_id) && empty($mysoc->country_code)) {
857 dol_print_error(null, 'Call to select_accounting_account with mysoc country not yet defined');
858 exit();
859 }
860
861 $pcgverid = getDolGlobalInt('CHARTOFACCOUNTS');
862 $pcgvercode = dol_getIdFromCode($this->db, (string) $pcgverid, 'accounting_system', 'rowid', 'pcg_version');
863 if (empty($pcgvercode)) {
864 $pcgvercode = $pcgverid;
865 }
866
867 if (!empty($cat_id)) {
868 $sql = "SELECT t.rowid, t.account_number, t.label as account_label";
869 $sql .= " FROM ".$this->db->prefix()."accounting_account as t";
870 $sql .= " WHERE t.fk_accounting_category = ".((int) $cat_id);
871 $sql .= " AND t.entity = ".$conf->entity;
872 $sql .= " AND t.active = 1";
873 $sql .= " AND t.fk_pcg_version = '".$this->db->escape($pcgvercode)."'";
874 $sql .= " ORDER BY t.account_number";
875 } else {
876 $sql = "SELECT t.rowid, t.account_number, t.label as account_label";
877 $sql .= " FROM ".$this->db->prefix()."accounting_account as t";
878 $sql .= " WHERE ".$predefinedgroupwhere;
879 $sql .= " AND t.entity = ".$conf->entity;
880 $sql .= ' AND t.active = 1';
881 $sql .= " AND t.fk_pcg_version = '".$this->db->escape($pcgvercode)."'";
882 $sql .= " ORDER BY t.account_number";
883 }
884
885 $resql = $this->db->query($sql);
886 if ($resql) {
887 $i = 0;
888 $obj = '';
889 $num = $this->db->num_rows($resql);
890 $data = array();
891 if ($num) {
892 while ($obj = $this->db->fetch_object($resql)) {
893 $data[] = array(
894 'id' => $obj->rowid,
895 'account_number' => $obj->account_number,
896 'account_label' => $obj->account_label,
897 );
898 $i++;
899 }
900 }
901 return $data;
902 } else {
903 $this->error = "Error ".$this->db->lasterror();
904 dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
905
906 return -1;
907 }
908 }
909}
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
print $object position
Definition edit.php:207
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.
global $mysoc
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:603
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:622
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.
editval_textarea active