179 public function fetch($rowid = 0, $account_number =
null, $limittocurrentchart = 0, $limittoachartaccount =
'')
183 if ($rowid || $account_number) {
184 $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";
185 $sql .=
", ca.label as category_label";
186 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as a";
187 $sql .=
" LEFT JOIN ".$this->db->prefix().
"c_accounting_category as ca ON a.fk_accounting_category = ca.rowid";
190 $sql .=
" a.rowid = ".(int) $rowid;
191 } elseif ($account_number) {
192 $sql .=
" a.account_number = '".$this->db->escape($account_number).
"'";
193 $sql .=
" AND a.entity = ".$conf->entity;
195 if (!empty($limittocurrentchart)) {
196 $sql .=
' AND a.fk_pcg_version IN (SELECT pcg_version FROM '.$this->db->prefix().
'accounting_system WHERE rowid = '.((int)
getDolGlobalInt(
'CHARTOFACCOUNTS')).
')';
198 if (!empty($limittoachartaccount)) {
199 $sql .=
" AND a.fk_pcg_version = '".$this->db->escape($limittoachartaccount).
"'";
202 dol_syslog(get_class($this).
"::fetch rowid=".$rowid.
" account_number=".$account_number, LOG_DEBUG);
204 $result = $this->db->query($sql);
206 $obj = $this->db->fetch_object($result);
209 $this->
id = $obj->rowid;
210 $this->rowid = $obj->rowid;
211 $this->
ref = $obj->account_number;
212 $this->datec = $this->db->jdate($obj->datec);
213 $this->date_creation = $this->db->jdate($obj->datec);
214 $this->date_modification = $this->db->jdate($obj->tms);
216 $this->fk_pcg_version = $obj->fk_pcg_version;
217 $this->pcg_type = $obj->pcg_type;
218 $this->account_number = $obj->account_number;
219 $this->account_parent = $obj->account_parent;
220 $this->label = $obj->label;
221 $this->labelshort = $obj->labelshort;
222 $this->account_category = $obj->fk_accounting_category;
223 $this->account_category_label = $obj->category_label;
224 $this->fk_user_author = $obj->fk_user_author;
225 $this->fk_user_modif = $obj->fk_user_modif;
226 $this->
active = $obj->active;
227 $this->
status = $obj->active;
228 $this->reconcilable = $obj->reconcilable;
235 $this->error =
"Error ".$this->db->lasterror();
236 $this->errors[] =
"Error ".$this->db->lasterror();
249 public function create($user, $notrigger = 0)
256 if (isset($this->fk_pcg_version)) {
257 $this->fk_pcg_version = trim($this->fk_pcg_version);
259 if (isset($this->pcg_type)) {
260 $this->pcg_type = trim($this->pcg_type);
262 if (isset($this->account_number)) {
263 $this->account_number = trim($this->account_number);
265 if (isset($this->label)) {
266 $this->label = trim($this->label);
268 if (isset($this->labelshort)) {
269 $this->labelshort = trim($this->labelshort);
272 if (empty($this->pcg_type) || $this->pcg_type ==
'-1') {
273 $this->pcg_type =
'XXXXXX';
279 $sql =
"INSERT INTO " . $this->db->prefix() . $this->table_element .
" (";
282 $sql .=
", fk_pcg_version";
283 $sql .=
", pcg_type";
284 $sql .=
", account_number";
285 $sql .=
", account_parent";
287 $sql .=
", labelshort";
288 $sql .=
", fk_accounting_category";
289 $sql .=
", fk_user_author";
291 $sql .=
", reconcilable";
292 $sql .=
") VALUES (";
293 $sql .=
" '".$this->db->idate($now).
"'";
294 $sql .=
", ".((int) $conf->entity);
295 $sql .=
", ".(empty($this->fk_pcg_version) ?
'NULL' :
"'".$this->db->escape($this->fk_pcg_version).
"'");
296 $sql .=
", ".(empty($this->pcg_type) ?
'NULL' :
"'".$this->db->escape($this->pcg_type).
"'");
297 $sql .=
", ".(empty($this->account_number) ?
'NULL' :
"'".$this->db->escape($this->account_number).
"'");
298 $sql .=
", ".(empty($this->account_parent) ? 0 : (int) $this->account_parent);
299 $sql .=
", ".(empty($this->label) ?
"''" :
"'".$this->db->escape($this->label).
"'");
300 $sql .=
", ".(empty($this->labelshort) ?
"''" :
"'".$this->db->escape($this->labelshort).
"'");
301 $sql .=
", ".(empty($this->account_category) ? 0 : (int) $this->account_category);
302 $sql .=
", ".((int) $user->id);
303 $sql .=
", ".(int) $this->
active;
304 $sql .=
", ".(int) $this->reconcilable;
309 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
310 $resql = $this->db->query($sql);
313 $this->errors[] =
"Error " . $this->db->lasterror();
317 $this->
id = $this->db->last_insert_id($this->db->prefix() . $this->table_element);
332 foreach ($this->errors as $errmsg) {
333 dol_syslog(get_class($this) .
"::create " . $errmsg, LOG_ERR);
334 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
336 $this->db->rollback();
479 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle =
'', $notooltip = 0, $save_lastsearch_value = -1, $withcompletelabel = 0, $option =
'')
481 global $langs, $conf, $hookmanager;
482 require_once DOL_DOCUMENT_ROOT .
'/core/lib/accounting.lib.php';
484 if (!empty($conf->dol_no_mouse_hover)) {
492 if (empty($option) || $option ==
'ledger') {
493 $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 :
''));
494 $labelurl = $langs->trans(
"ShowAccountingAccountInLedger");
495 } elseif ($option ==
'journals') {
496 $url = DOL_URL_ROOT .
'/accountancy/bookkeeping/list.php?search_accountancy_code_start=' . urlencode($this->account_number) .
'&search_accountancy_code_end=' . urlencode($this->account_number);
497 $labelurl = $langs->trans(
"ShowAccountingAccountInJournals");
498 } elseif ($option ==
'accountcard') {
499 $url = DOL_URL_ROOT .
'/accountancy/admin/card.php?id=' . urlencode((
string) ($this->
id));
500 $labelurl = $langs->trans(
"ShowAccountingAccount");
504 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
505 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
506 $add_save_lastsearch_values = 1;
508 if ($add_save_lastsearch_values) {
509 $url .=
'&save_lastsearch_values=1';
512 $picto =
'accounting_account';
515 if (empty($this->labelshort) || $withcompletelabel == 1) {
516 $labeltoshow = $this->label;
518 $labeltoshow = $this->labelshort;
521 $label =
'<u>' . $labelurl .
'</u>';
522 if (!empty($this->account_number)) {
523 $label .=
'<br><b>' . $langs->trans(
'AccountAccounting') .
':</b> ' .
length_accountg($this->account_number);
525 if (!empty($labeltoshow)) {
526 $label .=
'<br><b>' . $langs->trans(
'Label') .
':</b> ' . $labeltoshow;
529 $label .=
' - ' . $moretitle;
533 if (empty($notooltip)) {
539 $linkclose .=
' class="classfortooltip"';
542 $linkstart =
'<a href="' . $url .
'"';
543 $linkstart .= $linkclose .
'>';
554 $label_link .=
' - ' . ($nourl ?
'<span class="opacitymedium">' :
'') . $labeltoshow . ($nourl ?
'</span>' :
'');
558 $result .= ($linkstart .
img_object(($notooltip ?
'' : $label), $picto, ($notooltip ?
'' :
'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1) . $linkend);
560 if ($withpicto && $withpicto != 2) {
563 if ($withpicto != 2) {
564 $result .= $linkstart . $label_link . $linkend;
567 $hookmanager->initHooks(array($this->element .
'dao'));
568 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
569 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
571 $result = $hookmanager->resPrint;
573 $result .= $hookmanager->resPrint;
746 $hookmanager->initHooks(array(
'accountancyBindingCalculation'));
749 $parameters = array(
'buyer' => $buyer,
'seller' => $seller,
'product' => $product,
'facture' => $facture,
'factureDet' => $factureDet ,
'accountingAccount' => $accountingAccount, 0 => $type);
750 $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 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
780 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
781 } elseif ($isSellerInEEC && $isBuyerInEEC) {
783 $suggestedaccountingaccountbydefaultfor =
'eec';
785 $code_l =
getDolGlobalString(
'ACCOUNTING_SERVICE_' . $const_name .
'_EXPORT_ACCOUNT');
786 $suggestedaccountingaccountbydefaultfor =
'export';
789 } elseif ($factureDet->product_type == 0) {
790 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) {
792 $suggestedaccountingaccountbydefaultfor =
'';
794 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
796 $suggestedaccountingaccountbydefaultfor =
'eecwithvat';
797 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
799 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
800 } elseif ($isSellerInEEC && $isBuyerInEEC) {
802 $suggestedaccountingaccountbydefaultfor =
'eec';
804 $code_l =
getDolGlobalString(
'ACCOUNTING_PRODUCT_' . $const_name .
'_EXPORT_ACCOUNT');
805 $suggestedaccountingaccountbydefaultfor =
'export';
814 $suggestedaccountingaccountfor =
'';
815 if ((($buyer->country_code == $seller->country_code) || empty($buyer->country_code))) {
817 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
818 $code_p = $product->accountancy_code_sell;
819 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
820 $code_p = $product->accountancy_code_buy;
822 $suggestedid = $accountingAccount[
'dom'];
823 $suggestedaccountingaccountfor =
'prodserv';
825 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
827 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
828 $code_p = $product->accountancy_code_sell;
829 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
830 $code_p = $product->accountancy_code_buy;
832 $suggestedid = $accountingAccount[
'dom'];
833 $suggestedaccountingaccountfor =
'eecwithvat';
834 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
836 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
837 $code_p = $product->accountancy_code_sell;
838 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
839 $code_p = $product->accountancy_code_buy;
841 $suggestedid = $accountingAccount[
'dom'];
842 $suggestedaccountingaccountfor =
'eecwithoutvatnumber';
843 } elseif ($isSellerInEEC && $isBuyerInEEC && (($type ==
'customer' && !empty($product->accountancy_code_sell_intra)) || ($type ==
'supplier' && !empty($product->accountancy_code_buy_intra)))) {
845 if ($type ==
'customer' && !empty($product->accountancy_code_sell_intra)) {
846 $code_p = $product->accountancy_code_sell_intra;
847 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_intra)) {
848 $code_p = $product->accountancy_code_buy_intra;
850 $suggestedid = $accountingAccount[
'intra'];
851 $suggestedaccountingaccountfor =
'eec';
854 if ($type ==
'customer' && !empty($product->accountancy_code_sell_export)) {
855 $code_p = $product->accountancy_code_sell_export;
856 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_export)) {
857 $code_p = $product->accountancy_code_buy_export;
859 $suggestedid = $accountingAccount[
'export'];
860 $suggestedaccountingaccountfor =
'export';
866 if ($type ==
'customer' && !empty($buyer->code_compta_product)) {
867 $code_t = $buyer->code_compta_product;
868 $suggestedid = $accountingAccount[
'thirdparty'];
869 $suggestedaccountingaccountfor =
'thirdparty';
870 } elseif ($type ==
'supplier' && !empty($seller->code_compta_product)) {
871 $code_t = $seller->code_compta_product;
872 $suggestedid = $accountingAccount[
'thirdparty'];
873 $suggestedaccountingaccountfor =
'thirdparty';
878 $account_deposit =
getDolGlobalString(
'ACCOUNTING_ACCOUNT_' . strtoupper($type) .
'_DEPOSIT');
879 if (!empty($account_deposit) && $account_deposit !=
'-1') {
880 if ($factureDet->desc ==
"(DEPOSIT)" || $facture->type == $facture::TYPE_DEPOSIT) {
881 $accountdeposittoventilated =
new self($this->db);
882 if ($type ==
'customer') {
883 $result = $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
884 } elseif ($type ==
'supplier') {
885 $result = $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
887 if (isset($result) && $result < 0) {
891 $code_l = (string) $accountdeposittoventilated->ref;
894 $suggestedid = (int) $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(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
907 } elseif ($type ==
'supplier') {
908 $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
910 $code_l = (string) $accountdeposittoventilated->ref;
913 $suggestedid = (int) $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;