185 public function fetch($rowid = 0, $account_number =
null, $limittocurrentchart = 0, $limittoachartaccount =
'')
189 if ($rowid || $account_number) {
190 $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";
191 $sql .=
", ca.label as category_label";
192 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as a";
193 $sql .=
" LEFT JOIN ".$this->db->prefix().
"c_accounting_category as ca ON a.fk_accounting_category = ca.rowid";
196 $sql .=
" a.rowid = ".(int) $rowid;
197 } elseif ($account_number) {
198 $sql .=
" a.account_number = '".$this->db->escape($account_number).
"'";
199 $sql .=
" AND a.entity = ".((int)
$conf->entity);
201 if (!empty($limittocurrentchart)) {
202 $sql .=
' AND a.fk_pcg_version IN (SELECT pcg_version FROM '.$this->db->prefix().
'accounting_system WHERE rowid = '.((int)
getDolGlobalInt(
'CHARTOFACCOUNTS')).
')';
204 if (!empty($limittoachartaccount)) {
205 $sql .=
" AND a.fk_pcg_version = '".$this->db->escape($limittoachartaccount).
"'";
208 dol_syslog(get_class($this).
"::fetch rowid=".$rowid.
" account_number=".$account_number, LOG_DEBUG);
210 $result = $this->db->query($sql);
212 $obj = $this->db->fetch_object($result);
215 $this->
id = $obj->rowid;
216 $this->rowid = $obj->rowid;
217 $this->
ref = $obj->account_number;
218 $this->datec = $this->db->jdate($obj->datec);
219 $this->date_creation = $this->db->jdate($obj->datec);
220 $this->date_modification = $this->db->jdate($obj->tms);
222 $this->fk_pcg_version = $obj->fk_pcg_version;
223 $this->pcg_type = $obj->pcg_type;
224 $this->account_number = $obj->account_number;
225 $this->account_parent = $obj->account_parent;
226 $this->label = $obj->label;
227 $this->labelshort = $obj->labelshort;
228 $this->account_category = $obj->fk_accounting_category;
229 $this->account_category_label = $obj->category_label;
230 $this->fk_user_author = $obj->fk_user_author;
231 $this->fk_user_modif = $obj->fk_user_modif;
232 $this->
active = $obj->active;
233 $this->
status = $obj->active;
234 $this->reconcilable = $obj->reconcilable;
241 $this->error =
"Error ".$this->db->lasterror();
242 $this->errors[] =
"Error ".$this->db->lasterror();
255 public function create($user, $notrigger = 0)
262 if (isset($this->fk_pcg_version)) {
263 $this->fk_pcg_version = trim($this->fk_pcg_version);
265 if (isset($this->pcg_type)) {
266 $this->pcg_type = trim($this->pcg_type);
268 if (isset($this->account_number)) {
269 $this->account_number = trim($this->account_number);
271 if (isset($this->label)) {
272 $this->label = trim($this->label);
274 if (isset($this->labelshort)) {
275 $this->labelshort = trim($this->labelshort);
278 if (empty($this->pcg_type) || $this->pcg_type ==
'-1') {
279 $this->pcg_type =
'XXXXXX';
285 $sql =
"INSERT INTO " . $this->db->prefix() . $this->table_element .
" (";
288 $sql .=
", fk_pcg_version";
289 $sql .=
", pcg_type";
290 $sql .=
", account_number";
291 $sql .=
", account_parent";
293 $sql .=
", labelshort";
294 $sql .=
", fk_accounting_category";
295 $sql .=
", fk_user_author";
297 $sql .=
", reconcilable";
298 $sql .=
") VALUES (";
299 $sql .=
" '".$this->db->idate($now).
"'";
300 $sql .=
", ".((int)
$conf->entity);
301 $sql .=
", ".(empty($this->fk_pcg_version) ?
'NULL' :
"'".$this->db->escape($this->fk_pcg_version).
"'");
302 $sql .=
", ".(empty($this->pcg_type) ?
'NULL' :
"'".$this->db->escape($this->pcg_type).
"'");
303 $sql .=
", ".(empty($this->account_number) ?
'NULL' :
"'".$this->db->escape($this->account_number).
"'");
304 $sql .=
", ".(empty($this->account_parent) ? 0 : (int) $this->account_parent);
305 $sql .=
", ".(empty($this->label) ?
"''" :
"'".$this->db->escape($this->label).
"'");
306 $sql .=
", ".(empty($this->labelshort) ?
"''" :
"'".$this->db->escape($this->labelshort).
"'");
307 $sql .=
", ".(empty($this->account_category) ? 0 : (int) $this->account_category);
308 $sql .=
", ".((int) $user->id);
309 $sql .=
", ".(int) $this->
active;
310 $sql .=
", ".(int) $this->reconcilable;
315 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
316 $resql = $this->db->query($sql);
319 $this->errors[] =
"Error " . $this->db->lasterror();
323 $this->
id = $this->db->last_insert_id($this->db->prefix() . $this->table_element);
338 foreach ($this->errors as $errmsg) {
339 dol_syslog(get_class($this) .
"::create " . $errmsg, LOG_ERR);
340 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
342 $this->db->rollback();
485 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle =
'', $notooltip = 0, $save_lastsearch_value = -1, $withcompletelabel = 0, $option =
'')
487 global $langs,
$conf, $hookmanager;
488 require_once DOL_DOCUMENT_ROOT .
'/core/lib/accounting.lib.php';
490 if (!empty(
$conf->dol_no_mouse_hover)) {
498 if (empty($option) || $option ==
'ledger') {
499 $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 :
''));
500 $labelurl = $langs->trans(
"ShowAccountingAccountInLedger");
501 } elseif ($option ==
'journals') {
502 $url = DOL_URL_ROOT .
'/accountancy/bookkeeping/list.php?search_accountancy_code_start=' . urlencode($this->account_number) .
'&search_accountancy_code_end=' . urlencode($this->account_number);
503 $labelurl = $langs->trans(
"ShowAccountingAccountInJournals");
504 } elseif ($option ==
'accountcard') {
505 $url = DOL_URL_ROOT .
'/accountancy/admin/card.php?id=' . urlencode((
string) ($this->
id));
506 $labelurl = $langs->trans(
"ShowAccountingAccount");
510 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
511 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
512 $add_save_lastsearch_values = 1;
514 if ($add_save_lastsearch_values) {
515 $url .=
'&save_lastsearch_values=1';
518 $picto =
'accounting_account';
521 if (empty($this->labelshort) || $withcompletelabel == 1) {
522 $labeltoshow = $this->label;
524 $labeltoshow = $this->labelshort;
527 $label =
'<u>' . $labelurl .
'</u>';
528 if (!empty($this->account_number)) {
529 $label .=
'<br><b>' . $langs->trans(
'AccountAccounting') .
':</b> ' .
length_accountg($this->account_number);
531 if (!empty($labeltoshow)) {
532 $label .=
'<br><b>' . $langs->trans(
'Label') .
':</b> ' . $labeltoshow;
535 $label .=
' - ' . $moretitle;
539 if (empty($notooltip)) {
545 $linkclose .=
' class="classfortooltip"';
548 $linkstart =
'<a href="' . $url .
'"';
549 $linkstart .= $linkclose .
'>';
560 $label_link .=
' - ' . ($nourl ?
'<span class="opacitymedium">' :
'') . $labeltoshow . ($nourl ?
'</span>' :
'');
564 $result .= ($linkstart .
img_object(($notooltip ?
'' : $label), $picto, ($notooltip ?
'' :
'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1) . $linkend);
566 if ($withpicto && $withpicto != 2) {
569 if ($withpicto != 2) {
570 $result .= $linkstart . $label_link . $linkend;
573 $hookmanager->initHooks(array($this->element .
'dao'));
574 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
575 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
577 $result = $hookmanager->resPrint;
579 $result .= $hookmanager->resPrint;
755 $hookmanager->initHooks(array(
'accountancyBindingCalculation'));
758 $parameters = array(
'buyer' => $buyer,
'seller' => $seller,
'product' => $product,
'facture' => $facture,
'factureDet' => $factureDet ,
'accountingAccount' => $accountingAccount, 0 => $type);
759 $reshook = $hookmanager->executeHooks(
'accountancyBindingCalculation', $parameters);
762 if (empty($reshook)) {
764 if ($type ==
'customer') {
765 $const_name =
"SOLD";
766 } elseif ($type ==
'supplier') {
770 require_once DOL_DOCUMENT_ROOT .
'/core/lib/company.lib.php';
771 $isBuyerInEEC =
isInEEC($buyer);
772 $isSellerInEEC =
isInEEC($seller);
779 $suggestedaccountingaccountbydefaultfor =
'';
780 if ($factureDet->product_type == 1) {
781 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) {
783 $suggestedaccountingaccountbydefaultfor =
'';
785 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
787 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
789 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
790 } elseif ($isSellerInEEC && $isBuyerInEEC) {
792 $suggestedaccountingaccountbydefaultfor =
'eec';
794 $code_l =
getDolGlobalString(
'ACCOUNTING_SERVICE_' . $const_name .
'_EXPORT_ACCOUNT');
795 $suggestedaccountingaccountbydefaultfor =
'export';
798 } elseif ($factureDet->product_type == 0) {
799 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) {
801 $suggestedaccountingaccountbydefaultfor =
'';
803 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
805 $suggestedaccountingaccountbydefaultfor =
'eecwithvat';
806 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
808 $suggestedaccountingaccountbydefaultfor =
'eecwithoutvatnumber';
809 } elseif ($isSellerInEEC && $isBuyerInEEC) {
811 $suggestedaccountingaccountbydefaultfor =
'eec';
813 $code_l =
getDolGlobalString(
'ACCOUNTING_PRODUCT_' . $const_name .
'_EXPORT_ACCOUNT');
814 $suggestedaccountingaccountbydefaultfor =
'export';
823 $suggestedaccountingaccountfor =
'';
824 if ((($buyer->country_code == $seller->country_code) || empty($buyer->country_code))) {
826 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
827 $code_p = $product->accountancy_code_sell;
828 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
829 $code_p = $product->accountancy_code_buy;
831 $suggestedid = $accountingAccount[
'dom'];
832 $suggestedaccountingaccountfor =
'prodserv';
834 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
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 =
'eecwithvat';
843 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
845 if ($type ==
'customer' && !empty($product->accountancy_code_sell)) {
846 $code_p = $product->accountancy_code_sell;
847 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy)) {
848 $code_p = $product->accountancy_code_buy;
850 $suggestedid = $accountingAccount[
'dom'];
851 $suggestedaccountingaccountfor =
'eecwithoutvatnumber';
852 } elseif ($isSellerInEEC && $isBuyerInEEC && (($type ==
'customer' && !empty($product->accountancy_code_sell_intra)) || ($type ==
'supplier' && !empty($product->accountancy_code_buy_intra)))) {
854 if ($type ==
'customer' && !empty($product->accountancy_code_sell_intra)) {
855 $code_p = $product->accountancy_code_sell_intra;
856 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_intra)) {
857 $code_p = $product->accountancy_code_buy_intra;
859 $suggestedid = $accountingAccount[
'intra'];
860 $suggestedaccountingaccountfor =
'eec';
863 if ($type ==
'customer' && !empty($product->accountancy_code_sell_export)) {
864 $code_p = $product->accountancy_code_sell_export;
865 } elseif ($type ==
'supplier' && !empty($product->accountancy_code_buy_export)) {
866 $code_p = $product->accountancy_code_buy_export;
868 $suggestedid = $accountingAccount[
'export'];
869 $suggestedaccountingaccountfor =
'export';
875 if ($type ==
'customer' && !empty($buyer->code_compta_product)) {
876 $code_t = $buyer->code_compta_product;
877 $suggestedid = $accountingAccount[
'thirdparty'];
878 $suggestedaccountingaccountfor =
'thirdparty';
879 } elseif ($type ==
'supplier' && !empty($seller->code_compta_product)) {
880 $code_t = $seller->code_compta_product;
881 $suggestedid = $accountingAccount[
'thirdparty'];
882 $suggestedaccountingaccountfor =
'thirdparty';
887 $account_deposit =
getDolGlobalString(
'ACCOUNTING_ACCOUNT_' . strtoupper($type) .
'_DEPOSIT');
888 if (!empty($account_deposit) && $account_deposit !=
'-1') {
889 if ($factureDet->desc ==
"(DEPOSIT)" || $facture->type == $facture::TYPE_DEPOSIT) {
890 $accountdeposittoventilated =
new self($this->db);
891 if ($type ==
'customer') {
892 $result = $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
893 } elseif ($type ==
'supplier') {
894 $result = $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
896 if (isset($result) && $result < 0) {
900 $code_l = (
string) $accountdeposittoventilated->ref;
903 $suggestedid = (int) $accountdeposittoventilated->rowid;
904 $suggestedaccountingaccountfor =
'deposit';
908 if (!empty($facture->fk_facture_source)) {
909 $invoiceSource =
new $facture($this->db);
910 $invoiceSource->fetch($facture->fk_facture_source);
912 if ($facture->type == $facture::TYPE_CREDIT_NOTE && $invoiceSource->type == $facture::TYPE_DEPOSIT) {
913 $accountdeposittoventilated =
new self($this->db);
914 if ($type ==
'customer') {
915 $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
916 } elseif ($type ==
'supplier') {
917 $accountdeposittoventilated->fetch(0,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
919 $code_l = (
string) $accountdeposittoventilated->ref;
922 $suggestedid = (int) $accountdeposittoventilated->rowid;
923 $suggestedaccountingaccountfor =
'deposit';
929 if (empty($suggestedid) && empty($code_p) && !empty($code_l) && !
getDolGlobalString(
'ACCOUNTANCY_DO_NOT_AUTOFILL_ACCOUNT_WITH_GENERIC')) {
930 if (empty($this->accountingaccount_codetotid_cache[$code_l])) {
931 $tmpaccount =
new self($this->db);
932 $result = $tmpaccount->fetch(0, $code_l, 1);
936 if ($tmpaccount->id > 0) {
937 $suggestedid = $tmpaccount->id;
939 $this->accountingaccount_codetotid_cache[$code_l] = $tmpaccount->id;
941 $suggestedid = $this->accountingaccount_codetotid_cache[$code_l];
945 'suggestedaccountingaccountbydefaultfor' => $suggestedaccountingaccountbydefaultfor,
946 'suggestedaccountingaccountfor' => $suggestedaccountingaccountfor,
947 'suggestedid' => $suggestedid,
957 $parameters[
'return'] = $return;
958 $reshookafter = $hookmanager->executeHooks(
'afterAccountancyBindingCalculation', $parameters);
959 if ($reshookafter < 0) {
960 $this->error = $hookmanager->error;
961 $this->errors = $hookmanager->errors;
964 if (array_key_exists(
'suggestedaccountingaccountbydefaultfor', $hookmanager->resArray)) {
965 $return[
'suggestedaccountingaccountbydefaultfor'] = $hookmanager->resArray[
'suggestedaccountingaccountbydefaultfor'];
967 if (array_key_exists(
'suggestedaccountingaccountfor', $hookmanager->resArray)) {
968 $return[
'suggestedaccountingaccountfor'] = $hookmanager->resArray[
'suggestedaccountingaccountfor'];
970 if (array_key_exists(
'suggestedid', $hookmanager->resArray)) {
971 $return[
'suggestedid'] = $hookmanager->resArray[
'suggestedid'];
973 if (array_key_exists(
'code_l', $hookmanager->resArray)) {
974 $return[
'code_l'] = $hookmanager->resArray[
'code_l'];
976 if (array_key_exists(
'code_p', $hookmanager->resArray)) {
977 $return[
'code_p'] = $hookmanager->resArray[
'code_p'];
979 if (array_key_exists(
'code_t', $hookmanager->resArray)) {
980 $return[
'code_t'] = $hookmanager->resArray[
'code_t'];
985 if (is_array($hookmanager->resArray) && !empty($hookmanager->resArray)) {
986 return $hookmanager->resArray;