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 ".MAIN_DB_PREFIX.
"accounting_account as a";
187 $sql .=
" LEFT JOIN ".MAIN_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 '.MAIN_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 " . MAIN_DB_PREFIX .
"accounting_account(";
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(MAIN_DB_PREFIX .
"accounting_account");
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);
748 if (empty($reshook)) {
750 if ($type ==
'customer') {
751 $const_name =
"SOLD";
752 } elseif ($type ==
'supplier') {
756 require_once DOL_DOCUMENT_ROOT .
'/core/lib/company.lib.php';
757 $isBuyerInEEC =
isInEEC($buyer);
758 $isSellerInEEC =
isInEEC($seller);
765 $suggestedaccountingaccountbydefaultfor =
'';
766 if ($factureDet->product_type == 1) {
767 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) {
769 $suggestedaccountingaccountbydefaultfor =
'';
771 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
773 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
775 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
776 } elseif ($isSellerInEEC && $isBuyerInEEC) {
778 $suggestedaccountingaccountbydefaultfor =
'eec';
780 $code_l =
getDolGlobalString(
'ACCOUNTING_SERVICE_' . $const_name .
'_EXPORT_ACCOUNT');
781 $suggestedaccountingaccountbydefaultfor =
'export';
784 } elseif ($factureDet->product_type == 0) {
785 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) {
787 $suggestedaccountingaccountbydefaultfor =
'';
789 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
791 $suggestedaccountingaccountbydefaultfor =
'eecwithvat';
792 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
794 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
795 } elseif ($isSellerInEEC && $isBuyerInEEC) {
797 $suggestedaccountingaccountbydefaultfor =
'eec';
799 $code_l =
getDolGlobalString(
'ACCOUNTING_PRODUCT_' . $const_name .
'_EXPORT_ACCOUNT');
800 $suggestedaccountingaccountbydefaultfor =
'export';
809 $suggestedaccountingaccountfor =
'';
810 if ((($buyer->country_code == $seller->country_code) || empty($buyer->country_code))) {
812 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
813 $code_p = $product->accountancy_code_sell;
814 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
815 $code_p = $product->accountancy_code_buy;
817 $suggestedid = $accountingAccount[
'dom'];
818 $suggestedaccountingaccountfor =
'prodserv';
820 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
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 =
'eecwithvat';
829 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
831 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
832 $code_p = $product->accountancy_code_sell;
833 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
834 $code_p = $product->accountancy_code_buy;
836 $suggestedid = $accountingAccount[
'dom'];
837 $suggestedaccountingaccountfor =
'eecwithoutvatnumber';
838 } elseif ($isSellerInEEC && $isBuyerInEEC && (($type ==
'customer' && !empty($product->accountancy_code_sell_intra)) || ($type ==
'supplier' && !empty($product->accountancy_code_buy_intra)))) {
840 if ($type ==
'customer' && !empty($product->accountancy_code_sell_intra)) {
841 $code_p = $product->accountancy_code_sell_intra;
842 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_intra)) {
843 $code_p = $product->accountancy_code_buy_intra;
845 $suggestedid = $accountingAccount[
'intra'];
846 $suggestedaccountingaccountfor =
'eec';
849 if ($type ==
'customer' && !empty($product->accountancy_code_sell_export)) {
850 $code_p = $product->accountancy_code_sell_export;
851 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_export)) {
852 $code_p = $product->accountancy_code_buy_export;
854 $suggestedid = $accountingAccount[
'export'];
855 $suggestedaccountingaccountfor =
'export';
861 if ($type ==
'customer' && !empty($buyer->code_compta_product)) {
862 $code_t = $buyer->code_compta_product;
863 $suggestedid = $accountingAccount[
'thirdparty'];
864 $suggestedaccountingaccountfor =
'thirdparty';
865 } elseif ($type ==
'supplier' && !empty($seller->code_compta_product)) {
866 $code_t = $seller->code_compta_product;
867 $suggestedid = $accountingAccount[
'thirdparty'];
868 $suggestedaccountingaccountfor =
'thirdparty';
874 if ($factureDet->desc ==
"(DEPOSIT)" || $facture->type == $facture::TYPE_DEPOSIT) {
875 $accountdeposittoventilated =
new self($this->db);
876 if ($type ==
'customer') {
877 $result = $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
878 } elseif ($type ==
'supplier') {
879 $result = $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
881 if (isset($result) && $result < 0) {
885 $code_l = $accountdeposittoventilated->ref;
888 $suggestedid = $accountdeposittoventilated->rowid;
889 $suggestedaccountingaccountfor =
'deposit';
893 if (!empty($facture->fk_facture_source)) {
894 $invoiceSource =
new $facture($this->db);
895 $invoiceSource->fetch($facture->fk_facture_source);
897 if ($facture->type == $facture::TYPE_CREDIT_NOTE && $invoiceSource->type == $facture::TYPE_DEPOSIT) {
898 $accountdeposittoventilated =
new self($this->db);
899 if ($type ==
'customer') {
900 $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
901 } elseif ($type ==
'supplier') {
902 $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
904 $code_l = $accountdeposittoventilated->ref;
907 $suggestedid = $accountdeposittoventilated->rowid;
908 $suggestedaccountingaccountfor =
'deposit';
914 if (empty($suggestedid) && empty($code_p) && !empty($code_l) && !
getDolGlobalString(
'ACCOUNTANCY_DO_NOT_AUTOFILL_ACCOUNT_WITH_GENERIC')) {
915 if (empty($this->accountingaccount_codetotid_cache[$code_l])) {
916 $tmpaccount =
new self($this->db);
917 $result = $tmpaccount->fetch(0, $code_l, 1);
921 if ($tmpaccount->id > 0) {
922 $suggestedid = $tmpaccount->id;
924 $this->accountingaccount_codetotid_cache[$code_l] = $tmpaccount->id;
926 $suggestedid = $this->accountingaccount_codetotid_cache[$code_l];
930 'suggestedaccountingaccountbydefaultfor' => $suggestedaccountingaccountbydefaultfor,
931 'suggestedaccountingaccountfor' => $suggestedaccountingaccountfor,
932 'suggestedid' => $suggestedid,
938 if (is_array($hookmanager->resArray) && !empty($hookmanager->resArray)) {
939 return $hookmanager->resArray;