dolibarr  13.0.2
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-2020 Alexandre Spangaro <aspangaro@open-dsi.fr>
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 <https://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 
81  public $datec;
82 
86  public $fk_pcg_version;
87 
91  public $pcg_type;
92 
96  public $account_number;
97 
101  public $account_parent;
102 
106  public $account_category;
107 
111  public $status;
112 
116  public $label;
117 
121  public $labelshort;
122 
126  public $fk_user_author;
127 
131  public $fk_user_modif;
132 
136  public $active;
137 
141  public $reconcilable;
142 
148  public function __construct($db)
149  {
150  global $conf;
151 
152  $this->db = $db;
153  $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
154  }
155 
165  public function fetch($rowid = null, $account_number = null, $limittocurrentchart = 0, $limittoachartaccount = '')
166  {
167  global $conf;
168 
169  if ($rowid || $account_number) {
170  $sql = "SELECT a.rowid as rowid, a.datec, a.tms, a.fk_pcg_version, a.pcg_type, a.account_number, a.account_parent, a.label, a.labelshort, a.fk_accounting_category, a.fk_user_author, a.fk_user_modif, a.active, a.reconcilable";
171  $sql .= ", ca.label as category_label";
172  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_account as a";
173  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_accounting_category as ca ON a.fk_accounting_category = ca.rowid";
174  $sql .= " WHERE";
175  if ($rowid) {
176  $sql .= " a.rowid = ".(int) $rowid;
177  } elseif ($account_number) {
178  $sql .= " a.account_number = '".$this->db->escape($account_number)."'";
179  $sql .= " AND a.entity = ".$conf->entity;
180  }
181  if (!empty($limittocurrentchart)) {
182  $sql .= ' AND a.fk_pcg_version IN (SELECT pcg_version FROM '.MAIN_DB_PREFIX.'accounting_system WHERE rowid='.$this->db->escape($conf->global->CHARTOFACCOUNTS).')';
183  }
184  if (!empty($limittoachartaccount)) {
185  $sql .= " AND a.fk_pcg_version = '".$this->db->escape($limittoachartaccount)."'";
186  }
187 
188  dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
189  $result = $this->db->query($sql);
190  if ($result) {
191  $obj = $this->db->fetch_object($result);
192 
193  if ($obj) {
194  $this->id = $obj->rowid;
195  $this->rowid = $obj->rowid;
196  $this->ref = $obj->account_number;
197  $this->datec = $obj->datec;
198  $this->tms = $obj->tms;
199  $this->fk_pcg_version = $obj->fk_pcg_version;
200  $this->pcg_type = $obj->pcg_type;
201  $this->account_number = $obj->account_number;
202  $this->account_parent = $obj->account_parent;
203  $this->label = $obj->label;
204  $this->labelshort = $obj->labelshort;
205  $this->account_category = $obj->fk_accounting_category;
206  $this->account_category_label = $obj->category_label;
207  $this->fk_user_author = $obj->fk_user_author;
208  $this->fk_user_modif = $obj->fk_user_modif;
209  $this->active = $obj->active;
210  $this->status = $obj->active;
211  $this->reconcilable = $obj->reconcilable;
212 
213  return $this->id;
214  } else {
215  return 0;
216  }
217  } else {
218  $this->error = "Error ".$this->db->lasterror();
219  $this->errors[] = "Error ".$this->db->lasterror();
220  }
221  }
222  return -1;
223  }
224 
232  public function create($user, $notrigger = 0)
233  {
234  global $conf;
235  $error = 0;
236  $now = dol_now();
237 
238  // Clean parameters
239  if (isset($this->fk_pcg_version))
240  $this->fk_pcg_version = trim($this->fk_pcg_version);
241  if (isset($this->pcg_type))
242  $this->pcg_type = trim($this->pcg_type);
243  if (isset($this->account_number))
244  $this->account_number = trim($this->account_number);
245  if (isset($this->label))
246  $this->label = trim($this->label);
247  if (isset($this->labelshort))
248  $this->labelshort = trim($this->labelshort);
249 
250  if (empty($this->pcg_type) || $this->pcg_type == '-1')
251  {
252  $this->pcg_type = 'XXXXXX';
253  }
254  // Check parameters
255  // Put here code to add control on parameters values
256 
257  // Insert request
258  $sql = "INSERT INTO ".MAIN_DB_PREFIX."accounting_account(";
259  $sql .= "datec";
260  $sql .= ", entity";
261  $sql .= ", fk_pcg_version";
262  $sql .= ", pcg_type";
263  $sql .= ", account_number";
264  $sql .= ", account_parent";
265  $sql .= ", label";
266  $sql .= ", labelshort";
267  $sql .= ", fk_accounting_category";
268  $sql .= ", fk_user_author";
269  $sql .= ", active";
270  $sql .= ", reconcilable";
271  $sql .= ") VALUES (";
272  $sql .= " '".$this->db->idate($now)."'";
273  $sql .= ", ".$conf->entity;
274  $sql .= ", ".(empty($this->fk_pcg_version) ? 'NULL' : "'".$this->db->escape($this->fk_pcg_version)."'");
275  $sql .= ", ".(empty($this->pcg_type) ? 'NULL' : "'".$this->db->escape($this->pcg_type)."'");
276  $sql .= ", ".(empty($this->account_number) ? 'NULL' : "'".$this->db->escape($this->account_number)."'");
277  $sql .= ", ".(empty($this->account_parent) ? 0 : (int) $this->account_parent);
278  $sql .= ", ".(empty($this->label) ? "''" : "'".$this->db->escape($this->label)."'");
279  $sql .= ", ".(empty($this->labelshort) ? "''" : "'".$this->db->escape($this->labelshort)."'");
280  $sql .= ", ".(empty($this->account_category) ? 0 : (int) $this->account_category);
281  $sql .= ", ".$user->id;
282  $sql .= ", ".(int) $this->active;
283  $sql .= ", ".(int) $this->reconcilable;
284  $sql .= ")";
285 
286  $this->db->begin();
287 
288  dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
289  $resql = $this->db->query($sql);
290  if (!$resql) {
291  $error++;
292  $this->errors[] = "Error ".$this->db->lasterror();
293  }
294 
295  if (!$error) {
296  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."accounting_account");
297 
298  // Uncomment this and change MYOBJECT to your own tag if you
299  // want this action to call a trigger.
300  //if (! $error && ! $notrigger) {
301 
302  // // Call triggers
303  // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
304  // if ($result < 0) $error++;
305  // // End call triggers
306  //}
307  }
308 
309  // Commit or rollback
310  if ($error) {
311  foreach ($this->errors as $errmsg) {
312  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
313  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
314  }
315  $this->db->rollback();
316  return -1 * $error;
317  } else {
318  $this->db->commit();
319  return $this->id;
320  }
321  }
322 
329  public function update($user)
330  {
331  // Check parameters
332  if (empty($this->pcg_type) || $this->pcg_type == '-1')
333  {
334  $this->pcg_type = 'XXXXXX';
335  }
336 
337  $this->db->begin();
338 
339  $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_account ";
340  $sql .= " SET fk_pcg_version = ".($this->fk_pcg_version ? "'".$this->db->escape($this->fk_pcg_version)."'" : "null");
341  $sql .= " , pcg_type = ".($this->pcg_type ? "'".$this->db->escape($this->pcg_type)."'" : "null");
342  $sql .= " , account_number = '".$this->db->escape($this->account_number)."'";
343  $sql .= " , account_parent = ".(int) $this->account_parent;
344  $sql .= " , label = ".($this->label ? "'".$this->db->escape($this->label)."'" : "''");
345  $sql .= " , labelshort = ".($this->labelshort ? "'".$this->db->escape($this->labelshort)."'" : "''");
346  $sql .= " , fk_accounting_category = ".(empty($this->account_category) ? 0 : (int) $this->account_category);
347  $sql .= " , fk_user_modif = ".$user->id;
348  $sql .= " , active = ".(int) $this->active;
349  $sql .= " , reconcilable = ".(int) $this->reconcilable;
350  $sql .= " WHERE rowid = ".$this->id;
351 
352  dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
353  $result = $this->db->query($sql);
354  if ($result) {
355  $this->db->commit();
356  return 1;
357  } else {
358  $this->error = $this->db->lasterror();
359  $this->db->rollback();
360  return -1;
361  }
362  }
363 
369  public function checkUsage()
370  {
371  global $langs;
372 
373  $sql = "(SELECT fk_code_ventilation FROM ".MAIN_DB_PREFIX."facturedet";
374  $sql .= " WHERE fk_code_ventilation=".$this->id.")";
375  $sql .= "UNION";
376  $sql .= " (SELECT fk_code_ventilation FROM ".MAIN_DB_PREFIX."facture_fourn_det";
377  $sql .= " WHERE fk_code_ventilation=".$this->id.")";
378 
379  dol_syslog(get_class($this)."::checkUsage sql=".$sql, LOG_DEBUG);
380  $resql = $this->db->query($sql);
381 
382  if ($resql) {
383  $num = $this->db->num_rows($resql);
384  if ($num > 0) {
385  $this->error = $langs->trans('ErrorAccountancyCodeIsAlreadyUse');
386  return 0;
387  } else {
388  return 1;
389  }
390  } else {
391  $this->error = $this->db->lasterror();
392  return -1;
393  }
394  }
395 
403  public function delete($user, $notrigger = 0)
404  {
405  $error = 0;
406 
407  $result = $this->checkUsage();
408 
409  if ($result > 0) {
410  $this->db->begin();
411 
412  if (!$error) {
413  $sql = "DELETE FROM ".MAIN_DB_PREFIX."accounting_account";
414  $sql .= " WHERE rowid=".$this->id;
415 
416  dol_syslog(get_class($this)."::delete sql=".$sql);
417  $resql = $this->db->query($sql);
418  if (!$resql) {
419  $error++;
420  $this->errors[] = "Error ".$this->db->lasterror();
421  }
422  }
423 
424  // Commit or rollback
425  if ($error) {
426  foreach ($this->errors as $errmsg) {
427  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
428  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
429  }
430  $this->db->rollback();
431  return -1 * $error;
432  } else {
433  $this->db->commit();
434  return 1;
435  }
436  } else {
437  return -1;
438  }
439  }
440 
454  public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle = '', $notooltip = 0, $save_lastsearch_value = -1, $withcompletelabel = 0, $option = '')
455  {
456  global $langs, $conf;
457  require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
458 
459  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
460 
461  $result = '';
462 
463  $url = ''; $labelurl = '';
464  if (empty($option) || $option == 'ledger') {
465  $url = DOL_URL_ROOT.'/accountancy/bookkeeping/listbyaccount.php?search_accountancy_code_start='.urlencode($this->account_number).'&search_accountancy_code_end='.urlencode($this->account_number);
466  $labelurl = $langs->trans("ShowAccountingAccountInLedger");
467  } elseif ($option == 'journals') {
468  $url = DOL_URL_ROOT.'/accountancy/bookkeeping/list.php?search_accountancy_code_start='.urlencode($this->account_number).'&search_accountancy_code_end='.urlencode($this->account_number);
469  $labelurl = $langs->trans("ShowAccountingAccountInJournals");
470  } elseif ($option == 'accountcard') {
471  $url = DOL_URL_ROOT.'/accountancy/admin/card.php?id='.urlencode($this->id);
472  $labelurl = $langs->trans("ShowAccountingAccount");
473  }
474 
475  // Add param to save lastsearch_values or not
476  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
477  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
478  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
479 
480  $picto = 'billr';
481  $label = '';
482 
483  if (empty($this->labelshort) || $withcompletelabel == 1)
484  {
485  $labeltoshow = $this->label;
486  } else {
487  $labeltoshow = $this->labelshort;
488  }
489 
490  $label = '<u>'.$labelurl.'</u>';
491  if (!empty($this->account_number))
492  $label .= '<br><b>'.$langs->trans('AccountAccounting').':</b> '.length_accountg($this->account_number);
493  if (!empty($labeltoshow))
494  $label .= '<br><b>'.$langs->trans('Label').':</b> '.$labeltoshow;
495  if ($moretitle) $label .= ' - '.$moretitle;
496 
497  $linkclose = '';
498  if (empty($notooltip))
499  {
500  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
501  {
502  $label = $labelurl;
503  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
504  }
505  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
506  $linkclose .= ' class="classfortooltip"';
507  }
508 
509  $linkstart = '<a href="'.$url.'"';
510  $linkstart .= $linkclose.'>';
511  $linkend = '</a>';
512 
513  if ($nourl)
514  {
515  $linkstart = '';
516  $linkclose = '';
517  $linkend = '';
518  }
519 
520  $label_link = length_accountg($this->account_number);
521  if ($withlabel) $label_link .= ' - '.($nourl ? '<span class="opacitymedium">' : '').$labeltoshow.($nourl ? '</span>' : '');
522 
523  if ($withpicto) $result .= ($linkstart.img_object(($notooltip ? '' : $label), $picto, ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
524  if ($withpicto && $withpicto != 2) $result .= ' ';
525  if ($withpicto != 2) $result .= $linkstart.$label_link.$linkend;
526  return $result;
527  }
528 
535  public function info($id)
536  {
537  $sql = 'SELECT a.rowid, a.datec, a.fk_user_author, a.fk_user_modif, a.tms';
538  $sql .= ' FROM '.MAIN_DB_PREFIX.'accounting_account as a';
539  $sql .= ' WHERE a.rowid = '.$id;
540 
541  dol_syslog(get_class($this).'::info sql='.$sql);
542  $result = $this->db->query($sql);
543 
544  if ($result) {
545  if ($this->db->num_rows($result)) {
546  $obj = $this->db->fetch_object($result);
547  $this->id = $obj->rowid;
548  if ($obj->fk_user_author) {
549  $cuser = new User($this->db);
550  $cuser->fetch($obj->fk_user_author);
551  $this->user_creation = $cuser;
552  }
553  if ($obj->fk_user_modif) {
554  $muser = new User($this->db);
555  $muser->fetch($obj->fk_user_modif);
556  $this->user_modification = $muser;
557  }
558  $this->date_creation = $this->db->jdate($obj->datec);
559  $this->date_modification = $this->db->jdate($obj->tms);
560  }
561  $this->db->free($result);
562  } else {
563  dol_print_error($this->db);
564  }
565  }
566 
574  public function accountDeactivate($id, $mode = 0)
575  {
576  $result = $this->checkUsage();
577 
578  $fieldtouse = 'active';
579  if ($mode == 1)
580  {
581  $fieldtouse = 'reconcilable';
582  }
583 
584  if ($result > 0) {
585  $this->db->begin();
586 
587  $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_account ";
588  $sql .= "SET ".$fieldtouse." = '0'";
589  $sql .= " WHERE rowid = ".$this->db->escape($id);
590 
591  dol_syslog(get_class($this)."::accountDeactivate ".$fieldtouse." sql=".$sql, LOG_DEBUG);
592  $result = $this->db->query($sql);
593 
594  if ($result) {
595  $this->db->commit();
596  return 1;
597  } else {
598  $this->error = $this->db->lasterror();
599  $this->db->rollback();
600  return -1;
601  }
602  } else {
603  return -1;
604  }
605  }
606 
607  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
615  public function account_activate($id, $mode = 0)
616  {
617  // phpcs:enable
618  $this->db->begin();
619 
620  $fieldtouse = 'active';
621  if ($mode == 1) {
622  $fieldtouse = 'reconcilable';
623  }
624 
625  $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_account";
626  $sql .= " SET ".$fieldtouse." = '1'";
627  $sql .= " WHERE rowid = ".$this->db->escape($id);
628 
629  dol_syslog(get_class($this)."::account_activate ".$fieldtouse." sql=".$sql, LOG_DEBUG);
630  $result = $this->db->query($sql);
631  if ($result) {
632  $this->db->commit();
633  return 1;
634  } else {
635  $this->error = $this->db->lasterror();
636  $this->db->rollback();
637  return -1;
638  }
639  }
640 
641 
648  public function getLibStatut($mode = 0)
649  {
650  return $this->LibStatut($this->status, $mode);
651  }
652 
653  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
661  public function LibStatut($status, $mode = 0)
662  {
663  // phpcs:enable
664  global $langs;
665  $langs->loadLangs(array("users"));
666 
667  if ($mode == 0)
668  {
669  if ($status == 1) return $langs->trans('Enabled');
670  elseif ($status == 0) return $langs->trans('Disabled');
671  } elseif ($mode == 1)
672  {
673  if ($status == 1) return $langs->trans('Enabled');
674  elseif ($status == 0) return $langs->trans('Disabled');
675  } elseif ($mode == 2)
676  {
677  if ($status == 1) return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
678  elseif ($status == 0) return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
679  } elseif ($mode == 3)
680  {
681  if ($status == 1) return img_picto($langs->trans('Enabled'), 'statut4');
682  elseif ($status == 0) return img_picto($langs->trans('Disabled'), 'statut5');
683  } elseif ($mode == 4)
684  {
685  if ($status == 1) return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
686  elseif ($status == 0) return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
687  } elseif ($mode == 5)
688  {
689  if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
690  elseif ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
691  }
692  }
693 }
__construct($db)
Constructor.
info($id)
Information on record.
getNomUrl($withpicto=0, $withlabel=0, $nourl=0, $moretitle= '', $notooltip=0, $save_lastsearch_value=-1, $withcompletelabel=0, $option= '')
Return clicable name (with picto eventually)
</td >< tdcolspan="3">< spanclass="opacitymedium"></span ></td ></tr >< trclass="liste_total"> CREANCES DETTES< tdcolspan="3"class="right"></td >< tdcolspan="3"class="right"></td ></tr > CREANCES DETTES RECETTES DEPENSES trips CREANCES DETTES Y m expensereport p date_valid Y m expensereport pe datep $db idate($date_start)."' AND $column < p rowid
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
$conf db
API class for accounts.
Definition: inc.php:54
create($user, $notrigger=0)
Insert new accounting account in chart of accounts.
account_activate($id, $mode=0)
Account activated.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
update($user)
Update record.
checkUsage()
Check usage of accounting code.
LibStatut($status, $mode=0)
Renvoi le libelle d&#39;un statut donne.
getLibStatut($mode=0)
Retourne le libelle du statut d&#39;un user (actif, inactif)
print $_SERVER["PHP_SELF"]
Edit parameters.
accountDeactivate($id, $mode=0)
Deactivate an account (for status active or status reconcilable)
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
fetch($rowid=null, $account_number=null, $limittocurrentchart=0, $limittoachartaccount= '')
Load record in memory.
Class to manage accounting accounts.
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous) ...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)