dolibarr  9.0.0
accountingaccount.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
3  * Copyright (C) 2013-2016 Alexandre Spangaro <aspangaro@zendsi.com>
4  * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
5  * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
6  * Copyright (C) 2015 Ari Elbaz (elarifr) <github@accedinfo.com>
7  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
33 {
37  public $element='accounting_account';
38 
42  public $table_element='accounting_account';
43 
47  public $picto = 'billr';
48 
53  public $ismultientitymanaged = 1;
54 
59  public $restrictiononfksoc = 1;
60 
64  public $db;
65 
69  public $id;
70 
74  public $rowid;
75 
79  public $datec;
80 
84  public $fk_pcg_version;
85 
89  public $pcg_type;
90 
94  public $pcg_subtype;
95 
99  public $account_number;
100 
104  public $account_parent;
105 
109  public $account_category;
110 
114  public $status;
115 
119  public $label;
120 
124  public $fk_user_author;
125 
129  public $fk_user_modif;
130 
134  public $active;
135 
141  function __construct($db)
142  {
143  global $conf;
144 
145  $this->db = $db;
146  $this->next_prev_filter='fk_pcg_version IN (SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS . ')'; // Used to add a filter in Form::showrefnav method
147  }
148 
157  function fetch($rowid = null, $account_number = null, $limittocurrentchart = 0)
158  {
159  global $conf;
160 
161  if ($rowid || $account_number) {
162  $sql = "SELECT a.rowid as rowid, a.datec, a.tms, a.fk_pcg_version, a.pcg_type, a.pcg_subtype, a.account_number, a.account_parent, a.label, a.fk_accounting_category, a.fk_user_author, a.fk_user_modif, a.active";
163  $sql .= ", ca.label as category_label";
164  $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as a";
165  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_accounting_category as ca ON a.fk_accounting_category = ca.rowid";
166  $sql .= " WHERE";
167  if ($rowid) {
168  $sql .= " a.rowid = " . (int) $rowid;
169  } elseif ($account_number) {
170  $sql .= " a.account_number = '" . $this->db->escape($account_number) . "'";
171  }
172  if (! empty($limittocurrentchart)) {
173  $sql .= ' AND a.fk_pcg_version IN (SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $this->db->escape($conf->global->CHARTOFACCOUNTS) . ')';
174  }
175 
176  dol_syslog(get_class($this) . "::fetch sql=" . $sql, LOG_DEBUG);
177  $result = $this->db->query($sql);
178  if ($result) {
179  $obj = $this->db->fetch_object($result);
180 
181  if ($obj) {
182  $this->id = $obj->rowid;
183  $this->rowid = $obj->rowid;
184  $this->ref = $obj->account_number;
185  $this->datec = $obj->datec;
186  $this->tms = $obj->tms;
187  $this->fk_pcg_version = $obj->fk_pcg_version;
188  $this->pcg_type = $obj->pcg_type;
189  $this->pcg_subtype = $obj->pcg_subtype;
190  $this->account_number = $obj->account_number;
191  $this->account_parent = $obj->account_parent;
192  $this->label = $obj->label;
193  $this->account_category = $obj->fk_accounting_category;
194  $this->account_category_label = $obj->category_label;
195  $this->fk_user_author = $obj->fk_user_author;
196  $this->fk_user_modif = $obj->fk_user_modif;
197  $this->active = $obj->active;
198  $this->status = $obj->active;
199 
200  return $this->id;
201  } else {
202  return 0;
203  }
204  } else {
205  $this->error = "Error " . $this->db->lasterror();
206  $this->errors[] = "Error " . $this->db->lasterror();
207  }
208  }
209  return -1;
210  }
211 
219  function create($user, $notrigger = 0)
220  {
221  global $conf;
222  $error = 0;
223  $now = dol_now();
224 
225  // Clean parameters
226  if (isset($this->fk_pcg_version))
227  $this->fk_pcg_version = trim($this->fk_pcg_version);
228  if (isset($this->pcg_type))
229  $this->pcg_type = trim($this->pcg_type);
230  if (isset($this->pcg_subtype))
231  $this->pcg_subtype = trim($this->pcg_subtype);
232  if (isset($this->account_number))
233  $this->account_number = trim($this->account_number);
234  if (isset($this->account_parent))
235  $this->account_parent = trim($this->account_parent);
236  if (isset($this->label))
237  $this->label = trim($this->label);
238  if (isset($this->account_category))
239  $this->account_category = trim($this->account_category);
240  if (isset($this->fk_user_author))
241  $this->fk_user_author = trim($this->fk_user_author);
242  if (isset($this->active))
243  $this->active = trim($this->active);
244 
245  if (empty($this->pcg_type) || $this->pcg_type == '-1')
246  {
247  $this->pcg_type = 'XXXXXX';
248  }
249  if (empty($this->pcg_subtype) || $this->pcg_subtype == '-1')
250  {
251  $this->pcg_subtype = 'XXXXXX';
252  }
253  // Check parameters
254  // Put here code to add control on parameters values
255 
256  // Insert request
257  $sql = "INSERT INTO " . MAIN_DB_PREFIX . "accounting_account(";
258  $sql .= "datec";
259  $sql .= ", entity";
260  $sql .= ", fk_pcg_version";
261  $sql .= ", pcg_type";
262  $sql .= ", pcg_subtype";
263  $sql .= ", account_number";
264  $sql .= ", account_parent";
265  $sql .= ", label";
266  $sql .= ", fk_accounting_category";
267  $sql .= ", fk_user_author";
268  $sql .= ", active";
269  $sql .= ") VALUES (";
270  $sql .= " '" . $this->db->idate($now) . "'";
271  $sql .= ", " . $conf->entity;
272  $sql .= ", " . (empty($this->fk_pcg_version) ? 'NULL' : "'" . $this->db->escape($this->fk_pcg_version) . "'");
273  $sql .= ", " . (empty($this->pcg_type) ? 'NULL' : "'" . $this->db->escape($this->pcg_type) . "'");
274  $sql .= ", " . (empty($this->pcg_subtype) ? 'NULL' : "'" . $this->db->escape($this->pcg_subtype) . "'");
275  $sql .= ", " . (empty($this->account_number) ? 'NULL' : "'" . $this->db->escape($this->account_number) . "'");
276  $sql .= ", " . (empty($this->account_parent) ? 0 : (int) $this->account_parent);
277  $sql .= ", " . (empty($this->label) ? "''" : "'" . $this->db->escape($this->label) . "'");
278  $sql .= ", " . (empty($this->account_category) ? 0 : (int) $this->account_category);
279  $sql .= ", " . $user->id;
280  $sql .= ", " . (int) $this->active;
281  $sql .= ")";
282 
283  $this->db->begin();
284 
285  dol_syslog(get_class($this) . "::create sql=" . $sql, LOG_DEBUG);
286  $resql = $this->db->query($sql);
287  if (! $resql) {
288  $error++;
289  $this->errors[] = "Error " . $this->db->lasterror();
290  }
291 
292  if (! $error) {
293  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "accounting_account");
294 
295  // if (! $notrigger) {
296  // Uncomment this and change MYOBJECT to your own tag if you
297  // want this action calls a trigger.
298 
299  // // Call triggers
300  // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
301  // $interface=new Interfaces($this->db);
302  // $result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
303  // if ($result < 0) { $error++; $this->errors=$interface->errors; }
304  // // End call triggers
305  // }
306  }
307 
308  // Commit or rollback
309  if ($error) {
310  foreach ($this->errors as $errmsg) {
311  dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
312  $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
313  }
314  $this->db->rollback();
315  return -1 * $error;
316  } else {
317  $this->db->commit();
318  return $this->id;
319  }
320  }
321 
328  function update($user)
329  {
330  // Check parameters
331  if (empty($this->pcg_type) || $this->pcg_type == '-1')
332  {
333  $this->pcg_type = 'XXXXXX';
334  }
335  if (empty($this->pcg_subtype) || $this->pcg_subtype == '-1')
336  {
337  $this->pcg_subtype = 'XXXXXX';
338  }
339 
340  $this->db->begin();
341 
342  $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account ";
343  $sql .= " SET fk_pcg_version = " . ($this->fk_pcg_version ? "'" . $this->db->escape($this->fk_pcg_version) . "'" : "null");
344  $sql .= " , pcg_type = " . ($this->pcg_type ? "'" . $this->db->escape($this->pcg_type) . "'" : "null");
345  $sql .= " , pcg_subtype = " . ($this->pcg_subtype ? "'" . $this->db->escape($this->pcg_subtype) . "'" : "null");
346  $sql .= " , account_number = '" . $this->db->escape($this->account_number) . "'";
347  $sql .= " , account_parent = " . (int) $this->account_parent;
348  $sql .= " , label = " . ($this->label ? "'" . $this->db->escape($this->label) . "'" : "''");
349  $sql .= " , fk_accounting_category = " . (empty($this->account_category) ? 0 : (int) $this->account_category);
350  $sql .= " , fk_user_modif = " . $user->id;
351  $sql .= " , active = " . (int) $this->active;
352  $sql .= " WHERE rowid = " . $this->id;
353 
354  dol_syslog(get_class($this) . "::update sql=" . $sql, LOG_DEBUG);
355  $result = $this->db->query($sql);
356  if ($result) {
357  $this->db->commit();
358  return 1;
359  } else {
360  $this->error = $this->db->lasterror();
361  $this->db->rollback();
362  return - 1;
363  }
364  }
365 
371  function checkUsage()
372  {
373  global $langs;
374 
375  $sql = "(SELECT fk_code_ventilation FROM " . MAIN_DB_PREFIX . "facturedet";
376  $sql.= " WHERE fk_code_ventilation=" . $this->id . ")";
377  $sql.= "UNION";
378  $sql.= " (SELECT fk_code_ventilation FROM " . MAIN_DB_PREFIX . "facture_fourn_det";
379  $sql.= " WHERE fk_code_ventilation=" . $this->id . ")";
380 
381  dol_syslog(get_class($this) . "::checkUsage sql=" . $sql, LOG_DEBUG);
382  $resql = $this->db->query($sql);
383 
384  if ($resql) {
385  $num = $this->db->num_rows($resql);
386  if ($num > 0) {
387  $this->error = $langs->trans('ErrorAccountancyCodeIsAlreadyUse');
388  return 0;
389  } else {
390  return 1;
391  }
392  } else {
393  $this->error = $this->db->lasterror();
394  return - 1;
395  }
396  }
397 
405  function delete($user, $notrigger = 0)
406  {
407  $error = 0;
408 
409  $result = $this->checkUsage();
410 
411  if ($result > 0) {
412 
413  $this->db->begin();
414 
415  // if (! $error) {
416  // if (! $notrigger) {
417  // Uncomment this and change MYOBJECT to your own tag if you
418  // want this action calls a trigger.
419 
420  // // Call triggers
421  // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
422  // $interface=new Interfaces($this->db);
423  // $result=$interface->run_triggers('ACCOUNTANCY_ACCOUNT_DELETE',$this,$user,$langs,$conf);
424  // if ($result < 0) { $error++; $this->errors=$interface->errors; }
425  // // End call triggers
426  // }
427  // }
428 
429  if (! $error) {
430  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "accounting_account";
431  $sql .= " WHERE rowid=" . $this->id;
432 
433  dol_syslog(get_class($this) . "::delete sql=" . $sql);
434  $resql = $this->db->query($sql);
435  if (! $resql) {
436  $error ++;
437  $this->errors[] = "Error " . $this->db->lasterror();
438  }
439  }
440 
441  // Commit or rollback
442  if ($error) {
443  foreach ( $this->errors as $errmsg ) {
444  dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
445  $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
446  }
447  $this->db->rollback();
448  return - 1 * $error;
449  } else {
450  $this->db->commit();
451  return 1;
452  }
453  } else {
454  return - 1;
455  }
456  }
457 
469  function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle='',$notooltip=0, $save_lastsearch_value=-1)
470  {
471  global $langs, $conf, $user;
472  require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
473 
474  if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
475 
476  $result = '';
477 
478  $url = DOL_URL_ROOT . '/accountancy/admin/card.php?id=' . $this->id;
479 
480  // Add param to save lastsearch_values or not
481  $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
482  if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
483  if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
484 
485  $picto = 'billr';
486  $label='';
487 
488  $label = '<u>' . $langs->trans("ShowAccountingAccount") . '</u>';
489  if (! empty($this->account_number))
490  $label .= '<br><b>'.$langs->trans('AccountAccounting') . ':</b> ' . length_accountg($this->account_number);
491  if (! empty($this->label))
492  $label .= '<br><b>'.$langs->trans('Label') . ':</b> ' . $this->label;
493  if ($moretitle) $label.=' - '.$moretitle;
494 
495  $linkclose='';
496  if (empty($notooltip))
497  {
498  if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
499  {
500  $label=$langs->trans("ShowAccoutingAccount");
501  $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
502  }
503  $linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"';
504  $linkclose.=' class="classfortooltip"';
505  }
506 
507  $linkstart='<a href="'.$url.'"';
508  $linkstart.=$linkclose.'>';
509  $linkend='</a>';
510 
511  if ($nourl)
512  {
513  $linkstart = '';
514  $linkclose = '';
515  $linkend = '';
516  }
517 
518  $label_link = length_accountg($this->account_number);
519  if ($withlabel) $label_link .= ' - ' . $this->label;
520 
521  if ($withpicto) $result.=($linkstart.img_object(($notooltip?'':$label), $picto, ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend);
522  if ($withpicto && $withpicto != 2) $result .= ' ';
523  if ($withpicto != 2) $result.=$linkstart . $label_link . $linkend;
524  return $result;
525  }
526 
533  function info($id)
534  {
535  $sql = 'SELECT a.rowid, a.datec, a.fk_user_author, a.fk_user_modif, a.tms';
536  $sql .= ' FROM ' . MAIN_DB_PREFIX . 'accounting_account as a';
537  $sql .= ' WHERE a.rowid = ' . $id;
538 
539  dol_syslog(get_class($this) . '::info sql=' . $sql);
540  $result = $this->db->query($sql);
541 
542  if ($result) {
543  if ($this->db->num_rows($result)) {
544  $obj = $this->db->fetch_object($result);
545  $this->id = $obj->rowid;
546  if ($obj->fk_user_author) {
547  $cuser = new User($this->db);
548  $cuser->fetch($obj->fk_user_author);
549  $this->user_creation = $cuser;
550  }
551  if ($obj->fk_user_modif) {
552  $muser = new User($this->db);
553  $muser->fetch($obj->fk_user_modif);
554  $this->user_modification = $muser;
555  }
556  $this->date_creation = $this->db->jdate($obj->datec);
557  $this->date_modification = $this->db->jdate($obj->tms);
558  }
559  $this->db->free($result);
560  } else {
561  dol_print_error($this->db);
562  }
563  }
564 
565  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
572  function account_desactivate($id)
573  {
574  // phpcs:enable
575  $result = $this->checkUsage();
576 
577  if ($result > 0) {
578  $this->db->begin();
579 
580  $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account ";
581  $sql .= "SET active = '0'";
582  $sql .= " WHERE rowid = " . $this->db->escape($id);
583 
584  dol_syslog(get_class($this) . "::desactivate sql=" . $sql, LOG_DEBUG);
585  $result = $this->db->query($sql);
586 
587  if ($result) {
588  $this->db->commit();
589  return 1;
590  } else {
591  $this->error = $this->db->lasterror();
592  $this->db->rollback();
593  return - 1;
594  }
595  } else {
596  return - 1;
597  }
598  }
599 
600  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
607  function account_activate($id)
608  {
609  // phpcs:enable
610  $this->db->begin();
611 
612  $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account ";
613  $sql .= "SET active = '1'";
614  $sql .= " WHERE rowid = " . $this->db->escape($id);
615 
616  dol_syslog(get_class($this) . "::activate sql=" . $sql, LOG_DEBUG);
617  $result = $this->db->query($sql);
618  if ($result) {
619  $this->db->commit();
620  return 1;
621  } else {
622  $this->error = $this->db->lasterror();
623  $this->db->rollback();
624  return - 1;
625  }
626  }
627 
628 
635  function getLibStatut($mode=0)
636  {
637  return $this->LibStatut($this->status,$mode);
638  }
639 
640  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
648  function LibStatut($statut,$mode=0)
649  {
650  // phpcs:enable
651  global $langs;
652  $langs->loadLangs(array("users"));
653 
654  if ($mode == 0)
655  {
656  $prefix='';
657  if ($statut == 1) return $langs->trans('Enabled');
658  if ($statut == 0) return $langs->trans('Disabled');
659  }
660  elseif ($mode == 1)
661  {
662  if ($statut == 1) return $langs->trans('Enabled');
663  if ($statut == 0) return $langs->trans('Disabled');
664  }
665  elseif ($mode == 2)
666  {
667  if ($statut == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
668  if ($statut == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
669  }
670  elseif ($mode == 3)
671  {
672  if ($statut == 1) return img_picto($langs->trans('Enabled'),'statut4');
673  if ($statut == 0) return img_picto($langs->trans('Disabled'),'statut5');
674  }
675  elseif ($mode == 4)
676  {
677  if ($statut == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
678  if ($statut == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
679  }
680  elseif ($mode == 5)
681  {
682  if ($statut == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
683  if ($statut == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
684  }
685  }
686 }
print $object label
hash of file content (md5_file(dol_osencode($destfull))
Definition: edit.php:153
__construct($db)
Constructor.
LibStatut($statut, $mode=0)
Renvoi le libelle d&#39;un statut donne.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
info($id)
Information on record.
fetch($rowid=null, $account_number=null, $limittocurrentchart=0)
Load record in memory.
getNomUrl($withpicto=0, $withlabel=0, $nourl=0, $moretitle='', $notooltip=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Class to manage Dolibarr users.
Definition: user.class.php:41
account_activate($id)
Account activated.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
create($user, $notrigger=0)
Insert new accounting account in chart of accounts.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
update($user)
Update record.
checkUsage()
Check usage of accounting code.
getLibStatut($mode=0)
Retourne le libelle du statut d&#39;un user (actif, inactif)
dol_now($mode='gmt')
Return date for now.
Class to manage accounting accounts.
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous) ...
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
account_desactivate($id)
Account deactivated.