28require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
48 public $errors = array();
53 public $element =
'c_accounting_category';
58 public $table_element =
'c_accounting_category';
84 public $range_account;
94 public $category_type;
124 public $lines_display;
139 public $sdcperaccount;
159 public function create($user, $notrigger = 0)
161 global
$conf, $langs;
165 if (isset($this->code)) {
166 $this->code = trim($this->code);
168 if (isset($this->label)) {
169 $this->label = trim($this->label);
171 if (isset($this->range_account)) {
172 $this->range_account = trim($this->range_account);
174 if (isset($this->sens)) {
175 $this->sens = (int) $this->sens;
177 if (isset($this->category_type)) {
178 $this->category_type = (int) $this->category_type;
180 if (isset($this->formula)) {
181 $this->formula = trim($this->formula);
186 if (isset($this->fk_country)) {
187 $this->fk_country = (int) $this->fk_country;
189 if (isset($this->active)) {
190 $this->active = (int) $this->active;
197 $sql =
"INSERT INTO ".$this->db->prefix().$this->table_element.
" (";
198 if ($this->rowid > 0) {
203 $sql .=
"range_account, ";
205 $sql .=
"category_type, ";
207 $sql .=
"position, ";
208 $sql .=
"fk_country, ";
211 $sql .=
") VALUES (";
212 if ($this->rowid > 0) {
213 $sql .=
" ".((int) $this->rowid).
",";
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($this->sens).
"'").
",";
219 $sql .=
" ".(!isset($this->category_type) ?
'NULL' :
"'".$this->db->escape($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);
229 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
230 $resql = $this->db->query($sql);
233 $this->errors[] =
"Error ".$this->db->lasterror();
238 foreach ($this->errors as $errmsg) {
239 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
240 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
242 $this->db->rollback();
259 public function fetch($id, $code =
'', $label =
'')
265 $sql .=
" t.range_account,";
267 $sql .=
" t.category_type,";
268 $sql .=
" t.formula,";
269 $sql .=
" t.position,";
270 $sql .=
" t.fk_country,";
272 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
274 $sql .=
" WHERE t.rowid = ".((int) $id);
276 $sql .=
" WHERE t.entity IN (".getEntity(
'c_accounting_category').
")";
278 $sql .=
" AND t.code = '".$this->db->escape($code).
"'";
280 $sql .=
" AND t.label = '".$this->db->escape($label).
"'";
284 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
285 $resql = $this->db->query($sql);
287 if ($this->db->num_rows($resql)) {
288 $obj = $this->db->fetch_object($resql);
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;
298 $this->fk_country = $obj->fk_country;
299 $this->active = $obj->active;
301 $this->db->free($resql);
305 $this->error =
"Error ".$this->db->lasterror();
318 public function update($user =
null, $notrigger = 0)
320 global
$conf, $langs;
324 if (isset($this->code)) {
325 $this->code = trim($this->code);
327 if (isset($this->label)) {
328 $this->label = trim($this->label);
330 if (isset($this->range_account)) {
331 $this->range_account = trim($this->range_account);
333 if (isset($this->sens)) {
334 $this->sens = (int) $this->sens;
336 if (isset($this->category_type)) {
337 $this->category_type = (int) $this->category_type;
339 if (isset($this->formula)) {
340 $this->formula = trim($this->formula);
345 if (isset($this->fk_country)) {
346 $this->fk_country = (int) $this->fk_country;
348 if (isset($this->active)) {
349 $this->active = (int) $this->active;
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);
371 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
372 $resql = $this->db->query($sql);
375 $this->errors[] =
"Error ".$this->db->lasterror();
380 foreach ($this->errors as $errmsg) {
381 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
382 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
384 $this->db->rollback();
400 public function delete($user, $notrigger = 0)
402 global
$conf, $langs;
405 $sql =
"DELETE FROM ".$this->db->prefix().$this->table_element;
406 $sql .=
" WHERE rowid=".((int) $this->
id);
410 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
411 $resql = $this->db->query($sql);
414 $this->errors[] =
"Error ".$this->db->lasterror();
419 foreach ($this->errors as $errmsg) {
420 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
421 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
423 $this->db->rollback();
441 $sql =
"SELECT t.rowid, t.account_number, t.label";
442 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
443 $sql .=
" WHERE t.fk_accounting_category = ".((int) $id);
444 $sql .=
" AND t.entity = ".$conf->entity;
446 $this->lines_display = array();
449 $resql = $this->db->query($sql);
451 $num = $this->db->num_rows($resql);
453 while ($obj = $this->db->fetch_object($resql)) {
454 $this->lines_display[] = $obj;
459 $this->error =
"Error ".$this->db->lasterror();
460 $this->errors[] = $this->error;
461 dol_syslog(__METHOD__.
" ".implode(
',', $this->errors), LOG_ERR);
477 $sql =
"SELECT aa.account_number as numero_compte, aa.label as label_compte";
478 $sql .=
" FROM ".$this->db->prefix().
"accounting_account as aa";
479 $sql .=
" INNER JOIN ".$this->db->prefix().
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
480 $sql .=
" WHERE (aa.fk_accounting_category <> ".((int) $id).
" OR aa.fk_accounting_category IS NULL)";
481 $sql .=
" AND asy.rowid = ".((int)
getDolGlobalInt(
'CHARTOFACCOUNTS'));
482 $sql .=
" AND aa.active = 1";
483 $sql .=
" AND aa.entity = ".$conf->entity;
484 $sql .=
" GROUP BY aa.account_number, aa.label";
485 $sql .=
" ORDER BY aa.account_number, aa.label";
487 $this->lines_cptbk = array();
490 $resql = $this->db->query($sql);
492 $num = $this->db->num_rows($resql);
494 while ($obj = $this->db->fetch_object($resql)) {
495 $this->lines_cptbk[] = $obj;
501 $this->error =
"Error ".$this->db->lasterror();
502 $this->errors[] = $this->error;
503 dol_syslog(__METHOD__.
" ".implode(
',', $this->errors), LOG_ERR);
522 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
524 $sql =
"SELECT aa.rowid, aa.account_number";
525 $sql .=
" FROM ".$this->db->prefix().
"accounting_account as aa";
526 $sql .=
" INNER JOIN ".$this->db->prefix().
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
527 $sql .=
" AND asy.rowid = ".((int)
getDolGlobalInt(
'CHARTOFACCOUNTS'));
528 $sql .=
" AND aa.active = 1";
529 $sql .=
" AND aa.entity = ".$conf->entity;
530 $sql .=
" ORDER BY LENGTH(aa.account_number) DESC;";
535 $resql = $this->db->query($sql);
538 $this->errors[] =
"Error ".$this->db->lasterror();
539 $this->db->rollback();
543 $accountincptsadded = array();
544 while ($obj = $this->db->fetch_object($resql)) {
546 if (!empty($accountincptsadded[$account_number_formated])) {
550 if (array_key_exists($account_number_formated, $cpts)) {
551 $accountincptsadded[$account_number_formated] = 1;
553 $sql =
"UPDATE ".$this->db->prefix().
"accounting_account";
554 $sql .=
" SET fk_accounting_category=".((int) $id_cat);
555 $sql .=
" WHERE rowid=".((int) $obj->rowid);
557 $resqlupdate = $this->db->query($sql);
560 $this->errors[] =
"Error ".$this->db->lasterror();
567 foreach ($this->errors as $errmsg) {
569 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
571 $this->db->rollback();
592 $sql =
"UPDATE ".$this->db->prefix().
"accounting_account as aa";
593 $sql .=
" SET fk_accounting_category= 0";
594 $sql .=
" WHERE aa.rowid = ".((int) $cpt_id);
598 $resql = $this->db->query($sql);
601 $this->errors[] =
"Error ".$this->db->lasterror();
606 foreach ($this->errors as $errmsg) {
608 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
610 $this->db->rollback();
632 public function getSumDebitCredit($cpt, $date_start, $date_end, $sens, $thirdparty_code =
'nofilter', $month = 0, $year = 0)
637 $this->sdcpermonth = array();
641 if (is_array($cpt)) {
642 foreach ($cpt as $cptcursor) {
643 if (! is_null($cptcursor)) {
644 if ($listofaccount) {
645 $listofaccount .=
",";
647 $listofaccount .=
"'".$cptcursor.
"'";
650 if (empty($listofaccount)) {
656 $sql =
"SELECT SUM(t.debit) as debit, SUM(t.credit) as credit";
657 if (is_array($cpt)) {
658 $sql .=
", t.numero_compte as accountancy_account";
660 $sql .=
" FROM ".$this->db->prefix().
"accounting_bookkeeping as t";
662 $sql .=
" WHERE t.entity = ".((int)
$conf->entity);
663 if (is_array($cpt)) {
664 $sql .=
" AND t.numero_compte IN (".$this->db->sanitize($listofaccount, 1).
")";
666 $sql .=
" AND t.numero_compte = '".$this->db->escape($cpt).
"'";
668 if (!empty($date_start) && !empty($date_end) && (empty($month) || empty($year))) {
669 $sql .=
" AND (t.doc_date BETWEEN '".$this->db->idate($date_start).
"' AND '".$this->db->idate($date_end).
"')";
671 if (!empty($month) && !empty($year)) {
674 if ($thirdparty_code !=
'nofilter') {
675 $sql .=
" AND t.thirdparty_code = '".$this->db->escape($thirdparty_code).
"'";
677 if (is_array($cpt)) {
678 $sql .=
" GROUP BY t.numero_compte";
681 $resql = $this->db->query($sql);
683 $num = $this->db->num_rows($resql);
687 $obj = $this->db->fetch_object($resql);
690 $this->sdc = $obj->debit - $obj->credit;
692 $this->sdc = $obj->credit - $obj->debit;
694 if (is_array($cpt)) {
695 $this->sdcperaccount[$obj->accountancy_account] = $this->sdc;
704 $this->error =
"Error ".$this->db->lasterror();
705 $this->errors[] = $this->error;
706 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
720 global $mysoc,
$conf;
722 if (empty($mysoc->country_id)) {
723 $this->error =
"Error ".$this->db->lasterror();
724 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
728 $sql =
"SELECT t.rowid, t.account_number, t.label as account_label,";
729 $sql .=
" cat.code, cat.position, cat.label as name_cat, cat.sens, cat.category_type, cat.formula";
730 $sql .=
" FROM ".$this->db->prefix().
"accounting_account as t, ".$this->db->prefix().
"c_accounting_category as cat";
731 $sql .=
" WHERE t.fk_accounting_category IN (SELECT c.rowid";
732 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as c";
733 $sql .=
" WHERE c.active = 1";
734 $sql .=
" AND c.entity = ".$conf->entity;
735 $sql .=
" AND (c.fk_country = ".((int) $mysoc->country_id).
" OR c.fk_country = 0)";
736 $sql .=
" AND cat.rowid = t.fk_accounting_category";
737 $sql .=
" AND t.entity = ".$conf->entity;
739 $sql .=
" AND cat.rowid = ".((int) $catid);
741 $sql .=
" ORDER BY cat.position ASC";
743 $resql = $this->db->query($sql);
746 $num = $this->db->num_rows($resql);
749 while ($obj = $this->db->fetch_object($resql)) {
750 $name_cat = $obj->name_cat;
751 $data[$name_cat][$obj->rowid] = array(
753 'code' => $obj->code,
754 'label' => $obj->label,
755 'position' => $obj->position,
756 'category_type' => $obj->category_type,
757 'formula' => $obj->formula,
758 'sens' => $obj->sens,
759 'account_number' => $obj->account_number,
760 'account_label' => $obj->account_label
766 $this->error =
"Error ".$this->db->lasterror();
767 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
782 public function getCats($categorytype = -1, $active = 1, $id_report = 1)
784 global
$conf, $mysoc;
786 if (empty($mysoc->country_id)) {
787 dol_print_error(
null,
'Call to select_accounting_account with mysoc country not yet defined');
791 $sql =
"SELECT c.rowid, c.code, c.label, c.formula, c.position, c.category_type, c.sens";
792 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as c";
793 $sql .=
" WHERE c.active = " . (int) $active;
794 $sql .=
" AND c.fk_report=".((int) $id_report);
795 $sql .=
" AND c.entity = ".$conf->entity;
796 if ($categorytype >= 0) {
797 $sql .=
" AND c.category_type = 1";
799 $sql .=
" AND (c.fk_country = ".((int) $mysoc->country_id).
" OR c.fk_country = 0)";
800 $sql .=
" ORDER BY c.position ASC";
802 $resql = $this->db->query($sql);
806 $num = $this->db->num_rows($resql);
810 $obj = $this->db->fetch_object($resql);
813 'rowid' => $obj->rowid,
814 'code' => $obj->code,
815 'label' => $obj->label,
816 'position' => $obj->position,
817 'category_type' => $obj->category_type,
818 'formula' => $obj->formula,
819 'sens' => $obj->sens,
827 $this->error =
"Error ".$this->db->lasterror();
828 $this->errors[] = $this->error;
829 dol_syslog(__METHOD__.
" ".implode(
',', $this->errors), LOG_ERR);
847 public function getCptsCat($cat_id, $predefinedgroupwhere =
'')
849 global
$conf, $mysoc;
852 if (empty($mysoc->country_id) && empty($mysoc->country_code)) {
853 dol_print_error(
null,
'Call to select_accounting_account with mysoc country not yet defined');
858 $pcgvercode =
dol_getIdFromCode($this->db, (
string) $pcgverid,
'accounting_system',
'rowid',
'pcg_version');
859 if (empty($pcgvercode)) {
860 $pcgvercode = $pcgverid;
863 if (!empty($cat_id)) {
864 $sql =
"SELECT t.rowid, t.account_number, t.label as account_label";
865 $sql .=
" FROM ".$this->db->prefix().
"accounting_account as t";
866 $sql .=
" WHERE t.fk_accounting_category = ".((int) $cat_id);
867 $sql .=
" AND t.entity = ".$conf->entity;
868 $sql .=
" AND t.active = 1";
869 $sql .=
" AND t.fk_pcg_version = '".$this->db->escape($pcgvercode).
"'";
870 $sql .=
" ORDER BY t.account_number";
872 $sql =
"SELECT t.rowid, t.account_number, t.label as account_label";
873 $sql .=
" FROM ".$this->db->prefix().
"accounting_account as t";
874 $sql .=
" WHERE ".$predefinedgroupwhere;
875 $sql .=
" AND t.entity = ".$conf->entity;
876 $sql .=
' AND t.active = 1';
877 $sql .=
" AND t.fk_pcg_version = '".$this->db->escape($pcgvercode).
"'";
878 $sql .=
" ORDER BY t.account_number";
881 $resql = $this->db->query($sql);
885 $num = $this->db->num_rows($resql);
888 while ($obj = $this->db->fetch_object($resql)) {
891 'account_number' => $obj->account_number,
892 'account_label' => $obj->account_label,
899 $this->error =
"Error ".$this->db->lasterror();
900 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
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...
__construct($db)
Constructor.
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 show result of an accounting account from the ledger with a direction and a period.
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.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
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...