182 public function fetch($rowid =
null, $account_number =
null, $limittocurrentchart = 0, $limittoachartaccount =
'')
186 if ($rowid || $account_number) {
187 $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";
188 $sql .=
", ca.label as category_label";
189 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_account as a";
190 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_accounting_category as ca ON a.fk_accounting_category = ca.rowid";
193 $sql .=
" a.rowid = ".(int) $rowid;
194 } elseif ($account_number) {
195 $sql .=
" a.account_number = '".$this->db->escape($account_number).
"'";
196 $sql .=
" AND a.entity = ".$conf->entity;
198 if (!empty($limittocurrentchart)) {
199 $sql .=
' AND a.fk_pcg_version IN (SELECT pcg_version FROM '.MAIN_DB_PREFIX.
'accounting_system WHERE rowid = '.((int)
getDolGlobalInt(
'CHARTOFACCOUNTS')).
')';
201 if (!empty($limittoachartaccount)) {
202 $sql .=
" AND a.fk_pcg_version = '".$this->db->escape($limittoachartaccount).
"'";
205 dol_syslog(get_class($this).
"::fetch rowid=".$rowid.
" account_number=".$account_number, LOG_DEBUG);
207 $result = $this->db->query($sql);
209 $obj = $this->db->fetch_object($result);
212 $this->
id = $obj->rowid;
213 $this->
rowid = $obj->rowid;
214 $this->
ref = $obj->account_number;
215 $this->datec = $this->db->jdate($obj->datec);
216 $this->date_creation = $this->db->jdate($obj->datec);
217 $this->date_modification = $this->db->jdate($obj->tms);
219 $this->fk_pcg_version = $obj->fk_pcg_version;
220 $this->pcg_type = $obj->pcg_type;
221 $this->account_number = $obj->account_number;
222 $this->account_parent = $obj->account_parent;
223 $this->label = $obj->label;
224 $this->labelshort = $obj->labelshort;
225 $this->account_category = $obj->fk_accounting_category;
226 $this->account_category_label = $obj->category_label;
227 $this->fk_user_author = $obj->fk_user_author;
228 $this->fk_user_modif = $obj->fk_user_modif;
229 $this->active = $obj->active;
230 $this->
status = $obj->active;
231 $this->reconcilable = $obj->reconcilable;
238 $this->error =
"Error ".$this->db->lasterror();
239 $this->errors[] =
"Error ".$this->db->lasterror();
252 public function create($user, $notrigger = 0)
259 if (isset($this->fk_pcg_version)) {
260 $this->fk_pcg_version = trim($this->fk_pcg_version);
262 if (isset($this->pcg_type)) {
263 $this->pcg_type = trim($this->pcg_type);
265 if (isset($this->account_number)) {
266 $this->account_number = trim($this->account_number);
268 if (isset($this->label)) {
269 $this->label = trim($this->label);
271 if (isset($this->labelshort)) {
272 $this->labelshort = trim($this->labelshort);
275 if (empty($this->pcg_type) || $this->pcg_type ==
'-1') {
276 $this->pcg_type =
'XXXXXX';
282 $sql =
"INSERT INTO " . MAIN_DB_PREFIX .
"accounting_account(";
285 $sql .=
", fk_pcg_version";
286 $sql .=
", pcg_type";
287 $sql .=
", account_number";
288 $sql .=
", account_parent";
290 $sql .=
", labelshort";
291 $sql .=
", fk_accounting_category";
292 $sql .=
", fk_user_author";
294 $sql .=
", reconcilable";
295 $sql .=
") VALUES (";
296 $sql .=
" '".$this->db->idate($now).
"'";
297 $sql .=
", ".((int) $conf->entity);
298 $sql .=
", ".(empty($this->fk_pcg_version) ?
'NULL' :
"'".$this->db->escape($this->fk_pcg_version).
"'");
299 $sql .=
", ".(empty($this->pcg_type) ?
'NULL' :
"'".$this->db->escape($this->pcg_type).
"'");
300 $sql .=
", ".(empty($this->account_number) ?
'NULL' :
"'".$this->db->escape($this->account_number).
"'");
301 $sql .=
", ".(empty($this->account_parent) ? 0 : (int) $this->account_parent);
302 $sql .=
", ".(empty($this->label) ?
"''" :
"'".$this->db->escape($this->label).
"'");
303 $sql .=
", ".(empty($this->labelshort) ?
"''" :
"'".$this->db->escape($this->labelshort).
"'");
304 $sql .=
", ".(empty($this->account_category) ? 0 : (int) $this->account_category);
305 $sql .=
", ".((int) $user->id);
306 $sql .=
", ".(int) $this->active;
307 $sql .=
", ".(int) $this->reconcilable;
312 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
313 $resql = $this->db->query($sql);
316 $this->errors[] =
"Error " . $this->db->lasterror();
320 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX .
"accounting_account");
335 foreach ($this->errors as $errmsg) {
336 dol_syslog(get_class($this) .
"::create " . $errmsg, LOG_ERR);
337 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
339 $this->db->rollback();
484 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle =
'', $notooltip = 0, $save_lastsearch_value = -1, $withcompletelabel = 0, $option =
'')
486 global $langs, $conf, $hookmanager;
487 require_once DOL_DOCUMENT_ROOT .
'/core/lib/accounting.lib.php';
489 if (!empty($conf->dol_no_mouse_hover)) {
497 if (empty($option) || $option ==
'ledger') {
498 $url = DOL_URL_ROOT .
'/accountancy/bookkeeping/listbyaccount.php?search_accountancy_code_start=' . urlencode((isset($this->account_number) ? $this->account_number :
'')) .
'&search_accountancy_code_end=' . urlencode((isset($this->account_number) ? $this->account_number :
''));
499 $labelurl = $langs->trans(
"ShowAccountingAccountInLedger");
500 } elseif ($option ==
'journals') {
501 $url = DOL_URL_ROOT .
'/accountancy/bookkeeping/list.php?search_accountancy_code_start=' . urlencode($this->account_number) .
'&search_accountancy_code_end=' . urlencode($this->account_number);
502 $labelurl = $langs->trans(
"ShowAccountingAccountInJournals");
503 } elseif ($option ==
'accountcard') {
504 $url = DOL_URL_ROOT .
'/accountancy/admin/card.php?id=' . urlencode($this->
id);
505 $labelurl = $langs->trans(
"ShowAccountingAccount");
509 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
510 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
511 $add_save_lastsearch_values = 1;
513 if ($add_save_lastsearch_values) {
514 $url .=
'&save_lastsearch_values=1';
517 $picto =
'accounting_account';
520 if (empty($this->labelshort) || $withcompletelabel == 1) {
521 $labeltoshow = $this->label;
523 $labeltoshow = $this->labelshort;
526 $label =
'<u>' . $labelurl .
'</u>';
527 if (!empty($this->account_number)) {
528 $label .=
'<br><b>' . $langs->trans(
'AccountAccounting') .
':</b> ' .
length_accountg($this->account_number);
530 if (!empty($labeltoshow)) {
531 $label .=
'<br><b>' . $langs->trans(
'Label') .
':</b> ' . $labeltoshow;
534 $label .=
' - ' . $moretitle;
538 if (empty($notooltip)) {
544 $linkclose .=
' class="classfortooltip"';
547 $linkstart =
'<a href="' . $url .
'"';
548 $linkstart .= $linkclose .
'>';
559 $label_link .=
' - ' . ($nourl ?
'<span class="opacitymedium">' :
'') . $labeltoshow . ($nourl ?
'</span>' :
'');
563 $result .= ($linkstart .
img_object(($notooltip ?
'' : $label), $picto, ($notooltip ?
'' :
'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1) . $linkend);
565 if ($withpicto && $withpicto != 2) {
568 if ($withpicto != 2) {
569 $result .= $linkstart . $label_link . $linkend;
572 $hookmanager->initHooks(array($this->element .
'dao'));
573 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
574 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
576 $result = $hookmanager->resPrint;
578 $result .= $hookmanager->resPrint;
747 $hookmanager->initHooks(array(
'accountancyBindingCalculation'));
750 $parameters = array(
'buyer' => $buyer,
'seller' => $seller,
'product' => $product,
'facture' => $facture,
'factureDet' => $factureDet ,
'accountingAccount'=>$accountingAccount, $type);
751 $reshook = $hookmanager->executeHooks(
'accountancyBindingCalculation', $parameters);
753 if (empty($reshook)) {
755 if ($type ==
'customer') {
756 $const_name =
"SOLD";
757 } elseif ($type ==
'supplier') {
761 require_once DOL_DOCUMENT_ROOT .
'/core/lib/company.lib.php';
762 $isBuyerInEEC =
isInEEC($buyer);
763 $isSellerInEEC =
isInEEC($seller);
770 $suggestedaccountingaccountbydefaultfor =
'';
771 if ($factureDet->product_type == 1) {
772 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) {
774 $suggestedaccountingaccountbydefaultfor =
'';
776 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
778 $suggestedaccountingaccountbydefaultfor =
'eecwithvat';
779 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
781 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
782 } elseif ($isSellerInEEC && $isBuyerInEEC) {
784 $suggestedaccountingaccountbydefaultfor =
'eec';
786 $code_l =
getDolGlobalString(
'ACCOUNTING_SERVICE_' . $const_name .
'_EXPORT_ACCOUNT');
787 $suggestedaccountingaccountbydefaultfor =
'export';
790 } elseif ($factureDet->product_type == 0) {
791 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) {
793 $suggestedaccountingaccountbydefaultfor =
'';
795 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
797 $suggestedaccountingaccountbydefaultfor =
'eecwithvat';
798 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
800 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
801 } elseif ($isSellerInEEC && $isBuyerInEEC) {
803 $suggestedaccountingaccountbydefaultfor =
'eec';
805 $code_l =
getDolGlobalString(
'ACCOUNTING_PRODUCT_' . $const_name .
'_EXPORT_ACCOUNT');
806 $suggestedaccountingaccountbydefaultfor =
'export';
815 $suggestedaccountingaccountfor =
'';
816 if ((($buyer->country_code == $seller->country_code) || empty($buyer->country_code))) {
818 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
819 $code_p = $product->accountancy_code_sell;
820 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
821 $code_p = $product->accountancy_code_buy;
823 $suggestedid = $accountingAccount[
'dom'];
824 $suggestedaccountingaccountfor =
'prodserv';
826 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
828 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
829 $code_p = $product->accountancy_code_sell;
830 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
831 $code_p = $product->accountancy_code_buy;
833 $suggestedid = $accountingAccount[
'dom'];
834 $suggestedaccountingaccountfor =
'eecwithvat';
835 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
837 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
838 $code_p = $product->accountancy_code_sell;
839 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
840 $code_p = $product->accountancy_code_buy;
842 $suggestedid = $accountingAccount[
'dom'];
843 $suggestedaccountingaccountfor =
'eecwithoutvatnumber';
844 } elseif ($isSellerInEEC && $isBuyerInEEC && (($type ==
'customer' && !empty($product->accountancy_code_sell_intra)) || ($type ==
'supplier' && !empty($product->accountancy_code_buy_intra)))) {
846 if ($type ==
'customer' && !empty($product->accountancy_code_sell_intra)) {
847 $code_p = $product->accountancy_code_sell_intra;
848 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_intra)) {
849 $code_p = $product->accountancy_code_buy_intra;
851 $suggestedid = $accountingAccount[
'intra'];
852 $suggestedaccountingaccountfor =
'eec';
855 if ($type ==
'customer' && !empty($product->accountancy_code_sell_export)) {
856 $code_p = $product->accountancy_code_sell_export;
857 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_export)) {
858 $code_p = $product->accountancy_code_buy_export;
860 $suggestedid = $accountingAccount[
'export'];
861 $suggestedaccountingaccountfor =
'export';
867 if ($type ==
'customer' && !empty($buyer->code_compta_product)) {
868 $code_t = $buyer->code_compta_product;
869 $suggestedid = $accountingAccount[
'thirdparty'];
870 $suggestedaccountingaccountfor =
'thirdparty';
871 } elseif ($type ==
'supplier' && !empty($seller->code_compta_product)) {
872 $code_t = $seller->code_compta_product;
873 $suggestedid = $accountingAccount[
'thirdparty'];
874 $suggestedaccountingaccountfor =
'thirdparty';
880 if ($factureDet->desc ==
"(DEPOSIT)" || $facture->type == $facture::TYPE_DEPOSIT) {
881 $accountdeposittoventilated =
new self($this->db);
882 if ($type ==
'customer') {
883 $result = $accountdeposittoventilated->fetch(
'', $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT, 1);
884 } elseif ($type ==
'supplier') {
885 $result = $accountdeposittoventilated->fetch(
'', $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT, 1);
887 if (isset($result) && $result < 0) {
891 $code_l = $accountdeposittoventilated->ref;
894 $suggestedid = $accountdeposittoventilated->rowid;
895 $suggestedaccountingaccountfor =
'deposit';
899 if (!empty($facture->fk_facture_source)) {
900 $invoiceSource =
new $facture($this->db);
901 $invoiceSource->fetch($facture->fk_facture_source);
903 if ($facture->type == $facture::TYPE_CREDIT_NOTE && $invoiceSource->type == $facture::TYPE_DEPOSIT) {
904 $accountdeposittoventilated =
new self($this->db);
905 if ($type ==
'customer') {
906 $accountdeposittoventilated->fetch(
'', $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT, 1);
907 } elseif ($type ==
'supplier') {
908 $accountdeposittoventilated->fetch(
'', $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT, 1);
910 $code_l = $accountdeposittoventilated->ref;
913 $suggestedid = $accountdeposittoventilated->rowid;
914 $suggestedaccountingaccountfor =
'deposit';
920 if (empty($suggestedid) && empty($code_p) && !empty($code_l) && !
getDolGlobalString(
'ACCOUNTANCY_DO_NOT_AUTOFILL_ACCOUNT_WITH_GENERIC')) {
921 if (empty($this->accountingaccount_codetotid_cache[$code_l])) {
922 $tmpaccount =
new self($this->db);
923 $result = $tmpaccount->fetch(0, $code_l, 1);
927 if ($tmpaccount->id > 0) {
928 $suggestedid = $tmpaccount->id;
930 $this->accountingaccount_codetotid_cache[$code_l] = $tmpaccount->id;
932 $suggestedid = $this->accountingaccount_codetotid_cache[$code_l];
936 'suggestedaccountingaccountbydefaultfor' => $suggestedaccountingaccountbydefaultfor,
937 'suggestedaccountingaccountfor' => $suggestedaccountingaccountfor,
938 'suggestedid' => $suggestedid,
944 if (is_array($hookmanager->resArray) && !empty($hookmanager->resArray)) {
945 return $hookmanager->resArray;