dolibarr  7.0.0-beta
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  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
32 {
33  public $element='accounting_account';
34  public $table_element='accounting_account';
35  public $picto = 'billr';
36 
41  public $ismultientitymanaged = 1;
46  public $restrictiononfksoc = 1;
47 
48  var $db;
49  var $error;
50  var $errors;
51  var $id;
52  var $rowid;
53  var $datec; // Creation date
54  var $fk_pcg_version;
55  var $pcg_type;
56  var $pcg_subtype;
57  var $account_number;
58  var $account_parent;
59  var $account_category;
60  var $label;
61  var $fk_user_author;
62  var $fk_user_modif;
63  var $active; // duplicate with status
64  var $status;
65 
66 
72  function __construct($db) {
73  global $conf;
74 
75  $this->db = $db;
76  $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
77  }
78 
87  function fetch($rowid = null, $account_number = null, $limittocurrentchart = 0) {
88  global $conf;
89 
90  if ($rowid || $account_number) {
91  $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";
92  $sql .= ", ca.label as category_label";
93  $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as a";
94  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_accounting_category as ca ON a.fk_accounting_category = ca.rowid";
95  $sql .= " WHERE";
96  if ($rowid) {
97  $sql .= " a.rowid = '" . $rowid . "'";
98  } elseif ($account_number) {
99  $sql .= " a.account_number = '" . $account_number . "'";
100  }
101  if (! empty($limittocurrentchart)) {
102  $sql .= ' AND a.fk_pcg_version IN (SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS . ')';
103  }
104 
105  dol_syslog(get_class($this) . "::fetch sql=" . $sql, LOG_DEBUG);
106  $result = $this->db->query($sql);
107  if ($result) {
108  $obj = $this->db->fetch_object($result);
109 
110  if ($obj) {
111  $this->id = $obj->rowid;
112  $this->rowid = $obj->rowid;
113  $this->ref = $obj->account_number;
114  $this->datec = $obj->datec;
115  $this->tms = $obj->tms;
116  $this->fk_pcg_version = $obj->fk_pcg_version;
117  $this->pcg_type = $obj->pcg_type;
118  $this->pcg_subtype = $obj->pcg_subtype;
119  $this->account_number = $obj->account_number;
120  $this->account_parent = $obj->account_parent;
121  $this->label = $obj->label;
122  $this->account_category = $obj->fk_accounting_category;
123  $this->account_category_label = $obj->category_label;
124  $this->fk_user_author = $obj->fk_user_author;
125  $this->fk_user_modif = $obj->fk_user_modif;
126  $this->active = $obj->active;
127  $this->status = $obj->active;
128 
129  return $this->id;
130  } else {
131  return 0;
132  }
133  } else {
134  $this->error = "Error " . $this->db->lasterror();
135  $this->errors[] = "Error " . $this->db->lasterror();
136  }
137  }
138  return - 1;
139  }
140 
148  function create($user, $notrigger = 0) {
149  global $conf;
150  $error = 0;
151  $now = dol_now();
152 
153  // Clean parameters
154  if (isset($this->fk_pcg_version))
155  $this->fk_pcg_version = trim($this->fk_pcg_version);
156  if (isset($this->pcg_type))
157  $this->pcg_type = trim($this->pcg_type);
158  if (isset($this->pcg_subtype))
159  $this->pcg_subtype = trim($this->pcg_subtype);
160  if (isset($this->account_number))
161  $this->account_number = trim($this->account_number);
162  if (isset($this->account_parent))
163  $this->account_parent = trim($this->account_parent);
164  if (isset($this->label))
165  $this->label = trim($this->label);
166  if (isset($this->account_category))
167  $this->account_category = trim($this->account_category);
168  if (isset($this->fk_user_author))
169  $this->fk_user_author = trim($this->fk_user_author);
170  if (isset($this->active))
171  $this->active = trim($this->active);
172 
173  if (empty($this->pcg_type) || $this->pcg_type == '-1')
174  {
175  $this->pcg_type = 'XXXXXX';
176  }
177  if (empty($this->pcg_subtype) || $this->pcg_subtype == '-1')
178  {
179  $this->pcg_subtype = 'XXXXXX';
180  }
181  // Check parameters
182  // Put here code to add control on parameters values
183 
184  // Insert request
185  $sql = "INSERT INTO " . MAIN_DB_PREFIX . "accounting_account(";
186  $sql .= "datec";
187  $sql .= ", entity";
188  $sql .= ", fk_pcg_version";
189  $sql .= ", pcg_type";
190  $sql .= ", pcg_subtype";
191  $sql .= ", account_number";
192  $sql .= ", account_parent";
193  $sql .= ", label";
194  $sql .= ", fk_accounting_category";
195  $sql .= ", fk_user_author";
196  $sql .= ", active";
197  $sql .= ") VALUES (";
198  $sql .= " '" . $this->db->idate($now) . "'";
199  $sql .= ", " . $conf->entity;
200  $sql .= ", " . (empty($this->fk_pcg_version) ? 'NULL' : "'" . $this->db->escape($this->fk_pcg_version) . "'");
201  $sql .= ", " . (empty($this->pcg_type) ? 'NULL' : "'" . $this->db->escape($this->pcg_type) . "'");
202  $sql .= ", " . (empty($this->pcg_subtype) ? 'NULL' : "'" . $this->db->escape($this->pcg_subtype) . "'");
203  $sql .= ", " . (empty($this->account_number) ? 'NULL' : "'" . $this->db->escape($this->account_number) . "'");
204  $sql .= ", " . (empty($this->account_parent) ? 'NULL' : "'" . $this->db->escape($this->account_parent) . "'");
205  $sql .= ", " . (empty($this->label) ? 'NULL' : "'" . $this->db->escape($this->label) . "'");
206  $sql .= ", " . (empty($this->account_category) ? 'NULL' : "'" . $this->db->escape($this->account_category) . "'");
207  $sql .= ", " . $user->id;
208  $sql .= ", " . (! isset($this->active) ? 'NULL' : $this->db->escape($this->active));
209  $sql .= ")";
210 
211  $this->db->begin();
212 
213  dol_syslog(get_class($this) . "::create sql=" . $sql, LOG_DEBUG);
214  $resql = $this->db->query($sql);
215  if (! $resql) {
216  $error ++;
217  $this->errors[] = "Error " . $this->db->lasterror();
218  }
219 
220  if (! $error) {
221  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "accounting_account");
222 
223  // if (! $notrigger) {
224  // Uncomment this and change MYOBJECT to your own tag if you
225  // want this action calls a trigger.
226 
227  // // Call triggers
228  // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
229  // $interface=new Interfaces($this->db);
230  // $result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
231  // if ($result < 0) { $error++; $this->errors=$interface->errors; }
232  // // End call triggers
233  // }
234  }
235 
236  // Commit or rollback
237  if ($error) {
238  foreach ( $this->errors as $errmsg ) {
239  dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
240  $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
241  }
242  $this->db->rollback();
243  return - 1 * $error;
244  } else {
245  $this->db->commit();
246  return $this->id;
247  }
248  }
249 
256  function update($user)
257  {
258  // Check parameters
259  if (empty($this->pcg_type) || $this->pcg_type == '-1')
260  {
261  $this->pcg_type = 'XXXXXX';
262  }
263  if (empty($this->pcg_subtype) || $this->pcg_subtype == '-1')
264  {
265  $this->pcg_subtype = 'XXXXXX';
266  }
267 
268  $this->db->begin();
269 
270  $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account ";
271  $sql .= " SET fk_pcg_version = " . ($this->fk_pcg_version ? "'" . $this->db->escape($this->fk_pcg_version) . "'" : "null");
272  $sql .= " , pcg_type = " . ($this->pcg_type ? "'" . $this->db->escape($this->pcg_type) . "'" : "null");
273  $sql .= " , pcg_subtype = " . ($this->pcg_subtype ? "'" . $this->db->escape($this->pcg_subtype) . "'" : "null");
274  $sql .= " , account_number = '" . $this->db->escape($this->account_number) . "'";
275  $sql .= " , account_parent = '" . $this->db->escape($this->account_parent) . "'";
276  $sql .= " , label = " . ($this->label ? "'" . $this->db->escape($this->label) . "'" : "null");
277  $sql .= " , fk_accounting_category = '" . $this->db->escape($this->account_category) . "'";
278  $sql .= " , fk_user_modif = " . $user->id;
279  $sql .= " , active = " . $this->active;
280  $sql .= " WHERE rowid = " . $this->id;
281 
282  dol_syslog(get_class($this) . "::update sql=" . $sql, LOG_DEBUG);
283  $result = $this->db->query($sql);
284  if ($result) {
285  $this->db->commit();
286  return 1;
287  } else {
288  $this->error = $this->db->lasterror();
289  $this->db->rollback();
290  return - 1;
291  }
292  }
293 
299  function checkUsage() {
300  global $langs;
301 
302  $sql = "(SELECT fk_code_ventilation FROM " . MAIN_DB_PREFIX . "facturedet";
303  $sql .= " WHERE fk_code_ventilation=" . $this->id . ")";
304  $sql .= "UNION";
305  $sql .= "(SELECT fk_code_ventilation FROM " . MAIN_DB_PREFIX . "facture_fourn_det";
306  $sql .= " WHERE fk_code_ventilation=" . $this->id . ")";
307 
308  dol_syslog(get_class($this) . "::checkUsage sql=" . $sql, LOG_DEBUG);
309  $resql = $this->db->query($sql);
310 
311  if ($resql) {
312  $num = $this->db->num_rows($resql);
313  if ($num > 0) {
314  $this->error = $langs->trans('ErrorAccountancyCodeIsAlreadyUse');
315  return 0;
316  } else {
317  return 1;
318  }
319  } else {
320  $this->error = $this->db->lasterror();
321  return - 1;
322  }
323  }
324 
332  function delete($user, $notrigger = 0) {
333  $error = 0;
334 
335  $result = $this->checkUsage();
336 
337  if ($result > 0) {
338 
339  $this->db->begin();
340 
341  // if (! $error) {
342  // if (! $notrigger) {
343  // Uncomment this and change MYOBJECT to your own tag if you
344  // want this action calls a trigger.
345 
346  // // Call triggers
347  // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
348  // $interface=new Interfaces($this->db);
349  // $result=$interface->run_triggers('ACCOUNTANCY_ACCOUNT_DELETE',$this,$user,$langs,$conf);
350  // if ($result < 0) { $error++; $this->errors=$interface->errors; }
351  // // End call triggers
352  // }
353  // }
354 
355  if (! $error) {
356  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "accounting_account";
357  $sql .= " WHERE rowid=" . $this->id;
358 
359  dol_syslog(get_class($this) . "::delete sql=" . $sql);
360  $resql = $this->db->query($sql);
361  if (! $resql) {
362  $error ++;
363  $this->errors[] = "Error " . $this->db->lasterror();
364  }
365  }
366 
367  // Commit or rollback
368  if ($error) {
369  foreach ( $this->errors as $errmsg ) {
370  dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
371  $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
372  }
373  $this->db->rollback();
374  return - 1 * $error;
375  } else {
376  $this->db->commit();
377  return 1;
378  }
379  } else {
380  return - 1;
381  }
382  }
383 
394  function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle='',$notooltip=0)
395  {
396  global $langs, $conf, $user;
397  require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
398 
399  if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
400 
401  $result = '';
402 
403  $url = DOL_URL_ROOT . '/accountancy/admin/card.php?id=' . $this->id;
404 
405  $picto = 'billr';
406  $label='';
407 
408  $label = '<u>' . $langs->trans("ShowAccountingAccount") . '</u>';
409  if (! empty($this->account_number))
410  $label .= '<br><b>'.$langs->trans('AccountAccounting') . ':</b> ' . length_accountg($this->account_number);
411  if (! empty($this->label))
412  $label .= '<br><b>'.$langs->trans('Label') . ':</b> ' . $this->label;
413  if ($moretitle) $label.=' - '.$moretitle;
414 
415  $linkclose='';
416  if (empty($notooltip))
417  {
418  if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
419  {
420  $label=$langs->trans("ShowAccoutingAccount");
421  $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
422  }
423  $linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"';
424  $linkclose.=' class="classfortooltip"';
425  }
426 
427  $linkstart='<a href="'.$url.'"';
428  $linkstart.=$linkclose.'>';
429  $linkend='</a>';
430 
431  if ($nourl)
432  {
433  $linkstart = '';
434  $linkclose = '';
435  $linkend = '';
436  }
437 
438  $label_link = length_accountg($this->account_number);
439  if ($withlabel) $label_link .= ' - ' . $this->label;
440 
441  if ($withpicto) $result.=($linkstart.img_object(($notooltip?'':$label), $picto, ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend);
442  if ($withpicto && $withpicto != 2) $result .= ' ';
443  if ($withpicto != 2) $result.=$linkstart . $label_link . $linkend;
444  return $result;
445  }
446 
453  function info($id) {
454  $sql = 'SELECT a.rowid, a.datec, a.fk_user_author, a.fk_user_modif, a.tms';
455  $sql .= ' FROM ' . MAIN_DB_PREFIX . 'accounting_account as a';
456  $sql .= ' WHERE a.rowid = ' . $id;
457 
458  dol_syslog(get_class($this) . '::info sql=' . $sql);
459  $result = $this->db->query($sql);
460 
461  if ($result) {
462  if ($this->db->num_rows($result)) {
463  $obj = $this->db->fetch_object($result);
464  $this->id = $obj->rowid;
465  if ($obj->fk_user_author) {
466  $cuser = new User($this->db);
467  $cuser->fetch($obj->fk_user_author);
468  $this->user_creation = $cuser;
469  }
470  if ($obj->fk_user_modif) {
471  $muser = new User($this->db);
472  $muser->fetch($obj->fk_user_modif);
473  $this->user_modification = $muser;
474  }
475  $this->date_creation = $this->db->jdate($obj->datec);
476  $this->date_modification = $this->db->jdate($obj->tms);
477  }
478  $this->db->free($result);
479  } else {
480  dol_print_error($this->db);
481  }
482  }
483 
490  function account_desactivate($id) {
491  $result = $this->checkUsage();
492 
493  if ($result > 0) {
494  $this->db->begin();
495 
496  $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account ";
497  $sql .= "SET active = '0'";
498  $sql .= " WHERE rowid = " . $this->db->escape($id);
499 
500  dol_syslog(get_class($this) . "::desactivate sql=" . $sql, LOG_DEBUG);
501  $result = $this->db->query($sql);
502 
503  if ($result) {
504  $this->db->commit();
505  return 1;
506  } else {
507  $this->error = $this->db->lasterror();
508  $this->db->rollback();
509  return - 1;
510  }
511  } else {
512  return - 1;
513  }
514  }
515 
522  function account_activate($id) {
523  $this->db->begin();
524 
525  $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account ";
526  $sql .= "SET active = '1'";
527  $sql .= " WHERE rowid = " . $this->db->escape($id);
528 
529  dol_syslog(get_class($this) . "::activate sql=" . $sql, LOG_DEBUG);
530  $result = $this->db->query($sql);
531  if ($result) {
532  $this->db->commit();
533  return 1;
534  } else {
535  $this->error = $this->db->lasterror();
536  $this->db->rollback();
537  return - 1;
538  }
539  }
540 
541 
548  function getLibStatut($mode=0)
549  {
550  return $this->LibStatut($this->status,$mode);
551  }
552 
560  function LibStatut($statut,$mode=0)
561  {
562  global $langs;
563  $langs->load('users');
564 
565  if ($mode == 0)
566  {
567  $prefix='';
568  if ($statut == 1) return $langs->trans('Enabled');
569  if ($statut == 0) return $langs->trans('Disabled');
570  }
571  if ($mode == 1)
572  {
573  if ($statut == 1) return $langs->trans('Enabled');
574  if ($statut == 0) return $langs->trans('Disabled');
575  }
576  if ($mode == 2)
577  {
578  if ($statut == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
579  if ($statut == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
580  }
581  if ($mode == 3)
582  {
583  if ($statut == 1) return img_picto($langs->trans('Enabled'),'statut4');
584  if ($statut == 0) return img_picto($langs->trans('Disabled'),'statut5');
585  }
586  if ($mode == 4)
587  {
588  if ($statut == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
589  if ($statut == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
590  }
591  if ($mode == 5)
592  {
593  if ($statut == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
594  if ($statut == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
595  }
596  }
597 }
__construct($db)
Constructor.
LibStatut($statut, $mode=0)
Renvoi le libelle d'un statut donne.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
info($id)
Information on record.
fetch($rowid=null, $account_number=null, $limittocurrentchart=0)
Load record in memory.
Class to manage Dolibarr users.
Definition: user.class.php:39
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.
getNomUrl($withpicto=0, $withlabel=0, $nourl=0, $moretitle='', $notooltip=0)
Return clicable name (with picto eventually)
getLibStatut($mode=0)
Retourne le libelle du statut d'un user (actif, inactif)
dol_now($mode='gmt')
Return date for now.
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:1013
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, ...)
account_desactivate($id)
Account deactivated.