184 public function fetch($rowid =
null, $account_number =
null, $limittocurrentchart = 0, $limittoachartaccount =
'')
188 if ($rowid || $account_number) {
189 $sql =
"SELECT a.rowid as rowid, a.datec, a.tms, a.fk_pcg_version, a.pcg_type, a.account_number, a.account_parent, a.label, a.labelshort, a.fk_accounting_category, a.fk_user_author, a.fk_user_modif, a.active, a.reconcilable";
190 $sql .=
", ca.label as category_label";
191 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_account as a";
192 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_accounting_category as ca ON a.fk_accounting_category = ca.rowid";
195 $sql .=
" a.rowid = ".(int) $rowid;
196 } elseif ($account_number) {
197 $sql .=
" a.account_number = '".$this->db->escape($account_number).
"'";
198 $sql .=
" AND a.entity = ".$conf->entity;
200 if (!empty($limittocurrentchart)) {
201 $sql .=
' AND a.fk_pcg_version IN (SELECT pcg_version FROM '.MAIN_DB_PREFIX.
'accounting_system WHERE rowid = '.((int)
getDolGlobalInt(
'CHARTOFACCOUNTS')).
')';
203 if (!empty($limittoachartaccount)) {
204 $sql .=
" AND a.fk_pcg_version = '".$this->db->escape($limittoachartaccount).
"'";
207 dol_syslog(get_class($this).
"::fetch rowid=".$rowid.
" account_number=".$account_number, LOG_DEBUG);
209 $result = $this->db->query($sql);
211 $obj = $this->db->fetch_object($result);
214 $this->
id = $obj->rowid;
215 $this->
rowid = $obj->rowid;
216 $this->
ref = $obj->account_number;
217 $this->datec = $this->db->jdate($obj->datec);
218 $this->date_creation = $this->db->jdate($obj->datec);
219 $this->date_modification = $this->db->jdate($obj->tms);
221 $this->fk_pcg_version = $obj->fk_pcg_version;
222 $this->pcg_type = $obj->pcg_type;
223 $this->account_number = $obj->account_number;
224 $this->account_parent = $obj->account_parent;
225 $this->label = $obj->label;
226 $this->labelshort = $obj->labelshort;
227 $this->account_category = $obj->fk_accounting_category;
228 $this->account_category_label = $obj->category_label;
229 $this->fk_user_author = $obj->fk_user_author;
230 $this->fk_user_modif = $obj->fk_user_modif;
231 $this->active = $obj->active;
232 $this->status = $obj->active;
233 $this->reconcilable = $obj->reconcilable;
240 $this->error =
"Error ".$this->db->lasterror();
241 $this->errors[] =
"Error ".$this->db->lasterror();
254 public function create($user, $notrigger = 0)
261 if (isset($this->fk_pcg_version)) {
262 $this->fk_pcg_version = trim($this->fk_pcg_version);
264 if (isset($this->pcg_type)) {
265 $this->pcg_type = trim($this->pcg_type);
267 if (isset($this->account_number)) {
268 $this->account_number = trim($this->account_number);
270 if (isset($this->label)) {
271 $this->label = trim($this->label);
273 if (isset($this->labelshort)) {
274 $this->labelshort = trim($this->labelshort);
277 if (empty($this->pcg_type) || $this->pcg_type ==
'-1') {
278 $this->pcg_type =
'XXXXXX';
284 $sql =
"INSERT INTO " . MAIN_DB_PREFIX .
"accounting_account(";
287 $sql .=
", fk_pcg_version";
288 $sql .=
", pcg_type";
289 $sql .=
", account_number";
290 $sql .=
", account_parent";
292 $sql .=
", labelshort";
293 $sql .=
", fk_accounting_category";
294 $sql .=
", fk_user_author";
296 $sql .=
", reconcilable";
297 $sql .=
") VALUES (";
298 $sql .=
" '".$this->db->idate($now).
"'";
299 $sql .=
", ".((int) $conf->entity);
300 $sql .=
", ".(empty($this->fk_pcg_version) ?
'NULL' :
"'".$this->db->escape($this->fk_pcg_version).
"'");
301 $sql .=
", ".(empty($this->pcg_type) ?
'NULL' :
"'".$this->db->escape($this->pcg_type).
"'");
302 $sql .=
", ".(empty($this->account_number) ?
'NULL' :
"'".$this->db->escape($this->account_number).
"'");
303 $sql .=
", ".(empty($this->account_parent) ? 0 : (int) $this->account_parent);
304 $sql .=
", ".(empty($this->label) ?
"''" :
"'".$this->db->escape($this->label).
"'");
305 $sql .=
", ".(empty($this->labelshort) ?
"''" :
"'".$this->db->escape($this->labelshort).
"'");
306 $sql .=
", ".(empty($this->account_category) ? 0 : (int) $this->account_category);
307 $sql .=
", ".((int) $user->id);
308 $sql .=
", ".(int) $this->active;
309 $sql .=
", ".(int) $this->reconcilable;
314 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
315 $resql = $this->db->query($sql);
318 $this->errors[] =
"Error " . $this->db->lasterror();
322 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX .
"accounting_account");
337 foreach ($this->errors as $errmsg) {
338 dol_syslog(get_class($this) .
"::create " . $errmsg, LOG_ERR);
339 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
341 $this->db->rollback();
486 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle =
'', $notooltip = 0, $save_lastsearch_value = -1, $withcompletelabel = 0, $option =
'')
488 global $langs, $conf, $hookmanager;
489 require_once DOL_DOCUMENT_ROOT .
'/core/lib/accounting.lib.php';
491 if (!empty($conf->dol_no_mouse_hover)) {
499 if (empty($option) || $option ==
'ledger') {
500 $url = DOL_URL_ROOT .
'/accountancy/bookkeeping/listbyaccount.php?search_accountancy_code_start=' . urlencode($this->account_number) .
'&search_accountancy_code_end=' . urlencode($this->account_number);
501 $labelurl = $langs->trans(
"ShowAccountingAccountInLedger");
502 } elseif ($option ==
'journals') {
503 $url = DOL_URL_ROOT .
'/accountancy/bookkeeping/list.php?search_accountancy_code_start=' . urlencode($this->account_number) .
'&search_accountancy_code_end=' . urlencode($this->account_number);
504 $labelurl = $langs->trans(
"ShowAccountingAccountInJournals");
505 } elseif ($option ==
'accountcard') {
506 $url = DOL_URL_ROOT .
'/accountancy/admin/card.php?id=' . urlencode($this->
id);
507 $labelurl = $langs->trans(
"ShowAccountingAccount");
511 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
512 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
513 $add_save_lastsearch_values = 1;
515 if ($add_save_lastsearch_values) {
516 $url .=
'&save_lastsearch_values=1';
519 $picto =
'accounting_account';
522 if (empty($this->labelshort) || $withcompletelabel == 1) {
523 $labeltoshow = $this->label;
525 $labeltoshow = $this->labelshort;
528 $label =
'<u>' . $labelurl .
'</u>';
529 if (!empty($this->account_number)) {
530 $label .=
'<br><b>' . $langs->trans(
'AccountAccounting') .
':</b> ' .
length_accountg($this->account_number);
532 if (!empty($labeltoshow)) {
533 $label .=
'<br><b>' . $langs->trans(
'Label') .
':</b> ' . $labeltoshow;
536 $label .=
' - ' . $moretitle;
540 if (empty($notooltip)) {
541 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
546 $linkclose .=
' class="classfortooltip"';
549 $linkstart =
'<a href="' . $url .
'"';
550 $linkstart .= $linkclose .
'>';
561 $label_link .=
' - ' . ($nourl ?
'<span class="opacitymedium">' :
'') . $labeltoshow . ($nourl ?
'</span>' :
'');
565 $result .= ($linkstart .
img_object(($notooltip ?
'' : $label), $picto, ($notooltip ?
'' :
'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1) . $linkend);
567 if ($withpicto && $withpicto != 2) {
570 if ($withpicto != 2) {
571 $result .= $linkstart . $label_link . $linkend;
574 $hookmanager->initHooks(array($this->element .
'dao'));
575 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
576 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
578 $result = $hookmanager->resPrint;
580 $result .= $hookmanager->resPrint;
748 $hookmanager->initHooks(array(
'accountancyBindingCalculation'));
751 $parameters = array(
'buyer' => $buyer,
'seller' => $seller,
'product' => $product,
'facture' => $facture,
'factureDet' => $factureDet ,
'accountingAccount'=>$accountingAccount, $type);
752 $reshook = $hookmanager->executeHooks(
'accountancyBindingCalculation', $parameters);
754 if (empty($reshook)) {
756 if ($type ==
'customer') {
757 $const_name =
"SOLD";
758 } elseif ($type ==
'supplier') {
762 require_once DOL_DOCUMENT_ROOT .
'/core/lib/company.lib.php';
763 $isBuyerInEEC =
isInEEC($buyer);
764 $isSellerInEEC =
isInEEC($seller);
771 $suggestedaccountingaccountbydefaultfor =
'';
772 if ($factureDet->product_type == 1) {
773 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) {
774 $code_l = (!empty($conf->global->{
'ACCOUNTING_SERVICE_' . $const_name .
'_ACCOUNT'}) ? $conf->global->{
'ACCOUNTING_SERVICE_' . $const_name .
'_ACCOUNT'} :
'');
775 $suggestedaccountingaccountbydefaultfor =
'';
777 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
778 $code_l = (!empty($conf->global->{
'ACCOUNTING_SERVICE_' . $const_name .
'_ACCOUNT'}) ? $conf->global->{
'ACCOUNTING_SERVICE_' . $const_name .
'_ACCOUNT'} :
'');
779 $suggestedaccountingaccountbydefaultfor =
'eecwithvat';
780 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
781 $code_l = (!empty($conf->global->{
'ACCOUNTING_SERVICE_' . $const_name .
'_ACCOUNT'}) ? $conf->global->{
'ACCOUNTING_SERVICE_' . $const_name .
'_ACCOUNT'} :
'');
782 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
783 } elseif ($isSellerInEEC && $isBuyerInEEC) {
784 $code_l = (!empty($conf->global->{
'ACCOUNTING_SERVICE_' . $const_name .
'_INTRA_ACCOUNT'}) ? $conf->global->{
'ACCOUNTING_SERVICE_' . $const_name .
'_INTRA_ACCOUNT'} :
'');
785 $suggestedaccountingaccountbydefaultfor =
'eec';
787 $code_l = (!empty($conf->global->{
'ACCOUNTING_SERVICE_' . $const_name .
'_EXPORT_ACCOUNT'}) ? $conf->global->{
'ACCOUNTING_SERVICE_' . $const_name .
'_EXPORT_ACCOUNT'} :
'');
788 $suggestedaccountingaccountbydefaultfor =
'export';
791 } elseif ($factureDet->product_type == 0) {
792 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) {
793 $code_l = (!empty($conf->global->{
'ACCOUNTING_PRODUCT_' . $const_name .
'_ACCOUNT'}) ? $conf->global->{
'ACCOUNTING_PRODUCT_' . $const_name .
'_ACCOUNT'} :
'');
794 $suggestedaccountingaccountbydefaultfor =
'';
796 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
797 $code_l = (!empty($conf->global->{
'ACCOUNTING_PRODUCT_' . $const_name .
'_ACCOUNT'}) ? $conf->global->{
'ACCOUNTING_PRODUCT_' . $const_name .
'_ACCOUNT'} :
'');
798 $suggestedaccountingaccountbydefaultfor =
'eecwithvat';
799 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
800 $code_l = (!empty($conf->global->{
'ACCOUNTING_PRODUCT_' . $const_name .
'_ACCOUNT'}) ? $conf->global->{
'ACCOUNTING_PRODUCT_' . $const_name .
'_ACCOUNT'} :
'');
801 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
802 } elseif ($isSellerInEEC && $isBuyerInEEC) {
803 $code_l = (!empty($conf->global->{
'ACCOUNTING_PRODUCT_' . $const_name .
'_INTRA_ACCOUNT'}) ? $conf->global->{
'ACCOUNTING_PRODUCT_' . $const_name .
'_INTRA_ACCOUNT'} :
'');
804 $suggestedaccountingaccountbydefaultfor =
'eec';
806 $code_l = (!empty($conf->global->{
'ACCOUNTING_PRODUCT_' . $const_name .
'_EXPORT_ACCOUNT'}) ? $conf->global->{
'ACCOUNTING_PRODUCT_' . $const_name .
'_EXPORT_ACCOUNT'} :
'');
807 $suggestedaccountingaccountbydefaultfor =
'export';
816 $suggestedaccountingaccountfor =
'';
817 if ((($buyer->country_code == $seller->country_code) || empty($buyer->country_code))) {
819 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
820 $code_p = $product->accountancy_code_sell;
821 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
822 $code_p = $product->accountancy_code_buy;
824 $suggestedid = $accountingAccount[
'dom'];
825 $suggestedaccountingaccountfor =
'prodserv';
827 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
829 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
830 $code_p = $product->accountancy_code_sell;
831 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
832 $code_p = $product->accountancy_code_buy;
834 $suggestedid = $accountingAccount[
'dom'];
835 $suggestedaccountingaccountfor =
'eecwithvat';
836 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
838 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
839 $code_p = $product->accountancy_code_sell;
840 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
841 $code_p = $product->accountancy_code_buy;
843 $suggestedid = $accountingAccount[
'dom'];
844 $suggestedaccountingaccountfor =
'eecwithoutvatnumber';
845 } elseif ($isSellerInEEC && $isBuyerInEEC) {
847 if ($type ==
'customer' && !empty($product->accountancy_code_sell_intra)) {
848 $code_p = $product->accountancy_code_sell_intra;
849 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_intra)) {
850 $code_p = $product->accountancy_code_buy_intra;
852 $suggestedid = $accountingAccount[
'intra'];
853 $suggestedaccountingaccountfor =
'eec';
856 if ($type ==
'customer' && !empty($product->accountancy_code_sell_export)) {
857 $code_p = $product->accountancy_code_sell_export;
858 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_export)) {
859 $code_p = $product->accountancy_code_buy_export;
861 $suggestedid = $accountingAccount[
'export'];
862 $suggestedaccountingaccountfor =
'export';
867 if (!empty($conf->global->ACCOUNTANCY_USE_PRODUCT_ACCOUNT_ON_THIRDPARTY)) {
868 if ($type ==
'customer' && !empty($buyer->code_compta_product)) {
869 $code_t = $buyer->code_compta_product;
870 $suggestedid = $accountingAccount[
'thirdparty'];
871 $suggestedaccountingaccountfor =
'thirdparty';
872 } elseif ($type ==
'supplier' && !empty($seller->code_compta_product)) {
873 $code_t = $seller->code_compta_product;
874 $suggestedid = $accountingAccount[
'thirdparty'];
875 $suggestedaccountingaccountfor =
'thirdparty';
881 if ($factureDet->desc ==
"(DEPOSIT)" || $facture->type == $facture::TYPE_DEPOSIT) {
882 $accountdeposittoventilated =
new self($this->db);
883 if ($type ==
'customer') {
884 $result = $accountdeposittoventilated->fetch(
'', $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT, 1);
885 } elseif ($type ==
'supplier') {
886 $result = $accountdeposittoventilated->fetch(
'', $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT, 1);
888 if (isset($result) && $result < 0) {
892 $code_l = $accountdeposittoventilated->ref;
895 $suggestedid = $accountdeposittoventilated->rowid;
896 $suggestedaccountingaccountfor =
'deposit';
900 if (!empty($facture->fk_facture_source)) {
901 $invoiceSource =
new $facture($this->db);
902 $invoiceSource->fetch($facture->fk_facture_source);
904 if ($facture->type == $facture::TYPE_CREDIT_NOTE && $invoiceSource->type == $facture::TYPE_DEPOSIT) {
905 $accountdeposittoventilated =
new self($this->db);
906 if ($type ==
'customer') {
907 $accountdeposittoventilated->fetch(
'', $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT, 1);
908 } elseif ($type ==
'supplier') {
909 $accountdeposittoventilated->fetch(
'', $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT, 1);
911 $code_l = $accountdeposittoventilated->ref;
914 $suggestedid = $accountdeposittoventilated->rowid;
915 $suggestedaccountingaccountfor =
'deposit';
921 if (empty($suggestedid) && empty($code_p) && !empty($code_l) && empty($conf->global->ACCOUNTANCY_DO_NOT_AUTOFILL_ACCOUNT_WITH_GENERIC)) {
922 if (empty($this->accountingaccount_codetotid_cache[$code_l])) {
923 $tmpaccount =
new self($this->db);
924 $result = $tmpaccount->fetch(0, $code_l, 1);
928 if ($tmpaccount->id > 0) {
929 $suggestedid = $tmpaccount->id;
931 $this->accountingaccount_codetotid_cache[$code_l] = $tmpaccount->id;
933 $suggestedid = $this->accountingaccount_codetotid_cache[$code_l];
937 'suggestedaccountingaccountbydefaultfor' => $suggestedaccountingaccountbydefaultfor,
938 'suggestedaccountingaccountfor' => $suggestedaccountingaccountfor,
939 'suggestedid' => $suggestedid,
945 if (is_array($hookmanager->resArray) && !empty($hookmanager->resArray)) {
946 return $hookmanager->resArray;