27require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
47 public $errors = array();
52 public $element =
'c_accounting_category';
57 public $table_element =
'c_accounting_category';
83 public $range_account;
93 public $category_type;
123 public $lines_display;
150 public function create($user, $notrigger = 0)
152 global $conf, $langs;
156 if (isset($this->code)) {
157 $this->code = trim($this->code);
159 if (isset($this->label)) {
160 $this->label = trim($this->label);
162 if (isset($this->range_account)) {
163 $this->range_account = trim($this->range_account);
165 if (isset($this->sens)) {
166 $this->sens = (int) $this->sens;
168 if (isset($this->category_type)) {
169 $this->category_type = (int) $this->category_type;
171 if (isset($this->formula)) {
172 $this->formula = trim($this->formula);
177 if (isset($this->fk_country)) {
178 $this->fk_country = (int) $this->fk_country;
180 if (isset($this->active)) {
181 $this->active = (int) $this->active;
188 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"c_accounting_category(";
189 if ($this->
rowid > 0) {
194 $sql .=
"range_account, ";
196 $sql .=
"category_type, ";
198 $sql .=
"position, ";
199 $sql .=
"fk_country, ";
202 $sql .=
") VALUES (";
203 if ($this->
rowid > 0) {
204 $sql .=
" ".((int) $this->
rowid).
",";
206 $sql .=
" ".(!isset($this->code) ?
'NULL' :
"'".$this->db->escape($this->code).
"'").
",";
207 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
208 $sql .=
" ".(!isset($this->range_account) ?
'NULL' :
"'".$this->db->escape($this->range_account).
"'").
",";
209 $sql .=
" ".(!isset($this->sens) ?
'NULL' :
"'".$this->db->escape($this->sens).
"'").
",";
210 $sql .=
" ".(!isset($this->category_type) ?
'NULL' :
"'".$this->db->escape($this->category_type).
"'").
",";
211 $sql .=
" ".(!isset($this->formula) ?
'NULL' :
"'".$this->db->escape($this->formula).
"'").
",";
212 $sql .=
" ".(!isset($this->
position) ?
'NULL' : ((int) $this->
position)).
",";
213 $sql .=
" ".(!isset($this->fk_country) ?
'NULL' : ((int) $this->fk_country)).
",";
214 $sql .=
" ".(!isset($this->active) ?
'NULL' : ((int) $this->active));
215 $sql .=
", ".((int) $conf->entity);
220 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
221 $resql = $this->db->query($sql);
223 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
228 foreach ($this->errors as $errmsg) {
229 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
230 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
232 $this->db->rollback();
249 public function fetch($id, $code =
'', $label =
'')
255 $sql .=
" t.range_account,";
257 $sql .=
" t.category_type,";
258 $sql .=
" t.formula,";
259 $sql .=
" t.position,";
260 $sql .=
" t.fk_country,";
262 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_accounting_category as t";
264 $sql .=
" WHERE t.rowid = ".((int) $id);
266 $sql .=
" WHERE t.entity IN (".getEntity(
'c_accounting_category').
")";
268 $sql .=
" AND t.code = '".$this->db->escape($code).
"'";
270 $sql .=
" AND t.label = '".$this->db->escape($label).
"'";
274 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
275 $resql = $this->db->query($sql);
277 if ($this->db->num_rows($resql)) {
278 $obj = $this->db->fetch_object($resql);
280 $this->
id = $obj->rowid;
281 $this->code = $obj->code;
282 $this->label = $obj->label;
283 $this->range_account = $obj->range_account;
284 $this->sens = $obj->sens;
285 $this->category_type = $obj->category_type;
286 $this->formula = $obj->formula;
288 $this->fk_country = $obj->fk_country;
289 $this->active = $obj->active;
291 $this->db->free($resql);
295 $this->error =
"Error ".$this->db->lasterror();
308 public function update($user =
null, $notrigger = 0)
310 global $conf, $langs;
314 if (isset($this->code)) {
315 $this->code = trim($this->code);
317 if (isset($this->label)) {
318 $this->label = trim($this->label);
320 if (isset($this->range_account)) {
321 $this->range_account = trim($this->range_account);
323 if (isset($this->sens)) {
324 $this->sens = (int) $this->sens;
326 if (isset($this->category_type)) {
327 $this->category_type = (int) $this->category_type;
329 if (isset($this->formula)) {
330 $this->formula = trim($this->formula);
335 if (isset($this->fk_country)) {
336 $this->fk_country = (int) $this->fk_country;
338 if (isset($this->active)) {
339 $this->active = (int) $this->active;
347 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"c_accounting_category SET";
348 $sql .=
" code=".(isset($this->code) ?
"'".$this->db->escape($this->code).
"'" :
"null").
",";
349 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
350 $sql .=
" range_account=".(isset($this->range_account) ?
"'".$this->db->escape($this->range_account).
"'" :
"null").
",";
351 $sql .=
" sens=".(isset($this->sens) ? $this->sens :
"null").
",";
352 $sql .=
" category_type=".(isset($this->category_type) ? $this->category_type :
"null").
",";
353 $sql .=
" formula=".(isset($this->formula) ?
"'".$this->db->escape($this->formula).
"'" :
"null").
",";
354 $sql .=
" position=".(isset($this->
position) ? $this->
position :
"null").
",";
355 $sql .=
" fk_country=".(isset($this->fk_country) ? $this->fk_country :
"null").
",";
356 $sql .=
" active=".(isset($this->active) ? $this->active :
"null");
357 $sql .=
" WHERE rowid=".((int) $this->
id);
361 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
362 $resql = $this->db->query($sql);
364 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
369 foreach ($this->errors as $errmsg) {
370 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
371 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
373 $this->db->rollback();
389 public function delete($user, $notrigger = 0)
391 global $conf, $langs;
394 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"c_accounting_category";
395 $sql .=
" WHERE rowid=".((int) $this->
id);
399 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
400 $resql = $this->db->query($sql);
402 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
407 foreach ($this->errors as $errmsg) {
408 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
409 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
411 $this->db->rollback();
429 $sql =
"SELECT t.rowid, t.account_number, t.label";
430 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_account as t";
431 $sql .=
" WHERE t.fk_accounting_category = ".((int) $id);
432 $sql .=
" AND t.entity = ".$conf->entity;
434 $this->lines_display = array();
437 $resql = $this->db->query($sql);
439 $num = $this->db->num_rows($resql);
441 while ($obj = $this->db->fetch_object($resql)) {
442 $this->lines_display[] = $obj;
447 $this->error =
"Error ".$this->db->lasterror();
448 $this->errors[] = $this->error;
449 dol_syslog(__METHOD__.
" ".implode(
',', $this->errors), LOG_ERR);
465 $sql =
"SELECT aa.account_number as numero_compte, aa.label as label_compte";
466 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_account as aa";
467 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
468 $sql .=
" WHERE (aa.fk_accounting_category <> ".((int) $id).
" OR aa.fk_accounting_category IS NULL)";
469 $sql .=
" AND asy.rowid = ".((int)
getDolGlobalInt(
'CHARTOFACCOUNTS'));
470 $sql .=
" AND aa.active = 1";
471 $sql .=
" AND aa.entity = ".$conf->entity;
472 $sql .=
" GROUP BY aa.account_number, aa.label";
473 $sql .=
" ORDER BY aa.account_number, aa.label";
475 $this->lines_cptbk = array();
478 $resql = $this->db->query($sql);
480 $num = $this->db->num_rows($resql);
482 while ($obj = $this->db->fetch_object($resql)) {
483 $this->lines_cptbk[] = $obj;
489 $this->error =
"Error ".$this->db->lasterror();
490 $this->errors[] = $this->error;
491 dol_syslog(__METHOD__.
" ".implode(
',', $this->errors), LOG_ERR);
510 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
512 $sql =
"SELECT aa.rowid, aa.account_number";
513 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_account as aa";
514 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
515 $sql .=
" AND asy.rowid = ".((int)
getDolGlobalInt(
'CHARTOFACCOUNTS'));
516 $sql .=
" AND aa.active = 1";
517 $sql .=
" AND aa.entity = ".$conf->entity;
518 $sql .=
" ORDER BY LENGTH(aa.account_number) DESC;";
523 $resql = $this->db->query($sql);
526 $this->errors[] =
"Error ".$this->db->lasterror();
527 $this->db->rollback();
531 $accountincptsadded = array();
532 while ($obj = $this->db->fetch_object($resql)) {
534 if (!empty($accountincptsadded[$account_number_formated])) {
538 if (array_key_exists($account_number_formated, $cpts)) {
539 $accountincptsadded[$account_number_formated] = 1;
541 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"accounting_account";
542 $sql .=
" SET fk_accounting_category=".((int) $id_cat);
543 $sql .=
" WHERE rowid=".((int) $obj->rowid);
545 $resqlupdate = $this->db->query($sql);
548 $this->errors[] =
"Error ".$this->db->lasterror();
555 foreach ($this->errors as $errmsg) {
557 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
559 $this->db->rollback();
580 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"accounting_account as aa";
581 $sql .=
" SET fk_accounting_category= 0";
582 $sql .=
" WHERE aa.rowid = ".((int) $cpt_id);
586 $resql = $this->db->query($sql);
589 $this->errors[] =
"Error ".$this->db->lasterror();
594 foreach ($this->errors as $errmsg) {
596 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
598 $this->db->rollback();
620 public function getSumDebitCredit($cpt, $date_start, $date_end, $sens, $thirdparty_code =
'nofilter', $month = 0, $year = 0)
625 $this->sdcpermonth = array();
627 if (is_array($cpt)) {
629 foreach ($cpt as $cptcursor) {
630 if (! is_null($cptcursor)) {
631 if ($listofaccount) {
632 $listofaccount .=
",";
634 $listofaccount .=
"'".$cptcursor.
"'";
637 if (empty($listofaccount)) {
643 $sql =
"SELECT SUM(t.debit) as debit, SUM(t.credit) as credit";
644 if (is_array($cpt)) {
645 $sql .=
", t.numero_compte as accountancy_account";
647 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as t";
649 $sql .=
" WHERE t.entity = ".((int) $conf->entity);
650 if (is_array($cpt)) {
651 $sql .=
" AND t.numero_compte IN (".$this->db->sanitize($listofaccount, 1).
")";
653 $sql .=
" AND t.numero_compte = '".$this->db->escape($cpt).
"'";
655 if (!empty($date_start) && !empty($date_end) && (empty($month) || empty($year))) {
656 $sql .=
" AND (t.doc_date BETWEEN '".$this->db->idate($date_start).
"' AND '".$this->db->idate($date_end).
"')";
658 if (!empty($month) && !empty($year)) {
661 if ($thirdparty_code !=
'nofilter') {
662 $sql .=
" AND t.thirdparty_code = '".$this->db->escape($thirdparty_code).
"'";
664 if (is_array($cpt)) {
665 $sql .=
" GROUP BY t.numero_compte";
668 $resql = $this->db->query($sql);
670 $num = $this->db->num_rows($resql);
674 $obj = $this->db->fetch_object($resql);
677 $this->sdc = $obj->debit - $obj->credit;
679 $this->sdc = $obj->credit - $obj->debit;
681 if (is_array($cpt)) {
682 $this->sdcperaccount[$obj->accountancy_account] = $this->sdc;
691 $this->error =
"Error ".$this->db->lasterror();
692 $this->errors[] = $this->error;
693 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
707 global $mysoc, $conf;
709 if (empty($mysoc->country_id)) {
710 $this->error =
"Error ".$this->db->lasterror();
711 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
715 $sql =
"SELECT t.rowid, t.account_number, t.label as account_label,";
716 $sql .=
" cat.code, cat.position, cat.label as name_cat, cat.sens, cat.category_type, cat.formula";
717 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_account as t, ".MAIN_DB_PREFIX.
"c_accounting_category as cat";
718 $sql .=
" WHERE t.fk_accounting_category IN (SELECT c.rowid";
719 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_accounting_category as c";
720 $sql .=
" WHERE c.active = 1";
721 $sql .=
" AND c.entity = ".$conf->entity;
722 $sql .=
" AND (c.fk_country = ".((int) $mysoc->country_id).
" OR c.fk_country = 0)";
723 $sql .=
" AND cat.rowid = t.fk_accounting_category";
724 $sql .=
" AND t.entity = ".$conf->entity;
726 $sql .=
" AND cat.rowid = ".((int) $catid);
728 $sql .=
" ORDER BY cat.position ASC";
730 $resql = $this->db->query($sql);
733 $num = $this->db->num_rows($resql);
736 while ($obj = $this->db->fetch_object($resql)) {
737 $name_cat = $obj->name_cat;
738 $data[$name_cat][$obj->rowid] = array(
740 'code' => $obj->code,
741 'label' => $obj->label,
742 'position' => $obj->position,
743 'category_type' => $obj->category_type,
744 'formula' => $obj->formula,
745 'sens' => $obj->sens,
746 'account_number' => $obj->account_number,
747 'account_label' => $obj->account_label
753 $this->error =
"Error ".$this->db->lasterror();
754 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
768 public function getCats($categorytype = -1, $active = 1)
770 global $conf, $mysoc;
772 if (empty($mysoc->country_id)) {
773 dol_print_error(
'',
'Call to select_accounting_account with mysoc country not yet defined');
777 $sql =
"SELECT c.rowid, c.code, c.label, c.formula, c.position, c.category_type, c.sens";
778 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_accounting_category as c";
779 $sql .=
" WHERE c.active = " . (int) $active;
780 $sql .=
" AND c.entity = ".$conf->entity;
781 if ($categorytype >= 0) {
782 $sql .=
" AND c.category_type = 1";
784 $sql .=
" AND (c.fk_country = ".((int) $mysoc->country_id).
" OR c.fk_country = 0)";
785 $sql .=
" ORDER BY c.position ASC";
787 $resql = $this->db->query($sql);
791 $num = $this->db->num_rows($resql);
795 $obj = $this->db->fetch_object($resql);
798 'rowid' => $obj->rowid,
799 'code' => $obj->code,
800 'label' => $obj->label,
801 'position' => $obj->position,
802 'category_type' => $obj->category_type,
803 'formula' => $obj->formula,
804 'sens' => $obj->sens,
812 $this->error =
"Error ".$this->db->lasterror();
813 $this->errors[] = $this->error;
814 dol_syslog(__METHOD__.
" ".implode(
',', $this->errors), LOG_ERR);
832 public function getCptsCat($cat_id, $predefinedgroupwhere =
'')
834 global $conf, $mysoc;
837 if (empty($mysoc->country_id) && empty($mysoc->country_code)) {
838 dol_print_error(
'',
'Call to select_accounting_account with mysoc country not yet defined');
843 $pcgvercode =
dol_getIdFromCode($this->db, $pcgverid,
'accounting_system',
'rowid',
'pcg_version');
844 if (empty($pcgvercode)) {
845 $pcgvercode = $pcgverid;
848 if (!empty($cat_id)) {
849 $sql =
"SELECT t.rowid, t.account_number, t.label as account_label";
850 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_account as t";
851 $sql .=
" WHERE t.fk_accounting_category = ".((int) $cat_id);
852 $sql .=
" AND t.entity = ".$conf->entity;
853 $sql .=
" AND t.active = 1";
854 $sql .=
" AND t.fk_pcg_version = '".$this->db->escape($pcgvercode).
"'";
855 $sql .=
" ORDER BY t.account_number";
857 $sql =
"SELECT t.rowid, t.account_number, t.label as account_label";
858 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_account as t";
859 $sql .=
" WHERE ".$predefinedgroupwhere;
860 $sql .=
" AND t.entity = ".$conf->entity;
861 $sql .=
' AND t.active = 1';
862 $sql .=
" AND t.fk_pcg_version = '".$this->db->escape($pcgvercode).
"'";
863 $sql .=
" ORDER BY t.account_number";
866 $resql = $this->db->query($sql);
870 $num = $this->db->num_rows($resql);
873 while ($obj = $this->db->fetch_object($resql)) {
876 'account_number' => $obj->account_number,
877 'account_label' => $obj->account_label,
884 $this->error =
"Error ".$this->db->lasterror();
885 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).
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.
getCats($categorytype=-1, $active=1)
Return list of custom groups.
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.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalInt($key, $default=0)
Return 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_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall right right takeposterminal SELECT e rowid