dolibarr  13.0.2
bookkeeping.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2014-2017 Olivier Geffroy <jeff@jeffinfo.com>
3  * Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
4  * Copyright (C) 2015-2017 Florian Henry <florian.henry@open-concept.pro>
5  * Copyright (C) 2018-2020 Frédéric France <frederic.france@netlogic.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 // Class
28 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
29 
34 {
38  public $element = 'accountingbookkeeping';
39 
43  public $table_element = 'accounting_bookkeeping';
44 
48  public $entity;
49 
53  public $lines = array();
54 
58  public $id;
59 
63  public $doc_date;
64 
68  public $date_lim_reglement;
69 
73  public $doc_type;
74 
78  public $doc_ref;
79 
83  public $fk_doc;
84 
88  public $fk_docdet;
89 
93  public $thirdparty_code;
94 
98  public $subledger_account;
99 
103  public $subledger_label;
104 
108  public $numero_compte;
109 
113  public $label_compte;
114 
118  public $label_operation;
119 
123  public $debit;
124 
128  public $credit;
129 
134  public $montant;
135 
140  public $amount;
141 
145  public $sens;
146 
150  public $fk_user_author;
151 
155  public $import_key;
156 
160  public $code_journal;
161 
165  public $journal_label;
166 
170  public $piece_num;
171 
175  public $picto = 'generic';
176 
177 
183  public function __construct(DoliDB $db)
184  {
185  $this->db = $db;
186  }
187 
195  public function create(User $user, $notrigger = false)
196  {
197  global $conf, $langs;
198 
199  dol_syslog(__METHOD__, LOG_DEBUG);
200 
201  $error = 0;
202 
203  // Clean parameters
204  if (isset($this->doc_type)) {
205  $this->doc_type = trim($this->doc_type);
206  }
207  if (isset($this->doc_ref)) {
208  $this->doc_ref = trim($this->doc_ref);
209  }
210  if (isset($this->fk_doc)) {
211  $this->fk_doc = (int) $this->fk_doc;
212  }
213  if (isset($this->fk_docdet)) {
214  $this->fk_docdet = (int) $this->fk_docdet;
215  }
216  if (isset($this->thirdparty_code)) {
217  $this->thirdparty_code = trim($this->thirdparty_code);
218  }
219  if (isset($this->subledger_account)) {
220  $this->subledger_account = trim($this->subledger_account);
221  }
222  if (isset($this->subledger_label)) {
223  $this->subledger_label = trim($this->subledger_label);
224  }
225  if (isset($this->numero_compte)) {
226  $this->numero_compte = trim($this->numero_compte);
227  }
228  if (isset($this->label_compte)) {
229  $this->label_compte = trim($this->label_compte);
230  }
231  if (isset($this->label_operation)) {
232  $this->label_operation = trim($this->label_operation);
233  }
234  if (isset($this->debit)) {
235  $this->debit = (float) $this->debit;
236  }
237  if (isset($this->credit)) {
238  $this->credit = (float) $this->credit;
239  }
240  if (isset($this->montant)) {
241  $this->montant = (float) $this->montant;
242  }
243  if (isset($this->amount)) {
244  $this->amount = (float) $this->amount;
245  }
246  if (isset($this->sens)) {
247  $this->sens = trim($this->sens);
248  }
249  if (isset($this->import_key)) {
250  $this->import_key = trim($this->import_key);
251  }
252  if (isset($this->code_journal)) {
253  $this->code_journal = trim($this->code_journal);
254  }
255  if (isset($this->journal_label)) {
256  $this->journal_label = trim($this->journal_label);
257  }
258  if (isset($this->piece_num)) {
259  $this->piece_num = trim($this->piece_num);
260  }
261  if (empty($this->debit)) $this->debit = 0.0;
262  if (empty($this->credit)) $this->credit = 0.0;
263 
264  // Check parameters
265  if (($this->numero_compte == "") || $this->numero_compte == '-1' || $this->numero_compte == 'NotDefined')
266  {
267  $langs->loadLangs(array("errors"));
268  if (in_array($this->doc_type, array('bank', 'expense_report')))
269  {
270  $this->errors[] = $langs->trans('ErrorFieldAccountNotDefinedForBankLine', $this->fk_docdet, $this->doc_type);
271  } else {
272  //$this->errors[]=$langs->trans('ErrorFieldAccountNotDefinedForInvoiceLine', $this->doc_ref, $this->label_compte);
273  $mesg = $this->doc_ref.', '.$langs->trans("AccountAccounting").': '.$this->numero_compte;
274  if ($this->subledger_account && $this->subledger_account != $this->numero_compte)
275  {
276  $mesg .= ', '.$langs->trans("SubledgerAccount").': '.$this->subledger_account;
277  }
278  $this->errors[] = $langs->trans('ErrorFieldAccountNotDefinedForLine', $mesg);
279  }
280 
281  return -1;
282  }
283 
284  $this->db->begin();
285 
286  $this->piece_num = 0;
287 
288  // First check if line not yet already in bookkeeping.
289  // Note that we must include doc_type - fk_doc - numero_compte - label to be sure to have unicity of line (we may have several lines
290  // with same doc_type, fk_doc, numero_compte for 1 invoice line when using localtaxes with same account)
291  // WARNING: This is not reliable, label may have been modified. This is just a small protection.
292  // The page to make journalization make the test on couple doc_type - fk_doc only.
293  $sql = "SELECT count(*) as nb";
294  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
295  $sql .= " WHERE doc_type = '".$this->db->escape($this->doc_type)."'";
296  $sql .= " AND fk_doc = ".$this->fk_doc;
297  //$sql .= " AND fk_docdet = " . $this->fk_docdet; // This field can be 0 if record is for several lines
298  $sql .= " AND numero_compte = '".$this->db->escape($this->numero_compte)."'";
299  $sql .= " AND label_operation = '".$this->db->escape($this->label_operation)."'";
300  $sql .= " AND entity IN (".getEntity('accountancy').")";
301 
302  $resql = $this->db->query($sql);
303 
304  if ($resql) {
305  $row = $this->db->fetch_object($resql);
306  if ($row->nb == 0)
307  {
308  // Determine piece_num
309  $sqlnum = "SELECT piece_num";
310  $sqlnum .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
311  $sqlnum .= " WHERE doc_type = '".$this->db->escape($this->doc_type)."'"; // For example doc_type = 'bank'
312  $sqlnum .= " AND fk_docdet = ".$this->db->escape($this->fk_docdet); // fk_docdet is rowid into llx_bank or llx_facturedet or llx_facturefourndet, or ...
313  $sqlnum .= " AND doc_ref = '".$this->db->escape($this->doc_ref)."'"; // ref of source object
314  $sqlnum .= " AND entity IN (".getEntity('accountancy').")";
315 
316  dol_syslog(get_class($this).":: create sqlnum=".$sqlnum, LOG_DEBUG);
317  $resqlnum = $this->db->query($sqlnum);
318  if ($resqlnum) {
319  $objnum = $this->db->fetch_object($resqlnum);
320  $this->piece_num = $objnum->piece_num;
321  }
322 
323  dol_syslog(get_class($this).":: create this->piece_num=".$this->piece_num, LOG_DEBUG);
324  if (empty($this->piece_num)) {
325  $sqlnum = "SELECT MAX(piece_num)+1 as maxpiecenum";
326  $sqlnum .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
327  $sqlnum .= " WHERE entity IN (".getEntity('accountancy').")";
328 
329  dol_syslog(get_class($this).":: create sqlnum=".$sqlnum, LOG_DEBUG);
330  $resqlnum = $this->db->query($sqlnum);
331  if ($resqlnum) {
332  $objnum = $this->db->fetch_object($resqlnum);
333  $this->piece_num = $objnum->maxpiecenum;
334  }
335  dol_syslog(get_class($this).":: create this->piece_num=".$this->piece_num, LOG_DEBUG);
336  }
337  if (empty($this->piece_num)) {
338  $this->piece_num = 1;
339  }
340 
341  $now = dol_now();
342 
343  $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
344  $sql .= "doc_date";
345  $sql .= ", date_lim_reglement";
346  $sql .= ", doc_type";
347  $sql .= ", doc_ref";
348  $sql .= ", fk_doc";
349  $sql .= ", fk_docdet";
350  $sql .= ", thirdparty_code";
351  $sql .= ", subledger_account";
352  $sql .= ", subledger_label";
353  $sql .= ", numero_compte";
354  $sql .= ", label_compte";
355  $sql .= ", label_operation";
356  $sql .= ", debit";
357  $sql .= ", credit";
358  $sql .= ", montant";
359  $sql .= ", sens";
360  $sql .= ", fk_user_author";
361  $sql .= ", date_creation";
362  $sql .= ", code_journal";
363  $sql .= ", journal_label";
364  $sql .= ", piece_num";
365  $sql .= ', entity';
366  $sql .= ") VALUES (";
367  $sql .= "'".$this->db->idate($this->doc_date)."'";
368  $sql .= ", ".(!isset($this->date_lim_reglement) || dol_strlen($this->date_lim_reglement) == 0 ? 'NULL' : "'".$this->db->idate($this->date_lim_reglement)."'");
369  $sql .= ", '".$this->db->escape($this->doc_type)."'";
370  $sql .= ", '".$this->db->escape($this->doc_ref)."'";
371  $sql .= ", ".$this->fk_doc;
372  $sql .= ", ".$this->fk_docdet;
373  $sql .= ", ".(!empty($this->thirdparty_code) ? ("'".$this->db->escape($this->thirdparty_code)."'") : "NULL");
374  $sql .= ", ".(!empty($this->subledger_account) ? ("'".$this->db->escape($this->subledger_account)."'") : "NULL");
375  $sql .= ", ".(!empty($this->subledger_label) ? ("'".$this->db->escape($this->subledger_label)."'") : "NULL");
376  $sql .= ", '".$this->db->escape($this->numero_compte)."'";
377  $sql .= ", ".(!empty($this->label_compte) ? ("'".$this->db->escape($this->label_compte)."'") : "NULL");
378  $sql .= ", '".$this->db->escape($this->label_operation)."'";
379  $sql .= ", ".$this->debit;
380  $sql .= ", ".$this->credit;
381  $sql .= ", ".$this->montant;
382  $sql .= ", ".(!empty($this->sens) ? ("'".$this->db->escape($this->sens)."'") : "NULL");
383  $sql .= ", '".$this->db->escape($this->fk_user_author)."'";
384  $sql .= ", '".$this->db->idate($now)."'";
385  $sql .= ", '".$this->db->escape($this->code_journal)."'";
386  $sql .= ", ".(!empty($this->journal_label) ? ("'".$this->db->escape($this->journal_label)."'") : "NULL");
387  $sql .= ", ".$this->db->escape($this->piece_num);
388  $sql .= ", ".(!isset($this->entity) ? $conf->entity : $this->entity);
389  $sql .= ")";
390 
391  $resql = $this->db->query($sql);
392  if ($resql) {
393  $id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
394 
395  if ($id > 0) {
396  $this->id = $id;
397  $result = 0;
398  } else {
399  $result = -2;
400  $error++;
401  $this->errors[] = 'Error Create Error '.$result.' lecture ID';
402  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
403  }
404  } else {
405  $result = -1;
406  $error++;
407  $this->errors[] = 'Error '.$this->db->lasterror();
408  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
409  }
410  } else { // Already exists
411  $result = -3;
412  $error++;
413  $this->error = 'BookkeepingRecordAlreadyExists';
414  dol_syslog(__METHOD__.' '.$this->error, LOG_WARNING);
415  }
416  } else {
417  $result = -5;
418  $error++;
419  $this->errors[] = 'Error '.$this->db->lasterror();
420  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
421  }
422 
423  // Uncomment this and change MYOBJECT to your own tag if you
424  // want this action to call a trigger.
425  //if (! $error && ! $notrigger) {
426 
427  // // Call triggers
428  // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
429  // if ($result < 0) $error++;
430  // // End call triggers
431  //}
432 
433  // Commit or rollback
434  if ($error) {
435  $this->db->rollback();
436  return -1 * $error;
437  } else {
438  $this->db->commit();
439  return $result;
440  }
441  }
442 
453  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
454  {
455  global $db, $conf, $langs;
456  global $dolibarr_main_authentication, $dolibarr_main_demo;
457  global $menumanager;
458 
459  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
460 
461  $result = '';
462  $companylink = '';
463 
464  $label = '<u>'.$langs->trans("Transaction").'</u>';
465  $label .= '<br>';
466  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->piece_num;
467 
468  $url = DOL_URL_ROOT.'/accountancy/bookkeeping/card.php?piece_num='.$this->piece_num;
469 
470  if ($option != 'nolink')
471  {
472  // Add param to save lastsearch_values or not
473  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
474  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
475  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
476  }
477 
478  $linkclose = '';
479  if (empty($notooltip))
480  {
481  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
482  {
483  $label = $langs->trans("ShowTransaction");
484  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
485  }
486  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
487  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
488  } else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
489 
490  $linkstart = '<a href="'.$url.'"';
491  $linkstart .= $linkclose.'>';
492  $linkend = '</a>';
493 
494  $result .= $linkstart;
495  if ($withpicto) $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
496  if ($withpicto != 2) $result .= $this->piece_num;
497  $result .= $linkend;
498  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
499 
500  return $result;
501  }
502 
511  public function createStd(User $user, $notrigger = false, $mode = '')
512  {
513  global $conf, $langs;
514 
515  $langs->loadLangs(array("accountancy", "bills", "compta"));
516 
517  dol_syslog(__METHOD__, LOG_DEBUG);
518 
519  $error = 0;
520 
521  // Clean parameters
522  if (isset($this->doc_type)) {
523  $this->doc_type = trim($this->doc_type);
524  }
525  if (isset($this->doc_ref)) {
526  $this->doc_ref = trim($this->doc_ref);
527  }
528  if (isset($this->fk_doc)) {
529  $this->fk_doc = (int) $this->fk_doc;
530  }
531  if (isset($this->fk_docdet)) {
532  $this->fk_docdet = (int) $this->fk_docdet;
533  }
534  if (isset($this->thirdparty_code)) {
535  $this->thirdparty_code = trim($this->thirdparty_code);
536  }
537  if (isset($this->subledger_account)) {
538  $this->subledger_account = trim($this->subledger_account);
539  }
540  if (isset($this->subledger_label)) {
541  $this->subledger_label = trim($this->subledger_label);
542  }
543  if (isset($this->numero_compte)) {
544  $this->numero_compte = trim($this->numero_compte);
545  }
546  if (isset($this->label_compte)) {
547  $this->label_compte = trim($this->label_compte);
548  }
549  if (isset($this->label_operation)) {
550  $this->label_operation = trim($this->label_operation);
551  }
552  if (isset($this->debit)) {
553  $this->debit = trim($this->debit);
554  }
555  if (isset($this->credit)) {
556  $this->credit = trim($this->credit);
557  }
558  if (isset($this->montant)) {
559  $this->montant = trim($this->montant);
560  }
561  if (isset($this->amount)) {
562  $this->amount = trim($this->amount);
563  }
564  if (isset($this->sens)) {
565  $this->sens = trim($this->sens);
566  }
567  if (isset($this->import_key)) {
568  $this->import_key = trim($this->import_key);
569  }
570  if (isset($this->code_journal)) {
571  $this->code_journal = trim($this->code_journal);
572  }
573  if (isset($this->journal_label)) {
574  $this->journal_label = trim($this->journal_label);
575  }
576  if (isset($this->piece_num)) {
577  $this->piece_num = trim($this->piece_num);
578  }
579  if (empty($this->debit)) $this->debit = 0;
580  if (empty($this->credit)) $this->credit = 0;
581 
582  $this->debit = price2num($this->debit, 'MT');
583  $this->credit = price2num($this->credit, 'MT');
584 
585  $now = dol_now();
586 
587  // Check parameters
588  $this->journal_label = $langs->trans($this->journal_label);
589 
590  // Insert request
591  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.$mode.' (';
592  $sql .= 'doc_date,';
593  $sql .= 'date_lim_reglement,';
594  $sql .= 'doc_type,';
595  $sql .= 'doc_ref,';
596  $sql .= 'fk_doc,';
597  $sql .= 'fk_docdet,';
598  $sql .= 'thirdparty_code,';
599  $sql .= 'subledger_account,';
600  $sql .= 'subledger_label,';
601  $sql .= 'numero_compte,';
602  $sql .= 'label_compte,';
603  $sql .= 'label_operation,';
604  $sql .= 'debit,';
605  $sql .= 'credit,';
606  $sql .= 'montant,';
607  $sql .= 'sens,';
608  $sql .= 'fk_user_author,';
609  $sql .= 'date_creation,';
610  $sql .= 'code_journal,';
611  $sql .= 'journal_label,';
612  $sql .= 'piece_num,';
613  $sql .= 'entity';
614  $sql .= ') VALUES (';
615  $sql .= ' '.(!isset($this->doc_date) || dol_strlen($this->doc_date) == 0 ? 'NULL' : "'".$this->db->idate($this->doc_date)."'").',';
616  $sql .= ' '.(!isset($this->date_lim_reglement) || dol_strlen($this->date_lim_reglement) == 0 ? 'NULL' : "'".$this->db->idate($this->date_lim_reglement)."'").',';
617  $sql .= ' '.(!isset($this->doc_type) ? 'NULL' : "'".$this->db->escape($this->doc_type)."'").',';
618  $sql .= ' '.(!isset($this->doc_ref) ? 'NULL' : "'".$this->db->escape($this->doc_ref)."'").',';
619  $sql .= ' '.(empty($this->fk_doc) ? '0' : $this->fk_doc).',';
620  $sql .= ' '.(empty($this->fk_docdet) ? '0' : $this->fk_docdet).',';
621  $sql .= ' '.(!isset($this->thirdparty_code) ? 'NULL' : "'".$this->db->escape($this->thirdparty_code)."'").',';
622  $sql .= ' '.(!isset($this->subledger_account) ? 'NULL' : "'".$this->db->escape($this->subledger_account)."'").',';
623  $sql .= ' '.(!isset($this->subledger_label) ? 'NULL' : "'".$this->db->escape($this->subledger_label)."'").',';
624  $sql .= ' '.(!isset($this->numero_compte) ? 'NULL' : "'".$this->db->escape($this->numero_compte)."'").',';
625  $sql .= ' '.(!isset($this->label_compte) ? 'NULL' : "'".$this->db->escape($this->label_compte)."'").',';
626  $sql .= ' '.(!isset($this->label_operation) ? 'NULL' : "'".$this->db->escape($this->label_operation)."'").',';
627  $sql .= ' '.(!isset($this->debit) ? 'NULL' : $this->debit).',';
628  $sql .= ' '.(!isset($this->credit) ? 'NULL' : $this->credit).',';
629  $sql .= ' '.(!isset($this->montant) ? 'NULL' : $this->montant).',';
630  $sql .= ' '.(!isset($this->sens) ? 'NULL' : "'".$this->db->escape($this->sens)."'").',';
631  $sql .= ' '.$user->id.',';
632  $sql .= ' '."'".$this->db->idate($now)."',";
633  $sql .= ' '.(empty($this->code_journal) ? 'NULL' : "'".$this->db->escape($this->code_journal)."'").',';
634  $sql .= ' '.(empty($this->journal_label) ? 'NULL' : "'".$this->db->escape($this->journal_label)."'").',';
635  $sql .= ' '.(empty($this->piece_num) ? 'NULL' : $this->db->escape($this->piece_num)).',';
636  $sql .= ' '.(!isset($this->entity) ? $conf->entity : $this->entity);
637  $sql .= ')';
638 
639  $this->db->begin();
640 
641  $resql = $this->db->query($sql);
642  if (!$resql) {
643  $error++;
644  $this->errors[] = 'Error '.$this->db->lasterror();
645  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
646  }
647 
648  if (!$error) {
649  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element.$mode);
650 
651  // Uncomment this and change MYOBJECT to your own tag if you
652  // want this action to call a trigger.
653  //if (! $notrigger) {
654 
655  // // Call triggers
656  // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
657  // if ($result < 0) $error++;
658  // // End call triggers
659  //}
660  }
661 
662  // Commit or rollback
663  if ($error) {
664  $this->db->rollback();
665 
666  return -1 * $error;
667  } else {
668  $this->db->commit();
669 
670  return $this->id;
671  }
672  }
673 
683  public function fetch($id, $ref = null, $mode = '')
684  {
685  global $conf;
686 
687  dol_syslog(__METHOD__, LOG_DEBUG);
688 
689  $sql = 'SELECT';
690  $sql .= ' t.rowid,';
691  $sql .= " t.doc_date,";
692  $sql .= " t.date_lim_reglement,";
693  $sql .= " t.doc_type,";
694  $sql .= " t.doc_ref,";
695  $sql .= " t.fk_doc,";
696  $sql .= " t.fk_docdet,";
697  $sql .= " t.thirdparty_code,";
698  $sql .= " t.subledger_account,";
699  $sql .= " t.subledger_label,";
700  $sql .= " t.numero_compte,";
701  $sql .= " t.label_compte,";
702  $sql .= " t.label_operation,";
703  $sql .= " t.debit,";
704  $sql .= " t.credit,";
705  $sql .= " t.montant as amount,";
706  $sql .= " t.sens,";
707  $sql .= " t.fk_user_author,";
708  $sql .= " t.import_key,";
709  $sql .= " t.code_journal,";
710  $sql .= " t.journal_label,";
711  $sql .= " t.piece_num,";
712  $sql .= " t.date_creation";
713  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.$mode.' as t';
714  $sql .= ' WHERE 1 = 1';
715  $sql .= " AND entity IN (".getEntity('accountancy').")";
716  if (null !== $ref) {
717  $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
718  } else {
719  $sql .= ' AND t.rowid = '.$id;
720  }
721 
722  $resql = $this->db->query($sql);
723  if ($resql) {
724  $numrows = $this->db->num_rows($resql);
725  if ($numrows) {
726  $obj = $this->db->fetch_object($resql);
727 
728  $this->id = $obj->rowid;
729 
730  $this->doc_date = $this->db->jdate($obj->doc_date);
731  $this->date_lim_reglement = $this->db->jdate($obj->date_lim_reglement);
732  $this->doc_type = $obj->doc_type;
733  $this->doc_ref = $obj->doc_ref;
734  $this->fk_doc = $obj->fk_doc;
735  $this->fk_docdet = $obj->fk_docdet;
736  $this->thirdparty_code = $obj->thirdparty_code;
737  $this->subledger_account = $obj->subledger_account;
738  $this->subledger_label = $obj->subledger_label;
739  $this->numero_compte = $obj->numero_compte;
740  $this->label_compte = $obj->label_compte;
741  $this->label_operation = $obj->label_operation;
742  $this->debit = $obj->debit;
743  $this->credit = $obj->credit;
744  $this->montant = $obj->amount;
745  $this->amount = $obj->amount;
746  $this->sens = $obj->sens;
747  $this->fk_user_author = $obj->fk_user_author;
748  $this->import_key = $obj->import_key;
749  $this->code_journal = $obj->code_journal;
750  $this->journal_label = $obj->journal_label;
751  $this->piece_num = $obj->piece_num;
752  $this->date_creation = $this->db->jdate($obj->date_creation);
753  }
754  $this->db->free($resql);
755 
756  if ($numrows) {
757  return 1;
758  } else {
759  return 0;
760  }
761  } else {
762  $this->errors[] = 'Error '.$this->db->lasterror();
763  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
764 
765  return -1;
766  }
767  }
768 
769 
783  public function fetchAllByAccount($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND', $option = 0)
784  {
785  global $conf;
786 
787  dol_syslog(__METHOD__, LOG_DEBUG);
788 
789  $this->lines = array();
790 
791  $sql = 'SELECT';
792  $sql .= ' t.rowid,';
793  $sql .= " t.doc_date,";
794  $sql .= " t.doc_type,";
795  $sql .= " t.doc_ref,";
796  $sql .= " t.fk_doc,";
797  $sql .= " t.fk_docdet,";
798  $sql .= " t.thirdparty_code,";
799  $sql .= " t.subledger_account,";
800  $sql .= " t.subledger_label,";
801  $sql .= " t.numero_compte,";
802  $sql .= " t.label_compte,";
803  $sql .= " t.label_operation,";
804  $sql .= " t.debit,";
805  $sql .= " t.credit,";
806  $sql .= " t.montant as amount,";
807  $sql .= " t.sens,";
808  $sql .= " t.multicurrency_amount,";
809  $sql .= " t.multicurrency_code,";
810  $sql .= " t.lettering_code,";
811  $sql .= " t.date_lettering,";
812  $sql .= " t.fk_user_author,";
813  $sql .= " t.import_key,";
814  $sql .= " t.code_journal,";
815  $sql .= " t.journal_label,";
816  $sql .= " t.piece_num,";
817  $sql .= " t.date_creation,";
818  $sql .= " t.date_export";
819  // Manage filter
820  $sqlwhere = array();
821  if (count($filter) > 0) {
822  foreach ($filter as $key => $value) {
823  if ($key == 't.doc_date') {
824  $sqlwhere[] = $key.'=\''.$this->db->idate($value).'\'';
825  } elseif ($key == 't.doc_date>=' || $key == 't.doc_date<=') {
826  $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
827  } elseif ($key == 't.numero_compte>=' || $key == 't.numero_compte<=' || $key == 't.subledger_account>=' || $key == 't.subledger_account<=') {
828  $sqlwhere[] = $key.'\''.$this->db->escape($value).'\'';
829  } elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') {
830  $sqlwhere[] = $key.'='.$value;
831  } elseif ($key == 't.subledger_account' || $key == 't.numero_compte') {
832  $sqlwhere[] = $key.' LIKE \''.$this->db->escape($value).'%\'';
833  } elseif ($key == 't.date_creation>=' || $key == 't.date_creation<=') {
834  $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
835  } elseif ($key == 't.date_export>=' || $key == 't.date_export<=') {
836  $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
837  } elseif ($key == 't.credit' || $key == 't.debit') {
838  $sqlwhere[] = natural_search($key, $value, 1, 1);
839  } elseif ($key == 't.reconciled_option') {
840  $sqlwhere[] = 't.lettering_code IS NULL';
841  } elseif ($key == 't.code_journal' && !empty($value)) {
842  if (is_array($value)) {
843  $sqlwhere[] = natural_search("t.code_journal", join(',', $value), 3, 1);
844  } else {
845  $sqlwhere[] = natural_search("t.code_journal", $value, 3, 1);
846  }
847  } else {
848  $sqlwhere[] = natural_search($key, $value, 0, 1);
849  }
850  }
851  }
852  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
853  $sql .= ' WHERE 1 = 1';
854  $sql .= " AND entity IN (".getEntity('accountancy').")";
855  if (count($sqlwhere) > 0) {
856  $sql .= ' AND '.implode(' '.$filtermode.' ', $sqlwhere);
857  }
858  // Affichage par compte comptable
859  if (!empty($option)) {
860  $sql .= ' AND t.subledger_account IS NOT NULL';
861  $sql .= ' ORDER BY t.subledger_account ASC';
862  } else {
863  $sql .= ' ORDER BY t.numero_compte ASC';
864  }
865 
866  if (!empty($sortfield)) {
867  $sql .= ', '.$sortfield.' '.$sortorder;
868  }
869  if (!empty($limit)) {
870  $sql .= ' '.$this->db->plimit($limit + 1, $offset);
871  }
872 
873  $resql = $this->db->query($sql);
874  if ($resql) {
875  $num = $this->db->num_rows($resql);
876 
877  $i = 0;
878  while (($obj = $this->db->fetch_object($resql)) && (empty($limit) || $i < min($limit, $num))) {
879  $line = new BookKeepingLine();
880 
881  $line->id = $obj->rowid;
882 
883  $line->doc_date = $this->db->jdate($obj->doc_date);
884  $line->doc_type = $obj->doc_type;
885  $line->doc_ref = $obj->doc_ref;
886  $line->fk_doc = $obj->fk_doc;
887  $line->fk_docdet = $obj->fk_docdet;
888  $line->thirdparty_code = $obj->thirdparty_code;
889  $line->subledger_account = $obj->subledger_account;
890  $line->subledger_label = $obj->subledger_label;
891  $line->numero_compte = $obj->numero_compte;
892  $line->label_compte = $obj->label_compte;
893  $line->label_operation = $obj->label_operation;
894  $line->debit = $obj->debit;
895  $line->credit = $obj->credit;
896  $line->montant = $obj->amount; // deprecated
897  $line->amount = $obj->amount;
898  $line->sens = $obj->sens;
899  $line->multicurrency_amount = $obj->multicurrency_amount;
900  $line->multicurrency_code = $obj->multicurrency_code;
901  $line->lettering_code = $obj->lettering_code;
902  $line->date_lettering = $obj->date_lettering;
903  $line->fk_user_author = $obj->fk_user_author;
904  $line->import_key = $obj->import_key;
905  $line->code_journal = $obj->code_journal;
906  $line->journal_label = $obj->journal_label;
907  $line->piece_num = $obj->piece_num;
908  $line->date_creation = $this->db->jdate($obj->date_creation);
909  $line->date_export = $this->db->jdate($obj->date_export);
910 
911  $this->lines[] = $line;
912 
913  $i++;
914  }
915  $this->db->free($resql);
916 
917  return $num;
918  } else {
919  $this->errors[] = 'Error '.$this->db->lasterror();
920  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
921 
922  return -1;
923  }
924  }
925 
938  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND', $showAlreadyExportMovements = 1)
939  {
940  global $conf;
941 
942  dol_syslog(__METHOD__, LOG_DEBUG);
943 
944  $sql = 'SELECT';
945  $sql .= ' t.rowid,';
946  $sql .= " t.doc_date,";
947  $sql .= " t.doc_type,";
948  $sql .= " t.doc_ref,";
949  $sql .= " t.fk_doc,";
950  $sql .= " t.fk_docdet,";
951  $sql .= " t.thirdparty_code,";
952  $sql .= " t.subledger_account,";
953  $sql .= " t.subledger_label,";
954  $sql .= " t.numero_compte,";
955  $sql .= " t.label_compte,";
956  $sql .= " t.label_operation,";
957  $sql .= " t.debit,";
958  $sql .= " t.credit,";
959  $sql .= " t.lettering_code,";
960  $sql .= " t.date_lettering,";
961  $sql .= " t.montant as amount,";
962  $sql .= " t.sens,";
963  $sql .= " t.fk_user_author,";
964  $sql .= " t.import_key,";
965  $sql .= " t.code_journal,";
966  $sql .= " t.journal_label,";
967  $sql .= " t.piece_num,";
968  $sql .= " t.date_creation,";
969  $sql .= " t.date_lim_reglement,";
970  $sql .= " t.tms as date_modification,";
971  $sql .= " t.date_export";
972  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
973  // Manage filter
974  $sqlwhere = array();
975  if (count($filter) > 0) {
976  foreach ($filter as $key => $value) {
977  if ($key == 't.doc_date') {
978  $sqlwhere[] = $key.'=\''.$this->db->idate($value).'\'';
979  } elseif ($key == 't.doc_date>=' || $key == 't.doc_date<=') {
980  $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
981  } elseif ($key == 't.numero_compte>=' || $key == 't.numero_compte<=' || $key == 't.subledger_account>=' || $key == 't.subledger_account<=') {
982  $sqlwhere[] = $key.'\''.$this->db->escape($value).'\'';
983  } elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') {
984  $sqlwhere[] = $key.'='.$value;
985  } elseif ($key == 't.subledger_account' || $key == 't.numero_compte') {
986  $sqlwhere[] = $key.' LIKE \''.$this->db->escape($value).'%\'';
987  } elseif ($key == 't.date_creation>=' || $key == 't.date_creation<=') {
988  $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
989  } elseif ($key == 't.tms>=' || $key == 't.tms<=') {
990  $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
991  } elseif ($key == 't.date_export>=' || $key == 't.date_export<=') {
992  $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
993  } elseif ($key == 't.credit' || $key == 't.debit') {
994  $sqlwhere[] = natural_search($key, $value, 1, 1);
995  } else {
996  $sqlwhere[] = natural_search($key, $value, 0, 1);
997  }
998  }
999  }
1000  $sql .= ' WHERE t.entity IN ('.getEntity('accountancy').')';
1001  if ($showAlreadyExportMovements == 0) {
1002  $sql .= " AND t.date_export IS NULL";
1003  }
1004  if (count($sqlwhere) > 0) {
1005  $sql .= ' AND '.implode(' '.$filtermode.' ', $sqlwhere);
1006  }
1007  if (!empty($sortfield)) {
1008  $sql .= $this->db->order($sortfield, $sortorder);
1009  }
1010  if (!empty($limit)) {
1011  $sql .= ' '.$this->db->plimit($limit + 1, $offset);
1012  }
1013  $this->lines = array();
1014 
1015  $resql = $this->db->query($sql);
1016  if ($resql) {
1017  $num = $this->db->num_rows($resql);
1018 
1019  $i = 0;
1020  while (($obj = $this->db->fetch_object($resql)) && (empty($limit) || $i < min($limit, $num)))
1021  {
1022  $line = new BookKeepingLine();
1023 
1024  $line->id = $obj->rowid;
1025 
1026  $line->doc_date = $this->db->jdate($obj->doc_date);
1027  $line->doc_type = $obj->doc_type;
1028  $line->doc_ref = $obj->doc_ref;
1029  $line->fk_doc = $obj->fk_doc;
1030  $line->fk_docdet = $obj->fk_docdet;
1031  $line->thirdparty_code = $obj->thirdparty_code;
1032  $line->subledger_account = $obj->subledger_account;
1033  $line->subledger_label = $obj->subledger_label;
1034  $line->numero_compte = $obj->numero_compte;
1035  $line->label_compte = $obj->label_compte;
1036  $line->label_operation = $obj->label_operation;
1037  $line->debit = $obj->debit;
1038  $line->credit = $obj->credit;
1039  $line->montant = $obj->amount; // deprecated
1040  $line->amount = $obj->amount;
1041  $line->sens = $obj->sens;
1042  $line->lettering_code = $obj->lettering_code;
1043  $line->date_lettering = $obj->date_lettering;
1044  $line->fk_user_author = $obj->fk_user_author;
1045  $line->import_key = $obj->import_key;
1046  $line->code_journal = $obj->code_journal;
1047  $line->journal_label = $obj->journal_label;
1048  $line->piece_num = $obj->piece_num;
1049  $line->date_creation = $this->db->jdate($obj->date_creation);
1050  $line->date_lim_reglement = $this->db->jdate($obj->date_lim_reglement);
1051  $line->date_modification = $this->db->jdate($obj->date_modification);
1052  $line->date_export = $this->db->jdate($obj->date_export);
1053 
1054  $this->lines[] = $line;
1055 
1056  $i++;
1057  }
1058  $this->db->free($resql);
1059 
1060  return $num;
1061  } else {
1062  $this->errors[] = 'Error '.$this->db->lasterror();
1063  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
1064  return -1;
1065  }
1066  }
1067 
1080  public function fetchAllBalance($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
1081  {
1082  global $conf;
1083 
1084  $this->lines = array();
1085 
1086  dol_syslog(__METHOD__, LOG_DEBUG);
1087 
1088  $sql = 'SELECT';
1089  $sql .= " t.numero_compte,";
1090  $sql .= " SUM(t.debit) as debit,";
1091  $sql .= " SUM(t.credit) as credit";
1092  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
1093  // Manage filter
1094  $sqlwhere = array();
1095  if (count($filter) > 0) {
1096  foreach ($filter as $key => $value) {
1097  if ($key == 't.doc_date') {
1098  $sqlwhere[] = $key.'=\''.$this->db->idate($value).'\'';
1099  } elseif ($key == 't.doc_date>=' || $key == 't.doc_date<=') {
1100  $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
1101  } elseif ($key == 't.numero_compte>=' || $key == 't.numero_compte<=' || $key == 't.subledger_account>=' || $key == 't.subledger_account<=') {
1102  $sqlwhere[] = $key.'\''.$this->db->escape($value).'\'';
1103  } elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') {
1104  $sqlwhere[] = $key.'='.$value;
1105  } elseif ($key == 't.subledger_account' || $key == 't.numero_compte') {
1106  $sqlwhere[] = $key.' LIKE \''.$this->db->escape($value).'%\'';
1107  } elseif ($key == 't.subledger_label') {
1108  $sqlwhere[] = $key.' LIKE \''.$this->db->escape($value).'%\'';
1109  } else {
1110  $sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\'';
1111  }
1112  }
1113  }
1114  $sql .= ' WHERE entity IN ('.getEntity('accountancy').')';
1115  if (count($sqlwhere) > 0) {
1116  $sql .= ' AND '.implode(' '.$filtermode.' ', $sqlwhere);
1117  }
1118 
1119  $sql .= ' GROUP BY t.numero_compte';
1120 
1121  if (!empty($sortfield)) {
1122  $sql .= $this->db->order($sortfield, $sortorder);
1123  }
1124  if (!empty($limit)) {
1125  $sql .= ' '.$this->db->plimit($limit + 1, $offset);
1126  }
1127 
1128  $resql = $this->db->query($sql);
1129  if ($resql)
1130  {
1131  $num = $this->db->num_rows($resql);
1132 
1133  $i = 0;
1134  while (($obj = $this->db->fetch_object($resql)) && (empty($limit) || $i < min($limit, $num)))
1135  {
1136  $line = new BookKeepingLine();
1137 
1138  $line->numero_compte = $obj->numero_compte;
1139  $line->debit = $obj->debit;
1140  $line->credit = $obj->credit;
1141 
1142  $this->lines[] = $line;
1143 
1144  $i++;
1145  }
1146  $this->db->free($resql);
1147 
1148  return $num;
1149  } else {
1150  $this->errors[] = 'Error '.$this->db->lasterror();
1151  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
1152 
1153  return -1;
1154  }
1155  }
1156 
1165  public function update(User $user, $notrigger = false, $mode = '')
1166  {
1167  $error = 0;
1168 
1169  dol_syslog(__METHOD__, LOG_DEBUG);
1170 
1171  // Clean parameters
1172  if (isset($this->doc_type)) {
1173  $this->doc_type = trim($this->doc_type);
1174  }
1175  if (isset($this->doc_ref)) {
1176  $this->doc_ref = trim($this->doc_ref);
1177  }
1178  if (isset($this->fk_doc)) {
1179  $this->fk_doc = (int) $this->fk_doc;
1180  }
1181  if (isset($this->fk_docdet)) {
1182  $this->fk_docdet = (int) $this->fk_docdet;
1183  }
1184  if (isset($this->thirdparty_code)) {
1185  $this->thirdparty_code = trim($this->thirdparty_code);
1186  }
1187  if (isset($this->subledger_account)) {
1188  $this->subledger_account = trim($this->subledger_account);
1189  }
1190  if (isset($this->subledger_label)) {
1191  $this->subledger_label = trim($this->subledger_label);
1192  }
1193  if (isset($this->numero_compte)) {
1194  $this->numero_compte = trim($this->numero_compte);
1195  }
1196  if (isset($this->label_compte)) {
1197  $this->label_compte = trim($this->label_compte);
1198  }
1199  if (isset($this->label_operation)) {
1200  $this->label_operation = trim($this->label_operation);
1201  }
1202  if (isset($this->debit)) {
1203  $this->debit = trim($this->debit);
1204  }
1205  if (isset($this->credit)) {
1206  $this->credit = trim($this->credit);
1207  }
1208  if (isset($this->amount)) {
1209  $this->amount = trim($this->amount);
1210  }
1211  if (isset($this->sens)) {
1212  $this->sens = trim($this->sens);
1213  }
1214  if (isset($this->import_key)) {
1215  $this->import_key = trim($this->import_key);
1216  }
1217  if (isset($this->code_journal)) {
1218  $this->code_journal = trim($this->code_journal);
1219  }
1220  if (isset($this->journal_label)) {
1221  $this->journal_label = trim($this->journal_label);
1222  }
1223  if (isset($this->piece_num)) {
1224  $this->piece_num = trim($this->piece_num);
1225  }
1226 
1227  $this->debit = price2num($this->debit, 'MT');
1228  $this->credit = price2num($this->credit, 'MT');
1229 
1230  // Check parameters
1231  // Put here code to add a control on parameters values
1232 
1233  // Update request
1234  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.$mode.' SET';
1235  $sql .= ' doc_date = '.(!isset($this->doc_date) || dol_strlen($this->doc_date) != 0 ? "'".$this->db->idate($this->doc_date)."'" : 'null').',';
1236  $sql .= ' doc_type = '.(isset($this->doc_type) ? "'".$this->db->escape($this->doc_type)."'" : "null").',';
1237  $sql .= ' doc_ref = '.(isset($this->doc_ref) ? "'".$this->db->escape($this->doc_ref)."'" : "null").',';
1238  $sql .= ' fk_doc = '.(isset($this->fk_doc) ? $this->fk_doc : "null").',';
1239  $sql .= ' fk_docdet = '.(isset($this->fk_docdet) ? $this->fk_docdet : "null").',';
1240  $sql .= ' thirdparty_code = '.(isset($this->thirdparty_code) ? "'".$this->db->escape($this->thirdparty_code)."'" : "null").',';
1241  $sql .= ' subledger_account = '.(isset($this->subledger_account) ? "'".$this->db->escape($this->subledger_account)."'" : "null").',';
1242  $sql .= ' subledger_label = '.(isset($this->subledger_label) ? "'".$this->db->escape($this->subledger_label)."'" : "null").',';
1243  $sql .= ' numero_compte = '.(isset($this->numero_compte) ? "'".$this->db->escape($this->numero_compte)."'" : "null").',';
1244  $sql .= ' label_compte = '.(isset($this->label_compte) ? "'".$this->db->escape($this->label_compte)."'" : "null").',';
1245  $sql .= ' label_operation = '.(isset($this->label_operation) ? "'".$this->db->escape($this->label_operation)."'" : "null").',';
1246  $sql .= ' debit = '.(isset($this->debit) ? $this->debit : "null").',';
1247  $sql .= ' credit = '.(isset($this->credit) ? $this->credit : "null").',';
1248  $sql .= ' montant = '.(isset($this->montant) ? $this->montant : "null").',';
1249  $sql .= ' sens = '.(isset($this->sens) ? "'".$this->db->escape($this->sens)."'" : "null").',';
1250  $sql .= ' fk_user_author = '.(isset($this->fk_user_author) ? $this->fk_user_author : "null").',';
1251  $sql .= ' import_key = '.(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null").',';
1252  $sql .= ' code_journal = '.(isset($this->code_journal) ? "'".$this->db->escape($this->code_journal)."'" : "null").',';
1253  $sql .= ' journal_label = '.(isset($this->journal_label) ? "'".$this->db->escape($this->journal_label)."'" : "null").',';
1254  $sql .= ' piece_num = '.(isset($this->piece_num) ? $this->piece_num : "null");
1255  $sql .= ' WHERE rowid='.$this->id;
1256 
1257  $this->db->begin();
1258 
1259  $resql = $this->db->query($sql);
1260  if (!$resql) {
1261  $error++;
1262  $this->errors[] = 'Error '.$this->db->lasterror();
1263  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
1264  }
1265 
1266  // Uncomment this and change MYOBJECT to your own tag if you
1267  // want this action calls a trigger.
1268  //if (! $error && ! $notrigger) {
1269 
1270  // // Call triggers
1271  // $result=$this->call_trigger('MYOBJECT_MODIFY',$user);
1272  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
1273  // // End call triggers
1274  //}
1275 
1276  // Commit or rollback
1277  if ($error) {
1278  $this->db->rollback();
1279 
1280  return -1 * $error;
1281  } else {
1282  $this->db->commit();
1283 
1284  return 1;
1285  }
1286  }
1287 
1297  public function updateByMvt($piece_num = '', $field = '', $value = '', $mode = '')
1298  {
1299  $error = 0;
1300 
1301  $this->db->begin();
1302 
1303  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element.$mode;
1304  $sql .= ' SET '.$field.'='.(is_numeric($value) ? $value : "'".$this->db->escape($value)."'");
1305  $sql .= " WHERE piece_num = '".$this->db->escape($piece_num)."'";
1306  $resql = $this->db->query($sql);
1307 
1308  if (!$resql) {
1309  $error++;
1310  $this->errors[] = 'Error '.$this->db->lasterror();
1311  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
1312  }
1313  if ($error) {
1314  $this->db->rollback();
1315 
1316  return -1 * $error;
1317  } else {
1318  $this->db->commit();
1319 
1320  return 1;
1321  }
1322  }
1323 
1332  public function delete(User $user, $notrigger = false, $mode = '')
1333  {
1334  dol_syslog(__METHOD__, LOG_DEBUG);
1335 
1336  $error = 0;
1337 
1338  $this->db->begin();
1339 
1340  // Uncomment this and change MYOBJECT to your own tag if you
1341  // want this action calls a trigger.
1342  //if (! $error && ! $notrigger) {
1343 
1344  // // Call triggers
1345  // $result=$this->call_trigger('MYOBJECT_DELETE',$user);
1346  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
1347  // // End call triggers
1348  //}
1349 
1350  if (!$error) {
1351  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.$mode;
1352  $sql .= ' WHERE rowid='.$this->id;
1353 
1354  $resql = $this->db->query($sql);
1355  if (!$resql) {
1356  $error++;
1357  $this->errors[] = 'Error '.$this->db->lasterror();
1358  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
1359  }
1360  }
1361 
1362  // Commit or rollback
1363  if ($error) {
1364  $this->db->rollback();
1365 
1366  return -1 * $error;
1367  } else {
1368  $this->db->commit();
1369 
1370  return 1;
1371  }
1372  }
1373 
1380  public function deleteByImportkey($importkey)
1381  {
1382  $this->db->begin();
1383 
1384  // first check if line not yet in bookkeeping
1385  $sql = "DELETE";
1386  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
1387  $sql .= " WHERE import_key = '".$this->db->escape($importkey)."'";
1388 
1389  $resql = $this->db->query($sql);
1390 
1391  if (!$resql) {
1392  $this->errors[] = "Error ".$this->db->lasterror();
1393  dol_syslog(get_class($this)."::delete Error ".$this->db->lasterror(), LOG_ERR);
1394  $this->db->rollback();
1395  return -1;
1396  }
1397 
1398  $this->db->commit();
1399  return 1;
1400  }
1401 
1411  public function deleteByYearAndJournal($delyear = 0, $journal = '', $mode = '', $delmonth = 0)
1412  {
1413  global $langs;
1414 
1415  if (empty($delyear) && empty($journal))
1416  {
1417  $this->error = 'ErrorOneFieldRequired';
1418  return -1;
1419  }
1420  if (!empty($delmonth) && empty($delyear))
1421  {
1422  $this->error = 'YearRequiredIfMonthDefined';
1423  return -2;
1424  }
1425 
1426  $this->db->begin();
1427 
1428  // Delete record in bookkeeping
1429  $sql = "DELETE";
1430  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1431  $sql .= " WHERE 1 = 1";
1432  $sql .= dolSqlDateFilter('doc_date', 0, $delmonth, $delyear);
1433  if (!empty($journal)) $sql .= " AND code_journal = '".$this->db->escape($journal)."'";
1434  $sql .= " AND entity IN (".getEntity('accountancy').")";
1435 
1436  // TODO: In a future we must forbid deletion if record is inside a closed fiscal period.
1437 
1438  $resql = $this->db->query($sql);
1439 
1440  if (!$resql) {
1441  $this->errors[] = "Error ".$this->db->lasterror();
1442  foreach ($this->errors as $errmsg) {
1443  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
1444  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1445  }
1446  $this->db->rollback();
1447  return -1;
1448  }
1449 
1450  $this->db->commit();
1451  return 1;
1452  }
1453 
1460  public function deleteMvtNum($piecenum)
1461  {
1462  global $conf;
1463 
1464  $this->db->begin();
1465 
1466  // first check if line not yet in bookkeeping
1467  $sql = "DELETE";
1468  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
1469  $sql .= " WHERE piece_num = ".(int) $piecenum;
1470  $sql .= " AND entity IN (".getEntity('accountancy').")";
1471 
1472  $resql = $this->db->query($sql);
1473 
1474  if (!$resql) {
1475  $this->errors[] = "Error ".$this->db->lasterror();
1476  foreach ($this->errors as $errmsg) {
1477  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
1478  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1479  }
1480  $this->db->rollback();
1481  return -1;
1482  }
1483 
1484  $this->db->commit();
1485  return 1;
1486  }
1487 
1495  public function createFromClone(User $user, $fromid)
1496  {
1497  dol_syslog(__METHOD__, LOG_DEBUG);
1498 
1499  $error = 0;
1500  $object = new BookKeeping($this->db);
1501 
1502  $this->db->begin();
1503 
1504  // Load source object
1505  $object->fetch($fromid);
1506  // Reset object
1507  $object->id = 0;
1508 
1509  // Clear fields
1510  // ...
1511 
1512  // Create clone
1513  $object->context['createfromclone'] = 'createfromclone';
1514  $result = $object->create($user);
1515 
1516  // Other options
1517  if ($result < 0) {
1518  $error++;
1519  $this->errors = $object->errors;
1520  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
1521  }
1522 
1523  unset($object->context['createfromclone']);
1524 
1525  // End
1526  if (!$error) {
1527  $this->db->commit();
1528 
1529  return $object->id;
1530  } else {
1531  $this->db->rollback();
1532 
1533  return -1;
1534  }
1535  }
1536 
1543  public function initAsSpecimen()
1544  {
1545  global $user;
1546 
1547  $now = dol_now();
1548 
1549  $this->id = 0;
1550  $this->doc_date = $now;
1551  $this->doc_type = '';
1552  $this->doc_ref = '';
1553  $this->fk_doc = 0;
1554  $this->fk_docdet = 0;
1555  $this->thirdparty_code = 'CU001';
1556  $this->subledger_account = '41100001';
1557  $this->subledger_label = 'My customer company';
1558  $this->numero_compte = '411';
1559  $this->label_compte = 'Customer';
1560  $this->label_operation = 'Sales of pea';
1561  $this->debit = 99.9;
1562  $this->credit = 0.0;
1563  $this->amount = 0.0;
1564  $this->sens = 'D';
1565  $this->fk_user_author = $user->id;
1566  $this->import_key = '20201027';
1567  $this->code_journal = 'VT';
1568  $this->journal_label = 'Journal de vente';
1569  $this->piece_num = 1234;
1570  $this->date_creation = $now;
1571  }
1572 
1580  public function fetchPerMvt($piecenum, $mode = '')
1581  {
1582  global $conf;
1583 
1584  $sql = "SELECT piece_num,doc_date,code_journal,journal_label,doc_ref,doc_type,date_creation";
1585  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1586  $sql .= " WHERE piece_num = ".$piecenum;
1587  $sql .= " AND entity IN (".getEntity('accountancy').")";
1588 
1589  dol_syslog(__METHOD__, LOG_DEBUG);
1590  $result = $this->db->query($sql);
1591  if ($result) {
1592  $obj = $this->db->fetch_object($result);
1593 
1594  $this->piece_num = $obj->piece_num;
1595  $this->code_journal = $obj->code_journal;
1596  $this->journal_label = $obj->journal_label;
1597  $this->doc_date = $this->db->jdate($obj->doc_date);
1598  $this->doc_ref = $obj->doc_ref;
1599  $this->doc_type = $obj->doc_type;
1600  $this->date_creation = $obj->date_creation;
1601  } else {
1602  $this->error = "Error ".$this->db->lasterror();
1603  dol_syslog(__METHOD__.$this->error, LOG_ERR);
1604  return -1;
1605  }
1606 
1607  return 1;
1608  }
1609 
1616  public function getNextNumMvt($mode = '')
1617  {
1618  global $conf;
1619 
1620  $sql = "SELECT MAX(piece_num)+1 as max FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1621  $sql .= " WHERE entity IN (".getEntity('accountancy').")";
1622 
1623  dol_syslog(get_class($this)."getNextNumMvt sql=".$sql, LOG_DEBUG);
1624  $result = $this->db->query($sql);
1625 
1626  if ($result) {
1627  $obj = $this->db->fetch_object($result);
1628  if ($obj) $result = $obj->max;
1629  if (empty($result)) $result = 1;
1630  return $result;
1631  } else {
1632  $this->error = "Error ".$this->db->lasterror();
1633  dol_syslog(get_class($this)."::getNextNumMvt ".$this->error, LOG_ERR);
1634  return -1;
1635  }
1636  }
1637 
1645  public function fetchAllPerMvt($piecenum, $mode = '')
1646  {
1647  global $conf;
1648 
1649  $sql = "SELECT rowid, doc_date, doc_type,";
1650  $sql .= " doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,";
1651  $sql .= " numero_compte, label_compte, label_operation, debit, credit,";
1652  $sql .= " montant as amount, sens, fk_user_author, import_key, code_journal, journal_label, piece_num, date_creation";
1653  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1654  $sql .= " WHERE piece_num = ".$piecenum;
1655  $sql .= " AND entity IN (".getEntity('accountancy').")";
1656 
1657  dol_syslog(__METHOD__, LOG_DEBUG);
1658  $result = $this->db->query($sql);
1659  if ($result) {
1660  while ($obj = $this->db->fetch_object($result)) {
1661  $line = new BookKeepingLine();
1662 
1663  $line->id = $obj->rowid;
1664 
1665  $line->doc_date = $this->db->jdate($obj->doc_date);
1666  $line->doc_type = $obj->doc_type;
1667  $line->doc_ref = $obj->doc_ref;
1668  $line->fk_doc = $obj->fk_doc;
1669  $line->fk_docdet = $obj->fk_docdet;
1670  $line->thirdparty_code = $obj->thirdparty_code;
1671  $line->subledger_account = $obj->subledger_account;
1672  $line->subledger_label = $obj->subledger_label;
1673  $line->numero_compte = $obj->numero_compte;
1674  $line->label_compte = $obj->label_compte;
1675  $line->label_operation = $obj->label_operation;
1676  $line->debit = $obj->debit;
1677  $line->credit = $obj->credit;
1678  $line->montant = $obj->amount;
1679  $line->amount = $obj->amount;
1680  $line->sens = $obj->sens;
1681  $line->code_journal = $obj->code_journal;
1682  $line->journal_label = $obj->journal_label;
1683  $line->piece_num = $obj->piece_num;
1684  $line->date_creation = $obj->date_creation;
1685 
1686  $this->linesmvt[] = $line;
1687  }
1688  } else {
1689  $this->error = "Error ".$this->db->lasterror();
1690  dol_syslog(__METHOD__.$this->error, LOG_ERR);
1691  return -1;
1692  }
1693 
1694  return 1;
1695  }
1696 
1697  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1704  public function export_bookkeeping($model = 'ebp')
1705  {
1706  // phpcs:enable
1707  global $conf;
1708 
1709  $sql = "SELECT rowid, doc_date, doc_type,";
1710  $sql .= " doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,";
1711  $sql .= " numero_compte, label_compte, label_operation, debit, credit,";
1712  $sql .= " montant as amount, sens, fk_user_author, import_key, code_journal, piece_num";
1713  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
1714  $sql .= " WHERE entity IN (".getEntity('accountancy').")";
1715 
1716  dol_syslog(get_class($this)."::export_bookkeeping", LOG_DEBUG);
1717 
1718  $resql = $this->db->query($sql);
1719 
1720  if ($resql) {
1721  $this->linesexport = array();
1722 
1723  $num = $this->db->num_rows($resql);
1724  while ($obj = $this->db->fetch_object($resql)) {
1725  $line = new BookKeepingLine();
1726 
1727  $line->id = $obj->rowid;
1728 
1729  $line->doc_date = $this->db->jdate($obj->doc_date);
1730  $line->doc_type = $obj->doc_type;
1731  $line->doc_ref = $obj->doc_ref;
1732  $line->fk_doc = $obj->fk_doc;
1733  $line->fk_docdet = $obj->fk_docdet;
1734  $line->thirdparty_code = $obj->thirdparty_code;
1735  $line->subledger_account = $obj->subledger_account;
1736  $line->subledger_label = $obj->subledger_label;
1737  $line->numero_compte = $obj->numero_compte;
1738  $line->label_compte = $obj->label_compte;
1739  $line->label_operation = $obj->label_operation;
1740  $line->debit = $obj->debit;
1741  $line->credit = $obj->credit;
1742  $line->montant = $obj->amount;
1743  $line->amount = $obj->amount;
1744  $line->sens = $obj->sens;
1745  $line->code_journal = $obj->code_journal;
1746  $line->piece_num = $obj->piece_num;
1747 
1748  $this->linesexport[] = $line;
1749  }
1750  $this->db->free($resql);
1751 
1752  return $num;
1753  } else {
1754  $this->error = "Error ".$this->db->lasterror();
1755  dol_syslog(get_class($this)."::export_bookkeeping ".$this->error, LOG_ERR);
1756  return -1;
1757  }
1758  }
1759 
1767  public function transformTransaction($direction = 0, $piece_num = '')
1768  {
1769  $error = 0;
1770 
1771  $this->db->begin();
1772 
1773  if ($direction == 0)
1774  {
1775  $next_piecenum = $this->getNextNumMvt();
1776  $now = dol_now();
1777 
1778  if ($next_piecenum < 0) {
1779  $error++;
1780  }
1781  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.' (doc_date, doc_type,';
1782  $sql .= ' doc_ref, fk_doc, fk_docdet, entity, thirdparty_code, subledger_account, subledger_label,';
1783  $sql .= ' numero_compte, label_compte, label_operation, debit, credit,';
1784  $sql .= ' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num, date_creation)';
1785  $sql .= ' SELECT doc_date, doc_type,';
1786  $sql .= ' doc_ref, fk_doc, fk_docdet, entity, thirdparty_code, subledger_account, subledger_label,';
1787  $sql .= ' numero_compte, label_compte, label_operation, debit, credit,';
1788  $sql .= ' montant, sens, fk_user_author, import_key, code_journal, journal_label, '.$next_piecenum.", '".$this->db->idate($now)."'";
1789  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.'_tmp WHERE piece_num = '.((int) $piece_num);
1790  $resql = $this->db->query($sql);
1791  if (!$resql) {
1792  $error++;
1793  $this->errors[] = 'Error '.$this->db->lasterror();
1794  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
1795  }
1796  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.'_tmp WHERE piece_num = '.((int) $piece_num);
1797  $resql = $this->db->query($sql);
1798  if (!$resql) {
1799  $error++;
1800  $this->errors[] = 'Error '.$this->db->lasterror();
1801  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
1802  }
1803  } elseif ($direction == 1) {
1804  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.'_tmp WHERE piece_num = '.((int) $piece_num);
1805  $resql = $this->db->query($sql);
1806  if (!$resql) {
1807  $error++;
1808  $this->errors[] = 'Error '.$this->db->lasterror();
1809  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
1810  }
1811  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'_tmp (doc_date, doc_type,';
1812  $sql .= ' doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,';
1813  $sql .= ' numero_compte, label_compte, label_operation, debit, credit,';
1814  $sql .= ' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num)';
1815  $sql .= ' SELECT doc_date, doc_type,';
1816  $sql .= ' doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,';
1817  $sql .= ' numero_compte, label_compte, label_operation, debit, credit,';
1818  $sql .= ' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num';
1819  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE piece_num = '.((int) $piece_num);
1820  $resql = $this->db->query($sql);
1821  if (!$resql) {
1822  $error++;
1823  $this->errors[] = 'Error '.$this->db->lasterror();
1824  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
1825  }
1826  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.'_tmp WHERE piece_num = '.((int) $piece_num);
1827  $resql = $this->db->query($sql);
1828  if (!$resql) {
1829  $error++;
1830  $this->errors[] = 'Error '.$this->db->lasterror();
1831  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
1832  }
1833  }
1834  if (!$error) {
1835  $this->db->commit();
1836  return 1;
1837  } else {
1838  $this->db->rollback();
1839  return -1;
1840  }
1841  /*
1842  $sql = "DELETE FROM ";
1843  $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab";
1844  $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.account_number = ab.numero_compte";
1845  $sql .= " AND aa.active = 1";
1846  $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
1847  $sql .= " AND asy.rowid = " . $pcgver;
1848  $sql .= " AND ab.entity IN (" . getEntity('accountancy') . ")";
1849  $sql .= " ORDER BY account_number ASC";
1850  */
1851  }
1852 
1853  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1866  public function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $aabase = '')
1867  {
1868  // phpcs:enable
1869  global $conf;
1870 
1871  require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1872 
1873  $pcgver = $conf->global->CHARTOFACCOUNTS;
1874 
1875  $sql = "SELECT DISTINCT ab.numero_compte as account_number, aa.label as label, aa.rowid as rowid, aa.fk_pcg_version";
1876  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as ab";
1877  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.account_number = ab.numero_compte";
1878  $sql .= " AND aa.active = 1";
1879  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
1880  $sql .= " AND asy.rowid = ".$pcgver;
1881  $sql .= " AND ab.entity IN (".getEntity('accountancy').")";
1882  $sql .= " ORDER BY account_number ASC";
1883 
1884  dol_syslog(get_class($this)."::select_account", LOG_DEBUG);
1885  $resql = $this->db->query($sql);
1886 
1887  if (!$resql) {
1888  $this->error = "Error ".$this->db->lasterror();
1889  dol_syslog(get_class($this)."::select_account ".$this->error, LOG_ERR);
1890  return -1;
1891  }
1892 
1893  $out = ajax_combobox($htmlname, $event);
1894 
1895  $options = array();
1896  $selected = null;
1897 
1898  while ($obj = $this->db->fetch_object($resql)) {
1899  $label = length_accountg($obj->account_number).' - '.$obj->label;
1900 
1901  $select_value_in = $obj->rowid;
1902  $select_value_out = $obj->rowid;
1903 
1904  if ($select_in == 1) {
1905  $select_value_in = $obj->account_number;
1906  }
1907  if ($select_out == 1) {
1908  $select_value_out = $obj->account_number;
1909  }
1910 
1911  // Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
1912  // Because same account_number can be share between different accounting_system and do have the same meaning
1913  if (($selectid != '') && $selectid == $select_value_in) {
1914  $selected = $select_value_out;
1915  }
1916 
1917  $options[$select_value_out] = $label;
1918  }
1919 
1920  $out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', 'maxwidth300');
1921  $this->db->free($resql);
1922  return $out;
1923  }
1924 
1932  public function getRootAccount($account = null)
1933  {
1934  global $conf;
1935  $pcgver = $conf->global->CHARTOFACCOUNTS;
1936 
1937  $sql = "SELECT root.rowid, root.account_number, root.label as label,";
1938  $sql .= " parent.rowid as parent_rowid, parent.account_number as parent_account_number, parent.label as parent_label";
1939  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_account as aa";
1940  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
1941  $sql .= " AND asy.rowid = ".((int) $pcgver);
1942  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as parent ON aa.account_parent = parent.rowid AND parent.active = 1";
1943  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as root ON parent.account_parent = root.rowid AND root.active = 1";
1944  $sql .= " WHERE aa.account_number = '".$this->db->escape($account)."'";
1945  $sql .= " AND aa.entity IN (".getEntity('accountancy').")";
1946 
1947  dol_syslog(get_class($this)."::select_account sql=".$sql, LOG_DEBUG);
1948  $resql = $this->db->query($sql);
1949  if ($resql) {
1950  $obj = '';
1951  if ($this->db->num_rows($resql)) {
1952  $obj = $this->db->fetch_object($resql);
1953  }
1954 
1955  $result = array('id'=>$obj->rowid, 'account_number'=>$obj->account_number, 'label'=>$obj->label);
1956  return $result;
1957  } else {
1958  $this->error = "Error ".$this->db->lasterror();
1959  dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
1960 
1961  return -1;
1962  }
1963  }
1964 
1965  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1972  public function get_compte_desc($account = null)
1973  {
1974  // phpcs:enable
1975  global $conf;
1976 
1977  $pcgver = $conf->global->CHARTOFACCOUNTS;
1978  $sql = "SELECT aa.account_number, aa.label, aa.rowid, aa.fk_pcg_version, cat.label as category";
1979  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_account as aa ";
1980  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
1981  $sql .= " AND aa.account_number = '".$this->db->escape($account)."'";
1982  $sql .= " AND asy.rowid = ".((int) $pcgver);
1983  $sql .= " AND aa.active = 1";
1984  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_accounting_category as cat ON aa.fk_accounting_category = cat.rowid";
1985  $sql .= " WHERE aa.entity IN (".getEntity('accountancy').")";
1986 
1987  dol_syslog(get_class($this)."::select_account sql=".$sql, LOG_DEBUG);
1988  $resql = $this->db->query($sql);
1989  if ($resql) {
1990  $obj = '';
1991  if ($this->db->num_rows($resql)) {
1992  $obj = $this->db->fetch_object($resql);
1993  }
1994  if (empty($obj->category)) {
1995  return $obj->label;
1996  } else {
1997  return $obj->label.' ('.$obj->category.')';
1998  }
1999  } else {
2000  $this->error = "Error ".$this->db->lasterror();
2001  dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
2002  return -1;
2003  }
2004  }
2005 }
2006 
2011 {
2015  public $id;
2016 
2017  public $doc_date = '';
2018  public $doc_type;
2019  public $doc_ref;
2020 
2024  public $fk_doc;
2025 
2029  public $fk_docdet;
2030 
2031  public $thirdparty_code;
2032  public $subledger_account;
2033  public $subledger_label;
2034  public $numero_compte;
2035  public $label_compte;
2036  public $label_operation;
2037  public $debit;
2038  public $credit;
2039 
2044  public $montant;
2045 
2049  public $amount;
2050 
2054  public $sens;
2055  public $lettering_code;
2056 
2060  public $fk_user_author;
2061 
2062  public $import_key;
2063  public $code_journal;
2064  public $journal_label;
2065  public $piece_num;
2066 
2070  public $date_creation;
2071 
2075  public $date_modification;
2076 
2080  public $date_export;
2081 }
deleteByImportkey($importkey)
Delete bookkeeping by importkey.
static selectarray($htmlname, $array, $id= '', $show_empty=0, $key_in_label=0, $value_as_key=0, $moreparam= '', $translate=0, $maxlen=0, $disabled=0, $sort= '', $morecss= '', $addjscombo=0, $moreparamonempty= '', $disablebademail=0, $nohtmlescape=0)
Return a HTML select string, built from an array of key+value.
updateByMvt($piece_num= '', $field= '', $value= '', $mode= '')
Update accounting movement.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
Class to manage Dolibarr database access.
deleteMvtNum($piecenum)
Delete bookkeeping by piece number.
export_bookkeeping($model= 'ebp')
Export bookkeeping.
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete= 'resolve')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:391
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
__construct(DoliDB $db)
Constructor.
$conf db
API class for accounts.
Definition: inc.php:54
update(User $user, $notrigger=false, $mode= '')
Update object into database.
Class to manage Ledger (General Ledger and Subledger)
transformTransaction($direction=0, $piece_num= '')
Transform transaction.
getRootAccount($account=null)
Return id and description of a root accounting account.
fetchAll($sortorder= '', $sortfield= '', $limit=0, $offset=0, array $filter=array(), $filtermode= 'AND', $showAlreadyExportMovements=1)
Load object in memory from the database.
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
select_account($selectid, $htmlname= 'account', $showempty=0, $event=array(), $select_in=0, $select_out=0, $aabase= '')
Return list of accounts with label by chart of accounts.
fetchAllBalance($sortorder= '', $sortfield= '', $limit=0, $offset=0, array $filter=array(), $filtermode= 'AND')
Load object in memory from the database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
getNextNumMvt($mode= '')
Return next number movement.
natural_search($fields, $value, $mode=0, $nofirstand=0)
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
dolSqlDateFilter($datefield, $day_date, $month_date, $year_date, $excludefirstand=0)
Generate a SQL string to make a filter into a range (for second of date until last second of date) ...
Definition: date.lib.php:281
print $_SERVER["PHP_SELF"]
Edit parameters.
fetch($id, $ref=null, $mode= '')
Load object in memory from the database.
div float
Buy price without taxes.
Definition: style.css.php:650
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
create(User $user, $notrigger=false)
Create object into database.
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
get_compte_desc($account=null)
Description of accounting account.
getNomUrl($withpicto=0, $option= '', $notooltip=0, $morecss= '', $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
deleteByYearAndJournal($delyear=0, $journal= '', $mode= '', $delmonth=0)
Delete bookkeeping by year.
fetchAllPerMvt($piecenum, $mode= '')
Load all informations of accountancy document.
fetchAllByAccount($sortorder= '', $sortfield= '', $limit=0, $offset=0, array $filter=array(), $filtermode= 'AND', $option=0)
Load object in memory from the database.
fetchPerMvt($piecenum, $mode= '')
Load an accounting document into memory from database.
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous) ...
createStd(User $user, $notrigger=false, $mode= '')
Create object into database.
Class BookKeepingLine.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)