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();
481 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle =
'', $notooltip = 0, $save_lastsearch_value = -1, $withcompletelabel = 0, $option =
'')
483 global $langs,
$conf, $hookmanager;
484 require_once DOL_DOCUMENT_ROOT .
'/core/lib/accounting.lib.php';
486 if (!empty(
$conf->dol_no_mouse_hover)) {
494 if (empty($option) || $option ==
'ledger') {
495 $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 :
''));
496 $labelurl = $langs->trans(
"ShowAccountingAccountInLedger");
497 } elseif ($option ==
'journals') {
498 $url = DOL_URL_ROOT .
'/accountancy/bookkeeping/list.php?search_accountancy_code_start=' . urlencode($this->account_number) .
'&search_accountancy_code_end=' . urlencode($this->account_number);
499 $labelurl = $langs->trans(
"ShowAccountingAccountInJournals");
500 } elseif ($option ==
'accountcard') {
501 $url = DOL_URL_ROOT .
'/accountancy/admin/card.php?id=' . urlencode((
string) ($this->
id));
502 $labelurl = $langs->trans(
"ShowAccountingAccount");
506 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
507 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
508 $add_save_lastsearch_values = 1;
510 if ($add_save_lastsearch_values) {
511 $url .=
'&save_lastsearch_values=1';
514 $picto =
'accounting_account';
517 if (empty($this->labelshort) || $withcompletelabel == 1) {
518 $labeltoshow = $this->label;
520 $labeltoshow = $this->labelshort;
523 $label =
'<u>' . $labelurl .
'</u>';
524 if (!empty($this->account_number)) {
525 $label .=
'<br><b>' . $langs->trans(
'AccountAccounting') .
':</b> ' .
length_accountg($this->account_number);
527 if (!empty($labeltoshow)) {
528 $label .=
'<br><b>' . $langs->trans(
'Label') .
':</b> ' . $labeltoshow;
531 $label .=
' - ' . $moretitle;
535 if (empty($notooltip)) {
541 $linkclose .=
' class="classfortooltip"';
544 $linkstart =
'<a href="' . $url .
'"';
545 $linkstart .= $linkclose .
'>';
556 $label_link .=
' - ' . ($nourl ?
'<span class="opacitymedium">' :
'') . $labeltoshow . ($nourl ?
'</span>' :
'');
560 $result .= ($linkstart .
img_object(($notooltip ?
'' : $label), $picto, ($notooltip ?
'' :
'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1) . $linkend);
562 if ($withpicto && $withpicto != 2) {
565 if ($withpicto != 2) {
566 $result .= $linkstart . $label_link . $linkend;
569 $hookmanager->initHooks(array($this->element .
'dao'));
570 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
571 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
573 $result = $hookmanager->resPrint;
575 $result .= $hookmanager->resPrint;
742 $hookmanager->initHooks(array(
'accountancyBindingCalculation'));
745 $parameters = array(
'buyer' => $buyer,
'seller' => $seller,
'product' => $product,
'facture' => $facture,
'factureDet' => $factureDet ,
'accountingAccount' => $accountingAccount, 0 => $type);
746 $reshook = $hookmanager->executeHooks(
'accountancyBindingCalculation', $parameters);
749 if (empty($reshook)) {
751 if ($type ==
'customer') {
752 $const_name =
"SOLD";
753 } elseif ($type ==
'supplier') {
757 require_once DOL_DOCUMENT_ROOT .
'/core/lib/company.lib.php';
758 $isBuyerInEEC =
isInEEC($buyer);
759 $isSellerInEEC =
isInEEC($seller);
766 $suggestedaccountingaccountbydefaultfor =
'';
767 if ($factureDet->product_type == 1) {
768 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) {
770 $suggestedaccountingaccountbydefaultfor =
'';
772 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
774 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
776 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
777 } elseif ($isSellerInEEC && $isBuyerInEEC) {
779 $suggestedaccountingaccountbydefaultfor =
'eec';
781 $code_l =
getDolGlobalString(
'ACCOUNTING_SERVICE_' . $const_name .
'_EXPORT_ACCOUNT');
782 $suggestedaccountingaccountbydefaultfor =
'export';
785 } elseif ($factureDet->product_type == 0) {
786 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) {
788 $suggestedaccountingaccountbydefaultfor =
'';
790 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
792 $suggestedaccountingaccountbydefaultfor =
'eecwithvat';
793 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
795 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
796 } elseif ($isSellerInEEC && $isBuyerInEEC) {
798 $suggestedaccountingaccountbydefaultfor =
'eec';
800 $code_l =
getDolGlobalString(
'ACCOUNTING_PRODUCT_' . $const_name .
'_EXPORT_ACCOUNT');
801 $suggestedaccountingaccountbydefaultfor =
'export';
810 $suggestedaccountingaccountfor =
'';
811 if ((($buyer->country_code == $seller->country_code) || empty($buyer->country_code))) {
813 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
814 $code_p = $product->accountancy_code_sell;
815 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
816 $code_p = $product->accountancy_code_buy;
818 $suggestedid = $accountingAccount[
'dom'];
819 $suggestedaccountingaccountfor =
'prodserv';
821 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
823 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
824 $code_p = $product->accountancy_code_sell;
825 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
826 $code_p = $product->accountancy_code_buy;
828 $suggestedid = $accountingAccount[
'dom'];
829 $suggestedaccountingaccountfor =
'eecwithvat';
830 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
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 =
'eecwithoutvatnumber';
839 } elseif ($isSellerInEEC && $isBuyerInEEC && (($type ==
'customer' && !empty($product->accountancy_code_sell_intra)) || ($type ==
'supplier' && !empty($product->accountancy_code_buy_intra)))) {
841 if ($type ==
'customer' && !empty($product->accountancy_code_sell_intra)) {
842 $code_p = $product->accountancy_code_sell_intra;
843 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_intra)) {
844 $code_p = $product->accountancy_code_buy_intra;
846 $suggestedid = $accountingAccount[
'intra'];
847 $suggestedaccountingaccountfor =
'eec';
850 if ($type ==
'customer' && !empty($product->accountancy_code_sell_export)) {
851 $code_p = $product->accountancy_code_sell_export;
852 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_export)) {
853 $code_p = $product->accountancy_code_buy_export;
855 $suggestedid = $accountingAccount[
'export'];
856 $suggestedaccountingaccountfor =
'export';
862 if ($type ==
'customer' && !empty($buyer->code_compta_product)) {
863 $code_t = $buyer->code_compta_product;
864 $suggestedid = $accountingAccount[
'thirdparty'];
865 $suggestedaccountingaccountfor =
'thirdparty';
866 } elseif ($type ==
'supplier' && !empty($seller->code_compta_product)) {
867 $code_t = $seller->code_compta_product;
868 $suggestedid = $accountingAccount[
'thirdparty'];
869 $suggestedaccountingaccountfor =
'thirdparty';
875 if ($factureDet->desc ==
"(DEPOSIT)" || $facture->type == $facture::TYPE_DEPOSIT) {
876 $accountdeposittoventilated =
new self($this->db);
877 if ($type ==
'customer') {
878 $result = $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
879 } elseif ($type ==
'supplier') {
880 $result = $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
882 if (isset($result) && $result < 0) {
886 $code_l = $accountdeposittoventilated->ref;
889 $suggestedid = $accountdeposittoventilated->rowid;
890 $suggestedaccountingaccountfor =
'deposit';
894 if (!empty($facture->fk_facture_source)) {
895 $invoiceSource =
new $facture($this->db);
896 $invoiceSource->fetch($facture->fk_facture_source);
898 if ($facture->type == $facture::TYPE_CREDIT_NOTE && $invoiceSource->type == $facture::TYPE_DEPOSIT) {
899 $accountdeposittoventilated =
new self($this->db);
900 if ($type ==
'customer') {
901 $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
902 } elseif ($type ==
'supplier') {
903 $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
905 $code_l = $accountdeposittoventilated->ref;
908 $suggestedid = $accountdeposittoventilated->rowid;
909 $suggestedaccountingaccountfor =
'deposit';
915 if (empty($suggestedid) && empty($code_p) && !empty($code_l) && !
getDolGlobalString(
'ACCOUNTANCY_DO_NOT_AUTOFILL_ACCOUNT_WITH_GENERIC')) {
916 if (empty($this->accountingaccount_codetotid_cache[$code_l])) {
917 $tmpaccount =
new self($this->db);
918 $result = $tmpaccount->fetch(0, $code_l, 1);
922 if ($tmpaccount->id > 0) {
923 $suggestedid = $tmpaccount->id;
925 $this->accountingaccount_codetotid_cache[$code_l] = $tmpaccount->id;
927 $suggestedid = $this->accountingaccount_codetotid_cache[$code_l];
931 'suggestedaccountingaccountbydefaultfor' => $suggestedaccountingaccountbydefaultfor,
932 'suggestedaccountingaccountfor' => $suggestedaccountingaccountfor,
933 'suggestedid' => $suggestedid,
939 if (is_array($hookmanager->resArray) && !empty($hookmanager->resArray)) {
940 return $hookmanager->resArray;