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((
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);
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();
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;
450 $this->lines_display = array();
453 $resql = $this->db->query($sql);
455 $num = $this->db->num_rows($resql);
457 while ($obj = $this->db->fetch_object($resql)) {
458 $this->lines_display[] = $obj;
463 $this->error =
"Error ".$this->db->lasterror();
464 $this->errors[] = $this->error;
465 dol_syslog(__METHOD__.
" ".implode(
',', $this->errors), LOG_ERR);
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";
491 $this->lines_cptbk = array();
494 $resql = $this->db->query($sql);
496 $num = $this->db->num_rows($resql);
498 while ($obj = $this->db->fetch_object($resql)) {
499 $this->lines_cptbk[] = $obj;
505 $this->error =
"Error ".$this->db->lasterror();
506 $this->errors[] = $this->error;
507 dol_syslog(__METHOD__.
" ".implode(
',', $this->errors), LOG_ERR);
526 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
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;";
539 $resql = $this->db->query($sql);
542 $this->errors[] =
"Error ".$this->db->lasterror();
543 $this->db->rollback();
547 $accountincptsadded = array();
548 while ($obj = $this->db->fetch_object($resql)) {
550 if (!empty($accountincptsadded[$account_number_formated])) {
554 if (array_key_exists($account_number_formated, $cpts)) {
555 $accountincptsadded[$account_number_formated] = 1;
557 $sql =
"UPDATE ".$this->db->prefix().
"accounting_account";
558 $sql .=
" SET fk_accounting_category=".((int) $id_cat);
559 $sql .=
" WHERE rowid=".((int) $obj->rowid);
561 $resqlupdate = $this->db->query($sql);
564 $this->errors[] =
"Error ".$this->db->lasterror();
571 foreach ($this->errors as $errmsg) {
573 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
575 $this->db->rollback();
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);
602 $resql = $this->db->query($sql);
605 $this->errors[] =
"Error ".$this->db->lasterror();
610 foreach ($this->errors as $errmsg) {
612 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
614 $this->db->rollback();
636 public function getSumDebitCredit($cpt, $date_start, $date_end, $sens, $thirdparty_code =
'nofilter', $month = 0, $year = 0)
641 $this->sdcpermonth = array();
645 if (is_array($cpt)) {
646 foreach ($cpt as $cptcursor) {
647 if (! is_null($cptcursor)) {
648 if ($listofaccount) {
649 $listofaccount .=
",";
651 $listofaccount .=
"'".$cptcursor.
"'";
654 if (empty($listofaccount)) {
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";
664 $sql .=
" FROM ".$this->db->prefix().
"accounting_bookkeeping as t";
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).
")";
670 $sql .=
" AND t.numero_compte = '".$this->db->escape((
string) $cpt).
"'";
672 if (!empty($date_start) && !empty($date_end) && (empty($month) || empty($year))) {
673 $sql .=
" AND (t.doc_date BETWEEN '".$this->db->idate($date_start).
"' AND '".$this->db->idate($date_end).
"')";
675 if (!empty($month) && !empty($year)) {
678 if ($thirdparty_code !=
'nofilter') {
679 $sql .=
" AND t.thirdparty_code = '".$this->db->escape($thirdparty_code).
"'";
681 if (is_array($cpt)) {
682 $sql .=
" GROUP BY t.numero_compte";
685 $resql = $this->db->query($sql);
687 $num = $this->db->num_rows($resql);
691 $obj = $this->db->fetch_object($resql);
694 $this->sdc = $obj->debit - $obj->credit;
696 $this->sdc = $obj->credit - $obj->debit;
698 if (is_array($cpt)) {
699 $this->sdcperaccount[$obj->accountancy_account] = $this->sdc;
708 $this->error =
"Error ".$this->db->lasterror();
709 $this->errors[] = $this->error;
710 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
724 global $mysoc,
$conf;
726 if (empty($mysoc->country_id)) {
727 $this->error =
"Error ".$this->db->lasterror();
728 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
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;
743 $sql .=
" AND cat.rowid = ".((int) $catid);
745 $sql .=
" ORDER BY cat.position ASC";
747 $resql = $this->db->query($sql);
750 $num = $this->db->num_rows($resql);
753 while ($obj = $this->db->fetch_object($resql)) {
754 $name_cat = $obj->name_cat;
755 $data[$name_cat][$obj->rowid] = array(
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,
764 'account_number' => $obj->account_number,
765 'account_label' => $obj->account_label
771 $this->error =
"Error ".$this->db->lasterror();
772 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
787 public function getCats($categorytype = -1, $active = 1, $id_report = 1)
789 global
$conf, $mysoc;
791 if (empty($mysoc->country_id)) {
792 dol_print_error(
null,
'Call to getCats with mysoc country not yet defined');
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";
804 $sql .=
" AND (c.fk_country = ".((int) $mysoc->country_id).
" OR c.fk_country = 0)";
805 $sql .=
" ORDER BY c.position ASC";
807 $resql = $this->db->query($sql);
811 $num = $this->db->num_rows($resql);
815 $obj = $this->db->fetch_object($resql);
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,
832 $this->error =
"Error ".$this->db->lasterror();
833 $this->errors[] = $this->error;
834 dol_syslog(__METHOD__.
" ".implode(
',', $this->errors), LOG_ERR);
852 public function getCptsCat($cat_id, $predefinedgroupwhere =
'')
854 global
$conf, $mysoc;
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');
863 $pcgvercode =
dol_getIdFromCode($this->db, (
string) $pcgverid,
'accounting_system',
'rowid',
'pcg_version');
864 if (empty($pcgvercode)) {
865 $pcgvercode = $pcgverid;
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";
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";
886 $resql = $this->db->query($sql);
890 $num = $this->db->num_rows($resql);
893 while ($obj = $this->db->fetch_object($resql)) {
896 'account_number' => $obj->account_number,
897 'account_label' => $obj->account_label,
904 $this->error =
"Error ".$this->db->lasterror();
905 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 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.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
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...