184 public function fetch($rowid = 0, $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 ".$this->db->prefix().$this->table_element.
" as a";
192 $sql .=
" LEFT JOIN ".$this->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 '.$this->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 " . $this->db->prefix() . $this->table_element .
" (";
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($this->db->prefix() . $this->table_element);
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();
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((
string) ($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;
751 $hookmanager->initHooks(array(
'accountancyBindingCalculation'));
754 $parameters = array(
'buyer' => $buyer,
'seller' => $seller,
'product' => $product,
'facture' => $facture,
'factureDet' => $factureDet ,
'accountingAccount' => $accountingAccount, 0 => $type);
755 $reshook = $hookmanager->executeHooks(
'accountancyBindingCalculation', $parameters);
758 if (empty($reshook)) {
760 if ($type ==
'customer') {
761 $const_name =
"SOLD";
762 } elseif ($type ==
'supplier') {
766 require_once DOL_DOCUMENT_ROOT .
'/core/lib/company.lib.php';
767 $isBuyerInEEC =
isInEEC($buyer);
768 $isSellerInEEC =
isInEEC($seller);
775 $suggestedaccountingaccountbydefaultfor =
'';
776 if ($factureDet->product_type == 1) {
777 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) {
779 $suggestedaccountingaccountbydefaultfor =
'';
781 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
783 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
785 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
786 } elseif ($isSellerInEEC && $isBuyerInEEC) {
788 $suggestedaccountingaccountbydefaultfor =
'eec';
790 $code_l =
getDolGlobalString(
'ACCOUNTING_SERVICE_' . $const_name .
'_EXPORT_ACCOUNT');
791 $suggestedaccountingaccountbydefaultfor =
'export';
794 } elseif ($factureDet->product_type == 0) {
795 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) {
797 $suggestedaccountingaccountbydefaultfor =
'';
799 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
801 $suggestedaccountingaccountbydefaultfor =
'eecwithvat';
802 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
804 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
805 } elseif ($isSellerInEEC && $isBuyerInEEC) {
807 $suggestedaccountingaccountbydefaultfor =
'eec';
809 $code_l =
getDolGlobalString(
'ACCOUNTING_PRODUCT_' . $const_name .
'_EXPORT_ACCOUNT');
810 $suggestedaccountingaccountbydefaultfor =
'export';
819 $suggestedaccountingaccountfor =
'';
820 if ((($buyer->country_code == $seller->country_code) || empty($buyer->country_code))) {
822 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
823 $code_p = $product->accountancy_code_sell;
824 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
825 $code_p = $product->accountancy_code_buy;
827 $suggestedid = $accountingAccount[
'dom'];
828 $suggestedaccountingaccountfor =
'prodserv';
830 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
832 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
833 $code_p = $product->accountancy_code_sell;
834 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
835 $code_p = $product->accountancy_code_buy;
837 $suggestedid = $accountingAccount[
'dom'];
838 $suggestedaccountingaccountfor =
'eecwithvat';
839 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
841 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
842 $code_p = $product->accountancy_code_sell;
843 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
844 $code_p = $product->accountancy_code_buy;
846 $suggestedid = $accountingAccount[
'dom'];
847 $suggestedaccountingaccountfor =
'eecwithoutvatnumber';
848 } elseif ($isSellerInEEC && $isBuyerInEEC && (($type ==
'customer' && !empty($product->accountancy_code_sell_intra)) || ($type ==
'supplier' && !empty($product->accountancy_code_buy_intra)))) {
850 if ($type ==
'customer' && !empty($product->accountancy_code_sell_intra)) {
851 $code_p = $product->accountancy_code_sell_intra;
852 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_intra)) {
853 $code_p = $product->accountancy_code_buy_intra;
855 $suggestedid = $accountingAccount[
'intra'];
856 $suggestedaccountingaccountfor =
'eec';
859 if ($type ==
'customer' && !empty($product->accountancy_code_sell_export)) {
860 $code_p = $product->accountancy_code_sell_export;
861 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_export)) {
862 $code_p = $product->accountancy_code_buy_export;
864 $suggestedid = $accountingAccount[
'export'];
865 $suggestedaccountingaccountfor =
'export';
871 if ($type ==
'customer' && !empty($buyer->code_compta_product)) {
872 $code_t = $buyer->code_compta_product;
873 $suggestedid = $accountingAccount[
'thirdparty'];
874 $suggestedaccountingaccountfor =
'thirdparty';
875 } elseif ($type ==
'supplier' && !empty($seller->code_compta_product)) {
876 $code_t = $seller->code_compta_product;
877 $suggestedid = $accountingAccount[
'thirdparty'];
878 $suggestedaccountingaccountfor =
'thirdparty';
883 $account_deposit =
getDolGlobalString(
'ACCOUNTING_ACCOUNT_' . strtoupper($type) .
'_DEPOSIT');
884 if (!empty($account_deposit) && $account_deposit !=
'-1') {
885 if ($factureDet->desc ==
"(DEPOSIT)" || $facture->type == $facture::TYPE_DEPOSIT) {
886 $accountdeposittoventilated =
new self($this->db);
887 if ($type ==
'customer') {
888 $result = $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
889 } elseif ($type ==
'supplier') {
890 $result = $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
892 if (isset($result) && $result < 0) {
896 $code_l = (
string) $accountdeposittoventilated->ref;
899 $suggestedid = (int) $accountdeposittoventilated->rowid;
900 $suggestedaccountingaccountfor =
'deposit';
904 if (!empty($facture->fk_facture_source)) {
905 $invoiceSource =
new $facture($this->db);
906 $invoiceSource->fetch($facture->fk_facture_source);
908 if ($facture->type == $facture::TYPE_CREDIT_NOTE && $invoiceSource->type == $facture::TYPE_DEPOSIT) {
909 $accountdeposittoventilated =
new self($this->db);
910 if ($type ==
'customer') {
911 $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
912 } elseif ($type ==
'supplier') {
913 $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
915 $code_l = (
string) $accountdeposittoventilated->ref;
918 $suggestedid = (int) $accountdeposittoventilated->rowid;
919 $suggestedaccountingaccountfor =
'deposit';
925 if (empty($suggestedid) && empty($code_p) && !empty($code_l) && !
getDolGlobalString(
'ACCOUNTANCY_DO_NOT_AUTOFILL_ACCOUNT_WITH_GENERIC')) {
926 if (empty($this->accountingaccount_codetotid_cache[$code_l])) {
927 $tmpaccount =
new self($this->db);
928 $result = $tmpaccount->fetch(0, $code_l, 1);
932 if ($tmpaccount->id > 0) {
933 $suggestedid = $tmpaccount->id;
935 $this->accountingaccount_codetotid_cache[$code_l] = $tmpaccount->id;
937 $suggestedid = $this->accountingaccount_codetotid_cache[$code_l];
941 'suggestedaccountingaccountbydefaultfor' => $suggestedaccountingaccountbydefaultfor,
942 'suggestedaccountingaccountfor' => $suggestedaccountingaccountfor,
943 'suggestedid' => $suggestedid,
949 if (is_array($hookmanager->resArray) && !empty($hookmanager->resArray)) {
950 return $hookmanager->resArray;