29require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
30require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
31require_once DOL_DOCUMENT_ROOT.
'/core/class/fiscalyear.class.php';
32require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingjournal.class.php';
33require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingaccount.class.php';
43 public $element =
'accountingbookkeeping';
48 public $table_element =
'accounting_bookkeeping';
58 public $lines = array();
73 public $date_lim_reglement;
98 public $thirdparty_code;
103 public $subledger_account;
108 public $subledger_label;
113 public $numero_compte;
118 public $label_compte;
123 public $label_operation;
155 public $fk_user_author;
165 public $code_journal;
170 public $journal_label;
180 public $linesmvt = array();
185 public $linesexport = array();
190 public $date_validation;
200 public $picto =
'generic';
205 public static $can_modify_bookkeeping_sql_cached;
227 global $conf, $langs;
234 if (isset($this->doc_type)) {
235 $this->doc_type = trim($this->doc_type);
237 if (isset($this->doc_ref)) {
238 $this->doc_ref = trim($this->doc_ref);
240 if (isset($this->fk_doc)) {
241 $this->fk_doc = (int) $this->fk_doc;
243 if (isset($this->fk_docdet)) {
244 $this->fk_docdet = (int) $this->fk_docdet;
246 if (isset($this->thirdparty_code)) {
247 $this->thirdparty_code = trim($this->thirdparty_code);
249 if (isset($this->subledger_account)) {
250 $this->subledger_account = trim($this->subledger_account);
252 if (isset($this->subledger_label)) {
253 $this->subledger_label = trim($this->subledger_label);
255 if (isset($this->numero_compte)) {
256 $this->numero_compte = trim($this->numero_compte);
258 if (isset($this->label_compte)) {
259 $this->label_compte = trim($this->label_compte);
261 if (isset($this->label_operation)) {
262 $this->label_operation = trim($this->label_operation);
264 if (isset($this->debit)) {
265 $this->debit = (float) $this->debit;
267 if (isset($this->credit)) {
268 $this->credit = (float) $this->credit;
270 if (isset($this->montant)) {
271 $this->montant = (float) $this->montant;
273 if (isset($this->amount)) {
274 $this->amount = (float) $this->amount;
276 if (isset($this->sens)) {
277 $this->sens = trim($this->sens);
279 if (isset($this->import_key)) {
280 $this->import_key = trim($this->import_key);
282 if (isset($this->code_journal)) {
283 $this->code_journal = trim($this->code_journal);
285 if (isset($this->journal_label)) {
286 $this->journal_label = trim($this->journal_label);
288 if (isset($this->piece_num)) {
289 $this->piece_num = trim($this->piece_num);
291 if (empty($this->debit)) {
294 if (empty($this->credit)) {
301 } elseif ($result == 0) {
303 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateIsOnAClosedFiscalPeriod');
305 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateNotOnActiveFiscalPeriod');
311 if (($this->numero_compte ==
"") || $this->numero_compte ==
'-1' || $this->numero_compte ==
'NotDefined') {
312 $langs->loadLangs(array(
"errors"));
313 if (in_array($this->doc_type, array(
'bank',
'expense_report'))) {
314 $this->errors[] = $langs->trans(
'ErrorFieldAccountNotDefinedForBankLine', $this->fk_docdet, $this->doc_type);
317 $mesg = $this->doc_ref.
', '.$langs->trans(
"AccountAccounting").
': '.($this->numero_compte != -1 ? $this->numero_compte : $langs->trans(
"Unknown"));
318 if ($this->subledger_account && $this->subledger_account != $this->numero_compte) {
319 $mesg .=
', '.$langs->trans(
"SubledgerAccount").
': '.$this->subledger_account;
321 $this->errors[] = $langs->trans(
'ErrorFieldAccountNotDefinedForLine', $mesg);
329 $this->piece_num = 0;
336 $sql =
"SELECT count(*) as nb";
337 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element;
338 $sql .=
" WHERE doc_type = '".$this->db->escape($this->doc_type).
"'";
339 $sql .=
" AND fk_doc = ".((int) $this->fk_doc);
342 $sql .=
" AND fk_docdet = ".((int) $this->fk_docdet);
344 $sql .=
" AND numero_compte = '".$this->db->escape($this->numero_compte).
"'";
345 $sql .=
" AND label_operation = '".$this->db->escape($this->label_operation).
"'";
346 if (!empty($this->subledger_account)) {
347 $sql .=
" AND subledger_account = '".$this->db->escape($this->subledger_account).
"'";
349 $sql .=
" AND entity = ".$conf->entity;
351 $resql = $this->db->query($sql);
354 $row = $this->db->fetch_object($resql);
357 $sqlnum =
"SELECT piece_num";
358 $sqlnum .=
" FROM ".MAIN_DB_PREFIX.$this->table_element;
359 $sqlnum .=
" WHERE doc_type = '".$this->db->escape($this->doc_type).
"'";
360 $sqlnum .=
" AND fk_doc = ".((int) $this->fk_doc);
363 $sqlnum .=
" AND fk_docdet = ".((int) $this->fk_docdet);
365 $sqlnum .=
" AND doc_ref = '".$this->db->escape($this->doc_ref).
"'";
366 $sqlnum .=
" AND entity = ".$conf->entity;
368 dol_syslog(get_class($this).
":: create sqlnum=".$sqlnum, LOG_DEBUG);
369 $resqlnum = $this->db->query($sqlnum);
371 $objnum = $this->db->fetch_object($resqlnum);
372 $this->piece_num = $objnum->piece_num;
375 dol_syslog(get_class($this).
"::create this->piece_num=".$this->piece_num, LOG_DEBUG);
376 if (empty($this->piece_num)) {
377 $sqlnum =
"SELECT MAX(piece_num)+1 as maxpiecenum";
378 $sqlnum .=
" FROM ".MAIN_DB_PREFIX.$this->table_element;
379 $sqlnum .=
" WHERE entity = " . ((int) $conf->entity);
381 $resqlnum = $this->db->query($sqlnum);
383 $objnum = $this->db->fetch_object($resqlnum);
384 $this->piece_num = $objnum->maxpiecenum;
386 dol_syslog(get_class($this).
":: create now this->piece_num=".$this->piece_num, LOG_DEBUG);
388 if (empty($this->piece_num)) {
389 $this->piece_num = 1;
394 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (";
396 $sql .=
", date_lim_reglement";
397 $sql .=
", doc_type";
400 $sql .=
", fk_docdet";
401 $sql .=
", thirdparty_code";
402 $sql .=
", subledger_account";
403 $sql .=
", subledger_label";
404 $sql .=
", numero_compte";
405 $sql .=
", label_compte";
406 $sql .=
", label_operation";
411 $sql .=
", fk_user_author";
412 $sql .=
", date_creation";
413 $sql .=
", code_journal";
414 $sql .=
", journal_label";
415 $sql .=
", piece_num";
417 $sql .=
") VALUES (";
418 $sql .=
"'".$this->db->idate($this->doc_date).
"'";
419 $sql .=
", ".(!isset($this->date_lim_reglement) ||
dol_strlen($this->date_lim_reglement) == 0 ?
'NULL' :
"'".$this->db->idate($this->date_lim_reglement).
"'");
420 $sql .=
", '".$this->db->escape($this->doc_type).
"'";
421 $sql .=
", '".$this->db->escape($this->doc_ref).
"'";
422 $sql .=
", ".((int) $this->fk_doc);
423 $sql .=
", ".((int) $this->fk_docdet);
424 $sql .=
", ".(!empty($this->thirdparty_code) ? (
"'".$this->db->escape($this->thirdparty_code).
"'") :
"NULL");
425 $sql .=
", ".(!empty($this->subledger_account) ? (
"'".$this->db->escape($this->subledger_account).
"'") :
"NULL");
426 $sql .=
", ".(!empty($this->subledger_label) ? (
"'".$this->db->escape($this->subledger_label).
"'") :
"NULL");
427 $sql .=
", '".$this->db->escape($this->numero_compte).
"'";
428 $sql .=
", ".(!empty($this->label_compte) ? (
"'".$this->db->escape($this->label_compte).
"'") :
"NULL");
429 $sql .=
", '".$this->db->escape($this->label_operation).
"'";
430 $sql .=
", ".((float) $this->debit);
431 $sql .=
", ".((float) $this->credit);
432 $sql .=
", ".((float) $this->montant);
433 $sql .=
", ".(!empty($this->sens) ? (
"'".$this->db->escape($this->sens).
"'") :
"NULL");
434 $sql .=
", '".$this->db->escape($this->fk_user_author).
"'";
435 $sql .=
", '".$this->db->idate($now).
"'";
436 $sql .=
", '".$this->db->escape($this->code_journal).
"'";
437 $sql .=
", ".(!empty($this->journal_label) ? (
"'".$this->db->escape($this->journal_label).
"'") :
"NULL");
438 $sql .=
", ".((int) $this->piece_num);
439 $sql .=
", ".(!isset($this->entity) ? $conf->entity : $this->entity);
442 $resql = $this->db->query($sql);
444 $id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
452 $this->errors[] =
'Error Create Error '.$result.
' lecture ID';
453 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
458 $this->errors[] =
'Error '.$this->db->lasterror();
459 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
464 $this->error =
'BookkeepingRecordAlreadyExists';
465 dol_syslog(__METHOD__.
' '.$this->error, LOG_WARNING);
470 $this->errors[] =
'Error '.$this->db->lasterror();
471 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
486 $this->db->rollback();
504 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
506 global $db, $conf, $langs;
507 global $dolibarr_main_authentication, $dolibarr_main_demo;
508 global $menumanager, $hookmanager;
510 if (!empty($conf->dol_no_mouse_hover)) {
517 $label =
'<u>'.$langs->trans(
"Transaction").
'</u>';
519 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->piece_num;
521 $url = DOL_URL_ROOT.
'/accountancy/bookkeeping/card.php?piece_num='.$this->piece_num;
523 if ($option !=
'nolink') {
525 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
526 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
527 $add_save_lastsearch_values = 1;
529 if ($add_save_lastsearch_values) {
530 $url .=
'&save_lastsearch_values=1';
535 if (empty($notooltip)) {
537 $label = $langs->trans(
"ShowTransaction");
538 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
540 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
541 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
543 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
546 $linkstart =
'<a href="'.$url.
'"';
547 $linkstart .= $linkclose.
'>';
550 $result .= $linkstart;
552 $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);
554 if ($withpicto != 2) {
555 $result .= $this->piece_num;
561 $hookmanager->initHooks(array($this->element .
'dao'));
562 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
563 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
565 $result = $hookmanager->resPrint;
567 $result .= $hookmanager->resPrint;
582 global $conf, $langs;
584 $langs->loadLangs(array(
"accountancy",
"bills",
"compta"));
591 if (isset($this->doc_type)) {
592 $this->doc_type = trim($this->doc_type);
594 if (isset($this->doc_ref)) {
595 $this->doc_ref = trim($this->doc_ref);
597 if (isset($this->fk_doc)) {
598 $this->fk_doc = (int) $this->fk_doc;
600 if (isset($this->fk_docdet)) {
601 $this->fk_docdet = (int) $this->fk_docdet;
603 if (isset($this->thirdparty_code)) {
604 $this->thirdparty_code = trim($this->thirdparty_code);
606 if (isset($this->subledger_account)) {
607 $this->subledger_account = trim($this->subledger_account);
609 if (isset($this->subledger_label)) {
610 $this->subledger_label = trim($this->subledger_label);
612 if (isset($this->numero_compte)) {
613 $this->numero_compte = trim($this->numero_compte);
615 if (isset($this->label_compte)) {
616 $this->label_compte = trim($this->label_compte);
618 if (isset($this->label_operation)) {
619 $this->label_operation = trim($this->label_operation);
621 if (isset($this->debit)) {
622 $this->debit = trim($this->debit);
624 if (isset($this->credit)) {
625 $this->credit = trim($this->credit);
627 if (isset($this->montant)) {
628 $this->montant = trim($this->montant);
630 if (isset($this->amount)) {
631 $this->amount = trim($this->amount);
633 if (isset($this->sens)) {
634 $this->sens = trim($this->sens);
636 if (isset($this->import_key)) {
637 $this->import_key = trim($this->import_key);
639 if (isset($this->code_journal)) {
640 $this->code_journal = trim($this->code_journal);
642 if (isset($this->journal_label)) {
643 $this->journal_label = trim($this->journal_label);
645 if (isset($this->piece_num)) {
646 $this->piece_num = trim($this->piece_num);
648 if (empty($this->debit)) {
651 if (empty($this->credit)) {
654 if (empty($this->montant)) {
661 } elseif ($result == 0) {
663 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateIsOnAClosedFiscalPeriod');
665 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateNotOnActiveFiscalPeriod');
670 $this->debit =
price2num($this->debit,
'MT');
671 $this->credit =
price2num($this->credit,
'MT');
672 $this->montant =
price2num($this->montant,
'MT');
677 $this->journal_label = $langs->trans($this->journal_label);
680 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.$mode.
' (';
682 $sql .=
'date_lim_reglement,';
686 $sql .=
'fk_docdet,';
687 $sql .=
'thirdparty_code,';
688 $sql .=
'subledger_account,';
689 $sql .=
'subledger_label,';
690 $sql .=
'numero_compte,';
691 $sql .=
'label_compte,';
692 $sql .=
'label_operation,';
697 $sql .=
'fk_user_author,';
698 $sql .=
'date_creation,';
699 $sql .=
'code_journal,';
700 $sql .=
'journal_label,';
701 $sql .=
'piece_num,';
703 $sql .=
') VALUES (';
704 $sql .=
' '.(!isset($this->doc_date) ||
dol_strlen($this->doc_date) == 0 ?
'NULL' :
"'".$this->db->idate($this->doc_date).
"'").
',';
705 $sql .=
' '.(!isset($this->date_lim_reglement) ||
dol_strlen($this->date_lim_reglement) == 0 ?
'NULL' :
"'".$this->db->idate($this->date_lim_reglement).
"'").
',';
706 $sql .=
' '.(!isset($this->doc_type) ?
'NULL' :
"'".$this->db->escape($this->doc_type).
"'").
',';
707 $sql .=
' '.(!isset($this->doc_ref) ?
'NULL' :
"'".$this->db->escape($this->doc_ref).
"'").
',';
708 $sql .=
' '.(empty($this->fk_doc) ?
'0' : (int) $this->fk_doc).
',';
709 $sql .=
' '.(empty($this->fk_docdet) ?
'0' : (int) $this->fk_docdet).
',';
710 $sql .=
' '.(!isset($this->thirdparty_code) ?
'NULL' :
"'".$this->db->escape($this->thirdparty_code).
"'").
',';
711 $sql .=
' '.(!isset($this->subledger_account) ?
'NULL' :
"'".$this->db->escape($this->subledger_account).
"'").
',';
712 $sql .=
' '.(!isset($this->subledger_label) ?
'NULL' :
"'".$this->db->escape($this->subledger_label).
"'").
',';
713 $sql .=
' '.(!isset($this->numero_compte) ?
'NULL' :
"'".$this->db->escape($this->numero_compte).
"'").
',';
714 $sql .=
' '.(!isset($this->label_compte) ?
'NULL' :
"'".$this->db->escape($this->label_compte).
"'").
',';
715 $sql .=
' '.(!isset($this->label_operation) ?
'NULL' :
"'".$this->db->escape($this->label_operation).
"'").
',';
716 $sql .=
' '.(!isset($this->debit) ?
'NULL' : $this->debit).
',';
717 $sql .=
' '.(!isset($this->credit) ?
'NULL' : $this->credit).
',';
718 $sql .=
' '.(!isset($this->montant) ?
'NULL' : $this->montant).
',';
719 $sql .=
' '.(!isset($this->sens) ?
'NULL' :
"'".$this->db->escape($this->sens).
"'").
',';
720 $sql .=
' '.((int) $user->id).
',';
721 $sql .=
' '.
"'".$this->db->idate($now).
"',";
722 $sql .=
' '.(empty($this->code_journal) ?
'NULL' :
"'".$this->db->escape($this->code_journal).
"'").
',';
723 $sql .=
' '.(empty($this->journal_label) ?
'NULL' :
"'".$this->db->escape($this->journal_label).
"'").
',';
724 $sql .=
' '.(empty($this->piece_num) ?
'NULL' : $this->db->escape($this->piece_num)).
',';
725 $sql .=
' '.(!isset($this->entity) ? $conf->entity : $this->entity);
730 $resql = $this->db->query($sql);
733 $this->errors[] =
'Error '.$this->db->lasterror();
734 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
738 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element.$mode);
753 $this->db->rollback();
771 public function fetch($id, $ref =
null, $mode =
'')
779 $sql .=
" t.doc_date,";
780 $sql .=
" t.date_lim_reglement,";
781 $sql .=
" t.doc_type,";
782 $sql .=
" t.doc_ref,";
783 $sql .=
" t.fk_doc,";
784 $sql .=
" t.fk_docdet,";
785 $sql .=
" t.thirdparty_code,";
786 $sql .=
" t.subledger_account,";
787 $sql .=
" t.subledger_label,";
788 $sql .=
" t.numero_compte,";
789 $sql .=
" t.label_compte,";
790 $sql .=
" t.label_operation,";
792 $sql .=
" t.credit,";
793 $sql .=
" t.montant as amount,";
795 $sql .=
" t.fk_user_author,";
796 $sql .=
" t.import_key,";
797 $sql .=
" t.code_journal,";
798 $sql .=
" t.journal_label,";
799 $sql .=
" t.piece_num,";
800 $sql .=
" t.date_creation,";
802 if ($mode !=
"_tmp") {
803 $sql .=
" t.date_export,";
805 $sql .=
" t.date_validated as date_validation";
806 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.$mode.
' as t';
807 $sql .=
' WHERE 1 = 1';
808 $sql .=
" AND entity = " . ((int) $conf->entity);
810 $sql .=
" AND t.rowid = ".((int) $ref);
812 $sql .=
" AND t.rowid = ".((int) $id);
815 $resql = $this->db->query($sql);
817 $numrows = $this->db->num_rows($resql);
819 $obj = $this->db->fetch_object($resql);
821 $this->
id = $obj->rowid;
823 $this->doc_date = $this->db->jdate($obj->doc_date);
824 $this->date_lim_reglement = $this->db->jdate($obj->date_lim_reglement);
825 $this->doc_type = $obj->doc_type;
826 $this->doc_ref = $obj->doc_ref;
827 $this->fk_doc = $obj->fk_doc;
828 $this->fk_docdet = $obj->fk_docdet;
829 $this->thirdparty_code = $obj->thirdparty_code;
830 $this->subledger_account = $obj->subledger_account;
831 $this->subledger_label = $obj->subledger_label;
832 $this->numero_compte = $obj->numero_compte;
833 $this->label_compte = $obj->label_compte;
834 $this->label_operation = $obj->label_operation;
835 $this->debit = $obj->debit;
836 $this->credit = $obj->credit;
837 $this->montant = $obj->amount;
838 $this->amount = $obj->amount;
839 $this->sens = $obj->sens;
840 $this->fk_user_author = $obj->fk_user_author;
841 $this->import_key = $obj->import_key;
842 $this->code_journal = $obj->code_journal;
843 $this->journal_label = $obj->journal_label;
844 $this->piece_num = $obj->piece_num;
845 $this->date_creation = $this->db->jdate($obj->date_creation);
846 $this->date_export = $this->db->jdate($obj->date_export);
847 $this->date_validation = isset($obj->date_validation) ? $this->db->jdate($obj->date_validation) :
'';
849 $this->db->free($resql);
857 $this->errors[] =
'Error '.$this->db->lasterror();
858 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
878 public function fetchAllByAccount($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND', $option = 0, $countonly = 0)
884 $this->lines = array();
889 $sql .=
' COUNT(t.rowid) as nb';
892 $sql .=
" t.doc_date,";
893 $sql .=
" t.doc_type,";
894 $sql .=
" t.doc_ref,";
895 $sql .=
" t.fk_doc,";
896 $sql .=
" t.fk_docdet,";
897 $sql .=
" t.thirdparty_code,";
898 $sql .=
" t.subledger_account,";
899 $sql .=
" t.subledger_label,";
900 $sql .=
" t.numero_compte,";
901 $sql .=
" t.label_compte,";
902 $sql .=
" t.label_operation,";
904 $sql .=
" t.credit,";
905 $sql .=
" t.montant as amount,";
907 $sql .=
" t.multicurrency_amount,";
908 $sql .=
" t.multicurrency_code,";
909 $sql .=
" t.lettering_code,";
910 $sql .=
" t.date_lettering,";
911 $sql .=
" t.fk_user_author,";
912 $sql .=
" t.import_key,";
913 $sql .=
" t.code_journal,";
914 $sql .=
" t.journal_label,";
915 $sql .=
" t.piece_num,";
916 $sql .=
" t.date_creation,";
917 $sql .=
" t.date_export,";
918 $sql .=
" t.date_validated as date_validation,";
919 $sql .=
" t.import_key";
923 if (count($filter) > 0) {
924 foreach ($filter as $key => $value) {
925 if ($key ==
't.doc_date') {
926 $sqlwhere[] = $key.
'=\''.$this->db->idate($value).
'\'';
927 } elseif ($key ==
't.doc_date>=' || $key ==
't.doc_date<=') {
928 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
929 } elseif ($key ==
't.numero_compte>=' || $key ==
't.numero_compte<=' || $key ==
't.subledger_account>=' || $key ==
't.subledger_account<=') {
930 $sqlwhere[] = $key.
'\''.$this->db->escape($value).
'\'';
931 } elseif ($key ==
't.fk_doc' || $key ==
't.fk_docdet' || $key ==
't.piece_num') {
932 $sqlwhere[] = $key.
'='.$value;
933 } elseif ($key ==
't.subledger_account' || $key ==
't.numero_compte') {
934 $sqlwhere[] = $key.
' LIKE \''.$this->db->escape($this->db->escapeforlike($value)).
'%\'';
935 } elseif ($key ==
't.date_creation>=' || $key ==
't.date_creation<=') {
936 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
937 } elseif ($key ==
't.date_export>=' || $key ==
't.date_export<=') {
938 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
939 } elseif ($key ==
't.date_validated>=' || $key ==
't.date_validated<=') {
940 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
941 } elseif ($key ==
't.credit' || $key ==
't.debit') {
943 } elseif ($key ==
't.reconciled_option') {
944 $sqlwhere[] =
't.lettering_code IS NULL';
945 } elseif ($key ==
't.code_journal' && !empty($value)) {
946 if (is_array($value)) {
947 $sqlwhere[] =
natural_search(
"t.code_journal", join(
',', $value), 3, 1);
951 } elseif ($key ==
't.search_accounting_code_in' && !empty($value)) {
952 $sqlwhere[] =
't.numero_compte IN ('.$this->db->sanitize($value, 1).
')';
958 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
959 $sql .=
' WHERE entity = ' . ((int) $conf->entity);
960 if (count($sqlwhere) > 0) {
961 $sql .=
" AND ".implode(
" ".$filtermode.
" ", $sqlwhere);
964 if (!empty($option)) {
965 $sql .=
" AND t.subledger_account IS NOT NULL";
966 $sql .=
" AND t.subledger_account <> ''";
967 $sortfield =
't.subledger_account'.($sortfield ?
','.$sortfield :
'');
968 $sortorder =
'ASC'.($sortfield ?
','.$sortfield :
'');
970 $sortfield =
't.numero_compte'.($sortfield ?
','.$sortfield :
'');
971 $sortorder =
'ASC'.($sortorder ?
','.$sortorder :
'');
975 $sql .= $this->db->order($sortfield, $sortorder);
976 if (!empty($limit)) {
977 $sql .= $this->db->plimit($limit + 1, $offset);
981 $resql = $this->db->query($sql);
984 $obj = $this->db->fetch_object($resql);
989 $num = $this->db->num_rows($resql);
992 while (($obj = $this->db->fetch_object($resql)) && (empty($limit) || $i < min($limit, $num))) {
995 $line->id = $obj->rowid;
997 $line->doc_date = $this->db->jdate($obj->doc_date);
998 $line->doc_type = $obj->doc_type;
999 $line->doc_ref = $obj->doc_ref;
1000 $line->fk_doc = $obj->fk_doc;
1001 $line->fk_docdet = $obj->fk_docdet;
1002 $line->thirdparty_code = $obj->thirdparty_code;
1003 $line->subledger_account = $obj->subledger_account;
1004 $line->subledger_label = $obj->subledger_label;
1005 $line->numero_compte = $obj->numero_compte;
1006 $line->label_compte = $obj->label_compte;
1007 $line->label_operation = $obj->label_operation;
1008 $line->debit = $obj->debit;
1009 $line->credit = $obj->credit;
1010 $line->montant = $obj->amount;
1011 $line->amount = $obj->amount;
1012 $line->sens = $obj->sens;
1013 $line->multicurrency_amount = $obj->multicurrency_amount;
1014 $line->multicurrency_code = $obj->multicurrency_code;
1015 $line->lettering_code = $obj->lettering_code;
1016 $line->date_lettering = $obj->date_lettering;
1017 $line->fk_user_author = $obj->fk_user_author;
1018 $line->import_key = $obj->import_key;
1019 $line->code_journal = $obj->code_journal;
1020 $line->journal_label = $obj->journal_label;
1021 $line->piece_num = $obj->piece_num;
1022 $line->date_creation = $this->db->jdate($obj->date_creation);
1023 $line->date_export = $this->db->jdate($obj->date_export);
1024 $line->date_validation = $this->db->jdate($obj->date_validation);
1025 $line->import_key = $obj->import_key;
1027 $this->lines[] = $line;
1032 $this->db->free($resql);
1036 $this->errors[] =
'Error '.$this->db->lasterror();
1037 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1055 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND', $showAlreadyExportMovements = 1)
1062 $sql .=
' t.rowid,';
1063 $sql .=
" t.doc_date,";
1064 $sql .=
" t.doc_type,";
1065 $sql .=
" t.doc_ref,";
1066 $sql .=
" t.fk_doc,";
1067 $sql .=
" t.fk_docdet,";
1068 $sql .=
" t.thirdparty_code,";
1069 $sql .=
" t.subledger_account,";
1070 $sql .=
" t.subledger_label,";
1071 $sql .=
" t.numero_compte,";
1072 $sql .=
" t.label_compte,";
1073 $sql .=
" t.label_operation,";
1074 $sql .=
" t.debit,";
1075 $sql .=
" t.credit,";
1076 $sql .=
" t.lettering_code,";
1077 $sql .=
" t.date_lettering,";
1078 $sql .=
" t.montant as amount,";
1080 $sql .=
" t.fk_user_author,";
1081 $sql .=
" t.import_key,";
1082 $sql .=
" t.code_journal,";
1083 $sql .=
" t.journal_label,";
1084 $sql .=
" t.piece_num,";
1085 $sql .=
" t.date_creation,";
1086 $sql .=
" t.date_lim_reglement,";
1087 $sql .=
" t.tms as date_modification,";
1088 $sql .=
" t.date_export,";
1089 $sql .=
" t.date_validated as date_validation";
1090 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
1092 $sqlwhere = array();
1093 if (count($filter) > 0) {
1094 foreach ($filter as $key => $value) {
1095 if ($key ==
't.doc_date') {
1096 $sqlwhere[] = $key.
'=\''.$this->db->idate($value).
'\'';
1097 } elseif ($key ==
't.doc_date>=' || $key ==
't.doc_date<=') {
1098 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
1099 } elseif ($key ==
't.numero_compte>=' || $key ==
't.numero_compte<=' || $key ==
't.subledger_account>=' || $key ==
't.subledger_account<=') {
1100 $sqlwhere[] = $key.
'\''.$this->db->escape($value).
'\'';
1101 } elseif ($key ==
't.fk_doc' || $key ==
't.fk_docdet' || $key ==
't.piece_num') {
1102 $sqlwhere[] = $key.
'='.((int) $value);
1103 } elseif ($key ==
't.subledger_account' || $key ==
't.numero_compte') {
1104 $sqlwhere[] = $key.
' LIKE \''.$this->db->escape($value).
'%\'';
1105 } elseif ($key ==
't.date_creation>=' || $key ==
't.date_creation<=') {
1106 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
1107 } elseif ($key ==
't.tms>=' || $key ==
't.tms<=') {
1108 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
1109 } elseif ($key ==
't.date_export>=' || $key ==
't.date_export<=') {
1110 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
1111 } elseif ($key ==
't.date_validated>=' || $key ==
't.date_validated<=') {
1112 $sqlwhere[] = $key.
'\''.$this->db->idate($value).
'\'';
1113 } elseif ($key ==
't.credit' || $key ==
't.debit') {
1115 } elseif ($key ==
't.code_journal' && !empty($value)) {
1116 if (is_array($value)) {
1117 $sqlwhere[] =
natural_search(
"t.code_journal", join(
',', $value), 3, 1);
1126 $sql .=
' WHERE t.entity = ' . ((int) $conf->entity);
1127 if ($showAlreadyExportMovements == 0) {
1128 $sql .=
" AND t.date_export IS NULL";
1130 if (count($sqlwhere) > 0) {
1131 $sql .=
' AND '.implode(
" ".$filtermode.
" ", $sqlwhere);
1133 if (!empty($sortfield)) {
1134 $sql .= $this->db->order($sortfield, $sortorder);
1136 if (!empty($limit)) {
1137 $sql .= $this->db->plimit($limit + 1, $offset);
1139 $this->lines = array();
1141 $resql = $this->db->query($sql);
1143 $num = $this->db->num_rows($resql);
1146 while (($obj = $this->db->fetch_object($resql)) && (empty($limit) || $i < min($limit, $num))) {
1149 $line->id = $obj->rowid;
1151 $line->doc_date = $this->db->jdate($obj->doc_date);
1152 $line->doc_type = $obj->doc_type;
1153 $line->doc_ref = $obj->doc_ref;
1154 $line->fk_doc = $obj->fk_doc;
1155 $line->fk_docdet = $obj->fk_docdet;
1156 $line->thirdparty_code = $obj->thirdparty_code;
1157 $line->subledger_account = $obj->subledger_account;
1158 $line->subledger_label = $obj->subledger_label;
1159 $line->numero_compte = $obj->numero_compte;
1160 $line->label_compte = $obj->label_compte;
1161 $line->label_operation = $obj->label_operation;
1162 $line->debit = $obj->debit;
1163 $line->credit = $obj->credit;
1164 $line->montant = $obj->amount;
1165 $line->amount = $obj->amount;
1166 $line->sens = $obj->sens;
1167 $line->lettering_code = $obj->lettering_code;
1168 $line->date_lettering = $obj->date_lettering;
1169 $line->fk_user_author = $obj->fk_user_author;
1170 $line->import_key = $obj->import_key;
1171 $line->code_journal = $obj->code_journal;
1172 $line->journal_label = $obj->journal_label;
1173 $line->piece_num = $obj->piece_num;
1174 $line->date_creation = $this->db->jdate($obj->date_creation);
1175 $line->date_lim_reglement = $this->db->jdate($obj->date_lim_reglement);
1176 $line->date_modification = $this->db->jdate($obj->date_modification);
1177 $line->date_export = $this->db->jdate($obj->date_export);
1178 $line->date_validation = $this->db->jdate($obj->date_validation);
1180 $this->lines[] = $line;
1184 $this->db->free($resql);
1188 $this->errors[] =
'Error '.$this->db->lasterror();
1189 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1206 public function fetchAllBalance($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND', $option = 0)
1210 $this->lines = array();
1215 $sql .=
" t.numero_compte,";
1216 if (!empty($option)) {
1217 $sql .=
" t.subledger_account,";
1218 $sql .=
" t.subledger_label,";
1220 $sql .=
" SUM(t.debit) as debit,";
1221 $sql .=
" SUM(t.credit) as credit";
1222 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
1224 $sqlwhere = array();
1225 if (count($filter) > 0) {
1226 foreach ($filter as $key => $value) {
1227 if ($key ==
't.doc_date') {
1228 $sqlwhere[] = $key.
" = '".$this->db->idate($value).
"'";
1229 } elseif ($key ==
't.doc_date>=' || $key ==
't.doc_date<=' || $key ==
't.doc_date>' || $key ==
't.doc_date<') {
1230 $sqlwhere[] = $key.
"'".$this->db->idate($value).
"'";
1231 } elseif ($key ==
't.numero_compte>=' || $key ==
't.numero_compte<=' || $key ==
't.subledger_account>=' || $key ==
't.subledger_account<=') {
1232 $sqlwhere[] = $key.
"'".$this->db->escape($value).
"'";
1233 } elseif ($key ==
't.fk_doc' || $key ==
't.fk_docdet' || $key ==
't.piece_num') {
1234 $sqlwhere[] = $key.
" = ".((int) $value);
1235 } elseif ($key ==
't.subledger_account' || $key ==
't.numero_compte') {
1236 $sqlwhere[] = $key.
" LIKE '".$this->db->escape($value).
"%'";
1237 } elseif ($key ==
't.subledger_label') {
1238 $sqlwhere[] = $key.
" LIKE '".$this->db->escape($value).
"%'";
1239 } elseif ($key ==
't.code_journal' && !empty($value)) {
1240 if (is_array($value)) {
1241 $sqlwhere[] =
natural_search(
"t.code_journal", join(
',', $value), 3, 1);
1245 } elseif ($key ==
't.reconciled_option') {
1246 $sqlwhere[] =
't.lettering_code IS NULL';
1248 $sqlwhere[] = $key.
" LIKE '%".$this->db->escape($value).
"%'";
1252 $sql .=
' WHERE entity = ' . ((int) $conf->entity);
1253 if (count($sqlwhere) > 0) {
1254 $sql .=
" AND ".implode(
" ".$filtermode.
" ", $sqlwhere);
1257 if (!empty($option)) {
1258 $sql .=
" AND t.subledger_account IS NOT NULL";
1259 $sql .=
" AND t.subledger_account <> ''";
1260 $sql .=
" GROUP BY t.numero_compte, t.subledger_account, t.subledger_label";
1261 $sortfield =
't.subledger_account'.($sortfield ?
','.$sortfield :
'');
1262 $sortorder =
'ASC'.($sortfield ?
','.$sortfield :
'');
1264 $sql .=
' GROUP BY t.numero_compte';
1265 $sortfield =
't.numero_compte'.($sortfield ?
','.$sortfield :
'');
1266 $sortorder =
'ASC'.($sortorder ?
','.$sortorder :
'');
1269 if (!empty($sortfield)) {
1270 $sql .= $this->db->order($sortfield, $sortorder);
1272 if (!empty($limit)) {
1273 $sql .= $this->db->plimit($limit + 1, $offset);
1276 $resql = $this->db->query($sql);
1278 $num = $this->db->num_rows($resql);
1281 while (($obj = $this->db->fetch_object($resql)) && (empty($limit) || $i < min($limit, $num))) {
1284 $line->numero_compte = $obj->numero_compte;
1286 if (!empty($option)) {
1287 $line->subledger_account = $obj->subledger_account;
1288 $line->subledger_label = $obj->subledger_label;
1290 $line->debit = $obj->debit;
1291 $line->credit = $obj->credit;
1293 $this->lines[] = $line;
1297 $this->db->free($resql);
1301 $this->errors[] =
'Error '.$this->db->lasterror();
1302 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1316 public function update(
User $user, $notrigger =
false, $mode =
'')
1324 if (isset($this->doc_type)) {
1325 $this->doc_type = trim($this->doc_type);
1327 if (isset($this->doc_ref)) {
1328 $this->doc_ref = trim($this->doc_ref);
1330 if (isset($this->fk_doc)) {
1331 $this->fk_doc = (int) $this->fk_doc;
1333 if (isset($this->fk_docdet)) {
1334 $this->fk_docdet = (int) $this->fk_docdet;
1336 if (isset($this->thirdparty_code)) {
1337 $this->thirdparty_code = trim($this->thirdparty_code);
1339 if (isset($this->subledger_account)) {
1340 $this->subledger_account = trim($this->subledger_account);
1342 if (isset($this->subledger_label)) {
1343 $this->subledger_label = trim($this->subledger_label);
1345 if (isset($this->numero_compte)) {
1346 $this->numero_compte = trim($this->numero_compte);
1348 if (isset($this->label_compte)) {
1349 $this->label_compte = trim($this->label_compte);
1351 if (isset($this->label_operation)) {
1352 $this->label_operation = trim($this->label_operation);
1354 if (isset($this->debit)) {
1355 $this->debit = trim($this->debit);
1357 if (isset($this->credit)) {
1358 $this->credit = trim($this->credit);
1360 if (isset($this->amount)) {
1361 $this->amount = trim($this->amount);
1363 if (isset($this->sens)) {
1364 $this->sens = trim($this->sens);
1366 if (isset($this->import_key)) {
1367 $this->import_key = trim($this->import_key);
1369 if (isset($this->code_journal)) {
1370 $this->code_journal = trim($this->code_journal);
1372 if (isset($this->journal_label)) {
1373 $this->journal_label = trim($this->journal_label);
1375 if (isset($this->piece_num)) {
1376 $this->piece_num = trim($this->piece_num);
1382 } elseif ($result == 0) {
1384 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateIsOnAClosedFiscalPeriod');
1386 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateNotOnActiveFiscalPeriod');
1391 $this->debit =
price2num($this->debit,
'MT');
1392 $this->credit =
price2num($this->credit,
'MT');
1398 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element.$mode.
' SET';
1399 $sql .=
' doc_date = '.(!isset($this->doc_date) ||
dol_strlen($this->doc_date) != 0 ?
"'".$this->db->idate($this->doc_date).
"'" :
'null').
',';
1400 $sql .=
' doc_type = '.(isset($this->doc_type) ?
"'".$this->db->escape($this->doc_type).
"'" :
"null").
',';
1401 $sql .=
' doc_ref = '.(isset($this->doc_ref) ?
"'".$this->db->escape($this->doc_ref).
"'" :
"null").
',';
1402 $sql .=
' fk_doc = '.(isset($this->fk_doc) ? $this->fk_doc :
"null").
',';
1403 $sql .=
' fk_docdet = '.(isset($this->fk_docdet) ? $this->fk_docdet :
"null").
',';
1404 $sql .=
' thirdparty_code = '.(isset($this->thirdparty_code) ?
"'".$this->db->escape($this->thirdparty_code).
"'" :
"null").
',';
1405 $sql .=
' subledger_account = '.(isset($this->subledger_account) ?
"'".$this->db->escape($this->subledger_account).
"'" :
"null").
',';
1406 $sql .=
' subledger_label = '.(isset($this->subledger_label) ?
"'".$this->db->escape($this->subledger_label).
"'" :
"null").
',';
1407 $sql .=
' numero_compte = '.(isset($this->numero_compte) ?
"'".$this->db->escape($this->numero_compte).
"'" :
"null").
',';
1408 $sql .=
' label_compte = '.(isset($this->label_compte) ?
"'".$this->db->escape($this->label_compte).
"'" :
"null").
',';
1409 $sql .=
' label_operation = '.(isset($this->label_operation) ?
"'".$this->db->escape($this->label_operation).
"'" :
"null").
',';
1410 $sql .=
' debit = '.(isset($this->debit) ? $this->debit :
"null").
',';
1411 $sql .=
' credit = '.(isset($this->credit) ? $this->credit :
"null").
',';
1412 $sql .=
' montant = '.(isset($this->montant) ? $this->montant :
"null").
',';
1413 $sql .=
' sens = '.(isset($this->sens) ?
"'".$this->db->escape($this->sens).
"'" :
"null").
',';
1414 $sql .=
' fk_user_author = '.(isset($this->fk_user_author) ? $this->fk_user_author :
"null").
',';
1415 $sql .=
' import_key = '.(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
',';
1416 $sql .=
' code_journal = '.(isset($this->code_journal) ?
"'".$this->db->escape($this->code_journal).
"'" :
"null").
',';
1417 $sql .=
' journal_label = '.(isset($this->journal_label) ?
"'".$this->db->escape($this->journal_label).
"'" :
"null").
',';
1418 $sql .=
' piece_num = '.(isset($this->piece_num) ? $this->piece_num :
"null");
1419 $sql .=
' WHERE rowid='.((int) $this->
id);
1423 $resql = $this->db->query($sql);
1426 $this->errors[] =
'Error '.$this->db->lasterror();
1427 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1442 $this->db->rollback();
1446 $this->db->commit();
1461 public function updateByMvt($piece_num =
'', $field =
'', $value =
'', $mode =
'')
1466 if (!isset($sql_filter)) {
1472 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.$mode;
1473 $sql .=
" SET ".$field.
" = ".(is_numeric($value) ? ((float) $value) :
"'".$this->db->escape($value).
"'");
1474 $sql .=
" WHERE piece_num = ".((int) $piece_num);
1475 $sql .= $sql_filter;
1477 $resql = $this->db->query($sql);
1481 $this->errors[] =
'Error '.$this->db->lasterror();
1482 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1485 $this->db->rollback();
1489 $this->db->commit();
1503 public function delete(
User $user, $notrigger = 0, $mode =
'')
1512 } elseif ($result == 0) {
1514 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateIsOnAClosedFiscalPeriod');
1516 $this->errors[] = $langs->trans(
'ErrorBookkeepingDocDateNotOnActiveFiscalPeriod');
1536 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.$mode;
1537 $sql .=
' WHERE rowid='.((int) $this->
id);
1539 $resql = $this->db->query($sql);
1542 $this->errors[] =
'Error '.$this->db->lasterror();
1543 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1549 $this->db->rollback();
1553 $this->db->commit();
1571 if (!isset($sql_filter)) {
1577 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1578 $sql .=
" WHERE import_key = '".$this->db->escape($importkey).
"'";
1579 $sql .= $sql_filter;
1581 $resql = $this->db->query($sql);
1584 $this->errors[] =
"Error ".$this->db->lasterror();
1585 dol_syslog(get_class($this).
"::delete Error ".$this->db->lasterror(), LOG_ERR);
1586 $this->db->rollback();
1590 $this->db->commit();
1605 global $conf, $langs;
1607 if (empty($delyear) && empty($journal)) {
1608 $this->error =
'ErrorOneFieldRequired';
1611 if (!empty($delmonth) && empty($delyear)) {
1612 $this->error =
'YearRequiredIfMonthDefined';
1617 if (!isset($sql_filter)) {
1625 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1626 $sql .=
" WHERE 1 = 1";
1628 if (!empty($journal)) {
1629 $sql .=
" AND code_journal = '".$this->db->escape($journal).
"'";
1631 $sql .=
" AND entity = " . ((int) $conf->entity);
1633 $sql .=
" AND date_validated IS NULL";
1634 $sql .= $sql_filter;
1638 $resql = $this->db->query($sql);
1641 $this->errors[] =
"Error ".$this->db->lasterror();
1642 foreach ($this->errors as $errmsg) {
1643 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
1644 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1646 $this->db->rollback();
1650 $this->db->commit();
1666 if (!isset($sql_filter)) {
1674 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1675 $sql .=
" WHERE piece_num = ".(int) $piecenum;
1676 $sql .=
" AND date_validated IS NULL";
1677 $sql .=
" AND entity = " . ((int) $conf->entity);
1678 $sql .= $sql_filter;
1680 $resql = $this->db->query($sql);
1683 $this->errors[] =
"Error ".$this->db->lasterror();
1684 foreach ($this->errors as $errmsg) {
1685 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
1686 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1688 $this->db->rollback();
1692 $this->db->commit();
1713 $object->fetch($fromid);
1721 $object->context[
'createfromclone'] =
'createfromclone';
1722 $result = $object->create($user);
1727 $this->errors = $object->errors;
1728 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1731 unset($object->context[
'createfromclone']);
1735 $this->db->commit();
1739 $this->db->rollback();
1758 $this->doc_date = $now;
1759 $this->doc_type =
'';
1760 $this->doc_ref =
'';
1762 $this->fk_docdet = 0;
1763 $this->thirdparty_code =
'CU001';
1764 $this->subledger_account =
'41100001';
1765 $this->subledger_label =
'My customer company';
1766 $this->numero_compte =
'411';
1767 $this->label_compte =
'Customer';
1768 $this->label_operation =
'Sales of pea';
1769 $this->debit = 99.9;
1770 $this->credit = 0.0;
1771 $this->amount = 0.0;
1773 $this->fk_user_author = $user->id;
1774 $this->import_key =
'20201027';
1775 $this->code_journal =
'VT';
1776 $this->journal_label =
'Journal de vente';
1777 $this->piece_num = 1234;
1778 $this->date_creation = $now;
1792 $sql =
"SELECT piece_num, doc_date,code_journal, journal_label, doc_ref, doc_type,";
1793 $sql .=
" date_creation, tms as date_modification, date_validated as date_validation";
1795 if ($mode !=
"_tmp") {
1796 $sql .=
", date_export";
1798 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1799 $sql .=
" WHERE piece_num = ".((int) $piecenum);
1800 $sql .=
" AND entity = " . ((int) $conf->entity);
1803 $result = $this->db->query($sql);
1805 $obj = $this->db->fetch_object($result);
1807 $this->piece_num = $obj->piece_num;
1808 $this->code_journal = $obj->code_journal;
1809 $this->journal_label = $obj->journal_label;
1810 $this->doc_date = $this->db->jdate($obj->doc_date);
1811 $this->doc_ref = $obj->doc_ref;
1812 $this->doc_type = $obj->doc_type;
1813 $this->date_creation = $this->db->jdate($obj->date_creation);
1814 $this->date_modification = $this->db->jdate($obj->date_modification);
1815 if ($mode !=
"_tmp") {
1816 $this->date_export = $this->db->jdate($obj->date_export);
1818 $this->date_validation = $this->db->jdate($obj->date_validation);
1820 $this->error =
"Error ".$this->db->lasterror();
1821 dol_syslog(__METHOD__.$this->error, LOG_ERR);
1838 $sql =
"SELECT MAX(piece_num)+1 as max FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1839 $sql .=
" WHERE entity = " . ((int) $conf->entity);
1841 dol_syslog(get_class($this).
"::getNextNumMvt", LOG_DEBUG);
1843 $result = $this->db->query($sql);
1846 $obj = $this->db->fetch_object($result);
1848 $result = $obj->max;
1850 if (empty($result)) {
1855 $this->error =
"Error ".$this->db->lasterror();
1856 dol_syslog(get_class($this).
"::getNextNumMvt ".$this->error, LOG_ERR);
1872 $sql =
"SELECT rowid, doc_date, doc_type,";
1873 $sql .=
" doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,";
1874 $sql .=
" numero_compte, label_compte, label_operation, debit, credit,";
1875 $sql .=
" montant as amount, sens, fk_user_author, import_key, code_journal, journal_label, piece_num,";
1876 $sql .=
" date_creation, tms as date_modification, date_validated as date_validation";
1878 if ($mode !=
"_tmp") {
1879 $sql .=
", date_export";
1881 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
1882 $sql .=
" WHERE piece_num = ".((int) $piecenum);
1883 $sql .=
" AND entity = " . ((int) $conf->entity);
1886 $result = $this->db->query($sql);
1888 while ($obj = $this->db->fetch_object($result)) {
1891 $line->id = $obj->rowid;
1893 $line->doc_date = $this->db->jdate($obj->doc_date);
1894 $line->doc_type = $obj->doc_type;
1895 $line->doc_ref = $obj->doc_ref;
1896 $line->fk_doc = $obj->fk_doc;
1897 $line->fk_docdet = $obj->fk_docdet;
1898 $line->thirdparty_code = $obj->thirdparty_code;
1899 $line->subledger_account = $obj->subledger_account;
1900 $line->subledger_label = $obj->subledger_label;
1901 $line->numero_compte = $obj->numero_compte;
1902 $line->label_compte = $obj->label_compte;
1903 $line->label_operation = $obj->label_operation;
1904 $line->debit = $obj->debit;
1905 $line->credit = $obj->credit;
1906 $line->montant = $obj->amount;
1907 $line->amount = $obj->amount;
1908 $line->sens = $obj->sens;
1909 $line->code_journal = $obj->code_journal;
1910 $line->journal_label = $obj->journal_label;
1911 $line->piece_num = $obj->piece_num;
1912 $line->date_creation = $obj->date_creation;
1913 $line->date_modification = $obj->date_modification;
1914 if ($mode !=
"_tmp") {
1915 $line->date_export = $obj->date_export;
1917 $line->date_validation = $obj->date_validation;
1919 $this->linesmvt[] = $line;
1922 $this->error =
"Error ".$this->db->lasterror();
1923 dol_syslog(__METHOD__.$this->error, LOG_ERR);
1942 $sql =
"SELECT rowid, doc_date, doc_type,";
1943 $sql .=
" doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,";
1944 $sql .=
" numero_compte, label_compte, label_operation, debit, credit,";
1945 $sql .=
" montant as amount, sens, fk_user_author, import_key, code_journal, piece_num,";
1946 $sql .=
" date_validated as date_validation";
1947 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element;
1948 $sql .=
" WHERE entity = " . ((int) $conf->entity);
1950 dol_syslog(get_class($this).
"::export_bookkeeping", LOG_DEBUG);
1952 $resql = $this->db->query($sql);
1955 $this->linesexport = array();
1957 $num = $this->db->num_rows($resql);
1958 while ($obj = $this->db->fetch_object($resql)) {
1961 $line->id = $obj->rowid;
1963 $line->doc_date = $this->db->jdate($obj->doc_date);
1964 $line->doc_type = $obj->doc_type;
1965 $line->doc_ref = $obj->doc_ref;
1966 $line->fk_doc = $obj->fk_doc;
1967 $line->fk_docdet = $obj->fk_docdet;
1968 $line->thirdparty_code = $obj->thirdparty_code;
1969 $line->subledger_account = $obj->subledger_account;
1970 $line->subledger_label = $obj->subledger_label;
1971 $line->numero_compte = $obj->numero_compte;
1972 $line->label_compte = $obj->label_compte;
1973 $line->label_operation = $obj->label_operation;
1974 $line->debit = $obj->debit;
1975 $line->credit = $obj->credit;
1976 $line->montant = $obj->amount;
1977 $line->amount = $obj->amount;
1978 $line->sens = $obj->sens;
1979 $line->code_journal = $obj->code_journal;
1980 $line->piece_num = $obj->piece_num;
1981 $line->date_validation = $obj->date_validation;
1983 $this->linesexport[] = $line;
1985 $this->db->free($resql);
1989 $this->error =
"Error ".$this->db->lasterror();
1990 dol_syslog(get_class($this).
"::export_bookkeeping ".$this->error, LOG_ERR);
2009 if (!isset($sql_filter)) {
2015 if ($direction == 0) {
2019 if ($next_piecenum < 0) {
2025 $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";
2026 $resql = $this->db->query($sql);
2029 $this->errors[] =
'Error '.$this->db->lasterror();
2030 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
2035 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.
' (doc_date, doc_type,';
2036 $sql .=
' doc_ref, fk_doc, fk_docdet, entity, thirdparty_code, subledger_account, subledger_label,';
2037 $sql .=
' numero_compte, label_compte, label_operation, debit, credit,';
2038 $sql .=
' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num, date_creation)';
2039 $sql .=
' SELECT doc_date, doc_type,';
2040 $sql .=
' doc_ref, fk_doc, fk_docdet, entity, thirdparty_code, subledger_account, subledger_label,';
2041 $sql .=
' numero_compte, label_compte, label_operation, debit, credit,';
2042 $sql .=
' montant, sens, fk_user_author, import_key, code_journal, journal_label, '.((int) $next_piecenum).
", '".$this->db->idate($now).
"'";
2043 $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);
2044 $sql .= $sql_filter;
2045 $resql = $this->db->query($sql);
2048 $this->errors[] =
'Error '.$this->db->lasterror();
2049 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
2054 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
'_tmp WHERE piece_num = '.((int) $piece_num).
' AND entity = ' .((int) $conf->entity);
2055 $resql = $this->db->query($sql);
2058 $this->errors[] =
'Error '.$this->db->lasterror();
2059 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
2062 } elseif ($direction == 1) {
2064 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
'_tmp WHERE piece_num = '.((int) $piece_num).
' AND entity = ' .((int) $conf->entity);
2065 $resql = $this->db->query($sql);
2068 $this->errors[] =
'Error '.$this->db->lasterror();
2069 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
2074 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.
'_tmp (doc_date, doc_type,';
2075 $sql .=
' doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,';
2076 $sql .=
' numero_compte, label_compte, label_operation, debit, credit,';
2077 $sql .=
' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num)';
2078 $sql .=
' SELECT doc_date, doc_type,';
2079 $sql .=
' doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,';
2080 $sql .=
' numero_compte, label_compte, label_operation, debit, credit,';
2081 $sql .=
' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num';
2082 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' WHERE piece_num = '.((int) $piece_num).
' AND entity = ' .((int) $conf->entity);
2083 $sql .= $sql_filter;
2084 $resql = $this->db->query($sql);
2087 $this->errors[] =
'Error '.$this->db->lasterror();
2088 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
2093 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
'_tmp WHERE piece_num = '.((int) $piece_num).
' AND entity = ' .((int) $conf->entity);
2094 $sql .= $sql_filter;
2095 $resql = $this->db->query($sql);
2098 $this->errors[] =
'Error '.$this->db->lasterror();
2099 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
2104 $this->db->commit();
2107 $this->db->rollback();
2135 public function select_account($selectid, $htmlname =
'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $aabase =
'')
2140 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
2144 $sql =
"SELECT DISTINCT ab.numero_compte as account_number, aa.label as label, aa.rowid as rowid, aa.fk_pcg_version";
2145 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as ab";
2146 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"accounting_account as aa ON aa.account_number = ab.numero_compte";
2147 $sql .=
" AND aa.active = 1";
2148 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
2149 $sql .=
" AND asy.rowid = ".((int) $pcgver);
2150 $sql .=
" AND ab.entity = " . ((int) $conf->entity);
2151 $sql .=
" ORDER BY account_number ASC";
2153 dol_syslog(get_class($this).
"::select_account", LOG_DEBUG);
2154 $resql = $this->db->query($sql);
2157 $this->error =
"Error ".$this->db->lasterror();
2158 dol_syslog(get_class($this).
"::select_account ".$this->error, LOG_ERR);
2167 while ($obj = $this->db->fetch_object($resql)) {
2170 $select_value_in = $obj->rowid;
2171 $select_value_out = $obj->rowid;
2173 if ($select_in == 1) {
2174 $select_value_in = $obj->account_number;
2176 if ($select_out == 1) {
2177 $select_value_out = $obj->account_number;
2182 if (($selectid !=
'') && $selectid == $select_value_in) {
2183 $selected = $select_value_out;
2186 $options[$select_value_out] = $label;
2189 $out .=
Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0,
'', 0, 0, 0,
'',
'maxwidth300');
2190 $this->db->free($resql);
2206 $sql =
"SELECT root.rowid, root.account_number, root.label as label,";
2207 $sql .=
" parent.rowid as parent_rowid, parent.account_number as parent_account_number, parent.label as parent_label";
2208 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_account as aa";
2209 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
2210 $sql .=
" AND asy.rowid = ".((int) $pcgver);
2211 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"accounting_account as parent ON aa.account_parent = parent.rowid AND parent.active = 1";
2212 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"accounting_account as root ON parent.account_parent = root.rowid AND root.active = 1";
2213 $sql .=
" WHERE aa.account_number = '".$this->db->escape($account).
"'";
2214 $sql .=
" AND aa.entity = " . ((int) $conf->entity);
2216 dol_syslog(get_class($this).
"::select_account", LOG_DEBUG);
2217 $resql = $this->db->query($sql);
2220 if ($this->db->num_rows($resql)) {
2221 $obj = $this->db->fetch_object($resql);
2224 $result = array(
'id'=>$obj->rowid,
'account_number'=>$obj->account_number,
'label'=>$obj->label);
2227 $this->error =
"Error ".$this->db->lasterror();
2228 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
2247 $sql =
"SELECT aa.account_number, aa.label, aa.rowid, aa.fk_pcg_version, cat.label as category";
2248 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_account as aa ";
2249 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
2250 $sql .=
" AND aa.account_number = '".$this->db->escape($account).
"'";
2251 $sql .=
" AND asy.rowid = ".((int) $pcgver);
2252 $sql .=
" AND aa.active = 1";
2253 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_accounting_category as cat ON aa.fk_accounting_category = cat.rowid";
2254 $sql .=
" WHERE aa.entity = " . ((int) $conf->entity);
2256 dol_syslog(get_class($this).
"::select_account", LOG_DEBUG);
2257 $resql = $this->db->query($sql);
2260 if ($this->db->num_rows($resql)) {
2261 $obj = $this->db->fetch_object($resql);
2263 if (empty($obj->category)) {
2266 return $obj->label.
' ('.$obj->category.
')';
2269 $this->error =
"Error ".$this->db->lasterror();
2270 dol_syslog(__METHOD__.
" ".$this->error, LOG_ERR);
2286 $alias = trim($alias);
2287 $alias = !empty($alias) && strpos($alias,
'.') < 0 ? $alias .
"." : $alias;
2289 if (!isset(self::$can_modify_bookkeeping_sql_cached[$alias]) || $force) {
2295 $sql_list = array();
2296 if (!empty($conf->cache[
'active_fiscal_period_cached']) && is_array($conf->cache[
'active_fiscal_period_cached'])) {
2297 foreach ($conf->cache[
'active_fiscal_period_cached'] as $fiscal_period) {
2298 $sql_list[] =
"('" . $this->db->idate($fiscal_period[
'date_start']) .
"' <= {$alias}doc_date AND {$alias}doc_date <= '" . $this->db->idate($fiscal_period[
'date_end']) .
"')";
2301 self::$can_modify_bookkeeping_sql_cached[$alias] = !empty($sql_list) ?
' AND (' . implode(
' OR ', $sql_list) .
')' :
'';
2304 return self::$can_modify_bookkeeping_sql_cached[$alias];
2326 $result = $bookkeeping->fetch($id,
null, $mode);
2331 if (!empty($conf->cache[
'closed_fiscal_period_cached']) && is_array($conf->cache[
'closed_fiscal_period_cached'])) {
2332 foreach ($conf->cache[
'closed_fiscal_period_cached'] as $fiscal_period) {
2333 if ($fiscal_period[
'date_start'] <= $bookkeeping->doc_date && $bookkeeping->doc_date <= $fiscal_period[
'date_end']) {
2347 $result = $bookkeeping->fetch($id,
null, $mode);
2352 if (!empty($conf->cache[
'active_fiscal_period_cached']) && is_array($conf->cache[
'active_fiscal_period_cached'])) {
2353 foreach ($conf->cache[
'active_fiscal_period_cached'] as $fiscal_period) {
2354 if ($fiscal_period[
'date_start'] <= $bookkeeping->doc_date && $bookkeeping->doc_date <= $fiscal_period[
'date_end']) {
2381 if (!empty($conf->cache[
'closed_fiscal_period_cached']) && is_array($conf->cache[
'closed_fiscal_period_cached'])) {
2382 foreach ($conf->cache[
'closed_fiscal_period_cached'] as $fiscal_period) {
2383 if ($fiscal_period[
'date_start'] <= $date && $date <= $fiscal_period[
'date_end']) {
2396 if (!empty($conf->cache[
'active_fiscal_period_cached']) && is_array($conf->cache[
'active_fiscal_period_cached'])) {
2397 foreach ($conf->cache[
'active_fiscal_period_cached'] as $fiscal_period) {
2398 if ($fiscal_period[
'date_start'] <= $date && $date <= $fiscal_period[
'date_end']) {
2419 if ($mode ==
'active') {
2420 if (!isset($conf->cache[
'active_fiscal_period_cached']) || $force) {
2421 $sql =
"SELECT date_start, date_end";
2422 $sql .=
" FROM " . $this->db->prefix() .
"accounting_fiscalyear";
2423 $sql .=
" WHERE entity = " . ((int) $conf->entity);
2424 $sql .=
" AND statut = 0";
2426 $resql = $this->db->query($sql);
2428 $this->errors[] = $this->db->lasterror();
2433 while ($obj = $this->db->fetch_object($resql)) {
2435 'date_start' => $this->db->jdate($obj->date_start),
2436 'date_end' => $this->db->jdate($obj->date_end),
2439 $conf->cache[
'active_fiscal_period_cached'] = $list;
2442 if ($mode ==
'closed') {
2443 if (!isset($conf->cache[
'closed_fiscal_period_cached']) || $force) {
2444 $sql =
"SELECT date_start, date_end";
2445 $sql .=
" FROM " . $this->db->prefix() .
"accounting_fiscalyear";
2446 $sql .=
" WHERE entity = " . ((int) $conf->entity);
2447 $sql .=
" AND statut = 1";
2449 $resql = $this->db->query($sql);
2451 $this->errors[] = $this->db->lasterror();
2456 while ($obj = $this->db->fetch_object($resql)) {
2458 'date_start' => $this->db->jdate($obj->date_start),
2459 'date_end' => $this->db->jdate($obj->date_end),
2462 $conf->cache[
'closed_fiscal_period_cached'] = $list;
2480 $sql =
"SELECT rowid, label, date_start, date_end, statut";
2481 $sql .=
" FROM " . $this->db->prefix() .
"accounting_fiscalyear";
2482 $sql .=
" WHERE entity = " . ((int) $conf->entity);
2483 if (!empty($filter)) {
2484 $sql .=
" AND (" . $filter .
')';
2486 $sql .= $this->db->order(
'date_start',
'ASC');
2488 $resql = $this->db->query($sql);
2490 $this->errors[] = $this->db->lasterror();
2494 while ($obj = $this->db->fetch_object($resql)) {
2495 $list[$obj->rowid] = array(
2496 'id' => $obj->rowid,
2497 'label' => $obj->label,
2498 'date_start' => $this->db->jdate($obj->date_start),
2499 'date_end' => $this->db->jdate($obj->date_end),
2500 'status' => $obj->statut,
2519 $sql =
"SELECT YEAR(b.doc_date) as year";
2520 for ($i = 1; $i <= 12; $i++) {
2521 $sql .=
", SUM(" . $this->db->ifsql(
"MONTH(b.doc_date)=" . $i,
"1",
"0") .
") AS month" . $i;
2523 $sql .=
", COUNT(b.rowid) as total";
2524 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping as b";
2525 $sql .=
" WHERE b.doc_date >= '" . $this->db->idate($date_start) .
"'";
2526 $sql .=
" AND b.doc_date <= '" . $this->db->idate($date_end) .
"'";
2527 $sql .=
" AND b.entity IN (" .
getEntity(
'bookkeeping', 0) .
")";
2533 $sql .=
" AND b.doc_date BETWEEN 0 AND 0";
2536 $sql .=
" AND date_validated IS NULL";
2539 $sql .=
" GROUP BY YEAR(b.doc_date)";
2540 $sql .= $this->db->order(
"year",
'ASC');
2543 $resql = $this->db->query($sql);
2545 $this->errors[] = $this->db->lasterror();
2549 while ($obj = $this->db->fetch_object($resql)) {
2550 $total += (int) $obj->total;
2552 'year' => (
int) $obj->year,
2554 'total' => (
int) $obj->total,
2556 for ($i = 1; $i <= 12; $i++) {
2557 $year_list[
'count'][$i] = (int) $obj->{
'month' . $i};
2560 $list[] = $year_list;
2563 $this->db->free($resql);
2585 $sql =
" UPDATE " . MAIN_DB_PREFIX .
"accounting_bookkeeping";
2586 $sql .=
" SET date_validated = '" . $this->db->idate($now) .
"'";
2587 $sql .=
" WHERE entity = " . ((int) $conf->entity);
2588 $sql .=
" AND DATE(doc_date) >= '" . $this->db->idate($date_start) .
"'";
2589 $sql .=
" AND DATE(doc_date) <= '" . $this->db->idate($date_end) .
"'";
2590 $sql .=
" AND date_validated IS NULL";
2593 $resql = $this->db->query($sql);
2595 $this->errors[] = $this->db->lasterror();
2611 public function closeFiscalPeriod($fiscal_period_id, $new_fiscal_period_id, $separate_auxiliary_account =
false, $generate_bookkeeping_records =
true)
2613 global $conf, $langs, $user;
2616 $fiscal_period_id = max(0, $fiscal_period_id);
2617 if (empty($fiscal_period_id)) {
2618 $langs->load(
'errors');
2619 $this->errors[] = $langs->trans(
'ErrorBadParameters');
2623 $result = $fiscal_period->fetch($fiscal_period_id);
2625 $this->error = $fiscal_period->error;
2626 $this->errors = $fiscal_period->errors;
2628 } elseif (empty($fiscal_period->id)) {
2629 $langs->loadLangs(array(
'errors',
'compta'));
2630 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'FiscalPeriod') .
' (' . $fiscal_period_id .
')';
2635 $new_fiscal_period_id = max(0, $new_fiscal_period_id);
2636 if (empty($new_fiscal_period_id)) {
2637 $langs->load(
'errors');
2638 $this->errors[] = $langs->trans(
'ErrorBadParameters');
2641 $new_fiscal_period =
new Fiscalyear($this->db);
2642 $result = $new_fiscal_period->fetch($new_fiscal_period_id);
2644 $this->error = $new_fiscal_period->error;
2645 $this->errors = $new_fiscal_period->errors;
2647 } elseif (empty($new_fiscal_period->id)) {
2648 $langs->loadLangs(array(
'errors',
'compta'));
2649 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'FiscalPeriod') .
' (' . $new_fiscal_period_id .
')';
2656 $fiscal_period->statut = Fiscalyear::STATUS_CLOSED;
2657 $fiscal_period->status = Fiscalyear::STATUS_CLOSED;
2658 $result = $fiscal_period->update($user);
2660 $this->error = $fiscal_period->error;
2661 $this->errors = $fiscal_period->errors;
2665 if (!$error && !empty($generate_bookkeeping_records)) {
2667 if (empty($journal_id)) {
2668 $langs->loadLangs(array(
'errors',
'accountancy'));
2669 $this->errors[] = $langs->trans(
'ErrorBadParameters') .
' - ' . $langs->trans(
'Codejournal') .
' (' . $langs->trans(
'AccountingJournalType9') .
')';
2676 $result = $journal->fetch($journal_id);
2678 $this->error = $journal->error;
2679 $this->errors = $journal->errors;
2681 } elseif ($result == 0) {
2682 $langs->loadLangs(array(
'errors',
'accountancy'));
2683 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'Codejournal') .
' (' . $langs->trans(
'AccountingJournalType9') .
')';
2689 $accounting_groups_used_for_balance_sheet_account = array_filter(array_map(
'trim', explode(
',',
getDolGlobalString(
'ACCOUNTING_CLOSURE_ACCOUNTING_GROUPS_USED_FOR_BALANCE_SHEET_ACCOUNT'))),
'strlen');
2690 $accounting_groups_used_for_income_statement = array_filter(array_map(
'trim', explode(
',',
getDolGlobalString(
'ACCOUNTING_CLOSURE_ACCOUNTING_GROUPS_USED_FOR_INCOME_STATEMENT'))),
'strlen');
2692 $pcg_type_filter = array();
2693 $tmp = array_merge($accounting_groups_used_for_balance_sheet_account, $accounting_groups_used_for_income_statement);
2694 foreach ($tmp as $item) {
2695 $pcg_type_filter[] =
"'" . $this->db->escape($item) .
"'";
2699 $sql .=
" t.numero_compte,";
2700 if ($separate_auxiliary_account) {
2701 $sql .=
" NULLIF(t.subledger_account, '') as subledger_account,";
2703 $sql .=
" aa.pcg_type,";
2704 $sql .=
" (SUM(t.credit) - SUM(t.debit)) as opening_balance";
2705 $sql .=
' FROM ' . MAIN_DB_PREFIX . $this->table_element .
' as t';
2706 $sql .=
' LEFT JOIN ' . MAIN_DB_PREFIX .
'accounting_account as aa ON aa.account_number = t.numero_compte';
2707 $sql .=
' WHERE t.entity = ' . ((int) $conf->entity);
2708 $sql .=
" AND aa.entity = ". ((int) $conf->entity);
2709 $sql .=
' AND aa.fk_pcg_version IN (SELECT pcg_version FROM '.MAIN_DB_PREFIX.
'accounting_system WHERE rowid = '.((int)
getDolGlobalInt(
'CHARTOFACCOUNTS')).
')';
2710 $sql .=
' AND aa.pcg_type IN (' . $this->db->sanitize(implode(
',', $pcg_type_filter), 1) .
')';
2711 $sql .=
" AND DATE(t.doc_date) >= '" . $this->db->idate($fiscal_period->date_start) .
"'";
2712 $sql .=
" AND DATE(t.doc_date) <= '" . $this->db->idate($fiscal_period->date_end) .
"'";
2713 $sql .=
' GROUP BY t.numero_compte, aa.pcg_type';
2714 if ($separate_auxiliary_account) {
2715 $sql .=
" , NULLIF(t.subledger_account, '')";
2717 $sql .=
' HAVING (SUM(t.credit) - SUM(t.debit)) != 0 ';
2718 $sql .= $this->db->order(
"t.numero_compte",
"ASC");
2720 $resql = $this->db->query($sql);
2722 $this->errors[] =
'Error ' . $this->db->lasterror();
2723 dol_syslog(__METHOD__ .
' ' . join(
',', $this->errors), LOG_ERR);
2728 $income_statement_amount = 0;
2729 while ($obj = $this->db->fetch_object($resql)) {
2730 if (in_array($obj->pcg_type, $accounting_groups_used_for_income_statement)) {
2731 $income_statement_amount += $obj->opening_balance;
2734 $mt = $obj->opening_balance;
2737 $bookkeeping->doc_date = $new_fiscal_period->date_start;
2738 $bookkeeping->date_lim_reglement =
'';
2739 $bookkeeping->doc_ref = $fiscal_period->label;
2740 $bookkeeping->date_creation = $now;
2741 $bookkeeping->doc_type =
'closure';
2742 $bookkeeping->fk_doc = $fiscal_period->id;
2743 $bookkeeping->fk_docdet = 0;
2744 $bookkeeping->thirdparty_code =
'';
2746 if ($separate_auxiliary_account) {
2747 $bookkeeping->subledger_account = $obj->subledger_account;
2749 $sql .=
" subledger_label";
2750 $sql .=
" FROM " . MAIN_DB_PREFIX . $this->table_element;
2751 $sql .=
" WHERE subledger_account = '" . $this->db->escape($obj->subledger_account) .
"'";
2752 $sql .=
" ORDER BY doc_date DESC";
2754 $result = $this->db->query($sql);
2756 $this->errors[] =
'Error: ' . $this->db->lasterror();
2757 dol_syslog(__METHOD__ .
' ' . join(
',', $this->errors), LOG_ERR);
2760 $objtmp = $this->db->fetch_object($result);
2761 $bookkeeping->subledger_label = $objtmp->subledger_label;
2763 $bookkeeping->subledger_account =
null;
2764 $bookkeeping->subledger_label =
null;
2767 $bookkeeping->numero_compte = $obj->numero_compte;
2769 $accountingaccount->fetch(
'', $obj->numero_compte);
2770 $bookkeeping->label_compte = $accountingaccount->label;
2772 $bookkeeping->label_operation = $new_fiscal_period->label;
2773 $bookkeeping->montant = $mt;
2774 $bookkeeping->sens = ($mt >= 0) ?
'C' :
'D';
2775 $bookkeeping->debit = ($mt < 0) ? -$mt : 0;
2776 $bookkeeping->credit = ($mt >= 0) ? $mt : 0;
2777 $bookkeeping->code_journal = $journal->code;
2778 $bookkeeping->journal_label = $langs->transnoentities($journal->label);
2779 $bookkeeping->fk_user_author = $user->id;
2780 $bookkeeping->entity = $conf->entity;
2782 $result = $bookkeeping->create($user);
2784 $this->error = $bookkeeping->error;
2785 $this->errors = $bookkeeping->errors;
2793 if (!$error && $income_statement_amount != 0) {
2794 $mt = $income_statement_amount;
2796 $accountingaccount->fetch(
null,
getDolGlobalString($income_statement_amount < 0 ?
'ACCOUNTING_RESULT_LOSS' :
'ACCOUNTING_RESULT_PROFIT'),
true);
2799 $bookkeeping->doc_date = $new_fiscal_period->date_start;
2800 $bookkeeping->date_lim_reglement =
'';
2801 $bookkeeping->doc_ref = $fiscal_period->label;
2802 $bookkeeping->date_creation = $now;
2803 $bookkeeping->doc_type =
'closure';
2804 $bookkeeping->fk_doc = $fiscal_period->id;
2805 $bookkeeping->fk_docdet = 0;
2806 $bookkeeping->thirdparty_code =
'';
2808 if ($separate_auxiliary_account) {
2809 $bookkeeping->subledger_account = $obj->subledger_account;
2811 $sql .=
" subledger_label";
2812 $sql .=
" FROM " . MAIN_DB_PREFIX . $this->table_element;
2813 $sql .=
" WHERE subledger_account = '" . $this->db->escape($obj->subledger_account) .
"'";
2814 $sql .=
" ORDER BY doc_date DESC";
2816 $result = $this->db->query($sql);
2818 $this->errors[] =
'Error: ' . $this->db->lasterror();
2819 dol_syslog(__METHOD__ .
' ' . join(
',', $this->errors), LOG_ERR);
2822 $objtmp = $this->db->fetch_object($result);
2823 $bookkeeping->subledger_label = $objtmp->subledger_label;
2825 $bookkeeping->subledger_account =
null;
2826 $bookkeeping->subledger_label =
null;
2829 $bookkeeping->numero_compte = $accountingaccount->account_number;
2830 $bookkeeping->label_compte = $accountingaccount->label;
2832 $bookkeeping->label_operation = $new_fiscal_period->label;
2833 $bookkeeping->montant = $mt;
2834 $bookkeeping->sens = ($mt >= 0) ?
'C' :
'D';
2835 $bookkeeping->debit = ($mt < 0) ? -$mt : 0;
2836 $bookkeeping->credit = ($mt >= 0) ? $mt : 0;
2837 $bookkeeping->code_journal = $journal->code;
2838 $bookkeeping->journal_label = $langs->transnoentities($journal->label);
2839 $bookkeeping->fk_user_author = $user->id;
2840 $bookkeeping->entity = $conf->entity;
2842 $result = $bookkeeping->create($user);
2844 $this->error = $bookkeeping->error;
2845 $this->errors = $bookkeeping->errors;
2849 $this->db->free($resql);
2855 $this->db->rollback();
2858 $this->db->commit();
2875 global $conf, $langs, $user;
2878 $fiscal_period_id = max(0, $fiscal_period_id);
2879 if (empty($fiscal_period_id)) {
2880 $langs->load(
'errors');
2881 $this->errors[] = $langs->trans(
'ErrorBadParameters');
2885 $result = $fiscal_period->fetch($fiscal_period_id);
2887 $this->error = $fiscal_period->error;
2888 $this->errors = $fiscal_period->errors;
2890 } elseif (empty($fiscal_period->id)) {
2891 $langs->loadLangs(array(
'errors',
'compta'));
2892 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'FiscalPeriod') .
' (' . $fiscal_period_id .
')';
2897 $new_fiscal_period_id = max(0, $new_fiscal_period_id);
2898 if (empty($new_fiscal_period_id)) {
2899 $langs->load(
'errors');
2900 $this->errors[] = $langs->trans(
'ErrorBadParameters');
2903 $new_fiscal_period =
new Fiscalyear($this->db);
2904 $result = $new_fiscal_period->fetch($new_fiscal_period_id);
2906 $this->error = $new_fiscal_period->error;
2907 $this->errors = $new_fiscal_period->errors;
2909 } elseif (empty($new_fiscal_period->id)) {
2910 $langs->loadLangs(array(
'errors',
'compta'));
2911 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'FiscalPeriod') .
' (' . $new_fiscal_period_id .
')';
2916 $inventory_journal_id = max(0, $inventory_journal_id);
2917 if (empty($inventory_journal_id)) {
2918 $langs->load(
'errors');
2919 $this->errors[] = $langs->trans(
'ErrorBadParameters');
2924 $result = $inventory_journal->fetch($inventory_journal_id);
2926 $this->error = $inventory_journal->error;
2927 $this->errors = $inventory_journal->errors;
2929 } elseif ($result == 0) {
2930 $langs->loadLangs(array(
'errors',
'accountancy'));
2931 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'InventoryJournal');
2938 $sql =
'SELECT t.rowid';
2939 $sql .=
' FROM ' . MAIN_DB_PREFIX . $this->table_element .
' as t';
2940 $sql .=
' WHERE t.entity = ' . ((int) $conf->entity);
2941 $sql .=
" AND code_journal = '" . $this->db->escape($inventory_journal->code) .
"'";
2942 $sql .=
" AND DATE(t.doc_date) >= '" . $this->db->idate($date_start) .
"'";
2943 $sql .=
" AND DATE(t.doc_date) <= '" . $this->db->idate($date_end) .
"'";
2944 $sql .=
" AND DATE(t.doc_date) >= '" . $this->db->idate($fiscal_period->date_start) .
"'";
2945 $sql .=
" AND DATE(t.doc_date) <= '" . $this->db->idate($fiscal_period->date_end) .
"'";
2947 $resql = $this->db->query($sql);
2949 $this->errors[] =
'Error ' . $this->db->lasterror();
2950 dol_syslog(__METHOD__ .
' ' . join(
',', $this->errors), LOG_ERR);
2955 while ($obj = $this->db->fetch_object($resql)) {
2957 $result = $bookkeeping->fetch($obj->rowid);
2959 $this->error = $inventory_journal->error;
2960 $this->errors = $inventory_journal->errors;
2963 } elseif ($result == 0) {
2964 $langs->loadLangs(array(
'errors',
'accountancy'));
2965 $this->errors[] = $langs->trans(
'ErrorRecordNotFound') .
' - ' . $langs->trans(
'LineId') .
': ' . $obj->rowid;
2970 $bookkeeping->id = 0;
2971 $bookkeeping->doc_date = $new_fiscal_period->date_start;
2972 $bookkeeping->doc_ref = $new_fiscal_period->label;
2973 $bookkeeping->date_creation = $now;
2974 $bookkeeping->doc_type =
'accounting_reversal';
2975 $bookkeeping->fk_doc = $new_fiscal_period->id;
2976 $bookkeeping->fk_docdet = 0;
2978 $bookkeeping->montant = -$bookkeeping->montant;
2979 $bookkeeping->sens = ($bookkeeping->montant >= 0) ?
'C' :
'D';
2980 $old_debit = $bookkeeping->debit;
2981 $bookkeeping->debit = $bookkeeping->credit;
2982 $bookkeeping->credit = $old_debit;
2984 $bookkeeping->fk_user_author = $user->id;
2985 $bookkeeping->entity = $conf->entity;
2987 $result = $bookkeeping->create($user);
2989 $this->error = $bookkeeping->error;
2990 $this->errors = $bookkeeping->errors;
2995 $this->db->free($resql);
2999 $this->db->rollback();
3002 $this->db->commit();
3018 public $doc_date =
'';
3032 public $thirdparty_code;
3033 public $subledger_account;
3034 public $subledger_label;
3035 public $numero_compte;
3036 public $label_compte;
3037 public $label_operation;
3055 public $multicurrency_amount;
3060 public $multicurrency_code;
3066 public $lettering_code;
3067 public $date_lettering;
3072 public $fk_user_author;
3075 public $code_journal;
3076 public $journal_label;
3082 public $date_creation;
3087 public $date_modification;
3092 public $date_export;
3097 public $date_validation;
3102 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.