28require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
29require_once DOL_DOCUMENT_ROOT.
'/core/class/fiscalyear.class.php';
30require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingjournal.class.php';
31require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingaccount.class.php';
41 public $element =
'accountingbookkeeping';
46 public $table_element =
'accounting_bookkeeping';
56 public $lines = array();
71 public $date_lim_reglement;
96 public $thirdparty_code;
101 public $subledger_account;
106 public $subledger_label;
111 public $numero_compte;
116 public $label_compte;
121 public $label_operation;
153 public $fk_user_author;
163 public $code_journal;
168 public $journal_label;
178 public $linesmvt = array();
183 public $linesexport = array();
188 public $date_validation;
198 public $picto =
'generic';
203 public static $can_modify_bookkeeping_sql_cached;
225 global $conf, $langs;
232 if (isset($this->doc_type)) {
233 $this->doc_type = trim($this->doc_type);
235 if (isset($this->doc_ref)) {
236 $this->doc_ref = trim($this->doc_ref);
238 if (isset($this->fk_doc)) {
239 $this->fk_doc = (int) $this->fk_doc;
241 if (isset($this->fk_docdet)) {
242 $this->fk_docdet = (int) $this->fk_docdet;
244 if (isset($this->thirdparty_code)) {
245 $this->thirdparty_code = trim($this->thirdparty_code);
247 if (isset($this->subledger_account)) {
248 $this->subledger_account = trim($this->subledger_account);
250 if (isset($this->subledger_label)) {
251 $this->subledger_label = trim($this->subledger_label);
253 if (isset($this->numero_compte)) {
254 $this->numero_compte = trim($this->numero_compte);
256 if (isset($this->label_compte)) {
257 $this->label_compte = trim($this->label_compte);
259 if (isset($this->label_operation)) {
260 $this->label_operation = trim($this->label_operation);
262 if (isset($this->debit)) {
263 $this->debit = (float) $this->debit;
265 if (isset($this->credit)) {
266 $this->credit = (float) $this->credit;
268 if (isset($this->montant)) {
269 $this->montant = (float) $this->montant;
271 if (isset($this->amount)) {
272 $this->amount = (float) $this->amount;
274 if (isset($this->sens)) {
275 $this->sens = trim($this->sens);
277 if (isset($this->import_key)) {
278 $this->import_key = trim($this->import_key);
280 if (isset($this->code_journal)) {
281 $this->code_journal = trim($this->code_journal);
283 if (isset($this->journal_label)) {
284 $this->journal_label = trim($this->journal_label);
286 if (isset($this->piece_num)) {
287 $this->piece_num = trim($this->piece_num);
289 if (empty($this->debit)) {
292 if (empty($this->credit)) {
299 } elseif ($result == 0) {
301 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateIsOnAClosedFiscalPeriod');
303 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateNotOnActiveFiscalPeriod');
309 if (($this->numero_compte ==
"") || $this->numero_compte ==
'-1' || $this->numero_compte ==
'NotDefined') {
310 $langs->loadLangs(array(
"errors"));
311 if (in_array($this->doc_type, array(
'bank',
'expense_report'))) {
312 $this->errors[] = $langs->trans(
'ErrorFieldAccountNotDefinedForBankLine', $this->fk_docdet, $this->doc_type);
315 $mesg = $this->doc_ref.
', '.$langs->trans(
"AccountAccounting").
': '.($this->numero_compte != -1 ? $this->numero_compte : $langs->trans(
"Unknown"));
316 if ($this->subledger_account && $this->subledger_account != $this->numero_compte) {
317 $mesg .=
', '.$langs->trans(
"SubledgerAccount").
': '.$this->subledger_account;
319 $this->errors[] = $langs->trans(
'ErrorFieldAccountNotDefinedForLine', $mesg);
327 $this->piece_num = 0;
334 $sql =
"SELECT count(*) as nb";
335 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element;
336 $sql .=
" WHERE doc_type = '".$this->db->escape($this->doc_type).
"'";
337 $sql .=
" AND fk_doc = ".((int) $this->fk_doc);
340 $sql .=
" AND fk_docdet = ".((int) $this->fk_docdet);
342 $sql .=
" AND numero_compte = '".$this->db->escape($this->numero_compte).
"'";
343 $sql .=
" AND label_operation = '".$this->db->escape($this->label_operation).
"'";
344 $sql .=
" AND entity = ".$conf->entity;
346 $resql = $this->db->query($sql);
349 $row = $this->db->fetch_object($resql);
352 $sqlnum =
"SELECT piece_num";
353 $sqlnum .=
" FROM ".MAIN_DB_PREFIX.$this->table_element;
354 $sqlnum .=
" WHERE doc_type = '".$this->db->escape($this->doc_type).
"'";
355 $sqlnum .=
" AND fk_doc = ".((int) $this->fk_doc);
358 $sqlnum .=
" AND fk_docdet = ".((int) $this->fk_docdet);
360 $sqlnum .=
" AND doc_ref = '".$this->db->escape($this->doc_ref).
"'";
361 $sqlnum .=
" AND entity = ".$conf->entity;
363 dol_syslog(get_class($this).
":: create sqlnum=".$sqlnum, LOG_DEBUG);
364 $resqlnum = $this->db->query($sqlnum);
366 $objnum = $this->db->fetch_object($resqlnum);
367 $this->piece_num = $objnum->piece_num;
370 dol_syslog(get_class($this).
"::create this->piece_num=".$this->piece_num, LOG_DEBUG);
371 if (empty($this->piece_num)) {
372 $sqlnum =
"SELECT MAX(piece_num)+1 as maxpiecenum";
373 $sqlnum .=
" FROM ".MAIN_DB_PREFIX.$this->table_element;
374 $sqlnum .=
" WHERE entity = " . ((int) $conf->entity);
376 $resqlnum = $this->db->query($sqlnum);
378 $objnum = $this->db->fetch_object($resqlnum);
379 $this->piece_num = $objnum->maxpiecenum;
381 dol_syslog(get_class($this).
":: create now this->piece_num=".$this->piece_num, LOG_DEBUG);
383 if (empty($this->piece_num)) {
384 $this->piece_num = 1;
389 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (";
391 $sql .=
", date_lim_reglement";
392 $sql .=
", doc_type";
395 $sql .=
", fk_docdet";
396 $sql .=
", thirdparty_code";
397 $sql .=
", subledger_account";
398 $sql .=
", subledger_label";
399 $sql .=
", numero_compte";
400 $sql .=
", label_compte";
401 $sql .=
", label_operation";
406 $sql .=
", fk_user_author";
407 $sql .=
", date_creation";
408 $sql .=
", code_journal";
409 $sql .=
", journal_label";
410 $sql .=
", piece_num";
412 $sql .=
") VALUES (";
413 $sql .=
"'".$this->db->idate($this->doc_date).
"'";
414 $sql .=
", ".(!isset($this->date_lim_reglement) ||
dol_strlen($this->date_lim_reglement) == 0 ?
'NULL' :
"'".$this->db->idate($this->date_lim_reglement).
"'");
415 $sql .=
", '".$this->db->escape($this->doc_type).
"'";
416 $sql .=
", '".$this->db->escape($this->doc_ref).
"'";
417 $sql .=
", ".((int) $this->fk_doc);
418 $sql .=
", ".((int) $this->fk_docdet);
419 $sql .=
", ".(!empty($this->thirdparty_code) ? (
"'".$this->db->escape($this->thirdparty_code).
"'") :
"NULL");
420 $sql .=
", ".(!empty($this->subledger_account) ? (
"'".$this->db->escape($this->subledger_account).
"'") :
"NULL");
421 $sql .=
", ".(!empty($this->subledger_label) ? (
"'".$this->db->escape($this->subledger_label).
"'") :
"NULL");
422 $sql .=
", '".$this->db->escape($this->numero_compte).
"'";
423 $sql .=
", ".(!empty($this->label_compte) ? (
"'".$this->db->escape($this->label_compte).
"'") :
"NULL");
424 $sql .=
", '".$this->db->escape($this->label_operation).
"'";
425 $sql .=
", ".((float) $this->debit);
426 $sql .=
", ".((float) $this->credit);
427 $sql .=
", ".((float) $this->montant);
428 $sql .=
", ".(!empty($this->sens) ? (
"'".$this->db->escape($this->sens).
"'") :
"NULL");
429 $sql .=
", '".$this->db->escape($this->fk_user_author).
"'";
430 $sql .=
", '".$this->db->idate($now).
"'";
431 $sql .=
", '".$this->db->escape($this->code_journal).
"'";
432 $sql .=
", ".(!empty($this->journal_label) ? (
"'".$this->db->escape($this->journal_label).
"'") :
"NULL");
433 $sql .=
", ".((int) $this->piece_num);
434 $sql .=
", ".(!isset($this->entity) ? $conf->entity : $this->entity);
437 $resql = $this->db->query($sql);
439 $id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
447 $this->errors[] =
'Error Create Error '.$result.
' lecture ID';
448 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
453 $this->errors[] =
'Error '.$this->db->lasterror();
454 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
459 $this->error =
'BookkeepingRecordAlreadyExists';
460 dol_syslog(__METHOD__.
' '.$this->error, LOG_WARNING);
465 $this->errors[] =
'Error '.$this->db->lasterror();
466 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
481 $this->db->rollback();
499 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
501 global $db, $conf, $langs;
502 global $dolibarr_main_authentication, $dolibarr_main_demo;
503 global $menumanager, $hookmanager;
505 if (!empty($conf->dol_no_mouse_hover)) {
512 $label =
'<u>'.$langs->trans(
"Transaction").
'</u>';
514 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->piece_num;
516 $url = DOL_URL_ROOT.
'/accountancy/bookkeeping/card.php?piece_num='.$this->piece_num;
518 if ($option !=
'nolink') {
520 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
521 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
522 $add_save_lastsearch_values = 1;
524 if ($add_save_lastsearch_values) {
525 $url .=
'&save_lastsearch_values=1';
530 if (empty($notooltip)) {
532 $label = $langs->trans(
"ShowTransaction");
533 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
535 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
536 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
538 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
541 $linkstart =
'<a href="'.$url.
'"';
542 $linkstart .= $linkclose.
'>';
545 $result .= $linkstart;
547 $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);
549 if ($withpicto != 2) {
550 $result .= $this->piece_num;
556 $hookmanager->initHooks(array($this->element .
'dao'));
557 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
558 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
560 $result = $hookmanager->resPrint;
562 $result .= $hookmanager->resPrint;
577 global $conf, $langs;
579 $langs->loadLangs(array(
"accountancy",
"bills",
"compta"));
586 if (isset($this->doc_type)) {
587 $this->doc_type = trim($this->doc_type);
589 if (isset($this->doc_ref)) {
590 $this->doc_ref = trim($this->doc_ref);
592 if (isset($this->fk_doc)) {
593 $this->fk_doc = (int) $this->fk_doc;
595 if (isset($this->fk_docdet)) {
596 $this->fk_docdet = (int) $this->fk_docdet;
598 if (isset($this->thirdparty_code)) {
599 $this->thirdparty_code = trim($this->thirdparty_code);
601 if (isset($this->subledger_account)) {
602 $this->subledger_account = trim($this->subledger_account);
604 if (isset($this->subledger_label)) {
605 $this->subledger_label = trim($this->subledger_label);
607 if (isset($this->numero_compte)) {
608 $this->numero_compte = trim($this->numero_compte);
610 if (isset($this->label_compte)) {
611 $this->label_compte = trim($this->label_compte);
613 if (isset($this->label_operation)) {
614 $this->label_operation = trim($this->label_operation);
616 if (isset($this->debit)) {
617 $this->debit = trim($this->debit);
619 if (isset($this->credit)) {
620 $this->credit = trim($this->credit);
622 if (isset($this->montant)) {
623 $this->montant = trim($this->montant);
625 if (isset($this->amount)) {
626 $this->amount = trim($this->amount);
628 if (isset($this->sens)) {
629 $this->sens = trim($this->sens);
631 if (isset($this->import_key)) {
632 $this->import_key = trim($this->import_key);
634 if (isset($this->code_journal)) {
635 $this->code_journal = trim($this->code_journal);
637 if (isset($this->journal_label)) {
638 $this->journal_label = trim($this->journal_label);
640 if (isset($this->piece_num)) {
641 $this->piece_num = trim($this->piece_num);
643 if (empty($this->debit)) {
646 if (empty($this->credit)) {
649 if (empty($this->montant)) {
656 } elseif ($result == 0) {
658 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateIsOnAClosedFiscalPeriod');
660 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateNotOnActiveFiscalPeriod');
665 $this->debit =
price2num($this->debit,
'MT');
666 $this->credit =
price2num($this->credit,
'MT');
667 $this->montant =
price2num($this->montant,
'MT');
672 $this->journal_label = $langs->trans($this->journal_label);
675 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.$mode.
' (';
677 $sql .=
'date_lim_reglement,';
681 $sql .=
'fk_docdet,';
682 $sql .=
'thirdparty_code,';
683 $sql .=
'subledger_account,';
684 $sql .=
'subledger_label,';
685 $sql .=
'numero_compte,';
686 $sql .=
'label_compte,';
687 $sql .=
'label_operation,';
692 $sql .=
'fk_user_author,';
693 $sql .=
'date_creation,';
694 $sql .=
'code_journal,';
695 $sql .=
'journal_label,';
696 $sql .=
'piece_num,';
698 $sql .=
') VALUES (';
699 $sql .=
' '.(!isset($this->doc_date) ||
dol_strlen($this->doc_date) == 0 ?
'NULL' :
"'".$this->db->idate($this->doc_date).
"'").
',';
700 $sql .=
' '.(!isset($this->date_lim_reglement) ||
dol_strlen($this->date_lim_reglement) == 0 ?
'NULL' :
"'".$this->db->idate($this->date_lim_reglement).
"'").
',';
701 $sql .=
' '.(!isset($this->doc_type) ?
'NULL' :
"'".$this->db->escape($this->doc_type).
"'").
',';
702 $sql .=
' '.(!isset($this->doc_ref) ?
'NULL' :
"'".$this->db->escape($this->doc_ref).
"'").
',';
703 $sql .=
' '.(empty($this->fk_doc) ?
'0' : (int) $this->fk_doc).
',';
704 $sql .=
' '.(empty($this->fk_docdet) ?
'0' : (int) $this->fk_docdet).
',';
705 $sql .=
' '.(!isset($this->thirdparty_code) ?
'NULL' :
"'".$this->db->escape($this->thirdparty_code).
"'").
',';
706 $sql .=
' '.(!isset($this->subledger_account) ?
'NULL' :
"'".$this->db->escape($this->subledger_account).
"'").
',';
707 $sql .=
' '.(!isset($this->subledger_label) ?
'NULL' :
"'".$this->db->escape($this->subledger_label).
"'").
',';
708 $sql .=
' '.(!isset($this->numero_compte) ?
'NULL' :
"'".$this->db->escape($this->numero_compte).
"'").
',';
709 $sql .=
' '.(!isset($this->label_compte) ?
'NULL' :
"'".$this->db->escape($this->label_compte).
"'").
',';
710 $sql .=
' '.(!isset($this->label_operation) ?
'NULL' :
"'".$this->db->escape($this->label_operation).
"'").
',';
711 $sql .=
' '.(!isset($this->debit) ?
'NULL' : $this->debit).
',';
712 $sql .=
' '.(!isset($this->credit) ?
'NULL' : $this->credit).
',';
713 $sql .=
' '.(!isset($this->montant) ?
'NULL' : $this->montant).
',';
714 $sql .=
' '.(!isset($this->sens) ?
'NULL' :
"'".$this->db->escape($this->sens).
"'").
',';
715 $sql .=
' '.((int) $user->id).
',';
716 $sql .=
' '.
"'".$this->db->idate($now).
"',";
717 $sql .=
' '.(empty($this->code_journal) ?
'NULL' :
"'".$this->db->escape($this->code_journal).
"'").
',';
718 $sql .=
' '.(empty($this->journal_label) ?
'NULL' :
"'".$this->db->escape($this->journal_label).
"'").
',';
719 $sql .=
' '.(empty($this->piece_num) ?
'NULL' : $this->db->escape($this->piece_num)).
',';
720 $sql .=
' '.(!isset($this->entity) ? $conf->entity : $this->entity);
725 $resql = $this->db->query($sql);
728 $this->errors[] =
'Error '.$this->db->lasterror();
729 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
733 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element.$mode);
748 $this->db->rollback();
766 public function fetch($id, $ref =
null, $mode =
'')
774 $sql .=
" t.doc_date,";
775 $sql .=
" t.date_lim_reglement,";
776 $sql .=
" t.doc_type,";
777 $sql .=
" t.doc_ref,";
778 $sql .=
" t.fk_doc,";
779 $sql .=
" t.fk_docdet,";
780 $sql .=
" t.thirdparty_code,";
781 $sql .=
" t.subledger_account,";
782 $sql .=
" t.subledger_label,";
783 $sql .=
" t.numero_compte,";
784 $sql .=
" t.label_compte,";
785 $sql .=
" t.label_operation,";
787 $sql .=
" t.credit,";
788 $sql .=
" t.montant as amount,";
790 $sql .=
" t.fk_user_author,";
791 $sql .=
" t.import_key,";
792 $sql .=
" t.code_journal,";
793 $sql .=
" t.journal_label,";
794 $sql .=
" t.piece_num,";
795 $sql .=
" t.date_creation,";
797 if ($mode !=
"_tmp") {
798 $sql .=
" t.date_export,";
800 $sql .=
" t.date_validated as date_validation";
801 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.$mode.
' as t';
802 $sql .=
' WHERE 1 = 1';
803 $sql .=
" AND entity = " . ((int) $conf->entity);
805 $sql .=
" AND t.rowid = ".((int) $ref);
807 $sql .=
" AND t.rowid = ".((int) $id);
810 $resql = $this->db->query($sql);
812 $numrows = $this->db->num_rows($resql);
814 $obj = $this->db->fetch_object($resql);
816 $this->
id = $obj->rowid;
818 $this->doc_date = $this->db->jdate($obj->doc_date);
819 $this->date_lim_reglement = $this->db->jdate($obj->date_lim_reglement);
820 $this->doc_type = $obj->doc_type;
821 $this->doc_ref = $obj->doc_ref;
822 $this->fk_doc = $obj->fk_doc;
823 $this->fk_docdet = $obj->fk_docdet;
824 $this->thirdparty_code = $obj->thirdparty_code;
825 $this->subledger_account = $obj->subledger_account;
826 $this->subledger_label = $obj->subledger_label;
827 $this->numero_compte = $obj->numero_compte;
828 $this->label_compte = $obj->label_compte;
829 $this->label_operation = $obj->label_operation;
830 $this->debit = $obj->debit;
831 $this->credit = $obj->credit;
832 $this->montant = $obj->amount;
833 $this->amount = $obj->amount;
834 $this->sens = $obj->sens;
835 $this->fk_user_author = $obj->fk_user_author;
836 $this->import_key = $obj->import_key;
837 $this->code_journal = $obj->code_journal;
838 $this->journal_label = $obj->journal_label;
839 $this->piece_num = $obj->piece_num;
840 $this->date_creation = $this->db->jdate($obj->date_creation);
841 $this->date_export = $this->db->jdate($obj->date_export);
842 $this->date_validation = isset($obj->date_validation) ? $this->db->jdate($obj->date_validation) :
'';
844 $this->db->free($resql);
852 $this->errors[] =
'Error '.$this->db->lasterror();
853 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
873 public function fetchAllByAccount($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND', $option = 0, $countonly = 0)
879 $this->lines = array();
884 $sql .=
' COUNT(t.rowid) as nb';
887 $sql .=
" t.doc_date,";
888 $sql .=
" t.doc_type,";
889 $sql .=
" t.doc_ref,";
890 $sql .=
" t.fk_doc,";
891 $sql .=
" t.fk_docdet,";
892 $sql .=
" t.thirdparty_code,";
893 $sql .=
" t.subledger_account,";
894 $sql .=
" t.subledger_label,";
895 $sql .=
" t.numero_compte,";
896 $sql .=
" t.label_compte,";
897 $sql .=
" t.label_operation,";
899 $sql .=
" t.credit,";
900 $sql .=
" t.montant as amount,";
902 $sql .=
" t.multicurrency_amount,";
903 $sql .=
" t.multicurrency_code,";
904 $sql .=
" t.lettering_code,";
905 $sql .=
" t.date_lettering,";
906 $sql .=
" t.fk_user_author,";
907 $sql .=
" t.import_key,";
908 $sql .=
" t.code_journal,";
909 $sql .=
" t.journal_label,";
910 $sql .=
" t.piece_num,";
911 $sql .=
" t.date_creation,";
912 $sql .=
" t.date_export,";
913 $sql .=
" t.date_validated as date_validation,";
914 $sql .=
" t.import_key";
918 if (count($filter) > 0) {
919 foreach ($filter as $key => $value) {
920 if ($key ==
't.doc_date') {
921 $sqlwhere[] = $key.
'=\''.$this->db->idate($value).
'\'';
922 } elseif ($key ==
't.doc_date>=' || $key ==
't.doc_date<=') {
923 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
924 } elseif ($key ==
't.numero_compte>=' || $key ==
't.numero_compte<=' || $key ==
't.subledger_account>=' || $key ==
't.subledger_account<=') {
925 $sqlwhere[] = $key.
'\''.$this->db->escape($value).
'\'';
926 } elseif ($key ==
't.fk_doc' || $key ==
't.fk_docdet' || $key ==
't.piece_num') {
927 $sqlwhere[] = $key.
'='.$value;
928 } elseif ($key ==
't.subledger_account' || $key ==
't.numero_compte') {
929 $sqlwhere[] = $key.
' LIKE \''.$this->db->escape($this->db->escapeforlike($value)).
'%\'';
930 } elseif ($key ==
't.date_creation>=' || $key ==
't.date_creation<=') {
931 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
932 } elseif ($key ==
't.date_export>=' || $key ==
't.date_export<=') {
933 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
934 } elseif ($key ==
't.date_validated>=' || $key ==
't.date_validated<=') {
935 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
936 } elseif ($key ==
't.credit' || $key ==
't.debit') {
938 } elseif ($key ==
't.reconciled_option') {
939 $sqlwhere[] =
't.lettering_code IS NULL';
940 } elseif ($key ==
't.code_journal' && !empty($value)) {
941 if (is_array($value)) {
942 $sqlwhere[] =
natural_search(
"t.code_journal", join(
',', $value), 3, 1);
946 } elseif ($key ==
't.search_accounting_code_in' && !empty($value)) {
947 $sqlwhere[] =
't.numero_compte IN ('.$this->db->sanitize($value, 1).
')';
953 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
954 $sql .=
' WHERE entity = ' . ((int) $conf->entity);
955 if (count($sqlwhere) > 0) {
956 $sql .=
" AND ".implode(
" ".$filtermode.
" ", $sqlwhere);
959 if (!empty($option)) {
960 $sql .=
" AND t.subledger_account IS NOT NULL";
961 $sql .=
" AND t.subledger_account <> ''";
962 $sortfield =
't.subledger_account'.($sortfield ?
','.$sortfield :
'');
963 $sortorder =
'ASC'.($sortfield ?
','.$sortfield :
'');
965 $sortfield =
't.numero_compte'.($sortfield ?
','.$sortfield :
'');
966 $sortorder =
'ASC'.($sortorder ?
','.$sortorder :
'');
970 $sql .= $this->db->order($sortfield, $sortorder);
971 if (!empty($limit)) {
972 $sql .= $this->db->plimit($limit + 1, $offset);
976 $resql = $this->db->query($sql);
979 $obj = $this->db->fetch_object($resql);
984 $num = $this->db->num_rows($resql);
987 while (($obj = $this->db->fetch_object($resql)) && (empty($limit) || $i < min($limit, $num))) {
990 $line->id = $obj->rowid;
992 $line->doc_date = $this->db->jdate($obj->doc_date);
993 $line->doc_type = $obj->doc_type;
994 $line->doc_ref = $obj->doc_ref;
995 $line->fk_doc = $obj->fk_doc;
996 $line->fk_docdet = $obj->fk_docdet;
997 $line->thirdparty_code = $obj->thirdparty_code;
998 $line->subledger_account = $obj->subledger_account;
999 $line->subledger_label = $obj->subledger_label;
1000 $line->numero_compte = $obj->numero_compte;
1001 $line->label_compte = $obj->label_compte;
1002 $line->label_operation = $obj->label_operation;
1003 $line->debit = $obj->debit;
1004 $line->credit = $obj->credit;
1005 $line->montant = $obj->amount;
1006 $line->amount = $obj->amount;
1007 $line->sens = $obj->sens;
1008 $line->multicurrency_amount = $obj->multicurrency_amount;
1009 $line->multicurrency_code = $obj->multicurrency_code;
1010 $line->lettering_code = $obj->lettering_code;
1011 $line->date_lettering = $obj->date_lettering;
1012 $line->fk_user_author = $obj->fk_user_author;
1013 $line->import_key = $obj->import_key;
1014 $line->code_journal = $obj->code_journal;
1015 $line->journal_label = $obj->journal_label;
1016 $line->piece_num = $obj->piece_num;
1017 $line->date_creation = $this->db->jdate($obj->date_creation);
1018 $line->date_export = $this->db->jdate($obj->date_export);
1019 $line->date_validation = $this->db->jdate($obj->date_validation);
1020 $line->import_key = $obj->import_key;
1022 $this->lines[] = $line;
1027 $this->db->free($resql);
1031 $this->errors[] =
'Error '.$this->db->lasterror();
1032 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1050 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND', $showAlreadyExportMovements = 1)
1057 $sql .=
' t.rowid,';
1058 $sql .=
" t.doc_date,";
1059 $sql .=
" t.doc_type,";
1060 $sql .=
" t.doc_ref,";
1061 $sql .=
" t.fk_doc,";
1062 $sql .=
" t.fk_docdet,";
1063 $sql .=
" t.thirdparty_code,";
1064 $sql .=
" t.subledger_account,";
1065 $sql .=
" t.subledger_label,";
1066 $sql .=
" t.numero_compte,";
1067 $sql .=
" t.label_compte,";
1068 $sql .=
" t.label_operation,";
1069 $sql .=
" t.debit,";
1070 $sql .=
" t.credit,";
1071 $sql .=
" t.lettering_code,";
1072 $sql .=
" t.date_lettering,";
1073 $sql .=
" t.montant as amount,";
1075 $sql .=
" t.fk_user_author,";
1076 $sql .=
" t.import_key,";
1077 $sql .=
" t.code_journal,";
1078 $sql .=
" t.journal_label,";
1079 $sql .=
" t.piece_num,";
1080 $sql .=
" t.date_creation,";
1081 $sql .=
" t.date_lim_reglement,";
1082 $sql .=
" t.tms as date_modification,";
1083 $sql .=
" t.date_export,";
1084 $sql .=
" t.date_validated as date_validation";
1085 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
1087 $sqlwhere = array();
1088 if (count($filter) > 0) {
1089 foreach ($filter as $key => $value) {
1090 if ($key ==
't.doc_date') {
1091 $sqlwhere[] = $key.
'=\''.$this->db->idate($value).
'\'';
1092 } elseif ($key ==
't.doc_date>=' || $key ==
't.doc_date<=') {
1093 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
1094 } elseif ($key ==
't.numero_compte>=' || $key ==
't.numero_compte<=' || $key ==
't.subledger_account>=' || $key ==
't.subledger_account<=') {
1095 $sqlwhere[] = $key.
'\''.$this->db->escape($value).
'\'';
1096 } elseif ($key ==
't.fk_doc' || $key ==
't.fk_docdet' || $key ==
't.piece_num') {
1097 $sqlwhere[] = $key.
'='.((int) $value);
1098 } elseif ($key ==
't.subledger_account' || $key ==
't.numero_compte') {
1099 $sqlwhere[] = $key.
' LIKE \''.$this->db->escape($value).
'%\'';
1100 } elseif ($key ==
't.date_creation>=' || $key ==
't.date_creation<=') {
1101 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
1102 } elseif ($key ==
't.tms>=' || $key ==
't.tms<=') {
1103 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
1104 } elseif ($key ==
't.date_export>=' || $key ==
't.date_export<=') {
1105 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
1106 } elseif ($key ==
't.date_validated>=' || $key ==
't.date_validated<=') {
1107 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
1108 } elseif ($key ==
't.credit' || $key ==
't.debit') {
1110 } elseif ($key ==
't.code_journal' && !empty($value)) {
1111 if (is_array($value)) {
1112 $sqlwhere[] =
natural_search(
"t.code_journal", join(
',', $value), 3, 1);
1121 $sql .=
' WHERE t.entity = ' . ((int) $conf->entity);
1122 if ($showAlreadyExportMovements == 0) {
1123 $sql .=
" AND t.date_export IS NULL";
1125 if (count($sqlwhere) > 0) {
1126 $sql .=
' AND '.implode(
" ".$filtermode.
" ", $sqlwhere);
1128 if (!empty($sortfield)) {
1129 $sql .= $this->db->order($sortfield, $sortorder);
1131 if (!empty($limit)) {
1132 $sql .= $this->db->plimit($limit + 1, $offset);
1134 $this->lines = array();
1136 $resql = $this->db->query($sql);
1138 $num = $this->db->num_rows($resql);
1141 while (($obj = $this->db->fetch_object($resql)) && (empty($limit) || $i < min($limit, $num))) {
1144 $line->id = $obj->rowid;
1146 $line->doc_date = $this->db->jdate($obj->doc_date);
1147 $line->doc_type = $obj->doc_type;
1148 $line->doc_ref = $obj->doc_ref;
1149 $line->fk_doc = $obj->fk_doc;
1150 $line->fk_docdet = $obj->fk_docdet;
1151 $line->thirdparty_code = $obj->thirdparty_code;
1152 $line->subledger_account = $obj->subledger_account;
1153 $line->subledger_label = $obj->subledger_label;
1154 $line->numero_compte = $obj->numero_compte;
1155 $line->label_compte = $obj->label_compte;
1156 $line->label_operation = $obj->label_operation;
1157 $line->debit = $obj->debit;
1158 $line->credit = $obj->credit;
1159 $line->montant = $obj->amount;
1160 $line->amount = $obj->amount;
1161 $line->sens = $obj->sens;
1162 $line->lettering_code = $obj->lettering_code;
1163 $line->date_lettering = $obj->date_lettering;
1164 $line->fk_user_author = $obj->fk_user_author;
1165 $line->import_key = $obj->import_key;
1166 $line->code_journal = $obj->code_journal;
1167 $line->journal_label = $obj->journal_label;
1168 $line->piece_num = $obj->piece_num;
1169 $line->date_creation = $this->db->jdate($obj->date_creation);
1170 $line->date_lim_reglement = $this->db->jdate($obj->date_lim_reglement);
1171 $line->date_modification = $this->db->jdate($obj->date_modification);
1172 $line->date_export = $this->db->jdate($obj->date_export);
1173 $line->date_validation = $this->db->jdate($obj->date_validation);
1175 $this->lines[] = $line;
1179 $this->db->free($resql);
1183 $this->errors[] =
'Error '.$this->db->lasterror();
1184 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1201 public function fetchAllBalance($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND', $option = 0)
1205 $this->lines = array();
1210 $sql .=
" t.numero_compte,";
1211 if (!empty($option)) {
1212 $sql .=
" t.subledger_account,";
1213 $sql .=
" t.subledger_label,";
1215 $sql .=
" SUM(t.debit) as debit,";
1216 $sql .=
" SUM(t.credit) as credit";
1217 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
1219 $sqlwhere = array();
1220 if (count($filter) > 0) {
1221 foreach ($filter as $key => $value) {
1222 if ($key ==
't.doc_date') {
1223 $sqlwhere[] = $key.
" = '".$this->db->idate($value).
"'";
1224 } elseif ($key ==
't.doc_date>=' || $key ==
't.doc_date<=' || $key ==
't.doc_date>' || $key ==
't.doc_date<') {
1225 $sqlwhere[] = $key.
"'".$this->db->idate($value).
"'";
1226 } elseif ($key ==
't.numero_compte>=' || $key ==
't.numero_compte<=' || $key ==
't.subledger_account>=' || $key ==
't.subledger_account<=') {
1227 $sqlwhere[] = $key.
"'".$this->db->escape($value).
"'";
1228 } elseif ($key ==
't.fk_doc' || $key ==
't.fk_docdet' || $key ==
't.piece_num') {
1229 $sqlwhere[] = $key.
" = ".((int) $value);
1230 } elseif ($key ==
't.subledger_account' || $key ==
't.numero_compte') {
1231 $sqlwhere[] = $key.
" LIKE '".$this->db->escape($value).
"%'";
1232 } elseif ($key ==
't.subledger_label') {
1233 $sqlwhere[] = $key.
" LIKE '".$this->db->escape($value).
"%'";
1234 } elseif ($key ==
't.code_journal' && !empty($value)) {
1235 if (is_array($value)) {
1236 $sqlwhere[] =
natural_search(
"t.code_journal", join(
',', $value), 3, 1);
1240 } elseif ($key ==
't.reconciled_option') {
1241 $sqlwhere[] =
't.lettering_code IS NULL';
1243 $sqlwhere[] = $key.
" LIKE '%".$this->db->escape($value).
"%'";
1247 $sql .=
' WHERE entity = ' . ((int) $conf->entity);
1248 if (count($sqlwhere) > 0) {
1249 $sql .=
" AND ".implode(
" ".$filtermode.
" ", $sqlwhere);
1252 if (!empty($option)) {
1253 $sql .=
" AND t.subledger_account IS NOT NULL";
1254 $sql .=
" AND t.subledger_account <> ''";
1255 $sql .=
" GROUP BY t.numero_compte, t.subledger_account, t.subledger_label";
1256 $sortfield =
't.subledger_account'.($sortfield ?
','.$sortfield :
'');
1257 $sortorder =
'ASC'.($sortfield ?
','.$sortfield :
'');
1259 $sql .=
' GROUP BY t.numero_compte';
1260 $sortfield =
't.numero_compte'.($sortfield ?
','.$sortfield :
'');
1261 $sortorder =
'ASC'.($sortorder ?
','.$sortorder :
'');
1264 if (!empty($sortfield)) {
1265 $sql .= $this->db->order($sortfield, $sortorder);
1267 if (!empty($limit)) {
1268 $sql .= $this->db->plimit($limit + 1, $offset);
1271 $resql = $this->db->query($sql);
1273 $num = $this->db->num_rows($resql);
1276 while (($obj = $this->db->fetch_object($resql)) && (empty($limit) || $i < min($limit, $num))) {
1279 $line->numero_compte = $obj->numero_compte;
1281 if (!empty($option)) {
1282 $line->subledger_account = $obj->subledger_account;
1283 $line->subledger_label = $obj->subledger_label;
1285 $line->debit = $obj->debit;
1286 $line->credit = $obj->credit;
1288 $this->lines[] = $line;
1292 $this->db->free($resql);
1296 $this->errors[] =
'Error '.$this->db->lasterror();
1297 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1311 public function update(
User $user, $notrigger =
false, $mode =
'')
1319 if (isset($this->doc_type)) {
1320 $this->doc_type = trim($this->doc_type);
1322 if (isset($this->doc_ref)) {
1323 $this->doc_ref = trim($this->doc_ref);
1325 if (isset($this->fk_doc)) {
1326 $this->fk_doc = (int) $this->fk_doc;
1328 if (isset($this->fk_docdet)) {
1329 $this->fk_docdet = (int) $this->fk_docdet;
1331 if (isset($this->thirdparty_code)) {
1332 $this->thirdparty_code = trim($this->thirdparty_code);
1334 if (isset($this->subledger_account)) {
1335 $this->subledger_account = trim($this->subledger_account);
1337 if (isset($this->subledger_label)) {
1338 $this->subledger_label = trim($this->subledger_label);
1340 if (isset($this->numero_compte)) {
1341 $this->numero_compte = trim($this->numero_compte);
1343 if (isset($this->label_compte)) {
1344 $this->label_compte = trim($this->label_compte);
1346 if (isset($this->label_operation)) {
1347 $this->label_operation = trim($this->label_operation);
1349 if (isset($this->debit)) {
1350 $this->debit = trim($this->debit);
1352 if (isset($this->credit)) {
1353 $this->credit = trim($this->credit);
1355 if (isset($this->amount)) {
1356 $this->amount = trim($this->amount);
1358 if (isset($this->sens)) {
1359 $this->sens = trim($this->sens);
1361 if (isset($this->import_key)) {
1362 $this->import_key = trim($this->import_key);
1364 if (isset($this->code_journal)) {
1365 $this->code_journal = trim($this->code_journal);
1367 if (isset($this->journal_label)) {
1368 $this->journal_label = trim($this->journal_label);
1370 if (isset($this->piece_num)) {
1371 $this->piece_num = trim($this->piece_num);
1377 } elseif ($result == 0) {
1379 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateIsOnAClosedFiscalPeriod');
1381 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateNotOnActiveFiscalPeriod');
1386 $this->debit =
price2num($this->debit,
'MT');
1387 $this->credit =
price2num($this->credit,
'MT');
1393 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element.$mode.
' SET';
1394 $sql .=
' doc_date = '.(!isset($this->doc_date) ||
dol_strlen($this->doc_date) != 0 ?
"'".$this->db->idate($this->doc_date).
"'" :
'null').
',';
1395 $sql .=
' doc_type = '.(isset($this->doc_type) ?
"'".$this->db->escape($this->doc_type).
"'" :
"null").
',';
1396 $sql .=
' doc_ref = '.(isset($this->doc_ref) ?
"'".$this->db->escape($this->doc_ref).
"'" :
"null").
',';
1397 $sql .=
' fk_doc = '.(isset($this->fk_doc) ? $this->fk_doc :
"null").
',';
1398 $sql .=
' fk_docdet = '.(isset($this->fk_docdet) ? $this->fk_docdet :
"null").
',';
1399 $sql .=
' thirdparty_code = '.(isset($this->thirdparty_code) ?
"'".$this->db->escape($this->thirdparty_code).
"'" :
"null").
',';
1400 $sql .=
' subledger_account = '.(isset($this->subledger_account) ?
"'".$this->db->escape($this->subledger_account).
"'" :
"null").
',';
1401 $sql .=
' subledger_label = '.(isset($this->subledger_label) ?
"'".$this->db->escape($this->subledger_label).
"'" :
"null").
',';
1402 $sql .=
' numero_compte = '.(isset($this->numero_compte) ?
"'".$this->db->escape($this->numero_compte).
"'" :
"null").
',';
1403 $sql .=
' label_compte = '.(isset($this->label_compte) ?
"'".$this->db->escape($this->label_compte).
"'" :
"null").
',';
1404 $sql .=
' label_operation = '.(isset($this->label_operation) ?
"'".$this->db->escape($this->label_operation).
"'" :
"null").
',';
1405 $sql .=
' debit = '.(isset($this->debit) ? $this->debit :
"null").
',';
1406 $sql .=
' credit = '.(isset($this->credit) ? $this->credit :
"null").
',';
1407 $sql .=
' montant = '.(isset($this->montant) ? $this->montant :
"null").
',';
1408 $sql .=
' sens = '.(isset($this->sens) ?
"'".$this->db->escape($this->sens).
"'" :
"null").
',';
1409 $sql .=
' fk_user_author = '.(isset($this->fk_user_author) ? $this->fk_user_author :
"null").
',';
1410 $sql .=
' import_key = '.(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
',';
1411 $sql .=
' code_journal = '.(isset($this->code_journal) ?
"'".$this->db->escape($this->code_journal).
"'" :
"null").
',';
1412 $sql .=
' journal_label = '.(isset($this->journal_label) ?
"'".$this->db->escape($this->journal_label).
"'" :
"null").
',';
1413 $sql .=
' piece_num = '.(isset($this->piece_num) ? $this->piece_num :
"null");
1414 $sql .=
' WHERE rowid='.((int) $this->
id);
1418 $resql = $this->db->query($sql);
1421 $this->errors[] =
'Error '.$this->db->lasterror();
1422 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1437 $this->db->rollback();
1441 $this->db->commit();
1456 public function updateByMvt($piece_num =
'', $field =
'', $value =
'', $mode =
'')
1461 if (!isset($sql_filter)) {
1467 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.$mode;
1468 $sql .=
" SET ".$field.
" = ".(is_numeric($value) ? ((float) $value) :
"'".$this->db->escape($value).
"'");
1469 $sql .=
" WHERE piece_num = ".((int) $piece_num);
1470 $sql .= $sql_filter;
1472 $resql = $this->db->query($sql);
1476 $this->errors[] =
'Error '.$this->db->lasterror();
1477 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1480 $this->db->rollback();
1484 $this->db->commit();
1498 public function delete(
User $user, $notrigger = 0, $mode =
'')
1507 } elseif ($result == 0) {
1509 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateIsOnAClosedFiscalPeriod');
1511 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateNotOnActiveFiscalPeriod');
1531 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.$mode;
1532 $sql .=
' WHERE rowid='.((int) $this->
id);
1534 $resql = $this->db->query($sql);
1537 $this->errors[] =
'Error '.$this->db->lasterror();
1538 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1544 $this->db->rollback();
1548 $this->db->commit();
1566 if (!isset($sql_filter)) {
1572 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1573 $sql .=
" WHERE import_key = '".$this->db->escape($importkey).
"'";
1574 $sql .= $sql_filter;
1576 $resql = $this->db->query($sql);
1579 $this->errors[] =
"Error ".$this->db->lasterror();
1580 dol_syslog(get_class($this).
"::delete Error ".$this->db->lasterror(), LOG_ERR);
1581 $this->db->rollback();
1585 $this->db->commit();
1600 global $conf, $langs;
1602 if (empty($delyear) && empty($journal)) {
1603 $this->error =
'ErrorOneFieldRequired';
1606 if (!empty($delmonth) && empty($delyear)) {
1607 $this->error =
'YearRequiredIfMonthDefined';
1612 if (!isset($sql_filter)) {
1620 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1621 $sql .=
" WHERE 1 = 1";
1623 if (!empty($journal)) {
1624 $sql .=
" AND code_journal = '".$this->db->escape($journal).
"'";
1626 $sql .=
" AND entity = " . ((int) $conf->entity);
1628 $sql .=
" AND date_validated IS NULL";
1629 $sql .= $sql_filter;
1633 $resql = $this->db->query($sql);
1636 $this->errors[] =
"Error ".$this->db->lasterror();
1637 foreach ($this->errors as $errmsg) {
1638 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
1639 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1641 $this->db->rollback();
1645 $this->db->commit();
1661 if (!isset($sql_filter)) {
1669 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1670 $sql .=
" WHERE piece_num = ".(int) $piecenum;
1671 $sql .=
" AND date_validated IS NULL";
1672 $sql .=
" AND entity = " . ((int) $conf->entity);
1673 $sql .= $sql_filter;
1675 $resql = $this->db->query($sql);
1678 $this->errors[] =
"Error ".$this->db->lasterror();
1679 foreach ($this->errors as $errmsg) {
1680 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
1681 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1683 $this->db->rollback();
1687 $this->db->commit();
1708 $object->fetch($fromid);
1716 $object->context[
'createfromclone'] =
'createfromclone';
1717 $result = $object->create($user);
1722 $this->errors = $object->errors;
1723 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1726 unset($object->context[
'createfromclone']);
1730 $this->db->commit();
1734 $this->db->rollback();
1753 $this->doc_date = $now;
1754 $this->doc_type =
'';
1755 $this->doc_ref =
'';
1757 $this->fk_docdet = 0;
1758 $this->thirdparty_code =
'CU001';
1759 $this->subledger_account =
'41100001';
1760 $this->subledger_label =
'My customer company';
1761 $this->numero_compte =
'411';
1762 $this->label_compte =
'Customer';
1763 $this->label_operation =
'Sales of pea';
1764 $this->debit = 99.9;
1765 $this->credit = 0.0;
1766 $this->amount = 0.0;
1768 $this->fk_user_author = $user->id;
1769 $this->import_key =
'20201027';
1770 $this->code_journal =
'VT';
1771 $this->journal_label =
'Journal de vente';
1772 $this->piece_num = 1234;
1773 $this->date_creation = $now;
1787 $sql =
"SELECT piece_num, doc_date,code_journal, journal_label, doc_ref, doc_type,";
1788 $sql .=
" date_creation, tms as date_modification, date_validated as date_validation";
1790 if ($mode !=
"_tmp") {
1791 $sql .=
", date_export";
1793 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1794 $sql .=
" WHERE piece_num = ".((int) $piecenum);
1795 $sql .=
" AND entity = " . ((int) $conf->entity);
1798 $result = $this->db->query($sql);
1800 $obj = $this->db->fetch_object($result);
1802 $this->piece_num = $obj->piece_num;
1803 $this->code_journal = $obj->code_journal;
1804 $this->journal_label = $obj->journal_label;
1805 $this->doc_date = $this->db->jdate($obj->doc_date);
1806 $this->doc_ref = $obj->doc_ref;
1807 $this->doc_type = $obj->doc_type;
1808 $this->date_creation = $this->db->jdate($obj->date_creation);
1809 $this->date_modification = $this->db->jdate($obj->date_modification);
1810 if ($mode !=
"_tmp") {
1811 $this->date_export = $this->db->jdate($obj->date_export);
1813 $this->date_validation = $this->db->jdate($obj->date_validation);
1815 $this->error =
"Error ".$this->db->lasterror();
1816 dol_syslog(__METHOD__.$this->error, LOG_ERR);
1833 $sql =
"SELECT MAX(piece_num)+1 as max FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1834 $sql .=
" WHERE entity = " . ((int) $conf->entity);
1836 dol_syslog(get_class($this).
"::getNextNumMvt", LOG_DEBUG);
1838 $result = $this->db->query($sql);
1841 $obj = $this->db->fetch_object($result);
1843 $result = $obj->max;
1845 if (empty($result)) {
1850 $this->error =
"Error ".$this->db->lasterror();
1851 dol_syslog(get_class($this).
"::getNextNumMvt ".$this->error, LOG_ERR);
1867 $sql =
"SELECT rowid, doc_date, doc_type,";
1868 $sql .=
" doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,";
1869 $sql .=
" numero_compte, label_compte, label_operation, debit, credit,";
1870 $sql .=
" montant as amount, sens, fk_user_author, import_key, code_journal, journal_label, piece_num,";
1871 $sql .=
" date_creation, tms as date_modification, date_validated as date_validation";
1873 if ($mode !=
"_tmp") {
1874 $sql .=
", date_export";
1876 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1877 $sql .=
" WHERE piece_num = ".((int) $piecenum);
1878 $sql .=
" AND entity = " . ((int) $conf->entity);
1881 $result = $this->db->query($sql);
1883 while ($obj = $this->db->fetch_object($result)) {
1886 $line->id = $obj->rowid;
1888 $line->doc_date = $this->db->jdate($obj->doc_date);
1889 $line->doc_type = $obj->doc_type;
1890 $line->doc_ref = $obj->doc_ref;
1891 $line->fk_doc = $obj->fk_doc;
1892 $line->fk_docdet = $obj->fk_docdet;
1893 $line->thirdparty_code = $obj->thirdparty_code;
1894 $line->subledger_account = $obj->subledger_account;
1895 $line->subledger_label = $obj->subledger_label;
1896 $line->numero_compte = $obj->numero_compte;
1897 $line->label_compte = $obj->label_compte;
1898 $line->label_operation = $obj->label_operation;
1899 $line->debit = $obj->debit;
1900 $line->credit = $obj->credit;
1901 $line->montant = $obj->amount;
1902 $line->amount = $obj->amount;
1903 $line->sens = $obj->sens;
1904 $line->code_journal = $obj->code_journal;
1905 $line->journal_label = $obj->journal_label;
1906 $line->piece_num = $obj->piece_num;
1907 $line->date_creation = $obj->date_creation;
1908 $line->date_modification = $obj->date_modification;
1909 if ($mode !=
"_tmp") {
1910 $line->date_export = $obj->date_export;
1912 $line->date_validation = $obj->date_validation;
1914 $this->linesmvt[] = $line;
1917 $this->error =
"Error ".$this->db->lasterror();
1918 dol_syslog(__METHOD__.$this->error, LOG_ERR);
1937 $sql =
"SELECT rowid, doc_date, doc_type,";
1938 $sql .=
" doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,";
1939 $sql .=
" numero_compte, label_compte, label_operation, debit, credit,";
1940 $sql .=
" montant as amount, sens, fk_user_author, import_key, code_journal, piece_num,";
1941 $sql .=
" date_validated as date_validation";
1942 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element;
1943 $sql .=
" WHERE entity = " . ((int) $conf->entity);
1945 dol_syslog(get_class($this).
"::export_bookkeeping", LOG_DEBUG);
1947 $resql = $this->db->query($sql);
1950 $this->linesexport = array();
1952 $num = $this->db->num_rows($resql);
1953 while ($obj = $this->db->fetch_object($resql)) {
1956 $line->id = $obj->rowid;
1958 $line->doc_date = $this->db->jdate($obj->doc_date);
1959 $line->doc_type = $obj->doc_type;
1960 $line->doc_ref = $obj->doc_ref;
1961 $line->fk_doc = $obj->fk_doc;
1962 $line->fk_docdet = $obj->fk_docdet;
1963 $line->thirdparty_code = $obj->thirdparty_code;
1964 $line->subledger_account = $obj->subledger_account;
1965 $line->subledger_label = $obj->subledger_label;
1966 $line->numero_compte = $obj->numero_compte;
1967 $line->label_compte = $obj->label_compte;
1968 $line->label_operation = $obj->label_operation;
1969 $line->debit = $obj->debit;
1970 $line->credit = $obj->credit;
1971 $line->montant = $obj->amount;
1972 $line->amount = $obj->amount;
1973 $line->sens = $obj->sens;
1974 $line->code_journal = $obj->code_journal;
1975 $line->piece_num = $obj->piece_num;
1976 $line->date_validation = $obj->date_validation;
1978 $this->linesexport[] = $line;
1980 $this->db->free($resql);
1984 $this->error =
"Error ".$this->db->lasterror();
1985 dol_syslog(get_class($this).
"::export_bookkeeping ".$this->error, LOG_ERR);
2004 if (!isset($sql_filter)) {
2010 if ($direction == 0) {
2014 if ($next_piecenum < 0) {
2020 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
'_tmp WHERE piece_num = '.((int) $piece_num).
' AND entity = ' .((int) $conf->entity).
" AND numero_compte IS NULL AND debit = 0 AND credit = 0";
2021 $resql = $this->db->query($sql);
2024 $this->errors[] =
'Error '.$this->db->lasterror();
2025 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
2030 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.
' (doc_date, doc_type,';
2031 $sql .=
' doc_ref, fk_doc, fk_docdet, entity, thirdparty_code, subledger_account, subledger_label,';
2032 $sql .=
' numero_compte, label_compte, label_operation, debit, credit,';
2033 $sql .=
' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num, date_creation)';
2034 $sql .=
' SELECT doc_date, doc_type,';
2035 $sql .=
' doc_ref, fk_doc, fk_docdet, entity, thirdparty_code, subledger_account, subledger_label,';
2036 $sql .=
' numero_compte, label_compte, label_operation, debit, credit,';
2037 $sql .=
' montant, sens, fk_user_author, import_key, code_journal, journal_label, '.((int) $next_piecenum).
", '".$this->db->idate($now).
"'";
2038 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
'_tmp WHERE piece_num = '.((int) $piece_num).
' AND numero_compte IS NOT NULL AND entity = ' .((int) $conf->entity);
2039 $sql .= $sql_filter;
2040 $resql = $this->db->query($sql);
2043 $this->errors[] =
'Error '.$this->db->lasterror();
2044 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
2049 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
'_tmp WHERE piece_num = '.((int) $piece_num).
' AND entity = ' .((int) $conf->entity);
2050 $resql = $this->db->query($sql);
2053 $this->errors[] =
'Error '.$this->db->lasterror();
2054 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
2057 } elseif ($direction == 1) {
2059 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
'_tmp WHERE piece_num = '.((int) $piece_num).
' AND entity = ' .((int) $conf->entity);
2060 $resql = $this->db->query($sql);
2063 $this->errors[] =
'Error '.$this->db->lasterror();
2064 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
2069 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.
'_tmp (doc_date, doc_type,';
2070 $sql .=
' doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,';
2071 $sql .=
' numero_compte, label_compte, label_operation, debit, credit,';
2072 $sql .=
' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num)';
2073 $sql .=
' SELECT doc_date, doc_type,';
2074 $sql .=
' doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,';
2075 $sql .=
' numero_compte, label_compte, label_operation, debit, credit,';
2076 $sql .=
' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num';
2077 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' WHERE piece_num = '.((int) $piece_num).
' AND entity = ' .((int) $conf->entity);
2078 $sql .= $sql_filter;
2079 $resql = $this->db->query($sql);
2082 $this->errors[] =
'Error '.$this->db->lasterror();
2083 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
2088 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
'_tmp WHERE piece_num = '.((int) $piece_num).
' AND entity = ' .((int) $conf->entity);
2089 $sql .= $sql_filter;
2090 $resql = $this->db->query($sql);
2093 $this->errors[] =
'Error '.$this->db->lasterror();
2094 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
2099 $this->db->commit();
2102 $this->db->rollback();
2130 public function select_account($selectid, $htmlname =
'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $aabase =
'')
2135 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
2139 $sql =
"SELECT DISTINCT ab.numero_compte as account_number, aa.label as label, aa.rowid as rowid, aa.fk_pcg_version";
2140 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as ab";
2141 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"accounting_account as aa ON aa.account_number = ab.numero_compte";
2142 $sql .=
" AND aa.active = 1";
2143 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
2144 $sql .=
" AND asy.rowid = ".((int) $pcgver);
2145 $sql .=
" AND ab.entity = " . ((int) $conf->entity);
2146 $sql .=
" ORDER BY account_number ASC";
2148 dol_syslog(get_class($this).
"::select_account", LOG_DEBUG);
2149 $resql = $this->db->query($sql);
2152 $this->error =
"Error ".$this->db->lasterror();
2153 dol_syslog(get_class($this).
"::select_account ".$this->error, LOG_ERR);
2162 while ($obj = $this->db->fetch_object($resql)) {
2165 $select_value_in = $obj->rowid;
2166 $select_value_out = $obj->rowid;
2168 if ($select_in == 1) {
2169 $select_value_in = $obj->account_number;
2171 if ($select_out == 1) {
2172 $select_value_out = $obj->account_number;
2177 if (($selectid !=
'') && $selectid == $select_value_in) {
2178 $selected = $select_value_out;
2181 $options[$select_value_out] = $label;
2184 $out .=
Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0,
'', 0, 0, 0,
'',
'maxwidth300');
2185 $this->db->free($resql);
2201 $sql =
"SELECT root.rowid, root.account_number, root.label as label,";
2202 $sql .=
" parent.rowid as parent_rowid, parent.account_number as parent_account_number, parent.label as parent_label";
2203 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_account as aa";
2204 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
2205 $sql .=
" AND asy.rowid = ".((int) $pcgver);
2206 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"accounting_account as parent ON aa.account_parent = parent.rowid AND parent.active = 1";
2207 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"accounting_account as root ON parent.account_parent = root.rowid AND root.active = 1";
2208 $sql .=
" WHERE aa.account_number = '".$this->db->escape($account).
"'";
2209 $sql .=
" AND aa.entity = " . ((int) $conf->entity);
2211 dol_syslog(get_class($this).
"::select_account", LOG_DEBUG);
2212 $resql = $this->db->query($sql);
2215 if ($this->db->num_rows($resql)) {
2216 $obj = $this->db->fetch_object($resql);
2219 $result = array(
'id'=>$obj->rowid,
'account_number'=>$obj->account_number,
'label'=>$obj->label);
2222 $this->error =
"Error ".$this->db->lasterror();
2223 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
2242 $sql =
"SELECT aa.account_number, aa.label, aa.rowid, aa.fk_pcg_version, cat.label as category";
2243 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_account as aa ";
2244 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
2245 $sql .=
" AND aa.account_number = '".$this->db->escape($account).
"'";
2246 $sql .=
" AND asy.rowid = ".((int) $pcgver);
2247 $sql .=
" AND aa.active = 1";
2248 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_accounting_category as cat ON aa.fk_accounting_category = cat.rowid";
2249 $sql .=
" WHERE aa.entity = " . ((int) $conf->entity);
2251 dol_syslog(get_class($this).
"::select_account", LOG_DEBUG);
2252 $resql = $this->db->query($sql);
2255 if ($this->db->num_rows($resql)) {
2256 $obj = $this->db->fetch_object($resql);
2258 if (empty($obj->category)) {
2261 return $obj->label.
' ('.$obj->category.
')';
2264 $this->error =
"Error ".$this->db->lasterror();
2265 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
2281 $alias = trim($alias);
2282 $alias = !empty($alias) && strpos($alias,
'.') < 0 ? $alias .
"." : $alias;
2284 if (!isset(self::$can_modify_bookkeeping_sql_cached[$alias]) || $force) {
2290 $sql_list = array();
2291 if (!empty($conf->cache[
'active_fiscal_period_cached']) && is_array($conf->cache[
'active_fiscal_period_cached'])) {
2292 foreach ($conf->cache[
'active_fiscal_period_cached'] as $fiscal_period) {
2293 $sql_list[] =
"('" . $this->db->idate($fiscal_period[
'date_start']) .
"' <= {$alias}doc_date AND {$alias}doc_date <= '" . $this->db->idate($fiscal_period[
'date_end']) .
"')";
2296 self::$can_modify_bookkeeping_sql_cached[$alias] = !empty($sql_list) ?
' AND (' . implode(
' OR ', $sql_list) .
')' :
'';
2299 return self::$can_modify_bookkeeping_sql_cached[$alias];
2321 $result = $bookkeeping->fetch($id,
null, $mode);
2326 if (!empty($conf->cache[
'closed_fiscal_period_cached']) && is_array($conf->cache[
'closed_fiscal_period_cached'])) {
2327 foreach ($conf->cache[
'closed_fiscal_period_cached'] as $fiscal_period) {
2328 if ($fiscal_period[
'date_start'] <= $bookkeeping->doc_date && $bookkeeping->doc_date <= $fiscal_period[
'date_end']) {
2342 $result = $bookkeeping->fetch($id,
null, $mode);
2347 if (!empty($conf->cache[
'active_fiscal_period_cached']) && is_array($conf->cache[
'active_fiscal_period_cached'])) {
2348 foreach ($conf->cache[
'active_fiscal_period_cached'] as $fiscal_period) {
2349 if ($fiscal_period[
'date_start'] <= $bookkeeping->doc_date && $bookkeeping->doc_date <= $fiscal_period[
'date_end']) {
2376 if (!empty($conf->cache[
'closed_fiscal_period_cached']) && is_array($conf->cache[
'closed_fiscal_period_cached'])) {
2377 foreach ($conf->cache[
'closed_fiscal_period_cached'] as $fiscal_period) {
2378 if ($fiscal_period[
'date_start'] <= $date && $date <= $fiscal_period[
'date_end']) {
2391 if (!empty($conf->cache[
'active_fiscal_period_cached']) && is_array($conf->cache[
'active_fiscal_period_cached'])) {
2392 foreach ($conf->cache[
'active_fiscal_period_cached'] as $fiscal_period) {
2393 if ($fiscal_period[
'date_start'] <= $date && $date <= $fiscal_period[
'date_end']) {
2414 if ($mode ==
'active') {
2415 if (!isset($conf->cache[
'active_fiscal_period_cached']) || $force) {
2416 $sql =
"SELECT date_start, date_end";
2417 $sql .=
" FROM " . $this->db->prefix() .
"accounting_fiscalyear";
2418 $sql .=
" WHERE entity = " . ((int) $conf->entity);
2419 $sql .=
" AND statut = 0";
2421 $resql = $this->db->query($sql);
2423 $this->errors[] = $this->db->lasterror();
2428 while ($obj = $this->db->fetch_object($resql)) {
2430 'date_start' => $this->db->jdate($obj->date_start),
2431 'date_end' => $this->db->jdate($obj->date_end),
2434 $conf->cache[
'active_fiscal_period_cached'] = $list;
2437 if ($mode ==
'closed') {
2438 if (!isset($conf->cache[
'closed_fiscal_period_cached']) || $force) {
2439 $sql =
"SELECT date_start, date_end";
2440 $sql .=
" FROM " . $this->db->prefix() .
"accounting_fiscalyear";
2441 $sql .=
" WHERE entity = " . ((int) $conf->entity);
2442 $sql .=
" AND statut = 1";
2444 $resql = $this->db->query($sql);
2446 $this->errors[] = $this->db->lasterror();
2451 while ($obj = $this->db->fetch_object($resql)) {
2453 'date_start' => $this->db->jdate($obj->date_start),
2454 'date_end' => $this->db->jdate($obj->date_end),
2457 $conf->cache[
'closed_fiscal_period_cached'] = $list;
2475 $sql =
"SELECT rowid, label, date_start, date_end, statut";
2476 $sql .=
" FROM " . $this->db->prefix() .
"accounting_fiscalyear";
2477 $sql .=
" WHERE entity = " . ((int) $conf->entity);
2478 if (!empty($filter)) {
2479 $sql .=
" AND (" . $filter .
')';
2481 $sql .= $this->db->order(
'date_start',
'ASC');
2483 $resql = $this->db->query($sql);
2485 $this->errors[] = $this->db->lasterror();
2489 while ($obj = $this->db->fetch_object($resql)) {
2490 $list[$obj->rowid] = array(
2491 'id' => $obj->rowid,
2492 'label' => $obj->label,
2493 'date_start' => $this->db->jdate($obj->date_start),
2494 'date_end' => $this->db->jdate($obj->date_end),
2495 'status' => $obj->statut,
2514 $sql =
"SELECT YEAR(b.doc_date) as year";
2515 for ($i = 1; $i <= 12; $i++) {
2516 $sql .=
", SUM(" . $this->db->ifsql(
"MONTH(b.doc_date)=" . $i,
"1",
"0") .
") AS month" . $i;
2518 $sql .=
", COUNT(b.rowid) as total";
2519 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping as b";
2520 $sql .=
" WHERE b.doc_date >= '" . $this->db->idate($date_start) .
"'";
2521 $sql .=
" AND b.doc_date <= '" . $this->db->idate($date_end) .
"'";
2522 $sql .=
" AND b.entity IN (" .
getEntity(
'bookkeeping', 0) .
")";
2528 $sql .=
" AND b.doc_date BETWEEN 0 AND 0";
2531 $sql .=
" AND date_validated IS NULL";
2534 $sql .=
" GROUP BY YEAR(b.doc_date)";
2535 $sql .= $this->db->order(
"year",
'ASC');
2538 $resql = $this->db->query($sql);
2540 $this->errors[] = $this->db->lasterror();
2544 while ($obj = $this->db->fetch_object($resql)) {
2545 $total += (int) $obj->total;
2547 'year' => (
int) $obj->year,
2549 'total' => (
int) $obj->total,
2551 for ($i = 1; $i <= 12; $i++) {
2552 $year_list[
'count'][$i] = (int) $obj->{
'month' . $i};
2555 $list[] = $year_list;
2558 $this->db->free($resql);
2580 $sql =
" UPDATE " . MAIN_DB_PREFIX .
"accounting_bookkeeping";
2581 $sql .=
" SET date_validated = '" . $this->db->idate($now) .
"'";
2582 $sql .=
" WHERE entity = " . ((int) $conf->entity);
2583 $sql .=
" AND DATE(doc_date) >= '" . $this->db->idate($date_start) .
"'";
2584 $sql .=
" AND DATE(doc_date) <= '" . $this->db->idate($date_end) .
"'";
2585 $sql .=
" AND date_validated IS NULL";
2588 $resql = $this->db->query($sql);
2590 $this->errors[] = $this->db->lasterror();
2606 public function closeFiscalPeriod($fiscal_period_id, $new_fiscal_period_id, $separate_auxiliary_account =
false, $generate_bookkeeping_records =
true)
2608 global $conf, $langs, $user;
2611 $fiscal_period_id = max(0, $fiscal_period_id);
2612 if (empty($fiscal_period_id)) {
2613 $langs->load(
'errors');
2614 $this->errors[] = $langs->trans(
'ErrorBadParameters');
2618 $result = $fiscal_period->fetch($fiscal_period_id);
2620 $this->error = $fiscal_period->error;
2621 $this->errors = $fiscal_period->errors;
2623 } elseif (empty($fiscal_period->id)) {
2624 $langs->loadLangs(array(
'errors',
'compta'));
2625 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'FiscalPeriod') .
' (' . $fiscal_period_id .
')';
2630 $new_fiscal_period_id = max(0, $new_fiscal_period_id);
2631 if (empty($new_fiscal_period_id)) {
2632 $langs->load(
'errors');
2633 $this->errors[] = $langs->trans(
'ErrorBadParameters');
2636 $new_fiscal_period =
new Fiscalyear($this->db);
2637 $result = $new_fiscal_period->fetch($new_fiscal_period_id);
2639 $this->error = $new_fiscal_period->error;
2640 $this->errors = $new_fiscal_period->errors;
2642 } elseif (empty($new_fiscal_period->id)) {
2643 $langs->loadLangs(array(
'errors',
'compta'));
2644 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'FiscalPeriod') .
' (' . $new_fiscal_period_id .
')';
2651 $fiscal_period->statut = Fiscalyear::STATUS_CLOSED;
2652 $fiscal_period->status = Fiscalyear::STATUS_CLOSED;
2653 $result = $fiscal_period->update($user);
2655 $this->error = $fiscal_period->error;
2656 $this->errors = $fiscal_period->errors;
2660 if (!$error && !empty($generate_bookkeeping_records)) {
2662 if (empty($journal_id)) {
2663 $langs->loadLangs(array(
'errors',
'accountancy'));
2664 $this->errors[] = $langs->trans(
'ErrorBadParameters') .
' - ' . $langs->trans(
'Codejournal') .
' (' . $langs->trans(
'AccountingJournalType9') .
')';
2671 $result = $journal->fetch($journal_id);
2673 $this->error = $journal->error;
2674 $this->errors = $journal->errors;
2676 } elseif ($result == 0) {
2677 $langs->loadLangs(array(
'errors',
'accountancy'));
2678 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'Codejournal') .
' (' . $langs->trans(
'AccountingJournalType9') .
')';
2684 $accounting_groups_used_for_balance_sheet_account = array_filter(array_map(
'trim', explode(
',',
getDolGlobalString(
'ACCOUNTING_CLOSURE_ACCOUNTING_GROUPS_USED_FOR_BALANCE_SHEET_ACCOUNT'))),
'strlen');
2685 $accounting_groups_used_for_income_statement = array_filter(array_map(
'trim', explode(
',',
getDolGlobalString(
'ACCOUNTING_CLOSURE_ACCOUNTING_GROUPS_USED_FOR_INCOME_STATEMENT'))),
'strlen');
2687 $pcg_type_filter = array();
2688 $tmp = array_merge($accounting_groups_used_for_balance_sheet_account, $accounting_groups_used_for_income_statement);
2689 foreach ($tmp as $item) {
2690 $pcg_type_filter[] =
"'" . $this->db->escape($item) .
"'";
2694 $sql .=
" t.numero_compte,";
2695 $sql .=
" t.label_compte,";
2696 if ($separate_auxiliary_account) {
2697 $sql .=
" t.subledger_account,";
2698 $sql .=
" t.subledger_label,";
2700 $sql .=
" aa.pcg_type,";
2701 $sql .=
" (SUM(t.credit) - SUM(t.debit)) as opening_balance";
2702 $sql .=
' FROM ' . MAIN_DB_PREFIX . $this->table_element .
' as t';
2703 $sql .=
' LEFT JOIN ' . MAIN_DB_PREFIX .
'accounting_account as aa ON aa.account_number = t.numero_compte';
2704 $sql .=
' WHERE t.entity = ' . ((int) $conf->entity);
2705 $sql .=
" AND aa.entity = ". ((int) $conf->entity);
2706 $sql .=
' AND aa.fk_pcg_version IN (SELECT pcg_version FROM '.MAIN_DB_PREFIX.
'accounting_system WHERE rowid = '.((int)
getDolGlobalInt(
'CHARTOFACCOUNTS')).
')';
2707 $sql .=
' AND aa.pcg_type IN (' . $this->db->sanitize(implode(
',', $pcg_type_filter), 1) .
')';
2708 $sql .=
" AND DATE(t.doc_date) >= '" . $this->db->idate($fiscal_period->date_start) .
"'";
2709 $sql .=
" AND DATE(t.doc_date) <= '" . $this->db->idate($fiscal_period->date_end) .
"'";
2710 $sql .=
' GROUP BY t.numero_compte, t.label_compte, aa.pcg_type';
2711 if ($separate_auxiliary_account) {
2712 $sql .=
' ,t.subledger_account, t.subledger_label';
2714 $sql .= $this->db->order(
"t.numero_compte",
"ASC");
2716 $resql = $this->db->query($sql);
2718 $this->errors[] =
'Error ' . $this->db->lasterror();
2719 dol_syslog(__METHOD__ .
' ' . join(
',', $this->errors), LOG_ERR);
2724 $income_statement_amount = 0;
2725 while ($obj = $this->db->fetch_object($resql)) {
2726 if (in_array($obj->pcg_type, $accounting_groups_used_for_income_statement)) {
2727 $income_statement_amount += $obj->opening_balance;
2730 $mt = $obj->opening_balance;
2733 $bookkeeping->doc_date = $new_fiscal_period->date_start;
2734 $bookkeeping->date_lim_reglement =
'';
2735 $bookkeeping->doc_ref = $new_fiscal_period->label;
2736 $bookkeeping->date_creation = $now;
2737 $bookkeeping->doc_type =
'closure';
2738 $bookkeeping->fk_doc = $new_fiscal_period->id;
2739 $bookkeeping->fk_docdet = 0;
2740 $bookkeeping->thirdparty_code =
'';
2742 if ($separate_auxiliary_account) {
2743 $bookkeeping->subledger_account = $obj->subledger_account;
2744 $bookkeeping->subledger_label = $obj->subledger_label;
2746 $bookkeeping->subledger_account =
'';
2747 $bookkeeping->subledger_label =
'';
2750 $bookkeeping->numero_compte = $obj->numero_compte;
2751 $bookkeeping->label_compte = $obj->label_compte;
2753 $bookkeeping->label_operation = $new_fiscal_period->label;
2754 $bookkeeping->montant = $mt;
2755 $bookkeeping->sens = ($mt >= 0) ?
'C' :
'D';
2756 $bookkeeping->debit = ($mt < 0) ? -$mt : 0;
2757 $bookkeeping->credit = ($mt >= 0) ? $mt : 0;
2758 $bookkeeping->code_journal = $journal->code;
2759 $bookkeeping->journal_label = $langs->transnoentities($journal->label);
2760 $bookkeeping->fk_user_author = $user->id;
2761 $bookkeeping->entity = $conf->entity;
2763 $result = $bookkeeping->create($user);
2765 $this->error = $bookkeeping->error;
2766 $this->errors = $bookkeeping->errors;
2774 if (!$error && $income_statement_amount != 0) {
2775 $mt = $income_statement_amount;
2777 $accountingaccount->fetch(
null,
getDolGlobalString($income_statement_amount < 0 ?
'ACCOUNTING_RESULT_LOSS' :
'ACCOUNTING_RESULT_PROFIT'),
true);
2780 $bookkeeping->doc_date = $new_fiscal_period->date_start;
2781 $bookkeeping->date_lim_reglement =
'';
2782 $bookkeeping->doc_ref = $new_fiscal_period->label;
2783 $bookkeeping->date_creation = $now;
2784 $bookkeeping->doc_type =
'closure';
2785 $bookkeeping->fk_doc = $new_fiscal_period->id;
2786 $bookkeeping->fk_docdet = 0;
2787 $bookkeeping->thirdparty_code =
'';
2789 if ($separate_auxiliary_account) {
2790 $bookkeeping->subledger_label =
'';
2791 $bookkeeping->subledger_account = $obj->subledger_account;
2792 $bookkeeping->subledger_label = $obj->subledger_label;
2794 $bookkeeping->subledger_account =
'';
2795 $bookkeeping->subledger_label =
'';
2798 $bookkeeping->numero_compte = $accountingaccount->account_number;
2799 $bookkeeping->label_compte = $accountingaccount->label;
2801 $bookkeeping->label_operation = $new_fiscal_period->label;
2802 $bookkeeping->montant = $mt;
2803 $bookkeeping->sens = ($mt >= 0) ?
'C' :
'D';
2804 $bookkeeping->debit = ($mt < 0) ? -$mt : 0;
2805 $bookkeeping->credit = ($mt >= 0) ? $mt : 0;
2806 $bookkeeping->code_journal = $journal->code;
2807 $bookkeeping->journal_label = $langs->transnoentities($journal->label);
2808 $bookkeeping->fk_user_author = $user->id;
2809 $bookkeeping->entity = $conf->entity;
2811 $result = $bookkeeping->create($user);
2813 $this->error = $bookkeeping->error;
2814 $this->errors = $bookkeeping->errors;
2818 $this->db->free($resql);
2824 $this->db->rollback();
2827 $this->db->commit();
2844 global $conf, $langs, $user;
2847 $fiscal_period_id = max(0, $fiscal_period_id);
2848 if (empty($fiscal_period_id)) {
2849 $langs->load(
'errors');
2850 $this->errors[] = $langs->trans(
'ErrorBadParameters');
2854 $result = $fiscal_period->fetch($fiscal_period_id);
2856 $this->error = $fiscal_period->error;
2857 $this->errors = $fiscal_period->errors;
2859 } elseif (empty($fiscal_period->id)) {
2860 $langs->loadLangs(array(
'errors',
'compta'));
2861 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'FiscalPeriod') .
' (' . $fiscal_period_id .
')';
2866 $new_fiscal_period_id = max(0, $new_fiscal_period_id);
2867 if (empty($new_fiscal_period_id)) {
2868 $langs->load(
'errors');
2869 $this->errors[] = $langs->trans(
'ErrorBadParameters');
2872 $new_fiscal_period =
new Fiscalyear($this->db);
2873 $result = $new_fiscal_period->fetch($new_fiscal_period_id);
2875 $this->error = $new_fiscal_period->error;
2876 $this->errors = $new_fiscal_period->errors;
2878 } elseif (empty($new_fiscal_period->id)) {
2879 $langs->loadLangs(array(
'errors',
'compta'));
2880 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'FiscalPeriod') .
' (' . $new_fiscal_period_id .
')';
2885 $inventory_journal_id = max(0, $inventory_journal_id);
2886 if (empty($inventory_journal_id)) {
2887 $langs->load(
'errors');
2888 $this->errors[] = $langs->trans(
'ErrorBadParameters');
2893 $result = $inventory_journal->fetch($inventory_journal_id);
2895 $this->error = $inventory_journal->error;
2896 $this->errors = $inventory_journal->errors;
2898 } elseif ($result == 0) {
2899 $langs->loadLangs(array(
'errors',
'accountancy'));
2900 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'InventoryJournal');
2907 $sql =
'SELECT t.rowid';
2908 $sql .=
' FROM ' . MAIN_DB_PREFIX . $this->table_element .
' as t';
2909 $sql .=
' WHERE t.entity = ' . ((int) $conf->entity);
2910 $sql .=
" AND code_journal = '" . $this->db->escape($inventory_journal->code) .
"'";
2911 $sql .=
" AND DATE(t.doc_date) >= '" . $this->db->idate($date_start) .
"'";
2912 $sql .=
" AND DATE(t.doc_date) <= '" . $this->db->idate($date_end) .
"'";
2913 $sql .=
" AND DATE(t.doc_date) >= '" . $this->db->idate($fiscal_period->date_start) .
"'";
2914 $sql .=
" AND DATE(t.doc_date) <= '" . $this->db->idate($fiscal_period->date_end) .
"'";
2916 $resql = $this->db->query($sql);
2918 $this->errors[] =
'Error ' . $this->db->lasterror();
2919 dol_syslog(__METHOD__ .
' ' . join(
',', $this->errors), LOG_ERR);
2924 while ($obj = $this->db->fetch_object($resql)) {
2926 $result = $bookkeeping->fetch($obj->rowid);
2928 $this->error = $inventory_journal->error;
2929 $this->errors = $inventory_journal->errors;
2932 } elseif ($result == 0) {
2933 $langs->loadLangs(array(
'errors',
'accountancy'));
2934 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'LineId') .
': ' . $obj->rowid;
2939 $bookkeeping->id = 0;
2940 $bookkeeping->doc_date = $new_fiscal_period->date_start;
2941 $bookkeeping->doc_ref = $new_fiscal_period->label;
2942 $bookkeeping->date_creation = $now;
2943 $bookkeeping->doc_type =
'accounting_reversal';
2944 $bookkeeping->fk_doc = $new_fiscal_period->id;
2945 $bookkeeping->fk_docdet = 0;
2947 $bookkeeping->montant = -$bookkeeping->montant;
2948 $bookkeeping->sens = ($bookkeeping->montant >= 0) ?
'C' :
'D';
2949 $old_debit = $bookkeeping->debit;
2950 $bookkeeping->debit = $bookkeeping->credit;
2951 $bookkeeping->credit = $old_debit;
2953 $bookkeeping->fk_user_author = $user->id;
2954 $bookkeeping->entity = $conf->entity;
2956 $result = $bookkeeping->create($user);
2958 $this->error = $bookkeeping->error;
2959 $this->errors = $bookkeeping->errors;
2964 $this->db->free($resql);
2968 $this->db->rollback();
2971 $this->db->commit();
2987 public $doc_date =
'';
3001 public $thirdparty_code;
3002 public $subledger_account;
3003 public $subledger_label;
3004 public $numero_compte;
3005 public $label_compte;
3006 public $label_operation;
3024 public $multicurrency_amount;
3029 public $multicurrency_code;
3035 public $lettering_code;
3036 public $date_lettering;
3041 public $fk_user_author;
3044 public $code_journal;
3045 public $journal_label;
3051 public $date_creation;
3056 public $date_modification;
3061 public $date_export;
3066 public $date_validation;
3071 public $date_lim_reglement;
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Class to manage accounting accounts.
Class to manage accounting journals.
Class to manage Ledger (General Ledger and Subledger)
fetchAllBalance($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND', $option=0)
Load object in memory from the database.
closeFiscalPeriod($fiscal_period_id, $new_fiscal_period_id, $separate_auxiliary_account=false, $generate_bookkeeping_records=true)
Close fiscal period.
getCountByMonthForFiscalPeriod($date_start, $date_end)
Get list of count by month into the fiscal period.
create(User $user, $notrigger=false)
Create object into database.
createStd(User $user, $notrigger=false, $mode='')
Create object into database.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
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.
update(User $user, $notrigger=false, $mode='')
Update object into database.
getNextNumMvt($mode='')
Return next number movement.
get_compte_desc($account=null)
Description of accounting account.
__construct(DoliDB $db)
Constructor.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
deleteByImportkey($importkey, $mode='')
Delete bookkeeping by importkey.
getRootAccount($account=null)
Return id and description of a root accounting account.
transformTransaction($direction=0, $piece_num='')
Transform transaction.
insertAccountingReversal($fiscal_period_id, $inventory_journal_id, $new_fiscal_period_id, $date_start, $date_end)
Insert accounting reversal into the inventory journal of the new fiscal period.
fetchPerMvt($piecenum, $mode='')
Load an accounting document into memory from database.
updateByMvt($piece_num='', $field='', $value='', $mode='')
Update accounting movement.
deleteMvtNum($piecenum, $mode='')
Delete bookkeeping by piece number.
getFiscalPeriods($filter='')
Get list of fiscal period ordered by start date.
validBookkeepingDate($date)
Is the bookkeeping date valid (on an open period or not on a closed period) ?
deleteByYearAndJournal($delyear=0, $journal='', $mode='', $delmonth=0)
Delete bookkeeping by year.
loadFiscalPeriods($force=false, $mode='active')
Load list of active fiscal period.
fetchAllByAccount($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND', $option=0, $countonly=0)
Load object in memory from the database in ->lines.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
getCanModifyBookkeepingSQL($alias='', $force=false)
Get SQL string for check if the bookkeeping can be modified or deleted ? (cached)
fetch($id, $ref=null, $mode='')
Load object in memory from the database.
canModifyBookkeeping($id, $mode='')
Is the bookkeeping can be modified or deleted ?
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND', $showAlreadyExportMovements=1)
Load object in memory from the database.
fetchAllPerMvt($piecenum, $mode='')
Load all informations of accountancy document.
validateMovementForFiscalPeriod($date_start, $date_end)
Validate all movement between the specified dates.
export_bookkeeping($model='ebp')
Export bookkeeping.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Class to manage Dolibarr database access.
Class to manage fiscal year.
Class to manage Dolibarr users.
dolSqlDateFilter($datefield, $day_date, $month_date, $year_date, $excludefirstand=0, $gm=false)
Generate a SQL string to make a filter into a range (for second of date until last second of date).
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
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...
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.