28 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
38 public $element =
'accountingbookkeeping';
43 public $table_element =
'accounting_bookkeeping';
53 public $lines = array();
68 public $date_lim_reglement;
93 public $thirdparty_code;
98 public $subledger_account;
103 public $subledger_label;
108 public $numero_compte;
113 public $label_compte;
118 public $label_operation;
150 public $fk_user_author;
160 public $code_journal;
165 public $journal_label;
175 public $picto =
'generic';
197 global $conf, $langs;
204 if (isset($this->doc_type)) {
205 $this->doc_type = trim($this->doc_type);
207 if (isset($this->doc_ref)) {
208 $this->doc_ref = trim($this->doc_ref);
210 if (isset($this->fk_doc)) {
211 $this->fk_doc = (int) $this->fk_doc;
213 if (isset($this->fk_docdet)) {
214 $this->fk_docdet = (int) $this->fk_docdet;
216 if (isset($this->thirdparty_code)) {
217 $this->thirdparty_code = trim($this->thirdparty_code);
219 if (isset($this->subledger_account)) {
220 $this->subledger_account = trim($this->subledger_account);
222 if (isset($this->subledger_label)) {
223 $this->subledger_label = trim($this->subledger_label);
225 if (isset($this->numero_compte)) {
226 $this->numero_compte = trim($this->numero_compte);
228 if (isset($this->label_compte)) {
229 $this->label_compte = trim($this->label_compte);
231 if (isset($this->label_operation)) {
232 $this->label_operation = trim($this->label_operation);
234 if (isset($this->debit)) {
235 $this->debit = (
float) $this->debit;
237 if (isset($this->credit)) {
238 $this->credit = (
float) $this->credit;
240 if (isset($this->montant)) {
241 $this->montant = (
float) $this->montant;
243 if (isset($this->amount)) {
244 $this->amount = (
float) $this->amount;
246 if (isset($this->sens)) {
247 $this->sens = trim($this->sens);
249 if (isset($this->import_key)) {
250 $this->import_key = trim($this->import_key);
252 if (isset($this->code_journal)) {
253 $this->code_journal = trim($this->code_journal);
255 if (isset($this->journal_label)) {
256 $this->journal_label = trim($this->journal_label);
258 if (isset($this->piece_num)) {
259 $this->piece_num = trim($this->piece_num);
261 if (empty($this->debit)) $this->debit = 0.0;
262 if (empty($this->credit)) $this->credit = 0.0;
265 if (($this->numero_compte ==
"") || $this->numero_compte ==
'-1' || $this->numero_compte ==
'NotDefined')
267 $langs->loadLangs(array(
"errors"));
268 if (in_array($this->doc_type, array(
'bank',
'expense_report')))
270 $this->errors[] = $langs->trans(
'ErrorFieldAccountNotDefinedForBankLine', $this->fk_docdet, $this->doc_type);
273 $mesg = $this->doc_ref.
', '.$langs->trans(
"AccountAccounting").
': '.$this->numero_compte;
274 if ($this->subledger_account && $this->subledger_account != $this->numero_compte)
276 $mesg .=
', '.$langs->trans(
"SubledgerAccount").
': '.$this->subledger_account;
278 $this->errors[] = $langs->trans(
'ErrorFieldAccountNotDefinedForLine', $mesg);
286 $this->piece_num = 0;
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;
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').
")";
305 $row = $this->
db->fetch_object(
$resql);
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).
"'";
312 $sqlnum .=
" AND fk_docdet = ".$this->db->escape($this->fk_docdet);
313 $sqlnum .=
" AND doc_ref = '".$this->db->escape($this->doc_ref).
"'";
314 $sqlnum .=
" AND entity IN (".getEntity(
'accountancy').
")";
316 dol_syslog(get_class($this).
":: create sqlnum=".$sqlnum, LOG_DEBUG);
317 $resqlnum = $this->
db->query($sqlnum);
319 $objnum = $this->
db->fetch_object($resqlnum);
320 $this->piece_num = $objnum->piece_num;
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').
")";
329 dol_syslog(get_class($this).
":: create sqlnum=".$sqlnum, LOG_DEBUG);
330 $resqlnum = $this->
db->query($sqlnum);
332 $objnum = $this->
db->fetch_object($resqlnum);
333 $this->piece_num = $objnum->maxpiecenum;
335 dol_syslog(get_class($this).
":: create this->piece_num=".$this->piece_num, LOG_DEBUG);
337 if (empty($this->piece_num)) {
338 $this->piece_num = 1;
343 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (";
345 $sql .=
", date_lim_reglement";
346 $sql .=
", doc_type";
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";
360 $sql .=
", fk_user_author";
361 $sql .=
", date_creation";
362 $sql .=
", code_journal";
363 $sql .=
", journal_label";
364 $sql .=
", piece_num";
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);
393 $id = $this->
db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
401 $this->errors[] =
'Error Create Error '.$result.
' lecture ID';
402 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
407 $this->errors[] =
'Error '.$this->db->lasterror();
408 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
413 $this->error =
'BookkeepingRecordAlreadyExists';
414 dol_syslog(__METHOD__.
' '.$this->error, LOG_WARNING);
419 $this->errors[] =
'Error '.$this->db->lasterror();
420 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
435 $this->
db->rollback();
453 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
455 global $db, $conf, $langs;
456 global $dolibarr_main_authentication, $dolibarr_main_demo;
459 if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1;
464 $label =
'<u>'.$langs->trans(
"Transaction").
'</u>';
466 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->piece_num;
468 $url = DOL_URL_ROOT.
'/accountancy/bookkeeping/card.php?piece_num='.$this->piece_num;
470 if ($option !=
'nolink')
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';
479 if (empty($notooltip))
481 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
483 $label = $langs->trans(
"ShowTransaction");
484 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
486 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
487 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
488 }
else $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
490 $linkstart =
'<a href="'.$url.
'"';
491 $linkstart .= $linkclose.
'>';
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;
513 global $conf, $langs;
515 $langs->loadLangs(array(
"accountancy",
"bills",
"compta"));
522 if (isset($this->doc_type)) {
523 $this->doc_type = trim($this->doc_type);
525 if (isset($this->doc_ref)) {
526 $this->doc_ref = trim($this->doc_ref);
528 if (isset($this->fk_doc)) {
529 $this->fk_doc = (int) $this->fk_doc;
531 if (isset($this->fk_docdet)) {
532 $this->fk_docdet = (int) $this->fk_docdet;
534 if (isset($this->thirdparty_code)) {
535 $this->thirdparty_code = trim($this->thirdparty_code);
537 if (isset($this->subledger_account)) {
538 $this->subledger_account = trim($this->subledger_account);
540 if (isset($this->subledger_label)) {
541 $this->subledger_label = trim($this->subledger_label);
543 if (isset($this->numero_compte)) {
544 $this->numero_compte = trim($this->numero_compte);
546 if (isset($this->label_compte)) {
547 $this->label_compte = trim($this->label_compte);
549 if (isset($this->label_operation)) {
550 $this->label_operation = trim($this->label_operation);
552 if (isset($this->debit)) {
553 $this->debit = trim($this->debit);
555 if (isset($this->credit)) {
556 $this->credit = trim($this->credit);
558 if (isset($this->montant)) {
559 $this->montant = trim($this->montant);
561 if (isset($this->amount)) {
562 $this->amount = trim($this->amount);
564 if (isset($this->sens)) {
565 $this->sens = trim($this->sens);
567 if (isset($this->import_key)) {
568 $this->import_key = trim($this->import_key);
570 if (isset($this->code_journal)) {
571 $this->code_journal = trim($this->code_journal);
573 if (isset($this->journal_label)) {
574 $this->journal_label = trim($this->journal_label);
576 if (isset($this->piece_num)) {
577 $this->piece_num = trim($this->piece_num);
579 if (empty($this->debit)) $this->debit = 0;
580 if (empty($this->credit)) $this->credit = 0;
582 $this->debit =
price2num($this->debit,
'MT');
583 $this->credit =
price2num($this->credit,
'MT');
588 $this->journal_label = $langs->trans($this->journal_label);
591 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.$mode.
' (';
593 $sql .=
'date_lim_reglement,';
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,';
608 $sql .=
'fk_user_author,';
609 $sql .=
'date_creation,';
610 $sql .=
'code_journal,';
611 $sql .=
'journal_label,';
612 $sql .=
'piece_num,';
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);
644 $this->errors[] =
'Error '.$this->db->lasterror();
645 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
649 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.$this->table_element.$mode);
664 $this->
db->rollback();
683 public function fetch($id, $ref = null, $mode =
'')
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,";
704 $sql .=
" t.credit,";
705 $sql .=
" t.montant as amount,";
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').
")";
717 $sql .=
" AND t.ref = '".$this->db->escape($ref).
"'";
719 $sql .=
' AND t.rowid = '.$id;
724 $numrows = $this->
db->num_rows(
$resql);
726 $obj = $this->
db->fetch_object(
$resql);
728 $this->
id = $obj->rowid;
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);
762 $this->errors[] =
'Error '.$this->db->lasterror();
763 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
783 public function fetchAllByAccount($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND', $option = 0)
789 $this->lines = array();
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,";
805 $sql .=
" t.credit,";
806 $sql .=
" t.montant as amount,";
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";
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') {
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);
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);
859 if (!empty($option)) {
860 $sql .=
' AND t.subledger_account IS NOT NULL';
861 $sql .=
' ORDER BY t.subledger_account ASC';
863 $sql .=
' ORDER BY t.numero_compte ASC';
866 if (!empty($sortfield)) {
867 $sql .=
', '.$sortfield.
' '.$sortorder;
869 if (!empty($limit)) {
870 $sql .=
' '.$this->db->plimit($limit + 1, $offset);
878 while (($obj = $this->
db->fetch_object(
$resql)) && (empty($limit) || $i < min($limit, $num))) {
881 $line->id = $obj->rowid;
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;
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);
911 $this->lines[] = $line;
919 $this->errors[] =
'Error '.$this->db->lasterror();
920 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
938 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND', $showAlreadyExportMovements = 1)
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,";
958 $sql .=
" t.credit,";
959 $sql .=
" t.lettering_code,";
960 $sql .=
" t.date_lettering,";
961 $sql .=
" t.montant as amount,";
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';
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') {
1000 $sql .=
' WHERE t.entity IN ('.getEntity(
'accountancy').
')';
1001 if ($showAlreadyExportMovements == 0) {
1002 $sql .=
" AND t.date_export IS NULL";
1004 if (count($sqlwhere) > 0) {
1005 $sql .=
' AND '.implode(
' '.$filtermode.
' ', $sqlwhere);
1007 if (!empty($sortfield)) {
1008 $sql .= $this->
db->order($sortfield, $sortorder);
1010 if (!empty($limit)) {
1011 $sql .=
' '.$this->db->plimit($limit + 1, $offset);
1013 $this->lines = array();
1017 $num = $this->
db->num_rows(
$resql);
1020 while (($obj = $this->
db->fetch_object(
$resql)) && (empty($limit) || $i < min($limit, $num)))
1024 $line->id = $obj->rowid;
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;
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);
1054 $this->lines[] = $line;
1062 $this->errors[] =
'Error '.$this->db->lasterror();
1063 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1080 public function fetchAllBalance($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
1084 $this->lines = array();
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';
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).
'%\'';
1110 $sqlwhere[] = $key.
' LIKE \'%'.$this->
db->escape($value).
'%\'';
1114 $sql .=
' WHERE entity IN ('.getEntity(
'accountancy').
')';
1115 if (count($sqlwhere) > 0) {
1116 $sql .=
' AND '.implode(
' '.$filtermode.
' ', $sqlwhere);
1119 $sql .=
' GROUP BY t.numero_compte';
1121 if (!empty($sortfield)) {
1122 $sql .= $this->
db->order($sortfield, $sortorder);
1124 if (!empty($limit)) {
1125 $sql .=
' '.$this->db->plimit($limit + 1, $offset);
1131 $num = $this->
db->num_rows(
$resql);
1134 while (($obj = $this->
db->fetch_object(
$resql)) && (empty($limit) || $i < min($limit, $num)))
1138 $line->numero_compte = $obj->numero_compte;
1139 $line->debit = $obj->debit;
1140 $line->credit = $obj->credit;
1142 $this->lines[] = $line;
1150 $this->errors[] =
'Error '.$this->db->lasterror();
1151 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1165 public function update(
User $user, $notrigger =
false, $mode =
'')
1172 if (isset($this->doc_type)) {
1173 $this->doc_type = trim($this->doc_type);
1175 if (isset($this->doc_ref)) {
1176 $this->doc_ref = trim($this->doc_ref);
1178 if (isset($this->fk_doc)) {
1179 $this->fk_doc = (int) $this->fk_doc;
1181 if (isset($this->fk_docdet)) {
1182 $this->fk_docdet = (int) $this->fk_docdet;
1184 if (isset($this->thirdparty_code)) {
1185 $this->thirdparty_code = trim($this->thirdparty_code);
1187 if (isset($this->subledger_account)) {
1188 $this->subledger_account = trim($this->subledger_account);
1190 if (isset($this->subledger_label)) {
1191 $this->subledger_label = trim($this->subledger_label);
1193 if (isset($this->numero_compte)) {
1194 $this->numero_compte = trim($this->numero_compte);
1196 if (isset($this->label_compte)) {
1197 $this->label_compte = trim($this->label_compte);
1199 if (isset($this->label_operation)) {
1200 $this->label_operation = trim($this->label_operation);
1202 if (isset($this->debit)) {
1203 $this->debit = trim($this->debit);
1205 if (isset($this->credit)) {
1206 $this->credit = trim($this->credit);
1208 if (isset($this->amount)) {
1209 $this->amount = trim($this->amount);
1211 if (isset($this->sens)) {
1212 $this->sens = trim($this->sens);
1214 if (isset($this->import_key)) {
1215 $this->import_key = trim($this->import_key);
1217 if (isset($this->code_journal)) {
1218 $this->code_journal = trim($this->code_journal);
1220 if (isset($this->journal_label)) {
1221 $this->journal_label = trim($this->journal_label);
1223 if (isset($this->piece_num)) {
1224 $this->piece_num = trim($this->piece_num);
1227 $this->debit =
price2num($this->debit,
'MT');
1228 $this->credit =
price2num($this->credit,
'MT');
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;
1262 $this->errors[] =
'Error '.$this->db->lasterror();
1263 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1278 $this->
db->rollback();
1282 $this->
db->commit();
1297 public function updateByMvt($piece_num =
'', $field =
'', $value =
'', $mode =
'')
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).
"'";
1310 $this->errors[] =
'Error '.$this->db->lasterror();
1311 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1314 $this->
db->rollback();
1318 $this->
db->commit();
1332 public function delete(
User $user, $notrigger =
false, $mode =
'')
1351 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.$mode;
1352 $sql .=
' WHERE rowid='.$this->id;
1357 $this->errors[] =
'Error '.$this->db->lasterror();
1358 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1364 $this->
db->rollback();
1368 $this->
db->commit();
1386 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element;
1387 $sql .=
" WHERE import_key = '".$this->db->escape($importkey).
"'";
1392 $this->errors[] =
"Error ".$this->db->lasterror();
1393 dol_syslog(get_class($this).
"::delete Error ".$this->
db->lasterror(), LOG_ERR);
1394 $this->
db->rollback();
1398 $this->
db->commit();
1415 if (empty($delyear) && empty($journal))
1417 $this->error =
'ErrorOneFieldRequired';
1420 if (!empty($delmonth) && empty($delyear))
1422 $this->error =
'YearRequiredIfMonthDefined';
1430 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1431 $sql .=
" WHERE 1 = 1";
1433 if (!empty($journal)) $sql .=
" AND code_journal = '".$this->db->escape($journal).
"'";
1434 $sql .=
" AND entity IN (".getEntity(
'accountancy').
")";
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);
1446 $this->
db->rollback();
1450 $this->
db->commit();
1468 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element;
1469 $sql .=
" WHERE piece_num = ".(int) $piecenum;
1470 $sql .=
" AND entity IN (".getEntity(
'accountancy').
")";
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);
1480 $this->
db->rollback();
1484 $this->
db->commit();
1505 $object->fetch($fromid);
1513 $object->context[
'createfromclone'] =
'createfromclone';
1514 $result = $object->create($user);
1519 $this->errors = $object->errors;
1520 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1523 unset($object->context[
'createfromclone']);
1527 $this->
db->commit();
1531 $this->
db->rollback();
1550 $this->doc_date = $now;
1551 $this->doc_type =
'';
1552 $this->doc_ref =
'';
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;
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;
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').
")";
1590 $result = $this->
db->query($sql);
1592 $obj = $this->
db->fetch_object($result);
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;
1602 $this->error =
"Error ".$this->db->lasterror();
1603 dol_syslog(__METHOD__.$this->error, LOG_ERR);
1620 $sql =
"SELECT MAX(piece_num)+1 as max FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1621 $sql .=
" WHERE entity IN (".getEntity(
'accountancy').
")";
1623 dol_syslog(get_class($this).
"getNextNumMvt sql=".$sql, LOG_DEBUG);
1624 $result = $this->
db->query($sql);
1627 $obj = $this->
db->fetch_object($result);
1628 if ($obj) $result = $obj->max;
1629 if (empty($result)) $result = 1;
1632 $this->error =
"Error ".$this->db->lasterror();
1633 dol_syslog(get_class($this).
"::getNextNumMvt ".$this->error, LOG_ERR);
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').
")";
1658 $result = $this->
db->query($sql);
1660 while ($obj = $this->
db->fetch_object($result)) {
1663 $line->id = $obj->rowid;
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;
1686 $this->linesmvt[] = $line;
1689 $this->error =
"Error ".$this->db->lasterror();
1690 dol_syslog(__METHOD__.$this->error, LOG_ERR);
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').
")";
1716 dol_syslog(get_class($this).
"::export_bookkeeping", LOG_DEBUG);
1721 $this->linesexport = array();
1723 $num = $this->
db->num_rows(
$resql);
1724 while ($obj = $this->
db->fetch_object(
$resql)) {
1727 $line->id = $obj->rowid;
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;
1748 $this->linesexport[] = $line;
1754 $this->error =
"Error ".$this->db->lasterror();
1755 dol_syslog(get_class($this).
"::export_bookkeeping ".$this->error, LOG_ERR);
1773 if ($direction == 0)
1778 if ($next_piecenum < 0) {
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);
1793 $this->errors[] =
'Error '.$this->db->lasterror();
1794 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1796 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
'_tmp WHERE piece_num = '.((int) $piece_num);
1800 $this->errors[] =
'Error '.$this->db->lasterror();
1801 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1803 } elseif ($direction == 1) {
1804 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
'_tmp WHERE piece_num = '.((int) $piece_num);
1808 $this->errors[] =
'Error '.$this->db->lasterror();
1809 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
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);
1823 $this->errors[] =
'Error '.$this->db->lasterror();
1824 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1826 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
'_tmp WHERE piece_num = '.((int) $piece_num);
1830 $this->errors[] =
'Error '.$this->db->lasterror();
1831 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1835 $this->
db->commit();
1838 $this->
db->rollback();
1866 public function select_account($selectid, $htmlname =
'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $aabase =
'')
1871 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
1873 $pcgver = $conf->global->CHARTOFACCOUNTS;
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";
1884 dol_syslog(get_class($this).
"::select_account", LOG_DEBUG);
1888 $this->error =
"Error ".$this->db->lasterror();
1889 dol_syslog(get_class($this).
"::select_account ".$this->error, LOG_ERR);
1898 while ($obj = $this->
db->fetch_object(
$resql)) {
1901 $select_value_in = $obj->rowid;
1902 $select_value_out = $obj->rowid;
1904 if ($select_in == 1) {
1905 $select_value_in = $obj->account_number;
1907 if ($select_out == 1) {
1908 $select_value_out = $obj->account_number;
1913 if (($selectid !=
'') && $selectid == $select_value_in) {
1914 $selected = $select_value_out;
1917 $options[$select_value_out] = $label;
1920 $out .=
Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0,
'', 0, 0, 0,
'',
'maxwidth300');
1935 $pcgver = $conf->global->CHARTOFACCOUNTS;
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').
")";
1947 dol_syslog(get_class($this).
"::select_account sql=".$sql, LOG_DEBUG);
1952 $obj = $this->
db->fetch_object(
$resql);
1955 $result = array(
'id'=>$obj->rowid,
'account_number'=>$obj->account_number,
'label'=>$obj->label);
1958 $this->error =
"Error ".$this->db->lasterror();
1959 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
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').
")";
1987 dol_syslog(get_class($this).
"::select_account sql=".$sql, LOG_DEBUG);
1992 $obj = $this->
db->fetch_object(
$resql);
1994 if (empty($obj->category)) {
1997 return $obj->label.
' ('.$obj->category.
')';
2000 $this->error =
"Error ".$this->db->lasterror();
2001 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
2017 public $doc_date =
'';
2031 public $thirdparty_code;
2032 public $subledger_account;
2033 public $subledger_label;
2034 public $numero_compte;
2035 public $label_compte;
2036 public $label_operation;
2055 public $lettering_code;
2060 public $fk_user_author;
2063 public $code_journal;
2064 public $journal_label;
2070 public $date_creation;
2075 public $date_modification;
2080 public $date_export;
deleteByImportkey($importkey)
Delete bookkeeping by importkey.
updateByMvt($piece_num= '', $field= '', $value= '', $mode= '')
Update accounting movement.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
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.
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.
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 '...
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) ...
print $_SERVER["PHP_SELF"]
Edit parameters.
fetch($id, $ref=null, $mode= '')
Load object in memory from the database.
div float
Buy price without taxes.
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.
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.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)